[Libav-user] SDP RTP using Custom I/O

Vitsas Nikolaos p3070011 at dias.aueb.gr
Thu Oct 3 13:34:55 CEST 2013

Hi guys.

I'm currently encoding a H264 stream into RTP packets using 
libavformat and storing them in memory using avio_open_dyn and 
avio_close_dyn. The thing is that those packets are going to reach a 
destination going through some intermediate peers using a custom P2P 
structure. Everything reaches the destination correctly and as 
expected, but I can' t get the custom I/O setup on the destination to 
work correctly so that the packets can be decoded.

Here' s what I am doing right now. sdp_desc holds the SDP description 
and custom_queue is a queue full of RTP Packets as encoded by the 

// C O D E
fmt_ctx = avformat_alloc_context();	
if(!fmt_ctx) {	
	return false;

io_buffer = 
BYTE* sdp_buffer = 

sdp_io_context = avio_alloc_context(sdp_buffer, strlen(sdp_desc), 0, 
nfo, &ReadFunc, NULL, NULL);
avio_open2(&sdp_io_context, "", AVIO_FLAG_READ, NULL, NULL);

         return false;

sdp_io_context->seekable = 0;
sdp_io_context->write_flag = 0;

strcpy((char*)sdp_buffer, sdp_desc);
AVProbeData probeData;
probeData.buf = sdp_buffer;
probeData.buf_size = strlen(sdp_desc);
probeData.filename = "";

fmt_ctx->pb = sdp_io_context;
fmt_ctx->flags = AVFMT_FLAG_CUSTOM_IO;
fmt_ctx->iformat = av_probe_input_format(&probeData, 1);

AVDictionary *options = NULL;
av_dict_set(&options, "sdpflags", "custom_io", 0);
int err = avformat_open_input(&fmt_ctx, "", NULL, &options);
//int err = avformat_open_input(&fmt_ctx, "", fmt_ctx->iformat, 

io_context = avio_alloc_context(io_buffer, IO_BUFFER_SIZE, 1, 
custom_queue, &ReadRTPFunc, &WriteRTPFunc, NULL);
avio_open2(&io_context, "", AVIO_FLAG_READ_WRITE, NULL, NULL);
	return false;
io_context->seekable = 0;

AVInputFormat* fr = av_find_input_format("rtp");

fmt_ctx->pb = io_context;
fmt_ctx->iformat = fr;
video_dec_ctx = fmt_ctx->streams[0]->codec;
video_dec = avcodec_find_decoder(video_dec_ctx->codec_id);
if (!video_dec) {
         printf("Failed to find %s codec\n",
         return false;

if ((ret = avcodec_open2(video_dec_ctx, video_dec, NULL)) < 0) {
         printf("Failed to open %s codec\n",
         return false;

thre ReadFunc looks like this
static int ReadFunc(void *opaque, uint8_t *buf, int buf_size) {
	char* sdp = (char*)opaque;

         strcpy((char*)buf, sdp);

         return strlen(sdp);

ReadFunc is called and the AVInputFormat in the AvFormatContext 
correctly gets initialized to SDP and the codec is correctly set to 
H264. However, ReadFunc is called again requesting more data and the 
only thing I can do, is return 0 or the same sdp data. Despite the 
above, av_open_input returns with no error but when I make the switch 
to the RTP AVIOContext, av_read_frame does not do anything and just 
returns after a while not calling any of my other callbacks.

Any help would be appreciated. Thank you.

More information about the Libav-user mailing list