解决方法
1.增加解码线程调用子线程数量
这是由于处理的机器性能低,导致播放流的速度大于解码速度,
怎么解决这种问题呢,就是提高解码速度,采取的就是增加解码的线程,
参考文章
FFmpeg 解码 avcodec_find_decoder AVCodecContext
FFmpeg(8)-打开音视频解码器,配置解码器上下文(avcodec_find_decoder()、avcodec_alloc_context3())
具体来说
int avcodec_open2(AVCodecContext *avctx, const AVCodec
*codec, AVDictionary **options) 打开 options动态设置 多线程解码设置
• /libavcodec/options_table.h
• int thread_count CPU数量设置
• time_base 时间基数
中可以设置CPU数量,那么应该怎么做呢
设置调用的CPU线程数量
// 注册解码器
avcodec_register_all();
AVCodec *vc = avcodec_find_decoder(ic->streams[videoStream]->codecpar->codec_id); // 软解
// vc = avcodec_find_decoder_by_name("h264_mediacodec"); // 硬解
if (!vc) {
LOGE("avcodec_find_decoder[videoStream] failure");
return env->NewStringUTF(hello.c_str());
}
// 配置解码器
AVCodecContext *vct = avcodec_alloc_context3(vc);
avcodec_parameters_to_context(vct, ic->streams[videoStream]->codecpar);
vct->thread_count = 1;//设置CPU线程数量
// 打开解码器
int re = avcodec_open2(vct, vc, 0);
if (re != 0) {
LOGE("avcodec_open2 failure");
return env->NewStringUTF(hello.c_str());
}
2.将网络接收的缓冲设大
参考博客FFmpeg 获取RTSP传过来的视频数据并保存成文件
AVDictionary* options = NULL;
av_dict_set(&options, "buffer_size", "102400", 0); //设置缓存大小,1080p可将值调大
//打开网络流或文件流
if (avformat_open_input(&pFormatCtx, filepath, NULL, &options) != 0)
{
printf("Couldn't open input stream.\n");
return;
}
3.减少接受线程工作量
参考博客[FFmpeg 获取RTSP传过来的视频数据并保存成文件]
开一条线程给RTSP接收函数,该线程只负责接收,不做其他的工作。这样基本上就能保证数据被及时处理。
4.如果使用ros接收,增加接收的队列长度
ros::Subscriber image_sub = node.subscribe(h265_topic_, 10, &TrackerProcessTest::H265TopicCallback, this);
这里将接收长度改为10
设置Subscriber的queue_size(消息队列大小),保证数据不丢失