• 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 #ifndef AUDIO_MNGR_NAPI_H_
17 #define AUDIO_MNGR_NAPI_H_
18 
19 #include <iostream>
20 #include <map>
21 #include <vector>
22 #include "audio_system_manager.h"
23 #include "napi/native_api.h"
24 #include "napi/native_node_api.h"
25 #include "audio_stream_mgr_napi.h"
26 #include "audio_routing_manager_napi.h"
27 
28 namespace OHOS {
29 namespace AudioStandard {
30 static const std::string AUDIO_MNGR_NAPI_CLASS_NAME = "AudioManager";
31 
32 class AudioManagerNapi {
33 public:
34     AudioManagerNapi();
35     ~AudioManagerNapi();
36 
37     enum AudioVolumeType {
38         VOLUMETYPE_DEFAULT = -1,
39         VOICE_CALL = 0,
40         RINGTONE = 2,
41         MEDIA = 3,
42         ALARM = 4,
43         ACCESSIBILITY = 5,
44         VOICE_ASSISTANT = 9,
45         ULTRASONIC = 10,
46         VOLUMETYPE_MAX,
47         ALL = 100
48     };
49 
50     enum AudioRingMode {
51         RINGER_MODE_SILENT = 0,
52         RINGER_MODE_VIBRATE,
53         RINGER_MODE_NORMAL
54     };
55 
56     enum InterruptMode {
57         SHARE_MODE = 0,
58         INDEPENDENT_MODE = 1
59     };
60 
61     enum FocusType {
62         FOCUS_TYPE_RECORDING
63     };
64 
65     static napi_value Init(napi_env env, napi_value exports);
66 
67 private:
68 
69     static void Destructor(napi_env env, void *nativeObject, void *finalize_hint);
70     static napi_value Construct(napi_env env, napi_callback_info info);
71     static napi_value CreateAudioManagerWrapper(napi_env env);
72     static napi_value GetAudioManager(napi_env env, napi_callback_info info);
73     static napi_value SetVolume(napi_env env, napi_callback_info info);
74     static napi_value GetVolume(napi_env env, napi_callback_info info);
75     static napi_value GetMaxVolume(napi_env env, napi_callback_info info);
76     static napi_value GetMinVolume(napi_env env, napi_callback_info info);
77     static napi_value GetDevices(napi_env env, napi_callback_info info);
78     static napi_value SetStreamMute(napi_env env, napi_callback_info info);
79     static napi_value IsStreamMute(napi_env env, napi_callback_info info);
80     static napi_value IsStreamActive(napi_env env, napi_callback_info info);
81     static napi_value SetRingerMode(napi_env env, napi_callback_info info);
82     static napi_value GetRingerMode(napi_env env, napi_callback_info info);
83     static napi_value SetAudioScene(napi_env env, napi_callback_info info);
84     static napi_value GetAudioScene(napi_env env, napi_callback_info info);
85     static napi_value GetAudioSceneSync(napi_env env, napi_callback_info info);
86     static napi_value SetDeviceActive(napi_env env, napi_callback_info info);
87     static napi_value IsDeviceActive(napi_env env, napi_callback_info info);
88     static napi_value SetAudioParameter(napi_env env, napi_callback_info info);
89     static napi_value GetAudioParameter(napi_env env, napi_callback_info info);
90     static napi_value SetMicrophoneMute(napi_env env, napi_callback_info info);
91     static napi_value IsMicrophoneMute(napi_env env, napi_callback_info info);
92     static napi_status AddNamedProperty(napi_env env, napi_value object, const std::string name, int32_t enumValue);
93     static napi_value CreateAudioVolumeTypeObject(napi_env env);
94     static napi_value CreateDeviceFlagObject(napi_env env);
95     static napi_value CreateActiveDeviceTypeObject(napi_env env);
96     static napi_value CreateConnectTypeObject(napi_env env);
97     static napi_value CreateInterruptActionTypeObject(napi_env env);
98     static napi_value CreateAudioRingModeObject(napi_env env);
99     static napi_value On(napi_env env, napi_callback_info info);
100     static napi_value Off(napi_env env, napi_callback_info info);
101     static napi_value CreateDeviceChangeTypeObject(napi_env env);
102     static napi_value CreateAudioSceneObject(napi_env env);
103     static napi_value RequestIndependentInterrupt(napi_env env, napi_callback_info info);
104     static napi_value AbandonIndependentInterrupt(napi_env env, napi_callback_info info);
105     static napi_value GetStreamManager(napi_env env, napi_callback_info info);
106     static napi_value GetRoutingManager(napi_env env, napi_callback_info info);
107     static napi_value GetVolumeManager(napi_env env, napi_callback_info info);
108     static napi_value GetInterruptManager(napi_env env, napi_callback_info info);
109     static void UnregisterDeviceChangeCallback(napi_env env, napi_value callback, AudioManagerNapi* audioMgrNapi);
110     static void AddPropName(std::string& propName, napi_status& status, napi_env env, napi_value& result);
111 
112     template<typename T> static napi_value CreatePropertyBase(napi_env env, T& t_map, napi_ref ref);
113 
114     static napi_ref audioVolumeTypeRef_;
115     static napi_ref deviceFlagRef_;
116     static napi_ref deviceRoleRef_;
117     static napi_ref deviceTypeRef_;
118     static napi_ref activeDeviceTypeRef_;
119     static napi_ref audioRingModeRef_;
120     static napi_ref deviceChangeType_;
121     static napi_ref interruptActionType_;
122     static napi_ref interruptHint_;
123     static napi_ref interruptType_;
124     static napi_ref audioScene_;
125     static napi_ref interruptMode_;
126     static napi_ref focusType_;
127     static napi_ref connectTypeRef_;
128     static napi_ref audioErrors_;
129     static napi_ref communicationDeviceType_;
130     static napi_ref interruptRequestType_;
131     static napi_ref interruptRequestResultType_;
132 
133     AudioSystemManager *audioMngr_;
134     int32_t cachedClientId_ = -1;
135     std::shared_ptr<AudioManagerDeviceChangeCallback> deviceChangeCallbackNapi_ = nullptr;
136     std::shared_ptr<AudioManagerCallback> interruptCallbackNapi_ = nullptr;
137     std::shared_ptr<AudioRingerModeCallback> ringerModecallbackNapi_ = nullptr;
138     std::shared_ptr<VolumeKeyEventCallback> volumeKeyEventCallbackNapi_ = nullptr;
139     napi_env env_;
140 };
141 
142 static const std::map<std::string, DeviceChangeType> DEVICE_CHANGE_TYPE_MAP = {
143     {"CONNECT", CONNECT},
144     {"DISCONNECT", DISCONNECT}
145 };
146 
147 static const std::map<std::string, AudioScene> AUDIO_SCENE_MAP = {
148     {"AUDIO_SCENE_DEFAULT", AUDIO_SCENE_DEFAULT},
149     {"AUDIO_SCENE_RINGING", AUDIO_SCENE_RINGING},
150     {"AUDIO_SCENE_PHONE_CALL", AUDIO_SCENE_PHONE_CALL},
151     {"AUDIO_SCENE_VOICE_CHAT", AUDIO_SCENE_PHONE_CHAT}
152 };
153 
154 static const std::map<std::string, InterruptActionType> INTERRUPT_ACTION_TYPE_MAP = {
155     {"TYPE_ACTIVATED", TYPE_ACTIVATED},
156     {"TYPE_INTERRUPT", TYPE_INTERRUPT}
157 };
158 
159 static const std::map<std::string, AudioManagerNapi::AudioVolumeType> VOLUME_TYPE_MAP = {
160     {"VOICE_CALL", AudioManagerNapi::VOICE_CALL},
161     {"RINGTONE", AudioManagerNapi::RINGTONE},
162     {"MEDIA", AudioManagerNapi::MEDIA},
163     {"VOICE_ASSISTANT", AudioManagerNapi::VOICE_ASSISTANT},
164     {"ALARM", AudioManagerNapi::ALARM},
165     {"ACCESSIBILITY", AudioManagerNapi::ACCESSIBILITY},
166     {"ULTRASONIC", AudioManagerNapi::ULTRASONIC}
167 };
168 
169 static const std::map<std::string, AudioStandard::ActiveDeviceType> ACTIVE_DEVICE_TYPE = {
170     {"SPEAKER", SPEAKER},
171     {"BLUETOOTH_SCO", BLUETOOTH_SCO}
172 };
173 
174 static const std::map<std::string, AudioStandard::InterruptMode> INTERRUPT_MODE_MAP = {
175     {"SHARE_MODE", SHARE_MODE},
176     {"INDEPENDENT_MODE", INDEPENDENT_MODE}
177 };
178 
179 static const std::map<std::string, AudioStandard::FocusType> FOCUS_TYPE_MAP = {
180     {"FOCUS_TYPE_RECORDING", FOCUS_TYPE_RECORDING}
181 };
182 
183 static const std::map<std::string, AudioStandard::AudioErrors> AUDIO_ERRORS_MAP = {
184     {"ERROR_INVALID_PARAM", ERROR_INVALID_PARAM},
185     {"ERROR_NO_MEMORY", ERROR_NO_MEMORY},
186     {"ERROR_ILLEGAL_STATE", ERROR_ILLEGAL_STATE},
187     {"ERROR_UNSUPPORTED", ERROR_UNSUPPORTED},
188     {"ERROR_TIMEOUT", ERROR_TIMEOUT},
189     {"ERROR_STREAM_LIMIT", ERROR_STREAM_LIMIT},
190     {"ERROR_SYSTEM", ERROR_SYSTEM}
191 };
192 
193 static const std::map<std::string, AudioStandard::CommunicationDeviceType> COMMUNICATION_DEVICE_TYPE_MAP = {
194     {"SPEAKER", COMMUNICATION_SPEAKER}
195 };
196 
197 static const std::map<std::string, AudioStandard::InterruptRequestType> INTERRUPT_REQUEST_TYPE_MAP = {
198     {"INTERRUPT_REQUEST_TYPE_DEFAULT", INTERRUPT_REQUEST_TYPE_DEFAULT},
199 };
200 static const std::map<std::string, AudioStandard::InterruptRequestResultType> INTERRUPT_REQUEST_RESULT_TYPE_MAP = {
201     {"INTERRUPT_REQUEST_GRANT", INTERRUPT_REQUEST_GRANT},
202     {"INTERRUPT_REQUEST_REJECT", INTERRUPT_REQUEST_REJECT},
203 };
204 } // namespace AudioStandard
205 } // namespace OHOS
206 #endif /* AUDIO_MNGR_NAPI_H_ */
207