本文讨论的播放器是基于FFmpeg实现的播放器,使用场景为直播场景。由于直播场景对实时性要求比较高,接下来主要从播放延迟和CPU使用率方面进行优化。
原创文章,欢迎转载.转载请注明出处: https://www.jianshu.com/p/f045731629b3
一、播放延迟优化
造成播放延迟的因素比较多,本文主要从解码延迟入手分析。从收到视频数据第一帧到解码出第一帧所需要的时间。经自测发现主要有以下3种原因导致解码延迟:
- FFmpeg解码器延迟:
FFmpeg解码器默认是有缓冲区的,实测会缓存6帧(个别低端机型缓存2帧)。以帧率为15,缓存6帧为例,会造成1000ms / 15fps * 6帧 = 400ms
的延迟。
- 解决方式:
可以设置FFmpeg解码器参数,设置为低延迟即可。此方式只适用于GOP中不存在B帧的情况(只有I帧和P帧)。开启解码器低延迟的代码如下:
// 解码器低延迟
pDecoderContext->flags |= AV_CODEC_FLAG_LOW_DELAY;
- 首帧非I帧:
如果首帧非I帧,解码器就会解码失败直到遇到下一个I帧才能解码成功。假如GOP的大小为60,帧率为15,会造成60 / 15fps * 1000ms = 4000ms = 4s
的延迟。
- 解决方式:
需要服务端保证给到客户端的视频流首帧是I帧。
- CPU使用率过高:
CPU使用率过高会造成卡顿;由于软解码主要是利用CPU运算,CPU的使用率过高会造成软解码特别的缓慢,出现播放帧率降低、卡顿的情况,解码延迟自然会增加。
- 解决方式:
(1)降低CPU使用率;(2)将软解替换为硬解。(软解本身就比较耗CPU,将软解替换为硬解可以大大降低CPU使用率。)
二、CPU使用率优化
造成CPU使用率过高的主要因素是软解码需要大量的CPU计算,而且帧率和分辨率越高计算量越大。接下来主要是介绍如何将软解码替换为硬解码。主要有两种方式:
- 使用苹果自带的VideoToolbox.framework原生实现:
该方式对iOS开发者来说相对简单,可以查阅相关资料,本文不做介绍。 - 使用FFMpeg跨平台实现:
在iOS平台下FFMpeg实现硬解码的API底层也是基于VideoToolbox.framework实现的。本文介绍的播放器本身就是基于FFMpeg实现,为了使代码改动最小,所以决定基于FFMpeg实现硬解码。实现的核心步骤如下:
- 第一步,获取硬解码支持的像素格式:
// 硬解码输出的像素格式 static enum AVPixelFormat hw_pix_fmt = AV_PIX_FMT_NONE; // 指定硬件解码器为VideoToolBox;iOS平台对应的硬件编解码器是AV_HWDEVICE_TYPE_VIDEOTOOLBOX。(其他平台可以在这里加扩展代码) static enum AVHWDeviceType hw_device_type = AV_HWDEVICE_TYPE_VIDEOTOOLBOX; struct AVCodec *pCodec = avcodec_find_decoder(AV_CODEC_ID_H265); struct AVCodecContext *pDecoderContext = avcodec_alloc_context3(pCodec); bool isSupportHW = false; void getHWPixFmt { for (int i = 0;; i++) { const AVCodecHWConfig *config = avcodec_get_hw_config(pCodec, i); if (!config) { printf("Decoder %s does not support device type %s.\n", pCodec->name, av_hwdevice_get_type_name(hw_device_type)); break; } if (config->device_type == hw_device_type) { // iOS平台下device_type为 AV_HWDEVICE_TYPE_VIDEOTOOLBOX;像素格式为 AV_PIX_FMT_VIDEOTOOLBOX isSupportHW = true; hw_pix_fmt = config->pix_fmt; break; } } }
- 第二步,初始化解码器:
void initDecoder { // 初始化解码器 pDecoderContext->pix_fmt = AV_PIX_FMT_YUV420P; pDecoderContext->flags |= AV_CODEC_FLAG_LOW_DELAY; // 解码器低延迟 if (isSupportHW) { initHardwareCodec(pDecoderContext); } AVDictionary *opts; avcodec_open2(pDecoderContext, pCodec, &opts); } /** 初始化硬件编解码器 */ int initHardwareCodec(AVCodecContext *ctx) { // 获取硬件编解码器 const char *codecName = av_hwdevice_get_type_name(hw_device_type); enum AVHWDeviceType type = av_hwdevice_find_type_by_name(codecName); if (hw_device_type != type) { printf("not find hardware codec"); return -1; } AVBufferRef *device_ctx = NULL; int ret = av_hwdevice_ctx_create(&device_ctx, type, NULL, NULL, 0); if (ret < 0) { printf("failed to create device_ctx"); return ret; } ctx->hw_device_ctx = av_buffer_ref(device_ctx); return ret; }
- 第三步,将解码后数据从显存转换到内存:
avcodec_decode_video2(pDecoderContext, p_src_frame, &frameFinished, &packet); if (p_src_frame->format != AV_PIX_FMT_NONE && p_src_frame->format == hw_pix_fmt) { // 将解码后的数据从显存转换到内存 int ret = av_hwframe_transfer_data(p_sw_frame, p_src_frame, 0); if (ret >= 0) { AVFrame *p_dst_frame = av_frame_alloc(); av_frame_copy_props(dst_frame, p_src_frame); // 转换格式(对应(4)将像素格式转为YUV420P) convertToYUV420P(p_sw_frame, p_dst_frame); av_frame_unref(p_src_frame); av_free(p_src_frame); return dst_frame; } }
- 第四步,将像素格式转为YUV420P:
iOS平台下硬件解码输出的像素格式为AV_PIX_FMT_VIDEOTOOLBOX,转换到内存后像素格式为AV_PIX_FMT_NV12;为了不修改播放器现有的渲染代码,需要将AV_PIX_FMT_NV12转换为AV_PIX_FMT_YUV420P;转换代码如下:/** 将某像素格式转换为YUV420P @param src_frame 源视频帧(可以是任意像素格式) @param dst_frame 目标视频帧(像素格式为YUV240P) */ void convertToYUV420P(AVFrame *src_frame, AVFrame *dst_frame) { dst_frame->format = AV_PIX_FMT_YUV420P; dst_frame->width = src_frame->width; dst_frame->height = src_frame->height; struct SwsContext *convert_ctx = sws_getContext(src_frame->width, src_frame->height, src_frame->format, dst_frame->width, dst_frame->height, dst_frame->format, SWS_BILINEAR, NULL, NULL, NULL); if (!convert_ctx) { return; } av_frame_get_buffer(dst_frame, 0); sws_scale(convert_ctx, (const uint8_t **)src_frame->data, src_frame->linesize, 0, src_frame->height, dst_frame->data, dst_frame->linesize); sws_freeContext(convert_ctx); }