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