• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "audio_codec_decoder_adapter_impl.h"
17 
18 #include "native_avcodec_audiocodec.h"
19 #include "native_avcapability.h"
20 #include "native_avbuffer.h"
21 #include <multimedia/native_audio_channel_layout.h>
22 #include "native_drm_err.h"
23 #include "native_mediakeysession.h"
24 #include <cinttypes>
25 #include "audio_cenc_info_adapter_impl.h"
26 
27 #define MIME_SIZE 256
28 
29 namespace OHOS::NWeb {
30 
31 static const std::unordered_map<OHOS::MediaAVCodec::AVCodecBufferFlag, BufferFlag> BUFFER_FLAG_MAP = {
32     { OHOS::MediaAVCodec::AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_NONE, BufferFlag::CODEC_BUFFER_FLAG_NONE },
33     { OHOS::MediaAVCodec::AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_EOS, BufferFlag::CODEC_BUFFER_FLAG_EOS },
34     { OHOS::MediaAVCodec::AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_SYNC_FRAME, BufferFlag::CODEC_BUFFER_FLAG_SYNC_FRAME },
35     { OHOS::MediaAVCodec::AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_PARTIAL_FRAME,
36         BufferFlag::CODEC_BUFFER_FLAG_PARTIAL_FRAME },
37     { OHOS::MediaAVCodec::AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_CODEC_DATA, BufferFlag::CODEC_BUFFER_FLAG_CODEC_DATA }
38 };
39 
40 static const std::unordered_map<const char *, AudioMimeType>  MIME_TYPE_MAP = {
41     { OH_AVCODEC_MIMETYPE_AUDIO_AAC, AudioMimeType::MIMETYPE_AUDIO_AAC },
42     { OH_AVCODEC_MIMETYPE_AUDIO_FLAC, AudioMimeType::MIMETYPE_AUDIO_FLAC },
43     { OH_AVCODEC_MIMETYPE_AUDIO_VORBIS, AudioMimeType::MIMETYPE_AUDIO_VORBIS },
44     { OH_AVCODEC_MIMETYPE_AUDIO_MPEG, AudioMimeType::MIMETYPE_AUDIO_MPEG },
45     { OH_AVCODEC_MIMETYPE_AUDIO_AMR_NB, AudioMimeType::MIMETYPE_AUDIO_AMR_NB },
46     { OH_AVCODEC_MIMETYPE_AUDIO_AMR_WB, AudioMimeType::MIMETYPE_AUDIO_AMR_WB },
47     { OH_AVCODEC_MIMETYPE_AUDIO_G711MU, AudioMimeType::MIMETYPE_AUDIO_G711MU },
48     { OH_AVCODEC_MIMETYPE_AUDIO_APE, AudioMimeType::MIMETYPE_AUDIO_APE }
49 };
50 
~AudioDecoderFormatAdapterImpl()51 AudioDecoderFormatAdapterImpl::~AudioDecoderFormatAdapterImpl() {}
GetSampleRate()52 int32_t AudioDecoderFormatAdapterImpl::GetSampleRate()
53 {
54     return sampleRate_;
55 }
56 
GetChannelCount()57 int32_t AudioDecoderFormatAdapterImpl::GetChannelCount()
58 {
59     return channelCount_;
60 }
61 
GetBitRate()62 int64_t AudioDecoderFormatAdapterImpl::GetBitRate()
63 {
64     return bitRate_;
65 }
66 
GetMaxInputSize()67 int32_t AudioDecoderFormatAdapterImpl::GetMaxInputSize()
68 {
69     return maxInputSize_;
70 }
71 
GetAudioSampleFormat()72 int32_t AudioDecoderFormatAdapterImpl::GetAudioSampleFormat()
73 {
74     return audioSampleFormat_;
75 }
76 
GetAACIsAdts()77 bool AudioDecoderFormatAdapterImpl::GetAACIsAdts()
78 {
79     return aacIsAdts_;
80 }
81 
GetIdentificationHeader()82 int32_t AudioDecoderFormatAdapterImpl::GetIdentificationHeader()
83 {
84     return idHeader_;
85 }
86 
GetSetupHeader()87 int32_t AudioDecoderFormatAdapterImpl::GetSetupHeader()
88 {
89     return setupHeader_;
90 }
91 
GetCodecConfig()92 uint8_t* AudioDecoderFormatAdapterImpl::GetCodecConfig()
93 {
94     return codecConfig_;
95 }
96 
GetCodecConfigSize()97 uint32_t AudioDecoderFormatAdapterImpl::GetCodecConfigSize()
98 {
99     return codecConfigSize_;
100 }
101 
SetSampleRate(int32_t sampleRate)102 void AudioDecoderFormatAdapterImpl::SetSampleRate(int32_t sampleRate)
103 {
104     sampleRate_ = sampleRate;
105 }
106 
SetChannelCount(int32_t channelCount)107 void AudioDecoderFormatAdapterImpl::SetChannelCount(int32_t channelCount)
108 {
109     channelCount_ = channelCount;
110 }
111 
SetBitRate(int64_t bitRate)112 void AudioDecoderFormatAdapterImpl::SetBitRate(int64_t bitRate)
113 {
114     bitRate_ = bitRate;
115 }
116 
SetMaxInputSize(int32_t maxInputSize)117 void AudioDecoderFormatAdapterImpl::SetMaxInputSize(int32_t maxInputSize)
118 {
119     maxInputSize_ = maxInputSize;
120 }
121 
SetAACIsAdts(bool isAdts)122 void AudioDecoderFormatAdapterImpl::SetAACIsAdts(bool isAdts)
123 {
124     aacIsAdts_ = isAdts;
125 }
126 
SetAudioSampleFormat(int32_t audioSampleFormat)127 void AudioDecoderFormatAdapterImpl::SetAudioSampleFormat(int32_t audioSampleFormat)
128 {
129     audioSampleFormat_ = audioSampleFormat;
130 }
131 
SetIdentificationHeader(int32_t idHeader)132 void AudioDecoderFormatAdapterImpl::SetIdentificationHeader(int32_t idHeader)
133 {
134     idHeader_ = idHeader;
135 }
136 
SetSetupHeader(int32_t setupHeader)137 void AudioDecoderFormatAdapterImpl::SetSetupHeader(int32_t setupHeader)
138 {
139     setupHeader_ = setupHeader;
140 }
141 
SetCodecConfig(uint8_t * codecConfig)142 void AudioDecoderFormatAdapterImpl::SetCodecConfig(uint8_t* codecConfig)
143 {
144     codecConfig_ = codecConfig;
145 }
146 
SetCodecConfigSize(uint32_t size)147 void AudioDecoderFormatAdapterImpl::SetCodecConfigSize(uint32_t size)
148 {
149     codecConfigSize_ = size;
150 }
151 
PrintFormatData(std::shared_ptr<AudioDecoderFormatAdapter> format)152 void AudioDecoderFormatAdapterImpl::PrintFormatData(std::shared_ptr<AudioDecoderFormatAdapter> format)
153 {
154     WVLOG_I("PrintFormatData:SampleRate[%{public}d], ChannelCount[%{public}d],"
155             "MaxInputSize[%{public}d], AACIsAdts[%{public}d], AudioSampleFormat[%{public}d],"
156             "BitRate[%{public}" PRId64 "], IDHeader[%{public}d], SetupHeader[%{public}d],",
157             format->GetSampleRate(), format->GetChannelCount(), format->GetMaxInputSize(),
158             static_cast<int32_t>(format->GetAACIsAdts()), format->GetAudioSampleFormat(),
159             format->GetBitRate(), format->GetIdentificationHeader(), format->GetSetupHeader());
160 }
161 
AudioDecoderCallbackAdapterImpl(std::shared_ptr<OHOS::NWeb::AudioDecoderCallbackAdapter> cb)162 AudioDecoderCallbackAdapterImpl::AudioDecoderCallbackAdapterImpl(
163     std::shared_ptr<OHOS::NWeb::AudioDecoderCallbackAdapter> cb) :cb_(cb) {};
164 
OnError(int32_t errorCode)165 void AudioDecoderCallbackAdapterImpl::OnError(int32_t errorCode)
166 {
167     if (!cb_) {
168         WVLOG_E("callback is nullptr.");
169         return;
170     }
171 
172     cb_->OnError(errorCode);
173 }
174 
OnOutputFormatChanged()175 void AudioDecoderCallbackAdapterImpl::OnOutputFormatChanged()
176 {
177     if (!cb_) {
178         WVLOG_E("callback is nullptr.");
179         return;
180     }
181 
182     cb_->OnOutputFormatChanged();
183 }
184 
OnInputBufferAvailable(uint32_t index)185 void AudioDecoderCallbackAdapterImpl::OnInputBufferAvailable(uint32_t index)
186 {
187     WVLOG_I("inputBuffer[%{public}u] available .", index);
188     if (!cb_) {
189         WVLOG_E("callback is nullptr.");
190         return;
191     }
192 
193     cb_->OnInputBufferAvailable(index);
194 }
195 
OnOutputBufferAvailable(uint32_t index,uint8_t * bufferData,int32_t size,int64_t pts,int32_t offset,uint32_t flags)196 void AudioDecoderCallbackAdapterImpl::OnOutputBufferAvailable(
197     uint32_t index, uint8_t *bufferData, int32_t size, int64_t pts, int32_t offset, uint32_t flags)
198 {
199     WVLOG_I("outputBuffer[%{public}u] available, buffer size[%{public}d], pts[%{public}" PRId64 "],"
200         "offset[%{public}d], flags[%{public}u].", index, size, pts, offset, flags);
201     if (!cb_) {
202         WVLOG_E("callback is nullptr.");
203         return;
204     }
205 
206     cb_->OnOutputBufferAvailable(index, bufferData, size, pts, offset, flags);
207 }
208 
209 std::mutex AudioCodecDecoderAdapterImpl::decoderMutex_;
210 
GetDecoderMutex()211 std::mutex& AudioCodecDecoderAdapterImpl::GetDecoderMutex()
212 {
213     return decoderMutex_;
214 }
215 
AudioCodecDecoderAdapterImpl()216 AudioCodecDecoderAdapterImpl::AudioCodecDecoderAdapterImpl() {}
217 
~AudioCodecDecoderAdapterImpl()218 AudioCodecDecoderAdapterImpl::~AudioCodecDecoderAdapterImpl()
219 {
220     std::unique_lock<std::mutex> lock(decoderMutex_);
221     WVLOG_D("~AudioCodecDecoderAdapterImpl enter.");
222     callback_ = nullptr;
223     if (decoder_ != nullptr) {
224         AudioDecoderCallbackManager::DeleteAudioDecoder(decoder_);
225         OH_AVErrCode errCode = OH_AudioCodec_Destroy(decoder_);
226         if (errCode != AV_ERR_OK) {
227             WVLOG_E("destroy decoder_ fail, errCode = %{public}u.", uint32_t(errCode));
228         }
229         decoder_ = nullptr;
230     }
231 }
232 
GetAVCodec()233 OH_AVCodec* AudioCodecDecoderAdapterImpl::GetAVCodec()
234 {
235     return decoder_;
236 }
237 
GetAudioDecoderCallBack()238 AudioDecoderCallbackAdapterImpl* AudioCodecDecoderAdapterImpl::GetAudioDecoderCallBack()
239 {
240     return callback_.get();
241 }
242 
SetInputBuffer(uint32_t index,OH_AVBuffer * buffer)243 void AudioCodecDecoderAdapterImpl::SetInputBuffer(uint32_t index, OH_AVBuffer *buffer)
244 {
245     WVLOG_D("AudioCodecDecoderAdapterImpl %{public}s[%{public}u].", __FUNCTION__, index);
246     std::unique_lock<std::mutex> lock(inMutex_);
247     if (inputBuffers_.find(index) != inputBuffers_.end()) {
248         WVLOG_I("AudioCodecDecoderAdapterImpl %{public}s[%{public}u].", __FUNCTION__, index);
249         return;
250     }
251     inputBuffers_.insert(std::make_pair(index, buffer));
252 }
253 
SetOutputBuffer(uint32_t index,OH_AVBuffer * buffer)254 void AudioCodecDecoderAdapterImpl::SetOutputBuffer(uint32_t index, OH_AVBuffer *buffer)
255 {
256     std::unique_lock<std::mutex> lock(outMutex_);
257     if (outputBuffers_.find(index) != outputBuffers_.end()) {
258         return;
259     }
260     outputBuffers_.insert(std::make_pair(index, buffer));
261 }
262 
GetInputBuffer(uint32_t index)263 OH_AVBuffer* AudioCodecDecoderAdapterImpl::GetInputBuffer(uint32_t index)
264 {
265     std::unique_lock<std::mutex> lock(inMutex_);
266     if (inputBuffers_.find(index) == inputBuffers_.end()) {
267         return nullptr;
268     }
269     return inputBuffers_.at(index);
270 }
271 
GetOutputBuffer(uint32_t index)272 OH_AVBuffer* AudioCodecDecoderAdapterImpl::GetOutputBuffer(uint32_t index)
273 {
274     std::unique_lock<std::mutex> lock(outMutex_);
275     if (outputBuffers_.find(index) == outputBuffers_.end()) {
276         return nullptr;
277     }
278     return outputBuffers_.at(index);
279 }
280 
CreateAudioDecoderByMime(const std::string & mimetype)281 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::CreateAudioDecoderByMime(const std::string& mimetype)
282 {
283     WVLOG_I("%{public}s, mimetype[%{public}s].", __FUNCTION__, mimetype.c_str());
284     if (decoder_ != nullptr) {
285         WVLOG_I("decoder_ already has decoder.");
286         ReleaseDecoder();
287     }
288     decoder_ = OH_AudioCodec_CreateByMime(mimetype.c_str(), false);
289     if (decoder_ == nullptr) {
290         WVLOG_E("create decoder by mine[%{public}s] failed.", mimetype.c_str());
291         return AudioDecoderAdapterCode::DECODER_ERROR;
292     }
293     mimeType_ = AudioMimeType::MIMETYPE_UNKNOW;
294     for (auto it = MIME_TYPE_MAP.begin(); it != MIME_TYPE_MAP.end(); it++) {
295         if (strcmp(it->first, mimetype.c_str()) == 0) {
296             mimeType_ = it->second;
297         }
298     }
299 
300     AudioDecoderCallbackManager::AddAudioDecoder(this);
301     return AudioDecoderAdapterCode::DECODER_OK;
302 }
303 
CreateAudioDecoderByName(const std::string & name)304 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::CreateAudioDecoderByName(const std::string& name)
305 {
306     WVLOG_I("%{public}s, name[%{public}s].", __FUNCTION__, name.c_str());
307     if (decoder_ != nullptr) {
308         WVLOG_E("create decoder by name[%{public}s] failed.", name.c_str());
309         ReleaseDecoder();
310     }
311 
312     decoder_ = OH_AudioCodec_CreateByName(name.c_str());
313     if (decoder_ == nullptr) {
314         WVLOG_E("create decoder failed.");
315         return AudioDecoderAdapterCode::DECODER_ERROR;
316     }
317     AudioDecoderCallbackManager::AddAudioDecoder(this);
318     return AudioDecoderAdapterCode::DECODER_OK;
319 }
320 
ConfigureDecoder(const std::shared_ptr<AudioDecoderFormatAdapter> format)321 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::ConfigureDecoder(
322     const std::shared_ptr<AudioDecoderFormatAdapter> format)
323 {
324     WVLOG_I("%{public}s enter.", __FUNCTION__);
325     if (decoder_ == nullptr) {
326         WVLOG_E("decoder_ is nullptr.");
327         return AudioDecoderAdapterCode::DECODER_ERROR;
328     }
329 
330     if (format == nullptr) {
331         WVLOG_E("format is nullptr.");
332         return AudioDecoderAdapterCode::DECODER_ERROR;
333     }
334 
335     OH_AVFormat *avFormat = OH_AVFormat_Create();
336     if (avFormat == nullptr) {
337         WVLOG_E("create avformat fail.");
338         return AudioDecoderAdapterCode::DECODER_ERROR;
339     }
340 
341     OH_AVFormat_SetIntValue(avFormat, OH_MD_KEY_AUD_SAMPLE_RATE, format->GetSampleRate());
342     OH_AVFormat_SetIntValue(avFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, format->GetChannelCount());
343     OH_AVErrCode errCode = OH_AudioCodec_Configure(decoder_, avFormat);
344     OH_AVFormat_Destroy(avFormat);
345     avFormat = nullptr;
346     if (errCode != AV_ERR_OK) {
347         WVLOG_E("set config fail, errCode = %{public}u.", uint32_t(errCode));
348         return AudioDecoderAdapterCode::DECODER_ERROR;
349     }
350     return AudioDecoderAdapterCode::DECODER_OK;
351 }
352 
SetParameterDecoder(const std::shared_ptr<AudioDecoderFormatAdapter> format)353 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::SetParameterDecoder(
354     const std::shared_ptr<AudioDecoderFormatAdapter> format)
355 {
356     WVLOG_I("%{public}s enter.", __FUNCTION__);
357     if (decoder_ == nullptr) {
358         WVLOG_E("decoder_ is nullptr.");
359         return AudioDecoderAdapterCode::DECODER_ERROR;
360     }
361     if (format == nullptr) {
362         WVLOG_E("format is nullptr.");
363         return AudioDecoderAdapterCode::DECODER_ERROR;
364     }
365 
366     OH_AVFormat *avFormat = OH_AVFormat_Create();
367     if (avFormat == nullptr) {
368         WVLOG_E("create avformat fail.");
369         return AudioDecoderAdapterCode::DECODER_ERROR;
370     }
371 
372     OH_AVFormat_SetIntValue(avFormat, OH_MD_KEY_AUD_SAMPLE_RATE, format->GetSampleRate());
373     OH_AVFormat_SetIntValue(avFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, format->GetChannelCount());
374     OH_AVErrCode errCode = OH_AudioCodec_SetParameter(decoder_, avFormat);
375     OH_AVFormat_Destroy(avFormat);
376     avFormat = nullptr;
377     if (errCode != AV_ERR_OK) {
378         WVLOG_E("set config fail, errCode = %{public}u.", uint32_t(errCode));
379         return AudioDecoderAdapterCode::DECODER_ERROR;
380     }
381     return AudioDecoderAdapterCode::DECODER_OK;
382 }
383 
PrepareDecoder()384 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::PrepareDecoder()
385 {
386     WVLOG_I("%{public}s enter.", __FUNCTION__);
387     if (decoder_ == nullptr) {
388         WVLOG_E("decoder_ is nullptr.");
389         return AudioDecoderAdapterCode::DECODER_ERROR;
390     }
391 
392     OH_AVErrCode errCode = OH_AudioCodec_Prepare(decoder_);
393     if (errCode != AV_ERR_OK) {
394         WVLOG_E("prepare decoder fail, errCode = %{public}u.", uint32_t(errCode));
395         return AudioDecoderAdapterCode::DECODER_ERROR;
396     }
397     return AudioDecoderAdapterCode::DECODER_OK;
398 }
399 
StartDecoder()400 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::StartDecoder()
401 {
402     WVLOG_I("%{public}s enter.", __FUNCTION__);
403     if (decoder_ == nullptr) {
404         WVLOG_E("decoder_ is nullptr.");
405         return AudioDecoderAdapterCode::DECODER_ERROR;
406     }
407 
408     OH_AVErrCode errCode = OH_AudioCodec_Start(decoder_);
409     if (errCode != AV_ERR_OK) {
410         WVLOG_E("start decoder fail, errCode = %{public}u.", uint32_t(errCode));
411         return AudioDecoderAdapterCode::DECODER_ERROR;
412     }
413     return AudioDecoderAdapterCode::DECODER_OK;
414 }
415 
StopDecoder()416 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::StopDecoder()
417 {
418     WVLOG_I("%{public}s enter.", __FUNCTION__);
419     if (decoder_ == nullptr) {
420         WVLOG_E("decoder_ is nullptr.");
421         return AudioDecoderAdapterCode::DECODER_ERROR;
422     }
423 
424     OH_AVErrCode errCode = OH_AudioCodec_Stop(decoder_);
425     if (errCode != AV_ERR_OK) {
426         WVLOG_E("stop decoder fail, errCode = %{public}u.", uint32_t(errCode));
427         return AudioDecoderAdapterCode::DECODER_ERROR;
428     }
429     return AudioDecoderAdapterCode::DECODER_OK;
430 }
431 
FlushDecoder()432 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::FlushDecoder()
433 {
434     WVLOG_I("%{public}s enter.", __FUNCTION__);
435     if (decoder_ == nullptr) {
436         WVLOG_E("decoder_ is nullptr.");
437         return AudioDecoderAdapterCode::DECODER_ERROR;
438     }
439 
440     OH_AVErrCode errCode = OH_AudioCodec_Flush(decoder_);
441     if (errCode != AV_ERR_OK) {
442         WVLOG_E("flush decoder fail, errCode = %{public}u.", uint32_t(errCode));
443         return AudioDecoderAdapterCode::DECODER_ERROR;
444     }
445 
446     // clear input and output buffers
447     {
448         std::unique_lock<std::mutex> lock(inMutex_);
449         inputBuffers_.clear();
450     }
451     {
452         std::unique_lock<std::mutex> lock(outMutex_);
453         outputBuffers_.clear();
454     }
455     return AudioDecoderAdapterCode::DECODER_OK;
456 }
457 
ResetDecoder()458 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::ResetDecoder()
459 {
460     WVLOG_I("%{public}s enter.", __FUNCTION__);
461     if (decoder_ == nullptr) {
462         WVLOG_E("decoder_ is nullptr.");
463         return AudioDecoderAdapterCode::DECODER_ERROR;
464     }
465 
466     OH_AVErrCode errCode = OH_AudioCodec_Reset(decoder_);
467     if (errCode != AV_ERR_OK) {
468         WVLOG_E("start reset fail, errCode = %{public}u.", uint32_t(errCode));
469         return AudioDecoderAdapterCode::DECODER_ERROR;
470     }
471 
472     // clear input and output buffers
473     {
474         std::unique_lock<std::mutex> lock(inMutex_);
475         inputBuffers_.clear();
476     }
477     {
478         std::unique_lock<std::mutex> lock(outMutex_);
479         outputBuffers_.clear();
480     }
481     return AudioDecoderAdapterCode::DECODER_OK;
482 }
483 
ReleaseDecoder()484 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::ReleaseDecoder()
485 {
486     WVLOG_I("%{public}s enter.", __FUNCTION__);
487     if (decoder_ == nullptr) {
488         WVLOG_I("decoder_ already released.");
489         return AudioDecoderAdapterCode::DECODER_OK;
490     }
491 
492     AudioDecoderCallbackManager::DeleteAudioDecoder(decoder_);
493     OH_AVErrCode errCode = OH_AudioCodec_Destroy(decoder_);
494     if (errCode != AV_ERR_OK) {
495         WVLOG_E("destroy decoder_ fail, errCode = %{public}u.", uint32_t(errCode));
496         return AudioDecoderAdapterCode::DECODER_ERROR;
497     }
498     decoder_ = nullptr;
499 
500     // clear input and output buffers
501     {
502         std::unique_lock<std::mutex> lock(inMutex_);
503         inputBuffers_.clear();
504     }
505     {
506         std::unique_lock<std::mutex> lock(outMutex_);
507         outputBuffers_.clear();
508     }
509 
510     return AudioDecoderAdapterCode::DECODER_OK;
511 }
512 
SetAVCencInfo(OH_AVCencInfo * avCencInfo,std::shared_ptr<AudioCencInfoAdapter> cencInfo)513 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::SetAVCencInfo(
514     OH_AVCencInfo *avCencInfo, std::shared_ptr<AudioCencInfoAdapter> cencInfo)
515 {
516     OH_AVErrCode errNo = OH_AVCencInfo_SetAlgorithm(avCencInfo, static_cast<DrmCencAlgorithm>(cencInfo->GetAlgo()));
517     if (errNo != AV_ERR_OK) {
518         WVLOG_E("set AVCencInfo Algorithm fail, errNo = %{public}u", static_cast<uint32_t>(errNo));
519         return AudioDecoderAdapterCode::DECODER_ERROR;
520     }
521 
522     errNo = OH_AVCencInfo_SetKeyIdAndIv(
523         avCencInfo, cencInfo->GetKeyId(), cencInfo->GetKeyIdLen(), cencInfo->GetIv(), cencInfo->GetIvLen());
524     if (errNo != AV_ERR_OK) {
525         WVLOG_E("set AVCencInfo keyid and iv fail, errNo = %{public}u",
526             static_cast<uint32_t>(errNo));
527         return AudioDecoderAdapterCode::DECODER_ERROR;
528     }
529 
530     DrmSubsample subSamples[cencInfo->GetClearHeaderLens().size()];
531     for (uint32_t i = 0; i < cencInfo->GetClearHeaderLens().size(); i++) {
532         subSamples[i].clearHeaderLen = cencInfo->GetClearHeaderLens()[i];
533         subSamples[i].payLoadLen = cencInfo->GetPayLoadLens()[i];
534     }
535     errNo = OH_AVCencInfo_SetSubsampleInfo(
536         avCencInfo, cencInfo->GetEncryptedBlockCount(), cencInfo->GetSkippedBlockCount(),
537         cencInfo->GetFirstEncryptedOffset(), cencInfo->GetClearHeaderLens().size(), subSamples);
538     if (errNo != AV_ERR_OK) {
539         WVLOG_E("set AVCencInfo subsampleInfo fail, errNo = %{public}u",
540             static_cast<uint32_t>(errNo));
541         return AudioDecoderAdapterCode::DECODER_ERROR;
542     }
543 
544     errNo = OH_AVCencInfo_SetMode(avCencInfo, DRM_CENC_INFO_KEY_IV_SUBSAMPLES_SET);
545     if (errNo != AV_ERR_OK) {
546         WVLOG_E("set AVCencInfo mode fail, errNo = %{public}u", static_cast<uint32_t>(errNo));
547         return AudioDecoderAdapterCode::DECODER_ERROR;
548     }
549     return AudioDecoderAdapterCode::DECODER_OK;
550 }
551 
SetBufferCencInfo(uint32_t index,std::shared_ptr<AudioCencInfoAdapter> cencInfo)552 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::SetBufferCencInfo(
553     uint32_t index, std::shared_ptr<AudioCencInfoAdapter> cencInfo)
554 {
555     WVLOG_I("%{public}s enter.", __FUNCTION__);
556     AudioCencInfoAdapterImpl::PrintCencInfoData(cencInfo);
557     if (cencInfo == nullptr) {
558         WVLOG_E("cencInfo is nullptr.");
559         return AudioDecoderAdapterCode::DECODER_ERROR;
560     }
561     OH_AVCencInfo *avCencInfo = OH_AVCencInfo_Create();
562     if (avCencInfo == nullptr) {
563         WVLOG_E("create AVCencInfo fail.");
564         return AudioDecoderAdapterCode::DECODER_ERROR;
565     }
566 
567     AudioDecoderAdapterCode ret = SetAVCencInfo(avCencInfo, cencInfo);
568     if (ret != AudioDecoderAdapterCode::DECODER_OK) {
569         return ret;
570     }
571     // set CencInfo to AVBuffer
572     OH_AVBuffer *avBuffer = GetInputBuffer(index);
573     if (avBuffer == nullptr) {
574         WVLOG_E("set AVCencInfo fail, not find inputbuffererr[%{public}u]", index);
575         return AudioDecoderAdapterCode::DECODER_ERROR;
576     }
577     OH_AVErrCode errNo = OH_AVCencInfo_SetAVBuffer(avCencInfo, avBuffer);
578     if (errNo != AV_ERR_OK) {
579         WVLOG_E("set AVCencInfo fail, errNo = %{public}u", static_cast<uint32_t>(errNo));
580         return AudioDecoderAdapterCode::DECODER_ERROR;
581     }
582     errNo = OH_AVCencInfo_Destroy(avCencInfo);
583     if (errNo != AV_ERR_OK) {
584         WVLOG_E("destroy cencInfo fail, errNo = %{public}u", static_cast<uint32_t>(errNo));
585         return AudioDecoderAdapterCode::DECODER_ERROR;
586     }
587     return AudioDecoderAdapterCode::DECODER_OK;
588 }
589 
QueueInputBufferDec(uint32_t index,int64_t presentationTimeUs,uint8_t * bufferData,int32_t bufferSize,std::shared_ptr<AudioCencInfoAdapter> cencInfo,bool isEncrypted,BufferFlag flag)590 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::QueueInputBufferDec(uint32_t index, int64_t presentationTimeUs,
591     uint8_t *bufferData, int32_t bufferSize, std::shared_ptr<AudioCencInfoAdapter> cencInfo,
592     bool isEncrypted, BufferFlag flag)
593 {
594     WVLOG_I("%{public}s index[%{public}u],  buffer size[%{public}d], isEncrypted[%{public}d],"
595         "flag[%{public}d].", __FUNCTION__, index, bufferSize, static_cast<uint32_t>(isEncrypted),
596         static_cast<uint32_t>(flag));
597 
598     if (decoder_ == nullptr) {
599         WVLOG_E("decoder_ is nullptr.");
600         return AudioDecoderAdapterCode::DECODER_ERROR;
601     }
602 
603     if (isEncrypted && SetBufferCencInfo(index, cencInfo) != AudioDecoderAdapterCode::DECODER_OK) {
604         return AudioDecoderAdapterCode::DECODER_ERROR;
605     }
606 
607     OH_AVBuffer *avBuffer = GetInputBuffer(index);
608     if (avBuffer == nullptr) {
609         WVLOG_E("QueueInputBufferDec fail, inputbuffer[%{public}u] not find.", index);
610         return AudioDecoderAdapterCode::DECODER_ERROR;
611     }
612     uint8_t *addr = OH_AVBuffer_GetAddr(avBuffer);
613     if (flag != GetBufferFlag(OHOS::MediaAVCodec::AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_EOS)) {
614         if (bufferData == nullptr) {
615             WVLOG_E("index[%{public}u] bufferData is nullptr.", index);
616             return AudioDecoderAdapterCode::DECODER_ERROR;
617         }
618         if (memcpy_s(addr, bufferSize, bufferData, bufferSize) != EOK) {
619             WVLOG_E(" index[%{public}u] memcpy_s buffer fail.", index);
620             return AudioDecoderAdapterCode::DECODER_ERROR;
621         }
622     }
623 
624     // The size is the length of each frame of data to be decoded. The pts is the timestamp for
625     // each frame, indicating when the audio should be played. The values of size and pts are
626     // obtained from the audio/video resource file or the data stream to be decoded.
627     OH_AVCodecBufferAttr attr = {0};
628     if (flag == GetBufferFlag(OHOS::MediaAVCodec::AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_EOS)) {
629         attr.size = 0;
630         attr.flags = OHOS::MediaAVCodec::AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_EOS;
631     } else {
632         attr.size = bufferSize;
633         attr.flags = OHOS::MediaAVCodec::AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_NONE;
634     }
635     attr.pts = presentationTimeUs;
636     OH_AVBuffer_SetBufferAttr(avBuffer, &attr);
637     OH_AVErrCode errCode = OH_AudioCodec_PushInputBuffer(decoder_, index);
638     if (errCode != AV_ERR_OK) {
639         WVLOG_E("index[%{public}u] push input buffer fail, errCode = %{public}u.",
640             index, uint32_t(errCode));
641         return AudioDecoderAdapterCode::DECODER_ERROR;
642     }
643 
644     std::unique_lock<std::mutex> lock(inMutex_);
645     inputBuffers_.erase(index);
646     return AudioDecoderAdapterCode::DECODER_OK;
647 }
648 
GetOutputFormatDec(std::shared_ptr<AudioDecoderFormatAdapter> format)649 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::GetOutputFormatDec(
650     std::shared_ptr<AudioDecoderFormatAdapter> format)
651 {
652     WVLOG_I("%{public}s enter.", __FUNCTION__);
653     if (decoder_ == nullptr) {
654         WVLOG_E("decoder_ is nullptr.");
655         return AudioDecoderAdapterCode::DECODER_ERROR;
656     }
657 
658     OH_AVFormat *avFormat = OH_AudioCodec_GetOutputDescription(decoder_);
659     if (avFormat == nullptr) {
660         WVLOG_E("get output description fail.");
661         return AudioDecoderAdapterCode::DECODER_ERROR;
662     }
663     AudioDecoderAdapterCode ret = GetParamFromAVFormat(avFormat, format);
664     if (ret != AudioDecoderAdapterCode::DECODER_OK) {
665         return ret;
666     }
667     OH_AVFormat_Destroy(avFormat);
668     avFormat = nullptr;
669 
670     return AudioDecoderAdapterCode::DECODER_OK;
671 }
672 
ReleaseOutputBufferDec(uint32_t index)673 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::ReleaseOutputBufferDec(uint32_t index)
674 {
675     WVLOG_I("%{public}s index[%{public}u] .", __FUNCTION__, index);
676     if (decoder_ == nullptr) {
677         WVLOG_E("index[%{public}u] is nullptr.", index);
678         return AudioDecoderAdapterCode::DECODER_ERROR;
679     }
680 
681     OH_AVBuffer *avBuffer = GetOutputBuffer(index);
682     if (avBuffer == nullptr) {
683         WVLOG_E("ReleaseOutputBufferDec fail, outputbuffer[%{public}u] not find.", index);
684         return AudioDecoderAdapterCode::DECODER_ERROR;
685     }
686 
687     OH_AVCodecBufferAttr attr = {0};
688     OH_AVErrCode errCode = OH_AVBuffer_GetBufferAttr(avBuffer, &attr);
689     if (errCode != AV_ERR_OK) {
690         WVLOG_E("index[%{public}u] get output buffer attr fail, errCode = %{public}u.",
691             index, uint32_t(errCode));
692         return AudioDecoderAdapterCode::DECODER_ERROR;
693     }
694 
695     errCode = OH_AudioCodec_FreeOutputBuffer(decoder_, index);
696     if (errCode != AV_ERR_OK) {
697         WVLOG_E("index[%{public}u] free output buffer fail, errCode = %{public}u.",
698             index, uint32_t(errCode));
699         return AudioDecoderAdapterCode::DECODER_ERROR;
700     }
701     {
702         std::unique_lock<std::mutex> lock(outMutex_);
703         outputBuffers_.erase(index);
704     }
705 
706     if (attr.flags == OHOS::MediaAVCodec::AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_EOS) {
707         WVLOG_I("index[%{public}u] free output buffer, buffer flag is eos.", index);
708     }
709     return AudioDecoderAdapterCode::DECODER_OK;
710 }
711 
SetCallbackDec(const std::shared_ptr<OHOS::NWeb::AudioDecoderCallbackAdapter> callback)712 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::SetCallbackDec(
713     const std::shared_ptr<OHOS::NWeb::AudioDecoderCallbackAdapter> callback)
714 {
715     WVLOG_I("%{public}s enter.", __FUNCTION__);
716     if (decoder_ == nullptr) {
717         WVLOG_E("decoder is nullptr.");
718         return AudioDecoderAdapterCode::DECODER_ERROR;
719     }
720     if (callback == nullptr) {
721         WVLOG_E("calllback is nullptr.");
722         return AudioDecoderAdapterCode::DECODER_ERROR;
723     }
724 
725     callback_ = std::make_shared<AudioDecoderCallbackAdapterImpl>(callback);
726     if (callback_ == nullptr) {
727         WVLOG_E("Create Callback failed.");
728         return AudioDecoderAdapterCode::DECODER_ERROR;
729     }
730 
731     struct OH_AVCodecCallback cb = {&AudioDecoderCallbackManager::OnError,
732         &AudioDecoderCallbackManager::OnOutputFormatChanged, &AudioDecoderCallbackManager::OnInputBufferAvailable,
733         &AudioDecoderCallbackManager::OnOutputBufferAvailable};
734 
735     // Instead of using the ADecBufferSignal structure recommended by the media framework for buffer rotation,
736     // implement it in the chromium kernel to reduce the business logic at the webview layer.
737     OH_AVErrCode errCode = OH_AudioCodec_RegisterCallback(decoder_, cb, nullptr);
738     if (errCode != AV_ERR_OK) {
739         WVLOG_E("register callback fail, errCode = %{public}u.", uint32_t(errCode));
740         return AudioDecoderAdapterCode::DECODER_ERROR;
741     }
742 
743     return AudioDecoderAdapterCode::DECODER_OK;
744 }
745 
SetDecryptionConfig(void * session,bool secureAudio)746 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::SetDecryptionConfig(void *session, bool secureAudio)
747 {
748     WVLOG_I("%{public}s, secureAudio[%{public}d].", __FUNCTION__, static_cast<uint32_t>(secureAudio));
749     if (session == nullptr) {
750         WVLOG_E("session is nullptr.");
751         return AudioDecoderAdapterCode::DECODER_ERROR;
752     }
753 
754     if (decoder_ == nullptr) {
755         WVLOG_E("decoder is nullptr.");
756         return AudioDecoderAdapterCode::DECODER_ERROR;
757     }
758 
759     // The media audio codec module currently only supports non-secure decoding mode.
760     secureAudio = false;
761 
762     MediaKeySession *mediaKeySession = static_cast<MediaKeySession*>(session);
763     OH_AVErrCode errCode = OH_AudioCodec_SetDecryptionConfig(decoder_, mediaKeySession, secureAudio);
764     if (errCode != AV_ERR_OK) {
765         WVLOG_E("set decryption config fail, errCode = %{public}u.", uint32_t(errCode));
766         return AudioDecoderAdapterCode::DECODER_ERROR;
767     }
768 
769     return AudioDecoderAdapterCode::DECODER_OK;
770 }
771 
GetBufferFlag(OHOS::MediaAVCodec::AVCodecBufferFlag codecBufferFlag)772 BufferFlag AudioCodecDecoderAdapterImpl::GetBufferFlag(OHOS::MediaAVCodec::AVCodecBufferFlag codecBufferFlag)
773 {
774     auto flag = BUFFER_FLAG_MAP.find(codecBufferFlag);
775     if (flag == BUFFER_FLAG_MAP.end()) {
776         WVLOG_E("buffer flag not found.");
777         return BufferFlag::CODEC_BUFFER_FLAG_NONE;
778     }
779     return flag->second;
780 }
781 
GetParamFromAVFormat(OH_AVFormat * avFormat,std::shared_ptr<AudioDecoderFormatAdapter> format)782 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::GetParamFromAVFormat(
783     OH_AVFormat *avFormat, std::shared_ptr<AudioDecoderFormatAdapter> format)
784 {
785     if (avFormat == nullptr || format == nullptr) {
786         WVLOG_E("avFormat or format is nullptr, avFormat is %{public}d, format is. %{public}d",
787             int32_t(avFormat == nullptr), int32_t(format == nullptr));
788         return AudioDecoderAdapterCode::DECODER_ERROR;
789     }
790 
791     int32_t sampleRate = 0;
792     int32_t channels = 0;
793     int32_t maxInputSize = 0;
794     int32_t aacIsAdts = 0;
795     int32_t sampleFormat = 0;
796     int64_t bitRate = 0;
797     int32_t idHeader = 0;
798     int32_t setupHeader = 0;
799     uint8_t *codecConfig = nullptr;
800     size_t codecConfigSize = 0;
801     OH_AVFormat_GetIntValue(avFormat, OH_MD_KEY_AUD_SAMPLE_RATE, &sampleRate);
802     OH_AVFormat_GetIntValue(avFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, &channels);
803     OH_AVFormat_GetIntValue(avFormat, OH_MD_KEY_MAX_INPUT_SIZE, &maxInputSize);
804     OH_AVFormat_GetIntValue(avFormat, OH_MD_KEY_AAC_IS_ADTS, &aacIsAdts);
805     OH_AVFormat_GetIntValue(avFormat, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, &sampleFormat);
806     OH_AVFormat_GetLongValue(avFormat, OH_MD_KEY_BITRATE, &bitRate);
807     OH_AVFormat_GetIntValue(avFormat, OH_MD_KEY_IDENTIFICATION_HEADER, &idHeader);
808     OH_AVFormat_GetIntValue(avFormat, OH_MD_KEY_SETUP_HEADER, &setupHeader);
809     OH_AVFormat_GetBuffer(avFormat, OH_MD_KEY_CODEC_CONFIG, &codecConfig, &codecConfigSize);
810 
811     format->SetSampleRate(sampleRate);
812     format->SetChannelCount(channels);
813     format->SetMaxInputSize(maxInputSize);
814     if (aacIsAdts != 0) {
815         format->SetAACIsAdts(true);
816     } else {
817         format->SetAACIsAdts(false);
818     }
819     format->SetAudioSampleFormat(sampleFormat);
820     format->SetBitRate(bitRate);
821     format->SetIdentificationHeader(idHeader);
822     format->SetSetupHeader(setupHeader);
823     format->SetCodecConfig(codecConfig);
824     format->SetCodecConfigSize(uint32_t(codecConfigSize));
825     AudioDecoderFormatAdapterImpl::PrintFormatData(format);
826     return AudioDecoderAdapterCode::DECODER_OK;
827 }
828 
829 std::map<OH_AVCodec*, OHOS::NWeb::AudioCodecDecoderAdapterImpl *> AudioDecoderCallbackManager::decoders_;
830 
831 std::mutex AudioDecoderCallbackManager::decodersMapMutex_;
832 
FindAudioDecoder(OH_AVCodec * codec)833 OHOS::NWeb::AudioCodecDecoderAdapterImpl *AudioDecoderCallbackManager::FindAudioDecoder(
834     OH_AVCodec *codec)
835 {
836     std::unique_lock<std::mutex> lock(decodersMapMutex_);
837     if (decoders_.find(codec) != decoders_.end()) {
838         return decoders_.at(codec);
839     }
840     return nullptr;
841 }
842 
AddAudioDecoder(OHOS::NWeb::AudioCodecDecoderAdapterImpl * decoder)843 void AudioDecoderCallbackManager::AddAudioDecoder(OHOS::NWeb::AudioCodecDecoderAdapterImpl *decoder)
844 {
845     if (decoder == nullptr || decoder->GetAVCodec() == nullptr) {
846         return;
847     }
848     std::unique_lock<std::mutex> lock(decodersMapMutex_);
849     if (decoders_.find(decoder->GetAVCodec()) != decoders_.end()) {
850         return;
851     }
852     decoders_[decoder->GetAVCodec()] = decoder;
853 }
854 
DeleteAudioDecoder(OH_AVCodec * codec)855 void AudioDecoderCallbackManager::DeleteAudioDecoder(OH_AVCodec *codec)
856 {
857     if (codec == nullptr) {
858         return;
859     }
860     std::unique_lock<std::mutex> lock(decodersMapMutex_);
861     auto it = decoders_.find(codec);
862     if (it != decoders_.end()) {
863         decoders_.erase(codec);
864     }
865 }
866 
OnError(OH_AVCodec * codec,int32_t errorCode,void * userData)867 void AudioDecoderCallbackManager::OnError(OH_AVCodec *codec, int32_t errorCode, void *userData)
868 {
869     (void)userData;
870     WVLOG_I("AudioDecoderCallbackManager %{public}s.", __FUNCTION__);
871     if (codec == nullptr) {
872         WVLOG_E("AudioDecoderCallbackManager avcodec is nullptr.");
873         return;
874     }
875 
876     std::unique_lock<std::mutex> lock(AudioCodecDecoderAdapterImpl::GetDecoderMutex());
877     OHOS::NWeb::AudioCodecDecoderAdapterImpl *impl = FindAudioDecoder(codec);
878     if (impl == nullptr) {
879         WVLOG_E("AudioDecoderCallbackManager not find decoder.");
880         return;
881     }
882     if (impl->GetAudioDecoderCallBack() == nullptr) {
883         WVLOG_E("audio decoder callback is nullptr.");
884         return;
885     }
886 
887     impl->GetAudioDecoderCallBack()->OnError(errorCode);
888 }
889 
OnOutputFormatChanged(OH_AVCodec * codec,OH_AVFormat * format,void * userData)890 void AudioDecoderCallbackManager::OnOutputFormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData)
891 {
892     WVLOG_I("AudioDecoderCallbackManager %{public}s.", __FUNCTION__);
893     if (codec == nullptr) {
894         WVLOG_E("AudioDecoderCallbackManager avcodec is nullptr.");
895         return;
896     }
897     std::unique_lock<std::mutex> lock(AudioCodecDecoderAdapterImpl::GetDecoderMutex());
898     OHOS::NWeb::AudioCodecDecoderAdapterImpl *impl = FindAudioDecoder(codec);
899     if (impl == nullptr) {
900         WVLOG_E("AudioDecoderCallbackManager not find decoder.");
901         return;
902     }
903     if (impl->GetAudioDecoderCallBack() == nullptr) {
904         WVLOG_E("audio decoder callback is nullptr.");
905         return;
906     }
907 
908     impl->GetAudioDecoderCallBack()->OnOutputFormatChanged();
909 }
910 
OnInputBufferAvailable(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * data,void * userData)911 void AudioDecoderCallbackManager::OnInputBufferAvailable(
912     OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData)
913 {
914     WVLOG_D("AudioDecoderCallbackManager %{public}s[%{public}u].", __FUNCTION__, index);
915     if (codec == nullptr) {
916         WVLOG_E("AudioDecoderCallbackManager::OnInputBufferAvailable avcodec is nullptr.");
917         return;
918     }
919 
920     if (data == nullptr) {
921         WVLOG_E("AudioDecoderCallbackManager::OnInputBufferAvailable avbuffer is nullptr.");
922         return;
923     }
924 
925     std::unique_lock<std::mutex> lock(AudioCodecDecoderAdapterImpl::GetDecoderMutex());
926     OHOS::NWeb::AudioCodecDecoderAdapterImpl *impl = FindAudioDecoder(codec);
927     if (impl == nullptr) {
928         WVLOG_E("AudioDecoderCallbackManager::OnInputBufferAvailable not find decoder.");
929         return;
930     }
931     if (impl->GetAudioDecoderCallBack() == nullptr) {
932         WVLOG_E("AudioDecoderCallbackManager::OnInputBufferAvailable audio decoder callback is nullptr.");
933         return;
934     }
935     impl->SetInputBuffer(index, data);
936     impl->GetAudioDecoderCallBack()->OnInputBufferAvailable(index);
937 }
938 
OnOutputBufferAvailable(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * data,void * userData)939 void AudioDecoderCallbackManager::OnOutputBufferAvailable(
940     OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData)
941 {
942     (void)userData;
943     WVLOG_I("AudioDecoderCallbackManager %{public}s.", __FUNCTION__);
944     if (codec == nullptr) {
945         WVLOG_E("AudioDecoderCallbackManager avcodec is nullptr.");
946         return;
947     }
948 
949     std::unique_lock<std::mutex> lock(AudioCodecDecoderAdapterImpl::GetDecoderMutex());
950     OHOS::NWeb::AudioCodecDecoderAdapterImpl *impl = FindAudioDecoder(codec);
951     if (impl == nullptr) {
952         WVLOG_E("AudioDecoderCallbackManager not find decoder.");
953         return;
954     }
955     if (impl->GetAudioDecoderCallBack() == nullptr) {
956         WVLOG_E("audio decoder callback is nullptr.");
957         return;
958     }
959     impl->SetOutputBuffer(index, data);
960 
961     OH_AVCodecBufferAttr attr = {0};
962     OH_AVErrCode errCode = OH_AVBuffer_GetBufferAttr(data, &attr);
963     if (errCode != AV_ERR_OK || attr.size < 0) {
964         WVLOG_E(" get buffer attr fail.");
965         return;
966     }
967 
968     uint8_t bufferData[attr.size];
969     if (memcpy_s(bufferData, sizeof(bufferData), reinterpret_cast<uint8_t *>(OH_AVBuffer_GetAddr(data)),
970         attr.size) != EOK) {
971         WVLOG_E(" memcpy_s buffer fail.");
972         return;
973     }
974     // Copy the buffer data from the data.
975     impl->GetAudioDecoderCallBack()->OnOutputBufferAvailable(
976         index, bufferData, attr.size, attr.pts, attr.offset, attr.flags);
977 }
978 
979 } // namespace OHOS::NWeb
980