1 /* 2 * Copyright (c) 2021 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 <unistd.h> 22 23 #include "session.h" 24 #include "single_instance.h" 25 26 #include "dcamera_softbus_session.h" 27 28 namespace OHOS { 29 namespace DistributedHardware { 30 typedef enum { 31 DCAMERA_CHANNLE_ROLE_SOURCE = 0, 32 DCAMERA_CHANNLE_ROLE_SINK = 1, 33 } DCAMERA_CHANNEL_ROLE; 34 35 class DCameraSoftbusAdapter { 36 DECLARE_SINGLE_INSTANCE_BASE(DCameraSoftbusAdapter); 37 38 public: 39 int32_t CreateSoftbusSessionServer(std::string sessionName, DCAMERA_CHANNEL_ROLE role); 40 int32_t DestroySoftbusSessionServer(std::string sessionName); 41 int32_t OpenSoftbusSession(std::string mySessName, std::string peerSessName, int32_t sessionMode, 42 std::string peerDevId); 43 int32_t CloseSoftbusSession(int32_t sessionId); 44 int32_t SendSofbusBytes(int32_t sessionId, std::shared_ptr<DataBuffer>& buffer); 45 int32_t SendSofbusStream(int32_t sessionId, std::shared_ptr<DataBuffer>& buffer); 46 int32_t GetLocalNetworkId(std::string& myDevId); 47 48 int32_t OnSourceSessionOpened(int32_t sessionId, int32_t result); 49 void OnSourceSessionClosed(int32_t sessionId); 50 void OnSourceBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen); 51 void OnSourceMessageReceived(int32_t sessionId, const void *data, uint32_t dataLen); 52 void OnSourceStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext, 53 const StreamFrameInfo *param); 54 55 int32_t OnSinkSessionOpened(int32_t sessionId, int32_t result); 56 void OnSinkSessionClosed(int32_t sessionId); 57 void OnSinkBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen); 58 void OnSinkMessageReceived(int32_t sessionId, const void *data, uint32_t dataLen); 59 void OnSinkStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext, 60 const StreamFrameInfo *param); 61 int32_t ConstructSessionAttribute(int32_t sessionMode, SessionAttribute& attr); 62 int32_t HandleSourceStreamExt(std::shared_ptr<DataBuffer>& buffer, const StreamData *ext); 63 64 public: 65 std::map<std::string, std::shared_ptr<DCameraSoftbusSession>> sourceSessions_; 66 std::map<std::string, std::shared_ptr<DCameraSoftbusSession>> sinkSessions_; 67 68 private: 69 DCameraSoftbusAdapter(); 70 ~DCameraSoftbusAdapter(); 71 72 int32_t DCameraSoftbusSourceGetSession(int32_t sessionId, std::shared_ptr<DCameraSoftbusSession>& session); 73 int32_t DCameraSoftbusSinkGetSession(int32_t sessionId, std::shared_ptr<DCameraSoftbusSession>& session); 74 int32_t DCameraSoftbusGetSessionById(int32_t sessionId, std::shared_ptr<DCameraSoftbusSession>& session); 75 76 private: 77 std::mutex optLock_; 78 const std::string PKG_NAME = "ohos.dhardware.dcamera"; 79 static const uint32_t DCAMERA_SESSION_NAME_MAX_LEN = 128; 80 std::map<DCAMERA_CHANNEL_ROLE, ISessionListener> sessListeners_; 81 std::map<std::string, uint32_t> sessionTotal_; 82 static const uint32_t DCAMERA_LINK_TYPE_MAX = 4; 83 static const uint32_t DCAMERA_LINK_TYPE_INDEX_2 = 2; 84 std::mutex idMapLock_; 85 std::map<int32_t, std::shared_ptr<DCameraSoftbusSession>> sessionIdMap_; 86 }; 87 } // namespace DistributedHardware 88 } // namespace OHOS 89 #endif // OHOS_DCAMERA_SOFTBUS_ADAPTER_H 90