Repository navigation
media-playback: Time out silent RTSP sources - #193
Merged
Merged
Conversation
A Media Source playing an RTSP camera never reconnects once the camera goes silent (loses power, drops off the network) without closing the connection (upstream obsproject/obs-studio#12958, reported by canadianlocomotivelogistics; also #11537 by dzek69). The media thread blocks in av_read_frame() forever, so stop_cb never runs and obs-ffmpeg-source never schedules its reconnect. init_avformat() bounds network reads with the RTSP option "stimeout", which FFmpeg removed in 5.0 (renamed "timeout"). avformat_open_input() leaves the unknown key unused, so neither RTSP over TCP nor over UDP has any read timeout. Move that option into mp_media_set_network_timeouts() in media-playback/network-timeout.c, unchanged, and test it: - every option it sets for an rtsp:// input exists in the linked FFmpeg's RTSP demuxer (fails: 'stimeout'), - it sets the RTSP socket I/O "timeout" (fails), - a "timeout" from the source's FFmpeg Options is kept (passes), - end to end: a camera on 127.0.0.1 that answers OPTIONS, DESCRIBE, SETUP and PLAY and then sends nothing must make the media stop within 20 s (fails after the 20 s deadline). 3 of the 4 tests fail. Setting "timeout" (e.g. 5 s, without overwriting the user's value) turns them green in about 10 s. Refs obs-rust#189. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
An RTSP camera that went silent without closing the connection left the Media Source frozen for good: the media thread blocked in av_read_frame(), so it never stopped and the source never reconnected (upstream obsproject/obs-studio#12958, #11537). The read timeout was set with "stimeout", which FFmpeg 5.0 renamed to "timeout" in its RTSP demuxer; every FFmpeg OBS supports ignores the old name. Set "timeout" to 5 s for rtsp:// and rtsps:// inputs instead. A silent camera now makes the read fail after about 10 s (FFmpeg retries once), the media stops, and obs-ffmpeg-source reconnects after its reconnect delay. A "timeout" from the source's FFmpeg Options is kept. Other protocols give "timeout" other meanings, so they are left alone, as they effectively were before. All 4 tests in test_media_network_timeout now pass; the end-to-end silent camera case stops in about 10 s. Fixes obs-rust#189. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Local-Gate: v1 tree=a3d760ce52f8fe6958748ab671f6b1e9c151f1a5 secs=116 lanes=lint:run,rust-test:run,tier2-linux:run Ci-Attestation: {"at":1791692616,"gate":"rust/all/fmt","host":"linux-x86_64","key":"85215dd48055485ab498497a9a0463d56d127805d386abc4674fd5cc4a8b85d0","lane":"lint","parents":["14cee7d88dddd81608918690e003c6c3060397bd"],"secs":44,"stamp":"0a4a8086c71080ebcac96292a1d70a8b","tree":"a3d760ce52f8fe6958748ab671f6b1e9c151f1a5","v":1,"via":"run"} Ci-Attestation: {"at":1791692616,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"85215dd48055485ab498497a9a0463d56d127805d386abc4674fd5cc4a8b85d0","lane":"lint","parents":["14cee7d88dddd81608918690e003c6c3060397bd"],"secs":44,"stamp":"c52f9463672755a97487057e38ed89ea","tree":"a3d760ce52f8fe6958748ab671f6b1e9c151f1a5","v":1,"via":"run"} Ci-Attestation: {"at":1791692616,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"abe3c09d530d0d8ec9d83c41fb2085639839a3e015ce23eacb2f0bbfc713ad71","lane":"rust-test","parents":["14cee7d88dddd81608918690e003c6c3060397bd"],"secs":25,"stamp":"3219ce5232243e84e33ace997442953a","tree":"a3d760ce52f8fe6958748ab671f6b1e9c151f1a5","v":1,"via":"run"} Ci-Attestation: {"at":1791692616,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"7355211988cea6819a7c55b21478b0c45db50c513b33def98f2a8cc688928ab9","lane":"tier2-linux","parents":["14cee7d88dddd81608918690e003c6c3060397bd"],"secs":47,"stamp":"e449e485df7af3943fb30428f8b01811","tree":"a3d760ce52f8fe6958748ab671f6b1e9c151f1a5","v":1,"via":"run"}
zackees
force-pushed
the
fix/189-rtsp-timeout
branch
from
October 11, 2026 04:23
709c093 to
130722b
Compare
Member
|
Verified locally, rebased onto master and stamped. The new head is
Merging once CI is green. |
The Windows Tier 2 build failed to link test_media_network_timeout: fail_msg() expands to cm_print_error(), which the Windows cmocka library does not export (LNK2019). Print the message with fprintf() and call fail(), as test_base, test_video_fourcc and test_video_matrices do. Also drop the email address from the new files' copyright lines. Refs obs-rust#189. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Collaborator
Author
|
Thanks for stamping. CI's Rust Tier 2 windows job failed to link the new test: |
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.
Description
A Media Source playing an RTSP camera froze for good when the camera went silent without closing the connection, for example because it lost power or dropped off the network. It never reconnected, even after the camera came back. With this change the source gives up on the dead connection after about 10 s, and the existing reconnect logic brings it back once the camera answers again.
The cause:
init_avformat()limits network reads with the RTSP optionstimeout. FFmpeg 5.0 renamed that option totimeout.avformat_open_input()ignores option names it doesn't know, so nothing set a read timeout, over either TCP or UDP.av_read_frame()then blocked forever, the media thread never stopped,stop_cbnever ran, and obs-ffmpeg-source never started its reconnect.test/cmocka: Add RED RTSP silent-camera tests: moves the option, unchanged, intomp_media_set_network_timeouts()inshared/media-playback/media-playback/network-timeout.c, and addstest_media_network_timeout. 3 of its 4 tests fail on this commit:rtsp_timeout_options_are_known_to_ffmpeg: "FFmpeg's RTSP demuxer has no option 'stimeout'"rtsp_socket_timeout_is_set: "no 'timeout' option is set for an RTSP input"silent_rtsp_camera_stops_media: "media did not stop within 20000 ms of the camera going silent"user_timeout_is_keptpasses. It makes sure the fix keeps atimeoutthat a user already entered in the source's FFmpeg Options, which is today's workaround.media-playback: Time out silent RTSP sources: forrtsp://andrtsps://inputs, setstimeoutto 5 s (in microseconds) withAV_DICT_DONT_OVERWRITE, in place ofstimeout. Other protocols usetimeoutwith different meanings and units, so they are left alone. In practice they had no timeout before either, sincestimeoutdid nothing. All 4 tests pass.Motivation and Context
Fixes #189 (upstream obsproject/obs-studio#12958, reported by canadianlocomotivelogistics; related: obsproject/obs-studio#11537 by dzek69)
Why this one: it isn't one of the most-discussed upstream issues. It is a silent failure for 24/7 streams: the source looks fine but stays frozen until someone opens its properties and clicks OK, and nothing appears in the log. It has been broken on every FFmpeg version OBS supports since FFmpeg 5.0 renamed the option, and the fix is one line.
How Has This Been Tested?
Automated (cmocka; no server needed):
rtsp_timeout_options_are_known_to_ffmpegav_opt_find)rtsp_socket_timeout_is_settimeoutis set and is greater than 0user_timeout_is_kepttimeoutalready in the options is not overwrittensilent_rtsp_camera_stops_mediaI built and ran both commits on Arch Linux with FFmpeg 9.0.1. The rest of ctest (57 tests) passes on the GREEN commit.
By hand: I ran a local mediamtx v1.21.2 server on 127.0.0.1 with an ffmpeg test pattern published to it. A headless harness drove media-playback the way obs-ffmpeg-source does (
stop_cb, then destroy, then reconnect after the delay; 3 s delay here). To simulate a camera that vanishes, I froze the server with SIGSTOP, which leaves the TCP connection open with no data:clang-format 22.1.3 and gersemi 0.25.0 (
./lint's C and CMake checks) and theci/Python tests pass. The Rust steps of./lintand./unittestneedsoldr, which isn't installed here, and the Tier 2 Docker lane couldn't run without Docker access. This PR changes no Rust.Not tested:
interrupt_callback, which could be a follow-up.Types of changes
Checklist:
🤖 Generated with Claude Code