Skip to content

win-dshow: drop the virtual camera sleep spin - #1638

Merged
zackees merged 2 commits into
obs-rust:masterfrom
MCPVOT:fix/1279-virtualcam-sleepto-spin
Oct 21, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
MCPVOT:fix/1279-virtualcam-sleepto-spin

Conversation

@MCPVOT

@MCPVOT MCPVOT commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #1279

What and why

sleepto_100ns slept ms - 1 and then spun on Sleep(0) until the deadline. It runs once per frame inside the process of whatever app uses the OBS Virtual Camera, so the spin is CPU burned in Zoom, Teams or a browser.

This drops the spin: the sleep already stops within a timer tick of the target, and the only caller waits on an absolute deadline (cur_time += obs_interval), so returning early does not drift the frame schedule, the next wait is simply longer. It is the trade the issue offered ("accept ~1 ms jitter and drop the spin").

RED -> GREEN

No test commit: no test target compiles plugins/win-dshow/virtualcam-module, and the cmocka suite runs on Linux only, where this file is _WIN32-only. Recorded under ## Decisions on #1279 rather than silently skipped.

The evidence is a measurement of the real file, called on an absolute 60 fps grid, 2000 frames per run, two rounds per arm, thread CPU from GetThreadTimes:

host timer arm CPU/frame delta p50 p99
1 ms (libobs sets it in-process) before 0.71 / 1.41 ms 0.000 ms 0.08-1.55 ms
1 ms after 0.023 / 0.031 ms -0.91 ms 0.29-2.15 ms
default 15.6 ms before 0.02-0.09 ms 5.3-6.6 ms late
default 15.6 ms after 0.02-0.03 ms 2.2-7.1 ms late

Every run took 33333 ms for 2000 frames in both arms, so there is no drift. Where the host has a fine timer this is the ~1 ms the issue measured; where it does not, Sleep already overshoots and the change is inside the noise.

Not run: a live consumer app (DirectShow camera consumer). The frame loop and the shared-memory handoff are unchanged; only the wait is.

Checklist

  • Base is master
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test, then RED, then GREEN commits - not applicable, see the Decisions note above
  • User-visible change: its line in docs/features.md is added
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): not applicable
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): not a port

Also updates the sleepto_100ns contract in sleepto.h: it returns true when the target had not already passed on entry, and does not promise the target was reached. Before, the spin guaranteed it; nothing documented that.

`sleepto_100ns` slept `ms - 1` and then spun on `Sleep(0)` until the
deadline. It runs once per frame inside the process of whatever app uses
the OBS Virtual Camera, so that spin is CPU burned in Zoom, Teams or a
browser (obs-rust#1279).

The sleep stops within a timer tick of the target, and the only caller
waits on an absolute deadline (`cur_time += obs_interval`), so returning
there does not drift the frame schedule: the next wait is longer.

Measured with the real file on an absolute 60 fps grid, 2000 frames,
two rounds per arm, thread CPU:

  host timer 1 ms (what libobs sets for itself):
    before  cpu 0.71-1.41 ms/frame, delta p50  0.00 ms, p99 0.08-1.55 ms
    after   cpu 0.02-0.03 ms/frame, delta p50 -0.91 ms, p99 0.29-2.15 ms
  host timer default (15.6 ms granularity):
    before  cpu 0.02-0.09 ms/frame, delta p50 5.3-6.6 ms late
    after   cpu 0.02-0.03 ms/frame, delta p50 2.2-7.1 ms late

Both arms keep 16.667 ms per frame over 2000 frames (33333 ms), so the
early return does not drift. Where the host has a fine timer the spin is
the ~1 ms the issue measured; where it does not, `Sleep` already
overshoots the deadline, the spin rarely runs, and the change is not
measurable.

No test: no test target compiles the virtual camera module, and the C
test suite runs on Linux only, where this file is Windows-only. The
measurements are the evidence, recorded on obs-rust#1279.
@zackees
zackees added this pull request to the merge queue Oct 21, 2026
Merged via the queue into obs-rust:master with commit d53832f Oct 21, 2026
22 checks passed
@vpacher vpacher mentioned this pull request Oct 21, 2026
5 tasks done
@MCPVOT
MCPVOT deleted the fix/1279-virtualcam-sleepto-spin branch October 22, 2026 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

win-dshow: virtual camera sleepto spins on Sleep(0) each frame inside the consuming app

2 participants