ffmpeg用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定义返回机制。

附一段代码参考下吧。

    ic = avformat_alloc_context();
    if(ic == NULL){
        ERR("error avformat_alloc_context \n");
        return -1;
    }
    ic->interrupt_callback.callback = tvie_decode_interrupt_cb;
    ic->interrupt_callback.opaque = handle;


    //ic->flags |= AVFMT_FLAG_NONBLOCK;


    if(strncmp(handle->params.path, "rtmp:", sizeof("rtmp:")) == 0){
        av_dict_set(&opts, "timeout", "6", 0); // in secs


    }
    else if(strncmp(handle->params.path, "http:", sizeof("http:")) == 0){
        av_dict_set(&opts, "timeout", "6000", 0); // in ms
    }

    err = avformat_open_input(&ic, handle->params.path, NULL, &opts);

posted on 2015-01-12 14:34  瓦楞球  阅读(21081)  评论(1编辑  收藏  举报