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 "avsession_manager_impl.h"
17 #include "iservice_registry.h"
18 #include "ipc_skeleton.h"
19 #include "system_ability_definition.h"
20 #include "avsession_log.h"
21 #include "avsession_errors.h"
22 #include "session_listener_client.h"
23 #include "avsession_trace.h"
24 #include "avsession_sysevent.h"
25 #include "avsession_utils.h"
26
27 namespace OHOS::AVSession {
AVSessionManagerImpl()28 AVSessionManagerImpl::AVSessionManagerImpl()
29 {
30 SLOGD("constructor");
31 }
32
GetService()33 sptr<AVSessionServiceProxy> AVSessionManagerImpl::GetService()
34 {
35 std::lock_guard<std::mutex> lockGuard(lock_);
36 if (service_) {
37 return service_;
38 }
39
40 auto mgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
41 if (mgr == nullptr) {
42 SLOGE("failed to get sa mgr");
43 return nullptr;
44 }
45 auto object = mgr->GetSystemAbility(AVSESSION_SERVICE_ID);
46 if (object == nullptr) {
47 SLOGE("failed to get service");
48 return nullptr;
49 }
50 service_ = iface_cast<AVSessionServiceProxy>(object);
51 if (service_ != nullptr) {
52 serviceDeathRecipient_ = new(std::nothrow) ServiceDeathRecipient([this] { OnServiceDie(); });
53 if (serviceDeathRecipient_ == nullptr) {
54 SLOGE("register ServiceDeathRecipient failed");
55 return nullptr;
56 }
57
58 sptr<IAVSessionService> serviceBase = service_;
59 serviceBase->AsObject()->AddDeathRecipient(serviceDeathRecipient_);
60
61 SLOGD("get service success");
62 RegisterClientDeathObserver();
63 }
64 return service_;
65 }
66
OnServiceDie()67 void AVSessionManagerImpl::OnServiceDie()
68 {
69 SLOGI("enter");
70 auto callback = deathCallback_;
71 {
72 std::lock_guard<std::mutex> lockGuard(lock_);
73 service_.clear();
74 listenerMapByUserId_.clear();
75 deathCallback_ = nullptr;
76 }
77 if (callback) {
78 callback();
79 }
80 HISYSEVENT_RESET;
81 HISYSEVENT_UNREGISTER;
82 std::string cachePath(AVSessionUtils::GetCachePathName());
83 AVSessionUtils::DeleteCacheFiles(cachePath);
84 }
85
CreateSession(const std::string& tag, int32_t type, const AppExecFwk::ElementName& elementName)86 std::shared_ptr<AVSession> AVSessionManagerImpl::CreateSession(const std::string& tag, int32_t type,
87 const AppExecFwk::ElementName& elementName)
88 {
89 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::CreateSession");
90 if (tag.empty() || elementName.GetBundleName().empty() || elementName.GetAbilityName().empty()) {
91 SLOGE("param is invalid");
92 return nullptr;
93 }
94 if (type != AVSession::SESSION_TYPE_AUDIO && type != AVSession::SESSION_TYPE_VIDEO
95 && type != AVSession::SESSION_TYPE_VOICE_CALL && type != AVSession::SESSION_TYPE_VIDEO_CALL) {
96 SLOGE("type is invalid");
97 return nullptr;
98 }
99
100 auto service = GetService();
101 return service ? service->CreateSession(tag, type, elementName) : nullptr;
102 }
103
CreateSession(const std::string& tag, int32_t type, const AppExecFwk::ElementName& elementName, std::shared_ptr<AVSession>& session)104 int32_t AVSessionManagerImpl::CreateSession(const std::string& tag, int32_t type,
105 const AppExecFwk::ElementName& elementName,
106 std::shared_ptr<AVSession>& session)
107 {
108 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::CreateSession with ret");
109 if (tag.empty() || elementName.GetBundleName().empty() || elementName.GetAbilityName().empty()) {
110 SLOGE("param is invalid");
111 return ERR_INVALID_PARAM;
112 }
113 if (type != AVSession::SESSION_TYPE_AUDIO && type != AVSession::SESSION_TYPE_VIDEO
114 && type != AVSession::SESSION_TYPE_VOICE_CALL && type != AVSession::SESSION_TYPE_VIDEO_CALL) {
115 SLOGE("type is invalid");
116 return ERR_INVALID_PARAM;
117 }
118
119 auto service = GetService();
120 return service ? service->CreateSession(tag, type, elementName, session) : ERR_SERVICE_NOT_EXIST;
121 }
122
GetAllSessionDescriptors(std::vector<AVSessionDescriptor>& descriptors)123 int32_t AVSessionManagerImpl::GetAllSessionDescriptors(std::vector<AVSessionDescriptor>& descriptors)
124 {
125 auto service = GetService();
126 return service ? service->GetAllSessionDescriptors(descriptors) : ERR_SERVICE_NOT_EXIST;
127 }
128
GetActivatedSessionDescriptors(std::vector<AVSessionDescriptor>& activatedSessions)129 int32_t AVSessionManagerImpl::GetActivatedSessionDescriptors(std::vector<AVSessionDescriptor>& activatedSessions)
130 {
131 std::vector<AVSessionDescriptor> descriptors;
132 int32_t ret = GetAllSessionDescriptors(descriptors);
133 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "GetAllSessionDescriptors failed");
134
135 for (const auto& descriptor : descriptors) {
136 if (descriptor.isActive_) {
137 activatedSessions.push_back(descriptor);
138 }
139 }
140 return ret;
141 }
142
GetSessionDescriptorsBySessionId(const std::string& sessionId, AVSessionDescriptor& descriptor)143 int32_t AVSessionManagerImpl::GetSessionDescriptorsBySessionId(const std::string& sessionId,
144 AVSessionDescriptor& descriptor)
145 {
146 if (sessionId.empty()) {
147 SLOGE("sessionId is invalid");
148 return ERR_INVALID_PARAM;
149 }
150
151 auto service = GetService();
152 return service ? service->GetSessionDescriptorsBySessionId(sessionId, descriptor) : ERR_SERVICE_NOT_EXIST;
153 }
154
GetHistoricalSessionDescriptors(int32_t maxSize, std::vector<AVSessionDescriptor>& descriptors)155 int32_t AVSessionManagerImpl::GetHistoricalSessionDescriptors(int32_t maxSize,
156 std::vector<AVSessionDescriptor>& descriptors)
157 {
158 auto service = GetService();
159 return service ? service->GetHistoricalSessionDescriptors(maxSize, descriptors) : ERR_SERVICE_NOT_EXIST;
160 }
161
GetHistoricalAVQueueInfos(int32_t maxSize, int32_t maxAppSize, std::vector<AVQueueInfo>& avQueueInfos)162 int32_t AVSessionManagerImpl::GetHistoricalAVQueueInfos(int32_t maxSize, int32_t maxAppSize,
163 std::vector<AVQueueInfo>& avQueueInfos)
164 {
165 auto service = GetService();
166 return service ? service->GetHistoricalAVQueueInfos(maxSize, maxAppSize, avQueueInfos) : ERR_SERVICE_NOT_EXIST;
167 }
168
StartAVPlayback(const std::string& bundleName, const std::string& assetId)169 int32_t AVSessionManagerImpl::StartAVPlayback(const std::string& bundleName, const std::string& assetId)
170 {
171 auto service = GetService();
172 return service ? service->StartAVPlayback(bundleName, assetId) : ERR_SERVICE_NOT_EXIST;
173 }
174
IsAudioPlaybackAllowed(const int32_t uid, const int32_t pid)175 bool AVSessionManagerImpl::IsAudioPlaybackAllowed(const int32_t uid, const int32_t pid)
176 {
177 auto service = GetService();
178 return service ? service->IsAudioPlaybackAllowed(uid, pid) : ERR_SERVICE_NOT_EXIST;
179 }
180
CreateController(const std::string& sessionId, std::shared_ptr<AVSessionController>& controller)181 int32_t AVSessionManagerImpl::CreateController(const std::string& sessionId,
182 std::shared_ptr<AVSessionController>& controller)
183 {
184 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::CreateController");
185 if (sessionId.empty()) {
186 SLOGE("sessionId is invalid");
187 return ERR_INVALID_PARAM;
188 }
189
190 auto service = GetService();
191 return service ? service->CreateController(sessionId, controller) : ERR_SERVICE_NOT_EXIST;
192 }
193
194 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
GetAVCastController(const std::string& sessionId, std::shared_ptr<AVCastController>& castController)195 int32_t AVSessionManagerImpl::GetAVCastController(const std::string& sessionId,
196 std::shared_ptr<AVCastController>& castController)
197 {
198 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::GetAVCastController");
199 if (sessionId.empty()) {
200 SLOGE("sessionId is invalid");
201 return ERR_INVALID_PARAM;
202 }
203
204 auto service = GetService();
205 return service ? service->GetAVCastController(sessionId, castController) : ERR_SERVICE_NOT_EXIST;
206 }
207 #endif
208
GetSessionListenerClient(const std::shared_ptr<SessionListener>& listener)209 sptr<ISessionListener> AVSessionManagerImpl::GetSessionListenerClient(const std::shared_ptr<SessionListener>& listener)
210 {
211 if (listener == nullptr) {
212 SLOGE("listener recv is nullptr");
213 return nullptr;
214 }
215
216 sptr<ISessionListener> listenerPtr = new(std::nothrow) SessionListenerClient(listener);
217 if (listenerPtr == nullptr) {
218 SLOGE("listener create is nullptr");
219 return nullptr;
220 }
221 return listenerPtr;
222 }
223
RegisterSessionListener(const std::shared_ptr<SessionListener>& listener)224 int32_t AVSessionManagerImpl::RegisterSessionListener(const std::shared_ptr<SessionListener>& listener)
225 {
226 auto service = GetService();
227 if (service == nullptr) {
228 return ERR_SERVICE_NOT_EXIST;
229 }
230
231 std::lock_guard<std::mutex> lockGuard(lock_);
232 sptr<ISessionListener> listenerPtr = GetSessionListenerClient(listener);
233 if (listenerPtr == nullptr) {
234 return ERR_INVALID_PARAM;
235 }
236
237 int32_t ret = service->RegisterSessionListener(listenerPtr);
238 if (ret <= AVSESSION_SUCCESS) {
239 SLOGE("RegisterSessionListener fail with ret %{public}d", ret);
240 return ret;
241 }
242 SLOGI("RegisterSessionListener for user %{public}d", ret);
243 listenerMapByUserId_[ret] = listenerPtr;
244 return AVSESSION_SUCCESS;
245 }
246
RegisterSessionListenerForAllUsers(const std::shared_ptr<SessionListener>& listener)247 int32_t AVSessionManagerImpl::RegisterSessionListenerForAllUsers(const std::shared_ptr<SessionListener>& listener)
248 {
249 SLOGI("RegisterSessionListenerForAllUsers in");
250 auto service = GetService();
251 if (service == nullptr) {
252 return ERR_SERVICE_NOT_EXIST;
253 }
254
255 std::lock_guard<std::mutex> lockGuard(lock_);
256 sptr<ISessionListener> listenerPtr = GetSessionListenerClient(listener);
257 if (listenerPtr == nullptr) {
258 return ERR_INVALID_PARAM;
259 }
260
261 auto ret = service->RegisterSessionListenerForAllUsers(listenerPtr);
262 if (ret != AVSESSION_SUCCESS) {
263 SLOGE("RegisterSessionListenerForAllUsers fail with ret %{public}d", ret);
264 return ret;
265 }
266 listenerMapByUserId_[userIdForAllUsers_] = listenerPtr;
267 return AVSESSION_SUCCESS;
268 }
269
RegisterServiceDeathCallback(const DeathCallback& callback)270 int32_t AVSessionManagerImpl::RegisterServiceDeathCallback(const DeathCallback& callback)
271 {
272 deathCallback_ = callback;
273 return AVSESSION_SUCCESS;
274 }
275
UnregisterServiceDeathCallback()276 int32_t AVSessionManagerImpl::UnregisterServiceDeathCallback()
277 {
278 deathCallback_ = nullptr;
279 return AVSESSION_SUCCESS;
280 }
281
SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent)282 int32_t AVSessionManagerImpl::SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent)
283 {
284 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::SendSystemAVKeyEvent");
285 if (!keyEvent.IsValid()) {
286 SLOGE("keyEvent is invalid");
287 return ERR_COMMAND_NOT_SUPPORT;
288 }
289
290 auto service = GetService();
291 return service ? service->SendSystemAVKeyEvent(keyEvent) : ERR_SERVICE_NOT_EXIST;
292 }
293
SendSystemControlCommand(const AVControlCommand& command)294 int32_t AVSessionManagerImpl::SendSystemControlCommand(const AVControlCommand& command)
295 {
296 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::SendSystemControlCommand");
297 if (!command.IsValid()) {
298 SLOGE("command is invalid");
299 HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "INVALID_COMMAND", "CMD", command.GetCommand(),
300 "ERROR_CODE", ERR_INVALID_PARAM, "ERROR_INFO", "avsessionmanagerimpl command is invalid");
301 return ERR_COMMAND_NOT_SUPPORT;
302 }
303
304 auto service = GetService();
305 if (service == nullptr) {
306 HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "GET_SERVICE_ERROR",
307 "ERROR_CODE", ERR_SERVICE_NOT_EXIST, "ERROR_INFO", "mgrimp sendsystemcontrolcommand get service error");
308 return ERR_SERVICE_NOT_EXIST;
309 }
310 return service->SendSystemControlCommand(command);
311 }
312
CastAudio(const SessionToken& token, const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)313 int32_t AVSessionManagerImpl::CastAudio(const SessionToken& token,
314 const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)
315 {
316 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::CastAudio");
317 CHECK_AND_RETURN_RET_LOG(descriptors.size() > 0, ERR_INVALID_PARAM, "devices size is zero");
318 auto service = GetService();
319 return service ? service->CastAudio(token, descriptors) : ERR_SERVICE_NOT_EXIST;
320 }
321
CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)322 int32_t AVSessionManagerImpl::CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)
323 {
324 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::CastAudioForAll");
325 CHECK_AND_RETURN_RET_LOG(descriptors.size() > 0, ERR_INVALID_PARAM, "devices size is zero");
326 auto service = GetService();
327 return service ? service->CastAudioForAll(descriptors) : ERR_SERVICE_NOT_EXIST;
328 }
329
Close(void)330 int32_t AVSessionManagerImpl::Close(void)
331 {
332 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::Close");
333 int32_t ret = ERR_SERVICE_NOT_EXIST;
334 auto service = GetService();
335 if (service) {
336 sptr<IAVSessionService> serviceBase = service;
337 serviceBase->AsObject()->RemoveDeathRecipient(serviceDeathRecipient_);
338 serviceDeathRecipient_ = nullptr;
339 ret = service->Close();
340 }
341 SLOGI("manager impl close with listener clear");
342 listenerMapByUserId_.clear();
343 return ret;
344 }
345
346 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
StartCastDiscovery( const int32_t castDeviceCapability, std::vector<std::string> drmSchemes)347 int32_t AVSessionManagerImpl::StartCastDiscovery(
348 const int32_t castDeviceCapability, std::vector<std::string> drmSchemes)
349 {
350 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::StartCastDiscovery");
351 auto service = GetService();
352 return service ? service->StartCastDiscovery(castDeviceCapability, drmSchemes) : ERR_SERVICE_NOT_EXIST;
353 }
354
StopCastDiscovery()355 int32_t AVSessionManagerImpl::StopCastDiscovery()
356 {
357 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::StopCastDiscovery");
358 auto service = GetService();
359 return service ? service->StopCastDiscovery() : ERR_SERVICE_NOT_EXIST;
360 }
361
SetDiscoverable(const bool enable)362 int32_t AVSessionManagerImpl::SetDiscoverable(const bool enable)
363 {
364 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::SetDiscoverable");
365 auto service = GetService();
366 return service ? service->SetDiscoverable(enable) : ERR_SERVICE_NOT_EXIST;
367 }
368
StartDeviceLogging(int32_t fd, uint32_t maxSize)369 int32_t AVSessionManagerImpl::StartDeviceLogging(int32_t fd, uint32_t maxSize)
370 {
371 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::StartDeviceLogging");
372 auto service = GetService();
373 return service ? service->StartDeviceLogging(fd, maxSize) : ERR_SERVICE_NOT_EXIST;
374 }
375
StopDeviceLogging()376 int32_t AVSessionManagerImpl::StopDeviceLogging()
377 {
378 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::StopDeviceLogging");
379 auto service = GetService();
380 return service ? service->StopDeviceLogging() : ERR_SERVICE_NOT_EXIST;
381 }
382
StartCast(const SessionToken& sessionToken, const OutputDeviceInfo& outputDeviceInfo)383 int32_t AVSessionManagerImpl::StartCast(const SessionToken& sessionToken, const OutputDeviceInfo& outputDeviceInfo)
384 {
385 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::StartCast");
386 auto service = GetService();
387 return service ? service->StartCast(sessionToken, outputDeviceInfo) : ERR_SERVICE_NOT_EXIST;
388 }
389
StopCast(const SessionToken& sessionToken)390 int32_t AVSessionManagerImpl::StopCast(const SessionToken& sessionToken)
391 {
392 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::StopCast");
393 auto service = GetService();
394 return service ? service->StopCast(sessionToken) : ERR_SERVICE_NOT_EXIST;
395 }
396 #endif
397
RegisterClientDeathObserver()398 void AVSessionManagerImpl::RegisterClientDeathObserver()
399 {
400 clientDeath_ = new(std::nothrow) ClientDeathStub();
401 if (clientDeath_ == nullptr) {
402 SLOGE("malloc failed");
403 HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "MALLOC_FAILED",
404 "ERROR_INFO", "avsession manager impl register client death observer malloc failed");
405 return;
406 }
407
408 if (service_->RegisterClientDeathObserver(clientDeath_) != AVSESSION_SUCCESS) {
409 SLOGE("register failed");
410 HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "REGISTER_FAILED",
411 "ERROR_INFO", "avsession manager impl register client death observer register failed");
412 return;
413 }
414 SLOGI("success");
415 }
416
ServiceDeathRecipient(const std::function<void()>& callback)417 ServiceDeathRecipient::ServiceDeathRecipient(const std::function<void()>& callback)
418 : callback_(callback)
419 {
420 SLOGD("construct");
421 }
422
OnRemoteDied(const wptr<IRemoteObject>& object)423 void ServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object)
424 {
425 if (callback_) {
426 callback_();
427 }
428 }
429 } // namespace OHOS::AVSession
430