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 Used on the Sink end to set whether it can be discovered or not.
95      *
96      * @param { const bool } enable - whether the sink device can be discovered or not.
97      * @return { int32_t } Whether the operation was successful
98      * @since 10
99     */
100     virtual int32_t SetDiscoverable(const bool enable) = 0;
101 
102     /**
103      * @brief Notify Router that the device has been discovered (device is available).
104      *
105      * @param { OutputDeviceInfo } castOutputDeviceInfo - Discovered device infos.
106      * @return { int32_t } Whether the notify operation was successful.
107      * @since 10
108     */
109     virtual int32_t OnDeviceAvailable(OutputDeviceInfo& castOutputDeviceInfo) = 0;
110 
111     /**
112      * @brief Listen for the event of device logging.
113      *
114      * @param { DeviceLogEventCode } eventId - Event ID.
115      * @param { int64_t } int64_t - Param.
116      * @since 13
117     */
118     virtual int32_t OnDeviceLogEvent(const DeviceLogEventCode eventId, const int64_t param) = 0;
119 
120     /**
121      * @brief Notify Router that the device is offline.
122      *
123      * @param { std::string& } deviceId - Offlined device ID.
124      * @return { int32_t } Whether the notify operation was successful.
125      * @since 10
126     */
127     virtual int32_t OnDeviceOffline(const std::string& deviceId) = 0;
128 
129     /**
130      * @brief Release current cast session.
131      *
132      * @since 10
133     */
134     virtual void ReleaseCurrentCastSession() = 0;
135 
136     /**
137      * @brief Notify Router that the cast session has created.
138      *
139      * @param { int32_t } castId - Cast id for AVRouter's control.
140      * @return { int32_t } Whether the notify operation was successful.
141      * @since 10
142     */
143     virtual int32_t OnCastSessionCreated(const int32_t castId) = 0;
144 
145     /**
146      * @brief Notify Router that the the cast engine servie has died.
147      *
148      * @param { int32_t } providerId - Provider ID corresponding to cast engine service.
149      * @return { int32_t } Whether the notify operation was successful.
150      * @since 10
151     */
152     virtual int32_t OnCastServerDied(int32_t providerId) = 0;
153 
154     /**
155      * @brief Get the cast controller specified by castHandle.
156      *
157      * @param { const int64_t } castHandle - castHandle corresponding to cast engine session.
158      * @return { std::shared_ptr<IAVCastControllerProxy> } Obtained cast controller.
159      * @since 10
160     */
161     virtual std::shared_ptr<IAVCastControllerProxy> GetRemoteController(const int64_t castHandle) = 0;
162 
163     /**
164      * @brief Start cast process.
165      *
166      * @param { OutputDeviceInfo } outputDeviceInfo - Output device ready for use.
167      * @param { std::map<std::string, std::string>& } serviceNameMapState - serviceName state.
168      * @param { std::string } sessionId - avsession id.
169      * @return { int64_t } ID returned after successful start of cast.
170      * @since 10
171     */
172     virtual int64_t StartCast(const OutputDeviceInfo& outputDeviceInfo,
173         std::map<std::string, std::string>& serviceNameMapState, std::string sessionId) = 0;
174 
175     /**
176      * @brief Notify CastEngine to add (connect) remote devices.
177      *
178      * @param { int32_t } castId - Find the corresponding provider through this ID.
179      * @param { OutputDeviceInfo } outputDeviceInfo - Devices to be connected.
180      * @return { int32_t } Whether the operation was successful.
181      * @since 10
182     */
183     virtual int32_t AddDevice(const int32_t castId, const OutputDeviceInfo& outputDeviceInfo) = 0;
184 
185     /**
186      * @brief Stop cast process.
187      *
188      * @param { const int64_t } castHandle - The ID corresponding to the provider that needs to be stopped.
189      * @return { int32_t } Whether the operation was successful.
190      * @since 10
191     */
192     virtual int32_t StopCast(const int64_t castHandle) = 0;
193 
194     /**
195      * @brief Stop cast session process.
196      *
197      * @param { const int64_t } castHandle - The ID corresponding to the provider that needs to be stopped.
198      * @return { int32_t } Whether the operation was successful.
199      * @since 10
200     */
201     virtual int32_t StopCastSession(const int64_t castHandle) = 0;
202 
203     /**
204      * @brief Listen for AVRouter Callback event.
205      *
206      * @param { int64_t } castHandleconst - The ID corresponding to the provider.
207      * @param { std::shared_ptr<IAVRouterListener> } callback - Callback function.
208      * @param { std::string } sessionId - avsession id.
209      * @param deviceInfo The device info.
210      * @return { int32_t } Whether the operation was successful.
211      * @since 10
212     */
213     virtual int32_t RegisterCallback(int64_t castHandleconst,
214         std::shared_ptr<IAVRouterListener> callback, std::string sessionId, DeviceInfo deviceInfo) = 0;
215 
216     /**
217      * @brief Cancel listening for AVRouter Callback event.
218      *
219      * @param { int64_t } castHandleconst - The ID corresponding to the provider.
220      * @param { std::shared_ptr<IAVRouterListener> } callback - Callback function.
221      * @param { std::string } sessionId - avsession id.
222      * @return { int32_t } Whether the operation was successful.
223      * @since 10
224     */
225     virtual int32_t UnRegisterCallback(int64_t castHandleconst,
226         std::shared_ptr<IAVRouterListener> callback, std::string sessionId) = 0;
227 
228     /**
229      * @brief set allconnect state.
230      *
231      * @param { int64_t } castHandle const - The ID corresponding to the provider.
232      * @param { DeviceInfo } cast deviceinfo - The deviceinfo to the castprovider.
233      * @return { int32_t } Whether the operation was successful.
234      * @since 11
235     */
236     virtual int32_t SetServiceAllConnectState(int64_t castHandle, DeviceInfo deviceInfo) = 0;
237 
238     /**
239      * @brief get remote networkId.
240      *
241      * @param { int64_t } castHandle const - The ID corresponding to the castprovider.
242      * @param { string } cast deviceId - The deviceId give cast+ to get remote networkId.
243      * @param { string } cast networkId - The networkId to transmit remote networkId.
244      * @return { int32_t } Whether the operation was successful.
245      * @since 11
246     */
247     virtual int32_t GetRemoteNetWorkId(int64_t castHandle, std::string deviceId, std::string &networkId) = 0;
248 
249     /**
250      * @brief get mirror castHandle.
251      *
252      * @return { int64_t } mirror castHandle.
253      * @since 13
254     */
255     virtual int64_t GetMirrorCastHandle() = 0;
256 
257     /**
258      * @brief Listen to the change of cast state change.
259      *
260      * @param castState The cast state of device info.
261      * @param deviceInfo The device info.
262      * @param isNeedRemove is need remove cast device
263      * @since 13
264     */
265     void OnCastStateChange(int32_t castState, DeviceInfo deviceInfo, bool isNeedRemove);
266 
267     /**
268      * @brief Listen to the change of cast event.
269      *
270      * @param errorCode The error code of cast event.
271      * @param errorMsg The error message of cast event.
272      * @since 13
273     */
274     void OnCastEventRecv(int32_t errorCode, std::string& errorMsg);
275 
276     /**
277      * @brief disconnect other session.
278      *
279      * @param { std::string } sessionId - avsession id.
280      * @param { DeviceInfo } deviceinfo - The deviceinfo to the castprovider.
281      * @since 13
282     */
283     virtual void DisconnetOtherSession(std::string sessionId, DeviceInfo deviceInfo) = 0;
284 
285 struct CastHandleInfo {
286     OutputDeviceInfo outputDeviceInfo_;
287     std::string sessionId_ = "-1";
288     std::shared_ptr<IAVCastControllerProxy> avCastControllerProxy_ = nullptr;
289     std::shared_ptr<IAVRouterListener> avRouterListener_ = nullptr;
290 };
291 #endif
292 };
293 } // namespace OHOS::AVSession
294 #endif // OHOS_AVROUTER_H
295