• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 <list>
17 #include <mutex>
18 #include <shared_mutex>
19 #include "avcodec_audio_decoder.h"
20 #include "avcodec_errors.h"
21 #include "avcodec_log.h"
22 #include "avsharedmemory.h"
23 #include "native_avcodec_audiodecoder.h"
24 #include "native_avcodec_base.h"
25 #include "native_avmagic.h"
26 
27 namespace {
28 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "NativeAudioDecoder"};
29 }
30 
31 using namespace OHOS::MediaAVCodec;
32 class NativeAudioDecoder;
33 
34 struct AudioDecoderObject : public OH_AVCodec {
AudioDecoderObjectAudioDecoderObject35     explicit AudioDecoderObject(const std::shared_ptr<AVCodecAudioDecoder> &decoder)
36         : OH_AVCodec(AVMagic::AVCODEC_MAGIC_AUDIO_DECODER), audioDecoder_(decoder) {}
37     ~AudioDecoderObject() = default;
38 
39     const std::shared_ptr<AVCodecAudioDecoder> audioDecoder_;
40     std::list<OHOS::sptr<OH_AVMemory>> memoryObjList_;
41     std::shared_ptr<NativeAudioDecoder> callback_ = nullptr;
42     std::atomic<bool> isFlushing_ = false;
43     std::atomic<bool> isFlushed_ = false;
44     std::atomic<bool> isStop_ = false;
45     std::atomic<bool> isEOS_ = false;
46     std::shared_mutex memoryObjListMutex_;
47 };
48 
49 class NativeAudioDecoder : public AVCodecCallback {
50 public:
NativeAudioDecoder(OH_AVCodec * codec,struct OH_AVCodecAsyncCallback cb,void * userData)51     NativeAudioDecoder(OH_AVCodec *codec, struct OH_AVCodecAsyncCallback cb, void *userData)
52         : codec_(codec), callback_(cb), userData_(userData) {}
53     virtual ~NativeAudioDecoder() = default;
54 
OnError(AVCodecErrorType errorType,int32_t errorCode)55     void OnError(AVCodecErrorType errorType, int32_t errorCode) override
56     {
57         std::unique_lock<std::shared_mutex> lock(mutex_);
58         (void)errorType;
59         if (codec_ != nullptr && callback_.onError != nullptr) {
60             int32_t extErr = AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(errorCode));
61             callback_.onError(codec_, extErr, userData_);
62         }
63     }
64 
OnOutputFormatChanged(const Format & format)65     void OnOutputFormatChanged(const Format &format) override
66     {
67         std::unique_lock<std::shared_mutex> lock(mutex_);
68         if (codec_ != nullptr && callback_.onStreamChanged != nullptr) {
69             OHOS::sptr<OH_AVFormat> object = new(std::nothrow) OH_AVFormat(format);
70             // The object lifecycle is controlled by the current function stack
71             callback_.onStreamChanged(codec_, reinterpret_cast<OH_AVFormat *>(object.GetRefPtr()), userData_);
72         }
73     }
74 
OnInputBufferAvailable(uint32_t index,std::shared_ptr<AVSharedMemory> buffer)75     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer) override
76     {
77         std::shared_lock<std::shared_mutex> lock(mutex_);
78         if (codec_ != nullptr && callback_.onNeedInputData != nullptr) {
79             struct AudioDecoderObject *audioDecObj = reinterpret_cast<AudioDecoderObject *>(codec_);
80             CHECK_AND_RETURN_LOG(audioDecObj->audioDecoder_ != nullptr, "audioDecoder_ is nullptr!");
81             if (audioDecObj->isFlushing_.load() || audioDecObj->isFlushed_.load() || audioDecObj->isStop_.load() ||
82                 audioDecObj->isEOS_.load()) {
83                 AVCODEC_LOGD("At flush, eos or stop, no buffer available");
84                 return;
85             }
86 
87             OH_AVMemory *data = GetInputData(codec_, index, buffer);
88             callback_.onNeedInputData(codec_, index, data, userData_);
89         }
90     }
91 
OnOutputBufferAvailable(uint32_t index,AVCodecBufferInfo info,AVCodecBufferFlag flag,std::shared_ptr<AVSharedMemory> buffer)92     void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag,
93                                  std::shared_ptr<AVSharedMemory> buffer) override
94     {
95         std::shared_lock<std::shared_mutex> lock(mutex_);
96         if (codec_ != nullptr && callback_.onNeedOutputData != nullptr) {
97             struct AudioDecoderObject *audioDecObj = reinterpret_cast<AudioDecoderObject *>(codec_);
98             CHECK_AND_RETURN_LOG(audioDecObj->audioDecoder_ != nullptr, "audioDecoder_ is nullptr!");
99             if (audioDecObj->isFlushing_.load() || audioDecObj->isFlushed_.load() || audioDecObj->isStop_.load()) {
100                 AVCODEC_LOGD("At flush or stop, ignore");
101                 return;
102             }
103 
104             struct OH_AVCodecBufferAttr bufferAttr;
105             bufferAttr.pts = info.presentationTimeUs;
106             bufferAttr.size = info.size;
107             bufferAttr.offset = info.offset;
108             bufferAttr.flags = flag;
109             // The bufferInfo lifecycle is controlled by the current function stack
110             OH_AVMemory *data = GetOutputData(codec_, index, buffer);
111             callback_.onNeedOutputData(codec_, index, data, &bufferAttr, userData_);
112         }
113     }
114 
StopCallback()115     void StopCallback()
116     {
117         std::unique_lock<std::shared_mutex> lock(mutex_);
118         codec_ = nullptr;
119     }
120 
121 private:
GetInputData(struct OH_AVCodec * codec,uint32_t index,std::shared_ptr<AVSharedMemory> memory)122     OH_AVMemory *GetInputData(struct OH_AVCodec *codec, uint32_t index, std::shared_ptr<AVSharedMemory> memory)
123     {
124         CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "input codec is nullptr!");
125         CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER, nullptr, "magic error!");
126 
127         struct AudioDecoderObject *audioDecObj = reinterpret_cast<AudioDecoderObject *>(codec);
128         CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, nullptr, "audioDecoder_ is nullptr!");
129         CHECK_AND_RETURN_RET_LOG(memory != nullptr, nullptr, "get input buffer is nullptr!");
130 
131         {
132             std::shared_lock<std::shared_mutex> lock(audioDecObj->memoryObjListMutex_);
133             for (auto &memoryObj : audioDecObj->memoryObjList_) {
134                 if (memoryObj->IsEqualMemory(memory)) {
135                     return reinterpret_cast<OH_AVMemory *>(memoryObj.GetRefPtr());
136                 }
137             }
138         }
139 
140         OHOS::sptr<OH_AVMemory> object = new(std::nothrow) OH_AVMemory(memory);
141         CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new OH_AVMemory");
142 
143         std::lock_guard<std::shared_mutex> lock(audioDecObj->memoryObjListMutex_);
144         audioDecObj->memoryObjList_.push_back(object);
145         return reinterpret_cast<OH_AVMemory *>(object.GetRefPtr());
146     }
147 
GetOutputData(struct OH_AVCodec * codec,uint32_t index,std::shared_ptr<AVSharedMemory> memory)148     OH_AVMemory *GetOutputData(struct OH_AVCodec *codec, uint32_t index, std::shared_ptr<AVSharedMemory> memory)
149     {
150         CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "input codec is nullptr!");
151         CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER, nullptr, "magic error!");
152 
153         struct AudioDecoderObject *audioDecObj = reinterpret_cast<AudioDecoderObject *>(codec);
154         CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, nullptr, "audioDecoder_ is nullptr!");
155         CHECK_AND_RETURN_RET_LOG(memory != nullptr, nullptr, "get output buffer is nullptr!");
156 
157         {
158             std::shared_lock<std::shared_mutex> lock(audioDecObj->memoryObjListMutex_);
159             for (auto &memoryObj : audioDecObj->memoryObjList_) {
160                 if (memoryObj->IsEqualMemory(memory)) {
161                     return reinterpret_cast<OH_AVMemory *>(memoryObj.GetRefPtr());
162                 }
163             }
164         }
165 
166         OHOS::sptr<OH_AVMemory> object = new(std::nothrow) OH_AVMemory(memory);
167         CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new OH_AVMemory");
168 
169         std::lock_guard<std::shared_mutex> lock(audioDecObj->memoryObjListMutex_);
170         audioDecObj->memoryObjList_.push_back(object);
171         return reinterpret_cast<OH_AVMemory *>(object.GetRefPtr());
172     }
173 
174     struct OH_AVCodec *codec_;
175     struct OH_AVCodecAsyncCallback callback_;
176     void *userData_;
177     std::shared_mutex mutex_;
178 };
179 
180 namespace OHOS {
181 namespace MediaAVCodec {
182 #ifdef __cplusplus
183  extern "C" {
184 #endif
185 
OH_AudioDecoder_CreateByMime(const char * mime)186 struct OH_AVCodec *OH_AudioDecoder_CreateByMime(const char *mime)
187 {
188     CHECK_AND_RETURN_RET_LOG(mime != nullptr, nullptr, "input mime is nullptr!");
189 
190     std::shared_ptr<AVCodecAudioDecoder> audioDecoder = AudioDecoderFactory::CreateByMime(mime);
191     CHECK_AND_RETURN_RET_LOG(audioDecoder != nullptr, nullptr, "failed to AudioDecoderFactory::CreateByMime");
192 
193     struct AudioDecoderObject *object = new(std::nothrow) AudioDecoderObject(audioDecoder);
194     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new AudioDecoderObject");
195 
196     return object;
197 }
198 
OH_AudioDecoder_CreateByName(const char * name)199 struct OH_AVCodec *OH_AudioDecoder_CreateByName(const char *name)
200 {
201     CHECK_AND_RETURN_RET_LOG(name != nullptr, nullptr, "input name is nullptr!");
202 
203     std::shared_ptr<AVCodecAudioDecoder> audioDecoder = AudioDecoderFactory::CreateByName(name);
204     CHECK_AND_RETURN_RET_LOG(audioDecoder != nullptr, nullptr, "failed to AudioDecoderFactory::CreateByMime");
205 
206     struct AudioDecoderObject *object = new(std::nothrow) AudioDecoderObject(audioDecoder);
207     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new AudioDecoderObject");
208 
209     return object;
210 }
211 
OH_AudioDecoder_Destroy(struct OH_AVCodec * codec)212 OH_AVErrCode OH_AudioDecoder_Destroy(struct OH_AVCodec *codec)
213 {
214     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
215     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
216         AV_ERR_INVALID_VAL, "magic error!");
217 
218     struct AudioDecoderObject *audioDecObj = reinterpret_cast<AudioDecoderObject *>(codec);
219 
220     if (audioDecObj != nullptr && audioDecObj->audioDecoder_ != nullptr) {
221         if (audioDecObj->callback_ != nullptr) {
222             audioDecObj->callback_->StopCallback();
223         }
224         {
225             std::lock_guard<std::shared_mutex> lock(audioDecObj->memoryObjListMutex_);
226             audioDecObj->memoryObjList_.clear();
227         }
228         audioDecObj->isStop_.store(true);
229         int32_t ret = audioDecObj->audioDecoder_->Release();
230         if (ret != AVCS_ERR_OK) {
231             AVCODEC_LOGE("audioDecoder Release failed!");
232             delete codec;
233             return AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret));
234         }
235     } else {
236         AVCODEC_LOGD("audioDecoder_ is nullptr!");
237     }
238 
239     delete codec;
240     return AV_ERR_OK;
241 }
242 
OH_AudioDecoder_Configure(struct OH_AVCodec * codec,struct OH_AVFormat * format)243 OH_AVErrCode OH_AudioDecoder_Configure(struct OH_AVCodec *codec, struct OH_AVFormat *format)
244 {
245     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
246     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
247         AV_ERR_INVALID_VAL, "magic error!");
248     CHECK_AND_RETURN_RET_LOG(format != nullptr, AV_ERR_INVALID_VAL, "input format is nullptr!");
249     CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, AV_ERR_INVALID_VAL, "magic error!");
250 
251     struct AudioDecoderObject *audioDecObj = reinterpret_cast<AudioDecoderObject *>(codec);
252     CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, AV_ERR_INVALID_VAL, "audioDecoder is nullptr!");
253 
254     int32_t ret = audioDecObj->audioDecoder_->Configure(format->format_);
255     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
256                              "audioDecoder Configure failed!");
257 
258     return AV_ERR_OK;
259 }
260 
OH_AudioDecoder_Prepare(struct OH_AVCodec * codec)261 OH_AVErrCode OH_AudioDecoder_Prepare(struct OH_AVCodec *codec)
262 {
263     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
264     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
265         AV_ERR_INVALID_VAL, "magic error!");
266 
267     struct AudioDecoderObject *audioDecObj = reinterpret_cast<AudioDecoderObject *>(codec);
268     CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, AV_ERR_INVALID_VAL, "audioDecoder_ is nullptr!");
269 
270     int32_t ret = audioDecObj->audioDecoder_->Prepare();
271     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
272                              "audioDecoder Prepare failed!");
273     return AV_ERR_OK;
274 }
275 
OH_AudioDecoder_Start(struct OH_AVCodec * codec)276 OH_AVErrCode OH_AudioDecoder_Start(struct OH_AVCodec *codec)
277 {
278     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
279     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
280         AV_ERR_INVALID_VAL, "magic error!");
281 
282     struct AudioDecoderObject *audioDecObj = reinterpret_cast<AudioDecoderObject *>(codec);
283     CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, AV_ERR_INVALID_VAL, "audioDecoder_ is nullptr!");
284     audioDecObj->isStop_.store(false);
285     audioDecObj->isEOS_.store(false);
286     audioDecObj->isFlushed_.store(false);
287     int32_t ret = audioDecObj->audioDecoder_->Start();
288     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
289                              "audioDecoder Start failed!");
290 
291     return AV_ERR_OK;
292 }
293 
OH_AudioDecoder_Stop(struct OH_AVCodec * codec)294 OH_AVErrCode OH_AudioDecoder_Stop(struct OH_AVCodec *codec)
295 {
296     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
297     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
298         AV_ERR_INVALID_VAL, "magic error!");
299 
300     struct AudioDecoderObject *audioDecObj = reinterpret_cast<AudioDecoderObject *>(codec);
301     CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, AV_ERR_INVALID_VAL, "audioDecoder_ is nullptr!");
302 
303     audioDecObj->isStop_.store(true);
304     AVCODEC_LOGD("set stop status to true");
305 
306     int32_t ret = audioDecObj->audioDecoder_->Stop();
307     if (ret != AVCS_ERR_OK) {
308         audioDecObj->isStop_.store(false);
309         AVCODEC_LOGE("audioDecoder Stop failed!, set stop status to false");
310         return AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret));
311     }
312     std::lock_guard<std::shared_mutex> lock(audioDecObj->memoryObjListMutex_);
313     audioDecObj->memoryObjList_.clear();
314 
315     return AV_ERR_OK;
316 }
317 
OH_AudioDecoder_Flush(struct OH_AVCodec * codec)318 OH_AVErrCode OH_AudioDecoder_Flush(struct OH_AVCodec *codec)
319 {
320     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
321     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
322         AV_ERR_INVALID_VAL, "magic error!");
323 
324     struct AudioDecoderObject *audioDecObj = reinterpret_cast<AudioDecoderObject *>(codec);
325     CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, AV_ERR_INVALID_VAL, "audioDecoder_ is nullptr!");
326     audioDecObj->isFlushing_.store(true);
327     AVCODEC_LOGD("Set flush status to true");
328     int32_t ret = audioDecObj->audioDecoder_->Flush();
329     if (ret != AVCS_ERR_OK) {
330         audioDecObj->isFlushing_.store(false);
331         audioDecObj->isFlushed_.store(false);
332         AVCODEC_LOGE("audioDecoder Flush failed! Set flush status to false");
333         return AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret));
334     }
335 
336     audioDecObj->isFlushed_.store(true);
337     audioDecObj->isFlushing_.store(false);
338     AVCODEC_LOGD("set flush status to false");
339     std::lock_guard<std::shared_mutex> lock(audioDecObj->memoryObjListMutex_);
340     audioDecObj->memoryObjList_.clear();
341     return AV_ERR_OK;
342 }
343 
OH_AudioDecoder_Reset(struct OH_AVCodec * codec)344 OH_AVErrCode OH_AudioDecoder_Reset(struct OH_AVCodec *codec)
345 {
346     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
347     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
348         AV_ERR_INVALID_VAL, "magic error!");
349 
350     struct AudioDecoderObject *audioDecObj = reinterpret_cast<AudioDecoderObject *>(codec);
351     CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, AV_ERR_INVALID_VAL, "audioDecoder_ is nullptr!");
352     audioDecObj->isStop_.store(true);
353     AVCODEC_LOGD("Set stop status to true");
354 
355     int32_t ret = audioDecObj->audioDecoder_->Reset();
356     if (ret != AVCS_ERR_OK) {
357         audioDecObj->isStop_.store(false);
358         AVCODEC_LOGE("audioDecoder Reset failed! Set stop status to false");
359         return AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret));
360     }
361 
362     std::lock_guard<std::shared_mutex> lock(audioDecObj->memoryObjListMutex_);
363     audioDecObj->memoryObjList_.clear();
364     return AV_ERR_OK;
365 }
366 
OH_AudioDecoder_PushInputData(struct OH_AVCodec * codec,uint32_t index,OH_AVCodecBufferAttr attr)367 OH_AVErrCode OH_AudioDecoder_PushInputData(struct OH_AVCodec *codec, uint32_t index, OH_AVCodecBufferAttr attr)
368 {
369     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
370     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
371         AV_ERR_INVALID_VAL, "magic error!");
372     CHECK_AND_RETURN_RET_LOG(attr.size >= 0, AV_ERR_INVALID_VAL, "Invalid buffer size!");
373 
374     struct AudioDecoderObject *audioDecObj = reinterpret_cast<AudioDecoderObject *>(codec);
375     CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, AV_ERR_INVALID_VAL, "audioDecoder_ is nullptr!");
376 
377     struct AVCodecBufferInfo bufferInfo;
378     bufferInfo.presentationTimeUs = attr.pts;
379     bufferInfo.size = attr.size;
380     bufferInfo.offset = attr.offset;
381     AVCodecBufferFlag bufferFlag = static_cast<AVCodecBufferFlag>(attr.flags);
382 
383     int32_t ret = audioDecObj->audioDecoder_->QueueInputBuffer(index, bufferInfo, bufferFlag);
384     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
385                              "audioDecoder QueueInputBuffer failed!");
386     if (bufferFlag == AVCODEC_BUFFER_FLAG_EOS) {
387         audioDecObj->isEOS_.store(true);
388         AVCODEC_LOGD("Set eos status to true");
389     }
390 
391     return AV_ERR_OK;
392 }
393 
OH_AudioDecoder_GetOutputDescription(struct OH_AVCodec * codec)394 OH_AVFormat *OH_AudioDecoder_GetOutputDescription(struct OH_AVCodec *codec)
395 {
396     CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "input codec is nullptr!");
397     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER, nullptr, "magic error!");
398 
399     struct AudioDecoderObject *audioDecObj = reinterpret_cast<AudioDecoderObject *>(codec);
400     CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, nullptr, "audioDecoder_ is nullptr!");
401 
402     Format format;
403     int32_t ret = audioDecObj->audioDecoder_->GetOutputFormat(format);
404     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "audioDecoder GetOutputFormat failed!");
405 
406     OH_AVFormat *avFormat = OH_AVFormat_Create();
407     avFormat->format_ = format;
408 
409     return avFormat;
410 }
411 
OH_AudioDecoder_FreeOutputData(struct OH_AVCodec * codec,uint32_t index)412 OH_AVErrCode OH_AudioDecoder_FreeOutputData(struct OH_AVCodec *codec, uint32_t index)
413 {
414     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
415     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
416         AV_ERR_INVALID_VAL, "magic error!");
417 
418     struct AudioDecoderObject *audioDecObj = reinterpret_cast<AudioDecoderObject *>(codec);
419     CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, AV_ERR_INVALID_VAL, "audioDecoder_ is nullptr!");
420 
421     int32_t ret = audioDecObj->audioDecoder_->ReleaseOutputBuffer(index);
422     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
423         "audioDecoder ReleaseOutputBuffer failed!");
424 
425     return AV_ERR_OK;
426 }
427 
OH_AudioDecoder_SetParameter(struct OH_AVCodec * codec,struct OH_AVFormat * format)428 OH_AVErrCode OH_AudioDecoder_SetParameter(struct OH_AVCodec *codec, struct OH_AVFormat *format)
429 {
430     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
431     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
432         AV_ERR_INVALID_VAL, "magic error!");
433     CHECK_AND_RETURN_RET_LOG(format != nullptr, AV_ERR_INVALID_VAL, "input format is nullptr!");
434     CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, AV_ERR_INVALID_VAL, "magic error!");
435 
436     struct AudioDecoderObject *audioDecObj = reinterpret_cast<AudioDecoderObject *>(codec);
437     CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, AV_ERR_INVALID_VAL, "audioDecoder_ is nullptr!");
438 
439     int32_t ret = audioDecObj->audioDecoder_->SetParameter(format->format_);
440     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
441                              "audioDecoder SetParameter failed!");
442 
443     return AV_ERR_OK;
444 }
445 
OH_AudioDecoder_SetCallback(struct OH_AVCodec * codec,struct OH_AVCodecAsyncCallback callback,void * userData)446 OH_AVErrCode OH_AudioDecoder_SetCallback(
447     struct OH_AVCodec *codec, struct OH_AVCodecAsyncCallback callback, void *userData)
448 {
449     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
450     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
451         AV_ERR_INVALID_VAL, "magic error!");
452     CHECK_AND_RETURN_RET_LOG(callback.onError != nullptr,
453         AV_ERR_INVALID_VAL, "Callback onError is nullptr");
454     CHECK_AND_RETURN_RET_LOG(callback.onNeedInputData != nullptr,
455         AV_ERR_INVALID_VAL, "Callback onNeedInputData is nullptr");
456     CHECK_AND_RETURN_RET_LOG(callback.onNeedOutputData != nullptr,
457         AV_ERR_INVALID_VAL, "Callback onNeedOutputData is nullptr");
458     CHECK_AND_RETURN_RET_LOG(callback.onStreamChanged != nullptr,
459         AV_ERR_INVALID_VAL, "Callback onStreamChanged is nullptr");
460 
461     struct AudioDecoderObject *audioDecObj = reinterpret_cast<AudioDecoderObject *>(codec);
462     CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, AV_ERR_INVALID_VAL, "audioDecoder_ is nullptr!");
463 
464     audioDecObj->callback_ = std::make_shared<NativeAudioDecoder>(codec, callback, userData);
465 
466     int32_t ret = audioDecObj->audioDecoder_->SetCallback(audioDecObj->callback_);
467     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
468                              "audioDecoder SetCallback failed!");
469 
470     return AV_ERR_OK;
471 }
472 
OH_AudioDecoder_IsValid(OH_AVCodec * codec,bool * isValid)473 OH_AVErrCode OH_AudioDecoder_IsValid(OH_AVCodec *codec, bool *isValid)
474 {
475     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Input codec is nullptr!");
476     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER, AV_ERR_INVALID_VAL, "Magic error!");
477     CHECK_AND_RETURN_RET_LOG(isValid != nullptr, AV_ERR_INVALID_VAL, "Input isValid is nullptr!");
478     *isValid = true;
479     return AV_ERR_OK;
480 }
481 
482 #ifdef __cplusplus
483  };
484 #endif
485 } // namesapce AVCodec
486 } // OHOS