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 "NapiCapturerPeriodPositionCallback"
17 #endif
18 
19 #include "napi_capturer_period_position_callback.h"
20 
21 #include "audio_errors.h"
22 #include "audio_capturer_log.h"
23 #include "napi_param_utils.h"
24 #include "napi_audio_capturer_callbacks.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
NapiCapturerPeriodPositionCallback(napi_env env)28 NapiCapturerPeriodPositionCallback::NapiCapturerPeriodPositionCallback(napi_env env)
29     : env_(env)
30 {
31     AUDIO_DEBUG_LOG("NapiCapturerPeriodPositionCallback: instance create");
32 }
33 
~NapiCapturerPeriodPositionCallback()34 NapiCapturerPeriodPositionCallback::~NapiCapturerPeriodPositionCallback()
35 {
36     AUDIO_DEBUG_LOG("NapiCapturerPeriodPositionCallback: instance destroy");
37 }
38 
SaveCallbackReference(const std::string & callbackName,napi_value args)39 void NapiCapturerPeriodPositionCallback::SaveCallbackReference(const std::string &callbackName, napi_value args)
40 {
41     std::lock_guard<std::mutex> lock(mutex_);
42     napi_ref callback = nullptr;
43     const int32_t refCount = 1;
44     napi_status status = napi_create_reference(env_, args, refCount, &callback);
45     CHECK_AND_RETURN_LOG(status == napi_ok && callback != nullptr,
46         "NapiCapturerPeriodPositionCallback: creating reference for callback fail");
47 
48     std::shared_ptr<AutoRef> cb = std::make_shared<AutoRef>(env_, callback);
49     CHECK_AND_RETURN_LOG(cb != nullptr, "No memory");
50     if (callbackName == PERIOD_REACH_CALLBACK_NAME) {
51         capturerPeriodPositionCallback_ = cb;
52     } else {
53         AUDIO_ERR_LOG("NapiCapturerPeriodPositionCallback: Unknown callback type: %{public}s", callbackName.c_str());
54     }
55 }
56 
OnPeriodReached(const int64_t & frameNumber)57 void NapiCapturerPeriodPositionCallback::OnPeriodReached(const int64_t &frameNumber)
58 {
59     std::lock_guard<std::mutex> lock(mutex_);
60     AUDIO_DEBUG_LOG("NapiCapturerPeriodPositionCallback: period reached");
61     CHECK_AND_RETURN_LOG(capturerPeriodPositionCallback_ != nullptr, "Cannot find the reference of position callback");
62 
63     std::unique_ptr<CapturerPeriodPositionJsCallback> cb = std::make_unique<CapturerPeriodPositionJsCallback>();
64     CHECK_AND_RETURN_LOG(cb != nullptr, "No memory");
65     cb->callback = capturerPeriodPositionCallback_;
66     cb->callbackName = PERIOD_REACH_CALLBACK_NAME;
67     cb->position = frameNumber;
68     return OnJsCapturerPeriodPositionCallback(cb);
69 }
70 
OnJsCapturerPeriodPositionCallback(std::unique_ptr<CapturerPeriodPositionJsCallback> & jsCb)71 void NapiCapturerPeriodPositionCallback::OnJsCapturerPeriodPositionCallback(
72     std::unique_ptr<CapturerPeriodPositionJsCallback> &jsCb)
73 {
74     uv_loop_s *loop = nullptr;
75     napi_get_uv_event_loop(env_, &loop);
76     CHECK_AND_RETURN_LOG(loop != nullptr, "loop nullptr, No memory");
77 
78     uv_work_t *work = new(std::nothrow) uv_work_t;
79     CHECK_AND_RETURN_LOG(work != nullptr, "work nullptr, No memory");
80     if (jsCb.get() == nullptr) {
81         AUDIO_ERR_LOG("NapiCapturerPeriodPositionCallback: OnJsCapturerPeriodPositionCallback: jsCb.get() is null");
82         delete work;
83         return;
84     }
85 
86     work->data = reinterpret_cast<void *>(jsCb.get());
87 
88     int ret = uv_queue_work_with_qos(loop, work, [] (uv_work_t *work) {},
89         WorkCapturerPeriodPositionCallbackDone, uv_qos_default);
90     if (ret != 0) {
91         AUDIO_ERR_LOG("Failed to execute libuv work queue");
92         delete work;
93     } else {
94         jsCb.release();
95     }
96 }
97 
WorkCapturerPeriodPositionCallbackDone(uv_work_t * work,int status)98 void NapiCapturerPeriodPositionCallback::WorkCapturerPeriodPositionCallbackDone(uv_work_t *work, int status)
99 {
100     // Js Thread
101     std::shared_ptr<CapturerPeriodPositionJsCallback> context(
102         static_cast<CapturerPeriodPositionJsCallback*>(work->data),
103         [work](CapturerPeriodPositionJsCallback* ptr) {
104             delete ptr;
105             delete work;
106     });
107     CHECK_AND_RETURN_LOG(work != nullptr, "work is nullptr");
108     CapturerPeriodPositionJsCallback *event = reinterpret_cast<CapturerPeriodPositionJsCallback *>(work->data);
109     CHECK_AND_RETURN_LOG(event != nullptr, "event is nullptr");
110     std::string request = event->callbackName;
111 
112     CHECK_AND_RETURN_LOG(event->callback != nullptr, "event is nullptr");
113     napi_env env = event->callback->env_;
114     napi_ref callback = event->callback->cb_;
115 
116     napi_handle_scope scope = nullptr;
117     napi_open_handle_scope(env, &scope);
118     CHECK_AND_RETURN_LOG(scope != nullptr, "scope is nullptr");
119     AUDIO_DEBUG_LOG("NapiCapturerPeriodPositionCallback: JsCallBack %{public}s, uv_queue_work_with_qos start",
120         request.c_str());
121     do {
122         CHECK_AND_BREAK_LOG(status != UV_ECANCELED, "%{public}s canceled", request.c_str());
123 
124         napi_value jsCallback = nullptr;
125         napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
126         CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
127             request.c_str());
128 
129         // Call back function
130         napi_value args[ARGS_ONE] = { nullptr };
131         napi_create_int64(env, event->position, &args[PARAM0]);
132         CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[PARAM0] != nullptr,
133             "%{public}s fail to create position callback", request.c_str());
134 
135         const size_t argCount = ARGS_ONE;
136         napi_value result = nullptr;
137         nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
138         CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to call position callback", request.c_str());
139     } while (0);
140     napi_close_handle_scope(env, scope);
141 }
142 }  // namespace AudioStandard
143 }  // namespace OHOS
144