ffmpeg拉流长时间堵塞解决方式

由于网络堵塞或者推流端错误导致拉流端没有流数据,ffmpeg主要会堵塞两个函数,直到下次流数据的到来

  1. avformat_open_input()
    该函数是在打开流数据时,如果没有这个流的ip,http有自己的timeout,当链接失败,ffmpeg会自动断开.但是如果有这个ip,但是无法链接,就会堵塞,解决方式是添加超时控制.
    函数在ffmpeg源码的ffmpeg_opt.c文件中,
    ffmpeg拉流长时间堵塞解决方式
    我设置了3秒超时时间,添加如下代码
    av_dict_set(&o->g->format_opts, "rw_timeout", "3000000", 0);

  2. av_read_frame()
    该函数是链接成功后,由于网络堵塞或者其它问题导致packet丢失,无法读取,导致堵塞,
    函数在ffmpeg.c文件中,解决方式也是添加超时
    `f->ctx->interrupt_callback.callback = CheckInterrupt;

    f->ctx->interrupt_callback.opaque = (void*)f->ctx;

    time_t start_time;

    start_time = time(NULL);

    int l = time(&start_time);

    f->ctx->st = l;

    return av_read_frame(f->ctx, pkt);

`

下面是回调函数:

static int CheckInterrupt(void *ctx) { AVFormatContext *p = (AVFormatContext*)ctx; time_t et; et = time(NULL); int l = time(&et); av_log(NULL, AV_LOG_WARNING,"start time%d\n",p->st); av_log(NULL, AV_LOG_WARNING,"end time%d\n",l); return l - p->st >= 10 ? 1 : 0;//3秒超时 }
需要注意的是,我在f->ctx结构体中添加了st变量,由时间戳作为评判超时的依据,需要把变量类型统一,所以需要添加变量如下:
在avformat.h文件的AVFormatContext结构体中添加:
int st;

上一篇:av_dump_format函数使用说明


下一篇:ffmpeg中c语言sdk多媒体互转主要使用的api