Skip to content

linux-pulseaudio: Count packet timestamps in samples - #194

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/190-pulse-burst-timestamps
Oct 11, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/190-pulse-burst-timestamps

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 11, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Audio Input Capture (PulseAudio) on pipewire-pulse cuts out for ~75 ms at a time. The log shows Audio timestamp for '...' exceeded TS_SMOOTHING_THRESHOLD, diff=7xxxxxxx ns, and two such sources on one device sound choppy. The captured samples are all there; only their timestamps are wrong. With this change a packet's timestamp is counted on in samples from the previous packet, so batched delivery no longer reaches libobs as gaps and overlaps.

pulse-input stamped every packet as if it ended when the read callback ran. Record streams often arrive in bursts:

A burst then shares one timestamp. Once it spans libobs' 70 ms TS_SMOOTHING_THRESHOLD, libobs places one packet ~75 ms in the past (overwriting audio) and the next ~75 ms in the future (silence).

  1. test/cmocka: Add RED PulseAudio packet timing tests: moves the packet timestamp into linux-pulseaudio/pulse-timing.c (no mainloop or stream calls) with a stub that keeps today's formula, and adds test_pulse_timing. Each case replays packet arrivals and judges the timestamps by libobs' smoothing rule, after pulse-input's 500 ms startup skip. 4 of its 9 tests fail on this commit:

    The other 5 pin what a fix must keep: even delivery stays contiguous, no packet is ever stamped after it was read, 2 s of really lost audio moves the timeline to the clock, a device clock ±0.1% off over 10 minutes is followed, and a reset starts a new timeline.

  2. linux-pulseaudio: Count packet timestamps in samples: each packet starts where the previous one ended. The clock takes over only where counting can't be right: when a packet would end after it was read (fast device clock, or the timeline began on a late packet), or when it is more than 1 s late (audio was really lost). Once a second the timeline moves on by the least wait any packet of that second had, so a slow device clock doesn't build up lag; that step is far below the 70 ms window. All state lives in struct pulse_timing, one per stream, reset when the stream stops. All 9 tests pass.

Motivation and Context

Fixes #190

Why this one

We ranked upstream OBS's open issues by community activity over the last 12 months: human comments, distinct people, and reactions, with bots and off-topic comments excluded. obsproject/obs-studio#12835 ("Mic stutter/cutting for over 3 years of recording videos on linux/pipewire") is 4th overall and has the most distinct people of any issue (12). A maintainer replicated obsproject/obs-studio#10808, the same cause with two sources on one device. Neither has an upstream fix.

How Has This Been Tested?

Live, headless, against a private PipeWire 1.6.8 with pipewire-pulse: own runtime dir, no hardware, no D-Bus, a timer-driven virtual source fed with a sample-counter ramp. The real linux-pulseaudio.so from each commit ran in a headless libobs (private Xvfb, audio only), 15 s per run. Breaks are libobs' own exceeded TS_SMOOTHING_THRESHOLD log lines:

Scenario RED commit GREEN commit
one source, quantum 1024 0 0
two sources on one device, quantum 1024 (obsproject/obs-studio#10808) 0 + 70 (second source) 0 + 0
one source, quantum 4096 10 0
one source, quantum 8192 236 0

The ramp had no breaks in any run, so no samples were lost or reordered. With the fix the largest step error between packets dropped from 147 ms to under 17 ms.

For capture-thread stalls, an OBS-like libpulse client (25 ms fragsize, ADJUST_LATENCY, one peek per callback) logged every packet's arrival from the same private server. Those arrivals were replayed through each commit's pulse-timing.c and libobs' smoothing rule:

Scenario (15 s) RED GREEN GREEN max lag
quantum 1024 / 2048 0 / 0 0 / 0 21 / 42 ms
quantum 4096 10 0 83 ms
quantum 8192 239 of 580 0 166 ms
quantum 1024, 100 ms stall every 3 s 8 0 96 ms
quantum 1024, 150 ms stall every 3 s 9 0 146 ms

No packet was stamped after it was read. The lag is the time a packet waited in the burst, which the old timestamps hid by stamping it as new.

Automated (cmocka, no server needed; built where the PulseAudio plugin is):

Test Checks First green at
test_pulse_timing burst, server-cycle, stall and recorded-trace delivery stay seamless; even delivery contiguous; nothing stamped after it was read; real loss and ±0.1% clock drift follow the clock; reset fix 1 (5 of 9 pass at RED)

The full ctest suite passes at the GREEN commit (57/57); at the RED commit only test_pulse_timing fails (56/57). Both commits build without warnings in the touched files. clang-format 22.1.3, gersemi 0.25.0 and the ci/ self-tests (./lint's non-Rust lanes) pass. I couldn't run the Rust lanes (soldr is not installed here) or the Docker Tier 2 lane (no Docker access); this PR touches no Rust.

Hardware testing

Also run on real hardware: an i7-9700K desktop with an RTX 2070, kernel 7.2, PipeWire 1.6.8 with pipewire-pulse, its normal user session (nothing else playing or recording), capturing a USB headset microphone (s16le mono, 48 kHz). The same headless libobs harness loaded each commit's linux-pulseaudio.so, 30 s per run; breaks are libobs' exceeded TS_SMOOTHING_THRESHOLD log lines. The forced quanta were set with pw-metadata -n settings 0 clock.force-quantum and reset to 0 afterwards.

Scenario RED commit GREEN commit
one source, quantum 1024 0 0
two sources on one device, quantum 1024 (obsproject/obs-studio#10808) 0 + 323 (second source) 0 + 0
one source, quantum 4096 0 to 24 (three runs) 0
one source, quantum 8192 485 0

The largest step error between packets dropped from 124 ms (second source, quantum 1024) and 146 ms (quantum 8192) to under 5 ms. At quantum 4096 the old timestamps sit just under the 70 ms window (steps up to 61 ms), so whether libobs logs breaks depends on the run. With a forced quantum of 4096 or 8192, a second stream on the same device got no packets from pipewire-pulse at either commit; that is before the timestamp code and is not changed here. The full ctest suite passes at the GREEN commit (57/57) on this machine too, and the local gate (lint, rust-test, tier2-linux) passed and stamped the GREEN head (36ec30290).

Not tested:

  • PulseAudio itself (not pipewire-pulse): the timestamp logic doesn't depend on the server.
  • Audio holes reported by the server (pa_stream_peek with no data): the packet is still dropped as before, and the timeline catches up within a second, or resyncs if more than 1 s was lost.
  • After a reconnect, pulse-input outputs from the first packet with no startup skip, so a burst right at that point can still break smoothing once.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have read the contributing document.
  • My code has been run through clang-format.
  • My code follows the project's style guidelines
  • My code is not on the master branch.
  • My code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.
  • I have used AI tooling in the creation of this PR
  • I am an AI agent being used to assist with this pull request: Claude Code (Claude Opus 5.5) wrote the code, commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machine.

🤖 Generated with Claude Code

@rdoupe
rdoupe force-pushed the fix/190-pulse-burst-timestamps branch from 74e2fac to 36ec302 Compare October 11, 2026 04:28
@rdoupe
rdoupe marked this pull request as ready for review October 11, 2026 04:28
@rdoupe
rdoupe requested a review from zackees as a code owner October 11, 2026 04:28
rdoupe and others added 2 commits October 10, 2026 21:31
Microphones captured with the PulseAudio input on pipewire-pulse drop
out for ~75 ms at a time, logged as "Audio timestamp ... exceeded
TS_SMOOTHING_THRESHOLD, diff=7xxxxxxx ns" (obsproject/obs-studio#12835),
and two PulseAudio sources on one device sound choppy
(obsproject/obs-studio#10808).

pulse-input stamps every packet with its arrival time minus its own
length, but record streams arrive in bursts: pipewire-pulse hands over
all 25 ms fragments of a graph cycle at once when the quantum is longer,
a capture thread held up by load reads the fragments that queued up
back to back, and the second stream on OBS's one connection gets its
packets up to 150 ms at a time. A burst then shares one timestamp, and
once it spans libobs' 70 ms smoothing window a packet is placed ~75 ms
in the past (overwriting audio) and the next ~75 ms in the future
(silence).

Measured against a private, hardware-free PipeWire 1.6.8 with libobs'
smoothing replayed on the timestamps: 239 of 580 packets break at
quantum 8192 and 12 at 4096 (none at 1024 or 2048); at quantum 1024 a
100 ms capture-thread stall every 3 s breaks 8 times in 15 s; the
second of two streams on one context breaks about seven times a
second. Every packet is read alone, so the readable size cannot date
it.

Move the timestamp into linux-pulseaudio/pulse-timing.c, without
mainloop or stream calls, and test it with these burst patterns, a
recorded 3 s arrival trace of the second stream, real loss, device
clock drift and reset, judged by libobs' smoothing rule after
pulse-input's 500 ms startup skip. The function keeps today's formula,
so 4 of the 9 tests fail: burst_delivery_stays_contiguous,
server_cycle_bursts_are_seamless, capture_thread_stall_is_seamless and
recorded_second_stream_is_not_choppy. The others pin what a fix must
keep: no packet stamped after it was read, and real gaps and clock
drift still follow the system clock.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Stamping each packet with its arrival time gives all packets of a burst
one timestamp, and libobs then cuts ~75 ms holes and overlaps into the
audio (obsproject/obs-studio#12835, obsproject/obs-studio#10808).

The packets of a record stream follow on from each other in samples,
however the server batches them, so count each packet on from where the
previous one ended. Fall back to the clock only where counting cannot be
right:

- a packet would end after it was read: the device clock runs fast, or
  the timeline started on a packet that was already late;
- a packet is more than 1 s late against the timeline: audio was really
  lost.

A packet may also wait a while after its end on the timeline. Once a
second, move the timeline on by the least wait any packet of that second
had, so a slow device clock does not leave it ever further behind; that
step is far below libobs' 70 ms smoothing window. All state lives in
struct pulse_timing, one per stream.

This turns the 4 failing tests of test_pulse_timing green
(burst_delivery_stays_contiguous, server_cycle_bursts_are_seamless,
capture_thread_stall_is_seamless and
recorded_second_stream_is_not_choppy), and the 5 that pin real loss,
clock drift and reset keep passing.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Local-Gate: v1 tree=183262e0eb9b29ece23fd9105a4897ac2b02d2e7 secs=132 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791693212,"gate":"rust/all/fmt","host":"linux-x86_64","key":"cc5fee07f9f73a33dd0f25e00f3ab95bc51f29fec1b191516323743447b5d351","lane":"lint","parents":["1ac0561184bba031d2b1e50ea2cac17de2f8aad3"],"secs":52,"stamp":"e1630a0c74cb641ead843bffbcc4d14b","tree":"183262e0eb9b29ece23fd9105a4897ac2b02d2e7","v":1,"via":"run"}
Ci-Attestation: {"at":1791693212,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"cc5fee07f9f73a33dd0f25e00f3ab95bc51f29fec1b191516323743447b5d351","lane":"lint","parents":["1ac0561184bba031d2b1e50ea2cac17de2f8aad3"],"secs":52,"stamp":"b6b8fa9a730313c91641d0873934b6e5","tree":"183262e0eb9b29ece23fd9105a4897ac2b02d2e7","v":1,"via":"run"}
Ci-Attestation: {"at":1791693212,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"60c18784e70319e99bbbed48eb7d3628c8e634138d09038abf4f0dddf78fed94","lane":"rust-test","parents":["1ac0561184bba031d2b1e50ea2cac17de2f8aad3"],"secs":41,"stamp":"06f9d7668248fbf76a01cc3e64181f4f","tree":"183262e0eb9b29ece23fd9105a4897ac2b02d2e7","v":1,"via":"run"}
Ci-Attestation: {"at":1791693212,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"757c9f7973cc50b71e9c3d580f6c9cae01f6f4f13bfd6c9dd1d0e951f0e532ec","lane":"tier2-linux","parents":["1ac0561184bba031d2b1e50ea2cac17de2f8aad3"],"secs":39,"stamp":"7914df4ea4fac362989cb4fd3fb003b1","tree":"183262e0eb9b29ece23fd9105a4897ac2b02d2e7","v":1,"via":"run"}
@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

Verified locally, rebased onto master and stamped. The new head is 7bb1f335e.

  • RED (337a02d4b): Tier 2 passed 56 of 57. test_pulse_timing failed 4 of 9 (burst_delivery_stays_contiguous, capture_thread_stall_is_seamless, recorded_second_stream_is_not_choppy, server_cycle_bursts_are_seamless).
  • GREEN (36ec30290): Tier 2 passed 57 of 57 with ENABLE_RUST_LIBOBS OFF and 57 of 57 with it ON.
  • Local gate: lint, rust-test and tier2-linux all passed.

The resync rules look right: the timeline restarts from the clock if it would run ahead or falls more than 1 s behind, and it catches up by the smallest lag seen each second. Merging once CI is green.

@zackees
zackees force-pushed the fix/190-pulse-burst-timestamps branch from 36ec302 to 7bb1f33 Compare October 11, 2026 04:33
@zackees
zackees merged commit 46bdfb6 into obs-rust:master Oct 11, 2026
23 checks passed
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 13, 2026
With two Audio Input Capture sources on the same device and the
PipeWire graph quantum forced to 2048 or more (an app asking for a
large latency), the second source is mostly silent in recordings: 44 to
80 % of a 30 s recording on the release, with "exceeded
TS_SMOOTHING_THRESHOLD" lines. obs-rust#194 fixed how packets that come in
bursts are timed, but this was left.

The audio arrives late, not wrong: linux-pulseaudio creates every
source's record stream on its one shared connection, and pipewire-pulse
delivers the second record stream of a connection unevenly at such
quanta, nothing for seconds and then everything at once. OBS buffers at
most about a second of audio, so most of it comes too late. The same
two streams on two connections are delivered evenly (checked with a
small libpulse client and with parec).

test_pulse_two_sources runs the capture code against a private
PipeWire + WirePlumber + pipewire-pulse stack (test_pulse_monitor_device.sh),
with libobs's source calls stubbed like test_pulse_stream_restart. Two
captures record the monitor of a null sink:
- captures_use_separate_connections: the two record streams belong to
  different clients (pactl list source-outputs);
- second_capture_is_not_held_back: at quantum 2048, for 10 s, neither
  capture goes 500 ms without a packet.

RED: both fail. The captures share client 51; the second capture waits
2.1 to 4.4 s for packets (5 of 5 runs) while the first never waits more
than 47 ms.

Refs obs-rust#531.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zackees pushed a commit that referenced this pull request Oct 13, 2026
)

* test/cmocka: Add a failing test for two captures on one device

With two Audio Input Capture sources on the same device and the
PipeWire graph quantum forced to 2048 or more (an app asking for a
large latency), the second source is mostly silent in recordings: 44 to
80 % of a 30 s recording on the release, with "exceeded
TS_SMOOTHING_THRESHOLD" lines. #194 fixed how packets that come in
bursts are timed, but this was left.

The audio arrives late, not wrong: linux-pulseaudio creates every
source's record stream on its one shared connection, and pipewire-pulse
delivers the second record stream of a connection unevenly at such
quanta, nothing for seconds and then everything at once. OBS buffers at
most about a second of audio, so most of it comes too late. The same
two streams on two connections are delivered evenly (checked with a
small libpulse client and with parec).

test_pulse_two_sources runs the capture code against a private
PipeWire + WirePlumber + pipewire-pulse stack (test_pulse_monitor_device.sh),
with libobs's source calls stubbed like test_pulse_stream_restart. Two
captures record the monitor of a null sink:
- captures_use_separate_connections: the two record streams belong to
  different clients (pactl list source-outputs);
- second_capture_is_not_held_back: at quantum 2048, for 10 s, neither
  capture goes 500 ms without a packet.

RED: both fail. The captures share client 51; the second capture waits
2.1 to 4.4 s for packets (5 of 5 runs) while the first never waits more
than 47 ms.

Refs #531.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* linux-pulseaudio: Give each capture stream a connection of its own

Every Audio Input Capture source created its record stream on the
plugin's one shared PulseAudio connection. pipewire-pulse delivers the
second record stream of a connection unevenly when the graph quantum is
2048 or more: nothing for seconds at a time, then everything at once.
OBS buffers at most about a second of audio, so with two sources on one
device the second one was mostly silent in recordings.

pulse_stream_new() now connects a context of its own on the shared
mainloop for each stream, and the new pulse_stream_free() ends the
stream and that connection together. The wrapper keeps track of each
stream's context, because a failed stream no longer reports it. The
shared connection still serves device lists, server info, events and
reconnects.

GREEN: test_pulse_two_sources 2 of 2 (separate clients; the second
capture's longest wait 44 ms in 10 s, like the first). The other
PulseAudio tests pass, including test_pulse_stream_restart.

Fixes #531.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=2a677a94be708a4814021756d5dd1195513a7911 secs=359 lanes=lint:run,rust-test:run,cross:run,tier2-linux:run
Ci-Attestation: {"at":1791885703,"gate":"rust/all/fmt","host":"linux-x86_64","key":"03efbcd9393845fb1d417cb449fc9b1bfa8d188e36cfc35db9e56f46c2c8e263","lane":"lint","parents":["979995da81e03ac83f8d82d979ed767657c1ac0e"],"secs":31,"stamp":"7da3a7aad5abf93cea8b3cf372ee3bed","tree":"2a677a94be708a4814021756d5dd1195513a7911","v":1,"via":"run"}
Ci-Attestation: {"at":1791885703,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"03efbcd9393845fb1d417cb449fc9b1bfa8d188e36cfc35db9e56f46c2c8e263","lane":"lint","parents":["979995da81e03ac83f8d82d979ed767657c1ac0e"],"secs":31,"stamp":"88255a486176dd79ca4429640a884a13","tree":"2a677a94be708a4814021756d5dd1195513a7911","v":1,"via":"run"}
Ci-Attestation: {"at":1791885703,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"3ad68e9af81fa7637dd323dcf81ff16d52b2559ed355c942acf92d0766d0fcc6","lane":"rust-test","parents":["979995da81e03ac83f8d82d979ed767657c1ac0e"],"secs":123,"stamp":"073da15bfb03431401cf57ce754c2504","tree":"2a677a94be708a4814021756d5dd1195513a7911","v":1,"via":"run"}
Ci-Attestation: {"at":1791885703,"gate":"rust/x86_64-pc-windows-msvc/clippy","host":"linux-x86_64","key":"7387b9d78b1ca610414dabfb93c4efb4195c1d962c6d58cd5351f87d7b9dd142","lane":"cross","parents":["979995da81e03ac83f8d82d979ed767657c1ac0e"],"secs":105,"stamp":"d1dc08d75d065bad4933900719b7468f","tree":"2a677a94be708a4814021756d5dd1195513a7911","v":1,"via":"run"}
Ci-Attestation: {"at":1791885703,"gate":"rust/x86_64-pc-windows-msvc/dylint","host":"linux-x86_64","key":"7387b9d78b1ca610414dabfb93c4efb4195c1d962c6d58cd5351f87d7b9dd142","lane":"cross","parents":["979995da81e03ac83f8d82d979ed767657c1ac0e"],"secs":105,"stamp":"c74502b6c0249a0391857370ce67289c","tree":"2a677a94be708a4814021756d5dd1195513a7911","v":1,"via":"run"}
Ci-Attestation: {"at":1791885703,"gate":"rust/aarch64-apple-darwin/clippy","host":"linux-x86_64","key":"7387b9d78b1ca610414dabfb93c4efb4195c1d962c6d58cd5351f87d7b9dd142","lane":"cross","parents":["979995da81e03ac83f8d82d979ed767657c1ac0e"],"secs":105,"stamp":"2dc8efc2420944aeba15bc5445eeddba","tree":"2a677a94be708a4814021756d5dd1195513a7911","v":1,"via":"run"}
Ci-Attestation: {"at":1791885703,"gate":"rust/aarch64-apple-darwin/dylint","host":"linux-x86_64","key":"7387b9d78b1ca610414dabfb93c4efb4195c1d962c6d58cd5351f87d7b9dd142","lane":"cross","parents":["979995da81e03ac83f8d82d979ed767657c1ac0e"],"secs":105,"stamp":"66e314a5ed1f66051d93ba552ce1b2a8","tree":"2a677a94be708a4814021756d5dd1195513a7911","v":1,"via":"run"}
Ci-Attestation: {"at":1791885703,"gate":"rust/x86_64-apple-darwin/clippy","host":"linux-x86_64","key":"7387b9d78b1ca610414dabfb93c4efb4195c1d962c6d58cd5351f87d7b9dd142","lane":"cross","parents":["979995da81e03ac83f8d82d979ed767657c1ac0e"],"secs":105,"stamp":"ea9209739b430b4b810798ef608a2e5c","tree":"2a677a94be708a4814021756d5dd1195513a7911","v":1,"via":"run"}
Ci-Attestation: {"at":1791885703,"gate":"rust/x86_64-apple-darwin/dylint","host":"linux-x86_64","key":"7387b9d78b1ca610414dabfb93c4efb4195c1d962c6d58cd5351f87d7b9dd142","lane":"cross","parents":["979995da81e03ac83f8d82d979ed767657c1ac0e"],"secs":105,"stamp":"8afb438edc063052a161e6ee5fb51dfd","tree":"2a677a94be708a4814021756d5dd1195513a7911","v":1,"via":"run"}
Ci-Attestation: {"at":1791885703,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"57d9e6ba898a2b6eed47fc5bf3cbae9654ff4d0e40b6d943d1be34d9679d7feb","lane":"tier2-linux","parents":["979995da81e03ac83f8d82d979ed767657c1ac0e"],"secs":100,"stamp":"013062f3bf42ea24cb97723fb8a65141","tree":"2a677a94be708a4814021756d5dd1195513a7911","v":1,"via":"run"}

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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-pulseaudio: input stutters when packets arrive in bursts (upstream #12835, #10808)

2 participants