• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "NapiAudioSystemVolumeChangeCallback"
17 #endif
18 
19 #include "js_native_api.h"
20 #include "napi_audio_system_volume_change_callback.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 {
NapiAudioSystemVolumeChangeCallback(napi_env env)29 NapiAudioSystemVolumeChangeCallback::NapiAudioSystemVolumeChangeCallback(napi_env env)
30     :env_(env)
31 {
32     AUDIO_INFO_LOG("Constructor");
33 }
34 
~NapiAudioSystemVolumeChangeCallback()35 NapiAudioSystemVolumeChangeCallback::~NapiAudioSystemVolumeChangeCallback()
36 {
37     AUDIO_INFO_LOG("Destructor");
38 }
39 
CreateSystemVolumeChangeTsfn(napi_env env)40 void NapiAudioSystemVolumeChangeCallback::CreateSystemVolumeChangeTsfn(napi_env env)
41 {
42     regVolumeTsfn_ = true;
43     napi_value cbName;
44     std::string callbackName = "systemVolumeChange";
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         SystemVolumeChangeTsfnFinalize, nullptr, SafeJsCallbackSystemVolumeChangeWork, &amVolEntTsfn_);
49 }
50 
GetVolumeTsfnFlag()51 bool NapiAudioSystemVolumeChangeCallback::GetVolumeTsfnFlag()
52 {
53     return regVolumeTsfn_;
54 }
55 
GetTsfn()56 napi_threadsafe_function NapiAudioSystemVolumeChangeCallback::GetTsfn()
57 {
58     std::lock_guard<std::mutex> lock(mutex_);
59     return amVolEntTsfn_;
60 }
61 
OnSystemVolumeChange(VolumeEvent volumeEvent)62 void NapiAudioSystemVolumeChangeCallback::OnSystemVolumeChange(VolumeEvent volumeEvent)
63 {
64     std::lock_guard<std::mutex> lock(mutex_);
65     AUDIO_PRERELEASE_LOGI("OnSystemVolumeChange is called volumeType=%{public}d, volumeLevel=%{public}d,"
66         "isUpdateUi=%{public}d", volumeEvent.volumeType, volumeEvent.volume, volumeEvent.updateUi);
67     CHECK_AND_RETURN_LOG(audioSystemVolumeChangeCallback_ != nullptr,
68         "NapiAudioSystemVolumeChangeCallback:No JS callback registered return");
69     std::unique_ptr<AudioSystemVolumeChangeJsCallback> cb = std::make_unique<AudioSystemVolumeChangeJsCallback>();
70     CHECK_AND_RETURN_LOG(cb != nullptr, "No memory");
71     cb->callback = audioSystemVolumeChangeCallback_;
72     cb->callbackName = AUDIO_SYSTEM_VOLUME_CHANGE_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 OnJsCallbackSystemVolumeChange(cb);
80 }
81 
SaveCallbackReference(const std::string & callbackName,napi_value args)82 void NapiAudioSystemVolumeChangeCallback::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         "NapiAudioSystemVolumeChangeCallback: creating reference for callback fail");
90     callback_ = callback;
91     std::shared_ptr<AutoRef> cb = std::make_shared<AutoRef>(env_, callback);
92     if (callbackName == AUDIO_SYSTEM_VOLUME_CHANGE_CALLBACK_NAME) {
93         audioSystemVolumeChangeCallback_ = cb;
94     } else {
95         AUDIO_ERR_LOG("NapiAudioSystemVolumeChangeCallback: Unknown callback type: %{public}s", callbackName.c_str());
96     }
97 }
98 
SafeJsCallbackSystemVolumeChangeWork(napi_env env,napi_value js_cb,void * context,void * data)99 void NapiAudioSystemVolumeChangeCallback::SafeJsCallbackSystemVolumeChangeWork(
100     napi_env env, napi_value js_cb, void *context, void *data)
101 {
102     AudioSystemVolumeChangeJsCallback *event = reinterpret_cast<AudioSystemVolumeChangeJsCallback *>(data);
103     CHECK_AND_RETURN_LOG((event != nullptr) && (event->callback != nullptr),
104         "OnJsCallbackSystemVolumeChange: no memory");
105     std::shared_ptr<AudioSystemVolumeChangeJsCallback> safeContext(
106         static_cast<AudioSystemVolumeChangeJsCallback*>(event),
107         [](AudioSystemVolumeChangeJsCallback *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 
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 
SystemVolumeChangeTsfnFinalize(napi_env env,void * data,void * hint)135 void NapiAudioSystemVolumeChangeCallback::SystemVolumeChangeTsfnFinalize(napi_env env, void *data, void *hint)
136 {
137     AUDIO_INFO_LOG("SystemVolumeChangeTsfnFinalize: CleanUp is removed.");
138     NapiAudioSystemVolumeChangeCallback *context = reinterpret_cast<NapiAudioSystemVolumeChangeCallback*>(data);
139     napi_remove_env_cleanup_hook(env, NapiAudioSystemVolumeChangeCallback::CleanUp, context);
140     if (context->GetTsfn() != nullptr) {
141         AUDIO_INFO_LOG("SystemVolumeChangeTsfnFinalize: context is released.");
142         delete context;
143     }
144 }
145 
CleanUp(void * data)146 void NapiAudioSystemVolumeChangeCallback::CleanUp(void *data)
147 {
148     NapiAudioSystemVolumeChangeCallback *context = reinterpret_cast<NapiAudioSystemVolumeChangeCallback*>(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 
OnJsCallbackSystemVolumeChange(std::unique_ptr<AudioSystemVolumeChangeJsCallback> & jsCb)157 void NapiAudioSystemVolumeChangeCallback::OnJsCallbackSystemVolumeChange(
158     std::unique_ptr<AudioSystemVolumeChangeJsCallback> &jsCb)
159 {
160     if (jsCb.get() == nullptr) {
161         AUDIO_ERR_LOG("OnJsCallbackSystemVolumeChange: jsCb.get() is null");
162         return;
163     }
164 
165     AudioSystemVolumeChangeJsCallback *event = jsCb.release();
166     CHECK_AND_RETURN_LOG((event != nullptr) && (event->callback != nullptr), "event is nullptr.");
167     if (amVolEntTsfn_ == nullptr) {
168         AUDIO_INFO_LOG("OnJsCallbackSystemVolumeChange: tsfn nullptr.");
169         return;
170     }
171     napi_acquire_threadsafe_function(amVolEntTsfn_);
172     napi_call_threadsafe_function(amVolEntTsfn_, event, napi_tsfn_blocking);
173 }
174 
ContainSameJsCallback(napi_value args)175 bool NapiAudioSystemVolumeChangeCallback::ContainSameJsCallback(napi_value args)
176 {
177     bool isEquals = false;
178     napi_value copyValue = nullptr;
179 
180     napi_get_reference_value(env_, callback_, &copyValue);
181     CHECK_AND_RETURN_RET_LOG(args != nullptr, false, "args is nullptr");
182     CHECK_AND_RETURN_RET_LOG(napi_strict_equals(env_, copyValue, args, &isEquals) == napi_ok, false,
183         "Get napi_strict_equals failed");
184 
185     return isEquals;
186 }
187 } // namespace AudioStandard
188 } // namespace OHOS
189