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 "NapiAudioRingerModeCallback"
17 #endif
18
19 #include "js_native_api.h"
20 #include "napi_audio_ringermode_callback.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 {
NapiAudioRingerModeCallback(napi_env env)30 NapiAudioRingerModeCallback::NapiAudioRingerModeCallback(napi_env env)
31 : env_(env)
32 {
33 AUDIO_DEBUG_LOG("instance create");
34 }
35
~NapiAudioRingerModeCallback()36 NapiAudioRingerModeCallback::~NapiAudioRingerModeCallback()
37 {
38 if (regAmRmChgTsfn_) {
39 napi_release_threadsafe_function(amRmChgTsfn_, napi_tsfn_abort);
40 }
41 AUDIO_DEBUG_LOG("instance destroy");
42 }
43
SaveCallbackReference(const std::string & callbackName,napi_value args)44 void NapiAudioRingerModeCallback::SaveCallbackReference(const std::string &callbackName, napi_value args)
45 {
46 std::lock_guard<std::mutex> lock(mutex_);
47 napi_ref callback = nullptr;
48 const int32_t refCount = ARGS_ONE;
49 napi_status status = napi_create_reference(env_, args, refCount, &callback);
50 CHECK_AND_RETURN_LOG(status == napi_ok && callback != nullptr,
51 "NapiAudioRingerModeCallback: creating reference for callback fail");
52 std::shared_ptr<AutoRef> cb = std::make_shared<AutoRef>(env_, callback);
53 if (callbackName == RINGERMODE_CALLBACK_NAME) {
54 ringerModeCallback_ = cb;
55 } else {
56 AUDIO_ERR_LOG("NapiAudioRingerModeCallback: Unknown callback type: %{public}s", callbackName.c_str());
57 }
58 }
59
CreateRingModeTsfn(napi_env env)60 void NapiAudioRingerModeCallback::CreateRingModeTsfn(napi_env env)
61 {
62 regAmRmChgTsfn_ = true;
63 std::string callbackName = "RingerMode";
64 napi_value cbName;
65 napi_create_string_utf8(env, callbackName.c_str(), callbackName.length(), &cbName);
66 napi_create_threadsafe_function(env, nullptr, nullptr, cbName, 0, 1, nullptr, RingModeTsfnFinalize,
67 nullptr, SafeJsCallbackRingModeWork, &amRmChgTsfn_);
68 }
69
GetRingModeTsfnFlag()70 bool NapiAudioRingerModeCallback::GetRingModeTsfnFlag()
71 {
72 return regAmRmChgTsfn_;
73 }
74
RemoveCallbackReference(const napi_value args)75 void NapiAudioRingerModeCallback::RemoveCallbackReference(const napi_value args)
76 {
77 if (!IsSameCallback(args)) {
78 return;
79 }
80 std::lock_guard<std::mutex> lock(mutex_);
81 ringerModeCallback_->cb_ = nullptr;
82 ringerModeCallback_ = nullptr;
83 AUDIO_INFO_LOG("Remove callback reference successful.");
84 }
85
IsSameCallback(const napi_value args)86 bool NapiAudioRingerModeCallback::IsSameCallback(const napi_value args)
87 {
88 std::lock_guard<std::mutex> lock(mutex_);
89 if (ringerModeCallback_ == nullptr) {
90 return false;
91 }
92 if (args == nullptr) {
93 return true;
94 }
95 napi_value ringerModeCallback = nullptr;
96 napi_get_reference_value(env_, ringerModeCallback_->cb_, &ringerModeCallback);
97 bool isEquals = false;
98 CHECK_AND_RETURN_RET_LOG(napi_strict_equals(env_, args, ringerModeCallback, &isEquals) == napi_ok, false,
99 "get napi_strict_equals failed");
100 return isEquals;
101 }
102
OnRingerModeUpdated(const AudioRingerMode & ringerMode)103 void NapiAudioRingerModeCallback::OnRingerModeUpdated(const AudioRingerMode &ringerMode)
104 {
105 std::lock_guard<std::mutex> lock(mutex_);
106 AUDIO_INFO_LOG("NapiAudioRingerModeCallback: ringer mode: %{public}d", ringerMode);
107 CHECK_AND_RETURN_LOG(ringerModeCallback_ != nullptr, "Cannot find the reference of ringer mode callback");
108
109 std::unique_ptr<AudioRingerModeJsCallback> cb = std::make_unique<AudioRingerModeJsCallback>();
110 CHECK_AND_RETURN_LOG(cb != nullptr, "No memory");
111 cb->callback = ringerModeCallback_;
112 cb->callbackName = RINGERMODE_CALLBACK_NAME;
113 cb->ringerMode = ringerMode;
114 return OnJsCallbackRingerMode(cb);
115 }
116
GetJsAudioRingMode(int32_t ringerMode)117 static NapiAudioEnum::AudioRingMode GetJsAudioRingMode(int32_t ringerMode)
118 {
119 NapiAudioEnum::AudioRingMode result;
120
121 switch (ringerMode) {
122 case RINGER_MODE_SILENT:
123 result = NapiAudioEnum::RINGER_MODE_SILENT;
124 break;
125 case RINGER_MODE_VIBRATE:
126 result = NapiAudioEnum::RINGER_MODE_VIBRATE;
127 break;
128 case RINGER_MODE_NORMAL:
129 result = NapiAudioEnum::RINGER_MODE_NORMAL;
130 break;
131 default:
132 result = NapiAudioEnum::RINGER_MODE_NORMAL;
133 break;
134 }
135
136 return result;
137 }
138
SafeJsCallbackRingModeWork(napi_env env,napi_value js_cb,void * context,void * data)139 void NapiAudioRingerModeCallback::SafeJsCallbackRingModeWork(napi_env env, napi_value js_cb, void *context, void *data)
140 {
141 AudioRingerModeJsCallback *event = reinterpret_cast<AudioRingerModeJsCallback *>(data);
142 CHECK_AND_RETURN_LOG((event != nullptr) && (event->callback != nullptr),
143 "OnJsCallbackRingerMode: no memory");
144 std::shared_ptr<AudioRingerModeJsCallback> safeContext(
145 static_cast<AudioRingerModeJsCallback*>(data),
146 [](AudioRingerModeJsCallback *ptr) {
147 delete ptr;
148 });
149 std::string request = event->callbackName;
150 napi_ref callback = event->callback->cb_;
151 napi_handle_scope scope = nullptr;
152 napi_open_handle_scope(env, &scope);
153 CHECK_AND_RETURN_LOG(scope != nullptr, "scope is nullptr.");
154 AUDIO_INFO_LOG("SafeJsCallbackRingModeWork: safe js callback working.");
155
156 do {
157 napi_value jsCallback = nullptr;
158 napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
159 CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
160 request.c_str());
161 napi_value args[ARGS_ONE] = { nullptr };
162 NapiParamUtils::SetValueInt32(env, GetJsAudioRingMode(event->ringerMode), args[PARAM0]);
163 CHECK_AND_BREAK_LOG(args[PARAM0] != nullptr,
164 "%{public}s fail to create ringer mode callback", request.c_str());
165 const size_t argCount = ARGS_ONE;
166 napi_value result = nullptr;
167 nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
168 CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to call ringer mode callback",
169 request.c_str());
170 } while (0);
171 napi_close_handle_scope(env, scope);
172 }
173
RingModeTsfnFinalize(napi_env env,void * data,void * hint)174 void NapiAudioRingerModeCallback::RingModeTsfnFinalize(napi_env env, void *data, void *hint)
175 {
176 AUDIO_INFO_LOG("RingModeTsfnFinalize: safe thread resource release.");
177 }
178
OnJsCallbackRingerMode(std::unique_ptr<AudioRingerModeJsCallback> & jsCb)179 void NapiAudioRingerModeCallback::OnJsCallbackRingerMode(std::unique_ptr<AudioRingerModeJsCallback> &jsCb)
180 {
181 if (jsCb.get() == nullptr) {
182 AUDIO_ERR_LOG("NapiAudioRingerModeCallback: OnJsCallbackRingerMode: jsCb.get() is null");
183 return;
184 }
185
186 AudioRingerModeJsCallback *event = jsCb.release();
187 CHECK_AND_RETURN_LOG((event != nullptr) && (event->callback != nullptr), "event is nullptr.");
188
189 napi_acquire_threadsafe_function(amRmChgTsfn_);
190 napi_call_threadsafe_function(amRmChgTsfn_, event, napi_tsfn_blocking);
191 }
192 } // namespace AudioStandard
193 } // namespace OHOS