avformat_open_input超时问题

来源:互联网 发布:在香港用淘宝 编辑:程序博客网 时间:2024/06/13 06:51

fmpeg用avformat_open_input()解析网络流时,默认是阻塞的。

当遇到解析错误的网络流时,会导致该函数长时间不返回。

为此可以设置ffmpeg的-stimeout 的参数,要注意 -stimeout的单位是us 微妙。

用法就是设置在解析的 url 之前 (这里设置超时为5秒)即:

 “ffmpeg -stimeout 5000000  -i rtsp://admin:admin@192.168.7.102:554/h264...........”

或者:

“ffmpeg -stimeout 5000000 -rtsp_transport tcp  -i rtsp://admin:12345@172.16.7.166:554/h264.....”

 

备注:-stimeout 设置在你要解析的url之后是没有作用的。

=============================================

从网络上搜索到的有关资料:

一:

去看ffmpeg的tcp.c的源代码(会阻塞的网络地址基本都是tcp协议),搜索上面两个关键字,就明白是如何退出了。我之前的文章只说了open的时候阻塞,其实网络环境复杂,有可能在read或write时候断网或阻塞异常。但根基却是在tcp.c这个文件里。把interrupt_callback和timeout设置好了,就可以解决这些问题。

二:

ffmpeg的avformat_open_input()默认是阻塞的,用户可以通过设置“ic->flags |= AVFMT_FLAG_NONBLOCK;”设置成非阻塞(通常是不推荐的);或者是设置timeout设置超时时间;或者是设置interrupt_callback定义返回机制。

附一段代码参考下吧。

formatCtx = avformat_alloc_context();

    if (!formatCtx)

        returnLvErrorOpenFile;

    

    AVIOInterruptCB cb = {interrupt_callback,this};

    formatCtx->interrupt_callback = cb;

    //这个没什么用

    formatCtx->flags |=AVFMT_FLAG_NONBLOCK;

//主要是这个!!设置Opts

    AVDictionary* opts =NULL;

    av_dict_set(&opts,"timeout","6000",0);

    

    if (avformat_open_input(&formatCtx,

                            path.c_str(),

                            NULL,

                            &opts) < 0) {

        

        if (formatCtx)

            avformat_free_context(formatCtx);

        returnLvErrorOpenFile;

    }



0 0
原创粉丝点击