往期鸿蒙全套实战文章必看:
HDR Vivid视频播放
开发者可以调用本模块的Native API接口,实现在视频播放中支持HDR Vivid标准。
视频播放的主要流程,是将视频文件“解封装 > 解码 > 送显/播放”。
HDR Vivid视频解析
从视频文件中,可以解析出其是否为HDRVivid视频,如果视频源为HDRVivid视频,可以解析相关的信息,如CuvvBox信息、元数据、Color信息等。
在 CMake 脚本中链接动态库
target_link_libraries(sample PUBLIC libnative_media_codecbase.so)
target_link_libraries(sample PUBLIC libnative_media_avdemuxer.so)
target_link_libraries(sample PUBLIC libnative_media_avsource.so)
target_link_libraries(sample PUBLIC libnative_media_core.so)
开发步骤
- 添加头文件。
#include <multimedia/player_framework/native_avdemuxer.h> #include <multimedia/player_framework/native_avsource.h> #include <multimedia/player_framework/native_avcodec_base.h> #include <multimedia/player_framework/native_avformat.h> #include <multimedia/player_framework/native_avbuffer.h> #include <fcntl.h> #include <sys/stat.h>
- 文件解析器。
// 创建文件操作符 fd,打开时对文件句柄必须有读权限(filePath 为待解封装文件路径,需预置文件,保证路径指向的文件存在) std::string filePath = "test.mp4"; int fd = open(filePath.c_str(), O_RDONLY); struct stat fileStatus {}; // 获取fileSize size_t fileSize = 0; if (stat(filePath.c_str(), &fileStatus) == 0) { fileSize = static_cast<size_t>(fileStatus.st_size); } else { printf("get stat failed"); return; } // 为 fd 资源文件创建 source 资源对象 OH_AVSource *source = OH_AVSource_CreateWithFD(fd, 0, fileSize); if (source == nullptr) { printf("create source failed"); return; }
- 获取视频轨道信息,查询文件HDR类型。
int32_t trackCount = 0; uint32_t audioTrackIndex = 0; uint32_t videoTrackIndex = 0; int32_t trackType;
// 从文件 source 信息获取文件轨道数 OH_AVFormat *sourceFormat = OH_AVSource_GetSourceFormat(source); if (sourceFormat == nullptr) { printf("get source format failed"); return; } OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &trackCount); OH_AVFormat_Destroy(sourceFormat); for (uint32_t index = 0; index < (static_cast<int32_t>(trackCount)); index++) { // 获取轨道信息 OH_AVFormat *format = OH_AVSource_GetTrackFormat(source, index); if (format == nullptr) { printf("get track format failed"); return; } // 判断轨道类型 static_cast<OH_MediaType>(trackType) == OH_MediaType::MEDIA_TYPE_AUD ? audioTrackIndex = index : videoTrackIndex = index; // 查询文件HDR类型,是否为HDRVivid视频。 int32_t isHDRVivid; int32_t ret = OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_IS_HDR_VIVID, &isHDRVivid); if (ret != AV_ERR_OK) { printf("is not HDRVivid "); return; } OH_AVFormat_Destroy(format); // 销毁 }
HDR Vivid视频解码
应用创建H265解码器,并配置宽、高、format信息。解码器解析码流,生产对应的视频帧数据以及元数据。
说明
仅在Surface模式下支持HDR Vivid视频解码。
在 CMake 脚本中链接动态库
target_link_libraries(sample PUBLIC libnative_media_codecbase.so)
target_link_libraries(sample PUBLIC libnative_media_core.so)
target_link_libraries(sample PUBLIC libnative_media_vdec.so)
开发步骤
- 添加头文件。
#include <multimedia/player_framework/native_avcodec_videodecoder.h> #include <multimedia/player_framework/native_avcapability.h> #include <multimedia/player_framework/native_avcodec_base.h> #include <multimedia/player_framework/native_avformat.h> #include <multimedia/player_framework/native_avbuffer.h> #include <fstream>
- 创建H265解码器实例对象。
应用可以通过名称或媒体类型创建解码器。示例中的变量说明如下:
- videoDec:视频解码器实例的指针。
- OH_AVCODEC_MIMETYPE_VIDEO_HEVC:HEVC格式视频码流的名称。
// 通过mimetype创建H265解码器 OH_AVCodec *videoDec = OH_VideoDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_HEVC);
- 配置异步回调函数。添加头文件:
#include <condition_variable> #include <queue> #include <mutex>
struct CodecBufferInfo { uint32_t bufferIndex = 0; OH_AVBuffer *buffer = nullptr; uint8_t *bufferAddr = nullptr; OH_AVCodecBufferAttr attr = {0, 0, 0, AVCODEC_BUFFER_FLAGS_NONE}; }; std::mutex inputMutex_; std::condition_variable inputCond_; std::queue<CodecBufferInfo> inputBufferInfoQueue_; // 解码输入回调OH_AVCodecOnNeedInputBuffer实现 void OnNeedInputBuffer(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData) { (void)codec; std::unique_lock<std::mutex> lock(inputMutex_); inputBufferInfoQueue_.emplace(index, buffer); inputCond_.notify_all(); }
具体可参考:视频解码Surface模式中的“步骤4:调用OH_VideoDecoder_RegisterCallback()设置回调函数”。
- 配置解码器。
具体可参考:视频解码Surface模式中的“步骤6:调用OH_VideoDecoder_Configure()配置解码器”。
- (可选)设置Surface。
具体可参考:视频解码Surface模式中的“步骤7:设置Surface”。
- 调用OH_VideoDecoder_Start()启动解码器。
具体可参考:视频解码Surface模式中的“步骤10:调用OH_VideoDecoder_Start()启动解码器”。
处理视频帧数据
- 解封装,循环获取帧数据。
bool videoIsEnd = false; // 为资源对象创建对应的解封装器 OH_AVDemuxer *demuxer = OH_AVDemuxer_CreateWithSource(source); // 取出回调函数OnNeedInputBuffer存到输入队列的帧buffer CodecBufferInfo bufferInfo = inputBufferInfoQueue_.front(); inputBufferInfoQueue_.pop(); // 解封装帧数据 ret = OH_AVDemuxer_ReadSampleBuffer(demuxer, videoTrackIndex, bufferInfo.buffer); if (ret == AV_ERR_OK) { // 可通过buffer获取并处理视频帧数据 OH_AVBuffer_GetBufferAttr(bufferInfo.buffer, &bufferInfo.attr); if (bufferInfo.attr.flags == OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) { videoIsEnd = true; } }
- 将解封装后的视频帧数据送入解码输入队列。
// 送入解码输入队列进行解码,index为对应队列下标 ret = OH_VideoDecoder_PushInputBuffer(videoDec, bufferInfo.bufferIndex); if (ret != AV_ERR_OK) { // 异常处理 }