• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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_DISTRIBUTED_ABILITY_CONNECTION_SESSION_H
17 #define OHOS_DISTRIBUTED_ABILITY_CONNECTION_SESSION_H
18 
19 #include <map>
20 #include <string>
21 #include <shared_mutex>
22 #include <mutex>
23 #include <condition_variable>
24 #include <thread>
25 
26 #include "ability_connection_info.h"
27 #include "ability_connection_session_listener.h"
28 #include "av_receiver_engine.h"
29 #include "av_sender_engine.h"
30 #include "av_trans_data_buffer.h"
31 #include "channel_manager.h"
32 #include "event_handler.h"
33 #include "ichannel_listener.h"
34 #include "iengine_listener.h"
35 #include "js_ability_connection_session_listener.h"
36 
37 namespace OHOS {
38 namespace DistributedCollab {
39 enum class SessionStatus : int32_t {
40     UNCONNECTED,
41     CONNECTING,
42     CONNECTED,
43 };
44 
45 enum class TransChannelType : int32_t {
46     MESSAGE,
47     DATA,
48     STREAM_BYTES,
49     STREAM,
50     SEND_FILE,
51     RECEIVE_FILE,
52 };
53 
54 enum class CollabrateDirection : int32_t {
55     UNKNOWN = -1,
56     COLLABRATE_SOURCE,
57     COLLABRATE_SINK,
58 };
59 
60 enum class MessageType : uint32_t  {
61     NORMAL,
62     WIFI_OPEN,
63     UPDATE_RECV_ENGINE_CHANNEL,
64     UPDATE_SENDER_ENGINE_CHANNEL,
65     RECEIVE_STREAM_START,
66     STREAM_ENCODING,
67     CONNECT_FILE_CHANNEL,
68     FILE_CHANNEL_CONNECT_SUCCESS,
69     FILE_CHANNEL_CONNECT_FAILED,
70     SESSION_CONNECT_SUCCESS,
71 };
72 
73 typedef enum {
74     SURFACE_ROTATE_NONE = 0,
75     SURFACE_ROTATE_90 = 90,
76     SURFACE_ROTATE_180 = 180,
77     SURFACE_ROTATE_270 = 270,
78 } SurfaceRotateParams;
79 
80 struct TransChannelInfo {
81     int32_t channelId;
82     ChannelDataType channelType;
83     TransChannelType transType;
84     bool isConnected;
85 };
86 
87 struct AbilityConnectionSessionInfo {
88     std::string serverId_;
89     PeerInfo localInfo_;
90     PeerInfo peerInfo_;
91 
92     AbilityConnectionSessionInfo() = default;
AbilityConnectionSessionInfoAbilityConnectionSessionInfo93     AbilityConnectionSessionInfo(const std::string& serverId, const PeerInfo& localInfo,
94         const PeerInfo& peerInfo) : serverId_(serverId), localInfo_(localInfo), peerInfo_(peerInfo) {}
95 
96     bool operator == (const AbilityConnectionSessionInfo &index) const
97     {
98         return this->serverId_ == index.serverId_ && this->localInfo_ == index.localInfo_ &&
99             this->peerInfo_ == index.peerInfo_;
100     }
101 
102     bool operator < (const AbilityConnectionSessionInfo &index) const
103     {
104         if (this->serverId_ != index.serverId_) {
105             return this->serverId_ < index.serverId_;
106         }
107         if (this->localInfo_ == index.localInfo_) {
108             return this->peerInfo_ < index.peerInfo_;
109         }
110         return this->localInfo_ < index.localInfo_;
111     }
112 };
113 
114 class AbilityConnectionSession : public std::enable_shared_from_this<AbilityConnectionSession> {
115 public:
116     AbilityConnectionSession(int32_t sessionId, std::string serverSocketName, AbilityConnectionSessionInfo sessionInfo,
117         ConnectOption opt);
118     ~AbilityConnectionSession();
119 
120     void Init();
121     void UnInit();
122 
123     void Release();
124     PeerInfo GetPeerInfo();
125     PeerInfo GetLocalInfo();
126     using ConnectCallback = std::function<void(const ConnectResult& result)>;
127     int32_t Connect(ConnectCallback& callback);
128     int32_t Disconnect();
129     int32_t AcceptConnect(const std::string& token);
130 
131     int32_t HandleCollabResult(int32_t result, const std::string& peerSocketName,
132         const std::string& dmsServerToken, const std::string& reason);
133     int32_t HandleDisconnect();
134 
135     int32_t SendMessage(const std::string& msg, const MessageType& messageType = MessageType::NORMAL);
136     int32_t SendData(const std::shared_ptr<AVTransDataBuffer>& buffer);
137     int32_t SendImage(const std::shared_ptr<Media::PixelMap>& image, int32_t imageQuality);
138     int32_t SendFile(const std::vector<std::string>& sFiles,
139         const std::vector<std::string>& dFiles);
140 
141     int32_t CreateStream(int32_t streamId, const StreamParams& param);
142     int32_t DestroyStream();
143     int32_t SetSurfaceId(const std::string& surfaceId, const SurfaceParams& param);
144     int32_t GetSurfaceId(const SurfaceParams& param, std::string& surfaceId);
145     int32_t UpdateSurfaceParam(const SurfaceParams& param);
146     int32_t StartStream(int32_t streamId);
147     int32_t StopStream(int32_t streamId);
148 
149     void OnChannelConnect(int32_t channelId);
150     void OnChannelClosed(int32_t channelId, const ShutdownReason& reason);
151     void OnBytesReceived(int32_t channelId, const std::shared_ptr<AVTransDataBuffer> buffer);
152     void OnMessageReceived(int32_t channelId, const std::shared_ptr<AVTransDataBuffer> buffer);
153     void OnRecvPixelMap(const std::shared_ptr<Media::PixelMap>& pixelMap);
154     void OnError(int32_t channelId, const int32_t errorCode);
155     void OnSendFile(const int32_t channelId, const FileInfo& info);
156     void OnRecvFile(const int32_t channelId, const FileInfo& info);
157     const char* GetRecvPath(const int32_t channelId);
158 
159     int32_t RegisterEventCallback(const std::string& eventType,
160         const std::shared_ptr<JsAbilityConnectionSessionListener>& listener);
161     int32_t UnregisterEventCallback(const std::string& eventType);
162     int32_t ConnectStreamChannel();
163     void UpdateEngineTransChannel();
164     int32_t RegisterEventCallback(const std::shared_ptr<IAbilityConnectionSessionListener>& listener);
165     int32_t UnregisterEventCallback();
166     std::string GetServerToken();
167     int32_t HandlePeerVersion(int32_t version);
168     void FinishSessionConnect();
169 
170 private:
171     void StartEvent();
172     int32_t InitChannels();
173     int32_t CreateChannel(const std::string& channelName, const ChannelDataType& dataType,
174         const TransChannelType& channelType, bool isClientChannel);
175     std::string GetChannelName(const AbilityConnectionSessionInfo& sessionInfo);
176     int32_t ConnectChannels();
177     int32_t GetChannelByType(const TransChannelType& dataType, int32_t& channelId);
178     bool IsAllChannelConnected();
179     bool IsVaildChannel(const int32_t channelId);
180     void SetTimeOut(int32_t time);
181     void RemoveTimeout();
182     bool IsConnecting();
183     bool IsConnected();
184 
185     int32_t InitSenderEngine();
186     int32_t InitRecvEngine();
187     int32_t GetStreamTransChannel(TransChannelInfo& info);
188     template <typename T>
189     int32_t ConfigEngineParam(std::shared_ptr<T> &engine, const SurfaceParams& param);
190     void UpdateTransChannelStatus(int32_t channelId, bool isConnect);
191     int32_t GetTransChannelInfo(const TransChannelType& type, TransChannelInfo& info);
192     bool IsStreamBytesChannel(const int32_t channelId);
193 
194     void ExeuteConnectCallback(const ConnectResult& result);
195     int32_t ExeuteEventCallback(const std::string& eventType, EventCallbackInfo& info);
196     int32_t ExeuteEventCallback(const std::string& eventType, CollaborateEventInfo& info);
197     template <typename T>
198     int32_t ExeuteEventCallbackTemplate(const std::string& eventType, T& info);
199 
200     SurfaceParam ConvertToSurfaceParam(const SurfaceParams& param);
201     int32_t StartRecvEngine();
202     int32_t StartSenderEngine();
203     int32_t ConnectTransChannel(const TransChannelType channelType);
204     int32_t DoConnectStreamChannel(int32_t channelId);
205     void UpdateRecvEngineTransChannel();
206     void UpdateSenderEngineTransChannel();
207     void ExeuteMessageEventCallback(const std::string msg);
208     void NotifyAppConnectResult(bool isConnected,
209         const ConnectErrorCode errorCode = ConnectErrorCode::SYSTEM_INTERNAL_ERROR,
210         const std::string& reason = "");
211     int32_t CreateStreamChannel(const std::string& channelName, bool isClientChannel);
212     void ConnectFileChannel(const std::string& peerSocketName);
213     void HandleSessionConnect();
214     std::string CreateDmsServerToken();
215     void InitMessageHandlerMap();
216     int32_t RequestReceiveFileChannelConnection();
217     void NotifyPeerSessionConnected();
218     bool CheckConnectedSession();
219     bool CheckWifiStatus();
220     ConnectErrorCode ConvertToConnectErrorCode(int32_t collabResult);
221     void UpdateRecvEngineStatus();
222     DisconnectReason ConvertToDisconnectReason(const ShutdownReason& reason);
223 
224 private:
225     class CollabChannelListener : public IChannelListener {
226     public:
227         CollabChannelListener() = default;
228         explicit CollabChannelListener(const std::shared_ptr<AbilityConnectionSession>& AbilityConnectionSession);
229         virtual ~CollabChannelListener() = default;
230 
231         void OnConnect(const int32_t channelId) const override;
232         void OnDisConnect(const int32_t channelId, const ShutdownReason& reason) const override;
233         void OnStream(const int32_t channelId, const std::shared_ptr<AVTransStreamData>& sendData) const override;
234         void OnBytes(const int32_t channelId, const std::shared_ptr<AVTransDataBuffer>& buffer) const override;
235         void OnMessage(const int32_t channelId, const std::shared_ptr<AVTransDataBuffer>& buffer) const override;
236         void OnError(const int32_t channelId, const int32_t errorCode) const override;
237         void OnSendFile(const int32_t channelId, const FileInfo& info) const override;
238         void OnRecvFile(const int32_t channelId, const FileInfo& info) const override;
239         const char* GetRecvPath(const int32_t channelId) const override;
240 
241     private:
242         std::weak_ptr<AbilityConnectionSession> abilityConnectionSession_;
243     };
244 
245 private:
246     class PixelMapListener : public IEngineListener {
247     public:
248         PixelMapListener() = default;
249         explicit PixelMapListener(const std::shared_ptr<AbilityConnectionSession>& AbilityConnectionSession);
250         virtual ~PixelMapListener() = default;
251         void OnRecvPixelMap(const std::shared_ptr<Media::PixelMap>& pixelMap) override;
252         void OnRecvSurfaceParam(const SurfaceParam& param) override;
253 
254     private:
255         std::weak_ptr<AbilityConnectionSession> abilityConnectionSession_;
256     };
257 
258 private:
259     int32_t sessionId_ = 0;
260     int32_t streamId_ = 0;
261     int32_t version_ = 0;
262     std::string dmsServerToken_;
263     std::string localSocketName_;
264     std::string peerSocketName_;
265     std::string channelName_;
266     AbilityConnectionSessionInfo sessionInfo_;
267     ConnectOption connectOption_;
268     CollabrateDirection direction_ = CollabrateDirection::UNKNOWN;
269 
270     std::shared_ptr<CollabChannelListener> channelListener_ = nullptr;
271 
272     std::shared_mutex transChannelMutex_;
273     std::map<TransChannelType, TransChannelInfo> transChannels_;
274 
275     std::shared_mutex sessionMutex_;
276     SessionStatus sessionStatus_ = SessionStatus::UNCONNECTED;
277     ConnectCallback connectCallback_ = nullptr;
278 
279     std::thread eventThread_;
280     std::condition_variable eventCon_;
281     std::mutex eventMutex_;
282     std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_ = nullptr;
283 
284     std::shared_mutex listenerMutex_;
285     std::map<std::string, std::shared_ptr<JsAbilityConnectionSessionListener>> listeners_;
286 
287     std::shared_mutex engineMutex_;
288     std::shared_ptr<AVSenderEngine> senderEngine_ = nullptr;
289     std::shared_ptr<AVReceiverEngine> recvEngine_ = nullptr;
290     std::shared_ptr<IEngineListener> pixelMapListener = nullptr;
291     EngineState recvEngineState_ = EngineState::EMPTY;
292 
293     std::shared_mutex sessionListenerMutex_;
294     std::shared_ptr<IAbilityConnectionSessionListener> sessionListener_ = nullptr;
295 
296     std::map<uint32_t, std::function<void(const std::string&)>> messageHandlerMap_;
297     // wait for connect state
298     std::mutex connectionMutex_;
299     std::condition_variable connectionCondition_;
300 };
301 } // namespace DistributedCollab
302 } // namespace OHOS
303 #endif //OHOS_DISTRIBUTED_ABILITY_CONNECTION_SESSION_H