1 /*
2 * Copyright (c) 2021-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 #define HST_LOG_TAG "FilterPort"
17
18 #include "pipeline/core/port.h"
19 #include <algorithm>
20 #include "foundation/log.h"
21 #include "foundation/pre_defines.h"
22 #include "pipeline/core/filter.h"
23
24 namespace OHOS {
25 namespace Media {
26 namespace Pipeline {
27 std::shared_ptr<InPort> EmptyInPort::port = std::make_shared<EmptyInPort>();
28 std::shared_ptr<OutPort> EmptyOutPort::port = std::make_shared<EmptyOutPort>();
29
GetName()30 const std::string& Port::GetName()
31 {
32 return name;
33 }
34
GetOwnerFilter() const35 const InfoTransfer* Port::GetOwnerFilter() const
36 {
37 return filter;
38 }
39
GetPeerPort()40 std::shared_ptr<Port> Port::GetPeerPort()
41 {
42 return nullptr;
43 }
44
Connect(const std::shared_ptr<Port> & port)45 ErrorCode InPort::Connect(const std::shared_ptr<Port>& port)
46 {
47 prevPort = port;
48 return ErrorCode::SUCCESS;
49 }
50
Disconnect()51 ErrorCode InPort::Disconnect()
52 {
53 prevPort.reset();
54 return ErrorCode::SUCCESS;
55 }
56
Activate(const std::vector<WorkMode> & modes,WorkMode & outMode)57 ErrorCode InPort::Activate(const std::vector<WorkMode>& modes, WorkMode& outMode)
58 {
59 if (auto ptr = prevPort.lock()) {
60 FAIL_RETURN(ptr->Activate(modes, workMode));
61 outMode = workMode;
62 return ErrorCode::SUCCESS;
63 }
64 MEDIA_LOG_E("[Filter " PUBLIC_LOG_S "] InPort " PUBLIC_LOG_S " Activate error: prevPort destructed",
65 filter->GetName().c_str(), name.c_str());
66 return ErrorCode::ERROR_INVALID_PARAMETER_VALUE;
67 }
68
GetPeerPort()69 std::shared_ptr<Port> InPort::GetPeerPort()
70 {
71 return prevPort.lock();
72 }
73
Negotiate(const std::shared_ptr<const Plugin::Capability> & upstreamCap,Plugin::Capability & negotiatedCap,const Plugin::Meta & upstreamParams,Plugin::Meta & downstreamParams)74 bool InPort::Negotiate(const std::shared_ptr<const Plugin::Capability>& upstreamCap,
75 Plugin::Capability& negotiatedCap,
76 const Plugin::Meta& upstreamParams,
77 Plugin::Meta& downstreamParams)
78 {
79 return filter && filter->Negotiate(name, upstreamCap, negotiatedCap, upstreamParams, downstreamParams);
80 }
81
Configure(const std::shared_ptr<const Plugin::Meta> & upstreamMeta,Plugin::Meta & upstreamParams,Plugin::Meta & downstreamParams)82 bool InPort::Configure(const std::shared_ptr<const Plugin::Meta>& upstreamMeta, Plugin::Meta& upstreamParams,
83 Plugin::Meta& downstreamParams)
84 {
85 return filter && filter->Configure(name, upstreamMeta, upstreamParams, downstreamParams);
86 }
87
88
PushData(const AVBufferPtr & buffer,int64_t offset)89 void InPort::PushData(const AVBufferPtr& buffer, int64_t offset)
90 {
91 if (filter) {
92 filter->PushData(name, buffer, offset);
93 } else {
94 MEDIA_LOG_E("filter destructed");
95 }
96 }
97
PullData(uint64_t offset,size_t size,AVBufferPtr & data)98 ErrorCode InPort::PullData(uint64_t offset, size_t size, AVBufferPtr& data)
99 {
100 if (auto ptr = prevPort.lock()) {
101 return ptr->PullData(offset, size, data);
102 }
103 MEDIA_LOG_E("prevPort destructed");
104 return ErrorCode::ERROR_INVALID_PARAMETER_VALUE;
105 }
106
Connect(const std::shared_ptr<Port> & port)107 ErrorCode OutPort::Connect(const std::shared_ptr<Port>& port)
108 {
109 if (InSamePipeline(port)) {
110 nextPort = port;
111 return ErrorCode::SUCCESS;
112 }
113 MEDIA_LOG_E("Connect filters that are not in the same pipeline.");
114 return ErrorCode::ERROR_INVALID_PARAMETER_VALUE;
115 }
116
Disconnect()117 ErrorCode OutPort::Disconnect()
118 {
119 nextPort.reset();
120 return ErrorCode::SUCCESS;
121 }
122
InSamePipeline(const std::shared_ptr<Port> & port) const123 bool OutPort::InSamePipeline(const std::shared_ptr<Port>& port) const
124 {
125 auto filter1 = GetOwnerFilter();
126 FALSE_RETURN_V(filter1 != nullptr, false);
127 auto filter2 = port->GetOwnerFilter();
128 FALSE_RETURN_V(filter2 != nullptr, false);
129 auto pipeline1 = filter1->GetOwnerPipeline();
130 FALSE_RETURN_V(pipeline1 != nullptr, false);
131 auto pipeline2 = filter2->GetOwnerPipeline();
132 FALSE_RETURN_V(pipeline2 != nullptr, false);
133 return pipeline1 == pipeline2;
134 }
135
Activate(const std::vector<WorkMode> & modes,WorkMode & outMode)136 ErrorCode OutPort::Activate(const std::vector<WorkMode>& modes, WorkMode& outMode)
137 {
138 if (filter) {
139 auto supportedModes = filter->GetWorkModes();
140 for (auto mode : modes) {
141 auto found = std::find(supportedModes.cbegin(), supportedModes.cend(), mode);
142 if (found != supportedModes.cend()) {
143 outMode = mode;
144 workMode = mode;
145 return ErrorCode::SUCCESS; // 最先找到的兼容的mode,作为最后结果
146 }
147 }
148 } else {
149 MEDIA_LOG_E("no valid filter");
150 }
151 MEDIA_LOG_E("negotiate failed");
152 return ErrorCode::ERROR_UNKNOWN;
153 }
154
GetPeerPort()155 std::shared_ptr<Port> OutPort::GetPeerPort()
156 {
157 return nextPort;
158 }
159
Negotiate(const std::shared_ptr<const Plugin::Capability> & upstreamCap,Plugin::Capability & negotiatedCap,const Plugin::Meta & upstreamParams,Plugin::Meta & downstreamParams)160 bool OutPort::Negotiate(const std::shared_ptr<const Plugin::Capability>& upstreamCap,
161 Plugin::Capability& negotiatedCap,
162 const Plugin::Meta& upstreamParams,
163 Plugin::Meta& downstreamParams)
164 {
165 return nextPort->Negotiate(upstreamCap, negotiatedCap, upstreamParams, downstreamParams);
166 }
167
Configure(const std::shared_ptr<const Plugin::Meta> & upstreamMeta,Plugin::Meta & upstreamParams,Plugin::Meta & downstreamParams)168 bool OutPort::Configure(const std::shared_ptr<const Plugin::Meta>& upstreamMeta, Plugin::Meta& upstreamParams,
169 Plugin::Meta& downstreamParams)
170 {
171 return nextPort->Configure(upstreamMeta, upstreamParams, downstreamParams);
172 }
173
PushData(const AVBufferPtr & buffer,int64_t offset)174 void OutPort::PushData(const AVBufferPtr& buffer, int64_t offset)
175 {
176 nextPort->PushData(buffer, offset);
177 }
178
PullData(uint64_t offset,size_t size,AVBufferPtr & data)179 ErrorCode OutPort::PullData(uint64_t offset, size_t size, AVBufferPtr& data)
180 {
181 if (filter) {
182 return filter->PullData(name, offset, size, data);
183 }
184 MEDIA_LOG_E("filter destructed");
185 return ErrorCode::ERROR_INVALID_PARAMETER_VALUE;
186 }
187
Connect(const std::shared_ptr<Port> & another)188 ErrorCode EmptyInPort::Connect(const std::shared_ptr<Port>& another)
189 {
190 UNUSED_VARIABLE(another);
191 MEDIA_LOG_E("Connect in EmptyInPort");
192 return ErrorCode::SUCCESS;
193 }
194
Activate(const std::vector<WorkMode> & modes,WorkMode & outMode)195 ErrorCode EmptyInPort::Activate(const std::vector<WorkMode>& modes, WorkMode& outMode)
196 {
197 UNUSED_VARIABLE(modes);
198 UNUSED_VARIABLE(outMode);
199 MEDIA_LOG_E("Activate in EmptyInPort");
200 return ErrorCode::SUCCESS;
201 }
202
Negotiate(const std::shared_ptr<const Plugin::Capability> & upstreamCap,Plugin::Capability & negotiatedCap,const Plugin::Meta & upstreamParams,Plugin::Meta & downstreamParams)203 bool EmptyInPort::Negotiate(const std::shared_ptr<const Plugin::Capability>& upstreamCap,
204 Plugin::Capability& negotiatedCap,
205 const Plugin::Meta& upstreamParams,
206 Plugin::Meta& downstreamParams)
207 {
208 UNUSED_VARIABLE(upstreamCap);
209 UNUSED_VARIABLE(negotiatedCap);
210 UNUSED_VARIABLE(upstreamParams);
211 UNUSED_VARIABLE(downstreamParams);
212 MEDIA_LOG_E("Negotiate in EmptyInPort");
213 return false;
214 }
215
Configure(const std::shared_ptr<const Plugin::Meta> & upstreamMeta,Plugin::Meta & upstreamParams,Plugin::Meta & downstreamParams)216 bool EmptyInPort::Configure(const std::shared_ptr<const Plugin::Meta>& upstreamMeta, Plugin::Meta& upstreamParams,
217 Plugin::Meta& downstreamParams)
218 {
219 UNUSED_VARIABLE(upstreamMeta);
220 UNUSED_VARIABLE(upstreamParams);
221 UNUSED_VARIABLE(downstreamParams);
222 MEDIA_LOG_E("Configure in EmptyInPort");
223 return false;
224 }
225
PushData(const AVBufferPtr & buffer,int64_t offset)226 void EmptyInPort::PushData(const AVBufferPtr& buffer, int64_t offset)
227 {
228 UNUSED_VARIABLE(buffer);
229 UNUSED_VARIABLE(offset);
230 MEDIA_LOG_E("PushData in EmptyInPort");
231 }
PullData(uint64_t offset,size_t size,AVBufferPtr & data)232 ErrorCode EmptyInPort::PullData(uint64_t offset, size_t size, AVBufferPtr& data)
233 {
234 UNUSED_VARIABLE(offset);
235 UNUSED_VARIABLE(size);
236 UNUSED_VARIABLE(data);
237 MEDIA_LOG_E("PullData in EmptyInPort");
238 return ErrorCode::ERROR_UNIMPLEMENTED;
239 }
240
Connect(const std::shared_ptr<Port> & another)241 ErrorCode EmptyOutPort::Connect(const std::shared_ptr<Port>& another)
242 {
243 UNUSED_VARIABLE(another);
244 MEDIA_LOG_E("Connect in EmptyOutPort");
245 return ErrorCode::SUCCESS;
246 }
Activate(const std::vector<WorkMode> & modes,WorkMode & outMode)247 ErrorCode EmptyOutPort::Activate(const std::vector<WorkMode>& modes, WorkMode& outMode)
248 {
249 UNUSED_VARIABLE(modes);
250 UNUSED_VARIABLE(outMode);
251 MEDIA_LOG_E("Activate in EmptyOutPort");
252 return ErrorCode::SUCCESS;
253 }
Negotiate(const std::shared_ptr<const Plugin::Capability> & upstreamCap,Plugin::Capability & negotiatedCap,const Plugin::Meta & upstreamParams,Plugin::Meta & downstreamParams)254 bool EmptyOutPort::Negotiate(const std::shared_ptr<const Plugin::Capability>& upstreamCap,
255 Plugin::Capability& negotiatedCap,
256 const Plugin::Meta& upstreamParams,
257 Plugin::Meta& downstreamParams)
258 {
259 UNUSED_VARIABLE(upstreamCap);
260 UNUSED_VARIABLE(negotiatedCap);
261 UNUSED_VARIABLE(upstreamParams);
262 UNUSED_VARIABLE(downstreamParams);
263 MEDIA_LOG_E("Negotiate in EmptyOutPort");
264 return false;
265 }
266
Configure(const std::shared_ptr<const Plugin::Meta> & upstreamMeta,Plugin::Meta & upstreamParams,Plugin::Meta & downstreamParams)267 bool EmptyOutPort::Configure(const std::shared_ptr<const Plugin::Meta>& upstreamMeta, Plugin::Meta& upstreamParams,
268 Plugin::Meta& downstreamParams)
269 {
270 UNUSED_VARIABLE(upstreamMeta);
271 UNUSED_VARIABLE(upstreamParams);
272 UNUSED_VARIABLE(downstreamParams);
273 MEDIA_LOG_E("Configure in EmptyOutPort");
274 return false;
275 }
276
PushData(const AVBufferPtr & buffer,int64_t offset)277 void EmptyOutPort::PushData(const AVBufferPtr& buffer, int64_t offset)
278 {
279 UNUSED_VARIABLE(buffer);
280 UNUSED_VARIABLE(offset);
281 MEDIA_LOG_E("PushData in EmptyOutPort");
282 }
PullData(uint64_t offset,size_t size,AVBufferPtr & data)283 ErrorCode EmptyOutPort::PullData(uint64_t offset, size_t size, AVBufferPtr& data)
284 {
285 UNUSED_VARIABLE(offset);
286 UNUSED_VARIABLE(size);
287 UNUSED_VARIABLE(data);
288 MEDIA_LOG_E("PullData in EmptyOutPort");
289 return ErrorCode::ERROR_UNIMPLEMENTED;
290 }
291 } // namespace Pipeline
292 } // namespace Media
293 } // namespace OHOS
294