1 /*
2 * Copyright (c) 2022-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 <memory>
17
18 #include "avsession_log.h"
19 #include "avsession_info.h"
20 #include "avsession_trace.h"
21 #include "napi_session_listener.h"
22
23 namespace OHOS::AVSession {
NapiSessionListener()24 NapiSessionListener::NapiSessionListener()
25 {
26 SLOGI("construct");
27 }
28
~NapiSessionListener()29 NapiSessionListener::~NapiSessionListener()
30 {
31 SLOGI("destroy");
32 }
33
34 template<typename T>
HandleEvent(int32_t event,const T & param)35 void NapiSessionListener::HandleEvent(int32_t event, const T& param)
36 {
37 std::lock_guard<std::mutex> lockGuard(lock_);
38 if (callbacks_[event].empty()) {
39 SLOGE("not register callback event=%{public}d", event);
40 return;
41 }
42 for (auto& ref : callbacks_[event]) {
43 asyncCallback_->Call(ref, [param](napi_env env, int& argc, napi_value* argv) {
44 argc = 1;
45 NapiUtils::SetValue(env, param, *argv);
46 });
47 }
48 }
49
OnSessionCreate(const AVSessionDescriptor & descriptor)50 void NapiSessionListener::OnSessionCreate(const AVSessionDescriptor& descriptor)
51 {
52 AVSESSION_TRACE_SYNC_START("NapiSessionListener::OnSessionCreate");
53 SLOGI("sessionId=%{public}s", descriptor.sessionId_.c_str());
54 HandleEvent(EVENT_SESSION_CREATED, descriptor);
55 }
56
OnSessionRelease(const AVSessionDescriptor & descriptor)57 void NapiSessionListener::OnSessionRelease(const AVSessionDescriptor& descriptor)
58 {
59 SLOGI("sessionId=%{public}s", descriptor.sessionId_.c_str());
60 HandleEvent(EVENT_SESSION_DESTROYED, descriptor);
61 }
62
OnTopSessionChange(const AVSessionDescriptor & descriptor)63 void NapiSessionListener::OnTopSessionChange(const AVSessionDescriptor& descriptor)
64 {
65 AVSESSION_TRACE_SYNC_START("NapiSessionListener::OnTopSessionChange");
66 SLOGI("sessionId=%{public}s", descriptor.sessionId_.c_str());
67 HandleEvent(EVENT_TOP_SESSION_CHANGED, descriptor);
68 }
69
OnAudioSessionChecked(const int32_t uid)70 void NapiSessionListener::OnAudioSessionChecked(const int32_t uid)
71 {
72 AVSESSION_TRACE_SYNC_START("NapiSessionListener::OnAudioSessionCheck");
73 SLOGI("uid=%{public}d checked", uid);
74 HandleEvent(EVENT_AUDIO_SESSION_CHECKED, uid);
75 }
76
OnDeviceAvailable(const OutputDeviceInfo & castOutputDeviceInfo)77 void NapiSessionListener::OnDeviceAvailable(const OutputDeviceInfo& castOutputDeviceInfo)
78 {
79 AVSESSION_TRACE_SYNC_START("NapiSessionListener::OnDeviceAvailable");
80 SLOGI("Start handle device found event");
81 HandleEvent(EVENT_DEVICE_AVAILABLE, castOutputDeviceInfo);
82 }
83
OnDeviceOffline(const std::string & deviceId)84 void NapiSessionListener::OnDeviceOffline(const std::string& deviceId)
85 {
86 AVSESSION_TRACE_SYNC_START("NapiSessionListener::OnDeviceOffline");
87 SLOGI("Start handle device offline event");
88 HandleEvent(EVENT_DEVICE_OFFLINE, deviceId);
89 }
90
AddCallback(napi_env env,int32_t event,napi_value callback)91 napi_status NapiSessionListener::AddCallback(napi_env env, int32_t event, napi_value callback)
92 {
93 std::lock_guard<std::mutex> lockGuard(lock_);
94 napi_ref ref = nullptr;
95 CHECK_AND_RETURN_RET_LOG(napi_ok == NapiUtils::GetRefByCallback(env, callbacks_[event], callback, ref),
96 napi_generic_failure, "get callback reference failed");
97 CHECK_AND_RETURN_RET_LOG(ref == nullptr, napi_ok, "callback has been registered");
98 napi_status status = napi_create_reference(env, callback, 1, &ref);
99 if (status != napi_ok) {
100 SLOGE("napi_create_reference failed");
101 return status;
102 }
103 if (asyncCallback_ == nullptr) {
104 asyncCallback_ = std::make_shared<NapiAsyncCallback>(env);
105 }
106 callbacks_[event].push_back(ref);
107 return napi_ok;
108 }
109
RemoveCallback(napi_env env,int32_t event,napi_value callback)110 napi_status NapiSessionListener::RemoveCallback(napi_env env, int32_t event, napi_value callback)
111 {
112 std::lock_guard<std::mutex> lockGuard(lock_);
113 if (callback == nullptr) {
114 for (auto& callbackRef : callbacks_[event]) {
115 napi_status ret = napi_delete_reference(env, callbackRef);
116 CHECK_AND_RETURN_RET_LOG(napi_ok == ret, ret, "delete callback reference failed");
117 callbackRef = nullptr;
118 }
119 callbacks_[event].clear();
120 return napi_ok;
121 }
122 napi_ref ref = nullptr;
123 CHECK_AND_RETURN_RET_LOG(napi_ok == NapiUtils::GetRefByCallback(env, callbacks_[event], callback, ref),
124 napi_generic_failure, "get callback reference failed");
125 CHECK_AND_RETURN_RET_LOG(ref != nullptr, napi_ok, "callback has been remove");
126 callbacks_[event].remove(ref);
127 return napi_delete_reference(env, ref);
128 }
129 }