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_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_POSITION_CALLBACK_NAME = "markReach";
25 }
26
27 namespace OHOS {
28 namespace AudioStandard {
RendererPositionCallbackNapi(napi_env env)29 RendererPositionCallbackNapi::RendererPositionCallbackNapi(napi_env env)
30 : env_(env)
31 {
32 AUDIO_DEBUG_LOG("RendererPositionCallbackNapi: instance create");
33 }
34
~RendererPositionCallbackNapi()35 RendererPositionCallbackNapi::~RendererPositionCallbackNapi()
36 {
37 AUDIO_DEBUG_LOG("RendererPositionCallbackNapi: instance destroy");
38 }
39
SaveCallbackReference(const std::string & callbackName,napi_value args)40 void RendererPositionCallbackNapi::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 "RendererPositionCallbackNapi: creating reference for callback fail");
48
49 std::shared_ptr<AutoRef> cb = std::make_shared<AutoRef>(env_, callback);
50 if (callbackName == RENDERER_POSITION_CALLBACK_NAME) {
51 renderPositionCallback_ = cb;
52 } else {
53 AUDIO_ERR_LOG("RendererPositionCallbackNapi: Unknown callback type: %{public}s", callbackName.c_str());
54 }
55 }
56
OnMarkReached(const int64_t & framePosition)57 void RendererPositionCallbackNapi::OnMarkReached(const int64_t &framePosition)
58 {
59 std::lock_guard<std::mutex> lock(mutex_);
60 AUDIO_DEBUG_LOG("RendererPositionCallbackNapi: mark reached");
61 CHECK_AND_RETURN_LOG(renderPositionCallback_ != nullptr, "Cannot find the reference of position callback");
62
63 std::unique_ptr<RendererPositionJsCallback> cb = std::make_unique<RendererPositionJsCallback>();
64 CHECK_AND_RETURN_LOG(cb != nullptr, "No memory");
65 cb->callback = renderPositionCallback_;
66 cb->callbackName = RENDERER_POSITION_CALLBACK_NAME;
67 cb->position = framePosition;
68 return OnJsRendererPositionCallback(cb);
69 }
70
OnJsRendererPositionCallback(std::unique_ptr<RendererPositionJsCallback> & jsCb)71 void RendererPositionCallbackNapi::OnJsRendererPositionCallback(std::unique_ptr<RendererPositionJsCallback> &jsCb)
72 {
73 uv_loop_s *loop = nullptr;
74 napi_get_uv_event_loop(env_, &loop);
75 if (loop == nullptr) {
76 return;
77 }
78
79 uv_work_t *work = new(std::nothrow) uv_work_t;
80 if (work == nullptr) {
81 AUDIO_ERR_LOG("RendererPositionCallbackNapi: OnJsRendererPositionCallback: No memory");
82 return;
83 }
84 if (jsCb.get() == nullptr) {
85 AUDIO_ERR_LOG("RendererPositionCallbackNapi: OnJsRendererPositionCallback: 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 RendererPositionJsCallback *event = reinterpret_cast<RendererPositionJsCallback *>(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("RendererPositionCallbackNapi: JsCallBack %{public}s, uv_queue_work start", request.c_str());
98 do {
99 CHECK_AND_BREAK_LOG(status != UV_ECANCELED, "%{public}s canceled", request.c_str());
100
101 napi_value jsCallback = nullptr;
102 napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
103 CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
104 request.c_str());
105
106 // Call back function
107 napi_value args[1] = { nullptr };
108 napi_create_int64(env, event->position, &args[0]);
109 CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[0] != nullptr,
110 "%{public}s fail to create position callback", request.c_str());
111
112 const size_t argCount = 1;
113 napi_value result = nullptr;
114 nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
115 CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to call position callback", request.c_str());
116 } while (0);
117 delete event;
118 delete work;
119 });
120 if (ret != 0) {
121 AUDIO_ERR_LOG("Failed to execute libuv work queue");
122 delete work;
123 } else {
124 jsCb.release();
125 }
126 }
127 } // namespace AudioStandard
128 } // namespace OHOS
129