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