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_volume_key_event_napi.h"
17
18 #include <uv.h>
19
20 #include "audio_log.h"
21
22 using namespace std;
23 namespace {
24 const std::string VOLUME_KEY_EVENT_CALLBACK_NAME = "volumeChange";
25 }
26 namespace OHOS {
27 namespace AudioStandard {
AudioVolumeKeyEventNapi(napi_env env)28 AudioVolumeKeyEventNapi::AudioVolumeKeyEventNapi(napi_env env)
29 :env_(env)
30 {
31 AUDIO_DEBUG_LOG("AudioVolumeKeyEventNapi::Constructor");
32 }
33
~AudioVolumeKeyEventNapi()34 AudioVolumeKeyEventNapi::~AudioVolumeKeyEventNapi()
35 {
36 AUDIO_DEBUG_LOG("AudioVolumeKeyEventNapi::Destructor");
37 }
38
OnVolumeKeyEvent(VolumeEvent volumeEvent)39 void AudioVolumeKeyEventNapi::OnVolumeKeyEvent(VolumeEvent volumeEvent)
40 {
41 std::lock_guard<std::mutex> lock(mutex_);
42 AUDIO_DEBUG_LOG("AudioVolumeKeyEventNapi: OnVolumeKeyEvent is called volumeLevel=%{public}d", volumeEvent.volume);
43 AUDIO_DEBUG_LOG("AudioVolumeKeyEventNapi: isUpdateUi is called isUpdateUi=%{public}d", volumeEvent.updateUi);
44 if (audioVolumeKeyEventJsCallback_ == nullptr) {
45 AUDIO_DEBUG_LOG("AudioManagerCallbackNapi:No JS callback registered return");
46 return;
47 }
48 std::unique_ptr<AudioVolumeKeyEventJsCallback> cb = std::make_unique<AudioVolumeKeyEventJsCallback>();
49 CHECK_AND_RETURN_LOG(cb != nullptr, "No memory");
50 cb->callback = audioVolumeKeyEventJsCallback_;
51 cb->callbackName = VOLUME_KEY_EVENT_CALLBACK_NAME;
52 cb->volumeEvent.volumeType = volumeEvent.volumeType;
53 cb->volumeEvent.volume = volumeEvent.volume;
54 cb->volumeEvent.updateUi = volumeEvent.updateUi;
55 cb->volumeEvent.volumeGroupId = volumeEvent.volumeGroupId;
56 cb->volumeEvent.networkId = volumeEvent.networkId;
57
58 return OnJsCallbackVolumeEvent(cb);
59 }
60
SaveCallbackReference(const std::string & callbackName,napi_value args)61 void AudioVolumeKeyEventNapi::SaveCallbackReference(const std::string &callbackName, napi_value args)
62 {
63 std::lock_guard<std::mutex> lock(mutex_);
64 napi_ref callback = nullptr;
65 const int32_t refCount = 1;
66 napi_status status = napi_create_reference(env_, args, refCount, &callback);
67 CHECK_AND_RETURN_LOG(status == napi_ok && callback != nullptr,
68 "AudioVolumeKeyEventNapi: creating reference for callback fail");
69 std::shared_ptr<VolumeKeyEventAutoRef> cb = std::make_shared<VolumeKeyEventAutoRef>(env_, callback);
70 if (callbackName == VOLUME_KEY_EVENT_CALLBACK_NAME) {
71 audioVolumeKeyEventJsCallback_ = cb;
72 } else {
73 AUDIO_ERR_LOG("AudioVolumeKeyEventNapi: Unknown callback type: %{public}s", callbackName.c_str());
74 }
75 }
76
SetValueInt32(const napi_env & env,const std::string & fieldStr,const int intValue,napi_value & result)77 static void SetValueInt32(const napi_env& env, const std::string& fieldStr, const int intValue, napi_value& result)
78 {
79 napi_value value = nullptr;
80 napi_create_int32(env, intValue, &value);
81 napi_set_named_property(env, result, fieldStr.c_str(), value);
82 }
83
SetValueString(const napi_env & env,const std::string & fieldStr,const std::string stringValue,napi_value & result)84 static void SetValueString(const napi_env& env, const std::string& fieldStr, const std::string stringValue,
85 napi_value& result)
86 {
87 napi_value value = nullptr;
88 napi_create_string_utf8(env, stringValue.c_str(), NAPI_AUTO_LENGTH, &value);
89 napi_set_named_property(env, result, fieldStr.c_str(), value);
90 }
91
SetValueBoolean(const napi_env & env,const std::string & fieldStr,const bool boolValue,napi_value & result)92 static void SetValueBoolean(const napi_env& env, const std::string& fieldStr, const bool boolValue, napi_value& result)
93 {
94 napi_value value = nullptr;
95 napi_get_boolean(env, boolValue, &value);
96 napi_set_named_property(env, result, fieldStr.c_str(), value);
97 }
98
GetJsAudioVolumeType(AudioStreamType streamType)99 static int32_t GetJsAudioVolumeType(AudioStreamType streamType)
100 {
101 int32_t nativeStreamType = static_cast<int32_t>(streamType);
102 int32_t result = AudioManagerNapi::VOLUMETYPE_DEFAULT;
103 switch (nativeStreamType) {
104 case AudioStreamType::STREAM_VOICE_CALL:
105 case AudioStreamType::STREAM_VOICE_MESSAGE:
106 result = AudioManagerNapi::VOICE_CALL;
107 break;
108 case AudioStreamType::STREAM_RING:
109 case AudioStreamType::STREAM_SYSTEM:
110 case AudioStreamType::STREAM_NOTIFICATION:
111 case AudioStreamType::STREAM_SYSTEM_ENFORCED:
112 case AudioStreamType::STREAM_DTMF:
113 result = AudioManagerNapi::RINGTONE;
114 break;
115 case AudioStreamType::STREAM_MUSIC:
116 case AudioStreamType::STREAM_MEDIA:
117 case AudioStreamType::STREAM_MOVIE:
118 case AudioStreamType::STREAM_GAME:
119 case AudioStreamType::STREAM_SPEECH:
120 case AudioStreamType::STREAM_NAVIGATION:
121 result = AudioManagerNapi::MEDIA;
122 break;
123 case AudioStreamType::STREAM_ALARM:
124 result = AudioManagerNapi::ALARM;
125 break;
126 case AudioStreamType::STREAM_ACCESSIBILITY:
127 result = AudioManagerNapi::ACCESSIBILITY;
128 break;
129 case AudioStreamType::STREAM_VOICE_ASSISTANT:
130 result = AudioManagerNapi::VOICE_ASSISTANT;
131 break;
132 case AudioStreamType::STREAM_ULTRASONIC:
133 result = AudioManagerNapi::ULTRASONIC;
134 break;
135 default:
136 result = AudioManagerNapi::VOLUMETYPE_DEFAULT;
137 break;
138 }
139 return result;
140 }
141
NativeVolumeEventToJsObj(const napi_env & env,napi_value & jsObj,const VolumeEvent & volumeEvent)142 static void NativeVolumeEventToJsObj(const napi_env& env, napi_value& jsObj,
143 const VolumeEvent& volumeEvent)
144 {
145 napi_create_object(env, &jsObj);
146 SetValueInt32(env, "volumeType", GetJsAudioVolumeType(volumeEvent.volumeType), jsObj);
147 SetValueInt32(env, "volume", static_cast<int32_t>(volumeEvent.volume), jsObj);
148 SetValueBoolean(env, "updateUi", volumeEvent.updateUi, jsObj);
149 SetValueInt32(env, "volumeGroupId", volumeEvent.volumeGroupId, jsObj);
150 SetValueString(env, "networkId", volumeEvent.networkId, jsObj);
151 }
152
OnJsCallbackVolumeEvent(std::unique_ptr<AudioVolumeKeyEventJsCallback> & jsCb)153 void AudioVolumeKeyEventNapi::OnJsCallbackVolumeEvent(std::unique_ptr<AudioVolumeKeyEventJsCallback> &jsCb)
154 {
155 AUDIO_INFO_LOG("OnJsCallbackVolumeEvent");
156 uv_loop_s *loop = nullptr;
157 napi_get_uv_event_loop(env_, &loop);
158 if (loop == nullptr) {
159 return;
160 }
161
162 uv_work_t *work = new(std::nothrow) uv_work_t;
163 if (work == nullptr) {
164 AUDIO_ERR_LOG("OnJsCallBackInterrupt: No memory");
165 return;
166 }
167 if (jsCb.get() == nullptr) {
168 AUDIO_ERR_LOG("OnJsCallBackInterrupt: jsCb.get() is null");
169 delete work;
170 return;
171 }
172 work->data = reinterpret_cast<void *>(jsCb.get());
173
174 int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) {
175 // Js Thread
176 AudioVolumeKeyEventJsCallback *event = reinterpret_cast<AudioVolumeKeyEventJsCallback *>(work->data);
177 std::string request = event->callbackName;
178 napi_env env = event->callback->env_;
179 napi_ref callback = event->callback->cb_;
180 AUDIO_DEBUG_LOG("JsCallBack %{public}s, uv_queue_work start", request.c_str());
181 do {
182 CHECK_AND_BREAK_LOG(status != UV_ECANCELED, "%{public}s canceled", request.c_str());
183
184 napi_value jsCallback = nullptr;
185 napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
186 CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
187 request.c_str());
188
189 // Call back function
190 napi_value args[1] = { nullptr };
191 NativeVolumeEventToJsObj(env, args[0], event->volumeEvent);
192 CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[0] != nullptr,
193 "%{public}s fail to create Interrupt callback", request.c_str());
194
195 const size_t argCount = 1;
196 napi_value result = nullptr;
197 nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
198 CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to call Interrupt callback", request.c_str());
199 } while (0);
200 delete event;
201 delete work;
202 });
203 if (ret != 0) {
204 AUDIO_ERR_LOG("Failed to execute libuv work queue");
205 delete work;
206 } else {
207 jsCb.release();
208 }
209 }
210 } // namespace AudioStandard
211 } // namespace OHOS
212