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 "NapiAudioCapturerStateCallback"
17 #endif
18
19 #include "js_native_api.h"
20 #include "napi_audio_capturer_state_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
28 namespace OHOS {
29 namespace AudioStandard {
NapiAudioCapturerStateCallback(napi_env env)30 NapiAudioCapturerStateCallback::NapiAudioCapturerStateCallback(napi_env env)
31 : env_(env)
32 {
33 AUDIO_DEBUG_LOG("NapiAudioCapturerStateCallback: instance create");
34 }
35
~NapiAudioCapturerStateCallback()36 NapiAudioCapturerStateCallback::~NapiAudioCapturerStateCallback()
37 {
38 if (regAmacStateTsfn_) {
39 napi_release_threadsafe_function(amacStateTsfn_, napi_tsfn_abort);
40 }
41 AUDIO_DEBUG_LOG("NapiAudioCapturerStateCallback: instance destroy");
42 }
43
SaveCallbackReference(napi_value args)44 void NapiAudioCapturerStateCallback::SaveCallbackReference(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 "NapiAudioCapturerStateCallback: creating reference for callback fail");
52
53 std::shared_ptr<AutoRef> cb = std::make_shared<AutoRef>(env_, callback);
54 CHECK_AND_RETURN_LOG(cb != nullptr, "NapiAudioCapturerStateCallback: creating callback failed");
55
56 capturerStateCallback_ = cb;
57 }
58
RemoveCallbackReference(const napi_value args)59 void NapiAudioCapturerStateCallback::RemoveCallbackReference(const napi_value args)
60 {
61 if (!IsSameCallback(args)) {
62 return;
63 }
64 std::lock_guard<std::mutex> lock(mutex_);
65 capturerStateCallback_->cb_ = nullptr;
66 capturerStateCallback_.reset();
67 AUDIO_DEBUG_LOG("Remove capturerStateCallback success");
68 }
69
IsSameCallback(const napi_value args)70 bool NapiAudioCapturerStateCallback::IsSameCallback(const napi_value args)
71 {
72 std::lock_guard<std::mutex> lock(mutex_);
73 if (capturerStateCallback_ == nullptr) {
74 return false;
75 }
76 if (args == nullptr) {
77 return true;
78 }
79 napi_value capturerStateCallback = nullptr;
80 napi_get_reference_value(env_, capturerStateCallback_->cb_, &capturerStateCallback);
81 bool isEquals = false;
82 CHECK_AND_RETURN_RET_LOG(napi_strict_equals(env_, args, capturerStateCallback, &isEquals) == napi_ok, false,
83 "get napi_strict_equals failed");
84 return isEquals;
85 }
86
CreateCaptureStateTsfn(napi_env env)87 void NapiAudioCapturerStateCallback::CreateCaptureStateTsfn(napi_env env)
88 {
89 regAmacStateTsfn_ = true;
90 std::string callbackName = "AudioCapturerState";
91 napi_value cbName;
92 napi_create_string_utf8(env, callbackName.c_str(), callbackName.length(), &cbName);
93 napi_create_threadsafe_function(env, nullptr, nullptr, cbName, 0, 1, nullptr,
94 CapturerStateTsfnFinalize, nullptr, SafeJsCallbackCapturerStateWork, &amacStateTsfn_);
95 }
96
GetCaptureStateTsfnFlag()97 bool NapiAudioCapturerStateCallback::GetCaptureStateTsfnFlag()
98 {
99 return regAmacStateTsfn_;
100 }
101
OnCapturerStateChange(const std::vector<std::shared_ptr<AudioCapturerChangeInfo>> & audioCapturerChangeInfos)102 void NapiAudioCapturerStateCallback::OnCapturerStateChange(
103 const std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos)
104 {
105 AUDIO_INFO_LOG("OnCapturerStateChange is called");
106
107 std::lock_guard<std::mutex> lock(mutex_);
108 std::unique_ptr<AudioCapturerStateJsCallback> cb = std::make_unique<AudioCapturerStateJsCallback>();
109 CHECK_AND_RETURN_LOG(cb != nullptr, "No memory!!");
110
111 std::vector<std::shared_ptr<AudioCapturerChangeInfo>> capturerChangeInfos;
112 for (const auto &changeInfo : audioCapturerChangeInfos) {
113 capturerChangeInfos.push_back(std::make_shared<AudioCapturerChangeInfo>(*changeInfo));
114 }
115
116 cb->callback = capturerStateCallback_;
117 cb->changeInfos = move(capturerChangeInfos);
118
119 return OnJsCallbackCapturerState(cb);
120 }
121
SafeJsCallbackCapturerStateWork(napi_env env,napi_value js_cb,void * context,void * data)122 void NapiAudioCapturerStateCallback::SafeJsCallbackCapturerStateWork(
123 napi_env env, napi_value js_cb, void *context, void *data)
124 {
125 AudioCapturerStateJsCallback *event = reinterpret_cast<AudioCapturerStateJsCallback *>(data);
126 CHECK_AND_RETURN_LOG((event != nullptr) && (event->callback != nullptr),
127 "OnJsCallbackCapturerState: no memory");
128 std::shared_ptr<AudioCapturerStateJsCallback> safeContext(
129 static_cast<AudioCapturerStateJsCallback*>(data),
130 [](AudioCapturerStateJsCallback *ptr) {
131 delete ptr;
132 });
133 napi_ref callback = event->callback->cb_;
134 napi_handle_scope scope = nullptr;
135 napi_open_handle_scope(env, &scope);
136 CHECK_AND_RETURN_LOG(scope != nullptr, "scope is nullptr");
137 do {
138 napi_value jsCallback = nullptr;
139 napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
140 CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "callback get reference value fail");
141 napi_value args[ARGS_ONE] = { nullptr };
142 NapiParamUtils::SetCapturerChangeInfos(env, event->changeInfos, args[PARAM0]);
143 CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[PARAM0] != nullptr,
144 "fail to convert to jsobj");
145
146 const size_t argCount = ARGS_ONE;
147 napi_value result = nullptr;
148 nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
149 CHECK_AND_BREAK_LOG(nstatus == napi_ok, "fail to call Interrupt callback");
150 } while (0);
151 napi_close_handle_scope(env, scope);
152 }
153
CapturerStateTsfnFinalize(napi_env env,void * data,void * hint)154 void NapiAudioCapturerStateCallback::CapturerStateTsfnFinalize(napi_env env, void *data, void *hint)
155 {
156 AUDIO_INFO_LOG("CapturerStateTsfnFinalize: safe thread resource release.");
157 }
158
OnJsCallbackCapturerState(std::unique_ptr<AudioCapturerStateJsCallback> & jsCb)159 void NapiAudioCapturerStateCallback::OnJsCallbackCapturerState(std::unique_ptr<AudioCapturerStateJsCallback> &jsCb)
160 {
161 AudioCapturerStateJsCallback *event = jsCb.release();
162 CHECK_AND_RETURN_LOG((event != nullptr) && (event->callback != nullptr), "event is nullptr.");
163
164 napi_acquire_threadsafe_function(amacStateTsfn_);
165 napi_call_threadsafe_function(amacStateTsfn_, event, napi_tsfn_blocking);
166 }
167 } // namespace AudioStandard
168 } // namespace OHOS