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 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
12 */
13
14 #include "node_base.h"
15
16 namespace OHOS::Camera {
GetName() const17 std::string PortBase::GetName() const
18 {
19 return name_;
20 }
21
SetFormat(const PortFormat & format)22 RetCode PortBase::SetFormat(const PortFormat& format)
23 {
24 format_ = format;
25 return RC_OK;
26 }
27
GetFormat(PortFormat & format) const28 RetCode PortBase::GetFormat(PortFormat& format) const
29 {
30 format = format_;
31 return RC_OK;
32 }
33
GetStreamId() const34 int32_t PortBase::GetStreamId() const
35 {
36 PortFormat format = {};
37 GetFormat(format);
38 return format.streamId_;
39 }
40
Connect(const std::shared_ptr<IPort> & peer)41 RetCode PortBase::Connect(const std::shared_ptr<IPort>& peer)
42 {
43 peer_ = peer;
44 return RC_OK;
45 }
46
DisConnect()47 RetCode PortBase::DisConnect()
48 {
49 peer_.reset();
50 return RC_OK;
51 }
52
Direction() const53 int32_t PortBase::Direction() const
54 {
55 if (name_.empty()) {
56 return 0;
57 }
58
59 if (name_[0] == 'i') {
60 return 0;
61 } else {
62 return 1;
63 }
64 }
65
GetNode() const66 std::shared_ptr<INode> PortBase::GetNode() const
67 {
68 return owner_.lock();
69 }
70
Peer() const71 std::shared_ptr<IPort> PortBase::Peer() const
72 {
73 return peer_;
74 }
75
DeliverBuffer(std::shared_ptr<IBuffer> & buffer)76 void PortBase::DeliverBuffer(std::shared_ptr<IBuffer>& buffer)
77 {
78 auto peerPort = Peer();
79 CHECK_IF_PTR_NULL_RETURN_VOID(peerPort);
80 auto peerNode = peerPort->GetNode();
81 CHECK_IF_PTR_NULL_RETURN_VOID(peerNode);
82 peerNode->DeliverBuffer(buffer);
83
84 return;
85 }
86
DeliverBuffers(std::vector<std::shared_ptr<IBuffer>> & buffers)87 void PortBase::DeliverBuffers(std::vector<std::shared_ptr<IBuffer>>& buffers)
88 {
89 auto peerPort = Peer();
90 CHECK_IF_PTR_NULL_RETURN_VOID(peerPort);
91 auto peerNode = peerPort->GetNode();
92 CHECK_IF_PTR_NULL_RETURN_VOID(peerNode);
93 peerNode->DeliverBuffers(buffers);
94
95 return;
96 }
97
DeliverBuffers(std::shared_ptr<FrameSpec> frameSpec)98 void PortBase::DeliverBuffers(std::shared_ptr<FrameSpec> frameSpec)
99 {
100 (void)frameSpec;
101 return;
102 }
103
DeliverBuffers(std::vector<std::shared_ptr<FrameSpec>> mergeVec)104 void PortBase::DeliverBuffers(std::vector<std::shared_ptr<FrameSpec>> mergeVec)
105 {
106 (void)mergeVec;
107 return;
108 }
109
GetName() const110 std::string NodeBase::GetName() const
111 {
112 return name_;
113 }
114
GetType() const115 std::string NodeBase::GetType() const
116 {
117 return type_;
118 }
119
GetPort(const std::string & name)120 std::shared_ptr<IPort> NodeBase::GetPort(const std::string& name)
121 {
122 std::unique_lock<std::mutex> l(portLock_);
123 auto it = std::find_if(portVec_.begin(), portVec_.end(),
124 [name](std::shared_ptr<IPort> p) { return p->GetName() == name; });
125 if (it != portVec_.end()) {
126 return *it;
127 }
128 std::shared_ptr<IPort> port = std::make_shared<PortBase>(name, shared_from_this());
129 portVec_.push_back(port);
130 return port;
131 }
132
Init(const int32_t streamId)133 RetCode NodeBase::Init(const int32_t streamId)
134 {
135 (void)streamId;
136 return RC_OK;
137 }
138
Start(const int32_t streamId)139 RetCode NodeBase::Start(const int32_t streamId)
140 {
141 (void)streamId;
142 CAMERA_LOGI("name:%{public}s start enter\n", name_.c_str());
143 return RC_OK;
144 }
145
Flush(const int32_t streamId)146 RetCode NodeBase::Flush(const int32_t streamId)
147 {
148 (void)streamId;
149 return RC_OK;
150 }
151
SetCallback()152 RetCode NodeBase::SetCallback()
153 {
154 return RC_OK;
155 }
156
Stop(const int32_t streamId)157 RetCode NodeBase::Stop(const int32_t streamId)
158 {
159 (void)streamId;
160 return RC_OK;
161 }
162
Config(const int32_t streamId,const CaptureMeta & meta)163 RetCode NodeBase::Config(const int32_t streamId, const CaptureMeta& meta)
164 {
165 (void)streamId;
166 (void)meta;
167 return RC_OK;
168 }
169
170
GetNumberOfInPorts() const171 int32_t NodeBase::GetNumberOfInPorts() const
172 {
173 std::unique_lock<std::mutex> l(portLock_);
174 int32_t re = std::count_if(portVec_.begin(), portVec_.end(), [](auto &it) { return it->Direction() == 0; });
175 return re;
176 }
177
GetNumberOfOutPorts() const178 int32_t NodeBase::GetNumberOfOutPorts() const
179 {
180 std::unique_lock<std::mutex> l(portLock_);
181 int32_t re = std::count_if(portVec_.begin(), portVec_.end(), [](auto &it) { return it->Direction() == 1; });
182 return re;
183 }
184
GetInPorts() const185 std::vector<std::shared_ptr<IPort>> NodeBase::GetInPorts() const
186 {
187 std::unique_lock<std::mutex> l(portLock_);
188 std::vector<std::shared_ptr<IPort>> re;
189 std::copy_if(portVec_.begin(), portVec_.end(), std::back_inserter(re),
190 [](auto &it) { return it->Direction() == 0; });
191
192 return re;
193 }
194
GetOutPorts()195 std::vector<std::shared_ptr<IPort>> NodeBase::GetOutPorts()
196 {
197 std::unique_lock<std::mutex> l(portLock_);
198 std::vector<std::shared_ptr<IPort>> out = {};
199 std::copy_if(portVec_.begin(), portVec_.end(), std::back_inserter(out),
200 [](auto &it) { return it->Direction() == 1; });
201
202 return out;
203 }
204
GetOutPortById(const int32_t id)205 std::shared_ptr<IPort> NodeBase::GetOutPortById(const int32_t id)
206 {
207 auto ports = GetOutPorts();
208 if (ports.size() <= id) {
209 return nullptr;
210 }
211 return ports[id];
212 }
213
SetCallBack(BufferCb c)214 void NodeBase::SetCallBack(BufferCb c)
215 {
216 (void)c;
217 return;
218 }
219
Capture(const int32_t streamId,const int32_t captureId)220 RetCode NodeBase::Capture(const int32_t streamId, const int32_t captureId)
221 {
222 (void)streamId;
223 (void)captureId;
224 return RC_OK;
225 }
226
CancelCapture(const int32_t streamId)227 RetCode NodeBase::CancelCapture(const int32_t streamId)
228 {
229 (void)streamId;
230 return RC_OK;
231 }
232
DeliverBuffer(std::shared_ptr<IBuffer> & buffer)233 void NodeBase::DeliverBuffer(std::shared_ptr<IBuffer>& buffer)
234 {
235 auto outPorts = GetOutPorts();
236 auto it = std::find_if(outPorts.begin(), outPorts.end(),
237 [&buffer](const auto &port) { return port->format_.bufferPoolId_ == buffer->GetPoolId(); });
238 if (it != outPorts.end()) {
239 CAMERA_LOGE("NodeBase::DeliverBuffer to next node, %{public}s -> %{public}s, \
240 streamId = %{public}d, index = %{public}d",
241 GetName().c_str(), (*it)->Peer()->GetNode()->GetName().c_str(), buffer->GetStreamId(), buffer->GetIndex());
242 (*it)->DeliverBuffer(buffer);
243 }
244 }
245
DeliverBuffers(std::vector<std::shared_ptr<IBuffer>> & buffers)246 void NodeBase::DeliverBuffers(std::vector<std::shared_ptr<IBuffer>>& buffers)
247 {
248 if (buffers.empty()) {
249 return;
250 }
251 auto outPorts = GetOutPorts();
252 auto it = std::find_if(outPorts.begin(), outPorts.end(),
253 [&buffers](const auto &port) { return port->format_.bufferPoolId_ == buffers[0]->GetPoolId(); });
254 if (it != outPorts.end()) {
255 (*it)->DeliverBuffers(buffers);
256 return;
257 }
258 return;
259 }
260
ProvideBuffers(std::shared_ptr<FrameSpec> frameSpec)261 RetCode NodeBase::ProvideBuffers(std::shared_ptr<FrameSpec> frameSpec)
262 {
263 (void)frameSpec;
264 return RC_OK;
265 }
266
DeliverBuffers(std::shared_ptr<FrameSpec> frameSpec)267 void NodeBase::DeliverBuffers(std::shared_ptr<FrameSpec> frameSpec)
268 {
269 (void)frameSpec;
270 return;
271 }
272
DeliverBuffers(std::vector<std::shared_ptr<FrameSpec>> mergeVec)273 void NodeBase::DeliverBuffers(std::vector<std::shared_ptr<FrameSpec>> mergeVec)
274 {
275 (void)mergeVec;
276 return;
277 }
278 } // namespace OHOS::Camera