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