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