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 void NotifyCollabErrorResultFromSa(bool isConnected, 212 const ConnectErrorCode errorCode = ConnectErrorCode::SYSTEM_INTERNAL_ERROR, 213 const std::string& reason = ""); 214 int32_t CreateStreamChannel(const std::string& channelName, bool isClientChannel); 215 void ConnectFileChannel(const std::string& peerSocketName); 216 void HandleSessionConnect(); 217 std::string CreateDmsServerToken(); 218 void InitMessageHandlerMap(); 219 int32_t RequestReceiveFileChannelConnection(); 220 void NotifyPeerSessionConnected(); 221 bool CheckConnectedSession(); 222 bool CheckWifiStatus(); 223 ConnectErrorCode ConvertToConnectErrorCode(int32_t collabResult); 224 void UpdateRecvEngineStatus(); 225 DisconnectReason ConvertToDisconnectReason(const ShutdownReason& reason); 226 227 private: 228 class CollabChannelListener : public IChannelListener { 229 public: 230 CollabChannelListener() = default; 231 explicit CollabChannelListener(const std::shared_ptr<AbilityConnectionSession>& AbilityConnectionSession); 232 virtual ~CollabChannelListener() = default; 233 234 void OnConnect(const int32_t channelId) const override; 235 void OnDisConnect(const int32_t channelId, const ShutdownReason& reason) const override; 236 void OnStream(const int32_t channelId, const std::shared_ptr<AVTransStreamData>& sendData) const override; 237 void OnBytes(const int32_t channelId, const std::shared_ptr<AVTransDataBuffer>& buffer) const override; 238 void OnMessage(const int32_t channelId, const std::shared_ptr<AVTransDataBuffer>& buffer) const override; 239 void OnError(const int32_t channelId, const int32_t errorCode) const override; 240 void OnSendFile(const int32_t channelId, const FileInfo& info) const override; 241 void OnRecvFile(const int32_t channelId, const FileInfo& info) const override; 242 const char* GetRecvPath(const int32_t channelId) const override; 243 244 private: 245 std::weak_ptr<AbilityConnectionSession> abilityConnectionSession_; 246 }; 247 248 private: 249 class PixelMapListener : public IEngineListener { 250 public: 251 PixelMapListener() = default; 252 explicit PixelMapListener(const std::shared_ptr<AbilityConnectionSession>& AbilityConnectionSession); 253 virtual ~PixelMapListener() = default; 254 void OnRecvPixelMap(const std::shared_ptr<Media::PixelMap>& pixelMap) override; 255 void OnRecvSurfaceParam(const SurfaceParam& param) override; 256 257 private: 258 std::weak_ptr<AbilityConnectionSession> abilityConnectionSession_; 259 }; 260 261 private: 262 int32_t sessionId_ = 0; 263 int32_t streamId_ = 0; 264 int32_t version_ = 0; 265 std::string dmsServerToken_; 266 std::string localSocketName_; 267 std::string peerSocketName_; 268 std::string channelName_; 269 AbilityConnectionSessionInfo sessionInfo_; 270 ConnectOption connectOption_; 271 CollabrateDirection direction_ = CollabrateDirection::UNKNOWN; 272 StreamParams streamParam_; 273 274 std::shared_ptr<CollabChannelListener> channelListener_ = nullptr; 275 276 std::shared_mutex transChannelMutex_; 277 std::map<TransChannelType, TransChannelInfo> transChannels_; 278 279 std::shared_mutex sessionMutex_; 280 SessionStatus sessionStatus_ = SessionStatus::UNCONNECTED; 281 ConnectCallback connectCallback_ = nullptr; 282 283 std::thread eventThread_; 284 std::condition_variable eventCon_; 285 std::mutex eventMutex_; 286 std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_ = nullptr; 287 288 std::shared_mutex listenerMutex_; 289 std::map<std::string, std::shared_ptr<JsAbilityConnectionSessionListener>> listeners_; 290 291 std::shared_mutex engineMutex_; 292 std::shared_ptr<AVSenderEngine> senderEngine_ = nullptr; 293 std::shared_ptr<AVReceiverEngine> recvEngine_ = nullptr; 294 std::shared_ptr<IEngineListener> pixelMapListener = nullptr; 295 EngineState recvEngineState_ = EngineState::EMPTY; 296 297 std::shared_mutex sessionListenerMutex_; 298 std::shared_ptr<IAbilityConnectionSessionListener> sessionListener_ = nullptr; 299 300 std::map<uint32_t, std::function<void(const std::string&)>> messageHandlerMap_; 301 // wait for connect state 302 std::mutex connectionMutex_; 303 std::condition_variable connectionCondition_; 304 }; 305 } // namespace DistributedCollab 306 } // namespace OHOS 307 #endif //OHOS_DISTRIBUTED_ABILITY_CONNECTION_SESSION_H