1 /*
2 * Copyright (c) 2021-2022 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
16 #include "capturer_period_position_callback_napi.h"
17
18 #include <uv.h>
19
20 #include "audio_errors.h"
21 #include "audio_log.h"
22
23 namespace {
24 const std::string CAPTURER_PERIOD_POSITION_CALLBACK_NAME = "periodReach";
25 }
26
27 namespace OHOS {
28 namespace AudioStandard {
CapturerPeriodPositionCallbackNapi(napi_env env)29 CapturerPeriodPositionCallbackNapi::CapturerPeriodPositionCallbackNapi(napi_env env)
30 : env_(env)
31 {
32 AUDIO_DEBUG_LOG("CapturerPeriodPositionCallbackNapi: instance create");
33 }
34
~CapturerPeriodPositionCallbackNapi()35 CapturerPeriodPositionCallbackNapi::~CapturerPeriodPositionCallbackNapi()
36 {
37 AUDIO_DEBUG_LOG("CapturerPeriodPositionCallbackNapi: instance destroy");
38 }
39
SaveCallbackReference(const std::string & callbackName,napi_value args)40 void CapturerPeriodPositionCallbackNapi::SaveCallbackReference(const std::string &callbackName, napi_value args)
41 {
42 std::lock_guard<std::mutex> lock(mutex_);
43 napi_ref callback = nullptr;
44 const int32_t refCount = 1;
45 napi_status status = napi_create_reference(env_, args, refCount, &callback);
46 CHECK_AND_RETURN_LOG(status == napi_ok && callback != nullptr,
47 "CapturerPeriodPositionCallbackNapi: 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, "No memory");
51 if (callbackName == CAPTURER_PERIOD_POSITION_CALLBACK_NAME) {
52 capturerPeriodPositionCallback_ = cb;
53 } else {
54 AUDIO_ERR_LOG("CapturerPeriodPositionCallbackNapi: Unknown callback type: %{public}s", callbackName.c_str());
55 }
56 }
57
OnPeriodReached(const int64_t & frameNumber)58 void CapturerPeriodPositionCallbackNapi::OnPeriodReached(const int64_t &frameNumber)
59 {
60 std::lock_guard<std::mutex> lock(mutex_);
61 AUDIO_DEBUG_LOG("CapturerPeriodPositionCallbackNapi: period reached");
62 CHECK_AND_RETURN_LOG(capturerPeriodPositionCallback_ != nullptr, "Cannot find the reference of position callback");
63
64 std::unique_ptr<CapturerPeriodPositionJsCallback> cb = std::make_unique<CapturerPeriodPositionJsCallback>();
65 CHECK_AND_RETURN_LOG(cb != nullptr, "No memory");
66 cb->callback = capturerPeriodPositionCallback_;
67 cb->callbackName = CAPTURER_PERIOD_POSITION_CALLBACK_NAME;
68 cb->position = frameNumber;
69 return OnJsCapturerPeriodPositionCallback(cb);
70 }
71
OnJsCapturerPeriodPositionCallback(std::unique_ptr<CapturerPeriodPositionJsCallback> & jsCb)72 void CapturerPeriodPositionCallbackNapi::OnJsCapturerPeriodPositionCallback(
73 std::unique_ptr<CapturerPeriodPositionJsCallback> &jsCb)
74 {
75 uv_loop_s *loop = nullptr;
76 napi_get_uv_event_loop(env_, &loop);
77 if (loop == nullptr) {
78 return;
79 }
80
81 uv_work_t *work = new(std::nothrow) uv_work_t;
82 if (work == nullptr) {
83 AUDIO_ERR_LOG("CapturerPeriodPositionCallbackNapi: OnJsCapturerPeriodPositionCallback: No memory");
84 return;
85 }
86
87 if (jsCb.get() == nullptr) {
88 AUDIO_ERR_LOG("CapturerPeriodPositionCallbackNapi: OnJsCapturerPeriodPositionCallback: jsCb.get() is null");
89 delete work;
90 return;
91 }
92
93 work->data = reinterpret_cast<void *>(jsCb.get());
94
95 int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) {
96 // Js Thread
97 CapturerPeriodPositionJsCallback *event = reinterpret_cast<CapturerPeriodPositionJsCallback *>(work->data);
98 std::string request = event->callbackName;
99 napi_env env = event->callback->env_;
100 napi_ref callback = event->callback->cb_;
101 AUDIO_DEBUG_LOG("CapturerPeriodPositionCallbackNapi: JsCallBack %{public}s, uv_queue_work start",
102 request.c_str());
103 do {
104 CHECK_AND_BREAK_LOG(status != UV_ECANCELED, "%{public}s canceled", request.c_str());
105
106 napi_value jsCallback = nullptr;
107 napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
108 CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
109 request.c_str());
110
111 // Call back function
112 napi_value args[1] = { nullptr };
113 napi_create_int64(env, event->position, &args[0]);
114 CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[0] != nullptr,
115 "%{public}s fail to create position callback", request.c_str());
116
117 const size_t argCount = 1;
118 napi_value result = nullptr;
119 nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
120 CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to call position callback", request.c_str());
121 } while (0);
122 delete event;
123 delete work;
124 });
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
134