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