c - 如何使用 FFMpeg 同时收听 2 个传入的 rtsp 流

标签 c ffmpeg rtsp

我可以使用这段代码使用 FFMpeg 库收听和接收一个 rtsp 流:

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);
}

但是如果我尝试同时打开另一个类似的监听器(在另一个线程中使用另一个 url),我会得到错误:

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

看起来 avformat_open_input 试图打开之前调用 avformat_open_input 已经打开的套接字。有没有办法在两个线程之间共享这个套接字?可能在 FFMpeg 中有一些调度程序来完成这样的任务。

重要说明:在我的例子中,我的应用程序必须充当传入 RTSP 连接的监听服务器!它不是连接到另一个 RTSP 服务器的客户端。

最佳答案

你应该看看FFserver如果您希望您的应用充当服务器,监听发送数据到多个传入的 RTSP 连接。

下面尝试提供有用的资源信息来解决问题的标题

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

这个在 FFmpeg 论坛上提问的人设法从命令行上的两个 RTSP 流接收数据:http://ffmpeg.gusari.org/viewtopic.php?f=11&t=3246 (见 3 张图片后的文字)。

What I already achieved is to receive two streams via rtsp:

Server code :

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

and I faked two stream clients with :

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

Well it's working somehow. The Server starts and it's waiting for incoming connections. When both clients are connected, the ffmpeg server puts the streams together and outputs them in test.mp4. If one client stops, the red background appears and the video is continuing.

不幸的是,我只在命令行中使用 FFmpeg,而不是作为 C 库,因此无法提供代码。但这只是访问相同功能的不同方式。

关于c - 如何使用 FFMpeg 同时收听 2 个传入的 rtsp 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43979720/

相关文章:

google-chrome - 既然不再支持 VLC 插件,如何在 Firefox 和 Chrome 中流式传输 RTSP 实时视频?

c++ - 调用 shmdt() 后无法删除共享内存段

c - 在c中,值在函数调用后从函数末尾更改到函数外部

java - 传递常量会自动通过引用传递吗?

ffmpeg - 使用 FFmpeg 设置时间偏移后覆盖视频

android - 在android中使用RTSP的视频流

c - 总线错误,不知道该怎么办

python - 从 ffmpeg.input() 对象获取输入文件名

java - 使用 FFMPEG 命令时出现 Android 错误 - 打开输出流编码器时出错 - 可能参数不正确,例如 bit_rate 等

video - GStreamer管道以显示RTSP流