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 #include "dcamera_channel_sink_impl.h"
17 
18 #include "dcamera_softbus_adapter.h"
19 #include "dcamera_utils_tools.h"
20 
21 #include "anonymous_string.h"
22 #include "distributed_camera_constants.h"
23 #include "distributed_camera_errno.h"
24 #include "distributed_hardware_log.h"
25 
26 namespace OHOS {
27 namespace DistributedHardware {
DCameraChannelSinkImpl()28 DCameraChannelSinkImpl::DCameraChannelSinkImpl()
29 {
30     softbusSession_ = nullptr;
31 }
32 
~DCameraChannelSinkImpl()33 DCameraChannelSinkImpl::~DCameraChannelSinkImpl()
34 {
35 }
36 
CloseSession()37 int32_t DCameraChannelSinkImpl::CloseSession()
38 {
39     DHLOGI("DCameraChannelSinkImpl CloseSession name: %{public}s", GetAnonyString(mySessionName_).c_str());
40     if (softbusSession_ == nullptr) {
41         DHLOGE("DCameraChannelSinkImpl CloseSession %{public}s failed", GetAnonyString(mySessionName_).c_str());
42         return DCAMERA_BAD_OPERATE;
43     }
44     int32_t ret = softbusSession_->CloseSession();
45     if (ret != DCAMERA_OK) {
46         DHLOGE("DCameraChannelSinkImpl CloseSession %{public}s ret: %{public}d",
47             GetAnonyString(mySessionName_).c_str(), ret);
48     }
49 
50     return ret;
51 }
52 
CreateSession(std::vector<DCameraIndex>& camIndexs, std::string sessionFlag, DCameraSessionMode sessionMode, std::shared_ptr<ICameraChannelListener>& listener)53 int32_t DCameraChannelSinkImpl::CreateSession(std::vector<DCameraIndex>& camIndexs, std::string sessionFlag,
54     DCameraSessionMode sessionMode, std::shared_ptr<ICameraChannelListener>& listener)
55 {
56     if (camIndexs.size() > DCAMERA_MAX_NUM || listener == nullptr) {
57         return DCAMERA_BAD_VALUE;
58     }
59     std::string myDevId;
60     int32_t ret = GetLocalDeviceNetworkId(myDevId);
61     if (ret != DCAMERA_OK) {
62         DHLOGE("DCameraChannelSinkImpl get local device networkId error, ret%{public}d", ret);
63         return ret;
64     }
65     camIndexs_.assign(camIndexs.begin(), camIndexs.end());
66     listener_ = listener;
67     if (!ManageSelectChannel::GetInstance().GetSinkConnect()) {
68         mySessionName_ = SESSION_HEAD + camIndexs[0].dhId_ + std::string("_") + sessionFlag;
69     } else {
70         mySessionName_ = SESSION_HEAD + camIndexs[0].dhId_ + std::string("_") + sessionFlag + "_sender";
71     }
72     mode_ = sessionMode;
73     std::string peerDevId = camIndexs[0].devId_;
74     std::string peerSessionName = SESSION_HEAD + sessionFlag;
75     DHLOGI("DCameraChannelSinkImpl CreateSession Listen Start, devId: %{public}s", GetAnonyString(myDevId).c_str());
76     // sink_server_listen
77     softbusSession_ = std::make_shared<DCameraSoftbusSession>(myDevId, mySessionName_, peerDevId, peerSessionName,
78         listener, sessionMode);
79     ret = softbusSession_->CreateSocketServer();
80     if (ret != DCAMERA_OK) {
81         DHLOGE("DCameraChannelSinkImpl CreateSession Error, ret %{public}d", ret);
82         return ret;
83     }
84     DCameraSoftbusAdapter::GetInstance().sinkSessions_[mySessionName_] = softbusSession_;
85     DHLOGI("DCameraChannelSinkImpl CreateSession Listen End, devId: %{public}s", GetAnonyString(myDevId).c_str());
86     return DCAMERA_OK;
87 }
88 
ReleaseSession()89 int32_t DCameraChannelSinkImpl::ReleaseSession()
90 {
91     DHLOGI("DCameraChannelSinkImpl ReleaseSession name: %{public}s", GetAnonyString(mySessionName_).c_str());
92     if (softbusSession_ == nullptr) {
93         return DCAMERA_OK;
94     }
95     softbusSession_->ReleaseSession();
96     DCameraSoftbusAdapter::GetInstance().sinkSessions_.erase(softbusSession_->GetMySessionName());
97     softbusSession_ = nullptr;
98     return DCAMERA_OK;
99 }
100 
SendData(std::shared_ptr<DataBuffer>& buffer)101 int32_t DCameraChannelSinkImpl::SendData(std::shared_ptr<DataBuffer>& buffer)
102 {
103     if (softbusSession_ == nullptr) {
104         DHLOGE("DCameraChannelSinkImpl SendData %{public}s failed", GetAnonyString(mySessionName_).c_str());
105         return DCAMERA_BAD_OPERATE;
106     }
107     int32_t ret = softbusSession_->SendData(mode_, buffer);
108     if (ret != DCAMERA_OK) {
109         DHLOGE("DCameraChannelSinkImpl SendData %{public}s failed, ret: %{public}d",
110             GetAnonyString(mySessionName_).c_str(), ret);
111     }
112     return ret;
113 }
114 } // namespace DistributedHardware
115 } // namespace OHOS
116