Skip to content

plugins: Reconnect Linux capture after PipeWire restarts - #109

Merged
zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:pipewire-reconnect
Oct 10, 2026
Merged

zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:pipewire-reconnect

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 10, 2026 •

Copy link
Copy Markdown
Collaborator

Description

When PipeWire restarts (for example when the user restarts audio), OBS's PipeWire screen capture and its PulseAudio-protocol audio sources stay dead until OBS is restarted. With this, a recording carries on: the screen holds its last frame, then screen and audio come back by themselves.

  1. test/cmocka: Add RED Linux capture reconnect tests: moves the reconnect decisions into linux-pipewire/reconnect-policy.c and linux-pulseaudio/pulse-reconnect.c, with stubs that keep today's behaviour, and adds test_pipewire_reconnect and test_pulse_reconnect. 9 of their 13 tests fail on this commit; each fix below turns its own tests green.
  2. linux-pipewire: Report lost PipeWire connections: obs_pipewire gets a callback for a lost connection (core -EPIPE) or a stream that fails after it was streaming. Errors before a stream ever streamed would only repeat, so they are left alone as before.
  3. linux-pipewire: Reconnect screen capture:
  4. linux-pulseaudio: Reconnect to the server: replace the failed context with a PA_CONTEXT_NOFAIL one and notify sources when it is ready, resolving the old @todo. Only a connection that worked is retried (no server at startup fails as before), and a server that keeps refusing is retried with backoff.
  5. linux-pulseaudio: Handle a server with no default device: strlen(NULL) on the default sink name, hit right after the server restarts.
  6. linux-pulseaudio: Restart streams after reconnect: each source recreates its stream, retrying every 100 ms for up to 10 s until its device reappears.

Motivation and Context

Fixes #108

Restarting audio is a normal fix for a stuck Bluetooth headset or a wrong output, and it shouldn't silently break a running recording or stream.

How Has This Been Tested?

Tested by hand on Arch Linux, Hyprland, PipeWire 1.6.8 with pipewire-pulse, xdg-desktop-portal-hyprland 1.4.1. I recorded 1080p60 while a moving 60 fps test pattern was on screen, ran systemctl --user restart pipewire mid-recording, and measured the recording with ffmpeg freezedetect and silencedetect:

before after
Screen frozen until OBS restart 35 frames (0.58 s) frozen, then live; no picker
Audio silent until OBS restart 0.64 s silent, then live

Most of the 0.6 s is WirePlumber coming back and linking the stream; the portal round trips take about 0.1 s. The portal's own buffer bug (fixed in xdph master by hyprwm/xdg-desktop-portal-hyprland#424 and #425) sometimes fails the new stream a second later; OBS reconnects again in under 0.1 s.

Automated (cmocka, no daemon, portal or server needed; built only where the plugins are):

Test Checks First green at
test_pipewire_reconnect core -EPIPE is a lost connection, reported once; other core errors are not; a stream error after streaming is, before streaming is not; screencast backoff 500 ms → 10 s, giving up after 10 attempts fixes 1 and 2
test_pulse_reconnect no reconnect if the server was never ready; immediate first retry, then backoff to 10 s; sources notified once on reconnect; no default source/sink returns NULL instead of crashing (segfault on the RED commit); stream restart retry limit fixes 3, 4 and 5

I built every commit and ran both tests at each step: 4 pass on the RED commit (the ones that check errors are not treated as lost), then 7, 8, 11, 12 and 13 after each fix.

The manual measurement below was taken before the decisions were moved into their own files; that move does not change behaviour.

clang-format 22.1.3 and gersemi (./lint's C lanes) pass, and each commit builds on its own without warnings. I couldn't run the Docker Tier 2 lane locally (no Docker access here); the tests were built and run against Arch's PipeWire 1.6.8 and libpulse headers.

Not tested:

  • KDE and GNOME portals.
  • Window capture after the window closed: the token is rejected, so the portal shows its picker, which is not timed out.
  • Source removed mid-reconnect: by design, pending timers become no-ops through weak references, and late portal replies are dropped by the cancelled cancellable.
  • Frontends without a UI task handler (headless libobs users): audio streams are restarted through OBS_TASK_UI, so they are not restarted there; libobs logs "UI task could not be queued" for each source.
  • JACK, ALSA and the camera portal source (not touched).

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

@zackees

zackees commented Oct 10, 2026

Copy link
Copy Markdown
Member

Thanks, this is a real problem and the manual measurements are convincing. I approved CI so the plugins build on every platform.

Before this can merge, the project needs a RED→GREEN automated test for each fix. You offered to factor out a testable piece. Please do, for example:

  • PipeWire: the classification that decides when to report a lost connection (core -EPIPE, or a stream that fails after it has streamed, versus errors before the first stream).
  • PulseAudio: the reconnect state/backoff logic and the "no default device" handling, driven by a fake context state.

A cmocka test in test/cmocka/ that compiles the factored C piece directly (like test_hotkey_load/test_rtmp_error) works. Push it as a first commit that fails against the current code, then the fix. Avoid fail_msg(), because Windows cmocka doesn't export cm_print_error.

@rdoupe
rdoupe force-pushed the pipewire-reconnect branch from cc49e76 to b3ba41e Compare October 10, 2026 20:35
@rdoupe

rdoupe commented Oct 10, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for approving CI. I've added the tests:

  • The first commit, test/cmocka: Add RED Linux capture reconnect tests, moves the decisions into plugins/linux-pipewire/reconnect-policy.c and plugins/linux-pulseaudio/pulse-reconnect.c and adds test_pipewire_reconnect and test_pulse_reconnect. They compile the factored C directly, like test_rtmp_error. The functions start as stubs that keep today's behaviour, so 9 of the 13 tests fail on that commit. The no-default-device test segfaults there, the same strlen(NULL) crash OBS hits.
  • Each fix commit then implements its piece and turns its own tests green: 4 → 7 → 8 → 11 → 12 → 13 passing.
  • The tests cover the PipeWire classification (-EPIPE on the core, errors after vs. before the first stream, reported once), the screencast backoff and give-up, the PulseAudio reconnect state and backoff driven by fake context states, the no-default-device case, and the stream-restart retry limit.
  • They use no fail_msg(), and are registered only when OBS::pipewire/OBS::pulseaudio exist, so macOS and Windows skip them.

I also rebased onto current master. The PR description has the details.

@rdoupe

rdoupe commented Oct 10, 2026

Copy link
Copy Markdown
Collaborator Author

The push with the tests needs CI approval again (first-time contributor), when you have a moment.

@zackees

zackees commented Oct 10, 2026

Copy link
Copy Markdown
Member

Approved CI for b3ba41e (run 38084351283). It's running now.

I checked the tests locally. I built test_pipewire_reconnect and test_pulse_reconnect at each commit in the Tier 2 image with -Wall -Wextra -Werror:

  • RED a6aaa12: 9 of 13 fail (4 PipeWire, 5 PulseAudio). cmocka catches no_default_device as a segfault.
  • Then 4 → 7 → 8 → 11 → 12 → 13 pass across the fix commits. Head is fully green.
  • clang-format 22.1.3 and gersemi 0.25.0 report nothing on the changed files.

Looks right:

  • No existing C tests or public headers changed. pipewire.h and pulse-wrapper.h are plugin-private.
  • Registration in test/cmocka/CMakeLists.txt:410-441 is guarded by TARGET OBS::pipewire/OBS::pulseaudio and added to cmocka-tests. PipeWire::PipeWire/PulseAudio::PulseAudio resolve there because CMAKE_FIND_PACKAGE_TARGETS_GLOBAL is on.
  • The extraction doesn't change behaviour. The RED stubs aren't called by the plugins yet. pulse_default_device() matches the old inline code in pulse_server_info(), except that it now handles NULL.
  • The tests cover both sides of each decision: errors that must not count (non--EPIPE, non-core id, errors before the first stream, no server ever) as well as the backoff values and the give-up limits.

Must change before merge:

  • Rebase. test/cmocka/CMakeLists.txt conflicts with master. test_flv_mux landed after test_rtmp_error, at the spot where you append. Keep both blocks. CI is running against the merge ref from 20:35, so it won't show the conflict.

Not blocking, but worth fixing while you rebase:

  • pipewire.c:1188: obs_pipewire_is_disconnected() reads obs_pw->disconnected on the main loop. The PipeWire thread writes it at pipewire.c:1082 and :1107, and the reads at screencast-portal.c:219 and :729 take no lock. Use pw_thread_loop_lock/unlock around the read, or an os_atomic bool.
  • The screencast reconnect lifecycle in screencast-portal.c (held stream, timers, cancellable swap) is only covered by the backoff test plus your manual runs. That's acceptable given the portal. Put the manual test steps from the description into the final commit message so they stay with the code.

Left before merge: the rebase, then green CI on every platform. This touches plugins/, so it runs the full builds. After that I'll do a last pass on the streams_mutex/graphics-lock ordering in screencast-portal.c.

@rdoupe

rdoupe commented Oct 10, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for the thorough check. Pushed 29e0dce:

  • Rebased onto master. test/cmocka/CMakeLists.txt keeps the test_flv_mux block, with ours after it.
  • obs_pipewire_is_disconnected() now reads the flag under pw_thread_loop_lock. The writes already happen under that lock, since they run in PipeWire thread-loop callbacks. This is folded into linux-pipewire: Report lost PipeWire connections.
  • The hand-test steps are in the final commit message.

Each commit still builds without warnings, and the tests go 4 → 7 → 8 → 11 → 12 → 13. clang-format and gersemi are clean. This push will need CI approval again.

@rdoupe
rdoupe force-pushed the pipewire-reconnect branch from b3ba41e to 29e0dce Compare October 10, 2026 21:14
@zackees

zackees commented Oct 10, 2026

Copy link
Copy Markdown
Member

Thanks for adding the tests. Verified locally in Tier 2 (Docker, Linux):

  • RED at f13f34c (tests only): test_pipewire_reconnect fails 4/4 (core_epipe_is_lost_once, stream_error_after_streaming_is_lost, lost_connection_is_reported_once, reconnect_backs_off_then_gives_up), and test_pulse_reconnect fails too.
  • GREEN at head: both pass, and all 42 cmocka tests pass with the Rust port OFF and ON.

The local gate passes; I pushed the stamped head with --force-with-lease (same tree as your 29e0dce). I approved CI and will merge once it's green.

@zackees
zackees force-pushed the pipewire-reconnect branch from 29e0dce to 1a1166b Compare October 10, 2026 21:36
@rdoupe

rdoupe commented Oct 10, 2026

Copy link
Copy Markdown
Collaborator Author
image

rdoupe and others added 6 commits October 10, 2026 14:47
Move the decisions behind recovering from a PipeWire restart into
small files without PipeWire, portal or mainloop calls, and test them
directly:

- linux-pipewire/reconnect-policy.c: whether a core error or a stream
  state means the connection is lost, and the screencast retry backoff.
- linux-pulseaudio/pulse-reconnect.c: when a failed context is
  replaced and with what backoff, when sources must restart their
  streams, how long they retry, and the default device while the
  server has none.

The functions are stubs that keep today's behaviour (nothing is
reported, retried or restarted, and a server without a default sink
dereferences NULL), so 9 of the 13 tests fail. The fixes that follow
make them pass. Refs obs-rust#108.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The core error listener only logged when the connection to the PipeWire
daemon broke, so its owner kept a dead connection and stream forever.

Add a callback that fires once, on the PipeWire thread, when the core
reports -EPIPE or a stream that was streaming goes into the error
state, plus helpers to check for that and whether a stream has received
a frame yet. Errors before a stream ever streamed (no matching format,
no target node) would only fail again, so they are not reported.

The decisions are obs_pipewire_core_error_is_lost() and
obs_pipewire_stream_state_is_lost(); their tests in
test_pipewire_reconnect now pass.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
When PipeWire restarts, the screen capture froze until the source was
recreated, and the only way to do that by hand, Reload, also threw away
the restore token, so the picker came back.

On a lost connection, keep the dead stream to draw its last frame, and
run the portal session again with the saved restore token. Swap to the
new stream on its first frame. Failed attempts back off, and after 10
it gives up and logs a hint. An attempt that gets no session within
3 s is retried, but nothing from SelectSources on is timed out, because
the portal may show the picker if the token no longer matches.
Cancelling the picker ends the reconnect. Each attempt gets a new
cancellable, so a late portal reply cannot act on a session that is
gone.

Stream pointers are swapped under the graphics lock and a new streams
mutex. Sizes are queried under the mutex only, since callers may hold
scene locks that the graphics thread takes after the graphics lock.

The backoff is obs_pipewire_reconnect_next_delay(); its test in
test_pipewire_reconnect now passes.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The context state callback was a noop with a todo to reconnect. After
the server restarted, the shared context stayed failed and every audio
source, including newly added ones, stayed silent until OBS restarted.

Replace a failed context with a PA_CONTEXT_NOFAIL one that waits for the
server to come back, and notify registered callbacks once it is ready.
Only a connection that was ready once is retried, so a host without a
server fails as before. The first retry is immediate; a server that
keeps refusing is retried with backoff up to 10 s. While reconnecting,
calls that need the server fail right away instead of blocking. Add
pulse_call_later() for retries.

The decisions are pulse_reconnect_on_failed() and
pulse_reconnect_on_ready(); their tests in test_pulse_reconnect now
pass.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
pulse_server_info() called strlen() on the default sink name, which is
NULL when the server has no default device, e.g. right after it
restarted. Move the lookup into pulse_default_device(), which returns
NULL then, and keep the current device and log at debug level instead.
Its test in test_pulse_reconnect now passes.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Recreate each source's stream when the server connection is back,
retrying every 100 ms for up to 10 s until its device reappears, and
skip the 500 ms startup delay since audio was already running.

The retry limit is pulse_restart_should_retry(); its test in
test_pulse_reconnect now passes.

Tested by hand on Arch Linux, Hyprland, PipeWire 1.6.8 with
pipewire-pulse and xdg-desktop-portal-hyprland 1.4.1. The screencast's
reconnect lifecycle is otherwise only covered by the backoff test,
since it needs a live daemon and portal.

1. Add a Screen Capture (PipeWire) source with a saved restore token
   and the default audio sources, and record 1080p60 while a moving
   60 fps test pattern is on screen.
2. Mid-recording, run `systemctl --user restart pipewire`.
3. Check the recording with ffmpeg `freezedetect` and `silencedetect`:
   the screen was frozen for 35 frames (0.58 s) and audio silent for
   0.64 s, then both were live again, with no picker. Before this
   series both stayed dead until OBS restarted.
4. The portal's own buffer bug (fixed in xdph master) sometimes fails
   the new stream a second later; OBS reconnected again in under
   0.1 s.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Local-Gate: v1 tree=629867f49909fa5ff1015989940fbb84f02fdaa8 secs=300 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791669170,"gate":"rust/all/fmt","host":"linux-x86_64","key":"5a6fe2b880da4d98430ff03ba3f08ce84f2f82184b5c21e8ef225820f4300cdb","lane":"lint","parents":["669f624acc8b5af933115d2f884dba0b71a7947e"],"secs":134,"stamp":"4c42fbfd9193df566ba46423cab8f4e8","tree":"629867f49909fa5ff1015989940fbb84f02fdaa8","v":1,"via":"run"}
Ci-Attestation: {"at":1791669170,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"5a6fe2b880da4d98430ff03ba3f08ce84f2f82184b5c21e8ef225820f4300cdb","lane":"lint","parents":["669f624acc8b5af933115d2f884dba0b71a7947e"],"secs":134,"stamp":"4a45bf2926c921acec724940d619330b","tree":"629867f49909fa5ff1015989940fbb84f02fdaa8","v":1,"via":"run"}
Ci-Attestation: {"at":1791669170,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"dc69b4ef458ac0463a6cb75a5b73a3b8d84f592bf1df19c0f86b1339726d2b7c","lane":"rust-test","parents":["669f624acc8b5af933115d2f884dba0b71a7947e"],"secs":60,"stamp":"814b14a8cce95f9ad53a2b57d0ce72b7","tree":"629867f49909fa5ff1015989940fbb84f02fdaa8","v":1,"via":"run"}
Ci-Attestation: {"at":1791669170,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"4a62cde6e9a4cb5268830caf2c01e04f95815d1a7c6384041e330e9daa9f0798","lane":"tier2-linux","parents":["669f624acc8b5af933115d2f884dba0b71a7947e"],"secs":105,"stamp":"1b98a7491a26056229d6c4fdc69fcfd7","tree":"629867f49909fa5ff1015989940fbb84f02fdaa8","v":1,"via":"run"}
@zackees
zackees force-pushed the pipewire-reconnect branch from 1a1166b to 06e60d7 Compare October 10, 2026 21:52
@rdoupe

rdoupe commented Oct 10, 2026 •

Copy link
Copy Markdown
Collaborator Author

Master moved again (test_cube_lut), and I see you already rebased it as 06e60d7. My local rebase came out to the same tree, so I've left yours as it is. All 44 cmocka tests pass there.

zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 12, 2026
An Audio Input Capture stays silent for good once its own record
stream dies while the connection to the server stays up: the server
kills the stream, or the device goes away and comes back under the
same name (switching to another virtual console and back, replugging
a USB microphone). The restart from obs-rust#109 only runs
when the whole server connection comes back.

Watch the stream and the server's devices:

  - a stream state callback queues a restart when the stream fails
    while the context is still ready (with the context lost,
    pulse_reconnected already restarts it)
  - the wrapper subscribes to source events on every ready context and
    passes new and removed sources to the inputs
  - a removed device stops the stream recording from it (pipewire-pulse
    leaves a stream that may not move unlinked and silent; PulseAudio
    kills it), and a new device starts any input that has no running
    stream, which also starts a source whose device was missing when it
    was created

The decision lives in pulse_stream_on_event() (pulse-reconnect.c),
without context or stream calls. Each event leads to at most one start
attempt, so a device that is gone for good is not polled: its source
only looks again when some device appears. A stream on the default
device is left to the server, which moves it.

test_pulse_stream_restart: 3 of 4 failing -> 4/4 against a private
PipeWire 1.6.8, pipewire-pulse and WirePlumber 0.5.17 (10/10 repeated
runs). test_pulse_reconnect gains three cases for the decision, which
run without a sound server: 10/10.

Fixes obs-rust#377.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Local-Gate: v1 tree=b92cfda608c4102ab19ee759b83e3b204db9d58d secs=146 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791816870,"gate":"rust/all/fmt","host":"linux-x86_64","key":"43d368cdc63ffbee96f8f424fb25ff18bca826236b64358a15264b317ff71cb2","lane":"lint","parents":["71acac7f7ae8f22917dcb634078dba6979daa11a"],"secs":52,"stamp":"a6df21afe9b4561baff31ae98399fb4d","tree":"b92cfda608c4102ab19ee759b83e3b204db9d58d","v":1,"via":"run"}
Ci-Attestation: {"at":1791816870,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"43d368cdc63ffbee96f8f424fb25ff18bca826236b64358a15264b317ff71cb2","lane":"lint","parents":["71acac7f7ae8f22917dcb634078dba6979daa11a"],"secs":52,"stamp":"0781c02615b216aa99ce07842dd0d2f8","tree":"b92cfda608c4102ab19ee759b83e3b204db9d58d","v":1,"via":"run"}
Ci-Attestation: {"at":1791816870,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"711aaf4d1c9da2312829d055fca4b93e1998e322b173ed0296b9a59a2ce7a175","lane":"rust-test","parents":["71acac7f7ae8f22917dcb634078dba6979daa11a"],"secs":40,"stamp":"e34cb2cc9233bd8acca50ed5b20d7e47","tree":"b92cfda608c4102ab19ee759b83e3b204db9d58d","v":1,"via":"run"}
Ci-Attestation: {"at":1791816870,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"570801778dbea9a4b654ff807958d72ed42b6138b5a3f597049426543b6541e2","lane":"tier2-linux","parents":["71acac7f7ae8f22917dcb634078dba6979daa11a"],"secs":55,"stamp":"07ef830ec41989672618504fae491db0","tree":"b92cfda608c4102ab19ee759b83e3b204db9d58d","v":1,"via":"run"}
zackees pushed a commit that referenced this pull request Oct 12, 2026
…378)

* test/cmocka: Add failing test for a PulseAudio input whose stream dies

An Audio Input Capture on PulseAudio or PipeWire stays silent for good
once its own record stream dies while the connection to the server
stays up. Upstream, the reporter switched to another virtual console,
which takes the device away from the session; back on the desktop the
device returned under the same name, but OBS never recorded from it
again until the source's device was changed. Unplugging and replugging
a USB microphone, or the server killing the stream, end the same way.

pulse-input.c connects a chosen device with PA_STREAM_DONT_MOVE and
sets only a read callback: no stream state callback and no
subscription to source events. The restart added for a lost server
connection (pulse_reconnected) only runs when the whole context comes
back. On PipeWire 1.6.8 with pipewire-pulse and WirePlumber 0.5.17:

  - device removed and re-added under the same name: the stream stays
    READY but unlinked (source index 4294967295) and gets no data, with
    no state, moved or suspended callback; PulseAudio kills it instead
  - stream node destroyed: the stream goes FAILED ("Entity killed")
    and nothing restarts it
  - device missing when the source is created: the start fails once and
    is never retried, so a microphone plugged in later stays silent

test_pulse_stream_restart builds the linux-pulseaudio capture code into
the test and runs it against a private PipeWire, WirePlumber and
pipewire-pulse (test_pulse_monitor_device.sh: own runtime dir, config
and D-Bus, no hardware) with virtual sources it loads itself. libobs's
source calls are stubbed with --wrap, so no display is needed: each
source counts the frames handed to obs_source_output_audio, and UI
tasks run on a thread of their own. On master:

  replugged_source_resumes    no audio within 3 s after the replug
  killed_stream_restarts      no audio within 3 s after the kill
  late_source_starts          no audio within 3 s after plugging in
  deleted_source_stays_quiet  passes (control)

3 of 4 cases fail. The controls (a second microphone that is not
touched keeps its stream and its audio; a device deleted for good
causes no retry loop, no audio and no crash) pass. Exits 77 (skip)
without PipeWire, WirePlumber, pipewire-pulse or pactl.

Reported upstream in obsproject/obs-studio#10072 by foresto.

Refs #377.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* linux-pulseaudio: Restart an input whose stream dies while connected

An Audio Input Capture stays silent for good once its own record
stream dies while the connection to the server stays up: the server
kills the stream, or the device goes away and comes back under the
same name (switching to another virtual console and back, replugging
a USB microphone). The restart from #109 only runs
when the whole server connection comes back.

Watch the stream and the server's devices:

  - a stream state callback queues a restart when the stream fails
    while the context is still ready (with the context lost,
    pulse_reconnected already restarts it)
  - the wrapper subscribes to source events on every ready context and
    passes new and removed sources to the inputs
  - a removed device stops the stream recording from it (pipewire-pulse
    leaves a stream that may not move unlinked and silent; PulseAudio
    kills it), and a new device starts any input that has no running
    stream, which also starts a source whose device was missing when it
    was created

The decision lives in pulse_stream_on_event() (pulse-reconnect.c),
without context or stream calls. Each event leads to at most one start
attempt, so a device that is gone for good is not polled: its source
only looks again when some device appears. A stream on the default
device is left to the server, which moves it.

test_pulse_stream_restart: 3 of 4 failing -> 4/4 against a private
PipeWire 1.6.8, pipewire-pulse and WirePlumber 0.5.17 (10/10 repeated
runs). test_pulse_reconnect gains three cases for the decision, which
run without a sound server: 10/10.

Fixes #377.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Local-Gate: v1 tree=b92cfda608c4102ab19ee759b83e3b204db9d58d secs=146 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791816870,"gate":"rust/all/fmt","host":"linux-x86_64","key":"43d368cdc63ffbee96f8f424fb25ff18bca826236b64358a15264b317ff71cb2","lane":"lint","parents":["71acac7f7ae8f22917dcb634078dba6979daa11a"],"secs":52,"stamp":"a6df21afe9b4561baff31ae98399fb4d","tree":"b92cfda608c4102ab19ee759b83e3b204db9d58d","v":1,"via":"run"}
Ci-Attestation: {"at":1791816870,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"43d368cdc63ffbee96f8f424fb25ff18bca826236b64358a15264b317ff71cb2","lane":"lint","parents":["71acac7f7ae8f22917dcb634078dba6979daa11a"],"secs":52,"stamp":"0781c02615b216aa99ce07842dd0d2f8","tree":"b92cfda608c4102ab19ee759b83e3b204db9d58d","v":1,"via":"run"}
Ci-Attestation: {"at":1791816870,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"711aaf4d1c9da2312829d055fca4b93e1998e322b173ed0296b9a59a2ce7a175","lane":"rust-test","parents":["71acac7f7ae8f22917dcb634078dba6979daa11a"],"secs":40,"stamp":"e34cb2cc9233bd8acca50ed5b20d7e47","tree":"b92cfda608c4102ab19ee759b83e3b204db9d58d","v":1,"via":"run"}
Ci-Attestation: {"at":1791816870,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"570801778dbea9a4b654ff807958d72ed42b6138b5a3f597049426543b6541e2","lane":"tier2-linux","parents":["71acac7f7ae8f22917dcb634078dba6979daa11a"],"secs":55,"stamp":"07ef830ec41989672618504fae491db0","tree":"b92cfda608c4102ab19ee759b83e3b204db9d58d","v":1,"via":"run"}

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 12, 2026
When a monitor is switched off or unplugged, or the computer sleeps, the
compositor ends the screencast of that output and removes its PipeWire
node. OBS's stream only pauses, with no error, so the capture stayed
frozen until the source was selected again (upstream
obsproject/obs-studio#12063 and obsproject/obs-studio#12423).

- pipewire.c: owners that set a disconnected callback (the screencast
  portal source) also watch the registry for the removal of the node
  their stream is connected to. obs_pipewire_node_removed_is_lost()
  reports it once, like a lost connection, so the reconnection from obs-rust#109
  starts a new portal session with the restore token.
- reconnect-policy.c: reconnection never gives up. After the first 10
  attempts (the same fast backoff as before, about a minute) it retries
  every 30 s for as long as the source exists: a monitor can be off for
  hours. Cancelling the portal's dialog still ends it.
- screencast-portal.c: after the 10th failed attempt, one warning says
  that retries continue; later attempts are logged at debug level.

A paused stream alone is not treated as lost: hiding the source pauses
it too, and a monitor in DPMS sleep may just stop sending frames.

test_pipewire_reconnect: 10 of 10 pass. test_pipewire_screencast_resume:
3 of 3 pass; the capture shows the monitor again about 3.6 s after it
went away (2.5 s off, then the next retry).

Fixes obs-rust#398.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Local-Gate: v1 tree=2f8ee712fbb967927e2a4d0b3bcde2de99d390e0 secs=183 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791827929,"gate":"rust/all/fmt","host":"linux-x86_64","key":"016db496eb8d8359b45087af682aca9b4c8d21729b2f1a0b9e6cbe4dca41bbf0","lane":"lint","parents":["efa075b66ee59d563fea3ae867ac9fb6525094a3"],"secs":66,"stamp":"e0f625dd56b7ddd199149220a3ccb604","tree":"2f8ee712fbb967927e2a4d0b3bcde2de99d390e0","v":1,"via":"run"}
Ci-Attestation: {"at":1791827929,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"016db496eb8d8359b45087af682aca9b4c8d21729b2f1a0b9e6cbe4dca41bbf0","lane":"lint","parents":["efa075b66ee59d563fea3ae867ac9fb6525094a3"],"secs":66,"stamp":"bb406dbf4a7dbb5ac0016261a1a1bfa3","tree":"2f8ee712fbb967927e2a4d0b3bcde2de99d390e0","v":1,"via":"run"}
Ci-Attestation: {"at":1791827929,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"12e029fa9a0f918b577703b1a314265afcbe6bd0077ba57a4b60b8e030da2f9d","lane":"rust-test","parents":["efa075b66ee59d563fea3ae867ac9fb6525094a3"],"secs":51,"stamp":"c03f965bc23e8582da086528bef285f6","tree":"2f8ee712fbb967927e2a4d0b3bcde2de99d390e0","v":1,"via":"run"}
Ci-Attestation: {"at":1791827929,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"88688525bfaba3ca64113acbfdec9176fe32f1fcbd00081af6e6a85b7219797f","lane":"tier2-linux","parents":["efa075b66ee59d563fea3ae867ac9fb6525094a3"],"secs":66,"stamp":"99680d5239648b5ebb4b2815a9007eab","tree":"2f8ee712fbb967927e2a4d0b3bcde2de99d390e0","v":1,"via":"run"}
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 16, 2026
…s-rust#1111)

* test/cmocka: Screen captures that are not showing get frames

test_pipewire_screencast_resume gains three cases. Its fake compositor now
links every capture stream to the monitor (each once, as before), and the
test counts the frames each stream takes (pw_stream_dequeue_buffer,
wrapped at link time).

With a capture showing, a second one in a scene that is not live and a
third disabled in a live scene, all three streams get the monitor's frames.
Their streams connect active, and only a hide after a show pauses them, so
the compositor copies every frame for every screen capture
(obsproject/obs-studio#13836).

RED: sources_not_showing_keep_their_streams_inactive fails, "frames in 2 s:
showing capture 60, in another scene 60, disabled 60" (0 expected for the
two that are not showing). Guards that pass today:
showing_a_source_starts_its_stream (20 rounds of show, a new frame, hide,
no more frames) and sources_not_showing_stay_inactive_after_reconnect
(obs-rust#109: after the monitor comes back, only the showing capture streams).
The four existing cases still pass.

Upstream: obsproject/obs-studio#13836.

Refs obs-rust#1110.

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

* linux-pipewire: Keep the streams of hidden screen captures paused

Every Screen Capture (PipeWire) stream connected active. libobs calls
.hide only when a showing source stops showing, so a source that was
disabled or in a scene that is not live when its portal session finished
kept streaming, and the compositor copied every frame for it. With
several captures this lags the desktop and OBS
(obsproject/obs-studio#13836). Our reconnect (obs-rust#109) already paused a
replacement stream for a hidden source; the first connect did not.

The capture now remembers whether libobs last showed or hid it, and
connects its stream with PW_STREAM_FLAG_INACTIVE unless it is showing.
show() starts the stream as before. A show_mutex keeps that flag and the
stream's active state in step: show() and hide() run on the video thread,
a stream is connected or swapped out on the main loop. It is taken before
the graphics lock and streams_mutex; show() and hide() never hold either.

The camera source connects the same way but is left alone: nobody has
reported it, and pausing a never-shown camera would let the device stop,
so its first show would wait for the camera to start where today it is
already running.

GREEN for obs-rust#1110 (RED 2d6eb0d: a source in another scene and a disabled
one each got 60 frames in 2 s). Now 0 and 0, with 60 for the showing
capture. Shown 20 times, such a source got a new frame each time, and
hiding it stopped its frames. After a reconnect only the showing capture
streams.

Upstream: obsproject/obs-studio#13836.

Fixes obs-rust#1110.

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

---------

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.

Linux capture sources stay dead after PipeWire restarts

2 participants