• 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 #ifndef OHOS_AVSESSION_MANAGER_IMPL_H
17 #define OHOS_AVSESSION_MANAGER_IMPL_H
18 
19 #include <string>
20 #include <memory>
21 #include <mutex>
22 
23 #include "iremote_object.h"
24 #include "iservice_registry.h"
25 #include "system_ability_status_change_stub.h"
26 #include "av_session.h"
27 #include "avsession_service_proxy.h"
28 #include "avsession_info.h"
29 #include "client_death_stub.h"
30 #include "isession_listener.h"
31 #include "avsession_manager.h"
32 #include "avsession_controller.h"
33 
34 namespace OHOS::AVSession {
35 class ServiceDeathRecipient;
36 class ServiceStatusListener;
37 class AVSessionManagerImpl : public AVSessionManager {
38 public:
39     AVSessionManagerImpl();
40 
41     static void DetachCallback();
42 
43     std::shared_ptr<AVSession> CreateSession(const std::string& tag, int32_t type,
44                                              const AppExecFwk::ElementName& elementName) override;
45 
46     int32_t CreateSession(const std::string& tag, int32_t type, const AppExecFwk::ElementName& elementName,
47                           std::shared_ptr<AVSession>& session) override;
48 
49     int32_t GetAllSessionDescriptors(std::vector<AVSessionDescriptor>& descriptors) override;
50 
51     int32_t CreateController(const std::string& sessionId, std::shared_ptr<AVSessionController>& controller) override;
52 
53 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
54     int32_t GetAVCastController(const std::string& sessionId,
55         std::shared_ptr<AVCastController>& castController) override;
56 #endif
57 
58     int32_t GetActivatedSessionDescriptors(std::vector<AVSessionDescriptor>& activatedSessions) override;
59 
60     int32_t GetSessionDescriptorsBySessionId(const std::string& sessionId, AVSessionDescriptor& descriptor) override;
61 
62     int32_t GetHistoricalSessionDescriptors(int32_t maxSize, std::vector<AVSessionDescriptor>& descriptors) override;
63 
64     int32_t GetHistoricalAVQueueInfos(int32_t maxSize, int32_t maxAppSize,
65         std::vector<AVQueueInfo>& avQueueInfos) override;
66 
67     int32_t RegisterSessionListener(const std::shared_ptr<SessionListener>& listener) override;
68 
69     int32_t RegisterSessionListenerForAllUsers(const std::shared_ptr<SessionListener>& listener) override;
70 
71     int32_t RegisterServiceDeathCallback(const DeathCallback& callback) override;
72 
73     int32_t UnregisterServiceDeathCallback() override;
74 
75     int32_t RegisterServiceStartCallback(const std::function<void()> serviceStartCallback) override;
76 
77     int32_t UnregisterServiceStartCallback() override;
78 
79     int32_t SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent) override;
80 
81     int32_t SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent, const AAFwk::Want &wantParam) override;
82 
83     int32_t SendSystemControlCommand(const AVControlCommand& command) override;
84 
85     int32_t CastAudio(const SessionToken& token,
86                       const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors) override;
87 
88     int32_t CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors) override;
89 
90     int32_t StartAVPlayback(const std::string& bundleName, const std::string& assetId) override;
91 
92     int32_t Close(void) override;
93 
94     int32_t GetDistributedSessionControllers(const DistributedSessionType& sessionType,
95         std::vector<std::shared_ptr<AVSessionController>>& sessionControllers) override;
96 
97 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
98     int32_t StartCastDiscovery(int32_t castDeviceCapability, std::vector<std::string> drmSchemes) override;
99 
100     int32_t StopCastDiscovery() override;
101 
102     int32_t SetDiscoverable(const bool enable) override;
103 
104     int32_t StartDeviceLogging(int32_t fd, uint32_t maxSize) override;
105 
106     int32_t StopDeviceLogging() override;
107 
108     int32_t StartCast(const SessionToken& sessionToken, const OutputDeviceInfo& outputDeviceInfo) override;
109 
110     int32_t StopCast(const SessionToken& sessionToken) override;
111 #endif
112 
113 private:
114     sptr<AVSessionServiceProxy> GetService();
115 
116     void RegisterServiceStateListener(sptr<ISystemAbilityManager> mgr);
117 
118     void OnServiceDie();
119 
120     void OnServiceStart();
121 
122     void RegisterClientDeathObserver();
123 
124     sptr<ISessionListener> GetSessionListenerClient(const std::shared_ptr<SessionListener>& listener);
125 
126     std::mutex lock_;
127     sptr<AVSessionServiceProxy> service_;
128     sptr<ServiceDeathRecipient> serviceDeathRecipient_;
129     std::map<int32_t, sptr<ISessionListener>> listenerMapByUserId_;
130     static sptr<ClientDeathStub> clientDeath_;
131     DeathCallback deathCallback_;
132     std::function<void()> serviceStartCallback_;
133     sptr<ServiceStatusListener> serviceListener_ = nullptr;
134     static constexpr int userIdForAllUsers_ = -1;
135     std::atomic<bool> isServiceDie = false;
136 #ifdef START_STOP_ON_DEMAND_ENABLE
137     const int32_t loadSystemAbilityWaitTimeOut_ = 30;
138 #endif
139 };
140 
141 class ServiceStatusListener : public SystemAbilityStatusChangeStub {
142 public:
143     explicit ServiceStatusListener(const std::function<void()>& callback);
144 
145     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
146     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
147 private:
148     std::function<void()> callback_;
149 };
150 
151 class ServiceDeathRecipient : public IRemoteObject::DeathRecipient {
152 public:
153     explicit ServiceDeathRecipient(const std::function<void()>& callback);
154 
155     void OnRemoteDied(const wptr<IRemoteObject>& object) override;
156 
157 private:
158     std::function<void()> callback_;
159 };
160 } // namespace OHOS::AVSession
161 #endif // OHOS_AVSESSION_MANAGER_IMPL_H
162