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/avcodec_audio_channel_layout.h>
20 #include <multimedia/player_framework/native_avcodec_audioencoder.h>
21 #include <multimedia/player_framework/native_avcapability.h>
22 #include <multimedia/player_framework/native_avcodec_base.h>
23 #include <multimedia/player_framework/native_averrors.h>
24 #include <multimedia/player_framework/native_avformat.h>
25 #include <pthread.h>
26 #include <queue>
27 #include <iostream>
28 #include <fstream>
29 
30 #define FAIL (-1)
31 #define SUCCESS 0
32 #define WIDTH 1920
33 #define HEIGHT 1080
34 #define FRAMERATETHIRTY 30
35 constexpr uint32_t DEFAULT_SAMPLERATE = 44100;
36 constexpr uint64_t DEFAULT_BITRATE = 32000;
37 constexpr uint32_t DEFAULT_CHANNEL_COUNT = 2;
38 constexpr AudioChannelLayout CHANNEL_LAYOUT = AudioChannelLayout::STEREO;
39 constexpr OH_BitsPerSample SAMPLE_FORMAT = OH_BitsPerSample::SAMPLE_F32LE;
40 constexpr int32_t COMPLIANCE_LEVEL = 0;
41 constexpr OH_BitsPerSample BITS_PER_CODED_SAMPLE = OH_BitsPerSample::SAMPLE_S24LE;
42 constexpr uint32_t DEFAULT_MAX_INPUT_SIZE = 1024*DEFAULT_CHANNEL_COUNT *sizeof(float);
43 using namespace std;
44 
AudioEncoderCreateByMime(napi_env env, napi_callback_info info)45 static napi_value AudioEncoderCreateByMime(napi_env env, napi_callback_info info)
46 {
47     int backParam = FAIL;
48     OH_AVCodec *checkParam = nullptr;
49     checkParam = OH_AudioEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC);
50     if (checkParam != nullptr) {
51         backParam = SUCCESS;
52     }
53     napi_value result = nullptr;
54     napi_create_int32(env, backParam, &result);
55     return result;
56 }
57 
AudioEncoderCreateByName(napi_env env, napi_callback_info info)58 static napi_value AudioEncoderCreateByName(napi_env env, napi_callback_info info)
59 {
60     int backParam = FAIL;
61     OH_AVCodec *checkParam = nullptr;
62     OH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_AUDIO_FLAC, true);
63     const char *name = OH_AVCapability_GetName(capability);
64     checkParam = OH_AudioEncoder_CreateByName(name);
65     if (checkParam != nullptr) {
66         backParam = SUCCESS;
67     }
68     napi_value result = nullptr;
69     napi_create_int32(env, backParam, &result);
70     return result;
71 }
72 
AudioEncoderDestroy(napi_env env, napi_callback_info info)73 static napi_value AudioEncoderDestroy(napi_env env, napi_callback_info info)
74 {
75     int backParam = FAIL;
76     napi_value result = nullptr;
77     OH_AVCodec *audioEnc = nullptr;
78     OH_AVErrCode checkParam;
79     audioEnc = OH_AudioEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC);
80     checkParam = OH_AudioEncoder_Destroy(audioEnc);
81     if (checkParam == AV_ERR_OK) {
82         backParam = SUCCESS;
83     }
84     napi_create_int32(env, backParam, &result);
85     return result;
86 }
87 
88 class AEncSignal {
89 public:
90     mutex inMutex_;
91     mutex outMutex_;
92     mutex startMutex_;
93     condition_variable inCond_;
94     condition_variable outCond_;
95     condition_variable startCond_;
96     queue<uint32_t> inQueue_;
97     queue<uint32_t> outQueue_;
98     queue<OH_AVMemory *> inBufferQueue_;
99     queue<OH_AVMemory *> outBufferQueue_;
100     queue<OH_AVCodecBufferAttr> attrQueue_;
101 };
102 AEncSignal *signal_ = new AEncSignal();
OnError(OH_AVCodec *codec, int32_t errorCode, void *userData)103 static void OnError(OH_AVCodec *codec, int32_t errorCode, void *userData)
104 {
105     (void)codec;
106     (void)errorCode;
107     (void)userData;
108 }
OnStreamChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData)109 static void OnStreamChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData)
110 {
111     (void)codec;
112     (void)format;
113     (void)userData;
114 }
OnNeedInputData(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, void *userData)115 static void OnNeedInputData(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, void *userData)
116 {
117     (void)codec;
118     AEncSignal *signal = static_cast<AEncSignal *>(userData);
119     unique_lock<mutex> lock(signal->inMutex_);
120     signal->inQueue_.push(index);
121     signal->inBufferQueue_.push(data);
122     signal->inCond_.notify_all();
123 }
OnNeedOutputData(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, OH_AVCodecBufferAttr *attr, void *userData)124 static void OnNeedOutputData(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, OH_AVCodecBufferAttr *attr,
125                                         void *userData)
126 {
127     (void)codec;
128     AEncSignal *signal = static_cast<AEncSignal *>(userData);
129     unique_lock<mutex> lock(signal->outMutex_);
130     signal->outQueue_.push(index);
131     signal->outBufferQueue_.push(data);
132     if (attr) {
133         signal->attrQueue_.push(*attr);
134     }
135 }
136 
AudioEncoderSetCallback(napi_env env, napi_callback_info info)137 static napi_value AudioEncoderSetCallback(napi_env env, napi_callback_info info)
138 {
139     int backParam = FAIL;
140     napi_value result = nullptr;
141     OH_AVCodec *audioEnc = nullptr;
142     OH_AVErrCode checkParam;
143     audioEnc = OH_AudioEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC);
144     signal_ = new AEncSignal();
145     OH_AVCodecAsyncCallback callback = {&OnError, &OnStreamChanged, &OnNeedInputData, &OnNeedOutputData};
146     checkParam = OH_AudioEncoder_SetCallback(audioEnc, callback, signal_);
147     if (checkParam == AV_ERR_OK) {
148         backParam = SUCCESS;
149     }
150     OH_AudioEncoder_Destroy(audioEnc);
151     napi_create_int32(env, backParam, &result);
152     return result;
153 }
154 
AudioEncoderConfigure(napi_env env, napi_callback_info info)155 static napi_value AudioEncoderConfigure(napi_env env, napi_callback_info info)
156 {
157     int backParam = FAIL;
158     napi_value result = nullptr;
159     OH_AVCodec *audioEnc = nullptr;
160     OH_AVErrCode checkParam;
161     OH_AVFormat *format = nullptr;
162     format = OH_AVFormat_Create();
163     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
164     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
165     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
166     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, BITS_PER_CODED_SAMPLE);
167     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_FORMAT);
168     OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, CHANNEL_LAYOUT);
169     OH_AVFormat_SetLongValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, COMPLIANCE_LEVEL);
170     audioEnc = OH_AudioEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC);
171     checkParam = OH_AudioEncoder_Configure(audioEnc, format);
172     if (checkParam == AV_ERR_OK) {
173         backParam = SUCCESS;
174     }
175     napi_create_int32(env, backParam, &result);
176     return result;
177 }
178 
AudioEncoderPrepare(napi_env env, napi_callback_info info)179 static napi_value AudioEncoderPrepare(napi_env env, napi_callback_info info)
180 {
181     int backParam = FAIL;
182     napi_value result = nullptr;
183     OH_AVCodec *audioEnc = nullptr;
184     OH_AVErrCode checkParam;
185     OH_AVFormat *format = nullptr;
186     format = OH_AVFormat_Create();
187     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
188     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
189     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
190     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_FORMAT);
191     OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, CHANNEL_LAYOUT);
192     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
193     audioEnc = OH_AudioEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC);
194     OH_AudioEncoder_Configure(audioEnc, format);
195     checkParam = OH_AudioEncoder_Prepare(audioEnc);
196     if (checkParam == AV_ERR_OK) {
197         backParam = SUCCESS;
198     }
199     napi_create_int32(env, backParam, &result);
200     return result;
201 }
202 
AudioEncoderStart(napi_env env, napi_callback_info info)203 static napi_value AudioEncoderStart(napi_env env, napi_callback_info info)
204 {
205     int backParam = FAIL;
206     napi_value result = nullptr;
207     OH_AVCodec *audioEnc = nullptr;
208     OH_AVErrCode checkParam;
209     OH_AVFormat *format = nullptr;
210     format = OH_AVFormat_Create();
211     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
212     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
213     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
214     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_FORMAT);
215     OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, CHANNEL_LAYOUT);
216     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
217     audioEnc = OH_AudioEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC);
218     OH_AudioEncoder_Configure(audioEnc, format);
219     OH_AudioEncoder_Prepare(audioEnc);
220     checkParam = OH_AudioEncoder_Start(audioEnc);
221     if (checkParam == AV_ERR_OK) {
222         backParam = SUCCESS;
223     }
224     napi_create_int32(env, backParam, &result);
225     return result;
226 }
227 
AudioEncoderStop(napi_env env, napi_callback_info info)228 static napi_value AudioEncoderStop(napi_env env, napi_callback_info info)
229 {
230     int backParam = FAIL;
231     napi_value result = nullptr;
232     OH_AVCodec *audioEnc = nullptr;
233     OH_AVErrCode checkParam;
234     OH_AVFormat *format = nullptr;
235     format = OH_AVFormat_Create();
236     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
237     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
238     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
239     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_FORMAT);
240     OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, CHANNEL_LAYOUT);
241     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
242     audioEnc = OH_AudioEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC);
243     OH_AudioEncoder_Configure(audioEnc, format);
244     OH_AudioEncoder_Prepare(audioEnc);
245     if (OH_AudioEncoder_Start(audioEnc) == AV_ERR_OK) {
246         checkParam = OH_AudioEncoder_Stop(audioEnc);
247         if(checkParam == AV_ERR_OK){
248             backParam = SUCCESS;
249         }
250     }
251     napi_create_int32(env, backParam, &result);
252     return result;
253 }
254 
AudioEncoderFlush(napi_env env, napi_callback_info info)255 static napi_value AudioEncoderFlush(napi_env env, napi_callback_info info)
256 {
257     int backParam = FAIL;
258     napi_value result = nullptr;
259     OH_AVCodec *audioEnc = nullptr;
260     OH_AVErrCode checkParam;
261     OH_AVFormat *format = nullptr;
262     format = OH_AVFormat_Create();
263     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
264     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
265     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
266     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_FORMAT);
267     OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, CHANNEL_LAYOUT);
268     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
269     audioEnc = OH_AudioEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC);
270     OH_AudioEncoder_Configure(audioEnc, format);
271     OH_AudioEncoder_Prepare(audioEnc);
272     if (OH_AudioEncoder_Start(audioEnc) == AV_ERR_OK) {
273         checkParam = OH_AudioEncoder_Flush(audioEnc);
274         if(checkParam == AV_ERR_OK){
275             backParam = SUCCESS;
276             OH_AudioEncoder_Stop(audioEnc);
277         }
278     }
279     napi_create_int32(env, backParam, &result);
280     return result;
281 }
282 
AudioEncoderReset(napi_env env, napi_callback_info info)283 static napi_value AudioEncoderReset(napi_env env, napi_callback_info info)
284 {
285     int backParam = FAIL;
286     napi_value result = nullptr;
287     OH_AVCodec *audioEnc = nullptr;
288     OH_AVErrCode checkParam;
289     OH_AVFormat *format = nullptr;
290     format = OH_AVFormat_Create();
291     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
292     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
293     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
294     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_FORMAT);
295     OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, CHANNEL_LAYOUT);
296     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
297     audioEnc = OH_AudioEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC);
298     OH_AudioEncoder_Configure(audioEnc, format);
299     OH_AudioEncoder_Prepare(audioEnc);
300     if (OH_AudioEncoder_Start(audioEnc) == AV_ERR_OK) {
301         checkParam = OH_AudioEncoder_Reset(audioEnc);
302         if(checkParam == AV_ERR_OK){
303             backParam = SUCCESS;
304         }
305     }
306     napi_create_int32(env, backParam, &result);
307     return result;
308 }
309 
AudioEncoderGetOutputDescription(napi_env env, napi_callback_info info)310 static napi_value AudioEncoderGetOutputDescription(napi_env env, napi_callback_info info)
311 {
312     int backParam = FAIL;
313     napi_value result = nullptr;
314     OH_AVCodec *audioEnc = nullptr;
315     OH_AVFormat *checkParam = nullptr;
316     OH_AVFormat *format = nullptr;
317     format = OH_AVFormat_Create();
318     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
319     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
320     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
321     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_FORMAT);
322     OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, CHANNEL_LAYOUT);
323     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
324     audioEnc = OH_AudioEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC);
325     OH_AudioEncoder_Configure(audioEnc, format);
326     OH_AudioEncoder_Prepare(audioEnc);
327     if (OH_AudioEncoder_Start(audioEnc) == AV_ERR_OK) {
328         checkParam = OH_AudioEncoder_GetOutputDescription(audioEnc);
329         if(checkParam != nullptr){
330             backParam = SUCCESS;
331             free(checkParam);
332         }
333     }
334     napi_create_int32(env, backParam, &result);
335     return result;
336 }
337 
AudioEncoderSetParameter(napi_env env, napi_callback_info info)338 static napi_value AudioEncoderSetParameter(napi_env env, napi_callback_info info)
339 {
340     int backParam = FAIL;
341     napi_value result = nullptr;
342     OH_AVCodec *audioEnc = nullptr;
343     OH_AVErrCode checkParam;
344     OH_AVFormat *format = nullptr;
345     format = OH_AVFormat_Create();
346     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
347     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
348     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
349     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_FORMAT);
350     OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, CHANNEL_LAYOUT);
351     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
352     audioEnc = OH_AudioEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC);
353     OH_AudioEncoder_Configure(audioEnc, format);
354     OH_AudioEncoder_Prepare(audioEnc);
355     if (OH_AudioEncoder_Start(audioEnc) == AV_ERR_OK) {
356         format = OH_AudioEncoder_GetOutputDescription(audioEnc);
357         if(format != nullptr){
358             checkParam = OH_AudioEncoder_SetParameter(audioEnc, format);
359             if(checkParam == AV_ERR_OK){
360                 backParam = SUCCESS;
361                 free(format);
362             }
363         }
364     }
365     napi_create_int32(env, backParam, &result);
366     return result;
367 }
368 
AudioEncoderIsValid(napi_env env, napi_callback_info info)369 static napi_value AudioEncoderIsValid(napi_env env, napi_callback_info info)
370 {
371     int backParam = FAIL;
372     napi_value result = nullptr;
373     OH_AVCodec *audioEnc = nullptr;
374     OH_AVErrCode checkParam;
375     bool status = true;
376     OH_AVFormat *format = nullptr;
377     format = OH_AVFormat_Create();
378     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
379     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
380     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
381     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_FORMAT);
382     OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, CHANNEL_LAYOUT);
383     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
384     audioEnc = OH_AudioEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC);
385     OH_AudioEncoder_Configure(audioEnc, format);
386     OH_AudioEncoder_Prepare(audioEnc);
387     if (OH_AudioEncoder_Start(audioEnc) == AV_ERR_OK) {
388         checkParam = OH_AudioEncoder_Flush(audioEnc);
389         if (checkParam == AV_ERR_OK) {
390             checkParam = OH_AudioEncoder_IsValid(audioEnc, &status);
391             if (checkParam == AV_ERR_OK) {
392                 backParam = SUCCESS;
393                 OH_AudioEncoder_Stop(audioEnc);
394             }
395         }
396     }
397     napi_create_int32(env, backParam, &result);
398     return result;
399 }
400 
401 EXTERN_C_START
Init(napi_env env, napi_value exports)402 static napi_value Init(napi_env env, napi_value exports) {
403     napi_property_descriptor desc[] = {
404         {"OH_AudioEncoder_CreateByMime", nullptr, AudioEncoderCreateByMime, nullptr, nullptr, nullptr, napi_default,
405          nullptr},
406         {"OH_AudioEncoder_CreateByName", nullptr, AudioEncoderCreateByName, nullptr, nullptr, nullptr, napi_default,
407          nullptr},
408         {"OH_AudioEncoder_Destroy", nullptr, AudioEncoderDestroy, nullptr, nullptr, nullptr, napi_default, nullptr},
409         {"OH_AudioEncoder_SetCallback", nullptr, AudioEncoderSetCallback, nullptr, nullptr, nullptr, napi_default,
410          nullptr},
411         {"OH_AudioEncoder_Configure", nullptr, AudioEncoderConfigure, nullptr, nullptr, nullptr, napi_default, nullptr},
412         {"OH_AudioEncoder_Prepare", nullptr, AudioEncoderPrepare, nullptr, nullptr, nullptr, napi_default, nullptr},
413         {"OH_AudioEncoder_Start", nullptr, AudioEncoderStart, nullptr, nullptr, nullptr, napi_default, nullptr},
414         {"OH_AudioEncoder_Stop", nullptr, AudioEncoderStop, nullptr, nullptr, nullptr, napi_default, nullptr},
415         {"OH_AudioEncoder_Flush", nullptr, AudioEncoderFlush, nullptr, nullptr, nullptr, napi_default, nullptr},
416         {"OH_AudioEncoder_Reset", nullptr, AudioEncoderReset, nullptr, nullptr, nullptr, napi_default, nullptr},
417         {"OH_AudioEncoder_GetOutputDescription", nullptr, AudioEncoderGetOutputDescription, nullptr, nullptr, nullptr,
418          napi_default, nullptr},
419         {"OH_AudioEncoder_SetParameter", nullptr, AudioEncoderSetParameter, nullptr, nullptr, nullptr, napi_default,
420          nullptr},
421         {"OH_AudioEncoder_IsValid", nullptr, AudioEncoderIsValid, nullptr, nullptr, nullptr, napi_default, nullptr},
422     };
423     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
424     return exports;
425 }
426 EXTERN_C_END
427 
428 static napi_module demoModule = {
429     .nm_version = 1,
430     .nm_flags = 0,
431     .nm_filename = nullptr,
432     .nm_register_func = Init,
433     .nm_modname = "libaudioencoderxdlndk",
434     .nm_priv = ((void *)0),
435     .reserved = {0},
436 };
437 
RegisterModule(void)438 extern "C" __attribute__((constructor)) void RegisterModule(void) {
439     napi_module_register(&demoModule);
440 }
441