• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #ifndef LOG_TAG
16 #define LOG_TAG "NapiAudioVolumeKeyEvent"
17 #endif
18 
19 #include "js_native_api.h"
20 #include "napi_audio_volume_key_event.h"
21 #include "napi_audio_enum.h"
22 #include "napi_audio_error.h"
23 #include "napi_param_utils.h"
24 #include "audio_manager_log.h"
25 
26 using namespace std;
27 namespace OHOS {
28 namespace AudioStandard {
NapiAudioVolumeKeyEvent(napi_env env)29 NapiAudioVolumeKeyEvent::NapiAudioVolumeKeyEvent(napi_env env)
30     :env_(env)
31 {
32     AUDIO_INFO_LOG("NapiAudioVolumeKeyEvent::Constructor");
33 }
34 
~NapiAudioVolumeKeyEvent()35 NapiAudioVolumeKeyEvent::~NapiAudioVolumeKeyEvent()
36 {
37     AUDIO_INFO_LOG("NapiAudioVolumeKeyEvent::Destructor");
38 }
39 
CreateVolumeTsfn(napi_env env)40 void NapiAudioVolumeKeyEvent::CreateVolumeTsfn(napi_env env)
41 {
42     regVolumeTsfn_ = true;
43     napi_value cbName;
44     std::string callbackName = "volumeChange";
45     napi_create_string_utf8(env, callbackName.c_str(), callbackName.length(), &cbName);
46     napi_add_env_cleanup_hook(env, Cleanup, this);
47     napi_create_threadsafe_function(env, nullptr, nullptr, cbName, 0, 1, this,
48         VolumeEventTsfnFinalize, nullptr, SafeJsCallbackVolumeEventWork, &amVolEntTsfn_);
49 }
50 
GetVolumeTsfnFlag()51 bool NapiAudioVolumeKeyEvent::GetVolumeTsfnFlag()
52 {
53     return regVolumeTsfn_;
54 }
55 
GetTsfn()56 napi_threadsafe_function NapiAudioVolumeKeyEvent::GetTsfn()
57 {
58     std::lock_guard<std::mutex> lock(mutex_);
59     return amVolEntTsfn_;
60 }
61 
OnVolumeKeyEvent(VolumeEvent volumeEvent)62 void NapiAudioVolumeKeyEvent::OnVolumeKeyEvent(VolumeEvent volumeEvent)
63 {
64     std::lock_guard<std::mutex> lock(mutex_);
65     AUDIO_PRERELEASE_LOGI("OnVolumeKeyEvent is called volumeType=%{public}d, volumeLevel=%{public}d,"
66         "isUpdateUi=%{public}d", volumeEvent.volumeType, volumeEvent.volume, volumeEvent.updateUi);
67     CHECK_AND_RETURN_LOG(audioVolumeKeyEventJsCallback_ != nullptr,
68         "NapiAudioVolumeKeyEvent:No JS callback registered return");
69     std::unique_ptr<AudioVolumeKeyEventJsCallback> cb = std::make_unique<AudioVolumeKeyEventJsCallback>();
70     CHECK_AND_RETURN_LOG(cb != nullptr, "No memory");
71     cb->callback = audioVolumeKeyEventJsCallback_;
72     cb->callbackName = VOLUME_KEY_EVENT_CALLBACK_NAME;
73     cb->volumeEvent.volumeType = volumeEvent.volumeType;
74     cb->volumeEvent.volume = volumeEvent.volume;
75     cb->volumeEvent.updateUi = volumeEvent.updateUi;
76     cb->volumeEvent.volumeGroupId = volumeEvent.volumeGroupId;
77     cb->volumeEvent.networkId = volumeEvent.networkId;
78 
79     return OnJsCallbackVolumeEvent(cb);
80 }
81 
SaveCallbackReference(const std::string & callbackName,napi_value args)82 void NapiAudioVolumeKeyEvent::SaveCallbackReference(const std::string &callbackName, napi_value args)
83 {
84     std::lock_guard<std::mutex> lock(mutex_);
85     napi_ref callback = nullptr;
86     const int32_t refCount = 1;
87     napi_status status = napi_create_reference(env_, args, refCount, &callback);
88     CHECK_AND_RETURN_LOG(status == napi_ok && callback != nullptr,
89         "NapiAudioVolumeKeyEvent: creating reference for callback fail");
90     callback_ = callback;
91     std::shared_ptr<AutoRef> cb = std::make_shared<AutoRef>(env_, callback);
92     if (callbackName == VOLUME_KEY_EVENT_CALLBACK_NAME) {
93         audioVolumeKeyEventJsCallback_ = cb;
94     } else {
95         AUDIO_ERR_LOG("NapiAudioVolumeKeyEvent: Unknown callback type: %{public}s", callbackName.c_str());
96     }
97 }
98 
SafeJsCallbackVolumeEventWork(napi_env env,napi_value js_cb,void * context,void * data)99 void NapiAudioVolumeKeyEvent::SafeJsCallbackVolumeEventWork(napi_env env, napi_value js_cb, void *context, void *data)
100 {
101     AudioVolumeKeyEventJsCallback *event = reinterpret_cast<AudioVolumeKeyEventJsCallback *>(data);
102     CHECK_AND_RETURN_LOG((event != nullptr) && (event->callback != nullptr),
103         "OnJsCallbackVolumeEvent: no memory");
104     std::shared_ptr<AudioVolumeKeyEventJsCallback> safeContext(
105         static_cast<AudioVolumeKeyEventJsCallback*>(event),
106         [](AudioVolumeKeyEventJsCallback *ptr) {
107             delete ptr;
108     });
109     std::string request = event->callbackName;
110     napi_ref callback = event->callback->cb_;
111     napi_handle_scope scope = nullptr;
112     napi_open_handle_scope(env, &scope);
113     CHECK_AND_RETURN_LOG(scope != nullptr, "scope is nullptr");
114     AUDIO_INFO_LOG("SafeJsCallbackVolumeEventWork: safe js callback working.");
115 
116     do {
117         napi_value jsCallback = nullptr;
118         napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
119         CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
120             request.c_str());
121         napi_value args[ARGS_ONE] = { nullptr };
122         NapiParamUtils::SetValueVolumeEvent(env, event->volumeEvent, args[PARAM0]);
123         CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[PARAM0] != nullptr,
124             "%{public}s fail to create volumeChange callback", request.c_str());
125 
126         const size_t argCount = ARGS_ONE;
127         napi_value result = nullptr;
128         nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
129         CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to call volumeChange callback",
130             request.c_str());
131     } while (0);
132     napi_close_handle_scope(env, scope);
133 }
134 
VolumeEventTsfnFinalize(napi_env env,void * data,void * hint)135 void NapiAudioVolumeKeyEvent::VolumeEventTsfnFinalize(napi_env env, void *data, void *hint)
136 {
137     AUDIO_INFO_LOG("VolumeEventTsfnFinalize: Cleanup is removed.");
138     NapiAudioVolumeKeyEvent *context = reinterpret_cast<NapiAudioVolumeKeyEvent*>(data);
139     napi_remove_env_cleanup_hook(env, NapiAudioVolumeKeyEvent::Cleanup, context);
140     if (context->GetTsfn() != nullptr) {
141         AUDIO_INFO_LOG("VolumeEventTsfnFinalize: context is released.");
142         delete context;
143     }
144 }
145 
Cleanup(void * data)146 void NapiAudioVolumeKeyEvent::Cleanup(void *data)
147 {
148     NapiAudioVolumeKeyEvent *context = reinterpret_cast<NapiAudioVolumeKeyEvent*>(data);
149     napi_threadsafe_function tsfn = context->GetTsfn();
150     std::unique_lock<std::mutex> lock(context->mutex_);
151     context->amVolEntTsfn_ = nullptr;
152     lock.unlock();
153     AUDIO_INFO_LOG("Cleanup: safe thread resource release.");
154     napi_release_threadsafe_function(tsfn, napi_tsfn_abort);
155 }
156 
OnJsCallbackVolumeEvent(std::unique_ptr<AudioVolumeKeyEventJsCallback> & jsCb)157 void NapiAudioVolumeKeyEvent::OnJsCallbackVolumeEvent(std::unique_ptr<AudioVolumeKeyEventJsCallback> &jsCb)
158 {
159     if (jsCb.get() == nullptr) {
160         AUDIO_ERR_LOG("OnJsCallbackVolumeEvent: jsCb.get() is null");
161         return;
162     }
163 
164     AudioVolumeKeyEventJsCallback *event = jsCb.release();
165     CHECK_AND_RETURN_LOG((event != nullptr) && (event->callback != nullptr), "event is nullptr.");
166     if (amVolEntTsfn_ == nullptr) {
167         AUDIO_INFO_LOG("OnJsCallbackVolumeEvent: tsfn nullptr.");
168         return;
169     }
170     napi_acquire_threadsafe_function(amVolEntTsfn_);
171     napi_call_threadsafe_function(amVolEntTsfn_, event, napi_tsfn_blocking);
172 }
173 
ContainSameJsCallback(napi_value args)174 bool NapiAudioVolumeKeyEvent::ContainSameJsCallback(napi_value args)
175 {
176     bool isEquals = false;
177     napi_value copyValue = nullptr;
178 
179     napi_get_reference_value(env_, callback_, &copyValue);
180     CHECK_AND_RETURN_RET_LOG(args != nullptr, false, "args is nullptr");
181     CHECK_AND_RETURN_RET_LOG(napi_strict_equals(env_, copyValue, args, &isEquals) == napi_ok, false,
182         "Get napi_strict_equals failed");
183 
184     return isEquals;
185 }
186 } // namespace AudioStandard
187 } // namespace OHOS