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 "napi_audio_capturer_state_callback.h"
20 #include "napi_audio_enum.h"
21 #include "napi_audio_error.h"
22 #include "napi_param_utils.h"
23 #include "audio_manager_log.h"
24
25 using namespace std;
26
27 namespace OHOS {
28 namespace AudioStandard {
NapiAudioCapturerStateCallback(napi_env env)29 NapiAudioCapturerStateCallback::NapiAudioCapturerStateCallback(napi_env env)
30 : env_(env)
31 {
32 AUDIO_DEBUG_LOG("NapiAudioCapturerStateCallback: instance create");
33 }
34
~NapiAudioCapturerStateCallback()35 NapiAudioCapturerStateCallback::~NapiAudioCapturerStateCallback()
36 {
37 AUDIO_DEBUG_LOG("NapiAudioCapturerStateCallback: instance destroy");
38 }
39
SaveCallbackReference(napi_value args)40 void NapiAudioCapturerStateCallback::SaveCallbackReference(napi_value args)
41 {
42 std::lock_guard<std::mutex> lock(mutex_);
43 napi_ref callback = nullptr;
44 const int32_t refCount = ARGS_ONE;
45 napi_status status = napi_create_reference(env_, args, refCount, &callback);
46 CHECK_AND_RETURN_LOG(status == napi_ok && callback != nullptr,
47 "NapiAudioCapturerStateCallback: creating reference for callback fail");
48
49 std::shared_ptr<AutoRef> cb = std::make_shared<AutoRef>(env_, callback);
50 CHECK_AND_RETURN_LOG(cb != nullptr, "NapiAudioCapturerStateCallback: creating callback failed");
51
52 capturerStateCallback_ = cb;
53 }
54
OnCapturerStateChange(const std::vector<std::unique_ptr<AudioCapturerChangeInfo>> & audioCapturerChangeInfos)55 void NapiAudioCapturerStateCallback::OnCapturerStateChange(
56 const std::vector<std::unique_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos)
57 {
58 AUDIO_INFO_LOG("OnCapturerStateChange is called");
59
60 std::lock_guard<std::mutex> lock(mutex_);
61 std::unique_ptr<AudioCapturerStateJsCallback> cb = std::make_unique<AudioCapturerStateJsCallback>();
62 CHECK_AND_RETURN_LOG(cb != nullptr, "No memory!!");
63
64 std::vector<std::unique_ptr<AudioCapturerChangeInfo>> capturerChangeInfos;
65 for (const auto &changeInfo : audioCapturerChangeInfos) {
66 capturerChangeInfos.push_back(std::make_unique<AudioCapturerChangeInfo>(*changeInfo));
67 }
68
69 cb->callback = capturerStateCallback_;
70 cb->changeInfos = move(capturerChangeInfos);
71
72 return OnJsCallbackCapturerState(cb);
73 }
74
WorkCallbackInterruptDone(uv_work_t * work,int status)75 void NapiAudioCapturerStateCallback::WorkCallbackInterruptDone(uv_work_t *work, int status)
76 {
77 std::shared_ptr<AudioCapturerStateJsCallback> context(
78 static_cast<AudioCapturerStateJsCallback*>(work->data),
79 [work](AudioCapturerStateJsCallback* ptr) {
80 delete ptr;
81 delete work;
82 });
83 CHECK_AND_RETURN_LOG(work != nullptr, "work is nullptr");
84 AudioCapturerStateJsCallback *event = reinterpret_cast<AudioCapturerStateJsCallback *>(work->data);
85 CHECK_AND_RETURN_LOG(event != nullptr, "event is nullptr");
86 CHECK_AND_RETURN_LOG(event->callback != nullptr, "event is nullptr");
87 napi_env env = event->callback->env_;
88 napi_ref callback = event->callback->cb_;
89 napi_handle_scope scope = nullptr;
90 napi_open_handle_scope(env, &scope);
91 CHECK_AND_RETURN_LOG(scope != nullptr, "scope is nullptr");
92 do {
93 napi_value jsCallback = nullptr;
94 napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
95 CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "callback get reference value fail");
96 napi_value args[ARGS_ONE] = { nullptr };
97 NapiParamUtils::SetCapturerChangeInfos(env, event->changeInfos, args[PARAM0]);
98 CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[PARAM0] != nullptr,
99 "fail to convert to jsobj");
100
101 const size_t argCount = ARGS_ONE;
102 napi_value result = nullptr;
103 nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
104 CHECK_AND_BREAK_LOG(nstatus == napi_ok, "fail to call Interrupt callback");
105 } while (0);
106 napi_close_handle_scope(env, scope);
107 }
108
OnJsCallbackCapturerState(std::unique_ptr<AudioCapturerStateJsCallback> & jsCb)109 void NapiAudioCapturerStateCallback::OnJsCallbackCapturerState(std::unique_ptr<AudioCapturerStateJsCallback> &jsCb)
110 {
111 uv_loop_s *loop = nullptr;
112 napi_get_uv_event_loop(env_, &loop);
113 CHECK_AND_RETURN_LOG(loop != nullptr, "loop is nullptr");
114
115 uv_work_t *work = new(std::nothrow) uv_work_t;
116 CHECK_AND_RETURN_LOG(work != nullptr, "OnJsCallbackCapturerState: No memory");
117
118 work->data = reinterpret_cast<void *>(jsCb.get());
119 int ret = uv_queue_work_with_qos(loop, work, [] (uv_work_t *work) {}, WorkCallbackInterruptDone,
120 uv_qos_default);
121 if (ret != 0) {
122 AUDIO_ERR_LOG("Failed to execute libuv work queue");
123 delete work;
124 } else {
125 jsCb.release();
126 }
127 }
128 } // namespace AudioStandard
129 } // namespace OHOS