• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_audioencoder.h"
20 #include "native_avmagic.h"
21 #include "avcodec_audio_encoder.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, "NativeAudioEncoder"};
28 }
29 
30 using namespace OHOS::Media;
31 class NativeAudioEncoderCallback;
32 
33 struct AudioEncoderObject : public OH_AVCodec {
AudioEncoderObjectAudioEncoderObject34     explicit AudioEncoderObject(const std::shared_ptr<AVCodecAudioEncoder> &encoder)
35         : OH_AVCodec(AVMagic::MEDIA_MAGIC_AUDIO_ENCODER), audioEncoder_(encoder) {}
36     ~AudioEncoderObject() = default;
37 
38     const std::shared_ptr<AVCodecAudioEncoder> audioEncoder_;
39     std::list<OHOS::sptr<OH_AVMemory>> memoryObjList_;
40     std::shared_ptr<NativeAudioEncoderCallback> callback_ = nullptr;
41     std::atomic<bool> isFlushing_ = false;
42     std::atomic<bool> isStop_ = false;
43     std::atomic<bool> isEOS_ = false;
44 };
45 
46 class NativeAudioEncoderCallback : public AVCodecCallback {
47 public:
NativeAudioEncoderCallback(OH_AVCodec * codec,struct OH_AVCodecAsyncCallback cb,void * userData)48     NativeAudioEncoderCallback(OH_AVCodec *codec, struct OH_AVCodecAsyncCallback cb, void *userData)
49         : codec_(codec), callback_(cb), userData_(userData) {}
50     virtual ~NativeAudioEncoderCallback() = 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 AudioEncoderObject *audioEncObj = reinterpret_cast<AudioEncoderObject *>(codec_);
77             CHECK_AND_RETURN_LOG(audioEncObj->audioEncoder_ != nullptr, "audioEncoder_ is nullptr!");
78             if (audioEncObj->isFlushing_.load() || audioEncObj->isStop_.load() || audioEncObj->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 AudioEncoderObject *audioEncObj = reinterpret_cast<AudioEncoderObject *>(codec_);
95             CHECK_AND_RETURN_LOG(audioEncObj->audioEncoder_ != nullptr, "audioEncoder_ is nullptr!");
96             if (audioEncObj->isFlushing_.load() || audioEncObj->isStop_.load()) {
97                 MEDIA_LOGD("At flush or stop, ignore");
98                 return;
99             }
100             struct OH_AVCodecBufferAttr bufferAttr;
101             bufferAttr.pts = info.presentationTimeUs;
102             bufferAttr.size = info.size;
103             bufferAttr.offset = info.offset;
104             bufferAttr.flags = flag;
105             // The bufferInfo lifecycle is controlled by the current function stack
106             OH_AVMemory *data = GetOutputData(codec_, index);
107             callback_.onNeedOutputData(codec_, index, data, &bufferAttr, userData_);
108         }
109     }
110 
StopCallback()111     void StopCallback()
112     {
113         std::unique_lock<std::mutex> lock(mutex_);
114         codec_ = nullptr;
115     }
116 
117 private:
GetInputData(struct OH_AVCodec * codec,uint32_t index)118     OH_AVMemory *GetInputData(struct OH_AVCodec *codec, uint32_t index)
119     {
120         CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "input codec is nullptr!");
121         CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_AUDIO_ENCODER, nullptr, "magic error!");
122 
123         struct AudioEncoderObject *audioEncObj = reinterpret_cast<AudioEncoderObject *>(codec);
124         CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, nullptr, "audioEncoder_ is nullptr!");
125 
126         std::shared_ptr<AVSharedMemory> memory = audioEncObj->audioEncoder_->GetInputBuffer(index);
127         CHECK_AND_RETURN_RET_LOG(memory != nullptr, nullptr, "get input buffer is nullptr!");
128 
129         for (auto &memoryObj : audioEncObj->memoryObjList_) {
130             if (memoryObj->IsEqualMemory(memory)) {
131                 return reinterpret_cast<OH_AVMemory *>(memoryObj.GetRefPtr());
132             }
133         }
134 
135         OHOS::sptr<OH_AVMemory> object = new(std::nothrow) OH_AVMemory(memory);
136         CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new OH_AVMemory");
137 
138         audioEncObj->memoryObjList_.push_back(object);
139         return reinterpret_cast<OH_AVMemory *>(object.GetRefPtr());
140     }
141 
GetOutputData(struct OH_AVCodec * codec,uint32_t index)142     OH_AVMemory *GetOutputData(struct OH_AVCodec *codec, uint32_t index)
143     {
144         CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "input codec is nullptr!");
145         CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_AUDIO_ENCODER, nullptr, "magic error!");
146 
147         struct AudioEncoderObject *audioEncObj = reinterpret_cast<AudioEncoderObject *>(codec);
148         CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, nullptr, "audioEncoder_ is nullptr!");
149 
150         std::shared_ptr<AVSharedMemory> memory = audioEncObj->audioEncoder_->GetOutputBuffer(index);
151         CHECK_AND_RETURN_RET_LOG(memory != nullptr, nullptr, "get output buffer is nullptr!");
152 
153         for (auto &memoryObj : audioEncObj->memoryObjList_) {
154             if (memoryObj->IsEqualMemory(memory)) {
155                 return reinterpret_cast<OH_AVMemory *>(memoryObj.GetRefPtr());
156             }
157         }
158 
159         OHOS::sptr<OH_AVMemory> object = new(std::nothrow) OH_AVMemory(memory);
160         CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new OH_AVMemory");
161 
162         audioEncObj->memoryObjList_.push_back(object);
163         return reinterpret_cast<OH_AVMemory *>(object.GetRefPtr());
164     }
165 
166     struct OH_AVCodec *codec_;
167     struct OH_AVCodecAsyncCallback callback_;
168     void *userData_;
169     std::mutex mutex_;
170 };
171 
OH_AudioEncoder_CreateByMime(const char * mime)172 struct OH_AVCodec *OH_AudioEncoder_CreateByMime(const char *mime)
173 {
174     CHECK_AND_RETURN_RET_LOG(mime != nullptr, nullptr, "input mime is nullptr!");
175 
176     std::shared_ptr<AVCodecAudioEncoder> audioEncoder = AudioEncoderFactory::CreateByMime(mime);
177     CHECK_AND_RETURN_RET_LOG(audioEncoder != nullptr, nullptr, "failed to AudioEncoderFactory::CreateByMime");
178 
179     struct AudioEncoderObject *object = new(std::nothrow) AudioEncoderObject(audioEncoder);
180     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new AudioEncoderObject");
181 
182     return object;
183 }
184 
OH_AudioEncoder_CreateByName(const char * name)185 struct OH_AVCodec *OH_AudioEncoder_CreateByName(const char *name)
186 {
187     CHECK_AND_RETURN_RET_LOG(name != nullptr, nullptr, "input name is nullptr!");
188 
189     std::shared_ptr<AVCodecAudioEncoder> audioEncoder = AudioEncoderFactory::CreateByName(name);
190     CHECK_AND_RETURN_RET_LOG(audioEncoder != nullptr, nullptr, "failed to AudioEncoderFactory::CreateByMime");
191 
192     struct AudioEncoderObject *object = new(std::nothrow) AudioEncoderObject(audioEncoder);
193     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new AudioEncoderObject");
194 
195     return object;
196 }
197 
OH_AudioEncoder_Destroy(struct OH_AVCodec * codec)198 OH_AVErrCode OH_AudioEncoder_Destroy(struct OH_AVCodec *codec)
199 {
200     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
201     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
202 
203     struct AudioEncoderObject *audioEncObj = reinterpret_cast<AudioEncoderObject *>(codec);
204 
205     if (audioEncObj != nullptr && audioEncObj->audioEncoder_ != nullptr) {
206         audioEncObj->callback_->StopCallback();
207         audioEncObj->memoryObjList_.clear();
208         int32_t ret = audioEncObj->audioEncoder_->Release();
209         if (ret != MSERR_OK) {
210             MEDIA_LOGE("audioEncoder Release failed!");
211             delete codec;
212             return AV_ERR_OPERATE_NOT_PERMIT;
213         }
214     } else {
215         MEDIA_LOGD("audioEncoder_ is nullptr!");
216     }
217 
218     delete codec;
219     return AV_ERR_OK;
220 }
221 
OH_AudioEncoder_Configure(struct OH_AVCodec * codec,struct OH_AVFormat * format)222 OH_AVErrCode OH_AudioEncoder_Configure(struct OH_AVCodec *codec, struct OH_AVFormat *format)
223 {
224     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
225     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
226     CHECK_AND_RETURN_RET_LOG(format != nullptr, AV_ERR_INVALID_VAL, "input format is nullptr!");
227     CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::MEDIA_MAGIC_FORMAT, AV_ERR_INVALID_VAL, "magic error!");
228 
229     struct AudioEncoderObject *audioEncObj = reinterpret_cast<AudioEncoderObject *>(codec);
230     CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, AV_ERR_INVALID_VAL, "audioEncoder is nullptr!");
231 
232     int32_t ret = audioEncObj->audioEncoder_->Configure(format->format_);
233     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioEncoder Configure failed!");
234 
235     return AV_ERR_OK;
236 }
237 
OH_AudioEncoder_Prepare(struct OH_AVCodec * codec)238 OH_AVErrCode OH_AudioEncoder_Prepare(struct OH_AVCodec *codec)
239 {
240     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
241     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
242 
243     struct AudioEncoderObject *audioEncObj = reinterpret_cast<AudioEncoderObject *>(codec);
244     CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, AV_ERR_INVALID_VAL, "audioEncoder_ is nullptr!");
245 
246     int32_t ret = audioEncObj->audioEncoder_->Prepare();
247     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioEncoder Prepare failed!");
248 
249     return AV_ERR_OK;
250 }
251 
OH_AudioEncoder_Start(struct OH_AVCodec * codec)252 OH_AVErrCode OH_AudioEncoder_Start(struct OH_AVCodec *codec)
253 {
254     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
255     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
256 
257     struct AudioEncoderObject *audioEncObj = reinterpret_cast<AudioEncoderObject *>(codec);
258     CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, AV_ERR_INVALID_VAL, "audioEncoder_ is nullptr!");
259     audioEncObj->isStop_.store(false);
260     audioEncObj->isEOS_.store(false);
261     MEDIA_LOGD("Set stop and eos status to false");
262     int32_t ret = audioEncObj->audioEncoder_->Start();
263     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioEncoder Start failed!");
264 
265     return AV_ERR_OK;
266 }
267 
OH_AudioEncoder_Stop(struct OH_AVCodec * codec)268 OH_AVErrCode OH_AudioEncoder_Stop(struct OH_AVCodec *codec)
269 {
270     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
271     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
272 
273     struct AudioEncoderObject *audioEncObj = reinterpret_cast<AudioEncoderObject *>(codec);
274     CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, AV_ERR_INVALID_VAL, "audioEncoder_ is nullptr!");
275     audioEncObj->isStop_.store(true);
276     MEDIA_LOGD("Set stop status to true");
277 
278     int32_t ret = audioEncObj->audioEncoder_->Stop();
279     if (ret != MSERR_OK) {
280         audioEncObj->isStop_.store(false);
281         MEDIA_LOGE("audioEncoder Stop failed! Set stop status to false");
282         return AV_ERR_OPERATE_NOT_PERMIT;
283     }
284     audioEncObj->memoryObjList_.clear();
285 
286     return AV_ERR_OK;
287 }
288 
OH_AudioEncoder_Flush(struct OH_AVCodec * codec)289 OH_AVErrCode OH_AudioEncoder_Flush(struct OH_AVCodec *codec)
290 {
291     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
292     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
293 
294     struct AudioEncoderObject *audioEncObj = reinterpret_cast<AudioEncoderObject *>(codec);
295     CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, AV_ERR_INVALID_VAL, "audioEncoder_ is nullptr!");
296 
297     audioEncObj->isFlushing_.store(true);
298     MEDIA_LOGD("Set flush status to true");
299 
300     int32_t ret = audioEncObj->audioEncoder_->Flush();
301     if (ret != MSERR_OK) {
302         audioEncObj->isFlushing_.store(false);
303         MEDIA_LOGE("audioEncObj Flush failed! Set flush status to false");
304         return AV_ERR_OPERATE_NOT_PERMIT;
305     }
306     audioEncObj->memoryObjList_.clear();
307     audioEncObj->isFlushing_.store(false);
308     MEDIA_LOGD("Set flush status to false");
309     return AV_ERR_OK;
310 }
311 
OH_AudioEncoder_Reset(struct OH_AVCodec * codec)312 OH_AVErrCode OH_AudioEncoder_Reset(struct OH_AVCodec *codec)
313 {
314     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
315     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
316 
317     struct AudioEncoderObject *audioEncObj = reinterpret_cast<AudioEncoderObject *>(codec);
318     CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, AV_ERR_INVALID_VAL, "audioEncoder_ is nullptr!");
319     audioEncObj->isStop_.store(true);
320     int32_t ret = audioEncObj->audioEncoder_->Reset();
321     if (ret != MSERR_OK) {
322         audioEncObj->isStop_.store(false);
323         MEDIA_LOGE("audioEncoder Reset failed! Set stop status to false");
324         return AV_ERR_OPERATE_NOT_PERMIT;
325     }
326     audioEncObj->memoryObjList_.clear();
327     return AV_ERR_OK;
328 }
329 
OH_AudioEncoder_PushInputData(struct OH_AVCodec * codec,uint32_t index,OH_AVCodecBufferAttr attr)330 OH_AVErrCode OH_AudioEncoder_PushInputData(struct OH_AVCodec *codec, uint32_t index, OH_AVCodecBufferAttr attr)
331 {
332     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
333     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
334 
335     struct AudioEncoderObject *audioEncObj = reinterpret_cast<AudioEncoderObject *>(codec);
336     CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, AV_ERR_INVALID_VAL, "audioEncoder_ is nullptr!");
337 
338     struct AVCodecBufferInfo bufferInfo;
339     bufferInfo.presentationTimeUs = attr.pts;
340     bufferInfo.size = attr.size;
341     bufferInfo.offset = attr.offset;
342     enum AVCodecBufferFlag bufferFlag = static_cast<enum AVCodecBufferFlag>(attr.flags);
343 
344     int32_t ret = audioEncObj->audioEncoder_->QueueInputBuffer(index, bufferInfo, bufferFlag);
345     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioEncoder QueueInputBuffer failed!");
346     if (bufferFlag == AVCODEC_BUFFER_FLAG_EOS) {
347         audioEncObj->isEOS_.store(true);
348         MEDIA_LOGD("Set eos status to true");
349     }
350 
351     return AV_ERR_OK;
352 }
353 
OH_AudioEncoder_GetOutputDescription(struct OH_AVCodec * codec)354 OH_AVFormat *OH_AudioEncoder_GetOutputDescription(struct OH_AVCodec *codec)
355 {
356     CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "input codec is nullptr!");
357     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_AUDIO_ENCODER, nullptr, "magic error!");
358 
359     struct AudioEncoderObject *audioEncObj = reinterpret_cast<AudioEncoderObject *>(codec);
360     CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, nullptr, "audioEncoder_ is nullptr!");
361 
362     Format format;
363     int32_t ret = audioEncObj->audioEncoder_->GetOutputFormat(format);
364     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "audioEncoder GetOutputFormat failed!");
365 
366     OH_AVFormat *avFormat = OH_AVFormat_Create();
367     avFormat->format_ = format;
368 
369     return avFormat;
370 }
371 
OH_AudioEncoder_FreeOutputData(struct OH_AVCodec * codec,uint32_t index)372 OH_AVErrCode OH_AudioEncoder_FreeOutputData(struct OH_AVCodec *codec, uint32_t index)
373 {
374     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
375     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
376 
377     struct AudioEncoderObject *audioEncObj = reinterpret_cast<AudioEncoderObject *>(codec);
378     CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, AV_ERR_INVALID_VAL, "audioEncoder_ is nullptr!");
379 
380     int32_t ret = audioEncObj->audioEncoder_->ReleaseOutputBuffer(index);
381     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioEncoder ReleaseOutputBuffer failed!");
382 
383     return AV_ERR_OK;
384 }
385 
OH_AudioEncoder_SetParameter(struct OH_AVCodec * codec,struct OH_AVFormat * format)386 OH_AVErrCode OH_AudioEncoder_SetParameter(struct OH_AVCodec *codec, struct OH_AVFormat *format)
387 {
388     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
389     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
390     CHECK_AND_RETURN_RET_LOG(format != nullptr, AV_ERR_INVALID_VAL, "input format is nullptr!");
391     CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::MEDIA_MAGIC_FORMAT, AV_ERR_INVALID_VAL, "magic error!");
392 
393     struct AudioEncoderObject *audioEncObj = reinterpret_cast<AudioEncoderObject *>(codec);
394     CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, AV_ERR_INVALID_VAL, "audioEncoder_ is nullptr!");
395 
396     int32_t ret = audioEncObj->audioEncoder_->SetParameter(format->format_);
397     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioEncoder SetParameter failed!");
398 
399     return AV_ERR_OK;
400 }
401 
OH_AudioEncoder_SetCallback(struct OH_AVCodec * codec,struct OH_AVCodecAsyncCallback callback,void * userData)402 OH_AVErrCode OH_AudioEncoder_SetCallback(
403     struct OH_AVCodec *codec, struct OH_AVCodecAsyncCallback callback, void *userData)
404 {
405     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
406     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
407 
408     struct AudioEncoderObject *audioEncObj = reinterpret_cast<AudioEncoderObject *>(codec);
409     CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, AV_ERR_INVALID_VAL, "audioEncoder_ is nullptr!");
410 
411     audioEncObj->callback_ = std::make_shared<NativeAudioEncoderCallback>(codec, callback, userData);
412     CHECK_AND_RETURN_RET_LOG(audioEncObj->callback_ != nullptr, AV_ERR_INVALID_VAL, "callback_ is nullptr!");
413 
414     int32_t ret = audioEncObj->audioEncoder_->SetCallback(audioEncObj->callback_);
415     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioEncoder SetCallback failed!");
416 
417     return AV_ERR_OK;
418 }
419