为了做ffmpeg的断线重连,做的一些关于api的特性测试

822 阅读1分钟
  • 1.下面函数的行为是确定的,相机不在线的话会立即返回不会阻塞
// 打开不成功,(目前测试效果)返回-138
int ret = avformat_open_input(&format_ctx, url.c_str(), nullptr, &options );  
  • 2.相机掉线,这个函数默认会阻塞,可设置超时时间
int ret = av_read_frame(format_ctx, packet);

网友提供了如下几种设置,前一种有效

// 一种
AVDictionary* options;
options = nullptr;
av_dict_set(&options, "rtsp_transport", "tcp", 0);
av_dict_set(&options, "stimeout", timeout_us_str.c_str(), 0);

// 另一种,实际无效
// av_read_frame() will not be block; actually this set doesn't work, av_read_frame() will be block
AVFormatContext* format_ctx = avformat_alloc_context();
format_ctx->flags |= AVFMT_FLAG_NONBLOCK;