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