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 <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     int32_t GetSourceSocketId();
68     void RecordSourceSocketSession(int32_t socket, std::shared_ptr<DCameraSoftbusSession> session);
69 
70 public:
71     std::map<std::string, std::shared_ptr<DCameraSoftbusSession>> sourceSessions_;
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     int32_t DCameraSoftbusGetSessionById(int32_t socket, std::shared_ptr<DCameraSoftbusSession>& session);
83 
84 private:
85     std::mutex optLock_;
86     const std::string PKG_NAME = "ohos.dhardware.dcamera";
87     static const uint32_t DCAMERA_SESSION_NAME_MAX_LEN = 128;
88     std::map<DCAMERA_CHANNEL_ROLE, ISocketListener> sessListeners_;
89     std::map<std::string, uint32_t> sessionTotal_;
90     static const uint32_t DCAMERA_LINK_TYPE_MAX = 4;
91     static const uint32_t DCAMERA_LINK_TYPE_INDEX_2 = 2;
92     std::mutex idMapLock_;
93     std::map<int32_t, std::shared_ptr<DCameraSoftbusSession>> sessionIdMap_;
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::mutex mySessionNameLock_;
100     std::set<std::string> mySessionNameSet_;
101 
102     std::mutex sinkSocketLock_;
103     std::map<int32_t, std::shared_ptr<DCameraSoftbusSession>> sinkSocketSessionMap_;
104     std::mutex sourceSocketLock_;
105     std::map<int32_t, std::shared_ptr<DCameraSoftbusSession>> sourceSocketSessionMap_;
106 };
107 } // namespace DistributedHardware
108 } // namespace OHOS
109 #endif // OHOS_DCAMERA_SOFTBUS_ADAPTER_H
110