How to listen to 2 rtsp input streams simultaneously with FFMpeg

I can listen and receive one rtsp stream with the 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 a different thread with a different url) at the same time, I get an error:

Unable to open RTSP to listen rtsp: //example.com/in/2: address is already in use

It appears to be avformat_open_inputtrying to open a socket that has already been opened by a previous call avformat_open_input. Is there a way to share this socket between two threads? Maybe there is some kind of dispatcher in FFMpeg for such a task.

Important Note . In my case, my application should act as a listening server for incoming RTSP connections! This is not a client connecting to another RTSP server.

+4
3

FFserver, , , RTSP.

title .

... " 2 rtsp FFMpeg"

, FFmpeg, RTSP : http://ffmpeg.gusari.org/viewtopic.php?f=11&t=3246 (. 3 ).

, , rtsp:

:

ffmpeg -loop 1 -re -i ~/Desktop/background.png -rtsp_flags listen -timeout -1 -i rtsp://localhost:5001/live.mp4 -rtsp_flags listen -timeout -1 -i rtsp://localhost:5002/live.mp4 -filter_complex \
"[1:v] setpts=PTS-STARTPTS [left]; \
[2:v] setpts=PTS-STARTPTS [right]; \
[0:v][left]overlay=0:eof_action=pass:shortest=0 [bgleft]; \
[bgleft][right]overlay=w:eof_action=pass:shortest=0" ~/Desktop/test.mp4

:

ffmpeg -re -i ~/Desktop/normal.mp4 -f rtsp rtsp://localhost:5001/live.mp4
ffmpeg -re -i ~/Desktop/normal.mp4 -f rtsp rtsp://localhost:5002/live.mp4

, -. , . , ffmpeg test.mp4. , .

, FFmpeg , C, . .

+2

, , , , , NULL, . . , avformat_alloc_context , .

- , , , .

, ( , ), ffmpeg/libav cli, . , ffplay URL.

Edit: , URL- avformat_open_input, , , , , , , URL- . - RTSP- , ffmpeg? , ffmpeg .

, , .

0

You must check the video device information. The reason is that not every device can transmit more than one. There is a limitation. But most devices can transmit data on several channels, such as main_stream, channel_1, channel_2, mjpeg_stream, etc. So you can listen to different channels at the same time. This may solve your problem. PS: Channel names depend on the device manufacturer.

-2
source

Source: https://habr.com/ru/post/1677174/


All Articles