• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "audio_manager_napi.h"
17 
18 #include "audio_capturer_napi.h"
19 #include "audio_common_napi.h"
20 #include "audio_errors.h"
21 #include "audio_parameters_napi.h"
22 #include "audio_renderer_info_napi.h"
23 #include "audio_renderer_napi.h"
24 #include "audio_ringermode_callback_napi.h"
25 #include "audio_manager_callback_napi.h"
26 #include "audio_manager_interrupt_callback_napi.h"
27 #include "audio_volume_key_event_napi.h"
28 
29 #include "hilog/log.h"
30 #include "media_log.h"
31 #include "ringtone_options_napi.h"
32 #include "ringtone_player_napi.h"
33 #include "system_sound_manager_napi.h"
34 
35 using namespace std;
36 using OHOS::HiviewDFX::HiLog;
37 using OHOS::HiviewDFX::HiLogLabel;
38 
39 namespace {
40     const std::string RINGERMODE_CALLBACK_NAME = "ringerModeChange";
41     const std::string VOLUME_CHANGE_CALLBACK_NAME = "volumeChange";
42     const std::string INTERRUPT_CALLBACK_NAME = "interrupt";
43 }
44 
45 namespace OHOS {
46 namespace AudioStandard {
47 static __thread napi_ref g_managerConstructor = nullptr;
48 napi_ref AudioManagerNapi::audioVolumeTypeRef_ = nullptr;
49 napi_ref AudioManagerNapi::deviceFlagRef_ = nullptr;
50 napi_ref AudioManagerNapi::deviceRoleRef_ = nullptr;
51 napi_ref AudioManagerNapi::deviceTypeRef_ = nullptr;
52 napi_ref AudioManagerNapi::activeDeviceTypeRef_ = nullptr;
53 napi_ref AudioManagerNapi::audioRingModeRef_ = nullptr;
54 napi_ref AudioManagerNapi::deviceChangeType_ = nullptr;
55 napi_ref AudioManagerNapi::interruptActionType_ = nullptr;
56 napi_ref AudioManagerNapi::interruptHint_ = nullptr;
57 napi_ref AudioManagerNapi::interruptType_ = nullptr;
58 napi_ref AudioManagerNapi::audioScene_ = nullptr;
59 
60 #define GET_PARAMS(env, info, num) \
61     size_t argc = num;             \
62     napi_value argv[num] = {0};    \
63     napi_value thisVar = nullptr;  \
64     void *data;                    \
65     napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)
66 
67 struct AudioManagerAsyncContext {
68     napi_env env;
69     napi_async_work work;
70     napi_deferred deferred;
71     napi_ref callbackRef = nullptr;
72     int32_t volType;
73     int32_t volLevel;
74     int32_t deviceType;
75     int32_t ringMode;
76     int32_t scene;
77     int32_t deviceFlag;
78     int32_t intValue;
79     int32_t status;
80     bool isMute;
81     bool isActive;
82     bool isTrue;
83     string key;
84     string valueStr;
85     AudioManagerNapi *objectInfo;
86     vector<sptr<AudioDeviceDescriptor>> deviceDescriptors;
87 };
88 
89 namespace {
90     const int ARGS_ONE = 1;
91     const int ARGS_TWO = 2;
92     const int ARGS_THREE = 3;
93     const int PARAM0 = 0;
94     const int PARAM1 = 1;
95     const int PARAM2 = 2;
96     constexpr HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AudioManagerNapi"};
97 }
98 
AudioManagerNapi()99 AudioManagerNapi::AudioManagerNapi()
100     : audioMngr_(nullptr), env_(nullptr), wrapper_(nullptr) {}
101 
~AudioManagerNapi()102 AudioManagerNapi::~AudioManagerNapi()
103 {
104     if (wrapper_ != nullptr) {
105         napi_delete_reference(env_, wrapper_);
106     }
107     MEDIA_DEBUG_LOG("AudioManagerNapi::~AudioManagerNapi()");
108 }
109 
Destructor(napi_env env,void * nativeObject,void * finalize_hint)110 void AudioManagerNapi::Destructor(napi_env env, void *nativeObject, void *finalize_hint)
111 {
112     if (nativeObject != nullptr) {
113         auto obj = static_cast<AudioManagerNapi*>(nativeObject);
114         delete obj;
115         obj = nullptr;
116         MEDIA_DEBUG_LOG("AudioManagerNapi::Destructor delete AudioManagerNapi obj done");
117     }
118 }
119 
GetNativeAudioVolumeType(int32_t volumeType)120 static AudioSystemManager::AudioVolumeType GetNativeAudioVolumeType(int32_t volumeType)
121 {
122     AudioSystemManager::AudioVolumeType result = AudioSystemManager::STREAM_MUSIC;
123 
124     switch (volumeType) {
125         case AudioManagerNapi::RINGTONE:
126             result = AudioSystemManager::STREAM_RING;
127             break;
128         case AudioManagerNapi::MEDIA:
129             result = AudioSystemManager::STREAM_MUSIC;
130             break;
131         case AudioManagerNapi::VOICE_CALL:
132             result = AudioSystemManager::STREAM_VOICE_CALL;
133             break;
134         case AudioManagerNapi::VOICE_ASSISTANT:
135             result = AudioSystemManager::STREAM_VOICE_ASSISTANT;
136             break;
137         default:
138             result = AudioSystemManager::STREAM_MUSIC;
139             HiLog::Error(LABEL, "Unknown volume type, Set it to default MEDIA!");
140             break;
141     }
142 
143     return result;
144 }
145 
GetNativeAudioRingerMode(int32_t ringMode)146 static AudioRingerMode GetNativeAudioRingerMode(int32_t ringMode)
147 {
148     AudioRingerMode result = RINGER_MODE_NORMAL;
149 
150     switch (ringMode) {
151         case AudioManagerNapi::RINGER_MODE_SILENT:
152             result = RINGER_MODE_SILENT;
153             break;
154         case AudioManagerNapi::RINGER_MODE_VIBRATE:
155             result = RINGER_MODE_VIBRATE;
156             break;
157         case AudioManagerNapi::RINGER_MODE_NORMAL:
158             result = RINGER_MODE_NORMAL;
159             break;
160         default:
161             result = RINGER_MODE_NORMAL;
162             HiLog::Error(LABEL, "Unknown ringer mode requested by JS, Set it to default RINGER_MODE_NORMAL!");
163             break;
164     }
165 
166     return result;
167 }
168 
GetJsAudioRingMode(int32_t ringerMode)169 static AudioManagerNapi::AudioRingMode GetJsAudioRingMode(int32_t ringerMode)
170 {
171     AudioManagerNapi::AudioRingMode result = AudioManagerNapi::RINGER_MODE_NORMAL;
172 
173     switch (ringerMode) {
174         case RINGER_MODE_SILENT:
175             result = AudioManagerNapi::RINGER_MODE_SILENT;
176             break;
177         case RINGER_MODE_VIBRATE:
178             result = AudioManagerNapi::RINGER_MODE_VIBRATE;
179             break;
180         case RINGER_MODE_NORMAL:
181             result = AudioManagerNapi::RINGER_MODE_NORMAL;
182             break;
183         default:
184             result = AudioManagerNapi::RINGER_MODE_NORMAL;
185             HiLog::Error(LABEL, "Unknown ringer mode returned from native, Set it to default RINGER_MODE_NORMAL!");
186             break;
187     }
188 
189     return result;
190 }
191 
AddNamedProperty(napi_env env,napi_value object,const string name,int32_t enumValue)192 napi_status AudioManagerNapi::AddNamedProperty(napi_env env, napi_value object, const string name, int32_t enumValue)
193 {
194     napi_status status;
195     napi_value enumNapiValue;
196 
197     status = napi_create_int32(env, enumValue, &enumNapiValue);
198     if (status == napi_ok) {
199         status = napi_set_named_property(env, object, name.c_str(), enumNapiValue);
200     }
201 
202     return status;
203 }
204 
CreateDeviceChangeTypeObject(napi_env env)205 napi_value AudioManagerNapi::CreateDeviceChangeTypeObject(napi_env env)
206 {
207     napi_value result = nullptr;
208     napi_status status;
209     std::string propName;
210     int32_t refCount = 1;
211 
212     status = napi_create_object(env, &result);
213     if (status == napi_ok) {
214         for (auto &iter: deviceChangeTypeMap) {
215             propName = iter.first;
216             status = AddNamedProperty(env, result, propName, iter.second);
217             if (status != napi_ok) {
218                 HiLog::Error(LABEL, "Failed to add named prop in CreateDeviceChangeTypeObject!");
219                 break;
220             }
221             propName.clear();
222         }
223         if (status == napi_ok) {
224             status = napi_create_reference(env, result, refCount, &deviceChangeType_);
225             if (status == napi_ok) {
226                 return result;
227             }
228         }
229     }
230     HiLog::Error(LABEL, "CreateDeviceChangeTypeObject is Failed!");
231     napi_get_undefined(env, &result);
232 
233     return result;
234 }
235 
CreateInterruptTypeObject(napi_env env)236 napi_value AudioManagerNapi::CreateInterruptTypeObject(napi_env env)
237 {
238     napi_value result = nullptr;
239     napi_status status;
240     std::string propName;
241     int32_t refCount = 1;
242 
243     status = napi_create_object(env, &result);
244     if (status == napi_ok) {
245         for (auto &iter: interruptTypeMap) {
246             propName = iter.first;
247             status = AddNamedProperty(env, result, propName, iter.second);
248             if (status != napi_ok) {
249                 HiLog::Error(LABEL, "Failed to add named prop in CreateInterruptTypeObject!");
250                 break;
251             }
252             propName.clear();
253         }
254         if (status == napi_ok) {
255             status = napi_create_reference(env, result, refCount, &interruptType_);
256             if (status == napi_ok) {
257                 return result;
258             }
259         }
260     }
261     HiLog::Error(LABEL, "CreateInterruptTypeObject is Failed!");
262     napi_get_undefined(env, &result);
263 
264     return result;
265 }
266 
CreateInterruptActionTypeObject(napi_env env)267 napi_value AudioManagerNapi::CreateInterruptActionTypeObject(napi_env env)
268 {
269     napi_value result = nullptr;
270     napi_status status;
271     std::string propName;
272     int32_t refCount = 1;
273 
274     status = napi_create_object(env, &result);
275     if (status == napi_ok) {
276         for (auto &iter: interruptActionTypeMap) {
277             propName = iter.first;
278             status = AddNamedProperty(env, result, propName, iter.second);
279             if (status != napi_ok) {
280                 HiLog::Error(LABEL, "Failed to add named prop in CreateInterruptActionTypeObject!");
281                 break;
282             }
283             propName.clear();
284         }
285         if (status == napi_ok) {
286             status = napi_create_reference(env, result, refCount, &interruptActionType_);
287             if (status == napi_ok) {
288                 return result;
289             }
290         }
291     }
292     HiLog::Error(LABEL, "CreateInterruptActionTypeObject is Failed!");
293     napi_get_undefined(env, &result);
294 
295     return result;
296 }
297 
CreateInterruptHintObject(napi_env env)298 napi_value AudioManagerNapi::CreateInterruptHintObject(napi_env env)
299 {
300     napi_value result = nullptr;
301     napi_status status;
302     std::string propName;
303     int32_t refCount = 1;
304 
305     status = napi_create_object(env, &result);
306     if (status == napi_ok) {
307         for (auto &iter: interruptHintMap) {
308             propName = iter.first;
309             status = AddNamedProperty(env, result, propName, iter.second);
310             if (status != napi_ok) {
311                 HiLog::Error(LABEL, "Failed to add named prop in CreateInterruptTypeObject!");
312                 break;
313             }
314             propName.clear();
315         }
316         if (status == napi_ok) {
317             status = napi_create_reference(env, result, refCount, &interruptHint_);
318             if (status == napi_ok) {
319                 return result;
320             }
321         }
322     }
323     HiLog::Error(LABEL, "CreateInterruptHintObject is Failed!");
324     napi_get_undefined(env, &result);
325 
326     return result;
327 }
328 
CreateAudioSceneObject(napi_env env)329 napi_value AudioManagerNapi::CreateAudioSceneObject(napi_env env)
330 {
331     napi_value result = nullptr;
332     napi_status status;
333     std::string propName;
334     int32_t refCount = 1;
335 
336     status = napi_create_object(env, &result);
337     if (status == napi_ok) {
338         for (auto &iter: audioSceneMap) {
339             propName = iter.first;
340             status = AddNamedProperty(env, result, propName, iter.second);
341             if (status != napi_ok) {
342                 HiLog::Error(LABEL, "Failed to add named prop in CreateAudioSceneObject!");
343                 break;
344             }
345             propName.clear();
346         }
347         if (status == napi_ok) {
348             status = napi_create_reference(env, result, refCount, &audioScene_);
349             if (status == napi_ok) {
350                 return result;
351             }
352         }
353     }
354     HiLog::Error(LABEL, "CreateAudioSceneObject is Failed!");
355     napi_get_undefined(env, &result);
356 
357     return result;
358 }
359 
CreateAudioVolumeTypeObject(napi_env env)360 napi_value AudioManagerNapi::CreateAudioVolumeTypeObject(napi_env env)
361 {
362     napi_value result = nullptr;
363     napi_status status;
364     int32_t refCount = 1;
365     string propName;
366 
367     status = napi_create_object(env, &result);
368     if (status == napi_ok) {
369         for (int i = AudioManagerNapi::VOLUMETYPE_DEFAULT + 1; i < AudioManagerNapi::VOLUMETYPE_MAX; i++) {
370             switch (i) {
371                 case AudioManagerNapi::RINGTONE:
372                     propName = "RINGTONE";
373                     break;
374                 case AudioManagerNapi::MEDIA:
375                     propName = "MEDIA";
376                     break;
377                 case AudioManagerNapi::VOICE_CALL:
378                     propName = "VOICE_CALL";
379                     break;
380                 case AudioManagerNapi::VOICE_ASSISTANT:
381                     propName = "VOICE_ASSISTANT";
382                     break;
383                 default:
384                     HiLog::Error(LABEL, "CreateAudioVolumeTypeObject: No prob with this value try next value!");
385                     continue;
386             }
387             status = AddNamedProperty(env, result, propName, i);
388             if (status != napi_ok) {
389                 HiLog::Error(LABEL, "Failed to add named prop!");
390                 break;
391             }
392             propName.clear();
393         }
394         if (status == napi_ok) {
395             status = napi_create_reference(env, result, refCount, &audioVolumeTypeRef_);
396             if (status == napi_ok) {
397                 return result;
398             }
399         }
400     }
401     HiLog::Error(LABEL, "CreateAudioVolumeTypeObject is Failed!");
402     napi_get_undefined(env, &result);
403 
404     return result;
405 }
406 
CreateDeviceFlagObject(napi_env env)407 napi_value AudioManagerNapi::CreateDeviceFlagObject(napi_env env)
408 {
409     napi_value result = nullptr;
410     napi_status status;
411     int32_t refCount = 1;
412     string propName;
413 
414     status = napi_create_object(env, &result);
415     if (status == napi_ok) {
416         for (int i = DEVICE_FLAG_NONE + 1; i < DEVICE_FLAG_MAX; i++) {
417             switch (i) {
418                 case OUTPUT_DEVICES_FLAG:
419                     propName = "OUTPUT_DEVICES_FLAG";
420                     break;
421                 case INPUT_DEVICES_FLAG:
422                     propName = "INPUT_DEVICES_FLAG";
423                     break;
424                 case ALL_DEVICES_FLAG:
425                     propName = "ALL_DEVICES_FLAG";
426                     break;
427                 default:
428                     HiLog::Error(LABEL, "CreateDeviceFlagObject: No prob with this value try next value!");
429                     continue;
430             }
431             status = AddNamedProperty(env, result, propName, i);
432             if (status != napi_ok) {
433                 HiLog::Error(LABEL, "Failed to add named prop!");
434                 break;
435             }
436             propName.clear();
437         }
438         if (status == napi_ok) {
439             status = napi_create_reference(env, result, refCount, &deviceFlagRef_);
440             if (status == napi_ok) {
441                 return result;
442             }
443         }
444     }
445     HiLog::Error(LABEL, "CreateDeviceFlagObject is Failed!");
446     napi_get_undefined(env, &result);
447 
448     return result;
449 }
450 
CreateDeviceRoleObject(napi_env env)451 napi_value AudioManagerNapi::CreateDeviceRoleObject(napi_env env)
452 {
453     napi_value result = nullptr;
454     napi_status status;
455     int32_t refCount = 1;
456     string propName;
457 
458     status = napi_create_object(env, &result);
459     if (status == napi_ok) {
460         for (int i = DEVICE_ROLE_NONE + 1; i < DEVICE_ROLE_MAX; i++) {
461             switch (i) {
462                 case INPUT_DEVICE:
463                     propName = "INPUT_DEVICE";
464                     break;
465                 case OUTPUT_DEVICE:
466                     propName = "OUTPUT_DEVICE";
467                     break;
468                 default:
469                     HiLog::Error(LABEL, "CreateDeviceRoleObject: No prob with this value try next value!");
470                     continue;
471             }
472             status = AddNamedProperty(env, result, propName, i);
473             if (status != napi_ok) {
474                 HiLog::Error(LABEL, "Failed to add named prop!");
475                 break;
476             }
477             propName.clear();
478         }
479         if (status == napi_ok) {
480             status = napi_create_reference(env, result, refCount, &deviceRoleRef_);
481             if (status == napi_ok) {
482                 return result;
483             }
484         }
485     }
486     HiLog::Error(LABEL, "CreateDeviceRoleObject is Failed!");
487     napi_get_undefined(env, &result);
488 
489     return result;
490 }
491 
CreateDeviceTypeObject(napi_env env)492 napi_value AudioManagerNapi::CreateDeviceTypeObject(napi_env env)
493 {
494     napi_value result = nullptr;
495     napi_status status;
496     int32_t refCount = 1;
497     string propName;
498 
499     status = napi_create_object(env, &result);
500     if (status == napi_ok) {
501         for (int i = DEVICE_TYPE_NONE + 1; i < DEVICE_TYPE_MAX; i++) {
502             switch (i) {
503                 case DEVICE_TYPE_INVALID:
504                     propName = "INVALID";
505                     break;
506                 case DEVICE_TYPE_SPEAKER:
507                     propName = "SPEAKER";
508                     break;
509                 case DEVICE_TYPE_WIRED_HEADSET:
510                     propName = "WIRED_HEADSET";
511                     break;
512                 case DEVICE_TYPE_WIRED_HEADPHONES:
513                     propName = "WIRED_HEADPHONES";
514                     break;
515                 case DEVICE_TYPE_BLUETOOTH_SCO:
516                     propName = "BLUETOOTH_SCO";
517                     break;
518                 case DEVICE_TYPE_BLUETOOTH_A2DP:
519                     propName = "BLUETOOTH_A2DP";
520                     break;
521                 case DEVICE_TYPE_MIC:
522                     propName = "MIC";
523                     break;
524                 case DEVICE_TYPE_USB_HEADSET:
525                     propName = "USB_HEADSET";
526                     break;
527                 default:
528                     HiLog::Error(LABEL, "CreateDeviceTypeObject: No prob with this value try next value!");
529                     continue;
530             }
531             status = AddNamedProperty(env, result, propName, i);
532             if (status != napi_ok) {
533                 HiLog::Error(LABEL, "Failed to add named prop!");
534                 break;
535             }
536             propName.clear();
537         }
538         if (status == napi_ok) {
539             status = napi_create_reference(env, result, refCount, &deviceTypeRef_);
540             if (status == napi_ok) {
541                 return result;
542             }
543         }
544     }
545     HiLog::Error(LABEL, "CreateDeviceTypeObject is Failed!");
546     napi_get_undefined(env, &result);
547 
548     return result;
549 }
550 
CreateActiveDeviceTypeObject(napi_env env)551 napi_value AudioManagerNapi::CreateActiveDeviceTypeObject(napi_env env)
552 {
553     napi_value result = nullptr;
554     napi_status status;
555     int32_t refCount = 1;
556     string propName;
557 
558     status = napi_create_object(env, &result);
559     if (status == napi_ok) {
560         for (int i = ACTIVE_DEVICE_TYPE_NONE + 1; i < ACTIVE_DEVICE_TYPE_MAX; i++) {
561             switch (i) {
562                 case SPEAKER:
563                     propName = "SPEAKER";
564                     break;
565                 case BLUETOOTH_SCO:
566                     propName = "BLUETOOTH_SCO";
567                     break;
568                 default:
569                     HiLog::Error(LABEL, "CreateActiveDeviceTypeObject: No prob with this value try next value!");
570                     continue;
571             }
572             status = AddNamedProperty(env, result, propName, i);
573             if (status != napi_ok) {
574                 HiLog::Error(LABEL, "Failed to add named prop!");
575                 break;
576             }
577             propName.clear();
578         }
579         if (status == napi_ok) {
580             status = napi_create_reference(env, result, refCount, &activeDeviceTypeRef_);
581             if (status == napi_ok) {
582                 return result;
583             }
584         }
585     }
586     HiLog::Error(LABEL, "CreateActiveDeviceTypeObject is Failed!");
587     napi_get_undefined(env, &result);
588 
589     return result;
590 }
591 
CreateAudioRingModeObject(napi_env env)592 napi_value AudioManagerNapi::CreateAudioRingModeObject(napi_env env)
593 {
594     napi_value result = nullptr;
595     napi_status status;
596     int32_t refCount = 1;
597     string propName;
598 
599     status = napi_create_object(env, &result);
600     if (status == napi_ok) {
601         for (int i = AudioManagerNapi::RINGER_MODE_SILENT; i <= AudioManagerNapi::RINGER_MODE_NORMAL; i++) {
602             switch (i) {
603                 case AudioManagerNapi::RINGER_MODE_SILENT:
604                     propName = "RINGER_MODE_SILENT";
605                     break;
606                 case AudioManagerNapi::RINGER_MODE_VIBRATE:
607                     propName = "RINGER_MODE_VIBRATE";
608                     break;
609                 case AudioManagerNapi::RINGER_MODE_NORMAL:
610                     propName = "RINGER_MODE_NORMAL";
611                     break;
612                 default:
613                     HiLog::Error(LABEL, "CreateAudioRingModeObject: No prob with this value try next value!");
614                     continue;
615             }
616             status = AddNamedProperty(env, result, propName, i);
617             if (status != napi_ok) {
618                 HiLog::Error(LABEL, "Failed to add named prop!");
619                 break;
620             }
621             propName.clear();
622         }
623         if (status == napi_ok) {
624             status = napi_create_reference(env, result, refCount, &audioRingModeRef_);
625             if (status == napi_ok) {
626                 return result;
627             }
628         }
629     }
630     HiLog::Error(LABEL, "CreateAudioRingModeObject is Failed!");
631     napi_get_undefined(env, &result);
632 
633     return result;
634 }
635 
Init(napi_env env,napi_value exports)636 napi_value AudioManagerNapi::Init(napi_env env, napi_value exports)
637 {
638     napi_status status;
639     napi_value constructor;
640     napi_value result = nullptr;
641     const int32_t refCount = 1;
642 
643     napi_property_descriptor audio_svc_mngr_properties[] = {
644         DECLARE_NAPI_FUNCTION("setVolume", SetVolume),
645         DECLARE_NAPI_FUNCTION("getVolume", GetVolume),
646         DECLARE_NAPI_FUNCTION("getMaxVolume", GetMaxVolume),
647         DECLARE_NAPI_FUNCTION("getMinVolume", GetMinVolume),
648         DECLARE_NAPI_FUNCTION("getDevices", GetDevices),
649         DECLARE_NAPI_FUNCTION("mute", SetStreamMute),
650         DECLARE_NAPI_FUNCTION("isMute", IsStreamMute),
651         DECLARE_NAPI_FUNCTION("isActive", IsStreamActive),
652         DECLARE_NAPI_FUNCTION("setRingerMode", SetRingerMode),
653         DECLARE_NAPI_FUNCTION("getRingerMode", GetRingerMode),
654         DECLARE_NAPI_FUNCTION("setDeviceActive", SetDeviceActive),
655         DECLARE_NAPI_FUNCTION("isDeviceActive", IsDeviceActive),
656         DECLARE_NAPI_FUNCTION("setAudioParameter", SetAudioParameter),
657         DECLARE_NAPI_FUNCTION("getAudioParameter", GetAudioParameter),
658         DECLARE_NAPI_FUNCTION("setMicrophoneMute", SetMicrophoneMute),
659         DECLARE_NAPI_FUNCTION("isMicrophoneMute", IsMicrophoneMute),
660         DECLARE_NAPI_FUNCTION("setAudioScene", SetAudioScene),
661         DECLARE_NAPI_FUNCTION("getAudioScene", GetAudioScene),
662         DECLARE_NAPI_FUNCTION("on", On),
663         DECLARE_NAPI_FUNCTION("off", Off),
664     };
665 
666     napi_property_descriptor static_prop[] = {
667         DECLARE_NAPI_STATIC_FUNCTION("getAudioManager", GetAudioManager),
668         DECLARE_NAPI_PROPERTY("AudioVolumeType", CreateAudioVolumeTypeObject(env)),
669         DECLARE_NAPI_PROPERTY("DeviceFlag", CreateDeviceFlagObject(env)),
670         DECLARE_NAPI_PROPERTY("DeviceRole", CreateDeviceRoleObject(env)),
671         DECLARE_NAPI_PROPERTY("DeviceType", CreateDeviceTypeObject(env)),
672         DECLARE_NAPI_PROPERTY("ActiveDeviceType", CreateActiveDeviceTypeObject(env)),
673         DECLARE_NAPI_PROPERTY("AudioRingMode", CreateAudioRingModeObject(env)),
674         DECLARE_NAPI_PROPERTY("AudioScene", CreateAudioSceneObject(env)),
675         DECLARE_NAPI_PROPERTY("DeviceChangeType", CreateDeviceChangeTypeObject(env)),
676         DECLARE_NAPI_PROPERTY("InterruptActionType", CreateInterruptActionTypeObject(env)),
677         DECLARE_NAPI_PROPERTY("InterruptHint", CreateInterruptHintObject(env)),
678         DECLARE_NAPI_PROPERTY("InterruptType", CreateInterruptTypeObject(env))
679     };
680 
681     status = napi_define_class(env, AUDIO_MNGR_NAPI_CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Construct, nullptr,
682         sizeof(audio_svc_mngr_properties) / sizeof(audio_svc_mngr_properties[PARAM0]),
683         audio_svc_mngr_properties, &constructor);
684     if (status == napi_ok) {
685         status = napi_create_reference(env, constructor, refCount, &g_managerConstructor);
686         if (status == napi_ok) {
687             status = napi_set_named_property(env, exports, AUDIO_MNGR_NAPI_CLASS_NAME.c_str(), constructor);
688             if (status == napi_ok) {
689                 status = napi_define_properties(env, exports,
690                                                 sizeof(static_prop) / sizeof(static_prop[PARAM0]), static_prop);
691                 if (status == napi_ok) {
692                     return exports;
693                 }
694             }
695         }
696     }
697     HiLog::Error(LABEL, "Failure in AudioManagerNapi::Init()");
698     napi_get_undefined(env, &result);
699 
700     return result;
701 }
702 
703 // Constructor callback
Construct(napi_env env,napi_callback_info info)704 napi_value AudioManagerNapi::Construct(napi_env env, napi_callback_info info)
705 {
706     napi_status status;
707     napi_value jsThis;
708     napi_value undefinedResult = nullptr;
709     napi_get_undefined(env, &undefinedResult);
710     size_t argCount = 0;
711     int32_t ret = 0;
712 
713     status = napi_get_cb_info(env, info, &argCount, nullptr, &jsThis, nullptr);
714     if (status == napi_ok) {
715         unique_ptr<AudioManagerNapi> managerNapi = make_unique<AudioManagerNapi>();
716         if (managerNapi != nullptr) {
717             managerNapi->env_ = env;
718             managerNapi->audioMngr_ = AudioSystemManager::GetInstance();
719             managerNapi->cachedClientId = getpid();
720 
721             managerNapi->volumeKeyEventCallbackNapi_ = std::make_shared<AudioVolumeKeyEventNapi>(env);
722             ret = managerNapi->audioMngr_->RegisterVolumeKeyEventCallback(managerNapi->cachedClientId,
723                                                                           managerNapi->volumeKeyEventCallbackNapi_);
724             if (ret) {
725                 MEDIA_ERR_LOG("AudioManagerNapi: RegisterVolumeKeyEventCallback Failed");
726             } else {
727                 MEDIA_DEBUG_LOG("AudioManagerNapi: RegisterVolumeKeyEventCallback Success");
728             }
729 
730             status = napi_wrap(env, jsThis, static_cast<void*>(managerNapi.get()),
731                                AudioManagerNapi::Destructor, nullptr, &(managerNapi->wrapper_));
732             if (status == napi_ok) {
733                 managerNapi.release();
734                 return jsThis;
735             }
736         }
737     }
738     HiLog::Error(LABEL, "Failed in AudioManagerNapi::Construct()!");
739 
740     return undefinedResult;
741 }
742 
CreateAudioManagerWrapper(napi_env env)743 napi_value AudioManagerNapi::CreateAudioManagerWrapper(napi_env env)
744 {
745     napi_status status;
746     napi_value result = nullptr;
747     napi_value constructor;
748 
749     status = napi_get_reference_value(env, g_managerConstructor, &constructor);
750     if (status == napi_ok) {
751         status = napi_new_instance(env, constructor, 0, nullptr, &result);
752         if (status == napi_ok) {
753             return result;
754         }
755     }
756     HiLog::Error(LABEL, "Failed in AudioManagerNapi::CreateaudioMngrWrapper!");
757     napi_get_undefined(env, &result);
758 
759     return result;
760 }
761 
GetAudioManager(napi_env env,napi_callback_info info)762 napi_value AudioManagerNapi::GetAudioManager(napi_env env, napi_callback_info info)
763 {
764     napi_status status;
765     size_t argCount = 0;
766 
767     status = napi_get_cb_info(env, info, &argCount, nullptr, nullptr, nullptr);
768     if (status != napi_ok || argCount != 0) {
769         HiLog::Error(LABEL, "Invalid arguments!");
770         return nullptr;
771     }
772 
773     return AudioManagerNapi::CreateAudioManagerWrapper(env);
774 }
775 
CommonCallbackRoutine(napi_env env,AudioManagerAsyncContext * & asyncContext,const napi_value & valueParam)776 static void CommonCallbackRoutine(napi_env env, AudioManagerAsyncContext* &asyncContext, const napi_value &valueParam)
777 {
778     napi_value result[ARGS_TWO] = {0};
779     napi_value retVal;
780 
781     if (!asyncContext->status) {
782         napi_get_undefined(env, &result[PARAM0]);
783         result[PARAM1] = valueParam;
784     } else {
785         napi_value message = nullptr;
786         napi_create_string_utf8(env, "Error, Operation not supported or Failed", NAPI_AUTO_LENGTH, &message);
787         napi_create_error(env, nullptr, message, &result[PARAM0]);
788         napi_get_undefined(env, &result[PARAM1]);
789     }
790 
791     if (asyncContext->deferred) {
792         if (!asyncContext->status) {
793             napi_resolve_deferred(env, asyncContext->deferred, result[PARAM1]);
794         } else {
795             napi_reject_deferred(env, asyncContext->deferred, result[PARAM0]);
796         }
797     } else {
798         napi_value callback = nullptr;
799         napi_get_reference_value(env, asyncContext->callbackRef, &callback);
800         napi_call_function(env, nullptr, callback, ARGS_TWO, result, &retVal);
801         napi_delete_reference(env, asyncContext->callbackRef);
802     }
803     napi_delete_async_work(env, asyncContext->work);
804 
805     delete asyncContext;
806     asyncContext = nullptr;
807 }
808 
SetFunctionAsyncCallbackComplete(napi_env env,napi_status status,void * data)809 static void SetFunctionAsyncCallbackComplete(napi_env env, napi_status status, void *data)
810 {
811     auto asyncContext = static_cast<AudioManagerAsyncContext*>(data);
812     napi_value valueParam = nullptr;
813 
814     if (asyncContext != nullptr) {
815         if (!asyncContext->status) {
816             napi_get_undefined(env, &valueParam);
817         }
818         CommonCallbackRoutine(env, asyncContext, valueParam);
819     } else {
820         HiLog::Error(LABEL, "ERROR: AudioManagerAsyncContext* is Null!");
821     }
822 }
823 
IsTrueAsyncCallbackComplete(napi_env env,napi_status status,void * data)824 static void IsTrueAsyncCallbackComplete(napi_env env, napi_status status, void *data)
825 {
826     auto asyncContext = static_cast<AudioManagerAsyncContext*>(data);
827     napi_value valueParam = nullptr;
828 
829     if (asyncContext != nullptr) {
830         if (!asyncContext->status) {
831             napi_get_boolean(env, asyncContext->isTrue, &valueParam);
832         }
833         CommonCallbackRoutine(env, asyncContext, valueParam);
834     } else {
835         HiLog::Error(LABEL, "ERROR: AudioManagerAsyncContext* is Null!");
836     }
837 }
838 
GetStringValueAsyncCallbackComplete(napi_env env,napi_status status,void * data)839 static void GetStringValueAsyncCallbackComplete(napi_env env, napi_status status, void *data)
840 {
841     auto asyncContext = static_cast<AudioManagerAsyncContext*>(data);
842     napi_value valueParam = nullptr;
843 
844     if (asyncContext != nullptr) {
845         if (!asyncContext->status) {
846             napi_create_string_utf8(env, asyncContext->valueStr.c_str(), NAPI_AUTO_LENGTH, &valueParam);
847         }
848         CommonCallbackRoutine(env, asyncContext, valueParam);
849     } else {
850         HiLog::Error(LABEL, "ERROR: AudioManagerAsyncContext* is Null!");
851     }
852 }
853 
GetIntValueAsyncCallbackComplete(napi_env env,napi_status status,void * data)854 static void GetIntValueAsyncCallbackComplete(napi_env env, napi_status status, void *data)
855 {
856     auto asyncContext = static_cast<AudioManagerAsyncContext*>(data);
857     napi_value valueParam = nullptr;
858 
859     if (asyncContext != nullptr) {
860         if (!asyncContext->status) {
861             napi_create_int32(env, asyncContext->intValue, &valueParam);
862         }
863         CommonCallbackRoutine(env, asyncContext, valueParam);
864     } else {
865         HiLog::Error(LABEL, "ERROR: AudioManagerAsyncContext* is Null!");
866     }
867 }
868 
SetMicrophoneMute(napi_env env,napi_callback_info info)869 napi_value AudioManagerNapi::SetMicrophoneMute(napi_env env, napi_callback_info info)
870 {
871     napi_status status;
872     const int32_t refCount = 1;
873     napi_value result = nullptr;
874 
875     GET_PARAMS(env, info, ARGS_TWO);
876     NAPI_ASSERT(env, argc >= ARGS_ONE, "requires 1 parameter minimum");
877 
878     unique_ptr<AudioManagerAsyncContext> asyncContext = make_unique<AudioManagerAsyncContext>();
879 
880     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
881     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
882         for (size_t i = PARAM0; i < argc; i++) {
883             napi_valuetype valueType = napi_undefined;
884             napi_typeof(env, argv[i], &valueType);
885 
886             if (i == PARAM0 && valueType == napi_boolean) {
887                 napi_get_value_bool(env, argv[i], &asyncContext->isMute);
888             } else if (i == PARAM1 && valueType == napi_function) {
889                 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
890                 break;
891             } else {
892                 NAPI_ASSERT(env, false, "type mismatch");
893             }
894         }
895 
896         if (asyncContext->callbackRef == nullptr) {
897             napi_create_promise(env, &asyncContext->deferred, &result);
898         } else {
899             napi_get_undefined(env, &result);
900         }
901 
902         napi_value resource = nullptr;
903         napi_create_string_utf8(env, "SetMicrophoneMute", NAPI_AUTO_LENGTH, &resource);
904 
905         status = napi_create_async_work(
906             env, nullptr, resource,
907             [](napi_env env, void *data) {
908                 auto context = static_cast<AudioManagerAsyncContext*>(data);
909                 context->status = context->objectInfo->audioMngr_->SetMicrophoneMute(context->isMute);
910             },
911             SetFunctionAsyncCallbackComplete, static_cast<void*>(asyncContext.get()), &asyncContext->work);
912         if (status != napi_ok) {
913             result = nullptr;
914         } else {
915             status = napi_queue_async_work(env, asyncContext->work);
916             if (status == napi_ok) {
917                 asyncContext.release();
918             } else {
919                 result = nullptr;
920             }
921         }
922     }
923 
924     return result;
925 }
926 
IsMicrophoneMute(napi_env env,napi_callback_info info)927 napi_value AudioManagerNapi::IsMicrophoneMute(napi_env env, napi_callback_info info)
928 {
929     napi_status status;
930     const int32_t refCount = 1;
931     napi_value result = nullptr;
932 
933     GET_PARAMS(env, info, ARGS_ONE);
934 
935     unique_ptr<AudioManagerAsyncContext> asyncContext = make_unique<AudioManagerAsyncContext>();
936 
937     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
938     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
939         for (size_t i = PARAM0; i < argc; i++) {
940             napi_valuetype valueType = napi_undefined;
941             napi_typeof(env, argv[i], &valueType);
942 
943             if (i == PARAM0 && valueType == napi_function) {
944                 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
945                 break;
946             } else {
947                 NAPI_ASSERT(env, false, "type mismatch");
948             }
949         }
950 
951         if (asyncContext->callbackRef == nullptr) {
952             napi_create_promise(env, &asyncContext->deferred, &result);
953         } else {
954             napi_get_undefined(env, &result);
955         }
956 
957         napi_value resource = nullptr;
958         napi_create_string_utf8(env, "IsMicrophoneMute", NAPI_AUTO_LENGTH, &resource);
959 
960         status = napi_create_async_work(
961             env, nullptr, resource,
962             [](napi_env env, void *data) {
963                 auto context = static_cast<AudioManagerAsyncContext*>(data);
964                 context->isMute = context->objectInfo->audioMngr_->IsMicrophoneMute();
965                 context->isTrue = context->isMute;
966             },
967             IsTrueAsyncCallbackComplete, static_cast<void*>(asyncContext.get()), &asyncContext->work);
968         if (status != napi_ok) {
969             result = nullptr;
970         } else {
971             status = napi_queue_async_work(env, asyncContext->work);
972             if (status == napi_ok) {
973                 asyncContext.release();
974             } else {
975                 result = nullptr;
976             }
977         }
978     }
979 
980     return result;
981 }
982 
SetRingerMode(napi_env env,napi_callback_info info)983 napi_value AudioManagerNapi::SetRingerMode(napi_env env, napi_callback_info info)
984 {
985     napi_status status;
986     const int32_t refCount = 1;
987     napi_value result = nullptr;
988 
989     GET_PARAMS(env, info, ARGS_TWO);
990     NAPI_ASSERT(env, argc >= ARGS_ONE, "requires 1 parameter minimum");
991 
992     unique_ptr<AudioManagerAsyncContext> asyncContext = make_unique<AudioManagerAsyncContext>();
993 
994     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
995     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
996         for (size_t i = PARAM0; i < argc; i++) {
997             napi_valuetype valueType = napi_undefined;
998             napi_typeof(env, argv[i], &valueType);
999 
1000             if (i == PARAM0 && valueType == napi_number) {
1001                 napi_get_value_int32(env, argv[i], &asyncContext->ringMode);
1002             } else if (i == PARAM1 && valueType == napi_function) {
1003                 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
1004                 break;
1005             } else {
1006                 NAPI_ASSERT(env, false, "type mismatch");
1007             }
1008         }
1009 
1010         if (asyncContext->callbackRef == nullptr) {
1011             napi_create_promise(env, &asyncContext->deferred, &result);
1012         } else {
1013             napi_get_undefined(env, &result);
1014         }
1015 
1016         napi_value resource = nullptr;
1017         napi_create_string_utf8(env, "SetRingerMode", NAPI_AUTO_LENGTH, &resource);
1018 
1019         status = napi_create_async_work(
1020             env, nullptr, resource,
1021             [](napi_env env, void *data) {
1022                 auto context = static_cast<AudioManagerAsyncContext*>(data);
1023                 context->status =
1024                     context->objectInfo->audioMngr_->SetRingerMode(GetNativeAudioRingerMode(context->ringMode));
1025             },
1026             SetFunctionAsyncCallbackComplete, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1027         if (status != napi_ok) {
1028             result = nullptr;
1029         } else {
1030             status = napi_queue_async_work(env, asyncContext->work);
1031             if (status == napi_ok) {
1032                 asyncContext.release();
1033             } else {
1034                 result = nullptr;
1035             }
1036         }
1037     }
1038 
1039     return result;
1040 }
1041 
GetRingerMode(napi_env env,napi_callback_info info)1042 napi_value AudioManagerNapi::GetRingerMode(napi_env env, napi_callback_info info)
1043 {
1044     napi_status status;
1045     const int32_t refCount = 1;
1046     napi_value result = nullptr;
1047 
1048     GET_PARAMS(env, info, ARGS_ONE);
1049 
1050     unique_ptr<AudioManagerAsyncContext> asyncContext = make_unique<AudioManagerAsyncContext>();
1051 
1052     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1053     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1054         for (size_t i = PARAM0; i < argc; i++) {
1055             napi_valuetype valueType = napi_undefined;
1056             napi_typeof(env, argv[i], &valueType);
1057 
1058             if (i == PARAM0 && valueType == napi_function) {
1059                 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
1060                 break;
1061             } else {
1062                 NAPI_ASSERT(env, false, "type mismatch");
1063             }
1064         }
1065 
1066         if (asyncContext->callbackRef == nullptr) {
1067             napi_create_promise(env, &asyncContext->deferred, &result);
1068         } else {
1069             napi_get_undefined(env, &result);
1070         }
1071 
1072         napi_value resource = nullptr;
1073         napi_create_string_utf8(env, "GetRingerMode", NAPI_AUTO_LENGTH, &resource);
1074 
1075         status = napi_create_async_work(
1076             env, nullptr, resource,
1077             [](napi_env env, void *data) {
1078                 auto context = static_cast<AudioManagerAsyncContext*>(data);
1079                 context->ringMode = GetJsAudioRingMode(context->objectInfo->audioMngr_->GetRingerMode());
1080                 context->intValue = context->ringMode;
1081                 context->status = 0;
1082             },
1083             GetIntValueAsyncCallbackComplete, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1084         if (status != napi_ok) {
1085             result = nullptr;
1086         } else {
1087             status = napi_queue_async_work(env, asyncContext->work);
1088             if (status == napi_ok) {
1089                 asyncContext.release();
1090             } else {
1091                 result = nullptr;
1092             }
1093         }
1094     }
1095 
1096     return result;
1097 }
1098 
SetAudioScene(napi_env env,napi_callback_info info)1099 napi_value AudioManagerNapi::SetAudioScene(napi_env env, napi_callback_info info)
1100 {
1101     HiLog::Error(LABEL, "Enter SetAudioScene!");
1102     napi_status status;
1103     const int32_t refCount = 1;
1104     napi_value result = nullptr;
1105 
1106     GET_PARAMS(env, info, ARGS_TWO);
1107     NAPI_ASSERT(env, argc >= ARGS_ONE, "requires 1 parameter minimum");
1108 
1109     unique_ptr<AudioManagerAsyncContext> asyncContext = make_unique<AudioManagerAsyncContext>();
1110 
1111     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1112     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1113         for (size_t i = PARAM0; i < argc; i++) {
1114             napi_valuetype valueType = napi_undefined;
1115             napi_typeof(env, argv[i], &valueType);
1116 
1117             if (i == PARAM0 && valueType == napi_number) {
1118                 napi_get_value_int32(env, argv[i], &asyncContext->scene);
1119             } else if (i == PARAM1 && valueType == napi_function) {
1120                 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
1121                 break;
1122             } else {
1123                 NAPI_ASSERT(env, false, "type mismatch");
1124             }
1125         }
1126 
1127         if (asyncContext->callbackRef == nullptr) {
1128             napi_create_promise(env, &asyncContext->deferred, &result);
1129         } else {
1130             napi_get_undefined(env, &result);
1131         }
1132 
1133         napi_value resource = nullptr;
1134         napi_create_string_utf8(env, "SetAudioScene", NAPI_AUTO_LENGTH, &resource);
1135 
1136         status = napi_create_async_work(
1137             env, nullptr, resource,
1138             [](napi_env env, void *data) {
1139                 auto context = static_cast<AudioManagerAsyncContext*>(data);
1140                 if ((context->scene < AUDIO_SCENE_DEFAULT) || (context->scene > AUDIO_SCENE_PHONE_CHAT)) {
1141                     context->status = ERROR;
1142                 } else {
1143                     context->status =
1144                         context->objectInfo->audioMngr_->SetAudioScene(static_cast<AudioScene>(context->scene));
1145                 }
1146             },
1147             SetFunctionAsyncCallbackComplete, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1148         if (status != napi_ok) {
1149             result = nullptr;
1150         } else {
1151             status = napi_queue_async_work(env, asyncContext->work);
1152             if (status == napi_ok) {
1153                 asyncContext.release();
1154             } else {
1155                 result = nullptr;
1156             }
1157         }
1158     }
1159 
1160     return result;
1161 }
1162 
GetAudioScene(napi_env env,napi_callback_info info)1163 napi_value AudioManagerNapi::GetAudioScene(napi_env env, napi_callback_info info)
1164 {
1165     napi_status status;
1166     const int32_t refCount = 1;
1167     napi_value result = nullptr;
1168 
1169     GET_PARAMS(env, info, ARGS_ONE);
1170 
1171     unique_ptr<AudioManagerAsyncContext> asyncContext = make_unique<AudioManagerAsyncContext>();
1172 
1173     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1174     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1175         for (size_t i = PARAM0; i < argc; i++) {
1176             napi_valuetype valueType = napi_undefined;
1177             napi_typeof(env, argv[i], &valueType);
1178 
1179             if (i == PARAM0 && valueType == napi_function) {
1180                 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
1181                 break;
1182             } else {
1183                 NAPI_ASSERT(env, false, "type mismatch");
1184             }
1185         }
1186 
1187         if (asyncContext->callbackRef == nullptr) {
1188             napi_create_promise(env, &asyncContext->deferred, &result);
1189         } else {
1190             napi_get_undefined(env, &result);
1191         }
1192 
1193         napi_value resource = nullptr;
1194         napi_create_string_utf8(env, "GetAudioScene", NAPI_AUTO_LENGTH, &resource);
1195 
1196         status = napi_create_async_work(
1197             env, nullptr, resource,
1198             [](napi_env env, void *data) {
1199                 auto context = static_cast<AudioManagerAsyncContext*>(data);
1200                 context->intValue = context->objectInfo->audioMngr_->GetAudioScene();
1201                 context->status = 0;
1202             },
1203             GetIntValueAsyncCallbackComplete, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1204         if (status != napi_ok) {
1205             result = nullptr;
1206         } else {
1207             status = napi_queue_async_work(env, asyncContext->work);
1208             if (status == napi_ok) {
1209                 asyncContext.release();
1210             } else {
1211                 result = nullptr;
1212             }
1213         }
1214     }
1215 
1216     return result;
1217 }
1218 
SetStreamMute(napi_env env,napi_callback_info info)1219 napi_value AudioManagerNapi::SetStreamMute(napi_env env, napi_callback_info info)
1220 {
1221     napi_status status;
1222     const int32_t refCount = 1;
1223     napi_value result = nullptr;
1224 
1225     GET_PARAMS(env, info, ARGS_THREE);
1226     NAPI_ASSERT(env, argc >= ARGS_TWO, "requires 2 parameters minimum");
1227 
1228     unique_ptr<AudioManagerAsyncContext> asyncContext = make_unique<AudioManagerAsyncContext>();
1229 
1230     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1231     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1232         for (size_t i = PARAM0; i < argc; i++) {
1233             napi_valuetype valueType = napi_undefined;
1234             napi_typeof(env, argv[i], &valueType);
1235 
1236             if (i == PARAM0 && valueType == napi_number) {
1237                 napi_get_value_int32(env, argv[i], &asyncContext->volType);
1238             } else if (i == PARAM1 && valueType == napi_boolean) {
1239                 napi_get_value_bool(env, argv[i], &asyncContext->isMute);
1240             } else if (i == PARAM2 && valueType == napi_function) {
1241                 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
1242                 break;
1243             } else {
1244                 NAPI_ASSERT(env, false, "type mismatch");
1245             }
1246         }
1247 
1248         if (asyncContext->callbackRef == nullptr) {
1249             napi_create_promise(env, &asyncContext->deferred, &result);
1250         } else {
1251             napi_get_undefined(env, &result);
1252         }
1253 
1254         napi_value resource = nullptr;
1255         napi_create_string_utf8(env, "SetStreamMute", NAPI_AUTO_LENGTH, &resource);
1256 
1257         status = napi_create_async_work(
1258             env, nullptr, resource,
1259             [](napi_env env, void *data) {
1260                 auto context = static_cast<AudioManagerAsyncContext*>(data);
1261                 context->status = context->objectInfo->audioMngr_->SetMute(GetNativeAudioVolumeType(context->volType),
1262                                                                            context->isMute);
1263             },
1264             SetFunctionAsyncCallbackComplete, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1265         if (status != napi_ok) {
1266             result = nullptr;
1267         } else {
1268             status = napi_queue_async_work(env, asyncContext->work);
1269             if (status == napi_ok) {
1270                 asyncContext.release();
1271             } else {
1272                 result = nullptr;
1273             }
1274         }
1275     }
1276 
1277     return result;
1278 }
1279 
IsStreamMute(napi_env env,napi_callback_info info)1280 napi_value AudioManagerNapi::IsStreamMute(napi_env env, napi_callback_info info)
1281 {
1282     napi_status status;
1283     const int32_t refCount = 1;
1284     napi_value result = nullptr;
1285 
1286     GET_PARAMS(env, info, ARGS_TWO);
1287     NAPI_ASSERT(env, argc >= ARGS_ONE, "requires 1 parameter minimum");
1288 
1289     unique_ptr<AudioManagerAsyncContext> asyncContext = make_unique<AudioManagerAsyncContext>();
1290 
1291     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1292     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1293         for (size_t i = PARAM0; i < argc; i++) {
1294             napi_valuetype valueType = napi_undefined;
1295             napi_typeof(env, argv[i], &valueType);
1296 
1297             if (i == PARAM0 && valueType == napi_number) {
1298                 napi_get_value_int32(env, argv[i], &asyncContext->volType);
1299             } else if (i == PARAM1 && valueType == napi_function) {
1300                 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
1301                 break;
1302             } else {
1303                 NAPI_ASSERT(env, false, "type mismatch");
1304             }
1305         }
1306 
1307         if (asyncContext->callbackRef == nullptr) {
1308             napi_create_promise(env, &asyncContext->deferred, &result);
1309         } else {
1310             napi_get_undefined(env, &result);
1311         }
1312 
1313         napi_value resource = nullptr;
1314         napi_create_string_utf8(env, "IsStreamMute", NAPI_AUTO_LENGTH, &resource);
1315 
1316         status = napi_create_async_work(
1317             env, nullptr, resource,
1318             [](napi_env env, void *data) {
1319                 auto context = static_cast<AudioManagerAsyncContext*>(data);
1320                 context->isMute =
1321                     context->objectInfo->audioMngr_->IsStreamMute(GetNativeAudioVolumeType(context->volType));
1322                 context->isTrue = context->isMute;
1323                 context->status = 0;
1324             },
1325             IsTrueAsyncCallbackComplete, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1326         if (status != napi_ok) {
1327             result = nullptr;
1328         } else {
1329             status = napi_queue_async_work(env, asyncContext->work);
1330             if (status == napi_ok) {
1331                 asyncContext.release();
1332             } else {
1333                 result = nullptr;
1334             }
1335         }
1336     }
1337 
1338     return result;
1339 }
1340 
IsStreamActive(napi_env env,napi_callback_info info)1341 napi_value AudioManagerNapi::IsStreamActive(napi_env env, napi_callback_info info)
1342 {
1343     napi_status status;
1344     const int32_t refCount = 1;
1345     napi_value result = nullptr;
1346 
1347     GET_PARAMS(env, info, ARGS_TWO);
1348     NAPI_ASSERT(env, argc >= ARGS_ONE, "requires 1 parameter minimum");
1349 
1350     unique_ptr<AudioManagerAsyncContext> asyncContext = make_unique<AudioManagerAsyncContext>();
1351 
1352     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1353     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1354         for (size_t i = PARAM0; i < argc; i++) {
1355             napi_valuetype valueType = napi_undefined;
1356             napi_typeof(env, argv[i], &valueType);
1357 
1358             if (i == PARAM0 && valueType == napi_number) {
1359                 napi_get_value_int32(env, argv[i], &asyncContext->volType);
1360             } else if (i == PARAM1 && valueType == napi_function) {
1361                 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
1362                 break;
1363             } else {
1364                 NAPI_ASSERT(env, false, "type mismatch");
1365             }
1366         }
1367 
1368         if (asyncContext->callbackRef == nullptr) {
1369             napi_create_promise(env, &asyncContext->deferred, &result);
1370         } else {
1371             napi_get_undefined(env, &result);
1372         }
1373 
1374         napi_value resource = nullptr;
1375         napi_create_string_utf8(env, "IsStreamActive", NAPI_AUTO_LENGTH, &resource);
1376 
1377         status = napi_create_async_work(
1378             env, nullptr, resource,
1379             [](napi_env env, void *data) {
1380                 auto context = static_cast<AudioManagerAsyncContext*>(data);
1381                 context->isActive =
1382                     context->objectInfo->audioMngr_->IsStreamActive(GetNativeAudioVolumeType(context->volType));
1383                 context->isTrue = context->isActive;
1384                 context->status = 0;
1385             },
1386             IsTrueAsyncCallbackComplete, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1387         if (status != napi_ok) {
1388             result = nullptr;
1389         } else {
1390             status = napi_queue_async_work(env, asyncContext->work);
1391             if (status == napi_ok) {
1392                 asyncContext.release();
1393             } else {
1394                 result = nullptr;
1395             }
1396         }
1397     }
1398 
1399     return result;
1400 }
1401 
SetDeviceActive(napi_env env,napi_callback_info info)1402 napi_value AudioManagerNapi::SetDeviceActive(napi_env env, napi_callback_info info)
1403 {
1404     napi_status status;
1405     const int32_t refCount = 1;
1406     napi_value result = nullptr;
1407 
1408     GET_PARAMS(env, info, ARGS_THREE);
1409     NAPI_ASSERT(env, argc >= ARGS_TWO, "requires 2 parameters minimum");
1410 
1411     unique_ptr<AudioManagerAsyncContext> asyncContext = make_unique<AudioManagerAsyncContext>();
1412 
1413     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1414     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1415         for (size_t i = PARAM0; i < argc; i++) {
1416             napi_valuetype valueType = napi_undefined;
1417             napi_typeof(env, argv[i], &valueType);
1418 
1419             if (i == PARAM0 && valueType == napi_number) {
1420                 napi_get_value_int32(env, argv[i], &asyncContext->deviceType);
1421             } else if (i == PARAM1 && valueType == napi_boolean) {
1422                 napi_get_value_bool(env, argv[i], &asyncContext->isActive);
1423             } else if (i == PARAM2 && valueType == napi_function) {
1424                 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
1425                 break;
1426             } else {
1427                 NAPI_ASSERT(env, false, "type mismatch");
1428             }
1429         }
1430 
1431         if (asyncContext->callbackRef == nullptr) {
1432             napi_create_promise(env, &asyncContext->deferred, &result);
1433         } else {
1434             napi_get_undefined(env, &result);
1435         }
1436 
1437         napi_value resource = nullptr;
1438         napi_create_string_utf8(env, "SetDeviceActive", NAPI_AUTO_LENGTH, &resource);
1439 
1440         status = napi_create_async_work(
1441             env, nullptr, resource,
1442             [](napi_env env, void *data) {
1443                 auto context = static_cast<AudioManagerAsyncContext*>(data);
1444                 context->status = context->objectInfo->audioMngr_->SetDeviceActive(
1445                     static_cast<ActiveDeviceType>(context->deviceType), context->isActive);
1446             },
1447             SetFunctionAsyncCallbackComplete, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1448         if (status != napi_ok) {
1449             result = nullptr;
1450         } else {
1451             status = napi_queue_async_work(env, asyncContext->work);
1452             if (status == napi_ok) {
1453                 asyncContext.release();
1454             } else {
1455                 result = nullptr;
1456             }
1457         }
1458     }
1459 
1460     return result;
1461 }
1462 
IsDeviceActive(napi_env env,napi_callback_info info)1463 napi_value AudioManagerNapi::IsDeviceActive(napi_env env, napi_callback_info info)
1464 {
1465     napi_status status;
1466     const int32_t refCount = 1;
1467     napi_value result = nullptr;
1468 
1469     GET_PARAMS(env, info, ARGS_TWO);
1470     NAPI_ASSERT(env, argc >= ARGS_ONE, "requires 1 parameter minimum");
1471 
1472     unique_ptr<AudioManagerAsyncContext> asyncContext = make_unique<AudioManagerAsyncContext>();
1473 
1474     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1475     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1476         for (size_t i = PARAM0; i < argc; i++) {
1477             napi_valuetype valueType = napi_undefined;
1478             napi_typeof(env, argv[i], &valueType);
1479 
1480             if (i == PARAM0 && valueType == napi_number) {
1481                 napi_get_value_int32(env, argv[i], &asyncContext->deviceType);
1482             } else if (i == PARAM1 && valueType == napi_function) {
1483                 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
1484                 break;
1485             } else {
1486                 NAPI_ASSERT(env, false, "type mismatch");
1487             }
1488         }
1489 
1490         if (asyncContext->callbackRef == nullptr) {
1491             napi_create_promise(env, &asyncContext->deferred, &result);
1492         } else {
1493             napi_get_undefined(env, &result);
1494         }
1495 
1496         napi_value resource = nullptr;
1497         napi_create_string_utf8(env, "IsDeviceActive", NAPI_AUTO_LENGTH, &resource);
1498 
1499         status = napi_create_async_work(
1500             env, nullptr, resource,
1501             [](napi_env env, void *data) {
1502                 auto context = static_cast<AudioManagerAsyncContext*>(data);
1503                 context->isActive =
1504                     context->objectInfo->audioMngr_->IsDeviceActive(static_cast<ActiveDeviceType>(context->deviceType));
1505                 context->isTrue = context->isActive;
1506                 context->status = 0;
1507             },
1508             IsTrueAsyncCallbackComplete, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1509         if (status != napi_ok) {
1510             result = nullptr;
1511         } else {
1512             status = napi_queue_async_work(env, asyncContext->work);
1513             if (status == napi_ok) {
1514                 asyncContext.release();
1515             } else {
1516                 result = nullptr;
1517             }
1518         }
1519     }
1520 
1521     return result;
1522 }
1523 
SetAudioParameter(napi_env env,napi_callback_info info)1524 napi_value AudioManagerNapi::SetAudioParameter(napi_env env, napi_callback_info info)
1525 {
1526     napi_status status;
1527     const int32_t refCount = 1;
1528     napi_value result = nullptr;
1529 
1530     GET_PARAMS(env, info, ARGS_THREE);
1531     NAPI_ASSERT(env, argc >= ARGS_TWO, "requires 2 parameters minimum");
1532 
1533     unique_ptr<AudioManagerAsyncContext> asyncContext = make_unique<AudioManagerAsyncContext>();
1534 
1535     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1536     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1537         for (size_t i = PARAM0; i < argc; i++) {
1538             napi_valuetype valueType = napi_undefined;
1539             napi_typeof(env, argv[i], &valueType);
1540 
1541             if (i == PARAM0 && valueType == napi_string) {
1542                 asyncContext->key = AudioCommonNapi::GetStringArgument(env, argv[i]);
1543             } else if (i == PARAM1 && valueType == napi_string) {
1544                 asyncContext->valueStr = AudioCommonNapi::GetStringArgument(env, argv[i]);
1545             } else if (i == PARAM2 && valueType == napi_function) {
1546                 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
1547                 break;
1548             } else {
1549                 NAPI_ASSERT(env, false, "type mismatch");
1550             }
1551         }
1552 
1553         if (asyncContext->callbackRef == nullptr) {
1554             napi_create_promise(env, &asyncContext->deferred, &result);
1555         } else {
1556             napi_get_undefined(env, &result);
1557         }
1558 
1559         napi_value resource = nullptr;
1560         napi_create_string_utf8(env, "SetAudioParameter", NAPI_AUTO_LENGTH, &resource);
1561 
1562         status = napi_create_async_work(
1563             env, nullptr, resource,
1564             [](napi_env env, void *data) {
1565                 auto context = static_cast<AudioManagerAsyncContext*>(data);
1566                 context->objectInfo->audioMngr_->SetAudioParameter(context->key, context->valueStr);
1567                 context->status = 0;
1568             },
1569             SetFunctionAsyncCallbackComplete, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1570         if (status != napi_ok) {
1571             result = nullptr;
1572         } else {
1573             status = napi_queue_async_work(env, asyncContext->work);
1574             if (status == napi_ok) {
1575                 asyncContext.release();
1576             } else {
1577                 result = nullptr;
1578             }
1579         }
1580     }
1581 
1582     return result;
1583 }
1584 
GetAudioParameter(napi_env env,napi_callback_info info)1585 napi_value AudioManagerNapi::GetAudioParameter(napi_env env, napi_callback_info info)
1586 {
1587     napi_status status;
1588     const int32_t refCount = 1;
1589     napi_value result = nullptr;
1590 
1591     GET_PARAMS(env, info, ARGS_TWO);
1592     NAPI_ASSERT(env, argc >= ARGS_ONE, "requires 1 parameter minimum");
1593 
1594     unique_ptr<AudioManagerAsyncContext> asyncContext = make_unique<AudioManagerAsyncContext>();
1595 
1596     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1597     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1598         for (size_t i = PARAM0; i < argc; i++) {
1599             napi_valuetype valueType = napi_undefined;
1600             napi_typeof(env, argv[i], &valueType);
1601 
1602             if (i == PARAM0 && valueType == napi_string) {
1603                 asyncContext->key = AudioCommonNapi::GetStringArgument(env, argv[i]);
1604             } else if (i == PARAM1 && valueType == napi_function) {
1605                 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
1606                 break;
1607             } else {
1608                 NAPI_ASSERT(env, false, "type mismatch");
1609             }
1610         }
1611 
1612         if (asyncContext->callbackRef == nullptr) {
1613             napi_create_promise(env, &asyncContext->deferred, &result);
1614         } else {
1615             napi_get_undefined(env, &result);
1616         }
1617 
1618         napi_value resource = nullptr;
1619         napi_create_string_utf8(env, "GetAudioParameter", NAPI_AUTO_LENGTH, &resource);
1620         status = napi_create_async_work(
1621             env, nullptr, resource,
1622             [](napi_env env, void *data) {
1623                 auto context = static_cast<AudioManagerAsyncContext*>(data);
1624                 context->valueStr = context->objectInfo->audioMngr_->GetAudioParameter(context->key);
1625                 context->status = 0;
1626             },
1627             GetStringValueAsyncCallbackComplete, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1628         if (status != napi_ok) {
1629             result = nullptr;
1630         } else {
1631             status = napi_queue_async_work(env, asyncContext->work);
1632             if (status == napi_ok) {
1633                 asyncContext.release();
1634             } else {
1635                 result = nullptr;
1636             }
1637         }
1638     }
1639 
1640     return result;
1641 }
1642 
SetVolume(napi_env env,napi_callback_info info)1643 napi_value AudioManagerNapi::SetVolume(napi_env env, napi_callback_info info)
1644 {
1645     napi_status status;
1646     const int32_t refCount = 1;
1647     napi_value result = nullptr;
1648 
1649     GET_PARAMS(env, info, ARGS_THREE);
1650     NAPI_ASSERT(env, argc >= ARGS_TWO, "requires 2 parameters minimum");
1651 
1652     unique_ptr<AudioManagerAsyncContext> asyncContext = make_unique<AudioManagerAsyncContext>();
1653 
1654     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1655     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1656         for (size_t i = PARAM0; i < argc; i++) {
1657             napi_valuetype valueType = napi_undefined;
1658             napi_typeof(env, argv[i], &valueType);
1659 
1660             if (i == PARAM0 && valueType == napi_number) {
1661                 napi_get_value_int32(env, argv[i], &asyncContext->volType);
1662             } else if (i == PARAM1 && valueType == napi_number) {
1663                 napi_get_value_int32(env, argv[i], &asyncContext->volLevel);
1664             } else if (i == PARAM2 && valueType == napi_function) {
1665                 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
1666                 break;
1667             } else {
1668                 NAPI_ASSERT(env, false, "type mismatch");
1669             }
1670         }
1671 
1672         if (asyncContext->callbackRef == nullptr) {
1673             napi_create_promise(env, &asyncContext->deferred, &result);
1674         } else {
1675             napi_get_undefined(env, &result);
1676         }
1677 
1678         napi_value resource = nullptr;
1679         napi_create_string_utf8(env, "SetVolume", NAPI_AUTO_LENGTH, &resource);
1680 
1681         status = napi_create_async_work(
1682             env, nullptr, resource,
1683             [](napi_env env, void *data) {
1684                 auto context = static_cast<AudioManagerAsyncContext*>(data);
1685                 context->status = context->objectInfo->audioMngr_->SetVolume(GetNativeAudioVolumeType(context->volType),
1686                                                                              context->volLevel);
1687             },
1688             SetFunctionAsyncCallbackComplete, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1689         if (status != napi_ok) {
1690             result = nullptr;
1691         } else {
1692             status = napi_queue_async_work(env, asyncContext->work);
1693             if (status == napi_ok) {
1694                 asyncContext.release();
1695             } else {
1696                 result = nullptr;
1697             }
1698         }
1699     }
1700 
1701     return result;
1702 }
1703 
GetVolume(napi_env env,napi_callback_info info)1704 napi_value AudioManagerNapi::GetVolume(napi_env env, napi_callback_info info)
1705 {
1706     napi_status status;
1707     const int32_t refCount = 1;
1708     napi_value result = nullptr;
1709 
1710     GET_PARAMS(env, info, ARGS_TWO);
1711     NAPI_ASSERT(env, argc >= ARGS_ONE, "requires 1 parameter minimum");
1712 
1713     unique_ptr<AudioManagerAsyncContext> asyncContext = make_unique<AudioManagerAsyncContext>();
1714 
1715     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1716     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1717         for (size_t i = PARAM0; i < argc; i++) {
1718             napi_valuetype valueType = napi_undefined;
1719             napi_typeof(env, argv[i], &valueType);
1720 
1721             if (i == PARAM0 && valueType == napi_number) {
1722                 napi_get_value_int32(env, argv[i], &asyncContext->volType);
1723             } else if (i == PARAM1 && valueType == napi_function) {
1724                 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
1725                 break;
1726             } else {
1727                 NAPI_ASSERT(env, false, "type mismatch");
1728             }
1729         }
1730 
1731         if (asyncContext->callbackRef == nullptr) {
1732             napi_create_promise(env, &asyncContext->deferred, &result);
1733         } else {
1734             napi_get_undefined(env, &result);
1735         }
1736 
1737         napi_value resource = nullptr;
1738         napi_create_string_utf8(env, "GetVolume", NAPI_AUTO_LENGTH, &resource);
1739 
1740         status = napi_create_async_work(
1741             env, nullptr, resource,
1742             [](napi_env env, void *data) {
1743                 auto context = static_cast<AudioManagerAsyncContext*>(data);
1744                 context->volLevel = context->objectInfo->audioMngr_->GetVolume(
1745                     GetNativeAudioVolumeType(context->volType));
1746                 context->intValue = context->volLevel;
1747                 context->status = 0;
1748             },
1749             GetIntValueAsyncCallbackComplete, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1750         if (status != napi_ok) {
1751             result = nullptr;
1752         } else {
1753             status = napi_queue_async_work(env, asyncContext->work);
1754             if (status == napi_ok) {
1755                 asyncContext.release();
1756             } else {
1757                 result = nullptr;
1758             }
1759         }
1760     }
1761 
1762     return result;
1763 }
1764 
GetMaxVolume(napi_env env,napi_callback_info info)1765 napi_value AudioManagerNapi::GetMaxVolume(napi_env env, napi_callback_info info)
1766 {
1767     napi_status status;
1768     const int32_t refCount = 1;
1769     napi_value result = nullptr;
1770 
1771     GET_PARAMS(env, info, ARGS_TWO);
1772     NAPI_ASSERT(env, argc >= ARGS_ONE, "requires 1 parameter minimum");
1773 
1774     unique_ptr<AudioManagerAsyncContext> asyncContext = make_unique<AudioManagerAsyncContext>();
1775 
1776     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1777     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1778         for (size_t i = PARAM0; i < argc; i++) {
1779             napi_valuetype valueType = napi_undefined;
1780             napi_typeof(env, argv[i], &valueType);
1781 
1782             if (i == PARAM0 && valueType == napi_number) {
1783                 napi_get_value_int32(env, argv[i], &asyncContext->volType);
1784             } else if (i == PARAM1 && valueType == napi_function) {
1785                 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
1786                 break;
1787             } else {
1788                 NAPI_ASSERT(env, false, "type mismatch");
1789             }
1790         }
1791 
1792         if (asyncContext->callbackRef == nullptr) {
1793             napi_create_promise(env, &asyncContext->deferred, &result);
1794         } else {
1795             napi_get_undefined(env, &result);
1796         }
1797 
1798         napi_value resource = nullptr;
1799         napi_create_string_utf8(env, "GetMaxVolume", NAPI_AUTO_LENGTH, &resource);
1800 
1801         status = napi_create_async_work(
1802             env, nullptr, resource,
1803             [](napi_env env, void *data) {
1804                 auto context = static_cast<AudioManagerAsyncContext*>(data);
1805                 context->volLevel = context->objectInfo->audioMngr_->GetMaxVolume(
1806                     GetNativeAudioVolumeType(context->volType));
1807                 context->intValue = context->volLevel;
1808                 context->status = 0;
1809             },
1810             GetIntValueAsyncCallbackComplete, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1811         if (status != napi_ok) {
1812             result = nullptr;
1813         } else {
1814             status = napi_queue_async_work(env, asyncContext->work);
1815             if (status == napi_ok) {
1816                 asyncContext.release();
1817             } else {
1818                 result = nullptr;
1819             }
1820         }
1821     }
1822 
1823     return result;
1824 }
1825 
GetMinVolume(napi_env env,napi_callback_info info)1826 napi_value AudioManagerNapi::GetMinVolume(napi_env env, napi_callback_info info)
1827 {
1828     napi_status status;
1829     const int32_t refCount = 1;
1830     napi_value result = nullptr;
1831 
1832     GET_PARAMS(env, info, ARGS_TWO);
1833     NAPI_ASSERT(env, argc >= ARGS_ONE, "requires 1 parameter minimum");
1834 
1835     unique_ptr<AudioManagerAsyncContext> asyncContext = make_unique<AudioManagerAsyncContext>();
1836 
1837     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1838     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1839         for (size_t i = PARAM0; i < argc; i++) {
1840             napi_valuetype valueType = napi_undefined;
1841             napi_typeof(env, argv[i], &valueType);
1842 
1843             if (i == PARAM0 && valueType == napi_number) {
1844                 napi_get_value_int32(env, argv[i], &asyncContext->volType);
1845             } else if (i == PARAM1 && valueType == napi_function) {
1846                 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
1847                 break;
1848             } else {
1849                 NAPI_ASSERT(env, false, "type mismatch");
1850             }
1851         }
1852 
1853         if (asyncContext->callbackRef == nullptr) {
1854             napi_create_promise(env, &asyncContext->deferred, &result);
1855         } else {
1856             napi_get_undefined(env, &result);
1857         }
1858 
1859         napi_value resource = nullptr;
1860         napi_create_string_utf8(env, "GetMinVolume", NAPI_AUTO_LENGTH, &resource);
1861 
1862         status = napi_create_async_work(
1863             env, nullptr, resource,
1864             [](napi_env env, void *data) {
1865                 auto context = static_cast<AudioManagerAsyncContext*>(data);
1866                 context->volLevel = context->objectInfo->audioMngr_->GetMinVolume(
1867                     GetNativeAudioVolumeType(context->volType));
1868                 context->intValue = context->volLevel;
1869                 context->status = 0;
1870             },
1871             GetIntValueAsyncCallbackComplete, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1872         if (status != napi_ok) {
1873             result = nullptr;
1874         } else {
1875             status = napi_queue_async_work(env, asyncContext->work);
1876             if (status == napi_ok) {
1877                 asyncContext.release();
1878             } else {
1879                 result = nullptr;
1880             }
1881         }
1882     }
1883 
1884     return result;
1885 }
1886 
SetValueInt32(const napi_env & env,const std::string & fieldStr,const int intValue,napi_value & result)1887 static void SetValueInt32(const napi_env& env, const std::string& fieldStr, const int intValue, napi_value &result)
1888 {
1889     napi_value value = nullptr;
1890     napi_create_int32(env, intValue, &value);
1891     napi_set_named_property(env, result, fieldStr.c_str(), value);
1892 }
1893 
GetDevicesAsyncCallbackComplete(napi_env env,napi_status status,void * data)1894 static void GetDevicesAsyncCallbackComplete(napi_env env, napi_status status, void *data)
1895 {
1896     auto asyncContext = static_cast<AudioManagerAsyncContext*>(data);
1897     napi_value result[ARGS_TWO] = {0};
1898     napi_value valueParam = nullptr;
1899     napi_value retVal;
1900     size_t size = asyncContext->deviceDescriptors.size();
1901     HiLog::Info(LABEL, "number of devices = %{public}zu", size);
1902 
1903     napi_create_array_with_length(env, size, &result[PARAM1]);
1904     for (size_t i = 0; i < size; i ++) {
1905         if (asyncContext->deviceDescriptors[i] != nullptr) {
1906             (void)napi_create_object(env, &valueParam);
1907             SetValueInt32(env, "deviceRole", static_cast<int32_t>(
1908                 asyncContext->deviceDescriptors[i]->deviceRole_), valueParam);
1909             SetValueInt32(env, "deviceType", static_cast<int32_t>(
1910                 asyncContext->deviceDescriptors[i]->deviceType_), valueParam);
1911             napi_set_element(env, result[PARAM1], i, valueParam);
1912         }
1913     }
1914 
1915     napi_get_undefined(env, &result[PARAM0]);
1916 
1917     if (asyncContext->deferred) {
1918         napi_resolve_deferred(env, asyncContext->deferred, result[PARAM1]);
1919     } else {
1920         napi_value callback = nullptr;
1921         napi_get_reference_value(env, asyncContext->callbackRef, &callback);
1922         napi_call_function(env, nullptr, callback, ARGS_TWO, result, &retVal);
1923         napi_delete_reference(env, asyncContext->callbackRef);
1924     }
1925     napi_delete_async_work(env, asyncContext->work);
1926 
1927     delete asyncContext;
1928 }
1929 
GetDevices(napi_env env,napi_callback_info info)1930 napi_value AudioManagerNapi::GetDevices(napi_env env, napi_callback_info info)
1931 {
1932     napi_status status;
1933     const int32_t refCount = 1;
1934     napi_value result = nullptr;
1935 
1936     GET_PARAMS(env, info, ARGS_TWO);
1937     NAPI_ASSERT(env, argc >= ARGS_ONE, "requires 1 parameter minimum");
1938 
1939     unique_ptr<AudioManagerAsyncContext> asyncContext = make_unique<AudioManagerAsyncContext>();
1940 
1941     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1942     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1943         for (size_t i = PARAM0; i < argc; i++) {
1944             napi_valuetype valueType = napi_undefined;
1945             napi_typeof(env, argv[i], &valueType);
1946 
1947             if (i == PARAM0 && valueType == napi_number) {
1948                 napi_get_value_int32(env, argv[i], &asyncContext->deviceFlag);
1949             } else if (i == PARAM1 && valueType == napi_function) {
1950                 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
1951                 break;
1952             } else {
1953                 NAPI_ASSERT(env, false, "type mismatch");
1954             }
1955         }
1956 
1957         if (asyncContext->callbackRef == nullptr) {
1958             napi_create_promise(env, &asyncContext->deferred, &result);
1959         } else {
1960             napi_get_undefined(env, &result);
1961         }
1962 
1963         napi_value resource = nullptr;
1964         napi_create_string_utf8(env, "GetDevices", NAPI_AUTO_LENGTH, &resource);
1965 
1966         status = napi_create_async_work(
1967             env, nullptr, resource,
1968             [](napi_env env, void *data) {
1969                 auto context = static_cast<AudioManagerAsyncContext*>(data);
1970                 context->deviceDescriptors = context->objectInfo->audioMngr_->GetDevices(
1971                     static_cast<DeviceFlag>(context->deviceFlag));
1972                 context->status = 0;
1973             },
1974             GetDevicesAsyncCallbackComplete, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1975         if (status != napi_ok) {
1976             result = nullptr;
1977         } else {
1978             status = napi_queue_async_work(env, asyncContext->work);
1979             if (status == napi_ok) {
1980                 asyncContext.release();
1981             } else {
1982                 result = nullptr;
1983             }
1984         }
1985     }
1986 
1987     return result;
1988 }
1989 
UndefinedNapiValue(const napi_env & env)1990 napi_value UndefinedNapiValue(const napi_env& env)
1991 {
1992     napi_value result = nullptr;
1993     napi_get_undefined(env, &result);
1994     return result;
1995 }
1996 
JsObjectToInt(const napi_env & env,const napi_value & object,std::string fieldStr,int32_t & fieldRef)1997 napi_status JsObjectToInt(const napi_env &env, const napi_value &object, std::string fieldStr, int32_t &fieldRef)
1998 {
1999     napi_value tempValue = nullptr;
2000     napi_status status = napi_get_named_property(env, object, fieldStr.c_str(), &tempValue);
2001     if (status != napi_ok) {
2002         MEDIA_ERR_LOG("JsObjectToInt: Failed to retrieve property %{public}s", fieldStr.c_str());
2003         return status;
2004     }
2005     status = napi_get_value_int32(env, tempValue, &fieldRef);
2006     if (status != napi_ok) {
2007         MEDIA_ERR_LOG("JsObjectToInt: Failed to retrieve value for property %{public}s", fieldStr.c_str());
2008     }
2009     return status;
2010 }
2011 
JsObjectToBool(const napi_env & env,const napi_value & object,std::string fieldStr,bool & fieldRef)2012 napi_status JsObjectToBool(const napi_env &env, const napi_value &object, std::string fieldStr, bool &fieldRef)
2013 {
2014     napi_value tempValue = nullptr;
2015     napi_status status = napi_get_named_property(env, object, fieldStr.c_str(), &tempValue);
2016     if (status != napi_ok) {
2017         MEDIA_ERR_LOG("JsObjectToBool: Failed to retrieve property %{public}s", fieldStr.c_str());
2018         return status;
2019     }
2020     status = napi_get_value_bool(env, tempValue, &fieldRef);
2021     if (status != napi_ok) {
2022         MEDIA_ERR_LOG("JsObjectToBool: Failed to retrieve value for property %{public}s", fieldStr.c_str());
2023     }
2024     return status;
2025 }
2026 
JsObjToAudioInterrupt(const napi_env & env,const napi_value & object,AudioInterrupt & audioInterrupt)2027 napi_status JsObjToAudioInterrupt(const napi_env& env, const napi_value& object, AudioInterrupt &audioInterrupt)
2028 {
2029     int32_t propValue = -1;
2030     napi_status status = JsObjectToInt(env, object, "contentType", propValue);
2031     if (status != napi_ok) {
2032         MEDIA_ERR_LOG("JsObjToAudioInterrupt: Failed to retrieve contentType");
2033         return status;
2034     }
2035     audioInterrupt.contentType = static_cast<ContentType>(propValue);
2036 
2037     status = JsObjectToInt(env, object, "streamUsage", propValue);
2038     if (status != napi_ok) {
2039         MEDIA_ERR_LOG("JsObjToAudioInterrupt: Failed to retrieve streamUsage");
2040         return status;
2041     }
2042     audioInterrupt.streamUsage = static_cast<StreamUsage>(propValue);
2043 
2044     status = JsObjectToBool(env, object, "pauseWhenDucked", audioInterrupt.pauseWhenDucked);
2045     if (status != napi_ok) {
2046         MEDIA_ERR_LOG("JsObjToAudioInterrupt: Failed to retrieve pauseWhenDucked");
2047         return status;
2048     }
2049     // Update Stream type based on Content type and Stream Usage type
2050     audioInterrupt.streamType = AudioSystemManager::GetStreamType(audioInterrupt.contentType,
2051         audioInterrupt.streamUsage);
2052     MEDIA_INFO_LOG("JsObjToAudioInterrupt: ContentType %{public}d, streamUsage %{public}d, streamType %{public}d ",
2053         audioInterrupt.contentType, audioInterrupt.streamUsage, audioInterrupt.streamType);
2054     return status;
2055 }
2056 
On(napi_env env,napi_callback_info info)2057 napi_value AudioManagerNapi::On(napi_env env, napi_callback_info info)
2058 {
2059     napi_value undefinedResult = nullptr;
2060     napi_get_undefined(env, &undefinedResult);
2061 
2062     const size_t minArgCount = 2;
2063     size_t argCount = 3;
2064     napi_value args[minArgCount + 1] = {nullptr, nullptr, nullptr};
2065     napi_value jsThis = nullptr;
2066     napi_status status = napi_get_cb_info(env, info, &argCount, args, &jsThis, nullptr);
2067     if (status != napi_ok || argCount < minArgCount) {
2068         MEDIA_ERR_LOG("On fail to napi_get_cb_info/Requires min 2 parameters");
2069         return undefinedResult;
2070     }
2071 
2072     napi_valuetype eventType = napi_undefined;
2073     if (napi_typeof(env, args[PARAM0], &eventType) != napi_ok || eventType != napi_string) {
2074         return undefinedResult;
2075     }
2076     std::string callbackName = AudioCommonNapi::GetStringArgument(env, args[0]);
2077     MEDIA_INFO_LOG("AudioManagerNapi::On callbackName: %{public}s", callbackName.c_str());
2078 
2079     AudioManagerNapi *managerNapi = nullptr;
2080     status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&managerNapi));
2081     NAPI_ASSERT(env, status == napi_ok && managerNapi != nullptr, "Failed to retrieve audio manager napi instance.");
2082     NAPI_ASSERT(env, managerNapi->audioMngr_ != nullptr, "audio system manager instance is null.");
2083     napi_valuetype handler = napi_undefined;
2084     if (argCount == minArgCount) {
2085         napi_valuetype handler = napi_undefined;
2086         if (napi_typeof(env, args[PARAM1], &handler) != napi_ok || handler != napi_function) {
2087             MEDIA_ERR_LOG("AudioManagerNapi::On type mismatch for parameter 2");
2088             return undefinedResult;
2089         }
2090     } else {
2091         napi_valuetype paramArg1 = napi_undefined;
2092         napi_typeof(env, args[PARAM1], &paramArg1);
2093         if (!callbackName.compare(INTERRUPT_CALLBACK_NAME)) {
2094             if (paramArg1 != napi_object) {
2095                 MEDIA_ERR_LOG("AudioManagerNapi::On Type mismatch for parameter 2");
2096                 return undefinedResult;
2097             }
2098             if (napi_typeof(env, args[PARAM2], &handler) != napi_ok || handler != napi_function) {
2099                 MEDIA_ERR_LOG("AudioManagerNapi::On type mismatch for parameter 3");
2100                 return undefinedResult;
2101             }
2102         }
2103 
2104         if (managerNapi->interruptCallbackNapi_ == nullptr) {
2105             managerNapi->interruptCallbackNapi_ = std::make_shared<AudioManagerInterruptCallbackNapi>(env);
2106             int32_t ret = managerNapi->audioMngr_->
2107                 SetAudioManagerInterruptCallback(managerNapi->interruptCallbackNapi_);
2108             if (ret) {
2109                 MEDIA_ERR_LOG("AudioManagerNapi: SetAudioManagerInterruptCallback Failed");
2110                 return undefinedResult;
2111             }
2112         }
2113         std::shared_ptr<AudioManagerInterruptCallbackNapi> cb =
2114         std::static_pointer_cast<AudioManagerInterruptCallbackNapi>(managerNapi->interruptCallbackNapi_);
2115         cb->SaveCallbackReference(callbackName, args[PARAM2]);
2116         AudioInterrupt audioInterrupt;
2117         status = JsObjToAudioInterrupt(env, args[PARAM1], audioInterrupt);
2118         NAPI_ASSERT(env, status == napi_ok, "Failed to retrieve audioInterrupt value");
2119         int32_t ret = managerNapi->audioMngr_->RequestAudioFocus(audioInterrupt);
2120         if (ret) {
2121             MEDIA_ERR_LOG("AudioManagerNapi: RequestAudioFocus Failed");
2122             return undefinedResult;
2123         }
2124         MEDIA_INFO_LOG("AudioManagerNapi::On SetAudioManagerInterruptCallback and RequestAudioFocus is successful");
2125     }
2126 
2127     if (!callbackName.compare(RINGERMODE_CALLBACK_NAME)) {
2128         if (managerNapi->ringerModecallbackNapi_ == nullptr) {
2129             managerNapi->ringerModecallbackNapi_ = std::make_shared<AudioRingerModeCallbackNapi>(env);
2130             int32_t ret = managerNapi->audioMngr_->SetRingerModeCallback(
2131                 managerNapi->cachedClientId, managerNapi->ringerModecallbackNapi_);
2132             if (ret) {
2133                 MEDIA_ERR_LOG("AudioManagerNapi: SetRingerModeCallback Failed");
2134                 return undefinedResult;
2135             } else {
2136                 MEDIA_INFO_LOG("AudioManagerNapi: SetRingerModeCallback Success");
2137             }
2138         }
2139 
2140         std::shared_ptr<AudioRingerModeCallbackNapi> cb =
2141             std::static_pointer_cast<AudioRingerModeCallbackNapi>(managerNapi->ringerModecallbackNapi_);
2142         cb->SaveCallbackReference(callbackName, args[PARAM1]);
2143     } else if (!callbackName.compare(VOLUME_CHANGE_CALLBACK_NAME)) {
2144         std::shared_ptr<AudioVolumeKeyEventNapi> cb =
2145             std::static_pointer_cast<AudioVolumeKeyEventNapi>(managerNapi->volumeKeyEventCallbackNapi_);
2146         cb->SaveCallbackReference(callbackName, args[PARAM1]);
2147     } else if (!callbackName.compare(DEVICE_CHANGE_CALLBACK_NAME)) {
2148         if (managerNapi->deviceChangeCallbackNapi_ == nullptr) {
2149             managerNapi->deviceChangeCallbackNapi_ = std::make_shared<AudioManagerCallbackNapi>(env);
2150         }
2151         int32_t ret = managerNapi->audioMngr_->SetDeviceChangeCallback(managerNapi->deviceChangeCallbackNapi_);
2152         if (ret) {
2153             MEDIA_ERR_LOG("AudioManagerNapi: SetDeviceChangeCallback Failed");
2154             return undefinedResult;
2155         }
2156         std::shared_ptr<AudioManagerCallbackNapi> cb =
2157         std::static_pointer_cast<AudioManagerCallbackNapi>(managerNapi->deviceChangeCallbackNapi_);
2158         cb->SaveCallbackReference(callbackName, args[PARAM1]);
2159         MEDIA_INFO_LOG("AudioManagerNapi::On SetDeviceChangeCallback is successful");
2160     }
2161     return undefinedResult;
2162 }
2163 
Off(napi_env env,napi_callback_info info)2164 napi_value AudioManagerNapi::Off(napi_env env, napi_callback_info info)
2165 {
2166     napi_value undefinedResult = nullptr;
2167     napi_get_undefined(env, &undefinedResult);
2168 
2169     const size_t minArgCount = 1;
2170     size_t argCount = 3;
2171     napi_value args[minArgCount + 2] = {nullptr, nullptr, nullptr};
2172     napi_value jsThis = nullptr;
2173     napi_status status = napi_get_cb_info(env, info, &argCount, args, &jsThis, nullptr);
2174     if (status != napi_ok || argCount < minArgCount) {
2175         MEDIA_ERR_LOG("Off fail to napi_get_cb_info/Requires min 1 parameters");
2176         return undefinedResult;
2177     }
2178 
2179     napi_valuetype eventType = napi_undefined;
2180     if (napi_typeof(env, args[PARAM0], &eventType) != napi_ok || eventType != napi_string) {
2181         return undefinedResult;
2182     }
2183     std::string callbackName = AudioCommonNapi::GetStringArgument(env, args[0]);
2184     MEDIA_INFO_LOG("AudioManagerNapi::Off callbackName: %{public}s", callbackName.c_str());
2185 
2186     AudioManagerNapi *managerNapi = nullptr;
2187     status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&managerNapi));
2188     NAPI_ASSERT(env, status == napi_ok && managerNapi != nullptr, "Failed to retrieve audio mgr napi instance.");
2189     NAPI_ASSERT(env, managerNapi->audioMngr_ != nullptr, "audio system mgr instance is null.");
2190 
2191     napi_valuetype handler = napi_undefined;
2192     if (!callbackName.compare(INTERRUPT_CALLBACK_NAME) && argCount > ARGS_ONE) {
2193         napi_valuetype paramArg1 = napi_undefined;
2194         if (napi_typeof(env, args[PARAM1], &paramArg1) != napi_ok || paramArg1 != napi_object) {
2195             MEDIA_ERR_LOG("AudioManagerNapi::Off type mismatch for parameter 2");
2196             return undefinedResult;
2197         }
2198         if (argCount == ARGS_THREE) {
2199             if (napi_typeof(env, args[PARAM2], &handler) != napi_ok || handler != napi_function) {
2200                 MEDIA_ERR_LOG("AudioManagerNapi::Off type mismatch for parameter 3");
2201                 return undefinedResult;
2202             }
2203         }
2204         AudioInterrupt audioInterrupt;
2205         status = JsObjToAudioInterrupt(env, args[PARAM1], audioInterrupt);
2206         NAPI_ASSERT(env, status == napi_ok, "AudioManagerNapi::Off Failed to retrieve audioInterrupt value");
2207         int32_t ret = managerNapi->audioMngr_->AbandonAudioFocus(audioInterrupt);
2208         if (ret) {
2209             MEDIA_ERR_LOG("AudioManagerNapi::Off AbandonAudioFocus Failed");
2210         }
2211         ret = managerNapi->audioMngr_->UnsetAudioManagerInterruptCallback();
2212         if (ret) {
2213             MEDIA_ERR_LOG("AudioManagerNapi::Off UnsetAudioManagerInterruptCallback Failed");
2214             return undefinedResult;
2215         }
2216         if (managerNapi->interruptCallbackNapi_ != nullptr) {
2217             managerNapi->interruptCallbackNapi_.reset();
2218             managerNapi->interruptCallbackNapi_ = nullptr;
2219         }
2220         MEDIA_INFO_LOG("AudioManagerNapi::Off Abandon Focus and UnSetAudioInterruptCallback success");
2221     } else if (!callbackName.compare(DEVICE_CHANGE_CALLBACK_NAME)) {
2222         int32_t ret = managerNapi->audioMngr_->UnsetDeviceChangeCallback();
2223         if (ret) {
2224             MEDIA_ERR_LOG("AudioManagerNapi::Off UnsetDeviceChangeCallback Failed");
2225             return undefinedResult;
2226         }
2227         if (managerNapi->deviceChangeCallbackNapi_ != nullptr) {
2228             managerNapi->deviceChangeCallbackNapi_.reset();
2229             managerNapi->deviceChangeCallbackNapi_ = nullptr;
2230         }
2231         MEDIA_INFO_LOG("AudioManagerNapi::Off UnsetDeviceChangeCallback Success");
2232     }
2233     return undefinedResult;
2234 }
2235 
Init(napi_env env,napi_value exports)2236 static napi_value Init(napi_env env, napi_value exports)
2237 {
2238     AudioManagerNapi::Init(env, exports);
2239     AudioCapturerNapi::Init(env, exports);
2240     AudioRendererNapi::Init(env, exports);
2241     AudioParametersNapi::Init(env, exports);
2242     RingtonePlayerNapi::Init(env, exports);
2243     SystemSoundManagerNapi::Init(env, exports);
2244     RingtoneOptionsNapi::Init(env, exports);
2245     AudioRendererInfoNapi::Init(env, exports);
2246 
2247     return exports;
2248 }
2249 
2250 static napi_module g_module = {
2251     .nm_version = 1,
2252     .nm_flags = 0,
2253     .nm_filename = nullptr,
2254     .nm_register_func = Init,
2255     .nm_modname = "multimedia.audio",
2256     .nm_priv = ((void *)0),
2257     .reserved = {0}
2258 };
2259 
RegisterModule(void)2260 extern "C" __attribute__((constructor)) void RegisterModule(void)
2261 {
2262     napi_module_register(&g_module);
2263 }
2264 } // namespace AudioStandard
2265 } // namespace OHOS
2266