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_H
17 #define OHOS_AVSESSION_MANAGER_H
18 
19 #include <functional>
20 #include <string>
21 #include <memory>
22 
23 #include "audio_system_manager.h"
24 #include "av_session.h"
25 #include "avsession_controller.h"
26 #include "avsession_info.h"
27 #include "key_event.h"
28 #include "avqueue_info.h"
29 
30 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
31 #include "avcast_controller.h"
32 #endif
33 
34 namespace OHOS::AVSession {
35 class AVSessionManager {
36 public:
37     /**
38      * Get AVSessionManager instance.
39      *
40      * @return AVSessionManager instance.
41      * @since 7
42     */
43     static AVSessionManager& GetInstance();
44 
45     /**
46      * Create Session Object.
47      *
48      * @param tag Custom name of the session
49      * @param type Session type
50      * @param elementName element Name
51      * @return Returns Session Object
52      * @since 7
53     */
54     virtual std::shared_ptr<AVSession> CreateSession(const std::string& tag, int32_t type,
55                                                      const AppExecFwk::ElementName& elementName) = 0;
56 
57     /**
58      * Create Session Object.
59      *
60      * @param tag Custom name of the session
61      * @param type Session type
62      * @param elementName element Name
63      * @param session Created session {@link AVSession}
64      * @return Returns result of creating session
65      * @since 12
66     */
67     virtual int32_t CreateSession(const std::string& tag, int32_t type, const AppExecFwk::ElementName& elementName,
68                                   std::shared_ptr<AVSession>& session) = 0;
69     /**
70      * Send the key command to get the descriptor of all sessions.
71      *
72      * @param descriptors Get relevant descriptions of all sessions
73      * @return Whether the relevant descriptions of all sessions are obtained successfully
74      * @since 9
75     */
76     virtual int32_t GetAllSessionDescriptors(std::vector<AVSessionDescriptor>& descriptors) = 0;
77 
78     /**
79      * Send the key command to get the descriptor of activated sessions.
80      *
81      * @param activatedSessions Get relevant descriptions of activated sessions
82      * @return Returns whether the relevant descriptions of activated sessions are obtained successfully
83      * @since 9
84     */
85     virtual int32_t GetActivatedSessionDescriptors(std::vector<AVSessionDescriptor>& activatedSessions) = 0;
86 
87     /**
88      * Get SessionDescriptors By SessionId.
89      *
90      * @param sessionId current session id.
91      * @param descriptor obtain SessionDescriptors {@link AVSessionDescriptor}.
92      * @return Returns whether to obtain SessionDescriptors successfully.
93      * @since 9
94     */
95     virtual int32_t GetSessionDescriptorsBySessionId(const std::string& sessionId, AVSessionDescriptor& descriptor) = 0;
96 
97     /**
98      * Get historical session Descriptors.
99      *
100      * @param maxSize data write maxSize.
101      * @param descriptors obtain SessionDescriptors {@link AVSessionDescriptor}.
102      * @return Returns whether to obtain SessionDescriptors successfully.
103      * @since 9
104     */
105     virtual int32_t GetHistoricalSessionDescriptors(int32_t maxSize, std::vector<AVSessionDescriptor>& descriptors) = 0;
106 
107     /**
108      * Get historical AVQueueInfo.
109      *
110      * @param maxSize data write maxSize.
111      * @param maxAppSize max app count.
112      * @param avQueueInfo obtain history play avqueue {@link AVQueueInfo}.
113      * @return Returns whether to obtain AVQueueInfo successfully.
114      * @since 11
115     */
116     virtual int32_t GetHistoricalAVQueueInfos(int32_t maxSize, int32_t maxAppSize,
117         std::vector<AVQueueInfo>& avQueueInfo) = 0;
118 
119     /**
120      * Create a session controller based on the session ID.
121      *
122      * @param sessionId Current session id.
123      * @param controller Session controller{@link AVSessionController}
124      * @return Whether the session controller was successfully created
125      * @since 9
126     */
127     virtual int32_t CreateController(const std::string& sessionId,
128         std::shared_ptr<AVSessionController>& controller) = 0;
129 
130     /**
131      * @brief Listen for sessionListener callback event for current user.
132      *
133      * @param listener Listen for sessionListener Callback event{@link SessionListener}.
134      * @return Whether to return successful Listener.
135      * @since 9
136     */
137     virtual int32_t RegisterSessionListener(const std::shared_ptr<SessionListener>& listener) = 0;
138 
139     /**
140      * @brief Listen for sessionListener callback event for all users.
141      *
142      * @param listener Listen for sessionListener Callback event{@link SessionListener}.
143      * @return Whether to return successful Listener.
144      * @since 9
145     */
146     virtual int32_t RegisterSessionListenerForAllUsers(const std::shared_ptr<SessionListener>& listener) = 0;
147 
148     /**
149      * @brief Listen for service death callback event.
150      *
151      * @param callback Listen for death callback event{@link DeathCallback}.
152      * @return Whether to return successful callback.
153      * @since 9
154     */
155     virtual int32_t RegisterServiceDeathCallback(const DeathCallback& callback) = 0;
156 
157     /**
158      * @brief Listen for service death callback event.
159      *
160      * @param callback Listen for death callback event{@link DeathCallback}.
161      * @return Whether to return successful callback.
162      * @since 9
163     */
164     virtual int32_t UnregisterServiceDeathCallback() = 0;
165 
166     /**
167      * Send system key command.
168      *
169      * @param keyEvent Key event {@linkKeyEvent}
170      * @return Returns whether the key event was successfully sent to the top session.
171      * @since 9
172     */
173     virtual int32_t SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent) = 0;
174 
175     /**
176      * Send system control command.
177      *
178      * @param command Relevant commands and parameters of AVSession {@AVControlCommand}
179      * @return Returns whether send control command to the top session.
180      * @since 9
181     */
182     virtual int32_t SendSystemControlCommand(const AVControlCommand& command) = 0;
183 
184     /**
185      * Cast the session to the specified device list.
186      *
187      * @param token Session token
188      * @param descriptors Media device list {@link AudioDeviceDescriptor}
189      * @return Returns whether the session was successfully cast to the specified device list
190      * @since 9
191     */
192     virtual int32_t CastAudio(const SessionToken& token,
193                               const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors) = 0;
194 
195     /**
196      * Cast all sessions to the specified device list.
197      *
198      * @param descriptors Media device list {@link AudioDeviceDescriptor}
199      * @return Returns whether the session was successfully cast to the specified device list
200      * @since 9
201     */
202     virtual int32_t CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors) = 0;
203 
204     /**
205      * Start media playback
206      *
207      * @param bundleName bundleName.
208      * @param assetId assetId of media for playback.
209      * @return Returns whether to Start media playback successfully.
210      * @since 11
211     */
212     virtual int32_t StartAVPlayback(const std::string& bundleName, const std::string& assetId) = 0;
213 
214     /**
215      * Close avsession manager, clear resources.
216      *
217      * @return Returns whether close the avsession manager.
218      */
219     virtual int32_t Close(void) = 0;
220 
221 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
222     virtual int32_t GetAVCastController(const std::string& sessionId,
223         std::shared_ptr<AVCastController>& castController) = 0;
224 
225     /**
226      * Discovery nearby devices that can be cast.
227      *
228      * @param castDeviceCapability Device capability to filter device list
229      * @return Returns whether the device was successfully found
230      * @since 10
231     */
232     virtual int32_t StartCastDiscovery(int32_t castDeviceCapability, std::vector<std::string> drmSchemes) = 0;
233 
234     /**
235      * Stop cast process.
236      *
237      * @return Returns
238      * @since 10
239     */
240     virtual int32_t StopCastDiscovery() = 0;
241 
242     /**
243      * Transmission fd
244      *
245      * @param fd file descriptor
246      * @param maxSize file max size
247      * @return Returns whether the fd was transport successfully
248      * @since 13
249     */
250     virtual int32_t StartDeviceLogging(int32_t fd, uint32_t maxSize) = 0;
251 
252     /**
253      * Stop transmission fd
254      *
255      * @return Returns whether stop transport successfully
256      * @since 13
257     */
258     virtual int32_t StopDeviceLogging() = 0;
259 
260     /**
261      * Start cast process.
262      *
263      * @param sessionToken Session token
264      * @param outputDeviceInfo outputdeviceInfo
265      * @return Returns whether the device was successfully found
266      * @since 10
267     */
268     virtual int32_t StartCast(const SessionToken& sessionToken, const OutputDeviceInfo& outputDeviceInfo) = 0;
269 
270     /**
271      * Start cast process.
272      *
273      * @param sessionToken Session token
274      * @param outputDeviceInfo outputdeviceInfo
275      * @return Returns whether the device was successfully found
276      * @since 10
277     */
278     virtual int32_t StopCast(const SessionToken& sessionToken) = 0;
279 
280     virtual int32_t SetDiscoverable(const bool enable) = 0;
281 #endif
282 };
283 } // namespace OHOS::AVSession
284 #endif // OHOS_AVSESSION_MANAGER_H
285