• 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.", static_cast<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.", static_cast<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.", static_cast<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.", static_cast<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.", static_cast<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.", static_cast<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.", static_cast<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.", static_cast<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.", static_cast<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}u],"
595         "flag[%{public}u].", __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     int32_t bufferCapacity = OH_AVBuffer_GetCapacity(avBuffer);
609     if (avBuffer == nullptr || bufferCapacity < bufferSize) {
610         WVLOG_E("QueueInputBufferDec fail, inputbuffer[%{public}u] not find or cap size less than buffer size.", index);
611         return AudioDecoderAdapterCode::DECODER_ERROR;
612     }
613     uint8_t *addr = OH_AVBuffer_GetAddr(avBuffer);
614     if (flag != GetBufferFlag(OHOS::MediaAVCodec::AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_EOS)) {
615         if (bufferData == nullptr) {
616             WVLOG_E("index[%{public}u] bufferData is nullptr.", index);
617             return AudioDecoderAdapterCode::DECODER_ERROR;
618         }
619         if (memcpy_s(addr, bufferCapacity, bufferData, bufferSize) != EOK) {
620             WVLOG_E(" index[%{public}u] memcpy_s buffer fail.", index);
621             return AudioDecoderAdapterCode::DECODER_ERROR;
622         }
623     }
624 
625     // The size is the length of each frame of data to be decoded. The pts is the timestamp for
626     // each frame, indicating when the audio should be played. The values of size and pts are
627     // obtained from the audio/video resource file or the data stream to be decoded.
628     OH_AVCodecBufferAttr attr = {0};
629     if (flag == GetBufferFlag(OHOS::MediaAVCodec::AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_EOS)) {
630         attr.size = 0;
631         attr.flags = OHOS::MediaAVCodec::AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_EOS;
632     } else {
633         attr.size = bufferSize;
634         attr.flags = OHOS::MediaAVCodec::AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_NONE;
635     }
636     attr.pts = presentationTimeUs;
637     OH_AVBuffer_SetBufferAttr(avBuffer, &attr);
638     OH_AVErrCode errCode = OH_AudioCodec_PushInputBuffer(decoder_, index);
639     if (errCode != AV_ERR_OK) {
640         WVLOG_E("index[%{public}u] push input buffer fail, errCode = %{public}u.",
641             index, static_cast<uint32_t>(errCode));
642         return AudioDecoderAdapterCode::DECODER_ERROR;
643     }
644 
645     std::unique_lock<std::mutex> lock(inMutex_);
646     inputBuffers_.erase(index);
647     return AudioDecoderAdapterCode::DECODER_OK;
648 }
649 
GetOutputFormatDec(std::shared_ptr<AudioDecoderFormatAdapter> format)650 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::GetOutputFormatDec(
651     std::shared_ptr<AudioDecoderFormatAdapter> format)
652 {
653     WVLOG_I("%{public}s enter.", __FUNCTION__);
654     if (decoder_ == nullptr) {
655         WVLOG_E("decoder_ is nullptr.");
656         return AudioDecoderAdapterCode::DECODER_ERROR;
657     }
658 
659     OH_AVFormat *avFormat = OH_AudioCodec_GetOutputDescription(decoder_);
660     if (avFormat == nullptr) {
661         WVLOG_E("get output description fail.");
662         return AudioDecoderAdapterCode::DECODER_ERROR;
663     }
664     AudioDecoderAdapterCode ret = GetParamFromAVFormat(avFormat, format);
665     if (ret != AudioDecoderAdapterCode::DECODER_OK) {
666         return ret;
667     }
668     OH_AVFormat_Destroy(avFormat);
669     avFormat = nullptr;
670 
671     return AudioDecoderAdapterCode::DECODER_OK;
672 }
673 
ReleaseOutputBufferDec(uint32_t index)674 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::ReleaseOutputBufferDec(uint32_t index)
675 {
676     WVLOG_I("%{public}s index[%{public}u] .", __FUNCTION__, index);
677     if (decoder_ == nullptr) {
678         WVLOG_E("index[%{public}u] is nullptr.", index);
679         return AudioDecoderAdapterCode::DECODER_ERROR;
680     }
681 
682     OH_AVBuffer *avBuffer = GetOutputBuffer(index);
683     if (avBuffer == nullptr) {
684         WVLOG_E("ReleaseOutputBufferDec fail, outputbuffer[%{public}u] not find.", index);
685         return AudioDecoderAdapterCode::DECODER_ERROR;
686     }
687 
688     OH_AVCodecBufferAttr attr = {0};
689     OH_AVErrCode errCode = OH_AVBuffer_GetBufferAttr(avBuffer, &attr);
690     if (errCode != AV_ERR_OK) {
691         WVLOG_E("index[%{public}u] get output buffer attr fail, errCode = %{public}u.",
692             index, static_cast<uint32_t>(errCode));
693         return AudioDecoderAdapterCode::DECODER_ERROR;
694     }
695 
696     errCode = OH_AudioCodec_FreeOutputBuffer(decoder_, index);
697     if (errCode != AV_ERR_OK) {
698         WVLOG_E("index[%{public}u] free output buffer fail, errCode = %{public}u.",
699             index, static_cast<uint32_t>(errCode));
700         return AudioDecoderAdapterCode::DECODER_ERROR;
701     }
702     {
703         std::unique_lock<std::mutex> lock(outMutex_);
704         outputBuffers_.erase(index);
705     }
706 
707     if (attr.flags == OHOS::MediaAVCodec::AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_EOS) {
708         WVLOG_I("index[%{public}u] free output buffer, buffer flag is eos.", index);
709     }
710     return AudioDecoderAdapterCode::DECODER_OK;
711 }
712 
SetCallbackDec(const std::shared_ptr<OHOS::NWeb::AudioDecoderCallbackAdapter> callback)713 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::SetCallbackDec(
714     const std::shared_ptr<OHOS::NWeb::AudioDecoderCallbackAdapter> callback)
715 {
716     WVLOG_I("%{public}s enter.", __FUNCTION__);
717     if (decoder_ == nullptr) {
718         WVLOG_E("decoder is nullptr.");
719         return AudioDecoderAdapterCode::DECODER_ERROR;
720     }
721     if (callback == nullptr) {
722         WVLOG_E("calllback is nullptr.");
723         return AudioDecoderAdapterCode::DECODER_ERROR;
724     }
725 
726     callback_ = std::make_shared<AudioDecoderCallbackAdapterImpl>(callback);
727     if (callback_ == nullptr) {
728         WVLOG_E("Create Callback failed.");
729         return AudioDecoderAdapterCode::DECODER_ERROR;
730     }
731 
732     struct OH_AVCodecCallback cb = {&AudioDecoderCallbackManager::OnError,
733         &AudioDecoderCallbackManager::OnOutputFormatChanged, &AudioDecoderCallbackManager::OnInputBufferAvailable,
734         &AudioDecoderCallbackManager::OnOutputBufferAvailable};
735 
736     // Instead of using the ADecBufferSignal structure recommended by the media framework for buffer rotation,
737     // implement it in the chromium kernel to reduce the business logic at the webview layer.
738     OH_AVErrCode errCode = OH_AudioCodec_RegisterCallback(decoder_, cb, nullptr);
739     if (errCode != AV_ERR_OK) {
740         WVLOG_E("register callback fail, errCode = %{public}u.", static_cast<uint32_t>(errCode));
741         return AudioDecoderAdapterCode::DECODER_ERROR;
742     }
743 
744     return AudioDecoderAdapterCode::DECODER_OK;
745 }
746 
SetDecryptionConfig(void * session,bool secureAudio)747 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::SetDecryptionConfig(void *session, bool secureAudio)
748 {
749     WVLOG_I("%{public}s, secureAudio[%{public}u].", __FUNCTION__, static_cast<uint32_t>(secureAudio));
750     if (session == nullptr) {
751         WVLOG_E("session is nullptr.");
752         return AudioDecoderAdapterCode::DECODER_ERROR;
753     }
754 
755     if (decoder_ == nullptr) {
756         WVLOG_E("decoder is nullptr.");
757         return AudioDecoderAdapterCode::DECODER_ERROR;
758     }
759 
760     // The media audio codec module currently only supports non-secure decoding mode.
761     secureAudio = false;
762 
763     MediaKeySession *mediaKeySession = static_cast<MediaKeySession*>(session);
764     OH_AVErrCode errCode = OH_AudioCodec_SetDecryptionConfig(decoder_, mediaKeySession, secureAudio);
765     if (errCode != AV_ERR_OK) {
766         WVLOG_E("set decryption config fail, errCode = %{public}u.", static_cast<uint32_t>(errCode));
767         return AudioDecoderAdapterCode::DECODER_ERROR;
768     }
769 
770     return AudioDecoderAdapterCode::DECODER_OK;
771 }
772 
GetBufferFlag(OHOS::MediaAVCodec::AVCodecBufferFlag codecBufferFlag)773 BufferFlag AudioCodecDecoderAdapterImpl::GetBufferFlag(OHOS::MediaAVCodec::AVCodecBufferFlag codecBufferFlag)
774 {
775     auto flag = BUFFER_FLAG_MAP.find(codecBufferFlag);
776     if (flag == BUFFER_FLAG_MAP.end()) {
777         WVLOG_E("buffer flag not found.");
778         return BufferFlag::CODEC_BUFFER_FLAG_NONE;
779     }
780     return flag->second;
781 }
782 
GetParamFromAVFormat(OH_AVFormat * avFormat,std::shared_ptr<AudioDecoderFormatAdapter> format)783 AudioDecoderAdapterCode AudioCodecDecoderAdapterImpl::GetParamFromAVFormat(
784     OH_AVFormat *avFormat, std::shared_ptr<AudioDecoderFormatAdapter> format)
785 {
786     if (avFormat == nullptr || format == nullptr) {
787         WVLOG_E("avFormat or format is nullptr, avFormat is %{public}u, format is. %{public}u",
788             static_cast<uint32_t>(avFormat == nullptr), static_cast<uint32_t>(format == nullptr));
789         return AudioDecoderAdapterCode::DECODER_ERROR;
790     }
791 
792     int32_t sampleRate = 0;
793     int32_t channels = 0;
794     int32_t maxInputSize = 0;
795     int32_t aacIsAdts = 0;
796     int32_t sampleFormat = 0;
797     int64_t bitRate = 0;
798     int32_t idHeader = 0;
799     int32_t setupHeader = 0;
800     uint8_t *codecConfig = nullptr;
801     size_t codecConfigSize = 0;
802     OH_AVFormat_GetIntValue(avFormat, OH_MD_KEY_AUD_SAMPLE_RATE, &sampleRate);
803     OH_AVFormat_GetIntValue(avFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, &channels);
804     OH_AVFormat_GetIntValue(avFormat, OH_MD_KEY_MAX_INPUT_SIZE, &maxInputSize);
805     OH_AVFormat_GetIntValue(avFormat, OH_MD_KEY_AAC_IS_ADTS, &aacIsAdts);
806     OH_AVFormat_GetIntValue(avFormat, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, &sampleFormat);
807     OH_AVFormat_GetLongValue(avFormat, OH_MD_KEY_BITRATE, &bitRate);
808     OH_AVFormat_GetIntValue(avFormat, OH_MD_KEY_IDENTIFICATION_HEADER, &idHeader);
809     OH_AVFormat_GetIntValue(avFormat, OH_MD_KEY_SETUP_HEADER, &setupHeader);
810     OH_AVFormat_GetBuffer(avFormat, OH_MD_KEY_CODEC_CONFIG, &codecConfig, &codecConfigSize);
811 
812     format->SetSampleRate(sampleRate);
813     format->SetChannelCount(channels);
814     format->SetMaxInputSize(maxInputSize);
815     if (aacIsAdts != 0) {
816         format->SetAACIsAdts(true);
817     } else {
818         format->SetAACIsAdts(false);
819     }
820     format->SetAudioSampleFormat(sampleFormat);
821     format->SetBitRate(bitRate);
822     format->SetIdentificationHeader(idHeader);
823     format->SetSetupHeader(setupHeader);
824     format->SetCodecConfig(codecConfig);
825     format->SetCodecConfigSize(static_cast<uint32_t>(codecConfigSize));
826     AudioDecoderFormatAdapterImpl::PrintFormatData(format);
827     return AudioDecoderAdapterCode::DECODER_OK;
828 }
829 
830 std::map<OH_AVCodec*, OHOS::NWeb::AudioCodecDecoderAdapterImpl *> AudioDecoderCallbackManager::decoders_;
831 
832 std::mutex AudioDecoderCallbackManager::decodersMapMutex_;
833 
FindAudioDecoder(OH_AVCodec * codec)834 OHOS::NWeb::AudioCodecDecoderAdapterImpl *AudioDecoderCallbackManager::FindAudioDecoder(
835     OH_AVCodec *codec)
836 {
837     std::unique_lock<std::mutex> lock(decodersMapMutex_);
838     if (decoders_.find(codec) != decoders_.end()) {
839         return decoders_.at(codec);
840     }
841     return nullptr;
842 }
843 
AddAudioDecoder(OHOS::NWeb::AudioCodecDecoderAdapterImpl * decoder)844 void AudioDecoderCallbackManager::AddAudioDecoder(OHOS::NWeb::AudioCodecDecoderAdapterImpl *decoder)
845 {
846     if (decoder == nullptr || decoder->GetAVCodec() == nullptr) {
847         return;
848     }
849     std::unique_lock<std::mutex> lock(decodersMapMutex_);
850     if (decoders_.find(decoder->GetAVCodec()) != decoders_.end()) {
851         return;
852     }
853     decoders_[decoder->GetAVCodec()] = decoder;
854 }
855 
DeleteAudioDecoder(OH_AVCodec * codec)856 void AudioDecoderCallbackManager::DeleteAudioDecoder(OH_AVCodec *codec)
857 {
858     if (codec == nullptr) {
859         return;
860     }
861     std::unique_lock<std::mutex> lock(decodersMapMutex_);
862     auto it = decoders_.find(codec);
863     if (it != decoders_.end()) {
864         decoders_.erase(codec);
865     }
866 }
867 
OnError(OH_AVCodec * codec,int32_t errorCode,void * userData)868 void AudioDecoderCallbackManager::OnError(OH_AVCodec *codec, int32_t errorCode, void *userData)
869 {
870     (void)userData;
871     WVLOG_I("AudioDecoderCallbackManager %{public}s.", __FUNCTION__);
872     if (codec == nullptr) {
873         WVLOG_E("AudioDecoderCallbackManager avcodec is nullptr.");
874         return;
875     }
876 
877     std::unique_lock<std::mutex> lock(AudioCodecDecoderAdapterImpl::GetDecoderMutex());
878     OHOS::NWeb::AudioCodecDecoderAdapterImpl *impl = FindAudioDecoder(codec);
879     if (impl == nullptr) {
880         WVLOG_E("AudioDecoderCallbackManager not find decoder.");
881         return;
882     }
883     if (impl->GetAudioDecoderCallBack() == nullptr) {
884         WVLOG_E("audio decoder callback is nullptr.");
885         return;
886     }
887 
888     impl->GetAudioDecoderCallBack()->OnError(errorCode);
889 }
890 
OnOutputFormatChanged(OH_AVCodec * codec,OH_AVFormat * format,void * userData)891 void AudioDecoderCallbackManager::OnOutputFormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData)
892 {
893     WVLOG_I("AudioDecoderCallbackManager %{public}s.", __FUNCTION__);
894     if (codec == nullptr) {
895         WVLOG_E("AudioDecoderCallbackManager avcodec is nullptr.");
896         return;
897     }
898     std::unique_lock<std::mutex> lock(AudioCodecDecoderAdapterImpl::GetDecoderMutex());
899     OHOS::NWeb::AudioCodecDecoderAdapterImpl *impl = FindAudioDecoder(codec);
900     if (impl == nullptr) {
901         WVLOG_E("AudioDecoderCallbackManager not find decoder.");
902         return;
903     }
904     if (impl->GetAudioDecoderCallBack() == nullptr) {
905         WVLOG_E("audio decoder callback is nullptr.");
906         return;
907     }
908 
909     impl->GetAudioDecoderCallBack()->OnOutputFormatChanged();
910 }
911 
OnInputBufferAvailable(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * data,void * userData)912 void AudioDecoderCallbackManager::OnInputBufferAvailable(
913     OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData)
914 {
915     WVLOG_D("AudioDecoderCallbackManager %{public}s[%{public}u].", __FUNCTION__, index);
916     if (codec == nullptr) {
917         WVLOG_E("AudioDecoderCallbackManager::OnInputBufferAvailable avcodec is nullptr.");
918         return;
919     }
920 
921     if (data == nullptr) {
922         WVLOG_E("AudioDecoderCallbackManager::OnInputBufferAvailable avbuffer is nullptr.");
923         return;
924     }
925 
926     std::unique_lock<std::mutex> lock(AudioCodecDecoderAdapterImpl::GetDecoderMutex());
927     OHOS::NWeb::AudioCodecDecoderAdapterImpl *impl = FindAudioDecoder(codec);
928     if (impl == nullptr) {
929         WVLOG_E("AudioDecoderCallbackManager::OnInputBufferAvailable not find decoder.");
930         return;
931     }
932     if (impl->GetAudioDecoderCallBack() == nullptr) {
933         WVLOG_E("AudioDecoderCallbackManager::OnInputBufferAvailable audio decoder callback is nullptr.");
934         return;
935     }
936     impl->SetInputBuffer(index, data);
937     impl->GetAudioDecoderCallBack()->OnInputBufferAvailable(index);
938 }
939 
OnOutputBufferAvailable(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * data,void * userData)940 void AudioDecoderCallbackManager::OnOutputBufferAvailable(
941     OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData)
942 {
943     (void)userData;
944     WVLOG_I("AudioDecoderCallbackManager %{public}s.", __FUNCTION__);
945     if (codec == nullptr) {
946         WVLOG_E("AudioDecoderCallbackManager avcodec is nullptr.");
947         return;
948     }
949 
950     std::unique_lock<std::mutex> lock(AudioCodecDecoderAdapterImpl::GetDecoderMutex());
951     OHOS::NWeb::AudioCodecDecoderAdapterImpl *impl = FindAudioDecoder(codec);
952     if (impl == nullptr) {
953         WVLOG_E("AudioDecoderCallbackManager not find decoder.");
954         return;
955     }
956     if (impl->GetAudioDecoderCallBack() == nullptr) {
957         WVLOG_E("audio decoder callback is nullptr.");
958         return;
959     }
960     impl->SetOutputBuffer(index, data);
961 
962     OH_AVCodecBufferAttr attr = {0};
963     OH_AVErrCode errCode = OH_AVBuffer_GetBufferAttr(data, &attr);
964     if (errCode != AV_ERR_OK || attr.size < 0) {
965         WVLOG_E(" get buffer attr fail.");
966         return;
967     }
968 
969     uint8_t bufferData[attr.size];
970     if (memcpy_s(bufferData, sizeof(bufferData), reinterpret_cast<uint8_t *>(OH_AVBuffer_GetAddr(data)),
971         attr.size) != EOK) {
972         WVLOG_E(" memcpy_s buffer fail.");
973         return;
974     }
975     // Copy the buffer data from the data.
976     impl->GetAudioDecoderCallBack()->OnOutputBufferAvailable(
977         index, bufferData, attr.size, attr.pts, attr.offset, attr.flags);
978 }
979 
980 } // namespace OHOS::NWeb
981