1 /*
2 * Copyright 2019 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.1"
19 #include <android-base/logging.h>
20
21 #include <codec2/hidl/1.1/ComponentStore.h>
22 #include <codec2/hidl/1.1/InputSurface.h>
23 #include <codec2/hidl/1.1/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 namespace android {
39 namespace hardware {
40 namespace media {
41 namespace c2 {
42 namespace V1_1 {
43 namespace utils {
44
45 using namespace ::android;
46 using ::android::GraphicBufferSource;
47 using namespace ::android::hardware::media::bufferpool::V2_0::implementation;
48
49 namespace /* unnamed */ {
50
51 struct StoreIntf : public ConfigurableC2Intf {
StoreIntfandroid::hardware::media::c2::V1_1::utils::__anon74e9db230111::StoreIntf52 StoreIntf(const std::shared_ptr<C2ComponentStore>& store)
53 : ConfigurableC2Intf{store ? store->getName() : "", 0},
54 mStore{store} {
55 }
56
configandroid::hardware::media::c2::V1_1::utils::__anon74e9db230111::StoreIntf57 virtual c2_status_t config(
58 const std::vector<C2Param*> ¶ms,
59 c2_blocking_t mayBlock,
60 std::vector<std::unique_ptr<C2SettingResult>> *const failures
61 ) override {
62 // Assume all params are blocking
63 // TODO: Filter for supported params
64 if (mayBlock == C2_DONT_BLOCK && params.size() != 0) {
65 return C2_BLOCKING;
66 }
67 return mStore->config_sm(params, failures);
68 }
69
queryandroid::hardware::media::c2::V1_1::utils::__anon74e9db230111::StoreIntf70 virtual c2_status_t query(
71 const std::vector<C2Param::Index> &indices,
72 c2_blocking_t mayBlock,
73 std::vector<std::unique_ptr<C2Param>> *const params) const override {
74 // Assume all params are blocking
75 // TODO: Filter for supported params
76 if (mayBlock == C2_DONT_BLOCK && indices.size() != 0) {
77 return C2_BLOCKING;
78 }
79 return mStore->query_sm({}, indices, params);
80 }
81
querySupportedParamsandroid::hardware::media::c2::V1_1::utils::__anon74e9db230111::StoreIntf82 virtual c2_status_t querySupportedParams(
83 std::vector<std::shared_ptr<C2ParamDescriptor>> *const params
84 ) const override {
85 return mStore->querySupportedParams_nb(params);
86 }
87
querySupportedValuesandroid::hardware::media::c2::V1_1::utils::__anon74e9db230111::StoreIntf88 virtual c2_status_t querySupportedValues(
89 std::vector<C2FieldSupportedValuesQuery> &fields,
90 c2_blocking_t mayBlock) const override {
91 // Assume all params are blocking
92 // TODO: Filter for supported params
93 if (mayBlock == C2_DONT_BLOCK && fields.size() != 0) {
94 return C2_BLOCKING;
95 }
96 return mStore->querySupportedValues_sm(fields);
97 }
98
99 protected:
100 std::shared_ptr<C2ComponentStore> mStore;
101 };
102
103 } // unnamed namespace
104
105 struct ComponentStore::StoreParameterCache : public ParameterCache {
106 std::mutex mStoreMutex;
107 ComponentStore* mStore;
108
StoreParameterCacheandroid::hardware::media::c2::V1_1::utils::ComponentStore::StoreParameterCache109 StoreParameterCache(ComponentStore* store): mStore{store} {
110 }
111
validateandroid::hardware::media::c2::V1_1::utils::ComponentStore::StoreParameterCache112 virtual c2_status_t validate(
113 const std::vector<std::shared_ptr<C2ParamDescriptor>>& params
114 ) override {
115 std::scoped_lock _lock(mStoreMutex);
116 return mStore ? mStore->validateSupportedParams(params) : C2_NO_INIT;
117 }
118
onStoreDestroyedandroid::hardware::media::c2::V1_1::utils::ComponentStore::StoreParameterCache119 void onStoreDestroyed() {
120 std::scoped_lock _lock(mStoreMutex);
121 mStore = nullptr;
122 }
123 };
124
ComponentStore(const std::shared_ptr<C2ComponentStore> & store)125 ComponentStore::ComponentStore(const std::shared_ptr<C2ComponentStore>& store)
126 : mConfigurable{new CachedConfigurable(std::make_unique<StoreIntf>(store))},
127 mParameterCache{std::make_shared<StoreParameterCache>(this)},
128 mStore{store} {
129
130 std::shared_ptr<C2ComponentStore> platformStore = android::GetCodec2PlatformComponentStore();
131 SetPreferredCodec2ComponentStore(store);
132
133 // Retrieve struct descriptors
134 mParamReflector = mStore->getParamReflector();
135
136 // Retrieve supported parameters from store
137 using namespace std::placeholders;
138 mInit = mConfigurable->init(mParameterCache);
139 }
140
~ComponentStore()141 ComponentStore::~ComponentStore() {
142 mParameterCache->onStoreDestroyed();
143 }
144
status() const145 c2_status_t ComponentStore::status() const {
146 return mInit;
147 }
148
validateSupportedParams(const std::vector<std::shared_ptr<C2ParamDescriptor>> & params)149 c2_status_t ComponentStore::validateSupportedParams(
150 const std::vector<std::shared_ptr<C2ParamDescriptor>>& params) {
151 c2_status_t res = C2_OK;
152
153 for (const std::shared_ptr<C2ParamDescriptor> &desc : params) {
154 if (!desc) {
155 // All descriptors should be valid
156 res = res ? res : C2_BAD_VALUE;
157 continue;
158 }
159 C2Param::CoreIndex coreIndex = desc->index().coreIndex();
160 std::lock_guard<std::mutex> lock(mStructDescriptorsMutex);
161 auto it = mStructDescriptors.find(coreIndex);
162 if (it == mStructDescriptors.end()) {
163 std::shared_ptr<C2StructDescriptor> structDesc =
164 mParamReflector->describe(coreIndex);
165 if (!structDesc) {
166 // All supported params must be described
167 res = C2_BAD_INDEX;
168 }
169 mStructDescriptors.insert({ coreIndex, structDesc });
170 }
171 }
172 return res;
173 }
174
getParameterCache() const175 std::shared_ptr<ParameterCache> ComponentStore::getParameterCache() const {
176 return mParameterCache;
177 }
178
179 // 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)180 Return<void> ComponentStore::createComponent(
181 const hidl_string& name,
182 const sp<IComponentListener>& listener,
183 const sp<IClientManager>& pool,
184 createComponent_cb _hidl_cb) {
185
186 sp<Component> component;
187 std::shared_ptr<C2Component> c2component;
188 Status status = static_cast<Status>(
189 mStore->createComponent(name, &c2component));
190
191 if (status == Status::OK) {
192 onInterfaceLoaded(c2component->intf());
193 component = new Component(c2component, listener, this, pool);
194 if (!component) {
195 status = Status::CORRUPTED;
196 } else {
197 reportComponentBirth(component.get());
198 if (component->status() != C2_OK) {
199 status = static_cast<Status>(component->status());
200 } else {
201 component->initListener(component);
202 if (component->status() != C2_OK) {
203 status = static_cast<Status>(component->status());
204 }
205 }
206 }
207 }
208 _hidl_cb(status, component);
209 return Void();
210 }
211
createInterface(const hidl_string & name,createInterface_cb _hidl_cb)212 Return<void> ComponentStore::createInterface(
213 const hidl_string& name,
214 createInterface_cb _hidl_cb) {
215 std::shared_ptr<C2ComponentInterface> c2interface;
216 c2_status_t res = mStore->createInterface(name, &c2interface);
217 sp<IComponentInterface> interface;
218 if (res == C2_OK) {
219 onInterfaceLoaded(c2interface);
220 interface = new ComponentInterface(c2interface, mParameterCache);
221 }
222 _hidl_cb(static_cast<Status>(res), interface);
223 return Void();
224 }
225
listComponents(listComponents_cb _hidl_cb)226 Return<void> ComponentStore::listComponents(listComponents_cb _hidl_cb) {
227 std::vector<std::shared_ptr<const C2Component::Traits>> c2traits =
228 mStore->listComponents();
229 hidl_vec<IComponentStore::ComponentTraits> traits(c2traits.size());
230 size_t ix = 0;
231 for (const std::shared_ptr<const C2Component::Traits> &c2trait : c2traits) {
232 if (c2trait) {
233 if (objcpy(&traits[ix], *c2trait)) {
234 ++ix;
235 } else {
236 break;
237 }
238 }
239 }
240 traits.resize(ix);
241 _hidl_cb(Status::OK, traits);
242 return Void();
243 }
244
createInputSurface(createInputSurface_cb _hidl_cb)245 Return<void> ComponentStore::createInputSurface(createInputSurface_cb _hidl_cb) {
246 sp<GraphicBufferSource> source = new GraphicBufferSource();
247 if (source->initCheck() != OK) {
248 _hidl_cb(Status::CORRUPTED, nullptr);
249 return Void();
250 }
251 using namespace std::placeholders;
252 sp<InputSurface> inputSurface = new InputSurface(
253 mParameterCache,
254 std::make_shared<C2ReflectorHelper>(),
255 source->getHGraphicBufferProducer(),
256 source);
257 _hidl_cb(inputSurface ? Status::OK : Status::NO_MEMORY,
258 inputSurface);
259 return Void();
260 }
261
onInterfaceLoaded(const std::shared_ptr<C2ComponentInterface> & intf)262 void ComponentStore::onInterfaceLoaded(const std::shared_ptr<C2ComponentInterface> &intf) {
263 // invalidate unsupported struct descriptors if a new interface is loaded as it may have
264 // exposed new descriptors
265 std::lock_guard<std::mutex> lock(mStructDescriptorsMutex);
266 if (!mLoadedInterfaces.count(intf->getName())) {
267 mUnsupportedStructDescriptors.clear();
268 mLoadedInterfaces.emplace(intf->getName());
269 }
270 }
271
getStructDescriptors(const hidl_vec<uint32_t> & indices,getStructDescriptors_cb _hidl_cb)272 Return<void> ComponentStore::getStructDescriptors(
273 const hidl_vec<uint32_t>& indices,
274 getStructDescriptors_cb _hidl_cb) {
275 hidl_vec<StructDescriptor> descriptors(indices.size());
276 size_t dstIx = 0;
277 Status res = Status::OK;
278 for (size_t srcIx = 0; srcIx < indices.size(); ++srcIx) {
279 std::lock_guard<std::mutex> lock(mStructDescriptorsMutex);
280 const C2Param::CoreIndex coreIndex = C2Param::CoreIndex(indices[srcIx]).coreIndex();
281 const auto item = mStructDescriptors.find(coreIndex);
282 if (item == mStructDescriptors.end()) {
283 // not in the cache, and not known to be unsupported, query local reflector
284 if (!mUnsupportedStructDescriptors.count(coreIndex)) {
285 std::shared_ptr<C2StructDescriptor> structDesc =
286 mParamReflector->describe(coreIndex);
287 if (!structDesc) {
288 mUnsupportedStructDescriptors.emplace(coreIndex);
289 } else {
290 mStructDescriptors.insert({ coreIndex, structDesc });
291 if (objcpy(&descriptors[dstIx], *structDesc)) {
292 ++dstIx;
293 continue;
294 }
295 res = Status::CORRUPTED;
296 break;
297 }
298 }
299 res = Status::NOT_FOUND;
300 } else if (item->second) {
301 if (objcpy(&descriptors[dstIx], *item->second)) {
302 ++dstIx;
303 continue;
304 }
305 res = Status::CORRUPTED;
306 break;
307 } else {
308 res = Status::NO_MEMORY;
309 break;
310 }
311 }
312 descriptors.resize(dstIx);
313 _hidl_cb(res, descriptors);
314 return Void();
315 }
316
getPoolClientManager()317 Return<sp<IClientManager>> ComponentStore::getPoolClientManager() {
318 return ClientManager::getInstance();
319 }
320
copyBuffer(const Buffer & src,const Buffer & dst)321 Return<Status> ComponentStore::copyBuffer(const Buffer& src, const Buffer& dst) {
322 // TODO implement
323 (void)src;
324 (void)dst;
325 return Status::OMITTED;
326 }
327
getConfigurable()328 Return<sp<IConfigurable>> ComponentStore::getConfigurable() {
329 return mConfigurable;
330 }
331
332 // 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)333 Return<void> ComponentStore::createComponent_1_1(
334 const hidl_string& name,
335 const sp<IComponentListener>& listener,
336 const sp<IClientManager>& pool,
337 createComponent_1_1_cb _hidl_cb) {
338
339 sp<Component> component;
340 std::shared_ptr<C2Component> c2component;
341 Status status = static_cast<Status>(
342 mStore->createComponent(name, &c2component));
343
344 if (status == Status::OK) {
345 onInterfaceLoaded(c2component->intf());
346 component = new Component(c2component, listener, this, pool);
347 if (!component) {
348 status = Status::CORRUPTED;
349 } else {
350 reportComponentBirth(component.get());
351 if (component->status() != C2_OK) {
352 status = static_cast<Status>(component->status());
353 } else {
354 component->initListener(component);
355 if (component->status() != C2_OK) {
356 status = static_cast<Status>(component->status());
357 }
358 }
359 }
360 }
361 _hidl_cb(status, component);
362 return Void();
363 }
364
365 // Called from createComponent() after a successful creation of `component`.
reportComponentBirth(Component * component)366 void ComponentStore::reportComponentBirth(Component* component) {
367 ComponentStatus componentStatus;
368 componentStatus.c2Component = component->mComponent;
369 componentStatus.birthTime = std::chrono::system_clock::now();
370
371 std::lock_guard<std::mutex> lock(mComponentRosterMutex);
372 mComponentRoster.emplace(component, componentStatus);
373 }
374
375 // Called from within the destructor of `component`. No virtual function calls
376 // are made on `component` here.
reportComponentDeath(Component * component)377 void ComponentStore::reportComponentDeath(Component* component) {
378 std::lock_guard<std::mutex> lock(mComponentRosterMutex);
379 mComponentRoster.erase(component);
380 }
381
382 // Dumps component traits.
dump(std::ostream & out,const std::shared_ptr<const C2Component::Traits> & comp)383 std::ostream& ComponentStore::dump(
384 std::ostream& out,
385 const std::shared_ptr<const C2Component::Traits>& comp) {
386
387 constexpr const char indent[] = " ";
388
389 out << indent << "name: " << comp->name << std::endl;
390 out << indent << "domain: " << comp->domain << std::endl;
391 out << indent << "kind: " << comp->kind << std::endl;
392 out << indent << "rank: " << comp->rank << std::endl;
393 out << indent << "mediaType: " << comp->mediaType << std::endl;
394 out << indent << "aliases:";
395 for (const auto& alias : comp->aliases) {
396 out << ' ' << alias;
397 }
398 out << std::endl;
399
400 return out;
401 }
402
403 // Dumps component status.
dump(std::ostream & out,ComponentStatus & compStatus)404 std::ostream& ComponentStore::dump(
405 std::ostream& out,
406 ComponentStatus& compStatus) {
407
408 constexpr const char indent[] = " ";
409
410 // Print birth time.
411 std::chrono::milliseconds ms =
412 std::chrono::duration_cast<std::chrono::milliseconds>(
413 compStatus.birthTime.time_since_epoch());
414 std::time_t birthTime = std::chrono::system_clock::to_time_t(
415 compStatus.birthTime);
416 std::tm tm = *std::localtime(&birthTime);
417 out << indent << "Creation time: "
418 << std::put_time(&tm, "%Y-%m-%d %H:%M:%S")
419 << '.' << std::setfill('0') << std::setw(3) << ms.count() % 1000
420 << std::endl;
421
422 // Print name and id.
423 std::shared_ptr<C2ComponentInterface> intf = compStatus.c2Component->intf();
424 if (!intf) {
425 out << indent << "Unknown component -- null interface" << std::endl;
426 return out;
427 }
428 out << indent << "Name: " << intf->getName() << std::endl;
429 out << indent << "Id: " << intf->getId() << std::endl;
430
431 return out;
432 }
433
434 // Dumps information when lshal is called.
debug(const hidl_handle & handle,const hidl_vec<hidl_string> &)435 Return<void> ComponentStore::debug(
436 const hidl_handle& handle,
437 const hidl_vec<hidl_string>& /* args */) {
438 LOG(INFO) << "debug -- dumping...";
439 const native_handle_t *h = handle.getNativeHandle();
440 if (!h || h->numFds != 1) {
441 LOG(ERROR) << "debug -- dumping failed -- "
442 "invalid file descriptor to dump to";
443 return Void();
444 }
445 std::ostringstream out;
446
447 { // Populate "out".
448
449 constexpr const char indent[] = " ";
450
451 // Show name.
452 out << "Beginning of dump -- C2ComponentStore: "
453 << mStore->getName() << std::endl << std::endl;
454
455 // Retrieve the list of supported components.
456 std::vector<std::shared_ptr<const C2Component::Traits>> traitsList =
457 mStore->listComponents();
458
459 // Dump the traits of supported components.
460 out << indent << "Supported components:" << std::endl << std::endl;
461 if (traitsList.size() == 0) {
462 out << indent << indent << "NONE" << std::endl << std::endl;
463 } else {
464 for (const auto& traits : traitsList) {
465 dump(out, traits) << std::endl;
466 }
467 }
468
469 // Dump active components.
470 {
471 out << indent << "Active components:" << std::endl << std::endl;
472 std::lock_guard<std::mutex> lock(mComponentRosterMutex);
473 if (mComponentRoster.size() == 0) {
474 out << indent << indent << "NONE" << std::endl << std::endl;
475 } else {
476 for (auto& pair : mComponentRoster) {
477 dump(out, pair.second) << std::endl;
478 }
479 }
480 }
481
482 out << "End of dump -- C2ComponentStore: "
483 << mStore->getName() << std::endl;
484 }
485
486 if (!android::base::WriteStringToFd(out.str(), h->data[0])) {
487 PLOG(WARNING) << "debug -- dumping failed -- write()";
488 } else {
489 LOG(INFO) << "debug -- dumping succeeded";
490 }
491 return Void();
492 }
493
494 } // namespace utils
495 } // namespace V1_1
496 } // namespace c2
497 } // namespace media
498 } // namespace hardware
499 } // namespace android
500