• 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-Component"
19 #include <android-base/logging.h>
20 
21 #include <codec2/common/BqPoolInvalidateHelper.h>
22 #include <codec2/hidl/1.0/Component.h>
23 #include <codec2/hidl/1.0/ComponentStore.h>
24 #include <codec2/hidl/1.0/InputBufferManager.h>
25 
26 #ifndef __ANDROID_APEX__
27 #include <FilterWrapper.h>
28 #endif
29 
30 #include <hidl/HidlBinderSupport.h>
31 #include <utils/Timers.h>
32 
33 #include <C2BqBufferPriv.h>
34 #include <C2BqPoolInvalidator.h>
35 #include <C2Debug.h>
36 #include <C2PlatformSupport.h>
37 
38 #include <chrono>
39 #include <thread>
40 
41 namespace android {
42 namespace hardware {
43 namespace media {
44 namespace c2 {
45 namespace V1_0 {
46 namespace utils {
47 
48 using namespace ::android;
49 
50 // ComponentListener wrapper
51 struct Component::Listener : public C2Component::Listener {
52 
Listenerandroid::hardware::media::c2::V1_0::utils::Component::Listener53     Listener(const sp<Component>& component) :
54         mComponent(component),
55         mListener(component->mListener) {
56     }
57 
onError_nbandroid::hardware::media::c2::V1_0::utils::Component::Listener58     virtual void onError_nb(
59             std::weak_ptr<C2Component> /* c2component */,
60             uint32_t errorCode) override {
61         sp<IComponentListener> listener = mListener.promote();
62         if (listener) {
63             Return<void> transStatus = listener->onError(Status::OK, errorCode);
64             if (!transStatus.isOk()) {
65                 LOG(ERROR) << "Component::Listener::onError_nb -- "
66                            << "transaction failed.";
67             }
68         }
69     }
70 
onTripped_nbandroid::hardware::media::c2::V1_0::utils::Component::Listener71     virtual void onTripped_nb(
72             std::weak_ptr<C2Component> /* c2component */,
73             std::vector<std::shared_ptr<C2SettingResult>> c2settingResult
74             ) override {
75         sp<IComponentListener> listener = mListener.promote();
76         if (listener) {
77             hidl_vec<SettingResult> settingResults(c2settingResult.size());
78             size_t ix = 0;
79             for (const std::shared_ptr<C2SettingResult> &c2result :
80                     c2settingResult) {
81                 if (c2result) {
82                     if (!objcpy(&settingResults[ix++], *c2result)) {
83                         break;
84                     }
85                 }
86             }
87             settingResults.resize(ix);
88             Return<void> transStatus = listener->onTripped(settingResults);
89             if (!transStatus.isOk()) {
90                 LOG(ERROR) << "Component::Listener::onTripped_nb -- "
91                            << "transaction failed.";
92             }
93         }
94     }
95 
onWorkDone_nbandroid::hardware::media::c2::V1_0::utils::Component::Listener96     virtual void onWorkDone_nb(
97             std::weak_ptr<C2Component> /* c2component */,
98             std::list<std::unique_ptr<C2Work>> c2workItems) override {
99         for (const std::unique_ptr<C2Work>& work : c2workItems) {
100             if (work) {
101                 if (work->worklets.empty()
102                         || !work->worklets.back()
103                         || (work->worklets.back()->output.flags &
104                             C2FrameData::FLAG_INCOMPLETE) == 0) {
105                     InputBufferManager::
106                             unregisterFrameData(mListener, work->input);
107                 }
108             }
109         }
110 
111         sp<IComponentListener> listener = mListener.promote();
112         if (listener) {
113             WorkBundle workBundle;
114 
115             sp<Component> strongComponent = mComponent.promote();
116             beginTransferBufferQueueBlocks(c2workItems, true);
117             if (!objcpy(&workBundle, c2workItems, strongComponent ?
118                     &strongComponent->mBufferPoolSender : nullptr)) {
119                 LOG(ERROR) << "Component::Listener::onWorkDone_nb -- "
120                            << "received corrupted work items.";
121                 endTransferBufferQueueBlocks(c2workItems, false, true);
122                 return;
123             }
124             Return<void> transStatus = listener->onWorkDone(workBundle);
125             if (!transStatus.isOk()) {
126                 LOG(ERROR) << "Component::Listener::onWorkDone_nb -- "
127                            << "transaction failed.";
128                 endTransferBufferQueueBlocks(c2workItems, false, true);
129                 return;
130             }
131             endTransferBufferQueueBlocks(c2workItems, true, true);
132         }
133     }
134 
135 protected:
136     wp<Component> mComponent;
137     wp<IComponentListener> mListener;
138 };
139 
140 // Component listener for handle multiple access-units
141 struct MultiAccessUnitListener : public Component::Listener {
MultiAccessUnitListenerandroid::hardware::media::c2::V1_0::utils::MultiAccessUnitListener142     MultiAccessUnitListener(const sp<Component> &component,
143             const std::shared_ptr<MultiAccessUnitHelper> &handler):
144         Listener(component), mHandler(handler) {
145     }
146 
onError_nbandroid::hardware::media::c2::V1_0::utils::MultiAccessUnitListener147     virtual void onError_nb(
148             std::weak_ptr<C2Component> c2component,
149             uint32_t errorCode) override {
150         if (mHandler) {
151             std::list<std::unique_ptr<C2Work>> worklist;
152             mHandler->error(&worklist);
153             if (!worklist.empty()) {
154                 Listener::onWorkDone_nb(c2component, std::move(worklist));
155             }
156         }
157         Listener::onError_nb(c2component, errorCode);
158     }
159 
onTripped_nbandroid::hardware::media::c2::V1_0::utils::MultiAccessUnitListener160     virtual void onTripped_nb(
161             std::weak_ptr<C2Component> c2component,
162             std::vector<std::shared_ptr<C2SettingResult>> c2settingResult
163             ) override {
164         Listener::onTripped_nb(c2component,
165                 c2settingResult);
166     }
167 
onWorkDone_nbandroid::hardware::media::c2::V1_0::utils::MultiAccessUnitListener168     virtual void onWorkDone_nb(
169             std::weak_ptr<C2Component> c2component,
170             std::list<std::unique_ptr<C2Work>> c2workItems) override {
171         if (mHandler) {
172             std::list<std::unique_ptr<C2Work>> processedWork;
173             mHandler->gather(c2workItems, &processedWork);
174             if (!processedWork.empty()) {
175                 Listener::onWorkDone_nb(c2component, std::move(processedWork));
176             }
177         } else {
178             Listener::onWorkDone_nb(c2component, std::move(c2workItems));
179         }
180     }
181 
182     protected:
183         std::shared_ptr<MultiAccessUnitHelper> mHandler;
184 };
185 
186 // Component::Sink
187 struct Component::Sink : public IInputSink {
188     std::shared_ptr<Component> mComponent;
189     sp<IConfigurable> mConfigurable;
190 
queueandroid::hardware::media::c2::V1_0::utils::Component::Sink191     virtual Return<Status> queue(const WorkBundle& workBundle) override {
192         return mComponent->queue(workBundle);
193     }
194 
getConfigurableandroid::hardware::media::c2::V1_0::utils::Component::Sink195     virtual Return<sp<IConfigurable>> getConfigurable() override {
196         return mConfigurable;
197     }
198 
199     Sink(const std::shared_ptr<Component>& component);
200     virtual ~Sink() override;
201 
202     // Process-wide map: Component::Sink -> C2Component.
203     static std::mutex sSink2ComponentMutex;
204     static std::map<IInputSink*, std::weak_ptr<C2Component>> sSink2Component;
205 
206     static std::shared_ptr<C2Component> findLocalComponent(
207             const sp<IInputSink>& sink);
208 };
209 
210 std::mutex
211         Component::Sink::sSink2ComponentMutex{};
212 std::map<IInputSink*, std::weak_ptr<C2Component>>
213         Component::Sink::sSink2Component{};
214 
Sink(const std::shared_ptr<Component> & component)215 Component::Sink::Sink(const std::shared_ptr<Component>& component)
216         : mComponent{component},
__anon3855478f0102() 217           mConfigurable{[&component]() -> sp<IConfigurable> {
218               Return<sp<IComponentInterface>> ret1 = component->getInterface();
219               if (!ret1.isOk()) {
220                   LOG(ERROR) << "Sink::Sink -- component's transaction failed.";
221                   return nullptr;
222               }
223               Return<sp<IConfigurable>> ret2 =
224                       static_cast<sp<IComponentInterface>>(ret1)->
225                       getConfigurable();
226               if (!ret2.isOk()) {
227                   LOG(ERROR) << "Sink::Sink -- interface's transaction failed.";
228                   return nullptr;
229               }
230               return static_cast<sp<IConfigurable>>(ret2);
231           }()} {
232     std::lock_guard<std::mutex> lock(sSink2ComponentMutex);
233     sSink2Component.emplace(this, component->mComponent);
234 }
235 
~Sink()236 Component::Sink::~Sink() {
237     std::lock_guard<std::mutex> lock(sSink2ComponentMutex);
238     sSink2Component.erase(this);
239 }
240 
findLocalComponent(const sp<IInputSink> & sink)241 std::shared_ptr<C2Component> Component::Sink::findLocalComponent(
242         const sp<IInputSink>& sink) {
243     std::lock_guard<std::mutex> lock(sSink2ComponentMutex);
244     auto i = sSink2Component.find(sink.get());
245     if (i == sSink2Component.end()) {
246         return nullptr;
247     }
248     return i->second.lock();
249 }
250 
251 // Component
Component(const std::shared_ptr<C2Component> & component,const sp<IComponentListener> & listener,const sp<ComponentStore> & store,const sp<::android::hardware::media::bufferpool::V2_0::IClientManager> & clientPoolManager)252 Component::Component(
253         const std::shared_ptr<C2Component>& component,
254         const sp<IComponentListener>& listener,
255         const sp<ComponentStore>& store,
256         const sp<::android::hardware::media::bufferpool::V2_0::
257         IClientManager>& clientPoolManager)
258       : mComponent{component},
259         mListener{listener},
260         mStore{store},
261         mBufferPoolSender{clientPoolManager} {
262     // Retrieve supported parameters from store
263     // TODO: We could cache this per component/interface type
264     mMultiAccessUnitIntf = store->tryCreateMultiAccessUnitInterface(component->intf());
265     mInterface = new ComponentInterface(
266             component->intf(), mMultiAccessUnitIntf, store->getParameterCache());
267     mInit = mInterface->status();
268 }
269 
status() const270 c2_status_t Component::status() const {
271     return mInit;
272 }
273 
onDeathReceived()274 void Component::onDeathReceived() {
275     std::list<std::shared_ptr<C2BufferQueueBlockPool>> bqPools;
276     {
277         std::lock_guard<std::mutex> lock(mBlockPoolsMutex);
278         mClientDied = true;
279         transform_if(mBlockPools.begin(), mBlockPools.end(), std::back_inserter(bqPools),
280                 BqPoolFilterFn, BqPoolConvertFn);
281     }
282     if (!bqPools.empty()) {
283         std::shared_ptr<C2BqPoolInvalidateItem> bqInvalidateItem =
284                 std::make_shared<C2BqPoolInvalidateItem>(std::move(bqPools));
285         bqInvalidateItem->invalidate();
286     }
287     release();
288 }
289 
290 // Methods from ::android::hardware::media::c2::V1_0::IComponent
queue(const WorkBundle & workBundle)291 Return<Status> Component::queue(const WorkBundle& workBundle) {
292     std::list<std::unique_ptr<C2Work>> c2works;
293     if (!objcpy(&c2works, workBundle)) {
294         return Status::CORRUPTED;
295     }
296 
297     // Register input buffers.
298     for (const std::unique_ptr<C2Work>& work : c2works) {
299         if (work) {
300             InputBufferManager::
301                     registerFrameData(mListener, work->input);
302         }
303     }
304     c2_status_t err = C2_OK;
305     if (mMultiAccessUnitHelper) {
306         std::list<std::list<std::unique_ptr<C2Work>>> c2worklists;
307         mMultiAccessUnitHelper->scatter(c2works, &c2worklists);
308         for (auto &c2worklist : c2worklists) {
309             err = mComponent->queue_nb(&c2worklist);
310             if (err != C2_OK) {
311                 LOG(ERROR) << "Error Queuing to component.";
312                 break;
313             }
314         }
315         return static_cast<Status>(err);
316     }
317     return static_cast<Status>(mComponent->queue_nb(&c2works));
318 }
319 
flush(flush_cb _hidl_cb)320 Return<void> Component::flush(flush_cb _hidl_cb) {
321     std::list<std::unique_ptr<C2Work>> c2flushedWorks;
322     c2_status_t c2res = mComponent->flush_sm(
323             C2Component::FLUSH_COMPONENT,
324             &c2flushedWorks);
325     if (mMultiAccessUnitHelper) {
326         c2res = mMultiAccessUnitHelper->flush(&c2flushedWorks);
327     }
328 
329     // Unregister input buffers.
330     for (const std::unique_ptr<C2Work>& work : c2flushedWorks) {
331         if (work) {
332             if (work->worklets.empty()
333                     || !work->worklets.back()
334                     || (work->worklets.back()->output.flags &
335                         C2FrameData::FLAG_INCOMPLETE) == 0) {
336                 InputBufferManager::
337                         unregisterFrameData(mListener, work->input);
338             }
339         }
340     }
341 
342     WorkBundle flushedWorkBundle;
343     Status res = static_cast<Status>(c2res);
344     beginTransferBufferQueueBlocks(c2flushedWorks, true);
345     if (c2res == C2_OK) {
346         if (!objcpy(&flushedWorkBundle, c2flushedWorks, &mBufferPoolSender)) {
347             res = Status::CORRUPTED;
348         }
349     }
350     _hidl_cb(res, flushedWorkBundle);
351     endTransferBufferQueueBlocks(c2flushedWorks, true, true);
352     return Void();
353 }
354 
drain(bool withEos)355 Return<Status> Component::drain(bool withEos) {
356     return static_cast<Status>(mComponent->drain_nb(withEos ?
357             C2Component::DRAIN_COMPONENT_WITH_EOS :
358             C2Component::DRAIN_COMPONENT_NO_EOS));
359 }
360 
setOutputSurface(uint64_t blockPoolId,const sp<HGraphicBufferProducer2> & surface)361 Return<Status> Component::setOutputSurface(
362         uint64_t blockPoolId,
363         const sp<HGraphicBufferProducer2>& surface) {
364     std::shared_ptr<C2BlockPool> pool;
365     GetCodec2BlockPool(blockPoolId, mComponent, &pool);
366     if (pool && pool->getAllocatorId() == C2PlatformAllocatorStore::BUFFERQUEUE) {
367         std::shared_ptr<C2BufferQueueBlockPool> bqPool =
368                 std::static_pointer_cast<C2BufferQueueBlockPool>(pool);
369         C2BufferQueueBlockPool::OnRenderCallback cb =
370             [this](uint64_t producer, int32_t slot, int64_t nsecs) {
371                 // TODO: batch this
372                 hidl_vec<IComponentListener::RenderedFrame> rendered;
373                 rendered.resize(1);
374                 rendered[0] = { producer, slot, nsecs };
375                 (void)mListener->onFramesRendered(rendered).isOk();
376         };
377         if (bqPool) {
378             bqPool->setRenderCallback(cb);
379             bqPool->configureProducer(surface);
380         }
381     }
382     return Status::OK;
383 }
384 
connectToInputSurface(const sp<IInputSurface> & inputSurface,connectToInputSurface_cb _hidl_cb)385 Return<void> Component::connectToInputSurface(
386         const sp<IInputSurface>& inputSurface,
387         connectToInputSurface_cb _hidl_cb) {
388     Status status;
389     sp<IInputSurfaceConnection> connection;
390     auto transStatus = inputSurface->connect(
391             asInputSink(),
392             [&status, &connection](
393                     Status s, const sp<IInputSurfaceConnection>& c) {
394                 status = s;
395                 connection = c;
396             }
397         );
398     _hidl_cb(status, connection);
399     return Void();
400 }
401 
connectToOmxInputSurface(const sp<HGraphicBufferProducer1> & producer,const sp<::android::hardware::media::omx::V1_0::IGraphicBufferSource> & source,connectToOmxInputSurface_cb _hidl_cb)402 Return<void> Component::connectToOmxInputSurface(
403         const sp<HGraphicBufferProducer1>& producer,
404         const sp<::android::hardware::media::omx::V1_0::
405         IGraphicBufferSource>& source,
406         connectToOmxInputSurface_cb _hidl_cb) {
407     (void)producer;
408     (void)source;
409     (void)_hidl_cb;
410     return Void();
411 }
412 
disconnectFromInputSurface()413 Return<Status> Component::disconnectFromInputSurface() {
414     // TODO implement
415     return Status::OK;
416 }
417 
418 namespace /* unnamed */ {
419 
420 struct BlockPoolIntf : public ConfigurableC2Intf {
BlockPoolIntfandroid::hardware::media::c2::V1_0::utils::__anon3855478f0411::BlockPoolIntf421     BlockPoolIntf(const std::shared_ptr<C2BlockPool>& pool)
422           : ConfigurableC2Intf{
423                 "C2BlockPool:" +
424                     (pool ? std::to_string(pool->getLocalId()) : "null"),
425                 0},
426             mPool{pool} {
427     }
428 
configandroid::hardware::media::c2::V1_0::utils::__anon3855478f0411::BlockPoolIntf429     virtual c2_status_t config(
430             const std::vector<C2Param*>& params,
431             c2_blocking_t mayBlock,
432             std::vector<std::unique_ptr<C2SettingResult>>* const failures
433             ) override {
434         (void)params;
435         (void)mayBlock;
436         (void)failures;
437         return C2_OK;
438     }
439 
queryandroid::hardware::media::c2::V1_0::utils::__anon3855478f0411::BlockPoolIntf440     virtual c2_status_t query(
441             const std::vector<C2Param::Index>& indices,
442             c2_blocking_t mayBlock,
443             std::vector<std::unique_ptr<C2Param>>* const params
444             ) const override {
445         (void)indices;
446         (void)mayBlock;
447         (void)params;
448         return C2_OK;
449     }
450 
querySupportedParamsandroid::hardware::media::c2::V1_0::utils::__anon3855478f0411::BlockPoolIntf451     virtual c2_status_t querySupportedParams(
452             std::vector<std::shared_ptr<C2ParamDescriptor>>* const params
453             ) const override {
454         (void)params;
455         return C2_OK;
456     }
457 
querySupportedValuesandroid::hardware::media::c2::V1_0::utils::__anon3855478f0411::BlockPoolIntf458     virtual c2_status_t querySupportedValues(
459             std::vector<C2FieldSupportedValuesQuery>& fields,
460             c2_blocking_t mayBlock) const override {
461         (void)fields;
462         (void)mayBlock;
463         return C2_OK;
464     }
465 
466 protected:
467     std::shared_ptr<C2BlockPool> mPool;
468 };
469 
470 } // unnamed namespace
471 
createBlockPool(uint32_t allocatorId,createBlockPool_cb _hidl_cb)472 Return<void> Component::createBlockPool(
473         uint32_t allocatorId,
474         createBlockPool_cb _hidl_cb) {
475     std::shared_ptr<C2BlockPool> blockPool;
476 #ifdef __ANDROID_APEX__
477     c2_status_t status = CreateCodec2BlockPool(
478             static_cast<C2PlatformAllocatorStore::id_t>(allocatorId),
479             mComponent,
480             &blockPool);
481 #else
482     c2_status_t status = ComponentStore::GetFilterWrapper()->createBlockPool(
483             static_cast<C2PlatformAllocatorStore::id_t>(allocatorId),
484             mComponent,
485             &blockPool);
486 #endif
487     if (status != C2_OK) {
488         blockPool = nullptr;
489     }
490     if (blockPool) {
491         bool emplaced = false;
492         {
493             mBlockPoolsMutex.lock();
494             if (!mClientDied) {
495                 mBlockPools.emplace(blockPool->getLocalId(), blockPool);
496                 emplaced = true;
497             }
498             mBlockPoolsMutex.unlock();
499         }
500         if (!emplaced) {
501             blockPool.reset();
502             status = C2_BAD_STATE;
503         }
504     } else if (status == C2_OK) {
505         status = C2_CORRUPTED;
506     }
507 
508     _hidl_cb(static_cast<Status>(status),
509             blockPool ? blockPool->getLocalId() : 0,
510             new CachedConfigurable(
511             std::make_unique<BlockPoolIntf>(blockPool)));
512     return Void();
513 }
514 
destroyBlockPool(uint64_t blockPoolId)515 Return<Status> Component::destroyBlockPool(uint64_t blockPoolId) {
516     std::lock_guard<std::mutex> lock(mBlockPoolsMutex);
517     return mBlockPools.erase(blockPoolId) == 1 ?
518             Status::OK : Status::CORRUPTED;
519 }
520 
start()521 Return<Status> Component::start() {
522     return static_cast<Status>(mComponent->start());
523 }
524 
stop()525 Return<Status> Component::stop() {
526     InputBufferManager::unregisterFrameData(mListener);
527     Status status = static_cast<Status>(mComponent->stop());
528     {
529         std::lock_guard<std::mutex> lock(mBlockPoolsMutex);
530         for (auto it = mBlockPools.begin(); it != mBlockPools.end(); ++it) {
531             if (it->second->getAllocatorId() == C2PlatformAllocatorStore::BUFFERQUEUE) {
532                 std::shared_ptr<C2BufferQueueBlockPool> bqPool =
533                         std::static_pointer_cast<C2BufferQueueBlockPool>(it->second);
534                 bqPool->clearDeferredBlocks();
535             }
536         }
537     }
538     return status;
539 }
540 
reset()541 Return<Status> Component::reset() {
542     Status status = static_cast<Status>(mComponent->reset());
543     {
544         std::lock_guard<std::mutex> lock(mBlockPoolsMutex);
545         mBlockPools.clear();
546     }
547     if (mMultiAccessUnitHelper) {
548         mMultiAccessUnitHelper->reset();
549     }
550     InputBufferManager::unregisterFrameData(mListener);
551     return status;
552 }
553 
release()554 Return<Status> Component::release() {
555     std::list<std::shared_ptr<C2BufferQueueBlockPool>> bqPools;
556     {
557         std::lock_guard<std::mutex> lock(mBlockPoolsMutex);
558         if (!mClientDied) {
559             transform_if(mBlockPools.begin(), mBlockPools.end(), std::back_inserter(bqPools),
560                      BqPoolFilterFn, BqPoolConvertFn);
561         }
562     }
563     std::shared_ptr<C2BqPoolInvalidateItem> bqInvalidateItem;
564     if (!bqPools.empty()) {
565         // handling rare cases of process death just after release() called.
566         bqInvalidateItem = std::make_shared<C2BqPoolInvalidateItem>(std::move(bqPools));
567         C2BqPoolInvalidator::getInstance().queue(bqInvalidateItem);
568     }
569     Status status = static_cast<Status>(mComponent->release());
570     if (bqInvalidateItem) {
571         // If release is not blocked,
572         // skip invalidation and finish ASAP.
573         bqInvalidateItem->skip();
574     }
575     {
576         std::lock_guard<std::mutex> lock(mBlockPoolsMutex);
577         mBlockPools.clear();
578     }
579     if (mMultiAccessUnitHelper) {
580         mMultiAccessUnitHelper->reset();
581     }
582     InputBufferManager::unregisterFrameData(mListener);
583     return status;
584 }
585 
getInterface()586 Return<sp<IComponentInterface>> Component::getInterface() {
587     return sp<IComponentInterface>(mInterface);
588 }
589 
asInputSink()590 Return<sp<IInputSink>> Component::asInputSink() {
591     std::lock_guard<std::mutex> lock(mSinkMutex);
592     if (!mSink) {
593         mSink = new Sink(shared_from_this());
594     }
595     return {mSink};
596 }
597 
findLocalComponent(const sp<IInputSink> & sink)598 std::shared_ptr<C2Component> Component::findLocalComponent(
599         const sp<IInputSink>& sink) {
600     return Component::Sink::findLocalComponent(sink);
601 }
602 
initListener(const sp<Component> & self)603 void Component::initListener(const sp<Component>& self) {
604     std::shared_ptr<C2Component::Listener> c2listener;
605     if (mMultiAccessUnitIntf) {
606         std::shared_ptr<C2Allocator> allocator;
607         std::shared_ptr<C2BlockPool> linearPool;
608         std::shared_ptr<C2AllocatorStore> store = ::android::GetCodec2PlatformAllocatorStore();
609         if(store->fetchAllocator(C2AllocatorStore::DEFAULT_LINEAR, &allocator) == C2_OK) {
610             ::android::C2PlatformAllocatorDesc desc;
611             desc.allocatorId = allocator->getId();
612             if (C2_OK == CreateCodec2BlockPool(desc, mComponent, &linearPool)) {
613                 if (linearPool) {
614                     mMultiAccessUnitHelper = std::make_shared<MultiAccessUnitHelper>(
615                             mMultiAccessUnitIntf, linearPool);
616                 }
617             }
618         }
619     }
620     c2listener = mMultiAccessUnitHelper ?
621             std::make_shared<MultiAccessUnitListener>(self, mMultiAccessUnitHelper) :
622             std::make_shared<Listener>(self);
623 
624     c2_status_t res = mComponent->setListener_vb(c2listener, C2_DONT_BLOCK);
625     if (res != C2_OK) {
626         mInit = res;
627     }
628 
629     struct ListenerDeathRecipient : public HwDeathRecipient {
630         ListenerDeathRecipient(const wp<Component>& comp)
631             : mComponent{comp} {
632         }
633 
634         virtual void serviceDied(
635                 uint64_t /* cookie */,
636                 const wp<::android::hidl::base::V1_0::IBase>& /* who */
637                 ) override {
638             auto strongComponent = mComponent.promote();
639             if (strongComponent) {
640                 LOG(INFO) << "Client died ! notify and release the component !!";
641                 strongComponent->onDeathReceived();
642             } else {
643                 LOG(ERROR) << "Client died ! no component to release !!";
644             }
645         }
646 
647         wp<Component> mComponent;
648     };
649 
650     mDeathRecipient = new ListenerDeathRecipient(self);
651     Return<bool> transStatus = mListener->linkToDeath(
652             mDeathRecipient, 0);
653     if (!transStatus.isOk()) {
654         LOG(ERROR) << "Listener linkToDeath() transaction failed.";
655     }
656     if (!static_cast<bool>(transStatus)) {
657         LOG(DEBUG) << "Listener linkToDeath() call failed.";
658     }
659 }
660 
~Component()661 Component::~Component() {
662     std::list<std::shared_ptr<C2BufferQueueBlockPool>> bqPools;
663     {
664         std::lock_guard<std::mutex> lock(mBlockPoolsMutex);
665         transform_if(mBlockPools.begin(), mBlockPools.end(), std::back_inserter(bqPools),
666                      BqPoolFilterFn, BqPoolConvertFn);
667     }
668     if (!bqPools.empty()) {
669         LOG(ERROR) << "blockpools are not cleared yet at dtor";
670         std::shared_ptr<C2BqPoolInvalidateItem> bqInvalidateItem =
671                 std::make_shared<C2BqPoolInvalidateItem>(std::move(bqPools));
672         C2BqPoolInvalidator::getInstance().queue(bqInvalidateItem);
673     }
674     InputBufferManager::unregisterFrameData(mListener);
675     mStore->reportComponentDeath(this);
676 }
677 
678 }  // namespace utils
679 }  // namespace V1_0
680 }  // namespace c2
681 }  // namespace media
682 }  // namespace hardware
683 }  // namespace android
684 
685