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::__anon8de813ee0110::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::__anon8de813ee0110::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::__anon8de813ee0110::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::__anon8de813ee0110::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::__anon8de813ee0110::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     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 
239     sp<IComponentInterface> interface;
240     if (res == C2_OK) {
241 #ifndef __ANDROID_APEX__
242         c2interface = GetFilterWrapper()->maybeWrapInterface(c2interface);
243 #endif
244         onInterfaceLoaded(c2interface);
245         interface = new ComponentInterface(c2interface, mParameterCache);
246     }
247     _hidl_cb(static_cast<Status>(res), interface);
248     return Void();
249 }
250 
listComponents(listComponents_cb _hidl_cb)251 Return<void> ComponentStore::listComponents(listComponents_cb _hidl_cb) {
252     std::vector<std::shared_ptr<const C2Component::Traits>> c2traits =
253             mStore->listComponents();
254     hidl_vec<IComponentStore::ComponentTraits> traits(c2traits.size());
255     size_t ix = 0;
256     for (const std::shared_ptr<const C2Component::Traits> &c2trait : c2traits) {
257         if (c2trait) {
258             if (objcpy(&traits[ix], *c2trait)) {
259                 ++ix;
260             } else {
261                 break;
262             }
263         }
264     }
265     traits.resize(ix);
266     _hidl_cb(Status::OK, traits);
267     return Void();
268 }
269 
createInputSurface(createInputSurface_cb _hidl_cb)270 Return<void> ComponentStore::createInputSurface(createInputSurface_cb _hidl_cb) {
271     sp<GraphicBufferSource> source = new GraphicBufferSource();
272     if (source->initCheck() != OK) {
273         _hidl_cb(Status::CORRUPTED, nullptr);
274         return Void();
275     }
276     using namespace std::placeholders;
277     sp<InputSurface> inputSurface = new InputSurface(
278             mParameterCache,
279             std::make_shared<C2ReflectorHelper>(),
280             source->getHGraphicBufferProducer(),
281             source);
282     _hidl_cb(inputSurface ? Status::OK : Status::NO_MEMORY,
283              inputSurface);
284     return Void();
285 }
286 
onInterfaceLoaded(const std::shared_ptr<C2ComponentInterface> & intf)287 void ComponentStore::onInterfaceLoaded(const std::shared_ptr<C2ComponentInterface> &intf) {
288     // invalidate unsupported struct descriptors if a new interface is loaded as it may have
289     // exposed new descriptors
290     std::lock_guard<std::mutex> lock(mStructDescriptorsMutex);
291     if (!mLoadedInterfaces.count(intf->getName())) {
292         mUnsupportedStructDescriptors.clear();
293         mLoadedInterfaces.emplace(intf->getName());
294     }
295 }
296 
getStructDescriptors(const hidl_vec<uint32_t> & indices,getStructDescriptors_cb _hidl_cb)297 Return<void> ComponentStore::getStructDescriptors(
298         const hidl_vec<uint32_t>& indices,
299         getStructDescriptors_cb _hidl_cb) {
300     hidl_vec<StructDescriptor> descriptors(indices.size());
301     size_t dstIx = 0;
302     Status res = Status::OK;
303     for (size_t srcIx = 0; srcIx < indices.size(); ++srcIx) {
304         std::lock_guard<std::mutex> lock(mStructDescriptorsMutex);
305         const C2Param::CoreIndex coreIndex = C2Param::CoreIndex(indices[srcIx]).coreIndex();
306         const auto item = mStructDescriptors.find(coreIndex);
307         if (item == mStructDescriptors.end()) {
308             // not in the cache, and not known to be unsupported, query local reflector
309             if (!mUnsupportedStructDescriptors.count(coreIndex)) {
310                 std::shared_ptr<C2StructDescriptor> structDesc =
311                     mParamReflector->describe(coreIndex);
312                 if (!structDesc) {
313                     mUnsupportedStructDescriptors.emplace(coreIndex);
314                 } else {
315                     mStructDescriptors.insert({ coreIndex, structDesc });
316                     if (objcpy(&descriptors[dstIx], *structDesc)) {
317                         ++dstIx;
318                         continue;
319                     }
320                     res = Status::CORRUPTED;
321                     break;
322                 }
323             }
324             res = Status::NOT_FOUND;
325         } else if (item->second) {
326             if (objcpy(&descriptors[dstIx], *item->second)) {
327                 ++dstIx;
328                 continue;
329             }
330             res = Status::CORRUPTED;
331             break;
332         } else {
333             res = Status::NO_MEMORY;
334             break;
335         }
336     }
337     descriptors.resize(dstIx);
338     _hidl_cb(res, descriptors);
339     return Void();
340 }
341 
getPoolClientManager()342 Return<sp<IClientManager>> ComponentStore::getPoolClientManager() {
343     return ClientManager::getInstance();
344 }
345 
copyBuffer(const Buffer & src,const Buffer & dst)346 Return<Status> ComponentStore::copyBuffer(const Buffer& src, const Buffer& dst) {
347     // TODO implement
348     (void)src;
349     (void)dst;
350     return Status::OMITTED;
351 }
352 
getConfigurable()353 Return<sp<IConfigurable>> ComponentStore::getConfigurable() {
354     return mConfigurable;
355 }
356 
357 // Called from createComponent() after a successful creation of `component`.
reportComponentBirth(Component * component)358 void ComponentStore::reportComponentBirth(Component* component) {
359     ComponentStatus componentStatus;
360     componentStatus.c2Component = component->mComponent;
361     componentStatus.birthTime = std::chrono::system_clock::now();
362 
363     std::lock_guard<std::mutex> lock(mComponentRosterMutex);
364     mComponentRoster.emplace(component, componentStatus);
365 }
366 
367 // Called from within the destructor of `component`. No virtual function calls
368 // are made on `component` here.
reportComponentDeath(Component * component)369 void ComponentStore::reportComponentDeath(Component* component) {
370     std::lock_guard<std::mutex> lock(mComponentRosterMutex);
371     mComponentRoster.erase(component);
372 }
373 
374 // Dumps component traits.
dump(std::ostream & out,const std::shared_ptr<const C2Component::Traits> & comp)375 std::ostream& ComponentStore::dump(
376         std::ostream& out,
377         const std::shared_ptr<const C2Component::Traits>& comp) {
378 
379     constexpr const char indent[] = "    ";
380 
381     out << indent << "name: " << comp->name << std::endl;
382     out << indent << "domain: " << comp->domain << std::endl;
383     out << indent << "kind: " << comp->kind << std::endl;
384     out << indent << "rank: " << comp->rank << std::endl;
385     out << indent << "mediaType: " << comp->mediaType << std::endl;
386     out << indent << "aliases:";
387     for (const auto& alias : comp->aliases) {
388         out << ' ' << alias;
389     }
390     out << std::endl;
391 
392     return out;
393 }
394 
395 // Dumps component status.
dump(std::ostream & out,ComponentStatus & compStatus)396 std::ostream& ComponentStore::dump(
397         std::ostream& out,
398         ComponentStatus& compStatus) {
399 
400     constexpr const char indent[] = "    ";
401 
402     // Print birth time.
403     std::chrono::milliseconds ms =
404             std::chrono::duration_cast<std::chrono::milliseconds>(
405                 compStatus.birthTime.time_since_epoch());
406     std::time_t birthTime = std::chrono::system_clock::to_time_t(
407             compStatus.birthTime);
408     std::tm tm = *std::localtime(&birthTime);
409     out << indent << "Creation time: "
410         << std::put_time(&tm, "%Y-%m-%d %H:%M:%S")
411         << '.' << std::setfill('0') << std::setw(3) << ms.count() % 1000
412         << std::endl;
413 
414     // Print name and id.
415     std::shared_ptr<C2ComponentInterface> intf = compStatus.c2Component->intf();
416     if (!intf) {
417         out << indent << "Unknown component -- null interface" << std::endl;
418         return out;
419     }
420     out << indent << "Name: " << intf->getName() << std::endl;
421     out << indent << "Id: " << intf->getId() << std::endl;
422 
423     return out;
424 }
425 
426 // Dumps information when lshal is called.
debug(const hidl_handle & handle,const hidl_vec<hidl_string> &)427 Return<void> ComponentStore::debug(
428         const hidl_handle& handle,
429         const hidl_vec<hidl_string>& /* args */) {
430     LOG(INFO) << "debug -- dumping...";
431     const native_handle_t *h = handle.getNativeHandle();
432     if (!h || h->numFds != 1) {
433        LOG(ERROR) << "debug -- dumping failed -- "
434                "invalid file descriptor to dump to";
435        return Void();
436     }
437     std::ostringstream out;
438 
439     { // Populate "out".
440 
441         constexpr const char indent[] = "  ";
442 
443         // Show name.
444         out << "Beginning of dump -- C2ComponentStore: "
445                 << mStore->getName() << std::endl << std::endl;
446 
447         // Retrieve the list of supported components.
448         std::vector<std::shared_ptr<const C2Component::Traits>> traitsList =
449                 mStore->listComponents();
450 
451         // Dump the traits of supported components.
452         out << indent << "Supported components:" << std::endl << std::endl;
453         if (traitsList.size() == 0) {
454             out << indent << indent << "NONE" << std::endl << std::endl;
455         } else {
456             for (const auto& traits : traitsList) {
457                 dump(out, traits) << std::endl;
458             }
459         }
460 
461         // Dump active components.
462         {
463             out << indent << "Active components:" << std::endl << std::endl;
464             std::lock_guard<std::mutex> lock(mComponentRosterMutex);
465             if (mComponentRoster.size() == 0) {
466                 out << indent << indent << "NONE" << std::endl << std::endl;
467             } else {
468                 for (auto& pair : mComponentRoster) {
469                     dump(out, pair.second) << std::endl;
470                 }
471             }
472         }
473 
474         out << "End of dump -- C2ComponentStore: "
475                 << mStore->getName() << std::endl;
476     }
477 
478     if (!android::base::WriteStringToFd(out.str(), h->data[0])) {
479         PLOG(WARNING) << "debug -- dumping failed -- write()";
480     } else {
481         LOG(INFO) << "debug -- dumping succeeded";
482     }
483     return Void();
484 }
485 
486 }  // namespace utils
487 }  // namespace V1_0
488 }  // namespace c2
489 }  // namespace media
490 }  // namespace hardware
491 }  // namespace android
492 
493