• 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_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_MUSIC:
105             result = AudioManagerNapi::MEDIA;
106             break;
107         case AudioStreamType::STREAM_VOICE_CALL:
108             result = AudioManagerNapi::VOICE_CALL;
109             break;
110         case AudioStreamType::STREAM_RING:
111             result = AudioManagerNapi::RINGTONE;
112             break;
113         case AudioStreamType::STREAM_VOICE_ASSISTANT:
114             result = AudioManagerNapi::VOICE_ASSISTANT;
115             break;
116         default:
117             result = AudioManagerNapi::VOLUMETYPE_DEFAULT;
118             break;
119     }
120     return result;
121 }
122 
NativeVolumeEventToJsObj(const napi_env & env,napi_value & jsObj,const VolumeEvent & volumeEvent)123 static void NativeVolumeEventToJsObj(const napi_env& env, napi_value& jsObj,
124     const VolumeEvent& volumeEvent)
125 {
126     napi_create_object(env, &jsObj);
127     SetValueInt32(env, "volumeType", GetJsAudioVolumeType(volumeEvent.volumeType), jsObj);
128     SetValueInt32(env, "volume", static_cast<int32_t>(volumeEvent.volume), jsObj);
129     SetValueBoolean(env, "updateUi", volumeEvent.updateUi, jsObj);
130     SetValueInt32(env, "volumeGroupId", volumeEvent.volumeGroupId, jsObj);
131     SetValueString(env, "networkId", volumeEvent.networkId, jsObj);
132 }
133 
OnJsCallbackVolumeEvent(std::unique_ptr<AudioVolumeKeyEventJsCallback> & jsCb)134 void AudioVolumeKeyEventNapi::OnJsCallbackVolumeEvent(std::unique_ptr<AudioVolumeKeyEventJsCallback> &jsCb)
135 {
136     AUDIO_INFO_LOG("AudioVolumeKeyEventNapi:OnJsCallbackVolumeEvent");
137     uv_loop_s *loop = nullptr;
138     napi_get_uv_event_loop(env_, &loop);
139     if (loop == nullptr) {
140         return;
141     }
142 
143     uv_work_t *work = new(std::nothrow) uv_work_t;
144     if (work == nullptr) {
145         AUDIO_ERR_LOG("AudioVolumeKeyEventNapi: OnJsCallBackInterrupt: No memory");
146         return;
147     }
148     if (jsCb.get() == nullptr) {
149         AUDIO_ERR_LOG("AudioVolumeKeyEventNapi: OnJsCallBackInterrupt: jsCb.get() is null");
150         delete work;
151         return;
152     }
153     work->data = reinterpret_cast<void *>(jsCb.get());
154 
155     int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) {
156         // Js Thread
157         AudioVolumeKeyEventJsCallback *event = reinterpret_cast<AudioVolumeKeyEventJsCallback *>(work->data);
158         std::string request = event->callbackName;
159         napi_env env = event->callback->env_;
160         napi_ref callback = event->callback->cb_;
161         AUDIO_DEBUG_LOG("AudioVolumeKeyEventNapi: JsCallBack %{public}s, uv_queue_work start", request.c_str());
162         do {
163             CHECK_AND_BREAK_LOG(status != UV_ECANCELED, "%{public}s canceled", request.c_str());
164 
165             napi_value jsCallback = nullptr;
166             napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
167             CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
168                 request.c_str());
169 
170             // Call back function
171             napi_value args[1] = { nullptr };
172             NativeVolumeEventToJsObj(env, args[0], event->volumeEvent);
173             CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[0] != nullptr,
174                 "%{public}s fail to create Interrupt callback", request.c_str());
175 
176             const size_t argCount = 1;
177             napi_value result = nullptr;
178             nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
179             CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to call Interrupt callback", request.c_str());
180         } while (0);
181         delete event;
182         delete work;
183     });
184     if (ret != 0) {
185         AUDIO_ERR_LOG("Failed to execute libuv work queue");
186         delete work;
187     } else {
188         jsCb.release();
189     }
190 }
191 } // namespace AudioStandard
192 } // namespace OHOS
193