1 /* 2 * Copyright (c) 2021-2024 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_DCAMERA_SOFTBUS_ADAPTER_H 17 #define OHOS_DCAMERA_SOFTBUS_ADAPTER_H 18 19 #include <mutex> 20 #include <map> 21 #include <set> 22 #include <unistd.h> 23 24 #include "dcamera_softbus_session.h" 25 #include "icamera_channel.h" 26 #include "single_instance.h" 27 #include "socket.h" 28 #include "trans_type.h" 29 30 namespace OHOS { 31 namespace DistributedHardware { 32 typedef enum { 33 DCAMERA_CHANNLE_ROLE_SOURCE = 0, 34 DCAMERA_CHANNLE_ROLE_SINK = 1, 35 } DCAMERA_CHANNEL_ROLE; 36 37 class DCameraSoftbusAdapter { 38 DECLARE_SINGLE_INSTANCE_BASE(DCameraSoftbusAdapter); 39 40 public: 41 int32_t CreatSoftBusSinkSocketServer(std::string mySessionName, DCAMERA_CHANNEL_ROLE role, 42 DCameraSessionMode sessionMode, std::string peerDevId, std::string peerSessionName); 43 int32_t CreateSoftBusSourceSocketClient(std::string myDevId, std::string peerSessionName, 44 std::string peerDevId, DCameraSessionMode sessionMode, DCAMERA_CHANNEL_ROLE role); 45 46 int32_t DestroySoftbusSessionServer(std::string sessionName); 47 int32_t CloseSoftbusSession(int32_t socket); 48 int32_t SendSofbusBytes(int32_t socket, std::shared_ptr<DataBuffer> &buffer); 49 int32_t SendSofbusStream(int32_t socket, std::shared_ptr<DataBuffer> &buffer); 50 int32_t GetLocalNetworkId(std::string &myDevId); 51 52 int32_t SourceOnBind(int32_t socket, PeerSocketInfo info); 53 void SourceOnShutDown(int32_t socket, ShutdownReason reason); 54 void SourceOnBytes(int32_t socket, const void *data, uint32_t dataLen); 55 void SourceOnMessage(int32_t socket, const void *data, uint32_t dataLen); 56 void SourceOnStream(int32_t socket, const StreamData *data, const StreamData *ext, 57 const StreamFrameInfo *param); 58 59 int32_t SinkOnBind(int32_t socket, PeerSocketInfo info); 60 void SinkOnShutDown(int32_t socket, ShutdownReason reason); 61 void SinkOnBytes(int32_t socket, const void *data, uint32_t dataLen); 62 void SinkOnMessage(int32_t socket, const void *data, uint32_t dataLen); 63 void SinkOnStream(int32_t socket, const StreamData *data, const StreamData *ext, 64 const StreamFrameInfo *param); 65 66 int32_t HandleSourceStreamExt(std::shared_ptr<DataBuffer>& buffer, const StreamData *ext); 67 void RecordSourceSocketSession(int32_t socket, std::shared_ptr<DCameraSoftbusSession> session); 68 69 void CloseSessionWithNetWorkId(const std::string &networkId); 70 public: 71 std::map<std::string, std::shared_ptr<DCameraSoftbusSession>> sinkSessions_; 72 73 private: 74 DCameraSoftbusAdapter(); 75 ~DCameraSoftbusAdapter(); 76 77 int32_t DCameraSoftBusGetSessionByPeerSocket(int32_t socket, std::shared_ptr<DCameraSoftbusSession> &session, 78 PeerSocketInfo info); 79 int32_t DCameraSoftbusSourceGetSession(int32_t socket, std::shared_ptr<DCameraSoftbusSession>& session); 80 int32_t DCameraSoftbusSinkGetSession(int32_t socket, std::shared_ptr<DCameraSoftbusSession>& session); 81 void ReplaceSuffix(std::string &mySessNmRep, const std::string &suffix, const std::string &replacement); 82 83 private: 84 std::mutex optLock_; 85 const std::string PKG_NAME = "ohos.dhardware.dcamera"; 86 static const uint32_t DCAMERA_SESSION_NAME_MAX_LEN = 128; 87 std::map<DCAMERA_CHANNEL_ROLE, ISocketListener> sessListeners_; 88 std::map<std::string, uint32_t> sessionTotal_; 89 static const uint32_t DCAMERA_LINK_TYPE_MAX = 4; 90 static const uint32_t DCAMERA_LINK_TYPE_INDEX_2 = 2; 91 92 int32_t sourceSocketId_ = -1; 93 std::map<DCameraSessionMode, TransDataType> sessionModeAndDataTypeMap_; 94 std::mutex mySessionNamePeerDevIdLock_; 95 std::map<std::string, std::string> peerDevIdMySessionNameMap_; 96 std::map<std::string, std::string> mySessionNameMapV2_; 97 std::mutex mySocketSetLock_; 98 std::set<int32_t> mySocketSet_; 99 100 std::mutex sinkSocketLock_; 101 std::map<int32_t, std::shared_ptr<DCameraSoftbusSession>> sinkSocketSessionMap_; 102 std::mutex sourceSocketLock_; 103 std::map<int32_t, std::shared_ptr<DCameraSoftbusSession>> sourceSocketSessionMap_; 104 }; 105 } // namespace DistributedHardware 106 } // namespace OHOS 107 #endif // OHOS_DCAMERA_SOFTBUS_ADAPTER_H 108