1 /*
2 * Copyright (c) 2021-2022 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
16 #include "audio_capturer_callback_napi.h"
17
18 #include <uv.h>
19
20 #include "audio_errors.h"
21 #include "audio_log.h"
22
23 namespace OHOS {
24 namespace AudioStandard {
AudioCapturerCallbackNapi(napi_env env)25 AudioCapturerCallbackNapi::AudioCapturerCallbackNapi(napi_env env)
26 : env_(env)
27 {
28 AUDIO_DEBUG_LOG("AudioCapturerCallbackNapi: instance create");
29 }
30
~AudioCapturerCallbackNapi()31 AudioCapturerCallbackNapi::~AudioCapturerCallbackNapi()
32 {
33 AUDIO_DEBUG_LOG("AudioCapturerCallbackNapi: instance destroy");
34 }
35
SaveCallbackReference(const std::string & callbackName,napi_value args)36 void AudioCapturerCallbackNapi::SaveCallbackReference(const std::string &callbackName, napi_value args)
37 {
38 std::lock_guard<std::mutex> lock(mutex_);
39 napi_ref callback = nullptr;
40 const int32_t refCount = 1;
41 napi_status status = napi_create_reference(env_, args, refCount, &callback);
42 CHECK_AND_RETURN_LOG(status == napi_ok && callback != nullptr,
43 "AudioCapturerCallbackNapi: creating reference for callback fail");
44
45 std::shared_ptr<AutoRef> cb = std::make_shared<AutoRef>(env_, callback);
46 if (callbackName == INTERRUPT_CALLBACK_NAME || callbackName == AUDIO_INTERRUPT_CALLBACK_NAME) {
47 interruptCallback_ = cb;
48 } else if (callbackName == STATE_CHANGE_CALLBACK_NAME) {
49 stateChangeCallback_ = cb;
50 } else {
51 AUDIO_ERR_LOG("AudioCapturerCallbackNapi: Unknown callback type: %{public}s", callbackName.c_str());
52 }
53 }
54
RemoveCallbackReference(const std::string & callbackName)55 void AudioCapturerCallbackNapi::RemoveCallbackReference(const std::string &callbackName)
56 {
57 std::lock_guard<std::mutex> lock(mutex_);
58
59 if (callbackName == AUDIO_INTERRUPT_CALLBACK_NAME) {
60 interruptCallback_ = nullptr;
61 } else if (callbackName == STATE_CHANGE_CALLBACK_NAME) {
62 stateChangeCallback_ = nullptr;
63 } else {
64 AUDIO_ERR_LOG("Unknown callback type: %{public}s", callbackName.c_str());
65 }
66 }
67
SetValueInt32(const napi_env & env,const std::string & fieldStr,const int intValue,napi_value & result)68 static void SetValueInt32(const napi_env& env, const std::string& fieldStr, const int intValue, napi_value& result)
69 {
70 napi_value value = nullptr;
71 napi_create_int32(env, intValue, &value);
72 napi_set_named_property(env, result, fieldStr.c_str(), value);
73 }
74
NativeInterruptEventToJsObj(const napi_env & env,napi_value & jsObj,const InterruptEvent & interruptEvent)75 static void NativeInterruptEventToJsObj(const napi_env& env, napi_value& jsObj,
76 const InterruptEvent& interruptEvent)
77 {
78 napi_create_object(env, &jsObj);
79 SetValueInt32(env, "eventType", static_cast<int32_t>(interruptEvent.eventType), jsObj);
80 SetValueInt32(env, "forceType", static_cast<int32_t>(interruptEvent.forceType), jsObj);
81 SetValueInt32(env, "hintType", static_cast<int32_t>(interruptEvent.hintType), jsObj);
82 }
83
OnInterrupt(const InterruptEvent & interruptEvent)84 void AudioCapturerCallbackNapi::OnInterrupt(const InterruptEvent &interruptEvent)
85 {
86 std::lock_guard<std::mutex> lock(mutex_);
87 AUDIO_DEBUG_LOG("AudioCapturerCallbackNapi: OnInterrupt is called, hintType: %{public}d", interruptEvent.hintType);
88 CHECK_AND_RETURN_LOG(interruptCallback_ != nullptr, "Cannot find the reference of interrupt callback");
89
90 std::unique_ptr<AudioCapturerJsCallback> cb = std::make_unique<AudioCapturerJsCallback>();
91 CHECK_AND_RETURN_LOG(cb != nullptr, "No memory");
92 cb->callback = interruptCallback_;
93 cb->callbackName = INTERRUPT_CALLBACK_NAME;
94 cb->interruptEvent = interruptEvent;
95 return OnJsCallbackInterrupt(cb);
96 }
97
OnJsCallbackInterrupt(std::unique_ptr<AudioCapturerJsCallback> & jsCb)98 void AudioCapturerCallbackNapi::OnJsCallbackInterrupt(std::unique_ptr<AudioCapturerJsCallback> &jsCb)
99 {
100 uv_loop_s *loop = nullptr;
101 napi_get_uv_event_loop(env_, &loop);
102 if (loop == nullptr) {
103 return;
104 }
105
106 uv_work_t *work = new(std::nothrow) uv_work_t;
107 if (work == nullptr) {
108 AUDIO_ERR_LOG("OnJsCallBackInterrupt: No memory");
109 return;
110 }
111 if (jsCb.get() == nullptr) {
112 AUDIO_ERR_LOG("OnJsCallBackInterrupt: jsCb.get() is null");
113 delete work;
114 return;
115 }
116 work->data = reinterpret_cast<void *>(jsCb.get());
117
118 int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) {
119 // Js Thread
120 AudioCapturerJsCallback *event = reinterpret_cast<AudioCapturerJsCallback *>(work->data);
121 std::string request = event->callbackName;
122 napi_env env = event->callback->env_;
123 napi_ref callback = event->callback->cb_;
124 AUDIO_DEBUG_LOG("AudioCapturerCallbackNapi: JsCallBack %{public}s, uv_queue_work start", request.c_str());
125 do {
126 CHECK_AND_BREAK_LOG(status != UV_ECANCELED, "%{public}s cancelled", request.c_str());
127
128 napi_value jsCallback = nullptr;
129 napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
130 CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
131 request.c_str());
132
133 // Call back function
134 napi_value args[1] = { nullptr };
135 NativeInterruptEventToJsObj(env, args[0], event->interruptEvent);
136 CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[0] != nullptr,
137 "%{public}s fail to create Interrupt callback", request.c_str());
138
139 const size_t argCount = 1;
140 napi_value result = nullptr;
141 nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
142 CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to call Interrupt callback", request.c_str());
143 } while (0);
144 delete event;
145 delete work;
146 });
147 if (ret != 0) {
148 AUDIO_ERR_LOG("Failed to execute libuv work queue");
149 delete work;
150 } else {
151 jsCb.release();
152 }
153 }
154
OnStateChange(const CapturerState state)155 void AudioCapturerCallbackNapi::OnStateChange(const CapturerState state)
156 {
157 std::lock_guard<std::mutex> lock(mutex_);
158 AUDIO_DEBUG_LOG("AudioCapturerCallbackNapi: OnStateChange is called");
159 AUDIO_DEBUG_LOG("AudioCapturerCallbackNapi: state: %{public}d", state);
160 CHECK_AND_RETURN_LOG(stateChangeCallback_ != nullptr, "Cannot find the reference of stateChange callback");
161
162 std::unique_ptr<AudioCapturerJsCallback> cb = std::make_unique<AudioCapturerJsCallback>();
163 CHECK_AND_RETURN_LOG(cb != nullptr, "No memory");
164 cb->callback = stateChangeCallback_;
165 cb->callbackName = STATE_CHANGE_CALLBACK_NAME;
166 cb->state = state;
167 return OnJsCallbackStateChange(cb);
168 }
169
OnJsCallbackStateChange(std::unique_ptr<AudioCapturerJsCallback> & jsCb)170 void AudioCapturerCallbackNapi::OnJsCallbackStateChange(std::unique_ptr<AudioCapturerJsCallback> &jsCb)
171 {
172 uv_loop_s *loop = nullptr;
173 napi_get_uv_event_loop(env_, &loop);
174 if (loop == nullptr) {
175 return;
176 }
177
178 uv_work_t *work = new(std::nothrow) uv_work_t;
179 if (work == nullptr) {
180 AUDIO_ERR_LOG("AudioCapturerCallbackNapi: OnJsCallbackStateChange: No memory");
181 return;
182 }
183
184 if (jsCb.get() == nullptr) {
185 AUDIO_ERR_LOG("AudioCapturerCallbackNapi: OnJsCallbackStateChange: jsCb.get() is null");
186 delete work;
187 return;
188 }
189
190 work->data = reinterpret_cast<void *>(jsCb.get());
191
192 int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) {
193 // Js Thread
194 AudioCapturerJsCallback *event = reinterpret_cast<AudioCapturerJsCallback *>(work->data);
195 std::string request = event->callbackName;
196 napi_env env = event->callback->env_;
197 napi_ref callback = event->callback->cb_;
198 AUDIO_DEBUG_LOG("AudioCapturerCallbackNapi: JsCallBack %{public}s, uv_queue_work start", request.c_str());
199 do {
200 CHECK_AND_BREAK_LOG(status != UV_ECANCELED, "%{public}s cancelled", request.c_str());
201
202 napi_value jsCallback = nullptr;
203 napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
204 CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
205 request.c_str());
206
207 // Call back function
208 napi_value args[1] = { nullptr };
209 nstatus = napi_create_int32(env, event->state, &args[0]);
210 CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[0] != nullptr,
211 "%{public}s fail to create Interrupt callback", request.c_str());
212
213 const size_t argCount = 1;
214 napi_value result = nullptr;
215 nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
216 CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to call Interrupt callback", request.c_str());
217 } while (0);
218 delete event;
219 delete work;
220 });
221 if (ret != 0) {
222 AUDIO_ERR_LOG("Failed to execute libuv work queue");
223 delete work;
224 } else {
225 jsCb.release();
226 }
227 }
228 } // namespace AudioStandard
229 } // namespace OHOS
230