blob: 39263f9b93a703833c8148f7593c8bfe70d57442 [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
Pawin Vongmasa36653902018-11-15 00:10:25 -080029#include <android/IOMXBufferSource.h>
Pawin Vongmasabf69de92019-10-29 06:21:27 -070030#include <android/hardware/media/c2/1.0/IInputSurface.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080031#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>
Wonsik Kim9917d4a2019-10-24 12:56:38 -070038#include <media/omx/1.0/WOmxNode.h>
39#include <media/openmax/OMX_Core.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080040#include <media/openmax/OMX_IndexExt.h>
Wonsik Kim9917d4a2019-10-24 12:56:38 -070041#include <media/stagefright/omx/1.0/WGraphicBufferSource.h>
42#include <media/stagefright/omx/OmxGraphicBufferSource.h>
Wonsik Kim155d5cb2019-10-09 12:49:49 -070043#include <media/stagefright/CCodec.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080044#include <media/stagefright/BufferProducerWrapper.h>
45#include <media/stagefright/MediaCodecConstants.h>
46#include <media/stagefright/PersistentSurface.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080047
48#include "C2OMXNode.h"
Pawin Vongmasa36653902018-11-15 00:10:25 -080049#include "CCodecBufferChannel.h"
Wonsik Kim155d5cb2019-10-09 12:49:49 -070050#include "CCodecConfig.h"
Wonsik Kimfb7a7672019-12-27 17:13:33 -080051#include "Codec2Mapper.h"
Pawin Vongmasa36653902018-11-15 00:10:25 -080052#include "InputSurfaceWrapper.h"
53
54extern "C" android::PersistentSurface *CreateInputSurface();
55
56namespace android {
57
58using namespace std::chrono_literals;
59using ::android::hardware::graphics::bufferqueue::V1_0::utils::H2BGraphicBufferProducer;
60using android::base::StringPrintf;
Pawin Vongmasad0f0e142018-11-15 03:36:28 -080061using ::android::hardware::media::c2::V1_0::IInputSurface;
Pawin Vongmasa36653902018-11-15 00:10:25 -080062
Wonsik Kim9917d4a2019-10-24 12:56:38 -070063typedef hardware::media::omx::V1_0::IGraphicBufferSource HGraphicBufferSource;
Wonsik Kim155d5cb2019-10-09 12:49:49 -070064typedef CCodecConfig Config;
Wonsik Kim9917d4a2019-10-24 12:56:38 -070065
Pawin Vongmasa36653902018-11-15 00:10:25 -080066namespace {
67
68class CCodecWatchdog : public AHandler {
69private:
70 enum {
71 kWhatWatch,
72 };
73 constexpr static int64_t kWatchIntervalUs = 3300000; // 3.3 secs
74
75public:
76 static sp<CCodecWatchdog> getInstance() {
77 static sp<CCodecWatchdog> instance(new CCodecWatchdog);
78 static std::once_flag flag;
79 // Call Init() only once.
80 std::call_once(flag, Init, instance);
81 return instance;
82 }
83
84 ~CCodecWatchdog() = default;
85
86 void watch(sp<CCodec> codec) {
87 bool shouldPost = false;
88 {
89 Mutexed<std::set<wp<CCodec>>>::Locked codecs(mCodecsToWatch);
90 // If a watch message is in flight, piggy-back this instance as well.
91 // Otherwise, post a new watch message.
92 shouldPost = codecs->empty();
93 codecs->emplace(codec);
94 }
95 if (shouldPost) {
96 ALOGV("posting watch message");
97 (new AMessage(kWhatWatch, this))->post(kWatchIntervalUs);
98 }
99 }
100
101protected:
102 void onMessageReceived(const sp<AMessage> &msg) {
103 switch (msg->what()) {
104 case kWhatWatch: {
105 Mutexed<std::set<wp<CCodec>>>::Locked codecs(mCodecsToWatch);
106 ALOGV("watch for %zu codecs", codecs->size());
107 for (auto it = codecs->begin(); it != codecs->end(); ++it) {
108 sp<CCodec> codec = it->promote();
109 if (codec == nullptr) {
110 continue;
111 }
112 codec->initiateReleaseIfStuck();
113 }
114 codecs->clear();
115 break;
116 }
117
118 default: {
119 TRESPASS("CCodecWatchdog: unrecognized message");
120 }
121 }
122 }
123
124private:
125 CCodecWatchdog() : mLooper(new ALooper) {}
126
127 static void Init(const sp<CCodecWatchdog> &thiz) {
128 ALOGV("Init");
129 thiz->mLooper->setName("CCodecWatchdog");
130 thiz->mLooper->registerHandler(thiz);
131 thiz->mLooper->start();
132 }
133
134 sp<ALooper> mLooper;
135
136 Mutexed<std::set<wp<CCodec>>> mCodecsToWatch;
137};
138
139class C2InputSurfaceWrapper : public InputSurfaceWrapper {
140public:
141 explicit C2InputSurfaceWrapper(
142 const std::shared_ptr<Codec2Client::InputSurface> &surface) :
143 mSurface(surface) {
144 }
145
146 ~C2InputSurfaceWrapper() override = default;
147
148 status_t connect(const std::shared_ptr<Codec2Client::Component> &comp) override {
149 if (mConnection != nullptr) {
150 return ALREADY_EXISTS;
151 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800152 return toStatusT(comp->connectToInputSurface(mSurface, &mConnection));
Pawin Vongmasa36653902018-11-15 00:10:25 -0800153 }
154
155 void disconnect() override {
156 if (mConnection != nullptr) {
157 mConnection->disconnect();
158 mConnection = nullptr;
159 }
160 }
161
162 status_t start() override {
163 // InputSurface does not distinguish started state
164 return OK;
165 }
166
167 status_t signalEndOfInputStream() override {
168 C2InputSurfaceEosTuning eos(true);
169 std::vector<std::unique_ptr<C2SettingResult>> failures;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800170 c2_status_t err = mSurface->config({&eos}, C2_MAY_BLOCK, &failures);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800171 if (err != C2_OK) {
172 return UNKNOWN_ERROR;
173 }
174 return OK;
175 }
176
177 status_t configure(Config &config __unused) {
178 // TODO
179 return OK;
180 }
181
182private:
183 std::shared_ptr<Codec2Client::InputSurface> mSurface;
184 std::shared_ptr<Codec2Client::InputSurfaceConnection> mConnection;
185};
186
187class GraphicBufferSourceWrapper : public InputSurfaceWrapper {
188public:
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700189 typedef hardware::media::omx::V1_0::Status OmxStatus;
190
Pawin Vongmasa36653902018-11-15 00:10:25 -0800191 GraphicBufferSourceWrapper(
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700192 const sp<HGraphicBufferSource> &source,
Pawin Vongmasa36653902018-11-15 00:10:25 -0800193 uint32_t width,
Wonsik Kim9eac4d12019-05-23 12:58:48 -0700194 uint32_t height,
195 uint64_t usage)
Pawin Vongmasa36653902018-11-15 00:10:25 -0800196 : mSource(source), mWidth(width), mHeight(height) {
197 mDataSpace = HAL_DATASPACE_BT709;
Wonsik Kim9eac4d12019-05-23 12:58:48 -0700198 mConfig.mUsage = usage;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800199 }
200 ~GraphicBufferSourceWrapper() override = default;
201
202 status_t connect(const std::shared_ptr<Codec2Client::Component> &comp) override {
203 mNode = new C2OMXNode(comp);
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700204 mOmxNode = new hardware::media::omx::V1_0::utils::TWOmxNode(mNode);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800205 mNode->setFrameSize(mWidth, mHeight);
206
Wonsik Kim9eac4d12019-05-23 12:58:48 -0700207 // Usage is queried during configure(), so setting it beforehand.
208 OMX_U32 usage = mConfig.mUsage & 0xFFFFFFFF;
209 (void)mNode->setParameter(
210 (OMX_INDEXTYPE)OMX_IndexParamConsumerUsageBits,
211 &usage, sizeof(usage));
212
Pawin Vongmasa36653902018-11-15 00:10:25 -0800213 // NOTE: we do not use/pass through color aspects from GraphicBufferSource as we
214 // communicate that directly to the component.
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700215 mSource->configure(
216 mOmxNode, static_cast<hardware::graphics::common::V1_0::Dataspace>(mDataSpace));
Pawin Vongmasa36653902018-11-15 00:10:25 -0800217 return OK;
218 }
219
220 void disconnect() override {
221 if (mNode == nullptr) {
222 return;
223 }
224 sp<IOMXBufferSource> source = mNode->getSource();
225 if (source == nullptr) {
226 ALOGD("GBSWrapper::disconnect: node is not configured with OMXBufferSource.");
227 return;
228 }
229 source->onOmxIdle();
230 source->onOmxLoaded();
231 mNode.clear();
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700232 mOmxNode.clear();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800233 }
234
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700235 status_t GetStatus(hardware::Return<OmxStatus> &&status) {
236 if (status.isOk()) {
237 return static_cast<status_t>(status.withDefault(OmxStatus::UNKNOWN_ERROR));
238 } else if (status.isDeadObject()) {
239 return DEAD_OBJECT;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800240 }
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700241 return UNKNOWN_ERROR;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800242 }
243
244 status_t start() override {
245 sp<IOMXBufferSource> source = mNode->getSource();
246 if (source == nullptr) {
247 return NO_INIT;
248 }
249 constexpr size_t kNumSlots = 16;
250 for (size_t i = 0; i < kNumSlots; ++i) {
251 source->onInputBufferAdded(i);
252 }
253
254 source->onOmxExecuting();
255 return OK;
256 }
257
258 status_t signalEndOfInputStream() override {
259 return GetStatus(mSource->signalEndOfInputStream());
260 }
261
262 status_t configure(Config &config) {
263 std::stringstream status;
264 status_t err = OK;
265
266 // handle each configuration granually, in case we need to handle part of the configuration
267 // elsewhere
268
269 // TRICKY: we do not unset frame delay repeating
270 if (config.mMinFps > 0 && config.mMinFps != mConfig.mMinFps) {
271 int64_t us = 1e6 / config.mMinFps + 0.5;
272 status_t res = GetStatus(mSource->setRepeatPreviousFrameDelayUs(us));
273 status << " minFps=" << config.mMinFps << " => repeatDelayUs=" << us;
274 if (res != OK) {
275 status << " (=> " << asString(res) << ")";
276 err = res;
277 }
278 mConfig.mMinFps = config.mMinFps;
279 }
280
281 // pts gap
282 if (config.mMinAdjustedFps > 0 || config.mFixedAdjustedFps > 0) {
283 if (mNode != nullptr) {
284 OMX_PARAM_U32TYPE ptrGapParam = {};
285 ptrGapParam.nSize = sizeof(OMX_PARAM_U32TYPE);
Wonsik Kim95ba0162019-03-19 15:51:54 -0700286 float gap = (config.mMinAdjustedFps > 0)
Pawin Vongmasa36653902018-11-15 00:10:25 -0800287 ? c2_min(INT32_MAX + 0., 1e6 / config.mMinAdjustedFps + 0.5)
288 : c2_max(0. - INT32_MAX, -1e6 / config.mFixedAdjustedFps - 0.5);
Wonsik Kim95ba0162019-03-19 15:51:54 -0700289 // float -> uint32_t is undefined if the value is negative.
290 // First convert to int32_t to ensure the expected behavior.
291 ptrGapParam.nU32 = int32_t(gap);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800292 (void)mNode->setParameter(
293 (OMX_INDEXTYPE)OMX_IndexParamMaxFrameDurationForBitrateControl,
294 &ptrGapParam, sizeof(ptrGapParam));
295 }
296 }
297
298 // max fps
299 // TRICKY: we do not unset max fps to 0 unless using fixed fps
Wonsik Kim95ba0162019-03-19 15:51:54 -0700300 if ((config.mMaxFps > 0 || (config.mFixedAdjustedFps > 0 && config.mMaxFps == -1))
Pawin Vongmasa36653902018-11-15 00:10:25 -0800301 && config.mMaxFps != mConfig.mMaxFps) {
302 status_t res = GetStatus(mSource->setMaxFps(config.mMaxFps));
303 status << " maxFps=" << config.mMaxFps;
304 if (res != OK) {
305 status << " (=> " << asString(res) << ")";
306 err = res;
307 }
308 mConfig.mMaxFps = config.mMaxFps;
309 }
310
311 if (config.mTimeOffsetUs != mConfig.mTimeOffsetUs) {
312 status_t res = GetStatus(mSource->setTimeOffsetUs(config.mTimeOffsetUs));
313 status << " timeOffset " << config.mTimeOffsetUs << "us";
314 if (res != OK) {
315 status << " (=> " << asString(res) << ")";
316 err = res;
317 }
318 mConfig.mTimeOffsetUs = config.mTimeOffsetUs;
319 }
320
321 if (config.mCaptureFps != mConfig.mCaptureFps || config.mCodedFps != mConfig.mCodedFps) {
322 status_t res =
323 GetStatus(mSource->setTimeLapseConfig(config.mCodedFps, config.mCaptureFps));
324 status << " timeLapse " << config.mCaptureFps << "fps as " << config.mCodedFps << "fps";
325 if (res != OK) {
326 status << " (=> " << asString(res) << ")";
327 err = res;
328 }
329 mConfig.mCaptureFps = config.mCaptureFps;
330 mConfig.mCodedFps = config.mCodedFps;
331 }
332
333 if (config.mStartAtUs != mConfig.mStartAtUs
334 || (config.mStopped != mConfig.mStopped && !config.mStopped)) {
335 status_t res = GetStatus(mSource->setStartTimeUs(config.mStartAtUs));
336 status << " start at " << config.mStartAtUs << "us";
337 if (res != OK) {
338 status << " (=> " << asString(res) << ")";
339 err = res;
340 }
341 mConfig.mStartAtUs = config.mStartAtUs;
342 mConfig.mStopped = config.mStopped;
343 }
344
345 // suspend-resume
346 if (config.mSuspended != mConfig.mSuspended) {
347 status_t res = GetStatus(mSource->setSuspend(config.mSuspended, config.mSuspendAtUs));
348 status << " " << (config.mSuspended ? "suspend" : "resume")
349 << " at " << config.mSuspendAtUs << "us";
350 if (res != OK) {
351 status << " (=> " << asString(res) << ")";
352 err = res;
353 }
354 mConfig.mSuspended = config.mSuspended;
355 mConfig.mSuspendAtUs = config.mSuspendAtUs;
356 }
357
358 if (config.mStopped != mConfig.mStopped && config.mStopped) {
359 status_t res = GetStatus(mSource->setStopTimeUs(config.mStopAtUs));
360 status << " stop at " << config.mStopAtUs << "us";
361 if (res != OK) {
362 status << " (=> " << asString(res) << ")";
363 err = res;
364 } else {
365 status << " delayUs";
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700366 hardware::Return<void> trans = mSource->getStopTimeOffsetUs(
367 [&res, &delayUs = config.mInputDelayUs](
368 auto status, auto stopTimeOffsetUs) {
369 res = static_cast<status_t>(status);
370 delayUs = stopTimeOffsetUs;
371 });
372 if (!trans.isOk()) {
373 res = trans.isDeadObject() ? DEAD_OBJECT : UNKNOWN_ERROR;
374 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800375 if (res != OK) {
376 status << " (=> " << asString(res) << ")";
377 } else {
378 status << "=" << config.mInputDelayUs << "us";
379 }
380 mConfig.mInputDelayUs = config.mInputDelayUs;
381 }
382 mConfig.mStopAtUs = config.mStopAtUs;
383 mConfig.mStopped = config.mStopped;
384 }
385
386 // color aspects (android._color-aspects)
387
Wonsik Kim9eac4d12019-05-23 12:58:48 -0700388 // consumer usage is queried earlier.
389
Wonsik Kimbd557932019-07-02 15:51:20 -0700390 if (status.str().empty()) {
391 ALOGD("ISConfig not changed");
392 } else {
393 ALOGD("ISConfig%s", status.str().c_str());
394 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800395 return err;
396 }
397
Wonsik Kim4f3314d2019-03-26 17:00:34 -0700398 void onInputBufferDone(c2_cntr64_t index) override {
399 mNode->onInputBufferDone(index);
400 }
401
Pawin Vongmasa36653902018-11-15 00:10:25 -0800402private:
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700403 sp<HGraphicBufferSource> mSource;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800404 sp<C2OMXNode> mNode;
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700405 sp<hardware::media::omx::V1_0::IOmxNode> mOmxNode;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800406 uint32_t mWidth;
407 uint32_t mHeight;
408 Config mConfig;
409};
410
411class Codec2ClientInterfaceWrapper : public C2ComponentStore {
412 std::shared_ptr<Codec2Client> mClient;
413
414public:
415 Codec2ClientInterfaceWrapper(std::shared_ptr<Codec2Client> client)
416 : mClient(client) { }
417
418 virtual ~Codec2ClientInterfaceWrapper() = default;
419
420 virtual c2_status_t config_sm(
421 const std::vector<C2Param *> &params,
422 std::vector<std::unique_ptr<C2SettingResult>> *const failures) {
423 return mClient->config(params, C2_MAY_BLOCK, failures);
424 };
425
426 virtual c2_status_t copyBuffer(
427 std::shared_ptr<C2GraphicBuffer>,
428 std::shared_ptr<C2GraphicBuffer>) {
429 return C2_OMITTED;
430 }
431
432 virtual c2_status_t createComponent(
433 C2String, std::shared_ptr<C2Component> *const component) {
434 component->reset();
435 return C2_OMITTED;
436 }
437
438 virtual c2_status_t createInterface(
439 C2String, std::shared_ptr<C2ComponentInterface> *const interface) {
440 interface->reset();
441 return C2_OMITTED;
442 }
443
444 virtual c2_status_t query_sm(
445 const std::vector<C2Param *> &stackParams,
446 const std::vector<C2Param::Index> &heapParamIndices,
447 std::vector<std::unique_ptr<C2Param>> *const heapParams) const {
448 return mClient->query(stackParams, heapParamIndices, C2_MAY_BLOCK, heapParams);
449 }
450
451 virtual c2_status_t querySupportedParams_nb(
452 std::vector<std::shared_ptr<C2ParamDescriptor>> *const params) const {
453 return mClient->querySupportedParams(params);
454 }
455
456 virtual c2_status_t querySupportedValues_sm(
457 std::vector<C2FieldSupportedValuesQuery> &fields) const {
458 return mClient->querySupportedValues(fields, C2_MAY_BLOCK);
459 }
460
461 virtual C2String getName() const {
462 return mClient->getName();
463 }
464
465 virtual std::shared_ptr<C2ParamReflector> getParamReflector() const {
466 return mClient->getParamReflector();
467 }
468
469 virtual std::vector<std::shared_ptr<const C2Component::Traits>> listComponents() {
470 return std::vector<std::shared_ptr<const C2Component::Traits>>();
471 }
472};
473
474} // namespace
475
476// CCodec::ClientListener
477
478struct CCodec::ClientListener : public Codec2Client::Listener {
479
480 explicit ClientListener(const wp<CCodec> &codec) : mCodec(codec) {}
481
482 virtual void onWorkDone(
483 const std::weak_ptr<Codec2Client::Component>& component,
Wonsik Kimab34ed62019-01-31 15:28:46 -0800484 std::list<std::unique_ptr<C2Work>>& workItems) override {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800485 (void)component;
486 sp<CCodec> codec(mCodec.promote());
487 if (!codec) {
488 return;
489 }
Wonsik Kimab34ed62019-01-31 15:28:46 -0800490 codec->onWorkDone(workItems);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800491 }
492
493 virtual void onTripped(
494 const std::weak_ptr<Codec2Client::Component>& component,
495 const std::vector<std::shared_ptr<C2SettingResult>>& settingResult
496 ) override {
497 // TODO
498 (void)component;
499 (void)settingResult;
500 }
501
502 virtual void onError(
503 const std::weak_ptr<Codec2Client::Component>& component,
504 uint32_t errorCode) override {
505 // TODO
506 (void)component;
507 (void)errorCode;
508 }
509
510 virtual void onDeath(
511 const std::weak_ptr<Codec2Client::Component>& component) override {
512 { // Log the death of the component.
513 std::shared_ptr<Codec2Client::Component> comp = component.lock();
514 if (!comp) {
515 ALOGE("Codec2 component died.");
516 } else {
517 ALOGE("Codec2 component \"%s\" died.", comp->getName().c_str());
518 }
519 }
520
521 // Report to MediaCodec.
522 sp<CCodec> codec(mCodec.promote());
523 if (!codec || !codec->mCallback) {
524 return;
525 }
526 codec->mCallback->onError(DEAD_OBJECT, ACTION_CODE_FATAL);
527 }
528
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800529 virtual void onFrameRendered(uint64_t bufferQueueId,
530 int32_t slotId,
531 int64_t timestampNs) override {
532 // TODO: implement
533 (void)bufferQueueId;
534 (void)slotId;
535 (void)timestampNs;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800536 }
537
538 virtual void onInputBufferDone(
Wonsik Kimab34ed62019-01-31 15:28:46 -0800539 uint64_t frameIndex, size_t arrayIndex) override {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800540 sp<CCodec> codec(mCodec.promote());
541 if (codec) {
Wonsik Kimab34ed62019-01-31 15:28:46 -0800542 codec->onInputBufferDone(frameIndex, arrayIndex);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800543 }
544 }
545
546private:
547 wp<CCodec> mCodec;
548};
549
550// CCodecCallbackImpl
551
552class CCodecCallbackImpl : public CCodecCallback {
553public:
554 explicit CCodecCallbackImpl(CCodec *codec) : mCodec(codec) {}
555 ~CCodecCallbackImpl() override = default;
556
557 void onError(status_t err, enum ActionCode actionCode) override {
558 mCodec->mCallback->onError(err, actionCode);
559 }
560
561 void onOutputFramesRendered(int64_t mediaTimeUs, nsecs_t renderTimeNs) override {
562 mCodec->mCallback->onOutputFramesRendered(
563 {RenderedFrameInfo(mediaTimeUs, renderTimeNs)});
564 }
565
Pawin Vongmasa36653902018-11-15 00:10:25 -0800566 void onOutputBuffersChanged() override {
567 mCodec->mCallback->onOutputBuffersChanged();
568 }
569
570private:
571 CCodec *mCodec;
572};
573
574// CCodec
575
576CCodec::CCodec()
Wonsik Kim155d5cb2019-10-09 12:49:49 -0700577 : mChannel(new CCodecBufferChannel(std::make_shared<CCodecCallbackImpl>(this))),
578 mConfig(new CCodecConfig) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800579}
580
581CCodec::~CCodec() {
582}
583
584std::shared_ptr<BufferChannelBase> CCodec::getBufferChannel() {
585 return mChannel;
586}
587
588status_t CCodec::tryAndReportOnError(std::function<status_t()> job) {
589 status_t err = job();
590 if (err != C2_OK) {
591 mCallback->onError(err, ACTION_CODE_FATAL);
592 }
593 return err;
594}
595
596void CCodec::initiateAllocateComponent(const sp<AMessage> &msg) {
597 auto setAllocating = [this] {
598 Mutexed<State>::Locked state(mState);
599 if (state->get() != RELEASED) {
600 return INVALID_OPERATION;
601 }
602 state->set(ALLOCATING);
603 return OK;
604 };
605 if (tryAndReportOnError(setAllocating) != OK) {
606 return;
607 }
608
609 sp<RefBase> codecInfo;
610 CHECK(msg->findObject("codecInfo", &codecInfo));
611 // For Codec 2.0 components, componentName == codecInfo->getCodecName().
612
613 sp<AMessage> allocMsg(new AMessage(kWhatAllocate, this));
614 allocMsg->setObject("codecInfo", codecInfo);
615 allocMsg->post();
616}
617
618void CCodec::allocate(const sp<MediaCodecInfo> &codecInfo) {
619 if (codecInfo == nullptr) {
620 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
621 return;
622 }
623 ALOGD("allocate(%s)", codecInfo->getCodecName());
624 mClientListener.reset(new ClientListener(this));
625
626 AString componentName = codecInfo->getCodecName();
627 std::shared_ptr<Codec2Client> client;
628
629 // set up preferred component store to access vendor store parameters
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700630 client = Codec2Client::CreateFromService("default");
Pawin Vongmasa36653902018-11-15 00:10:25 -0800631 if (client) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800632 ALOGI("setting up '%s' as default (vendor) store", client->getServiceName().c_str());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800633 SetPreferredCodec2ComponentStore(
634 std::make_shared<Codec2ClientInterfaceWrapper>(client));
635 }
636
637 std::shared_ptr<Codec2Client::Component> comp =
638 Codec2Client::CreateComponentByName(
639 componentName.c_str(),
640 mClientListener,
641 &client);
642 if (!comp) {
643 ALOGE("Failed Create component: %s", componentName.c_str());
644 Mutexed<State>::Locked state(mState);
645 state->set(RELEASED);
646 state.unlock();
647 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
648 state.lock();
649 return;
650 }
651 ALOGI("Created component [%s]", componentName.c_str());
652 mChannel->setComponent(comp);
653 auto setAllocated = [this, comp, client] {
654 Mutexed<State>::Locked state(mState);
655 if (state->get() != ALLOCATING) {
656 state->set(RELEASED);
657 return UNKNOWN_ERROR;
658 }
659 state->set(ALLOCATED);
660 state->comp = comp;
661 mClient = client;
662 return OK;
663 };
664 if (tryAndReportOnError(setAllocated) != OK) {
665 return;
666 }
667
668 // initialize config here in case setParameters is called prior to configure
Wonsik Kim155d5cb2019-10-09 12:49:49 -0700669 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
670 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800671 status_t err = config->initialize(mClient, comp);
672 if (err != OK) {
673 ALOGW("Failed to initialize configuration support");
674 // TODO: report error once we complete implementation.
675 }
676 config->queryConfiguration(comp);
677
678 mCallback->onComponentAllocated(componentName.c_str());
679}
680
681void CCodec::initiateConfigureComponent(const sp<AMessage> &format) {
682 auto checkAllocated = [this] {
683 Mutexed<State>::Locked state(mState);
684 return (state->get() != ALLOCATED) ? UNKNOWN_ERROR : OK;
685 };
686 if (tryAndReportOnError(checkAllocated) != OK) {
687 return;
688 }
689
690 sp<AMessage> msg(new AMessage(kWhatConfigure, this));
691 msg->setMessage("format", format);
692 msg->post();
693}
694
695void CCodec::configure(const sp<AMessage> &msg) {
696 std::shared_ptr<Codec2Client::Component> comp;
697 auto checkAllocated = [this, &comp] {
698 Mutexed<State>::Locked state(mState);
699 if (state->get() != ALLOCATED) {
700 state->set(RELEASED);
701 return UNKNOWN_ERROR;
702 }
703 comp = state->comp;
704 return OK;
705 };
706 if (tryAndReportOnError(checkAllocated) != OK) {
707 return;
708 }
709
710 auto doConfig = [msg, comp, this]() -> status_t {
711 AString mime;
712 if (!msg->findString("mime", &mime)) {
713 return BAD_VALUE;
714 }
715
716 int32_t encoder;
717 if (!msg->findInt32("encoder", &encoder)) {
718 encoder = false;
719 }
720
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800721 int32_t flags;
722 if (!msg->findInt32("flags", &flags)) {
723 return BAD_VALUE;
724 }
725
Pawin Vongmasa36653902018-11-15 00:10:25 -0800726 // TODO: read from intf()
727 if ((!encoder) != (comp->getName().find("encoder") == std::string::npos)) {
728 return UNKNOWN_ERROR;
729 }
730
731 int32_t storeMeta;
732 if (encoder
733 && msg->findInt32("android._input-metadata-buffer-type", &storeMeta)
734 && storeMeta != kMetadataBufferTypeInvalid) {
735 if (storeMeta != kMetadataBufferTypeANWBuffer) {
736 ALOGD("Only ANW buffers are supported for legacy metadata mode");
737 return BAD_VALUE;
738 }
739 mChannel->setMetaMode(CCodecBufferChannel::MODE_ANW);
740 }
741
742 sp<RefBase> obj;
743 sp<Surface> surface;
744 if (msg->findObject("native-window", &obj)) {
745 surface = static_cast<Surface *>(obj.get());
746 setSurface(surface);
747 }
748
Wonsik Kim155d5cb2019-10-09 12:49:49 -0700749 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
750 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800751 config->mUsingSurface = surface != nullptr;
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800752 config->mBuffersBoundToCodec = ((flags & CONFIGURE_FLAG_USE_BLOCK_MODEL) == 0);
753 ALOGD("[%s] buffers are %sbound to CCodec for this session",
754 comp->getName().c_str(), config->mBuffersBoundToCodec ? "" : "not ");
Pawin Vongmasa36653902018-11-15 00:10:25 -0800755
Wonsik Kim1114eea2019-02-25 14:35:24 -0800756 // Enforce required parameters
757 int32_t i32;
758 float flt;
759 if (config->mDomain & Config::IS_AUDIO) {
760 if (!msg->findInt32(KEY_SAMPLE_RATE, &i32)) {
761 ALOGD("sample rate is missing, which is required for audio components.");
762 return BAD_VALUE;
763 }
764 if (!msg->findInt32(KEY_CHANNEL_COUNT, &i32)) {
765 ALOGD("channel count is missing, which is required for audio components.");
766 return BAD_VALUE;
767 }
768 if ((config->mDomain & Config::IS_ENCODER)
769 && !mime.equalsIgnoreCase(MEDIA_MIMETYPE_AUDIO_FLAC)
770 && !msg->findInt32(KEY_BIT_RATE, &i32)
771 && !msg->findFloat(KEY_BIT_RATE, &flt)) {
772 ALOGD("bitrate is missing, which is required for audio encoders.");
773 return BAD_VALUE;
774 }
775 }
776 if (config->mDomain & (Config::IS_IMAGE | Config::IS_VIDEO)) {
777 if (!msg->findInt32(KEY_WIDTH, &i32)) {
778 ALOGD("width is missing, which is required for image/video components.");
779 return BAD_VALUE;
780 }
781 if (!msg->findInt32(KEY_HEIGHT, &i32)) {
782 ALOGD("height is missing, which is required for image/video components.");
783 return BAD_VALUE;
784 }
785 if ((config->mDomain & Config::IS_ENCODER) && (config->mDomain & Config::IS_VIDEO)) {
Harish Mahendrakar71cbb9d2019-05-21 11:21:27 -0700786 int32_t mode = BITRATE_MODE_VBR;
787 if (msg->findInt32(KEY_BITRATE_MODE, &mode) && mode == BITRATE_MODE_CQ) {
Harish Mahendrakar817d3182019-03-11 16:37:47 -0700788 if (!msg->findInt32(KEY_QUALITY, &i32)) {
789 ALOGD("quality is missing, which is required for video encoders in CQ.");
790 return BAD_VALUE;
791 }
792 } else {
793 if (!msg->findInt32(KEY_BIT_RATE, &i32)
794 && !msg->findFloat(KEY_BIT_RATE, &flt)) {
795 ALOGD("bitrate is missing, which is required for video encoders.");
796 return BAD_VALUE;
797 }
Wonsik Kim1114eea2019-02-25 14:35:24 -0800798 }
799 if (!msg->findInt32(KEY_I_FRAME_INTERVAL, &i32)
800 && !msg->findFloat(KEY_I_FRAME_INTERVAL, &flt)) {
801 ALOGD("I frame interval is missing, which is required for video encoders.");
802 return BAD_VALUE;
803 }
Wonsik Kimaab2eea2019-05-22 10:37:58 -0700804 if (!msg->findInt32(KEY_FRAME_RATE, &i32)
805 && !msg->findFloat(KEY_FRAME_RATE, &flt)) {
806 ALOGD("frame rate is missing, which is required for video encoders.");
807 return BAD_VALUE;
808 }
Wonsik Kim1114eea2019-02-25 14:35:24 -0800809 }
810 }
811
Pawin Vongmasa36653902018-11-15 00:10:25 -0800812 /*
813 * Handle input surface configuration
814 */
815 if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))
816 && (config->mDomain & Config::IS_ENCODER)) {
817 config->mISConfig.reset(new InputSurfaceWrapper::Config{});
818 {
819 config->mISConfig->mMinFps = 0;
820 int64_t value;
Chong Zhang038e8f82019-02-06 19:05:14 -0800821 if (msg->findInt64(KEY_REPEAT_PREVIOUS_FRAME_AFTER, &value) && value > 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800822 config->mISConfig->mMinFps = 1e6 / value;
823 }
Wonsik Kim95ba0162019-03-19 15:51:54 -0700824 if (!msg->findFloat(
825 KEY_MAX_FPS_TO_ENCODER, &config->mISConfig->mMaxFps)) {
826 config->mISConfig->mMaxFps = -1;
827 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800828 config->mISConfig->mMinAdjustedFps = 0;
829 config->mISConfig->mFixedAdjustedFps = 0;
Chong Zhang038e8f82019-02-06 19:05:14 -0800830 if (msg->findInt64(KEY_MAX_PTS_GAP_TO_ENCODER, &value)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800831 if (value < 0 && value >= INT32_MIN) {
832 config->mISConfig->mFixedAdjustedFps = -1e6 / value;
Wonsik Kim95ba0162019-03-19 15:51:54 -0700833 config->mISConfig->mMaxFps = -1;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800834 } else if (value > 0 && value <= INT32_MAX) {
835 config->mISConfig->mMinAdjustedFps = 1e6 / value;
836 }
837 }
838 }
839
840 {
Wonsik Kim8e55f3a2019-09-03 14:10:37 -0700841 bool captureFpsFound = false;
842 double timeLapseFps;
843 float captureRate;
844 if (msg->findDouble("time-lapse-fps", &timeLapseFps)) {
845 config->mISConfig->mCaptureFps = timeLapseFps;
846 captureFpsFound = true;
847 } else if (msg->findAsFloat(KEY_CAPTURE_RATE, &captureRate)) {
848 config->mISConfig->mCaptureFps = captureRate;
849 captureFpsFound = true;
850 }
851 if (captureFpsFound) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800852 (void)msg->findAsFloat(KEY_FRAME_RATE, &config->mISConfig->mCodedFps);
853 }
854 }
855
856 {
857 config->mISConfig->mSuspended = false;
858 config->mISConfig->mSuspendAtUs = -1;
859 int32_t value;
Chong Zhang038e8f82019-02-06 19:05:14 -0800860 if (msg->findInt32(KEY_CREATE_INPUT_SURFACE_SUSPENDED, &value) && value) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800861 config->mISConfig->mSuspended = true;
862 }
863 }
Wonsik Kim9eac4d12019-05-23 12:58:48 -0700864 config->mISConfig->mUsage = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800865 }
866
867 /*
868 * Handle desired color format.
869 */
870 if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))) {
871 int32_t format = -1;
872 if (!msg->findInt32(KEY_COLOR_FORMAT, &format)) {
873 /*
874 * Also handle default color format (encoders require color format, so this is only
875 * needed for decoders.
876 */
877 if (!(config->mDomain & Config::IS_ENCODER)) {
878 format = (surface == nullptr) ? COLOR_FormatYUV420Planar : COLOR_FormatSurface;
879 }
880 }
881
882 if (format >= 0) {
883 msg->setInt32("android._color-format", format);
884 }
885 }
886
887 std::vector<std::unique_ptr<C2Param>> configUpdate;
Wonsik Kimaa484ac2019-02-13 16:54:02 -0800888 // NOTE: We used to ignore "video-bitrate" at configure; replicate
889 // the behavior here.
890 sp<AMessage> sdkParams = msg;
891 int32_t videoBitrate;
892 if (sdkParams->findInt32(PARAMETER_KEY_VIDEO_BITRATE, &videoBitrate)) {
893 sdkParams = msg->dup();
894 sdkParams->removeEntryAt(sdkParams->findEntryByName(PARAMETER_KEY_VIDEO_BITRATE));
895 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800896 status_t err = config->getConfigUpdateFromSdkParams(
Wonsik Kimaa484ac2019-02-13 16:54:02 -0800897 comp, sdkParams, Config::IS_CONFIG, C2_DONT_BLOCK, &configUpdate);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800898 if (err != OK) {
899 ALOGW("failed to convert configuration to c2 params");
900 }
Wonsik Kimaab2eea2019-05-22 10:37:58 -0700901
902 int32_t maxBframes = 0;
903 if ((config->mDomain & Config::IS_ENCODER)
904 && (config->mDomain & Config::IS_VIDEO)
905 && sdkParams->findInt32(KEY_MAX_B_FRAMES, &maxBframes)
906 && maxBframes > 0) {
907 std::unique_ptr<C2StreamGopTuning::output> gop =
908 C2StreamGopTuning::output::AllocUnique(2 /* flexCount */, 0u /* stream */);
909 gop->m.values[0] = { P_FRAME, UINT32_MAX };
910 gop->m.values[1] = {
911 C2Config::picture_type_t(P_FRAME | B_FRAME),
912 uint32_t(maxBframes)
913 };
914 configUpdate.push_back(std::move(gop));
915 }
916
Pawin Vongmasa36653902018-11-15 00:10:25 -0800917 err = config->setParameters(comp, configUpdate, C2_DONT_BLOCK);
918 if (err != OK) {
919 ALOGW("failed to configure c2 params");
920 return err;
921 }
922
923 std::vector<std::unique_ptr<C2Param>> params;
924 C2StreamUsageTuning::input usage(0u, 0u);
925 C2StreamMaxBufferSizeInfo::input maxInputSize(0u, 0u);
Wonsik Kim9ca01d32019-04-01 14:45:47 -0700926 C2PrependHeaderModeSetting prepend(PREPEND_HEADER_TO_NONE);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800927
928 std::initializer_list<C2Param::Index> indices {
929 };
930 c2_status_t c2err = comp->query(
Wonsik Kim9ca01d32019-04-01 14:45:47 -0700931 { &usage, &maxInputSize, &prepend },
Pawin Vongmasa36653902018-11-15 00:10:25 -0800932 indices,
933 C2_DONT_BLOCK,
934 &params);
935 if (c2err != C2_OK && c2err != C2_BAD_INDEX) {
936 ALOGE("Failed to query component interface: %d", c2err);
937 return UNKNOWN_ERROR;
938 }
939 if (params.size() != indices.size()) {
940 ALOGE("Component returns wrong number of params: expected %zu actual %zu",
941 indices.size(), params.size());
942 return UNKNOWN_ERROR;
943 }
Wonsik Kim9eac4d12019-05-23 12:58:48 -0700944 if (usage) {
945 if (usage.value & C2MemoryUsage::CPU_READ) {
946 config->mInputFormat->setInt32("using-sw-read-often", true);
947 }
948 if (config->mISConfig) {
949 C2AndroidMemoryUsage androidUsage(C2MemoryUsage(usage.value));
950 config->mISConfig->mUsage = androidUsage.asGrallocUsage();
951 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800952 }
953
954 // NOTE: we don't blindly use client specified input size if specified as clients
955 // at times specify too small size. Instead, mimic the behavior from OMX, where the
956 // client specified size is only used to ask for bigger buffers than component suggested
957 // size.
958 int32_t clientInputSize = 0;
959 bool clientSpecifiedInputSize =
960 msg->findInt32(KEY_MAX_INPUT_SIZE, &clientInputSize) && clientInputSize > 0;
961 // TEMP: enforce minimum buffer size of 1MB for video decoders
962 // and 16K / 4K for audio encoders/decoders
963 if (maxInputSize.value == 0) {
964 if (config->mDomain & Config::IS_AUDIO) {
965 maxInputSize.value = encoder ? 16384 : 4096;
966 } else if (!encoder) {
967 maxInputSize.value = 1048576u;
968 }
969 }
970
971 // verify that CSD fits into this size (if defined)
972 if ((config->mDomain & Config::IS_DECODER) && maxInputSize.value > 0) {
973 sp<ABuffer> csd;
974 for (size_t ix = 0; msg->findBuffer(StringPrintf("csd-%zu", ix).c_str(), &csd); ++ix) {
975 if (csd && csd->size() > maxInputSize.value) {
976 maxInputSize.value = csd->size();
977 }
978 }
979 }
980
981 // TODO: do this based on component requiring linear allocator for input
982 if ((config->mDomain & Config::IS_DECODER) || (config->mDomain & Config::IS_AUDIO)) {
983 if (clientSpecifiedInputSize) {
984 // Warn that we're overriding client's max input size if necessary.
985 if ((uint32_t)clientInputSize < maxInputSize.value) {
986 ALOGD("client requested max input size %d, which is smaller than "
987 "what component recommended (%u); overriding with component "
988 "recommendation.", clientInputSize, maxInputSize.value);
989 ALOGW("This behavior is subject to change. It is recommended that "
990 "app developers double check whether the requested "
991 "max input size is in reasonable range.");
992 } else {
993 maxInputSize.value = clientInputSize;
994 }
995 }
996 // Pass max input size on input format to the buffer channel (if supplied by the
997 // component or by a default)
998 if (maxInputSize.value) {
999 config->mInputFormat->setInt32(
1000 KEY_MAX_INPUT_SIZE,
1001 (int32_t)(c2_min(maxInputSize.value, uint32_t(INT32_MAX))));
1002 }
1003 }
1004
Wonsik Kim9ca01d32019-04-01 14:45:47 -07001005 int32_t clientPrepend;
1006 if ((config->mDomain & Config::IS_VIDEO)
1007 && (config->mDomain & Config::IS_ENCODER)
1008 && msg->findInt32(KEY_PREPEND_HEADERS_TO_SYNC_FRAMES, &clientPrepend)
1009 && clientPrepend
1010 && (!prepend || prepend.value != PREPEND_HEADER_TO_ALL_SYNC)) {
1011 ALOGE("Failed to set KEY_PREPEND_HEADERS_TO_SYNC_FRAMES");
1012 return BAD_VALUE;
1013 }
1014
Pawin Vongmasa36653902018-11-15 00:10:25 -08001015 if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))) {
1016 // propagate HDR static info to output format for both encoders and decoders
1017 // if component supports this info, we will update from component, but only the raw port,
1018 // so don't propagate if component already filled it in.
1019 sp<ABuffer> hdrInfo;
1020 if (msg->findBuffer(KEY_HDR_STATIC_INFO, &hdrInfo)
1021 && !config->mOutputFormat->findBuffer(KEY_HDR_STATIC_INFO, &hdrInfo)) {
1022 config->mOutputFormat->setBuffer(KEY_HDR_STATIC_INFO, hdrInfo);
1023 }
1024
1025 // Set desired color format from configuration parameter
1026 int32_t format;
1027 if (msg->findInt32("android._color-format", &format)) {
1028 if (config->mDomain & Config::IS_ENCODER) {
1029 config->mInputFormat->setInt32(KEY_COLOR_FORMAT, format);
1030 } else {
1031 config->mOutputFormat->setInt32(KEY_COLOR_FORMAT, format);
1032 }
1033 }
1034 }
1035
1036 // propagate encoder delay and padding to output format
1037 if ((config->mDomain & Config::IS_DECODER) && (config->mDomain & Config::IS_AUDIO)) {
1038 int delay = 0;
1039 if (msg->findInt32("encoder-delay", &delay)) {
1040 config->mOutputFormat->setInt32("encoder-delay", delay);
1041 }
1042 int padding = 0;
1043 if (msg->findInt32("encoder-padding", &padding)) {
1044 config->mOutputFormat->setInt32("encoder-padding", padding);
1045 }
1046 }
1047
1048 // set channel-mask
1049 if (config->mDomain & Config::IS_AUDIO) {
1050 int32_t mask;
1051 if (msg->findInt32(KEY_CHANNEL_MASK, &mask)) {
1052 if (config->mDomain & Config::IS_ENCODER) {
1053 config->mInputFormat->setInt32(KEY_CHANNEL_MASK, mask);
1054 } else {
1055 config->mOutputFormat->setInt32(KEY_CHANNEL_MASK, mask);
1056 }
1057 }
1058 }
1059
1060 ALOGD("setup formats input: %s and output: %s",
1061 config->mInputFormat->debugString().c_str(),
1062 config->mOutputFormat->debugString().c_str());
1063 return OK;
1064 };
1065 if (tryAndReportOnError(doConfig) != OK) {
1066 return;
1067 }
1068
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001069 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1070 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001071
1072 mCallback->onComponentConfigured(config->mInputFormat, config->mOutputFormat);
1073}
1074
1075void CCodec::initiateCreateInputSurface() {
1076 status_t err = [this] {
1077 Mutexed<State>::Locked state(mState);
1078 if (state->get() != ALLOCATED) {
1079 return UNKNOWN_ERROR;
1080 }
1081 // TODO: read it from intf() properly.
1082 if (state->comp->getName().find("encoder") == std::string::npos) {
1083 return INVALID_OPERATION;
1084 }
1085 return OK;
1086 }();
1087 if (err != OK) {
1088 mCallback->onInputSurfaceCreationFailed(err);
1089 return;
1090 }
1091
1092 (new AMessage(kWhatCreateInputSurface, this))->post();
1093}
1094
Lajos Molnar47118272019-01-31 16:28:04 -08001095sp<PersistentSurface> CCodec::CreateOmxInputSurface() {
1096 using namespace android::hardware::media::omx::V1_0;
1097 using namespace android::hardware::media::omx::V1_0::utils;
1098 using namespace android::hardware::graphics::bufferqueue::V1_0::utils;
1099 typedef android::hardware::media::omx::V1_0::Status OmxStatus;
1100 android::sp<IOmx> omx = IOmx::getService();
1101 typedef android::hardware::graphics::bufferqueue::V1_0::
1102 IGraphicBufferProducer HGraphicBufferProducer;
1103 typedef android::hardware::media::omx::V1_0::
1104 IGraphicBufferSource HGraphicBufferSource;
1105 OmxStatus s;
1106 android::sp<HGraphicBufferProducer> gbp;
1107 android::sp<HGraphicBufferSource> gbs;
Pawin Vongmasa18588322019-05-18 01:52:13 -07001108
Chong Zhangc8ce1d82019-03-27 10:18:38 -07001109 using ::android::hardware::Return;
1110 Return<void> transStatus = omx->createInputSurface(
Lajos Molnar47118272019-01-31 16:28:04 -08001111 [&s, &gbp, &gbs](
1112 OmxStatus status,
1113 const android::sp<HGraphicBufferProducer>& producer,
1114 const android::sp<HGraphicBufferSource>& source) {
1115 s = status;
1116 gbp = producer;
1117 gbs = source;
1118 });
1119 if (transStatus.isOk() && s == OmxStatus::OK) {
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001120 return new PersistentSurface(new H2BGraphicBufferProducer(gbp), gbs);
Lajos Molnar47118272019-01-31 16:28:04 -08001121 }
1122
1123 return nullptr;
1124}
1125
1126sp<PersistentSurface> CCodec::CreateCompatibleInputSurface() {
1127 sp<PersistentSurface> surface(CreateInputSurface());
1128
1129 if (surface == nullptr) {
1130 surface = CreateOmxInputSurface();
1131 }
1132
1133 return surface;
1134}
1135
Pawin Vongmasa36653902018-11-15 00:10:25 -08001136void CCodec::createInputSurface() {
1137 status_t err;
1138 sp<IGraphicBufferProducer> bufferProducer;
1139
1140 sp<AMessage> inputFormat;
1141 sp<AMessage> outputFormat;
Wonsik Kim9eac4d12019-05-23 12:58:48 -07001142 uint64_t usage = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001143 {
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001144 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1145 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001146 inputFormat = config->mInputFormat;
1147 outputFormat = config->mOutputFormat;
Wonsik Kim9eac4d12019-05-23 12:58:48 -07001148 usage = config->mISConfig ? config->mISConfig->mUsage : 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001149 }
1150
Lajos Molnar47118272019-01-31 16:28:04 -08001151 sp<PersistentSurface> persistentSurface = CreateCompatibleInputSurface();
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001152 sp<hidl::base::V1_0::IBase> hidlTarget = persistentSurface->getHidlTarget();
1153 sp<IInputSurface> hidlInputSurface = IInputSurface::castFrom(hidlTarget);
1154 sp<HGraphicBufferSource> gbs = HGraphicBufferSource::castFrom(hidlTarget);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001155
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001156 if (hidlInputSurface) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001157 std::shared_ptr<Codec2Client::InputSurface> inputSurface =
1158 std::make_shared<Codec2Client::InputSurface>(hidlInputSurface);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001159 err = setupInputSurface(std::make_shared<C2InputSurfaceWrapper>(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001160 inputSurface));
1161 bufferProducer = inputSurface->getGraphicBufferProducer();
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001162 } else if (gbs) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001163 int32_t width = 0;
1164 (void)outputFormat->findInt32("width", &width);
1165 int32_t height = 0;
1166 (void)outputFormat->findInt32("height", &height);
1167 err = setupInputSurface(std::make_shared<GraphicBufferSourceWrapper>(
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001168 gbs, width, height, usage));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001169 bufferProducer = persistentSurface->getBufferProducer();
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001170 } else {
1171 ALOGE("Corrupted input surface");
1172 mCallback->onInputSurfaceCreationFailed(UNKNOWN_ERROR);
1173 return;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001174 }
1175
1176 if (err != OK) {
1177 ALOGE("Failed to set up input surface: %d", err);
1178 mCallback->onInputSurfaceCreationFailed(err);
1179 return;
1180 }
1181
1182 mCallback->onInputSurfaceCreated(
1183 inputFormat,
1184 outputFormat,
1185 new BufferProducerWrapper(bufferProducer));
1186}
1187
1188status_t CCodec::setupInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface) {
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001189 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1190 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001191 config->mUsingSurface = true;
1192
1193 // we are now using surface - apply default color aspects to input format - as well as
1194 // get dataspace
1195 bool inputFormatChanged = config->updateFormats(config->IS_INPUT);
1196 ALOGD("input format %s to %s",
1197 inputFormatChanged ? "changed" : "unchanged",
1198 config->mInputFormat->debugString().c_str());
1199
1200 // configure dataspace
1201 static_assert(sizeof(int32_t) == sizeof(android_dataspace), "dataspace size mismatch");
1202 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
1203 (void)config->mInputFormat->findInt32("android._dataspace", (int32_t*)&dataSpace);
1204 surface->setDataSpace(dataSpace);
1205
1206 status_t err = mChannel->setInputSurface(surface);
1207 if (err != OK) {
1208 // undo input format update
1209 config->mUsingSurface = false;
1210 (void)config->updateFormats(config->IS_INPUT);
1211 return err;
1212 }
1213 config->mInputSurface = surface;
1214
1215 if (config->mISConfig) {
1216 surface->configure(*config->mISConfig);
1217 } else {
1218 ALOGD("ISConfig: no configuration");
1219 }
1220
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001221 return OK;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001222}
1223
1224void CCodec::initiateSetInputSurface(const sp<PersistentSurface> &surface) {
1225 sp<AMessage> msg = new AMessage(kWhatSetInputSurface, this);
1226 msg->setObject("surface", surface);
1227 msg->post();
1228}
1229
1230void CCodec::setInputSurface(const sp<PersistentSurface> &surface) {
1231 sp<AMessage> inputFormat;
1232 sp<AMessage> outputFormat;
Wonsik Kim9eac4d12019-05-23 12:58:48 -07001233 uint64_t usage = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001234 {
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001235 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1236 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001237 inputFormat = config->mInputFormat;
1238 outputFormat = config->mOutputFormat;
Wonsik Kim9eac4d12019-05-23 12:58:48 -07001239 usage = config->mISConfig ? config->mISConfig->mUsage : 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001240 }
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001241 sp<hidl::base::V1_0::IBase> hidlTarget = surface->getHidlTarget();
1242 sp<IInputSurface> inputSurface = IInputSurface::castFrom(hidlTarget);
1243 sp<HGraphicBufferSource> gbs = HGraphicBufferSource::castFrom(hidlTarget);
1244 if (inputSurface) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001245 status_t err = setupInputSurface(std::make_shared<C2InputSurfaceWrapper>(
1246 std::make_shared<Codec2Client::InputSurface>(inputSurface)));
1247 if (err != OK) {
1248 ALOGE("Failed to set up input surface: %d", err);
1249 mCallback->onInputSurfaceDeclined(err);
1250 return;
1251 }
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001252 } else if (gbs) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001253 int32_t width = 0;
1254 (void)outputFormat->findInt32("width", &width);
1255 int32_t height = 0;
1256 (void)outputFormat->findInt32("height", &height);
1257 status_t err = setupInputSurface(std::make_shared<GraphicBufferSourceWrapper>(
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001258 gbs, width, height, usage));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001259 if (err != OK) {
1260 ALOGE("Failed to set up input surface: %d", err);
1261 mCallback->onInputSurfaceDeclined(err);
1262 return;
1263 }
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001264 } else {
1265 ALOGE("Failed to set input surface: Corrupted surface.");
1266 mCallback->onInputSurfaceDeclined(UNKNOWN_ERROR);
1267 return;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001268 }
1269 mCallback->onInputSurfaceAccepted(inputFormat, outputFormat);
1270}
1271
1272void CCodec::initiateStart() {
1273 auto setStarting = [this] {
1274 Mutexed<State>::Locked state(mState);
1275 if (state->get() != ALLOCATED) {
1276 return UNKNOWN_ERROR;
1277 }
1278 state->set(STARTING);
1279 return OK;
1280 };
1281 if (tryAndReportOnError(setStarting) != OK) {
1282 return;
1283 }
1284
1285 (new AMessage(kWhatStart, this))->post();
1286}
1287
1288void CCodec::start() {
1289 std::shared_ptr<Codec2Client::Component> comp;
1290 auto checkStarting = [this, &comp] {
1291 Mutexed<State>::Locked state(mState);
1292 if (state->get() != STARTING) {
1293 return UNKNOWN_ERROR;
1294 }
1295 comp = state->comp;
1296 return OK;
1297 };
1298 if (tryAndReportOnError(checkStarting) != OK) {
1299 return;
1300 }
1301
1302 c2_status_t err = comp->start();
1303 if (err != C2_OK) {
1304 mCallback->onError(toStatusT(err, C2_OPERATION_Component_start),
1305 ACTION_CODE_FATAL);
1306 return;
1307 }
1308 sp<AMessage> inputFormat;
1309 sp<AMessage> outputFormat;
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001310 status_t err2 = OK;
Wonsik Kimfb7a7672019-12-27 17:13:33 -08001311 bool buffersBoundToCodec = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001312 {
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001313 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1314 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001315 inputFormat = config->mInputFormat;
1316 outputFormat = config->mOutputFormat;
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001317 if (config->mInputSurface) {
1318 err2 = config->mInputSurface->start();
1319 }
Wonsik Kimfb7a7672019-12-27 17:13:33 -08001320 buffersBoundToCodec = config->mBuffersBoundToCodec;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001321 }
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001322 if (err2 != OK) {
1323 mCallback->onError(err2, ACTION_CODE_FATAL);
1324 return;
1325 }
Wonsik Kimfb7a7672019-12-27 17:13:33 -08001326 err2 = mChannel->start(inputFormat, outputFormat, buffersBoundToCodec);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001327 if (err2 != OK) {
1328 mCallback->onError(err2, ACTION_CODE_FATAL);
1329 return;
1330 }
1331
1332 auto setRunning = [this] {
1333 Mutexed<State>::Locked state(mState);
1334 if (state->get() != STARTING) {
1335 return UNKNOWN_ERROR;
1336 }
1337 state->set(RUNNING);
1338 return OK;
1339 };
1340 if (tryAndReportOnError(setRunning) != OK) {
1341 return;
1342 }
1343 mCallback->onStartCompleted();
1344
1345 (void)mChannel->requestInitialInputBuffers();
1346}
1347
1348void CCodec::initiateShutdown(bool keepComponentAllocated) {
1349 if (keepComponentAllocated) {
1350 initiateStop();
1351 } else {
1352 initiateRelease();
1353 }
1354}
1355
1356void CCodec::initiateStop() {
1357 {
1358 Mutexed<State>::Locked state(mState);
1359 if (state->get() == ALLOCATED
1360 || state->get() == RELEASED
1361 || state->get() == STOPPING
1362 || state->get() == RELEASING) {
1363 // We're already stopped, released, or doing it right now.
1364 state.unlock();
1365 mCallback->onStopCompleted();
1366 state.lock();
1367 return;
1368 }
1369 state->set(STOPPING);
1370 }
1371
1372 mChannel->stop();
1373 (new AMessage(kWhatStop, this))->post();
1374}
1375
1376void CCodec::stop() {
1377 std::shared_ptr<Codec2Client::Component> comp;
1378 {
1379 Mutexed<State>::Locked state(mState);
1380 if (state->get() == RELEASING) {
1381 state.unlock();
1382 // We're already stopped or release is in progress.
1383 mCallback->onStopCompleted();
1384 state.lock();
1385 return;
1386 } else if (state->get() != STOPPING) {
1387 state.unlock();
1388 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1389 state.lock();
1390 return;
1391 }
1392 comp = state->comp;
1393 }
1394 status_t err = comp->stop();
1395 if (err != C2_OK) {
1396 // TODO: convert err into status_t
1397 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1398 }
1399
1400 {
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001401 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1402 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001403 if (config->mInputSurface) {
1404 config->mInputSurface->disconnect();
1405 config->mInputSurface = nullptr;
1406 }
1407 }
1408 {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001409 Mutexed<State>::Locked state(mState);
1410 if (state->get() == STOPPING) {
1411 state->set(ALLOCATED);
1412 }
1413 }
1414 mCallback->onStopCompleted();
1415}
1416
1417void CCodec::initiateRelease(bool sendCallback /* = true */) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001418 bool clearInputSurfaceIfNeeded = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001419 {
1420 Mutexed<State>::Locked state(mState);
1421 if (state->get() == RELEASED || state->get() == RELEASING) {
1422 // We're already released or doing it right now.
1423 if (sendCallback) {
1424 state.unlock();
1425 mCallback->onReleaseCompleted();
1426 state.lock();
1427 }
1428 return;
1429 }
1430 if (state->get() == ALLOCATING) {
1431 state->set(RELEASING);
1432 // With the altered state allocate() would fail and clean up.
1433 if (sendCallback) {
1434 state.unlock();
1435 mCallback->onReleaseCompleted();
1436 state.lock();
1437 }
1438 return;
1439 }
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001440 if (state->get() == STARTING
1441 || state->get() == RUNNING
1442 || state->get() == STOPPING) {
1443 // Input surface may have been started, so clean up is needed.
1444 clearInputSurfaceIfNeeded = true;
1445 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001446 state->set(RELEASING);
1447 }
1448
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001449 if (clearInputSurfaceIfNeeded) {
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001450 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1451 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001452 if (config->mInputSurface) {
1453 config->mInputSurface->disconnect();
1454 config->mInputSurface = nullptr;
1455 }
1456 }
1457
Pawin Vongmasa36653902018-11-15 00:10:25 -08001458 mChannel->stop();
1459 // thiz holds strong ref to this while the thread is running.
1460 sp<CCodec> thiz(this);
1461 std::thread([thiz, sendCallback] { thiz->release(sendCallback); }).detach();
1462}
1463
1464void CCodec::release(bool sendCallback) {
1465 std::shared_ptr<Codec2Client::Component> comp;
1466 {
1467 Mutexed<State>::Locked state(mState);
1468 if (state->get() == RELEASED) {
1469 if (sendCallback) {
1470 state.unlock();
1471 mCallback->onReleaseCompleted();
1472 state.lock();
1473 }
1474 return;
1475 }
1476 comp = state->comp;
1477 }
1478 comp->release();
1479
1480 {
1481 Mutexed<State>::Locked state(mState);
1482 state->set(RELEASED);
1483 state->comp.reset();
1484 }
1485 if (sendCallback) {
1486 mCallback->onReleaseCompleted();
1487 }
1488}
1489
1490status_t CCodec::setSurface(const sp<Surface> &surface) {
1491 return mChannel->setSurface(surface);
1492}
1493
1494void CCodec::signalFlush() {
1495 status_t err = [this] {
1496 Mutexed<State>::Locked state(mState);
1497 if (state->get() == FLUSHED) {
1498 return ALREADY_EXISTS;
1499 }
1500 if (state->get() != RUNNING) {
1501 return UNKNOWN_ERROR;
1502 }
1503 state->set(FLUSHING);
1504 return OK;
1505 }();
1506 switch (err) {
1507 case ALREADY_EXISTS:
1508 mCallback->onFlushCompleted();
1509 return;
1510 case OK:
1511 break;
1512 default:
1513 mCallback->onError(err, ACTION_CODE_FATAL);
1514 return;
1515 }
1516
1517 mChannel->stop();
1518 (new AMessage(kWhatFlush, this))->post();
1519}
1520
1521void CCodec::flush() {
1522 std::shared_ptr<Codec2Client::Component> comp;
1523 auto checkFlushing = [this, &comp] {
1524 Mutexed<State>::Locked state(mState);
1525 if (state->get() != FLUSHING) {
1526 return UNKNOWN_ERROR;
1527 }
1528 comp = state->comp;
1529 return OK;
1530 };
1531 if (tryAndReportOnError(checkFlushing) != OK) {
1532 return;
1533 }
1534
1535 std::list<std::unique_ptr<C2Work>> flushedWork;
1536 c2_status_t err = comp->flush(C2Component::FLUSH_COMPONENT, &flushedWork);
1537 {
1538 Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue);
1539 flushedWork.splice(flushedWork.end(), *queue);
1540 }
1541 if (err != C2_OK) {
1542 // TODO: convert err into status_t
1543 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1544 }
1545
1546 mChannel->flush(flushedWork);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001547
1548 {
1549 Mutexed<State>::Locked state(mState);
Iris Changce521ee2019-07-05 16:18:54 +08001550 if (state->get() == FLUSHING) {
1551 state->set(FLUSHED);
1552 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001553 }
1554 mCallback->onFlushCompleted();
1555}
1556
1557void CCodec::signalResume() {
1558 auto setResuming = [this] {
1559 Mutexed<State>::Locked state(mState);
1560 if (state->get() != FLUSHED) {
1561 return UNKNOWN_ERROR;
1562 }
1563 state->set(RESUMING);
1564 return OK;
1565 };
1566 if (tryAndReportOnError(setResuming) != OK) {
1567 return;
1568 }
1569
Wonsik Kimfb7a7672019-12-27 17:13:33 -08001570 (void)mChannel->start(nullptr, nullptr, [&]{
1571 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1572 const std::unique_ptr<Config> &config = *configLocked;
1573 return config->mBuffersBoundToCodec;
1574 }());
Pawin Vongmasa36653902018-11-15 00:10:25 -08001575
1576 {
1577 Mutexed<State>::Locked state(mState);
1578 if (state->get() != RESUMING) {
1579 state.unlock();
1580 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1581 state.lock();
1582 return;
1583 }
1584 state->set(RUNNING);
1585 }
1586
1587 (void)mChannel->requestInitialInputBuffers();
1588}
1589
Wonsik Kimaa484ac2019-02-13 16:54:02 -08001590void CCodec::signalSetParameters(const sp<AMessage> &msg) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001591 std::shared_ptr<Codec2Client::Component> comp;
1592 auto checkState = [this, &comp] {
1593 Mutexed<State>::Locked state(mState);
1594 if (state->get() == RELEASED) {
1595 return INVALID_OPERATION;
1596 }
1597 comp = state->comp;
1598 return OK;
1599 };
1600 if (tryAndReportOnError(checkState) != OK) {
1601 return;
1602 }
1603
Wonsik Kimaa484ac2019-02-13 16:54:02 -08001604 // NOTE: We used to ignore "bitrate" at setParameters; replicate
1605 // the behavior here.
1606 sp<AMessage> params = msg;
1607 int32_t bitrate;
1608 if (params->findInt32(KEY_BIT_RATE, &bitrate)) {
1609 params = msg->dup();
1610 params->removeEntryAt(params->findEntryByName(KEY_BIT_RATE));
1611 }
1612
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001613 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1614 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001615
1616 /**
1617 * Handle input surface parameters
1618 */
1619 if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))
1620 && (config->mDomain & Config::IS_ENCODER) && config->mInputSurface && config->mISConfig) {
Chong Zhang038e8f82019-02-06 19:05:14 -08001621 (void)params->findInt64(PARAMETER_KEY_OFFSET_TIME, &config->mISConfig->mTimeOffsetUs);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001622
1623 if (params->findInt64("skip-frames-before", &config->mISConfig->mStartAtUs)) {
1624 config->mISConfig->mStopped = false;
1625 } else if (params->findInt64("stop-time-us", &config->mISConfig->mStopAtUs)) {
1626 config->mISConfig->mStopped = true;
1627 }
1628
1629 int32_t value;
Chong Zhang038e8f82019-02-06 19:05:14 -08001630 if (params->findInt32(PARAMETER_KEY_SUSPEND, &value)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001631 config->mISConfig->mSuspended = value;
1632 config->mISConfig->mSuspendAtUs = -1;
Chong Zhang038e8f82019-02-06 19:05:14 -08001633 (void)params->findInt64(PARAMETER_KEY_SUSPEND_TIME, &config->mISConfig->mSuspendAtUs);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001634 }
1635
1636 (void)config->mInputSurface->configure(*config->mISConfig);
1637 if (config->mISConfig->mStopped) {
1638 config->mInputFormat->setInt64(
1639 "android._stop-time-offset-us", config->mISConfig->mInputDelayUs);
1640 }
1641 }
1642
1643 std::vector<std::unique_ptr<C2Param>> configUpdate;
1644 (void)config->getConfigUpdateFromSdkParams(
1645 comp, params, Config::IS_PARAM, C2_MAY_BLOCK, &configUpdate);
1646 // Prefer to pass parameters to the buffer channel, so they can be synchronized with the frames.
1647 // Parameter synchronization is not defined when using input surface. For now, route
1648 // these directly to the component.
1649 if (config->mInputSurface == nullptr
1650 && (property_get_bool("debug.stagefright.ccodec_delayed_params", false)
1651 || comp->getName().find("c2.android.") == 0)) {
1652 mChannel->setParameters(configUpdate);
1653 } else {
1654 (void)config->setParameters(comp, configUpdate, C2_MAY_BLOCK);
1655 }
1656}
1657
1658void CCodec::signalEndOfInputStream() {
1659 mCallback->onSignaledInputEOS(mChannel->signalEndOfInputStream());
1660}
1661
1662void CCodec::signalRequestIDRFrame() {
1663 std::shared_ptr<Codec2Client::Component> comp;
1664 {
1665 Mutexed<State>::Locked state(mState);
1666 if (state->get() == RELEASED) {
1667 ALOGD("no IDR request sent since component is released");
1668 return;
1669 }
1670 comp = state->comp;
1671 }
1672 ALOGV("request IDR");
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001673 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1674 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001675 std::vector<std::unique_ptr<C2Param>> params;
1676 params.push_back(
1677 std::make_unique<C2StreamRequestSyncFrameTuning::output>(0u, true));
1678 config->setParameters(comp, params, C2_MAY_BLOCK);
1679}
1680
Wonsik Kimab34ed62019-01-31 15:28:46 -08001681void CCodec::onWorkDone(std::list<std::unique_ptr<C2Work>> &workItems) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001682 if (!workItems.empty()) {
Wonsik Kimab34ed62019-01-31 15:28:46 -08001683 Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue);
1684 queue->splice(queue->end(), workItems);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001685 }
1686 (new AMessage(kWhatWorkDone, this))->post();
1687}
1688
Wonsik Kimab34ed62019-01-31 15:28:46 -08001689void CCodec::onInputBufferDone(uint64_t frameIndex, size_t arrayIndex) {
1690 mChannel->onInputBufferDone(frameIndex, arrayIndex);
Wonsik Kim4f3314d2019-03-26 17:00:34 -07001691 if (arrayIndex == 0) {
1692 // We always put no more than one buffer per work, if we use an input surface.
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001693 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1694 const std::unique_ptr<Config> &config = *configLocked;
Wonsik Kim4f3314d2019-03-26 17:00:34 -07001695 if (config->mInputSurface) {
1696 config->mInputSurface->onInputBufferDone(frameIndex);
1697 }
1698 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001699}
1700
1701void CCodec::onMessageReceived(const sp<AMessage> &msg) {
1702 TimePoint now = std::chrono::steady_clock::now();
1703 CCodecWatchdog::getInstance()->watch(this);
1704 switch (msg->what()) {
1705 case kWhatAllocate: {
1706 // C2ComponentStore::createComponent() should return within 100ms.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07001707 setDeadline(now, 1500ms, "allocate");
Pawin Vongmasa36653902018-11-15 00:10:25 -08001708 sp<RefBase> obj;
1709 CHECK(msg->findObject("codecInfo", &obj));
1710 allocate((MediaCodecInfo *)obj.get());
1711 break;
1712 }
1713 case kWhatConfigure: {
1714 // C2Component::commit_sm() should return within 5ms.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07001715 setDeadline(now, 1500ms, "configure");
Pawin Vongmasa36653902018-11-15 00:10:25 -08001716 sp<AMessage> format;
1717 CHECK(msg->findMessage("format", &format));
1718 configure(format);
1719 break;
1720 }
1721 case kWhatStart: {
1722 // C2Component::start() should return within 500ms.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07001723 setDeadline(now, 1500ms, "start");
Pawin Vongmasa36653902018-11-15 00:10:25 -08001724 start();
1725 break;
1726 }
1727 case kWhatStop: {
1728 // C2Component::stop() should return within 500ms.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07001729 setDeadline(now, 1500ms, "stop");
Pawin Vongmasa36653902018-11-15 00:10:25 -08001730 stop();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001731 break;
1732 }
1733 case kWhatFlush: {
1734 // C2Component::flush_sm() should return within 5ms.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07001735 setDeadline(now, 1500ms, "flush");
Pawin Vongmasa36653902018-11-15 00:10:25 -08001736 flush();
1737 break;
1738 }
1739 case kWhatCreateInputSurface: {
1740 // Surface operations may be briefly blocking.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07001741 setDeadline(now, 1500ms, "createInputSurface");
Pawin Vongmasa36653902018-11-15 00:10:25 -08001742 createInputSurface();
1743 break;
1744 }
1745 case kWhatSetInputSurface: {
1746 // Surface operations may be briefly blocking.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07001747 setDeadline(now, 1500ms, "setInputSurface");
Pawin Vongmasa36653902018-11-15 00:10:25 -08001748 sp<RefBase> obj;
1749 CHECK(msg->findObject("surface", &obj));
1750 sp<PersistentSurface> surface(static_cast<PersistentSurface *>(obj.get()));
1751 setInputSurface(surface);
1752 break;
1753 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001754 case kWhatWorkDone: {
1755 std::unique_ptr<C2Work> work;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001756 bool shouldPost = false;
1757 {
1758 Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue);
1759 if (queue->empty()) {
1760 break;
1761 }
1762 work.swap(queue->front());
1763 queue->pop_front();
1764 shouldPost = !queue->empty();
1765 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001766 if (shouldPost) {
1767 (new AMessage(kWhatWorkDone, this))->post();
1768 }
1769
Pawin Vongmasa36653902018-11-15 00:10:25 -08001770 // handle configuration changes in work done
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001771 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1772 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001773 bool changed = false;
1774 Config::Watcher<C2StreamInitDataInfo::output> initData =
1775 config->watch<C2StreamInitDataInfo::output>();
1776 if (!work->worklets.empty()
1777 && (work->worklets.front()->output.flags
1778 & C2FrameData::FLAG_DISCARD_FRAME) == 0) {
1779
1780 // copy buffer info to config
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001781 std::vector<std::unique_ptr<C2Param>> updates;
1782 for (const std::unique_ptr<C2Param> &param
1783 : work->worklets.front()->output.configUpdate) {
1784 updates.push_back(C2Param::Copy(*param));
1785 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001786 unsigned stream = 0;
1787 for (const std::shared_ptr<C2Buffer> &buf : work->worklets.front()->output.buffers) {
1788 for (const std::shared_ptr<const C2Info> &info : buf->info()) {
1789 // move all info into output-stream #0 domain
1790 updates.emplace_back(C2Param::CopyAsStream(*info, true /* output */, stream));
1791 }
1792 for (const C2ConstGraphicBlock &block : buf->data().graphicBlocks()) {
1793 // ALOGV("got output buffer with crop %u,%u+%u,%u and size %u,%u",
1794 // block.crop().left, block.crop().top,
1795 // block.crop().width, block.crop().height,
1796 // block.width(), block.height());
1797 updates.emplace_back(new C2StreamCropRectInfo::output(stream, block.crop()));
1798 updates.emplace_back(new C2StreamPictureSizeInfo::output(
Harish Mahendrakarf7c49e22019-05-24 14:19:16 -07001799 stream, block.crop().width, block.crop().height));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001800 break; // for now only do the first block
1801 }
1802 ++stream;
1803 }
1804
1805 changed = config->updateConfiguration(updates, config->mOutputDomain);
1806
1807 // copy standard infos to graphic buffers if not already present (otherwise, we
1808 // may overwrite the actual intermediate value with a final value)
1809 stream = 0;
1810 const static std::vector<C2Param::Index> stdGfxInfos = {
1811 C2StreamRotationInfo::output::PARAM_TYPE,
1812 C2StreamColorAspectsInfo::output::PARAM_TYPE,
1813 C2StreamDataSpaceInfo::output::PARAM_TYPE,
1814 C2StreamHdrStaticInfo::output::PARAM_TYPE,
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001815 C2StreamHdr10PlusInfo::output::PARAM_TYPE,
Pawin Vongmasa36653902018-11-15 00:10:25 -08001816 C2StreamPixelAspectRatioInfo::output::PARAM_TYPE,
1817 C2StreamSurfaceScalingInfo::output::PARAM_TYPE
1818 };
1819 for (const std::shared_ptr<C2Buffer> &buf : work->worklets.front()->output.buffers) {
1820 if (buf->data().graphicBlocks().size()) {
1821 for (C2Param::Index ix : stdGfxInfos) {
1822 if (!buf->hasInfo(ix)) {
1823 const C2Param *param =
1824 config->getConfigParameterValue(ix.withStream(stream));
1825 if (param) {
1826 std::shared_ptr<C2Param> info(C2Param::Copy(*param));
1827 buf->setInfo(std::static_pointer_cast<C2Info>(info));
1828 }
1829 }
1830 }
1831 }
1832 ++stream;
1833 }
1834 }
Wonsik Kim4f3314d2019-03-26 17:00:34 -07001835 if (config->mInputSurface) {
1836 config->mInputSurface->onInputBufferDone(work->input.ordinal.frameIndex);
1837 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001838 mChannel->onWorkDone(
1839 std::move(work), changed ? config->mOutputFormat : nullptr,
Wonsik Kimab34ed62019-01-31 15:28:46 -08001840 initData.hasChanged() ? initData.update().get() : nullptr);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001841 break;
1842 }
1843 case kWhatWatch: {
1844 // watch message already posted; no-op.
1845 break;
1846 }
1847 default: {
1848 ALOGE("unrecognized message");
1849 break;
1850 }
1851 }
1852 setDeadline(TimePoint::max(), 0ms, "none");
1853}
1854
1855void CCodec::setDeadline(
1856 const TimePoint &now,
1857 const std::chrono::milliseconds &timeout,
1858 const char *name) {
1859 int32_t mult = std::max(1, property_get_int32("debug.stagefright.ccodec_timeout_mult", 1));
1860 Mutexed<NamedTimePoint>::Locked deadline(mDeadline);
1861 deadline->set(now + (timeout * mult), name);
1862}
1863
1864void CCodec::initiateReleaseIfStuck() {
1865 std::string name;
1866 bool pendingDeadline = false;
Wonsik Kimab34ed62019-01-31 15:28:46 -08001867 {
1868 Mutexed<NamedTimePoint>::Locked deadline(mDeadline);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001869 if (deadline->get() < std::chrono::steady_clock::now()) {
1870 name = deadline->getName();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001871 }
1872 if (deadline->get() != TimePoint::max()) {
1873 pendingDeadline = true;
1874 }
1875 }
1876 if (name.empty()) {
Wonsik Kimab34ed62019-01-31 15:28:46 -08001877 constexpr std::chrono::steady_clock::duration kWorkDurationThreshold = 3s;
1878 std::chrono::steady_clock::duration elapsed = mChannel->elapsed();
1879 if (elapsed >= kWorkDurationThreshold) {
1880 name = "queue";
1881 }
1882 if (elapsed > 0s) {
1883 pendingDeadline = true;
1884 }
1885 }
1886 if (name.empty()) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001887 // We're not stuck.
1888 if (pendingDeadline) {
1889 // If we are not stuck yet but still has deadline coming up,
1890 // post watch message to check back later.
1891 (new AMessage(kWhatWatch, this))->post();
1892 }
1893 return;
1894 }
1895
1896 ALOGW("previous call to %s exceeded timeout", name.c_str());
1897 initiateRelease(false);
1898 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1899}
1900
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001901// static
1902PersistentSurface *CCodec::CreateInputSurface() {
Pawin Vongmasa18588322019-05-18 01:52:13 -07001903 using namespace android;
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001904 using ::android::hardware::media::omx::V1_0::implementation::TWGraphicBufferSource;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001905 // Attempt to create a Codec2's input surface.
Pawin Vongmasa18588322019-05-18 01:52:13 -07001906 std::shared_ptr<Codec2Client::InputSurface> inputSurface =
1907 Codec2Client::CreateInputSurface();
Lajos Molnar47118272019-01-31 16:28:04 -08001908 if (!inputSurface) {
Pawin Vongmasa18588322019-05-18 01:52:13 -07001909 if (property_get_int32("debug.stagefright.c2inputsurface", 0) == -1) {
1910 sp<IGraphicBufferProducer> gbp;
1911 sp<OmxGraphicBufferSource> gbs = new OmxGraphicBufferSource();
1912 status_t err = gbs->initCheck();
1913 if (err != OK) {
1914 ALOGE("Failed to create persistent input surface: error %d", err);
1915 return nullptr;
1916 }
1917 return new PersistentSurface(
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001918 gbs->getIGraphicBufferProducer(), new TWGraphicBufferSource(gbs));
Pawin Vongmasa18588322019-05-18 01:52:13 -07001919 } else {
1920 return nullptr;
1921 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001922 }
Pawin Vongmasa18588322019-05-18 01:52:13 -07001923 return new PersistentSurface(
Lajos Molnar47118272019-01-31 16:28:04 -08001924 inputSurface->getGraphicBufferProducer(),
Pawin Vongmasa18588322019-05-18 01:52:13 -07001925 static_cast<sp<android::hidl::base::V1_0::IBase>>(
Lajos Molnar47118272019-01-31 16:28:04 -08001926 inputSurface->getHalInterface()));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001927}
1928
Wonsik Kimfb7a7672019-12-27 17:13:33 -08001929static status_t GetCommonAllocatorIds(
1930 const std::vector<std::string> &names,
1931 C2Allocator::type_t type,
1932 std::set<C2Allocator::id_t> *ids) {
1933 int poolMask = GetCodec2PoolMask();
1934 C2PlatformAllocatorStore::id_t preferredLinearId = GetPreferredLinearAllocatorId(poolMask);
1935 C2Allocator::id_t defaultAllocatorId =
1936 (type == C2Allocator::LINEAR) ? preferredLinearId : C2PlatformAllocatorStore::GRALLOC;
1937
1938 ids->clear();
1939 if (names.empty()) {
1940 return OK;
1941 }
1942 std::shared_ptr<Codec2Client::Interface> intf{
1943 Codec2Client::CreateInterfaceByName(names[0].c_str())};
1944 std::vector<std::unique_ptr<C2Param>> params;
1945 c2_status_t err = intf->query(
1946 {}, {C2PortAllocatorsTuning::input::PARAM_TYPE}, C2_MAY_BLOCK, &params);
1947 if (err == C2_OK && params.size() == 1u) {
1948 C2PortAllocatorsTuning::input *allocators =
1949 C2PortAllocatorsTuning::input::From(params[0].get());
1950 if (allocators && allocators->flexCount() > 0) {
1951 ids->insert(allocators->m.values, allocators->m.values + allocators->flexCount());
1952 }
1953 }
1954 if (ids->empty()) {
1955 // The component does not advertise allocators. Use default.
1956 ids->insert(defaultAllocatorId);
1957 }
1958 for (size_t i = 1; i < names.size(); ++i) {
1959 intf = Codec2Client::CreateInterfaceByName(names[i].c_str());
1960 err = intf->query(
1961 {}, {C2PortAllocatorsTuning::input::PARAM_TYPE}, C2_MAY_BLOCK, &params);
1962 bool filtered = false;
1963 if (err == C2_OK && params.size() == 1u) {
1964 C2PortAllocatorsTuning::input *allocators =
1965 C2PortAllocatorsTuning::input::From(params[0].get());
1966 if (allocators && allocators->flexCount() > 0) {
1967 filtered = true;
1968 for (auto it = ids->begin(); it != ids->end(); ) {
1969 bool found = false;
1970 for (size_t j = 0; j < allocators->flexCount(); ++j) {
1971 if (allocators->m.values[j] == *it) {
1972 found = true;
1973 break;
1974 }
1975 }
1976 if (found) {
1977 ++it;
1978 } else {
1979 it = ids->erase(it);
1980 }
1981 }
1982 }
1983 }
1984 if (!filtered) {
1985 // The component does not advertise supported allocators. Use default.
1986 bool containsDefault = (ids->count(defaultAllocatorId) > 0u);
1987 if (ids->size() != (containsDefault ? 1 : 0)) {
1988 ids->clear();
1989 if (containsDefault) {
1990 ids->insert(defaultAllocatorId);
1991 }
1992 }
1993 }
1994 }
1995 // Finally, filter with pool masks
1996 for (auto it = ids->begin(); it != ids->end(); ) {
1997 if ((poolMask >> *it) & 1) {
1998 ++it;
1999 } else {
2000 it = ids->erase(it);
2001 }
2002 }
2003 return OK;
2004}
2005
2006static status_t CalculateMinMaxUsage(
2007 const std::vector<std::string> &names, uint64_t *minUsage, uint64_t *maxUsage) {
2008 static C2StreamUsageTuning::input sUsage{0u /* stream id */};
2009 *minUsage = 0;
2010 *maxUsage = ~0ull;
2011 for (const std::string &name : names) {
2012 std::shared_ptr<Codec2Client::Interface> intf{
2013 Codec2Client::CreateInterfaceByName(name.c_str())};
2014 std::vector<C2FieldSupportedValuesQuery> fields;
2015 fields.push_back(C2FieldSupportedValuesQuery::Possible(
2016 C2ParamField{&sUsage, &sUsage.value}));
2017 c2_status_t err = intf->querySupportedValues(fields, C2_MAY_BLOCK);
2018 if (err != C2_OK) {
2019 continue;
2020 }
2021 if (fields[0].status != C2_OK) {
2022 continue;
2023 }
2024 const C2FieldSupportedValues &supported = fields[0].values;
2025 if (supported.type != C2FieldSupportedValues::FLAGS) {
2026 continue;
2027 }
2028 if (supported.values.empty()) {
2029 *maxUsage = 0;
2030 continue;
2031 }
2032 *minUsage |= supported.values[0].u64;
2033 int64_t currentMaxUsage = 0;
2034 for (const C2Value::Primitive &flags : supported.values) {
2035 currentMaxUsage |= flags.u64;
2036 }
2037 *maxUsage &= currentMaxUsage;
2038 }
2039 return OK;
2040}
2041
2042// static
2043status_t CCodec::CanFetchLinearBlock(
2044 const std::vector<std::string> &names, const C2MemoryUsage &usage, bool *isCompatible) {
2045 uint64_t minUsage = usage.expected;
2046 uint64_t maxUsage = ~0ull;
2047 std::set<C2Allocator::id_t> allocators;
2048 GetCommonAllocatorIds(names, C2Allocator::LINEAR, &allocators);
2049 if (allocators.empty()) {
2050 *isCompatible = false;
2051 return OK;
2052 }
2053 CalculateMinMaxUsage(names, &minUsage, &maxUsage);
2054 *isCompatible = ((maxUsage & minUsage) == minUsage);
2055 return OK;
2056}
2057
2058static std::shared_ptr<C2BlockPool> GetPool(C2Allocator::id_t allocId) {
2059 static std::mutex sMutex{};
2060 static std::map<C2Allocator::id_t, std::shared_ptr<C2BlockPool>> sPools;
2061 std::unique_lock<std::mutex> lock{sMutex};
2062 std::shared_ptr<C2BlockPool> pool;
2063 auto it = sPools.find(allocId);
2064 if (it == sPools.end()) {
2065 c2_status_t err = CreateCodec2BlockPool(allocId, nullptr, &pool);
2066 if (err == OK) {
2067 sPools.emplace(allocId, pool);
2068 } else {
2069 pool.reset();
2070 }
2071 } else {
2072 pool = it->second;
2073 }
2074 return pool;
2075}
2076
2077// static
2078std::shared_ptr<C2LinearBlock> CCodec::FetchLinearBlock(
2079 size_t capacity, const C2MemoryUsage &usage, const std::vector<std::string> &names) {
2080 uint64_t minUsage = usage.expected;
2081 uint64_t maxUsage = ~0ull;
2082 std::set<C2Allocator::id_t> allocators;
2083 GetCommonAllocatorIds(names, C2Allocator::LINEAR, &allocators);
2084 if (allocators.empty()) {
2085 allocators.insert(C2PlatformAllocatorStore::DEFAULT_LINEAR);
2086 }
2087 CalculateMinMaxUsage(names, &minUsage, &maxUsage);
2088 if ((maxUsage & minUsage) != minUsage) {
2089 allocators.clear();
2090 allocators.insert(C2PlatformAllocatorStore::DEFAULT_LINEAR);
2091 }
2092 std::shared_ptr<C2LinearBlock> block;
2093 for (C2Allocator::id_t allocId : allocators) {
2094 std::shared_ptr<C2BlockPool> pool = GetPool(allocId);
2095 if (!pool) {
2096 continue;
2097 }
2098 c2_status_t err = pool->fetchLinearBlock(capacity, C2MemoryUsage{minUsage}, &block);
2099 if (err != C2_OK || !block) {
2100 block.reset();
2101 continue;
2102 }
2103 break;
2104 }
2105 return block;
2106}
2107
2108// static
2109status_t CCodec::CanFetchGraphicBlock(
2110 const std::vector<std::string> &names, bool *isCompatible) {
2111 uint64_t minUsage = 0;
2112 uint64_t maxUsage = ~0ull;
2113 std::set<C2Allocator::id_t> allocators;
2114 GetCommonAllocatorIds(names, C2Allocator::GRAPHIC, &allocators);
2115 if (allocators.empty()) {
2116 *isCompatible = false;
2117 return OK;
2118 }
2119 CalculateMinMaxUsage(names, &minUsage, &maxUsage);
2120 *isCompatible = ((maxUsage & minUsage) == minUsage);
2121 return OK;
2122}
2123
2124// static
2125std::shared_ptr<C2GraphicBlock> CCodec::FetchGraphicBlock(
2126 int32_t width,
2127 int32_t height,
2128 int32_t format,
2129 uint64_t usage,
2130 const std::vector<std::string> &names) {
2131 uint32_t halPixelFormat = HAL_PIXEL_FORMAT_YCBCR_420_888;
2132 if (!C2Mapper::mapPixelFormatFrameworkToCodec(format, &halPixelFormat)) {
2133 ALOGD("Unrecognized pixel format: %d", format);
2134 return nullptr;
2135 }
2136 uint64_t minUsage = 0;
2137 uint64_t maxUsage = ~0ull;
2138 std::set<C2Allocator::id_t> allocators;
2139 GetCommonAllocatorIds(names, C2Allocator::GRAPHIC, &allocators);
2140 if (allocators.empty()) {
2141 allocators.insert(C2PlatformAllocatorStore::DEFAULT_GRAPHIC);
2142 }
2143 CalculateMinMaxUsage(names, &minUsage, &maxUsage);
2144 minUsage |= usage;
2145 if ((maxUsage & minUsage) != minUsage) {
2146 allocators.clear();
2147 allocators.insert(C2PlatformAllocatorStore::DEFAULT_GRAPHIC);
2148 }
2149 std::shared_ptr<C2GraphicBlock> block;
2150 for (C2Allocator::id_t allocId : allocators) {
2151 std::shared_ptr<C2BlockPool> pool;
2152 c2_status_t err = CreateCodec2BlockPool(allocId, nullptr, &pool);
2153 if (err != C2_OK || !pool) {
2154 continue;
2155 }
2156 err = pool->fetchGraphicBlock(
2157 width, height, halPixelFormat, C2MemoryUsage{minUsage}, &block);
2158 if (err != C2_OK || !block) {
2159 block.reset();
2160 continue;
2161 }
2162 break;
2163 }
2164 return block;
2165}
2166
Wonsik Kim155d5cb2019-10-09 12:49:49 -07002167} // namespace android
2168