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 "CodecFilterBase"
17
18 #include "codec_filter_base.h"
19 #include "foundation/cpp_ext/memory_ext.h"
20 #include "pipeline/core/plugin_attr_desc.h"
21 #include "pipeline/filters/common/plugin_settings.h"
22 #include "utils/steady_clock.h"
23
24 namespace {
25 constexpr uint32_t DEFAULT_OUT_BUFFER_POOL_SIZE = 5;
26 constexpr int32_t MAX_OUT_DECODED_DATA_SIZE_PER_FRAME = 20 * 1024; // 20kB
27 };
28
29 namespace OHOS {
30 namespace Media {
31 namespace Pipeline {
CodecFilterBase(const std::string & name)32 CodecFilterBase::CodecFilterBase(const std::string &name): FilterBase(name) {}
33
~CodecFilterBase()34 CodecFilterBase::~CodecFilterBase()
35 {
36 MEDIA_LOG_D("codec filter base dtor called");
37 }
38
Start()39 ErrorCode CodecFilterBase::Start()
40 {
41 MEDIA_LOG_I("codec filter base start called");
42 if (state_ != FilterState::READY && state_ != FilterState::PAUSED) {
43 MEDIA_LOG_W("call decoder start() when state is not ready or working");
44 return ErrorCode::ERROR_INVALID_OPERATION;
45 }
46 return FilterBase::Start();
47 }
48
Stop()49 ErrorCode CodecFilterBase::Stop()
50 {
51 MEDIA_LOG_D("CodecFilterBase stop start.");
52 // 先改变底层状态 然后停掉上层线程 否则会产生死锁
53 RETURN_AGAIN_IF_NULL(plugin_);
54 FAIL_RETURN_MSG(TranslatePluginStatus(plugin_->Flush()), "Flush plugin fail");
55 FAIL_RETURN_MSG(TranslatePluginStatus(plugin_->Stop()), "Stop plugin fail");
56 FAIL_RETURN_MSG(codecMode_->Stop(), "Codec mode stop fail");
57 MEDIA_LOG_D("CodecFilterBase stop end.");
58 return FilterBase::Stop();
59 }
60
Prepare()61 ErrorCode CodecFilterBase::Prepare()
62 {
63 MEDIA_LOG_I("codec filter base prepare called");
64 if (state_ != FilterState::INITIALIZED) {
65 MEDIA_LOG_W("codec filter is not in init state");
66 return ErrorCode::ERROR_INVALID_OPERATION;
67 }
68 auto err = FilterBase::Prepare();
69 FAIL_RETURN_MSG(err, "codec prepare error because of filter base prepare error");
70 return err;
71 }
72
UpdateMetaFromPlugin(Plugin::Meta & meta)73 ErrorCode CodecFilterBase::UpdateMetaFromPlugin(Plugin::Meta& meta)
74 {
75 auto parameterMap = PluginParameterTable::FindAllowedParameterMap(filterType_);
76 for (const auto& keyPair : parameterMap) {
77 if ((keyPair.second.second & PARAM_GET) == 0) {
78 continue;
79 }
80 Plugin::ValueType tmpVal;
81 auto ret = TranslatePluginStatus(plugin_->GetParameter(keyPair.first, tmpVal));
82 if (ret != ErrorCode::SUCCESS) {
83 if (HasTagInfo(keyPair.first)) {
84 MEDIA_LOG_I("GetParameter " PUBLIC_LOG_S " from plugin " PUBLIC_LOG_S "failed with code "
85 PUBLIC_LOG_D32, GetTagStrName(keyPair.first), pluginInfo_->name.c_str(), ret);
86 } else {
87 MEDIA_LOG_I("Tag " PUBLIC_LOG_D32 " is not is map, may be update it?", keyPair.first);
88 MEDIA_LOG_I("GetParameter " PUBLIC_LOG_D32 " from plugin " PUBLIC_LOG_S " failed with code "
89 PUBLIC_LOG_D32, keyPair.first, pluginInfo_->name.c_str(), ret);
90 }
91 continue;
92 }
93 if (!keyPair.second.first(keyPair.first, tmpVal)) {
94 if (HasTagInfo(keyPair.first)) {
95 MEDIA_LOG_I("Type of Tag " PUBLIC_LOG_S " should be " PUBLIC_LOG_S,
96 GetTagStrName(keyPair.first), std::get<2>(g_tagInfoMap.at(keyPair.first)));
97 } else {
98 MEDIA_LOG_I("Tag " PUBLIC_LOG_D32 " is not is map, may be update it?", keyPair.first);
99 MEDIA_LOG_I("Type of Tag " PUBLIC_LOG_D32 "mismatch", keyPair.first);
100 }
101 continue;
102 }
103 meta.SetData(static_cast<Plugin::MetaID>(keyPair.first), tmpVal);
104 }
105 return ErrorCode::SUCCESS;
106 }
107
SetPluginParameterLocked(Tag tag,const Plugin::ValueType & value)108 ErrorCode CodecFilterBase::SetPluginParameterLocked(Tag tag, const Plugin::ValueType& value)
109 {
110 return TranslatePluginStatus(plugin_->SetParameter(tag, value));
111 }
112
SetParameter(int32_t key,const Plugin::Any & value)113 ErrorCode CodecFilterBase::SetParameter(int32_t key, const Plugin::Any& value)
114 {
115 if (state_.load() == FilterState::CREATED) {
116 return ErrorCode::ERROR_AGAIN;
117 }
118 Tag tag = Tag::INVALID;
119 FALSE_RETURN_V_MSG_E(TranslateIntoParameter(key, tag), ErrorCode::ERROR_INVALID_PARAMETER_VALUE,
120 "key " PUBLIC_LOG_D32 " is out of boundary", key);
121 RETURN_AGAIN_IF_NULL(plugin_);
122 return SetPluginParameterLocked(tag, value);
123 }
124
GetParameter(int32_t key,Plugin::Any & outVal)125 ErrorCode CodecFilterBase::GetParameter(int32_t key, Plugin::Any& outVal)
126 {
127 if (state_.load() == FilterState::CREATED) {
128 return ErrorCode::ERROR_AGAIN;
129 }
130 Tag tag = Tag::INVALID;
131 FALSE_RETURN_V_MSG_E(TranslateIntoParameter(key, tag), ErrorCode::ERROR_INVALID_PARAMETER_VALUE,
132 "key " PUBLIC_LOG_D32 " is out of boundary", key);
133 RETURN_AGAIN_IF_NULL(plugin_);
134 return TranslatePluginStatus(plugin_->GetParameter(tag, outVal));
135 }
136
UpdateParams(const std::shared_ptr<const Plugin::Meta> & upMeta,std::shared_ptr<Plugin::Meta> & meta)137 void CodecFilterBase::UpdateParams(const std::shared_ptr<const Plugin::Meta>& upMeta,
138 std::shared_ptr<Plugin::Meta>& meta)
139 {
140 }
141
GetAllocator()142 std::shared_ptr<Allocator> CodecFilterBase::GetAllocator()
143 {
144 return plugin_->GetAllocator();
145 }
146
GetOutBufferPoolSize()147 uint32_t CodecFilterBase::GetOutBufferPoolSize()
148 {
149 return DEFAULT_OUT_BUFFER_POOL_SIZE;
150 }
151
CalculateBufferSize(const std::shared_ptr<const OHOS::Media::Plugin::Meta> & meta)152 uint32_t CodecFilterBase::CalculateBufferSize(const std::shared_ptr<const OHOS::Media::Plugin::Meta> &meta)
153 {
154 return 0;
155 }
156
GetNegotiateParams(const Plugin::TagMap & upstreamParams)157 Plugin::TagMap CodecFilterBase::GetNegotiateParams(const Plugin::TagMap& upstreamParams)
158 {
159 return upstreamParams;
160 }
161
CheckRequiredOutCapKeys(const Capability & capability)162 bool CodecFilterBase::CheckRequiredOutCapKeys(const Capability& capability)
163 {
164 std::vector<Capability::Key> outCapKeys = GetRequiredOutCapKeys();
165 for (auto outCapKey : outCapKeys) {
166 if (capability.keys.count(outCapKey) == 0) {
167 MEDIA_LOG_W("decoder plugin must specify key " PUBLIC_LOG_S " in out caps",
168 Tag2String(static_cast<Plugin::Tag>(outCapKey)));
169 return false;
170 }
171 }
172 return true;
173 }
174
GetRequiredOutCapKeys()175 std::vector<Capability::Key> CodecFilterBase::GetRequiredOutCapKeys()
176 {
177 // Not required by default
178 return {};
179 }
180
Negotiate(const std::string & inPort,const std::shared_ptr<const Plugin::Capability> & upstreamCap,Plugin::Capability & negotiatedCap,const Plugin::TagMap & upstreamParams,Plugin::TagMap & downstreamParams)181 bool CodecFilterBase::Negotiate(const std::string& inPort,
182 const std::shared_ptr<const Plugin::Capability>& upstreamCap,
183 Plugin::Capability& negotiatedCap,
184 const Plugin::TagMap& upstreamParams,
185 Plugin::TagMap& downstreamParams)
186 {
187 PROFILE_BEGIN("CodecFilterBase negotiate start");
188 FALSE_RETURN_V_MSG_W(state_ == FilterState::PREPARING, false, "filter is not preparing when negotiate");
189 auto targetOutPort = GetRouteOutPort(inPort);
190 FALSE_RETURN_V_MSG_W(targetOutPort != nullptr, false, "codec outPort is not found");
191 std::shared_ptr<Plugin::PluginInfo> selectedPluginInfo = nullptr;
192 bool atLeastOutCapMatched = false;
193 auto candidatePlugins = FindAvailablePlugins(*upstreamCap, Plugin::PluginType::CODEC);
194 for (const auto& candidate : candidatePlugins) {
195 FALSE_LOG_MSG(!candidate.first->outCaps.empty(),
196 "plugin " PUBLIC_LOG_S " has no out caps", candidate.first->name.c_str());
197 for (const auto& outCap : candidate.first->outCaps) { // each codec plugin should have at least one out cap
198 if (!CheckRequiredOutCapKeys(outCap)) {
199 continue;
200 }
201 auto thisOut = std::make_shared<Plugin::Capability>();
202 if (!MergeCapabilityKeys(*upstreamCap, outCap, *thisOut)) {
203 MEDIA_LOG_W("one cap of plugin " PUBLIC_LOG_S " mismatch upstream cap", candidate.first->name.c_str());
204 continue;
205 }
206 atLeastOutCapMatched = true;
207 thisOut->mime = outCap.mime;
208
209 // need to get the max buffer num from plugin capability when use hdi as codec plugin interfaces
210 Plugin::TagMap proposeParams;
211 if (targetOutPort->Negotiate(thisOut, capNegWithDownstream_, proposeParams, downstreamParams)) {
212 negotiatedCap = candidate.second;
213 selectedPluginInfo = candidate.first;
214 MEDIA_LOG_I("use plugin " PUBLIC_LOG_S, candidate.first->name.c_str());
215 MEDIA_LOG_I("neg upstream cap " PUBLIC_LOG_S, Capability2String(negotiatedCap).c_str());
216 MEDIA_LOG_I("neg downstream cap " PUBLIC_LOG_S, Capability2String(capNegWithDownstream_).c_str());
217 break;
218 }
219 }
220 if (selectedPluginInfo != nullptr) { // select the first one
221 break;
222 }
223 }
224 FALSE_RETURN_V_MSG_E(atLeastOutCapMatched && selectedPluginInfo != nullptr, false,
225 "can't find available codec plugin with " PUBLIC_LOG_S, Capability2String(*upstreamCap).c_str());
226
227 auto res = UpdateAndInitPluginByInfo<Plugin::Codec>(plugin_, pluginInfo_, selectedPluginInfo,
228 [](const std::string& name)-> std::shared_ptr<Plugin::Codec> {
229 return Plugin::PluginManager::Instance().CreateCodecPlugin(name);
230 });
231 FALSE_RETURN_V(codecMode_->Init(plugin_, outPorts_), false);
232 PROFILE_END("async codec negotiate end");
233 MEDIA_LOG_D("codec filter base negotiate end");
234 return res;
235 }
236
Configure(const std::string & inPort,const std::shared_ptr<const Plugin::Meta> & upstreamMeta,Plugin::TagMap & upstreamParams,Plugin::TagMap & downstreamParams)237 bool CodecFilterBase::Configure(const std::string &inPort, const std::shared_ptr<const Plugin::Meta> &upstreamMeta,
238 Plugin::TagMap &upstreamParams, Plugin::TagMap &downstreamParams)
239 {
240 MEDIA_LOG_I("receive upstream meta " PUBLIC_LOG_S, Meta2String(*upstreamMeta).c_str());
241 FALSE_RETURN_V_MSG_E(plugin_ != nullptr && pluginInfo_ != nullptr, false,
242 "can't configure codec when no plugin available");
243 auto thisMeta = std::make_shared<Plugin::Meta>();
244 FALSE_RETURN_V_MSG_E(MergeMetaWithCapability(*upstreamMeta, capNegWithDownstream_, *thisMeta), false,
245 "can't configure codec plugin since meta is not compatible with negotiated caps");
246 UpdateParams(upstreamMeta, thisMeta);
247
248 // HDI: must set width & height into hdi, hid use these params calc out buffer size & count then return to filter
249 if (ConfigPluginWithMeta(*plugin_, *thisMeta) != ErrorCode::SUCCESS) {
250 MEDIA_LOG_E("set params into plugin failed");
251 return false;
252 }
253 uint32_t bufferCnt = 0;
254 if (GetPluginParameterLocked(Tag::REQUIRED_OUT_BUFFER_CNT, bufferCnt) != ErrorCode::SUCCESS) {
255 bufferCnt = GetOutBufferPoolSize();
256 }
257 MEDIA_LOG_D("bufferCnt: " PUBLIC_LOG_U32, bufferCnt);
258
259 upstreamParams.Insert<Plugin::Tag::VIDEO_MAX_SURFACE_NUM>(bufferCnt);
260 auto targetOutPort = GetRouteOutPort(inPort);
261 if (targetOutPort == nullptr || !targetOutPort->Configure(thisMeta, upstreamParams, downstreamParams)) {
262 MEDIA_LOG_E("decoder filter downstream Configure failed");
263 return false;
264 }
265 sinkParams_ = downstreamParams;
266 uint32_t bufferSize = 0;
267 if (GetPluginParameterLocked(Tag::REQUIRED_OUT_BUFFER_SIZE, bufferSize) != ErrorCode::SUCCESS) {
268 bufferSize = CalculateBufferSize(thisMeta);
269 if (bufferSize == 0) {
270 bufferSize = MAX_OUT_DECODED_DATA_SIZE_PER_FRAME;
271 }
272 }
273 std::shared_ptr<Allocator> outAllocator = GetAllocator();
274 codecMode_->CreateOutBufferPool(outAllocator, bufferCnt, bufferSize, bufferMetaType_);
275 MEDIA_LOG_D("AllocateOutputBuffers success");
276 auto err = ConfigureToStartPluginLocked(thisMeta);
277 if (err != ErrorCode::SUCCESS) {
278 MEDIA_LOG_E("CodecFilterBase configure error");
279 OnEvent({name_, EventType::EVENT_ERROR, err});
280 return false;
281 }
282 state_ = FilterState::READY;
283 OnEvent({name_, EventType::EVENT_READY});
284 MEDIA_LOG_I("CodecFilterBase send EVENT_READY");
285 return true;
286 }
287
ConfigureToStartPluginLocked(const std::shared_ptr<const Plugin::Meta> & meta)288 ErrorCode CodecFilterBase::ConfigureToStartPluginLocked(const std::shared_ptr<const Plugin::Meta>& meta)
289 {
290 MEDIA_LOG_D("CodecFilterBase configure called");
291 FAIL_RETURN_MSG(TranslatePluginStatus(plugin_->SetCallback(this)), "plugin set callback fail");
292 FAIL_RETURN_MSG(TranslatePluginStatus(plugin_->SetDataCallback(this)), "plugin set data callback fail");
293 FAIL_RETURN_MSG(codecMode_->Configure(), "codec mode configure error");
294 return ErrorCode::SUCCESS;
295 }
296
FlushStart()297 void CodecFilterBase::FlushStart()
298 {
299 MEDIA_LOG_D("FlushStart entered.");
300 isFlushing_ = true;
301 if (plugin_ != nullptr) {
302 auto err = TranslatePluginStatus(plugin_->Flush());
303 if (err != ErrorCode::SUCCESS) {
304 MEDIA_LOG_E("codec plugin flush error");
305 }
306 }
307 MEDIA_LOG_D("FlushStart exit.");
308 }
309
FlushEnd()310 void CodecFilterBase::FlushEnd()
311 {
312 MEDIA_LOG_I("FlushEnd entered");
313 isFlushing_ = false;
314 }
315 } // namespace Pipeline
316 } // namespace Media
317 } // namespace OHOS
318