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 "NapiAudioSceneChangedCallback"
17 #endif
18
19 #include "js_native_api.h"
20 #include "napi_audio_scene_callbacks.h"
21 #include "audio_errors.h"
22 #include "audio_manager_log.h"
23 #include "napi_param_utils.h"
24 #include "napi_audio_error.h"
25 #include "napi_audio_enum.h"
26 #include "napi/native_api.h"
27
28 namespace OHOS {
29 namespace AudioStandard {
IsSameCallback(napi_env env,napi_value callback,napi_ref refCallback)30 bool NapiAudioSceneChangedCallback::IsSameCallback(napi_env env, napi_value callback, napi_ref refCallback)
31 {
32 bool isEquals = false;
33 napi_value copyValue = nullptr;
34
35 napi_get_reference_value(env, refCallback, ©Value);
36 if (napi_strict_equals(env, copyValue, callback, &isEquals) != napi_ok) {
37 AUDIO_ERR_LOG("get napi_strict_equals failed");
38 return false;
39 }
40
41 return isEquals;
42 }
43
NapiAudioSceneChangedCallback(napi_env env)44 NapiAudioSceneChangedCallback::NapiAudioSceneChangedCallback(napi_env env)
45 : env_(env)
46 {
47 AUDIO_DEBUG_LOG("instance create");
48 }
49
~NapiAudioSceneChangedCallback()50 NapiAudioSceneChangedCallback::~NapiAudioSceneChangedCallback()
51 {
52 if (regAmSceneChgTsfn_) {
53 napi_release_threadsafe_function(amSceneChgTsfn_, napi_tsfn_abort);
54 }
55 AUDIO_DEBUG_LOG("instance destroy");
56 }
57
SaveCallbackReference(const std::string & callbackName,napi_value callback)58 void NapiAudioSceneChangedCallback::SaveCallbackReference(const std::string &callbackName, napi_value callback)
59 {
60 std::lock_guard<std::mutex> lock(mutex_);
61 napi_ref callbackRef = nullptr;
62 const int32_t refCount = ARGS_ONE;
63
64 CHECK_AND_RETURN_LOG(callbackName == AUDIO_SCENE_CHANGE_CALLBACK_NAME,
65 "Unknown callback type: %{public}s", callbackName.c_str());
66 for (auto &item : audioSceneChangeCbList_) {
67 if (item == nullptr) {
68 continue;
69 }
70 bool isSameCallback = IsSameCallback(env_, callback, item->cb_);
71 CHECK_AND_RETURN_LOG(!isSameCallback, "has same callback, nothing to do");
72 }
73
74 napi_status status = napi_create_reference(env_, callback, refCount, &callbackRef);
75 CHECK_AND_RETURN_LOG(status == napi_ok && callbackRef != nullptr, "creating reference for callback fail");
76 std::shared_ptr<AutoRef> cb = std::make_shared<AutoRef>(env_, callbackRef);
77 audioSceneChangeCbList_.push_back(cb);
78 AUDIO_INFO_LOG("save callback ref success, list size [%{public}zu]", audioSceneChangeCbList_.size());
79 }
80
RemoveCallbackReference(napi_env env,napi_value callback)81 void NapiAudioSceneChangedCallback::RemoveCallbackReference(napi_env env, napi_value callback)
82 {
83 std::lock_guard<std::mutex> lock(mutex_);
84 for (auto it = audioSceneChangeCbList_.begin(); it != audioSceneChangeCbList_.end(); ++it) {
85 std::shared_ptr<AutoRef> temp = (*it);
86 if (temp == nullptr) {
87 continue;
88 }
89 bool isSameCallback = IsSameCallback(env_, callback, temp->cb_);
90 if (isSameCallback) {
91 AUDIO_INFO_LOG("find audioSceneChanged callback, remove it");
92 napi_delete_reference(env_, temp->cb_);
93 temp->cb_ = nullptr;
94 audioSceneChangeCbList_.erase(it);
95 return;
96 }
97 }
98 AUDIO_INFO_LOG("remove audioSceneChanged callback no find");
99 }
100
RemoveAllCallbackReference()101 void NapiAudioSceneChangedCallback::RemoveAllCallbackReference()
102 {
103 std::lock_guard<std::mutex> lock(mutex_);
104 for (auto &item : audioSceneChangeCbList_) {
105 if (item == nullptr) {
106 continue;
107 }
108 napi_delete_reference(env_, item->cb_);
109 item->cb_ = nullptr;
110 }
111 audioSceneChangeCbList_.clear();
112 AUDIO_INFO_LOG("remove all js callback success");
113 }
114
GetAudioSceneCbListSize()115 int32_t NapiAudioSceneChangedCallback::GetAudioSceneCbListSize()
116 {
117 std::lock_guard<std::mutex> lock(mutex_);
118 return static_cast<int32_t>(audioSceneChangeCbList_.size());
119 }
120
OnAudioSceneChange(const AudioScene audioScene)121 void NapiAudioSceneChangedCallback::OnAudioSceneChange(const AudioScene audioScene)
122 {
123 std::lock_guard<std::mutex> lock(mutex_);
124 AUDIO_INFO_LOG("audioScene status [%{public}d]", audioScene);
125
126 for (auto &item : audioSceneChangeCbList_) {
127 std::unique_ptr<AudioSceneJsCallback> cb = std::make_unique<AudioSceneJsCallback>();
128 CHECK_AND_RETURN_LOG(cb != nullptr, "no memory");
129 cb->callback = item;
130 cb->callbackName = AUDIO_SCENE_CHANGE_CALLBACK_NAME;
131 cb->audioScene = NapiAudioEnum::GetJsAudioScene(audioScene);
132 OnJsCallbackAudioSceneChange(cb);
133 }
134 }
135
SafeJsCallbackAudioSceneChangeWork(napi_env env,napi_value js_cb,void * context,void * data)136 void NapiAudioSceneChangedCallback::SafeJsCallbackAudioSceneChangeWork(napi_env env, napi_value js_cb,
137 void *context, void *data)
138 {
139 AudioSceneJsCallback *event = reinterpret_cast<AudioSceneJsCallback *>(data);
140 CHECK_AND_RETURN_LOG((event != nullptr) && (event->callback != nullptr), "event or event->callback is nullptr");
141 std::shared_ptr<AudioSceneJsCallback> safeContext(
142 static_cast<AudioSceneJsCallback*>(data),
143 [](AudioSceneJsCallback *ptr) {
144 delete ptr;
145 });
146 std::string request = event->callbackName;
147 napi_ref callback = event->callback->cb_;
148 napi_handle_scope scope = nullptr;
149 napi_open_handle_scope(env, &scope);
150 CHECK_AND_RETURN_LOG(scope != nullptr, "scope is nullptr");
151 AUDIO_INFO_LOG("safe js callback working.");
152 do {
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 napi_value args[ARGS_ONE] = { nullptr };
158 NapiParamUtils::SetValueInt32(env, event->audioScene, args[PARAM0]);
159 CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[PARAM0] != nullptr,
160 "%{public}s fail to create audioScene callback", request.c_str());
161 const size_t argCount = ARGS_ONE;
162 napi_value result = nullptr;
163 nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
164 CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to call audioScene callback", request.c_str());
165 } while (0);
166 napi_close_handle_scope(env, scope);
167 }
168
AudioSceneChangeTsfnFinalize(napi_env env,void * data,void * hint)169 void NapiAudioSceneChangedCallback::AudioSceneChangeTsfnFinalize(napi_env env, void *data, void *hint)
170 {
171 AUDIO_INFO_LOG("safe thread resource release.");
172 }
173
OnJsCallbackAudioSceneChange(std::unique_ptr<AudioSceneJsCallback> & jsCb)174 void NapiAudioSceneChangedCallback::OnJsCallbackAudioSceneChange(std::unique_ptr<AudioSceneJsCallback> &jsCb)
175 {
176 if (jsCb.get() == nullptr) {
177 AUDIO_ERR_LOG("jsCb.get() is null");
178 return;
179 }
180
181 AudioSceneJsCallback *event = jsCb.release();
182 CHECK_AND_RETURN_LOG((event != nullptr) && (event->callback != nullptr), "event or event->callback is nullptr");
183
184 napi_acquire_threadsafe_function(amSceneChgTsfn_);
185 napi_call_threadsafe_function(amSceneChgTsfn_, event, napi_tsfn_blocking);
186 }
187
CreateSceneChgTsfn(napi_env env)188 void NapiAudioSceneChangedCallback::CreateSceneChgTsfn(napi_env env)
189 {
190 if (regAmSceneChgTsfn_) {
191 AUDIO_INFO_LOG("amSceneChgTsfn_ has been created");
192 return;
193 }
194 regAmSceneChgTsfn_ = true;
195 napi_value cbName;
196 std::string callbackName = "AudioSceneChange";
197 napi_create_string_utf8(env, callbackName.c_str(), callbackName.length(), &cbName);
198 napi_create_threadsafe_function(env, nullptr, nullptr, cbName, 0, 1, nullptr,
199 AudioSceneChangeTsfnFinalize, nullptr, SafeJsCallbackAudioSceneChangeWork, &amSceneChgTsfn_);
200 }
201
GetSceneChgTsfnFlag() const202 bool NapiAudioSceneChangedCallback::GetSceneChgTsfnFlag() const
203 {
204 return regAmSceneChgTsfn_;
205 }
206 } // namespace AudioStandard
207 } // namespace OHOS