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 "NapiAudioManagerMicStateChangeCallback"
17 #endif
18 
19 #include "napi_audio_micstatechange_callback.h"
20 #include "napi_param_utils.h"
21 #include "napi_audio_error.h"
22 
23 namespace OHOS {
24 namespace AudioStandard {
NapiAudioManagerMicStateChangeCallback(napi_env env)25 NapiAudioManagerMicStateChangeCallback::NapiAudioManagerMicStateChangeCallback(napi_env env)
26     : env_(env)
27 {
28     AUDIO_DEBUG_LOG("NapiAudioManagerMicStateChangeCallback: instance create");
29 }
30 
~NapiAudioManagerMicStateChangeCallback()31 NapiAudioManagerMicStateChangeCallback::~NapiAudioManagerMicStateChangeCallback()
32 {
33     AUDIO_DEBUG_LOG("NapiAudioManagerMicStateChangeCallback: instance destroy");
34 }
35 
SaveCallbackReference(const std::string & callbackName,napi_value args)36 void NapiAudioManagerMicStateChangeCallback::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 = ARGS_ONE;
41     napi_status status = napi_create_reference(env_, args, refCount, &callback);
42     CHECK_AND_RETURN_LOG(status == napi_ok && callback != nullptr,
43         "NapiAudioManagerMicStateChangeCallback: creating reference for callback fail");
44     std::shared_ptr<AutoRef> cb = std::make_shared<AutoRef>(env_, callback);
45     CHECK_AND_RETURN_LOG(callbackName == MIC_STATE_CHANGE_CALLBACK_NAME,
46         "NapiAudioManagerMicStateChangeCallback: Unknown callback type: %{public}s", callbackName.c_str());
47     micStateChangeCallback_ = cb;
48 }
49 
RemoveCallbackReference(const napi_value args)50 void NapiAudioManagerMicStateChangeCallback::RemoveCallbackReference(const napi_value args)
51 {
52     if (!IsSameCallback(args)) {
53         return;
54     }
55     std::lock_guard<std::mutex> lock(mutex_);
56     napi_delete_reference(env_, micStateChangeCallback_->cb_);
57     micStateChangeCallback_->cb_ = nullptr;
58     micStateChangeCallback_ = nullptr;
59     AUDIO_INFO_LOG("Remove callback reference successful.");
60 }
61 
IsSameCallback(const napi_value args)62 bool NapiAudioManagerMicStateChangeCallback::IsSameCallback(const napi_value args)
63 {
64     std::lock_guard<std::mutex> lock(mutex_);
65     if (args == nullptr) {
66         return true;
67     }
68     if (micStateChangeCallback_ == nullptr) {
69         return false;
70     }
71     napi_value micStateChangeCallback = nullptr;
72     napi_get_reference_value(env_, micStateChangeCallback_->cb_, &micStateChangeCallback);
73     bool isEquals = false;
74     CHECK_AND_RETURN_RET_LOG(napi_strict_equals(env_, args, micStateChangeCallback, &isEquals) == napi_ok, false,
75         "get napi_strict_equals failed");
76     return isEquals;
77 }
78 
OnMicStateUpdated(const MicStateChangeEvent & micStateChangeEvent)79 void NapiAudioManagerMicStateChangeCallback::OnMicStateUpdated(const MicStateChangeEvent &micStateChangeEvent)
80 {
81     std::lock_guard<std::mutex> lock(mutex_);
82     CHECK_AND_RETURN_LOG(micStateChangeCallback_ != nullptr, "callback not registered by JS client");
83 
84     std::unique_ptr<AudioManagerMicStateChangeJsCallback> cb = std::make_unique<AudioManagerMicStateChangeJsCallback>();
85     CHECK_AND_RETURN_LOG(cb != nullptr, "No memory");
86 
87     cb->callback = micStateChangeCallback_;
88     cb->callbackName = MIC_STATE_CHANGE_CALLBACK_NAME;
89     cb->micStateChangeEvent = micStateChangeEvent;
90     return OnJsCallbackMicStateChange(cb);
91 }
92 
WorkCallbackInterruptDone(uv_work_t * work,int status)93 void NapiAudioManagerMicStateChangeCallback::WorkCallbackInterruptDone(uv_work_t *work, int status)
94 {
95     std::shared_ptr<AudioManagerMicStateChangeJsCallback> context(
96         static_cast<AudioManagerMicStateChangeJsCallback*>(work->data),
97         [work](AudioManagerMicStateChangeJsCallback* ptr) {
98             delete ptr;
99             delete work;
100     });
101     CHECK_AND_RETURN_LOG(work != nullptr, "work is nullptr");
102     AudioManagerMicStateChangeJsCallback *event = reinterpret_cast<AudioManagerMicStateChangeJsCallback *>(work->data);
103     CHECK_AND_RETURN_LOG(event != nullptr, "event is nullptr");
104     std::string request = event->callbackName;
105     CHECK_AND_RETURN_LOG(event->callback != nullptr, "event is nullptr");
106     napi_env env = event->callback->env_;
107     napi_ref callback = event->callback->cb_;
108     napi_handle_scope scope = nullptr;
109     napi_open_handle_scope(env, &scope);
110     CHECK_AND_RETURN_LOG(scope != nullptr, "scope is nullptr");
111     AUDIO_DEBUG_LOG("JsCallBack %{public}s, uv_queue_work_with_qos start", request.c_str());
112     do {
113         CHECK_AND_BREAK_LOG(status != UV_ECANCELED, "%{public}s canceled", request.c_str());
114         napi_value jsCallback = nullptr;
115         napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
116         CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
117             request.c_str());
118         napi_value args[ARGS_ONE] = { nullptr };
119         NapiParamUtils::SetValueMicStateChange(env, event->micStateChangeEvent, args[PARAM0]);
120         CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[0] != nullptr,
121             "%{public}s fail to create DeviceChange callback", request.c_str());
122         const size_t argCount = ARGS_ONE;
123         napi_value result = nullptr;
124         nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
125         CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to call DeviceChange callback",
126             request.c_str());
127     } while (0);
128     napi_close_handle_scope(env, scope);
129 }
130 
OnJsCallbackMicStateChange(std::unique_ptr<AudioManagerMicStateChangeJsCallback> & jsCb)131 void NapiAudioManagerMicStateChangeCallback::OnJsCallbackMicStateChange
132     (std::unique_ptr<AudioManagerMicStateChangeJsCallback> &jsCb)
133 {
134     uv_loop_s *loop = nullptr;
135     napi_get_uv_event_loop(env_, &loop);
136     CHECK_AND_RETURN_LOG(loop != nullptr, "loop is nullptr");
137 
138     uv_work_t *work = new(std::nothrow) uv_work_t;
139     CHECK_AND_RETURN_LOG(work != nullptr, "OnJsCallbackMicStateChange: No memory");
140 
141     if (jsCb.get() == nullptr) {
142         AUDIO_ERR_LOG("NapiAudioManagerCallback: OnJsCallbackMicStateChange: jsCb.get() is null");
143         delete work;
144         return;
145     }
146 
147     work->data = reinterpret_cast<void *>(jsCb.get());
148     int ret = uv_queue_work_with_qos(loop, work, [] (uv_work_t *work) {}, WorkCallbackInterruptDone,
149         uv_qos_default);
150     if (ret != 0) {
151         AUDIO_ERR_LOG("Failed to execute libuv work queue");
152         delete work;
153     } else {
154         jsCb.release();
155     }
156 }
157 } // namespace AudioStandard
158 } // namespace OHOS