1 /*
2  * Copyright (c) 2021-2023 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 #include "dcamera_channel_source_impl.h"
17 
18 #include "dcamera_softbus_adapter.h"
19 
20 #include "anonymous_string.h"
21 #include "distributed_camera_constants.h"
22 #include "distributed_camera_errno.h"
23 #include "distributed_hardware_log.h"
24 
25 namespace OHOS {
26 namespace DistributedHardware {
DCameraChannelSourceImpl()27 DCameraChannelSourceImpl::DCameraChannelSourceImpl()
28 {
29 }
30 
~DCameraChannelSourceImpl()31 DCameraChannelSourceImpl::~DCameraChannelSourceImpl()
32 {
33 }
34 
CloseSession()35 int32_t DCameraChannelSourceImpl::CloseSession()
36 {
37     DHLOGI("DCameraChannelSourceImpl CloseSession name: %{public}s", GetAnonyString(mySessionName_).c_str());
38     if (softbusSessions_.empty()) {
39         DHLOGE("DCameraChannelSourceImpl CloseSession %{public}s failed", GetAnonyString(mySessionName_).c_str());
40         return DCAMERA_BAD_OPERATE;
41     }
42     int32_t ret = DCAMERA_OK;
43     for (auto iter = softbusSessions_.begin(); iter != softbusSessions_.end(); iter++) {
44         if ((*iter) == nullptr) {
45             continue;
46         }
47         int32_t retOpen = (*iter)->CloseSession();
48         if (retOpen != DCAMERA_OK) {
49             DHLOGE("DCameraChannelSourceImpl CloseSession %{public}s failed, ret: %{public}d",
50                 GetAnonyString(mySessionName_).c_str(), retOpen);
51             ret = DCAMERA_BAD_OPERATE;
52         }
53     }
54 
55     return ret;
56 }
57 
CreateSession(std::vector<DCameraIndex> & camIndexs,std::string sessionFlag,DCameraSessionMode sessionMode,std::shared_ptr<ICameraChannelListener> & listener)58 int32_t DCameraChannelSourceImpl::CreateSession(std::vector<DCameraIndex>& camIndexs, std::string sessionFlag,
59     DCameraSessionMode sessionMode, std::shared_ptr<ICameraChannelListener>& listener)
60 {
61     if (camIndexs.size() > DCAMERA_MAX_NUM || listener == nullptr) {
62         return DCAMERA_BAD_VALUE;
63     }
64     if (!softbusSessions_.empty()) {
65         DHLOGI("DCameraChannelSourceImpl session has already create %{public}s", sessionFlag.c_str());
66         return DCAMERA_OK;
67     }
68     camIndexs_.assign(camIndexs.begin(), camIndexs.end());
69     listener_ = listener;
70     mySessionName_ = SESSION_HEAD + sessionFlag;
71     mode_ = sessionMode;
72     std::string myDevId;
73     DCameraSoftbusAdapter::GetInstance().GetLocalNetworkId(myDevId);
74     DHLOGI("DCameraChannelSourceImpl CreateSession Start, name: %{public}s devId: %{public}s",
75         GetAnonyString(mySessionName_).c_str(), GetAnonyString(myDevId).c_str());
76     for (auto iter = camIndexs.begin(); iter != camIndexs.end(); iter++) {
77         std::string peerDevId = (*iter).devId_;
78         std::string peerSessionName = SESSION_HEAD + (*iter).dhId_ + std::string("_") + sessionFlag;
79         // source_bind
80         int32_t ret = DCameraSoftbusAdapter::GetInstance().CreateSoftBusSourceSocketClient(myDevId, peerSessionName,
81             peerDevId, sessionMode, DCAMERA_CHANNLE_ROLE_SOURCE);
82         if (ret != DCAMERA_OK) {
83             DHLOGE("DCameraChannelSourceImpl CreateSession failed, ret: %{public}d", ret);
84             return ret;
85         }
86         int32_t sourceSocket = DCameraSoftbusAdapter::GetInstance().GetSourceSocketId();
87         std::shared_ptr<DCameraSoftbusSession> softbusSess = std::make_shared<DCameraSoftbusSession>(myDevId,
88             mySessionName_, peerDevId, peerSessionName, listener, sessionMode);
89         softbusSess->OnSessionOpened(sourceSocket);
90         DCameraSoftbusAdapter::GetInstance().RecordSourceSocketSession(sourceSocket, softbusSess);
91         softbusSessions_.push_back(softbusSess);
92         DCameraSoftbusAdapter::GetInstance().sourceSessions_[peerDevId + peerSessionName] = softbusSess;
93     }
94     DHLOGI("DCameraChannelSourceImpl CreateSession End");
95     return DCAMERA_OK;
96 }
97 
ReleaseSession()98 int32_t DCameraChannelSourceImpl::ReleaseSession()
99 {
100     DHLOGI("DCameraChannelSourceImpl ReleaseSession name: %{public}s", GetAnonyString(mySessionName_).c_str());
101     for (auto iter = softbusSessions_.begin(); iter != softbusSessions_.end(); iter++) {
102         if ((*iter) == nullptr) {
103             continue;
104         }
105         std::string sessKey = (*iter)->GetPeerDevId() + (*iter)->GetPeerSessionName();
106         DCameraSoftbusAdapter::GetInstance().sourceSessions_.erase(sessKey);
107     }
108     std::vector<std::shared_ptr<DCameraSoftbusSession>>().swap(softbusSessions_);
109     int32_t ret = DCameraSoftbusAdapter::GetInstance().DestroySoftbusSessionServer(mySessionName_);
110     if (ret != DCAMERA_OK) {
111         DHLOGE("DCameraChannelSourceImpl ReleaseSession %{public}s failed, ret: %{public}d",
112             GetAnonyString(mySessionName_).c_str(), ret);
113     }
114     return ret;
115 }
116 
SendData(std::shared_ptr<DataBuffer> & buffer)117 int32_t DCameraChannelSourceImpl::SendData(std::shared_ptr<DataBuffer>& buffer)
118 {
119     if (softbusSessions_.empty()) {
120         DHLOGE("DCameraChannelSourceImpl SendData %{public}s failed", GetAnonyString(mySessionName_).c_str());
121         return DCAMERA_BAD_OPERATE;
122     }
123     int32_t ret = DCAMERA_OK;
124     for (auto iter = softbusSessions_.begin(); iter != softbusSessions_.end(); iter++) {
125         if ((*iter) == nullptr) {
126             continue;
127         }
128         int32_t retSend = (*iter)->SendData(mode_, buffer);
129         if (retSend != DCAMERA_OK) {
130             DHLOGE("DCameraChannelSourceImpl SendData %{public}s failed, ret: %{public}d",
131                 GetAnonyString(mySessionName_).c_str(), retSend);
132             ret = DCAMERA_BAD_OPERATE;
133         }
134     }
135     return ret;
136 }
137 } // namespace DistributedHardware
138 } // namespace OHOS
139