How to listen to 2 incoming rtsp streams at the same time with FFMpeg

Viewed 4648

I can listen and receive one rtsp stream with FFMpeg library using this code:

AVFormatContext* format_context = NULL
char* url = "rtsp://example.com/in/1";
AVDictionary *options = NULL;
av_dict_set(&options, "rtsp_flags", "listen", 0);
av_dict_set(&options, "rtsp_transport", "tcp", 0);

int status = avformat_open_input(&format_context, url, NULL, &options);
av_dict_free(&options);
if( status >= 0 )
{
    status = avformat_find_stream_info( format_context, NULL);
    if( status >= 0 )
    {
        AVPacket av_packet;
        av_init_packet(&av_packet);

        for(;;)
        {                                                                      
            status = av_read_frame( format_context, &av_packet );
            if( status < 0 )
            {
                break;
            }
        }
    }
    avformat_close_input(&format_context);
}

But if I try to open another similar listener (in another thread with another url) at the same time, I get error:

Unable to open RTSP for listening rtsp://example.com/in/2: Address already in use

It looks like avformat_open_input tries to open socket which is already opened by previous call of avformat_open_input. Is there any way to share this socket between 2 threads? May be there is some dispatcher in FFMpeg for such task.

Important Note: In my case my application must serve as a listen server for incoming RTSP connections! It is not a client connecting to another RTSP server.

3 Answers
Related