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