智能客服
你问我答,随时在线为你解决问题
开发者可以调用本模块的Native API接口,实现在视频播放中支持HDR Vivid标准。
视频播放的主要流程,是将视频文件“解封装 > 解码 > 送显/播放”。
从视频文件中,可以解析出其是否为HDR Vivid视频,如果视频源为HDR Vivid视频,可以解析相关的信息,如CuvvBox信息、元数据、Color信息等。
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;
} 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类型,是否为HDR Vivid视频。
int32_t isHDRVivid = 0;
bool ret = OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_IS_HDR_VIVID, &isHDRVivid);
if (ret == false || isHDRVivid == 0) {
printf("is not HDRVivid ");
}
OH_AVFormat_Destroy(format); // 销毁。
} 应用创建H.265解码器,并配置宽、高、format信息。解码器解析码流,生产对应的视频帧数据以及元数据。
仅在Surface模式下支持HDR Vivid视频解码。
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>
应用可以通过名称或媒体类型创建解码器。示例中的变量说明如下:
// 通过mimetype创建H.265解码器实例。 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模式中的“步骤-3:调用OH_VideoDecoder_RegisterCallback()设置回调函数”。
具体可参考:视频解码Surface模式中的“步骤-5:调用OH_VideoDecoder_Configure()配置解码器”。
具体可参考:视频解码Surface模式中的“步骤-6:设置surface”。
具体可参考:视频解码Surface模式中的“步骤-9:调用OH_VideoDecoder_Start()启动解码器”。
bool videoIsEnd = false;
// 为资源实例创建对应的解封装器。
OH_AVDemuxer *demuxer = OH_AVDemuxer_CreateWithSource(source);
// 取出回调函数OnNeedInputBuffer存到输入队列的帧buffer。
CodecBufferInfo bufferInfo = inputBufferInfoQueue_.front();
inputBufferInfoQueue_.pop();
// 解封装帧数据。
int32_t 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) {
// 异常处理。
}