Skip to content

obs-ffmpeg: Fix reconnection for SRT protocol and mpegts muxer & other bugfixes - #12165

Merged
RytoEX merged 7 commits into
obsproject:masterfrom
pkviet:srtcleanup
Aug 22, 2025
Merged

RytoEX merged 7 commits into
obsproject:masterfrom
pkviet:srtcleanup

Conversation

@pkviet

@pkviet pkviet commented May 17, 2025 •

Copy link
Copy Markdown
Member

Description

The mpegts output used with srt protocol has long been plagued by reconnection issues, leading to crashes.
This fixes (hopefully) the issue.
This also fixes the following bugs:

  • crash when using a pkt_size != 1316 with SRT;
  • crash when using an invalid URL with SRT.

Motivation and Context

The reconnection logic proved difficult to implement for several reasons:

  • the reconnection with rtmp and the logic used in backend (in obs-output.c) is difficult to adapt to the mpegts muxer; it would require to track several bools, which make very easy to be sidetracked along the many paths of failure.
  • on srt side, the srt_connect function can return a success in socket non-blocking mode and caller mode, which would trigger a signal to obs UI of OBS_OUTPUT_SUCCESS, while no connection has really been made.
  • this could lead to crashes because the UI and the real state of the output might not be in sync, leading to crashes.

In order to fix the issue, we have:

  • reworked the mpegts reconnect logic by moving the start and stop functions to a single thread controlled by a mutex, so that these two pipelines are now mutually exclusive. This avoids one major point of failure by preventing a call to start while stop has not finished cleaning up. We have followed the logic used in the whip output which is much simpler to implement than the
    multi-layered bools used in rtmp-stream.c.
  • made explicit checks on the socket connection state on the srt protocol side, so that an OBS_OUTPUT_SUCCESS code is never returned before having ascertained that a successful connection was realized.

This fixes:
#11062
#11620
#11721
#12133
#11885

How Has This Been Tested?

Used the clumsy app to simulate a complete network failure.
OBS detects correctly a network disconnection.
It triggers correctly the reconnect thread from obs-output.c.
The reconnects start.
After a few reconnects, upon reestablishment of the network, obs manages to reconnect.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Code cleanup (non-breaking change which makes code smaller or more readable)

Checklist:

  • My code has been run through clang-format.
  • I have read the contributing document.
  • My code is not on the master branch.
  • The code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.

@pkviet pkviet added kind/bug Categorizes issue or PR as related to a bug. Seeking Testers Build artifacts on CI labels May 17, 2025
@pkviet
pkviet force-pushed the srtcleanup branch 2 times, most recently from 5f7da71 to 77a3365 Compare May 17, 2025 02:10
@RytoEX
RytoEX requested a review from tt2468 May 20, 2025 16:43
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c Resolved
@ogmkp

ogmkp commented Jul 4, 2025

Copy link
Copy Markdown

If I use the pkt_size parameter in the SRT output streaming url to reduce packet size (because of reduced MTU in the path), OBS can't connect and display and error in popup telling me to look at log.
OBS 31.0.4 on linux

17:35:21.094: [obs-ffmpeg mpegts muxer: 'adv_stream']: Output format name and long_name: mpegts, MPEG-TS (MPEG-2 Transport Stream)
17:35:21.095: [obs-ffmpeg mpegts muxer / libsrt]: libsrt version 1.5.4 loaded
17:35:21.191: ==== Streaming Start ===============================================
17:35:21.454: [obs-ffmpeg mpegts muxer / libsrt]: Operation not supported: Incorrect use of Message API (sendmsg/recvmsg)
17:35:21.454: process_packet: Error writing packet: Unknown error occurred
17:35:21.454: Output 'adv_stream': stopping
17:35:21.454: Output 'adv_stream': Total frames output: 3
17:35:21.454: Output 'adv_stream': Total drawn frames: 22
17:35:21.454: ---------------------------------
17:35:21.454: [obs-ffmpeg mpegts muxer / libsrt]: Session Summary
17:35:21.454: time elapsed [0.4 sec]
17:35:21.454: mean speed [0.0 Mbp]
17:35:21.454: total bytes sent [0.0 MB]
17:35:21.454: bytes retransmitted [0.0 %]
17:35:21.454: bytes dropped [0.0 %]
17:35:21.454:
17:35:21.456: ==== Streaming Stop ================================================
17:35:21.456: [obs-ffmpeg mpegts muxer / libsrt]: SRT connection closed

@ogmkp

ogmkp commented Jul 7, 2025

Copy link
Copy Markdown

If I start a stream to srt://:port?mode=listener or srt://0.0.0.0:port?mode=listener or 127.0.0.1:port?mode=listener,
The connection (streaming) button still stuck and grayed, I have to kill OBS.

debug: obs_output_set_audio_encoder: Null 'output' parameter
debug: output 'adv_stream' (ffmpeg_mpegts_muxer) created
debug: [obs-nvenc: 'advanced_video_stream'] Loading up CUDA on device 0
info: [obs-nvenc: 'advanced_video_stream'] settings:
	codec:        H264
	rate_control: CBR
	bitrate:      1000
	keyint:       120
	preset:       p5
	tuning:       hq
	multipass:    qres
	profile:      high
	width:        1920
	height:       1080
	b-frames:     2
	b-ref-mode:   0
	lookahead:    false (0 frames)
	aq:           true

info: ---------------------------------
info: [FFmpeg aac encoder: 'Track1'] bitrate: 128, channels: 2, channel_layout: stereo, track: 1

info: [obs-ffmpeg mpegts muxer: 'adv_stream']: Output format name and long_name: mpegts, MPEG-TS (MPEG-2 Transport Stream)
info: [obs-ffmpeg mpegts muxer / libsrt]: libsrt version 1.5.4 loaded

If I stream to srt://ip:port?mode=caller with a destination port that is not open on other side, OBS crash with segfault.

debug: obs_output_set_audio_encoder: Null 'output' parameter
debug: output 'adv_stream' (ffmpeg_mpegts_muxer) created
debug: [obs-nvenc: 'advanced_video_stream'] Loading up CUDA on device 0
info: [obs-nvenc: 'advanced_video_stream'] settings:
	codec:        H264
	rate_control: CBR
	bitrate:      1000
	keyint:       120
	preset:       p5
	tuning:       hq
	multipass:    qres
	profile:      high
	width:        1920
	height:       1080
	b-frames:     2
	b-ref-mode:   0
	lookahead:    false (0 frames)
	aq:           true

info: ---------------------------------
info: [FFmpeg aac encoder: 'Track1'] bitrate: 128, channels: 2, channel_layout: stereo, track: 1

info: [obs-ffmpeg mpegts muxer: 'adv_stream']: Output format name and long_name: mpegts, MPEG-TS (MPEG-2 Transport Stream)
info: [obs-ffmpeg mpegts muxer / libsrt]: libsrt version 1.5.4 loaded
error: [obs-ffmpeg mpegts muxer: 'adv_stream']: Failed to open the url or invalid stream
error: [obs-ffmpeg mpegts muxer: 'adv_stream']: Failed to open the url
error: failed to start the mpegts output
Erreur de segmentation

Again, if I add pkt_size=1128 parameter in the srt url, OBS don't start the streaming and ask to look at log:

06:40:13.388754/obs*E:SRT.cc: LiveCC: payload size: 1316 exceeds maximum allowed 1128
error: [obs-ffmpeg mpegts muxer / libsrt]: Operation not supported: Incorrect use of Message API (sendmsg/recvmsg)
warning: process_packet: Error writing packet: Unknown error occurred
info: Output 'adv_stream': stopping
info: Output 'adv_stream': Total frames output: 3
info: Output 'adv_stream': Total drawn frames: 21
info: ---------------------------------
info: [obs-ffmpeg mpegts muxer / libsrt]: Session Summary
	time elapsed [0.3 sec]
	mean speed [0.0 Mbp]
	total bytes sent [0.0 MB]
	bytes retransmitted [0.0 %]
	bytes dropped [0.0 %]

info: ==== Streaming Stop ================================================
info: [obs-ffmpeg mpegts muxer / libsrt]: SRT connection closed
debug: Qavg: 65536.000

debug: 2 frames left in the queue on closing

When I open 2 srt in two media sources and when I re-apply the stream in one of them, both media source reset (like before the patch) but no reset on external player (ffplay) with the same url, its only in OBS.

06:43:58.002567/mp_media_thread*E:SRT.ea: EID:21 no sockets to check, this would deadlock
debug: Operation not supported: All sockets removed from epoll, waiting would deadlock

info: [Media Source 'Source média 2']: settings:
	input:                   srt://ip:port?streamid=read:test&latency=200000
	input_format:            
	speed:                   100
	is_looping:              no
	is_linear_alpha:         no
	is_hw_decoding:          yes
	is_clear_on_media_end:   no
	restart_on_activate:     no
	close_when_inactive:     no
	full_decode:             no
	ffmpeg_options:          
warning: MP: av_read_frame failed: Unknown error occurred (-1313558101)
warning: [Media Source 'Source média']: Disconnected. Reconnecting...
06:43:58.061258/mp_media_thread*E:SRT.ea: EID:23 no sockets to check, this would deadlock
debug: Operation not supported: All sockets removed from epoll, waiting would deadlock

debug: Connection to srt://ip:port?streamid=read:test&latency=200000 failed: Unknown error occurred

warning: MP: Failed to open media: 'srt://ip:port?streamid=read:test&latency=200000'
warning: [Media Source 'Source média 2']: Disconnected. Reconnecting...
debug: co located POCs unavailable

debug: co located POCs unavailable

debug: Timestamp for source 'Source média 2' jumped by '3875810308', expected value 85508535951, input value 89384346259
info: [Media Source 'Source média 2']: Reconnected.
debug: Timestamp for source 'Source média' jumped by '3819330843', expected value 85573533628, input value 89392864471
info: [Media Source 'Source média']: Reconnected.

@pkviet

pkviet commented Jul 11, 2025

Copy link
Copy Markdown
Member Author

If I use the pkt_size parameter in the SRT output streaming url to reduce packet size (because of reduced MTU in the path), OBS can't connect and display and error in popup telling me to look at log. OBS 31.0.4 on linux

are you reporting this for current release or specifically for this PR ? if there's the same issue on master, then post a specific issue; if it works on master, but is broken in the PR, please provide details about your test setup

@pkviet

pkviet commented Jul 11, 2025

Copy link
Copy Markdown
Member Author

If I start a stream to srt://:port?mode=listener or srt://0.0.0.0:port?mode=listener or 127.0.0.1:port?mode=listener, The connection (streaming) button still stuck and grayed, I have to kill OBS.

you have to have a caller or it will not connect, so it keeps staying in the connecting state. This is not a bug, and won't be fixed.
Search the issues where this was already reported and I explained why i am not fixing that.

If I stream to srt://ip:port?mode=caller with a destination port that is not open on other side, OBS crash with segfault.

did you test with the current PR ? if you did, give me details on your test setup.
In my tests of this PR, there were no such issues on window 11.
If you tested on linux with your own build (with gnutls , i assume ?), you're on your own ...

Again, if I add pkt_size=1128 parameter in the srt url, OBS don't start the streaming and ask to look at log:

we pass the pkt size to libsrt, so if you have an issue, i suspect it's on libsrt side; i would check with FFmpeg compiled with the exact same version of libsrt, in order to confirm where the issue dwells.

When I open 2 srt in two media sources and when I re-apply the stream in one of them, both media source reset (like before the patch) but no reset on external player (ffplay) with the same url, its only in OBS.

It is unacceptable to mix issues; this is not a general SRT fix thread but a PR. That issue you report is with Media Sources, not with SRT output. The current PR deals with outputting an SRT stream from OBS.
If you want to get actual consideration of your issues, create your own issues in the repo and don't hijack either other people's issues or PRs

@ogmkp

ogmkp commented Jul 12, 2025

Copy link
Copy Markdown

If I use the pkt_size parameter in the SRT output streaming url to reduce packet size (because of reduced MTU in the path), OBS can't connect and display and error in popup telling me to look at log. OBS 31.0.4 on linux

are you reporting this for current release or specifically for this PR ? if there's the same issue on master, then post a specific issue; if it works on master, but is broken in the PR, please provide details about your test setup

All comments are for this PR.
It crash on master (does not concern this PR).
Print error message for this PR that is a better way.

If I start a stream to srt://:port?mode=listener or srt://0.0.0.0:port?mode=listener or 127.0.0.1:port?mode=listener, The connection (streaming) button still stuck and grayed, I have to kill OBS.

you have to have a caller or it will not connect, so it keeps staying in the connecting state. This is not a bug, and won't be fixed. Search the issues where this was already reported and I explained why i am not fixing that.

For this PR and for master, the connection state remains suspended until a caller comes and holds the button. Its needed to set a timeout or something from libsrt to release the listener if no one calls.
It's a bug because it freezes a feature that remains... frozen.
I thought this PR was supposed to solve this kind of problem with SRT.

If I stream to srt://ip:port?mode=caller with a destination port that is not open on other side, OBS crash with segfault.

did you test with the current PR ? if you did, give me details on your test setup. In my tests of this PR, there were no such issues on window 11. If you tested on linux with your own build (with gnutls , i assume ?), you're on your own ...

Yes, it's with this PR and if there are no tests done with Linux then this PR is not viable for multi-platform.

When I open 2 srt in two media sources and when I re-apply the stream in one of them, both media source reset (like before the patch) but no reset on external player (ffplay) with the same url, its only in OBS.

It is unacceptable to mix issues; this is not a general SRT fix thread but a PR. That issue you report is with Media Sources, not with SRT output. The current PR deals with outputting an SRT stream from OBS. If you want to get actual consideration of your issues, create your own issues in the repo and don't hijack either other people's issues or PRs

You've never made a mistake or misunderstood anything in your life, that's unacceptable.

@pkviet

pkviet commented Jul 12, 2025

Copy link
Copy Markdown
Member Author

@ogmkp i apologize for the harsh words but from a dev point of view, it's difficult when info is dumped and lacks enough context to make it actionable. Realize that the maintenance for srt is done in my free time on top of a regular job. I am not paid for it.
As stated already we can't possibly support all linux variations. I myself develop chiefly on windows so to optimize consideration it is better to provide steps on windows.
So please try to provide repro steps with the ppa binary or with the flatpak. It's to ensure that the issue does not stem from the reporter local setup but is generic enough to deserve our attention.
For your crash with pkt_size on master, create an issue for it after reproducing it with the ppa.
For the listener in a connecting state, it is because the connection and reconnection pipeline is tailored to rtmp. It would require changes in libobs obs-output.c that i am not prepared to do, such as creating a waiting state for connections. This is less trivial than it sounds. So right now it works as I intended it to. The alternative for me would be to just disable the listener if people are unhappy with it. When i test a listener, i connect it to a caller. Afterwards, the stream can be stopped. This was already discussed so i won't delve into more details.

@ogmkp

ogmkp commented Jul 12, 2025

Copy link
Copy Markdown

No problem, most of us are volunteers and we do it with passion. Whether we're a developer or a power user.
I don't build with PPA directions, because it have to be compliant with Ubuntu and flatpak is too sealed to be agile, and then I build on Debian...
Ok for listener, maybe a popup before going any further would be useful, anyway, it's to avoid any other issues.
It's your PR, you've got the upper hand on it, since all I do is only report problems, as long as it don't create new ones.

@pkviet

pkviet commented Jul 12, 2025

Copy link
Copy Markdown
Member Author

@ogmkp i could definitively add a log entry about the listener waiting for a connection. For a pop up that will have to be negotiated with our UI/UX people but yeah that's a good suggestion.
The plan forward is to make a real UI for SRT settings and to have the stats graphs. But there's interference with other plans for the other outputs so it is very slow.

@pkviet
pkviet force-pushed the srtcleanup branch 2 times, most recently from 3ba2ee8 to 766b7a9 Compare July 19, 2025 09:11
@pkviet

pkviet commented Jul 19, 2025

Copy link
Copy Markdown
Member Author

@ogmkp managed to repro your pkt_size issue and fixed it.
Fixed also the crash for an invalid URL.
And added a log entry in listener mode. The popup requires much more work.

@pkviet
pkviet force-pushed the srtcleanup branch 2 times, most recently from 287c526 to fc67008 Compare August 11, 2025 17:45
@PatTheMav PatTheMav moved this from In progress to Done in OBS Studio 32.0 PR Considerations Aug 11, 2025
@RytoEX RytoEX changed the title obs-ffmpeg: Fix reconnection for srt protocol and mpegts muxer & other bugfixes obs-ffmpeg: Fix reconnection for SRT protocol and mpegts muxer & other bugfixes Aug 21, 2025
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-srt.h Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-srt.h Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-srt.h Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-srt.h Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c Resolved
@pkviet

pkviet commented Aug 22, 2025 •

Copy link
Copy Markdown
Member Author

On hold.
I have a regression between current iteration of the PR and earlier ones.

@pkviet
pkviet force-pushed the srtcleanup branch 2 times, most recently from 0f6f2d8 to 55a09d8 Compare August 22, 2025 18:01
@pkviet

pkviet commented Aug 22, 2025

Copy link
Copy Markdown
Member Author

Update
Comments addressed and regression fixed.

Comment thread plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c Resolved

@RytoEX RytoEX left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit message nits:

  • srt -> SRT
  • whip -> WHIP

Nits.

Comment thread plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c Resolved
pkviet added 7 commits August 22, 2025 23:52
The do_log function defined for the muxer expects that the struct is
named stream rather than output. So rename output to stream.
This also avoids confusion with stream->output which is an obs_output_t
pointer.

Signed-off-by: pkv <[email protected]>
This is the first part of a fix for reconnection issues with SRT
protocol.
This part implements the logic on the side of the protocol.
The fixes are as follows:
- a cleanup of return values of the various functions used. The main
libsrt_open function returns OBS_OUTPUT_XXX enum members. But the other
functions return now only AVERROR(xxx).
- an explicit check of the socket state before returning
OBS_OUTPUT_SUCCESS. This fixes an issue due to relying on socket
non-blocking mode; in caller mode, the srt_connect function will return
a success even before a connection has succeeded since it relies on
notifications by srt_epoll_wait. This would prompt obs UI to display a
successful reconnection in spite of a failure.

Signed-off-by: pkv <[email protected]>
This reimplements the reconnect logic in mpegts muxer.
Taking cues from the WHIP output, a start and stop thread is used,
controlled by a mutex, ensuring the start and stop pipelines can never
be run concurrently.
Fixes bugs obsproject#11885, obsproject#11721, obsproject#11620, obsproject#11062, obsproject#12133.
For better readibility, this commits also breaks the big set_config
function into smaller units.

Signed-off-by: pkv <[email protected]>
This fixes a crash when an invalid URL is used.

Signed-off-by: pkv <[email protected]>
This updates a few OBS-OUTPUT-XXX to better reflect the output failures
for the RIST protocol.
Signed-off-by: pkv <[email protected]>
This fixes a crash when using a pkt_size which is not the UDP default of
1316 Bytes ( 7 * 188 ).

Signed-off-by: pkv <[email protected]>
@pkviet

pkviet commented Aug 22, 2025

Copy link
Copy Markdown
Member Author

Commit message nits:

  • srt -> SRT
  • whip -> WHIP

Nits.

Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Categorizes issue or PR as related to a bug. Seeking Testers Build artifacts on CI

Projects

No open projects

Development

Successfully merging this pull request may close these issues.

OBS 31.0.0 (Mac): Crash when streaming – '[adv_stream] Failed to open the URL'

5 participants