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