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_audiodecoder.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 <pthread.h>
25#include <queue>
26#include <fstream>
27#include <iostream>
28
29#define FAIL (-1)
30#define SUCCESS 0
31
32constexpr uint32_t DEFAULT_SAMPLERATE = 44100;
33constexpr uint32_t DEFAULT_BITRATE = 32000;
34constexpr uint32_t DEFAULT_CHANNEL_COUNT = 2;
35constexpr uint32_t DEFAULT_MAX_INPUT_SIZE = 1152;
36constexpr uint32_t DEFAULT_AAC_TYPE = 1;
37constexpr uint8_t DEFAULT_VORBIS_TYPE = 10;
38constexpr uint8_t DEFAULT_VORBISTWO_TYPE = 20;
39using namespace std;
40
41static napi_value TestInitPtr(napi_env env, OH_AVCodec *param)
42{
43    int backParam = FAIL;
44    napi_value result = nullptr;
45    OH_AVCodec *checkParam = param;
46    if (checkParam != nullptr) {
47        backParam = SUCCESS;
48    }
49    napi_create_int32(env, backParam, &result);
50    return result;
51}
52
53static napi_value TestInitAVErrCode(napi_env env, OH_AVErrCode param)
54{
55    int backParam = FAIL;
56    napi_value result = nullptr;
57    OH_AVErrCode checkParam = param;
58    if (checkParam == AV_ERR_OK) {
59        backParam = SUCCESS;
60    }
61    napi_create_int32(env, backParam, &result);
62    return result;
63}
64
65static napi_value AudioDecoderCreateByMimeAudioAac(napi_env env, napi_callback_info info)
66{
67    return TestInitPtr(env, OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC));
68}
69
70static napi_value AudioDecoderCreateByName(napi_env env, napi_callback_info info)
71{
72    OH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_AUDIO_VORBIS, false);
73    const char *name = OH_AVCapability_GetName(capability);
74    return TestInitPtr(env, OH_AudioDecoder_CreateByName(name));
75}
76
77static napi_value AudioDecoderDestroy(napi_env env, napi_callback_info info)
78{
79    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_FLAC);
80    return TestInitAVErrCode(env, OH_AudioDecoder_Destroy(audioDec));
81}
82
83class ADecSignal {
84public:
85    pthread_mutex_t inMutex_;
86    pthread_mutex_t outMutex_;
87    pthread_mutex_t startMutex_;
88    std::condition_variable inCond_;
89    std::condition_variable outCond_;
90    std::condition_variable startCond_;
91    std::queue<uint32_t> inQueue_;
92    std::queue<uint32_t> outQueue_;
93    std::queue<OH_AVMemory *> inBufferQueue_;
94    std::queue<OH_AVMemory *> outBufferQueue_;
95    std::queue<OH_AVCodecBufferAttr> attrQueue_;
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 onNeedInputData(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, void *userData)
111{
112    (void)codec;
113    ADecSignal *signal = static_cast<ADecSignal *>(userData);
114    pthread_mutex_lock(&signal->inMutex_);
115    signal->inQueue_.push(index);
116    signal->inBufferQueue_.push(data);
117    signal->inCond_.notify_all();
118}
119static void onNeedOutputData(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, OH_AVCodecBufferAttr *attr,
120    void *userData)
121{
122    (void)codec;
123    ADecSignal *signal = static_cast<ADecSignal *>(userData);
124    pthread_mutex_unlock(&signal->outMutex_);
125    signal->outQueue_.push(index);
126    signal->outBufferQueue_.push(data);
127    if (attr) {
128        signal->attrQueue_.push(*attr);
129    }
130    signal->outCond_.notify_all();
131}
132
133static napi_value AudioDecoderSetCallback(napi_env env, napi_callback_info info)
134{
135    napi_value result;
136    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC);
137    signal_ = new ADecSignal();
138    OH_AVCodecAsyncCallback callback = { &OnError, &OnStreamChanged, &onNeedInputData, &onNeedOutputData };
139    result = TestInitAVErrCode(env, OH_AudioDecoder_SetCallback(audioDec, callback, signal_));
140    OH_AudioDecoder_Destroy(audioDec);
141    return result;
142}
143
144static napi_value AudioDecoderConfigure(napi_env env, napi_callback_info info)
145{
146    OH_AVFormat *format = nullptr;
147    format = OH_AVFormat_Create();
148    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
149    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
150    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
151    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
152    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
153    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
154    return TestInitAVErrCode(env, OH_AudioDecoder_Configure(audioDec, format));
155}
156
157static napi_value AudioDecoderPrepare(napi_env env, napi_callback_info info)
158{
159    OH_AVFormat *format = nullptr;
160    format = OH_AVFormat_Create();
161    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
162    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
163    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
164    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
165    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
166    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
167    OH_AudioDecoder_Configure(audioDec, format);
168    return TestInitAVErrCode(env, OH_AudioDecoder_Prepare(audioDec));
169}
170
171static napi_value AudioDecoderStart(napi_env env, napi_callback_info info)
172{
173    int backParam = FAIL;
174    napi_value result = nullptr;
175    OH_AVErrCode checkParam;
176    OH_AVFormat *format = nullptr;
177    format = OH_AVFormat_Create();
178    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
179    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
180    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
181    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
182    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
183    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
184    checkParam = OH_AudioDecoder_Configure(audioDec, format);
185    checkParam = OH_AudioDecoder_Prepare(audioDec);
186    checkParam = OH_AudioDecoder_Start(audioDec);
187    if (checkParam == AV_ERR_OK) {
188        backParam = SUCCESS;
189    }
190    napi_create_int32(env, backParam, &result);
191    return result;
192}
193
194static napi_value AudioDecoderStop(napi_env env, napi_callback_info info)
195{
196    int backParam = FAIL;
197    napi_value result = nullptr;
198    OH_AVErrCode checkParam;
199    OH_AVFormat *format = nullptr;
200    format = OH_AVFormat_Create();
201    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
202    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
203    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
204    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
205    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
206    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
207    checkParam = OH_AudioDecoder_Configure(audioDec, format);
208    checkParam = OH_AudioDecoder_Prepare(audioDec);
209    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
210        checkParam = OH_AudioDecoder_Stop(audioDec);
211        if (checkParam == AV_ERR_OK) {
212            backParam = SUCCESS;
213        }
214    }
215    napi_create_int32(env, backParam, &result);
216    return result;
217}
218
219static napi_value AudioDecoderFlush(napi_env env, napi_callback_info info)
220{
221    int backParam = FAIL;
222    napi_value result = nullptr;
223    OH_AVErrCode checkParam;
224    OH_AVFormat *format = nullptr;
225    format = OH_AVFormat_Create();
226    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
227    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
228    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
229    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
230    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
231    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
232    OH_AudioDecoder_Configure(audioDec, format);
233    OH_AudioDecoder_Prepare(audioDec);
234    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
235        checkParam = OH_AudioDecoder_Flush(audioDec);
236        if (checkParam == AV_ERR_OK) {
237            backParam = SUCCESS;
238            OH_AudioDecoder_Stop(audioDec);
239        }
240    }
241    napi_create_int32(env, backParam, &result);
242    return result;
243}
244
245static napi_value AudioDecoderReset(napi_env env, napi_callback_info info)
246{
247    int backParam = FAIL;
248    napi_value result = nullptr;
249    OH_AVErrCode checkParam;
250    OH_AVFormat *format = nullptr;
251    format = OH_AVFormat_Create();
252    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
253    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
254    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
255    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
256    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
257    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
258    OH_AudioDecoder_Configure(audioDec, format);
259    OH_AudioDecoder_Prepare(audioDec);
260    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
261        checkParam = OH_AudioDecoder_Reset(audioDec);
262        if (checkParam == AV_ERR_OK) {
263            backParam = SUCCESS;
264            OH_AudioDecoder_Stop(audioDec);
265        }
266    }
267    napi_create_int32(env, backParam, &result);
268    return result;
269}
270
271static napi_value AudioDecoderGetOutputDescription(napi_env env, napi_callback_info info)
272{
273    int backParam = FAIL;
274    napi_value result = nullptr;
275    OH_AVFormat *checkParam = nullptr;
276    OH_AVFormat *format = nullptr;
277    format = OH_AVFormat_Create();
278    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
279    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
280    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
281    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
282    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
283    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
284    OH_AudioDecoder_Configure(audioDec, format);
285    OH_AudioDecoder_Prepare(audioDec);
286    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
287        checkParam = OH_AudioDecoder_GetOutputDescription(audioDec);
288        if (checkParam != nullptr) {
289            backParam = SUCCESS;
290            OH_AudioDecoder_Stop(audioDec);
291            free(checkParam);
292        }
293    }
294    napi_create_int32(env, backParam, &result);
295    return result;
296}
297
298static napi_value AudioDecoderSetParameter(napi_env env, napi_callback_info info)
299{
300    int backParam = FAIL;
301    napi_value result = nullptr;
302    OH_AVErrCode checkParam;
303    OH_AVFormat *format = nullptr;
304    format = OH_AVFormat_Create();
305    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
306    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
307    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
308    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
309    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
310    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
311    checkParam = OH_AudioDecoder_Configure(audioDec, format);
312    checkParam = OH_AudioDecoder_Prepare(audioDec);
313    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
314        format = OH_AudioDecoder_GetOutputDescription(audioDec);
315        if (format != nullptr) {
316            checkParam = OH_AudioDecoder_SetParameter(audioDec, format);
317            if (checkParam == AV_ERR_OK) {
318                backParam = SUCCESS;
319                free(format);
320            }
321        }
322    }
323    napi_create_int32(env, backParam, &result);
324    return result;
325}
326
327static napi_value AudioDecoderIsValid(napi_env env, napi_callback_info info)
328{
329    int backParam = FAIL;
330    napi_value result = nullptr;
331    OH_AVErrCode checkParam;
332    bool status = true;
333    OH_AVFormat *format = nullptr;
334    format = OH_AVFormat_Create();
335    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
336    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
337    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
338    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
339    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
340    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
341    checkParam = OH_AudioDecoder_Configure(audioDec, format);
342    checkParam = OH_AudioDecoder_Prepare(audioDec);
343    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
344        checkParam = OH_AudioDecoder_Flush(audioDec);
345        if (checkParam == AV_ERR_OK) {
346            checkParam = OH_AudioDecoder_IsValid(audioDec, &status);
347            if (checkParam == AV_ERR_OK) {
348                backParam = SUCCESS;
349                OH_AudioDecoder_Stop(audioDec);
350            }
351        }
352    }
353    napi_create_int32(env, backParam, &result);
354    return result;
355}
356
357static napi_value OHAudioDecoderCreateByNameAnormal(napi_env env, napi_callback_info info)
358{
359    OH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_AUDIO_AAC, false);
360    const char *name = OH_AVCapability_GetName(capability);
361    return TestInitPtr(env, OH_AudioDecoder_CreateByName(name));
362}
363
364static napi_value OHAudioDecoderCreateByNameBnormal(napi_env env, napi_callback_info info)
365{
366    OH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, false);
367    const char *name = OH_AVCapability_GetName(capability);
368    return TestInitPtr(env, OH_AudioDecoder_CreateByName(name));
369}
370
371static napi_value OHAudioDecoderCreateByNameCnormal(napi_env env, napi_callback_info info)
372{
373    OH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_AUDIO_FLAC, false);
374    const char *name = OH_AVCapability_GetName(capability);
375    return TestInitPtr(env, OH_AudioDecoder_CreateByName(name));
376}
377
378static napi_value OHAudioDecoderDestroyAnormal(napi_env env, napi_callback_info info)
379{
380    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC);
381    return TestInitAVErrCode(env, OH_AudioDecoder_Destroy(audioDec));
382}
383
384static napi_value OHAudioDecoderDestroyBnormal(napi_env env, napi_callback_info info)
385{
386    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
387    return TestInitAVErrCode(env, OH_AudioDecoder_Destroy(audioDec));
388}
389
390static napi_value OHAudioDecoderDestroyCnormal(napi_env env, napi_callback_info info)
391{
392    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_VORBIS);
393    return TestInitAVErrCode(env, OH_AudioDecoder_Destroy(audioDec));
394}
395
396static napi_value OHAudioDecoderStartAnormal(napi_env env, napi_callback_info info)
397{
398    int backParam = FAIL;
399    napi_value result = nullptr;
400    OH_AVErrCode checkParam;
401    OH_AVFormat *format = nullptr;
402    format = OH_AVFormat_Create();
403    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
404    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
405    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
406    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
407    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
408    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC);
409    OH_AudioDecoder_Configure(audioDec, format);
410    OH_AudioDecoder_Prepare(audioDec);
411    checkParam = OH_AudioDecoder_Start(audioDec);
412    if (checkParam == AV_ERR_OK) {
413        backParam = SUCCESS;
414    }
415    napi_create_int32(env, backParam, &result);
416    return result;
417}
418
419static napi_value OHAudioDecoderStartBnormal(napi_env env, napi_callback_info info)
420{
421    int backParam = FAIL;
422    napi_value result = nullptr;
423    OH_AVErrCode checkParam;
424    OH_AVFormat *format = nullptr;
425    format = OH_AVFormat_Create();
426    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
427    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
428    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
429    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
430    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
431    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_FLAC);
432    OH_AudioDecoder_Configure(audioDec, format);
433    OH_AudioDecoder_Prepare(audioDec);
434    checkParam = OH_AudioDecoder_Start(audioDec);
435    if (checkParam == AV_ERR_OK) {
436        backParam = SUCCESS;
437    }
438    napi_create_int32(env, backParam, &result);
439    return result;
440}
441
442static napi_value OHAudioDecoderStartCnormal(napi_env env, napi_callback_info info)
443{
444    int backParam = FAIL;
445    napi_value result = nullptr;
446    OH_AVErrCode checkParam;
447    OH_AVFormat *format = nullptr;
448    format = OH_AVFormat_Create();
449    OH_AVFormat_SetIntValue(format, OH_MD_KEY_IDENTIFICATION_HEADER, DEFAULT_VORBISTWO_TYPE);
450    OH_AVFormat_SetIntValue(format, OH_MD_KEY_SETUP_HEADER, DEFAULT_VORBIS_TYPE);
451    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_VORBIS);
452    OH_AudioDecoder_Configure(audioDec, format);
453    OH_AudioDecoder_Prepare(audioDec);
454    checkParam = OH_AudioDecoder_Start(audioDec);
455    if (checkParam == AV_ERR_OK) {
456        backParam = SUCCESS;
457    }
458    napi_create_int32(env, backParam, &result);
459    return result;
460}
461
462static napi_value OHAudioDecoderStopAnormal(napi_env env, napi_callback_info info)
463{
464    int backParam = FAIL;
465    napi_value result = nullptr;
466    OH_AVErrCode checkParam;
467    OH_AVFormat *format = nullptr;
468    format = OH_AVFormat_Create();
469    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
470    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
471    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
472    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
473    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
474    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC);
475    OH_AudioDecoder_Configure(audioDec, format);
476    OH_AudioDecoder_Prepare(audioDec);
477    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
478        checkParam = OH_AudioDecoder_Stop(audioDec);
479        if (checkParam == AV_ERR_OK) {
480            backParam = SUCCESS;
481        }
482    }
483    napi_create_int32(env, backParam, &result);
484    return result;
485}
486
487static napi_value OHAudioDecoderStopBnormal(napi_env env, napi_callback_info info)
488{
489    int backParam = FAIL;
490    napi_value result = nullptr;
491    OH_AVErrCode checkParam;
492    OH_AVFormat *format = nullptr;
493    format = OH_AVFormat_Create();
494    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
495    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
496    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
497    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
498    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
499    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_FLAC);
500    OH_AudioDecoder_Configure(audioDec, format);
501    OH_AudioDecoder_Prepare(audioDec);
502    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
503        checkParam = OH_AudioDecoder_Stop(audioDec);
504        if (checkParam == AV_ERR_OK) {
505            backParam = SUCCESS;
506        }
507    }
508    napi_create_int32(env, backParam, &result);
509    return result;
510}
511
512static napi_value OHAudioDecoderStopCnormal(napi_env env, napi_callback_info info)
513{
514    int backParam = FAIL;
515    napi_value result = nullptr;
516    OH_AVErrCode checkParam;
517    OH_AVFormat *format = nullptr;
518    format = OH_AVFormat_Create();
519    OH_AVFormat_SetIntValue(format, OH_MD_KEY_IDENTIFICATION_HEADER, DEFAULT_VORBISTWO_TYPE);
520    OH_AVFormat_SetIntValue(format, OH_MD_KEY_SETUP_HEADER, DEFAULT_VORBIS_TYPE);
521    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_VORBIS);
522    OH_AudioDecoder_Configure(audioDec, format);
523    OH_AudioDecoder_Prepare(audioDec);
524    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
525        checkParam = OH_AudioDecoder_Stop(audioDec);
526        if (checkParam == AV_ERR_OK) {
527            backParam = SUCCESS;
528        }
529    }
530    napi_create_int32(env, backParam, &result);
531    return result;
532}
533
534static napi_value OHAudioDecoderFlushAnormal(napi_env env, napi_callback_info info)
535{
536    int backParam = FAIL;
537    napi_value result = nullptr;
538    OH_AVErrCode checkParam;
539    OH_AVFormat *format = nullptr;
540    format = OH_AVFormat_Create();
541    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
542    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
543    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
544    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
545    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
546    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC);
547    OH_AudioDecoder_Configure(audioDec, format);
548    OH_AudioDecoder_Prepare(audioDec);
549    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
550        checkParam = OH_AudioDecoder_Flush(audioDec);
551        if (checkParam == AV_ERR_OK) {
552            backParam = SUCCESS;
553            OH_AudioDecoder_Stop(audioDec);
554        }
555    }
556    napi_create_int32(env, backParam, &result);
557    return result;
558}
559
560static napi_value OHAudioDecoderFlushBnormal(napi_env env, napi_callback_info info)
561{
562    int backParam = FAIL;
563    napi_value result = nullptr;
564    OH_AVErrCode checkParam;
565    OH_AVFormat *format = nullptr;
566    format = OH_AVFormat_Create();
567    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
568    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
569    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
570    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
571    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
572    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_FLAC);
573    OH_AudioDecoder_Configure(audioDec, format);
574    OH_AudioDecoder_Prepare(audioDec);
575    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
576        checkParam = OH_AudioDecoder_Flush(audioDec);
577        if (checkParam == AV_ERR_OK) {
578            backParam = SUCCESS;
579            OH_AudioDecoder_Stop(audioDec);
580        }
581    }
582    napi_create_int32(env, backParam, &result);
583    return result;
584}
585
586static napi_value OHAudioDecoderFlushCnormal(napi_env env, napi_callback_info info)
587{
588    int backParam = FAIL;
589    napi_value result = nullptr;
590    OH_AVErrCode checkParam;
591    OH_AVFormat *format = nullptr;
592    format = OH_AVFormat_Create();
593    OH_AVFormat_SetIntValue(format, OH_MD_KEY_IDENTIFICATION_HEADER, DEFAULT_VORBISTWO_TYPE);
594    OH_AVFormat_SetIntValue(format, OH_MD_KEY_SETUP_HEADER, DEFAULT_VORBIS_TYPE);
595    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_VORBIS);
596    OH_AudioDecoder_Configure(audioDec, format);
597    OH_AudioDecoder_Prepare(audioDec);
598    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
599        checkParam = OH_AudioDecoder_Flush(audioDec);
600        if (checkParam == AV_ERR_OK) {
601            backParam = SUCCESS;
602            OH_AudioDecoder_Stop(audioDec);
603        }
604    }
605    napi_create_int32(env, backParam, &result);
606    return result;
607}
608
609static napi_value OHAudioDecoderResetAnormal(napi_env env, napi_callback_info info)
610{
611    int backParam = FAIL;
612    napi_value result = nullptr;
613    OH_AVErrCode checkParam;
614    OH_AVFormat *format = nullptr;
615    format = OH_AVFormat_Create();
616    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
617    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
618    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
619    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
620    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
621    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
622    OH_AudioDecoder_Configure(audioDec, format);
623    OH_AudioDecoder_Prepare(audioDec);
624    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
625        checkParam = OH_AudioDecoder_Reset(audioDec);
626        if (checkParam == AV_ERR_OK) {
627            backParam = SUCCESS;
628            OH_AudioDecoder_Stop(audioDec);
629        }
630    }
631    napi_create_int32(env, backParam, &result);
632    return result;
633}
634
635static napi_value OHAudioDecoderResetBnormal(napi_env env, napi_callback_info info)
636{
637    int backParam = FAIL;
638    napi_value result = nullptr;
639    OH_AVErrCode checkParam;
640    OH_AVFormat *format = nullptr;
641    format = OH_AVFormat_Create();
642    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
643    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
644    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
645    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
646    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
647    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_FLAC);
648    OH_AudioDecoder_Configure(audioDec, format);
649    OH_AudioDecoder_Prepare(audioDec);
650    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
651        checkParam = OH_AudioDecoder_Reset(audioDec);
652        if (checkParam == AV_ERR_OK) {
653            backParam = SUCCESS;
654            OH_AudioDecoder_Stop(audioDec);
655        }
656    }
657    napi_create_int32(env, backParam, &result);
658    return result;
659}
660
661static napi_value OHAudioDecoderResetCnormal(napi_env env, napi_callback_info info)
662{
663    int backParam = FAIL;
664    napi_value result = nullptr;
665    OH_AVErrCode checkParam;
666    OH_AVFormat *format = nullptr;
667    format = OH_AVFormat_Create();
668    OH_AVFormat_SetIntValue(format, OH_MD_KEY_IDENTIFICATION_HEADER, DEFAULT_VORBISTWO_TYPE);
669    OH_AVFormat_SetIntValue(format, OH_MD_KEY_SETUP_HEADER, DEFAULT_VORBIS_TYPE);
670    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_VORBIS);
671    OH_AudioDecoder_Configure(audioDec, format);
672    OH_AudioDecoder_Prepare(audioDec);
673    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
674        checkParam = OH_AudioDecoder_Reset(audioDec);
675        if (checkParam == AV_ERR_OK) {
676            backParam = SUCCESS;
677            OH_AudioDecoder_Stop(audioDec);
678        }
679    }
680    napi_create_int32(env, backParam, &result);
681    return result;
682}
683
684static napi_value OHAudioDecoderGetOutputDescriptionAnormal(napi_env env, napi_callback_info info)
685{
686    int backParam = FAIL;
687    napi_value result = nullptr;
688    OH_AVFormat *checkParam = nullptr;
689    OH_AVFormat *format = nullptr;
690    format = OH_AVFormat_Create();
691    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
692    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
693    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
694    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
695    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
696    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC);
697    OH_AudioDecoder_Configure(audioDec, format);
698    OH_AudioDecoder_Prepare(audioDec);
699    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
700        checkParam = OH_AudioDecoder_GetOutputDescription(audioDec);
701        if (checkParam != nullptr) {
702            backParam = SUCCESS;
703            OH_AudioDecoder_Stop(audioDec);
704            free(checkParam);
705        }
706    }
707    napi_create_int32(env, backParam, &result);
708    return result;
709}
710
711static napi_value OHAudioDecoderGetOutputDescriptionBnormal(napi_env env, napi_callback_info info)
712{
713    int backParam = FAIL;
714    napi_value result = nullptr;
715    OH_AVFormat *checkParam = nullptr;
716    OH_AVFormat *format = nullptr;
717    format = OH_AVFormat_Create();
718    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
719    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
720    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
721    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
722    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
723    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_FLAC);
724    OH_AudioDecoder_Configure(audioDec, format);
725    OH_AudioDecoder_Prepare(audioDec);
726    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
727        checkParam = OH_AudioDecoder_GetOutputDescription(audioDec);
728        if (checkParam != nullptr) {
729            backParam = SUCCESS;
730            OH_AudioDecoder_Stop(audioDec);
731            free(checkParam);
732        }
733    }
734    napi_create_int32(env, backParam, &result);
735    return result;
736}
737
738static napi_value OHAudioDecoderGetOutputDescriptionCnormal(napi_env env, napi_callback_info info)
739{
740    int backParam = FAIL;
741    napi_value result = nullptr;
742    OH_AVFormat *checkParam = nullptr;
743    OH_AVFormat *format = nullptr;
744    format = OH_AVFormat_Create();
745    OH_AVFormat_SetIntValue(format, OH_MD_KEY_IDENTIFICATION_HEADER, DEFAULT_VORBISTWO_TYPE);
746    OH_AVFormat_SetIntValue(format, OH_MD_KEY_SETUP_HEADER, DEFAULT_VORBIS_TYPE);
747    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_VORBIS);
748    OH_AudioDecoder_Configure(audioDec, format);
749    OH_AudioDecoder_Prepare(audioDec);
750    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
751        checkParam = OH_AudioDecoder_GetOutputDescription(audioDec);
752        if (checkParam != nullptr) {
753            backParam = SUCCESS;
754            OH_AudioDecoder_Stop(audioDec);
755            free(checkParam);
756        }
757    }
758    napi_create_int32(env, backParam, &result);
759    return result;
760}
761
762static napi_value OHAudioDecoderSetParameterAnormal(napi_env env, napi_callback_info info)
763{
764    int backParam = FAIL;
765    napi_value result = nullptr;
766    OH_AVErrCode checkParam;
767    OH_AVFormat *format = nullptr;
768    format = OH_AVFormat_Create();
769    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
770    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
771    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
772    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
773    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
774    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC);
775    OH_AudioDecoder_Configure(audioDec, format);
776    OH_AudioDecoder_Prepare(audioDec);
777    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
778        format = OH_AudioDecoder_GetOutputDescription(audioDec);
779        if (format != nullptr) {
780            checkParam = OH_AudioDecoder_SetParameter(audioDec, format);
781            if (checkParam == AV_ERR_OK) {
782                backParam = SUCCESS;
783                free(format);
784            }
785        }
786    }
787    napi_create_int32(env, backParam, &result);
788    return result;
789}
790
791static napi_value OHAudioDecoderSetParameterBnormal(napi_env env, napi_callback_info info)
792{
793    int backParam = FAIL;
794    napi_value result = nullptr;
795    OH_AVErrCode checkParam;
796    OH_AVFormat *format = nullptr;
797    format = OH_AVFormat_Create();
798    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
799    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
800    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
801    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
802    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
803    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_FLAC);
804    OH_AudioDecoder_Configure(audioDec, format);
805    OH_AudioDecoder_Prepare(audioDec);
806    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
807        format = OH_AudioDecoder_GetOutputDescription(audioDec);
808        if (format != nullptr) {
809            checkParam = OH_AudioDecoder_SetParameter(audioDec, format);
810            if (checkParam == AV_ERR_OK) {
811                backParam = SUCCESS;
812                free(format);
813            }
814        }
815    }
816    napi_create_int32(env, backParam, &result);
817    return result;
818}
819
820static napi_value OHAudioDecoderSetParameterCnormal(napi_env env, napi_callback_info info)
821{
822    int backParam = FAIL;
823    napi_value result = nullptr;
824    OH_AVErrCode checkParam;
825    OH_AVFormat *format = nullptr;
826    format = OH_AVFormat_Create();
827    OH_AVFormat_SetIntValue(format, OH_MD_KEY_IDENTIFICATION_HEADER, DEFAULT_VORBISTWO_TYPE);
828    OH_AVFormat_SetIntValue(format, OH_MD_KEY_SETUP_HEADER, DEFAULT_VORBIS_TYPE);
829    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_VORBIS);
830    OH_AudioDecoder_Configure(audioDec, format);
831    OH_AudioDecoder_Prepare(audioDec);
832    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
833        format = OH_AudioDecoder_GetOutputDescription(audioDec);
834        if (format != nullptr) {
835            checkParam = OH_AudioDecoder_SetParameter(audioDec, format);
836            if (checkParam == AV_ERR_OK) {
837                backParam = SUCCESS;
838                free(format);
839            }
840        }
841    }
842    napi_create_int32(env, backParam, &result);
843    return result;
844}
845
846static napi_value OHAudioDecoderIsValidAnormal(napi_env env, napi_callback_info info)
847{
848    int backParam = FAIL;
849    napi_value result = nullptr;
850    OH_AVErrCode checkParam;
851    bool status = true;
852    OH_AVFormat *format = nullptr;
853    format = OH_AVFormat_Create();
854    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
855    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
856    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
857    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
858    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
859    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC);
860    OH_AudioDecoder_Configure(audioDec, format);
861    OH_AudioDecoder_Prepare(audioDec);
862    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
863        checkParam = OH_AudioDecoder_Flush(audioDec);
864        if (checkParam == AV_ERR_OK) {
865            checkParam = OH_AudioDecoder_IsValid(audioDec, &status);
866            if (checkParam == AV_ERR_OK) {
867                backParam = SUCCESS;
868                OH_AudioDecoder_Stop(audioDec);
869            }
870        }
871    }
872    napi_create_int32(env, backParam, &result);
873    return result;
874}
875
876static napi_value OHAudioDecoderIsValidBnormal(napi_env env, napi_callback_info info)
877{
878    int backParam = FAIL;
879    napi_value result = nullptr;
880    OH_AVErrCode checkParam;
881    bool status = true;
882    OH_AVFormat *format = nullptr;
883    format = OH_AVFormat_Create();
884    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
885    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
886    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
887    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
888    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
889    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_FLAC);
890    OH_AudioDecoder_Configure(audioDec, format);
891    OH_AudioDecoder_Prepare(audioDec);
892    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
893        checkParam = OH_AudioDecoder_Flush(audioDec);
894        if (checkParam == AV_ERR_OK) {
895            checkParam = OH_AudioDecoder_IsValid(audioDec, &status);
896            if (checkParam == AV_ERR_OK) {
897                backParam = SUCCESS;
898                OH_AudioDecoder_Stop(audioDec);
899            }
900        }
901    }
902    napi_create_int32(env, backParam, &result);
903    return result;
904}
905
906static napi_value OHAudioDecoderIsValidCnormal(napi_env env, napi_callback_info info)
907{
908    int backParam = FAIL;
909    napi_value result = nullptr;
910    OH_AVErrCode checkParam;
911    bool status = true;
912    OH_AVFormat *format = nullptr;
913    format = OH_AVFormat_Create();
914    OH_AVFormat_SetIntValue(format, OH_MD_KEY_IDENTIFICATION_HEADER, DEFAULT_VORBISTWO_TYPE);
915    OH_AVFormat_SetIntValue(format, OH_MD_KEY_SETUP_HEADER, DEFAULT_VORBIS_TYPE);
916    OH_AVCodec *audioDec = OH_AudioDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_VORBIS);
917    OH_AudioDecoder_Configure(audioDec, format);
918    OH_AudioDecoder_Prepare(audioDec);
919    if (OH_AudioDecoder_Start(audioDec) == AV_ERR_OK) {
920        checkParam = OH_AudioDecoder_Flush(audioDec);
921        if (checkParam == AV_ERR_OK) {
922            checkParam = OH_AudioDecoder_IsValid(audioDec, &status);
923            if (checkParam == AV_ERR_OK) {
924                backParam = SUCCESS;
925                OH_AudioDecoder_Stop(audioDec);
926            }
927        }
928    }
929    napi_create_int32(env, backParam, &result);
930    return result;
931}
932
933EXTERN_C_START
934static napi_value Init(napi_env env, napi_value exports)
935{
936    napi_property_descriptor desc[] = {
937        {"OH_AudioDecoder_CreateByMime", nullptr, AudioDecoderCreateByMimeAudioAac, nullptr, nullptr, nullptr,
938         napi_default, nullptr},
939        {"OH_AudioDecoder_CreateByName", nullptr, AudioDecoderCreateByName, nullptr, nullptr, nullptr, napi_default,
940         nullptr},
941        {"OH_AudioDecoder_Destroy", nullptr, AudioDecoderDestroy, nullptr, nullptr, nullptr, napi_default, nullptr},
942        {"OH_AudioDecoder_SetCallback", nullptr, AudioDecoderSetCallback, nullptr, nullptr, nullptr, napi_default,
943         nullptr},
944        {"OH_AudioDecoder_Configure", nullptr, AudioDecoderConfigure, nullptr, nullptr, nullptr, napi_default, nullptr},
945        {"OH_AudioDecoder_Prepare", nullptr, AudioDecoderPrepare, nullptr, nullptr, nullptr, napi_default, nullptr},
946        {"OH_AudioDecoder_Start", nullptr, AudioDecoderStart, nullptr, nullptr, nullptr, napi_default, nullptr},
947        {"OH_AudioDecoder_Stop", nullptr, AudioDecoderStop, nullptr, nullptr, nullptr, napi_default, nullptr},
948        {"OH_AudioDecoder_Flush", nullptr, AudioDecoderFlush, nullptr, nullptr, nullptr, napi_default, nullptr},
949        {"OH_AudioDecoder_Reset", nullptr, AudioDecoderReset, nullptr, nullptr, nullptr, napi_default, nullptr},
950        {"OH_AudioDecoder_GetOutputDescription", nullptr, AudioDecoderGetOutputDescription, nullptr, nullptr, nullptr,
951         napi_default, nullptr},
952        {"OH_AudioDecoder_SetParameter", nullptr, AudioDecoderSetParameter, nullptr, nullptr, nullptr, napi_default,
953         nullptr},
954        {"OH_AudioDecoder_IsValid", nullptr, AudioDecoderIsValid, nullptr, nullptr, nullptr, napi_default, nullptr},
955        {"OHAudioDecoderCreateByNameAnormal", nullptr, OHAudioDecoderCreateByNameAnormal, nullptr, nullptr, nullptr,
956         napi_default, nullptr},
957        {"OHAudioDecoderCreateByNameBnormal", nullptr, OHAudioDecoderCreateByNameBnormal, nullptr, nullptr, nullptr,
958         napi_default, nullptr},
959        {"OHAudioDecoderCreateByNameCnormal", nullptr, OHAudioDecoderCreateByNameCnormal, nullptr, nullptr, nullptr,
960         napi_default, nullptr},
961        {"OHAudioDecoderDestroyAnormal", nullptr, OHAudioDecoderDestroyAnormal, nullptr, nullptr, nullptr, napi_default,
962         nullptr},
963        {"OHAudioDecoderDestroyBnormal", nullptr, OHAudioDecoderDestroyBnormal, nullptr, nullptr, nullptr, napi_default,
964         nullptr},
965        {"OHAudioDecoderDestroyCnormal", nullptr, OHAudioDecoderDestroyCnormal, nullptr, nullptr, nullptr, napi_default,
966         nullptr},
967        {"OHAudioDecoderStartAnormal", nullptr, OHAudioDecoderStartAnormal, nullptr, nullptr, nullptr, napi_default,
968         nullptr},
969        {"OHAudioDecoderStartBnormal", nullptr, OHAudioDecoderStartBnormal, nullptr, nullptr, nullptr, napi_default,
970         nullptr},
971        {"OHAudioDecoderStartCnormal", nullptr, OHAudioDecoderStartCnormal, nullptr, nullptr, nullptr, napi_default,
972         nullptr},
973        {"OHAudioDecoderStopAnormal", nullptr, OHAudioDecoderStopAnormal, nullptr, nullptr, nullptr, napi_default,
974         nullptr},
975        {"OHAudioDecoderStopBnormal", nullptr, OHAudioDecoderStopBnormal, nullptr, nullptr, nullptr, napi_default,
976         nullptr},
977        {"OHAudioDecoderStopCnormal", nullptr, OHAudioDecoderStopCnormal, nullptr, nullptr, nullptr, napi_default,
978         nullptr},
979        {"OHAudioDecoderFlushAnormal", nullptr, OHAudioDecoderFlushAnormal, nullptr, nullptr, nullptr, napi_default,
980         nullptr},
981        {"OHAudioDecoderFlushBnormal", nullptr, OHAudioDecoderFlushBnormal, nullptr, nullptr, nullptr, napi_default,
982         nullptr},
983        {"OHAudioDecoderFlushCnormal", nullptr, OHAudioDecoderFlushCnormal, nullptr, nullptr, nullptr, napi_default,
984         nullptr},
985        {"OHAudioDecoderResetAnormal", nullptr, OHAudioDecoderResetAnormal, nullptr, nullptr, nullptr, napi_default,
986         nullptr},
987        {"OHAudioDecoderResetBnormal", nullptr, OHAudioDecoderResetBnormal, nullptr, nullptr, nullptr, napi_default,
988         nullptr},
989        {"OHAudioDecoderResetCnormal", nullptr, OHAudioDecoderResetCnormal, nullptr, nullptr, nullptr, napi_default,
990         nullptr},
991        {"OHAudioDecoderGetOutputDescriptionAnormal", nullptr, OHAudioDecoderGetOutputDescriptionAnormal, nullptr,
992         nullptr, nullptr, napi_default, nullptr},
993        {"OHAudioDecoderGetOutputDescriptionBnormal", nullptr, OHAudioDecoderGetOutputDescriptionBnormal, nullptr,
994         nullptr, nullptr, napi_default, nullptr},
995        {"OHAudioDecoderGetOutputDescriptionCnormal", nullptr, OHAudioDecoderGetOutputDescriptionCnormal, nullptr,
996         nullptr, nullptr, napi_default, nullptr},
997        {"OHAudioDecoderSetParameterAnormal", nullptr, OHAudioDecoderSetParameterAnormal, nullptr, nullptr, nullptr,
998         napi_default, nullptr},
999        {"OHAudioDecoderSetParameterBnormal", nullptr, OHAudioDecoderSetParameterBnormal, nullptr, nullptr, nullptr,
1000         napi_default, nullptr},
1001        {"OHAudioDecoderSetParameterCnormal", nullptr, OHAudioDecoderSetParameterCnormal, nullptr, nullptr, nullptr,
1002         napi_default, nullptr},
1003        {"OHAudioDecoderIsValidAnormal", nullptr, OHAudioDecoderIsValidAnormal, nullptr, nullptr, nullptr, napi_default,
1004         nullptr},
1005        {"OHAudioDecoderIsValidBnormal", nullptr, OHAudioDecoderIsValidBnormal, nullptr, nullptr, nullptr, napi_default,
1006         nullptr},
1007        {"OHAudioDecoderIsValidCnormal", nullptr, OHAudioDecoderIsValidCnormal, nullptr, nullptr, nullptr, napi_default,
1008         nullptr},
1009    };
1010    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1011    return exports;
1012}
1013EXTERN_C_END
1014
1015static napi_module demoModule = {
1016    .nm_version = 1,
1017    .nm_flags = 0,
1018    .nm_filename = nullptr,
1019    .nm_register_func = Init,
1020    .nm_modname = "libaudiodecoderxdlndk",
1021    .nm_priv = ((void *)0),
1022    .reserved = { 0 },
1023};
1024
1025extern "C" __attribute__((constructor)) void RegisterModule(void)
1026{
1027    napi_module_register(&demoModule);
1028}
1029