diff --git a/entry/src/main/cpp/capbilities/AudioDecoder.cpp b/entry/src/main/cpp/capbilities/AudioDecoder.cpp index 1e61e17c7b6bc325c0f34b14b174ecccd7886728..0c77fa19a55ae1b0f673d1ce603a13c6a5d80e3b 100644 --- a/entry/src/main/cpp/capbilities/AudioDecoder.cpp +++ b/entry/src/main/cpp/capbilities/AudioDecoder.cpp @@ -46,8 +46,22 @@ int32_t AudioDecoder::Configure(const SampleInfo &sampleInfo) { 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_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, sampleInfo.audioChannelLayout); + if (sampleInfo.codecConfigLen > 0) { + AVCODEC_SAMPLE_LOGI("====== AudioDecoder config ====== codecConfig:%{public}p, len:%{public}i, " + "adts:${public}i, 0:0x%{public}02x, 1:0x%{public}02x", + sampleInfo.codecConfig, sampleInfo.codecConfigLen, sampleInfo.aacAdts, + sampleInfo.codecConfig[0], sampleInfo.codecConfig[1]); + uint8_t tmpCodecConfig[2]; + tmpCodecConfig[0] = 0x13; // 0x11 + tmpCodecConfig[1] = 0x10; // 0x90 + tmpCodecConfig[0] = sampleInfo.codecConfig[0]; // 0x11 + tmpCodecConfig[1] = sampleInfo.codecConfig[1]; // 0x90 + AVCODEC_SAMPLE_LOGI("====== AudioDecoder config ====== 0:0x%{public}02x, 1:0x%{public}02x", tmpCodecConfig[0], + tmpCodecConfig[1]); + OH_AVFormat_SetBuffer(format, OH_MD_KEY_CODEC_CONFIG, sampleInfo.codecConfig, sampleInfo.codecConfigLen); + } + AVCODEC_SAMPLE_LOGI("====== AudioDecoder config ======"); - int ret = OH_AudioCodec_Configure(decoder_, format); AVCODEC_SAMPLE_LOGI("====== AudioDecoder config ======"); OH_AVFormat_Destroy(format); diff --git a/entry/src/main/cpp/capbilities/Demuxer.cpp b/entry/src/main/cpp/capbilities/Demuxer.cpp index 2b5501e4749a1bf6ec7230f45db946b6dbf52ae9..094a9e949ac0e545a55497382477b9d5739138ed 100644 --- a/entry/src/main/cpp/capbilities/Demuxer.cpp +++ b/entry/src/main/cpp/capbilities/Demuxer.cpp @@ -95,15 +95,26 @@ int32_t Demuxer::GetTrackInfo(std::shared_ptr sourceFormat, SampleI char *audioCodecMime; OH_AVFormat_GetStringValue(trackFormat.get(), OH_MD_KEY_CODEC_MIME, const_cast(&audioCodecMime)); + uint8_t *codecConfig = nullptr; + OH_AVFormat_GetBuffer(trackFormat.get(), OH_MD_KEY_CODEC_CONFIG, &codecConfig, &info.codecConfigLen); + if (info.codecConfigLen > 0 && info.codecConfigLen < sizeof(info.codecConfig)) { + memcpy(info.codecConfig, codecConfig, info.codecConfigLen); + AVCODEC_SAMPLE_LOGI( + "codecConfig:%{public}p, len:%{public}i, 0:0x%{public}02x 1:0x:%{public}02x, bufLen:%{public}u", + info.codecConfig, (int)info.codecConfigLen, info.codecConfig[0], info.codecConfig[1], + sizeof(info.codecConfig)); + } + OH_AVFormat_GetIntValue(trackFormat.get(), OH_MD_KEY_AAC_IS_ADTS, &info.aacAdts); + info.audioCodecMime = audioCodecMime; audioTrackId_ = index; AVCODEC_SAMPLE_LOGI("====== Demuxer Audio config ======"); - AVCODEC_SAMPLE_LOGI("Mime: %{public}s", audioCodecMime); - AVCODEC_SAMPLE_LOGI("audioMime:%{public}s sampleForamt:%{public}d " - "sampleRate:%{public}d channelCount:%{public}d channelLayout:%{public}d", - info.audioCodecMime.c_str(), info.audioSampleForamt, info.audioSampleRate, - info.audioChannelCount, (int)info.audioChannelLayout); + AVCODEC_SAMPLE_LOGI( + "audioMime:%{public}s sampleForamt:%{public}d " + "sampleRate:%{public}d channelCount:%{public}d channelLayout:%{public}d adts:%{public}i", + info.audioCodecMime.c_str(), info.audioSampleForamt, info.audioSampleRate, info.audioChannelCount, + info.audioChannelLayout, info.aacAdts); AVCODEC_SAMPLE_LOGI("====== Demuxer Audio config ======"); } } diff --git a/entry/src/main/cpp/common/SampleInfo.h b/entry/src/main/cpp/common/SampleInfo.h index 4c2639a86d87625047487cb52364fed6ad3f5313..b08dbb6de5836e7a54dee78d4fe1d54ff2f67368 100644 --- a/entry/src/main/cpp/common/SampleInfo.h +++ b/entry/src/main/cpp/common/SampleInfo.h @@ -68,6 +68,9 @@ struct SampleInfo { void (*playDoneCallback)(void *context) = nullptr; void *playDoneCallbackData = nullptr; + uint8_t codecConfig[1024]; + size_t codecConfigLen = 0; + int32_t aacAdts = -1; }; struct CodecBufferInfo { diff --git a/entry/src/main/cpp/sample/player/Player.cpp b/entry/src/main/cpp/sample/player/Player.cpp index a6ae98fc5df9599b97e04864e8385365d974242f..1e571c2b73957380103582c8d5fc87facf6a1dc2 100644 --- a/entry/src/main/cpp/sample/player/Player.cpp +++ b/entry/src/main/cpp/sample/player/Player.cpp @@ -201,11 +201,7 @@ void Player::ReleaseThread() { void Player::Release() { std::lock_guard lock(mutex_); isStarted_ = false; - - // Clear the queue - while (audioDecContext_ && !audioDecContext_->renderQueue.empty()) { - audioDecContext_->renderQueue.pop(); - } + if (audioRenderer_ != nullptr) { OH_AudioRenderer_Release(audioRenderer_); audioRenderer_ = nullptr; @@ -237,6 +233,10 @@ void Player::Release() { doneCond_.notify_all(); // Trigger the callback sampleInfo_.playDoneCallback(sampleInfo_.playDoneCallbackData); + // Clear the queue + while (audioDecContext_ && !audioDecContext_->renderQueue.empty()) { + audioDecContext_->renderQueue.pop(); + } AVCODEC_SAMPLE_LOGI("Succeed"); } @@ -352,6 +352,10 @@ void Player::AudioDecOutputThread() { return audioDecContext_->renderQueue.size() < BALANCE_VALUE * bufferInfo.attr.size; }); } + std::unique_lock lockRender(audioDecContext_->renderMutex); + audioDecContext_->renderCond.wait_for(lockRender, 500ms, [this](){ + return audioDecContext_->renderQueue.size() < 1; + }); AVCODEC_SAMPLE_LOGI("Out buffer end"); StartRelease(); } \ No newline at end of file