• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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("instance create");
33 }
34 
~RendererPeriodPositionCallbackNapi()35 RendererPeriodPositionCallbackNapi::~RendererPeriodPositionCallbackNapi()
36 {
37     AUDIO_DEBUG_LOG("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                          "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("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("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         "OnJsRendererPeriodPositionCallback: env_ is null");
77 
78     napi_get_uv_event_loop(env_, &loop);
79     if (loop == nullptr) {
80         return;
81     }
82     uv_work_t *work = new(std::nothrow) uv_work_t;
83     CHECK_AND_RETURN_LOG(work != nullptr,
84         "OnJsRendererPeriodPositionCallback: No memory");
85 
86     if (jsCb.get() == nullptr) {
87         AUDIO_ERR_LOG("OnJsRendererPeriodPositionCallback: jsCb.get() is null");
88         delete work;
89         return;
90     }
91     work->data = reinterpret_cast<void *>(jsCb.get());
92 
93     int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) {
94         // Js Thread
95         RendererPeriodPositionJsCallback *event = reinterpret_cast<RendererPeriodPositionJsCallback *>(work->data);
96         std::string request = event->callbackName;
97         napi_env env = event->callback->env_;
98         napi_ref callback = event->callback->cb_;
99         AUDIO_DEBUG_LOG("JsCallBack %{public}s, uv_queue_work start",
100             request.c_str());
101         do {
102             CHECK_AND_BREAK_LOG(status != UV_ECANCELED, "%{public}s canceled", request.c_str());
103 
104             napi_value jsCallback = nullptr;
105             napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
106             CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
107                 request.c_str());
108 
109             // Call back function
110             napi_value args[1] = { nullptr };
111             napi_create_int64(env, event->position, &args[0]);
112             CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[0] != nullptr,
113                 "%{public}s fail to create position callback", request.c_str());
114 
115             const size_t argCount = 1;
116             napi_value result = nullptr;
117             nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
118             CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to call position callback", request.c_str());
119         } while (0);
120         delete event;
121         delete work;
122     });
123     if (ret != 0) {
124         AUDIO_ERR_LOG("Failed to execute libuv work queue");
125         delete work;
126     } else {
127         jsCb.release();
128     }
129 }
130 }  // namespace AudioStandard
131 }  // namespace OHOS
132