ffmpeglibavlibavcodeclibavformat

Demuxing and decoding raw RTP with libavformat


I'm implementing a pipeline where I receive inbound RTP packets in memory but I'm having trouble figuring out how to set up libavformat to handle/unwrap the RTP packets.

I have all relevant information about the underlying codec necessary but since it's h264 I cannot simply strip the RTP header trivially. I create the input context with goInputFunction writing one packet per invocation.

void *readbuf = av_malloc(1500);
AVIOContext *avioreadctx = avio_alloc_context(readbuf, 1500, 0, transcoder, &goInputFunction, NULL, NULL);
AVFormatContext *inputctx = avformat_alloc_context();
inputctx->pb = avioreadctx;
inputctx->flags |= AVFMT_FLAG_CUSTOM_IO;

When I open it with avformat_open_input(&inputctx, NULL, NULL, NULL) it repeatedly calls the read function but doesn't actually progress. I suspect because the RTP stream itself does not have enough information to fully describe the codec? If I leave this open out, then av_read_frame(inputctx, input_packet) down the road segfaults, I'm guessing because the input context is uninitialized.

So to my question, is it possible to set the codec details that the SDP would typically set, but manually?

I'm looking for an example of how to manually configure the AVFormatContext to consume RTP packets without an SDP and setting up a UDP port listener.


Solution

  • Turns out this is possible through the use of an undocumented flag, FLAG_RTSP_CUSTOM_IO. I've detailed how to do that here: https://blog.kevmo314.com/custom-rtp-io-with-ffmpeg.html