1 /*
2 * Copyright 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "Codec2-ComponentStore@1.2"
19 #include <android-base/logging.h>
20
21 #include <codec2/hidl/1.2/ComponentStore.h>
22 #include <codec2/hidl/1.2/InputSurface.h>
23 #include <codec2/hidl/1.2/types.h>
24
25 #include <android-base/file.h>
26 #include <media/stagefright/bqhelper/GraphicBufferSource.h>
27 #include <utils/Errors.h>
28
29 #include <C2PlatformSupport.h>
30 #include <util/C2InterfaceHelper.h>
31
32 #include <chrono>
33 #include <ctime>
34 #include <iomanip>
35 #include <ostream>
36 #include <sstream>
37
38 #ifndef __ANDROID_APEX__
39 #include <codec2/hidl/plugin/FilterPlugin.h>
40 #include <dlfcn.h>
41 #include <C2Config.h>
42 #include <DefaultFilterPlugin.h>
43 #include <FilterWrapper.h>
44 #endif
45
46 namespace android {
47 namespace hardware {
48 namespace media {
49 namespace c2 {
50 namespace V1_2 {
51 namespace utils {
52
53 using namespace ::android;
54 using ::android::GraphicBufferSource;
55 using namespace ::android::hardware::media::bufferpool::V2_0::implementation;
56
57 namespace /* unnamed */ {
58
59 struct StoreIntf : public ConfigurableC2Intf {
StoreIntfandroid::hardware::media::c2::V1_2::utils::__anonec2d7a300110::StoreIntf60 StoreIntf(const std::shared_ptr<C2ComponentStore>& store)
61 : ConfigurableC2Intf{store ? store->getName() : "", 0},
62 mStore{store} {
63 }
64
configandroid::hardware::media::c2::V1_2::utils::__anonec2d7a300110::StoreIntf65 virtual c2_status_t config(
66 const std::vector<C2Param*> ¶ms,
67 c2_blocking_t mayBlock,
68 std::vector<std::unique_ptr<C2SettingResult>> *const failures
69 ) override {
70 // Assume all params are blocking
71 // TODO: Filter for supported params
72 if (mayBlock == C2_DONT_BLOCK && params.size() != 0) {
73 return C2_BLOCKING;
74 }
75 return mStore->config_sm(params, failures);
76 }
77
queryandroid::hardware::media::c2::V1_2::utils::__anonec2d7a300110::StoreIntf78 virtual c2_status_t query(
79 const std::vector<C2Param::Index> &indices,
80 c2_blocking_t mayBlock,
81 std::vector<std::unique_ptr<C2Param>> *const params) const override {
82 // Assume all params are blocking
83 // TODO: Filter for supported params
84 if (mayBlock == C2_DONT_BLOCK && indices.size() != 0) {
85 return C2_BLOCKING;
86 }
87 return mStore->query_sm({}, indices, params);
88 }
89
querySupportedParamsandroid::hardware::media::c2::V1_2::utils::__anonec2d7a300110::StoreIntf90 virtual c2_status_t querySupportedParams(
91 std::vector<std::shared_ptr<C2ParamDescriptor>> *const params
92 ) const override {
93 return mStore->querySupportedParams_nb(params);
94 }
95
querySupportedValuesandroid::hardware::media::c2::V1_2::utils::__anonec2d7a300110::StoreIntf96 virtual c2_status_t querySupportedValues(
97 std::vector<C2FieldSupportedValuesQuery> &fields,
98 c2_blocking_t mayBlock) const override {
99 // Assume all params are blocking
100 // TODO: Filter for supported params
101 if (mayBlock == C2_DONT_BLOCK && fields.size() != 0) {
102 return C2_BLOCKING;
103 }
104 return mStore->querySupportedValues_sm(fields);
105 }
106
107 protected:
108 std::shared_ptr<C2ComponentStore> mStore;
109 };
110
111 } // unnamed namespace
112
113 struct ComponentStore::StoreParameterCache : public ParameterCache {
114 std::mutex mStoreMutex;
115 ComponentStore* mStore;
116
StoreParameterCacheandroid::hardware::media::c2::V1_2::utils::ComponentStore::StoreParameterCache117 StoreParameterCache(ComponentStore* store): mStore{store} {
118 }
119
validateandroid::hardware::media::c2::V1_2::utils::ComponentStore::StoreParameterCache120 virtual c2_status_t validate(
121 const std::vector<std::shared_ptr<C2ParamDescriptor>>& params
122 ) override {
123 std::scoped_lock _lock(mStoreMutex);
124 return mStore ? mStore->validateSupportedParams(params) : C2_NO_INIT;
125 }
126
onStoreDestroyedandroid::hardware::media::c2::V1_2::utils::ComponentStore::StoreParameterCache127 void onStoreDestroyed() {
128 std::scoped_lock _lock(mStoreMutex);
129 mStore = nullptr;
130 }
131 };
132
ComponentStore(const std::shared_ptr<C2ComponentStore> & store)133 ComponentStore::ComponentStore(const std::shared_ptr<C2ComponentStore>& store)
134 : mConfigurable{new CachedConfigurable(std::make_unique<StoreIntf>(store))},
135 mParameterCache{std::make_shared<StoreParameterCache>(this)},
136 mStore{store} {
137
138 std::shared_ptr<C2ComponentStore> platformStore = android::GetCodec2PlatformComponentStore();
139 SetPreferredCodec2ComponentStore(store);
140
141 // Retrieve struct descriptors
142 mParamReflector = mStore->getParamReflector();
143
144 // Retrieve supported parameters from store
145 using namespace std::placeholders;
146 mInit = mConfigurable->init(mParameterCache);
147 }
148
~ComponentStore()149 ComponentStore::~ComponentStore() {
150 mParameterCache->onStoreDestroyed();
151 }
152
status() const153 c2_status_t ComponentStore::status() const {
154 return mInit;
155 }
156
validateSupportedParams(const std::vector<std::shared_ptr<C2ParamDescriptor>> & params)157 c2_status_t ComponentStore::validateSupportedParams(
158 const std::vector<std::shared_ptr<C2ParamDescriptor>>& params) {
159 c2_status_t res = C2_OK;
160
161 for (const std::shared_ptr<C2ParamDescriptor> &desc : params) {
162 if (!desc) {
163 // All descriptors should be valid
164 res = res ? res : C2_BAD_VALUE;
165 continue;
166 }
167 C2Param::CoreIndex coreIndex = desc->index().coreIndex();
168 std::lock_guard<std::mutex> lock(mStructDescriptorsMutex);
169 auto it = mStructDescriptors.find(coreIndex);
170 if (it == mStructDescriptors.end()) {
171 std::shared_ptr<C2StructDescriptor> structDesc =
172 mParamReflector->describe(coreIndex);
173 if (!structDesc) {
174 // All supported params must be described
175 res = C2_BAD_INDEX;
176 }
177 mStructDescriptors.insert({ coreIndex, structDesc });
178 }
179 }
180 return res;
181 }
182
getParameterCache() const183 std::shared_ptr<ParameterCache> ComponentStore::getParameterCache() const {
184 return mParameterCache;
185 }
186
187 #ifndef __ANDROID_APEX__
188 // static
GetFilterWrapper()189 std::shared_ptr<FilterWrapper> ComponentStore::GetFilterWrapper() {
190 constexpr const char kPluginPath[] = "libc2filterplugin.so";
191 static std::shared_ptr<FilterWrapper> wrapper = FilterWrapper::Create(
192 std::make_unique<DefaultFilterPlugin>(kPluginPath));
193 return wrapper;
194 }
195 #endif
196
197 // Methods from ::android::hardware::media::c2::V1_0::IComponentStore
createComponent(const hidl_string & name,const sp<IComponentListener> & listener,const sp<IClientManager> & pool,createComponent_cb _hidl_cb)198 Return<void> ComponentStore::createComponent(
199 const hidl_string& name,
200 const sp<IComponentListener>& listener,
201 const sp<IClientManager>& pool,
202 createComponent_cb _hidl_cb) {
203
204 sp<Component> component;
205 std::shared_ptr<C2Component> c2component;
206 Status status = static_cast<Status>(
207 mStore->createComponent(name, &c2component));
208
209 if (status == Status::OK) {
210 #ifndef __ANDROID_APEX__
211 c2component = GetFilterWrapper()->maybeWrapComponent(c2component);
212 #endif
213 onInterfaceLoaded(c2component->intf());
214 component = new Component(c2component, listener, this, pool);
215 if (!component) {
216 status = Status::CORRUPTED;
217 } else {
218 reportComponentBirth(component.get());
219 if (component->status() != C2_OK) {
220 status = static_cast<Status>(component->status());
221 } else {
222 component->initListener(component);
223 if (component->status() != C2_OK) {
224 status = static_cast<Status>(component->status());
225 }
226 }
227 }
228 }
229 _hidl_cb(status, component);
230 return Void();
231 }
232
createInterface(const hidl_string & name,createInterface_cb _hidl_cb)233 Return<void> ComponentStore::createInterface(
234 const hidl_string& name,
235 createInterface_cb _hidl_cb) {
236 std::shared_ptr<C2ComponentInterface> c2interface;
237 c2_status_t res = mStore->createInterface(name, &c2interface);
238 sp<IComponentInterface> interface;
239 if (res == C2_OK) {
240 #ifndef __ANDROID_APEX__
241 c2interface = GetFilterWrapper()->maybeWrapInterface(c2interface);
242 #endif
243 onInterfaceLoaded(c2interface);
244 interface = new ComponentInterface(c2interface, mParameterCache);
245 }
246 _hidl_cb(static_cast<Status>(res), interface);
247 return Void();
248 }
249
listComponents(listComponents_cb _hidl_cb)250 Return<void> ComponentStore::listComponents(listComponents_cb _hidl_cb) {
251 std::vector<std::shared_ptr<const C2Component::Traits>> c2traits =
252 mStore->listComponents();
253 hidl_vec<IComponentStore::ComponentTraits> traits(c2traits.size());
254 size_t ix = 0;
255 for (const std::shared_ptr<const C2Component::Traits> &c2trait : c2traits) {
256 if (c2trait) {
257 if (objcpy(&traits[ix], *c2trait)) {
258 ++ix;
259 } else {
260 break;
261 }
262 }
263 }
264 traits.resize(ix);
265 _hidl_cb(Status::OK, traits);
266 return Void();
267 }
268
createInputSurface(createInputSurface_cb _hidl_cb)269 Return<void> ComponentStore::createInputSurface(createInputSurface_cb _hidl_cb) {
270 sp<GraphicBufferSource> source = new GraphicBufferSource();
271 if (source->initCheck() != OK) {
272 _hidl_cb(Status::CORRUPTED, nullptr);
273 return Void();
274 }
275 using namespace std::placeholders;
276 sp<InputSurface> inputSurface = new InputSurface(
277 mParameterCache,
278 std::make_shared<C2ReflectorHelper>(),
279 source->getHGraphicBufferProducer(),
280 source);
281 _hidl_cb(inputSurface ? Status::OK : Status::NO_MEMORY,
282 inputSurface);
283 return Void();
284 }
285
onInterfaceLoaded(const std::shared_ptr<C2ComponentInterface> & intf)286 void ComponentStore::onInterfaceLoaded(const std::shared_ptr<C2ComponentInterface> &intf) {
287 // invalidate unsupported struct descriptors if a new interface is loaded as it may have
288 // exposed new descriptors
289 std::lock_guard<std::mutex> lock(mStructDescriptorsMutex);
290 if (!mLoadedInterfaces.count(intf->getName())) {
291 mUnsupportedStructDescriptors.clear();
292 mLoadedInterfaces.emplace(intf->getName());
293 }
294 }
295
getStructDescriptors(const hidl_vec<uint32_t> & indices,getStructDescriptors_cb _hidl_cb)296 Return<void> ComponentStore::getStructDescriptors(
297 const hidl_vec<uint32_t>& indices,
298 getStructDescriptors_cb _hidl_cb) {
299 hidl_vec<StructDescriptor> descriptors(indices.size());
300 size_t dstIx = 0;
301 Status res = Status::OK;
302 for (size_t srcIx = 0; srcIx < indices.size(); ++srcIx) {
303 std::lock_guard<std::mutex> lock(mStructDescriptorsMutex);
304 const C2Param::CoreIndex coreIndex = C2Param::CoreIndex(indices[srcIx]).coreIndex();
305 const auto item = mStructDescriptors.find(coreIndex);
306 if (item == mStructDescriptors.end()) {
307 // not in the cache, and not known to be unsupported, query local reflector
308 if (!mUnsupportedStructDescriptors.count(coreIndex)) {
309 std::shared_ptr<C2StructDescriptor> structDesc =
310 mParamReflector->describe(coreIndex);
311 if (!structDesc) {
312 mUnsupportedStructDescriptors.emplace(coreIndex);
313 } else {
314 mStructDescriptors.insert({ coreIndex, structDesc });
315 if (objcpy(&descriptors[dstIx], *structDesc)) {
316 ++dstIx;
317 continue;
318 }
319 res = Status::CORRUPTED;
320 break;
321 }
322 }
323 res = Status::NOT_FOUND;
324 } else if (item->second) {
325 if (objcpy(&descriptors[dstIx], *item->second)) {
326 ++dstIx;
327 continue;
328 }
329 res = Status::CORRUPTED;
330 break;
331 } else {
332 res = Status::NO_MEMORY;
333 break;
334 }
335 }
336 descriptors.resize(dstIx);
337 _hidl_cb(res, descriptors);
338 return Void();
339 }
340
getPoolClientManager()341 Return<sp<IClientManager>> ComponentStore::getPoolClientManager() {
342 return ClientManager::getInstance();
343 }
344
copyBuffer(const Buffer & src,const Buffer & dst)345 Return<Status> ComponentStore::copyBuffer(const Buffer& src, const Buffer& dst) {
346 // TODO implement
347 (void)src;
348 (void)dst;
349 return Status::OMITTED;
350 }
351
getConfigurable()352 Return<sp<IConfigurable>> ComponentStore::getConfigurable() {
353 return mConfigurable;
354 }
355
356 // Methods from ::android::hardware::media::c2::V1_1::IComponentStore
createComponent_1_1(const hidl_string & name,const sp<IComponentListener> & listener,const sp<IClientManager> & pool,createComponent_1_1_cb _hidl_cb)357 Return<void> ComponentStore::createComponent_1_1(
358 const hidl_string& name,
359 const sp<IComponentListener>& listener,
360 const sp<IClientManager>& pool,
361 createComponent_1_1_cb _hidl_cb) {
362
363 sp<Component> component;
364 std::shared_ptr<C2Component> c2component;
365 Status status = static_cast<Status>(
366 mStore->createComponent(name, &c2component));
367
368 if (status == Status::OK) {
369 #ifndef __ANDROID_APEX__
370 c2component = GetFilterWrapper()->maybeWrapComponent(c2component);
371 #endif
372 onInterfaceLoaded(c2component->intf());
373 component = new Component(c2component, listener, this, pool);
374 if (!component) {
375 status = Status::CORRUPTED;
376 } else {
377 reportComponentBirth(component.get());
378 if (component->status() != C2_OK) {
379 status = static_cast<Status>(component->status());
380 } else {
381 component->initListener(component);
382 if (component->status() != C2_OK) {
383 status = static_cast<Status>(component->status());
384 }
385 }
386 }
387 }
388 _hidl_cb(status, component);
389 return Void();
390 }
391
392 // Methods from ::android::hardware::media::c2::V1_2::IComponentStore
createComponent_1_2(const hidl_string & name,const sp<IComponentListener> & listener,const sp<IClientManager> & pool,createComponent_1_2_cb _hidl_cb)393 Return<void> ComponentStore::createComponent_1_2(
394 const hidl_string& name,
395 const sp<IComponentListener>& listener,
396 const sp<IClientManager>& pool,
397 createComponent_1_2_cb _hidl_cb) {
398
399 sp<Component> component;
400 std::shared_ptr<C2Component> c2component;
401 Status status = static_cast<Status>(
402 mStore->createComponent(name, &c2component));
403
404 if (status == Status::OK) {
405 #ifndef __ANDROID_APEX__
406 c2component = GetFilterWrapper()->maybeWrapComponent(c2component);
407 #endif
408 onInterfaceLoaded(c2component->intf());
409 component = new Component(c2component, listener, this, pool);
410 if (!component) {
411 status = Status::CORRUPTED;
412 } else {
413 reportComponentBirth(component.get());
414 if (component->status() != C2_OK) {
415 status = static_cast<Status>(component->status());
416 } else {
417 component->initListener(component);
418 if (component->status() != C2_OK) {
419 status = static_cast<Status>(component->status());
420 }
421 }
422 }
423 }
424 _hidl_cb(status, component);
425 return Void();
426 }
427
428 // Called from createComponent() after a successful creation of `component`.
reportComponentBirth(Component * component)429 void ComponentStore::reportComponentBirth(Component* component) {
430 ComponentStatus componentStatus;
431 componentStatus.c2Component = component->mComponent;
432 componentStatus.birthTime = std::chrono::system_clock::now();
433
434 std::lock_guard<std::mutex> lock(mComponentRosterMutex);
435 mComponentRoster.emplace(component, componentStatus);
436 }
437
438 // Called from within the destructor of `component`. No virtual function calls
439 // are made on `component` here.
reportComponentDeath(Component * component)440 void ComponentStore::reportComponentDeath(Component* component) {
441 std::lock_guard<std::mutex> lock(mComponentRosterMutex);
442 mComponentRoster.erase(component);
443 }
444
445 // Dumps component traits.
dump(std::ostream & out,const std::shared_ptr<const C2Component::Traits> & comp)446 std::ostream& ComponentStore::dump(
447 std::ostream& out,
448 const std::shared_ptr<const C2Component::Traits>& comp) {
449
450 constexpr const char indent[] = " ";
451
452 out << indent << "name: " << comp->name << std::endl;
453 out << indent << "domain: " << comp->domain << std::endl;
454 out << indent << "kind: " << comp->kind << std::endl;
455 out << indent << "rank: " << comp->rank << std::endl;
456 out << indent << "mediaType: " << comp->mediaType << std::endl;
457 out << indent << "aliases:";
458 for (const auto& alias : comp->aliases) {
459 out << ' ' << alias;
460 }
461 out << std::endl;
462
463 return out;
464 }
465
466 // Dumps component status.
dump(std::ostream & out,ComponentStatus & compStatus)467 std::ostream& ComponentStore::dump(
468 std::ostream& out,
469 ComponentStatus& compStatus) {
470
471 constexpr const char indent[] = " ";
472
473 // Print birth time.
474 std::chrono::milliseconds ms =
475 std::chrono::duration_cast<std::chrono::milliseconds>(
476 compStatus.birthTime.time_since_epoch());
477 std::time_t birthTime = std::chrono::system_clock::to_time_t(
478 compStatus.birthTime);
479 std::tm tm = *std::localtime(&birthTime);
480 out << indent << "Creation time: "
481 << std::put_time(&tm, "%Y-%m-%d %H:%M:%S")
482 << '.' << std::setfill('0') << std::setw(3) << ms.count() % 1000
483 << std::endl;
484
485 // Print name and id.
486 std::shared_ptr<C2ComponentInterface> intf = compStatus.c2Component->intf();
487 if (!intf) {
488 out << indent << "Unknown component -- null interface" << std::endl;
489 return out;
490 }
491 out << indent << "Name: " << intf->getName() << std::endl;
492 out << indent << "Id: " << intf->getId() << std::endl;
493
494 return out;
495 }
496
497 // Dumps information when lshal is called.
debug(const hidl_handle & handle,const hidl_vec<hidl_string> &)498 Return<void> ComponentStore::debug(
499 const hidl_handle& handle,
500 const hidl_vec<hidl_string>& /* args */) {
501 LOG(INFO) << "debug -- dumping...";
502 const native_handle_t *h = handle.getNativeHandle();
503 if (!h || h->numFds != 1) {
504 LOG(ERROR) << "debug -- dumping failed -- "
505 "invalid file descriptor to dump to";
506 return Void();
507 }
508 std::ostringstream out;
509
510 { // Populate "out".
511
512 constexpr const char indent[] = " ";
513
514 // Show name.
515 out << "Beginning of dump -- C2ComponentStore: "
516 << mStore->getName() << std::endl << std::endl;
517
518 // Retrieve the list of supported components.
519 std::vector<std::shared_ptr<const C2Component::Traits>> traitsList =
520 mStore->listComponents();
521
522 // Dump the traits of supported components.
523 out << indent << "Supported components:" << std::endl << std::endl;
524 if (traitsList.size() == 0) {
525 out << indent << indent << "NONE" << std::endl << std::endl;
526 } else {
527 for (const auto& traits : traitsList) {
528 dump(out, traits) << std::endl;
529 }
530 }
531
532 // Dump active components.
533 {
534 out << indent << "Active components:" << std::endl << std::endl;
535 std::lock_guard<std::mutex> lock(mComponentRosterMutex);
536 if (mComponentRoster.size() == 0) {
537 out << indent << indent << "NONE" << std::endl << std::endl;
538 } else {
539 for (auto& pair : mComponentRoster) {
540 dump(out, pair.second) << std::endl;
541 }
542 }
543 }
544
545 out << "End of dump -- C2ComponentStore: "
546 << mStore->getName() << std::endl;
547 }
548
549 if (!android::base::WriteStringToFd(out.str(), h->data[0])) {
550 PLOG(WARNING) << "debug -- dumping failed -- write()";
551 } else {
552 LOG(INFO) << "debug -- dumping succeeded";
553 }
554 return Void();
555 }
556
557 } // namespace utils
558 } // namespace V1_2
559 } // namespace c2
560 } // namespace media
561 } // namespace hardware
562 } // namespace android
563