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: %s", GetAnonyString(mySessionName_).c_str());
39 if (softbusSession_ == nullptr) {
40 DHLOGE("DCameraChannelSinkImpl CloseSession %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 %s ret: %d", GetAnonyString(mySessionName_).c_str(), ret);
46 }
47
48 return ret;
49 }
50
CreateSession(std::vector<DCameraIndex> & camIndexs,std::string sessionFlag,DCameraSessionMode sessionMode,std::shared_ptr<ICameraChannelListener> & listener)51 int32_t DCameraChannelSinkImpl::CreateSession(std::vector<DCameraIndex>& camIndexs, std::string sessionFlag,
52 DCameraSessionMode sessionMode, std::shared_ptr<ICameraChannelListener>& listener)
53 {
54 if (camIndexs.size() > DCAMERA_MAX_NUM || listener == nullptr) {
55 return DCAMERA_BAD_VALUE;
56 }
57 if (softbusSession_ != nullptr) {
58 DHLOGI("DCameraChannelSinkImpl session has already create %s", sessionFlag.c_str());
59 return DCAMERA_OK;
60 }
61 camIndexs_.assign(camIndexs.begin(), camIndexs.end());
62 listener_ = listener;
63 mySessionName_ = SESSION_HEAD + camIndexs[0].dhId_ + std::string("_") + sessionFlag;
64 mode_ = sessionMode;
65 std::string myDevId;
66 DCameraSoftbusAdapter::GetInstance().GetLocalNetworkId(myDevId);
67 std::string peerDevId = camIndexs[0].devId_;
68 std::string peerSessionName = SESSION_HEAD + sessionFlag;
69 DHLOGI("DCameraChannelSinkImpl CreateSession Listen Start, devId: %s", GetAnonyString(myDevId).c_str());
70 // sink_server_listen
71 int32_t ret = DCameraSoftbusAdapter::GetInstance().CreatSoftBusSinkSocketServer(mySessionName_,
72 DCAMERA_CHANNLE_ROLE_SINK, sessionMode, peerDevId, peerSessionName);
73 if (ret != DCAMERA_OK) {
74 DHLOGE("DCameraChannelSinkImpl CreateSession Error, ret %d", ret);
75 return ret;
76 }
77 softbusSession_ = std::make_shared<DCameraSoftbusSession>(myDevId, mySessionName_, peerDevId, peerSessionName,
78 listener, sessionMode);
79 DCameraSoftbusAdapter::GetInstance().sinkSessions_[mySessionName_] = softbusSession_;
80 DHLOGI("DCameraChannelSinkImpl CreateSession Listen End, devId: %s", GetAnonyString(myDevId).c_str());
81 return DCAMERA_OK;
82 }
83
ReleaseSession()84 int32_t DCameraChannelSinkImpl::ReleaseSession()
85 {
86 DHLOGI("DCameraChannelSinkImpl ReleaseSession name: %s", GetAnonyString(mySessionName_).c_str());
87 if (softbusSession_ == nullptr) {
88 return DCAMERA_OK;
89 }
90 DCameraSoftbusAdapter::GetInstance().sinkSessions_.erase(softbusSession_->GetMySessionName());
91 int32_t ret = DCameraSoftbusAdapter::GetInstance().DestroySoftbusSessionServer(softbusSession_->GetMySessionName());
92 if (ret != DCAMERA_OK) {
93 DHLOGE("DCameraChannelSinkImpl ReleaseSession %s failed, ret: %d", GetAnonyString(mySessionName_).c_str(), ret);
94 }
95 softbusSession_ = nullptr;
96 return ret;
97 }
98
SendData(std::shared_ptr<DataBuffer> & buffer)99 int32_t DCameraChannelSinkImpl::SendData(std::shared_ptr<DataBuffer>& buffer)
100 {
101 if (softbusSession_ == nullptr) {
102 DHLOGE("DCameraChannelSinkImpl SendData %s failed", GetAnonyString(mySessionName_).c_str());
103 return DCAMERA_BAD_OPERATE;
104 }
105 int32_t ret = softbusSession_->SendData(mode_, buffer);
106 if (ret != DCAMERA_OK) {
107 DHLOGE("DCameraChannelSinkImpl SendData %s failed, ret: %d", GetAnonyString(mySessionName_).c_str(), ret);
108 }
109 return ret;
110 }
111 } // namespace DistributedHardware
112 } // namespace OHOS
113