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
30namespace OHOS {
31namespace DistributedHardware {
32typedef enum {
33    DCAMERA_CHANNLE_ROLE_SOURCE = 0,
34    DCAMERA_CHANNLE_ROLE_SINK = 1,
35} DCAMERA_CHANNEL_ROLE;
36
37class DCameraSoftbusAdapter {
38DECLARE_SINGLE_INSTANCE_BASE(DCameraSoftbusAdapter);
39
40public:
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
69public:
70    std::map<std::string, std::shared_ptr<DCameraSoftbusSession>> sinkSessions_;
71
72private:
73    DCameraSoftbusAdapter();
74    ~DCameraSoftbusAdapter();
75
76    int32_t DCameraSoftBusGetSessionByPeerSocket(int32_t socket, std::shared_ptr<DCameraSoftbusSession> &session,
77        PeerSocketInfo info);
78    int32_t DCameraSoftbusSourceGetSession(int32_t socket, std::shared_ptr<DCameraSoftbusSession>& session);
79    int32_t DCameraSoftbusSinkGetSession(int32_t socket, std::shared_ptr<DCameraSoftbusSession>& session);
80    std::string FindSessNameByPeerSessName(const std::string peerSessionName);
81
82private:
83    std::mutex optLock_;
84    const std::string PKG_NAME = "ohos.dhardware.dcamera";
85    static const uint32_t DCAMERA_SESSION_NAME_MAX_LEN = 128;
86    std::map<DCAMERA_CHANNEL_ROLE, ISocketListener> sessListeners_;
87    std::map<std::string, uint32_t> sessionTotal_;
88    static const uint32_t DCAMERA_LINK_TYPE_MAX = 4;
89    static const uint32_t DCAMERA_LINK_TYPE_INDEX_2 = 2;
90
91    int32_t sourceSocketId_ = -1;
92    std::map<DCameraSessionMode, TransDataType> sessionModeAndDataTypeMap_;
93    std::mutex mySessionNamePeerDevIdLock_;
94    std::map<std::string, std::string> peerDevIdMySessionNameMap_;
95    std::mutex mySocketSetLock_;
96    std::set<int32_t> mySocketSet_;
97
98    std::mutex sinkSocketLock_;
99    std::map<int32_t, std::shared_ptr<DCameraSoftbusSession>> sinkSocketSessionMap_;
100    std::mutex sourceSocketLock_;
101    std::map<int32_t, std::shared_ptr<DCameraSoftbusSession>> sourceSocketSessionMap_;
102};
103} // namespace DistributedHardware
104} // namespace OHOS
105#endif // OHOS_DCAMERA_SOFTBUS_ADAPTER_H
106