Search code examples
audioffmpegudprtpmpeg2-ts

FFmpeg RTP_Mpegts over RTP protocol


I'm tryin to implement a client/server application based on FFmpeg. Unfortunately RTP_MPEGTS isn't documented in the official FFmpeg Documentation - Formats. Anyway i found inspiration from this old thread.

Server Side

(1) Capture mic audio as input. (2)Encode it as pcm 8khz mono and (3) send it locally as RTP_MPEGTS format over rtp protocol.

ffmpeg -f avfoundation -i none:2  -ar 8000 -acodec pcm_u8 -ac 1 -f rtp_mpegts rtp://127.0.0.1:41954
  • This works, but on initiation it alerts "[mpegts @ 0x7fda13024600] frame size not set"

Client Side (on the same machine)

(1) Receive rtp audio stream input (2) write it in a file or playback.

ffmpeg -i rtp://127.0.0.1:41954 -vcodec copy -y "output.wav"
  • I'm using -vcodec copy because i've already verified it in another rtp stream in which -acodec copy didn't work.
  • This stuck and while closing with Ctrl+C shortcut it prints:

    Input #0, rtp, from 'rtp://127.0.0.1:41954':
    Duration: N/A, start: 8.956122, bitrate: N/A
    Program 1 
    Metadata:
      service_name    : Service01
      service_provider: FFmpeg
    Stream #0:0: Data: bin_data ([6][0][0][0] / 0x0006)
    Output #0, wav, to 'output.wav':
    Output file #0 does not contain any stream
    

  1. I don't understand if the client didn't receive any stream, or it cannot write rtp packets into "output.wav" file. (Client or server problem?)
  2. In the old thread is explained a workaround. On server could run 2 ffmpeg instance: One produces "tmp.ts" file due to mpegts, and the other takes "tmp.ts" as input and streams it over rtp. Is it possibile?

  3. Is there any better way to do implement this client/server with the lowest latency possible?


Thanks for any help provided.


Solution

  • I tested this with an .aac file and it worked:

    Streaming:

    (notice I use a multicast address.

    But if you test the streaming and receiving on the same machine you might use your 127.0.0.1 as loopback address to the local host.)

    ffmpeg -f lavfi -i testsrc \
    -stream_loop -1 -re -i "music.aac" \
    -map 0:v -map 1:a \
    -ar 8000 -ac 1 \
    -f rtp_mpegts "rtp://239.1.1.9:1234"
    

    You need a video source for the rtp_mpegts muxer. I created one with lavfi.

    I used -stream_loop to loop the .aac file forever for my test. You don't need this with a mic as input.

    Capture stream:

    ffmpeg -y -i "rtp://239.1.1.9:1234" -c:a pcm_u8 "captured_stream.wav"
    

    I use the -c:a pcm_u8 while capturing on purpose, because using it in the Streaming did not work on the capturing side.

    The output is a low quality 8bit, 8kHz mono audio file but that was what you've asked for.