Unable to get mpegts with klv-metadata to flow to end of pipe from udpsrc

421 views Asked by At

I've been fighting with this problem for quite a while now. I have mpegts stream with klv-metadata coming from udpsrc and the bellow gstreamer command to handle it and to pass it through to rtspclientsink.

   gst-launch-1.0 -e udpsrc address=224.1.1.1 reuse=true port=50088 socket-timestamp=1 buffer-size=100000000  ! 'video/mpegts, systemstream=(boolean)true, packetsize=(int)188' !
        queue max-size-time=1700000000 max-size-buffers=50 ! tsparse set-timestamps=true ! 
        tsdemux parse-private-sections=true ignore-pcr=true name=demux
        demux. ! queue max-size-time=0 max-size-buffers=0 ! decodebin use-buffering=true ! videoconvert ! videorate ! videoscale ! x264enc tune=zerolatency bitrate=5500 key-int-max=30 ! mpegtsmux name=mux
        demux. ! queue max-size-time=0 max-size-buffers=0 ! meta/x-klv ! mux. mux. ! rtspclientsink location=rtsp://127.0.0.1:8554/testpath 

If I switch the udpsrc to a filesrc (https://samples.ffmpeg.org/MPEG2/mpegts-klv/ <-- either of the files should work), the whole pipe works just fine.

But after I switch it to udpsrc, I only get a few frames worth of klv-data after EOS, other times I keep getting "WARNING: from element /GstPipeline:pipeline0/GstTSDemux:demux: CONTINUITY: Mismatch packet" .

I've used below ffmpeg command to forward the rtsp back to udp

ffmpeg -i rtsp://127.0.0.1:8554/testpath -map 0 -c copy -f mpegts udp://224.1.1.1:50068

and then I have used bellow command to listen to above udp forwarding.

gst-launch-1.0 -v -e udpsrc address=224.1.1.1 reuse=true port=50068 ! 'video/mpegts, systemstream=(boolean)true, packetsize=(int)188' ! tsdemux name=demux demux. ! queue ! h264parse ! 'video/x-h264, stream-format=byte-stream, alignment=au' ! avdec_h264 ! autovideosink demux. ! queue ! 'meta/x-klv' ! fdsink

And like I said the whole flow works with filesrc in the beginning, but with udpsrc it doesn't.

All help is much appreciated. Thanks already.

Edit: My assumption is that there's lot less klv-data in the ffmpeg files than the file I'm using (which I can't share), that the fdsink pipe doesn't work with it. But managed to write the data to a file using following

gst-launch-1.0 -e udpsrc address=224.1.1.1 port=50088 ! tsdemux name=demux demux. ! queue ! meta/x-klv ! filesink location=klv_output_night.bin

ffprobe from the actual udp stream

Input #0, mpegts, from 'udp://<multicast-addr:port>':
  Duration: N/A, start: 288.961000, bitrate: N/A
  Program 1
    Metadata:
      service_name    : Service01
      service_provider: FFmpeg
  Stream #0:0[0x100]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p(progressive), 1280x722, 90k tbr, 90k tbn
  Stream #0:1[0xfc]: Data: klv (KLVA / 0x41564C4B)
Unsupported codec with id 98311 for input stream 1

And 10 second analyzis from the stream made with tsduck.

===============================================================================
|  TRANSPORT STREAM ANALYSIS REPORT                                           |
|=============================================================================|
|  Transport Stream Id: .......... 1 (0x0001)  |  Services: .............. 1  |
|  Bytes: ......................... 6,549,920  |  PID's: Total: .......... 5  |
|  TS packets: ....................... 34,840  |         Clear: .......... 5  |
|     With invalid sync: .................. 0  |         Scrambled: ...... 0  |
|     With transport error: ............... 0  |         With PCR's: ..... 1  |
|     Suspect and ignored: ................ 0  |         Unreferenced: ... 0  |
|-----------------------------------------------------------------------------|
|  Transport stream bitrate, based on ....... 188 bytes/pkt    204 bytes/pkt  |
|  User-specified: .......................... 5,185,598 b/s    5,626,926 b/s  |
|  Estimated based on PCR's: ................ 5,185,598 b/s    5,626,926 b/s  |
|  Selected reference bitrate: .............. 5,185,598 b/s    5,626,926 b/s  |
|-----------------------------------------------------------------------------|
|  Broadcast time: ................................... 10 sec (0 min 10 sec)  |
|-----------------------------------------------------------------------------|
|  Srv Id  Service Name                              Access          Bitrate  |
|  0x0001  Service01 .................................... C    5,034,525 b/s  |
|                                                                             |
|  Note 1: C=Clear, S=Scrambled                                               |
|  Note 2: Unless specified otherwise, bitrates are based on 188 bytes/pkt    |
===============================================================================

===============================================================================
|  SERVICES ANALYSIS REPORT                                                   |
|=============================================================================|
|  Global PID's                                                               |
|  TS packets: 1,015, PID's: 2 (clear: 2, scrambled: 0)                       |
|-----------------------------------------------------------------------------|
|     PID  Usage                                     Access          Bitrate  |
|   Total  Global PID's ................................. C      151,073 b/s  |
|   Subt.  Global PSI/SI PID's (0x00-0x1F) .............. C      151,073 b/s  |
|  0x0000  PAT .......................................... C      126,514 b/s  |
|  0x0011  SDT/BAT ...................................... C       24,559 b/s  |
|=============================================================================|
|  Service: 0x0001 (1), TS: 0x0001 (1), Original Netw: 0xFF01 (65281)         |
|  Service name: Service01, provider: FFmpeg                                  |
|  Service type: 0x01 (Digital television service)                            |
|  TS packets: 33,825, PID's: 3 (clear: 3, scrambled: 0)                      |
|  PMT PID: 0x1000 (4096), PCR PID: 0x0100 (256)                              |
|-----------------------------------------------------------------------------|
|     PID  Usage                                     Access          Bitrate  |
|   Total  Digital television service ................... C    5,034,525 b/s  |
|  0x00FC  MPEG-2 PES private data ...................... C        2,977 b/s  |
|  0x0100  AVC video (1280x722, high profile, level 4.0,  C    4,905,034 b/s  |
|  0x1000  PMT .......................................... C      126,514 b/s  |
|          (C=Clear, S=Scrambled, +=Shared)                                   |
===============================================================================

===============================================================================
|  PIDS ANALYSIS REPORT                                                       |
|=============================================================================|
|  PID: 0x0000 (0)                                                       PAT  |
|-----------------------------------------------------------------------------|
|  Global PID                Transport:                Discontinuities:       |
|  Bitrate: ... 126,514 b/s  Packets: ........... 850  Expected: ......... 0  |
|  Access: .......... Clear  Adapt.F.: ............ 0  Unexpect: ......... 0  |
|                            Duplicated: .......... 0  Sections:              |
|                                                      Unit start: ..... 850  |
|=============================================================================|
|  PID: 0x0011 (17)                                                  SDT/BAT  |
|-----------------------------------------------------------------------------|
|  Global PID                Transport:                Discontinuities:       |
|  Bitrate: .... 24,559 b/s  Packets: ........... 165  Expected: ......... 0  |
|  Access: .......... Clear  Adapt.F.: ............ 0  Unexpect: ......... 0  |
|                            Duplicated: .......... 0  Sections:              |
|                                                      Unit start: ..... 165  |
|=============================================================================|
|  PID: 0x00FC (252)                                 MPEG-2 PES private data  |
|  PES stream id: 0xFC (MPEG-7 metadata stream)                               |
|  Service: 0x0001 (1) Service01                                              |
|-----------------------------------------------------------------------------|
|  Single Service PID        Transport:                Discontinuities:       |
|  Bitrate: ..... 2,977 b/s  Packets: ............ 20  Expected: ......... 0  |
|  Access: .......... Clear  Adapt.F.: ........... 10  Unexpect: ......... 0  |
|                            Duplicated: .......... 0  PES:                   |
|                                                      Packets: ......... 10  |
|                                                      Inv.Start: ........ 0  |
|  Clock values range:                                                        |
|                            PTS: ................ 10                         |
|                            from ................. 0                         |
|                            to ................... 0                         |
|                            Leaps: ............... 0                         |
|=============================================================================|
|  PID: 0x0100 (256)                                               AVC video  |
|  PES stream id: 0xE0 (Video 0)                                              |
|  1280x722, high profile, level 4.0, 4:2:0                                   |
|  Service: 0x0001 (1) Service01                                              |
|-----------------------------------------------------------------------------|
|  Single Service PID        Transport:                Discontinuities:       |
|  Bitrate: . 4,905,034 b/s  Packets: ........ 32,955  Expected: ......... 0  |
|  Access: .......... Clear  Adapt.F.: .......... 353  Unexpect: ......... 0  |
|                            Duplicated: .......... 0  PES:                   |
|                            TSrate: .. 5,185,598 b/s  Packets: ........ 304  |
|                                                      Inv.Start: ........ 0  |
|  Clock values range:                                                        |
|  PCR: ................ 51  PTS: ............... 304                         |
|  from ....... 316,791,000  from ......... 1,052,910                         |
|  to ......... 584,766,000  to ........... 1,955,160                         |
|  Leaps: ............... 0  Leaps: ............... 0                         |
|=============================================================================|
|  PID: 0x1000 (4096)                                                    PMT  |
|  Service: 0x0001 (1) Service01                                              |
|-----------------------------------------------------------------------------|
|  Single Service PID        Transport:                Discontinuities:       |
|  Bitrate: ... 126,514 b/s  Packets: ........... 850  Expected: ......... 0  |
|  Access: .......... Clear  Adapt.F.: ............ 0  Unexpect: ......... 0  |
|                            Duplicated: .......... 0  Sections:              |
|                                                      Unit start: ..... 850  |
===============================================================================

===============================================================================
|  TABLES & SECTIONS ANALYSIS REPORT                                          |
|=============================================================================|
|  PID: 0x0000 (0)                                                       PAT  |
|-----------------------------------------------------------------------------|
|  0x00 (0, PAT), TID ext: 0x0001 (1)                                         |
|      Repetition: ...... 12  ms  Section cnt: ...... 850                     |
|      Min repet.: ....... 1  ms  Table cnt: ........ 850                     |
|      Max repet.: ...... 12  ms  Version: ............ 0                     |
|=============================================================================|
|  PID: 0x0011 (17)                                                  SDT/BAT  |
|-----------------------------------------------------------------------------|
|  0x42 (66, SDT Actual), TID ext: 0x0001 (1)                                 |
|      Repetition: ...... 61  ms  Section cnt: ...... 165                     |
|      Min repet.: ...... 61  ms  Table cnt: ........ 165                     |
|      Max repet.: ...... 62  ms  Version: ............ 0                     |
|=============================================================================|
|  PID: 0x1000 (4096)                                                    PMT  |
|  Service: 0x0001 (1) Service01                                              |
|-----------------------------------------------------------------------------|
|  0x02 (2, PMT), TID ext: 0x0001 (1)                                         |
|      Repetition: ...... 12  ms  Section cnt: ...... 850                     |
|      Min repet.: ....... 1  ms  Table cnt: ........ 850                     |
|      Max repet.: ...... 12  ms  Version: ............ 0                     |
===============================================================================
1

There are 1 answers

5
Wayne On

There are 2 defined formats for KLV data in MPEG-TS streams Asynchronous and Synchronous. Current gstreamer-plugins tsdemux do not support Synchronous. (I am using v1.16.3, and I believe this is still true as of v1.22)

The test files you post contain Asynchronous KLV.

PID: 0x01F1 (497) MPEG-2 PES private data | PES stream id: 0xBD

stream_id: 0xBD implies Async KLV. Both of those streams can be processed by gstreamer using filesrc or streamed over udp and ingested with udpsrc. This pipeline works:

gst-launch-1.0 -v udpsrc port=$STREAM_PORT ! tsdemux name=demux demux. ! queue ! 'video/x-h264' ! filesink location="video.ts" demux. ! queue ! 'meta/x-klv' ! filesink buffer-mode=2 location="klv.ts"

The tsduck output you post for your UDP stream indicates Synchronous KLV.

PID: 0x00FC (252) MPEG-2 PES private data PES stream id: 0xFC

stream id: 0xFC implies Synchronous KLV which current tsdemux does not support. There is a very old patch out there from Michael Fien to add Sync KLV to tsdemux:

https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/-/issues/642

I expect the code has evolved over time, so don't know how difficult it is to apply to current tsdemux code. I also don't know how robust it is. I am working on this myself, but have very limited time for my project.

Another option is to use ffmpeg. It seems to work on Sync KLV streams:

ffmpeg -i udp://$STREAM_IP:$STREAM_PORT -map 0:0 -codec copy -f data "video.ts" -map 0:1 -codec copy -f data "klv.ts"