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 "ipc_skeleton.h"
18 #include "system_ability_definition.h"
19 #include "avsession_log.h"
20 #include "avsession_errors.h"
21 #include "avsession_event_handler.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 {
28
29 sptr<ClientDeathStub> AVSessionManagerImpl::clientDeath_ = nullptr;
30
AVSessionManagerImpl()31 AVSessionManagerImpl::AVSessionManagerImpl()
32 {
33 SLOGD("constructor");
34 }
35
DetachCallback()36 extern "C" __attribute__((destructor)) void AVSessionManagerImpl::DetachCallback()
37 {
38 SLOGI("DetachCallback success");
39 }
40
GetService()41 sptr<AVSessionServiceProxy> AVSessionManagerImpl::GetService()
42 {
43 SLOGI("enter GetService");
44 std::lock_guard<std::mutex> lockGuard(lock_);
45 if (service_) {
46 return service_;
47 }
48
49 auto mgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
50 if (mgr == nullptr) {
51 SLOGE("failed to get sa mgr");
52 return nullptr;
53 }
54 #ifndef START_STOP_ON_DEMAND_ENABLE
55 auto object = mgr->GetSystemAbility(AVSESSION_SERVICE_ID);
56 if (object == nullptr) {
57 SLOGE("failed to get service");
58 return nullptr;
59 }
60 #else
61 SLOGI("enter check SystemAbility");
62 auto object = mgr->CheckSystemAbility(AVSESSION_SERVICE_ID);
63 if (object == nullptr) {
64 SLOGI("check no SystemAbility");
65 object = mgr->LoadSystemAbility(AVSESSION_SERVICE_ID, loadSystemAbilityWaitTimeOut_);
66 if (object == nullptr) {
67 SLOGE("failed to load SystemAbility");
68 return nullptr;
69 }
70 }
71 #endif
72 service_ = iface_cast<AVSessionServiceProxy>(object);
73 if (service_ != nullptr) {
74 serviceDeathRecipient_ = new(std::nothrow) ServiceDeathRecipient([this] { OnServiceDie(); });
75 if (serviceDeathRecipient_ == nullptr) {
76 SLOGE("register ServiceDeathRecipient failed");
77 return nullptr;
78 }
79
80 sptr<IAVSessionService> serviceBase = service_;
81 serviceBase->AsObject()->AddDeathRecipient(serviceDeathRecipient_);
82
83 SLOGD("get service success");
84 RegisterClientDeathObserver();
85 RegisterServiceStateListener(mgr);
86 }
87 return service_;
88 }
89
RegisterServiceStateListener(sptr<ISystemAbilityManager> mgr)90 void AVSessionManagerImpl::RegisterServiceStateListener(sptr<ISystemAbilityManager> mgr)
91 {
92 SLOGI("RegisterServiceStateListener enter");
93 if (serviceListener_ == nullptr && serviceStartCallback_) {
94 serviceListener_ = new(std::nothrow) ServiceStatusListener([this](bool isAddSystemAbility) {
95 OnServiceStateChange(isAddSystemAbility);
96 });
97 CHECK_AND_RETURN_LOG(serviceListener_ != nullptr, "serviceListener_ alloc failed");
98 auto ret = mgr->SubscribeSystemAbility(AVSESSION_SERVICE_ID, serviceListener_);
99 SLOGI("RegisterServiceStateListener ret=%{public}d", ret);
100 }
101 }
102
OnServiceStateChange(bool isAddSystemAbility)103 void AVSessionManagerImpl::OnServiceStateChange(bool isAddSystemAbility)
104 {
105 if (!serviceStartCallback_) {
106 SLOGI("OnServiceStateChange not register ServiceStartCallback");
107 return;
108 }
109 SLOGI("OnServiceStateChange enter isAddSystemAbility:%{public}d isServiceDie:%{public}d",
110 isAddSystemAbility, isServiceDie_.load());
111 if (isAddSystemAbility) {
112 if (isServiceDie_.load()) {
113 serviceStartCallback_();
114 isServiceDie_ = false;
115 }
116 } else {
117 {
118 std::lock_guard<std::mutex> lockGuard(lock_);
119 service_.clear();
120 isServiceDie_ = true;
121 }
122 }
123 }
124
OnServiceDie()125 void AVSessionManagerImpl::OnServiceDie()
126 {
127 SLOGI("OnServiceDie enter");
128 auto callback = deathCallback_;
129 {
130 std::lock_guard<std::mutex> lockGuard(lock_);
131 if (!serviceStartCallback_) {
132 SLOGI("OnServiceDie not register ServiceStartCallback");
133 service_.clear();
134 }
135 listenerMapByUserId_.clear();
136 deathCallback_ = nullptr;
137 }
138 if (callback) {
139 callback();
140 }
141 HISYSEVENT_RESET;
142 HISYSEVENT_UNREGISTER;
143 std::string cachePath(AVSessionUtils::GetCachePathName());
144 AVSessionUtils::DeleteCacheFiles(cachePath);
145 }
146
CreateSession(const std::string & tag,int32_t type,const AppExecFwk::ElementName & elementName)147 std::shared_ptr<AVSession> AVSessionManagerImpl::CreateSession(const std::string& tag, int32_t type,
148 const AppExecFwk::ElementName& elementName)
149 {
150 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::CreateSession");
151 if (tag.empty() || elementName.GetBundleName().empty() || elementName.GetAbilityName().empty()) {
152 SLOGE("param is invalid");
153 return nullptr;
154 }
155 if (type != AVSession::SESSION_TYPE_AUDIO && type != AVSession::SESSION_TYPE_VIDEO
156 && type != AVSession::SESSION_TYPE_VOICE_CALL && type != AVSession::SESSION_TYPE_VIDEO_CALL) {
157 SLOGE("type is invalid");
158 return nullptr;
159 }
160
161 auto service = GetService();
162 return service ? service->CreateSession(tag, type, elementName) : nullptr;
163 }
164
CreateSession(const std::string & tag,int32_t type,const AppExecFwk::ElementName & elementName,std::shared_ptr<AVSession> & session)165 int32_t AVSessionManagerImpl::CreateSession(const std::string& tag, int32_t type,
166 const AppExecFwk::ElementName& elementName,
167 std::shared_ptr<AVSession>& session)
168 {
169 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::CreateSession with ret");
170 if (tag.empty() || elementName.GetBundleName().empty() || elementName.GetAbilityName().empty()) {
171 SLOGE("param is invalid");
172 return ERR_INVALID_PARAM;
173 }
174 if (type != AVSession::SESSION_TYPE_AUDIO && type != AVSession::SESSION_TYPE_VIDEO
175 && type != AVSession::SESSION_TYPE_VOICE_CALL && type != AVSession::SESSION_TYPE_VIDEO_CALL) {
176 SLOGE("type is invalid");
177 return ERR_INVALID_PARAM;
178 }
179
180 auto service = GetService();
181 return service ? service->CreateSession(tag, type, elementName, session) : ERR_SERVICE_NOT_EXIST;
182 }
183
CreateSessionWithExtra(const std::string & tag,int32_t type,const std::string & extraInfo,const AppExecFwk::ElementName & elementName,std::shared_ptr<AVSession> & session)184 int32_t AVSessionManagerImpl::CreateSessionWithExtra(const std::string& tag, int32_t type, const std::string& extraInfo,
185 const AppExecFwk::ElementName& elementName,
186 std::shared_ptr<AVSession>& session)
187 {
188 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::CreateSessionWithExtra with ret");
189 if (tag.empty() || elementName.GetBundleName().empty() || elementName.GetAbilityName().empty() ||
190 extraInfo.empty()) {
191 SLOGE("param is invalid");
192 return ERR_INVALID_PARAM;
193 }
194 if (type != AVSession::SESSION_TYPE_AUDIO && type != AVSession::SESSION_TYPE_VIDEO
195 && type != AVSession::SESSION_TYPE_VOICE_CALL && type != AVSession::SESSION_TYPE_VIDEO_CALL) {
196 SLOGE("type is invalid");
197 return ERR_INVALID_PARAM;
198 }
199
200 auto service = GetService();
201 return service ? service->CreateSession(tag, type, elementName, session) : ERR_SERVICE_NOT_EXIST;
202 }
203
GetAllSessionDescriptors(std::vector<AVSessionDescriptor> & descriptors)204 int32_t AVSessionManagerImpl::GetAllSessionDescriptors(std::vector<AVSessionDescriptor>& descriptors)
205 {
206 auto service = GetService();
207 return service ? service->GetAllSessionDescriptors(descriptors) : ERR_SERVICE_NOT_EXIST;
208 }
209
GetActivatedSessionDescriptors(std::vector<AVSessionDescriptor> & activatedSessions)210 int32_t AVSessionManagerImpl::GetActivatedSessionDescriptors(std::vector<AVSessionDescriptor>& activatedSessions)
211 {
212 std::vector<AVSessionDescriptor> descriptors;
213 int32_t ret = GetAllSessionDescriptors(descriptors);
214 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "GetAllSessionDescriptors failed");
215
216 for (const auto& descriptor : descriptors) {
217 if (descriptor.isActive_) {
218 activatedSessions.push_back(descriptor);
219 }
220 }
221 return ret;
222 }
223
GetSessionDescriptorsBySessionId(const std::string & sessionId,AVSessionDescriptor & descriptor)224 int32_t AVSessionManagerImpl::GetSessionDescriptorsBySessionId(const std::string& sessionId,
225 AVSessionDescriptor& descriptor)
226 {
227 if (sessionId.empty()) {
228 SLOGE("sessionId is invalid");
229 return ERR_INVALID_PARAM;
230 }
231
232 auto service = GetService();
233 return service ? service->GetSessionDescriptorsBySessionId(sessionId, descriptor) : ERR_SERVICE_NOT_EXIST;
234 }
235
GetHistoricalSessionDescriptors(int32_t maxSize,std::vector<AVSessionDescriptor> & descriptors)236 int32_t AVSessionManagerImpl::GetHistoricalSessionDescriptors(int32_t maxSize,
237 std::vector<AVSessionDescriptor>& descriptors)
238 {
239 auto service = GetService();
240 return service ? service->GetHistoricalSessionDescriptors(maxSize, descriptors) : ERR_SERVICE_NOT_EXIST;
241 }
242
GetHistoricalAVQueueInfos(int32_t maxSize,int32_t maxAppSize,std::vector<AVQueueInfo> & avQueueInfos)243 int32_t AVSessionManagerImpl::GetHistoricalAVQueueInfos(int32_t maxSize, int32_t maxAppSize,
244 std::vector<AVQueueInfo>& avQueueInfos)
245 {
246 auto service = GetService();
247 return service ? service->GetHistoricalAVQueueInfos(maxSize, maxAppSize, avQueueInfos) : ERR_SERVICE_NOT_EXIST;
248 }
249
StartAVPlayback(const std::string & bundleName,const std::string & assetId)250 int32_t AVSessionManagerImpl::StartAVPlayback(const std::string& bundleName, const std::string& assetId)
251 {
252 auto service = GetService();
253 return service ? service->StartAVPlayback(bundleName, assetId) : ERR_SERVICE_NOT_EXIST;
254 }
255
GetDistributedSessionControllers(const DistributedSessionType & sessionType,std::vector<std::shared_ptr<AVSessionController>> & sessionControllers)256 int32_t AVSessionManagerImpl::GetDistributedSessionControllers(const DistributedSessionType& sessionType,
257 std::vector<std::shared_ptr<AVSessionController>>& sessionControllers)
258 {
259 auto service = GetService();
260 return service ? service->GetDistributedSessionControllers(sessionType, sessionControllers) : ERR_SERVICE_NOT_EXIST;
261 }
262
CreateController(const std::string & sessionId,std::shared_ptr<AVSessionController> & controller)263 int32_t AVSessionManagerImpl::CreateController(const std::string& sessionId,
264 std::shared_ptr<AVSessionController>& controller)
265 {
266 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::CreateController");
267 if (sessionId.empty()) {
268 SLOGE("sessionId is invalid");
269 return ERR_INVALID_PARAM;
270 }
271
272 auto service = GetService();
273 return service ? service->CreateController(sessionId, controller) : ERR_SERVICE_NOT_EXIST;
274 }
275
276 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
GetAVCastController(const std::string & sessionId,std::shared_ptr<AVCastController> & castController)277 int32_t AVSessionManagerImpl::GetAVCastController(const std::string& sessionId,
278 std::shared_ptr<AVCastController>& castController)
279 {
280 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::GetAVCastController");
281 if (sessionId.empty()) {
282 SLOGE("sessionId is invalid");
283 return ERR_INVALID_PARAM;
284 }
285
286 auto service = GetService();
287 return service ? service->GetAVCastController(sessionId, castController) : ERR_SERVICE_NOT_EXIST;
288 }
289 #endif
290
GetSessionListenerClient(const std::shared_ptr<SessionListener> & listener)291 sptr<ISessionListener> AVSessionManagerImpl::GetSessionListenerClient(const std::shared_ptr<SessionListener>& listener)
292 {
293 if (listener == nullptr) {
294 SLOGE("listener recv is nullptr");
295 return nullptr;
296 }
297
298 sptr<ISessionListener> listenerPtr = new(std::nothrow) SessionListenerClient(listener);
299 if (listenerPtr == nullptr) {
300 SLOGE("listener create is nullptr");
301 return nullptr;
302 }
303 return listenerPtr;
304 }
305
RegisterSessionListener(const std::shared_ptr<SessionListener> & listener)306 int32_t AVSessionManagerImpl::RegisterSessionListener(const std::shared_ptr<SessionListener>& listener)
307 {
308 auto service = GetService();
309 if (service == nullptr) {
310 return ERR_SERVICE_NOT_EXIST;
311 }
312
313 std::lock_guard<std::mutex> lockGuard(lock_);
314 sptr<ISessionListener> listenerPtr = GetSessionListenerClient(listener);
315 if (listenerPtr == nullptr) {
316 return ERR_INVALID_PARAM;
317 }
318
319 int32_t ret = service->RegisterSessionListener(listenerPtr);
320 if (ret <= AVSESSION_SUCCESS) {
321 SLOGE("RegisterSessionListener fail with ret %{public}d", ret);
322 return ret;
323 }
324 SLOGI("RegisterSessionListener for user %{public}d", ret);
325 listenerMapByUserId_[ret] = listenerPtr;
326 return AVSESSION_SUCCESS;
327 }
328
RegisterSessionListenerForAllUsers(const std::shared_ptr<SessionListener> & listener)329 int32_t AVSessionManagerImpl::RegisterSessionListenerForAllUsers(const std::shared_ptr<SessionListener>& listener)
330 {
331 SLOGI("RegisterSessionListenerForAllUsers in");
332 auto service = GetService();
333 if (service == nullptr) {
334 return ERR_SERVICE_NOT_EXIST;
335 }
336
337 std::lock_guard<std::mutex> lockGuard(lock_);
338 sptr<ISessionListener> listenerPtr = GetSessionListenerClient(listener);
339 if (listenerPtr == nullptr) {
340 return ERR_INVALID_PARAM;
341 }
342
343 auto ret = service->RegisterSessionListenerForAllUsers(listenerPtr);
344 if (ret != AVSESSION_SUCCESS) {
345 SLOGE("RegisterSessionListenerForAllUsers fail with ret %{public}d", ret);
346 return ret;
347 }
348 listenerMapByUserId_[userIdForAllUsers_] = listenerPtr;
349 return AVSESSION_SUCCESS;
350 }
351
RegisterServiceDeathCallback(const DeathCallback & callback)352 int32_t AVSessionManagerImpl::RegisterServiceDeathCallback(const DeathCallback& callback)
353 {
354 deathCallback_ = callback;
355 return AVSESSION_SUCCESS;
356 }
357
UnregisterServiceDeathCallback()358 int32_t AVSessionManagerImpl::UnregisterServiceDeathCallback()
359 {
360 deathCallback_ = nullptr;
361 return AVSESSION_SUCCESS;
362 }
363
RegisterServiceStartCallback(const std::function<void ()> serviceStartCallback)364 int32_t AVSessionManagerImpl::RegisterServiceStartCallback(const std::function<void()> serviceStartCallback)
365 {
366 SLOGI("RegisterServiceStartCallback");
367 serviceStartCallback_ = serviceStartCallback;
368 return AVSESSION_SUCCESS;
369 }
370
UnregisterServiceStartCallback()371 int32_t AVSessionManagerImpl::UnregisterServiceStartCallback()
372 {
373 serviceStartCallback_= nullptr;
374 return AVSESSION_SUCCESS;
375 }
376
SendSystemAVKeyEvent(const MMI::KeyEvent & keyEvent)377 int32_t AVSessionManagerImpl::SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent)
378 {
379 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::SendSystemAVKeyEvent");
380 if (!keyEvent.IsValid()) {
381 SLOGE("keyEvent is invalid");
382 return ERR_COMMAND_NOT_SUPPORT;
383 }
384
385 auto service = GetService();
386 return service ? service->SendSystemAVKeyEvent(keyEvent) : ERR_SERVICE_NOT_EXIST;
387 }
388
SendSystemAVKeyEvent(const MMI::KeyEvent & keyEvent,const AAFwk::Want & wantParam)389 int32_t AVSessionManagerImpl::SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent, const AAFwk::Want &wantParam)
390 {
391 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::SendSystemAVKeyEvent");
392 if (!keyEvent.IsValid()) {
393 SLOGE("keyEvent is invalid");
394 return ERR_COMMAND_NOT_SUPPORT;
395 }
396
397 auto service = GetService();
398 return service ? service->SendSystemAVKeyEvent(keyEvent, wantParam) : ERR_SERVICE_NOT_EXIST;
399 }
400
SendSystemControlCommand(const AVControlCommand & command)401 int32_t AVSessionManagerImpl::SendSystemControlCommand(const AVControlCommand& command)
402 {
403 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::SendSystemControlCommand");
404 if (!command.IsValid()) {
405 SLOGE("command is invalid");
406 HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "INVALID_COMMAND", "CMD", command.GetCommand(),
407 "ERROR_CODE", ERR_INVALID_PARAM, "ERROR_INFO", "avsessionmanagerimpl command is invalid");
408 return ERR_COMMAND_NOT_SUPPORT;
409 }
410
411 auto service = GetService();
412 if (service == nullptr) {
413 HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "GET_SERVICE_ERROR",
414 "ERROR_CODE", ERR_SERVICE_NOT_EXIST, "ERROR_INFO", "mgrimp sendsystemcontrolcommand get service error");
415 return ERR_SERVICE_NOT_EXIST;
416 }
417 return service->SendSystemControlCommand(command);
418 }
419
CastAudio(const SessionToken & token,const std::vector<AudioStandard::AudioDeviceDescriptor> & descriptors)420 int32_t AVSessionManagerImpl::CastAudio(const SessionToken& token,
421 const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)
422 {
423 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::CastAudio");
424 CHECK_AND_RETURN_RET_LOG(descriptors.size() > 0, ERR_INVALID_PARAM, "devices size is zero");
425 auto service = GetService();
426 return service ? service->CastAudio(token, descriptors) : ERR_SERVICE_NOT_EXIST;
427 }
428
CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor> & descriptors)429 int32_t AVSessionManagerImpl::CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)
430 {
431 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::CastAudioForAll");
432 CHECK_AND_RETURN_RET_LOG(descriptors.size() > 0, ERR_INVALID_PARAM, "devices size is zero");
433 auto service = GetService();
434 return service ? service->CastAudioForAll(descriptors) : ERR_SERVICE_NOT_EXIST;
435 }
436
Close(void)437 int32_t AVSessionManagerImpl::Close(void)
438 {
439 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::Close");
440 int32_t ret = ERR_SERVICE_NOT_EXIST;
441 auto service = GetService();
442 if (service) {
443 sptr<IAVSessionService> serviceBase = service;
444 serviceBase->AsObject()->RemoveDeathRecipient(serviceDeathRecipient_);
445 serviceDeathRecipient_ = nullptr;
446 ret = service->Close();
447 }
448 SLOGI("manager impl close with listener clear");
449 listenerMapByUserId_.clear();
450
451 AVSessionEventHandler::GetInstance().AVSessionRemoveHandler();
452 return ret;
453 }
454
455 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
StartCastDiscovery(const int32_t castDeviceCapability,std::vector<std::string> drmSchemes)456 int32_t AVSessionManagerImpl::StartCastDiscovery(
457 const int32_t castDeviceCapability, std::vector<std::string> drmSchemes)
458 {
459 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::StartCastDiscovery");
460 auto service = GetService();
461 return service ? service->StartCastDiscovery(castDeviceCapability, drmSchemes) : ERR_SERVICE_NOT_EXIST;
462 }
463
StopCastDiscovery()464 int32_t AVSessionManagerImpl::StopCastDiscovery()
465 {
466 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::StopCastDiscovery");
467 auto service = GetService();
468 return service ? service->StopCastDiscovery() : ERR_SERVICE_NOT_EXIST;
469 }
470
SetDiscoverable(const bool enable)471 int32_t AVSessionManagerImpl::SetDiscoverable(const bool enable)
472 {
473 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::SetDiscoverable");
474 auto service = GetService();
475 return service ? service->SetDiscoverable(enable) : ERR_SERVICE_NOT_EXIST;
476 }
477
StartDeviceLogging(int32_t fd,uint32_t maxSize)478 int32_t AVSessionManagerImpl::StartDeviceLogging(int32_t fd, uint32_t maxSize)
479 {
480 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::StartDeviceLogging");
481 auto service = GetService();
482 return service ? service->StartDeviceLogging(fd, maxSize) : ERR_SERVICE_NOT_EXIST;
483 }
484
StopDeviceLogging()485 int32_t AVSessionManagerImpl::StopDeviceLogging()
486 {
487 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::StopDeviceLogging");
488 auto service = GetService();
489 return service ? service->StopDeviceLogging() : ERR_SERVICE_NOT_EXIST;
490 }
491
StartCast(const SessionToken & sessionToken,const OutputDeviceInfo & outputDeviceInfo)492 int32_t AVSessionManagerImpl::StartCast(const SessionToken& sessionToken, const OutputDeviceInfo& outputDeviceInfo)
493 {
494 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::StartCast");
495 auto service = GetService();
496 return service ? service->StartCast(sessionToken, outputDeviceInfo) : ERR_SERVICE_NOT_EXIST;
497 }
498
StopCast(const SessionToken & sessionToken)499 int32_t AVSessionManagerImpl::StopCast(const SessionToken& sessionToken)
500 {
501 AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::StopCast");
502 auto service = GetService();
503 return service ? service->StopCast(sessionToken) : ERR_SERVICE_NOT_EXIST;
504 }
505 #endif
506
RegisterClientDeathObserver()507 void AVSessionManagerImpl::RegisterClientDeathObserver()
508 {
509 clientDeath_ = new(std::nothrow) ClientDeathStub();
510 if (clientDeath_ == nullptr) {
511 SLOGE("malloc failed");
512 HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "MALLOC_FAILED",
513 "ERROR_INFO", "avsession manager impl register client death observer malloc failed");
514 return;
515 }
516
517 if (service_->RegisterClientDeathObserver(clientDeath_) != AVSESSION_SUCCESS) {
518 SLOGE("register failed");
519 HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "REGISTER_FAILED",
520 "ERROR_INFO", "avsession manager impl register client death observer register failed");
521 return;
522 }
523 SLOGI("RegisterClientDeathObserver with ClientDeathStub success");
524 }
525
ServiceDeathRecipient(const std::function<void ()> & callback)526 ServiceDeathRecipient::ServiceDeathRecipient(const std::function<void()>& callback)
527 : callback_(callback)
528 {
529 SLOGD("construct");
530 }
531
OnRemoteDied(const wptr<IRemoteObject> & object)532 void ServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object)
533 {
534 if (callback_) {
535 callback_();
536 }
537 }
538
ServiceStatusListener(const std::function<void (bool)> & callback)539 ServiceStatusListener::ServiceStatusListener(const std::function<void(bool)>& callback)
540 : callback_(callback)
541 {
542 SLOGD("construct");
543 }
544
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)545 void ServiceStatusListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
546 {
547 SLOGI("OnAddSystemAbility systemAbilityId=%{public}d", systemAbilityId);
548 if (callback_) {
549 callback_(true);
550 }
551 }
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)552 void ServiceStatusListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
553 {
554 SLOGI("OnRemoveSystemAbility systemAbilityId=%{public}d", systemAbilityId);
555 if (callback_) {
556 callback_(false);
557 }
558 }
559 } // namespace OHOS::AVSession
560