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 <queue>
27#include <fstream>
28#include <iostream>
29
30#define FAIL (-1)
31#define SUCCESS 0
32
33constexpr uint32_t DEFAULT_SAMPLERATE = 44100;
34constexpr uint32_t DEFAULT_BITRATE = 32000;
35constexpr uint32_t DEFAULT_CHANNEL_COUNT = 2;
36constexpr uint32_t DEFAULT_MAX_INPUT_SIZE = 1152;
37constexpr uint32_t DEFAULT_AAC_TYPE = 1;
38constexpr uint8_t DEFAULT_VORBIS_TYPE = 10;
39constexpr uint8_t DEFAULT_VORBISTWO_TYPE = 20;
40using namespace std;
41
42static 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
54static 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
66static 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
71static 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
78static 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
84class ADecSignal {
85public:
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};
97ADecSignal *signal_;
98static void OnError(OH_AVCodec *codec, int32_t errorCode, void *userData)
99{
100    (void)codec;
101    (void)errorCode;
102    (void)userData;
103}
104static void OnStreamChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData)
105{
106    (void)codec;
107    (void)format;
108    (void)userData;
109}
110static 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}
122static 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
132static 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
143static 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
156static 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
170static 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
193static 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
218static 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
244static 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
270static 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
297static 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
326static 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
356static 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
363static 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
370static 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
377static 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
383static 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
389static 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
395static 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
418static 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
441static 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
461static 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
486static 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
511static 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
533static 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
559static 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
585static 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
608static 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
634static 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
660static 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
683static 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
710static 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
737static 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
761static 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
790static 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
819static 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
845static 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
875static 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
905static 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
932static 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
965static 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
998static 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
1012static 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
1026static 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
1037static 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
1051static 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
1085EXTERN_C_START
1086static napi_value Init(napi_env env, napi_value exports)
1087{
1088    napi_property_descriptor desc[] = {
1089        {"OH_AudioCodec_CreateByMime", nullptr, AudioDecoderCreateByMimeAudioAac, nullptr, nullptr, nullptr,
1090         napi_default, nullptr},
1091        {"OH_AudioCodec_CreateByName", nullptr, AudioDecoderCreateByName, nullptr, nullptr, nullptr, napi_default,
1092         nullptr},
1093        {"OH_AudioCodec_Destroy", nullptr, AudioDecoderDestroy, nullptr, nullptr, nullptr, napi_default, nullptr},
1094        {"OH_AudioCodec_RegisterCallback", nullptr, AudioDecoderRegisterCallback, nullptr, nullptr, nullptr, napi_default,
1095         nullptr},
1096        {"OH_AudioCodec_Configure", nullptr, AudioDecoderConfigure, nullptr, nullptr, nullptr, napi_default, nullptr},
1097        {"OH_AudioCodec_Prepare", nullptr, AudioDecoderPrepare, nullptr, nullptr, nullptr, napi_default, nullptr},
1098        {"OH_AudioCodec_Start", nullptr, AudioDecoderStart, nullptr, nullptr, nullptr, napi_default, nullptr},
1099        {"OH_AudioCodec_Stop", nullptr, AudioDecoderStop, nullptr, nullptr, nullptr, napi_default, nullptr},
1100        {"OH_AudioCodec_Flush", nullptr, AudioDecoderFlush, nullptr, nullptr, nullptr, napi_default, nullptr},
1101        {"OH_AudioCodec_Reset", nullptr, AudioDecoderReset, nullptr, nullptr, nullptr, napi_default, nullptr},
1102        {"OH_AudioCodec_GetOutputDescription", nullptr, AudioDecoderGetOutputDescription, nullptr, nullptr, nullptr,
1103         napi_default, nullptr},
1104        {"OH_AudioCodec_SetParameter", nullptr, AudioDecoderSetParameter, nullptr, nullptr, nullptr, napi_default,
1105         nullptr},
1106        {"OH_AudioCodec_IsValid", nullptr, AudioDecoderIsValid, nullptr, nullptr, nullptr, napi_default, nullptr},
1107        {"OHAudioDecoderCreateByNameAnormal", nullptr, OHAudioDecoderCreateByNameAnormal, nullptr, nullptr, nullptr,
1108         napi_default, nullptr},
1109        {"OHAudioDecoderCreateByNameBnormal", nullptr, OHAudioDecoderCreateByNameBnormal, nullptr, nullptr, nullptr,
1110         napi_default, nullptr},
1111        {"OHAudioDecoderCreateByNameCnormal", nullptr, OHAudioDecoderCreateByNameCnormal, nullptr, nullptr, nullptr,
1112         napi_default, nullptr},
1113        {"OHAudioDecoderDestroyAnormal", nullptr, OHAudioDecoderDestroyAnormal, nullptr, nullptr, nullptr, napi_default,
1114         nullptr},
1115        {"OHAudioDecoderDestroyBnormal", nullptr, OHAudioDecoderDestroyBnormal, nullptr, nullptr, nullptr, napi_default,
1116         nullptr},
1117        {"OHAudioDecoderDestroyCnormal", nullptr, OHAudioDecoderDestroyCnormal, nullptr, nullptr, nullptr, napi_default,
1118         nullptr},
1119        {"OHAudioDecoderStartAnormal", nullptr, OHAudioDecoderStartAnormal, nullptr, nullptr, nullptr, napi_default,
1120         nullptr},
1121        {"OHAudioDecoderStartBnormal", nullptr, OHAudioDecoderStartBnormal, nullptr, nullptr, nullptr, napi_default,
1122         nullptr},
1123        {"OHAudioDecoderStartCnormal", nullptr, OHAudioDecoderStartCnormal, nullptr, nullptr, nullptr, napi_default,
1124         nullptr},
1125        {"OHAudioDecoderStopAnormal", nullptr, OHAudioDecoderStopAnormal, nullptr, nullptr, nullptr, napi_default,
1126         nullptr},
1127        {"OHAudioDecoderStopBnormal", nullptr, OHAudioDecoderStopBnormal, nullptr, nullptr, nullptr, napi_default,
1128         nullptr},
1129        {"OHAudioDecoderStopCnormal", nullptr, OHAudioDecoderStopCnormal, nullptr, nullptr, nullptr, napi_default,
1130         nullptr},
1131        {"OHAudioDecoderFlushAnormal", nullptr, OHAudioDecoderFlushAnormal, nullptr, nullptr, nullptr, napi_default,
1132         nullptr},
1133        {"OHAudioDecoderFlushBnormal", nullptr, OHAudioDecoderFlushBnormal, nullptr, nullptr, nullptr, napi_default,
1134         nullptr},
1135        {"OHAudioDecoderFlushCnormal", nullptr, OHAudioDecoderFlushCnormal, nullptr, nullptr, nullptr, napi_default,
1136         nullptr},
1137        {"OHAudioDecoderResetAnormal", nullptr, OHAudioDecoderResetAnormal, nullptr, nullptr, nullptr, napi_default,
1138         nullptr},
1139        {"OHAudioDecoderResetBnormal", nullptr, OHAudioDecoderResetBnormal, nullptr, nullptr, nullptr, napi_default,
1140         nullptr},
1141        {"OHAudioDecoderResetCnormal", nullptr, OHAudioDecoderResetCnormal, nullptr, nullptr, nullptr, napi_default,
1142         nullptr},
1143        {"OHAudioDecoderGetOutputDescriptionAnormal", nullptr, OHAudioDecoderGetOutputDescriptionAnormal, nullptr,
1144         nullptr, nullptr, napi_default, nullptr},
1145        {"OHAudioDecoderGetOutputDescriptionBnormal", nullptr, OHAudioDecoderGetOutputDescriptionBnormal, nullptr,
1146         nullptr, nullptr, napi_default, nullptr},
1147        {"OHAudioDecoderGetOutputDescriptionCnormal", nullptr, OHAudioDecoderGetOutputDescriptionCnormal, nullptr,
1148         nullptr, nullptr, napi_default, nullptr},
1149        {"OHAudioDecoderSetParameterAnormal", nullptr, OHAudioDecoderSetParameterAnormal, nullptr, nullptr, nullptr,
1150         napi_default, nullptr},
1151        {"OHAudioDecoderSetParameterBnormal", nullptr, OHAudioDecoderSetParameterBnormal, nullptr, nullptr, nullptr,
1152         napi_default, nullptr},
1153        {"OHAudioDecoderSetParameterCnormal", nullptr, OHAudioDecoderSetParameterCnormal, nullptr, nullptr, nullptr,
1154         napi_default, nullptr},
1155        {"OHAudioDecoderIsValidAnormal", nullptr, OHAudioDecoderIsValidAnormal, nullptr, nullptr, nullptr, napi_default,
1156         nullptr},
1157        {"OHAudioDecoderIsValidBnormal", nullptr, OHAudioDecoderIsValidBnormal, nullptr, nullptr, nullptr, napi_default,
1158         nullptr},
1159        {"OHAudioDecoderIsValidCnormal", nullptr, OHAudioDecoderIsValidCnormal, nullptr, nullptr, nullptr, napi_default,
1160         nullptr},
1161        {"OHAudioDecoderPushInputBuffer", nullptr, OHAudioDecoderPushInputBuffer, nullptr, nullptr, nullptr, napi_default,
1162         nullptr},
1163        {"OHAudioDecoderFreeOutputBuffer", nullptr, OHAudioDecoderFreeOutputBuffer, nullptr, nullptr, nullptr, napi_default,
1164         nullptr},
1165        {"OHAudioDecoderAmrnb", nullptr, OHAudioDecoderAmrnb, nullptr, nullptr, nullptr, napi_default,
1166         nullptr},
1167        {"OHAudioDecoderAmrwb", nullptr, OHAudioDecoderAmrwb, nullptr, nullptr, nullptr, napi_default,
1168         nullptr},
1169        {"OHAudioDecoderOpus", nullptr, OHAudioDecoderOpus, nullptr, nullptr, nullptr, napi_default,
1170         nullptr},
1171        {"OHAudioDecoderG711Mu", nullptr, OHAudioDecoderG711Mu, nullptr, nullptr, nullptr, napi_default,
1172         nullptr},
1173        {"OHAudioDecoderViVid", nullptr, OHAudioDecoderViVid, nullptr, nullptr, nullptr, napi_default,
1174         nullptr},
1175    };
1176    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1177    return exports;
1178}
1179EXTERN_C_END
1180
1181static napi_module demoModule = {
1182    .nm_version = 1,
1183    .nm_flags = 0,
1184    .nm_filename = nullptr,
1185    .nm_register_func = Init,
1186    .nm_modname = "libaudioDecoderAvBuffer",
1187    .nm_priv = ((void *)0),
1188    .reserved = { 0 },
1189};
1190
1191extern "C" __attribute__((constructor)) void RegisterModule(void)
1192{
1193    napi_module_register(&demoModule);
1194}
1195