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 "renderer_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 RENDERER_PERIOD_POSITION_CALLBACK_NAME = "periodReach";
25 }
26
27 namespace OHOS {
28 namespace AudioStandard {
RendererPeriodPositionCallbackNapi(napi_env env)29 RendererPeriodPositionCallbackNapi::RendererPeriodPositionCallbackNapi(napi_env env)
30 : env_(env)
31 {
32 AUDIO_DEBUG_LOG("RendererPeriodPositionCallbackNapi: instance create");
33 }
34
~RendererPeriodPositionCallbackNapi()35 RendererPeriodPositionCallbackNapi::~RendererPeriodPositionCallbackNapi()
36 {
37 AUDIO_DEBUG_LOG("RendererPeriodPositionCallbackNapi: instance destroy");
38 }
39
SaveCallbackReference(const std::string & callbackName,napi_value args)40 void RendererPeriodPositionCallbackNapi::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 "RendererPeriodPositionCallbackNapi: creating reference for callback fail");
48
49 std::shared_ptr<AutoRef> cb = std::make_shared<AutoRef>(env_, callback);
50 if (callbackName == RENDERER_PERIOD_POSITION_CALLBACK_NAME) {
51 renderPeriodPositionCallback_ = cb;
52 } else {
53 AUDIO_ERR_LOG("RendererPeriodPositionCallbackNapi: Unknown callback type: %{public}s", callbackName.c_str());
54 }
55 }
56
OnPeriodReached(const int64_t & frameNumber)57 void RendererPeriodPositionCallbackNapi::OnPeriodReached(const int64_t &frameNumber)
58 {
59 std::lock_guard<std::mutex> lock(mutex_);
60 AUDIO_DEBUG_LOG("RendererPeriodPositionCallbackNapi: period reached");
61 CHECK_AND_RETURN_LOG(renderPeriodPositionCallback_ != nullptr, "Cannot find the reference of position callback");
62
63 std::unique_ptr<RendererPeriodPositionJsCallback> cb = std::make_unique<RendererPeriodPositionJsCallback>();
64 CHECK_AND_RETURN_LOG(cb != nullptr, "No memory");
65 cb->callback = renderPeriodPositionCallback_;
66 cb->callbackName = RENDERER_PERIOD_POSITION_CALLBACK_NAME;
67 cb->position = frameNumber;
68 return OnJsRendererPeriodPositionCallback(cb);
69 }
70
OnJsRendererPeriodPositionCallback(std::unique_ptr<RendererPeriodPositionJsCallback> & jsCb)71 void RendererPeriodPositionCallbackNapi::OnJsRendererPeriodPositionCallback(
72 std::unique_ptr<RendererPeriodPositionJsCallback> &jsCb)
73 {
74 uv_loop_s *loop = nullptr;
75 CHECK_AND_RETURN_LOG(env_ != nullptr,
76 "RendererPeriodPositionCallbackNapi: OnJsRendererPeriodPositionCallback: env_ is null");
77 napi_get_uv_event_loop(env_, &loop);
78 if (loop == nullptr) {
79 return;
80 }
81 uv_work_t *work = new(std::nothrow) uv_work_t;
82 CHECK_AND_RETURN_LOG(work != nullptr,
83 "RendererPeriodPositionCallbackNapi: OnJsRendererPeriodPositionCallback: No memory");
84 if (jsCb.get() == nullptr) {
85 AUDIO_ERR_LOG("RendererPeriodPositionCallbackNapi: OnJsRendererPeriodPositionCallback: jsCb.get() is null");
86 delete work;
87 return;
88 }
89 work->data = reinterpret_cast<void *>(jsCb.get());
90
91 int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) {
92 // Js Thread
93 RendererPeriodPositionJsCallback *event = reinterpret_cast<RendererPeriodPositionJsCallback *>(work->data);
94 std::string request = event->callbackName;
95 napi_env env = event->callback->env_;
96 napi_ref callback = event->callback->cb_;
97 AUDIO_DEBUG_LOG("RendererPeriodPositionCallbackNapi: JsCallBack %{public}s, uv_queue_work start",
98 request.c_str());
99 do {
100 CHECK_AND_BREAK_LOG(status != UV_ECANCELED, "%{public}s canceled", request.c_str());
101
102 napi_value jsCallback = nullptr;
103 napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
104 CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
105 request.c_str());
106
107 // Call back function
108 napi_value args[1] = { nullptr };
109 napi_create_int64(env, event->position, &args[0]);
110 CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[0] != nullptr,
111 "%{public}s fail to create position callback", request.c_str());
112
113 const size_t argCount = 1;
114 napi_value result = nullptr;
115 nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
116 CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to call position callback", request.c_str());
117 } while (0);
118 delete event;
119 delete work;
120 });
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
130