• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_AVROUTER_H
17 #define OHOS_AVROUTER_H
18 
19 #include "avsession_descriptor.h"
20 #include "avsession_info.h"
21 #include "i_avsession_service_listener.h"
22 
23 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
24 #include "i_avcast_controller_proxy.h"
25 #endif
26 
27 /**
28  * @brief Router is a part related to cast media
29  * @since 10
30  */
31 namespace OHOS::AVSession {
32 class AVRouter {
33 public:
34     /**
35      * Get AVRouter instance.
36      *
37      * @return AVRouter instance.
38      * @since 10
39     */
40     static AVRouter& GetInstance();
41 
42     /**
43      * Get AVRouter instance.
44      * @param { AVSessionService* } servicePtr - The pointer of avsession service.
45      * @return {int32_t} Returns whether init successfully.
46      * @since 10
47     */
48     virtual int32_t Init(IAVSessionServiceListener *servicePtr) = 0;
49 
50     /**
51      * Release AVRouter instance.
52      *
53      * @since 10
54     */
55     virtual bool Release() = 0;
56 
57 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
58     /**
59      * Transmission fd
60      *
61      * @param fd file descriptor
62      * @param maxSize file max size
63      * @return Returns whether the fd was transport successfully
64      * @since 13
65     */
66     virtual int32_t StartDeviceLogging(int32_t fd, uint32_t maxSize) = 0;
67 
68     /**
69      * Stop transmission fd
70      *
71      * @return Returns whether stop transport successfully
72      * @since 13
73     */
74     virtual int32_t StopDeviceLogging() = 0;
75 
76     /**
77      * @brief Starting to discover devices.
78      *
79      * @param { int32_t } castDeviceCapability - The type of device want to discover.
80      * @return { int32_t } Whether the device discovery operation was successful.
81      * @since 10
82     */
83     virtual int32_t StartCastDiscovery(int32_t castDeviceCapability, std::vector<std::string> drmSchemes) = 0;
84 
85     /**
86      * @brief Stop Discovering Devices
87      *
88      * @return { int32_t } Whether the stop operation was successful
89      * @since 10
90     */
91     virtual int32_t StopCastDiscovery() = 0;
92 
93     /**
94      * @brief Is stop Discovering Devices
95      *
96      * @return { bool } Whether execute StopCastDiscovery func
97      * @since 10
98     */
99     virtual bool IsStopCastDiscovery(pid_t pid) = 0;
100 
101     /**
102      * @brief Used on the Sink end to set whether it can be discovered or not.
103      *
104      * @param { const bool } enable - whether the sink device can be discovered or not.
105      * @return { int32_t } Whether the operation was successful
106      * @since 10
107     */
108     virtual int32_t SetDiscoverable(const bool enable) = 0;
109 
110     /**
111      * @brief Notify Router that the device has been discovered (device is available).
112      *
113      * @param { OutputDeviceInfo } castOutputDeviceInfo - Discovered device infos.
114      * @return { int32_t } Whether the notify operation was successful.
115      * @since 10
116     */
117     virtual int32_t OnDeviceAvailable(OutputDeviceInfo& castOutputDeviceInfo) = 0;
118 
119     /**
120      * @brief Listen for the event of device logging.
121      *
122      * @param { DeviceLogEventCode } eventId - Event ID.
123      * @param { int64_t } int64_t - Param.
124      * @since 13
125     */
126     virtual int32_t OnDeviceLogEvent(const DeviceLogEventCode eventId, const int64_t param) = 0;
127 
128     /**
129      * @brief Notify Router that the device is offline.
130      *
131      * @param { std::string& } deviceId - Offlined device ID.
132      * @return { int32_t } Whether the notify operation was successful.
133      * @since 10
134     */
135     virtual int32_t OnDeviceOffline(const std::string& deviceId) = 0;
136 
137     /**
138      * @brief Listen for the event of device changed.
139      *
140      * @param { DeviceState& } device state info.
141      * @return { int32_t } Whether the device state changed was successful.
142      * @since 20
143     */
144     virtual int32_t OnDeviceStateChange(const DeviceState& deviceState) = 0;
145 
146     /**
147      * @brief Release current cast session.
148      *
149      * @since 10
150     */
151     virtual void ReleaseCurrentCastSession() = 0;
152 
153     /**
154      * @brief Notify Router that the cast session has created.
155      *
156      * @param { int32_t } castId - Cast id for AVRouter's control.
157      * @return { int32_t } Whether the notify operation was successful.
158      * @since 10
159     */
160     virtual int32_t OnCastSessionCreated(const int32_t castId) = 0;
161 
162     /**
163      * @brief Notify Router that the the cast engine servie has died.
164      *
165      * @param { int32_t } providerId - Provider ID corresponding to cast engine service.
166      * @return { int32_t } Whether the notify operation was successful.
167      * @since 10
168     */
169     virtual int32_t OnCastServerDied(int32_t providerId) = 0;
170 
171     /**
172      * @brief Get the cast controller specified by castHandle.
173      *
174      * @param { const int64_t } castHandle - castHandle corresponding to cast engine session.
175      * @return { std::shared_ptr<IAVCastControllerProxy> } Obtained cast controller.
176      * @since 10
177     */
178     virtual std::shared_ptr<IAVCastControllerProxy> GetRemoteController(const int64_t castHandle) = 0;
179 
180     /**
181      * @brief Start cast process.
182      *
183      * @param { OutputDeviceInfo } outputDeviceInfo - Output device ready for use.
184      * @param { std::pair<std::string, std::string>& } serviceNameStatePair - serviceName state.
185      * @param { std::string } sessionId - avsession id.
186      * @return { int64_t } ID returned after successful start of cast.
187      * @since 10
188     */
189     virtual int64_t StartCast(const OutputDeviceInfo& outputDeviceInfo,
190         std::pair<std::string, std::string>& serviceNameStatePair, std::string sessionId) = 0;
191 
192     /**
193      * @brief Notify CastEngine to add (connect) remote devices.
194      *
195      * @param { int32_t } castId - Find the corresponding provider through this ID.
196      * @param { OutputDeviceInfo } outputDeviceInfo - Devices to be connected.
197      * @param { uint32_t } spid - app id for pulling client.
198      * @return { int32_t } Whether the operation was successful.
199      * @since 10
200     */
201     virtual int32_t AddDevice(const int32_t castId, const OutputDeviceInfo& outputDeviceInfo,
202         uint32_t spid) = 0;
203 
204     /**
205      * @brief Stop cast process.
206      *
207      * @param { const int64_t } castHandle - The ID corresponding to the provider that needs to be stopped.
208      * @param { bool } continuePlay - whether continue play when stop cast.
209      * @return { int32_t } Whether the operation was successful.
210      * @since 10
211     */
212     virtual int32_t StopCast(const int64_t castHandle, bool continuePlay = false) = 0;
213 
214     /**
215      * @brief Stop cast session process.
216      *
217      * @param { const int64_t } castHandle - The ID corresponding to the provider that needs to be stopped.
218      * @return { int32_t } Whether the operation was successful.
219      * @since 10
220     */
221     virtual int32_t StopCastSession(const int64_t castHandle) = 0;
222 
223     /**
224      * @brief Listen for AVRouter Callback event.
225      *
226      * @param { int64_t } castHandleconst - The ID corresponding to the provider.
227      * @param { std::shared_ptr<IAVRouterListener> } callback - Callback function.
228      * @param { std::string } sessionId - avsession id.
229      * @param { DeviceInfo } deviceInfo The device info.
230      * @return { int32_t } Whether the operation was successful.
231      * @since 10
232     */
233     virtual int32_t RegisterCallback(int64_t castHandleconst,
234         std::shared_ptr<IAVRouterListener> callback, std::string sessionId, DeviceInfo deviceInfo) = 0;
235 
236     /**
237      * @brief Cancel listening for AVRouter Callback event.
238      *
239      * @param { int64_t } castHandleconst - The ID corresponding to the provider.
240      * @param { std::shared_ptr<IAVRouterListener> } callback - Callback function.
241      * @param { std::string } sessionId - avsession id.
242      * @return { int32_t } Whether the operation was successful.
243      * @since 10
244     */
245     virtual int32_t UnRegisterCallback(int64_t castHandleconst,
246         std::shared_ptr<IAVRouterListener> callback, std::string sessionId) = 0;
247 
248     /**
249      * @brief set allconnect state.
250      *
251      * @param { int64_t } castHandle const - The ID corresponding to the provider.
252      * @param { DeviceInfo } cast deviceinfo - The deviceinfo to the castprovider.
253      * @return { int32_t } Whether the operation was successful.
254      * @since 11
255     */
256     virtual int32_t SetServiceAllConnectState(int64_t castHandle, DeviceInfo deviceInfo) = 0;
257 
258     /**
259      * @brief get remote networkId.
260      *
261      * @param { int64_t } castHandle const - The ID corresponding to the castprovider.
262      * @param { string } cast deviceId - The deviceId give cast+ to get remote networkId.
263      * @param { string } cast networkId - The networkId to transmit remote networkId.
264      * @return { int32_t } Whether the operation was successful.
265      * @since 11
266     */
267     virtual int32_t GetRemoteNetWorkId(int64_t castHandle, std::string deviceId, std::string &networkId) = 0;
268 
269     /**
270      * @brief get remote DrmCapabilities.
271      *
272      * @param { int64_t } castHandle const - The ID corresponding to the castprovider.
273      * @param { string } cast deviceId - The deviceId give cast+ to get remote DrmCapabilities.
274      * @param { string } cast DrmCapabilities - The DrmCapabilities to transmit remote DrmCapabilities.
275      * @return { int32_t } Whether the operation was successful.
276      * @since 11
277     */
278     virtual int32_t GetRemoteDrmCapabilities(int64_t castHandle, std::string deviceId,
279         std::vector<std::string> &drmCapabilities) = 0;
280 
281     /**
282      * @brief get mirror castHandle.
283      *
284      * @return { int64_t } mirror castHandle.
285      * @since 13
286     */
287     virtual int64_t GetMirrorCastHandle() = 0;
288 
289     /**
290      * @brief Listen to the change of cast state change.
291      *
292      * @param castState The cast state of device info.
293      * @param deviceInfo The device info.
294      * @param isNeedRemove is need remove cast device
295      * @since 13
296     */
297     void OnCastStateChange(int32_t castState, DeviceInfo deviceInfo, bool isNeedRemove);
298 
299     /**
300      * @brief Listen to the change of cast event.
301      *
302      * @param errorCode The error code of cast event.
303      * @param errorMsg The error message of cast event.
304      * @since 13
305     */
306     void OnCastEventRecv(int32_t errorCode, std::string& errorMsg);
307 
308     /**
309      * @brief disconnect other session.
310      *
311      * @param { std::string } sessionId - avsession id.
312      * @param { DeviceInfo } deviceinfo - The deviceinfo to the castprovider.
313      * @since 13
314     */
315     virtual void DisconnectOtherSession(std::string sessionId, DeviceInfo deviceInfo) = 0;
316 
317     /**
318      * @brief check is in mirror to stream state.
319      *
320      * @return { bool } is in mirror to stream state.
321      * @since 20
322     */
323     virtual bool IsInMirrorToStreamState() = 0;
324 
325     /**
326      * @brief check is in stream state.
327      *
328      * @return { bool } is in stream state.
329      * @since 20
330     */
331     virtual bool IsRemoteCasting() = 0;
332 
333 struct CastHandleInfo {
334     OutputDeviceInfo outputDeviceInfo_;
335     std::string sessionId_ = "-1";
336     std::shared_ptr<IAVCastControllerProxy> avCastControllerProxy_ = nullptr;
337     std::shared_ptr<IAVRouterListener> avRouterListener_ = nullptr;
338 };
339 #endif
340 };
341 } // namespace OHOS::AVSession
342 #endif // OHOS_AVROUTER_H
343