Skip to content

obs-ffmpeg: Reopen local media when restart finds a pending close - #732

Merged
zackees merged 2 commits into
obs-rust:masterfrom
chriscoveries:fix/media-restart-close
Oct 14, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
chriscoveries:fix/media-restart-close

Conversation

@chriscoveries

Copy link
Copy Markdown
Contributor

Draft. Opened so upstream PR CI can run it. Our own CI is down, so this hasn't been through our usual CI runs: see our note on #699.

Status: draft. Opened so upstream PR CI can run it. No issue has been opened. Branch fix/media-restart-close is on the fork at 5cefbf0b8d9d5d9aa659a1955775a307362146bd, based on upstream/master at 5f55dc0df49bad85def95aae9ab82b6e656d9a93.

Problem

A local media source set to close when inactive can report PLAYING while it has no playback object. This happens when it is restarted or its settings change while a close is pending at EOF: for example a media_ended script that loops or switches files, or a restart queued by the restart hotkey or proc, or by restart_on_activate. The source then shows nothing until its settings change again. No issue has been opened, so this draft has no issue link.

Network sources go through the same window, but the base already recovers them: the tick closes the dead media and reconnects after reconnect_delay_sec. This PR leaves them on that path.

Root cause

The media worker's stop callback, media_stopped (plugins/obs-ffmpeg/obs-ffmpeg-source.c:291-292), sets destroy_media for a local close-when-inactive source (and for every network source), and then signals media_ended (:294-297). The worker calls it at EOF and also when it exits after a failed open or read (shared/media-playback/media-playback/media.c:960-967).

Within the next obs_source_video_tick():

  • queued media actions run first (libobs/obs-source.c:1407-1408), so a restart queued by obs_source_media_restart (:5864-5880) runs here;
  • then deferred settings updates run (:1410-1411; obs_source_update defers them for video sources at :1112-1113);
  • then the plugin's video_tick (:1463-1464).

At the base commit, neither ffmpeg_source_restart (obs-ffmpeg-source.c:731-738) nor ffmpeg_source_update (:491-494, :501-505) touches destroy_media. Each starts or reopens media, and ffmpeg_source_tick (:371-377) then destroys it in the same frame.

Fix

  • Restart: when a visible local file restarts while a close is pending, ffmpeg_source_restart runs that close itself. It destroys the media, clears destroy_media, and lets ffmpeg_source_start open new media. This works whether the worker is parked at EOF or has exited after a failed open or read.
  • Update: when ffmpeg_source_update destroys the media to reopen it, it also clears the old media's close request (one line).
  • Unchanged: network sources and hidden restarts leave the close to the tick, as before.

Options considered:

  • A, clear the pending close in ffmpeg_source_restart: the smallest change, but it keeps media whose worker may already have exited. A network source then never reconnects, and a local file whose open failed stays dead. Rejected.
  • B, close the pending media inside the restart and reopen it, for local files (implemented): works for parked and exited workers. Network sources keep their delayed reconnect.
  • B for network sources too: an earlier revision did this. It turned a restart-on-end handler against a down server into one stream open per frame (60 per second), instead of one reconnect every reconnect_delay_sec. Network sources weren't broken, so this was rejected.
  • C, change libobs to run the source tick before media actions and updates: changes the ordering for every controllable media source to fix one plugin's state. Rejected as broader than the bug.

Evidence

  • RED → GREEN: test_ffmpeg_restart_close, a headless Linux cmocka test with six cases. It calls the production media_stopped, ffmpeg_source_restart, ffmpeg_source_update and ffmpeg_source_tick callbacks in libobs's order. Linker wrappers stand in for the OBS and media-playback calls, so no libobs test hook or display is needed.

    On the unmodified base source, the two local cases fail: the media that the restart or the update opened is gone after the tick.

    1/1 Test #71: test_ffmpeg_restart_close ........***Failed    0.11 sec
    [ RUN      ] restart_after_eof_keeps_media_playing
    [  ERROR   ] --- s.media
    [   LINE   ] --- .../test/cmocka/test_ffmpeg_restart_close.c:162: error: Failure!
    [  FAILED  ] restart_after_eof_keeps_media_playing
    ...
    [ RUN      ] update_with_pending_close_keeps_new_media
    [  ERROR   ] --- 0 != 0x55a5320d4b60
    [   LINE   ] --- .../test/cmocka/test_ffmpeg_restart_close.c:363: error: Failure!
    [  FAILED  ] update_with_pending_close_keeps_new_media
    [==========] tests: 6 test(s) run.
    [  PASSED  ] 4 test(s).
    

    With GREEN, all six cases pass, with ENABLE_RUST_LIBOBS both OFF and ON (1/1 Test #71: test_ffmpeg_restart_close ... Passed). ctest --repeat until-fail:20 also passes in both modes.

    The four network and hidden-source cases pass on the base too, because they pin behaviour the base already has. Two of them guard the network reconnect:

    • restart_after_network_failure_waits_for_reconnect: the restart must leave the dead media to the tick, open nothing, and schedule the delayed reconnect.
    • restart_storm_against_down_server_waits_for_reconnect: a restart-on-end handler fires against a down server for 60 frames. The source must make 0 opens and start 1 reconnect.
  • Mutation check: each variant was built with this test and run, with OFF and ON giving identical results.

    Variant Failing cases
    Clear the flag on restart (first revision, 60cb26a54) 4: EOF, network, storm, update
    Close and reopen on restart for network sources too (previous revision, 62a7afd7c) 3: network, storm, update. Its storm run made 60 opens in 60 frames.
    Clear the flag even when hidden 6
    Clear the flag in ffmpeg_source_start instead 3: EOF, network, storm
    Clear the flag only for local files 2: EOF, update
    Close and reopen for local files when hidden too 1: hidden local (the hidden restart must leave the close to the tick)
    Leave out the update change 1: update
    Close and reopen for local files inside ffmpeg_source_start none. It is equivalent: with the update change, only a visible restart can reach ffmpeg_source_start with a local close pending. The reconnect thread runs only for network sources.
  • Commits: RED 3c4ce9dd43f229b38ebe95e498df4c514e2623b9, GREEN 5cefbf0b8d9d5d9aa659a1955775a307362146bd.

  • Fork CI: not run. Our self-hosted CI is down (see our note on #699), so this relies on upstream PR CI.

  • Real runs: not run. The test drives the production callbacks around the exact state transitions without starting OBS video or a GUI. A full playback run with a real file and a media_ended script is still untested.

Compatibility & risk

No public API or ABI changes. Behaviour changes only when a close is pending:

  • A visible restart of a local close-when-inactive file reopens the file, instead of having the restarted media destroyed by the next tick.
  • A settings update that reopens the media keeps it, instead of having it destroyed by the next tick. This applies to network sources as well: a network source updated during a pending close now keeps its reopened stream, where the base closed it on the next tick and reconnected after the reconnect delay.
  • Residual risk: take a local close-when-inactive file that is missing or corrupt, together with a handler that restarts it on every media_ended. It is now reopened once per frame, with a "Failed to open media" warning each time. On the base, the source stayed dead instead, which is the bug. A valid file loops only at its own duration. Throttling this would need extra state, so it is left out.

Apart from that update case, network reconnect behaviour is unchanged, as are hidden restarts and closes with no restart or update. The Linux test is the only coverage. macOS, Windows and a full OBS playback run have not been tested.

Status

Status: draft. Opened so upstream PR CI can run it.


How this was tested

  • RED → GREEN: test_ffmpeg_restart_close fails on the base callbacks and passes with GREEN. Shown locally on Linux (GCC 14.2, RelWithDebInfo, -Werror, ASan and UBSan, ASAN_OPTIONS=detect_leaks=1) with ENABLE_RUST_LIBOBS both OFF and ON. The sanitizer flags were added locally; CI builds the test with its own flags. RED fails because the tick destroys the media that the restart or the update just opened.
  • Our own CI: not run. Our self-hosted CI is down (see our note on #699), so this relies on upstream PR CI.
  • Real runs: not applicable yet. The cmocka test drives the production EOF, restart, update and tick callbacks without OBS video or a GUI. A full source playback run is still pending.

Thank you to the obs-rust maintainers and contributors for building this project in the open. In the American spirit of free expression and individual independence, and with the principle of judging contributions on their merit alone, this PR is a small piece of what independent people can build when they come together.

A media source sets destroy_media when its worker stops: at EOF for a
local close-when-inactive file, and whenever a network stream ends or
fails. libobs runs a restart queued in that window, and a settings
update deferred to the same frame, before the source's video tick. The
tick then destroys the media the restart or update just started, which
leaves a local file PLAYING with no media.

Drive the production EOF, restart, update and tick callbacks in libobs's
order, with linker wrappers around the OBS and media-playback calls:

- after EOF, a restart must leave live, newly opened media playing;
- after EOF, a settings update (such as a playlist script switching
  files) must open its media once, and the tick must not close it;
- after a network failure, a restart must not keep the dead media and
  must not reopen at once: the tick closes it and schedules the delayed
  reconnect, also when a restart-on-end subscriber fires every frame;
- a hidden restart must leave the close (and reconnect) to the tick.
…EEN)

When a visible local file restarts while destroy_media is still set,
close the media inside the restart and let ffmpeg_source_start open new
media, instead of starting the media the next tick destroys. This works
whether the worker is parked at EOF or has exited after a failed open
or read.

When a settings update replaces the media, clear the old media's close
request too. libobs defers updates to the same frame window, so a
media_ended handler that switches files otherwise has its new media
destroyed by the next tick.

Network sources keep the existing path: the tick closes the dead media
and reconnects after reconnect_delay_sec, so a restart-on-end handler
against a down server cannot reopen the stream every frame. A hidden
restart still leaves the close to the tick.
@zackees
zackees marked this pull request as ready for review October 14, 2026 23:09
@zackees
zackees self-requested a review as a code owner October 14, 2026 23:09
@zackees
zackees merged commit 807d801 into obs-rust:master Oct 14, 2026
22 checks passed
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.

2 participants