Recording reconnection handling

Hi all,

I have a mediasoup application that via plain transport connects to a remote host which is running FFMpeg to record the producers, and this works great.
But when the producer disconnects, the FFMpeg process timesout, for my requirement I want this ffmpeg process to keep running even if the producer disconnects and adds blank frames at that time, once the producer is back, I can then create plain transport, and related consumers again and connect to the same FFMpeg process again.

Things I’ve tried in FFMpeg cmds, -stimeout doesnt exist. -timeout doesn’t work (its recognised by ffmpeg but later I get the error timeout not set in AVOption).
I’ve also tried pausing the consumer and keeping it alive (ffmpeg process still times out).

Here are the logs from ffmpeg:

'[sdp @ 0x562e5118a540] RTP: dropping old packet received too late\n' +
   '    Last message repeated 1 times\n' +
   '[webm @ 0x562e511e7c40] Writing block of size 81 with pts 20281, dts 20281, duration 0 at relative offset 10906 in cluster at offset 313421. TrackNumber 2, keyframe 1\n' +
   '[sdp @ 0x562e5118a540] RTP: dropping old packet received too late\n']
 ffmpeg::process::data [data:'    Last message repeated 2 times\n' +
   '[webm @ 0x562e511e7c40] Writing block of size 81 with pts 20301, dts 20301, duration 0 at relative offset 10993 in cluster at offset 313421. TrackNumber 2, keyframe 1\n' +
   '[webm @ 0x562e511e7c40] Writing block of size 902 with pts 20312, dts 20312, duration 67 at relative offset 11080 in cluster at offset 313421. TrackNumber 1, keyframe 0\n' +
   '[sdp @ 0x562e5118a540] RTP: dropping old packet received too late\n']
 ffmpeg::process::data [data:'pipe:0: Connection timed out\n' +
   'No more output streams to write to, finishing.\n' +
   '[webm @ 0x562e511e7c40] Writing block of size 81 with pts 20321, dts 20321, duration 0 at relative offset 11989 in cluster at offset 313421. TrackNumber 2, keyframe 1\n']
 ffmpeg::process::data [data:'[webm @ 0x562e511e7c40] end duration = 20379\n' +
   '[webm @ 0x562e511e7c40] stream 0 end duration = 20379\n' +
   '[webm @ 0x562e511e7c40] stream 1 end duration = 20321\n' +
   'frame=  234 fps=9.2 q=-1.0 Lsize=     318kB time=00:00:20.32 bitrate= 128.2kbits/s speed=0.799x    \n' +
   'video:229kB audio:81kB subtitle:0

Wondering if anyone has faced a similar issue or found how to handle this scenario.
Any help will be appreciated.

The option we use is listen_timeout=-1

1 Like

Hey @jbaudanza,

Thanks! I’m able to reconnect again.
However, when I reconnect, it takes about ~5mins to get the data to flow again.

ffmpeg::process::stderr::data [data:'    Last message repeated 5881 times\n' +
   'frame=  890 fps=6.0 q=-1.0 size=     768kB time=00:02:28.76 bitrate=  42.3kbits/s speed=1.01x    \r']
 ffmpeg::process::stderr::data [data:'[sdp @ 0x564a81a1f580] RTP: dropping old packet received too late\n']
 ffmpeg::process::stderr::data [data:'    Last message repeated 38 times\n' +
   'frame=  890 fps=6.0 q=-1.0 size=     768kB time=00:02:29.26 bitrate=  42.1kbits/s speed=1.01x    \r']
 ffmpeg::process::stderr::data [data:'[sdp @ 0x564a81a1f580] RTP: dropping old packet received too late\n']
 ffmpeg::process::stderr::data [data:'    Last message repeated 41 times\n' +
   'frame=  890 fps=6.0 q=-1.0 size=     768kB time=00:02:29.78 bitrate=  42.0kbits/s speed=1.01x    \r']
 ffmpeg::process::stderr::data [data:'[sdp @ 0x564a81a1f580] RTP: dropping old packet received too late\n']
 ffmpeg::process::stderr::data [data:'    Last message repeated 40 times\n' +
   'frame=  890 fps=6.0 q=-1.0 size=     768kB time=00:02:30.28 bitrate=  41.9kbits/s speed=1.01x    \r']
 ffmpeg::process::stderr::data [data:'[sdp @ 0x564a81a1f580] RTP: dropping old packet received too late\n']
 ffmpeg::process::stderr::data [data:'    Last message repeated 40 times\n' +
   'frame=  890 fps=6.0 q=-1.0 size=     768kB time=00:02:30.80 bitrate=  41.7kbits/s speed=1.01x    \r[sdp @ 0x564a81a1f580] RTP: dropping old packet received too late\n']
 ffmpeg::process::stderr::data [data:'    Last message repeated 8 times\n' +
   '[sdp @ 0x564a81a1f580] Missed a picture, sequence broken\n' +
   '[sdp @ 0x564a81a1f580] RTP: dropping old packet received too late\n']
 ffmpeg::process::stderr::data [data:'    Last message repeated 25 times\n' +
   'frame=  890 fps=5.9 q=-1.0 size=     768kB time=00:02:31.32 bitrate=  41.6kbits/s speed=1.01x    \r']
 ffmpeg::process::stderr::data [data:'[sdp @ 0x564a81a1f580] RTP: dropping old packet received too late\n']
 ffmpeg::process::stderr::data [data:'    Last message repeated 33 times\n' +
   'frame=  890 fps=5.9 q=-1.0 size=     768kB time=00:02:31.82 bitrate=  41.4kbits/s speed=1.01x    \r']
 ffmpeg::process::stderr::data [data:'[sdp @ 0x564a81a1f580] RTP: dropping old packet received too late\n']
 ffmpeg::process::stderr::data [data:'    Last message repeated 30 times\n' +
   'frame=  890 fps=5.9 q=-1.0 size=     768kB time=00:02:32.30 bitrate=  41.3kbits/s speed=1.01x    \r[sdp @ 0x564a81a1f580] RTP: dropping old packet received too late\n']
 ffmpeg::process::stderr::data [data:'    Last message repeated 34 times\n' +
   'frame=  890 fps=5.9 q=-1.0 size=     768kB time=00:02:32.82 bitrate=  41.2kbits/s speed=1.01x    \r[sdp @ 0x564a81a1f580] RTP: dropping old packet received too late\n']
 ffmpeg::process::stderr::data [data:'    Last message repeated 33 times\n' +
   'frame=  890 fps=5.9 q=-1.0 size=     768kB time=00:02:33.34 bitrate=  41.0kbits/s speed=1.01x    \r']
 ffmpeg::process::stderr::data [data:'[sdp @ 0x564a81a1f580] RTP: dropping old packet received too late\n']
 ffmpeg::process::stderr::data [data:'    Last message repeated 26 times\n' +
   'frame=  890 fps=5.8 q=-1.0 size=     768kB time=00:02:33.78 bitrate=  40.9kbits/s speed=1.01x    \r[sdp @ 0x564a81a1f580] RTP: dropping old packet received too late\n']
 ffmpeg::process::stderr::data [data:'    Last message repeated 37 times\n' +
   'frame=  890 fps=5.8 q=-1.0 size=     768kB time=00:02:34.34 bitrate=  40.8kbits/s speed=1.01x    \r']
 ffmpeg::process::stderr::data [data:'[sdp @ 0x564a81a1f580] RTP: dropping old packet received too late\n']
 ffmpeg::process::stderr::data [data:'    Last message repeated 31 times\n' +
   'frame=  890 fps=5.8 q=-1.0 size=     768kB time=00:02:34.84 bitrate=  40.6kbits/s speed=1.01x    \r']
 ffmpeg::process::stderr::data [data:'[sdp @ 0x564a81a1f580] RTP: dropping old packet received too late\n']

These are the logs, and when I try to play the file, the part gets skipped.
And then it starts writing again to the file which works fine. So basically theres a skip between the files.

I’ve also played with max_delay and reorder_queue_size options in ffmpeg but no luck.

On the mediasoup side, I’m closing the old transports/consumers when producers disconnect and recreate them when the producer joins back. Don’t think anything is wrong on this side.

Additionally, I’m thinking of saving files with t1 timestamp and subsequent files with t2 and so on producer reconnection, and then at the end of the recording, concat them all together with black A/V frame file. Do you think the above is the better approach?

Are you re-using the same ffmpeg session with different transports? I don’t think that will work, and wwould explain the “dropping old packets” message

This sounds like a more reasonable approach. Different producers should get stored in different files, and then mixed later, as needed.

For values for t1 and t2, you should use the start_time_realtime property from the input AVFormatContext, I’m not sure if this is accessible from the command line, but it’s definitely accessible from the C API. Alternatively, you could just get the timestamp from Date.now(), but start_time_realtime will use the high-resolution clock from the mediasoup worker process. This will be more precise.

Can you show me about ffmpeg command?