FFmpeg passthrough of MPEG transport stream causes errors&glitches in stream

6,846

So apparently my issue was the -re flag in the command that starts the pass-through FFmpeg instance.

If I had looked into the official documentation beforehand, I wouldn't have had to ask this question:

-re (input)

Read input at native frame rate. Mainly used to simulate a grab device, or live input stream (e.g. when reading from a file). Should not be used with actual grab devices or live input streams (where it can cause packet loss).

http://ffmpeg.org/ffmpeg.html#Advanced-options

Which is exactly what happened to me.

Share:
6,846
UnbescholtenerBuerger
Author by

UnbescholtenerBuerger

comm engineer, hobby tinkerer, sport addict. I watched some youtube videos and am now an expert in signal processing, strength training, martial arts, mountain biking, software development, and watching youtube videos.

Updated on September 18, 2022

Comments

  • UnbescholtenerBuerger
    UnbescholtenerBuerger over 1 year

    Unbescholtener Bürger back with another FFmpeg question. My goal is to have an instance of FFmpeg running that receives a mpeg transport stream via RTP, executes arbitrary operations on the received stream like transcoding or filtering, then passes the altered transport stream on via RTP.

    However, I don't get it to work in even the most basic configuration: I have a .ts file that contains a single program consisting of one video and one audio stream. I use an instance of FFmpeg to stream this file to localhost:

    ffmpeg -re -i 1.ts -c copy -f rtp_mpegts rtp://127.0.0.1:5003
    

    With ffplay, I validate that this works correctly:

    ffplay -i rtp://127.0.0.1:5003
    

    Result looks good, sounds good, doesn't cause error messages on the console.

    Now I try to put in another FFmpeg instance in between and things go downhill. I just want FFmpeg to copy the stream from port 5003 to port 5005 without any further processing or transcoding:

    ffmpeg -re -probesize 50M -analyzeduration 50M -i rtp://127.0.0.1:5003?fifo_size=10000 -c copy -f rtp_mpegts rtp://127.0.0.1:5005
    

    I get a lot of recurring error messages on that console, and when I monitor the stream at port 5005 with ffplay, lots of artifacts, glitches and dropouts appear. The console output looks like this for the most part:

    ...
    Input #0, rtp, from 'rtp://127.0.0.1:5003?fifo_size=10000':
      Duration: N/A, start: 1.400022, bitrate: N/A
      Program 1 
        Metadata:
          service_name    : Service01
          service_provider: FFmpeg
        Stream #0:0: Video: h264 (Constrained Baseline) ([27][0][0][0] / 0x001B), yuv420p(progressive), 352x240 [SAR 1:1 DAR 22:15], 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
        Stream #0:1: Audio: ac3 ([129][0][0][0] / 0x0081), 44100 Hz, stereo, fltp, 192 kb/s
    Output #0, rtp_mpegts, to 'rtp://127.0.0.1:5005':
      Metadata:
        encoder         : Lavf58.2.102
        Stream #0:0: Video: h264 (Constrained Baseline) ([27][0][0][0] / 0x001B), yuv420p(progressive), 352x240 [SAR 1:1 DAR 22:15], q=2-31, 29.97 fps, 29.97 tbr, 90k tbn, 29.97 tbc
        Stream #0:1: Audio: ac3 ([129][0][0][0] / 0x0081), 44100 Hz, stereo, fltp, 192 kb/s
    Stream mapping:
      Stream #0:0 -> #0:0 (copy)
      Stream #0:1 -> #0:1 (copy)
    Press [q] to stop, [?] for help
    frame=    0 fps=0.0 q=-1.0 size=       0kB time=00:00:00.48 bitrate=   0.0kbits/s speed=0.957x
    frame=    0 fps=0.0 q=-1.0 size=       0kB time=00:00:01.01 bitrate=   0.0kbits/s speed=0.991x
    frame=    0 fps=0.0 q=-1.0 size=       0kB time=00:00:01.53 bitrate=   0.0kbits/s speed=1.01x 
    frame=    1 fps=0.5 q=-1.0 size=      57kB time=00:00:02.05 bitrate= 227.6kbits/s speed=1.01x 
    frame=   17 fps=6.7 q=-1.0 size=      83kB time=00:00:02.54 bitrate= 267.6kbits/s speed=   1x 
    frame=   32 fps= 11 q=-1.0 size=     121kB time=00:00:03.04 bitrate= 324.9kbits/s speed=   1x 
    [rtp @ 0x3db64c0] max delay reached. need to consume packet
    [rtp @ 0x3db64c0] RTP: missed 50 packets
    [rtp @ 0x3db64c0] PES packet size mismatch
    frame=   37 fps= 10 q=-1.0 size=     132kB time=00:00:04.07 bitrate= 266.0kbits/s speed=1.15x frame=   37 fps=9.1 q=-1.0 size=     132kB time=00:00:04.07 bitrate= 266.0kbits/s speed=1.01x 
    [rtp @ 0x3db64c0] max delay reached. need to consume packet
    [rtp @ 0x3db64c0] RTP: missed 1 packets
    [rtp @ 0x3db64c0] PES packet size mismatch
    [rtp @ 0x3db64c0] max delay reached. need to consume packet
    [rtp @ 0x3db64c0] RTP: missed 1 packets
    frame=   47 fps= 10 q=-1.0 size=     150kB time=00:00:04.57 bitrate= 269.3kbits/s speed=1.01x
    ...
    

    So... any idea what went wrong and how to fix it? A glance at the resource monitor doesn't indicate any high cpu, memory or network load.

    • Admin
      Admin over 6 years
      Can you provide an example .ts file?
    • UnbescholtenerBuerger
      UnbescholtenerBuerger over 6 years
      I could, but it wouldn't be legal I think. I downloaded Bronski Beat - Smalltown Boy from YT and used FFmpeg to convert it to .ts. Anyways, that doesn't matter because I actually found the culprit.