Skip to content

media-playback: Time out silent RTSP sources - #193

Merged
zackees merged 3 commits into
obs-rust:masterfrom
rdoupe:fix/189-rtsp-timeout
Oct 11, 2026
Merged

zackees merged 3 commits into
obs-rust:masterfrom
rdoupe:fix/189-rtsp-timeout

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

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 option stimeout. FFmpeg 5.0 renamed that option to timeout. 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_cb never ran, and obs-ffmpeg-source never started its reconnect.

  1. test/cmocka: Add RED RTSP silent-camera tests: moves the option, unchanged, into mp_media_set_network_timeouts() in shared/media-playback/media-playback/network-timeout.c, and adds test_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_kept passes. It makes sure the fix keeps a timeout that a user already entered in the source's FFmpeg Options, which is today's workaround.
  2. media-playback: Time out silent RTSP sources: for rtsp:// and rtsps:// inputs, sets timeout to 5 s (in microseconds) with AV_DICT_DONT_OVERWRITE, in place of stimeout. Other protocols use timeout with different meanings and units, so they are left alone. In practice they had no timeout before either, since stimeout did 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):

Test Checks RED commit GREEN commit
rtsp_timeout_options_are_known_to_ffmpeg every option set for an RTSP input exists in the linked libavformat's RTSP demuxer (av_opt_find) fails passes
rtsp_socket_timeout_is_set timeout is set and is greater than 0 fails passes
user_timeout_is_kept a timeout already in the options is not overwritten passes passes
silent_rtsp_camera_stops_media end to end through media-playback and FFmpeg: a fake camera on 127.0.0.1 inside the test answers OPTIONS, DESCRIBE, SETUP and PLAY, then sends nothing; the media must stop within 20 s fails after 20 s passes in about 10 s

I 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:

before after
RTSP over TCP, server frozen for 54 s 0 frames, never noticed, no log read fails about 10 s after the freeze, then it retries every ~8 s
RTSP over UDP, same 0 frames, never noticed same as TCP
Frozen server replaced by a new one (camera rebooted) stays frozen frames resume about 1 s after the new server is up
Server killed and restarted reconnects (EOF was already handled) unchanged

clang-format 22.1.3 and gersemi 0.25.0 (./lint's C and CMake checks) and the ci/ Python tests pass. The Rust steps of ./lint and ./unittest need soldr, which isn't installed here, and the Tier 2 Docker lane couldn't run without Docker access. This PR changes no Rust.

Not tested:

  • Windows and macOS. The end-to-end test is compiled out on Windows because it uses POSIX sockets; the three option tests are portable.
  • A real IP camera.
  • A source that keeps sending data but no video frames (the Media source hangs (RTSP), but OBS is unaware obsproject/obs-studio#11537 case where RTCP or keepalives still flow). A socket timeout can't catch that; it would need a watchdog on the time since the last packet in interrupt_callback, which could be a follow-up.
  • Other network protocols (HTTP, SRT, RIST, UDP). Their behaviour is unchanged.

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 requested a review from zackees as a code owner October 11, 2026 04:16
rdoupe and others added 2 commits October 10, 2026 21:21
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
zackees force-pushed the fix/189-rtsp-timeout branch from 709c093 to 130722b Compare October 11, 2026 04:23
@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

Verified locally, rebased onto master and stamped. The new head is 130722bc4.

  • RED (317d36a07): Tier 2 passed 56 of 57. test_media_network_timeout failed 3 of 4 (rtsp_socket_timeout_is_set, rtsp_timeout_options_are_known_to_ffmpeg, silent_rtsp_camera_stops_media).
  • GREEN (709c093a9): Tier 2 passed 57 of 57 with ENABLE_RUST_LIBOBS OFF and 57 of 57 with it ON.
  • Local gate on the rebased head: lint, rust-test and tier2-linux all passed.

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]>
@rdoupe

rdoupe commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for stamping. CI's Rust Tier 2 windows job failed to link the new test: fail_msg() expands to cm_print_error(), which the Windows cmocka library doesn't export (LNK2019). Pushed 6b5e7e754 on top of your stamped head: it reports those failures with fprintf() + fail() like test_base, test_video_fourcc and test_video_matrices, and drops the email address from the new files' copyright lines. The test still passes 4/4 locally; the stamp will need refreshing for the new head.

@zackees
zackees merged commit 62b02b2 into obs-rust:master Oct 11, 2026
23 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.

bug: media-playback: RTSP source never reconnects when the camera goes silent (upstream #12958)

2 participants