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 #include <uv.h>
16
17 #include "audio_errors.h"
18 #include "audio_log.h"
19 #include "audio_manager_interrupt_callback_napi.h"
20
21 namespace OHOS {
22 namespace AudioStandard {
AudioManagerInterruptCallbackNapi(napi_env env)23 AudioManagerInterruptCallbackNapi::AudioManagerInterruptCallbackNapi(napi_env env)
24 : env_(env)
25 {
26 AUDIO_INFO_LOG("AudioManagerInterruptCallbackNapi: instance create");
27 }
28
~AudioManagerInterruptCallbackNapi()29 AudioManagerInterruptCallbackNapi::~AudioManagerInterruptCallbackNapi()
30 {
31 AUDIO_INFO_LOG("AudioManagerInterruptCallbackNapi: instance destroy");
32 }
33
SaveCallbackReference(const std::string & callbackName,napi_value args)34 void AudioManagerInterruptCallbackNapi::SaveCallbackReference(const std::string &callbackName, napi_value args)
35 {
36 std::lock_guard<std::mutex> lock(mutex_);
37 napi_ref callback = nullptr;
38 const int32_t refCount = 1;
39 napi_status status = napi_create_reference(env_, args, refCount, &callback);
40 CHECK_AND_RETURN_LOG(status == napi_ok && callback != nullptr,
41 "SaveCallbackReference: creating reference for callback fail");
42 std::shared_ptr<AutoRef> cb = std::make_shared<AutoRef>(env_, callback);
43 if (callbackName == INTERRUPT_CALLBACK_NAME) {
44 audioManagerInterruptCallback_ = cb;
45 } else {
46 AUDIO_ERR_LOG("SaveCallbackReference: Unknown callback type: %{public}s", callbackName.c_str());
47 }
48 }
49
SetValueInt32(const napi_env & env,const std::string & fieldStr,const int intValue,napi_value & result)50 static void SetValueInt32(const napi_env &env, const std::string &fieldStr, const int intValue, napi_value &result)
51 {
52 napi_value value;
53 napi_create_int32(env, intValue, &value);
54 napi_set_named_property(env, result, fieldStr.c_str(), value);
55 }
56
SetValueBoolean(const napi_env & env,const std::string & fieldStr,const bool boolValue,napi_value & result)57 static void SetValueBoolean(const napi_env &env, const std::string &fieldStr, const bool boolValue, napi_value &result)
58 {
59 napi_value value;
60 napi_get_boolean(env, boolValue, &value);
61 napi_set_named_property(env, result, fieldStr.c_str(), value);
62 }
63
NativeInterruptActionToJsObj(const napi_env & env,napi_value & jsObj,const InterruptAction & interruptAction)64 static void NativeInterruptActionToJsObj(const napi_env &env, napi_value &jsObj,
65 const InterruptAction &interruptAction)
66 {
67 napi_create_object(env, &jsObj);
68 SetValueInt32(env, "actionType", static_cast<int32_t>(interruptAction.actionType), jsObj);
69 SetValueInt32(env, "type", static_cast<int32_t>(interruptAction.interruptType), jsObj);
70 SetValueInt32(env, "hint", static_cast<int32_t>(interruptAction.interruptHint), jsObj);
71 SetValueBoolean(env, "activated", interruptAction.activated, jsObj);
72 }
73
OnInterrupt(const InterruptAction & interruptAction)74 void AudioManagerInterruptCallbackNapi::OnInterrupt(const InterruptAction &interruptAction)
75 {
76 std::lock_guard<std::mutex> lock(mutex_);
77 AUDIO_INFO_LOG("OnInterrupt action: %{public}d IntType: %{public}d, IntHint: %{public}d, activated: %{public}d",
78 interruptAction.actionType, interruptAction.interruptType, interruptAction.interruptHint,
79 interruptAction.activated);
80 CHECK_AND_RETURN_LOG(audioManagerInterruptCallback_ != nullptr, "Cannot find the reference of interrupt callback");
81 std::unique_ptr<AudioManagerInterruptJsCallback> cb = std::make_unique<AudioManagerInterruptJsCallback>();
82 CHECK_AND_RETURN_LOG(cb != nullptr, "No memory");
83 cb->callback = audioManagerInterruptCallback_;
84 cb->callbackName = INTERRUPT_CALLBACK_NAME;
85 cb->interruptAction = interruptAction;
86 return OnJsCallbackAudioManagerInterrupt(cb);
87 }
88
OnJsCallbackAudioManagerInterrupt(std::unique_ptr<AudioManagerInterruptJsCallback> & jsCb)89 void AudioManagerInterruptCallbackNapi::OnJsCallbackAudioManagerInterrupt (
90 std::unique_ptr<AudioManagerInterruptJsCallback> &jsCb)
91 {
92 uv_loop_s *loop = nullptr;
93 napi_get_uv_event_loop(env_, &loop);
94 if (loop == nullptr) {
95 return;
96 }
97
98 uv_work_t *work = new(std::nothrow) uv_work_t;
99 if (work == nullptr) {
100 AUDIO_ERR_LOG("OnJsCallbackAudioManagerInterrupt: No memory");
101 return;
102 }
103 if (jsCb.get() == nullptr) {
104 AUDIO_ERR_LOG("OnJsCallbackAudioManagerInterrupt: jsCb.get() is null");
105 delete work;
106 return;
107 }
108 work->data = reinterpret_cast<void *>(jsCb.get());
109
110 int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) {
111 // Js Thread
112 AudioManagerInterruptJsCallback *event = reinterpret_cast<AudioManagerInterruptJsCallback *>(work->data);
113 std::string request = event->callbackName;
114 napi_env env = event->callback->env_;
115 napi_ref callback = event->callback->cb_;
116 AUDIO_INFO_LOG("OnJsCallbackAudioManagerInterrupt: JsCallBack %{public}s, uv_queue_work start",
117 request.c_str());
118 do {
119 CHECK_AND_BREAK_LOG(status != UV_ECANCELED, "%{public}s canceled", request.c_str());
120
121 napi_value jsCallback = nullptr;
122 napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
123 CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
124 request.c_str());
125
126 // Call back function
127 napi_value args[1] = { nullptr };
128 NativeInterruptActionToJsObj(env, args[0], event->interruptAction);
129 CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[0] != nullptr,
130 "%{public}s fail to create Interrupt callback", request.c_str());
131
132 const size_t argCount = 1;
133 napi_value result = nullptr;
134 nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
135 CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to call Interrupt callback", request.c_str());
136 } while (0);
137 delete event;
138 delete work;
139 });
140 if (ret != 0) {
141 AUDIO_ERR_LOG("OnJsCallbackAudioManagerInterrupt: Failed to execute libuv work queue");
142 delete work;
143 } else {
144 jsCb.release();
145 }
146 }
147 } // namespace AudioStandard
148 } // namespace OHOS
149