• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "NapiRendererDataRequestCallback"
17 #endif
18 
19 #include "napi_renderer_data_request_callback.h"
20 
21 #include "audio_errors.h"
22 #include "audio_renderer_log.h"
23 
24 namespace OHOS {
25 namespace AudioStandard {
26 namespace {
27     const std::string RENDERER_DATA_REQUEST_CALLBACK_NAME = "dataRequest";
28 }
29 
NapiRendererDataRequestCallback(napi_env env,NapiAudioRenderer * napiRenderer)30 NapiRendererDataRequestCallback::NapiRendererDataRequestCallback(napi_env env, NapiAudioRenderer *napiRenderer)
31     : env_(env), napiRenderer_(napiRenderer)
32 {
33     AUDIO_INFO_LOG("instance create");
34 }
35 
~NapiRendererDataRequestCallback()36 NapiRendererDataRequestCallback::~NapiRendererDataRequestCallback()
37 {
38     AUDIO_INFO_LOG("instance destroy");
39 }
40 
SaveCallbackReference(const std::string & callbackName,napi_value args)41 void NapiRendererDataRequestCallback::SaveCallbackReference(const std::string &callbackName, napi_value args)
42 {
43     std::lock_guard<std::mutex> lock(mutex_);
44     napi_ref callback = nullptr;
45     const int32_t refCount = 1;
46     napi_status status = napi_create_reference(env_, args, refCount, &callback);
47     CHECK_AND_RETURN_LOG(status == napi_ok && callback != nullptr, "creating reference for callback fail");
48 
49     std::shared_ptr<AutoRef> cb = std::make_shared<AutoRef>(env_, callback);
50     if (callbackName == DATA_REQUEST_CALLBACK_NAME) {
51         rendererDataRequestCallback_ = cb;
52     } else {
53         AUDIO_ERR_LOG("Unknown callback type: %{public}s", callbackName.c_str());
54     }
55 }
56 
OnWriteData(size_t length)57 void NapiRendererDataRequestCallback::OnWriteData(size_t length)
58 {
59     std::lock_guard<std::mutex> lock(mutex_);
60     AUDIO_DEBUG_LOG("onDataRequest enqueue added");
61     CHECK_AND_RETURN_LOG(rendererDataRequestCallback_ != nullptr, "Cannot find the reference of dataRequest callback");
62     CHECK_AND_RETURN_LOG(napiRenderer_ != nullptr, "Cannot find the reference to audio renderer napi");
63     std::unique_ptr<RendererDataRequestJsCallback> cb = std::make_unique<RendererDataRequestJsCallback>();
64     CHECK_AND_RETURN_LOG(cb != nullptr, "No memory");
65     cb->callback = rendererDataRequestCallback_;
66     cb->callbackName = DATA_REQUEST_CALLBACK_NAME;
67     size_t reqLen = length;
68     cb->bufDesc_.buffer = nullptr;
69     cb->rendererNapiObj = napiRenderer_;
70     napiRenderer_->audioRenderer_->GetBufferDesc(cb->bufDesc_);
71     if (cb->bufDesc_.buffer == nullptr) {
72         return;
73     }
74     if (reqLen > cb->bufDesc_.bufLength) {
75         cb->bufDesc_.dataLength = cb->bufDesc_.bufLength;
76     } else {
77         cb->bufDesc_.dataLength = reqLen;
78     }
79     AudioRendererDataInfo audioRendererDataInfo = {};
80     audioRendererDataInfo.buffer = cb->bufDesc_.buffer;
81     audioRendererDataInfo.flag =  cb->bufDesc_.bufLength;
82     cb->audioRendererDataInfo = audioRendererDataInfo;
83     return OnJsRendererDataRequestCallback(cb);
84 }
85 
WorkCallbackRendererDataRequest(uv_work_t * work,int status)86 void NapiRendererDataRequestCallback::WorkCallbackRendererDataRequest(uv_work_t *work, int status)
87 {
88     // Js Thread
89     std::shared_ptr<RendererDataRequestJsCallback> context(
90         static_cast<RendererDataRequestJsCallback*>(work->data),
91         [work](RendererDataRequestJsCallback* ptr) {
92             delete ptr;
93             delete work;
94     });
95     CHECK_AND_RETURN_LOG(work != nullptr, "WorkCallbackRendererDataRequest work is nullptr");
96     RendererDataRequestJsCallback *event = reinterpret_cast<RendererDataRequestJsCallback *>(work->data);
97     CHECK_AND_RETURN_LOG(event != nullptr, "WorkCallbackRendererDataRequest event is nullptr");
98     std::string request = event->callbackName;
99 
100     CHECK_AND_RETURN_LOG(event->callback != nullptr, "event->callback is nullptr");
101     napi_env env = event->callback->env_;
102     napi_ref callback = event->callback->cb_;
103 
104     napi_handle_scope scope = nullptr;
105     napi_open_handle_scope(env, &scope);
106     CHECK_AND_RETURN_LOG(scope != nullptr, "scope is nullptr");
107     AUDIO_DEBUG_LOG("JsCallBack %{public}s, uv_queue_work_with_qos start", request.c_str());
108     do {
109         CHECK_AND_BREAK_LOG(status != UV_ECANCELED, "%{public}s canceled", request.c_str());
110 
111         napi_value jsCallback = nullptr;
112         napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
113         CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
114             request.c_str());
115 
116         napi_value args[ARGS_ONE] = { nullptr };
117         NapiParamUtils::SetNativeAudioRendererDataInfo(env, event->audioRendererDataInfo, args[0]);
118         CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[PARAM0] != nullptr,
119             "%{public}s fail to create position callback", request.c_str());
120         const size_t argCount = 1;
121         napi_value result = nullptr;
122         nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
123         event->rendererNapiObj->audioRenderer_->Enqueue(event->bufDesc_);
124             CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to call position callback", request.c_str());
125     } while (0);
126     napi_close_handle_scope(env, scope);
127 }
128 
OnJsRendererDataRequestCallback(std::unique_ptr<RendererDataRequestJsCallback> & jsCb)129 void NapiRendererDataRequestCallback::OnJsRendererDataRequestCallback(
130     std::unique_ptr<RendererDataRequestJsCallback> &jsCb)
131 {
132     uv_loop_s *loop = nullptr;
133     napi_get_uv_event_loop(env_, &loop);
134     CHECK_AND_RETURN_LOG(loop != nullptr, "loop is nullptr fail");
135 
136     uv_work_t *work = new(std::nothrow) uv_work_t;
137     CHECK_AND_RETURN_LOG(work != nullptr, "OnJsRendererPeriodPositionCallback: No memory");
138 
139     if (jsCb.get() == nullptr) {
140         AUDIO_ERR_LOG("OnJsRendererPeriodPositionCallback: jsCb.get() is null");
141         delete work;
142         return;
143     }
144     work->data = reinterpret_cast<void *>(jsCb.get());
145 
146     int ret = uv_queue_work_with_qos(loop, work, [] (uv_work_t *work) {},
147         WorkCallbackRendererDataRequest, uv_qos_default);
148     if (ret != 0) {
149         AUDIO_ERR_LOG("Failed to execute libuv work queue");
150         delete work;
151     } else {
152         jsCb.release();
153     }
154 }
155 }  // namespace AudioStandard
156 }  // namespace OHOS