• 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 "napi/native_api.h"
17 #include <condition_variable>
18 #include <js_native_api_types.h>
19 #include <multimedia/player_framework/native_avcapability.h>
20 #include <multimedia/player_framework/native_avcodec_audiocodec.h>
21 #include <multimedia/player_framework/native_avcodec_base.h>
22 #include <multimedia/player_framework/native_averrors.h>
23 #include <multimedia/player_framework/native_avformat.h>
24 #include <multimedia/player_framework/native_avbuffer.h>
25 #include <pthread.h>
26 #include <thread>
27 #include <queue>
28 #include <fstream>
29 #include <iostream>
30 
31 #define FAIL (-1)
32 #define SUCCESS 0
33 constexpr uint32_t DEFAULT_SAMPLERATE = 44100;
34 constexpr uint32_t DEFAULT_BITRATE = 32000;
35 constexpr uint32_t DEFAULT_CHANNEL_COUNT = 2;
36 constexpr uint32_t DEFAULT_MAX_INPUT_SIZE = 1152;
37 constexpr uint32_t DEFAULT_AAC_TYPE = 1;
38 constexpr uint8_t DEFAULT_VORBIS_TYPE = 10;
39 constexpr uint8_t DEFAULT_VORBISTWO_TYPE = 20;
40 using namespace std;
41 
TestInitPtr(napi_env env,OH_AVCodec * param)42 static napi_value TestInitPtr(napi_env env, OH_AVCodec *param)
43 {
44     int backParam = FAIL;
45     napi_value result = nullptr;
46     OH_AVCodec *checkParam = param;
47     if (checkParam != nullptr) {
48         backParam = SUCCESS;
49     }
50     napi_create_int32(env, backParam, &result);
51     return result;
52 }
53 
TestInitAVErrCode(napi_env env,OH_AVErrCode param)54 static napi_value TestInitAVErrCode(napi_env env, OH_AVErrCode param)
55 {
56     int backParam = FAIL;
57     napi_value result = nullptr;
58     OH_AVErrCode checkParam = param;
59     if (checkParam == AV_ERR_OK) {
60         backParam = SUCCESS;
61     }
62     napi_create_int32(env, backParam, &result);
63     return result;
64 }
65 
AudioDecoderCreateByMimeAudioAac(napi_env env,napi_callback_info info)66 static napi_value AudioDecoderCreateByMimeAudioAac(napi_env env, napi_callback_info info)
67 {
68     return TestInitPtr(env, OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC, false));
69 }
70 
AudioDecoderCreateByName(napi_env env,napi_callback_info info)71 static napi_value AudioDecoderCreateByName(napi_env env, napi_callback_info info)
72 {
73     OH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_AUDIO_VORBIS, false);
74     const char *name = OH_AVCapability_GetName(capability);
75     return TestInitPtr(env, OH_AudioCodec_CreateByName(name));
76 }
77 
AudioDecoderDestroy(napi_env env,napi_callback_info info)78 static napi_value AudioDecoderDestroy(napi_env env, napi_callback_info info)
79 {
80     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_FLAC, false);
81     return TestInitAVErrCode(env, OH_AudioCodec_Destroy(audioDec));
82 }
83 
84 class ADecSignal {
85 public:
86     std::mutex inMutex_;
87     std::mutex outMutex_;
88     std::mutex startMutex_;
89     std::condition_variable inCond_;
90     std::condition_variable outCond_;
91     std::condition_variable startCond_;
92     std::queue<uint32_t> inQueue_;
93     std::queue<uint32_t> outQueue_;
94     std::queue<OH_AVBuffer *> inBufferQueue_;
95     std::queue<OH_AVBuffer *> outBufferQueue_;
96 };
97 ADecSignal *signal_;
OnError(OH_AVCodec * codec,int32_t errorCode,void * userData)98 static void OnError(OH_AVCodec *codec, int32_t errorCode, void *userData)
99 {
100     (void)codec;
101     (void)errorCode;
102     (void)userData;
103 }
OnStreamChanged(OH_AVCodec * codec,OH_AVFormat * format,void * userData)104 static void OnStreamChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData)
105 {
106     (void)codec;
107     (void)format;
108     (void)userData;
109 }
OnNeedInputBuffer(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * buffer,void * userData)110 static void OnNeedInputBuffer(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData)
111 {
112     (void)codec;
113     ADecSignal *signal = static_cast<ADecSignal *>(userData);
114     unique_lock<mutex> lock(signal_->inMutex_);
115     OH_AVCodecBufferAttr attr = {0};
116     attr.flags = 1;
117     OH_AVBuffer_SetBufferAttr(buffer, &attr);
118     OH_AVErrCode ret = OH_AudioCodec_PushInputBuffer(codec, index);
119     signal->inQueue_.push(ret);
120     signal->inCond_.notify_all();
121 }
OnNewOutputBuffer(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * buffer,void * userData)122 static void OnNewOutputBuffer(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData)
123 {
124     (void)codec;
125     ADecSignal *signal = static_cast<ADecSignal *>(userData);
126     unique_lock<mutex> lock(signal_->outMutex_);
127     OH_AVErrCode ret = OH_AudioCodec_FreeOutputBuffer(codec, index);
128     signal->outQueue_.push(ret);
129     signal->outCond_.notify_all();
130 }
131 
AudioDecoderRegisterCallback(napi_env env,napi_callback_info info)132 static napi_value AudioDecoderRegisterCallback(napi_env env, napi_callback_info info)
133 {
134     napi_value result;
135     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC, false);
136     signal_ = new ADecSignal();
137     OH_AVCodecCallback callback = { &OnError, &OnStreamChanged, &OnNeedInputBuffer, &OnNewOutputBuffer };
138     result = TestInitAVErrCode(env, OH_AudioCodec_RegisterCallback(audioDec, callback, signal_));
139     OH_AudioCodec_Destroy(audioDec);
140     return result;
141 }
142 
AudioDecoderConfigure(napi_env env,napi_callback_info info)143 static napi_value AudioDecoderConfigure(napi_env env, napi_callback_info info)
144 {
145     OH_AVFormat *format = nullptr;
146     format = OH_AVFormat_Create();
147     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
148     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
149     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
150     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
151     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
152     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, false);
153     return TestInitAVErrCode(env, OH_AudioCodec_Configure(audioDec, format));
154 }
155 
AudioDecoderPrepare(napi_env env,napi_callback_info info)156 static napi_value AudioDecoderPrepare(napi_env env, napi_callback_info info)
157 {
158     OH_AVFormat *format = nullptr;
159     format = OH_AVFormat_Create();
160     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
161     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
162     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
163     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
164     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
165     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, false);
166     OH_AudioCodec_Configure(audioDec, format);
167     return TestInitAVErrCode(env, OH_AudioCodec_Prepare(audioDec));
168 }
169 
AudioDecoderStart(napi_env env,napi_callback_info info)170 static napi_value AudioDecoderStart(napi_env env, napi_callback_info info)
171 {
172     int backParam = FAIL;
173     napi_value result = nullptr;
174     OH_AVErrCode checkParam;
175     OH_AVFormat *format = nullptr;
176     format = OH_AVFormat_Create();
177     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
178     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
179     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
180     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
181     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
182     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, false);
183     checkParam = OH_AudioCodec_Configure(audioDec, format);
184     checkParam = OH_AudioCodec_Prepare(audioDec);
185     checkParam = OH_AudioCodec_Start(audioDec);
186     if (checkParam == AV_ERR_OK) {
187         backParam = SUCCESS;
188     }
189     napi_create_int32(env, backParam, &result);
190     return result;
191 }
192 
AudioDecoderStop(napi_env env,napi_callback_info info)193 static napi_value AudioDecoderStop(napi_env env, napi_callback_info info)
194 {
195     int backParam = FAIL;
196     napi_value result = nullptr;
197     OH_AVErrCode checkParam;
198     OH_AVFormat *format = nullptr;
199     format = OH_AVFormat_Create();
200     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
201     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
202     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
203     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
204     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
205     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, false);
206     checkParam = OH_AudioCodec_Configure(audioDec, format);
207     checkParam = OH_AudioCodec_Prepare(audioDec);
208     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
209         checkParam = OH_AudioCodec_Stop(audioDec);
210         if (checkParam == AV_ERR_OK) {
211             backParam = SUCCESS;
212         }
213     }
214     napi_create_int32(env, backParam, &result);
215     return result;
216 }
217 
AudioDecoderFlush(napi_env env,napi_callback_info info)218 static napi_value AudioDecoderFlush(napi_env env, napi_callback_info info)
219 {
220     int backParam = FAIL;
221     napi_value result = nullptr;
222     OH_AVErrCode checkParam;
223     OH_AVFormat *format = nullptr;
224     format = OH_AVFormat_Create();
225     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
226     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
227     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
228     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
229     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
230     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, false);
231     OH_AudioCodec_Configure(audioDec, format);
232     OH_AudioCodec_Prepare(audioDec);
233     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
234         checkParam = OH_AudioCodec_Flush(audioDec);
235         if (checkParam == AV_ERR_OK) {
236             backParam = SUCCESS;
237             OH_AudioCodec_Stop(audioDec);
238         }
239     }
240     napi_create_int32(env, backParam, &result);
241     return result;
242 }
243 
AudioDecoderReset(napi_env env,napi_callback_info info)244 static napi_value AudioDecoderReset(napi_env env, napi_callback_info info)
245 {
246     int backParam = FAIL;
247     napi_value result = nullptr;
248     OH_AVErrCode checkParam;
249     OH_AVFormat *format = nullptr;
250     format = OH_AVFormat_Create();
251     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
252     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
253     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
254     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
255     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
256     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, false);
257     OH_AudioCodec_Configure(audioDec, format);
258     OH_AudioCodec_Prepare(audioDec);
259     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
260         checkParam = OH_AudioCodec_Reset(audioDec);
261         if (checkParam == AV_ERR_OK) {
262             backParam = SUCCESS;
263             OH_AudioCodec_Stop(audioDec);
264         }
265     }
266     napi_create_int32(env, backParam, &result);
267     return result;
268 }
269 
AudioDecoderGetOutputDescription(napi_env env,napi_callback_info info)270 static napi_value AudioDecoderGetOutputDescription(napi_env env, napi_callback_info info)
271 {
272     int backParam = FAIL;
273     napi_value result = nullptr;
274     OH_AVFormat *checkParam = nullptr;
275     OH_AVFormat *format = nullptr;
276     format = OH_AVFormat_Create();
277     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
278     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
279     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
280     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
281     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
282     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, false);
283     OH_AudioCodec_Configure(audioDec, format);
284     OH_AudioCodec_Prepare(audioDec);
285     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
286         checkParam = OH_AudioCodec_GetOutputDescription(audioDec);
287         if (checkParam != nullptr) {
288             backParam = SUCCESS;
289             OH_AudioCodec_Stop(audioDec);
290             free(checkParam);
291         }
292     }
293     napi_create_int32(env, backParam, &result);
294     return result;
295 }
296 
AudioDecoderSetParameter(napi_env env,napi_callback_info info)297 static napi_value AudioDecoderSetParameter(napi_env env, napi_callback_info info)
298 {
299     int backParam = FAIL;
300     napi_value result = nullptr;
301     OH_AVErrCode checkParam;
302     OH_AVFormat *format = nullptr;
303     format = OH_AVFormat_Create();
304     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
305     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
306     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
307     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
308     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
309     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, false);
310     checkParam = OH_AudioCodec_Configure(audioDec, format);
311     checkParam = OH_AudioCodec_Prepare(audioDec);
312     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
313         format = OH_AudioCodec_GetOutputDescription(audioDec);
314         if (format != nullptr) {
315             checkParam = OH_AudioCodec_SetParameter(audioDec, format);
316             if (checkParam == AV_ERR_OK) {
317                 backParam = SUCCESS;
318                 free(format);
319             }
320         }
321     }
322     napi_create_int32(env, backParam, &result);
323     return result;
324 }
325 
AudioDecoderIsValid(napi_env env,napi_callback_info info)326 static napi_value AudioDecoderIsValid(napi_env env, napi_callback_info info)
327 {
328     int backParam = FAIL;
329     napi_value result = nullptr;
330     OH_AVErrCode checkParam;
331     bool status = true;
332     OH_AVFormat *format = nullptr;
333     format = OH_AVFormat_Create();
334     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
335     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
336     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
337     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
338     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
339     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, false);
340     checkParam = OH_AudioCodec_Configure(audioDec, format);
341     checkParam = OH_AudioCodec_Prepare(audioDec);
342     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
343         checkParam = OH_AudioCodec_Flush(audioDec);
344         if (checkParam == AV_ERR_OK) {
345             checkParam = OH_AudioCodec_IsValid(audioDec, &status);
346             if (checkParam == AV_ERR_OK) {
347                 backParam = SUCCESS;
348                 OH_AudioCodec_Stop(audioDec);
349             }
350         }
351     }
352     napi_create_int32(env, backParam, &result);
353     return result;
354 }
355 
OHAudioDecoderCreateByNameAnormal(napi_env env,napi_callback_info info)356 static napi_value OHAudioDecoderCreateByNameAnormal(napi_env env, napi_callback_info info)
357 {
358     OH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_AUDIO_AAC, false);
359     const char *name = OH_AVCapability_GetName(capability);
360     return TestInitPtr(env, OH_AudioCodec_CreateByName(name));
361 }
362 
OHAudioDecoderCreateByNameBnormal(napi_env env,napi_callback_info info)363 static napi_value OHAudioDecoderCreateByNameBnormal(napi_env env, napi_callback_info info)
364 {
365     OH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, false);
366     const char *name = OH_AVCapability_GetName(capability);
367     return TestInitPtr(env, OH_AudioCodec_CreateByName(name));
368 }
369 
OHAudioDecoderCreateByNameCnormal(napi_env env,napi_callback_info info)370 static napi_value OHAudioDecoderCreateByNameCnormal(napi_env env, napi_callback_info info)
371 {
372     OH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_AUDIO_FLAC, false);
373     const char *name = OH_AVCapability_GetName(capability);
374     return TestInitPtr(env, OH_AudioCodec_CreateByName(name));
375 }
376 
OHAudioDecoderDestroyAnormal(napi_env env,napi_callback_info info)377 static napi_value OHAudioDecoderDestroyAnormal(napi_env env, napi_callback_info info)
378 {
379     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC, false);
380     return TestInitAVErrCode(env, OH_AudioCodec_Destroy(audioDec));
381 }
382 
OHAudioDecoderDestroyBnormal(napi_env env,napi_callback_info info)383 static napi_value OHAudioDecoderDestroyBnormal(napi_env env, napi_callback_info info)
384 {
385     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, false);
386     return TestInitAVErrCode(env, OH_AudioCodec_Destroy(audioDec));
387 }
388 
OHAudioDecoderDestroyCnormal(napi_env env,napi_callback_info info)389 static napi_value OHAudioDecoderDestroyCnormal(napi_env env, napi_callback_info info)
390 {
391     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_VORBIS, false);
392     return TestInitAVErrCode(env, OH_AudioCodec_Destroy(audioDec));
393 }
394 
OHAudioDecoderStartAnormal(napi_env env,napi_callback_info info)395 static napi_value OHAudioDecoderStartAnormal(napi_env env, napi_callback_info info)
396 {
397     int backParam = FAIL;
398     napi_value result = nullptr;
399     OH_AVErrCode checkParam;
400     OH_AVFormat *format = nullptr;
401     format = OH_AVFormat_Create();
402     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
403     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
404     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
405     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
406     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
407     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC, false);
408     OH_AudioCodec_Configure(audioDec, format);
409     OH_AudioCodec_Prepare(audioDec);
410     checkParam = OH_AudioCodec_Start(audioDec);
411     if (checkParam == AV_ERR_OK) {
412         backParam = SUCCESS;
413     }
414     napi_create_int32(env, backParam, &result);
415     return result;
416 }
417 
OHAudioDecoderStartBnormal(napi_env env,napi_callback_info info)418 static napi_value OHAudioDecoderStartBnormal(napi_env env, napi_callback_info info)
419 {
420     int backParam = FAIL;
421     napi_value result = nullptr;
422     OH_AVErrCode checkParam;
423     OH_AVFormat *format = nullptr;
424     format = OH_AVFormat_Create();
425     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
426     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
427     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
428     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
429     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
430     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_FLAC, false);
431     OH_AudioCodec_Configure(audioDec, format);
432     OH_AudioCodec_Prepare(audioDec);
433     checkParam = OH_AudioCodec_Start(audioDec);
434     if (checkParam == AV_ERR_OK) {
435         backParam = SUCCESS;
436     }
437     napi_create_int32(env, backParam, &result);
438     return result;
439 }
440 
OHAudioDecoderStartCnormal(napi_env env,napi_callback_info info)441 static napi_value OHAudioDecoderStartCnormal(napi_env env, napi_callback_info info)
442 {
443     int backParam = FAIL;
444     napi_value result = nullptr;
445     OH_AVErrCode checkParam;
446     OH_AVFormat *format = nullptr;
447     format = OH_AVFormat_Create();
448     OH_AVFormat_SetIntValue(format, OH_MD_KEY_IDENTIFICATION_HEADER, DEFAULT_VORBISTWO_TYPE);
449     OH_AVFormat_SetIntValue(format, OH_MD_KEY_SETUP_HEADER, DEFAULT_VORBIS_TYPE);
450     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_VORBIS, false);
451     OH_AudioCodec_Configure(audioDec, format);
452     OH_AudioCodec_Prepare(audioDec);
453     checkParam = OH_AudioCodec_Start(audioDec);
454     if (checkParam == AV_ERR_OK) {
455         backParam = SUCCESS;
456     }
457     napi_create_int32(env, backParam, &result);
458     return result;
459 }
460 
OHAudioDecoderStopAnormal(napi_env env,napi_callback_info info)461 static napi_value OHAudioDecoderStopAnormal(napi_env env, napi_callback_info info)
462 {
463     int backParam = FAIL;
464     napi_value result = nullptr;
465     OH_AVErrCode checkParam;
466     OH_AVFormat *format = nullptr;
467     format = OH_AVFormat_Create();
468     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
469     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
470     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
471     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
472     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
473     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC, false);
474     OH_AudioCodec_Configure(audioDec, format);
475     OH_AudioCodec_Prepare(audioDec);
476     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
477         checkParam = OH_AudioCodec_Stop(audioDec);
478         if (checkParam == AV_ERR_OK) {
479             backParam = SUCCESS;
480         }
481     }
482     napi_create_int32(env, backParam, &result);
483     return result;
484 }
485 
OHAudioDecoderStopBnormal(napi_env env,napi_callback_info info)486 static napi_value OHAudioDecoderStopBnormal(napi_env env, napi_callback_info info)
487 {
488     int backParam = FAIL;
489     napi_value result = nullptr;
490     OH_AVErrCode checkParam;
491     OH_AVFormat *format = nullptr;
492     format = OH_AVFormat_Create();
493     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
494     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
495     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
496     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
497     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
498     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_FLAC, false);
499     OH_AudioCodec_Configure(audioDec, format);
500     OH_AudioCodec_Prepare(audioDec);
501     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
502         checkParam = OH_AudioCodec_Stop(audioDec);
503         if (checkParam == AV_ERR_OK) {
504             backParam = SUCCESS;
505         }
506     }
507     napi_create_int32(env, backParam, &result);
508     return result;
509 }
510 
OHAudioDecoderStopCnormal(napi_env env,napi_callback_info info)511 static napi_value OHAudioDecoderStopCnormal(napi_env env, napi_callback_info info)
512 {
513     int backParam = FAIL;
514     napi_value result = nullptr;
515     OH_AVErrCode checkParam;
516     OH_AVFormat *format = nullptr;
517     format = OH_AVFormat_Create();
518     OH_AVFormat_SetIntValue(format, OH_MD_KEY_IDENTIFICATION_HEADER, DEFAULT_VORBISTWO_TYPE);
519     OH_AVFormat_SetIntValue(format, OH_MD_KEY_SETUP_HEADER, DEFAULT_VORBIS_TYPE);
520     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_VORBIS, false);
521     OH_AudioCodec_Configure(audioDec, format);
522     OH_AudioCodec_Prepare(audioDec);
523     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
524         checkParam = OH_AudioCodec_Stop(audioDec);
525         if (checkParam == AV_ERR_OK) {
526             backParam = SUCCESS;
527         }
528     }
529     napi_create_int32(env, backParam, &result);
530     return result;
531 }
532 
OHAudioDecoderFlushAnormal(napi_env env,napi_callback_info info)533 static napi_value OHAudioDecoderFlushAnormal(napi_env env, napi_callback_info info)
534 {
535     int backParam = FAIL;
536     napi_value result = nullptr;
537     OH_AVErrCode checkParam;
538     OH_AVFormat *format = nullptr;
539     format = OH_AVFormat_Create();
540     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
541     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
542     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
543     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
544     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
545     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC, false);
546     OH_AudioCodec_Configure(audioDec, format);
547     OH_AudioCodec_Prepare(audioDec);
548     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
549         checkParam = OH_AudioCodec_Flush(audioDec);
550         if (checkParam == AV_ERR_OK) {
551             backParam = SUCCESS;
552             OH_AudioCodec_Stop(audioDec);
553         }
554     }
555     napi_create_int32(env, backParam, &result);
556     return result;
557 }
558 
OHAudioDecoderFlushBnormal(napi_env env,napi_callback_info info)559 static napi_value OHAudioDecoderFlushBnormal(napi_env env, napi_callback_info info)
560 {
561     int backParam = FAIL;
562     napi_value result = nullptr;
563     OH_AVErrCode checkParam;
564     OH_AVFormat *format = nullptr;
565     format = OH_AVFormat_Create();
566     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
567     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
568     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
569     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
570     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
571     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_FLAC, false);
572     OH_AudioCodec_Configure(audioDec, format);
573     OH_AudioCodec_Prepare(audioDec);
574     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
575         checkParam = OH_AudioCodec_Flush(audioDec);
576         if (checkParam == AV_ERR_OK) {
577             backParam = SUCCESS;
578             OH_AudioCodec_Stop(audioDec);
579         }
580     }
581     napi_create_int32(env, backParam, &result);
582     return result;
583 }
584 
OHAudioDecoderFlushCnormal(napi_env env,napi_callback_info info)585 static napi_value OHAudioDecoderFlushCnormal(napi_env env, napi_callback_info info)
586 {
587     int backParam = FAIL;
588     napi_value result = nullptr;
589     OH_AVErrCode checkParam;
590     OH_AVFormat *format = nullptr;
591     format = OH_AVFormat_Create();
592     OH_AVFormat_SetIntValue(format, OH_MD_KEY_IDENTIFICATION_HEADER, DEFAULT_VORBISTWO_TYPE);
593     OH_AVFormat_SetIntValue(format, OH_MD_KEY_SETUP_HEADER, DEFAULT_VORBIS_TYPE);
594     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_VORBIS, false);
595     OH_AudioCodec_Configure(audioDec, format);
596     OH_AudioCodec_Prepare(audioDec);
597     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
598         checkParam = OH_AudioCodec_Flush(audioDec);
599         if (checkParam == AV_ERR_OK) {
600             backParam = SUCCESS;
601             OH_AudioCodec_Stop(audioDec);
602         }
603     }
604     napi_create_int32(env, backParam, &result);
605     return result;
606 }
607 
OHAudioDecoderResetAnormal(napi_env env,napi_callback_info info)608 static napi_value OHAudioDecoderResetAnormal(napi_env env, napi_callback_info info)
609 {
610     int backParam = FAIL;
611     napi_value result = nullptr;
612     OH_AVErrCode checkParam;
613     OH_AVFormat *format = nullptr;
614     format = OH_AVFormat_Create();
615     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
616     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
617     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
618     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
619     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
620     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, false);
621     OH_AudioCodec_Configure(audioDec, format);
622     OH_AudioCodec_Prepare(audioDec);
623     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
624         checkParam = OH_AudioCodec_Reset(audioDec);
625         if (checkParam == AV_ERR_OK) {
626             backParam = SUCCESS;
627             OH_AudioCodec_Stop(audioDec);
628         }
629     }
630     napi_create_int32(env, backParam, &result);
631     return result;
632 }
633 
OHAudioDecoderResetBnormal(napi_env env,napi_callback_info info)634 static napi_value OHAudioDecoderResetBnormal(napi_env env, napi_callback_info info)
635 {
636     int backParam = FAIL;
637     napi_value result = nullptr;
638     OH_AVErrCode checkParam;
639     OH_AVFormat *format = nullptr;
640     format = OH_AVFormat_Create();
641     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
642     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
643     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
644     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
645     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
646     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_FLAC, false);
647     OH_AudioCodec_Configure(audioDec, format);
648     OH_AudioCodec_Prepare(audioDec);
649     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
650         checkParam = OH_AudioCodec_Reset(audioDec);
651         if (checkParam == AV_ERR_OK) {
652             backParam = SUCCESS;
653             OH_AudioCodec_Stop(audioDec);
654         }
655     }
656     napi_create_int32(env, backParam, &result);
657     return result;
658 }
659 
OHAudioDecoderResetCnormal(napi_env env,napi_callback_info info)660 static napi_value OHAudioDecoderResetCnormal(napi_env env, napi_callback_info info)
661 {
662     int backParam = FAIL;
663     napi_value result = nullptr;
664     OH_AVErrCode checkParam;
665     OH_AVFormat *format = nullptr;
666     format = OH_AVFormat_Create();
667     OH_AVFormat_SetIntValue(format, OH_MD_KEY_IDENTIFICATION_HEADER, DEFAULT_VORBISTWO_TYPE);
668     OH_AVFormat_SetIntValue(format, OH_MD_KEY_SETUP_HEADER, DEFAULT_VORBIS_TYPE);
669     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_VORBIS, false);
670     OH_AudioCodec_Configure(audioDec, format);
671     OH_AudioCodec_Prepare(audioDec);
672     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
673         checkParam = OH_AudioCodec_Reset(audioDec);
674         if (checkParam == AV_ERR_OK) {
675             backParam = SUCCESS;
676             OH_AudioCodec_Stop(audioDec);
677         }
678     }
679     napi_create_int32(env, backParam, &result);
680     return result;
681 }
682 
OHAudioDecoderGetOutputDescriptionAnormal(napi_env env,napi_callback_info info)683 static napi_value OHAudioDecoderGetOutputDescriptionAnormal(napi_env env, napi_callback_info info)
684 {
685     int backParam = FAIL;
686     napi_value result = nullptr;
687     OH_AVFormat *checkParam = nullptr;
688     OH_AVFormat *format = nullptr;
689     format = OH_AVFormat_Create();
690     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
691     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
692     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
693     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
694     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
695     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC, false);
696     OH_AudioCodec_Configure(audioDec, format);
697     OH_AudioCodec_Prepare(audioDec);
698     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
699         checkParam = OH_AudioCodec_GetOutputDescription(audioDec);
700         if (checkParam != nullptr) {
701             backParam = SUCCESS;
702             OH_AudioCodec_Stop(audioDec);
703             free(checkParam);
704         }
705     }
706     napi_create_int32(env, backParam, &result);
707     return result;
708 }
709 
OHAudioDecoderGetOutputDescriptionBnormal(napi_env env,napi_callback_info info)710 static napi_value OHAudioDecoderGetOutputDescriptionBnormal(napi_env env, napi_callback_info info)
711 {
712     int backParam = FAIL;
713     napi_value result = nullptr;
714     OH_AVFormat *checkParam = nullptr;
715     OH_AVFormat *format = nullptr;
716     format = OH_AVFormat_Create();
717     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
718     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
719     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
720     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
721     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
722     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_FLAC, false);
723     OH_AudioCodec_Configure(audioDec, format);
724     OH_AudioCodec_Prepare(audioDec);
725     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
726         checkParam = OH_AudioCodec_GetOutputDescription(audioDec);
727         if (checkParam != nullptr) {
728             backParam = SUCCESS;
729             OH_AudioCodec_Stop(audioDec);
730             free(checkParam);
731         }
732     }
733     napi_create_int32(env, backParam, &result);
734     return result;
735 }
736 
OHAudioDecoderGetOutputDescriptionCnormal(napi_env env,napi_callback_info info)737 static napi_value OHAudioDecoderGetOutputDescriptionCnormal(napi_env env, napi_callback_info info)
738 {
739     int backParam = FAIL;
740     napi_value result = nullptr;
741     OH_AVFormat *checkParam = nullptr;
742     OH_AVFormat *format = nullptr;
743     format = OH_AVFormat_Create();
744     OH_AVFormat_SetIntValue(format, OH_MD_KEY_IDENTIFICATION_HEADER, DEFAULT_VORBISTWO_TYPE);
745     OH_AVFormat_SetIntValue(format, OH_MD_KEY_SETUP_HEADER, DEFAULT_VORBIS_TYPE);
746     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_VORBIS, false);
747     OH_AudioCodec_Configure(audioDec, format);
748     OH_AudioCodec_Prepare(audioDec);
749     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
750         checkParam = OH_AudioCodec_GetOutputDescription(audioDec);
751         if (checkParam != nullptr) {
752             backParam = SUCCESS;
753             OH_AudioCodec_Stop(audioDec);
754             free(checkParam);
755         }
756     }
757     napi_create_int32(env, backParam, &result);
758     return result;
759 }
760 
OHAudioDecoderSetParameterAnormal(napi_env env,napi_callback_info info)761 static napi_value OHAudioDecoderSetParameterAnormal(napi_env env, napi_callback_info info)
762 {
763     int backParam = FAIL;
764     napi_value result = nullptr;
765     OH_AVErrCode checkParam;
766     OH_AVFormat *format = nullptr;
767     format = OH_AVFormat_Create();
768     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
769     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
770     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
771     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
772     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
773     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC, false);
774     OH_AudioCodec_Configure(audioDec, format);
775     OH_AudioCodec_Prepare(audioDec);
776     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
777         format = OH_AudioCodec_GetOutputDescription(audioDec);
778         if (format != nullptr) {
779             checkParam = OH_AudioCodec_SetParameter(audioDec, format);
780             if (checkParam == AV_ERR_OK) {
781                 backParam = SUCCESS;
782                 free(format);
783             }
784         }
785     }
786     napi_create_int32(env, backParam, &result);
787     return result;
788 }
789 
OHAudioDecoderSetParameterBnormal(napi_env env,napi_callback_info info)790 static napi_value OHAudioDecoderSetParameterBnormal(napi_env env, napi_callback_info info)
791 {
792     int backParam = FAIL;
793     napi_value result = nullptr;
794     OH_AVErrCode checkParam;
795     OH_AVFormat *format = nullptr;
796     format = OH_AVFormat_Create();
797     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
798     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
799     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
800     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
801     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
802     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_FLAC, false);
803     OH_AudioCodec_Configure(audioDec, format);
804     OH_AudioCodec_Prepare(audioDec);
805     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
806         format = OH_AudioCodec_GetOutputDescription(audioDec);
807         if (format != nullptr) {
808             checkParam = OH_AudioCodec_SetParameter(audioDec, format);
809             if (checkParam == AV_ERR_OK) {
810                 backParam = SUCCESS;
811                 free(format);
812             }
813         }
814     }
815     napi_create_int32(env, backParam, &result);
816     return result;
817 }
818 
OHAudioDecoderSetParameterCnormal(napi_env env,napi_callback_info info)819 static napi_value OHAudioDecoderSetParameterCnormal(napi_env env, napi_callback_info info)
820 {
821     int backParam = FAIL;
822     napi_value result = nullptr;
823     OH_AVErrCode checkParam;
824     OH_AVFormat *format = nullptr;
825     format = OH_AVFormat_Create();
826     OH_AVFormat_SetIntValue(format, OH_MD_KEY_IDENTIFICATION_HEADER, DEFAULT_VORBISTWO_TYPE);
827     OH_AVFormat_SetIntValue(format, OH_MD_KEY_SETUP_HEADER, DEFAULT_VORBIS_TYPE);
828     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_VORBIS, false);
829     OH_AudioCodec_Configure(audioDec, format);
830     OH_AudioCodec_Prepare(audioDec);
831     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
832         format = OH_AudioCodec_GetOutputDescription(audioDec);
833         if (format != nullptr) {
834             checkParam = OH_AudioCodec_SetParameter(audioDec, format);
835             if (checkParam == AV_ERR_OK) {
836                 backParam = SUCCESS;
837                 free(format);
838             }
839         }
840     }
841     napi_create_int32(env, backParam, &result);
842     return result;
843 }
844 
OHAudioDecoderIsValidAnormal(napi_env env,napi_callback_info info)845 static napi_value OHAudioDecoderIsValidAnormal(napi_env env, napi_callback_info info)
846 {
847     int backParam = FAIL;
848     napi_value result = nullptr;
849     OH_AVErrCode checkParam;
850     bool status = true;
851     OH_AVFormat *format = nullptr;
852     format = OH_AVFormat_Create();
853     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
854     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
855     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
856     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
857     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
858     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC, false);
859     OH_AudioCodec_Configure(audioDec, format);
860     OH_AudioCodec_Prepare(audioDec);
861     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
862         checkParam = OH_AudioCodec_Flush(audioDec);
863         if (checkParam == AV_ERR_OK) {
864             checkParam = OH_AudioCodec_IsValid(audioDec, &status);
865             if (checkParam == AV_ERR_OK) {
866                 backParam = SUCCESS;
867                 OH_AudioCodec_Stop(audioDec);
868             }
869         }
870     }
871     napi_create_int32(env, backParam, &result);
872     return result;
873 }
874 
OHAudioDecoderIsValidBnormal(napi_env env,napi_callback_info info)875 static napi_value OHAudioDecoderIsValidBnormal(napi_env env, napi_callback_info info)
876 {
877     int backParam = FAIL;
878     napi_value result = nullptr;
879     OH_AVErrCode checkParam;
880     bool status = true;
881     OH_AVFormat *format = nullptr;
882     format = OH_AVFormat_Create();
883     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
884     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
885     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
886     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
887     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
888     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_FLAC, false);
889     OH_AudioCodec_Configure(audioDec, format);
890     OH_AudioCodec_Prepare(audioDec);
891     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
892         checkParam = OH_AudioCodec_Flush(audioDec);
893         if (checkParam == AV_ERR_OK) {
894             checkParam = OH_AudioCodec_IsValid(audioDec, &status);
895             if (checkParam == AV_ERR_OK) {
896                 backParam = SUCCESS;
897                 OH_AudioCodec_Stop(audioDec);
898             }
899         }
900     }
901     napi_create_int32(env, backParam, &result);
902     return result;
903 }
904 
OHAudioDecoderIsValidCnormal(napi_env env,napi_callback_info info)905 static napi_value OHAudioDecoderIsValidCnormal(napi_env env, napi_callback_info info)
906 {
907     int backParam = FAIL;
908     napi_value result = nullptr;
909     OH_AVErrCode checkParam;
910     bool status = true;
911     OH_AVFormat *format = nullptr;
912     format = OH_AVFormat_Create();
913     OH_AVFormat_SetIntValue(format, OH_MD_KEY_IDENTIFICATION_HEADER, DEFAULT_VORBISTWO_TYPE);
914     OH_AVFormat_SetIntValue(format, OH_MD_KEY_SETUP_HEADER, DEFAULT_VORBIS_TYPE);
915     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_VORBIS, false);
916     OH_AudioCodec_Configure(audioDec, format);
917     OH_AudioCodec_Prepare(audioDec);
918     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
919         checkParam = OH_AudioCodec_Flush(audioDec);
920         if (checkParam == AV_ERR_OK) {
921             checkParam = OH_AudioCodec_IsValid(audioDec, &status);
922             if (checkParam == AV_ERR_OK) {
923                 backParam = SUCCESS;
924                 OH_AudioCodec_Stop(audioDec);
925             }
926         }
927     }
928     napi_create_int32(env, backParam, &result);
929     return result;
930 }
931 
OHAudioDecoderPushInputBuffer(napi_env env,napi_callback_info info)932 static napi_value OHAudioDecoderPushInputBuffer(napi_env env, napi_callback_info info)
933 {
934     int backParam = FAIL;
935     napi_value result = nullptr;
936     OH_AVCodec *audioDec = nullptr;
937     uint32_t index = 0;
938     OH_AVErrCode checkParam;
939     OH_AVFormat *format = nullptr;
940     format = OH_AVFormat_Create();
941     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
942     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
943     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
944     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
945     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
946     audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, false);
947     OH_AVCodecCallback callback = {&OnError, &OnStreamChanged, &OnNeedInputBuffer, &OnNewOutputBuffer};
948     signal_ = new ADecSignal();
949     OH_AudioCodec_RegisterCallback(audioDec, callback, signal_);
950     OH_AudioCodec_Configure(audioDec, format);
951     OH_AudioCodec_Start(audioDec);
952 	{
953 	    unique_lock<mutex> lock(signal_->inMutex_);
954 	    signal_->inCond_.wait(lock, []() { return (signal_->inQueue_.size() > 0);});
955 	}
956     if (signal_->inQueue_.front() == 0) {
957         backParam = SUCCESS;
958     }
959     OH_AudioCodec_Stop(audioDec);
960     OH_AudioCodec_Destroy(audioDec);
961     napi_create_int32(env, backParam, &result);
962     return result;
963 }
964 
OHAudioDecoderFreeOutputBuffer(napi_env env,napi_callback_info info)965 static napi_value OHAudioDecoderFreeOutputBuffer(napi_env env, napi_callback_info info)
966 {
967     int backParam = FAIL;
968     napi_value result = nullptr;
969     OH_AVCodec *audioDec = nullptr;
970     uint32_t index = 0;
971     OH_AVErrCode checkParam;
972     OH_AVFormat *format = nullptr;
973     format = OH_AVFormat_Create();
974     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
975     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
976     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
977     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
978     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
979     audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, false);
980     OH_AVCodecCallback callback = {&OnError, &OnStreamChanged, &OnNeedInputBuffer, &OnNewOutputBuffer};
981     signal_ = new ADecSignal();
982     OH_AudioCodec_RegisterCallback(audioDec, callback, signal_);
983     OH_AudioCodec_Configure(audioDec, format);
984     OH_AudioCodec_Start(audioDec);
985     {
986         unique_lock<mutex> lock(signal_->outMutex_);
987         signal_->outCond_.wait(lock, []() { return (signal_->outQueue_.size() > 0); });
988     }
989     if (signal_->outQueue_.front() == 0) {
990         backParam = SUCCESS;
991     }
992     OH_AudioCodec_Stop(audioDec);
993     OH_AudioCodec_Destroy(audioDec);
994     napi_create_int32(env, backParam, &result);
995     return result;
996 }
997 
OHAudioDecoderAmrnb(napi_env env,napi_callback_info info)998 static napi_value OHAudioDecoderAmrnb(napi_env env, napi_callback_info info)
999 {
1000     int backParam = FAIL;
1001     napi_value result = nullptr;
1002     OH_AVCodec *audioDec = nullptr;
1003     audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AMR_NB, false);
1004     if (audioDec) {
1005         backParam = SUCCESS;
1006     }
1007     OH_AudioCodec_Destroy(audioDec);
1008     napi_create_int32(env, backParam, &result);
1009     return result;
1010 }
1011 
OHAudioDecoderAmrwb(napi_env env,napi_callback_info info)1012 static napi_value OHAudioDecoderAmrwb(napi_env env, napi_callback_info info)
1013 {
1014     int backParam = FAIL;
1015     napi_value result = nullptr;
1016     OH_AVCodec *audioDec = nullptr;
1017     audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AMR_WB, false);
1018     if (audioDec) {
1019         backParam = SUCCESS;
1020     }
1021     OH_AudioCodec_Destroy(audioDec);
1022     napi_create_int32(env, backParam, &result);
1023     return result;
1024 }
1025 
OHAudioDecoderOpus(napi_env env,napi_callback_info info)1026 static napi_value OHAudioDecoderOpus(napi_env env, napi_callback_info info)
1027 {
1028     int backParam = FAIL;
1029     napi_value result = nullptr;
1030     OH_AVCodec *audioDec = nullptr;
1031     audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_OPUS, false);
1032     backParam = SUCCESS;
1033     napi_create_int32(env, backParam, &result);
1034     return result;
1035 }
1036 
OHAudioDecoderG711Mu(napi_env env,napi_callback_info info)1037 static napi_value OHAudioDecoderG711Mu(napi_env env, napi_callback_info info)
1038 {
1039     int backParam = FAIL;
1040     napi_value result = nullptr;
1041     OH_AVCodec *audioDec = nullptr;
1042     audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_G711MU, false);
1043     if (audioDec) {
1044         backParam = SUCCESS;
1045     }
1046     OH_AudioCodec_Destroy(audioDec);
1047     napi_create_int32(env, backParam, &result);
1048     return result;
1049 }
1050 
OHAudioDecoderViVid(napi_env env,napi_callback_info info)1051 static napi_value OHAudioDecoderViVid(napi_env env, napi_callback_info info)
1052 {
1053     int backParam = FAIL;
1054     napi_value result = nullptr;
1055     OH_AVFormat *checkParam = nullptr;
1056     OH_AVFormat *format = nullptr;
1057     format = OH_AVFormat_Create();
1058     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
1059     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
1060     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_VIVID, false);
1061     backParam = SUCCESS;
1062     if (audioDec) {
1063         OH_AudioCodec_Configure(audioDec, format);
1064         OH_AudioCodec_Prepare(audioDec);
1065         if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
1066             checkParam = OH_AudioCodec_GetOutputDescription(audioDec);
1067             if (checkParam != nullptr) {
1068                 backParam = SUCCESS;
1069                 int32_t objNum;
1070                 std::vector<uint8_t> metaData;
1071                 uint8_t **addr;
1072                 size_t size;
1073                 OH_AVFormat_GetIntValue(format, OH_MD_KEY_AUDIO_OBJECT_NUMBER, &objNum);
1074                 OH_AVFormat_GetBuffer(format, OH_MD_KEY_AUDIO_VIVID_METADATA, addr, &size);
1075                 OH_AudioCodec_Stop(audioDec);
1076                 free(checkParam);
1077             }
1078         }
1079     }
1080     OH_AudioCodec_Destroy(audioDec);
1081     napi_create_int32(env, backParam, &result);
1082     return result;
1083 }
1084 
1085 static uint32_t g_outputFormatChangedTimes = 0;
1086 static int32_t g_outputSampleRate = 0;
1087 static int32_t g_outputChannels = 0;
1088 static OH_AVCodec *g_audioDec = nullptr;
1089 static std::atomic<bool> g_isRunning(false);
1090 static std::ifstream g_inputFile;
1091 
TestOnError(OH_AVCodec * codec,int32_t errorCode,void * userData)1092 static void TestOnError(OH_AVCodec *codec, int32_t errorCode, void *userData)
1093 {
1094     (void)codec;
1095     (void)errorCode;
1096     (void)userData;
1097 }
TestOnStreamChanged(OH_AVCodec * codec,OH_AVFormat * format,void * userData)1098 static void TestOnStreamChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData)
1099 {
1100     (void)codec;
1101     (void)userData;
1102     g_outputFormatChangedTimes++;
1103     OH_AVFormat_GetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, &g_outputChannels);
1104     OH_AVFormat_GetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, &g_outputSampleRate);
1105 }
TestOnNeedInputBuffer(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * buffer,void * userData)1106 static void TestOnNeedInputBuffer(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData)
1107 {
1108     (void)codec;
1109     ADecSignal *signal = static_cast<ADecSignal *>(userData);
1110     unique_lock<mutex> lock(signal->inMutex_);
1111     signal->inQueue_.push(index);
1112     signal->inBufferQueue_.push(buffer);
1113     signal->inCond_.notify_all();
1114 }
TestOnNewOutputBuffer(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * buffer,void * userData)1115 static void TestOnNewOutputBuffer(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData)
1116 {
1117     (void)buffer;
1118     (void)userData;
1119     OH_AudioCodec_FreeOutputBuffer(codec, index);
1120 }
1121 
ReadBuffer(OH_AVBuffer * buffer,uint32_t index)1122 static bool ReadBuffer(OH_AVBuffer *buffer, uint32_t index)
1123 {
1124     OH_AVCodecBufferAttr info;
1125     uint64_t needSize;
1126     bool readResult = false;
1127     do {
1128         g_inputFile.read(reinterpret_cast<char *>(&needSize), sizeof(needSize));
1129         if (g_inputFile.eof() || g_inputFile.gcount() == 0 || needSize == 0) {
1130             break;
1131         }
1132         if (g_inputFile.gcount() != sizeof(needSize)) {
1133             break;
1134         }
1135         info.size = (int32_t)needSize;
1136         g_inputFile.read(reinterpret_cast<char *>(&info.pts), sizeof(info.pts));
1137         if (g_inputFile.gcount() != sizeof(info.pts)) {
1138             break;
1139         }
1140         info.flags = AVCODEC_BUFFER_FLAGS_NONE;
1141         OH_AVBuffer_SetBufferAttr(buffer, &info);
1142         g_inputFile.read((char *)OH_AVBuffer_GetAddr(buffer), info.size);
1143         if (g_inputFile.gcount() != info.size) {
1144             break;
1145         }
1146         readResult = true;
1147     } while (0);
1148 
1149     if (!readResult) {
1150         info.size = 1;
1151         info.flags = AVCODEC_BUFFER_FLAGS_EOS;
1152         OH_AVBuffer_SetBufferAttr(buffer, &info);
1153     }
1154     return readResult;
1155 }
1156 
InputFunc()1157 void InputFunc()
1158 {
1159     g_inputFile.open("/data/storage/el2/base/haps/entry_test/files/aac_2c_44100hz_199k_lc.dat", std::ios::binary);
1160     if (!g_inputFile.is_open()) {
1161         return;
1162     }
1163     int32_t i = 0;
1164     while (g_isRunning.load()) {
1165         unique_lock<mutex> lock(signal_->inMutex_);
1166         signal_->inCond_.wait(lock, []() { return (signal_->inQueue_.size() > 0 || !g_isRunning.load()); });
1167 
1168         if (!g_isRunning.load()) {
1169             break;
1170         }
1171 
1172         uint32_t index = signal_->inQueue_.front();
1173         auto buffer = signal_->inBufferQueue_.front();
1174         if (buffer == nullptr) {
1175             break;
1176         }
1177         if (ReadBuffer(buffer, index) == false) {
1178             OH_AudioCodec_PushInputBuffer(g_audioDec, index);
1179             signal_->inBufferQueue_.pop();
1180             signal_->inQueue_.pop();
1181             break;
1182         }
1183         int32_t ret = OH_AudioCodec_PushInputBuffer(g_audioDec, index);
1184         signal_->inQueue_.pop();
1185         signal_->inBufferQueue_.pop();
1186         if (ret != 0) {
1187             break;
1188         }
1189     }
1190     g_inputFile.close();
1191     g_isRunning.store(false);
1192     signal_->startCond_.notify_all();
1193 }
1194 
OHAudioDecoderOutputFormatChange(napi_env env,napi_callback_info info)1195 static napi_value OHAudioDecoderOutputFormatChange(napi_env env, napi_callback_info info)
1196 {
1197     int backParam = FAIL;
1198     napi_value result = nullptr;
1199     uint32_t index = 0;
1200     OH_AVErrCode checkParam;
1201     OH_AVFormat *format = nullptr;
1202     signal_ = new ADecSignal();
1203     format = OH_AVFormat_Create();
1204     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
1205     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
1206     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_S16LE);
1207     // not match actual channels
1208     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, 1);
1209     g_audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC, false);
1210     OH_AVCodecCallback callback = {&TestOnError, &TestOnStreamChanged, &TestOnNeedInputBuffer, &TestOnNewOutputBuffer};
1211 
1212     OH_AudioCodec_RegisterCallback(g_audioDec, callback, signal_);
1213     OH_AudioCodec_Configure(g_audioDec, format);
1214     g_isRunning.store(true);
1215     std::thread inputLoop(InputFunc);
1216 
1217     OH_AudioCodec_Start(g_audioDec);
1218     {
1219         unique_lock<mutex> lock(signal_->startMutex_);
1220         signal_->startCond_.wait(lock, []() { return (!(g_isRunning.load())); });
1221     }
1222 
1223     OH_AudioCodec_Stop(g_audioDec);
1224     OH_AudioCodec_Destroy(g_audioDec);
1225     OH_AVFormat_Destroy(format);
1226     if (inputLoop.joinable()) {
1227         inputLoop.join();
1228     }
1229     if (g_outputSampleRate == DEFAULT_SAMPLERATE && g_outputChannels == DEFAULT_CHANNEL_COUNT &&
1230         g_outputFormatChangedTimes == 1) {
1231         backParam = SUCCESS;
1232     }
1233     g_outputFormatChangedTimes = 0;
1234     delete signal_;
1235     signal_ = nullptr;
1236 
1237     napi_create_int32(env, backParam, &result);
1238     return result;
1239 }
1240 
1241 EXTERN_C_START
Init(napi_env env,napi_value exports)1242 static napi_value Init(napi_env env, napi_value exports)
1243 {
1244     napi_property_descriptor desc[] = {
1245         {"OH_AudioCodec_CreateByMime", nullptr, AudioDecoderCreateByMimeAudioAac, nullptr, nullptr, nullptr,
1246          napi_default, nullptr},
1247         {"OH_AudioCodec_CreateByName", nullptr, AudioDecoderCreateByName, nullptr, nullptr, nullptr, napi_default,
1248          nullptr},
1249         {"OH_AudioCodec_Destroy", nullptr, AudioDecoderDestroy, nullptr, nullptr, nullptr, napi_default, nullptr},
1250         {"OH_AudioCodec_RegisterCallback", nullptr, AudioDecoderRegisterCallback, nullptr, nullptr, nullptr, napi_default,
1251          nullptr},
1252         {"OH_AudioCodec_Configure", nullptr, AudioDecoderConfigure, nullptr, nullptr, nullptr, napi_default, nullptr},
1253         {"OH_AudioCodec_Prepare", nullptr, AudioDecoderPrepare, nullptr, nullptr, nullptr, napi_default, nullptr},
1254         {"OH_AudioCodec_Start", nullptr, AudioDecoderStart, nullptr, nullptr, nullptr, napi_default, nullptr},
1255         {"OH_AudioCodec_Stop", nullptr, AudioDecoderStop, nullptr, nullptr, nullptr, napi_default, nullptr},
1256         {"OH_AudioCodec_Flush", nullptr, AudioDecoderFlush, nullptr, nullptr, nullptr, napi_default, nullptr},
1257         {"OH_AudioCodec_Reset", nullptr, AudioDecoderReset, nullptr, nullptr, nullptr, napi_default, nullptr},
1258         {"OH_AudioCodec_GetOutputDescription", nullptr, AudioDecoderGetOutputDescription, nullptr, nullptr, nullptr,
1259          napi_default, nullptr},
1260         {"OH_AudioCodec_SetParameter", nullptr, AudioDecoderSetParameter, nullptr, nullptr, nullptr, napi_default,
1261          nullptr},
1262         {"OH_AudioCodec_IsValid", nullptr, AudioDecoderIsValid, nullptr, nullptr, nullptr, napi_default, nullptr},
1263         {"OHAudioDecoderCreateByNameAnormal", nullptr, OHAudioDecoderCreateByNameAnormal, nullptr, nullptr, nullptr,
1264          napi_default, nullptr},
1265         {"OHAudioDecoderCreateByNameBnormal", nullptr, OHAudioDecoderCreateByNameBnormal, nullptr, nullptr, nullptr,
1266          napi_default, nullptr},
1267         {"OHAudioDecoderCreateByNameCnormal", nullptr, OHAudioDecoderCreateByNameCnormal, nullptr, nullptr, nullptr,
1268          napi_default, nullptr},
1269         {"OHAudioDecoderDestroyAnormal", nullptr, OHAudioDecoderDestroyAnormal, nullptr, nullptr, nullptr, napi_default,
1270          nullptr},
1271         {"OHAudioDecoderDestroyBnormal", nullptr, OHAudioDecoderDestroyBnormal, nullptr, nullptr, nullptr, napi_default,
1272          nullptr},
1273         {"OHAudioDecoderDestroyCnormal", nullptr, OHAudioDecoderDestroyCnormal, nullptr, nullptr, nullptr, napi_default,
1274          nullptr},
1275         {"OHAudioDecoderStartAnormal", nullptr, OHAudioDecoderStartAnormal, nullptr, nullptr, nullptr, napi_default,
1276          nullptr},
1277         {"OHAudioDecoderStartBnormal", nullptr, OHAudioDecoderStartBnormal, nullptr, nullptr, nullptr, napi_default,
1278          nullptr},
1279         {"OHAudioDecoderStartCnormal", nullptr, OHAudioDecoderStartCnormal, nullptr, nullptr, nullptr, napi_default,
1280          nullptr},
1281         {"OHAudioDecoderStopAnormal", nullptr, OHAudioDecoderStopAnormal, nullptr, nullptr, nullptr, napi_default,
1282          nullptr},
1283         {"OHAudioDecoderStopBnormal", nullptr, OHAudioDecoderStopBnormal, nullptr, nullptr, nullptr, napi_default,
1284          nullptr},
1285         {"OHAudioDecoderStopCnormal", nullptr, OHAudioDecoderStopCnormal, nullptr, nullptr, nullptr, napi_default,
1286          nullptr},
1287         {"OHAudioDecoderFlushAnormal", nullptr, OHAudioDecoderFlushAnormal, nullptr, nullptr, nullptr, napi_default,
1288          nullptr},
1289         {"OHAudioDecoderFlushBnormal", nullptr, OHAudioDecoderFlushBnormal, nullptr, nullptr, nullptr, napi_default,
1290          nullptr},
1291         {"OHAudioDecoderFlushCnormal", nullptr, OHAudioDecoderFlushCnormal, nullptr, nullptr, nullptr, napi_default,
1292          nullptr},
1293         {"OHAudioDecoderResetAnormal", nullptr, OHAudioDecoderResetAnormal, nullptr, nullptr, nullptr, napi_default,
1294          nullptr},
1295         {"OHAudioDecoderResetBnormal", nullptr, OHAudioDecoderResetBnormal, nullptr, nullptr, nullptr, napi_default,
1296          nullptr},
1297         {"OHAudioDecoderResetCnormal", nullptr, OHAudioDecoderResetCnormal, nullptr, nullptr, nullptr, napi_default,
1298          nullptr},
1299         {"OHAudioDecoderGetOutputDescriptionAnormal", nullptr, OHAudioDecoderGetOutputDescriptionAnormal, nullptr,
1300          nullptr, nullptr, napi_default, nullptr},
1301         {"OHAudioDecoderGetOutputDescriptionBnormal", nullptr, OHAudioDecoderGetOutputDescriptionBnormal, nullptr,
1302          nullptr, nullptr, napi_default, nullptr},
1303         {"OHAudioDecoderGetOutputDescriptionCnormal", nullptr, OHAudioDecoderGetOutputDescriptionCnormal, nullptr,
1304          nullptr, nullptr, napi_default, nullptr},
1305         {"OHAudioDecoderSetParameterAnormal", nullptr, OHAudioDecoderSetParameterAnormal, nullptr, nullptr, nullptr,
1306          napi_default, nullptr},
1307         {"OHAudioDecoderSetParameterBnormal", nullptr, OHAudioDecoderSetParameterBnormal, nullptr, nullptr, nullptr,
1308          napi_default, nullptr},
1309         {"OHAudioDecoderSetParameterCnormal", nullptr, OHAudioDecoderSetParameterCnormal, nullptr, nullptr, nullptr,
1310          napi_default, nullptr},
1311         {"OHAudioDecoderIsValidAnormal", nullptr, OHAudioDecoderIsValidAnormal, nullptr, nullptr, nullptr, napi_default,
1312          nullptr},
1313         {"OHAudioDecoderIsValidBnormal", nullptr, OHAudioDecoderIsValidBnormal, nullptr, nullptr, nullptr, napi_default,
1314          nullptr},
1315         {"OHAudioDecoderIsValidCnormal", nullptr, OHAudioDecoderIsValidCnormal, nullptr, nullptr, nullptr, napi_default,
1316          nullptr},
1317         {"OHAudioDecoderPushInputBuffer", nullptr, OHAudioDecoderPushInputBuffer, nullptr, nullptr, nullptr, napi_default,
1318          nullptr},
1319         {"OHAudioDecoderFreeOutputBuffer", nullptr, OHAudioDecoderFreeOutputBuffer, nullptr, nullptr, nullptr, napi_default,
1320          nullptr},
1321         {"OHAudioDecoderAmrnb", nullptr, OHAudioDecoderAmrnb, nullptr, nullptr, nullptr, napi_default,
1322          nullptr},
1323         {"OHAudioDecoderAmrwb", nullptr, OHAudioDecoderAmrwb, nullptr, nullptr, nullptr, napi_default,
1324          nullptr},
1325         {"OHAudioDecoderOpus", nullptr, OHAudioDecoderOpus, nullptr, nullptr, nullptr, napi_default,
1326          nullptr},
1327         {"OHAudioDecoderG711Mu", nullptr, OHAudioDecoderG711Mu, nullptr, nullptr, nullptr, napi_default,
1328          nullptr},
1329         {"OHAudioDecoderViVid", nullptr, OHAudioDecoderViVid, nullptr, nullptr, nullptr, napi_default,
1330          nullptr},
1331         {"OHAudioDecoderOutputFormatChange", nullptr, OHAudioDecoderOutputFormatChange, nullptr, nullptr, nullptr,
1332          napi_default, nullptr},
1333     };
1334     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1335     return exports;
1336 }
1337 EXTERN_C_END
1338 
1339 static napi_module demoModule = {
1340     .nm_version = 1,
1341     .nm_flags = 0,
1342     .nm_filename = nullptr,
1343     .nm_register_func = Init,
1344     .nm_modname = "libaudioDecoderAvBuffer",
1345     .nm_priv = ((void *)0),
1346     .reserved = { 0 },
1347 };
1348 
RegisterModule(void)1349 extern "C" __attribute__((constructor)) void RegisterModule(void)
1350 {
1351     napi_module_register(&demoModule);
1352 }
1353