Skip to content

fix(linux-v4l2): frame-sync the Virtual Camera with its microphone (OBS Audio), drop stale FIFO audio (#4087) - #4093

Merged
zackees merged 7 commits into
masterfrom
fix/vmic-av-sync
Nov 9, 2026
Merged

zackees merged 7 commits into
masterfrom
fix/vmic-av-sync

Conversation

@zackees

@zackees zackees commented Nov 8, 2026

Copy link
Copy Markdown
Member

Fixes #4087

Problem

On Linux the virtual mic trailed the Virtual Camera picture by 732 ms: video arrived 143 ms after the clip started, audio 875 ms after. There were two causes:

  1. libobs passes raw_video frames to the output as they render, but passes raw_audio only after its audio buffering (490 ms in the user's session). The writer did not line the two up.
  2. While no app read the source, the 16 KiB FIFO stayed full of old audio. An app that linked later heard that backlog first and stayed that far behind for the whole session.

Change (plugins/linux-v4l2/v4l2-output.c.h)

  • Frame sync, on by default: while the mic runs, each frame waits in a bounded ring of 64 frames until the audio up to its timestamp has gone to the mic. When the ring is full, the oldest frame goes out, so the picture never freezes. Without a mic, nothing waits.
  • Stale audio: a read end of the FIFO throws away audio beyond one packet before each write (vmic_drop_stale).
  • Name: the mic's description is now "OBS Audio". The source name obs_virtual_microphone and the camera label "OBS Virtual Camera" stay the same. The OBS wiki, Debian and Snap docs, and users' modprobe configs select the camera by that label, so renaming it would break existing setups.
  • Measurement tool: ci/vcam_av_sync.py drives OBS over obs-websocket. It plays a clip where a white flash and a 1 kHz beep start on the same frame, records /dev/video9 and the mic in one ffmpeg process with wall-clock timestamps, and prints av_offset_ms.

Tests

  • RED 0ca0ecd: video_waits_for_its_audio, held_video_is_bounded and stale_audio_is_dropped fail against the stubs. video_is_not_held_without_microphone passes against both.
  • GREEN e58f976: the fix.
  • test_v4l2_vmic_server now expects the "OBS Audio" description.
  • The test_v4l2_vcam_audio pass count goes from 14 to 18.
  • Tiers 1, 3 and 4 do not apply: this changes a C plugin, not ported Rust.
  • Device measurement after the change will be posted in a comment.

…one audio (#4087)

video_waits_for_its_audio, held_video_is_bounded and stale_audio_is_dropped
fail against the stubs; ci/vcam_av_sync.py measures the A/V offset of a
running OBS over obs-websocket (baseline: audio 732 ms behind video).
…p stale FIFO audio, name it OBS Audio (#4087)

Frames wait (at most 64) until the audio with the same timestamp has gone
to the microphone; audio waiting unread in the FIFO beyond one packet is
thrown away, so an app that links later hears the newest audio. The
microphone's description becomes "OBS Audio"; the source name
obs_virtual_microphone and the camera's "OBS Virtual Camera" label stay,
since existing setups select them by name.
…'s 8192-frame buffer (#4087)

On the device, after frame sync audio still trailed by 162-176 ms: the
tunnel keeps target_buffer = 8192 frames (170.7 ms) and no argument
changes it. pipewire_tunnel_lead_is_8192_frames and
video_waits_for_the_tunnel fail against the stub.
…nel's buffer too (#4087)

Under pipewire-pulse (a source's driver column reads PipeWire), frames
wait an extra 8192 frames of audio, the tunnel's fixed target_buffer, so
they leave with the sound that comes out of the tunnel. The hold ring
grows to 128 frames for 60 fps with deep audio buffering.
@zackees

zackees commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

Device measurement with ci/vcam_av_sync.py measure. Each run drives OBS over obs-websocket and records /dev/video9 and the mic in one ffmpeg process.

build audio behind video audio latency video latency
1.0.1-beta.20261108 (d4a0770, before) 732 ms 875 ms 143 ms
e58f976 (frame sync + stale drain) 162 / 176 ms 355 / 337 ms 193 / 162 ms

The mic is listed as "OBS Audio".

The remaining ~170 ms comes from PipeWire's pipe tunnel, which keeps a fixed target_buffer = 8192 * frame_size (170.7 ms at 48 kHz) in module-pipe-tunnel.c. No module argument changes it: node.latency, reader PULSE_LATENCY_MSEC and fragment_size were tested on a separate probe source and all stayed at about 150 ms.

RED cb1a6d9 / GREEN 0bd1dc9 also hold frames for those 8192 frames when the server is pipewire-pulse. A device run on 0bd1dc9 will follow.

…ource list (#4087)

On the device the hold stayed off: vmic_pa_list(false) writes index and
name only, so vmic_tunnel_lead_ns never sees the PipeWire driver column
(video latency stayed 158-199 ms, offset 108-168 ms).
@zackees

zackees commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

Device result on dca7330 (AppImage from verify run 42543994284, plugin swapped into the running desktop OBS). The libpulse source list now carries the driver column, so the PipeWire tunnel hold applies.

run audio vs video audio latency video latency
1 -27 ms 344 ms 371 ms
2 -17.5 ms 338 ms 356 ms
3 -61 ms 244 ms 305 ms

Before this PR the audio was 732 ms late. The residual is under two frames, audio slightly early, which is inside the usual lip-sync tolerance.

@zackees
zackees marked this pull request as ready for review November 9, 2026 01:56
@zackees
zackees added this pull request to the merge queue Nov 9, 2026
Merged via the queue into master with commit 77107c4 Nov 9, 2026
93 of 95 checks passed
@zackees
zackees deleted the fix/vmic-av-sync branch November 9, 2026 02:35

This branch was previously deployed

1 inactive deployment
bouf — dca7330e Deployed Nov 9, 2026 by zackees via Build windows arm64 🧱 #161
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.

bug: linux-v4l2: OBS Virtual Microphone lags the Virtual Camera by ~0.7 s (no A/V sync, stale FIFO audio)

1 participant