• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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"
19 #include <android-base/logging.h>
20 
21 #include <codec2/hidl/1.0/ComponentStore.h>
22 #include <codec2/hidl/1.0/InputSurface.h>
23 #include <codec2/hidl/1.0/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_0 {
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_0::utils::__anon5620fcba0111::StoreIntf60     StoreIntf(const std::shared_ptr<C2ComponentStore>& store)
61           : ConfigurableC2Intf{store ? store->getName() : "", 0},
62             mStore{store} {
63     }
64 
configandroid::hardware::media::c2::V1_0::utils::__anon5620fcba0111::StoreIntf65     virtual c2_status_t config(
66             const std::vector<C2Param*> &params,
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_0::utils::__anon5620fcba0111::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_0::utils::__anon5620fcba0111::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_0::utils::__anon5620fcba0111::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_0::utils::ComponentStore::StoreParameterCache117     StoreParameterCache(ComponentStore* store): mStore{store} {
118     }
119 
validateandroid::hardware::media::c2::V1_0::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_0::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     mParamReflectors.push_back(mStore->getParamReflector());
143 #ifndef __ANDROID_APEX__
144     std::shared_ptr<C2ParamReflector> paramReflector =
145         GetFilterWrapper()->getParamReflector();
146     if (paramReflector != nullptr) {
147         ALOGD("[%s] added param reflector from filter wrapper", mStore->getName().c_str());
148         mParamReflectors.push_back(paramReflector);
149     }
150 #endif
151 
152     // Retrieve supported parameters from store
153     using namespace std::placeholders;
154     mInit = mConfigurable->init(mParameterCache);
155 }
156 
~ComponentStore()157 ComponentStore::~ComponentStore() {
158     mParameterCache->onStoreDestroyed();
159 }
160 
status() const161 c2_status_t ComponentStore::status() const {
162     return mInit;
163 }
164 
validateSupportedParams(const std::vector<std::shared_ptr<C2ParamDescriptor>> & params)165 c2_status_t ComponentStore::validateSupportedParams(
166         const std::vector<std::shared_ptr<C2ParamDescriptor>>& params) {
167     c2_status_t res = C2_OK;
168 
169     for (const std::shared_ptr<C2ParamDescriptor> &desc : params) {
170         if (!desc) {
171             // All descriptors should be valid
172             res = res ? res : C2_BAD_VALUE;
173             continue;
174         }
175         C2Param::CoreIndex coreIndex = desc->index().coreIndex();
176         std::lock_guard<std::mutex> lock(mStructDescriptorsMutex);
177         auto it = mStructDescriptors.find(coreIndex);
178         if (it == mStructDescriptors.end()) {
179             std::shared_ptr<C2StructDescriptor> structDesc = describe(coreIndex);
180             if (!structDesc) {
181                 // All supported params must be described
182                 res = C2_BAD_INDEX;
183             }
184             mStructDescriptors.insert({ coreIndex, structDesc });
185         }
186     }
187     return res;
188 }
189 
getParameterCache() const190 std::shared_ptr<ParameterCache> ComponentStore::getParameterCache() const {
191     return mParameterCache;
192 }
193 
194 #ifndef __ANDROID_APEX__
195 // static
GetFilterWrapper()196 std::shared_ptr<FilterWrapper> ComponentStore::GetFilterWrapper() {
197     constexpr const char kPluginPath[] = "libc2filterplugin.so";
198     static std::shared_ptr<FilterWrapper> wrapper = FilterWrapper::Create(
199             std::make_unique<DefaultFilterPlugin>(kPluginPath));
200     return wrapper;
201 }
202 #endif
203 
tryCreateMultiAccessUnitInterface(const std::shared_ptr<C2ComponentInterface> & c2interface)204 std::shared_ptr<MultiAccessUnitInterface> ComponentStore::tryCreateMultiAccessUnitInterface(
205         const std::shared_ptr<C2ComponentInterface> &c2interface) {
206     std::shared_ptr<MultiAccessUnitInterface> multiAccessUnitIntf = nullptr;
207     if (c2interface == nullptr) {
208         return nullptr;
209     }
210     if (MultiAccessUnitHelper::isEnabledOnPlatform()) {
211         c2_status_t err = C2_OK;
212         C2ComponentDomainSetting domain;
213         std::vector<std::unique_ptr<C2Param>> heapParams;
214         err = c2interface->query_vb({&domain}, {}, C2_MAY_BLOCK, &heapParams);
215         if (err == C2_OK && (domain.value == C2Component::DOMAIN_AUDIO)) {
216             std::vector<std::shared_ptr<C2ParamDescriptor>> params;
217             bool isComponentSupportsLargeAudioFrame = false;
218             c2interface->querySupportedParams_nb(&params);
219             for (const auto &paramDesc : params) {
220                 if (paramDesc->name().compare(C2_PARAMKEY_OUTPUT_LARGE_FRAME) == 0) {
221                     isComponentSupportsLargeAudioFrame = true;
222                     break;
223                 }
224             }
225             if (!isComponentSupportsLargeAudioFrame) {
226                 multiAccessUnitIntf = std::make_shared<MultiAccessUnitInterface>(
227                         c2interface,
228                         std::static_pointer_cast<C2ReflectorHelper>(mParamReflectors[0]));
229             }
230         }
231     }
232     return multiAccessUnitIntf;
233 }
234 
235 // 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)236 Return<void> ComponentStore::createComponent(
237         const hidl_string& name,
238         const sp<IComponentListener>& listener,
239         const sp<IClientManager>& pool,
240         createComponent_cb _hidl_cb) {
241 
242     sp<Component> component;
243     std::shared_ptr<C2Component> c2component;
244     Status status = static_cast<Status>(
245             mStore->createComponent(name, &c2component));
246 
247     if (status == Status::OK) {
248 #ifndef __ANDROID_APEX__
249         c2component = GetFilterWrapper()->maybeWrapComponent(c2component);
250 #endif
251         onInterfaceLoaded(c2component->intf());
252         component = new Component(c2component, listener, this, pool);
253         if (!component) {
254             status = Status::CORRUPTED;
255         } else {
256             reportComponentBirth(component.get());
257             if (component->status() != C2_OK) {
258                 status = static_cast<Status>(component->status());
259             } else {
260                 component->initListener(component);
261                 if (component->status() != C2_OK) {
262                     status = static_cast<Status>(component->status());
263                 }
264             }
265         }
266     }
267     _hidl_cb(status, component);
268     return Void();
269 }
270 
createInterface(const hidl_string & name,createInterface_cb _hidl_cb)271 Return<void> ComponentStore::createInterface(
272         const hidl_string& name,
273         createInterface_cb _hidl_cb) {
274     std::shared_ptr<C2ComponentInterface> c2interface;
275     c2_status_t res = mStore->createInterface(name, &c2interface);
276 
277     sp<IComponentInterface> interface;
278     if (res == C2_OK) {
279 #ifndef __ANDROID_APEX__
280         c2interface = GetFilterWrapper()->maybeWrapInterface(c2interface);
281 #endif
282         onInterfaceLoaded(c2interface);
283         std::shared_ptr<MultiAccessUnitInterface> multiAccessUnitIntf =
284                 tryCreateMultiAccessUnitInterface(c2interface);
285         interface = new ComponentInterface(c2interface, multiAccessUnitIntf, mParameterCache);
286     }
287     _hidl_cb(static_cast<Status>(res), interface);
288     return Void();
289 }
290 
listComponents(listComponents_cb _hidl_cb)291 Return<void> ComponentStore::listComponents(listComponents_cb _hidl_cb) {
292     std::vector<std::shared_ptr<const C2Component::Traits>> c2traits =
293             mStore->listComponents();
294     hidl_vec<IComponentStore::ComponentTraits> traits(c2traits.size());
295     size_t ix = 0;
296     for (const std::shared_ptr<const C2Component::Traits> &c2trait : c2traits) {
297         if (c2trait) {
298             if (objcpy(&traits[ix], *c2trait)) {
299                 ++ix;
300             } else {
301                 break;
302             }
303         }
304     }
305     traits.resize(ix);
306     _hidl_cb(Status::OK, traits);
307     return Void();
308 }
309 
createInputSurface(createInputSurface_cb _hidl_cb)310 Return<void> ComponentStore::createInputSurface(createInputSurface_cb _hidl_cb) {
311     sp<GraphicBufferSource> source = new GraphicBufferSource();
312     if (source->initCheck() != OK) {
313         _hidl_cb(Status::CORRUPTED, nullptr);
314         return Void();
315     }
316     using namespace std::placeholders;
317     sp<InputSurface> inputSurface = new InputSurface(
318             mParameterCache,
319             std::make_shared<C2ReflectorHelper>(),
320             source->getHGraphicBufferProducer(),
321             source);
322     _hidl_cb(inputSurface ? Status::OK : Status::NO_MEMORY,
323              inputSurface);
324     return Void();
325 }
326 
onInterfaceLoaded(const std::shared_ptr<C2ComponentInterface> & intf)327 void ComponentStore::onInterfaceLoaded(const std::shared_ptr<C2ComponentInterface> &intf) {
328     // invalidate unsupported struct descriptors if a new interface is loaded as it may have
329     // exposed new descriptors
330     std::lock_guard<std::mutex> lock(mStructDescriptorsMutex);
331     if (!mLoadedInterfaces.count(intf->getName())) {
332         mUnsupportedStructDescriptors.clear();
333         mLoadedInterfaces.emplace(intf->getName());
334     }
335 }
336 
getStructDescriptors(const hidl_vec<uint32_t> & indices,getStructDescriptors_cb _hidl_cb)337 Return<void> ComponentStore::getStructDescriptors(
338         const hidl_vec<uint32_t>& indices,
339         getStructDescriptors_cb _hidl_cb) {
340     hidl_vec<StructDescriptor> descriptors(indices.size());
341     size_t dstIx = 0;
342     Status res = Status::OK;
343     for (size_t srcIx = 0; srcIx < indices.size(); ++srcIx) {
344         std::lock_guard<std::mutex> lock(mStructDescriptorsMutex);
345         const C2Param::CoreIndex coreIndex = C2Param::CoreIndex(indices[srcIx]).coreIndex();
346         const auto item = mStructDescriptors.find(coreIndex);
347         if (item == mStructDescriptors.end()) {
348             // not in the cache, and not known to be unsupported, query local reflector
349             if (!mUnsupportedStructDescriptors.count(coreIndex)) {
350                 std::shared_ptr<C2StructDescriptor> structDesc = describe(coreIndex);
351                 if (!structDesc) {
352                     mUnsupportedStructDescriptors.emplace(coreIndex);
353                 } else {
354                     mStructDescriptors.insert({ coreIndex, structDesc });
355                     if (objcpy(&descriptors[dstIx], *structDesc)) {
356                         ++dstIx;
357                         continue;
358                     }
359                     res = Status::CORRUPTED;
360                     break;
361                 }
362             }
363             res = Status::NOT_FOUND;
364         } else if (item->second) {
365             if (objcpy(&descriptors[dstIx], *item->second)) {
366                 ++dstIx;
367                 continue;
368             }
369             res = Status::CORRUPTED;
370             break;
371         } else {
372             res = Status::NO_MEMORY;
373             break;
374         }
375     }
376     descriptors.resize(dstIx);
377     _hidl_cb(res, descriptors);
378     return Void();
379 }
380 
getPoolClientManager()381 Return<sp<IClientManager>> ComponentStore::getPoolClientManager() {
382     return ClientManager::getInstance();
383 }
384 
copyBuffer(const Buffer & src,const Buffer & dst)385 Return<Status> ComponentStore::copyBuffer(const Buffer& src, const Buffer& dst) {
386     // TODO implement
387     (void)src;
388     (void)dst;
389     return Status::OMITTED;
390 }
391 
getConfigurable()392 Return<sp<IConfigurable>> ComponentStore::getConfigurable() {
393     return mConfigurable;
394 }
395 
describe(const C2Param::CoreIndex & index)396 std::shared_ptr<C2StructDescriptor> ComponentStore::describe(const C2Param::CoreIndex &index) {
397     for (const std::shared_ptr<C2ParamReflector> &reflector : mParamReflectors) {
398         std::shared_ptr<C2StructDescriptor> desc = reflector->describe(index);
399         if (desc) {
400             return desc;
401         }
402     }
403     return nullptr;
404 }
405 
406 // Called from createComponent() after a successful creation of `component`.
reportComponentBirth(Component * component)407 void ComponentStore::reportComponentBirth(Component* component) {
408     ComponentStatus componentStatus;
409     componentStatus.c2Component = component->mComponent;
410     componentStatus.birthTime = std::chrono::system_clock::now();
411 
412     std::lock_guard<std::mutex> lock(mComponentRosterMutex);
413     mComponentRoster.emplace(component, componentStatus);
414 }
415 
416 // Called from within the destructor of `component`. No virtual function calls
417 // are made on `component` here.
reportComponentDeath(Component * component)418 void ComponentStore::reportComponentDeath(Component* component) {
419     std::lock_guard<std::mutex> lock(mComponentRosterMutex);
420     mComponentRoster.erase(component);
421 }
422 
423 // Dumps component traits.
dump(std::ostream & out,const std::shared_ptr<const C2Component::Traits> & comp)424 std::ostream& ComponentStore::dump(
425         std::ostream& out,
426         const std::shared_ptr<const C2Component::Traits>& comp) {
427 
428     constexpr const char indent[] = "    ";
429 
430     out << indent << "name: " << comp->name << std::endl;
431     out << indent << "domain: " << comp->domain << std::endl;
432     out << indent << "kind: " << comp->kind << std::endl;
433     out << indent << "rank: " << comp->rank << std::endl;
434     out << indent << "mediaType: " << comp->mediaType << std::endl;
435     out << indent << "aliases:";
436     for (const auto& alias : comp->aliases) {
437         out << ' ' << alias;
438     }
439     out << std::endl;
440 
441     return out;
442 }
443 
444 // Dumps component status.
dump(std::ostream & out,ComponentStatus & compStatus)445 std::ostream& ComponentStore::dump(
446         std::ostream& out,
447         ComponentStatus& compStatus) {
448 
449     constexpr const char indent[] = "    ";
450 
451     // Print birth time.
452     std::chrono::milliseconds ms =
453             std::chrono::duration_cast<std::chrono::milliseconds>(
454                 compStatus.birthTime.time_since_epoch());
455     std::time_t birthTime = std::chrono::system_clock::to_time_t(
456             compStatus.birthTime);
457     std::tm tm = *std::localtime(&birthTime);
458     out << indent << "Creation time: "
459         << std::put_time(&tm, "%Y-%m-%d %H:%M:%S")
460         << '.' << std::setfill('0') << std::setw(3) << ms.count() % 1000
461         << std::endl;
462 
463     // Print name and id.
464     std::shared_ptr<C2ComponentInterface> intf = compStatus.c2Component->intf();
465     if (!intf) {
466         out << indent << "Unknown component -- null interface" << std::endl;
467         return out;
468     }
469     out << indent << "Name: " << intf->getName() << std::endl;
470     out << indent << "Id: " << intf->getId() << std::endl;
471 
472     return out;
473 }
474 
475 // Dumps information when lshal is called.
debug(const hidl_handle & handle,const hidl_vec<hidl_string> &)476 Return<void> ComponentStore::debug(
477         const hidl_handle& handle,
478         const hidl_vec<hidl_string>& /* args */) {
479     LOG(INFO) << "debug -- dumping...";
480     const native_handle_t *h = handle.getNativeHandle();
481     if (!h || h->numFds != 1) {
482        LOG(ERROR) << "debug -- dumping failed -- "
483                "invalid file descriptor to dump to";
484        return Void();
485     }
486     std::ostringstream out;
487 
488     { // Populate "out".
489 
490         constexpr const char indent[] = "  ";
491 
492         // Show name.
493         out << "Beginning of dump -- C2ComponentStore: "
494                 << mStore->getName() << std::endl << std::endl;
495 
496         // Retrieve the list of supported components.
497         std::vector<std::shared_ptr<const C2Component::Traits>> traitsList =
498                 mStore->listComponents();
499 
500         // Dump the traits of supported components.
501         out << indent << "Supported components:" << std::endl << std::endl;
502         if (traitsList.size() == 0) {
503             out << indent << indent << "NONE" << std::endl << std::endl;
504         } else {
505             for (const auto& traits : traitsList) {
506                 dump(out, traits) << std::endl;
507             }
508         }
509 
510         // Dump active components.
511         {
512             out << indent << "Active components:" << std::endl << std::endl;
513             std::lock_guard<std::mutex> lock(mComponentRosterMutex);
514             if (mComponentRoster.size() == 0) {
515                 out << indent << indent << "NONE" << std::endl << std::endl;
516             } else {
517                 for (auto& pair : mComponentRoster) {
518                     dump(out, pair.second) << std::endl;
519                 }
520             }
521         }
522 
523         out << "End of dump -- C2ComponentStore: "
524                 << mStore->getName() << std::endl;
525     }
526 
527     if (!android::base::WriteStringToFd(out.str(), h->data[0])) {
528         PLOG(WARNING) << "debug -- dumping failed -- write()";
529     } else {
530         LOG(INFO) << "debug -- dumping succeeded";
531     }
532     return Void();
533 }
534 
535 }  // namespace utils
536 }  // namespace V1_0
537 }  // namespace c2
538 }  // namespace media
539 }  // namespace hardware
540 }  // namespace android
541 
542