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