• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
CreateController(const std::string & sessionId,std::shared_ptr<AVSessionController> & controller)175 int32_t AVSessionManagerImpl::CreateController(const std::string& sessionId,
176     std::shared_ptr<AVSessionController>& controller)
177 {
178     AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::CreateController");
179     if (sessionId.empty()) {
180         SLOGE("sessionId is invalid");
181         return ERR_INVALID_PARAM;
182     }
183 
184     auto service = GetService();
185     return service ? service->CreateController(sessionId, controller) : ERR_SERVICE_NOT_EXIST;
186 }
187 
188 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
GetAVCastController(const std::string & sessionId,std::shared_ptr<AVCastController> & castController)189 int32_t AVSessionManagerImpl::GetAVCastController(const std::string& sessionId,
190     std::shared_ptr<AVCastController>& castController)
191 {
192     AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::GetAVCastController");
193     if (sessionId.empty()) {
194         SLOGE("sessionId is invalid");
195         return ERR_INVALID_PARAM;
196     }
197 
198     auto service = GetService();
199     return service ? service->GetAVCastController(sessionId, castController) : ERR_SERVICE_NOT_EXIST;
200 }
201 #endif
202 
GetSessionListenerClient(const std::shared_ptr<SessionListener> & listener)203 sptr<ISessionListener> AVSessionManagerImpl::GetSessionListenerClient(const std::shared_ptr<SessionListener>& listener)
204 {
205     if (listener == nullptr) {
206         SLOGE("listener recv is nullptr");
207         return nullptr;
208     }
209 
210     sptr<ISessionListener> listenerPtr = new(std::nothrow) SessionListenerClient(listener);
211     if (listenerPtr == nullptr) {
212         SLOGE("listener create is nullptr");
213         return nullptr;
214     }
215     return listenerPtr;
216 }
217 
RegisterSessionListener(const std::shared_ptr<SessionListener> & listener)218 int32_t AVSessionManagerImpl::RegisterSessionListener(const std::shared_ptr<SessionListener>& listener)
219 {
220     auto service = GetService();
221     if (service == nullptr) {
222         return ERR_SERVICE_NOT_EXIST;
223     }
224 
225     std::lock_guard<std::mutex> lockGuard(lock_);
226     sptr<ISessionListener> listenerPtr = GetSessionListenerClient(listener);
227     if (listenerPtr == nullptr) {
228         return ERR_INVALID_PARAM;
229     }
230 
231     int32_t ret = service->RegisterSessionListener(listenerPtr);
232     if (ret <= AVSESSION_SUCCESS) {
233         SLOGE("RegisterSessionListener fail with ret %{public}d", ret);
234         return ret;
235     }
236     SLOGI("RegisterSessionListener for user %{public}d", ret);
237     listenerMapByUserId_[ret] = listenerPtr;
238     return AVSESSION_SUCCESS;
239 }
240 
RegisterSessionListenerForAllUsers(const std::shared_ptr<SessionListener> & listener)241 int32_t AVSessionManagerImpl::RegisterSessionListenerForAllUsers(const std::shared_ptr<SessionListener>& listener)
242 {
243     SLOGI("RegisterSessionListenerForAllUsers in");
244     auto service = GetService();
245     if (service == nullptr) {
246         return ERR_SERVICE_NOT_EXIST;
247     }
248 
249     std::lock_guard<std::mutex> lockGuard(lock_);
250     sptr<ISessionListener> listenerPtr = GetSessionListenerClient(listener);
251     if (listenerPtr == nullptr) {
252         return ERR_INVALID_PARAM;
253     }
254 
255     auto ret = service->RegisterSessionListenerForAllUsers(listenerPtr);
256     if (ret != AVSESSION_SUCCESS) {
257         SLOGE("RegisterSessionListenerForAllUsers fail with ret %{public}d", ret);
258         return ret;
259     }
260     listenerMapByUserId_[userIdForAllUsers_] = listenerPtr;
261     return AVSESSION_SUCCESS;
262 }
263 
RegisterServiceDeathCallback(const DeathCallback & callback)264 int32_t AVSessionManagerImpl::RegisterServiceDeathCallback(const DeathCallback& callback)
265 {
266     deathCallback_ = callback;
267     return AVSESSION_SUCCESS;
268 }
269 
UnregisterServiceDeathCallback()270 int32_t AVSessionManagerImpl::UnregisterServiceDeathCallback()
271 {
272     deathCallback_ = nullptr;
273     return AVSESSION_SUCCESS;
274 }
275 
SendSystemAVKeyEvent(const MMI::KeyEvent & keyEvent)276 int32_t AVSessionManagerImpl::SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent)
277 {
278     AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::SendSystemAVKeyEvent");
279     if (!keyEvent.IsValid()) {
280         SLOGE("keyEvent is invalid");
281         return ERR_COMMAND_NOT_SUPPORT;
282     }
283 
284     auto service = GetService();
285     return service ? service->SendSystemAVKeyEvent(keyEvent) : ERR_SERVICE_NOT_EXIST;
286 }
287 
SendSystemControlCommand(const AVControlCommand & command)288 int32_t AVSessionManagerImpl::SendSystemControlCommand(const AVControlCommand& command)
289 {
290     AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::SendSystemControlCommand");
291     if (!command.IsValid()) {
292         SLOGE("command is invalid");
293         HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "INVALID_COMMAND", "CMD", command.GetCommand(),
294             "ERROR_CODE", ERR_INVALID_PARAM, "ERROR_INFO", "avsessionmanagerimpl command is invalid");
295         return ERR_COMMAND_NOT_SUPPORT;
296     }
297 
298     auto service = GetService();
299     if (service == nullptr) {
300         HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "GET_SERVICE_ERROR",
301             "ERROR_CODE", ERR_SERVICE_NOT_EXIST, "ERROR_INFO", "mgrimp sendsystemcontrolcommand get service error");
302         return ERR_SERVICE_NOT_EXIST;
303     }
304     return service->SendSystemControlCommand(command);
305 }
306 
CastAudio(const SessionToken & token,const std::vector<AudioStandard::AudioDeviceDescriptor> & descriptors)307 int32_t AVSessionManagerImpl::CastAudio(const SessionToken& token,
308                                         const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)
309 {
310     AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::CastAudio");
311     CHECK_AND_RETURN_RET_LOG(descriptors.size() > 0, ERR_INVALID_PARAM, "devices size is zero");
312     auto service = GetService();
313     return service ? service->CastAudio(token, descriptors) : ERR_SERVICE_NOT_EXIST;
314 }
315 
CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor> & descriptors)316 int32_t AVSessionManagerImpl::CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)
317 {
318     AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::CastAudioForAll");
319     CHECK_AND_RETURN_RET_LOG(descriptors.size() > 0, ERR_INVALID_PARAM, "devices size is zero");
320     auto service = GetService();
321     return service ? service->CastAudioForAll(descriptors) : ERR_SERVICE_NOT_EXIST;
322 }
323 
Close(void)324 int32_t AVSessionManagerImpl::Close(void)
325 {
326     AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::Close");
327     int32_t ret = ERR_SERVICE_NOT_EXIST;
328     auto service = GetService();
329     if (service) {
330         sptr<IAVSessionService> serviceBase = service;
331         serviceBase->AsObject()->RemoveDeathRecipient(serviceDeathRecipient_);
332         serviceDeathRecipient_ = nullptr;
333         ret = service->Close();
334     }
335     SLOGI("manager impl close with listener clear");
336     listenerMapByUserId_.clear();
337     return ret;
338 }
339 
340 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
StartCastDiscovery(const int32_t castDeviceCapability,std::vector<std::string> drmSchemes)341 int32_t AVSessionManagerImpl::StartCastDiscovery(
342     const int32_t castDeviceCapability, std::vector<std::string> drmSchemes)
343 {
344     AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::StartCastDiscovery");
345     auto service = GetService();
346     return service ? service->StartCastDiscovery(castDeviceCapability, drmSchemes) : ERR_SERVICE_NOT_EXIST;
347 }
348 
StopCastDiscovery()349 int32_t AVSessionManagerImpl::StopCastDiscovery()
350 {
351     AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::StopCastDiscovery");
352     auto service = GetService();
353     return service ? service->StopCastDiscovery() : ERR_SERVICE_NOT_EXIST;
354 }
355 
SetDiscoverable(const bool enable)356 int32_t AVSessionManagerImpl::SetDiscoverable(const bool enable)
357 {
358     AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::SetDiscoverable");
359     auto service = GetService();
360     return service ? service->SetDiscoverable(enable) : ERR_SERVICE_NOT_EXIST;
361 }
362 
StartDeviceLogging(int32_t fd,uint32_t maxSize)363 int32_t AVSessionManagerImpl::StartDeviceLogging(int32_t fd, uint32_t maxSize)
364 {
365     AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::StartDeviceLogging");
366     auto service = GetService();
367     return service ? service->StartDeviceLogging(fd, maxSize) : ERR_SERVICE_NOT_EXIST;
368 }
369 
StopDeviceLogging()370 int32_t AVSessionManagerImpl::StopDeviceLogging()
371 {
372     AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::StopDeviceLogging");
373     auto service = GetService();
374     return service ? service->StopDeviceLogging() : ERR_SERVICE_NOT_EXIST;
375 }
376 
StartCast(const SessionToken & sessionToken,const OutputDeviceInfo & outputDeviceInfo)377 int32_t AVSessionManagerImpl::StartCast(const SessionToken& sessionToken, const OutputDeviceInfo& outputDeviceInfo)
378 {
379     AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::StartCast");
380     auto service = GetService();
381     return service ? service->StartCast(sessionToken, outputDeviceInfo) : ERR_SERVICE_NOT_EXIST;
382 }
383 
StopCast(const SessionToken & sessionToken)384 int32_t AVSessionManagerImpl::StopCast(const SessionToken& sessionToken)
385 {
386     AVSESSION_TRACE_SYNC_START("AVSessionManagerImpl::StopCast");
387     auto service = GetService();
388     return service ? service->StopCast(sessionToken) : ERR_SERVICE_NOT_EXIST;
389 }
390 #endif
391 
RegisterClientDeathObserver()392 void AVSessionManagerImpl::RegisterClientDeathObserver()
393 {
394     clientDeath_ = new(std::nothrow) ClientDeathStub();
395     if (clientDeath_ == nullptr) {
396         SLOGE("malloc failed");
397         HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "MALLOC_FAILED",
398             "ERROR_INFO", "avsession manager impl register client death observer malloc failed");
399         return;
400     }
401 
402     if (service_->RegisterClientDeathObserver(clientDeath_) != AVSESSION_SUCCESS) {
403         SLOGE("register failed");
404         HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "REGISTER_FAILED",
405             "ERROR_INFO", "avsession manager impl register client death observer register failed");
406         return;
407     }
408     SLOGI("success");
409 }
410 
ServiceDeathRecipient(const std::function<void ()> & callback)411 ServiceDeathRecipient::ServiceDeathRecipient(const std::function<void()>& callback)
412     : callback_(callback)
413 {
414     SLOGD("construct");
415 }
416 
OnRemoteDied(const wptr<IRemoteObject> & object)417 void ServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object)
418 {
419     if (callback_) {
420         callback_();
421     }
422 }
423 } // namespace OHOS::AVSession
424