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_sink_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 {
DCameraChannelSinkImpl()27 DCameraChannelSinkImpl::DCameraChannelSinkImpl()
28 {
29     softbusSession_ = nullptr;
30 }
31 
~DCameraChannelSinkImpl()32 DCameraChannelSinkImpl::~DCameraChannelSinkImpl()
33 {
34 }
35 
CloseSession()36 int32_t DCameraChannelSinkImpl::CloseSession()
37 {
38     DHLOGI("DCameraChannelSinkImpl CloseSession name: %{public}s", GetAnonyString(mySessionName_).c_str());
39     if (softbusSession_ == nullptr) {
40         DHLOGE("DCameraChannelSinkImpl CloseSession %{public}s failed", GetAnonyString(mySessionName_).c_str());
41         return DCAMERA_BAD_OPERATE;
42     }
43     int32_t ret = softbusSession_->CloseSession();
44     if (ret != DCAMERA_OK) {
45         DHLOGE("DCameraChannelSinkImpl CloseSession %{public}s ret: %{public}d",
46             GetAnonyString(mySessionName_).c_str(), ret);
47     }
48 
49     return ret;
50 }
51 
CreateSession(std::vector<DCameraIndex> & camIndexs,std::string sessionFlag,DCameraSessionMode sessionMode,std::shared_ptr<ICameraChannelListener> & listener)52 int32_t DCameraChannelSinkImpl::CreateSession(std::vector<DCameraIndex>& camIndexs, std::string sessionFlag,
53     DCameraSessionMode sessionMode, std::shared_ptr<ICameraChannelListener>& listener)
54 {
55     if (camIndexs.size() > DCAMERA_MAX_NUM || listener == nullptr) {
56         return DCAMERA_BAD_VALUE;
57     }
58     if (softbusSession_ != nullptr) {
59         DHLOGI("DCameraChannelSinkImpl session has already create %{public}s", sessionFlag.c_str());
60         return DCAMERA_OK;
61     }
62     camIndexs_.assign(camIndexs.begin(), camIndexs.end());
63     listener_ = listener;
64     mySessionName_ = SESSION_HEAD + camIndexs[0].dhId_ + std::string("_") + sessionFlag;
65     mode_ = sessionMode;
66     std::string myDevId;
67     DCameraSoftbusAdapter::GetInstance().GetLocalNetworkId(myDevId);
68     std::string peerDevId = camIndexs[0].devId_;
69     std::string peerSessionName = SESSION_HEAD + sessionFlag;
70     DHLOGI("DCameraChannelSinkImpl CreateSession Listen Start, devId: %{public}s", GetAnonyString(myDevId).c_str());
71     // sink_server_listen
72     int32_t ret = DCameraSoftbusAdapter::GetInstance().CreatSoftBusSinkSocketServer(mySessionName_,
73         DCAMERA_CHANNLE_ROLE_SINK, sessionMode, peerDevId, peerSessionName);
74     if (ret != DCAMERA_OK) {
75         DHLOGE("DCameraChannelSinkImpl CreateSession Error, ret %{public}d", ret);
76         return ret;
77     }
78     softbusSession_ = std::make_shared<DCameraSoftbusSession>(myDevId, mySessionName_, peerDevId, peerSessionName,
79         listener, sessionMode);
80     DCameraSoftbusAdapter::GetInstance().sinkSessions_[mySessionName_] = softbusSession_;
81     DHLOGI("DCameraChannelSinkImpl CreateSession Listen End, devId: %{public}s", GetAnonyString(myDevId).c_str());
82     return DCAMERA_OK;
83 }
84 
ReleaseSession()85 int32_t DCameraChannelSinkImpl::ReleaseSession()
86 {
87     DHLOGI("DCameraChannelSinkImpl ReleaseSession name: %{public}s", GetAnonyString(mySessionName_).c_str());
88     if (softbusSession_ == nullptr) {
89         return DCAMERA_OK;
90     }
91     DCameraSoftbusAdapter::GetInstance().sinkSessions_.erase(softbusSession_->GetMySessionName());
92     int32_t ret = DCameraSoftbusAdapter::GetInstance().DestroySoftbusSessionServer(softbusSession_->GetMySessionName());
93     if (ret != DCAMERA_OK) {
94         DHLOGE("DCameraChannelSinkImpl ReleaseSession %{public}s failed, ret: %{public}d",
95             GetAnonyString(mySessionName_).c_str(), ret);
96     }
97     softbusSession_ = nullptr;
98     return ret;
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