xref: /aosp_15_r20/frameworks/av/media/codec2/sfplugin/CCodec.cpp (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
2  * Copyright (C) 2017 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 "CCodec"
19 #include <utils/Log.h>
20 
21 #include <sstream>
22 #include <thread>
23 
24 #include <android_media_codec.h>
25 
26 #include <C2Config.h>
27 #include <C2Debug.h>
28 #include <C2ParamInternal.h>
29 #include <C2PlatformSupport.h>
30 
31 #include <aidl/android/hardware/graphics/common/Dataspace.h>
32 #include <aidl/android/media/IAidlGraphicBufferSource.h>
33 #include <aidl/android/media/IAidlBufferSource.h>
34 #include <android/IOMXBufferSource.h>
35 #include <android/hardware/media/c2/1.0/IInputSurface.h>
36 #include <android/hardware/media/omx/1.0/IGraphicBufferSource.h>
37 #include <android/hardware/media/omx/1.0/IOmx.h>
38 #include <android-base/properties.h>
39 #include <android-base/stringprintf.h>
40 #include <cutils/properties.h>
41 #include <gui/IGraphicBufferProducer.h>
42 #include <gui/Surface.h>
43 #include <gui/bufferqueue/1.0/H2BGraphicBufferProducer.h>
44 #include <media/omx/1.0/WOmxNode.h>
45 #include <media/openmax/OMX_Core.h>
46 #include <media/openmax/OMX_IndexExt.h>
47 #include <media/stagefright/foundation/avc_utils.h>
48 #include <media/stagefright/foundation/AUtils.h>
49 #include <media/stagefright/aidlpersistentsurface/AidlGraphicBufferSource.h>
50 #include <media/stagefright/aidlpersistentsurface/C2NodeDef.h>
51 #include <media/stagefright/aidlpersistentsurface/wrapper/Conversion.h>
52 #include <media/stagefright/aidlpersistentsurface/wrapper/WAidlGraphicBufferSource.h>
53 #include <media/stagefright/omx/1.0/WGraphicBufferSource.h>
54 #include <media/stagefright/omx/OmxGraphicBufferSource.h>
55 #include <media/stagefright/CCodec.h>
56 #include <media/stagefright/BufferProducerWrapper.h>
57 #include <media/stagefright/MediaCodecConstants.h>
58 #include <media/stagefright/MediaCodecMetricsConstants.h>
59 #include <media/stagefright/PersistentSurface.h>
60 #include <media/stagefright/RenderedFrameInfo.h>
61 #include <utils/NativeHandle.h>
62 
63 #include "C2AidlNode.h"
64 #include "C2OMXNode.h"
65 #include "CCodecBufferChannel.h"
66 #include "CCodecConfig.h"
67 #include "Codec2Mapper.h"
68 #include "InputSurfaceWrapper.h"
69 
70 extern "C" android::PersistentSurface *CreateInputSurface();
71 
72 namespace android {
73 
74 using namespace std::chrono_literals;
75 using ::android::hardware::graphics::bufferqueue::V1_0::utils::H2BGraphicBufferProducer;
76 using android::base::StringPrintf;
77 using ::android::hardware::media::c2::V1_0::IInputSurface;
78 using ::aidl::android::media::IAidlBufferSource;
79 using ::aidl::android::media::IAidlNode;
80 using ::android::media::AidlGraphicBufferSource;
81 using ::android::media::WAidlGraphicBufferSource;
82 using ::android::media::aidl_conversion::fromAidlStatus;
83 
84 typedef hardware::media::omx::V1_0::IGraphicBufferSource HGraphicBufferSource;
85 typedef aidl::android::media::IAidlGraphicBufferSource AGraphicBufferSource;
86 typedef CCodecConfig Config;
87 
88 namespace {
89 
90 class CCodecWatchdog : public AHandler {
91 private:
92     enum {
93         kWhatWatch,
94     };
95     constexpr static int64_t kWatchIntervalUs = 3300000;  // 3.3 secs
96 
97 public:
getInstance()98     static sp<CCodecWatchdog> getInstance() {
99         static sp<CCodecWatchdog> sInstance = [] {
100             sp<CCodecWatchdog> instance = new CCodecWatchdog;
101             // the instance should never get destructed
102             instance->incStrong((void *)CCodecWatchdog::getInstance);
103             instance->init();
104             return instance;
105         }();
106         return sInstance;
107     }
108 
109     ~CCodecWatchdog() = default;
110 
watch(sp<CCodec> codec)111     void watch(sp<CCodec> codec) {
112         bool shouldPost = false;
113         {
114             Mutexed<std::set<wp<CCodec>>>::Locked codecs(mCodecsToWatch);
115             // If a watch message is in flight, piggy-back this instance as well.
116             // Otherwise, post a new watch message.
117             shouldPost = codecs->empty();
118             codecs->emplace(codec);
119         }
120         if (shouldPost) {
121             ALOGV("posting watch message");
122             (new AMessage(kWhatWatch, this))->post(kWatchIntervalUs);
123         }
124     }
125 
126 protected:
onMessageReceived(const sp<AMessage> & msg)127     void onMessageReceived(const sp<AMessage> &msg) {
128         switch (msg->what()) {
129             case kWhatWatch: {
130                 Mutexed<std::set<wp<CCodec>>>::Locked codecs(mCodecsToWatch);
131                 ALOGV("watch for %zu codecs", codecs->size());
132                 for (auto it = codecs->begin(); it != codecs->end(); ++it) {
133                     sp<CCodec> codec = it->promote();
134                     if (codec == nullptr) {
135                         continue;
136                     }
137                     codec->initiateReleaseIfStuck();
138                 }
139                 codecs->clear();
140                 break;
141             }
142 
143             default: {
144                 TRESPASS("CCodecWatchdog: unrecognized message");
145             }
146         }
147     }
148 
149 private:
CCodecWatchdog()150     CCodecWatchdog() : mLooper(new ALooper) {}
151 
init()152     void init() {
153         ALOGV("init");
154         mLooper->setName("CCodecWatchdog");
155         mLooper->registerHandler(this);
156         mLooper->start();
157     }
158 
159     sp<ALooper> mLooper;
160 
161     Mutexed<std::set<wp<CCodec>>> mCodecsToWatch;
162 };
163 
164 class C2InputSurfaceWrapper : public InputSurfaceWrapper {
165 public:
C2InputSurfaceWrapper(const std::shared_ptr<Codec2Client::InputSurface> & surface)166     explicit C2InputSurfaceWrapper(
167             const std::shared_ptr<Codec2Client::InputSurface> &surface) :
168         mSurface(surface) {
169     }
170 
171     ~C2InputSurfaceWrapper() override = default;
172 
connect(const std::shared_ptr<Codec2Client::Component> & comp)173     status_t connect(const std::shared_ptr<Codec2Client::Component> &comp) override {
174         if (mConnection != nullptr) {
175             return ALREADY_EXISTS;
176         }
177         return toStatusT(comp->connectToInputSurface(mSurface, &mConnection));
178     }
179 
disconnect()180     void disconnect() override {
181         if (mConnection != nullptr) {
182             mConnection->disconnect();
183             mConnection = nullptr;
184         }
185     }
186 
start()187     status_t start() override {
188         // InputSurface does not distinguish started state
189         return OK;
190     }
191 
signalEndOfInputStream()192     status_t signalEndOfInputStream() override {
193         C2InputSurfaceEosTuning eos(true);
194         std::vector<std::unique_ptr<C2SettingResult>> failures;
195         c2_status_t err = mSurface->config({&eos}, C2_MAY_BLOCK, &failures);
196         if (err != C2_OK) {
197             return UNKNOWN_ERROR;
198         }
199         return OK;
200     }
201 
configure(Config & config __unused)202     status_t configure(Config &config __unused) {
203         // TODO
204         return OK;
205     }
206 
207 private:
208     std::shared_ptr<Codec2Client::InputSurface> mSurface;
209     std::shared_ptr<Codec2Client::InputSurfaceConnection> mConnection;
210 };
211 
212 class HGraphicBufferSourceWrapper : public InputSurfaceWrapper {
213 public:
214     typedef hardware::media::omx::V1_0::Status OmxStatus;
215 
HGraphicBufferSourceWrapper(const sp<HGraphicBufferSource> & source,uint32_t width,uint32_t height,uint64_t usage)216     HGraphicBufferSourceWrapper(
217             const sp<HGraphicBufferSource> &source,
218             uint32_t width,
219             uint32_t height,
220             uint64_t usage)
221         : mSource(source), mWidth(width), mHeight(height) {
222         mDataSpace = HAL_DATASPACE_BT709;
223         mConfig.mUsage = usage;
224     }
225     ~HGraphicBufferSourceWrapper() override = default;
226 
connect(const std::shared_ptr<Codec2Client::Component> & comp)227     status_t connect(const std::shared_ptr<Codec2Client::Component> &comp) override {
228         Mutexed<sp<C2OMXNode>>::Locked node(mNode);
229         *node = new C2OMXNode(comp);
230         mOmxNode = new hardware::media::omx::V1_0::utils::TWOmxNode(*node);
231         (*node)->setFrameSize(mWidth, mHeight);
232         // Usage is queried during configure(), so setting it beforehand.
233         // 64 bit set parameter is existing only in C2OMXNode.
234         OMX_U64 usage64 = mConfig.mUsage;
235         status_t res = (*node)->setParameter(
236                 (OMX_INDEXTYPE)OMX_IndexParamConsumerUsageBits64,
237                 &usage64, sizeof(usage64));
238 
239         if (res != OK) {
240             OMX_U32 usage = mConfig.mUsage & 0xFFFFFFFF;
241             (void)(*node)->setParameter(
242                     (OMX_INDEXTYPE)OMX_IndexParamConsumerUsageBits,
243                     &usage, sizeof(usage));
244         }
245 
246         return GetStatus(mSource->configure(
247                 mOmxNode, static_cast<hardware::graphics::common::V1_0::Dataspace>(mDataSpace)));
248     }
249 
disconnect()250     void disconnect() override {
251         Mutexed<sp<C2OMXNode>>::Locked node(mNode);
252         if ((*node) == nullptr) {
253             return;
254         }
255         sp<IOMXBufferSource> source = (*node)->getSource();
256         if (source == nullptr) {
257             ALOGD("GBSWrapper::disconnect: node is not configured with OMXBufferSource.");
258             return;
259         }
260         source->onOmxIdle();
261         source->onOmxLoaded();
262         node->clear();
263         mOmxNode.clear();
264     }
265 
GetStatus(hardware::Return<OmxStatus> && status)266     status_t GetStatus(hardware::Return<OmxStatus> &&status) {
267         if (status.isOk()) {
268             return static_cast<status_t>(status.withDefault(OmxStatus::UNKNOWN_ERROR));
269         } else if (status.isDeadObject()) {
270             return DEAD_OBJECT;
271         }
272         return UNKNOWN_ERROR;
273     }
274 
start()275     status_t start() override {
276         Mutexed<sp<C2OMXNode>>::Locked node(mNode);
277         if ((*node) == nullptr) {
278             return NO_INIT;
279         }
280         sp<IOMXBufferSource> source = (*node)->getSource();
281         if (source == nullptr) {
282             return NO_INIT;
283         }
284 
285         size_t numSlots = 16;
286         constexpr OMX_U32 kPortIndexInput = 0;
287 
288         OMX_PARAM_PORTDEFINITIONTYPE param;
289         param.nPortIndex = kPortIndexInput;
290         status_t err = (*node)->getParameter(OMX_IndexParamPortDefinition,
291                                            &param, sizeof(param));
292         if (err == OK) {
293             numSlots = param.nBufferCountActual;
294         }
295 
296         for (size_t i = 0; i < numSlots; ++i) {
297             source->onInputBufferAdded(i);
298         }
299 
300         source->onOmxExecuting();
301         return OK;
302     }
303 
signalEndOfInputStream()304     status_t signalEndOfInputStream() override {
305         return GetStatus(mSource->signalEndOfInputStream());
306     }
307 
configure(Config & config)308     status_t configure(Config &config) {
309         Mutexed<sp<C2OMXNode>>::Locked node(mNode);
310         std::stringstream status;
311         status_t err = OK;
312 
313         // handle each configuration granually, in case we need to handle part of the configuration
314         // elsewhere
315 
316         // TRICKY: we do not unset frame delay repeating
317         if (config.mMinFps > 0 && config.mMinFps != mConfig.mMinFps) {
318             int64_t us = 1e6 / config.mMinFps + 0.5;
319             status_t res = GetStatus(mSource->setRepeatPreviousFrameDelayUs(us));
320             status << " minFps=" << config.mMinFps << " => repeatDelayUs=" << us;
321             if (res != OK) {
322                 status << " (=> " << asString(res) << ")";
323                 err = res;
324             }
325             mConfig.mMinFps = config.mMinFps;
326         }
327 
328         // pts gap
329         if (config.mMinAdjustedFps > 0 || config.mFixedAdjustedFps > 0) {
330             if ((*node) != nullptr) {
331                 OMX_PARAM_U32TYPE ptrGapParam = {};
332                 ptrGapParam.nSize = sizeof(OMX_PARAM_U32TYPE);
333                 float gap = (config.mMinAdjustedFps > 0)
334                         ? c2_min(INT32_MAX + 0., 1e6 / config.mMinAdjustedFps + 0.5)
335                         : c2_max(0. - INT32_MAX, -1e6 / config.mFixedAdjustedFps - 0.5);
336                 // float -> uint32_t is undefined if the value is negative.
337                 // First convert to int32_t to ensure the expected behavior.
338                 ptrGapParam.nU32 = int32_t(gap);
339                 (void)(*node)->setParameter(
340                         (OMX_INDEXTYPE)OMX_IndexParamMaxFrameDurationForBitrateControl,
341                         &ptrGapParam, sizeof(ptrGapParam));
342             }
343         }
344 
345         // max fps
346         // TRICKY: we do not unset max fps to 0 unless using fixed fps
347         if ((config.mMaxFps > 0 || (config.mFixedAdjustedFps > 0 && config.mMaxFps == -1))
348                 && config.mMaxFps != mConfig.mMaxFps) {
349             status_t res = GetStatus(mSource->setMaxFps(config.mMaxFps));
350             status << " maxFps=" << config.mMaxFps;
351             if (res != OK) {
352                 status << " (=> " << asString(res) << ")";
353                 err = res;
354             }
355             mConfig.mMaxFps = config.mMaxFps;
356         }
357 
358         if (config.mTimeOffsetUs != mConfig.mTimeOffsetUs) {
359             status_t res = GetStatus(mSource->setTimeOffsetUs(config.mTimeOffsetUs));
360             status << " timeOffset " << config.mTimeOffsetUs << "us";
361             if (res != OK) {
362                 status << " (=> " << asString(res) << ")";
363                 err = res;
364             }
365             mConfig.mTimeOffsetUs = config.mTimeOffsetUs;
366         }
367 
368         if (config.mCaptureFps != mConfig.mCaptureFps || config.mCodedFps != mConfig.mCodedFps) {
369             status_t res =
370                 GetStatus(mSource->setTimeLapseConfig(config.mCodedFps, config.mCaptureFps));
371             status << " timeLapse " << config.mCaptureFps << "fps as " << config.mCodedFps << "fps";
372             if (res != OK) {
373                 status << " (=> " << asString(res) << ")";
374                 err = res;
375             }
376             mConfig.mCaptureFps = config.mCaptureFps;
377             mConfig.mCodedFps = config.mCodedFps;
378         }
379 
380         if (config.mStartAtUs != mConfig.mStartAtUs
381                 || (config.mStopped != mConfig.mStopped && !config.mStopped)) {
382             status_t res = GetStatus(mSource->setStartTimeUs(config.mStartAtUs));
383             status << " start at " << config.mStartAtUs << "us";
384             if (res != OK) {
385                 status << " (=> " << asString(res) << ")";
386                 err = res;
387             }
388             mConfig.mStartAtUs = config.mStartAtUs;
389             mConfig.mStopped = config.mStopped;
390         }
391 
392         // suspend-resume
393         if (config.mSuspended != mConfig.mSuspended) {
394             status_t res = GetStatus(mSource->setSuspend(config.mSuspended, config.mSuspendAtUs));
395             status << " " << (config.mSuspended ? "suspend" : "resume")
396                     << " at " << config.mSuspendAtUs << "us";
397             if (res != OK) {
398                 status << " (=> " << asString(res) << ")";
399                 err = res;
400             }
401             mConfig.mSuspended = config.mSuspended;
402             mConfig.mSuspendAtUs = config.mSuspendAtUs;
403         }
404 
405         if (config.mStopped != mConfig.mStopped && config.mStopped) {
406             status_t res = GetStatus(mSource->setStopTimeUs(config.mStopAtUs));
407             status << " stop at " << config.mStopAtUs << "us";
408             if (res != OK) {
409                 status << " (=> " << asString(res) << ")";
410                 err = res;
411             } else {
412                 status << " delayUs";
413                 hardware::Return<void> trans = mSource->getStopTimeOffsetUs(
414                         [&res, &delayUs = config.mInputDelayUs](
415                                 auto status, auto stopTimeOffsetUs) {
416                             res = static_cast<status_t>(status);
417                             delayUs = stopTimeOffsetUs;
418                         });
419                 if (!trans.isOk()) {
420                     res = trans.isDeadObject() ? DEAD_OBJECT : UNKNOWN_ERROR;
421                 }
422                 if (res != OK) {
423                     status << " (=> " << asString(res) << ")";
424                 } else {
425                     status << "=" << config.mInputDelayUs << "us";
426                 }
427                 mConfig.mInputDelayUs = config.mInputDelayUs;
428             }
429             mConfig.mStopAtUs = config.mStopAtUs;
430             mConfig.mStopped = config.mStopped;
431         }
432 
433         // color aspects (android._color-aspects)
434 
435         // consumer usage is queried earlier.
436 
437         // priority
438         if (mConfig.mPriority != config.mPriority) {
439             if (config.mPriority != INT_MAX && (*node) != nullptr) {
440                 (*node)->setPriority(config.mPriority);
441             }
442             mConfig.mPriority = config.mPriority;
443         }
444 
445         if (status.str().empty()) {
446             ALOGD("ISConfig not changed");
447         } else {
448             ALOGD("ISConfig%s", status.str().c_str());
449         }
450         return err;
451     }
452 
onInputBufferDone(c2_cntr64_t index)453     void onInputBufferDone(c2_cntr64_t index) override {
454         Mutexed<sp<C2OMXNode>>::Locked node(mNode);
455         if ((*node) == nullptr) {
456             return;
457         }
458         (*node)->onInputBufferDone(index);
459     }
460 
onInputBufferEmptied()461     void onInputBufferEmptied() override {
462         Mutexed<sp<C2OMXNode>>::Locked node(mNode);
463         if ((*node) == nullptr) {
464             return;
465         }
466         (*node)->onInputBufferEmptied();
467     }
468 
getDataspace()469     android_dataspace getDataspace() override {
470         Mutexed<sp<C2OMXNode>>::Locked node(mNode);
471         if ((*node) == nullptr) {
472             return HAL_DATASPACE_UNKNOWN;
473         }
474         return (*node)->getDataspace();
475     }
476 
getPixelFormat()477     uint32_t getPixelFormat() override {
478         Mutexed<sp<C2OMXNode>>::Locked node(mNode);
479         if ((*node) == nullptr) {
480             return PIXEL_FORMAT_UNKNOWN;
481         }
482         return (*node)->getPixelFormat();
483     }
484 
485 private:
486     sp<HGraphicBufferSource> mSource;
487     Mutexed<sp<C2OMXNode>> mNode;
488     sp<hardware::media::omx::V1_0::IOmxNode> mOmxNode;
489     uint32_t mWidth;
490     uint32_t mHeight;
491     Config mConfig;
492 };
493 
494 class AGraphicBufferSourceWrapper : public InputSurfaceWrapper {
495 public:
AGraphicBufferSourceWrapper(const std::shared_ptr<AGraphicBufferSource> & source,uint32_t width,uint32_t height,uint64_t usage)496     AGraphicBufferSourceWrapper(
497             const std::shared_ptr<AGraphicBufferSource> &source,
498             uint32_t width,
499             uint32_t height,
500             uint64_t usage)
501         : mSource(source), mWidth(width), mHeight(height) {
502         mDataSpace = HAL_DATASPACE_BT709;
503         mConfig.mUsage = usage;
504     }
505     ~AGraphicBufferSourceWrapper() override = default;
506 
connect(const std::shared_ptr<Codec2Client::Component> & comp)507     status_t connect(const std::shared_ptr<Codec2Client::Component> &comp) override {
508         Mutexed<std::shared_ptr<C2AidlNode>>::Locked node(mNode);
509         *node = ::ndk::SharedRefBase::make<C2AidlNode>(comp);
510         (*node)->setFrameSize(mWidth, mHeight);
511         // Usage is queried during configure(), so setting it beforehand.
512         uint64_t usage = mConfig.mUsage;
513         (void)(*node)->setConsumerUsage((int64_t)usage);
514 
515         // AIDL does not define legacy dataspace.
516         android_dataspace_t dataspace = mDataSpace;
517         if (android::media::codec::provider_->dataspace_v0_partial()) {
518             ColorUtils::convertDataSpaceToV0(dataspace);
519         }
520         return fromAidlStatus(mSource->configure(
521                 (*node), static_cast<::aidl::android::hardware::graphics::common::Dataspace>(
522                         dataspace)));
523     }
524 
disconnect()525     void disconnect() override {
526         Mutexed<std::shared_ptr<C2AidlNode>>::Locked node(mNode);
527         if ((*node) == nullptr) {
528             return;
529         }
530         std::shared_ptr<IAidlBufferSource> source = (*node)->getSource();
531         if (source == nullptr) {
532             ALOGD("GBSWrapper::disconnect: node is not configured with OMXBufferSource.");
533             return;
534         }
535         (void)source->onStop();
536         (void)source->onRelease();
537         node->reset();
538     }
539 
start()540     status_t start() override {
541         Mutexed<std::shared_ptr<C2AidlNode>>::Locked node(mNode);
542         if ((*node) == nullptr) {
543             return NO_INIT;
544         }
545         std::shared_ptr<IAidlBufferSource> source = (*node)->getSource();
546         if (source == nullptr) {
547             return NO_INIT;
548         }
549 
550         size_t numSlots = 16;
551 
552         IAidlNode::InputBufferParams param;
553         status_t err = fromAidlStatus((*node)->getInputBufferParams(&param));
554         if (err == OK) {
555             numSlots = param.bufferCountActual;
556         }
557 
558         for (size_t i = 0; i < numSlots; ++i) {
559             (void)source->onInputBufferAdded(i);
560         }
561 
562         (void)source->onStart();
563         return OK;
564     }
565 
signalEndOfInputStream()566     status_t signalEndOfInputStream() override {
567         return fromAidlStatus(mSource->signalEndOfInputStream());
568     }
569 
configure(Config & config)570     status_t configure(Config &config) {
571         Mutexed<std::shared_ptr<C2AidlNode>>::Locked node(mNode);
572         std::stringstream status;
573         status_t err = OK;
574 
575         // handle each configuration granually, in case we need to handle part of the configuration
576         // elsewhere
577 
578         // TRICKY: we do not unset frame delay repeating
579         if (config.mMinFps > 0 && config.mMinFps != mConfig.mMinFps) {
580             int64_t us = 1e6 / config.mMinFps + 0.5;
581             status_t res = fromAidlStatus(mSource->setRepeatPreviousFrameDelayUs(us));
582             status << " minFps=" << config.mMinFps << " => repeatDelayUs=" << us;
583             if (res != OK) {
584                 status << " (=> " << asString(res) << ")";
585                 err = res;
586             }
587             mConfig.mMinFps = config.mMinFps;
588         }
589 
590         // pts gap
591         if (config.mMinAdjustedFps > 0 || config.mFixedAdjustedFps > 0) {
592             if ((*node) != nullptr) {
593                 float gap = (config.mMinAdjustedFps > 0)
594                         ? c2_min(INT32_MAX + 0., 1e6 / config.mMinAdjustedFps + 0.5)
595                         : c2_max(0. - INT32_MAX, -1e6 / config.mFixedAdjustedFps - 0.5);
596                 // float -> uint32_t is undefined if the value is negative.
597                 // First convert to int32_t to ensure the expected behavior.
598                 int32_t gapUs = int32_t(gap);
599                 (void)(*node)->setAdjustTimestampGapUs(gapUs);
600             }
601         }
602 
603         // max fps
604         // TRICKY: we do not unset max fps to 0 unless using fixed fps
605         if ((config.mMaxFps > 0 || (config.mFixedAdjustedFps > 0 && config.mMaxFps == -1))
606                 && config.mMaxFps != mConfig.mMaxFps) {
607             status_t res = fromAidlStatus(mSource->setMaxFps(config.mMaxFps));
608             status << " maxFps=" << config.mMaxFps;
609             if (res != OK) {
610                 status << " (=> " << asString(res) << ")";
611                 err = res;
612             }
613             mConfig.mMaxFps = config.mMaxFps;
614         }
615 
616         if (config.mTimeOffsetUs != mConfig.mTimeOffsetUs) {
617             status_t res = fromAidlStatus(mSource->setTimeOffsetUs(config.mTimeOffsetUs));
618             status << " timeOffset " << config.mTimeOffsetUs << "us";
619             if (res != OK) {
620                 status << " (=> " << asString(res) << ")";
621                 err = res;
622             }
623             mConfig.mTimeOffsetUs = config.mTimeOffsetUs;
624         }
625 
626         if (config.mCaptureFps != mConfig.mCaptureFps || config.mCodedFps != mConfig.mCodedFps) {
627             status_t res =
628                 fromAidlStatus(mSource->setTimeLapseConfig(config.mCodedFps, config.mCaptureFps));
629             status << " timeLapse " << config.mCaptureFps << "fps as " << config.mCodedFps << "fps";
630             if (res != OK) {
631                 status << " (=> " << asString(res) << ")";
632                 err = res;
633             }
634             mConfig.mCaptureFps = config.mCaptureFps;
635             mConfig.mCodedFps = config.mCodedFps;
636         }
637 
638         if (config.mStartAtUs != mConfig.mStartAtUs
639                 || (config.mStopped != mConfig.mStopped && !config.mStopped)) {
640             status_t res = fromAidlStatus(mSource->setStartTimeUs(config.mStartAtUs));
641             status << " start at " << config.mStartAtUs << "us";
642             if (res != OK) {
643                 status << " (=> " << asString(res) << ")";
644                 err = res;
645             }
646             mConfig.mStartAtUs = config.mStartAtUs;
647             mConfig.mStopped = config.mStopped;
648         }
649 
650         // suspend-resume
651         if (config.mSuspended != mConfig.mSuspended) {
652             status_t res = fromAidlStatus(mSource->setSuspend(
653                     config.mSuspended, config.mSuspendAtUs));
654             status << " " << (config.mSuspended ? "suspend" : "resume")
655                     << " at " << config.mSuspendAtUs << "us";
656             if (res != OK) {
657                 status << " (=> " << asString(res) << ")";
658                 err = res;
659             }
660             mConfig.mSuspended = config.mSuspended;
661             mConfig.mSuspendAtUs = config.mSuspendAtUs;
662         }
663 
664         if (config.mStopped != mConfig.mStopped && config.mStopped) {
665             status_t res = fromAidlStatus(mSource->setStopTimeUs(config.mStopAtUs));
666             status << " stop at " << config.mStopAtUs << "us";
667             if (res != OK) {
668                 status << " (=> " << asString(res) << ")";
669                 err = res;
670             } else {
671                 status << " delayUs";
672                 res = fromAidlStatus(mSource->getStopTimeOffsetUs(&config.mInputDelayUs));
673                 if (res != OK) {
674                     status << " (=> " << asString(res) << ")";
675                 } else {
676                     status << "=" << config.mInputDelayUs << "us";
677                 }
678                 mConfig.mInputDelayUs = config.mInputDelayUs;
679             }
680             mConfig.mStopAtUs = config.mStopAtUs;
681             mConfig.mStopped = config.mStopped;
682         }
683 
684         // color aspects (android._color-aspects)
685 
686         // consumer usage is queried earlier.
687 
688         // priority
689         if (mConfig.mPriority != config.mPriority) {
690             if (config.mPriority != INT_MAX) {
691                 (*node)->setPriority(config.mPriority);
692             }
693             mConfig.mPriority = config.mPriority;
694         }
695 
696         if (status.str().empty()) {
697             ALOGD("ISConfig not changed");
698         } else {
699             ALOGD("ISConfig%s", status.str().c_str());
700         }
701         return err;
702     }
703 
onInputBufferDone(c2_cntr64_t index)704     void onInputBufferDone(c2_cntr64_t index) override {
705         Mutexed<std::shared_ptr<C2AidlNode>>::Locked node(mNode);
706         if ((*node) == nullptr) {
707             return;
708         }
709         (*node)->onInputBufferDone(index);
710     }
711 
onInputBufferEmptied()712     void onInputBufferEmptied() override {
713         Mutexed<std::shared_ptr<C2AidlNode>>::Locked node(mNode);
714         if ((*node) == nullptr) {
715             return;
716         }
717         (*node)->onInputBufferEmptied();
718     }
719 
getDataspace()720     android_dataspace getDataspace() override {
721         Mutexed<std::shared_ptr<C2AidlNode>>::Locked node(mNode);
722         if ((*node) == nullptr) {
723             return HAL_DATASPACE_UNKNOWN;
724         }
725         return (*node)->getDataspace();
726     }
727 
getPixelFormat()728     uint32_t getPixelFormat() override {
729         Mutexed<std::shared_ptr<C2AidlNode>>::Locked node(mNode);
730         if ((*node) == nullptr) {
731             return PIXEL_FORMAT_UNKNOWN;
732         }
733         return (*node)->getPixelFormat();
734     }
735 
736 private:
737     std::shared_ptr<AGraphicBufferSource> mSource;
738     Mutexed<std::shared_ptr<C2AidlNode>> mNode;
739     uint32_t mWidth;
740     uint32_t mHeight;
741     Config mConfig;
742 };
743 
744 class Codec2ClientInterfaceWrapper : public C2ComponentStore {
745     std::shared_ptr<Codec2Client> mClient;
746 
747 public:
Codec2ClientInterfaceWrapper(std::shared_ptr<Codec2Client> client)748     Codec2ClientInterfaceWrapper(std::shared_ptr<Codec2Client> client)
749         : mClient(client) { }
750 
751     virtual ~Codec2ClientInterfaceWrapper() = default;
752 
config_sm(const std::vector<C2Param * > & params,std::vector<std::unique_ptr<C2SettingResult>> * const failures)753     virtual c2_status_t config_sm(
754             const std::vector<C2Param *> &params,
755             std::vector<std::unique_ptr<C2SettingResult>> *const failures) {
756         return mClient->config(params, C2_MAY_BLOCK, failures);
757     };
758 
copyBuffer(std::shared_ptr<C2GraphicBuffer>,std::shared_ptr<C2GraphicBuffer>)759     virtual c2_status_t copyBuffer(
760             std::shared_ptr<C2GraphicBuffer>,
761             std::shared_ptr<C2GraphicBuffer>) {
762         return C2_OMITTED;
763     }
764 
createComponent(C2String,std::shared_ptr<C2Component> * const component)765     virtual c2_status_t createComponent(
766             C2String, std::shared_ptr<C2Component> *const component) {
767         component->reset();
768         return C2_OMITTED;
769     }
770 
createInterface(C2String,std::shared_ptr<C2ComponentInterface> * const interface)771     virtual c2_status_t createInterface(
772             C2String, std::shared_ptr<C2ComponentInterface> *const interface) {
773         interface->reset();
774         return C2_OMITTED;
775     }
776 
query_sm(const std::vector<C2Param * > & stackParams,const std::vector<C2Param::Index> & heapParamIndices,std::vector<std::unique_ptr<C2Param>> * const heapParams) const777     virtual c2_status_t query_sm(
778             const std::vector<C2Param *> &stackParams,
779             const std::vector<C2Param::Index> &heapParamIndices,
780             std::vector<std::unique_ptr<C2Param>> *const heapParams) const {
781         return mClient->query(stackParams, heapParamIndices, C2_MAY_BLOCK, heapParams);
782     }
783 
querySupportedParams_nb(std::vector<std::shared_ptr<C2ParamDescriptor>> * const params) const784     virtual c2_status_t querySupportedParams_nb(
785             std::vector<std::shared_ptr<C2ParamDescriptor>> *const params) const {
786         return mClient->querySupportedParams(params);
787     }
788 
querySupportedValues_sm(std::vector<C2FieldSupportedValuesQuery> & fields) const789     virtual c2_status_t querySupportedValues_sm(
790             std::vector<C2FieldSupportedValuesQuery> &fields) const {
791         return mClient->querySupportedValues(fields, C2_MAY_BLOCK);
792     }
793 
getName() const794     virtual C2String getName() const {
795         return mClient->getName();
796     }
797 
getParamReflector() const798     virtual std::shared_ptr<C2ParamReflector> getParamReflector() const {
799         return mClient->getParamReflector();
800     }
801 
listComponents()802     virtual std::vector<std::shared_ptr<const C2Component::Traits>> listComponents() {
803         return std::vector<std::shared_ptr<const C2Component::Traits>>();
804     }
805 };
806 
RevertOutputFormatIfNeeded(const sp<AMessage> & oldFormat,sp<AMessage> & currentFormat)807 void RevertOutputFormatIfNeeded(
808         const sp<AMessage> &oldFormat, sp<AMessage> &currentFormat) {
809     // We used to not report changes to these keys to the client.
810     const static std::set<std::string> sIgnoredKeys({
811             KEY_BIT_RATE,
812             KEY_FRAME_RATE,
813             KEY_MAX_BIT_RATE,
814             KEY_MAX_WIDTH,
815             KEY_MAX_HEIGHT,
816             "csd-0",
817             "csd-1",
818             "csd-2",
819     });
820     if (currentFormat == oldFormat) {
821         return;
822     }
823     sp<AMessage> diff = currentFormat->changesFrom(oldFormat);
824     AMessage::Type type;
825     for (size_t i = diff->countEntries(); i > 0; --i) {
826         if (sIgnoredKeys.count(diff->getEntryNameAt(i - 1, &type)) > 0) {
827             diff->removeEntryAt(i - 1);
828         }
829     }
830     if (diff->countEntries() == 0) {
831         currentFormat = oldFormat;
832     }
833 }
834 
AmendOutputFormatWithCodecSpecificData(const uint8_t * data,size_t size,const std::string & mediaType,const sp<AMessage> & outputFormat)835 void AmendOutputFormatWithCodecSpecificData(
836         const uint8_t *data, size_t size, const std::string &mediaType,
837         const sp<AMessage> &outputFormat) {
838     if (mediaType == MIMETYPE_VIDEO_AVC) {
839         // Codec specific data should be SPS and PPS in a single buffer,
840         // each prefixed by a startcode (0x00 0x00 0x00 0x01).
841         // We separate the two and put them into the output format
842         // under the keys "csd-0" and "csd-1".
843 
844         unsigned csdIndex = 0;
845 
846         const uint8_t *nalStart;
847         size_t nalSize;
848         while (getNextNALUnit(&data, &size, &nalStart, &nalSize, true) == OK) {
849             sp<ABuffer> csd = new ABuffer(nalSize + 4);
850             memcpy(csd->data(), "\x00\x00\x00\x01", 4);
851             memcpy(csd->data() + 4, nalStart, nalSize);
852 
853             outputFormat->setBuffer(
854                     AStringPrintf("csd-%u", csdIndex).c_str(), csd);
855 
856             ++csdIndex;
857         }
858 
859         if (csdIndex != 2) {
860             ALOGW("Expected two NAL units from AVC codec config, but %u found",
861                     csdIndex);
862         }
863     } else {
864         // For everything else we just stash the codec specific data into
865         // the output format as a single piece of csd under "csd-0".
866         sp<ABuffer> csd = new ABuffer(size);
867         memcpy(csd->data(), data, size);
868         csd->setRange(0, size);
869         outputFormat->setBuffer("csd-0", csd);
870     }
871 }
872 
873 }  // namespace
874 
875 // CCodec::ClientListener
876 
877 struct CCodec::ClientListener : public Codec2Client::Listener {
878 
ClientListenerandroid::CCodec::ClientListener879     explicit ClientListener(const wp<CCodec> &codec) : mCodec(codec) {}
880 
onWorkDoneandroid::CCodec::ClientListener881     virtual void onWorkDone(
882             const std::weak_ptr<Codec2Client::Component>& component,
883             std::list<std::unique_ptr<C2Work>>& workItems) override {
884         (void)component;
885         sp<CCodec> codec(mCodec.promote());
886         if (!codec) {
887             return;
888         }
889         codec->onWorkDone(workItems);
890     }
891 
onTrippedandroid::CCodec::ClientListener892     virtual void onTripped(
893             const std::weak_ptr<Codec2Client::Component>& component,
894             const std::vector<std::shared_ptr<C2SettingResult>>& settingResult
895             ) override {
896         // TODO
897         (void)component;
898         (void)settingResult;
899     }
900 
onErrorandroid::CCodec::ClientListener901     virtual void onError(
902             const std::weak_ptr<Codec2Client::Component>& component,
903             uint32_t errorCode) override {
904         {
905             // Component is only used for reporting as we use a separate listener for each instance
906             std::shared_ptr<Codec2Client::Component> comp = component.lock();
907             if (!comp) {
908                 ALOGD("Component died with error: 0x%x", errorCode);
909             } else {
910                 ALOGD("Component \"%s\" returned error: 0x%x", comp->getName().c_str(), errorCode);
911             }
912         }
913 
914         // Report to MediaCodec
915         // Note: for now we do not propagate the error code to MediaCodec
916         // except for C2_NO_MEMORY, as we would need to translate to a MediaCodec error.
917         sp<CCodec> codec(mCodec.promote());
918         if (!codec || !codec->mCallback) {
919             return;
920         }
921         codec->mCallback->onError(
922                 errorCode == C2_NO_MEMORY ? NO_MEMORY : UNKNOWN_ERROR,
923                 ACTION_CODE_FATAL);
924     }
925 
onDeathandroid::CCodec::ClientListener926     virtual void onDeath(
927             const std::weak_ptr<Codec2Client::Component>& component) override {
928         { // Log the death of the component.
929             std::shared_ptr<Codec2Client::Component> comp = component.lock();
930             if (!comp) {
931                 ALOGE("Codec2 component died.");
932             } else {
933                 ALOGE("Codec2 component \"%s\" died.", comp->getName().c_str());
934             }
935         }
936 
937         // Report to MediaCodec.
938         sp<CCodec> codec(mCodec.promote());
939         if (!codec || !codec->mCallback) {
940             return;
941         }
942         codec->mCallback->onError(DEAD_OBJECT, ACTION_CODE_FATAL);
943     }
944 
onFrameRenderedandroid::CCodec::ClientListener945     virtual void onFrameRendered(uint64_t bufferQueueId,
946                                  int32_t slotId,
947                                  int64_t timestampNs) override {
948         // TODO: implement
949         (void)bufferQueueId;
950         (void)slotId;
951         (void)timestampNs;
952     }
953 
onInputBufferDoneandroid::CCodec::ClientListener954     virtual void onInputBufferDone(
955             uint64_t frameIndex, size_t arrayIndex) override {
956         sp<CCodec> codec(mCodec.promote());
957         if (codec) {
958             codec->onInputBufferDone(frameIndex, arrayIndex);
959         }
960     }
961 
962 private:
963     wp<CCodec> mCodec;
964 };
965 
966 // CCodecCallbackImpl
967 
968 class CCodecCallbackImpl : public CCodecCallback {
969 public:
CCodecCallbackImpl(CCodec * codec)970     explicit CCodecCallbackImpl(CCodec *codec) : mCodec(codec) {}
971     ~CCodecCallbackImpl() override = default;
972 
onError(status_t err,enum ActionCode actionCode)973     void onError(status_t err, enum ActionCode actionCode) override {
974         mCodec->mCallback->onError(err, actionCode);
975     }
976 
onOutputFramesRendered(int64_t mediaTimeUs,nsecs_t renderTimeNs)977     void onOutputFramesRendered(int64_t mediaTimeUs, nsecs_t renderTimeNs) override {
978         mCodec->mCallback->onOutputFramesRendered({RenderedFrameInfo(mediaTimeUs, renderTimeNs)});
979     }
980 
onOutputBuffersChanged()981     void onOutputBuffersChanged() override {
982         mCodec->mCallback->onOutputBuffersChanged();
983     }
984 
onFirstTunnelFrameReady()985     void onFirstTunnelFrameReady() override {
986         mCodec->mCallback->onFirstTunnelFrameReady();
987     }
988 
989 private:
990     CCodec *mCodec;
991 };
992 
993 // CCodec
994 
CCodec()995 CCodec::CCodec()
996     : mChannel(new CCodecBufferChannel(std::make_shared<CCodecCallbackImpl>(this))),
997       mConfig(new CCodecConfig) {
998 }
999 
~CCodec()1000 CCodec::~CCodec() {
1001 }
1002 
getBufferChannel()1003 std::shared_ptr<BufferChannelBase> CCodec::getBufferChannel() {
1004     return mChannel;
1005 }
1006 
tryAndReportOnError(std::function<status_t ()> job)1007 status_t CCodec::tryAndReportOnError(std::function<status_t()> job) {
1008     status_t err = job();
1009     if (err != C2_OK) {
1010         mCallback->onError(err, ACTION_CODE_FATAL);
1011     }
1012     return err;
1013 }
1014 
initiateAllocateComponent(const sp<AMessage> & msg)1015 void CCodec::initiateAllocateComponent(const sp<AMessage> &msg) {
1016     auto setAllocating = [this] {
1017         Mutexed<State>::Locked state(mState);
1018         if (state->get() != RELEASED) {
1019             return INVALID_OPERATION;
1020         }
1021         state->set(ALLOCATING);
1022         return OK;
1023     };
1024     if (tryAndReportOnError(setAllocating) != OK) {
1025         return;
1026     }
1027 
1028     sp<RefBase> codecInfo;
1029     CHECK(msg->findObject("codecInfo", &codecInfo));
1030     // For Codec 2.0 components, componentName == codecInfo->getCodecName().
1031 
1032     sp<AMessage> allocMsg(new AMessage(kWhatAllocate, this));
1033     allocMsg->setObject("codecInfo", codecInfo);
1034     allocMsg->post();
1035 }
1036 
allocate(const sp<MediaCodecInfo> & codecInfo)1037 void CCodec::allocate(const sp<MediaCodecInfo> &codecInfo) {
1038     if (codecInfo == nullptr) {
1039         mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1040         return;
1041     }
1042     ALOGD("allocate(%s)", codecInfo->getCodecName());
1043     mClientListener.reset(new ClientListener(this));
1044 
1045     AString componentName = codecInfo->getCodecName();
1046     std::shared_ptr<Codec2Client> client;
1047 
1048     // set up preferred component store to access vendor store parameters
1049     client = Codec2Client::CreateFromService("default");
1050     if (client) {
1051         ALOGI("setting up '%s' as default (vendor) store", client->getServiceName().c_str());
1052         SetPreferredCodec2ComponentStore(
1053                 std::make_shared<Codec2ClientInterfaceWrapper>(client));
1054     }
1055 
1056     std::shared_ptr<Codec2Client::Component> comp;
1057     c2_status_t status = Codec2Client::CreateComponentByName(
1058             componentName.c_str(),
1059             mClientListener,
1060             &comp,
1061             &client);
1062     if (status != C2_OK) {
1063         ALOGE("Failed Create component: %s, error=%d", componentName.c_str(), status);
1064         Mutexed<State>::Locked state(mState);
1065         state->set(RELEASED);
1066         state.unlock();
1067         mCallback->onError((status == C2_NO_MEMORY ? NO_MEMORY : UNKNOWN_ERROR), ACTION_CODE_FATAL);
1068         state.lock();
1069         return;
1070     }
1071     ALOGI("Created component [%s]", componentName.c_str());
1072     mChannel->setComponent(comp);
1073     auto setAllocated = [this, comp, client] {
1074         Mutexed<State>::Locked state(mState);
1075         if (state->get() != ALLOCATING) {
1076             state->set(RELEASED);
1077             return UNKNOWN_ERROR;
1078         }
1079         state->set(ALLOCATED);
1080         state->comp = comp;
1081         mClient = client;
1082         return OK;
1083     };
1084     if (tryAndReportOnError(setAllocated) != OK) {
1085         return;
1086     }
1087 
1088     // initialize config here in case setParameters is called prior to configure
1089     Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1090     const std::unique_ptr<Config> &config = *configLocked;
1091     status_t err = config->initialize(mClient->getParamReflector(), comp);
1092     if (err != OK) {
1093         ALOGW("Failed to initialize configuration support");
1094         // TODO: report error once we complete implementation.
1095     }
1096     config->queryConfiguration(comp);
1097 
1098     mCallback->onComponentAllocated(componentName.c_str());
1099 }
1100 
initiateConfigureComponent(const sp<AMessage> & format)1101 void CCodec::initiateConfigureComponent(const sp<AMessage> &format) {
1102     auto checkAllocated = [this] {
1103         Mutexed<State>::Locked state(mState);
1104         return (state->get() != ALLOCATED) ? UNKNOWN_ERROR : OK;
1105     };
1106     if (tryAndReportOnError(checkAllocated) != OK) {
1107         return;
1108     }
1109 
1110     sp<AMessage> msg(new AMessage(kWhatConfigure, this));
1111     msg->setMessage("format", format);
1112     msg->post();
1113 }
1114 
configure(const sp<AMessage> & msg)1115 void CCodec::configure(const sp<AMessage> &msg) {
1116     std::shared_ptr<Codec2Client::Component> comp;
1117     auto checkAllocated = [this, &comp] {
1118         Mutexed<State>::Locked state(mState);
1119         if (state->get() != ALLOCATED) {
1120             state->set(RELEASED);
1121             return UNKNOWN_ERROR;
1122         }
1123         comp = state->comp;
1124         return OK;
1125     };
1126     if (tryAndReportOnError(checkAllocated) != OK) {
1127         return;
1128     }
1129 
1130     auto doConfig = [msg, comp, this]() -> status_t {
1131         AString mime;
1132         if (!msg->findString("mime", &mime)) {
1133             return BAD_VALUE;
1134         }
1135 
1136         int32_t encoder;
1137         if (!msg->findInt32("encoder", &encoder)) {
1138             encoder = false;
1139         }
1140 
1141         int32_t flags;
1142         if (!msg->findInt32("flags", &flags)) {
1143             return BAD_VALUE;
1144         }
1145 
1146         // TODO: read from intf()
1147         if ((!encoder) != (comp->getName().find("encoder") == std::string::npos)) {
1148             return UNKNOWN_ERROR;
1149         }
1150 
1151         int32_t storeMeta;
1152         if (encoder
1153                 && msg->findInt32("android._input-metadata-buffer-type", &storeMeta)
1154                 && storeMeta != kMetadataBufferTypeInvalid) {
1155             if (storeMeta != kMetadataBufferTypeANWBuffer) {
1156                 ALOGD("Only ANW buffers are supported for legacy metadata mode");
1157                 return BAD_VALUE;
1158             }
1159             mChannel->setMetaMode(CCodecBufferChannel::MODE_ANW);
1160         }
1161 
1162         status_t err = OK;
1163         sp<RefBase> obj;
1164         sp<Surface> surface;
1165         if (msg->findObject("native-window", &obj)) {
1166             surface = static_cast<Surface *>(obj.get());
1167             int32_t generation;
1168             (void)msg->findInt32("native-window-generation", &generation);
1169             // setup tunneled playback
1170             if (surface != nullptr) {
1171                 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1172                 const std::unique_ptr<Config> &config = *configLocked;
1173                 if ((config->mDomain & Config::IS_DECODER)
1174                         && (config->mDomain & Config::IS_VIDEO)) {
1175                     int32_t tunneled;
1176                     if (msg->findInt32("feature-tunneled-playback", &tunneled) && tunneled != 0) {
1177                         ALOGI("Configuring TUNNELED video playback.");
1178 
1179                         err = configureTunneledVideoPlayback(comp, &config->mSidebandHandle, msg);
1180                         if (err != OK) {
1181                             ALOGE("configureTunneledVideoPlayback failed!");
1182                             return err;
1183                         }
1184                         config->mTunneled = true;
1185                     }
1186 
1187                     int32_t pushBlankBuffersOnStop = 0;
1188                     if (msg->findInt32(KEY_PUSH_BLANK_BUFFERS_ON_STOP, &pushBlankBuffersOnStop)) {
1189                         config->mPushBlankBuffersOnStop = pushBlankBuffersOnStop == 1;
1190                     }
1191                     // secure compoment or protected content default with
1192                     // "push-blank-buffers-on-shutdown" flag
1193                     if (!config->mPushBlankBuffersOnStop) {
1194                         int32_t usageProtected;
1195                         if (comp->getName().find(".secure") != std::string::npos) {
1196                             config->mPushBlankBuffersOnStop = true;
1197                         } else if (msg->findInt32("protected", &usageProtected) && usageProtected) {
1198                             config->mPushBlankBuffersOnStop = true;
1199                         }
1200                     }
1201                 }
1202             }
1203             setSurface(surface, (uint32_t)generation);
1204         }
1205 
1206         Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1207         const std::unique_ptr<Config> &config = *configLocked;
1208         config->mUsingSurface = surface != nullptr;
1209         config->mBuffersBoundToCodec = ((flags & CONFIGURE_FLAG_USE_BLOCK_MODEL) == 0);
1210         ALOGD("[%s] buffers are %sbound to CCodec for this session",
1211               comp->getName().c_str(), config->mBuffersBoundToCodec ? "" : "not ");
1212 
1213         // Enforce required parameters
1214         int32_t i32;
1215         float flt;
1216         if (config->mDomain & Config::IS_AUDIO) {
1217             if (!msg->findInt32(KEY_SAMPLE_RATE, &i32)) {
1218                 ALOGD("sample rate is missing, which is required for audio components.");
1219                 return BAD_VALUE;
1220             }
1221             if (!msg->findInt32(KEY_CHANNEL_COUNT, &i32)) {
1222                 ALOGD("channel count is missing, which is required for audio components.");
1223                 return BAD_VALUE;
1224             }
1225             if ((config->mDomain & Config::IS_ENCODER)
1226                     && !mime.equalsIgnoreCase(MEDIA_MIMETYPE_AUDIO_FLAC)
1227                     && !msg->findInt32(KEY_BIT_RATE, &i32)
1228                     && !msg->findFloat(KEY_BIT_RATE, &flt)) {
1229                 ALOGD("bitrate is missing, which is required for audio encoders.");
1230                 return BAD_VALUE;
1231             }
1232         }
1233         int32_t width = 0;
1234         int32_t height = 0;
1235         if (config->mDomain & (Config::IS_IMAGE | Config::IS_VIDEO)) {
1236             if (!msg->findInt32(KEY_WIDTH, &width)) {
1237                 ALOGD("width is missing, which is required for image/video components.");
1238                 return BAD_VALUE;
1239             }
1240             if (!msg->findInt32(KEY_HEIGHT, &height)) {
1241                 ALOGD("height is missing, which is required for image/video components.");
1242                 return BAD_VALUE;
1243             }
1244             if ((config->mDomain & Config::IS_ENCODER) && (config->mDomain & Config::IS_VIDEO)) {
1245                 int32_t mode = BITRATE_MODE_VBR;
1246                 if (msg->findInt32(KEY_BITRATE_MODE, &mode) && mode == BITRATE_MODE_CQ) {
1247                     if (!msg->findInt32(KEY_QUALITY, &i32)) {
1248                         ALOGD("quality is missing, which is required for video encoders in CQ.");
1249                         return BAD_VALUE;
1250                     }
1251                 } else {
1252                     if (!msg->findInt32(KEY_BIT_RATE, &i32)
1253                             && !msg->findFloat(KEY_BIT_RATE, &flt)) {
1254                         ALOGD("bitrate is missing, which is required for video encoders.");
1255                         return BAD_VALUE;
1256                     }
1257                 }
1258                 if (!msg->findInt32(KEY_I_FRAME_INTERVAL, &i32)
1259                         && !msg->findFloat(KEY_I_FRAME_INTERVAL, &flt)) {
1260                     ALOGD("I frame interval is missing, which is required for video encoders.");
1261                     return BAD_VALUE;
1262                 }
1263                 if (!msg->findInt32(KEY_FRAME_RATE, &i32)
1264                         && !msg->findFloat(KEY_FRAME_RATE, &flt)) {
1265                     ALOGD("frame rate is missing, which is required for video encoders.");
1266                     return BAD_VALUE;
1267                 }
1268             }
1269         }
1270 
1271         /*
1272          * Handle input surface configuration
1273          */
1274         if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))
1275                 && (config->mDomain & Config::IS_ENCODER)) {
1276             config->mISConfig.reset(new InputSurfaceWrapper::Config{});
1277             {
1278                 config->mISConfig->mMinFps = 0;
1279                 int64_t value;
1280                 if (msg->findInt64(KEY_REPEAT_PREVIOUS_FRAME_AFTER, &value) && value > 0) {
1281                     config->mISConfig->mMinFps = 1e6 / value;
1282                 }
1283                 if (!msg->findFloat(
1284                         KEY_MAX_FPS_TO_ENCODER, &config->mISConfig->mMaxFps)) {
1285                     config->mISConfig->mMaxFps = -1;
1286                 }
1287                 config->mISConfig->mMinAdjustedFps = 0;
1288                 config->mISConfig->mFixedAdjustedFps = 0;
1289                 if (msg->findInt64(KEY_MAX_PTS_GAP_TO_ENCODER, &value)) {
1290                     if (value < 0 && value >= INT32_MIN) {
1291                         config->mISConfig->mFixedAdjustedFps = -1e6 / value;
1292                         config->mISConfig->mMaxFps = -1;
1293                     } else if (value > 0 && value <= INT32_MAX) {
1294                         config->mISConfig->mMinAdjustedFps = 1e6 / value;
1295                     }
1296                 }
1297             }
1298 
1299             {
1300                 bool captureFpsFound = false;
1301                 double timeLapseFps;
1302                 float captureRate;
1303                 if (msg->findDouble("time-lapse-fps", &timeLapseFps)) {
1304                     config->mISConfig->mCaptureFps = timeLapseFps;
1305                     captureFpsFound = true;
1306                 } else if (msg->findAsFloat(KEY_CAPTURE_RATE, &captureRate)) {
1307                     config->mISConfig->mCaptureFps = captureRate;
1308                     captureFpsFound = true;
1309                 }
1310                 if (captureFpsFound) {
1311                     (void)msg->findAsFloat(KEY_FRAME_RATE, &config->mISConfig->mCodedFps);
1312                 }
1313             }
1314 
1315             {
1316                 config->mISConfig->mSuspended = false;
1317                 config->mISConfig->mSuspendAtUs = -1;
1318                 int32_t value;
1319                 if (msg->findInt32(KEY_CREATE_INPUT_SURFACE_SUSPENDED, &value) && value) {
1320                     config->mISConfig->mSuspended = true;
1321                 }
1322             }
1323             config->mISConfig->mUsage = 0;
1324             config->mISConfig->mPriority = INT_MAX;
1325         }
1326 
1327         /*
1328          * Handle desired color format.
1329          */
1330         int32_t defaultColorFormat = COLOR_FormatYUV420Flexible;
1331         if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))) {
1332             int32_t format = 0;
1333             // Query vendor format for Flexible YUV
1334             std::vector<std::unique_ptr<C2Param>> heapParams;
1335             C2StoreFlexiblePixelFormatDescriptorsInfo *pixelFormatInfo = nullptr;
1336             int vendorSdkVersion = base::GetIntProperty(
1337                     "ro.vendor.build.version.sdk", android_get_device_api_level());
1338             if (mClient->query(
1339                         {},
1340                         {C2StoreFlexiblePixelFormatDescriptorsInfo::PARAM_TYPE},
1341                         C2_MAY_BLOCK,
1342                         &heapParams) == C2_OK
1343                     && heapParams.size() == 1u) {
1344                 pixelFormatInfo = C2StoreFlexiblePixelFormatDescriptorsInfo::From(
1345                         heapParams[0].get());
1346             } else {
1347                 pixelFormatInfo = nullptr;
1348             }
1349             // bit depth -> format
1350             std::map<uint32_t, uint32_t> flexPixelFormat;
1351             std::map<uint32_t, uint32_t> flexPlanarPixelFormat;
1352             std::map<uint32_t, uint32_t> flexSemiPlanarPixelFormat;
1353             if (pixelFormatInfo && *pixelFormatInfo) {
1354                 for (size_t i = 0; i < pixelFormatInfo->flexCount(); ++i) {
1355                     const C2FlexiblePixelFormatDescriptorStruct &desc =
1356                         pixelFormatInfo->m.values[i];
1357                     if (desc.subsampling != C2Color::YUV_420
1358                             // TODO(b/180076105): some device report wrong layout
1359                             // || desc.layout == C2Color::INTERLEAVED_PACKED
1360                             // || desc.layout == C2Color::INTERLEAVED_ALIGNED
1361                             || desc.layout == C2Color::UNKNOWN_LAYOUT) {
1362                         continue;
1363                     }
1364                     if (flexPixelFormat.count(desc.bitDepth) == 0) {
1365                         flexPixelFormat.emplace(desc.bitDepth, desc.pixelFormat);
1366                     }
1367                     if (desc.layout == C2Color::PLANAR_PACKED
1368                             && flexPlanarPixelFormat.count(desc.bitDepth) == 0) {
1369                         flexPlanarPixelFormat.emplace(desc.bitDepth, desc.pixelFormat);
1370                     }
1371                     if (desc.layout == C2Color::SEMIPLANAR_PACKED
1372                             && flexSemiPlanarPixelFormat.count(desc.bitDepth) == 0) {
1373                         flexSemiPlanarPixelFormat.emplace(desc.bitDepth, desc.pixelFormat);
1374                     }
1375                 }
1376             }
1377             if (!msg->findInt32(KEY_COLOR_FORMAT, &format)) {
1378                 // Also handle default color format (encoders require color format, so this is only
1379                 // needed for decoders.
1380                 if (!(config->mDomain & Config::IS_ENCODER)) {
1381                     if (surface == nullptr) {
1382                         const char *prefix = "";
1383                         if (flexSemiPlanarPixelFormat.count(8) != 0) {
1384                             format = COLOR_FormatYUV420SemiPlanar;
1385                             prefix = "semi-";
1386                         } else {
1387                             format = COLOR_FormatYUV420Planar;
1388                         }
1389                         ALOGD("Client requested ByteBuffer mode decoder w/o color format set: "
1390                                 "using default %splanar color format", prefix);
1391                     } else {
1392                         format = COLOR_FormatSurface;
1393                     }
1394                     defaultColorFormat = format;
1395                 }
1396             } else {
1397                 if ((config->mDomain & Config::IS_ENCODER) || !surface) {
1398                     if (vendorSdkVersion < __ANDROID_API_S__ &&
1399                             (format == COLOR_FormatYUV420Planar ||
1400                              format == COLOR_FormatYUV420PackedPlanar ||
1401                              format == COLOR_FormatYUV420SemiPlanar ||
1402                              format == COLOR_FormatYUV420PackedSemiPlanar)) {
1403                         // pre-S framework used to map these color formats into YV12.
1404                         // Codecs from older vendor partition may be relying on
1405                         // this assumption.
1406                         format = HAL_PIXEL_FORMAT_YV12;
1407                     }
1408                     switch (format) {
1409                         case COLOR_FormatYUV420Flexible:
1410                             format = COLOR_FormatYUV420Planar;
1411                             if (flexPixelFormat.count(8) != 0) {
1412                                 format = flexPixelFormat[8];
1413                             }
1414                             break;
1415                         case COLOR_FormatYUV420Planar:
1416                         case COLOR_FormatYUV420PackedPlanar:
1417                             if (flexPlanarPixelFormat.count(8) != 0) {
1418                                 format = flexPlanarPixelFormat[8];
1419                             } else if (flexPixelFormat.count(8) != 0) {
1420                                 format = flexPixelFormat[8];
1421                             }
1422                             break;
1423                         case COLOR_FormatYUV420SemiPlanar:
1424                         case COLOR_FormatYUV420PackedSemiPlanar:
1425                             if (flexSemiPlanarPixelFormat.count(8) != 0) {
1426                                 format = flexSemiPlanarPixelFormat[8];
1427                             } else if (flexPixelFormat.count(8) != 0) {
1428                                 format = flexPixelFormat[8];
1429                             }
1430                             break;
1431                         case COLOR_FormatYUVP010:
1432                             format = COLOR_FormatYUVP010;
1433                             if (flexSemiPlanarPixelFormat.count(10) != 0) {
1434                                 format = flexSemiPlanarPixelFormat[10];
1435                             } else if (flexPixelFormat.count(10) != 0) {
1436                                 format = flexPixelFormat[10];
1437                             }
1438                             break;
1439                         default:
1440                             // No-op
1441                             break;
1442                     }
1443                 }
1444             }
1445 
1446             if (format != 0) {
1447                 msg->setInt32("android._color-format", format);
1448             }
1449         }
1450 
1451         /*
1452          * Handle dataspace
1453          */
1454         int32_t usingRecorder;
1455         if (msg->findInt32("android._using-recorder", &usingRecorder) && usingRecorder) {
1456             android_dataspace dataSpace = HAL_DATASPACE_BT709;
1457             int32_t width, height;
1458             if (msg->findInt32("width", &width)
1459                     && msg->findInt32("height", &height)) {
1460                 ColorAspects aspects;
1461                 getColorAspectsFromFormat(msg, aspects);
1462                 setDefaultCodecColorAspectsIfNeeded(aspects, width, height);
1463                 // TODO: read dataspace / color aspect from the component
1464                 setColorAspectsIntoFormat(aspects, const_cast<sp<AMessage> &>(msg));
1465                 dataSpace = getDataSpaceForColorAspects(aspects, true /* mayexpand */);
1466             }
1467             msg->setInt32("android._dataspace", (int32_t)dataSpace);
1468             ALOGD("setting dataspace to %x", dataSpace);
1469         }
1470 
1471         int32_t subscribeToAllVendorParams;
1472         if (msg->findInt32("x-*", &subscribeToAllVendorParams) && subscribeToAllVendorParams) {
1473             if (config->subscribeToAllVendorParams(comp, C2_MAY_BLOCK) != OK) {
1474                 ALOGD("[%s] Failed to subscribe to all vendor params", comp->getName().c_str());
1475             }
1476         }
1477 
1478         /*
1479          * configure mock region of interest if Feature_Roi is enabled
1480          */
1481         if (android::media::codec::provider_->region_of_interest()
1482             && android::media::codec::provider_->region_of_interest_support()) {
1483             if ((config->mDomain & Config::IS_ENCODER) && (config->mDomain & Config::IS_VIDEO)) {
1484                 int32_t enableRoi;
1485                 if (msg->findInt32("feature-region-of-interest", &enableRoi) && enableRoi != 0) {
1486                     if (!msg->contains(PARAMETER_KEY_QP_OFFSET_MAP) &&
1487                         !msg->contains(PARAMETER_KEY_QP_OFFSET_RECTS)) {
1488                         msg->setString(PARAMETER_KEY_QP_OFFSET_RECTS,
1489                                        AStringPrintf("%d,%d-%d,%d=%d;", 0, 0, height, width, 0));
1490                     }
1491                 }
1492             }
1493         }
1494 
1495         std::vector<std::unique_ptr<C2Param>> configUpdate;
1496         // NOTE: We used to ignore "video-bitrate" at configure; replicate
1497         //       the behavior here.
1498         sp<AMessage> sdkParams = msg;
1499         int32_t videoBitrate;
1500         if (sdkParams->findInt32(PARAMETER_KEY_VIDEO_BITRATE, &videoBitrate)) {
1501             sdkParams = msg->dup();
1502             sdkParams->removeEntryAt(sdkParams->findEntryByName(PARAMETER_KEY_VIDEO_BITRATE));
1503         }
1504         err = config->getConfigUpdateFromSdkParams(
1505                 comp, sdkParams, Config::IS_CONFIG, C2_DONT_BLOCK, &configUpdate);
1506         if (err != OK) {
1507             ALOGW("failed to convert configuration to c2 params");
1508         }
1509 
1510         int32_t maxBframes = 0;
1511         if ((config->mDomain & Config::IS_ENCODER)
1512                 && (config->mDomain & Config::IS_VIDEO)
1513                 && sdkParams->findInt32(KEY_MAX_B_FRAMES, &maxBframes)
1514                 && maxBframes > 0) {
1515             std::unique_ptr<C2StreamGopTuning::output> gop =
1516                 C2StreamGopTuning::output::AllocUnique(2 /* flexCount */, 0u /* stream */);
1517             gop->m.values[0] = { P_FRAME, UINT32_MAX };
1518             gop->m.values[1] = {
1519                 C2Config::picture_type_t(P_FRAME | B_FRAME),
1520                 uint32_t(maxBframes)
1521             };
1522             configUpdate.push_back(std::move(gop));
1523         }
1524 
1525         if ((config->mDomain & Config::IS_ENCODER)
1526                 && (config->mDomain & Config::IS_VIDEO)) {
1527             // we may not use all 3 of these entries
1528             std::unique_ptr<C2StreamPictureQuantizationTuning::output> qp =
1529                 C2StreamPictureQuantizationTuning::output::AllocUnique(3 /* flexCount */,
1530                                                                        0u /* stream */);
1531 
1532             int ix = 0;
1533 
1534             int32_t iMax = INT32_MAX;
1535             int32_t iMin = INT32_MIN;
1536             (void) sdkParams->findInt32(KEY_VIDEO_QP_I_MAX, &iMax);
1537             (void) sdkParams->findInt32(KEY_VIDEO_QP_I_MIN, &iMin);
1538             if (iMax != INT32_MAX || iMin != INT32_MIN) {
1539                 qp->m.values[ix++] = {I_FRAME, iMin, iMax};
1540             }
1541 
1542             int32_t pMax = INT32_MAX;
1543             int32_t pMin = INT32_MIN;
1544             (void) sdkParams->findInt32(KEY_VIDEO_QP_P_MAX, &pMax);
1545             (void) sdkParams->findInt32(KEY_VIDEO_QP_P_MIN, &pMin);
1546             if (pMax != INT32_MAX || pMin != INT32_MIN) {
1547                 qp->m.values[ix++] = {P_FRAME, pMin, pMax};
1548             }
1549 
1550             int32_t bMax = INT32_MAX;
1551             int32_t bMin = INT32_MIN;
1552             (void) sdkParams->findInt32(KEY_VIDEO_QP_B_MAX, &bMax);
1553             (void) sdkParams->findInt32(KEY_VIDEO_QP_B_MIN, &bMin);
1554             if (bMax != INT32_MAX || bMin != INT32_MIN) {
1555                 qp->m.values[ix++] = {B_FRAME, bMin, bMax};
1556             }
1557 
1558             // adjust to reflect actual use.
1559             qp->setFlexCount(ix);
1560 
1561             configUpdate.push_back(std::move(qp));
1562         }
1563 
1564         int32_t background = 0;
1565         if ((config->mDomain & Config::IS_VIDEO)
1566                 && msg->findInt32("android._background-mode", &background)
1567                 && background) {
1568             androidSetThreadPriority(gettid(), ANDROID_PRIORITY_BACKGROUND);
1569             if (config->mISConfig) {
1570                 config->mISConfig->mPriority = ANDROID_PRIORITY_BACKGROUND;
1571             }
1572         }
1573 
1574         err = config->setParameters(comp, configUpdate, C2_DONT_BLOCK);
1575         if (err != OK) {
1576             ALOGW("failed to configure c2 params");
1577             return err;
1578         }
1579 
1580         std::vector<std::unique_ptr<C2Param>> params;
1581         C2StreamUsageTuning::input usage(0u, 0u);
1582         C2StreamMaxBufferSizeInfo::input maxInputSize(0u, 0u);
1583         C2PrependHeaderModeSetting prepend(PREPEND_HEADER_TO_NONE);
1584 
1585         C2Param::Index colorAspectsRequestIndex =
1586             C2StreamColorAspectsInfo::output::PARAM_TYPE | C2Param::CoreIndex::IS_REQUEST_FLAG;
1587         std::initializer_list<C2Param::Index> indices {
1588             colorAspectsRequestIndex.withStream(0u),
1589         };
1590         int32_t colorTransferRequest = 0;
1591         if (config->mDomain & (Config::IS_IMAGE | Config::IS_VIDEO)
1592                 && !sdkParams->findInt32("color-transfer-request", &colorTransferRequest)) {
1593             colorTransferRequest = 0;
1594         }
1595         c2_status_t c2err = C2_OK;
1596         if (colorTransferRequest != 0) {
1597             c2err = comp->query(
1598                     { &usage, &maxInputSize, &prepend },
1599                     indices,
1600                     C2_DONT_BLOCK,
1601                     &params);
1602         } else {
1603             c2err = comp->query(
1604                     { &usage, &maxInputSize, &prepend },
1605                     {},
1606                     C2_DONT_BLOCK,
1607                     &params);
1608         }
1609         if (c2err != C2_OK && c2err != C2_BAD_INDEX) {
1610             ALOGE("Failed to query component interface: %d", c2err);
1611             return UNKNOWN_ERROR;
1612         }
1613         if (usage) {
1614             if (usage.value & C2MemoryUsage::CPU_READ) {
1615                 config->mInputFormat->setInt32("using-sw-read-often", true);
1616             }
1617             if (config->mISConfig) {
1618                 C2AndroidMemoryUsage androidUsage(C2MemoryUsage(usage.value));
1619                 config->mISConfig->mUsage = androidUsage.asGrallocUsage();
1620             }
1621             config->mInputFormat->setInt64("android._C2MemoryUsage", usage.value);
1622         }
1623 
1624         // NOTE: we don't blindly use client specified input size if specified as clients
1625         // at times specify too small size. Instead, mimic the behavior from OMX, where the
1626         // client specified size is only used to ask for bigger buffers than component suggested
1627         // size.
1628         int32_t clientInputSize = 0;
1629         bool clientSpecifiedInputSize =
1630             msg->findInt32(KEY_MAX_INPUT_SIZE, &clientInputSize) && clientInputSize > 0;
1631         // TEMP: enforce minimum buffer size of 1MB for video decoders
1632         // and 16K / 4K for audio encoders/decoders
1633         if (maxInputSize.value == 0) {
1634             if (config->mDomain & Config::IS_AUDIO) {
1635                 maxInputSize.value = encoder ? 16384 : 4096;
1636             } else if (!encoder) {
1637                 maxInputSize.value = 1048576u;
1638             }
1639         }
1640 
1641         // verify that CSD fits into this size (if defined)
1642         if ((config->mDomain & Config::IS_DECODER) && maxInputSize.value > 0) {
1643             sp<ABuffer> csd;
1644             for (size_t ix = 0; msg->findBuffer(StringPrintf("csd-%zu", ix).c_str(), &csd); ++ix) {
1645                 if (csd && csd->size() > maxInputSize.value) {
1646                     maxInputSize.value = csd->size();
1647                 }
1648             }
1649         }
1650 
1651         // TODO: do this based on component requiring linear allocator for input
1652         if ((config->mDomain & Config::IS_DECODER) || (config->mDomain & Config::IS_AUDIO)) {
1653             if (clientSpecifiedInputSize) {
1654                 // Warn that we're overriding client's max input size if necessary.
1655                 if ((uint32_t)clientInputSize < maxInputSize.value) {
1656                     ALOGD("client requested max input size %d, which is smaller than "
1657                           "what component recommended (%u); overriding with component "
1658                           "recommendation.", clientInputSize, maxInputSize.value);
1659                     ALOGW("This behavior is subject to change. It is recommended that "
1660                           "app developers double check whether the requested "
1661                           "max input size is in reasonable range.");
1662                 } else {
1663                     maxInputSize.value = clientInputSize;
1664                 }
1665             }
1666             // Pass max input size on input format to the buffer channel (if supplied by the
1667             // component or by a default)
1668             if (maxInputSize.value) {
1669                 config->mInputFormat->setInt32(
1670                         KEY_MAX_INPUT_SIZE,
1671                         (int32_t)(c2_min(maxInputSize.value, uint32_t(INT32_MAX))));
1672             }
1673         }
1674 
1675         int32_t clientPrepend;
1676         if ((config->mDomain & Config::IS_VIDEO)
1677                 && (config->mDomain & Config::IS_ENCODER)
1678                 && msg->findInt32(KEY_PREPEND_HEADER_TO_SYNC_FRAMES, &clientPrepend)
1679                 && clientPrepend
1680                 && (!prepend || prepend.value != PREPEND_HEADER_TO_ALL_SYNC)) {
1681             ALOGE("Failed to set KEY_PREPEND_HEADER_TO_SYNC_FRAMES");
1682             return BAD_VALUE;
1683         }
1684 
1685         int32_t componentColorFormat = 0;
1686         if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))) {
1687             // propagate HDR static info to output format for both encoders and decoders
1688             // if component supports this info, we will update from component, but only the raw port,
1689             // so don't propagate if component already filled it in.
1690             sp<ABuffer> hdrInfo;
1691             if (msg->findBuffer(KEY_HDR_STATIC_INFO, &hdrInfo)
1692                     && !config->mOutputFormat->findBuffer(KEY_HDR_STATIC_INFO, &hdrInfo)) {
1693                 config->mOutputFormat->setBuffer(KEY_HDR_STATIC_INFO, hdrInfo);
1694             }
1695 
1696             // Set desired color format from configuration parameter
1697             int32_t format;
1698             if (!msg->findInt32(KEY_COLOR_FORMAT, &format)) {
1699                 format = defaultColorFormat;
1700             }
1701             if (config->mDomain & Config::IS_ENCODER) {
1702                 config->mInputFormat->setInt32(KEY_COLOR_FORMAT, format);
1703                 if (msg->findInt32("android._color-format", &componentColorFormat)) {
1704                     config->mInputFormat->setInt32("android._color-format", componentColorFormat);
1705                 }
1706             } else {
1707                 config->mOutputFormat->setInt32(KEY_COLOR_FORMAT, format);
1708             }
1709         }
1710 
1711         // propagate encoder delay and padding to output format
1712         if ((config->mDomain & Config::IS_DECODER) && (config->mDomain & Config::IS_AUDIO)) {
1713             int delay = 0;
1714             if (msg->findInt32("encoder-delay", &delay)) {
1715                 config->mOutputFormat->setInt32("encoder-delay", delay);
1716             }
1717             int padding = 0;
1718             if (msg->findInt32("encoder-padding", &padding)) {
1719                 config->mOutputFormat->setInt32("encoder-padding", padding);
1720             }
1721         }
1722 
1723         if (config->mDomain & Config::IS_AUDIO) {
1724             // set channel-mask
1725             int32_t mask;
1726             if (msg->findInt32(KEY_CHANNEL_MASK, &mask)) {
1727                 if (config->mDomain & Config::IS_ENCODER) {
1728                     config->mInputFormat->setInt32(KEY_CHANNEL_MASK, mask);
1729                 } else {
1730                     config->mOutputFormat->setInt32(KEY_CHANNEL_MASK, mask);
1731                 }
1732             }
1733 
1734             // set PCM encoding
1735             int32_t pcmEncoding = kAudioEncodingPcm16bit;
1736             msg->findInt32(KEY_PCM_ENCODING, &pcmEncoding);
1737             if (encoder) {
1738                 config->mInputFormat->setInt32("android._config-pcm-encoding", pcmEncoding);
1739             } else {
1740                 config->mOutputFormat->setInt32("android._config-pcm-encoding", pcmEncoding);
1741             }
1742         }
1743 
1744         std::unique_ptr<C2Param> colorTransferRequestParam;
1745         for (std::unique_ptr<C2Param> &param : params) {
1746             if (param->index() == colorAspectsRequestIndex.withStream(0u)) {
1747                 ALOGI("found color transfer request param");
1748                 colorTransferRequestParam = std::move(param);
1749             }
1750         }
1751 
1752         if (colorTransferRequest != 0) {
1753             if (colorTransferRequestParam && *colorTransferRequestParam) {
1754                 C2StreamColorAspectsInfo::output *info =
1755                     static_cast<C2StreamColorAspectsInfo::output *>(
1756                             colorTransferRequestParam.get());
1757                 if (!C2Mapper::map(info->transfer, &colorTransferRequest)) {
1758                     colorTransferRequest = 0;
1759                 }
1760             } else {
1761                 colorTransferRequest = 0;
1762             }
1763             config->mInputFormat->setInt32("color-transfer-request", colorTransferRequest);
1764         }
1765 
1766         if (componentColorFormat != 0 && componentColorFormat != COLOR_FormatSurface) {
1767             // Need to get stride/vstride
1768             uint32_t pixelFormat = PIXEL_FORMAT_UNKNOWN;
1769             if (C2Mapper::mapPixelFormatFrameworkToCodec(componentColorFormat, &pixelFormat)) {
1770                 // TODO: retrieve these values without allocating a buffer.
1771                 //       Currently allocating a buffer is necessary to retrieve the layout.
1772                 int64_t blockUsage =
1773                     usage.value | C2MemoryUsage::CPU_READ | C2MemoryUsage::CPU_WRITE;
1774                 std::shared_ptr<C2GraphicBlock> block = FetchGraphicBlock(
1775                         align(width, 2), align(height, 2), componentColorFormat, blockUsage,
1776                         {comp->getName()});
1777                 sp<GraphicBlockBuffer> buffer;
1778                 if (block) {
1779                     buffer = GraphicBlockBuffer::Allocate(
1780                             config->mInputFormat,
1781                             block,
1782                             [](size_t size) -> sp<ABuffer> { return new ABuffer(size); });
1783                 } else {
1784                     ALOGD("Failed to allocate a graphic block "
1785                             "(width=%d height=%d pixelFormat=%u usage=%llx)",
1786                             width, height, pixelFormat, (long long)blockUsage);
1787                     // This means that byte buffer mode is not supported in this configuration
1788                     // anyway. Skip setting stride/vstride to input format.
1789                 }
1790                 if (buffer) {
1791                     sp<ABuffer> imageData = buffer->getImageData();
1792                     MediaImage2 *img = nullptr;
1793                     if (imageData && imageData->data()
1794                             && imageData->size() >= sizeof(MediaImage2)) {
1795                         img = (MediaImage2*)imageData->data();
1796                     }
1797                     if (img && img->mNumPlanes > 0 && img->mType != img->MEDIA_IMAGE_TYPE_UNKNOWN) {
1798                         int32_t stride = img->mPlane[0].mRowInc;
1799                         config->mInputFormat->setInt32(KEY_STRIDE, stride);
1800                         if (img->mNumPlanes > 1 && stride > 0) {
1801                             int64_t offsetDelta =
1802                                 (int64_t)img->mPlane[1].mOffset - (int64_t)img->mPlane[0].mOffset;
1803                             if (offsetDelta % stride == 0) {
1804                                 int32_t vstride = int32_t(offsetDelta / stride);
1805                                 config->mInputFormat->setInt32(KEY_SLICE_HEIGHT, vstride);
1806                             } else {
1807                                 ALOGD("Cannot report accurate slice height: "
1808                                         "offsetDelta = %lld stride = %d",
1809                                         (long long)offsetDelta, stride);
1810                             }
1811                         }
1812                     }
1813                 }
1814             }
1815         }
1816 
1817         if (config->mTunneled) {
1818             config->mOutputFormat->setInt32("android._tunneled", 1);
1819         }
1820 
1821         // Convert an encoding statistics level to corresponding encoding statistics
1822         // kinds
1823         int32_t encodingStatisticsLevel = VIDEO_ENCODING_STATISTICS_LEVEL_NONE;
1824         if ((config->mDomain & Config::IS_ENCODER)
1825             && (config->mDomain & Config::IS_VIDEO)
1826             && msg->findInt32(KEY_VIDEO_ENCODING_STATISTICS_LEVEL, &encodingStatisticsLevel)) {
1827             // Higher level include all the enc stats belong to lower level.
1828             switch (encodingStatisticsLevel) {
1829                 // case VIDEO_ENCODING_STATISTICS_LEVEL_2: // reserved for the future level 2
1830                                                            // with more enc stat kinds
1831                 // Future extended encoding statistics for the level 2 should be added here
1832                 case VIDEO_ENCODING_STATISTICS_LEVEL_1:
1833                     config->subscribeToConfigUpdate(
1834                             comp,
1835                             {
1836                                 C2AndroidStreamAverageBlockQuantizationInfo::output::PARAM_TYPE,
1837                                 C2StreamPictureTypeInfo::output::PARAM_TYPE,
1838                             });
1839                     break;
1840                 case VIDEO_ENCODING_STATISTICS_LEVEL_NONE:
1841                     break;
1842             }
1843         }
1844         ALOGD("encoding statistics level = %d", encodingStatisticsLevel);
1845 
1846         ALOGD("setup formats input: %s",
1847                 config->mInputFormat->debugString().c_str());
1848         ALOGD("setup formats output: %s",
1849                 config->mOutputFormat->debugString().c_str());
1850         return OK;
1851     };
1852     if (tryAndReportOnError(doConfig) != OK) {
1853         return;
1854     }
1855 
1856     Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1857     const std::unique_ptr<Config> &config = *configLocked;
1858 
1859     config->queryConfiguration(comp);
1860 
1861     mMetrics = new AMessage;
1862     mChannel->resetBuffersPixelFormat((config->mDomain & Config::IS_ENCODER) ? true : false);
1863 
1864     mCallback->onComponentConfigured(config->mInputFormat, config->mOutputFormat);
1865 }
1866 
initiateCreateInputSurface()1867 void CCodec::initiateCreateInputSurface() {
1868     status_t err = [this] {
1869         Mutexed<State>::Locked state(mState);
1870         if (state->get() != ALLOCATED) {
1871             return UNKNOWN_ERROR;
1872         }
1873         // TODO: read it from intf() properly.
1874         if (state->comp->getName().find("encoder") == std::string::npos) {
1875             return INVALID_OPERATION;
1876         }
1877         return OK;
1878     }();
1879     if (err != OK) {
1880         mCallback->onInputSurfaceCreationFailed(err);
1881         return;
1882     }
1883 
1884     (new AMessage(kWhatCreateInputSurface, this))->post();
1885 }
1886 
CreateOmxInputSurface()1887 sp<PersistentSurface> CCodec::CreateOmxInputSurface() {
1888     using namespace android::hardware::media::omx::V1_0;
1889     using namespace android::hardware::media::omx::V1_0::utils;
1890     using namespace android::hardware::graphics::bufferqueue::V1_0::utils;
1891     typedef android::hardware::media::omx::V1_0::Status OmxStatus;
1892     android::sp<IOmx> omx = IOmx::getService();
1893     if (omx == nullptr) {
1894         return nullptr;
1895     }
1896     typedef android::hardware::graphics::bufferqueue::V1_0::
1897             IGraphicBufferProducer HGraphicBufferProducer;
1898     typedef android::hardware::media::omx::V1_0::
1899             IGraphicBufferSource HGraphicBufferSource;
1900     OmxStatus s;
1901     android::sp<HGraphicBufferProducer> gbp;
1902     android::sp<HGraphicBufferSource> gbs;
1903 
1904     using ::android::hardware::Return;
1905     Return<void> transStatus = omx->createInputSurface(
1906             [&s, &gbp, &gbs](
1907                     OmxStatus status,
1908                     const android::sp<HGraphicBufferProducer>& producer,
1909                     const android::sp<HGraphicBufferSource>& source) {
1910                 s = status;
1911                 gbp = producer;
1912                 gbs = source;
1913             });
1914     if (transStatus.isOk() && s == OmxStatus::OK) {
1915         return new PersistentSurface(new H2BGraphicBufferProducer(gbp), gbs);
1916     }
1917 
1918     return nullptr;
1919 }
1920 
CreateCompatibleInputSurface()1921 sp<PersistentSurface> CCodec::CreateCompatibleInputSurface() {
1922     sp<PersistentSurface> surface(CreateInputSurface());
1923 
1924     if (surface == nullptr) {
1925         surface = CreateOmxInputSurface();
1926     }
1927 
1928     return surface;
1929 }
1930 
createInputSurface()1931 void CCodec::createInputSurface() {
1932     status_t err;
1933     sp<IGraphicBufferProducer> bufferProducer;
1934 
1935     sp<AMessage> outputFormat;
1936     uint64_t usage = 0;
1937     {
1938         Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1939         const std::unique_ptr<Config> &config = *configLocked;
1940         outputFormat = config->mOutputFormat;
1941         usage = config->mISConfig ? config->mISConfig->mUsage : 0;
1942     }
1943 
1944     sp<PersistentSurface> persistentSurface = CreateCompatibleInputSurface();
1945     if (persistentSurface->isTargetAidl()) {
1946         ::ndk::SpAIBinder aidlTarget = persistentSurface->getAidlTarget();
1947         std::shared_ptr<AGraphicBufferSource> gbs = AGraphicBufferSource::fromBinder(aidlTarget);
1948         if (gbs) {
1949             int32_t width = 0;
1950             (void)outputFormat->findInt32("width", &width);
1951             int32_t height = 0;
1952             (void)outputFormat->findInt32("height", &height);
1953             err = setupInputSurface(std::make_shared<AGraphicBufferSourceWrapper>(
1954                     gbs, width, height, usage));
1955             bufferProducer = persistentSurface->getBufferProducer();
1956         } else {
1957             ALOGE("Corrupted input surface(aidl)");
1958             mCallback->onInputSurfaceCreationFailed(UNKNOWN_ERROR);
1959             return;
1960         }
1961     } else {
1962         sp<hidl::base::V1_0::IBase> hidlTarget = persistentSurface->getHidlTarget();
1963         sp<IInputSurface> hidlInputSurface = IInputSurface::castFrom(hidlTarget);
1964         sp<HGraphicBufferSource> gbs = HGraphicBufferSource::castFrom(hidlTarget);
1965 
1966         if (hidlInputSurface) {
1967             std::shared_ptr<Codec2Client::InputSurface> inputSurface =
1968                     std::make_shared<Codec2Client::InputSurface>(hidlInputSurface);
1969             err = setupInputSurface(std::make_shared<C2InputSurfaceWrapper>(
1970                     inputSurface));
1971             bufferProducer = inputSurface->getGraphicBufferProducer();
1972         } else if (gbs) {
1973             int32_t width = 0;
1974             (void)outputFormat->findInt32("width", &width);
1975             int32_t height = 0;
1976             (void)outputFormat->findInt32("height", &height);
1977             err = setupInputSurface(std::make_shared<HGraphicBufferSourceWrapper>(
1978                     gbs, width, height, usage));
1979             bufferProducer = persistentSurface->getBufferProducer();
1980         } else {
1981             ALOGE("Corrupted input surface");
1982             mCallback->onInputSurfaceCreationFailed(UNKNOWN_ERROR);
1983             return;
1984         }
1985     }
1986 
1987     if (err != OK) {
1988         ALOGE("Failed to set up input surface: %d", err);
1989         mCallback->onInputSurfaceCreationFailed(err);
1990         return;
1991     }
1992 
1993     // Formats can change after setupInputSurface
1994     sp<AMessage> inputFormat;
1995     {
1996         Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1997         const std::unique_ptr<Config> &config = *configLocked;
1998         inputFormat = config->mInputFormat;
1999         outputFormat = config->mOutputFormat;
2000     }
2001     mCallback->onInputSurfaceCreated(
2002             inputFormat,
2003             outputFormat,
2004             new BufferProducerWrapper(bufferProducer));
2005 }
2006 
setupInputSurface(const std::shared_ptr<InputSurfaceWrapper> & surface)2007 status_t CCodec::setupInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface) {
2008     Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2009     const std::unique_ptr<Config> &config = *configLocked;
2010     config->mUsingSurface = true;
2011 
2012     // we are now using surface - apply default color aspects to input format - as well as
2013     // get dataspace
2014     bool inputFormatChanged = config->updateFormats(Config::IS_INPUT);
2015 
2016     // configure dataspace
2017     static_assert(sizeof(int32_t) == sizeof(android_dataspace), "dataspace size mismatch");
2018 
2019     // The output format contains app-configured color aspects, and the input format
2020     // has the default color aspects. Use the default for the unspecified params.
2021     ColorAspects inputColorAspects, colorAspects;
2022     getColorAspectsFromFormat(config->mOutputFormat, colorAspects);
2023     getColorAspectsFromFormat(config->mInputFormat, inputColorAspects);
2024     if (colorAspects.mRange == ColorAspects::RangeUnspecified) {
2025         colorAspects.mRange = inputColorAspects.mRange;
2026     }
2027     if (colorAspects.mPrimaries == ColorAspects::PrimariesUnspecified) {
2028         colorAspects.mPrimaries = inputColorAspects.mPrimaries;
2029     }
2030     if (colorAspects.mTransfer == ColorAspects::TransferUnspecified) {
2031         colorAspects.mTransfer = inputColorAspects.mTransfer;
2032     }
2033     if (colorAspects.mMatrixCoeffs == ColorAspects::MatrixUnspecified) {
2034         colorAspects.mMatrixCoeffs = inputColorAspects.mMatrixCoeffs;
2035     }
2036     android_dataspace dataSpace = getDataSpaceForColorAspects(
2037             colorAspects, /* mayExtend = */ false);
2038     surface->setDataSpace(dataSpace);
2039     setColorAspectsIntoFormat(colorAspects, config->mInputFormat, /* force = */ true);
2040     config->mInputFormat->setInt32("android._dataspace", int32_t(dataSpace));
2041 
2042     ALOGD("input format %s to %s",
2043             inputFormatChanged ? "changed" : "unchanged",
2044             config->mInputFormat->debugString().c_str());
2045 
2046     status_t err = mChannel->setInputSurface(surface);
2047     if (err != OK) {
2048         // undo input format update
2049         config->mUsingSurface = false;
2050         (void)config->updateFormats(Config::IS_INPUT);
2051         return err;
2052     }
2053     config->mInputSurface = surface;
2054 
2055     if (config->mISConfig) {
2056         surface->configure(*config->mISConfig);
2057     } else {
2058         ALOGD("ISConfig: no configuration");
2059     }
2060 
2061     return OK;
2062 }
2063 
initiateSetInputSurface(const sp<PersistentSurface> & surface)2064 void CCodec::initiateSetInputSurface(const sp<PersistentSurface> &surface) {
2065     sp<AMessage> msg = new AMessage(kWhatSetInputSurface, this);
2066     msg->setObject("surface", surface);
2067     msg->post();
2068 }
2069 
setInputSurface(const sp<PersistentSurface> & surface)2070 void CCodec::setInputSurface(const sp<PersistentSurface> &surface) {
2071     sp<AMessage> outputFormat;
2072     uint64_t usage = 0;
2073     {
2074         Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2075         const std::unique_ptr<Config> &config = *configLocked;
2076         outputFormat = config->mOutputFormat;
2077         usage = config->mISConfig ? config->mISConfig->mUsage : 0;
2078     }
2079     if (surface->isTargetAidl()) {
2080         ::ndk::SpAIBinder aidlTarget = surface->getAidlTarget();
2081         std::shared_ptr<AGraphicBufferSource> gbs = AGraphicBufferSource::fromBinder(aidlTarget);
2082         if (gbs) {
2083             int32_t width = 0;
2084             (void)outputFormat->findInt32("width", &width);
2085             int32_t height = 0;
2086             (void)outputFormat->findInt32("height", &height);
2087 
2088             status_t err = setupInputSurface(std::make_shared<AGraphicBufferSourceWrapper>(
2089                     gbs, width, height, usage));
2090             if (err != OK) {
2091                 ALOGE("Failed to set up input surface(aidl): %d", err);
2092                 mCallback->onInputSurfaceDeclined(err);
2093                 return;
2094             }
2095         } else {
2096             ALOGE("Failed to set input surface(aidl): Corrupted surface.");
2097             mCallback->onInputSurfaceDeclined(UNKNOWN_ERROR);
2098             return;
2099         }
2100     } else {
2101         sp<hidl::base::V1_0::IBase> hidlTarget = surface->getHidlTarget();
2102         sp<IInputSurface> inputSurface = IInputSurface::castFrom(hidlTarget);
2103         sp<HGraphicBufferSource> gbs = HGraphicBufferSource::castFrom(hidlTarget);
2104         if (inputSurface) {
2105             status_t err = setupInputSurface(std::make_shared<C2InputSurfaceWrapper>(
2106                     std::make_shared<Codec2Client::InputSurface>(inputSurface)));
2107             if (err != OK) {
2108                 ALOGE("Failed to set up input surface: %d", err);
2109                 mCallback->onInputSurfaceDeclined(err);
2110                 return;
2111             }
2112         } else if (gbs) {
2113             int32_t width = 0;
2114             (void)outputFormat->findInt32("width", &width);
2115             int32_t height = 0;
2116             (void)outputFormat->findInt32("height", &height);
2117             status_t err = setupInputSurface(std::make_shared<HGraphicBufferSourceWrapper>(
2118                     gbs, width, height, usage));
2119             if (err != OK) {
2120                 ALOGE("Failed to set up input surface: %d", err);
2121                 mCallback->onInputSurfaceDeclined(err);
2122                 return;
2123             }
2124         } else {
2125             ALOGE("Failed to set input surface: Corrupted surface.");
2126             mCallback->onInputSurfaceDeclined(UNKNOWN_ERROR);
2127             return;
2128         }
2129     }
2130     // Formats can change after setupInputSurface
2131     sp<AMessage> inputFormat;
2132     {
2133         Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2134         const std::unique_ptr<Config> &config = *configLocked;
2135         inputFormat = config->mInputFormat;
2136         outputFormat = config->mOutputFormat;
2137     }
2138     mCallback->onInputSurfaceAccepted(inputFormat, outputFormat);
2139 }
2140 
initiateStart()2141 void CCodec::initiateStart() {
2142     auto setStarting = [this] {
2143         Mutexed<State>::Locked state(mState);
2144         if (state->get() != ALLOCATED) {
2145             return UNKNOWN_ERROR;
2146         }
2147         state->set(STARTING);
2148         return OK;
2149     };
2150     if (tryAndReportOnError(setStarting) != OK) {
2151         return;
2152     }
2153 
2154     (new AMessage(kWhatStart, this))->post();
2155 }
2156 
start()2157 void CCodec::start() {
2158     std::shared_ptr<Codec2Client::Component> comp;
2159     auto checkStarting = [this, &comp] {
2160         Mutexed<State>::Locked state(mState);
2161         if (state->get() != STARTING) {
2162             return UNKNOWN_ERROR;
2163         }
2164         comp = state->comp;
2165         return OK;
2166     };
2167     if (tryAndReportOnError(checkStarting) != OK) {
2168         return;
2169     }
2170 
2171     c2_status_t err = comp->start();
2172     if (err != C2_OK) {
2173         mCallback->onError(toStatusT(err, C2_OPERATION_Component_start),
2174                            ACTION_CODE_FATAL);
2175         return;
2176     }
2177 
2178     // clear the deadline after the component starts
2179     setDeadline(TimePoint::max(), 0ms, "none");
2180 
2181     sp<AMessage> inputFormat;
2182     sp<AMessage> outputFormat;
2183     status_t err2 = OK;
2184     bool buffersBoundToCodec = false;
2185     {
2186         Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2187         const std::unique_ptr<Config> &config = *configLocked;
2188         inputFormat = config->mInputFormat;
2189         // start triggers format dup
2190         outputFormat = config->mOutputFormat = config->mOutputFormat->dup();
2191         if (config->mInputSurface) {
2192             err2 = config->mInputSurface->start();
2193             config->mInputSurfaceDataspace = config->mInputSurface->getDataspace();
2194         }
2195         buffersBoundToCodec = config->mBuffersBoundToCodec;
2196     }
2197     if (err2 != OK) {
2198         mCallback->onError(err2, ACTION_CODE_FATAL);
2199         return;
2200     }
2201 
2202     err2 = mChannel->start(inputFormat, outputFormat, buffersBoundToCodec);
2203     if (err2 != OK) {
2204         mCallback->onError(err2, ACTION_CODE_FATAL);
2205         return;
2206     }
2207 
2208     auto setRunning = [this] {
2209         Mutexed<State>::Locked state(mState);
2210         if (state->get() != STARTING) {
2211             return UNKNOWN_ERROR;
2212         }
2213         state->set(RUNNING);
2214         return OK;
2215     };
2216     if (tryAndReportOnError(setRunning) != OK) {
2217         return;
2218     }
2219 
2220     // preparation of input buffers may not succeed due to the lack of
2221     // memory; returning correct error code (NO_MEMORY) as an error allows
2222     // MediaCodec to try reclaim and restart codec gracefully.
2223     std::map<size_t, sp<MediaCodecBuffer>> clientInputBuffers;
2224     err2 = mChannel->prepareInitialInputBuffers(&clientInputBuffers);
2225     if (err2 != OK) {
2226         ALOGE("Initial preparation for Input Buffers failed");
2227         mCallback->onError(err2, ACTION_CODE_FATAL);
2228         return;
2229     }
2230 
2231     mCallback->onStartCompleted();
2232 
2233     mChannel->requestInitialInputBuffers(std::move(clientInputBuffers));
2234 }
2235 
initiateShutdown(bool keepComponentAllocated)2236 void CCodec::initiateShutdown(bool keepComponentAllocated) {
2237     if (keepComponentAllocated) {
2238         initiateStop();
2239     } else {
2240         initiateRelease();
2241     }
2242 }
2243 
initiateStop()2244 void CCodec::initiateStop() {
2245     {
2246         Mutexed<State>::Locked state(mState);
2247         if (state->get() == ALLOCATED
2248                 || state->get()  == RELEASED
2249                 || state->get() == STOPPING
2250                 || state->get() == RELEASING) {
2251             // We're already stopped, released, or doing it right now.
2252             state.unlock();
2253             mCallback->onStopCompleted();
2254             state.lock();
2255             return;
2256         }
2257         state->set(STOPPING);
2258     }
2259     mChannel->reset();
2260     bool pushBlankBuffer = mConfig.lock().get()->mPushBlankBuffersOnStop;
2261     sp<AMessage> stopMessage(new AMessage(kWhatStop, this));
2262     stopMessage->setInt32("pushBlankBuffer", pushBlankBuffer);
2263     stopMessage->post();
2264 }
2265 
stop(bool pushBlankBuffer)2266 void CCodec::stop(bool pushBlankBuffer) {
2267     std::shared_ptr<Codec2Client::Component> comp;
2268     {
2269         Mutexed<State>::Locked state(mState);
2270         if (state->get() == RELEASING) {
2271             state.unlock();
2272             // We're already stopped or release is in progress.
2273             mCallback->onStopCompleted();
2274             state.lock();
2275             return;
2276         } else if (state->get() != STOPPING) {
2277             state.unlock();
2278             mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
2279             state.lock();
2280             return;
2281         }
2282         comp = state->comp;
2283     }
2284 
2285     // Note: Logically mChannel->stopUseOutputSurface() should be after comp->stop().
2286     // But in the case some HAL implementations hang forever on comp->stop().
2287     // (HAL is waiting for C2Fence until fetchGraphicBlock unblocks and not
2288     // completing stop()).
2289     // So we reverse their order for stopUseOutputSurface() to notify C2Fence waiters
2290     // prior to comp->stop().
2291     // See also b/300350761.
2292     //
2293     // The workaround is no longer needed with fetchGraphicBlock & C2Fence changes.
2294     // so we are reverting back to the logical sequence of the operations when
2295     // AIDL HALs are selected.
2296     // When the HIDL HALs are selected, we retained workaround(the reversed
2297     // order) as default in order to keep legacy behavior.
2298     bool stopHalBeforeSurface =
2299             Codec2Client::IsAidlSelected() ||
2300             property_get_bool("debug.codec2.stop_hal_before_surface", false);
2301     status_t err = C2_OK;
2302     if (stopHalBeforeSurface && android::media::codec::provider_->stop_hal_before_surface()) {
2303         err = comp->stop();
2304         mChannel->stopUseOutputSurface(pushBlankBuffer);
2305     } else {
2306         mChannel->stopUseOutputSurface(pushBlankBuffer);
2307         err = comp->stop();
2308     }
2309     if (err != C2_OK) {
2310         // TODO: convert err into status_t
2311         mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
2312     }
2313 
2314     {
2315         Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2316         const std::unique_ptr<Config> &config = *configLocked;
2317         if (config->mInputSurface) {
2318             config->mInputSurface->disconnect();
2319             config->mInputSurface = nullptr;
2320             config->mInputSurfaceDataspace = HAL_DATASPACE_UNKNOWN;
2321         }
2322     }
2323     {
2324         Mutexed<State>::Locked state(mState);
2325         if (state->get() == STOPPING) {
2326             state->set(ALLOCATED);
2327         }
2328     }
2329     mCallback->onStopCompleted();
2330 }
2331 
initiateRelease(bool sendCallback)2332 void CCodec::initiateRelease(bool sendCallback /* = true */) {
2333     bool clearInputSurfaceIfNeeded = false;
2334     {
2335         Mutexed<State>::Locked state(mState);
2336         if (state->get() == RELEASED || state->get() == RELEASING) {
2337             // We're already released or doing it right now.
2338             if (sendCallback) {
2339                 state.unlock();
2340                 mCallback->onReleaseCompleted();
2341                 state.lock();
2342             }
2343             return;
2344         }
2345         if (state->get() == ALLOCATING) {
2346             state->set(RELEASING);
2347             // With the altered state allocate() would fail and clean up.
2348             if (sendCallback) {
2349                 state.unlock();
2350                 mCallback->onReleaseCompleted();
2351                 state.lock();
2352             }
2353             return;
2354         }
2355         if (state->get() == STARTING
2356                 || state->get() == RUNNING
2357                 || state->get() == STOPPING) {
2358             // Input surface may have been started, so clean up is needed.
2359             clearInputSurfaceIfNeeded = true;
2360         }
2361         state->set(RELEASING);
2362     }
2363 
2364     if (clearInputSurfaceIfNeeded) {
2365         Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2366         const std::unique_ptr<Config> &config = *configLocked;
2367         if (config->mInputSurface) {
2368             config->mInputSurface->disconnect();
2369             config->mInputSurface = nullptr;
2370             config->mInputSurfaceDataspace = HAL_DATASPACE_UNKNOWN;
2371         }
2372     }
2373 
2374     mChannel->reset();
2375     bool pushBlankBuffer = mConfig.lock().get()->mPushBlankBuffersOnStop;
2376     // thiz holds strong ref to this while the thread is running.
2377     sp<CCodec> thiz(this);
2378     std::thread([thiz, sendCallback, pushBlankBuffer]
2379                 { thiz->release(sendCallback, pushBlankBuffer); }).detach();
2380 }
2381 
release(bool sendCallback,bool pushBlankBuffer)2382 void CCodec::release(bool sendCallback, bool pushBlankBuffer) {
2383     std::shared_ptr<Codec2Client::Component> comp;
2384     {
2385         Mutexed<State>::Locked state(mState);
2386         if (state->get() == RELEASED) {
2387             if (sendCallback) {
2388                 state.unlock();
2389                 mCallback->onReleaseCompleted();
2390                 state.lock();
2391             }
2392             return;
2393         }
2394         comp = state->comp;
2395     }
2396     // Note: Logically mChannel->stopUseOutputSurface() should be after comp->release().
2397     // But in the case some HAL implementations hang forever on comp->release().
2398     // (HAL is waiting for C2Fence until fetchGraphicBlock unblocks and not
2399     // completing release()).
2400     // So we reverse their order for stopUseOutputSurface() to notify C2Fence waiters
2401     // prior to comp->release().
2402     // See also b/300350761.
2403     //
2404     // The workaround is no longer needed with fetchGraphicBlock & C2Fence changes.
2405     // so we are reverting back to the logical sequence of the operations when
2406     // AIDL HALs are selected.
2407     // When the HIDL HALs are selected, we retained workaround(the reversed
2408     // order) as default in order to keep legacy behavior.
2409     bool stopHalBeforeSurface =
2410             Codec2Client::IsAidlSelected() ||
2411             property_get_bool("debug.codec2.stop_hal_before_surface", false);
2412     if (stopHalBeforeSurface && android::media::codec::provider_->stop_hal_before_surface()) {
2413         comp->release();
2414         mChannel->stopUseOutputSurface(pushBlankBuffer);
2415     } else {
2416         mChannel->stopUseOutputSurface(pushBlankBuffer);
2417         comp->release();
2418     }
2419 
2420     {
2421         Mutexed<State>::Locked state(mState);
2422         state->set(RELEASED);
2423         state->comp.reset();
2424     }
2425     (new AMessage(kWhatRelease, this))->post();
2426     if (sendCallback) {
2427         mCallback->onReleaseCompleted();
2428     }
2429 }
2430 
setSurface(const sp<Surface> & surface,uint32_t generation)2431 status_t CCodec::setSurface(const sp<Surface> &surface, uint32_t generation) {
2432     bool pushBlankBuffer = false;
2433     {
2434         Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2435         const std::unique_ptr<Config> &config = *configLocked;
2436         sp<ANativeWindow> nativeWindow = static_cast<ANativeWindow *>(surface.get());
2437         status_t err = OK;
2438 
2439         if (config->mTunneled && config->mSidebandHandle != nullptr) {
2440             err = native_window_set_sideband_stream(
2441                     nativeWindow.get(),
2442                     const_cast<native_handle_t *>(config->mSidebandHandle->handle()));
2443             if (err != OK) {
2444                 ALOGE("NativeWindow(%p) native_window_set_sideband_stream(%p) failed! (err %d).",
2445                         nativeWindow.get(), config->mSidebandHandle->handle(), err);
2446                 return err;
2447             }
2448         } else {
2449             // Explicitly reset the sideband handle of the window for
2450             // non-tunneled video in case the window was previously used
2451             // for a tunneled video playback.
2452             err = native_window_set_sideband_stream(nativeWindow.get(), nullptr);
2453             if (err != OK) {
2454                 ALOGE("native_window_set_sideband_stream(nullptr) failed! (err %d).", err);
2455                 return err;
2456             }
2457         }
2458         pushBlankBuffer = config->mPushBlankBuffersOnStop;
2459     }
2460     return mChannel->setSurface(surface, generation, pushBlankBuffer);
2461 }
2462 
signalFlush()2463 void CCodec::signalFlush() {
2464     status_t err = [this] {
2465         Mutexed<State>::Locked state(mState);
2466         if (state->get() == FLUSHED) {
2467             return ALREADY_EXISTS;
2468         }
2469         if (state->get() != RUNNING) {
2470             return UNKNOWN_ERROR;
2471         }
2472         state->set(FLUSHING);
2473         return OK;
2474     }();
2475     switch (err) {
2476         case ALREADY_EXISTS:
2477             mCallback->onFlushCompleted();
2478             return;
2479         case OK:
2480             break;
2481         default:
2482             mCallback->onError(err, ACTION_CODE_FATAL);
2483             return;
2484     }
2485 
2486     mChannel->stop();
2487     (new AMessage(kWhatFlush, this))->post();
2488 }
2489 
flush()2490 void CCodec::flush() {
2491     std::shared_ptr<Codec2Client::Component> comp;
2492     auto checkFlushing = [this, &comp] {
2493         Mutexed<State>::Locked state(mState);
2494         if (state->get() != FLUSHING) {
2495             return UNKNOWN_ERROR;
2496         }
2497         comp = state->comp;
2498         return OK;
2499     };
2500     if (tryAndReportOnError(checkFlushing) != OK) {
2501         return;
2502     }
2503 
2504     std::list<std::unique_ptr<C2Work>> flushedWork;
2505     c2_status_t err = comp->flush(C2Component::FLUSH_COMPONENT, &flushedWork);
2506     {
2507         Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue);
2508         flushedWork.splice(flushedWork.end(), *queue);
2509     }
2510     if (err != C2_OK) {
2511         // TODO: convert err into status_t
2512         mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
2513     }
2514 
2515     mChannel->flush(flushedWork);
2516 
2517     {
2518         Mutexed<State>::Locked state(mState);
2519         if (state->get() == FLUSHING) {
2520             state->set(FLUSHED);
2521         }
2522     }
2523     mCallback->onFlushCompleted();
2524 }
2525 
signalResume()2526 void CCodec::signalResume() {
2527     std::shared_ptr<Codec2Client::Component> comp;
2528     auto setResuming = [this, &comp] {
2529         Mutexed<State>::Locked state(mState);
2530         if (state->get() != FLUSHED) {
2531             return UNKNOWN_ERROR;
2532         }
2533         state->set(RESUMING);
2534         comp = state->comp;
2535         return OK;
2536     };
2537     if (tryAndReportOnError(setResuming) != OK) {
2538         return;
2539     }
2540 
2541     {
2542         Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2543         const std::unique_ptr<Config> &config = *configLocked;
2544         sp<AMessage> outputFormat = config->mOutputFormat;
2545         config->queryConfiguration(comp);
2546         RevertOutputFormatIfNeeded(outputFormat, config->mOutputFormat);
2547     }
2548 
2549     std::map<size_t, sp<MediaCodecBuffer>> clientInputBuffers;
2550     status_t err = mChannel->prepareInitialInputBuffers(&clientInputBuffers, true);
2551     if (err != OK) {
2552         if (err == NO_MEMORY) {
2553             // NO_MEMORY happens here when all the buffers are still
2554             // with the codec. That is not an error as it is momentarily
2555             // and the buffers are send to the client as soon as the codec
2556             // releases them
2557             ALOGI("Resuming with all input buffers still with codec");
2558         } else {
2559             ALOGE("Resume request for Input Buffers failed");
2560             mCallback->onError(err, ACTION_CODE_FATAL);
2561             return;
2562         }
2563     }
2564 
2565     // channel start should be called after prepareInitialBuffers
2566     // Calling before can cause a failure during prepare when
2567     // buffers are sent to the client before preparation from onWorkDone
2568     (void)mChannel->start(nullptr, nullptr, [&]{
2569         Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2570         const std::unique_ptr<Config> &config = *configLocked;
2571         return config->mBuffersBoundToCodec;
2572     }());
2573     {
2574         Mutexed<State>::Locked state(mState);
2575         if (state->get() != RESUMING) {
2576             state.unlock();
2577             mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
2578             state.lock();
2579             return;
2580         }
2581         state->set(RUNNING);
2582     }
2583 
2584     mChannel->requestInitialInputBuffers(std::move(clientInputBuffers));
2585 }
2586 
signalSetParameters(const sp<AMessage> & msg)2587 void CCodec::signalSetParameters(const sp<AMessage> &msg) {
2588     std::shared_ptr<Codec2Client::Component> comp;
2589     auto checkState = [this, &comp] {
2590         Mutexed<State>::Locked state(mState);
2591         if (state->get() == RELEASED) {
2592             return INVALID_OPERATION;
2593         }
2594         comp = state->comp;
2595         return OK;
2596     };
2597     if (tryAndReportOnError(checkState) != OK) {
2598         return;
2599     }
2600 
2601     // NOTE: We used to ignore "bitrate" at setParameters; replicate
2602     //       the behavior here.
2603     sp<AMessage> params = msg;
2604     int32_t bitrate;
2605     if (params->findInt32(KEY_BIT_RATE, &bitrate)) {
2606         params = msg->dup();
2607         params->removeEntryAt(params->findEntryByName(KEY_BIT_RATE));
2608     }
2609 
2610     int32_t syncId = 0;
2611     if (params->findInt32("audio-hw-sync", &syncId)
2612             || params->findInt32("hw-av-sync-id", &syncId)) {
2613         configureTunneledVideoPlayback(comp, nullptr, params);
2614     }
2615 
2616     Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2617     const std::unique_ptr<Config> &config = *configLocked;
2618 
2619     /**
2620      * Handle input surface parameters
2621      */
2622     if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))
2623             && (config->mDomain & Config::IS_ENCODER)
2624             && config->mInputSurface && config->mISConfig) {
2625         (void)params->findInt64(PARAMETER_KEY_OFFSET_TIME, &config->mISConfig->mTimeOffsetUs);
2626 
2627         if (params->findInt64("skip-frames-before", &config->mISConfig->mStartAtUs)) {
2628             config->mISConfig->mStopped = false;
2629         } else if (params->findInt64("stop-time-us", &config->mISConfig->mStopAtUs)) {
2630             config->mISConfig->mStopped = true;
2631         }
2632 
2633         int32_t value;
2634         if (params->findInt32(PARAMETER_KEY_SUSPEND, &value)) {
2635             config->mISConfig->mSuspended = value;
2636             config->mISConfig->mSuspendAtUs = -1;
2637             (void)params->findInt64(PARAMETER_KEY_SUSPEND_TIME, &config->mISConfig->mSuspendAtUs);
2638         }
2639 
2640         (void)config->mInputSurface->configure(*config->mISConfig);
2641         if (config->mISConfig->mStopped) {
2642             config->mInputFormat->setInt64(
2643                     "android._stop-time-offset-us", config->mISConfig->mInputDelayUs);
2644         }
2645     }
2646 
2647     /**
2648      * Handle ROI QP map configuration. Recover the QP map configuration from AMessage as an
2649      * ABuffer and configure to CCodecBufferChannel as a C2InfoBuffer
2650      */
2651     if (android::media::codec::provider_->region_of_interest()
2652             && android::media::codec::provider_->region_of_interest_support()) {
2653         sp<ABuffer> qpOffsetMap;
2654         if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))
2655                 && (config->mDomain & Config::IS_ENCODER)
2656                 &&  params->findBuffer(PARAMETER_KEY_QP_OFFSET_MAP, &qpOffsetMap)) {
2657             std::shared_ptr<C2BlockPool> pool;
2658             // TODO(b/331443865) Use pooled block pool to improve efficiency
2659             c2_status_t status = GetCodec2BlockPool(C2BlockPool::BASIC_LINEAR, nullptr, &pool);
2660 
2661             if (status == C2_OK) {
2662                 int width, height;
2663                 config->mInputFormat->findInt32("width", &width);
2664                 config->mInputFormat->findInt32("height", &height);
2665                 // The length of the qp-map corresponds to the number of 16x16 blocks in one frame
2666                 int expectedMapSize = ((width + 15) / 16) * ((height + 15) / 16);
2667                 size_t mapSize = qpOffsetMap->size();
2668                 if (mapSize >= expectedMapSize) {
2669                     std::shared_ptr<C2LinearBlock> block;
2670                     status = pool->fetchLinearBlock(
2671                             expectedMapSize,
2672                             C2MemoryUsage{C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE},
2673                             &block);
2674                     if (status == C2_OK && !block->map().get().error()) {
2675                         C2WriteView wView = block->map().get();
2676                         uint8_t* outData = wView.data();
2677                         memcpy(outData, qpOffsetMap->data(), expectedMapSize);
2678                         C2InfoBuffer info = C2InfoBuffer::CreateLinearBuffer(
2679                                 kParamIndexQpOffsetMapBuffer,
2680                                 block->share(0, expectedMapSize, C2Fence()));
2681                         mChannel->setInfoBuffer(std::make_shared<C2InfoBuffer>(info));
2682                     }
2683                 } else {
2684                     ALOGE("Ignoring param key %s as buffer size %zu is less than expected "
2685                           "buffer size %d",
2686                           PARAMETER_KEY_QP_OFFSET_MAP, mapSize, expectedMapSize);
2687                 }
2688             }
2689             params->removeEntryByName(PARAMETER_KEY_QP_OFFSET_MAP);
2690         }
2691     }
2692 
2693 
2694     std::vector<std::unique_ptr<C2Param>> configUpdate;
2695     (void)config->getConfigUpdateFromSdkParams(
2696             comp, params, Config::IS_PARAM, C2_MAY_BLOCK, &configUpdate);
2697     // Prefer to pass parameters to the buffer channel, so they can be synchronized with the frames.
2698     // Parameter synchronization is not defined when using input surface. For now, route
2699     // these directly to the component.
2700     if (config->mInputSurface == nullptr
2701             && (property_get_bool("debug.stagefright.ccodec_delayed_params", false)
2702                     || comp->getName().find("c2.android.") == 0)) {
2703         std::vector<std::unique_ptr<C2Param>> localConfigUpdate;
2704         for (const std::unique_ptr<C2Param> &param : configUpdate) {
2705             if (param && param->coreIndex().coreIndex() == C2StreamSurfaceScalingInfo::CORE_INDEX) {
2706                 localConfigUpdate.push_back(C2Param::Copy(*param));
2707             }
2708         }
2709         if (!localConfigUpdate.empty()) {
2710             (void)config->setParameters(comp, localConfigUpdate, C2_MAY_BLOCK);
2711         }
2712         mChannel->setParameters(configUpdate);
2713     } else {
2714         sp<AMessage> outputFormat = config->mOutputFormat;
2715         (void)config->setParameters(comp, configUpdate, C2_MAY_BLOCK);
2716         RevertOutputFormatIfNeeded(outputFormat, config->mOutputFormat);
2717     }
2718 }
2719 
signalEndOfInputStream()2720 void CCodec::signalEndOfInputStream() {
2721     mCallback->onSignaledInputEOS(mChannel->signalEndOfInputStream());
2722 }
2723 
signalRequestIDRFrame()2724 void CCodec::signalRequestIDRFrame() {
2725     std::shared_ptr<Codec2Client::Component> comp;
2726     {
2727         Mutexed<State>::Locked state(mState);
2728         if (state->get() == RELEASED) {
2729             ALOGD("no IDR request sent since component is released");
2730             return;
2731         }
2732         comp = state->comp;
2733     }
2734     ALOGV("request IDR");
2735     Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2736     const std::unique_ptr<Config> &config = *configLocked;
2737     std::vector<std::unique_ptr<C2Param>> params;
2738     params.push_back(
2739             std::make_unique<C2StreamRequestSyncFrameTuning::output>(0u, true));
2740     config->setParameters(comp, params, C2_MAY_BLOCK);
2741 }
2742 
querySupportedParameters(std::vector<std::string> * names)2743 status_t CCodec::querySupportedParameters(std::vector<std::string> *names) {
2744     Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2745     const std::unique_ptr<Config> &config = *configLocked;
2746     return config->querySupportedParameters(names);
2747 }
2748 
describeParameter(const std::string & name,CodecParameterDescriptor * desc)2749 status_t CCodec::describeParameter(
2750         const std::string &name, CodecParameterDescriptor *desc) {
2751     Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2752     const std::unique_ptr<Config> &config = *configLocked;
2753     return config->describe(name, desc);
2754 }
2755 
subscribeToParameters(const std::vector<std::string> & names)2756 status_t CCodec::subscribeToParameters(const std::vector<std::string> &names) {
2757     std::shared_ptr<Codec2Client::Component> comp = mState.lock()->comp;
2758     if (!comp) {
2759         return INVALID_OPERATION;
2760     }
2761     Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2762     const std::unique_ptr<Config> &config = *configLocked;
2763     return config->subscribeToVendorConfigUpdate(comp, names);
2764 }
2765 
unsubscribeFromParameters(const std::vector<std::string> & names)2766 status_t CCodec::unsubscribeFromParameters(const std::vector<std::string> &names) {
2767     std::shared_ptr<Codec2Client::Component> comp = mState.lock()->comp;
2768     if (!comp) {
2769         return INVALID_OPERATION;
2770     }
2771     Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2772     const std::unique_ptr<Config> &config = *configLocked;
2773     return config->unsubscribeFromVendorConfigUpdate(comp, names);
2774 }
2775 
onWorkDone(std::list<std::unique_ptr<C2Work>> & workItems)2776 void CCodec::onWorkDone(std::list<std::unique_ptr<C2Work>> &workItems) {
2777     if (!workItems.empty()) {
2778         Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue);
2779         bool shouldPost = queue->empty();
2780         queue->splice(queue->end(), workItems);
2781         if (shouldPost) {
2782             (new AMessage(kWhatWorkDone, this))->post();
2783         }
2784     }
2785 }
2786 
onInputBufferDone(uint64_t frameIndex,size_t arrayIndex)2787 void CCodec::onInputBufferDone(uint64_t frameIndex, size_t arrayIndex) {
2788     mChannel->onInputBufferDone(frameIndex, arrayIndex);
2789     if (arrayIndex == 0) {
2790         // We always put no more than one buffer per work, if we use an input surface.
2791         Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2792         const std::unique_ptr<Config> &config = *configLocked;
2793         if (config->mInputSurface) {
2794             config->mInputSurface->onInputBufferDone(frameIndex);
2795         }
2796     }
2797 }
2798 
onMessageReceived(const sp<AMessage> & msg)2799 void CCodec::onMessageReceived(const sp<AMessage> &msg) {
2800     TimePoint now = std::chrono::steady_clock::now();
2801     CCodecWatchdog::getInstance()->watch(this);
2802     switch (msg->what()) {
2803         case kWhatAllocate: {
2804             // C2ComponentStore::createComponent() should return within 100ms.
2805             setDeadline(now, 1500ms, "allocate");
2806             sp<RefBase> obj;
2807             CHECK(msg->findObject("codecInfo", &obj));
2808             allocate((MediaCodecInfo *)obj.get());
2809             break;
2810         }
2811         case kWhatConfigure: {
2812             // C2Component::commit_sm() should return within 5ms.
2813             setDeadline(now, 1500ms, "configure");
2814             sp<AMessage> format;
2815             CHECK(msg->findMessage("format", &format));
2816             configure(format);
2817             break;
2818         }
2819         case kWhatStart: {
2820             // C2Component::start() should return within 500ms.
2821             setDeadline(now, 1500ms, "start");
2822             start();
2823             break;
2824         }
2825         case kWhatStop: {
2826             // C2Component::stop() should return within 500ms.
2827             setDeadline(now, 1500ms, "stop");
2828             int32_t pushBlankBuffer;
2829             if (!msg->findInt32("pushBlankBuffer", &pushBlankBuffer)) {
2830                 pushBlankBuffer = 0;
2831             }
2832             stop(static_cast<bool>(pushBlankBuffer));
2833             break;
2834         }
2835         case kWhatFlush: {
2836             // C2Component::flush_sm() should return within 5ms.
2837             setDeadline(now, 1500ms, "flush");
2838             flush();
2839             break;
2840         }
2841         case kWhatRelease: {
2842             mChannel->release();
2843             mClient.reset();
2844             mClientListener.reset();
2845             break;
2846         }
2847         case kWhatCreateInputSurface: {
2848             // Surface operations may be briefly blocking.
2849             setDeadline(now, 1500ms, "createInputSurface");
2850             createInputSurface();
2851             break;
2852         }
2853         case kWhatSetInputSurface: {
2854             // Surface operations may be briefly blocking.
2855             setDeadline(now, 1500ms, "setInputSurface");
2856             sp<RefBase> obj;
2857             CHECK(msg->findObject("surface", &obj));
2858             sp<PersistentSurface> surface(static_cast<PersistentSurface *>(obj.get()));
2859             setInputSurface(surface);
2860             break;
2861         }
2862         case kWhatWorkDone: {
2863             std::unique_ptr<C2Work> work;
2864             bool shouldPost = false;
2865             {
2866                 Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue);
2867                 if (queue->empty()) {
2868                     break;
2869                 }
2870                 work.swap(queue->front());
2871                 queue->pop_front();
2872                 shouldPost = !queue->empty();
2873             }
2874             if (shouldPost) {
2875                 (new AMessage(kWhatWorkDone, this))->post();
2876             }
2877 
2878             // handle configuration changes in work done
2879             std::shared_ptr<const C2StreamInitDataInfo::output> initData;
2880             sp<AMessage> inputFormat = nullptr;
2881             sp<AMessage> outputFormat = nullptr;
2882             {
2883                 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2884                 const std::unique_ptr<Config> &config = *configLocked;
2885                 Config::Watcher<C2StreamInitDataInfo::output> initDataWatcher =
2886                     config->watch<C2StreamInitDataInfo::output>();
2887                 if (!work->worklets.empty()
2888                         && (work->worklets.front()->output.flags
2889                                 & C2FrameData::FLAG_DISCARD_FRAME) == 0) {
2890 
2891                     // copy buffer info to config
2892                     std::vector<std::unique_ptr<C2Param>> updates;
2893                     for (const std::unique_ptr<C2Param> &param
2894                             : work->worklets.front()->output.configUpdate) {
2895                         updates.push_back(C2Param::Copy(*param));
2896                     }
2897                     unsigned stream = 0;
2898                     std::vector<std::shared_ptr<C2Buffer>> &outputBuffers =
2899                         work->worklets.front()->output.buffers;
2900                     for (const std::shared_ptr<C2Buffer> &buf : outputBuffers) {
2901                         for (const std::shared_ptr<const C2Info> &info : buf->info()) {
2902                             // move all info into output-stream #0 domain
2903                             updates.emplace_back(
2904                                     C2Param::CopyAsStream(*info, true /* output */, stream));
2905                         }
2906 
2907                         const std::vector<C2ConstGraphicBlock> blocks = buf->data().graphicBlocks();
2908                         // for now only do the first block
2909                         if (!blocks.empty()) {
2910                             // ALOGV("got output buffer with crop %u,%u+%u,%u and size %u,%u",
2911                             //      block.crop().left, block.crop().top,
2912                             //      block.crop().width, block.crop().height,
2913                             //      block.width(), block.height());
2914                             const C2ConstGraphicBlock &block = blocks[0];
2915                             updates.emplace_back(new C2StreamCropRectInfo::output(
2916                                     stream, block.crop()));
2917                         }
2918                         ++stream;
2919                     }
2920 
2921                     sp<AMessage> oldFormat = config->mOutputFormat;
2922                     config->updateConfiguration(updates, config->mOutputDomain);
2923                     RevertOutputFormatIfNeeded(oldFormat, config->mOutputFormat);
2924 
2925                     // copy standard infos to graphic buffers if not already present (otherwise, we
2926                     // may overwrite the actual intermediate value with a final value)
2927                     stream = 0;
2928                     const static C2Param::Index stdGfxInfos[] = {
2929                         C2StreamRotationInfo::output::PARAM_TYPE,
2930                         C2StreamColorAspectsInfo::output::PARAM_TYPE,
2931                         C2StreamDataSpaceInfo::output::PARAM_TYPE,
2932                         C2StreamHdrStaticInfo::output::PARAM_TYPE,
2933                         C2StreamHdr10PlusInfo::output::PARAM_TYPE,  // will be deprecated
2934                         C2StreamHdrDynamicMetadataInfo::output::PARAM_TYPE,
2935                         C2StreamPixelAspectRatioInfo::output::PARAM_TYPE,
2936                         C2StreamSurfaceScalingInfo::output::PARAM_TYPE
2937                     };
2938                     for (const std::shared_ptr<C2Buffer> &buf : outputBuffers) {
2939                         if (buf->data().graphicBlocks().size()) {
2940                             for (C2Param::Index ix : stdGfxInfos) {
2941                                 if (!buf->hasInfo(ix)) {
2942                                     const C2Param *param =
2943                                         config->getConfigParameterValue(ix.withStream(stream));
2944                                     if (param) {
2945                                         std::shared_ptr<C2Param> info(C2Param::Copy(*param));
2946                                         buf->setInfo(std::static_pointer_cast<C2Info>(info));
2947                                     }
2948                                 }
2949                             }
2950                         }
2951                         ++stream;
2952                     }
2953                 }
2954                 if (config->mInputSurface) {
2955                     if (work->worklets.empty()
2956                            || !work->worklets.back()
2957                            || (work->worklets.back()->output.flags
2958                                   & C2FrameData::FLAG_INCOMPLETE) == 0) {
2959                         config->mInputSurface->onInputBufferDone(work->input.ordinal.frameIndex);
2960                     }
2961                 }
2962                 if (initDataWatcher.hasChanged()) {
2963                     initData = initDataWatcher.update();
2964                     AmendOutputFormatWithCodecSpecificData(
2965                             initData->m.value, initData->flexCount(), config->mCodingMediaType,
2966                             config->mOutputFormat);
2967                 }
2968                 inputFormat = config->mInputFormat;
2969                 outputFormat = config->mOutputFormat;
2970             }
2971             mChannel->onWorkDone(
2972                     std::move(work), inputFormat, outputFormat,
2973                     initData ? initData.get() : nullptr);
2974             // log metrics to MediaCodec
2975             if (mMetrics->countEntries() == 0) {
2976                 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2977                 const std::unique_ptr<Config> &config = *configLocked;
2978                 uint32_t pf = PIXEL_FORMAT_UNKNOWN;
2979                 if (!config->mInputSurface) {
2980                     pf = mChannel->getBuffersPixelFormat(config->mDomain & Config::IS_ENCODER);
2981                 } else {
2982                     pf = config->mInputSurface->getPixelFormat();
2983                 }
2984                 if (pf != PIXEL_FORMAT_UNKNOWN) {
2985                     mMetrics->setInt64(kCodecPixelFormat, pf);
2986                     mCallback->onMetricsUpdated(mMetrics);
2987                 }
2988             }
2989             break;
2990         }
2991         case kWhatWatch: {
2992             // watch message already posted; no-op.
2993             break;
2994         }
2995         default: {
2996             ALOGE("unrecognized message");
2997             break;
2998         }
2999     }
3000     setDeadline(TimePoint::max(), 0ms, "none");
3001 }
3002 
setDeadline(const TimePoint & now,const std::chrono::milliseconds & timeout,const char * name)3003 void CCodec::setDeadline(
3004         const TimePoint &now,
3005         const std::chrono::milliseconds &timeout,
3006         const char *name) {
3007     int32_t mult = std::max(1, property_get_int32("debug.stagefright.ccodec_timeout_mult", 1));
3008     Mutexed<NamedTimePoint>::Locked deadline(mDeadline);
3009     deadline->set(now + (timeout * mult), name);
3010 }
3011 
configureTunneledVideoPlayback(std::shared_ptr<Codec2Client::Component> comp,sp<NativeHandle> * sidebandHandle,const sp<AMessage> & msg)3012 status_t CCodec::configureTunneledVideoPlayback(
3013         std::shared_ptr<Codec2Client::Component> comp,
3014         sp<NativeHandle> *sidebandHandle,
3015         const sp<AMessage> &msg) {
3016     std::vector<std::unique_ptr<C2SettingResult>> failures;
3017 
3018     std::unique_ptr<C2PortTunneledModeTuning::output> tunneledPlayback =
3019         C2PortTunneledModeTuning::output::AllocUnique(
3020             1,
3021             C2PortTunneledModeTuning::Struct::SIDEBAND,
3022             C2PortTunneledModeTuning::Struct::REALTIME,
3023             0);
3024     // TODO: use KEY_AUDIO_HW_SYNC, KEY_HARDWARE_AV_SYNC_ID when they are in MediaCodecConstants.h
3025     if (msg->findInt32("audio-hw-sync", &tunneledPlayback->m.syncId[0])) {
3026         tunneledPlayback->m.syncType = C2PortTunneledModeTuning::Struct::sync_type_t::AUDIO_HW_SYNC;
3027     } else if (msg->findInt32("hw-av-sync-id", &tunneledPlayback->m.syncId[0])) {
3028         tunneledPlayback->m.syncType = C2PortTunneledModeTuning::Struct::sync_type_t::HW_AV_SYNC;
3029     } else {
3030         tunneledPlayback->m.syncType = C2PortTunneledModeTuning::Struct::sync_type_t::REALTIME;
3031         tunneledPlayback->setFlexCount(0);
3032     }
3033     c2_status_t c2err = comp->config({ tunneledPlayback.get() }, C2_MAY_BLOCK, &failures);
3034     if (c2err != C2_OK) {
3035         return UNKNOWN_ERROR;
3036     }
3037 
3038     if (sidebandHandle == nullptr) {
3039         return OK;
3040     }
3041 
3042     std::vector<std::unique_ptr<C2Param>> params;
3043     c2err = comp->query({}, {C2PortTunnelHandleTuning::output::PARAM_TYPE}, C2_DONT_BLOCK, &params);
3044     if (c2err == C2_OK && params.size() == 1u) {
3045         C2PortTunnelHandleTuning::output *videoTunnelSideband =
3046             C2PortTunnelHandleTuning::output::From(params[0].get());
3047         // Currently, Codec2 only supports non-fd case for sideband native_handle.
3048         native_handle_t *handle = native_handle_create(0, videoTunnelSideband->flexCount());
3049         *sidebandHandle = NativeHandle::create(handle, true /* ownsHandle */);
3050         if (handle != nullptr && videoTunnelSideband->flexCount()) {
3051             memcpy(handle->data, videoTunnelSideband->m.values,
3052                     sizeof(int32_t) * videoTunnelSideband->flexCount());
3053             return OK;
3054         } else {
3055             return NO_MEMORY;
3056         }
3057     }
3058     return UNKNOWN_ERROR;
3059 }
3060 
initiateReleaseIfStuck()3061 void CCodec::initiateReleaseIfStuck() {
3062     std::string name;
3063     bool pendingDeadline = false;
3064     {
3065         Mutexed<NamedTimePoint>::Locked deadline(mDeadline);
3066         if (deadline->get() < std::chrono::steady_clock::now()) {
3067             name = deadline->getName();
3068         }
3069         if (deadline->get() != TimePoint::max()) {
3070             pendingDeadline = true;
3071         }
3072     }
3073     if (name.empty()) {
3074         // We're not stuck.
3075         if (pendingDeadline) {
3076             // If we are not stuck yet but still has deadline coming up,
3077             // post watch message to check back later.
3078             (new AMessage(kWhatWatch, this))->post();
3079         }
3080         return;
3081     }
3082 
3083     C2String compName;
3084     {
3085         Mutexed<State>::Locked state(mState);
3086         if (!state->comp) {
3087             ALOGD("previous call to %s exceeded timeout "
3088                   "and the component is already released", name.c_str());
3089             return;
3090         }
3091         compName = state->comp->getName();
3092     }
3093     ALOGW("[%s] previous call to %s exceeded timeout", compName.c_str(), name.c_str());
3094 
3095     initiateRelease(false);
3096     mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
3097 }
3098 
3099 // static
CreateInputSurface()3100 PersistentSurface *CCodec::CreateInputSurface() {
3101     using namespace android;
3102     using ::android::hardware::media::omx::V1_0::implementation::TWGraphicBufferSource;
3103     // Attempt to create a Codec2's input surface.
3104     std::shared_ptr<Codec2Client::InputSurface> inputSurface =
3105             Codec2Client::CreateInputSurface();
3106     if (!inputSurface) {
3107         if (property_get_int32("debug.stagefright.c2inputsurface", 0) == -1) {
3108             if (Codec2Client::IsAidlSelected()) {
3109                 sp<IGraphicBufferProducer> gbp;
3110                 sp<AidlGraphicBufferSource> gbs = new AidlGraphicBufferSource();
3111                 status_t err = gbs->initCheck();
3112                 if (err != OK) {
3113                     ALOGE("Failed to create persistent input surface: error %d", err);
3114                     return nullptr;
3115                 }
3116                 ALOGD("aidl based PersistentSurface created");
3117                 std::shared_ptr<WAidlGraphicBufferSource> wrapper =
3118                         ::ndk::SharedRefBase::make<WAidlGraphicBufferSource>(gbs);
3119 
3120                 return new PersistentSurface(
3121                       gbs->getIGraphicBufferProducer(), wrapper->asBinder());
3122             } else {
3123                 sp<IGraphicBufferProducer> gbp;
3124                 sp<OmxGraphicBufferSource> gbs = new OmxGraphicBufferSource();
3125                 status_t err = gbs->initCheck();
3126                 if (err != OK) {
3127                     ALOGE("Failed to create persistent input surface: error %d", err);
3128                     return nullptr;
3129                 }
3130                 ALOGD("hidl based PersistentSurface created");
3131                 return new PersistentSurface(
3132                         gbs->getIGraphicBufferProducer(), new TWGraphicBufferSource(gbs));
3133             }
3134         } else {
3135             return nullptr;
3136         }
3137     }
3138     return new PersistentSurface(
3139             inputSurface->getGraphicBufferProducer(),
3140             static_cast<sp<android::hidl::base::V1_0::IBase>>(
3141             inputSurface->getHalInterface()));
3142 }
3143 
3144 class IntfCache {
3145 public:
3146     IntfCache() = default;
3147 
init(const std::string & name)3148     status_t init(const std::string &name) {
3149         std::shared_ptr<Codec2Client::Interface> intf{
3150             Codec2Client::CreateInterfaceByName(name.c_str())};
3151         if (!intf) {
3152             ALOGW("IntfCache [%s]: Unrecognized interface name", name.c_str());
3153             mInitStatus = NO_INIT;
3154             return NO_INIT;
3155         }
3156         const static C2StreamUsageTuning::input sUsage{0u /* stream id */};
3157         mFields.push_back(C2FieldSupportedValuesQuery::Possible(
3158                 C2ParamField{&sUsage, &sUsage.value}));
3159         c2_status_t err = intf->querySupportedValues(mFields, C2_MAY_BLOCK);
3160         if (err != C2_OK) {
3161             ALOGW("IntfCache [%s]: failed to query usage supported value (err=%d)",
3162                     name.c_str(), err);
3163             mFields[0].status = err;
3164         }
3165         std::vector<std::unique_ptr<C2Param>> params;
3166         err = intf->query(
3167                 {&mApiFeatures},
3168                 {
3169                     C2StreamBufferTypeSetting::input::PARAM_TYPE,
3170                     C2PortAllocatorsTuning::input::PARAM_TYPE
3171                 },
3172                 C2_MAY_BLOCK,
3173                 &params);
3174         if (err != C2_OK && err != C2_BAD_INDEX) {
3175             ALOGW("IntfCache [%s]: failed to query api features (err=%d)",
3176                     name.c_str(), err);
3177         }
3178         while (!params.empty()) {
3179             C2Param *param = params.back().release();
3180             params.pop_back();
3181             if (!param) {
3182                 continue;
3183             }
3184             if (param->type() == C2StreamBufferTypeSetting::input::PARAM_TYPE) {
3185                 mInputStreamFormat.reset(
3186                         C2StreamBufferTypeSetting::input::From(param));
3187             } else if (param->type() == C2PortAllocatorsTuning::input::PARAM_TYPE) {
3188                 mInputAllocators.reset(
3189                         C2PortAllocatorsTuning::input::From(param));
3190             }
3191         }
3192         mInitStatus = OK;
3193         return OK;
3194     }
3195 
initCheck() const3196     status_t initCheck() const { return mInitStatus; }
3197 
getUsageSupportedValues() const3198     const C2FieldSupportedValuesQuery &getUsageSupportedValues() const {
3199         CHECK_EQ(1u, mFields.size());
3200         return mFields[0];
3201     }
3202 
getApiFeatures() const3203     const C2ApiFeaturesSetting &getApiFeatures() const {
3204         return mApiFeatures;
3205     }
3206 
getInputStreamFormat() const3207     const C2StreamBufferTypeSetting::input &getInputStreamFormat() const {
3208         static std::unique_ptr<C2StreamBufferTypeSetting::input> sInvalidated = []{
3209             std::unique_ptr<C2StreamBufferTypeSetting::input> param;
3210             param.reset(new C2StreamBufferTypeSetting::input(0u, C2BufferData::INVALID));
3211             param->invalidate();
3212             return param;
3213         }();
3214         return mInputStreamFormat ? *mInputStreamFormat : *sInvalidated;
3215     }
3216 
getInputAllocators() const3217     const C2PortAllocatorsTuning::input &getInputAllocators() const {
3218         static std::unique_ptr<C2PortAllocatorsTuning::input> sInvalidated = []{
3219             std::unique_ptr<C2PortAllocatorsTuning::input> param =
3220                 C2PortAllocatorsTuning::input::AllocUnique(0);
3221             param->invalidate();
3222             return param;
3223         }();
3224         return mInputAllocators ? *mInputAllocators : *sInvalidated;
3225     }
3226 
3227 private:
3228     status_t mInitStatus{NO_INIT};
3229 
3230     std::vector<C2FieldSupportedValuesQuery> mFields;
3231     C2ApiFeaturesSetting mApiFeatures;
3232     std::unique_ptr<C2StreamBufferTypeSetting::input> mInputStreamFormat;
3233     std::unique_ptr<C2PortAllocatorsTuning::input> mInputAllocators;
3234 };
3235 
GetIntfCache(const std::string & name)3236 static const IntfCache &GetIntfCache(const std::string &name) {
3237     static IntfCache sNullIntfCache;
3238     static std::mutex sMutex;
3239     static std::map<std::string, IntfCache> sCache;
3240     std::unique_lock<std::mutex> lock{sMutex};
3241     auto it = sCache.find(name);
3242     if (it == sCache.end()) {
3243         lock.unlock();
3244         IntfCache intfCache;
3245         status_t err = intfCache.init(name);
3246         if (err != OK) {
3247             return sNullIntfCache;
3248         }
3249         lock.lock();
3250         it = sCache.insert({name, std::move(intfCache)}).first;
3251     }
3252     return it->second;
3253 }
3254 
GetCommonAllocatorIds(const std::vector<std::string> & names,C2Allocator::type_t type,std::set<C2Allocator::id_t> * ids)3255 static status_t GetCommonAllocatorIds(
3256         const std::vector<std::string> &names,
3257         C2Allocator::type_t type,
3258         std::set<C2Allocator::id_t> *ids) {
3259     int poolMask = GetCodec2PoolMask();
3260     C2PlatformAllocatorStore::id_t preferredLinearId = GetPreferredLinearAllocatorId(poolMask);
3261     C2Allocator::id_t defaultAllocatorId =
3262         (type == C2Allocator::LINEAR) ? preferredLinearId : C2PlatformAllocatorStore::GRALLOC;
3263 
3264     ids->clear();
3265     if (names.empty()) {
3266         return OK;
3267     }
3268     bool firstIteration = true;
3269     for (const std::string &name : names) {
3270         const IntfCache &intfCache = GetIntfCache(name);
3271         if (intfCache.initCheck() != OK) {
3272             continue;
3273         }
3274         const C2StreamBufferTypeSetting::input &streamFormat = intfCache.getInputStreamFormat();
3275         if (streamFormat) {
3276             C2Allocator::type_t allocatorType = C2Allocator::LINEAR;
3277             if (streamFormat.value == C2BufferData::GRAPHIC
3278                     || streamFormat.value == C2BufferData::GRAPHIC_CHUNKS) {
3279                 allocatorType = C2Allocator::GRAPHIC;
3280             }
3281 
3282             if (type != allocatorType) {
3283                 // requested type is not supported at input allocators
3284                 ids->clear();
3285                 ids->insert(defaultAllocatorId);
3286                 ALOGV("name(%s) does not support a type(0x%x) as input allocator."
3287                         " uses default allocator id(%d)", name.c_str(), type, defaultAllocatorId);
3288                 break;
3289             }
3290         }
3291 
3292         const C2PortAllocatorsTuning::input &allocators = intfCache.getInputAllocators();
3293         if (firstIteration) {
3294             firstIteration = false;
3295             if (allocators && allocators.flexCount() > 0) {
3296                 ids->insert(allocators.m.values,
3297                             allocators.m.values + allocators.flexCount());
3298             }
3299             if (ids->empty()) {
3300                 // The component does not advertise allocators. Use default.
3301                 ids->insert(defaultAllocatorId);
3302             }
3303             continue;
3304         }
3305         bool filtered = false;
3306         if (allocators && allocators.flexCount() > 0) {
3307             filtered = true;
3308             for (auto it = ids->begin(); it != ids->end(); ) {
3309                 bool found = false;
3310                 for (size_t j = 0; j < allocators.flexCount(); ++j) {
3311                     if (allocators.m.values[j] == *it) {
3312                         found = true;
3313                         break;
3314                     }
3315                 }
3316                 if (found) {
3317                     ++it;
3318                 } else {
3319                     it = ids->erase(it);
3320                 }
3321             }
3322         }
3323         if (!filtered) {
3324             // The component does not advertise supported allocators. Use default.
3325             bool containsDefault = (ids->count(defaultAllocatorId) > 0u);
3326             if (ids->size() != (containsDefault ? 1 : 0)) {
3327                 ids->clear();
3328                 if (containsDefault) {
3329                     ids->insert(defaultAllocatorId);
3330                 }
3331             }
3332         }
3333     }
3334     // Finally, filter with pool masks
3335     for (auto it = ids->begin(); it != ids->end(); ) {
3336         if ((poolMask >> *it) & 1) {
3337             ++it;
3338         } else {
3339             it = ids->erase(it);
3340         }
3341     }
3342     return OK;
3343 }
3344 
CalculateMinMaxUsage(const std::vector<std::string> & names,uint64_t * minUsage,uint64_t * maxUsage)3345 static status_t CalculateMinMaxUsage(
3346         const std::vector<std::string> &names, uint64_t *minUsage, uint64_t *maxUsage) {
3347     static C2StreamUsageTuning::input sUsage{0u /* stream id */};
3348     *minUsage = 0;
3349     *maxUsage = ~0ull;
3350     for (const std::string &name : names) {
3351         const IntfCache &intfCache = GetIntfCache(name);
3352         if (intfCache.initCheck() != OK) {
3353             continue;
3354         }
3355         const C2FieldSupportedValuesQuery &usageSupportedValues =
3356             intfCache.getUsageSupportedValues();
3357         if (usageSupportedValues.status != C2_OK) {
3358             continue;
3359         }
3360         const C2FieldSupportedValues &supported = usageSupportedValues.values;
3361         if (supported.type != C2FieldSupportedValues::FLAGS) {
3362             continue;
3363         }
3364         if (supported.values.empty()) {
3365             *maxUsage = 0;
3366             continue;
3367         }
3368         if (supported.values.size() > 1) {
3369             *minUsage |= supported.values[1].u64;
3370         } else {
3371             *minUsage |= supported.values[0].u64;
3372         }
3373         int64_t currentMaxUsage = 0;
3374         for (const C2Value::Primitive &flags : supported.values) {
3375             currentMaxUsage |= flags.u64;
3376         }
3377         *maxUsage &= currentMaxUsage;
3378     }
3379     return OK;
3380 }
3381 
3382 // static
CanFetchLinearBlock(const std::vector<std::string> & names,const C2MemoryUsage & usage,bool * isCompatible)3383 status_t CCodec::CanFetchLinearBlock(
3384         const std::vector<std::string> &names, const C2MemoryUsage &usage, bool *isCompatible) {
3385     for (const std::string &name : names) {
3386         const IntfCache &intfCache = GetIntfCache(name);
3387         if (intfCache.initCheck() != OK) {
3388             continue;
3389         }
3390         const C2ApiFeaturesSetting &features = intfCache.getApiFeatures();
3391         if (features && !(features.value & API_SAME_INPUT_BUFFER)) {
3392             *isCompatible = false;
3393             return OK;
3394         }
3395     }
3396     std::set<C2Allocator::id_t> allocators;
3397     GetCommonAllocatorIds(names, C2Allocator::LINEAR, &allocators);
3398     if (allocators.empty()) {
3399         *isCompatible = false;
3400         return OK;
3401     }
3402 
3403     uint64_t minUsage = 0;
3404     uint64_t maxUsage = ~0ull;
3405     CalculateMinMaxUsage(names, &minUsage, &maxUsage);
3406     minUsage |= usage.expected;
3407     *isCompatible = ((maxUsage & minUsage) == minUsage);
3408     return OK;
3409 }
3410 
GetPool(C2Allocator::id_t allocId)3411 static std::shared_ptr<C2BlockPool> GetPool(C2Allocator::id_t allocId) {
3412     static std::mutex sMutex{};
3413     static std::map<C2Allocator::id_t, std::shared_ptr<C2BlockPool>> sPools;
3414     std::unique_lock<std::mutex> lock{sMutex};
3415     std::shared_ptr<C2BlockPool> pool;
3416     auto it = sPools.find(allocId);
3417     if (it == sPools.end()) {
3418         c2_status_t err = CreateCodec2BlockPool(allocId, nullptr, &pool);
3419         if (err == OK) {
3420             sPools.emplace(allocId, pool);
3421         } else {
3422             pool.reset();
3423         }
3424     } else {
3425         pool = it->second;
3426     }
3427     return pool;
3428 }
3429 
3430 // static
FetchLinearBlock(size_t capacity,const C2MemoryUsage & usage,const std::vector<std::string> & names)3431 std::shared_ptr<C2LinearBlock> CCodec::FetchLinearBlock(
3432         size_t capacity, const C2MemoryUsage &usage, const std::vector<std::string> &names) {
3433     std::set<C2Allocator::id_t> allocators;
3434     GetCommonAllocatorIds(names, C2Allocator::LINEAR, &allocators);
3435     if (allocators.empty()) {
3436         allocators.insert(C2PlatformAllocatorStore::DEFAULT_LINEAR);
3437     }
3438 
3439     uint64_t minUsage = 0;
3440     uint64_t maxUsage = ~0ull;
3441     CalculateMinMaxUsage(names, &minUsage, &maxUsage);
3442     minUsage |= usage.expected;
3443     if ((maxUsage & minUsage) != minUsage) {
3444         allocators.clear();
3445         allocators.insert(C2PlatformAllocatorStore::DEFAULT_LINEAR);
3446     }
3447     std::shared_ptr<C2LinearBlock> block;
3448     for (C2Allocator::id_t allocId : allocators) {
3449         std::shared_ptr<C2BlockPool> pool = GetPool(allocId);
3450         if (!pool) {
3451             continue;
3452         }
3453         c2_status_t err = pool->fetchLinearBlock(capacity, C2MemoryUsage{minUsage}, &block);
3454         if (err != C2_OK || !block) {
3455             block.reset();
3456             continue;
3457         }
3458         break;
3459     }
3460     return block;
3461 }
3462 
3463 // static
CanFetchGraphicBlock(const std::vector<std::string> & names,bool * isCompatible)3464 status_t CCodec::CanFetchGraphicBlock(
3465         const std::vector<std::string> &names, bool *isCompatible) {
3466     uint64_t minUsage = 0;
3467     uint64_t maxUsage = ~0ull;
3468     std::set<C2Allocator::id_t> allocators;
3469     GetCommonAllocatorIds(names, C2Allocator::GRAPHIC, &allocators);
3470     if (allocators.empty()) {
3471         *isCompatible = false;
3472         return OK;
3473     }
3474     CalculateMinMaxUsage(names, &minUsage, &maxUsage);
3475     *isCompatible = ((maxUsage & minUsage) == minUsage);
3476     return OK;
3477 }
3478 
3479 // static
FetchGraphicBlock(int32_t width,int32_t height,int32_t format,uint64_t usage,const std::vector<std::string> & names)3480 std::shared_ptr<C2GraphicBlock> CCodec::FetchGraphicBlock(
3481         int32_t width,
3482         int32_t height,
3483         int32_t format,
3484         uint64_t usage,
3485         const std::vector<std::string> &names) {
3486     uint32_t halPixelFormat = HAL_PIXEL_FORMAT_YCBCR_420_888;
3487     if (!C2Mapper::mapPixelFormatFrameworkToCodec(format, &halPixelFormat)) {
3488         ALOGD("Unrecognized pixel format: %d", format);
3489         return nullptr;
3490     }
3491     uint64_t minUsage = 0;
3492     uint64_t maxUsage = ~0ull;
3493     std::set<C2Allocator::id_t> allocators;
3494     GetCommonAllocatorIds(names, C2Allocator::GRAPHIC, &allocators);
3495     if (allocators.empty()) {
3496         allocators.insert(C2PlatformAllocatorStore::DEFAULT_GRAPHIC);
3497     }
3498     CalculateMinMaxUsage(names, &minUsage, &maxUsage);
3499     minUsage |= usage;
3500     if ((maxUsage & minUsage) != minUsage) {
3501         allocators.clear();
3502         allocators.insert(C2PlatformAllocatorStore::DEFAULT_GRAPHIC);
3503     }
3504     std::shared_ptr<C2GraphicBlock> block;
3505     for (C2Allocator::id_t allocId : allocators) {
3506         std::shared_ptr<C2BlockPool> pool;
3507         c2_status_t err = CreateCodec2BlockPool(allocId, nullptr, &pool);
3508         if (err != C2_OK || !pool) {
3509             continue;
3510         }
3511         err = pool->fetchGraphicBlock(
3512                 width, height, halPixelFormat, C2MemoryUsage{minUsage}, &block);
3513         if (err != C2_OK || !block) {
3514             block.reset();
3515             continue;
3516         }
3517         break;
3518     }
3519     return block;
3520 }
3521 
3522 }  // namespace android
3523