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