鸿蒙5.0版开发:AudioVivid解封装

 往期鸿蒙全套实战文章必看:


AudioVivid解封装

获取到Audio Vivid封装的mp4文件后,先调用解封装相关接口,选中音频轨,读取每一帧Audio Vivid,送入解码器中

在Cmake脚本中链接到动态库

target_link_libraries(sample PUBLIC 
libnative_media_codecbase.so libnative_media_core.so 
libnative_media_acodec.so libnative_media_avdemuxer.so libnative_media_avsource.so
)

添加头文件

//解封装头文件
#include "multimedia/player_framework/native_avdemuxer.h"

// 解封装解码传递信息结构体
struct AudioSampleInfo {
std::string audioCodecMime = "";
int32_t audioSampleForamt = 0;
int32_t audioSampleRate = 0; 
int32_t audioChannelCount = 0;
int64_t audioChannelLayout = 0;
uint8_t audioCodecConfig[100] = {0};
size_t audioCodecSize = 0;
};

AudioSampleInfo  info;

开发步骤

  1. 创建解封装实例。
    // ts code获取fd和size
    let inputFile = fs.openSync(filepath,fs.OpenMode.READ_ONLY);
    if(inputFile){
        let inputFileState = fs.stateSync(inputFile.fd);
        let inputFileSize = inputFIleState.size;
    }
    //C++ code
    OH_AVSource *source = OH_AVSource_CreateWithFD(inputFd,0,inputFileSize);
    OH_AVDemuxer *demuxer = OH_AVDemuxer_CreateWithSource(source);
    auto sourceFormat = std::shared_ptr<OH_AVFormat>(OH_AVSource_GetSourceFormat(source_), OH_AVFormat_Destroy);
    int32_t trackCount = 0;
    OH_AVFormat_GetIntValue(sourceFormat.get(), OH_MD_KEY_TRACK_COUNT, &trackCount);
  2. 选中音频轨。
    int32_t trackCount = 0;
    OH_AVFormat_GetIntValue(sourceFormat.get(), OH_MD_KEY_TRACK_COUNT, &trackCount);
    for (int32_t index = 0; index < trackCount; index++) {
    int trackType = -1;
    auto trackFormat =
        std::shared_ptr<OH_AVFormat>(OH_AVSource_GetTrackFormat(source_, index), OH_AVFormat_Destroy);
    // 获取轨道类型
    OH_AVFormat_GetIntValue(trackFormat.get(), OH_MD_KEY_TRACK_TYPE, &trackType);
    // 判断当前轨道为音频轨
    if (trackType == MEDIA_TYPE_AUD) { 
        // 选中音频轨
        OH_AVDemuxer_SelectTrackByID(demuxer, index);
        // 获取位深
        OH_AVFormat_GetIntValue(trackFormat.get(), OH_MD_KEY_AUDIO_SAMPLE_FORMAT, &info.audioSampleForamt);
        // 获取声道数
        OH_AVFormat_GetIntValue(trackFormat.get(), OH_MD_KEY_AUD_CHANNEL_COUNT, &info.audioChannelCount);
        // 获取声道布局
        OH_AVFormat_GetLongValue(trackFormat.get(), OH_MD_KEY_CHANNEL_LAYOUT, &info.audioChannelLayout);
        // 获取采样率
        OH_AVFormat_GetIntValue(trackFormat.get(), OH_MD_KEY_AUD_SAMPLE_RATE, &info.audioSampleRate);
        // 获取额外配置信息
        uint8_t *addr = nullptr;
        OH_AVFormat_GetBuffer(trackFormat.get(), OH_MD_KEY_CODEC_CONFIG, &addr, &info.audioCodecSize);
        memcpy((void *)info.audioCodecConfig, (void *)addr, info.audioCodecSize);
        // 获取解码器类型
        char *audioCodecMime;
        OH_AVFormat_GetStringValue(trackFormat.get(), OH_MD_KEY_CODEC_MIME, const_cast<char const **>(&audioCodecMime));
        info.audioCodecMime = audioCodecMime;
        int32_t trackId = index;
        break;
        }
    }
  3. 读取每一帧数据。
    OH_AVBuffer *buffer;
    int32_t ret = OH_AVDemuxer_ReadSampleBuffer(demuxer, trackId, buffer);
  4. 释放解封装实例。
    int32_t Release()
    {
        if (demuxer != nullptr) {
            OH_AVDemuxer_Destroy(demuxer);
            demuxer = nullptr;
        }
        if (source != nullptr) {
            OH_AVSource_Destroy(source);
            source = nullptr;
        }
        return AVCODEC_SAMPLE_ERR_OK;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值