• 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 "NapiAudioSessionCallback"
17 #endif
18 
19 #include "napi_audio_session_callback.h"
20 #include "napi_param_utils.h"
21 
22 namespace OHOS {
23 namespace AudioStandard {
24 
NapiAudioSessionCallback(napi_env env)25 NapiAudioSessionCallback::NapiAudioSessionCallback(napi_env env)
26     :env_(env)
27 {
28     AUDIO_DEBUG_LOG("NapiAudioSessionCallback::Constructor");
29 }
30 
~NapiAudioSessionCallback()31 NapiAudioSessionCallback::~NapiAudioSessionCallback()
32 {
33     AUDIO_DEBUG_LOG("NapiAudioSessionCallback::Destructor");
34 }
35 
OnAudioSessionDeactive(const AudioSessionDeactiveEvent & deactiveEvent)36 void NapiAudioSessionCallback::OnAudioSessionDeactive(const AudioSessionDeactiveEvent &deactiveEvent)
37 {
38     AUDIO_INFO_LOG("OnAudioSessionDeactive is called AudioSessionDeactiveEvent=%{public}d",
39         deactiveEvent.deactiveReason);
40     CHECK_AND_RETURN_LOG(audioSessionJsCallback_ != nullptr,
41         "OnAudioSessionDeactive:No JS callback registered return");
42     std::unique_ptr<AudioSessionJsCallback> cb = std::make_unique<AudioSessionJsCallback>();
43     CHECK_AND_RETURN_LOG(cb != nullptr, "No memory");
44     cb->callback = audioSessionJsCallback_;
45     cb->callbackName = AUDIO_SESSION_CALLBACK_NAME;
46     cb->audioSessionDeactiveEvent.deactiveReason = deactiveEvent.deactiveReason;
47 
48     return OnJsCallbackAudioSession(cb);
49 }
50 
SaveCallbackReference(napi_value args)51 void NapiAudioSessionCallback::SaveCallbackReference(napi_value args)
52 {
53     std::lock_guard<std::mutex> lock(mutex_);
54     napi_ref callback = nullptr;
55     const int32_t refCount = 1;
56     napi_status status = napi_create_reference(env_, args, refCount, &callback);
57     CHECK_AND_RETURN_LOG(status == napi_ok && callback != nullptr,
58         "NapiAudioSessionCallback: creating reference for callback fail");
59     std::shared_ptr<AutoRef> cb = std::make_shared<AutoRef>(env_, callback);
60     audioSessionJsCallback_ = cb;
61 }
62 
WorkCallbackAudioSessionChangeDone(uv_work_t * work,int status)63 void NapiAudioSessionCallback::WorkCallbackAudioSessionChangeDone(uv_work_t *work, int status)
64 {
65     std::shared_ptr<AudioSessionJsCallback> context(
66         static_cast<AudioSessionJsCallback*>(work->data),
67         [work](AudioSessionJsCallback* ptr) {
68             delete ptr;
69             delete work;
70     });
71     CHECK_AND_RETURN_LOG(work != nullptr, "work is nullptr");
72     AudioSessionJsCallback *event = reinterpret_cast<AudioSessionJsCallback *>(work->data);
73     CHECK_AND_RETURN_LOG(event != nullptr, "event is nullptr");
74     std::string request = event->callbackName;
75     CHECK_AND_RETURN_LOG(event->callback != nullptr, "event is nullptr");
76     napi_env env = event->callback->env_;
77     napi_ref callback = event->callback->cb_;
78     napi_handle_scope scope = nullptr;
79     napi_open_handle_scope(env, &scope);
80     CHECK_AND_RETURN_LOG(scope != nullptr, "scope is nullptr");
81     AUDIO_INFO_LOG("JsCallBack %{public}s, uv_queue_work_with_qos start", request.c_str());
82     do {
83         CHECK_AND_BREAK_LOG(status != UV_ECANCELED, "%{public}s canceled", request.c_str());
84 
85         napi_value jsCallback = nullptr;
86         napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
87         CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
88             request.c_str());
89 
90         napi_value args[ARGS_ONE] = { nullptr };
91         NapiParamUtils::SetAudioSessionDeactiveEvent(env, event->audioSessionDeactiveEvent, args[PARAM0]);
92         CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[PARAM0] != nullptr,
93             "%{public}s fail to SetAudioSessionDeactiveEvent callback", request.c_str());
94 
95         const size_t argCount = ARGS_ONE;
96         napi_value result = nullptr;
97         nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
98         CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to call SetaudioSession callback",
99             request.c_str());
100     } while (0);
101     napi_close_handle_scope(env, scope);
102 }
103 
OnJsCallbackAudioSession(std::unique_ptr<AudioSessionJsCallback> & jsCb)104 void NapiAudioSessionCallback::OnJsCallbackAudioSession(std::unique_ptr<AudioSessionJsCallback> &jsCb)
105 {
106     uv_loop_s *loop = nullptr;
107     napi_get_uv_event_loop(env_, &loop);
108     CHECK_AND_RETURN_LOG(loop != nullptr, "loop is nullptr");
109 
110     uv_work_t *work = new(std::nothrow) uv_work_t;
111     CHECK_AND_RETURN_LOG(work != nullptr, "OnJsCallbackDeviceChange: No memory");
112 
113     if (jsCb.get() == nullptr) {
114         AUDIO_ERR_LOG("OnJsCallbackDeviceChange: jsCb.get() is null");
115         delete work;
116         return;
117     }
118 
119     work->data = reinterpret_cast<void *>(jsCb.get());
120     int ret = uv_queue_work_with_qos(loop, work, [] (uv_work_t *work) {}, WorkCallbackAudioSessionChangeDone,
121         uv_qos_default);
122     if (ret != 0) {
123         AUDIO_ERR_LOG("Failed to execute libuv work queue");
124         delete work;
125     } else {
126         jsCb.release();
127     }
128 }
129 } // namespace AudioStandard
130 } // namespace OHOS