After obtaining the demultiplexed data, send the data to the decoder, and use the decoder to obtain the PCM data and metadata. For details about the APIs, see AudioDecoder API Reference.
The following table lists the specifications supported in Audio Vivid decoding.
| Item | Range |
|---|---|
| Sampling Rate | 32000, 44100, 48000, 96000, 192000 |
| Bit Rate Range | 16000~3075000 |
| Audio Channel Count | 1~16 |
| Bit Depth | S16, S24 |
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 )
// Demuxer header file
#include "multimedia/player_framework/native_avdemuxer.h"
#include <string>
// Struct about the demuxer and decoder
struct AudioSampleInfo {
std::string audioCodecMime = "";
int32_t audioSampleFormat = 0;
int32_t audioSampleRate = 0;
int32_t audioChannelCount = 0;
int64_t audioChannelLayout = 0;
uint8_t audioCodecConfig[100] = {0};
size_t audioCodecSize = 0;
};
AudioSampleInfo info; Create a decoder instance.
// Create a decoder.
OH_AVCodec * decoder = OH_AudioCodec_CreateByMime(info.audioCodecMime,false);
// Set parameters.
OH_AVFormat *format = OH_AVFormat_Create();
OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_S16LE); // S24LE is also acceptable.
OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, sampleInfo.audioChannelCount);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, sampleInfo.audioSampleRate);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, 1);
OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, 96422); // Bit rate, which is for reference only. It can also be obtained during demultiplexing.
OH_AVFormat_SetBuffer(format, OH_MD_KEY_CODEC_CONFIG, sampleInfo.audioCodecConfig, sampleInfo.audioCodecSize);
bool res = OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, sampleInfo.audioChannelLayout);
ret = OH_AudioCodec_Configure(decoder, format);
OH_AVFormat_Destroy(format);
format = nullptr;
// Set the callback functions, which are called by the system after the input and output buffers are prepared.
int32_t ret = OH_AudioCodec_RegisterCallback(decoder,
{SampleCallback::OnCodecError, SampleCallback::OnCodecFormatChange,
SampleCallback::OnNeedInputBuffer, SampleCallback::OnNewOutputBuffer},codecUserData);
// After the callback and parameter setting are complete, notify the system that the decoder is ready and the next step can continue.
ret = OH_AudioCodec_Prepare(decoder) Write audio to the decoder.
int32_t PushInputData(CodecBufferInfo &info)
{
int32_t ret = OH_AVBuffer_SetBufferAttr(reinterpret_cast<OH_AVBuffer *>(info.buffer), &info.attr);
ret = OH_AudioCodec_PushInputBuffer(decoder, info.bufferIndex);
return 0;
} Release the used output stream.
int32_t AudioDecoder::FreeOutputData(uint32_t bufferIndex)
{
int32_t ret = 0;
ret = OH_AudioCodec_FreeOutputBuffer(decoder, bufferIndex);
return ret ;
} Implement the audio write thread.
CodecUserData*audioDecContext_ = new CodecUserData;
void AudioDecInputThread()
{
while (true) {
if(!isStarted_){
return;
}
std::unique_lock<std::mutex> lock(audioDecContext_->inputMutex_);
// Block the input thread until the program execution ends, or the input queue is not empty.
bool condRet = audioDecContext_->inputCond_.wait_for(
lock, 5s, [this]() { return !isStarted_ || !audioDecContext_->inputBufferInfoQueue_.empty(); });
if(!isStarted_ || audioDecContext_->inputBufferInfoQueue_.empty()){
return;
}
// Obtain the input buffer.
CodecBufferInfo bufferInfo = audioDecContext_->inputBufferInfoQueue_.front();
audioDecContext_->inputBufferInfoQueue_.pop();
audioDecContext_->inputFrameCount_++;
lock.unlock();
// Read a frame of data from the demuxer and write it to the input buffer.
demuxer_->ReadSample(demuxer_->GetAudioTrackId(), reinterpret_cast<OH_AVBuffer *>(bufferInfo.buffer), bufferInfo.attr);
int32_t ret = audioDecoder_->PushInputData(bufferInfo);
if(ret != 0){
return;
}
if(bufferInfo.attr.flags & AVCODEC_BUFFER_FLAGS_EOS){
return;
}
}
// StartRelease();
} Implement the audio decoding output thread.
void AudioDecOutputThread()
{
while (true) {
if(!isStarted_){
return;
}
std::unique_lock<std::mutex> lock(audioDecContext_->outputMutex_);
// Block the output thread until the program execution ends, or the output queue is not empty.
bool condRet = audioDecContext_->outputCond_.wait_for(
lock, 5s, [this]() { return !isStarted_ | !audioDecContext_->outputBufferInfoQueue_.empty(); });
if(!isStarted_ || audioDecContext_->outputBufferInfoQueue_.empty()){
return;
}
// Obtain the output buffer.
CodecBufferInfo bufferInfo = audioDecContext_->outputBufferInfoQueue_.front();
audioDecContext_->outputBufferInfoQueue_.pop();
if(bufferInfo.attr.flags & AVCODEC_BUFFER_FLAGS_EOS){
return;
}
audioDecContext_->outputFrameCount_++;
// Obtain the decoded PCM data.
uint8_t *source = OH_AVBuffer_GetAddr(reinterpret_cast<OH_AVBuffer *>(bufferInfo.buffer));
OH_AVFormat * format = OH_AVBuffer_GetParameter(reinterpret_cast<OH_AVBuffer *>(bufferInfo.buffer));
uint8_t * metadata;
size_t size;
// Obtain metadata.
OH_AVFormat_GetBuffer(format, OH_MD_KEY_AUDIO_VIVID_METADATA, &metadata, &size);
#ifdef DEBUG_DECODE
if (audioOutputFile_.is_open()) {
audioOutputFile_.write((const char*)OH_AVBuffer_GetAddr(reinterpret_cast<OH_AVBuffer *>(bufferInfo.buffer)), bufferInfo.attr.size);
}
#endif
lock.unlock();
int32_t ret = audioDecoder_->FreeOutputData(bufferInfo.bufferIndex);
if(ret != 0){
return;
}
}
} Start the decoder.
int ret = OH_AudioCodec_Start(decoder);
Stop the decoder, and release the decoder instance.
OH_AudioCodec_Stop(decoder); OH_AudioCodec_Destroy(decoder); decoder = nullptr;