Repository navigation
obs-ffmpeg: Reopen local media when restart finds a pending close - #732
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Status: draft. Opened so upstream PR CI can run it. No issue has been opened. Branch
fix/media-restart-closeis on the fork at5cefbf0b8d9d5d9aa659a1955775a307362146bd, based onupstream/masterat5f55dc0df49bad85def95aae9ab82b6e656d9a93.Problem
A local media source set to close when inactive can report
PLAYINGwhile it has no playback object. This happens when it is restarted or its settings change while a close is pending at EOF: for example amedia_endedscript that loops or switches files, or a restart queued by the restart hotkey or proc, or byrestart_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), setsdestroy_mediafor a local close-when-inactive source (and for every network source), and then signalsmedia_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():libobs/obs-source.c:1407-1408), so a restart queued byobs_source_media_restart(:5864-5880) runs here;:1410-1411;obs_source_updatedefers them for video sources at:1112-1113);video_tick(:1463-1464).At the base commit, neither
ffmpeg_source_restart(obs-ffmpeg-source.c:731-738) norffmpeg_source_update(:491-494,:501-505) touchesdestroy_media. Each starts or reopens media, andffmpeg_source_tick(:371-377) then destroys it in the same frame.Fix
ffmpeg_source_restartruns that close itself. It destroys the media, clearsdestroy_media, and letsffmpeg_source_startopen new media. This works whether the worker is parked at EOF or has exited after a failed open or read.ffmpeg_source_updatedestroys the media to reopen it, it also clears the old media's close request (one line).Options considered:
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.reconnect_delay_sec. Network sources weren't broken, so this was rejected.Evidence
RED → GREEN:
test_ffmpeg_restart_close, a headless Linux cmocka test with six cases. It calls the productionmedia_stopped,ffmpeg_source_restart,ffmpeg_source_updateandffmpeg_source_tickcallbacks 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.
With GREEN, all six cases pass, with
ENABLE_RUST_LIBOBSboth OFF and ON (1/1 Test #71: test_ffmpeg_restart_close ... Passed).ctest --repeat until-fail:20also 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.
60cb26a54)62a7afd7c)ffmpeg_source_startinsteadffmpeg_source_startffmpeg_source_startwith a local close pending. The reconnect thread runs only for network sources.Commits: RED
3c4ce9dd43f229b38ebe95e498df4c514e2623b9, GREEN5cefbf0b8d9d5d9aa659a1955775a307362146bd.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:
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
test_ffmpeg_restart_closefails 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) withENABLE_RUST_LIBOBSboth 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.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.