从UDP多播RTSP视频流中读取

hat*_*ero 1 udp ffmpeg multicast video-streaming

我目前正在开发一个需要解码UDP多播RTSP流的应用程序.目前,我可以使用ffplay via查看RTP流

ffplay -rtsp_transport udp_multicast rtsp://streamURLGoesHere
Run Code Online (Sandbox Code Playgroud)

但是,我试图使用FFMPEG来打开UDP流(为了简洁起见,错误检查和清除代码被删除).

AVFormatContext* ctxt = NULL;
av_open_input_file(
    &ctxt,
    urlString,
    NULL,
    0,
    NULL
);

av_find_stream_info(ctxt);

AVCodecContext* codecCtxt;

int videoStreamIdx = -1;
for (int i = 0; i < ctxt->nb_streams; i++)
{
    if (ctxt->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
    {
        videoStreamIdx = i;
        break;
    }
}

AVCodecContext* codecCtxt = ctxt->streams[videoStreamIdx]->codec;
AVCodec* codec = avcodec_fine_decoder(codecCtxt->codec_id);
avcodec_open(codecCtxt, codec);

AVPacket packet;
while(av_read_frame(ctxt, &packet) >= 0)
{
    if (packet.stream_index == videoStreamIdx)
    {
        /// Decoding performed here
        ...
    }
}

...
Run Code Online (Sandbox Code Playgroud)

这种方法适用于由原始编码视频流组成的文件输入,但对于UDP多播RTSP流,它无法执行任何错误检查av_open_input_file().请指教...

hat*_*ero 6

事实证明,打开多播UDP RTSP流可以通过以下方式执行:

AVFormatContext* ctxt = avformat_alloc_context();

AVDictionary* options = NULL;
av_dict_set(&options, "rtsp_transport", "udp_multicast", 0);
avformat_open_input(
    &ctxt,
    urlString,
    NULL,
    &options
);

...

avformat_free_context(ctxt);
Run Code Online (Sandbox Code Playgroud)

avformat_open_input()以这种方式使用而不是av_open_input_file()导致期望的行为.我猜这av_open_input_file()是被弃用的,或者从来没有打算以这种方式使用 - 更可能是后者;)