1 /*
2 * Copyright (c) 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_state_callback_napi.h"
17
18 #include <uv.h>
19
20 #include "audio_errors.h"
21 #include "audio_log.h"
22
23 using namespace std;
24
25 namespace OHOS {
26 namespace AudioStandard {
AudioCapturerStateCallbackNapi(napi_env env)27 AudioCapturerStateCallbackNapi::AudioCapturerStateCallbackNapi(napi_env env)
28 : env_(env)
29 {
30 AUDIO_DEBUG_LOG("AudioCapturerStateCallbackNapi: instance create");
31 }
32
~AudioCapturerStateCallbackNapi()33 AudioCapturerStateCallbackNapi::~AudioCapturerStateCallbackNapi()
34 {
35 AUDIO_DEBUG_LOG("AudioCapturerStateCallbackNapi: instance destroy");
36 }
37
SaveCallbackReference(napi_value args)38 void AudioCapturerStateCallbackNapi::SaveCallbackReference(napi_value args)
39 {
40 std::lock_guard<std::mutex> lock(mutex_);
41 napi_ref callback = nullptr;
42 const int32_t refCount = 1;
43 napi_status status = napi_create_reference(env_, args, refCount, &callback);
44 CHECK_AND_RETURN_LOG(status == napi_ok && callback != nullptr,
45 "AudioCapturerStateCallbackNapi: creating reference for callback fail");
46
47 std::shared_ptr<AutoRef> cb = std::make_shared<AutoRef>(env_, callback);
48 CHECK_AND_RETURN_LOG(cb != nullptr, "AudioCapturerStateCallbackNapi: creating callback failed");
49
50 capturerStateCallback_ = cb;
51 }
52
OnCapturerStateChange(const std::vector<std::unique_ptr<AudioCapturerChangeInfo>> & audioCapturerChangeInfos)53 void AudioCapturerStateCallbackNapi::OnCapturerStateChange(
54 const std::vector<std::unique_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos)
55 {
56 AUDIO_INFO_LOG("AudioCapturerStateCallbackNapi: OnCapturerStateChange is called");
57
58 std::lock_guard<std::mutex> lock(mutex_);
59 std::unique_ptr<AudioCapturerStateJsCallback> cb = std::make_unique<AudioCapturerStateJsCallback>();
60 CHECK_AND_RETURN_LOG(cb != nullptr, "No memory!!");
61
62 std::vector<std::unique_ptr<AudioCapturerChangeInfo>> capturerChangeInfos;
63 for (const auto &changeInfo : audioCapturerChangeInfos) {
64 capturerChangeInfos.push_back(std::make_unique<AudioCapturerChangeInfo>(*changeInfo));
65 }
66
67 cb->callback = capturerStateCallback_;
68 cb->changeInfos = move(capturerChangeInfos);
69
70 return OnJsCallbackCapturerState(cb);
71 }
72
SetValueInt32(const napi_env & env,const std::string & fieldStr,const int intValue,napi_value & obj)73 static void SetValueInt32(const napi_env& env, const std::string& fieldStr, const int intValue, napi_value& obj)
74 {
75 napi_value value = nullptr;
76 napi_create_int32(env, intValue, &value);
77 napi_set_named_property(env, obj, fieldStr.c_str(), value);
78 }
79
SetValueString(const napi_env & env,const std::string & fieldStr,const std::string stringValue,napi_value & result)80 static void SetValueString(const napi_env &env, const std::string &fieldStr, const std::string stringValue,
81 napi_value &result)
82 {
83 napi_value value = nullptr;
84 napi_create_string_utf8(env, stringValue.c_str(), NAPI_AUTO_LENGTH, &value);
85 napi_set_named_property(env, result, fieldStr.c_str(), value);
86 }
87
SetDeviceDescriptors(const napi_env & env,napi_value & jsChangeInfoObj,const DeviceInfo & deviceInfo)88 static void SetDeviceDescriptors(const napi_env& env, napi_value &jsChangeInfoObj, const DeviceInfo &deviceInfo)
89 {
90 napi_value jsDeviceDescriptorsObj = nullptr;
91 napi_value valueParam = nullptr;
92 napi_create_array_with_length(env, 1, &jsDeviceDescriptorsObj);
93
94 (void)napi_create_object(env, &valueParam);
95 SetValueInt32(env, "deviceRole", static_cast<int32_t>(deviceInfo.deviceRole), valueParam);
96 SetValueInt32(env, "deviceType", static_cast<int32_t>(deviceInfo.deviceType), valueParam);
97 SetValueInt32(env, "id", static_cast<int32_t>(deviceInfo.deviceId), valueParam);
98 SetValueString(env, "name", deviceInfo.deviceName, valueParam);
99 SetValueString(env, "address", deviceInfo.macAddress, valueParam);
100
101 napi_value value = nullptr;
102 napi_value sampleRates;
103 napi_create_array_with_length(env, 1, &sampleRates);
104 napi_create_int32(env, deviceInfo.audioStreamInfo.samplingRate, &value);
105 napi_set_element(env, sampleRates, 0, value);
106 napi_set_named_property(env, valueParam, "sampleRates", sampleRates);
107
108 napi_value channelCounts;
109 napi_create_array_with_length(env, 1, &channelCounts);
110 napi_create_int32(env, deviceInfo.audioStreamInfo.channels, &value);
111 napi_set_element(env, channelCounts, 0, value);
112 napi_set_named_property(env, valueParam, "channelCounts", channelCounts);
113
114 napi_value channelMasks;
115 napi_create_array_with_length(env, 1, &channelMasks);
116 napi_create_int32(env, deviceInfo.channelMasks, &value);
117 napi_set_element(env, channelMasks, 0, value);
118 napi_set_named_property(env, valueParam, "channelMasks", channelMasks);
119
120 napi_set_element(env, jsDeviceDescriptorsObj, 0, valueParam);
121 napi_set_named_property(env, jsChangeInfoObj, "deviceDescriptors", jsDeviceDescriptorsObj);
122 }
123
NativeCapturerChangeInfoToJsObj(const napi_env & env,napi_value & jsArrayChangeInfoObj,const vector<unique_ptr<AudioCapturerChangeInfo>> & changeInfos)124 static void NativeCapturerChangeInfoToJsObj(const napi_env &env, napi_value &jsArrayChangeInfoObj,
125 const vector<unique_ptr<AudioCapturerChangeInfo>> &changeInfos)
126 {
127 napi_value jsChangeInfoObj = nullptr;
128 napi_value jsCapInfoObj = nullptr;
129
130 size_t size = changeInfos.size();
131 int32_t position = 0;
132
133 napi_create_array_with_length(env, size, &jsArrayChangeInfoObj);
134 for (const unique_ptr<AudioCapturerChangeInfo> &changeInfo : changeInfos) {
135 AUDIO_DEBUG_LOG("AudioCapturerStateCallbackNapi: ChangeInfo to jsobj called");
136 napi_create_object(env, &jsChangeInfoObj);
137 SetValueInt32(env, "streamId", static_cast<int32_t>(changeInfo->sessionId), jsChangeInfoObj);
138 SetValueInt32(env, "capturerState", static_cast<int32_t>(changeInfo->capturerState), jsChangeInfoObj);
139 SetValueInt32(env, "clientUid", static_cast<int32_t>(changeInfo->clientUID), jsChangeInfoObj);
140
141 napi_create_object(env, &jsCapInfoObj);
142 SetValueInt32(env, "source", static_cast<int32_t>(changeInfo->capturerInfo.sourceType), jsCapInfoObj);
143 SetValueInt32(env, "capturerFlags", changeInfo->capturerInfo.capturerFlags, jsCapInfoObj);
144 napi_set_named_property(env, jsChangeInfoObj, "capturerInfo", jsCapInfoObj);
145 SetDeviceDescriptors(env, jsChangeInfoObj, changeInfo->inputDeviceInfo);
146
147 napi_set_element(env, jsArrayChangeInfoObj, position, jsChangeInfoObj);
148 position++;
149 }
150 }
151
OnJsCallbackCapturerState(std::unique_ptr<AudioCapturerStateJsCallback> & jsCb)152 void AudioCapturerStateCallbackNapi::OnJsCallbackCapturerState(std::unique_ptr<AudioCapturerStateJsCallback> &jsCb)
153 {
154 uv_loop_s *loop = nullptr;
155 napi_get_uv_event_loop(env_, &loop);
156 if (loop == nullptr) {
157 return;
158 }
159
160 uv_work_t *work = new(std::nothrow) uv_work_t;
161 if (work == nullptr) {
162 AUDIO_ERR_LOG("AudioCapturerStateCallbackNapi: OnJsCallbackCapturerState: No memory");
163 return;
164 }
165
166 work->data = reinterpret_cast<void *>(jsCb.get());
167
168 int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) {
169 // Js Thread
170 AudioCapturerStateJsCallback *event = reinterpret_cast<AudioCapturerStateJsCallback *>(work->data);
171 if (event == nullptr || event->callback == nullptr) {
172 AUDIO_ERR_LOG("AudioCapturerStateCallbackNapi: OnJsCallbackCapturerState: data or callback is null.");
173 delete event;
174 delete work;
175 return;
176 }
177 napi_env env = event->callback->env_;
178 napi_ref callback = event->callback->cb_;
179
180 do {
181 napi_value jsCallback = nullptr;
182 napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
183 CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr,
184 "callback get reference value fail");
185 // Call back function
186 napi_value args[1] = { nullptr };
187 NativeCapturerChangeInfoToJsObj(env, args[0], event->changeInfos);
188
189 CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[0] != nullptr,
190 " fail to convert to jsobj");
191
192 const size_t argCount = 1;
193 napi_value result = nullptr;
194 nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
195 CHECK_AND_BREAK_LOG(nstatus == napi_ok, "Fail to call renderstate callback");
196 } while (0);
197 delete event;
198 delete work;
199 });
200 if (ret != 0) {
201 AUDIO_ERR_LOG("Failed to execute libuv work queue");
202 delete work;
203 } else {
204 jsCb.release();
205 }
206 }
207 } // namespace AudioStandard
208 } // namespace OHOS
209