• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 <exception>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <multimedia/player_framework/native_avcodec_audiocodec.h>
21 #include <multimedia/player_framework/native_avcodec_base.h>
22 #include <multimedia/player_framework/native_avformat.h>
23 #include <multimedia/player_framework/native_avcodec_base.h>
24 #include <multimedia/player_framework/native_avformat.h>
25 #include <multimedia/player_framework/native_avcodec_audiodecoder.h>
26 #include <js_native_api_types.h>
27 #include <unistd.h>
28 #include <iostream>
29 #include "hilog/log.h"
30 
31 #define FAIL (-1)
32 #define SUCCESS 0
33 
34 constexpr uint32_t DEFAULT_SAMPLERATE = 44100;
35 constexpr uint32_t DEFAULT_BITRATE_MIN = 8000;
36 constexpr uint32_t DEFAULT_BITRATE_MAX = 320000;
37 constexpr uint32_t DEFAULT_CHANNEL_COUNT_ONE = 1;
38 constexpr uint32_t DEFAULT_CHANNEL_COUNT_TWO = 1;
39 constexpr uint32_t DEFAULT_MAX_INPUT_SIZE = 1152;
40 constexpr uint32_t DEFAULT_AAC_TYPE = 1;
41 constexpr uint8_t DEFAULT_VORBIS_TYPE = 10;
42 constexpr uint8_t DEFAULT_VORBISTWO_TYPE = 20;
43 
AudioDecoderFlushFirst(napi_env env,napi_callback_info info)44 static napi_value AudioDecoderFlushFirst(napi_env env, napi_callback_info info)
45 {
46     int backParam = FAIL;
47     napi_value result = nullptr;
48     OH_AVErrCode checkParam;
49     OH_AVFormat *format = nullptr;
50     format = OH_AVFormat_Create();
51     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
52     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE_MIN);
53     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT_ONE);
54     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
55     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
56     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, false);
57     OH_AudioCodec_Configure(audioDec, format);
58     OH_AudioCodec_Prepare(audioDec);
59     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
60         checkParam = OH_AudioCodec_Flush(audioDec);
61         if (checkParam == AV_ERR_OK) {
62             backParam = SUCCESS;
63             OH_AudioCodec_Stop(audioDec);
64         }
65     }
66     napi_create_int32(env, backParam, &result);
67     return result;
68 }
69 
AudioDecoderFlushSecond(napi_env env,napi_callback_info info)70 static napi_value AudioDecoderFlushSecond(napi_env env, napi_callback_info info)
71 {
72     int backParam = FAIL;
73     napi_value result = nullptr;
74     OH_AVErrCode checkParam;
75     OH_AVFormat *format = nullptr;
76     format = OH_AVFormat_Create();
77     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
78     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE_MAX);
79     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT_TWO);
80     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
81     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
82     OH_AVCodec *audioDec = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, false);
83     OH_AudioCodec_Configure(audioDec, format);
84     OH_AudioCodec_Prepare(audioDec);
85     if (OH_AudioCodec_Start(audioDec) == AV_ERR_OK) {
86         checkParam = OH_AudioCodec_Flush(audioDec);
87         if (checkParam == AV_ERR_OK) {
88             backParam = SUCCESS;
89             OH_AudioCodec_Stop(audioDec);
90         }
91     }
92     napi_create_int32(env, backParam, &result);
93     return result;
94 }
95 
96 EXTERN_C_START
Init(napi_env env,napi_value exports)97 static napi_value Init(napi_env env, napi_value exports)
98 {
99     napi_property_descriptor desc[] = {
100         {"OH_AudioCodec_Flush_First", nullptr, AudioDecoderFlushFirst, nullptr, nullptr, nullptr, napi_default,
101          nullptr},
102         {"OH_AudioCodec_Flush_Second", nullptr, AudioDecoderFlushSecond, nullptr, nullptr, nullptr, napi_default,
103          nullptr},
104     };
105     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
106     return exports;
107 }
108 EXTERN_C_END
109 
110 static napi_module demoModule = {
111     .nm_version = 1,
112     .nm_flags = 0,
113     .nm_filename = nullptr,
114     .nm_register_func = Init,
115     .nm_modname = "pcsAudioTest",
116     .nm_priv = ((void *)0),
117     .reserved = {0},
118 };
119 
RegisterEntryModule(void)120 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
121