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 "NapiAudioRendererStateCallback"
17 #endif
18 
19 #include "napi_audio_renderer_state_callback.h"
20 #include "napi_audio_enum.h"
21 #include "napi_audio_error.h"
22 #include "napi_param_utils.h"
23 #include "audio_manager_log.h"
24 
25 using namespace std;
26 
27 namespace OHOS {
28 namespace AudioStandard {
NapiAudioRendererStateCallback(napi_env env)29 NapiAudioRendererStateCallback::NapiAudioRendererStateCallback(napi_env env)
30     : env_(env)
31 {
32     AUDIO_DEBUG_LOG("NapiAudioRendererStateCallback: instance create");
33 }
34 
~NapiAudioRendererStateCallback()35 NapiAudioRendererStateCallback::~NapiAudioRendererStateCallback()
36 {
37     AUDIO_DEBUG_LOG("NapiAudioRendererStateCallback: instance destroy");
38 }
39 
RemoveCallbackReference()40 void NapiAudioRendererStateCallback::RemoveCallbackReference()
41 {
42     std::lock_guard<std::mutex> lock(mutex_);
43     rendererStateCallback_.reset();
44 }
45 
SaveCallbackReference(napi_value args)46 void NapiAudioRendererStateCallback::SaveCallbackReference(napi_value args)
47 {
48     std::lock_guard<std::mutex> lock(mutex_);
49     napi_ref callback = nullptr;
50     const int32_t refCount = ARGS_ONE;
51     napi_status status = napi_create_reference(env_, args, refCount, &callback);
52     CHECK_AND_RETURN_LOG(status == napi_ok && callback != nullptr,
53         "NapiAudioRendererStateCallback: creating reference for callback fail");
54 
55     std::shared_ptr<AutoRef> cb = std::make_shared<AutoRef>(env_, callback);
56     CHECK_AND_RETURN_LOG(cb != nullptr, "NapiAudioRendererStateCallback: creating callback failed");
57 
58     rendererStateCallback_ = cb;
59 }
60 
OnRendererStateChange(const std::vector<std::unique_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)61 void NapiAudioRendererStateCallback::OnRendererStateChange(
62     const std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
63 {
64     AUDIO_PRERELEASE_LOGI("OnRendererStateChange entered");
65 
66     std::lock_guard<std::mutex> lock(mutex_);
67 
68     CHECK_AND_RETURN_LOG(rendererStateCallback_ != nullptr, "rendererStateCallback_ is nullptr!");
69 
70     std::unique_ptr<AudioRendererStateJsCallback> cb = std::make_unique<AudioRendererStateJsCallback>();
71     CHECK_AND_RETURN_LOG(cb != nullptr, "No memory!!");
72 
73     std::vector<std::unique_ptr<AudioRendererChangeInfo>> rendererChangeInfos;
74     for (const auto &changeInfo : audioRendererChangeInfos) {
75         rendererChangeInfos.push_back(std::make_unique<AudioRendererChangeInfo>(*changeInfo));
76     }
77 
78     cb->callback = rendererStateCallback_;
79     cb->changeInfos = move(rendererChangeInfos);
80 
81     return OnJsCallbackRendererState(cb);
82 }
83 
WorkCallbackInterruptDone(uv_work_t * work,int status)84 void NapiAudioRendererStateCallback::WorkCallbackInterruptDone(uv_work_t *work, int status)
85 {
86     std::shared_ptr<AudioRendererStateJsCallback> context(
87         static_cast<AudioRendererStateJsCallback*>(work->data),
88         [work](AudioRendererStateJsCallback* ptr) {
89             delete ptr;
90             delete work;
91     });
92     CHECK_AND_RETURN_LOG(work != nullptr, "work is nullptr");
93     AudioRendererStateJsCallback *event = reinterpret_cast<AudioRendererStateJsCallback *>(work->data);
94     CHECK_AND_RETURN_LOG(event != nullptr, "event is nullptr");
95     CHECK_AND_RETURN_LOG(event->callback != nullptr, "event is nullptr");
96     napi_env env = event->callback->env_;
97     napi_ref callback = event->callback->cb_;
98     napi_handle_scope scope = nullptr;
99     napi_open_handle_scope(env, &scope);
100     CHECK_AND_RETURN_LOG(scope != nullptr, "scope is nullptr");
101     do {
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, "callback get reference value fail");
105         napi_value args[ARGS_ONE] = { nullptr };
106         NapiParamUtils::SetRendererChangeInfos(env, event->changeInfos, args[PARAM0]);
107         CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[PARAM0] != nullptr,
108             "fail to convert to jsobj");
109 
110         const size_t argCount = ARGS_ONE;
111         napi_value result = nullptr;
112         nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
113         CHECK_AND_BREAK_LOG(nstatus == napi_ok, "fail to call Interrupt callback");
114     } while (0);
115     napi_close_handle_scope(env, scope);
116 }
117 
OnJsCallbackRendererState(std::unique_ptr<AudioRendererStateJsCallback> & jsCb)118 void NapiAudioRendererStateCallback::OnJsCallbackRendererState(std::unique_ptr<AudioRendererStateJsCallback> &jsCb)
119 {
120     uv_loop_s *loop = nullptr;
121     napi_get_uv_event_loop(env_, &loop);
122     CHECK_AND_RETURN_LOG(loop != nullptr, "loop is nullptr");
123 
124     uv_work_t *work = new(std::nothrow) uv_work_t;
125     CHECK_AND_RETURN_LOG(work != nullptr, "OnJsCallbackRendererState: No memory");
126 
127     work->data = reinterpret_cast<void *>(jsCb.get());
128     int ret = uv_queue_work_with_qos(loop, work, [] (uv_work_t *work) {}, WorkCallbackInterruptDone,
129         uv_qos_default);
130     if (ret != 0) {
131         AUDIO_ERR_LOG("Failed to execute libuv work queue");
132         delete work;
133     } else {
134         jsCb.release();
135     }
136 }
137 } // namespace AudioStandard
138 } // namespace OHOS
139