blob: 9c679aa4a9172040f2521da37670a74ce42c27d9 [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
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/IGraphicBufferSource.h>
30#include <android/IOMXBufferSource.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/stringprintf.h>
34#include <cutils/properties.h>
35#include <gui/IGraphicBufferProducer.h>
36#include <gui/Surface.h>
37#include <gui/bufferqueue/1.0/H2BGraphicBufferProducer.h>
38#include <media/omx/1.0/WGraphicBufferSource.h>
39#include <media/openmax/OMX_IndexExt.h>
40#include <media/stagefright/BufferProducerWrapper.h>
41#include <media/stagefright/MediaCodecConstants.h>
42#include <media/stagefright/PersistentSurface.h>
43#include <media/stagefright/codec2/1.0/InputSurface.h>
44
45#include "C2OMXNode.h"
46#include "CCodec.h"
47#include "CCodecBufferChannel.h"
48#include "InputSurfaceWrapper.h"
49
50extern "C" android::PersistentSurface *CreateInputSurface();
51
52namespace android {
53
54using namespace std::chrono_literals;
55using ::android::hardware::graphics::bufferqueue::V1_0::utils::H2BGraphicBufferProducer;
56using android::base::StringPrintf;
57using BGraphicBufferSource = ::android::IGraphicBufferSource;
Pawin Vongmasad0f0e142018-11-15 03:36:28 -080058using ::android::hardware::media::c2::V1_0::IInputSurface;
Pawin Vongmasa36653902018-11-15 00:10:25 -080059
60namespace {
61
62class CCodecWatchdog : public AHandler {
63private:
64 enum {
65 kWhatWatch,
66 };
67 constexpr static int64_t kWatchIntervalUs = 3300000; // 3.3 secs
68
69public:
70 static sp<CCodecWatchdog> getInstance() {
71 static sp<CCodecWatchdog> instance(new CCodecWatchdog);
72 static std::once_flag flag;
73 // Call Init() only once.
74 std::call_once(flag, Init, instance);
75 return instance;
76 }
77
78 ~CCodecWatchdog() = default;
79
80 void watch(sp<CCodec> codec) {
81 bool shouldPost = false;
82 {
83 Mutexed<std::set<wp<CCodec>>>::Locked codecs(mCodecsToWatch);
84 // If a watch message is in flight, piggy-back this instance as well.
85 // Otherwise, post a new watch message.
86 shouldPost = codecs->empty();
87 codecs->emplace(codec);
88 }
89 if (shouldPost) {
90 ALOGV("posting watch message");
91 (new AMessage(kWhatWatch, this))->post(kWatchIntervalUs);
92 }
93 }
94
95protected:
96 void onMessageReceived(const sp<AMessage> &msg) {
97 switch (msg->what()) {
98 case kWhatWatch: {
99 Mutexed<std::set<wp<CCodec>>>::Locked codecs(mCodecsToWatch);
100 ALOGV("watch for %zu codecs", codecs->size());
101 for (auto it = codecs->begin(); it != codecs->end(); ++it) {
102 sp<CCodec> codec = it->promote();
103 if (codec == nullptr) {
104 continue;
105 }
106 codec->initiateReleaseIfStuck();
107 }
108 codecs->clear();
109 break;
110 }
111
112 default: {
113 TRESPASS("CCodecWatchdog: unrecognized message");
114 }
115 }
116 }
117
118private:
119 CCodecWatchdog() : mLooper(new ALooper) {}
120
121 static void Init(const sp<CCodecWatchdog> &thiz) {
122 ALOGV("Init");
123 thiz->mLooper->setName("CCodecWatchdog");
124 thiz->mLooper->registerHandler(thiz);
125 thiz->mLooper->start();
126 }
127
128 sp<ALooper> mLooper;
129
130 Mutexed<std::set<wp<CCodec>>> mCodecsToWatch;
131};
132
133class C2InputSurfaceWrapper : public InputSurfaceWrapper {
134public:
135 explicit C2InputSurfaceWrapper(
136 const std::shared_ptr<Codec2Client::InputSurface> &surface) :
137 mSurface(surface) {
138 }
139
140 ~C2InputSurfaceWrapper() override = default;
141
142 status_t connect(const std::shared_ptr<Codec2Client::Component> &comp) override {
143 if (mConnection != nullptr) {
144 return ALREADY_EXISTS;
145 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800146 return toStatusT(comp->connectToInputSurface(mSurface, &mConnection));
Pawin Vongmasa36653902018-11-15 00:10:25 -0800147 }
148
149 void disconnect() override {
150 if (mConnection != nullptr) {
151 mConnection->disconnect();
152 mConnection = nullptr;
153 }
154 }
155
156 status_t start() override {
157 // InputSurface does not distinguish started state
158 return OK;
159 }
160
161 status_t signalEndOfInputStream() override {
162 C2InputSurfaceEosTuning eos(true);
163 std::vector<std::unique_ptr<C2SettingResult>> failures;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800164 c2_status_t err = mSurface->config({&eos}, C2_MAY_BLOCK, &failures);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800165 if (err != C2_OK) {
166 return UNKNOWN_ERROR;
167 }
168 return OK;
169 }
170
171 status_t configure(Config &config __unused) {
172 // TODO
173 return OK;
174 }
175
176private:
177 std::shared_ptr<Codec2Client::InputSurface> mSurface;
178 std::shared_ptr<Codec2Client::InputSurfaceConnection> mConnection;
179};
180
181class GraphicBufferSourceWrapper : public InputSurfaceWrapper {
182public:
183// explicit GraphicBufferSourceWrapper(const sp<BGraphicBufferSource> &source) : mSource(source) {}
184 GraphicBufferSourceWrapper(
185 const sp<BGraphicBufferSource> &source,
186 uint32_t width,
187 uint32_t height)
188 : mSource(source), mWidth(width), mHeight(height) {
189 mDataSpace = HAL_DATASPACE_BT709;
190 }
191 ~GraphicBufferSourceWrapper() override = default;
192
193 status_t connect(const std::shared_ptr<Codec2Client::Component> &comp) override {
194 mNode = new C2OMXNode(comp);
195 mNode->setFrameSize(mWidth, mHeight);
196
197 // NOTE: we do not use/pass through color aspects from GraphicBufferSource as we
198 // communicate that directly to the component.
199 mSource->configure(mNode, mDataSpace);
200 return OK;
201 }
202
203 void disconnect() override {
204 if (mNode == nullptr) {
205 return;
206 }
207 sp<IOMXBufferSource> source = mNode->getSource();
208 if (source == nullptr) {
209 ALOGD("GBSWrapper::disconnect: node is not configured with OMXBufferSource.");
210 return;
211 }
212 source->onOmxIdle();
213 source->onOmxLoaded();
214 mNode.clear();
215 }
216
217 status_t GetStatus(const binder::Status &status) {
218 status_t err = OK;
219 if (!status.isOk()) {
220 err = status.serviceSpecificErrorCode();
221 if (err == OK) {
222 err = status.transactionError();
223 if (err == OK) {
224 // binder status failed, but there is no servie or transaction error
225 err = UNKNOWN_ERROR;
226 }
227 }
228 }
229 return err;
230 }
231
232 status_t start() override {
233 sp<IOMXBufferSource> source = mNode->getSource();
234 if (source == nullptr) {
235 return NO_INIT;
236 }
237 constexpr size_t kNumSlots = 16;
238 for (size_t i = 0; i < kNumSlots; ++i) {
239 source->onInputBufferAdded(i);
240 }
241
242 source->onOmxExecuting();
243 return OK;
244 }
245
246 status_t signalEndOfInputStream() override {
247 return GetStatus(mSource->signalEndOfInputStream());
248 }
249
250 status_t configure(Config &config) {
251 std::stringstream status;
252 status_t err = OK;
253
254 // handle each configuration granually, in case we need to handle part of the configuration
255 // elsewhere
256
257 // TRICKY: we do not unset frame delay repeating
258 if (config.mMinFps > 0 && config.mMinFps != mConfig.mMinFps) {
259 int64_t us = 1e6 / config.mMinFps + 0.5;
260 status_t res = GetStatus(mSource->setRepeatPreviousFrameDelayUs(us));
261 status << " minFps=" << config.mMinFps << " => repeatDelayUs=" << us;
262 if (res != OK) {
263 status << " (=> " << asString(res) << ")";
264 err = res;
265 }
266 mConfig.mMinFps = config.mMinFps;
267 }
268
269 // pts gap
270 if (config.mMinAdjustedFps > 0 || config.mFixedAdjustedFps > 0) {
271 if (mNode != nullptr) {
272 OMX_PARAM_U32TYPE ptrGapParam = {};
273 ptrGapParam.nSize = sizeof(OMX_PARAM_U32TYPE);
Wonsik Kim95ba0162019-03-19 15:51:54 -0700274 float gap = (config.mMinAdjustedFps > 0)
Pawin Vongmasa36653902018-11-15 00:10:25 -0800275 ? c2_min(INT32_MAX + 0., 1e6 / config.mMinAdjustedFps + 0.5)
276 : c2_max(0. - INT32_MAX, -1e6 / config.mFixedAdjustedFps - 0.5);
Wonsik Kim95ba0162019-03-19 15:51:54 -0700277 // float -> uint32_t is undefined if the value is negative.
278 // First convert to int32_t to ensure the expected behavior.
279 ptrGapParam.nU32 = int32_t(gap);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800280 (void)mNode->setParameter(
281 (OMX_INDEXTYPE)OMX_IndexParamMaxFrameDurationForBitrateControl,
282 &ptrGapParam, sizeof(ptrGapParam));
283 }
284 }
285
286 // max fps
287 // TRICKY: we do not unset max fps to 0 unless using fixed fps
Wonsik Kim95ba0162019-03-19 15:51:54 -0700288 if ((config.mMaxFps > 0 || (config.mFixedAdjustedFps > 0 && config.mMaxFps == -1))
Pawin Vongmasa36653902018-11-15 00:10:25 -0800289 && config.mMaxFps != mConfig.mMaxFps) {
290 status_t res = GetStatus(mSource->setMaxFps(config.mMaxFps));
291 status << " maxFps=" << config.mMaxFps;
292 if (res != OK) {
293 status << " (=> " << asString(res) << ")";
294 err = res;
295 }
296 mConfig.mMaxFps = config.mMaxFps;
297 }
298
299 if (config.mTimeOffsetUs != mConfig.mTimeOffsetUs) {
300 status_t res = GetStatus(mSource->setTimeOffsetUs(config.mTimeOffsetUs));
301 status << " timeOffset " << config.mTimeOffsetUs << "us";
302 if (res != OK) {
303 status << " (=> " << asString(res) << ")";
304 err = res;
305 }
306 mConfig.mTimeOffsetUs = config.mTimeOffsetUs;
307 }
308
309 if (config.mCaptureFps != mConfig.mCaptureFps || config.mCodedFps != mConfig.mCodedFps) {
310 status_t res =
311 GetStatus(mSource->setTimeLapseConfig(config.mCodedFps, config.mCaptureFps));
312 status << " timeLapse " << config.mCaptureFps << "fps as " << config.mCodedFps << "fps";
313 if (res != OK) {
314 status << " (=> " << asString(res) << ")";
315 err = res;
316 }
317 mConfig.mCaptureFps = config.mCaptureFps;
318 mConfig.mCodedFps = config.mCodedFps;
319 }
320
321 if (config.mStartAtUs != mConfig.mStartAtUs
322 || (config.mStopped != mConfig.mStopped && !config.mStopped)) {
323 status_t res = GetStatus(mSource->setStartTimeUs(config.mStartAtUs));
324 status << " start at " << config.mStartAtUs << "us";
325 if (res != OK) {
326 status << " (=> " << asString(res) << ")";
327 err = res;
328 }
329 mConfig.mStartAtUs = config.mStartAtUs;
330 mConfig.mStopped = config.mStopped;
331 }
332
333 // suspend-resume
334 if (config.mSuspended != mConfig.mSuspended) {
335 status_t res = GetStatus(mSource->setSuspend(config.mSuspended, config.mSuspendAtUs));
336 status << " " << (config.mSuspended ? "suspend" : "resume")
337 << " at " << config.mSuspendAtUs << "us";
338 if (res != OK) {
339 status << " (=> " << asString(res) << ")";
340 err = res;
341 }
342 mConfig.mSuspended = config.mSuspended;
343 mConfig.mSuspendAtUs = config.mSuspendAtUs;
344 }
345
346 if (config.mStopped != mConfig.mStopped && config.mStopped) {
347 status_t res = GetStatus(mSource->setStopTimeUs(config.mStopAtUs));
348 status << " stop at " << config.mStopAtUs << "us";
349 if (res != OK) {
350 status << " (=> " << asString(res) << ")";
351 err = res;
352 } else {
353 status << " delayUs";
354 res = GetStatus(mSource->getStopTimeOffsetUs(&config.mInputDelayUs));
355 if (res != OK) {
356 status << " (=> " << asString(res) << ")";
357 } else {
358 status << "=" << config.mInputDelayUs << "us";
359 }
360 mConfig.mInputDelayUs = config.mInputDelayUs;
361 }
362 mConfig.mStopAtUs = config.mStopAtUs;
363 mConfig.mStopped = config.mStopped;
364 }
365
366 // color aspects (android._color-aspects)
367
368 // consumer usage
369 ALOGD("ISConfig%s", status.str().c_str());
370 return err;
371 }
372
373private:
374 sp<BGraphicBufferSource> mSource;
375 sp<C2OMXNode> mNode;
376 uint32_t mWidth;
377 uint32_t mHeight;
378 Config mConfig;
379};
380
381class Codec2ClientInterfaceWrapper : public C2ComponentStore {
382 std::shared_ptr<Codec2Client> mClient;
383
384public:
385 Codec2ClientInterfaceWrapper(std::shared_ptr<Codec2Client> client)
386 : mClient(client) { }
387
388 virtual ~Codec2ClientInterfaceWrapper() = default;
389
390 virtual c2_status_t config_sm(
391 const std::vector<C2Param *> &params,
392 std::vector<std::unique_ptr<C2SettingResult>> *const failures) {
393 return mClient->config(params, C2_MAY_BLOCK, failures);
394 };
395
396 virtual c2_status_t copyBuffer(
397 std::shared_ptr<C2GraphicBuffer>,
398 std::shared_ptr<C2GraphicBuffer>) {
399 return C2_OMITTED;
400 }
401
402 virtual c2_status_t createComponent(
403 C2String, std::shared_ptr<C2Component> *const component) {
404 component->reset();
405 return C2_OMITTED;
406 }
407
408 virtual c2_status_t createInterface(
409 C2String, std::shared_ptr<C2ComponentInterface> *const interface) {
410 interface->reset();
411 return C2_OMITTED;
412 }
413
414 virtual c2_status_t query_sm(
415 const std::vector<C2Param *> &stackParams,
416 const std::vector<C2Param::Index> &heapParamIndices,
417 std::vector<std::unique_ptr<C2Param>> *const heapParams) const {
418 return mClient->query(stackParams, heapParamIndices, C2_MAY_BLOCK, heapParams);
419 }
420
421 virtual c2_status_t querySupportedParams_nb(
422 std::vector<std::shared_ptr<C2ParamDescriptor>> *const params) const {
423 return mClient->querySupportedParams(params);
424 }
425
426 virtual c2_status_t querySupportedValues_sm(
427 std::vector<C2FieldSupportedValuesQuery> &fields) const {
428 return mClient->querySupportedValues(fields, C2_MAY_BLOCK);
429 }
430
431 virtual C2String getName() const {
432 return mClient->getName();
433 }
434
435 virtual std::shared_ptr<C2ParamReflector> getParamReflector() const {
436 return mClient->getParamReflector();
437 }
438
439 virtual std::vector<std::shared_ptr<const C2Component::Traits>> listComponents() {
440 return std::vector<std::shared_ptr<const C2Component::Traits>>();
441 }
442};
443
444} // namespace
445
446// CCodec::ClientListener
447
448struct CCodec::ClientListener : public Codec2Client::Listener {
449
450 explicit ClientListener(const wp<CCodec> &codec) : mCodec(codec) {}
451
452 virtual void onWorkDone(
453 const std::weak_ptr<Codec2Client::Component>& component,
Wonsik Kimab34ed62019-01-31 15:28:46 -0800454 std::list<std::unique_ptr<C2Work>>& workItems) override {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800455 (void)component;
456 sp<CCodec> codec(mCodec.promote());
457 if (!codec) {
458 return;
459 }
Wonsik Kimab34ed62019-01-31 15:28:46 -0800460 codec->onWorkDone(workItems);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800461 }
462
463 virtual void onTripped(
464 const std::weak_ptr<Codec2Client::Component>& component,
465 const std::vector<std::shared_ptr<C2SettingResult>>& settingResult
466 ) override {
467 // TODO
468 (void)component;
469 (void)settingResult;
470 }
471
472 virtual void onError(
473 const std::weak_ptr<Codec2Client::Component>& component,
474 uint32_t errorCode) override {
475 // TODO
476 (void)component;
477 (void)errorCode;
478 }
479
480 virtual void onDeath(
481 const std::weak_ptr<Codec2Client::Component>& component) override {
482 { // Log the death of the component.
483 std::shared_ptr<Codec2Client::Component> comp = component.lock();
484 if (!comp) {
485 ALOGE("Codec2 component died.");
486 } else {
487 ALOGE("Codec2 component \"%s\" died.", comp->getName().c_str());
488 }
489 }
490
491 // Report to MediaCodec.
492 sp<CCodec> codec(mCodec.promote());
493 if (!codec || !codec->mCallback) {
494 return;
495 }
496 codec->mCallback->onError(DEAD_OBJECT, ACTION_CODE_FATAL);
497 }
498
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800499 virtual void onFrameRendered(uint64_t bufferQueueId,
500 int32_t slotId,
501 int64_t timestampNs) override {
502 // TODO: implement
503 (void)bufferQueueId;
504 (void)slotId;
505 (void)timestampNs;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800506 }
507
508 virtual void onInputBufferDone(
Wonsik Kimab34ed62019-01-31 15:28:46 -0800509 uint64_t frameIndex, size_t arrayIndex) override {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800510 sp<CCodec> codec(mCodec.promote());
511 if (codec) {
Wonsik Kimab34ed62019-01-31 15:28:46 -0800512 codec->onInputBufferDone(frameIndex, arrayIndex);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800513 }
514 }
515
516private:
517 wp<CCodec> mCodec;
518};
519
520// CCodecCallbackImpl
521
522class CCodecCallbackImpl : public CCodecCallback {
523public:
524 explicit CCodecCallbackImpl(CCodec *codec) : mCodec(codec) {}
525 ~CCodecCallbackImpl() override = default;
526
527 void onError(status_t err, enum ActionCode actionCode) override {
528 mCodec->mCallback->onError(err, actionCode);
529 }
530
531 void onOutputFramesRendered(int64_t mediaTimeUs, nsecs_t renderTimeNs) override {
532 mCodec->mCallback->onOutputFramesRendered(
533 {RenderedFrameInfo(mediaTimeUs, renderTimeNs)});
534 }
535
Pawin Vongmasa36653902018-11-15 00:10:25 -0800536 void onOutputBuffersChanged() override {
537 mCodec->mCallback->onOutputBuffersChanged();
538 }
539
540private:
541 CCodec *mCodec;
542};
543
544// CCodec
545
546CCodec::CCodec()
Wonsik Kimab34ed62019-01-31 15:28:46 -0800547 : mChannel(new CCodecBufferChannel(std::make_shared<CCodecCallbackImpl>(this))) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800548}
549
550CCodec::~CCodec() {
551}
552
553std::shared_ptr<BufferChannelBase> CCodec::getBufferChannel() {
554 return mChannel;
555}
556
557status_t CCodec::tryAndReportOnError(std::function<status_t()> job) {
558 status_t err = job();
559 if (err != C2_OK) {
560 mCallback->onError(err, ACTION_CODE_FATAL);
561 }
562 return err;
563}
564
565void CCodec::initiateAllocateComponent(const sp<AMessage> &msg) {
566 auto setAllocating = [this] {
567 Mutexed<State>::Locked state(mState);
568 if (state->get() != RELEASED) {
569 return INVALID_OPERATION;
570 }
571 state->set(ALLOCATING);
572 return OK;
573 };
574 if (tryAndReportOnError(setAllocating) != OK) {
575 return;
576 }
577
578 sp<RefBase> codecInfo;
579 CHECK(msg->findObject("codecInfo", &codecInfo));
580 // For Codec 2.0 components, componentName == codecInfo->getCodecName().
581
582 sp<AMessage> allocMsg(new AMessage(kWhatAllocate, this));
583 allocMsg->setObject("codecInfo", codecInfo);
584 allocMsg->post();
585}
586
587void CCodec::allocate(const sp<MediaCodecInfo> &codecInfo) {
588 if (codecInfo == nullptr) {
589 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
590 return;
591 }
592 ALOGD("allocate(%s)", codecInfo->getCodecName());
593 mClientListener.reset(new ClientListener(this));
594
595 AString componentName = codecInfo->getCodecName();
596 std::shared_ptr<Codec2Client> client;
597
598 // set up preferred component store to access vendor store parameters
599 client = Codec2Client::CreateFromService("default", false);
600 if (client) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800601 ALOGI("setting up '%s' as default (vendor) store", client->getServiceName().c_str());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800602 SetPreferredCodec2ComponentStore(
603 std::make_shared<Codec2ClientInterfaceWrapper>(client));
604 }
605
606 std::shared_ptr<Codec2Client::Component> comp =
607 Codec2Client::CreateComponentByName(
608 componentName.c_str(),
609 mClientListener,
610 &client);
611 if (!comp) {
612 ALOGE("Failed Create component: %s", componentName.c_str());
613 Mutexed<State>::Locked state(mState);
614 state->set(RELEASED);
615 state.unlock();
616 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
617 state.lock();
618 return;
619 }
620 ALOGI("Created component [%s]", componentName.c_str());
621 mChannel->setComponent(comp);
622 auto setAllocated = [this, comp, client] {
623 Mutexed<State>::Locked state(mState);
624 if (state->get() != ALLOCATING) {
625 state->set(RELEASED);
626 return UNKNOWN_ERROR;
627 }
628 state->set(ALLOCATED);
629 state->comp = comp;
630 mClient = client;
631 return OK;
632 };
633 if (tryAndReportOnError(setAllocated) != OK) {
634 return;
635 }
636
637 // initialize config here in case setParameters is called prior to configure
638 Mutexed<Config>::Locked config(mConfig);
639 status_t err = config->initialize(mClient, comp);
640 if (err != OK) {
641 ALOGW("Failed to initialize configuration support");
642 // TODO: report error once we complete implementation.
643 }
644 config->queryConfiguration(comp);
645
646 mCallback->onComponentAllocated(componentName.c_str());
647}
648
649void CCodec::initiateConfigureComponent(const sp<AMessage> &format) {
650 auto checkAllocated = [this] {
651 Mutexed<State>::Locked state(mState);
652 return (state->get() != ALLOCATED) ? UNKNOWN_ERROR : OK;
653 };
654 if (tryAndReportOnError(checkAllocated) != OK) {
655 return;
656 }
657
658 sp<AMessage> msg(new AMessage(kWhatConfigure, this));
659 msg->setMessage("format", format);
660 msg->post();
661}
662
663void CCodec::configure(const sp<AMessage> &msg) {
664 std::shared_ptr<Codec2Client::Component> comp;
665 auto checkAllocated = [this, &comp] {
666 Mutexed<State>::Locked state(mState);
667 if (state->get() != ALLOCATED) {
668 state->set(RELEASED);
669 return UNKNOWN_ERROR;
670 }
671 comp = state->comp;
672 return OK;
673 };
674 if (tryAndReportOnError(checkAllocated) != OK) {
675 return;
676 }
677
678 auto doConfig = [msg, comp, this]() -> status_t {
679 AString mime;
680 if (!msg->findString("mime", &mime)) {
681 return BAD_VALUE;
682 }
683
684 int32_t encoder;
685 if (!msg->findInt32("encoder", &encoder)) {
686 encoder = false;
687 }
688
689 // TODO: read from intf()
690 if ((!encoder) != (comp->getName().find("encoder") == std::string::npos)) {
691 return UNKNOWN_ERROR;
692 }
693
694 int32_t storeMeta;
695 if (encoder
696 && msg->findInt32("android._input-metadata-buffer-type", &storeMeta)
697 && storeMeta != kMetadataBufferTypeInvalid) {
698 if (storeMeta != kMetadataBufferTypeANWBuffer) {
699 ALOGD("Only ANW buffers are supported for legacy metadata mode");
700 return BAD_VALUE;
701 }
702 mChannel->setMetaMode(CCodecBufferChannel::MODE_ANW);
703 }
704
705 sp<RefBase> obj;
706 sp<Surface> surface;
707 if (msg->findObject("native-window", &obj)) {
708 surface = static_cast<Surface *>(obj.get());
709 setSurface(surface);
710 }
711
712 Mutexed<Config>::Locked config(mConfig);
713 config->mUsingSurface = surface != nullptr;
714
Wonsik Kim1114eea2019-02-25 14:35:24 -0800715 // Enforce required parameters
716 int32_t i32;
717 float flt;
718 if (config->mDomain & Config::IS_AUDIO) {
719 if (!msg->findInt32(KEY_SAMPLE_RATE, &i32)) {
720 ALOGD("sample rate is missing, which is required for audio components.");
721 return BAD_VALUE;
722 }
723 if (!msg->findInt32(KEY_CHANNEL_COUNT, &i32)) {
724 ALOGD("channel count is missing, which is required for audio components.");
725 return BAD_VALUE;
726 }
727 if ((config->mDomain & Config::IS_ENCODER)
728 && !mime.equalsIgnoreCase(MEDIA_MIMETYPE_AUDIO_FLAC)
729 && !msg->findInt32(KEY_BIT_RATE, &i32)
730 && !msg->findFloat(KEY_BIT_RATE, &flt)) {
731 ALOGD("bitrate is missing, which is required for audio encoders.");
732 return BAD_VALUE;
733 }
734 }
735 if (config->mDomain & (Config::IS_IMAGE | Config::IS_VIDEO)) {
736 if (!msg->findInt32(KEY_WIDTH, &i32)) {
737 ALOGD("width is missing, which is required for image/video components.");
738 return BAD_VALUE;
739 }
740 if (!msg->findInt32(KEY_HEIGHT, &i32)) {
741 ALOGD("height is missing, which is required for image/video components.");
742 return BAD_VALUE;
743 }
744 if ((config->mDomain & Config::IS_ENCODER) && (config->mDomain & Config::IS_VIDEO)) {
Harish Mahendrakar817d3182019-03-11 16:37:47 -0700745 C2Config::bitrate_mode_t mode = C2Config::BITRATE_VARIABLE;
746 if (msg->findInt32(KEY_BITRATE_MODE, &i32)) {
747 mode = (C2Config::bitrate_mode_t) i32;
748 }
749 if (mode == BITRATE_MODE_CQ) {
750 if (!msg->findInt32(KEY_QUALITY, &i32)) {
751 ALOGD("quality is missing, which is required for video encoders in CQ.");
752 return BAD_VALUE;
753 }
754 } else {
755 if (!msg->findInt32(KEY_BIT_RATE, &i32)
756 && !msg->findFloat(KEY_BIT_RATE, &flt)) {
757 ALOGD("bitrate is missing, which is required for video encoders.");
758 return BAD_VALUE;
759 }
Wonsik Kim1114eea2019-02-25 14:35:24 -0800760 }
761 if (!msg->findInt32(KEY_I_FRAME_INTERVAL, &i32)
762 && !msg->findFloat(KEY_I_FRAME_INTERVAL, &flt)) {
763 ALOGD("I frame interval is missing, which is required for video encoders.");
764 return BAD_VALUE;
765 }
766 }
767 }
768
Pawin Vongmasa36653902018-11-15 00:10:25 -0800769 /*
770 * Handle input surface configuration
771 */
772 if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))
773 && (config->mDomain & Config::IS_ENCODER)) {
774 config->mISConfig.reset(new InputSurfaceWrapper::Config{});
775 {
776 config->mISConfig->mMinFps = 0;
777 int64_t value;
Chong Zhang038e8f82019-02-06 19:05:14 -0800778 if (msg->findInt64(KEY_REPEAT_PREVIOUS_FRAME_AFTER, &value) && value > 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800779 config->mISConfig->mMinFps = 1e6 / value;
780 }
Wonsik Kim95ba0162019-03-19 15:51:54 -0700781 if (!msg->findFloat(
782 KEY_MAX_FPS_TO_ENCODER, &config->mISConfig->mMaxFps)) {
783 config->mISConfig->mMaxFps = -1;
784 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800785 config->mISConfig->mMinAdjustedFps = 0;
786 config->mISConfig->mFixedAdjustedFps = 0;
Chong Zhang038e8f82019-02-06 19:05:14 -0800787 if (msg->findInt64(KEY_MAX_PTS_GAP_TO_ENCODER, &value)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800788 if (value < 0 && value >= INT32_MIN) {
789 config->mISConfig->mFixedAdjustedFps = -1e6 / value;
Wonsik Kim95ba0162019-03-19 15:51:54 -0700790 config->mISConfig->mMaxFps = -1;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800791 } else if (value > 0 && value <= INT32_MAX) {
792 config->mISConfig->mMinAdjustedFps = 1e6 / value;
793 }
794 }
795 }
796
797 {
798 double value;
799 if (msg->findDouble("time-lapse-fps", &value)) {
800 config->mISConfig->mCaptureFps = value;
801 (void)msg->findAsFloat(KEY_FRAME_RATE, &config->mISConfig->mCodedFps);
802 }
803 }
804
805 {
806 config->mISConfig->mSuspended = false;
807 config->mISConfig->mSuspendAtUs = -1;
808 int32_t value;
Chong Zhang038e8f82019-02-06 19:05:14 -0800809 if (msg->findInt32(KEY_CREATE_INPUT_SURFACE_SUSPENDED, &value) && value) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800810 config->mISConfig->mSuspended = true;
811 }
812 }
813 }
814
815 /*
816 * Handle desired color format.
817 */
818 if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))) {
819 int32_t format = -1;
820 if (!msg->findInt32(KEY_COLOR_FORMAT, &format)) {
821 /*
822 * Also handle default color format (encoders require color format, so this is only
823 * needed for decoders.
824 */
825 if (!(config->mDomain & Config::IS_ENCODER)) {
826 format = (surface == nullptr) ? COLOR_FormatYUV420Planar : COLOR_FormatSurface;
827 }
828 }
829
830 if (format >= 0) {
831 msg->setInt32("android._color-format", format);
832 }
833 }
834
835 std::vector<std::unique_ptr<C2Param>> configUpdate;
Wonsik Kimaa484ac2019-02-13 16:54:02 -0800836 // NOTE: We used to ignore "video-bitrate" at configure; replicate
837 // the behavior here.
838 sp<AMessage> sdkParams = msg;
839 int32_t videoBitrate;
840 if (sdkParams->findInt32(PARAMETER_KEY_VIDEO_BITRATE, &videoBitrate)) {
841 sdkParams = msg->dup();
842 sdkParams->removeEntryAt(sdkParams->findEntryByName(PARAMETER_KEY_VIDEO_BITRATE));
843 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800844 status_t err = config->getConfigUpdateFromSdkParams(
Wonsik Kimaa484ac2019-02-13 16:54:02 -0800845 comp, sdkParams, Config::IS_CONFIG, C2_DONT_BLOCK, &configUpdate);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800846 if (err != OK) {
847 ALOGW("failed to convert configuration to c2 params");
848 }
849 err = config->setParameters(comp, configUpdate, C2_DONT_BLOCK);
850 if (err != OK) {
851 ALOGW("failed to configure c2 params");
852 return err;
853 }
854
855 std::vector<std::unique_ptr<C2Param>> params;
856 C2StreamUsageTuning::input usage(0u, 0u);
857 C2StreamMaxBufferSizeInfo::input maxInputSize(0u, 0u);
858
859 std::initializer_list<C2Param::Index> indices {
860 };
861 c2_status_t c2err = comp->query(
862 { &usage, &maxInputSize },
863 indices,
864 C2_DONT_BLOCK,
865 &params);
866 if (c2err != C2_OK && c2err != C2_BAD_INDEX) {
867 ALOGE("Failed to query component interface: %d", c2err);
868 return UNKNOWN_ERROR;
869 }
870 if (params.size() != indices.size()) {
871 ALOGE("Component returns wrong number of params: expected %zu actual %zu",
872 indices.size(), params.size());
873 return UNKNOWN_ERROR;
874 }
875 if (usage && (usage.value & C2MemoryUsage::CPU_READ)) {
876 config->mInputFormat->setInt32("using-sw-read-often", true);
877 }
878
879 // NOTE: we don't blindly use client specified input size if specified as clients
880 // at times specify too small size. Instead, mimic the behavior from OMX, where the
881 // client specified size is only used to ask for bigger buffers than component suggested
882 // size.
883 int32_t clientInputSize = 0;
884 bool clientSpecifiedInputSize =
885 msg->findInt32(KEY_MAX_INPUT_SIZE, &clientInputSize) && clientInputSize > 0;
886 // TEMP: enforce minimum buffer size of 1MB for video decoders
887 // and 16K / 4K for audio encoders/decoders
888 if (maxInputSize.value == 0) {
889 if (config->mDomain & Config::IS_AUDIO) {
890 maxInputSize.value = encoder ? 16384 : 4096;
891 } else if (!encoder) {
892 maxInputSize.value = 1048576u;
893 }
894 }
895
896 // verify that CSD fits into this size (if defined)
897 if ((config->mDomain & Config::IS_DECODER) && maxInputSize.value > 0) {
898 sp<ABuffer> csd;
899 for (size_t ix = 0; msg->findBuffer(StringPrintf("csd-%zu", ix).c_str(), &csd); ++ix) {
900 if (csd && csd->size() > maxInputSize.value) {
901 maxInputSize.value = csd->size();
902 }
903 }
904 }
905
906 // TODO: do this based on component requiring linear allocator for input
907 if ((config->mDomain & Config::IS_DECODER) || (config->mDomain & Config::IS_AUDIO)) {
908 if (clientSpecifiedInputSize) {
909 // Warn that we're overriding client's max input size if necessary.
910 if ((uint32_t)clientInputSize < maxInputSize.value) {
911 ALOGD("client requested max input size %d, which is smaller than "
912 "what component recommended (%u); overriding with component "
913 "recommendation.", clientInputSize, maxInputSize.value);
914 ALOGW("This behavior is subject to change. It is recommended that "
915 "app developers double check whether the requested "
916 "max input size is in reasonable range.");
917 } else {
918 maxInputSize.value = clientInputSize;
919 }
920 }
921 // Pass max input size on input format to the buffer channel (if supplied by the
922 // component or by a default)
923 if (maxInputSize.value) {
924 config->mInputFormat->setInt32(
925 KEY_MAX_INPUT_SIZE,
926 (int32_t)(c2_min(maxInputSize.value, uint32_t(INT32_MAX))));
927 }
928 }
929
930 if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))) {
931 // propagate HDR static info to output format for both encoders and decoders
932 // if component supports this info, we will update from component, but only the raw port,
933 // so don't propagate if component already filled it in.
934 sp<ABuffer> hdrInfo;
935 if (msg->findBuffer(KEY_HDR_STATIC_INFO, &hdrInfo)
936 && !config->mOutputFormat->findBuffer(KEY_HDR_STATIC_INFO, &hdrInfo)) {
937 config->mOutputFormat->setBuffer(KEY_HDR_STATIC_INFO, hdrInfo);
938 }
939
940 // Set desired color format from configuration parameter
941 int32_t format;
942 if (msg->findInt32("android._color-format", &format)) {
943 if (config->mDomain & Config::IS_ENCODER) {
944 config->mInputFormat->setInt32(KEY_COLOR_FORMAT, format);
945 } else {
946 config->mOutputFormat->setInt32(KEY_COLOR_FORMAT, format);
947 }
948 }
949 }
950
951 // propagate encoder delay and padding to output format
952 if ((config->mDomain & Config::IS_DECODER) && (config->mDomain & Config::IS_AUDIO)) {
953 int delay = 0;
954 if (msg->findInt32("encoder-delay", &delay)) {
955 config->mOutputFormat->setInt32("encoder-delay", delay);
956 }
957 int padding = 0;
958 if (msg->findInt32("encoder-padding", &padding)) {
959 config->mOutputFormat->setInt32("encoder-padding", padding);
960 }
961 }
962
963 // set channel-mask
964 if (config->mDomain & Config::IS_AUDIO) {
965 int32_t mask;
966 if (msg->findInt32(KEY_CHANNEL_MASK, &mask)) {
967 if (config->mDomain & Config::IS_ENCODER) {
968 config->mInputFormat->setInt32(KEY_CHANNEL_MASK, mask);
969 } else {
970 config->mOutputFormat->setInt32(KEY_CHANNEL_MASK, mask);
971 }
972 }
973 }
974
975 ALOGD("setup formats input: %s and output: %s",
976 config->mInputFormat->debugString().c_str(),
977 config->mOutputFormat->debugString().c_str());
978 return OK;
979 };
980 if (tryAndReportOnError(doConfig) != OK) {
981 return;
982 }
983
984 Mutexed<Config>::Locked config(mConfig);
985
986 mCallback->onComponentConfigured(config->mInputFormat, config->mOutputFormat);
987}
988
989void CCodec::initiateCreateInputSurface() {
990 status_t err = [this] {
991 Mutexed<State>::Locked state(mState);
992 if (state->get() != ALLOCATED) {
993 return UNKNOWN_ERROR;
994 }
995 // TODO: read it from intf() properly.
996 if (state->comp->getName().find("encoder") == std::string::npos) {
997 return INVALID_OPERATION;
998 }
999 return OK;
1000 }();
1001 if (err != OK) {
1002 mCallback->onInputSurfaceCreationFailed(err);
1003 return;
1004 }
1005
1006 (new AMessage(kWhatCreateInputSurface, this))->post();
1007}
1008
Lajos Molnar47118272019-01-31 16:28:04 -08001009sp<PersistentSurface> CCodec::CreateOmxInputSurface() {
1010 using namespace android::hardware::media::omx::V1_0;
1011 using namespace android::hardware::media::omx::V1_0::utils;
1012 using namespace android::hardware::graphics::bufferqueue::V1_0::utils;
1013 typedef android::hardware::media::omx::V1_0::Status OmxStatus;
1014 android::sp<IOmx> omx = IOmx::getService();
1015 typedef android::hardware::graphics::bufferqueue::V1_0::
1016 IGraphicBufferProducer HGraphicBufferProducer;
1017 typedef android::hardware::media::omx::V1_0::
1018 IGraphicBufferSource HGraphicBufferSource;
1019 OmxStatus s;
1020 android::sp<HGraphicBufferProducer> gbp;
1021 android::sp<HGraphicBufferSource> gbs;
1022 android::Return<void> transStatus = omx->createInputSurface(
1023 [&s, &gbp, &gbs](
1024 OmxStatus status,
1025 const android::sp<HGraphicBufferProducer>& producer,
1026 const android::sp<HGraphicBufferSource>& source) {
1027 s = status;
1028 gbp = producer;
1029 gbs = source;
1030 });
1031 if (transStatus.isOk() && s == OmxStatus::OK) {
1032 return new PersistentSurface(
1033 new H2BGraphicBufferProducer(gbp),
1034 sp<::android::IGraphicBufferSource>(new LWGraphicBufferSource(gbs)));
1035 }
1036
1037 return nullptr;
1038}
1039
1040sp<PersistentSurface> CCodec::CreateCompatibleInputSurface() {
1041 sp<PersistentSurface> surface(CreateInputSurface());
1042
1043 if (surface == nullptr) {
1044 surface = CreateOmxInputSurface();
1045 }
1046
1047 return surface;
1048}
1049
Pawin Vongmasa36653902018-11-15 00:10:25 -08001050void CCodec::createInputSurface() {
1051 status_t err;
1052 sp<IGraphicBufferProducer> bufferProducer;
1053
1054 sp<AMessage> inputFormat;
1055 sp<AMessage> outputFormat;
1056 {
1057 Mutexed<Config>::Locked config(mConfig);
1058 inputFormat = config->mInputFormat;
1059 outputFormat = config->mOutputFormat;
1060 }
1061
Lajos Molnar47118272019-01-31 16:28:04 -08001062 sp<PersistentSurface> persistentSurface = CreateCompatibleInputSurface();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001063
1064 if (persistentSurface->getHidlTarget()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001065 sp<IInputSurface> hidlInputSurface = IInputSurface::castFrom(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001066 persistentSurface->getHidlTarget());
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001067 if (!hidlInputSurface) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001068 ALOGE("Corrupted input surface");
1069 mCallback->onInputSurfaceCreationFailed(UNKNOWN_ERROR);
1070 return;
1071 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001072 std::shared_ptr<Codec2Client::InputSurface> inputSurface =
1073 std::make_shared<Codec2Client::InputSurface>(hidlInputSurface);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001074 err = setupInputSurface(std::make_shared<C2InputSurfaceWrapper>(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001075 inputSurface));
1076 bufferProducer = inputSurface->getGraphicBufferProducer();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001077 } else {
1078 int32_t width = 0;
1079 (void)outputFormat->findInt32("width", &width);
1080 int32_t height = 0;
1081 (void)outputFormat->findInt32("height", &height);
1082 err = setupInputSurface(std::make_shared<GraphicBufferSourceWrapper>(
1083 persistentSurface->getBufferSource(), width, height));
1084 bufferProducer = persistentSurface->getBufferProducer();
1085 }
1086
1087 if (err != OK) {
1088 ALOGE("Failed to set up input surface: %d", err);
1089 mCallback->onInputSurfaceCreationFailed(err);
1090 return;
1091 }
1092
1093 mCallback->onInputSurfaceCreated(
1094 inputFormat,
1095 outputFormat,
1096 new BufferProducerWrapper(bufferProducer));
1097}
1098
1099status_t CCodec::setupInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface) {
1100 Mutexed<Config>::Locked config(mConfig);
1101 config->mUsingSurface = true;
1102
1103 // we are now using surface - apply default color aspects to input format - as well as
1104 // get dataspace
1105 bool inputFormatChanged = config->updateFormats(config->IS_INPUT);
1106 ALOGD("input format %s to %s",
1107 inputFormatChanged ? "changed" : "unchanged",
1108 config->mInputFormat->debugString().c_str());
1109
1110 // configure dataspace
1111 static_assert(sizeof(int32_t) == sizeof(android_dataspace), "dataspace size mismatch");
1112 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
1113 (void)config->mInputFormat->findInt32("android._dataspace", (int32_t*)&dataSpace);
1114 surface->setDataSpace(dataSpace);
1115
1116 status_t err = mChannel->setInputSurface(surface);
1117 if (err != OK) {
1118 // undo input format update
1119 config->mUsingSurface = false;
1120 (void)config->updateFormats(config->IS_INPUT);
1121 return err;
1122 }
1123 config->mInputSurface = surface;
1124
1125 if (config->mISConfig) {
1126 surface->configure(*config->mISConfig);
1127 } else {
1128 ALOGD("ISConfig: no configuration");
1129 }
1130
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001131 return OK;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001132}
1133
1134void CCodec::initiateSetInputSurface(const sp<PersistentSurface> &surface) {
1135 sp<AMessage> msg = new AMessage(kWhatSetInputSurface, this);
1136 msg->setObject("surface", surface);
1137 msg->post();
1138}
1139
1140void CCodec::setInputSurface(const sp<PersistentSurface> &surface) {
1141 sp<AMessage> inputFormat;
1142 sp<AMessage> outputFormat;
1143 {
1144 Mutexed<Config>::Locked config(mConfig);
1145 inputFormat = config->mInputFormat;
1146 outputFormat = config->mOutputFormat;
1147 }
1148 auto hidlTarget = surface->getHidlTarget();
1149 if (hidlTarget) {
1150 sp<IInputSurface> inputSurface =
1151 IInputSurface::castFrom(hidlTarget);
1152 if (!inputSurface) {
1153 ALOGE("Failed to set input surface: Corrupted surface.");
1154 mCallback->onInputSurfaceDeclined(UNKNOWN_ERROR);
1155 return;
1156 }
1157 status_t err = setupInputSurface(std::make_shared<C2InputSurfaceWrapper>(
1158 std::make_shared<Codec2Client::InputSurface>(inputSurface)));
1159 if (err != OK) {
1160 ALOGE("Failed to set up input surface: %d", err);
1161 mCallback->onInputSurfaceDeclined(err);
1162 return;
1163 }
1164 } else {
1165 int32_t width = 0;
1166 (void)outputFormat->findInt32("width", &width);
1167 int32_t height = 0;
1168 (void)outputFormat->findInt32("height", &height);
1169 status_t err = setupInputSurface(std::make_shared<GraphicBufferSourceWrapper>(
1170 surface->getBufferSource(), width, height));
1171 if (err != OK) {
1172 ALOGE("Failed to set up input surface: %d", err);
1173 mCallback->onInputSurfaceDeclined(err);
1174 return;
1175 }
1176 }
1177 mCallback->onInputSurfaceAccepted(inputFormat, outputFormat);
1178}
1179
1180void CCodec::initiateStart() {
1181 auto setStarting = [this] {
1182 Mutexed<State>::Locked state(mState);
1183 if (state->get() != ALLOCATED) {
1184 return UNKNOWN_ERROR;
1185 }
1186 state->set(STARTING);
1187 return OK;
1188 };
1189 if (tryAndReportOnError(setStarting) != OK) {
1190 return;
1191 }
1192
1193 (new AMessage(kWhatStart, this))->post();
1194}
1195
1196void CCodec::start() {
1197 std::shared_ptr<Codec2Client::Component> comp;
1198 auto checkStarting = [this, &comp] {
1199 Mutexed<State>::Locked state(mState);
1200 if (state->get() != STARTING) {
1201 return UNKNOWN_ERROR;
1202 }
1203 comp = state->comp;
1204 return OK;
1205 };
1206 if (tryAndReportOnError(checkStarting) != OK) {
1207 return;
1208 }
1209
1210 c2_status_t err = comp->start();
1211 if (err != C2_OK) {
1212 mCallback->onError(toStatusT(err, C2_OPERATION_Component_start),
1213 ACTION_CODE_FATAL);
1214 return;
1215 }
1216 sp<AMessage> inputFormat;
1217 sp<AMessage> outputFormat;
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001218 status_t err2 = OK;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001219 {
1220 Mutexed<Config>::Locked config(mConfig);
1221 inputFormat = config->mInputFormat;
1222 outputFormat = config->mOutputFormat;
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001223 if (config->mInputSurface) {
1224 err2 = config->mInputSurface->start();
1225 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001226 }
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001227 if (err2 != OK) {
1228 mCallback->onError(err2, ACTION_CODE_FATAL);
1229 return;
1230 }
1231 err2 = mChannel->start(inputFormat, outputFormat);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001232 if (err2 != OK) {
1233 mCallback->onError(err2, ACTION_CODE_FATAL);
1234 return;
1235 }
1236
1237 auto setRunning = [this] {
1238 Mutexed<State>::Locked state(mState);
1239 if (state->get() != STARTING) {
1240 return UNKNOWN_ERROR;
1241 }
1242 state->set(RUNNING);
1243 return OK;
1244 };
1245 if (tryAndReportOnError(setRunning) != OK) {
1246 return;
1247 }
1248 mCallback->onStartCompleted();
1249
1250 (void)mChannel->requestInitialInputBuffers();
1251}
1252
1253void CCodec::initiateShutdown(bool keepComponentAllocated) {
1254 if (keepComponentAllocated) {
1255 initiateStop();
1256 } else {
1257 initiateRelease();
1258 }
1259}
1260
1261void CCodec::initiateStop() {
1262 {
1263 Mutexed<State>::Locked state(mState);
1264 if (state->get() == ALLOCATED
1265 || state->get() == RELEASED
1266 || state->get() == STOPPING
1267 || state->get() == RELEASING) {
1268 // We're already stopped, released, or doing it right now.
1269 state.unlock();
1270 mCallback->onStopCompleted();
1271 state.lock();
1272 return;
1273 }
1274 state->set(STOPPING);
1275 }
1276
1277 mChannel->stop();
1278 (new AMessage(kWhatStop, this))->post();
1279}
1280
1281void CCodec::stop() {
1282 std::shared_ptr<Codec2Client::Component> comp;
1283 {
1284 Mutexed<State>::Locked state(mState);
1285 if (state->get() == RELEASING) {
1286 state.unlock();
1287 // We're already stopped or release is in progress.
1288 mCallback->onStopCompleted();
1289 state.lock();
1290 return;
1291 } else if (state->get() != STOPPING) {
1292 state.unlock();
1293 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1294 state.lock();
1295 return;
1296 }
1297 comp = state->comp;
1298 }
1299 status_t err = comp->stop();
1300 if (err != C2_OK) {
1301 // TODO: convert err into status_t
1302 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1303 }
1304
1305 {
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001306 Mutexed<Config>::Locked config(mConfig);
1307 if (config->mInputSurface) {
1308 config->mInputSurface->disconnect();
1309 config->mInputSurface = nullptr;
1310 }
1311 }
1312 {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001313 Mutexed<State>::Locked state(mState);
1314 if (state->get() == STOPPING) {
1315 state->set(ALLOCATED);
1316 }
1317 }
1318 mCallback->onStopCompleted();
1319}
1320
1321void CCodec::initiateRelease(bool sendCallback /* = true */) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001322 bool clearInputSurfaceIfNeeded = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001323 {
1324 Mutexed<State>::Locked state(mState);
1325 if (state->get() == RELEASED || state->get() == RELEASING) {
1326 // We're already released or doing it right now.
1327 if (sendCallback) {
1328 state.unlock();
1329 mCallback->onReleaseCompleted();
1330 state.lock();
1331 }
1332 return;
1333 }
1334 if (state->get() == ALLOCATING) {
1335 state->set(RELEASING);
1336 // With the altered state allocate() would fail and clean up.
1337 if (sendCallback) {
1338 state.unlock();
1339 mCallback->onReleaseCompleted();
1340 state.lock();
1341 }
1342 return;
1343 }
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001344 if (state->get() == STARTING
1345 || state->get() == RUNNING
1346 || state->get() == STOPPING) {
1347 // Input surface may have been started, so clean up is needed.
1348 clearInputSurfaceIfNeeded = true;
1349 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001350 state->set(RELEASING);
1351 }
1352
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001353 if (clearInputSurfaceIfNeeded) {
1354 Mutexed<Config>::Locked config(mConfig);
1355 if (config->mInputSurface) {
1356 config->mInputSurface->disconnect();
1357 config->mInputSurface = nullptr;
1358 }
1359 }
1360
Pawin Vongmasa36653902018-11-15 00:10:25 -08001361 mChannel->stop();
1362 // thiz holds strong ref to this while the thread is running.
1363 sp<CCodec> thiz(this);
1364 std::thread([thiz, sendCallback] { thiz->release(sendCallback); }).detach();
1365}
1366
1367void CCodec::release(bool sendCallback) {
1368 std::shared_ptr<Codec2Client::Component> comp;
1369 {
1370 Mutexed<State>::Locked state(mState);
1371 if (state->get() == RELEASED) {
1372 if (sendCallback) {
1373 state.unlock();
1374 mCallback->onReleaseCompleted();
1375 state.lock();
1376 }
1377 return;
1378 }
1379 comp = state->comp;
1380 }
1381 comp->release();
1382
1383 {
1384 Mutexed<State>::Locked state(mState);
1385 state->set(RELEASED);
1386 state->comp.reset();
1387 }
1388 if (sendCallback) {
1389 mCallback->onReleaseCompleted();
1390 }
1391}
1392
1393status_t CCodec::setSurface(const sp<Surface> &surface) {
1394 return mChannel->setSurface(surface);
1395}
1396
1397void CCodec::signalFlush() {
1398 status_t err = [this] {
1399 Mutexed<State>::Locked state(mState);
1400 if (state->get() == FLUSHED) {
1401 return ALREADY_EXISTS;
1402 }
1403 if (state->get() != RUNNING) {
1404 return UNKNOWN_ERROR;
1405 }
1406 state->set(FLUSHING);
1407 return OK;
1408 }();
1409 switch (err) {
1410 case ALREADY_EXISTS:
1411 mCallback->onFlushCompleted();
1412 return;
1413 case OK:
1414 break;
1415 default:
1416 mCallback->onError(err, ACTION_CODE_FATAL);
1417 return;
1418 }
1419
1420 mChannel->stop();
1421 (new AMessage(kWhatFlush, this))->post();
1422}
1423
1424void CCodec::flush() {
1425 std::shared_ptr<Codec2Client::Component> comp;
1426 auto checkFlushing = [this, &comp] {
1427 Mutexed<State>::Locked state(mState);
1428 if (state->get() != FLUSHING) {
1429 return UNKNOWN_ERROR;
1430 }
1431 comp = state->comp;
1432 return OK;
1433 };
1434 if (tryAndReportOnError(checkFlushing) != OK) {
1435 return;
1436 }
1437
1438 std::list<std::unique_ptr<C2Work>> flushedWork;
1439 c2_status_t err = comp->flush(C2Component::FLUSH_COMPONENT, &flushedWork);
1440 {
1441 Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue);
1442 flushedWork.splice(flushedWork.end(), *queue);
1443 }
1444 if (err != C2_OK) {
1445 // TODO: convert err into status_t
1446 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1447 }
1448
1449 mChannel->flush(flushedWork);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001450
1451 {
1452 Mutexed<State>::Locked state(mState);
1453 state->set(FLUSHED);
1454 }
1455 mCallback->onFlushCompleted();
1456}
1457
1458void CCodec::signalResume() {
1459 auto setResuming = [this] {
1460 Mutexed<State>::Locked state(mState);
1461 if (state->get() != FLUSHED) {
1462 return UNKNOWN_ERROR;
1463 }
1464 state->set(RESUMING);
1465 return OK;
1466 };
1467 if (tryAndReportOnError(setResuming) != OK) {
1468 return;
1469 }
1470
1471 (void)mChannel->start(nullptr, nullptr);
1472
1473 {
1474 Mutexed<State>::Locked state(mState);
1475 if (state->get() != RESUMING) {
1476 state.unlock();
1477 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1478 state.lock();
1479 return;
1480 }
1481 state->set(RUNNING);
1482 }
1483
1484 (void)mChannel->requestInitialInputBuffers();
1485}
1486
Wonsik Kimaa484ac2019-02-13 16:54:02 -08001487void CCodec::signalSetParameters(const sp<AMessage> &msg) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001488 std::shared_ptr<Codec2Client::Component> comp;
1489 auto checkState = [this, &comp] {
1490 Mutexed<State>::Locked state(mState);
1491 if (state->get() == RELEASED) {
1492 return INVALID_OPERATION;
1493 }
1494 comp = state->comp;
1495 return OK;
1496 };
1497 if (tryAndReportOnError(checkState) != OK) {
1498 return;
1499 }
1500
Wonsik Kimaa484ac2019-02-13 16:54:02 -08001501 // NOTE: We used to ignore "bitrate" at setParameters; replicate
1502 // the behavior here.
1503 sp<AMessage> params = msg;
1504 int32_t bitrate;
1505 if (params->findInt32(KEY_BIT_RATE, &bitrate)) {
1506 params = msg->dup();
1507 params->removeEntryAt(params->findEntryByName(KEY_BIT_RATE));
1508 }
1509
Pawin Vongmasa36653902018-11-15 00:10:25 -08001510 Mutexed<Config>::Locked config(mConfig);
1511
1512 /**
1513 * Handle input surface parameters
1514 */
1515 if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))
1516 && (config->mDomain & Config::IS_ENCODER) && config->mInputSurface && config->mISConfig) {
Chong Zhang038e8f82019-02-06 19:05:14 -08001517 (void)params->findInt64(PARAMETER_KEY_OFFSET_TIME, &config->mISConfig->mTimeOffsetUs);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001518
1519 if (params->findInt64("skip-frames-before", &config->mISConfig->mStartAtUs)) {
1520 config->mISConfig->mStopped = false;
1521 } else if (params->findInt64("stop-time-us", &config->mISConfig->mStopAtUs)) {
1522 config->mISConfig->mStopped = true;
1523 }
1524
1525 int32_t value;
Chong Zhang038e8f82019-02-06 19:05:14 -08001526 if (params->findInt32(PARAMETER_KEY_SUSPEND, &value)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001527 config->mISConfig->mSuspended = value;
1528 config->mISConfig->mSuspendAtUs = -1;
Chong Zhang038e8f82019-02-06 19:05:14 -08001529 (void)params->findInt64(PARAMETER_KEY_SUSPEND_TIME, &config->mISConfig->mSuspendAtUs);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001530 }
1531
1532 (void)config->mInputSurface->configure(*config->mISConfig);
1533 if (config->mISConfig->mStopped) {
1534 config->mInputFormat->setInt64(
1535 "android._stop-time-offset-us", config->mISConfig->mInputDelayUs);
1536 }
1537 }
1538
1539 std::vector<std::unique_ptr<C2Param>> configUpdate;
1540 (void)config->getConfigUpdateFromSdkParams(
1541 comp, params, Config::IS_PARAM, C2_MAY_BLOCK, &configUpdate);
1542 // Prefer to pass parameters to the buffer channel, so they can be synchronized with the frames.
1543 // Parameter synchronization is not defined when using input surface. For now, route
1544 // these directly to the component.
1545 if (config->mInputSurface == nullptr
1546 && (property_get_bool("debug.stagefright.ccodec_delayed_params", false)
1547 || comp->getName().find("c2.android.") == 0)) {
1548 mChannel->setParameters(configUpdate);
1549 } else {
1550 (void)config->setParameters(comp, configUpdate, C2_MAY_BLOCK);
1551 }
1552}
1553
1554void CCodec::signalEndOfInputStream() {
1555 mCallback->onSignaledInputEOS(mChannel->signalEndOfInputStream());
1556}
1557
1558void CCodec::signalRequestIDRFrame() {
1559 std::shared_ptr<Codec2Client::Component> comp;
1560 {
1561 Mutexed<State>::Locked state(mState);
1562 if (state->get() == RELEASED) {
1563 ALOGD("no IDR request sent since component is released");
1564 return;
1565 }
1566 comp = state->comp;
1567 }
1568 ALOGV("request IDR");
1569 Mutexed<Config>::Locked config(mConfig);
1570 std::vector<std::unique_ptr<C2Param>> params;
1571 params.push_back(
1572 std::make_unique<C2StreamRequestSyncFrameTuning::output>(0u, true));
1573 config->setParameters(comp, params, C2_MAY_BLOCK);
1574}
1575
Wonsik Kimab34ed62019-01-31 15:28:46 -08001576void CCodec::onWorkDone(std::list<std::unique_ptr<C2Work>> &workItems) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001577 if (!workItems.empty()) {
Wonsik Kimab34ed62019-01-31 15:28:46 -08001578 Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue);
1579 queue->splice(queue->end(), workItems);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001580 }
1581 (new AMessage(kWhatWorkDone, this))->post();
1582}
1583
Wonsik Kimab34ed62019-01-31 15:28:46 -08001584void CCodec::onInputBufferDone(uint64_t frameIndex, size_t arrayIndex) {
1585 mChannel->onInputBufferDone(frameIndex, arrayIndex);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001586}
1587
1588void CCodec::onMessageReceived(const sp<AMessage> &msg) {
1589 TimePoint now = std::chrono::steady_clock::now();
1590 CCodecWatchdog::getInstance()->watch(this);
1591 switch (msg->what()) {
1592 case kWhatAllocate: {
1593 // C2ComponentStore::createComponent() should return within 100ms.
1594 setDeadline(now, 150ms, "allocate");
1595 sp<RefBase> obj;
1596 CHECK(msg->findObject("codecInfo", &obj));
1597 allocate((MediaCodecInfo *)obj.get());
1598 break;
1599 }
1600 case kWhatConfigure: {
1601 // C2Component::commit_sm() should return within 5ms.
1602 setDeadline(now, 250ms, "configure");
1603 sp<AMessage> format;
1604 CHECK(msg->findMessage("format", &format));
1605 configure(format);
1606 break;
1607 }
1608 case kWhatStart: {
1609 // C2Component::start() should return within 500ms.
1610 setDeadline(now, 550ms, "start");
Pawin Vongmasa36653902018-11-15 00:10:25 -08001611 start();
1612 break;
1613 }
1614 case kWhatStop: {
1615 // C2Component::stop() should return within 500ms.
1616 setDeadline(now, 550ms, "stop");
1617 stop();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001618 break;
1619 }
1620 case kWhatFlush: {
1621 // C2Component::flush_sm() should return within 5ms.
1622 setDeadline(now, 50ms, "flush");
1623 flush();
1624 break;
1625 }
1626 case kWhatCreateInputSurface: {
1627 // Surface operations may be briefly blocking.
1628 setDeadline(now, 100ms, "createInputSurface");
1629 createInputSurface();
1630 break;
1631 }
1632 case kWhatSetInputSurface: {
1633 // Surface operations may be briefly blocking.
1634 setDeadline(now, 100ms, "setInputSurface");
1635 sp<RefBase> obj;
1636 CHECK(msg->findObject("surface", &obj));
1637 sp<PersistentSurface> surface(static_cast<PersistentSurface *>(obj.get()));
1638 setInputSurface(surface);
1639 break;
1640 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001641 case kWhatWorkDone: {
1642 std::unique_ptr<C2Work> work;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001643 bool shouldPost = false;
1644 {
1645 Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue);
1646 if (queue->empty()) {
1647 break;
1648 }
1649 work.swap(queue->front());
1650 queue->pop_front();
1651 shouldPost = !queue->empty();
1652 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001653 if (shouldPost) {
1654 (new AMessage(kWhatWorkDone, this))->post();
1655 }
1656
Pawin Vongmasa36653902018-11-15 00:10:25 -08001657 // handle configuration changes in work done
1658 Mutexed<Config>::Locked config(mConfig);
1659 bool changed = false;
1660 Config::Watcher<C2StreamInitDataInfo::output> initData =
1661 config->watch<C2StreamInitDataInfo::output>();
1662 if (!work->worklets.empty()
1663 && (work->worklets.front()->output.flags
1664 & C2FrameData::FLAG_DISCARD_FRAME) == 0) {
1665
1666 // copy buffer info to config
1667 std::vector<std::unique_ptr<C2Param>> updates =
1668 std::move(work->worklets.front()->output.configUpdate);
1669 unsigned stream = 0;
1670 for (const std::shared_ptr<C2Buffer> &buf : work->worklets.front()->output.buffers) {
1671 for (const std::shared_ptr<const C2Info> &info : buf->info()) {
1672 // move all info into output-stream #0 domain
1673 updates.emplace_back(C2Param::CopyAsStream(*info, true /* output */, stream));
1674 }
1675 for (const C2ConstGraphicBlock &block : buf->data().graphicBlocks()) {
1676 // ALOGV("got output buffer with crop %u,%u+%u,%u and size %u,%u",
1677 // block.crop().left, block.crop().top,
1678 // block.crop().width, block.crop().height,
1679 // block.width(), block.height());
1680 updates.emplace_back(new C2StreamCropRectInfo::output(stream, block.crop()));
1681 updates.emplace_back(new C2StreamPictureSizeInfo::output(
1682 stream, block.width(), block.height()));
1683 break; // for now only do the first block
1684 }
1685 ++stream;
1686 }
1687
1688 changed = config->updateConfiguration(updates, config->mOutputDomain);
1689
1690 // copy standard infos to graphic buffers if not already present (otherwise, we
1691 // may overwrite the actual intermediate value with a final value)
1692 stream = 0;
1693 const static std::vector<C2Param::Index> stdGfxInfos = {
1694 C2StreamRotationInfo::output::PARAM_TYPE,
1695 C2StreamColorAspectsInfo::output::PARAM_TYPE,
1696 C2StreamDataSpaceInfo::output::PARAM_TYPE,
1697 C2StreamHdrStaticInfo::output::PARAM_TYPE,
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001698 C2StreamHdr10PlusInfo::output::PARAM_TYPE,
Pawin Vongmasa36653902018-11-15 00:10:25 -08001699 C2StreamPixelAspectRatioInfo::output::PARAM_TYPE,
1700 C2StreamSurfaceScalingInfo::output::PARAM_TYPE
1701 };
1702 for (const std::shared_ptr<C2Buffer> &buf : work->worklets.front()->output.buffers) {
1703 if (buf->data().graphicBlocks().size()) {
1704 for (C2Param::Index ix : stdGfxInfos) {
1705 if (!buf->hasInfo(ix)) {
1706 const C2Param *param =
1707 config->getConfigParameterValue(ix.withStream(stream));
1708 if (param) {
1709 std::shared_ptr<C2Param> info(C2Param::Copy(*param));
1710 buf->setInfo(std::static_pointer_cast<C2Info>(info));
1711 }
1712 }
1713 }
1714 }
1715 ++stream;
1716 }
1717 }
1718 mChannel->onWorkDone(
1719 std::move(work), changed ? config->mOutputFormat : nullptr,
Wonsik Kimab34ed62019-01-31 15:28:46 -08001720 initData.hasChanged() ? initData.update().get() : nullptr);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001721 break;
1722 }
1723 case kWhatWatch: {
1724 // watch message already posted; no-op.
1725 break;
1726 }
1727 default: {
1728 ALOGE("unrecognized message");
1729 break;
1730 }
1731 }
1732 setDeadline(TimePoint::max(), 0ms, "none");
1733}
1734
1735void CCodec::setDeadline(
1736 const TimePoint &now,
1737 const std::chrono::milliseconds &timeout,
1738 const char *name) {
1739 int32_t mult = std::max(1, property_get_int32("debug.stagefright.ccodec_timeout_mult", 1));
1740 Mutexed<NamedTimePoint>::Locked deadline(mDeadline);
1741 deadline->set(now + (timeout * mult), name);
1742}
1743
1744void CCodec::initiateReleaseIfStuck() {
1745 std::string name;
1746 bool pendingDeadline = false;
Wonsik Kimab34ed62019-01-31 15:28:46 -08001747 {
1748 Mutexed<NamedTimePoint>::Locked deadline(mDeadline);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001749 if (deadline->get() < std::chrono::steady_clock::now()) {
1750 name = deadline->getName();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001751 }
1752 if (deadline->get() != TimePoint::max()) {
1753 pendingDeadline = true;
1754 }
1755 }
1756 if (name.empty()) {
Wonsik Kimab34ed62019-01-31 15:28:46 -08001757 constexpr std::chrono::steady_clock::duration kWorkDurationThreshold = 3s;
1758 std::chrono::steady_clock::duration elapsed = mChannel->elapsed();
1759 if (elapsed >= kWorkDurationThreshold) {
1760 name = "queue";
1761 }
1762 if (elapsed > 0s) {
1763 pendingDeadline = true;
1764 }
1765 }
1766 if (name.empty()) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001767 // We're not stuck.
1768 if (pendingDeadline) {
1769 // If we are not stuck yet but still has deadline coming up,
1770 // post watch message to check back later.
1771 (new AMessage(kWhatWatch, this))->post();
1772 }
1773 return;
1774 }
1775
1776 ALOGW("previous call to %s exceeded timeout", name.c_str());
1777 initiateRelease(false);
1778 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1779}
1780
Pawin Vongmasa36653902018-11-15 00:10:25 -08001781} // namespace android
1782
1783extern "C" android::CodecBase *CreateCodec() {
1784 return new android::CCodec;
1785}
1786
Lajos Molnar47118272019-01-31 16:28:04 -08001787// Create Codec 2.0 input surface
Pawin Vongmasa36653902018-11-15 00:10:25 -08001788extern "C" android::PersistentSurface *CreateInputSurface() {
1789 // Attempt to create a Codec2's input surface.
1790 std::shared_ptr<android::Codec2Client::InputSurface> inputSurface =
1791 android::Codec2Client::CreateInputSurface();
Lajos Molnar47118272019-01-31 16:28:04 -08001792 if (!inputSurface) {
1793 return nullptr;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001794 }
Lajos Molnar47118272019-01-31 16:28:04 -08001795 return new android::PersistentSurface(
1796 inputSurface->getGraphicBufferProducer(),
1797 static_cast<android::sp<android::hidl::base::V1_0::IBase>>(
1798 inputSurface->getHalInterface()));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001799}
1800