• 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 "media_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     MEDIA_DEBUG_LOG("AudioVolumeKeyEventNapi::Constructor");
32 }
33 
~AudioVolumeKeyEventNapi()34 AudioVolumeKeyEventNapi::~AudioVolumeKeyEventNapi()
35 {
36     MEDIA_DEBUG_LOG("AudioVolumeKeyEventNapi::Destructor");
37 }
38 
OnVolumeKeyEvent(AudioStreamType streamType,int32_t volumeLevel,bool isUpdateUi)39 void AudioVolumeKeyEventNapi::OnVolumeKeyEvent(AudioStreamType streamType, int32_t volumeLevel, bool isUpdateUi)
40 {
41     std::lock_guard<std::mutex> lock(mutex_);
42     MEDIA_DEBUG_LOG("AudioVolumeKeyEventNapi: OnVolumeKeyEvent is called volumeLevel=%{public}d", volumeLevel);
43     MEDIA_DEBUG_LOG("AudioVolumeKeyEventNapi: isUpdateUi is called isUpdateUi=%{public}d", isUpdateUi);
44     if (audioVolumeKeyEventJsCallback_ == nullptr) {
45         MEDIA_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 = streamType;
53     cb->volumeEvent.volume = volumeLevel;
54     cb->volumeEvent.updateUi = isUpdateUi;
55 
56     return OnJsCallbackVolumeEvent(cb);
57 }
58 
SaveCallbackReference(const std::string & callbackName,napi_value args)59 void AudioVolumeKeyEventNapi::SaveCallbackReference(const std::string &callbackName, napi_value args)
60 {
61     std::lock_guard<std::mutex> lock(mutex_);
62     napi_ref callback = nullptr;
63     const int32_t refCount = 1;
64     napi_status status = napi_create_reference(env_, args, refCount, &callback);
65     CHECK_AND_RETURN_LOG(status == napi_ok && callback != nullptr,
66                          "AudioVolumeKeyEventNapi: creating reference for callback fail");
67     std::shared_ptr<VolumeKeyEventAutoRef> cb = std::make_shared<VolumeKeyEventAutoRef>(env_, callback);
68     if (callbackName == VOLUME_KEY_EVENT_CALLBACK_NAME) {
69         audioVolumeKeyEventJsCallback_ = cb;
70     } else {
71         MEDIA_ERR_LOG("AudioVolumeKeyEventNapi: Unknown callback type: %{public}s", callbackName.c_str());
72     }
73 }
74 
SetValueInt32(const napi_env & env,const std::string & fieldStr,const int intValue,napi_value & result)75 static void SetValueInt32(const napi_env& env, const std::string& fieldStr, const int intValue, napi_value& result)
76 {
77     napi_value value = nullptr;
78     napi_create_int32(env, intValue, &value);
79     napi_set_named_property(env, result, fieldStr.c_str(), value);
80 }
81 
SetValueBoolean(const napi_env & env,const std::string & fieldStr,const bool boolValue,napi_value & result)82 static void SetValueBoolean(const napi_env& env, const std::string& fieldStr, const bool boolValue, napi_value& result)
83 {
84     napi_value value = nullptr;
85     napi_get_boolean(env, boolValue, &value);
86     napi_set_named_property(env, result, fieldStr.c_str(), value);
87 }
88 
GetJsAudioVolumeType(AudioStreamType streamType)89 static int32_t GetJsAudioVolumeType(AudioStreamType streamType)
90 {
91     int32_t nativeStreamType = static_cast<int32_t>(streamType);
92     int32_t result = AudioManagerNapi::VOLUMETYPE_DEFAULT;
93     switch (nativeStreamType) {
94         case AudioStreamType::STREAM_MUSIC:
95             result = AudioManagerNapi::MEDIA;
96             break;
97         case AudioStreamType::STREAM_VOICE_CALL:
98             result = AudioManagerNapi::VOICE_CALL;
99             break;
100         case AudioStreamType::STREAM_RING:
101             result = AudioManagerNapi::RINGTONE;
102             break;
103         case AudioStreamType::STREAM_VOICE_ASSISTANT:
104             result = AudioManagerNapi::VOICE_ASSISTANT;
105             break;
106         default:
107             result = AudioManagerNapi::VOLUMETYPE_DEFAULT;
108             break;
109     }
110     return result;
111 }
112 
NativeVolumeEventToJsObj(const napi_env & env,napi_value & jsObj,const VolumeEvent & volumeEvent)113 static void NativeVolumeEventToJsObj(const napi_env& env, napi_value& jsObj,
114     const VolumeEvent& volumeEvent)
115 {
116     napi_create_object(env, &jsObj);
117     SetValueInt32(env, "volumeType", GetJsAudioVolumeType(volumeEvent.volumeType), jsObj);
118     SetValueInt32(env, "volume", static_cast<int32_t>(volumeEvent.volume), jsObj);
119     SetValueBoolean(env, "updateUi", volumeEvent.updateUi, jsObj);
120 }
121 
OnJsCallbackVolumeEvent(std::unique_ptr<AudioVolumeKeyEventJsCallback> & jsCb)122 void AudioVolumeKeyEventNapi::OnJsCallbackVolumeEvent(std::unique_ptr<AudioVolumeKeyEventJsCallback> &jsCb)
123 {
124     MEDIA_INFO_LOG("AudioVolumeKeyEventNapi:OnJsCallbackVolumeEvent");
125     uv_loop_s *loop = nullptr;
126     napi_get_uv_event_loop(env_, &loop);
127     if (loop == nullptr) {
128         return;
129     }
130 
131     uv_work_t *work = new(std::nothrow) uv_work_t;
132     if (work == nullptr) {
133         MEDIA_ERR_LOG("AudioVolumeKeyEventNapi: OnJsCallBackInterrupt: No memory");
134         return;
135     }
136     if (jsCb.get() == nullptr) {
137         MEDIA_ERR_LOG("AudioVolumeKeyEventNapi: OnJsCallBackInterrupt: jsCb.get() is null");
138         delete work;
139         return;
140     }
141     work->data = reinterpret_cast<void *>(jsCb.get());
142 
143     int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) {
144         // Js Thread
145         AudioVolumeKeyEventJsCallback *event = reinterpret_cast<AudioVolumeKeyEventJsCallback *>(work->data);
146         std::string request = event->callbackName;
147         napi_env env = event->callback->env_;
148         napi_ref callback = event->callback->cb_;
149         MEDIA_DEBUG_LOG("AudioVolumeKeyEventNapi: JsCallBack %{public}s, uv_queue_work start", request.c_str());
150         do {
151             CHECK_AND_BREAK_LOG(status != UV_ECANCELED, "%{public}s canceled", request.c_str());
152 
153             napi_value jsCallback = nullptr;
154             napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
155             CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
156                 request.c_str());
157 
158             // Call back function
159             napi_value args[1] = { nullptr };
160             NativeVolumeEventToJsObj(env, args[0], event->volumeEvent);
161             CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[0] != nullptr,
162                 "%{public}s fail to create Interrupt callback", request.c_str());
163 
164             const size_t argCount = 1;
165             napi_value result = nullptr;
166             nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
167             CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to call Interrupt callback", request.c_str());
168         } while (0);
169         delete event;
170         delete work;
171     });
172     if (ret != 0) {
173         MEDIA_ERR_LOG("Failed to execute libuv work queue");
174         delete work;
175     } else {
176         jsCb.release();
177     }
178 }
179 } // namespace AudioStandard
180 } // namespace OHOS
181