ffmpeg超时时间

需要解决的问题

av_read_frame的过程中相机断开连接导致读取码流一直死等

解决方法1

打开流媒体之前注册FFmpeg回调函数

AVFormatContext *m_pRtspFmt = avformat_alloc_context();
 
m_pRtspFmt->interrupt_callback.callback = AVInterruptCallBackFun;
m_pRtspFmt->interrupt_callback.opaque = this;

回调函数类型为:

typedef struct AVIOInterruptCB {int (*callback)(void*);void *opaque;} AVIOInterruptCB;

回调函数中返回1则代表ffmpeg结束阻塞可以将操纵权交给用户线程并返回错误码

回调函数中返回0则代表ffmpeg继续阻塞直到ffmpeg正常工作为止

所以要退出死等则需要返回1

实例

AVCodec         *pCodec;
// Register all formats and codecs
av_register_all();
avcodec_register_all();
avformat_network_init();

pFormatCtx = avformat_alloc_context();
pFormatCtx->interrupt_callback.callback = interrupt_cb;--------注册回调函数
pFormatCtx->interrupt_callback.opaque = pFormatCtx;------回调函数的参数,可以根据需求改
AVDictionary* options = NULL;
av_dict_set(&options, "rtsp_transport", "tcp", 0);


if(avformat_open_input(&pFormatCtx, [url cStringUsingEncoding:NSASCIIStringEncoding], 0, &options) != 0) {
    av_log(NULL, AV_LOG_ERROR, "Couldn't open file\n");
    goto initError;
 }



static int interrupt_cb(void *ctx)
{
    // do something
    NSLog(@"%d",time_out);
    time_out++;
    if (time_out > 40) {
        NSLog(@"------%d", firsttimeplay);
        time_out=0;
        if (firsttimeplay) {
            firsttimeplay=0;
            NSLog(@"++++++++");
            return 1;//这个就是超时的返回
        }
    }
    return 0;
}

方法2

AVFormatContext *pAVFormatContext = avformat_alloc_context();//申请一个AVFormatContext结构的内存,并进行简单初始化
AVDictionary* options = NULL;
av_dict_set(&options, "buffer_size", "102400", 0); //设置缓存大小,1080p可将值调大
av_dict_set(&options, "rtsp_transport", "tcp", 0); //以udp方式打开,如果以tcp方式打开将udp替换为tcp
av_dict_set(&options, "stimeout", "2000000", 0); //设置超时断开连接时间,单位微秒
av_dict_set(&options, "max_delay", "500000", 0); //设置最大时延

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×