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 
16 #include "ringtone_player_callback_napi.h"
17 
18 #include <uv.h>
19 
20 #include "media_errors.h"
21 #include "system_sound_log.h"
22 
23 namespace {
24 const std::string AUDIO_INTERRUPT_CALLBACK_NAME = "audioInterrupt";
25 
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_AUDIO_NAPI, "RingtonePlayerCallbackNapi"};
27 }
28 
29 namespace OHOS {
30 namespace Media {
RingtonePlayerCallbackNapi(napi_env env)31 RingtonePlayerCallbackNapi::RingtonePlayerCallbackNapi(napi_env env)
32     : env_(env)
33 {
34     MEDIA_LOGI("RingtonePlayerCallbackNapi: instance create");
35 }
36 
~RingtonePlayerCallbackNapi()37 RingtonePlayerCallbackNapi::~RingtonePlayerCallbackNapi()
38 {
39     MEDIA_LOGI("RingtonePlayerCallbackNapi: instance destroy");
40 }
41 
SaveCallbackReference(const std::string & callbackName,napi_value args)42 void RingtonePlayerCallbackNapi::SaveCallbackReference(const std::string &callbackName, napi_value args)
43 {
44     std::lock_guard<std::mutex> lock(mutex_);
45     napi_ref callback = nullptr;
46     const int32_t refCount = 1;
47     napi_status status = napi_create_reference(env_, args, refCount, &callback);
48     CHECK_AND_RETURN_LOG(status == napi_ok && callback != nullptr,
49                          "RingtonePlayerCallbackNapi: creating reference for callback fail");
50 
51     std::shared_ptr<AutoRef> cb = std::make_shared<AutoRef>(env_, callback);
52     if (callbackName == AUDIO_INTERRUPT_CALLBACK_NAME) {
53         interruptCallback_ = cb;
54     } else {
55         MEDIA_LOGE("RingtonePlayerCallbackNapi: Unknown callback type: %{public}s", callbackName.c_str());
56     }
57 }
58 
RemoveCallbackReference(const std::string & callbackName)59 void RingtonePlayerCallbackNapi::RemoveCallbackReference(const std::string &callbackName)
60 {
61     std::lock_guard<std::mutex> lock(mutex_);
62 
63     if (callbackName == AUDIO_INTERRUPT_CALLBACK_NAME) {
64         interruptCallback_ = nullptr;
65     } else {
66         MEDIA_LOGE("Unknown callback type: %{public}s", callbackName.c_str());
67     }
68 }
69 
SetValueInt32(const napi_env & env,const std::string & fieldStr,const int intValue,napi_value & result)70 static void SetValueInt32(const napi_env& env, const std::string& fieldStr, const int intValue, napi_value& result)
71 {
72     napi_handle_scope scope = nullptr;
73     napi_open_handle_scope(env, &scope);
74 
75     napi_value value = nullptr;
76     napi_create_int32(env, intValue, &value);
77     napi_set_named_property(env, result, fieldStr.c_str(), value);
78 
79     napi_close_handle_scope(env, scope);
80 }
81 
NativeInterruptEventToJsObj(const napi_env & env,napi_value & jsObj,const AudioStandard::InterruptEvent & interruptEvent)82 static void NativeInterruptEventToJsObj(const napi_env& env, napi_value& jsObj,
83     const AudioStandard::InterruptEvent& interruptEvent)
84 {
85     napi_create_object(env, &jsObj);
86     SetValueInt32(env, "eventType", static_cast<int32_t>(interruptEvent.eventType), jsObj);
87     SetValueInt32(env, "forceType", static_cast<int32_t>(interruptEvent.forceType), jsObj);
88     SetValueInt32(env, "hintType", static_cast<int32_t>(interruptEvent.hintType), jsObj);
89 }
90 
OnInterrupt(const AudioStandard::InterruptEvent & interruptEvent)91 void RingtonePlayerCallbackNapi::OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent)
92 {
93     std::lock_guard<std::mutex> lock(mutex_);
94     MEDIA_LOGI("RingtonePlayerCallbackNapi: OnInterrupt is called");
95     MEDIA_LOGI("RingtonePlayerCallbackNapi: hintType: %{public}d", interruptEvent.hintType);
96     CHECK_AND_RETURN_LOG(interruptCallback_ != nullptr, "Cannot find the reference of interrupt callback");
97 
98     std::unique_ptr<RingtonePlayerJsCallback> cb = std::make_unique<RingtonePlayerJsCallback>();
99     CHECK_AND_RETURN_LOG(cb != nullptr, "No memory");
100     cb->callback = interruptCallback_;
101     cb->callbackName = AUDIO_INTERRUPT_CALLBACK_NAME;
102     cb->interruptEvent = interruptEvent;
103     return OnJsCallbackInterrupt(cb);
104 }
105 
OnJsCallbackInterrupt(std::unique_ptr<RingtonePlayerJsCallback> & jsCb)106 void RingtonePlayerCallbackNapi::OnJsCallbackInterrupt(std::unique_ptr<RingtonePlayerJsCallback> &jsCb)
107 {
108     uv_loop_s *loop = nullptr;
109     napi_get_uv_event_loop(env_, &loop);
110     CHECK_AND_RETURN(loop != nullptr);
111 
112     uv_work_t *work = new(std::nothrow) uv_work_t;
113     CHECK_AND_RETURN_LOG(work != nullptr, "RingtonePlayerCallbackNapi: OnJsCallBackInterrupt: No memory");
114     if (jsCb.get() == nullptr) {
115         MEDIA_LOGE("RingtonePlayerCallbackNapi: OnJsCallBackInterrupt: jsCb.get() is null");
116         delete work;
117         return;
118     }
119     work->data = reinterpret_cast<void *>(jsCb.get());
120 
121     int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) {
122         // Js Thread
123         RingtonePlayerJsCallback *event = reinterpret_cast<RingtonePlayerJsCallback *>(work->data);
124         std::string request = event->callbackName;
125         napi_env env = event->callback->env_;
126         napi_ref callback = event->callback->cb_;
127         napi_handle_scope scope = nullptr;
128         napi_open_handle_scope(env, &scope);
129         MEDIA_LOGI("RingtonePlayerCallbackNapi: JsCallBack %{public}s, uv_queue_work start", request.c_str());
130         do {
131             CHECK_AND_BREAK_LOG(status != UV_ECANCELED, "%{public}s cancelled", request.c_str());
132 
133             napi_value jsCallback = nullptr;
134             napi_status napiStatus = napi_get_reference_value(env, callback, &jsCallback);
135             CHECK_AND_BREAK_LOG(napiStatus == napi_ok && jsCallback != nullptr,
136                 "%{public}s get reference value fail", request.c_str());
137 
138             // Call back function
139             napi_value args[1] = { nullptr };
140             NativeInterruptEventToJsObj(env, args[0], event->interruptEvent);
141             CHECK_AND_BREAK_LOG(napiStatus == napi_ok && args[0] != nullptr,
142                 "%{public}s fail to create Interrupt callback", request.c_str());
143 
144             const size_t argCount = 1;
145             napi_value result = nullptr;
146             napiStatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
147             CHECK_AND_BREAK_LOG(napiStatus == napi_ok, "%{public}s fail to send interrupt callback", request.c_str());
148         } while (0);
149         napi_close_handle_scope(env, scope);
150         delete event;
151         delete work;
152     });
153     if (ret != 0) {
154         MEDIA_LOGE("Failed to execute libuv work queue");
155         delete work;
156     } else {
157         jsCb.release();
158     }
159 }
160 }  // namespace Media
161 }  // namespace OHOS
162