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_codec.h"
20 #include "avcodec_errors.h"
21 #include "avcodec_log.h"
22 #include "common/native_mfmagic.h"
23 #include "buffer/avsharedmemory.h"
24 #include "native_avcodec_audiocodec.h"
25 #include "native_avcodec_base.h"
26 #include "native_avmagic.h"
27 #include "avcodec_codec_name.h"
28 #include "avcodec_audio_codec_impl.h"
29 #ifdef SUPPORT_DRM
30 #include "native_drm_object.h"
31 #endif
32
33 namespace {
34 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_AUDIO, "NativeAudioCodec"};
35 constexpr uint32_t MAX_LENGTH = 255;
36 }
37
38 using namespace OHOS::MediaAVCodec;
39 class NativeAudioCodec;
40
41 struct AudioCodecObject : public OH_AVCodec {
AudioCodecObjectAudioCodecObject42 explicit AudioCodecObject(const std::shared_ptr<AVCodecAudioCodecImpl> &decoder)
43 : OH_AVCodec(AVMagic::AVCODEC_MAGIC_AUDIO_DECODER), audioCodec_(decoder)
44 {
45 }
46 ~AudioCodecObject() = default;
47
48 const std::shared_ptr<AVCodecAudioCodecImpl> audioCodec_;
49 std::list<OHOS::sptr<OH_AVBuffer>> bufferObjList_;
50 std::shared_ptr<NativeAudioCodec> callback_ = nullptr;
51 std::atomic<bool> isFlushing_ = false;
52 std::atomic<bool> isFlushed_ = false;
53 std::atomic<bool> isStop_ = false;
54 std::atomic<bool> isEOS_ = false;
55 std::shared_mutex memoryObjListMutex_;
56 };
57
58 class NativeAudioCodec : public MediaCodecCallback {
59 public:
NativeAudioCodec(OH_AVCodec * codec,struct OH_AVCodecCallback cb,void * userData)60 NativeAudioCodec(OH_AVCodec *codec, struct OH_AVCodecCallback cb, void *userData)
61 : codec_(codec), callback_(cb), userData_(userData)
62 {
63 }
64 virtual ~NativeAudioCodec() = default;
65
OnError(AVCodecErrorType errorType,int32_t errorCode)66 void OnError(AVCodecErrorType errorType, int32_t errorCode) override
67 {
68 std::unique_lock<std::shared_mutex> lock(mutex_);
69 (void)errorType;
70 if (codec_ != nullptr && callback_.onError != nullptr) {
71 int32_t extErr = AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(errorCode));
72 callback_.onError(codec_, extErr, userData_);
73 }
74 }
75
OnOutputFormatChanged(const Format & format)76 void OnOutputFormatChanged(const Format &format) override
77 {
78 std::unique_lock<std::shared_mutex> lock(mutex_);
79 if (codec_ != nullptr && callback_.onStreamChanged != nullptr) {
80 OHOS::sptr<OH_AVFormat> object = new (std::nothrow) OH_AVFormat(format);
81 // The object lifecycle is controlled by the current function stack
82 callback_.onStreamChanged(codec_, reinterpret_cast<OH_AVFormat *>(object.GetRefPtr()), userData_);
83 } else {
84 AVCODEC_LOGE("receive format changed but callback is nullptr");
85 }
86 }
87
OnInputBufferAvailable(uint32_t index,std::shared_ptr<AVBuffer> buffer)88 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override
89 {
90 std::shared_lock<std::shared_mutex> lock(mutex_);
91 if (codec_ != nullptr && callback_.onNeedInputBuffer != nullptr) {
92 struct AudioCodecObject *audioCodecObj = reinterpret_cast<AudioCodecObject *>(codec_);
93 CHECK_AND_RETURN_LOG(audioCodecObj->audioCodec_ != nullptr, "audioCodec_ is nullptr!");
94 if (audioCodecObj->isFlushing_.load() || audioCodecObj->isFlushed_.load() ||
95 audioCodecObj->isStop_.load() || audioCodecObj->isEOS_.load()) {
96 AVCODEC_LOGD("At flush, eos or stop, no buffer available");
97 return;
98 }
99 OH_AVBuffer *data = GetTransData(codec_, index, buffer);
100 callback_.onNeedInputBuffer(codec_, index, data, userData_);
101 }
102 }
103
OnOutputBufferAvailable(uint32_t index,std::shared_ptr<AVBuffer> buffer)104 void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override
105 {
106 std::shared_lock<std::shared_mutex> lock(mutex_);
107 if (codec_ != nullptr && callback_.onNewOutputBuffer != nullptr) {
108 struct AudioCodecObject *audioCodecObj = reinterpret_cast<AudioCodecObject *>(codec_);
109 CHECK_AND_RETURN_LOG(audioCodecObj->audioCodec_ != nullptr, "audioCodec_ is nullptr!");
110 if (audioCodecObj->isFlushing_.load() || audioCodecObj->isFlushed_.load() ||
111 audioCodecObj->isStop_.load()) {
112 AVCODEC_LOGD("At flush or stop, ignore");
113 return;
114 }
115 OH_AVBuffer *data = GetTransData(codec_, index, buffer);
116 callback_.onNewOutputBuffer(codec_, index, data, userData_);
117 }
118 }
119
StopCallback()120 void StopCallback()
121 {
122 std::unique_lock<std::shared_mutex> lock(mutex_);
123 codec_ = nullptr;
124 }
125
126 private:
GetTransData(struct OH_AVCodec * codec,uint32_t index,std::shared_ptr<AVBuffer> buffer)127 OH_AVBuffer *GetTransData(struct OH_AVCodec *codec, uint32_t index, std::shared_ptr<AVBuffer> buffer)
128 {
129 CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "input codec is nullptr!");
130 CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER ||
131 codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_ENCODER, nullptr, "magic error!");
132
133 struct AudioCodecObject *audioCodecObj = reinterpret_cast<AudioCodecObject *>(codec);
134 CHECK_AND_RETURN_RET_LOG(audioCodecObj->audioCodec_ != nullptr, nullptr, "audioc odec is nullptr!");
135 CHECK_AND_RETURN_RET_LOG(buffer != nullptr, nullptr, "get output buffer is nullptr!");
136
137 {
138 std::shared_lock<std::shared_mutex> lock(audioCodecObj->memoryObjListMutex_);
139 for (auto &bufferObj : audioCodecObj->bufferObjList_) {
140 if (bufferObj->IsEqualBuffer(buffer)) {
141 return reinterpret_cast<OH_AVBuffer *>(bufferObj.GetRefPtr());
142 }
143 }
144 }
145
146 OHOS::sptr<OH_AVBuffer> object = new (std::nothrow) OH_AVBuffer(buffer);
147 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new OH_AVBuffer");
148
149 std::lock_guard<std::shared_mutex> lock(audioCodecObj->memoryObjListMutex_);
150 audioCodecObj->bufferObjList_.push_back(object);
151 return reinterpret_cast<OH_AVBuffer *>(object.GetRefPtr());
152 }
153 struct OH_AVCodec *codec_;
154 struct OH_AVCodecCallback callback_;
155 void *userData_;
156 std::shared_mutex mutex_;
157 };
158
159 namespace OHOS {
160 namespace MediaAVCodec {
161 #ifdef __cplusplus
162 extern "C" {
163 #endif
164
OH_AudioCodec_CreateByMime(const char * mime,bool isEncoder)165 struct OH_AVCodec *OH_AudioCodec_CreateByMime(const char *mime, bool isEncoder)
166 {
167 CHECK_AND_RETURN_RET_LOG(mime != nullptr, nullptr, "input mime is nullptr!");
168 CHECK_AND_RETURN_RET_LOG(strlen(mime) < MAX_LENGTH, nullptr, "input mime is too long!");
169 std::shared_ptr<AVCodecAudioCodecImpl> audioCodec = std::make_shared<AVCodecAudioCodecImpl>();
170 CHECK_AND_RETURN_RET_LOG(audioCodec != nullptr, nullptr, "failed to AudioCodecFactory::CreateByMime");
171 AVCodecType type = AVCODEC_TYPE_AUDIO_DECODER;
172 if (isEncoder) {
173 type = AVCODEC_TYPE_AUDIO_ENCODER;
174 }
175 int32_t ret = audioCodec->Init(type, true, mime);
176 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "failed to init AVCodecAudioCodecImpl");
177 struct AudioCodecObject *object = new (std::nothrow) AudioCodecObject(audioCodec);
178 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new AudioCodecObject");
179
180 return object;
181 }
182
OH_AudioCodec_CreateByName(const char * name)183 struct OH_AVCodec *OH_AudioCodec_CreateByName(const char *name)
184 {
185 CHECK_AND_RETURN_RET_LOG(name != nullptr, nullptr, "input name is nullptr!");
186 CHECK_AND_RETURN_RET_LOG(strlen(name) < MAX_LENGTH, nullptr, "input name is too long!");
187 std::shared_ptr<AVCodecAudioCodecImpl> audioCodec = std::make_shared<AVCodecAudioCodecImpl>();
188 CHECK_AND_RETURN_RET_LOG(audioCodec != nullptr, nullptr, "failed to AudioCodecFactory::CreateByMime");
189 std::string codecMimeName = name;
190 std::string targetName = name;
191 if (targetName.compare(AVCodecCodecName::AUDIO_DECODER_API9_AAC_NAME) == 0) {
192 codecMimeName = AVCodecCodecName::AUDIO_DECODER_AAC_NAME;
193 } else if (targetName.compare(AVCodecCodecName::AUDIO_ENCODER_API9_AAC_NAME) == 0) {
194 codecMimeName = AVCodecCodecName::AUDIO_ENCODER_AAC_NAME;
195 }
196 AVCodecType type = AVCODEC_TYPE_AUDIO_ENCODER;
197 if (codecMimeName.find("Encoder") != codecMimeName.npos) {
198 type = AVCODEC_TYPE_AUDIO_ENCODER;
199 } else {
200 type = AVCODEC_TYPE_AUDIO_DECODER;
201 }
202 int32_t ret = audioCodec->Init(type, false, name);
203 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "failed to init AVCodecAudioCodecImpl");
204 struct AudioCodecObject *object = new(std::nothrow) AudioCodecObject(audioCodec);
205 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new AudioCodecObject");
206
207 return object;
208 }
209
OH_AudioCodec_Destroy(struct OH_AVCodec * codec)210 OH_AVErrCode OH_AudioCodec_Destroy(struct OH_AVCodec *codec)
211 {
212 CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
213 CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
214 AV_ERR_INVALID_VAL, "magic error!");
215
216 struct AudioCodecObject *audioCodecObj = reinterpret_cast<AudioCodecObject *>(codec);
217 AVCODEC_LOGI("OH_AudioCodec_Destroy enter");
218 if (audioCodecObj != nullptr && audioCodecObj->audioCodec_ != nullptr) {
219 if (audioCodecObj->callback_ != nullptr) {
220 audioCodecObj->callback_->StopCallback();
221 }
222 {
223 std::lock_guard<std::shared_mutex> lock(audioCodecObj->memoryObjListMutex_);
224 audioCodecObj->bufferObjList_.clear();
225 }
226 audioCodecObj->isStop_.store(true);
227 int32_t ret = audioCodecObj->audioCodec_->Release();
228 if (ret != AVCS_ERR_OK) {
229 AVCODEC_LOGE("audioCodec Release failed!");
230 delete codec;
231 return AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret));
232 }
233 } else {
234 AVCODEC_LOGD("audioCodec_ is nullptr!");
235 }
236
237 delete codec;
238 return AV_ERR_OK;
239 }
240
OH_AudioCodec_Configure(struct OH_AVCodec * codec,const OH_AVFormat * format)241 OH_AVErrCode OH_AudioCodec_Configure(struct OH_AVCodec *codec, const OH_AVFormat *format)
242 {
243 CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
244 CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
245 AV_ERR_INVALID_VAL, "magic error!");
246 CHECK_AND_RETURN_RET_LOG(format != nullptr, AV_ERR_INVALID_VAL, "input format is nullptr!");
247 CHECK_AND_RETURN_RET_LOG(format->magic_ == MFMagic::MFMAGIC_FORMAT, AV_ERR_INVALID_VAL, "magic error!");
248
249 struct AudioCodecObject *audioCodecObj = reinterpret_cast<AudioCodecObject *>(codec);
250 CHECK_AND_RETURN_RET_LOG(audioCodecObj->audioCodec_ != nullptr, AV_ERR_INVALID_VAL, "audioCodec is nullptr!");
251
252 int32_t ret = audioCodecObj->audioCodec_->Configure(format->format_);
253 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
254 "audioCodec Configure failed!");
255
256 return AV_ERR_OK;
257 }
258
OH_AudioCodec_Prepare(struct OH_AVCodec * codec)259 OH_AVErrCode OH_AudioCodec_Prepare(struct OH_AVCodec *codec)
260 {
261 CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
262 CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
263 AV_ERR_INVALID_VAL, "magic error!");
264
265 struct AudioCodecObject *audioCodecObj = reinterpret_cast<AudioCodecObject *>(codec);
266 CHECK_AND_RETURN_RET_LOG(audioCodecObj->audioCodec_ != nullptr, AV_ERR_INVALID_VAL, "audioCodec_ is nullptr!");
267
268 int32_t ret = audioCodecObj->audioCodec_->Prepare();
269 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
270 "audioCodec Prepare failed!");
271 return AV_ERR_OK;
272 }
273
OH_AudioCodec_Start(struct OH_AVCodec * codec)274 OH_AVErrCode OH_AudioCodec_Start(struct OH_AVCodec *codec)
275 {
276 CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
277 CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
278 AV_ERR_INVALID_VAL, "magic error!");
279
280 struct AudioCodecObject *audioCodecObj = reinterpret_cast<AudioCodecObject *>(codec);
281 CHECK_AND_RETURN_RET_LOG(audioCodecObj->audioCodec_ != nullptr, AV_ERR_INVALID_VAL, "audioCodec_ is nullptr!");
282 audioCodecObj->isStop_.store(false);
283 audioCodecObj->isEOS_.store(false);
284 audioCodecObj->isFlushed_.store(false);
285 int32_t ret = audioCodecObj->audioCodec_->Start();
286 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
287 "audioCodec Start failed!");
288
289 return AV_ERR_OK;
290 }
291
OH_AudioCodec_Stop(struct OH_AVCodec * codec)292 OH_AVErrCode OH_AudioCodec_Stop(struct OH_AVCodec *codec)
293 {
294 CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
295 CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
296 AV_ERR_INVALID_VAL, "magic error!");
297
298 struct AudioCodecObject *audioCodecObj = reinterpret_cast<AudioCodecObject *>(codec);
299 CHECK_AND_RETURN_RET_LOG(audioCodecObj->audioCodec_ != nullptr, AV_ERR_INVALID_VAL, "audioCodec_ is nullptr!");
300
301 audioCodecObj->isStop_.store(true);
302 AVCODEC_LOGD("set stop status to true");
303
304 int32_t ret = audioCodecObj->audioCodec_->Stop();
305 if (ret != AVCS_ERR_OK) {
306 audioCodecObj->isStop_.store(false);
307 AVCODEC_LOGE("audioCodec Stop failed!, set stop status to false");
308 return AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret));
309 }
310 std::lock_guard<std::shared_mutex> lock(audioCodecObj->memoryObjListMutex_);
311 audioCodecObj->bufferObjList_.clear();
312
313 return AV_ERR_OK;
314 }
315
OH_AudioCodec_Flush(struct OH_AVCodec * codec)316 OH_AVErrCode OH_AudioCodec_Flush(struct OH_AVCodec *codec)
317 {
318 CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
319 CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
320 AV_ERR_INVALID_VAL, "magic error!");
321
322 struct AudioCodecObject *audioCodecObj = reinterpret_cast<AudioCodecObject *>(codec);
323 CHECK_AND_RETURN_RET_LOG(audioCodecObj->audioCodec_ != nullptr, AV_ERR_INVALID_VAL, "audioCodec_ is nullptr!");
324 audioCodecObj->isFlushing_.store(true);
325 AVCODEC_LOGD("Set flush status to true");
326 int32_t ret = audioCodecObj->audioCodec_->Flush();
327 if (ret != AVCS_ERR_OK) {
328 audioCodecObj->isFlushing_.store(false);
329 audioCodecObj->isFlushed_.store(false);
330 AVCODEC_LOGE("audioCodec Flush failed! Set flush status to false");
331 return AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret));
332 }
333
334 audioCodecObj->isFlushed_.store(true);
335 audioCodecObj->isFlushing_.store(false);
336 AVCODEC_LOGD("set flush status to false");
337 std::lock_guard<std::shared_mutex> lock(audioCodecObj->memoryObjListMutex_);
338 audioCodecObj->bufferObjList_.clear();
339 return AV_ERR_OK;
340 }
341
OH_AudioCodec_Reset(struct OH_AVCodec * codec)342 OH_AVErrCode OH_AudioCodec_Reset(struct OH_AVCodec *codec)
343 {
344 CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
345 CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
346 AV_ERR_INVALID_VAL, "magic error!");
347
348 struct AudioCodecObject *audioCodecObj = reinterpret_cast<AudioCodecObject *>(codec);
349 CHECK_AND_RETURN_RET_LOG(audioCodecObj->audioCodec_ != nullptr, AV_ERR_INVALID_VAL, "audioCodec_ is nullptr!");
350 audioCodecObj->isStop_.store(true);
351 AVCODEC_LOGD("Set stop status to true");
352
353 int32_t ret = audioCodecObj->audioCodec_->Reset();
354 if (ret != AVCS_ERR_OK) {
355 audioCodecObj->isStop_.store(false);
356 AVCODEC_LOGE("audioCodec Reset failed! Set stop status to false");
357 return AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret));
358 }
359
360 std::lock_guard<std::shared_mutex> lock(audioCodecObj->memoryObjListMutex_);
361 audioCodecObj->bufferObjList_.clear();
362 return AV_ERR_OK;
363 }
364
OH_AudioCodec_PushInputBuffer(struct OH_AVCodec * codec,uint32_t index)365 OH_AVErrCode OH_AudioCodec_PushInputBuffer(struct OH_AVCodec *codec, uint32_t index)
366 {
367 CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
368 CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
369 AV_ERR_INVALID_VAL, "magic error!");
370
371 struct AudioCodecObject *audioCodecObj = reinterpret_cast<AudioCodecObject *>(codec);
372 CHECK_AND_RETURN_RET_LOG(audioCodecObj->audioCodec_ != nullptr, AV_ERR_INVALID_VAL, "audioCodec_ is nullptr!");
373
374 int32_t ret = audioCodecObj->audioCodec_->QueueInputBuffer(index);
375 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
376 "audioCodec QueueInputBuffer failed!");
377 return AV_ERR_OK;
378 }
379
OH_AudioCodec_GetOutputDescription(struct OH_AVCodec * codec)380 OH_AVFormat *OH_AudioCodec_GetOutputDescription(struct OH_AVCodec *codec)
381 {
382 CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "input codec is nullptr!");
383 CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER, nullptr, "magic error!");
384
385 struct AudioCodecObject *audioCodecObj = reinterpret_cast<AudioCodecObject *>(codec);
386 CHECK_AND_RETURN_RET_LOG(audioCodecObj->audioCodec_ != nullptr, nullptr, "audioCodec_ is nullptr!");
387
388 Format format;
389 int32_t ret = audioCodecObj->audioCodec_->GetOutputFormat(format);
390 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "audioCodec GetOutputFormat failed!");
391
392 OH_AVFormat *avFormat = OH_AVFormat_Create();
393 CHECK_AND_RETURN_RET_LOG(avFormat != nullptr, nullptr, "audioCodec OH_AVFormat_Create failed!");
394 avFormat->format_ = format;
395
396 return avFormat;
397 }
398
OH_AudioCodec_FreeOutputBuffer(struct OH_AVCodec * codec,uint32_t index)399 OH_AVErrCode OH_AudioCodec_FreeOutputBuffer(struct OH_AVCodec *codec, uint32_t index)
400 {
401 CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
402 CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
403 AV_ERR_INVALID_VAL, "magic error!");
404
405 struct AudioCodecObject *audioCodecObj = reinterpret_cast<AudioCodecObject *>(codec);
406 CHECK_AND_RETURN_RET_LOG(audioCodecObj->audioCodec_ != nullptr, AV_ERR_INVALID_VAL, "audioCodec_ is nullptr!");
407
408 int32_t ret = audioCodecObj->audioCodec_->ReleaseOutputBuffer(index);
409 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
410 "audioCodec ReleaseOutputBuffer failed!");
411
412 return AV_ERR_OK;
413 }
414
OH_AudioCodec_SetParameter(struct OH_AVCodec * codec,const OH_AVFormat * format)415 OH_AVErrCode OH_AudioCodec_SetParameter(struct OH_AVCodec *codec, const OH_AVFormat *format)
416 {
417 CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
418 CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
419 AV_ERR_INVALID_VAL, "magic error!");
420 CHECK_AND_RETURN_RET_LOG(format != nullptr, AV_ERR_INVALID_VAL, "input format is nullptr!");
421 CHECK_AND_RETURN_RET_LOG(format->magic_ == MFMagic::MFMAGIC_FORMAT, AV_ERR_INVALID_VAL, "magic error!");
422
423 struct AudioCodecObject *audioCodecObj = reinterpret_cast<AudioCodecObject *>(codec);
424 CHECK_AND_RETURN_RET_LOG(audioCodecObj->audioCodec_ != nullptr, AV_ERR_INVALID_VAL, "audioCodec_ is nullptr!");
425
426 int32_t ret = audioCodecObj->audioCodec_->SetParameter(format->format_);
427 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
428 "audioCodec SetParameter failed!");
429
430 return AV_ERR_OK;
431 }
432
OH_AudioCodec_RegisterCallback(OH_AVCodec * codec,OH_AVCodecCallback callback,void * userData)433 OH_AVErrCode OH_AudioCodec_RegisterCallback(OH_AVCodec *codec, OH_AVCodecCallback callback, void *userData)
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(callback.onError != nullptr,
439 AV_ERR_INVALID_VAL, "Callback onError is nullptr");
440 CHECK_AND_RETURN_RET_LOG(callback.onNeedInputBuffer != nullptr,
441 AV_ERR_INVALID_VAL, "Callback onNeedInputBuffer is nullptr");
442 CHECK_AND_RETURN_RET_LOG(callback.onNewOutputBuffer != nullptr,
443 AV_ERR_INVALID_VAL, "Callback onNewOutputBuffer is nullptr");
444 CHECK_AND_RETURN_RET_LOG(callback.onStreamChanged != nullptr,
445 AV_ERR_INVALID_VAL, "Callback onStreamChanged is nullptr");
446
447 struct AudioCodecObject *audioCodecObj = reinterpret_cast<AudioCodecObject *>(codec);
448 CHECK_AND_RETURN_RET_LOG(audioCodecObj->audioCodec_ != nullptr, AV_ERR_INVALID_VAL, "audioCodec_ is nullptr!");
449
450 audioCodecObj->callback_ = std::make_shared<NativeAudioCodec>(codec, callback, userData);
451
452 int32_t ret = audioCodecObj->audioCodec_->SetCallback(audioCodecObj->callback_);
453 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
454 "audioCodec SetCallback failed!");
455
456 return AV_ERR_OK;
457 }
458
OH_AudioCodec_IsValid(OH_AVCodec * codec,bool * isValid)459 OH_AVErrCode OH_AudioCodec_IsValid(OH_AVCodec *codec, bool *isValid)
460 {
461 CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Input codec is nullptr!");
462 CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER, AV_ERR_INVALID_VAL, "Magic error!");
463 CHECK_AND_RETURN_RET_LOG(isValid != nullptr, AV_ERR_INVALID_VAL, "Input isValid is nullptr!");
464 *isValid = true;
465 return AV_ERR_OK;
466 }
467
468 #ifdef SUPPORT_DRM
OH_AudioCodec_SetDecryptionConfig(OH_AVCodec * codec,MediaKeySession * mediaKeySession,bool secureAudio)469 OH_AVErrCode OH_AudioCodec_SetDecryptionConfig(OH_AVCodec *codec, MediaKeySession *mediaKeySession,
470 bool secureAudio)
471 {
472 AVCODEC_LOGI("OH_AudioCodec_SetDecryptionConfig");
473 CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
474 CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER,
475 AV_ERR_INVALID_VAL, "magic error!");
476
477 struct AudioCodecObject *audioCodecObj = reinterpret_cast<AudioCodecObject *>(codec);
478 CHECK_AND_RETURN_RET_LOG(audioCodecObj->audioCodec_ != nullptr, AV_ERR_INVALID_VAL, "audioCodec_ is nullptr!");
479
480 DrmStandard::MediaKeySessionObject *sessionObject =
481 reinterpret_cast<DrmStandard::MediaKeySessionObject *>(mediaKeySession);
482 CHECK_AND_RETURN_RET_LOG(sessionObject != nullptr, AV_ERR_INVALID_VAL, "sessionObject is nullptr!");
483 AVCODEC_LOGD("DRM sessionObject impl :0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(sessionObject));
484
485 CHECK_AND_RETURN_RET_LOG(sessionObject->sessionImpl_ != nullptr, AV_ERR_INVALID_VAL,
486 "sessionObject->impl is nullptr!");
487 AVCODEC_LOGD("DRM impl :0x%{public}06" PRIXPTR " Instances create",
488 FAKE_POINTER(sessionObject->sessionImpl_.GetRefPtr()));
489 CHECK_AND_RETURN_RET_LOG(sessionObject->sessionImpl_ ->GetMediaKeySessionServiceProxy() != nullptr,
490 AV_ERR_INVALID_VAL, "MediaKeySessionServiceProxy is nullptr!");
491
492 int32_t ret = audioCodecObj->audioCodec_->SetAudioDecryptionConfig(
493 sessionObject->sessionImpl_->GetMediaKeySessionServiceProxy(), secureAudio);
494 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
495 "audioCodec SetAudioDecryptionConfig failed!");
496
497 return AV_ERR_OK;
498 }
499 #else
OH_AudioCodec_SetDecryptionConfig(OH_AVCodec * codec,MediaKeySession * mediaKeySession,bool secureAudio)500 OH_AVErrCode OH_AudioCodec_SetDecryptionConfig(OH_AVCodec *codec, MediaKeySession *mediaKeySession,
501 bool secureAudio)
502 {
503 AVCODEC_LOGI("OH_AudioCodec_SetDecryptionConfig");
504 (void)codec;
505 (void)mediaKeySession;
506 (void)secureAudio;
507 return AV_ERR_OK;
508 }
509 #endif
510
511 #ifdef __cplusplus
512 };
513 #endif
514 } // namespace MediaAVCodec
515 } // namespace OHOS