• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 <bits/alltypes.h>
17 #include <cstdint>
18 #include <cstdio>
19 #include <cstring>
20 #include <net/if.h>
21 #include <thread>
22 #include <chrono>
23 #include <sys/stat.h>
24 #include <linux/quota.h>
25 #include "napi/native_api.h"
26 #include "ohaudio/native_audiocapturer.h"
27 #include "ohaudio/native_audiorenderer.h"
28 #include "ohaudio/native_audiostreambuilder.h"
29 #include "ohaudio/native_audiostream_base.h"
30 #include "hilog/log.h"
31 #include <fstream>
32 
33 namespace AudioTestConstants {
34     constexpr int32_t FIRST_ARG_IDX = 1;
35     constexpr int32_t SECOND_ARG_IDX = 2;
36     constexpr int32_t THIRD_ARG_IDX = 3;
37     constexpr int32_t RECODER_TIME = 10000;
38     constexpr int32_t COUNTDOWN_INTERVAL = 1000;
39     constexpr int32_t CONVERT_RATE = 1000;
40     constexpr int32_t WAIT_INTERVAL = 1000;
41 } // namespace AudioTestConstants
42 
43 static std::string g_filePath = "/data/storage/el2/base/haps/entry/files/oh_test_audio.pcm";
44 static std::string g_filePath_avp = "/data/storage/el2/base/haps/entry/files/2p0.pcm";
45 static std::string g_filePath_avp_vivid = "/data/storage/el2/base/haps/entry/files/avs3_16.wav";
46 static std::string g_filePath_avp_metadata = "/data/storage/el2/base/haps/entry/files/avs3_bitstream.bin";
47 
48 const int GLOBAL_RESMGR = 0xFF00;
49 const char *TAG = "[Sample_audio]";
50 FILE *g_file = nullptr;
51 FILE *g_file_avp = nullptr;
52 bool g_readEnd = false;
53 bool g_rendererLowLatency = false;
54 int32_t g_samplingRate = 48000;
55 int32_t g_channelCount = 2;
56 int32_t g_avpsamplingRate = 48000;
57 int32_t g_avpchannelCount = 2;
58 int32_t g_avpvividchannelCount = 8;
59 
60 std::unique_ptr<std::ifstream> normalPCM;
61 std::unique_ptr<std::ifstream> vividPCM;
62 std::unique_ptr<std::ifstream> metaDataFile;
63 
64 static OH_AudioStream_Result ret;
65 static OH_AudioCapturer *audioCapturer;
66 static OH_AudioRenderer *audioRenderer;
67 static OH_AudioStreamBuilder *builder;
68 static OH_AudioStreamBuilder *rendererBuilder;
69 static OH_AudioRenderer *audioRendererNormal;
70 static OH_AudioRenderer *audioRendererVivid;
71 
GetRendererState(napi_env env,napi_callback_info info)72 static napi_value GetRendererState(napi_env env, napi_callback_info info)
73 {
74     OH_AudioStreamBuilder *builder;
75     OH_AudioStreamBuilder_Create(&builder, AUDIOSTREAM_TYPE_RENDERER);
76     OH_AudioStream_State state;
77     OH_AudioRenderer_GetCurrentState(audioRenderer, &state);
78     napi_value sum;
79     napi_create_int32(env, state, &sum);
80     return sum;
81 }
82 
GetCapturerState(napi_env env,napi_callback_info info)83 static napi_value GetCapturerState(napi_env env, napi_callback_info info)
84 {
85     OH_AudioStreamBuilder *builder;
86     OH_AudioStreamBuilder_Create(&builder, AUDIOSTREAM_TYPE_CAPTURER);
87     OH_AudioStream_State state;
88     OH_AudioCapturer_GetCurrentState(audioCapturer, &state);
89     napi_value sum;
90     napi_create_int32(env, state, &sum);
91     return sum;
92 }
93 
GetFileState(napi_env env,napi_callback_info info)94 static napi_value GetFileState(napi_env env, napi_callback_info info)
95 {
96     napi_value sum;
97     napi_create_int32(env, g_readEnd, &sum);
98     return sum;
99 }
100 
GetFastState(napi_env env,napi_callback_info info)101 static napi_value GetFastState(napi_env env, napi_callback_info info)
102 {
103     napi_value sum;
104     napi_create_int32(env, g_rendererLowLatency, &sum);
105     return sum;
106 }
107 
GetFramesWritten(napi_env env,napi_callback_info info)108 static napi_value GetFramesWritten(napi_env env, napi_callback_info info)
109 {
110     napi_value sum;
111     int64_t frames;
112     OH_AudioRenderer_GetFramesWritten(audioRenderer, &frames);
113     napi_create_int64(env, frames, &sum);
114     return sum;
115 }
116 
GetFileSize(napi_env env,napi_callback_info info)117 static napi_value GetFileSize(napi_env env, napi_callback_info info)
118 {
119     struct stat statbuf;
120     stat(g_filePath.c_str(), &statbuf);
121     napi_value sum;
122     int64_t fileSize = statbuf.st_size;
123     napi_create_int64(env, fileSize, &sum);
124     return sum;
125 }
126 
AudioCapturerOnReadData(OH_AudioCapturer * capturer,void * userData,void * buffer,int32_t bufferLen)127 static int32_t AudioCapturerOnReadData(OH_AudioCapturer *capturer, void *userData, void *buffer, int32_t bufferLen)
128 {
129     size_t count = 1;
130     if (fwrite(buffer, bufferLen, count, g_file) != count) {
131         printf("buffer fwrite err");
132     }
133     return 0;
134 }
135 
AudioRendererOnWriteData(OH_AudioRenderer * renderer,void * userData,void * buffer,int32_t bufferLen)136 static int32_t AudioRendererOnWriteData(OH_AudioRenderer *renderer, void *userData, void *buffer, int32_t bufferLen)
137 {
138     size_t readCount = fread(buffer, bufferLen, 1, g_file);
139     if (!readCount) {
140         if (feof(g_file)) {
141             g_readEnd = true;
142         }
143     }
144     return 0;
145 }
146 
AudioCapturerLowLatencyInit(napi_env env,napi_callback_info info)147 static napi_value AudioCapturerLowLatencyInit(napi_env env, napi_callback_info info)
148 {
149     if (audioCapturer) {
150         OH_AudioCapturer_Release(audioCapturer);
151         OH_AudioStreamBuilder_Destroy(builder);
152         audioCapturer = nullptr;
153         builder = nullptr;
154     }
155     if (g_file) {
156         fclose(g_file);
157         g_file = nullptr;
158     }
159     g_file = fopen(g_filePath.c_str(), "wb");
160     // 1. create builder
161     OH_AudioStream_Type type = AUDIOSTREAM_TYPE_CAPTURER;
162     OH_AudioStreamBuilder_Create(&builder, type);
163     // 2. set params and callbacks
164     OH_AudioStreamBuilder_SetSamplingRate(builder, g_samplingRate);
165     OH_AudioStreamBuilder_SetChannelCount(builder, g_channelCount);
166     OH_AudioStreamBuilder_SetLatencyMode(builder, AUDIOSTREAM_LATENCY_MODE_FAST);
167     OH_AudioCapturer_Callbacks callbacks;
168     callbacks.OH_AudioCapturer_OnReadData = AudioCapturerOnReadData;
169     callbacks.OH_AudioCapturer_OnError = nullptr;
170     callbacks.OH_AudioCapturer_OnInterruptEvent = nullptr;
171     callbacks.OH_AudioCapturer_OnStreamEvent = nullptr;
172     OH_AudioStreamBuilder_SetCapturerCallback(builder, callbacks, nullptr);
173     // 3. create OH_AudioCapturer
174     OH_AudioStreamBuilder_GenerateCapturer(builder, &audioCapturer);
175     return nullptr;
176 }
177 
AudioCapturerInit(napi_env env,napi_callback_info info)178 static napi_value AudioCapturerInit(napi_env env, napi_callback_info info)
179 {
180     if (audioCapturer) {
181         OH_AudioCapturer_Release(audioCapturer);
182         OH_AudioStreamBuilder_Destroy(builder);
183         audioCapturer = nullptr;
184         builder = nullptr;
185     }
186     if (g_file) {
187         fclose(g_file);
188         g_file = nullptr;
189     }
190     g_file = fopen(g_filePath.c_str(), "wb");
191     // 1. create builder
192     OH_AudioStream_Type type = AUDIOSTREAM_TYPE_CAPTURER;
193     OH_AudioStreamBuilder_Create(&builder, type);
194     // 2. set params and callbacks
195     OH_AudioStreamBuilder_SetSamplingRate(builder, g_samplingRate);
196     OH_AudioStreamBuilder_SetChannelCount(builder, g_channelCount);
197     OH_AudioStreamBuilder_SetLatencyMode(builder, AUDIOSTREAM_LATENCY_MODE_NORMAL);
198     OH_AudioCapturer_Callbacks callbacks;
199     callbacks.OH_AudioCapturer_OnReadData = AudioCapturerOnReadData;
200     callbacks.OH_AudioCapturer_OnStreamEvent = nullptr;
201     callbacks.OH_AudioCapturer_OnInterruptEvent = nullptr;
202     callbacks.OH_AudioCapturer_OnError = nullptr;
203     OH_AudioStreamBuilder_SetCapturerCallback(builder, callbacks, nullptr);
204     // 3. create OH_AudioCapturer
205     OH_AudioStreamBuilder_GenerateCapturer(builder, &audioCapturer);
206     return nullptr;
207 }
208 
AudioCapturerStart(napi_env env,napi_callback_info info)209 static napi_value AudioCapturerStart(napi_env env, napi_callback_info info)
210 {
211     // start
212     OH_AudioCapturer_Start(audioCapturer);
213     return nullptr;
214 }
215 
AudioCapturerPause(napi_env env,napi_callback_info info)216 static napi_value AudioCapturerPause(napi_env env, napi_callback_info info)
217 {
218     OH_AudioCapturer_Pause(audioCapturer);
219     return nullptr;
220 }
221 
AudioCapturerStop(napi_env env,napi_callback_info info)222 static napi_value AudioCapturerStop(napi_env env, napi_callback_info info)
223 {
224     OH_AudioCapturer_Stop(audioCapturer);
225     return nullptr;
226 }
227 
AudioCapturerRelease(napi_env env,napi_callback_info info)228 static napi_value AudioCapturerRelease(napi_env env, napi_callback_info info)
229 {
230     if (audioCapturer) {
231         OH_AudioCapturer_Release(audioCapturer);
232         OH_AudioStreamBuilder_Destroy(builder);
233         audioCapturer = nullptr;
234         builder = nullptr;
235     }
236     if (g_file) {
237         fclose(g_file);
238         g_file = nullptr;
239     }
240     return nullptr;
241 }
242 
CloseFile(napi_env env,napi_callback_info info)243 static napi_value CloseFile(napi_env env, napi_callback_info info)
244 {
245     if (g_file) {
246         fclose(g_file);
247         g_file = nullptr;
248     }
249     return nullptr;
250 }
251 
AudioRendererLowLatencyInit(napi_env env,napi_callback_info info)252 static napi_value AudioRendererLowLatencyInit(napi_env env, napi_callback_info info)
253 {
254     if (audioRenderer) {
255         OH_AudioRenderer_Release(audioRenderer);
256         OH_AudioStreamBuilder_Destroy(rendererBuilder);
257         audioRenderer = nullptr;
258         rendererBuilder = nullptr;
259     }
260     if (g_file) {
261         fclose(g_file);
262         g_file = nullptr;
263     }
264     g_file = fopen(g_filePath.c_str(), "rb");
265     if (g_file == nullptr) {
266         return 0;
267     }
268     // create builder
269     OH_AudioStream_Type type = AUDIOSTREAM_TYPE_RENDERER;
270     OH_AudioStreamBuilder_Create(&rendererBuilder, type);
271     // set params and callbacks
272     OH_AudioStreamBuilder_SetSamplingRate(rendererBuilder, g_samplingRate);
273     OH_AudioStreamBuilder_SetChannelCount(rendererBuilder, g_channelCount);
274     OH_AudioStreamBuilder_SetLatencyMode(rendererBuilder, AUDIOSTREAM_LATENCY_MODE_FAST);
275     OH_AudioRenderer_Callbacks rendererCallbacks;
276     rendererCallbacks.OH_AudioRenderer_OnWriteData = AudioRendererOnWriteData;
277     rendererCallbacks.OH_AudioRenderer_OnError = nullptr;
278     rendererCallbacks.OH_AudioRenderer_OnInterruptEvent = nullptr;
279     rendererCallbacks.OH_AudioRenderer_OnStreamEvent = nullptr;
280     OH_AudioStreamBuilder_SetRendererCallback(rendererBuilder, rendererCallbacks, nullptr);
281     // create OH_AudioRenderer
282     OH_AudioStreamBuilder_GenerateRenderer(rendererBuilder, &audioRenderer);
283     g_readEnd = false;
284     g_rendererLowLatency = true;
285     return nullptr;
286 }
287 
AudioRendererInit(napi_env env,napi_callback_info info)288 static napi_value AudioRendererInit(napi_env env, napi_callback_info info)
289 {
290     if (audioRenderer) {
291         OH_AudioRenderer_Release(audioRenderer);
292         OH_AudioStreamBuilder_Destroy(rendererBuilder);
293         audioRenderer = nullptr;
294         rendererBuilder = nullptr;
295     }
296     if (g_file) {
297         fclose(g_file);
298         g_file = nullptr;
299     }
300     g_file = fopen(g_filePath.c_str(), "rb");
301     if (g_file == nullptr) {
302         return 0;
303     }
304     // create builder
305     OH_AudioStream_Type type = AUDIOSTREAM_TYPE_RENDERER;
306     OH_AudioStreamBuilder_Create(&rendererBuilder, type);
307     // set params and callbacks
308     OH_AudioStreamBuilder_SetSamplingRate(rendererBuilder, g_samplingRate);
309     OH_AudioStreamBuilder_SetChannelCount(rendererBuilder, g_channelCount);
310     OH_AudioStreamBuilder_SetLatencyMode(rendererBuilder, AUDIOSTREAM_LATENCY_MODE_NORMAL);
311     OH_AudioRenderer_Callbacks rendererCallbacks;
312     rendererCallbacks.OH_AudioRenderer_OnWriteData = AudioRendererOnWriteData;
313     rendererCallbacks.OH_AudioRenderer_OnStreamEvent = nullptr;
314     rendererCallbacks.OH_AudioRenderer_OnInterruptEvent = nullptr;
315     rendererCallbacks.OH_AudioRenderer_OnError = nullptr;
316     OH_AudioStreamBuilder_SetRendererCallback(rendererBuilder, rendererCallbacks, nullptr);
317     // create OH_AudioRenderer
318     OH_AudioStreamBuilder_GenerateRenderer(rendererBuilder, &audioRenderer);
319     g_readEnd = false;
320     g_rendererLowLatency = false;
321     return nullptr;
322 }
323 
324 
AudioRendererStart(napi_env env,napi_callback_info info)325 static napi_value AudioRendererStart(napi_env env, napi_callback_info info)
326 {
327     g_readEnd = false;
328     // start
329     OH_AudioRenderer_Start(audioRenderer);
330     return nullptr;
331 }
332 
AudioRendererPause(napi_env env,napi_callback_info info)333 static napi_value AudioRendererPause(napi_env env, napi_callback_info info)
334 {
335     g_readEnd = false;
336     OH_AudioRenderer_Pause(audioRenderer);
337     return nullptr;
338 }
339 
AudioRendererStop(napi_env env,napi_callback_info info)340 static napi_value AudioRendererStop(napi_env env, napi_callback_info info)
341 {
342     g_readEnd = false;
343     OH_AudioRenderer_Stop(audioRenderer);
344     return nullptr;
345 }
346 
AudioRendererRelease(napi_env env,napi_callback_info info)347 static napi_value AudioRendererRelease(napi_env env, napi_callback_info info)
348 {
349     if (audioRenderer) {
350         OH_AudioRenderer_Release(audioRenderer);
351         OH_AudioStreamBuilder_Destroy(rendererBuilder);
352         audioRenderer = nullptr;
353         rendererBuilder = nullptr;
354     }
355     if (g_file) {
356         fclose(g_file);
357         g_file = nullptr;
358     }
359     return nullptr;
360 }
361 
AvpAudioRendererOnWriteData(OH_AudioRenderer * renderer,void * userData,void * buffer,int32_t bufferLen)362 static int32_t AvpAudioRendererOnWriteData(OH_AudioRenderer *renderer, void *userData, void *buffer, int32_t bufferLen)
363 {
364     if (!normalPCM->is_open()) {
365         return 0;
366     }
367     normalPCM->read(static_cast<char *>(buffer), bufferLen);
368     if (normalPCM->eof()) {
369         normalPCM->clear();
370         normalPCM->seekg(0, std::ios::beg);
371     }
372     return 0;
373 }
374 
AvpAudioRendererInit(napi_env env,napi_callback_info info)375 static napi_value AvpAudioRendererInit(napi_env env, napi_callback_info info)
376 {
377     if (audioRendererNormal) {
378         OH_AudioRenderer_Release(audioRendererNormal);
379         OH_AudioStreamBuilder_Destroy(rendererBuilder);
380         audioRendererNormal = nullptr;
381         rendererBuilder = nullptr;
382     }
383 
384     normalPCM = std::make_unique<std::ifstream>(g_filePath_avp, std::ios::binary);
385     // create builder
386     OH_AudioStreamBuilder_Create(&rendererBuilder, AUDIOSTREAM_TYPE_RENDERER);
387     // set params and callbacks
388     OH_AudioStreamBuilder_SetSamplingRate(rendererBuilder, g_avpsamplingRate);
389     OH_AudioStreamBuilder_SetChannelCount(rendererBuilder, g_avpchannelCount);
390     OH_AudioStreamBuilder_SetSampleFormat(rendererBuilder, AUDIOSTREAM_SAMPLE_S16LE);
391     OH_AudioStreamBuilder_SetEncodingType(rendererBuilder, AUDIOSTREAM_ENCODING_TYPE_RAW);
392     OH_AudioStreamBuilder_SetRendererInfo(rendererBuilder, AUDIOSTREAM_USAGE_MOVIE);
393 
394     OH_AudioRenderer_Callbacks rendererCallbacks;
395     rendererCallbacks.OH_AudioRenderer_OnWriteData = AvpAudioRendererOnWriteData;
396     rendererCallbacks.OH_AudioRenderer_OnStreamEvent = nullptr;
397     rendererCallbacks.OH_AudioRenderer_OnInterruptEvent = nullptr;
398     rendererCallbacks.OH_AudioRenderer_OnError = nullptr;
399     OH_AudioStreamBuilder_SetRendererCallback(rendererBuilder, rendererCallbacks, nullptr);
400     // create OH_AudioRenderer
401     OH_AudioStreamBuilder_GenerateRenderer(rendererBuilder, &audioRendererNormal);
402     OH_AudioStreamBuilder_Destroy(rendererBuilder);
403     rendererBuilder = nullptr;
404     return nullptr;
405 }
406 
AvpAudioRendererStart(napi_env env,napi_callback_info info)407 static napi_value AvpAudioRendererStart(napi_env env, napi_callback_info info)
408 {
409     // start
410     if (audioRendererNormal == nullptr) {
411         OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "audioRendererNormal is nullptr");
412     }
413     OH_AudioRenderer_Start(audioRendererNormal);
414     return nullptr;
415 }
416 
AvpAudioRendererPause(napi_env env,napi_callback_info info)417 static napi_value AvpAudioRendererPause(napi_env env, napi_callback_info info)
418 {
419     OH_AudioRenderer_Pause(audioRendererNormal);
420     return nullptr;
421 }
422 
AvpAudioRendererStop(napi_env env,napi_callback_info info)423 static napi_value AvpAudioRendererStop(napi_env env, napi_callback_info info)
424 {
425     OH_AudioRenderer_Stop(audioRendererNormal);
426     return nullptr;
427 }
428 
AvpAudioRendererRelease(napi_env env,napi_callback_info info)429 static napi_value AvpAudioRendererRelease(napi_env env, napi_callback_info info)
430 {
431     if (audioRendererNormal) {
432         OH_AudioRenderer_Release(audioRendererNormal);
433         audioRendererNormal = nullptr;
434     }
435     return nullptr;
436 }
437 
AvpGetRendererState(napi_env env,napi_callback_info info)438 static napi_value AvpGetRendererState(napi_env env, napi_callback_info info)
439 {
440     OH_AudioStreamBuilder *builder;
441     OH_AudioStreamBuilder_Create(&builder, AUDIOSTREAM_TYPE_RENDERER);
442     OH_AudioStream_State state;
443     OH_AudioRenderer_GetCurrentState(audioRendererNormal, &state);
444     napi_value sum;
445     napi_create_int32(env, state, &sum);
446     return sum;
447 }
448 
AvpVividAudioRendererOnWriteData(OH_AudioRenderer * renderer,void * userData,void * audioData,int32_t audioDataSize,void * metaData,int32_t metaDataSize)449 static int32_t AvpVividAudioRendererOnWriteData(OH_AudioRenderer *renderer, void *userData, void *audioData,
450                                                 int32_t audioDataSize, void *metaData, int32_t metaDataSize)
451 {
452     if (!vividPCM->is_open() || !metaDataFile->is_open()) {
453         return 0;
454     }
455     vividPCM->read(static_cast<char *>(audioData), audioDataSize);
456     metaDataFile->read(static_cast<char *>(metaData), metaDataSize);
457     if (vividPCM->eof()) {
458         vividPCM->clear();
459         vividPCM->seekg(0, std::ios::beg);
460     }
461     if (metaDataFile->eof()) {
462         metaDataFile->clear();
463         metaDataFile->seekg(0, std::ios::beg);
464     }
465     return 0;
466 }
467 
AvpVividAudioRendererInit(napi_env env,napi_callback_info info)468 static napi_value AvpVividAudioRendererInit(napi_env env, napi_callback_info info)
469 {
470     if (audioRendererVivid) {
471         OH_AudioRenderer_Release(audioRendererVivid);
472         OH_AudioStreamBuilder_Destroy(rendererBuilder);
473         audioRendererVivid = nullptr;
474         rendererBuilder = nullptr;
475     }
476     vividPCM = std::make_unique<std::ifstream>(g_filePath_avp_vivid, std::ios::binary);
477     metaDataFile = std::make_unique<std::ifstream>(g_filePath_avp_metadata, std::ios::binary);
478     // create builder
479     OH_AudioStreamBuilder_Create(&rendererBuilder, AUDIOSTREAM_TYPE_RENDERER);
480     // set params and callbacks
481     OH_AudioStreamBuilder_SetSamplingRate(rendererBuilder, g_avpsamplingRate);
482     OH_AudioStreamBuilder_SetChannelCount(rendererBuilder, g_avpvividchannelCount);
483     OH_AudioStreamBuilder_SetEncodingType(rendererBuilder, AUDIOSTREAM_ENCODING_TYPE_AUDIOVIVID);
484     OH_AudioStreamBuilder_SetRendererInfo(rendererBuilder, AUDIOSTREAM_USAGE_MOVIE);
485     OH_AudioRenderer_Callbacks rendererCallbacks;
486     rendererCallbacks.OH_AudioRenderer_OnWriteData = nullptr;
487     rendererCallbacks.OH_AudioRenderer_OnStreamEvent = nullptr;
488     rendererCallbacks.OH_AudioRenderer_OnInterruptEvent = nullptr;
489     rendererCallbacks.OH_AudioRenderer_OnError = nullptr;
490     OH_AudioStreamBuilder_SetRendererCallback(rendererBuilder, rendererCallbacks, nullptr);
491     OH_AudioRenderer_WriteDataWithMetadataCallback metadataCallback = AvpVividAudioRendererOnWriteData;
492     OH_AudioStreamBuilder_SetWriteDataWithMetadataCallback(rendererBuilder, metadataCallback, nullptr);
493     // create OH_AudioRenderer
494     OH_AudioStreamBuilder_GenerateRenderer(rendererBuilder, &audioRendererVivid);
495     OH_AudioStreamBuilder_Destroy(rendererBuilder);
496     rendererBuilder = nullptr;
497     return nullptr;
498 }
499 
AvpVividAudioRendererStart(napi_env env,napi_callback_info info)500 static napi_value AvpVividAudioRendererStart(napi_env env, napi_callback_info info)
501 {
502     // start
503     if (audioRendererVivid == nullptr) {
504         OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "audioRendererNormal is nullptr");
505     }
506     OH_AudioRenderer_Start(audioRendererVivid);
507     return nullptr;
508 }
509 
AvpVividAudioRendererPause(napi_env env,napi_callback_info info)510 static napi_value AvpVividAudioRendererPause(napi_env env, napi_callback_info info)
511 {
512     OH_AudioRenderer_Pause(audioRendererVivid);
513     return nullptr;
514 }
515 
AvpVividAudioRendererStop(napi_env env,napi_callback_info info)516 static napi_value AvpVividAudioRendererStop(napi_env env, napi_callback_info info)
517 {
518     OH_AudioRenderer_Stop(audioRendererVivid);
519     return nullptr;
520 }
521 
AvpVividAudioRendererRelease(napi_env env,napi_callback_info info)522 static napi_value AvpVividAudioRendererRelease(napi_env env, napi_callback_info info)
523 {
524     if (audioRendererVivid) {
525         OH_AudioRenderer_Release(audioRendererVivid);
526         audioRendererVivid = nullptr;
527     }
528     return nullptr;
529 }
530 
AvpVividGetRendererState(napi_env env,napi_callback_info info)531 static napi_value AvpVividGetRendererState(napi_env env, napi_callback_info info)
532 {
533     OH_AudioStreamBuilder *builder;
534     OH_AudioStreamBuilder_Create(&builder, AUDIOSTREAM_TYPE_RENDERER);
535     OH_AudioStream_State state;
536     OH_AudioRenderer_GetCurrentState(audioRendererVivid, &state);
537     napi_value sum;
538     napi_create_int32(env, state, &sum);
539     return sum;
540 }
541 
Init(napi_env env,napi_value exports)542 EXTERN_C_START static napi_value Init(napi_env env, napi_value exports)
543 {
544     napi_property_descriptor desc[] = {
545         {"closeFile", nullptr, CloseFile, nullptr, nullptr, nullptr, napi_default, nullptr},
546         {"audioCapturerLowLatencyInit", nullptr, AudioCapturerLowLatencyInit, nullptr, nullptr, nullptr, napi_default,
547          nullptr},
548         {"audioCapturerInit", nullptr, AudioCapturerInit, nullptr, nullptr, nullptr, napi_default, nullptr},
549         {"audioCapturerStart", nullptr, AudioCapturerStart, nullptr, nullptr, nullptr, napi_default, nullptr},
550         {"audioCapturerPause", nullptr, AudioCapturerPause, nullptr, nullptr, nullptr, napi_default, nullptr},
551         {"audioCapturerRelease", nullptr, AudioCapturerRelease, nullptr, nullptr, nullptr, napi_default, nullptr},
552         {"audioCapturerStop", nullptr, AudioCapturerStop, nullptr, nullptr, nullptr, napi_default, nullptr},
553         {"audioRendererLowLatencyInit", nullptr, AudioRendererLowLatencyInit, nullptr, nullptr, nullptr, napi_default,
554          nullptr},
555         {"audioRendererInit", nullptr, AudioRendererInit, nullptr, nullptr, nullptr, napi_default, nullptr},
556         {"audioRendererStart", nullptr, AudioRendererStart, nullptr, nullptr, nullptr, napi_default, nullptr},
557         {"audioRendererPause", nullptr, AudioRendererPause, nullptr, nullptr, nullptr, napi_default, nullptr},
558         {"audioRendererRelease", nullptr, AudioRendererRelease, nullptr, nullptr, nullptr, napi_default, nullptr},
559         {"getRendererState", nullptr, GetRendererState, nullptr, nullptr, nullptr, napi_default, nullptr},
560         {"getCapturerState", nullptr, GetCapturerState, nullptr, nullptr, nullptr, napi_default, nullptr},
561         {"getFileSize", nullptr, GetFileSize, nullptr, nullptr, nullptr, napi_default, nullptr},
562         {"getFramesWritten", nullptr, GetFramesWritten, nullptr, nullptr, nullptr, napi_default, nullptr},
563         {"getFileState", nullptr, GetFileState, nullptr, nullptr, nullptr, napi_default, nullptr},
564         {"getFastState", nullptr, GetFastState, nullptr, nullptr, nullptr, napi_default, nullptr},
565         {"audioRendererStop", nullptr, AudioRendererStop, nullptr, nullptr, nullptr, napi_default, nullptr},
566 
567         {"avpAudioRendererInit", nullptr, AvpAudioRendererInit, nullptr, nullptr, nullptr, napi_default, nullptr},
568         {"avpAudioRendererStart", nullptr, AvpAudioRendererStart, nullptr, nullptr, nullptr, napi_default, nullptr},
569         {"avpAudioRendererPause", nullptr, AvpAudioRendererPause, nullptr, nullptr, nullptr, napi_default, nullptr},
570         {"avpAudioRendererStop", nullptr, AvpAudioRendererStop, nullptr, nullptr, nullptr, napi_default, nullptr},
571         {"avpAudioRendererRelease", nullptr, AvpAudioRendererRelease, nullptr, nullptr, nullptr, napi_default,
572             nullptr},
573         {"avpGetRendererState", nullptr, AvpGetRendererState, nullptr, nullptr, nullptr, napi_default, nullptr},
574         {"avpVividAudioRendererInit", nullptr, AvpVividAudioRendererInit, nullptr, nullptr, nullptr, napi_default,
575             nullptr},
576         {"avpVividAudioRendererStart", nullptr, AvpVividAudioRendererStart, nullptr, nullptr, nullptr, napi_default,
577             nullptr},
578         {"avpVividAudioRendererPause", nullptr, AvpVividAudioRendererPause, nullptr, nullptr, nullptr, napi_default,
579             nullptr},
580         {"avpVividAudioRendererStop", nullptr, AvpVividAudioRendererStop, nullptr, nullptr, nullptr, napi_default,
581             nullptr},
582         {"avpVividAudioRendererRelease", nullptr, AvpVividAudioRendererRelease, nullptr, nullptr, nullptr,
583             napi_default, nullptr},
584         {"avpVividGetRendererState", nullptr, AvpVividGetRendererState, nullptr, nullptr, nullptr, napi_default,
585             nullptr}};
586     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
587     return exports;
588 }
589 EXTERN_C_END
590 
591 static napi_module demoModule = {
592     .nm_version = 1,
593     .nm_flags = 0,
594     .nm_filename = nullptr,
595     .nm_register_func = Init,
596     .nm_modname = "entry",
597     .nm_priv = ((void *)0),
598     .reserved = {0},
599 };
600 
RegisterEntryModule(void)601 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
602 {
603     napi_module_register(&demoModule);
604 }