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 #include <thread>
19 #include "js_native_api.h"
20 #include "napi_audio_session_callback.h"
21 #include "napi_param_utils.h"
22 #include "napi/native_api.h"
23
24 namespace OHOS {
25 namespace AudioStandard {
NapiAudioSessionCallback(napi_env env)26 NapiAudioSessionCallback::NapiAudioSessionCallback(napi_env env)
27 :env_(env)
28 {
29 AUDIO_DEBUG_LOG("NapiAudioSessionCallback::Constructor");
30 }
31
~NapiAudioSessionCallback()32 NapiAudioSessionCallback::~NapiAudioSessionCallback()
33 {
34 if (regAmSessionChgTsfn_) {
35 napi_release_threadsafe_function(amSessionChgTsfn_, napi_tsfn_abort);
36 }
37 AUDIO_DEBUG_LOG("NapiAudioSessionCallback::Destructor");
38 }
39
OnAudioSessionDeactive(const AudioSessionDeactiveEvent & deactiveEvent)40 void NapiAudioSessionCallback::OnAudioSessionDeactive(const AudioSessionDeactiveEvent &deactiveEvent)
41 {
42 AUDIO_INFO_LOG("OnAudioSessionDeactive is called AudioSessionDeactiveEvent=%{public}d",
43 deactiveEvent.deactiveReason);
44 CHECK_AND_RETURN_LOG(audioSessionJsCallback_ != nullptr,
45 "OnAudioSessionDeactive:No JS callback registered return");
46 std::unique_ptr<AudioSessionJsCallback> cb = std::make_unique<AudioSessionJsCallback>();
47 CHECK_AND_RETURN_LOG(cb != nullptr, "No memory");
48 cb->callback = audioSessionJsCallback_;
49 cb->callbackName = AUDIO_SESSION_CALLBACK_NAME;
50 cb->audioSessionDeactiveEvent.deactiveReason = deactiveEvent.deactiveReason;
51
52 return OnJsCallbackAudioSession(cb);
53 }
54
SaveCallbackReference(napi_value args)55 void NapiAudioSessionCallback::SaveCallbackReference(napi_value args)
56 {
57 std::lock_guard<std::mutex> lock(mutex_);
58 napi_ref callback = nullptr;
59 const int32_t refCount = 1;
60 napi_status status = napi_create_reference(env_, args, refCount, &callback);
61 CHECK_AND_RETURN_LOG(status == napi_ok && callback != nullptr,
62 "NapiAudioSessionCallback: creating reference for callback fail");
63 std::shared_ptr<AutoRef> cb = std::make_shared<AutoRef>(env_, callback);
64 audioSessionJsCallback_ = cb;
65 }
66
CreateAudioSessionTsfn(napi_env env)67 void NapiAudioSessionCallback::CreateAudioSessionTsfn(napi_env env)
68 {
69 regAmSessionChgTsfn_ = true;
70 std::string callbackName = "AudioSession";
71 napi_value cbName;
72 napi_create_string_utf8(env, callbackName.c_str(), callbackName.length(), &cbName);
73 napi_create_threadsafe_function(env, nullptr, nullptr, cbName, 0, 1, nullptr, AudioSessionTsfnFinalize,
74 nullptr, SafeJsCallbackAudioSessionWork, &amSessionChgTsfn_);
75 }
76
GetAudioSessionTsfnFlag()77 bool NapiAudioSessionCallback::GetAudioSessionTsfnFlag()
78 {
79 return regAmSessionChgTsfn_;
80 }
81
SafeJsCallbackAudioSessionWork(napi_env env,napi_value js_cb,void * context,void * data)82 void NapiAudioSessionCallback::SafeJsCallbackAudioSessionWork(napi_env env, napi_value js_cb, void *context, void *data)
83 {
84 AudioSessionJsCallback *event = reinterpret_cast<AudioSessionJsCallback *>(data);
85 CHECK_AND_RETURN_LOG((event != nullptr) && (event->callback != nullptr),
86 "OnJsCallbackAudioSession: no memory");
87 std::shared_ptr<AudioSessionJsCallback> safeContext(
88 static_cast<AudioSessionJsCallback*>(data),
89 [](AudioSessionJsCallback *ptr) {
90 delete ptr;
91 });
92
93 std::string request = event->callbackName;
94 napi_ref callback = event->callback->cb_;
95 napi_handle_scope scope = nullptr;
96 napi_open_handle_scope(env, &scope);
97 CHECK_AND_RETURN_LOG(scope != nullptr, "scope is nullptr");
98 AUDIO_INFO_LOG("SafeJsCallbackAudioSessionWork: safe js callback working.");
99 do {
100 napi_value jsCallback = nullptr;
101 napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
102 CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
103 request.c_str());
104
105 napi_value args[ARGS_ONE] = { nullptr };
106 NapiParamUtils::SetAudioSessionDeactiveEvent(env, event->audioSessionDeactiveEvent, args[PARAM0]);
107 CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[PARAM0] != nullptr,
108 "%{public}s fail to SetAudioSessionDeactiveEvent callback", request.c_str());
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, "%{public}s fail to call SetaudioSession callback",
114 request.c_str());
115 } while (0);
116 napi_close_handle_scope(env, scope);
117 }
118
AudioSessionTsfnFinalize(napi_env env,void * data,void * hint)119 void NapiAudioSessionCallback::AudioSessionTsfnFinalize(napi_env env, void *data, void *hint)
120 {
121 AUDIO_INFO_LOG("AudioSessionTsfnFinalize: safe thread resource release.");
122 }
123
OnJsCallbackAudioSession(std::unique_ptr<AudioSessionJsCallback> & jsCb)124 void NapiAudioSessionCallback::OnJsCallbackAudioSession(std::unique_ptr<AudioSessionJsCallback> &jsCb)
125 {
126 if (jsCb.get() == nullptr) {
127 AUDIO_ERR_LOG("NapiAudioSessionCallback: OnJsCallbackAudioSession: jsCb.get() is null");
128 return;
129 }
130
131 AudioSessionJsCallback *event = jsCb.release();
132 CHECK_AND_RETURN_LOG((event != nullptr) && (event->callback != nullptr), "event is nullptr.");
133
134 napi_acquire_threadsafe_function(amSessionChgTsfn_);
135 napi_call_threadsafe_function(amSessionChgTsfn_, event, napi_tsfn_blocking);
136 }
137 } // namespace AudioStandard
138 } // namespace OHOS