blob: 96af7cbf762ac5541b3406ed00e1bbae70e289a6 [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
2 * Copyright 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 "CCodecBufferChannel"
19#include <utils/Log.h>
20
Pawin Vongmasae7bb8612020-06-04 06:15:22 -070021#include <algorithm>
22#include <list>
Pawin Vongmasa36653902018-11-15 00:10:25 -080023#include <numeric>
24
25#include <C2AllocatorGralloc.h>
26#include <C2PlatformSupport.h>
27#include <C2BlockInternal.h>
28#include <C2Config.h>
29#include <C2Debug.h>
30
31#include <android/hardware/cas/native/1.0/IDescrambler.h>
Robert Shih895fba92019-07-16 16:29:44 -070032#include <android/hardware/drm/1.0/types.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080033#include <android-base/stringprintf.h>
Wonsik Kimfb7a7672019-12-27 17:13:33 -080034#include <binder/MemoryBase.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080035#include <binder/MemoryDealer.h>
Ray Essick18ea0452019-08-27 16:07:27 -070036#include <cutils/properties.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080037#include <gui/Surface.h>
Robert Shih895fba92019-07-16 16:29:44 -070038#include <hidlmemory/FrameworkUtils.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080039#include <media/openmax/OMX_Core.h>
40#include <media/stagefright/foundation/ABuffer.h>
41#include <media/stagefright/foundation/ALookup.h>
42#include <media/stagefright/foundation/AMessage.h>
43#include <media/stagefright/foundation/AUtils.h>
44#include <media/stagefright/foundation/hexdump.h>
45#include <media/stagefright/MediaCodec.h>
46#include <media/stagefright/MediaCodecConstants.h>
Wonsik Kim155d5cb2019-10-09 12:49:49 -070047#include <media/stagefright/SkipCutBuffer.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080048#include <media/MediaCodecBuffer.h>
Wonsik Kim41d83432020-04-27 16:40:49 -070049#include <mediadrm/ICrypto.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080050#include <system/window.h>
51
52#include "CCodecBufferChannel.h"
53#include "Codec2Buffer.h"
Pawin Vongmasa36653902018-11-15 00:10:25 -080054
55namespace android {
56
57using android::base::StringPrintf;
58using hardware::hidl_handle;
59using hardware::hidl_string;
60using hardware::hidl_vec;
Robert Shih895fba92019-07-16 16:29:44 -070061using hardware::fromHeap;
62using hardware::HidlMemory;
63
Pawin Vongmasa36653902018-11-15 00:10:25 -080064using namespace hardware::cas::V1_0;
65using namespace hardware::cas::native::V1_0;
66
67using CasStatus = hardware::cas::V1_0::Status;
Robert Shih895fba92019-07-16 16:29:44 -070068using DrmBufferType = hardware::drm::V1_0::BufferType;
Pawin Vongmasa36653902018-11-15 00:10:25 -080069
Pawin Vongmasa36653902018-11-15 00:10:25 -080070namespace {
71
Wonsik Kim469c8342019-04-11 16:46:09 -070072constexpr size_t kSmoothnessFactor = 4;
73constexpr size_t kRenderingDepth = 3;
Pawin Vongmasa36653902018-11-15 00:10:25 -080074
Sungtak Leeab6f2f32019-02-15 14:43:51 -080075// This is for keeping IGBP's buffer dropping logic in legacy mode other
76// than making it non-blocking. Do not change this value.
77const static size_t kDequeueTimeoutNs = 0;
78
Pawin Vongmasa36653902018-11-15 00:10:25 -080079} // namespace
80
81CCodecBufferChannel::QueueGuard::QueueGuard(
82 CCodecBufferChannel::QueueSync &sync) : mSync(sync) {
83 Mutex::Autolock l(mSync.mGuardLock);
84 // At this point it's guaranteed that mSync is not under state transition,
85 // as we are holding its mutex.
86
87 Mutexed<CCodecBufferChannel::QueueSync::Counter>::Locked count(mSync.mCount);
88 if (count->value == -1) {
89 mRunning = false;
90 } else {
91 ++count->value;
92 mRunning = true;
93 }
94}
95
96CCodecBufferChannel::QueueGuard::~QueueGuard() {
97 if (mRunning) {
98 // We are not holding mGuardLock at this point so that QueueSync::stop() can
99 // keep holding the lock until mCount reaches zero.
100 Mutexed<CCodecBufferChannel::QueueSync::Counter>::Locked count(mSync.mCount);
101 --count->value;
102 count->cond.broadcast();
103 }
104}
105
106void CCodecBufferChannel::QueueSync::start() {
107 Mutex::Autolock l(mGuardLock);
108 // If stopped, it goes to running state; otherwise no-op.
109 Mutexed<Counter>::Locked count(mCount);
110 if (count->value == -1) {
111 count->value = 0;
112 }
113}
114
115void CCodecBufferChannel::QueueSync::stop() {
116 Mutex::Autolock l(mGuardLock);
117 Mutexed<Counter>::Locked count(mCount);
118 if (count->value == -1) {
119 // no-op
120 return;
121 }
122 // Holding mGuardLock here blocks creation of additional QueueGuard objects, so
123 // mCount can only decrement. In other words, threads that acquired the lock
124 // are allowed to finish execution but additional threads trying to acquire
125 // the lock at this point will block, and then get QueueGuard at STOPPED
126 // state.
127 while (count->value != 0) {
128 count.waitForCondition(count->cond);
129 }
130 count->value = -1;
131}
132
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700133// Input
134
135CCodecBufferChannel::Input::Input() : extraBuffers("extra") {}
136
Pawin Vongmasa36653902018-11-15 00:10:25 -0800137// CCodecBufferChannel
138
139CCodecBufferChannel::CCodecBufferChannel(
140 const std::shared_ptr<CCodecCallback> &callback)
141 : mHeapSeqNum(-1),
142 mCCodecCallback(callback),
143 mFrameIndex(0u),
144 mFirstValidFrameIndex(0u),
145 mMetaMode(MODE_NONE),
Pawin Vongmasa36653902018-11-15 00:10:25 -0800146 mInputMetEos(false) {
Sungtak Leed7463d12019-09-04 16:01:00 -0700147 mOutputSurface.lock()->maxDequeueBuffers = kSmoothnessFactor + kRenderingDepth;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700148 {
149 Mutexed<Input>::Locked input(mInput);
150 input->buffers.reset(new DummyInputBuffers(""));
151 input->extraBuffers.flush();
152 input->inputDelay = 0u;
153 input->pipelineDelay = 0u;
154 input->numSlots = kSmoothnessFactor;
155 input->numExtraSlots = 0u;
156 }
157 {
158 Mutexed<Output>::Locked output(mOutput);
159 output->outputDelay = 0u;
160 output->numSlots = kSmoothnessFactor;
161 }
David Stevensc3fbb282021-01-18 18:11:20 +0900162 {
163 Mutexed<BlockPools>::Locked pools(mBlockPools);
164 pools->outputPoolId = C2BlockPool::BASIC_LINEAR;
165 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800166}
167
168CCodecBufferChannel::~CCodecBufferChannel() {
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800169 if (mCrypto != nullptr && mHeapSeqNum >= 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800170 mCrypto->unsetHeap(mHeapSeqNum);
171 }
172}
173
174void CCodecBufferChannel::setComponent(
175 const std::shared_ptr<Codec2Client::Component> &component) {
176 mComponent = component;
177 mComponentName = component->getName() + StringPrintf("#%d", int(uintptr_t(component.get()) % 997));
178 mName = mComponentName.c_str();
179}
180
181status_t CCodecBufferChannel::setInputSurface(
182 const std::shared_ptr<InputSurfaceWrapper> &surface) {
183 ALOGV("[%s] setInputSurface", mName);
184 mInputSurface = surface;
185 return mInputSurface->connect(mComponent);
186}
187
188status_t CCodecBufferChannel::signalEndOfInputStream() {
189 if (mInputSurface == nullptr) {
190 return INVALID_OPERATION;
191 }
192 return mInputSurface->signalEndOfInputStream();
193}
194
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700195status_t CCodecBufferChannel::queueInputBufferInternal(sp<MediaCodecBuffer> buffer) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800196 int64_t timeUs;
197 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
198
199 if (mInputMetEos) {
200 ALOGD("[%s] buffers after EOS ignored (%lld us)", mName, (long long)timeUs);
201 return OK;
202 }
203
204 int32_t flags = 0;
205 int32_t tmp = 0;
206 bool eos = false;
207 if (buffer->meta()->findInt32("eos", &tmp) && tmp) {
208 eos = true;
209 mInputMetEos = true;
210 ALOGV("[%s] input EOS", mName);
211 }
212 if (buffer->meta()->findInt32("csd", &tmp) && tmp) {
213 flags |= C2FrameData::FLAG_CODEC_CONFIG;
214 }
215 ALOGV("[%s] queueInputBuffer: buffer->size() = %zu", mName, buffer->size());
216 std::unique_ptr<C2Work> work(new C2Work);
217 work->input.ordinal.timestamp = timeUs;
218 work->input.ordinal.frameIndex = mFrameIndex++;
219 // WORKAROUND: until codecs support handling work after EOS and max output sizing, use timestamp
220 // manipulation to achieve image encoding via video codec, and to constrain encoded output.
221 // Keep client timestamp in customOrdinal
222 work->input.ordinal.customOrdinal = timeUs;
223 work->input.buffers.clear();
224
Wonsik Kimab34ed62019-01-31 15:28:46 -0800225 uint64_t queuedFrameIndex = work->input.ordinal.frameIndex.peeku();
226 std::vector<std::shared_ptr<C2Buffer>> queuedBuffers;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700227 sp<Codec2Buffer> copy;
Wonsik Kimab34ed62019-01-31 15:28:46 -0800228
Pawin Vongmasa36653902018-11-15 00:10:25 -0800229 if (buffer->size() > 0u) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700230 Mutexed<Input>::Locked input(mInput);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800231 std::shared_ptr<C2Buffer> c2buffer;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700232 if (!input->buffers->releaseBuffer(buffer, &c2buffer, false)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800233 return -ENOENT;
234 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700235 // TODO: we want to delay copying buffers.
236 if (input->extraBuffers.numComponentBuffers() < input->numExtraSlots) {
237 copy = input->buffers->cloneAndReleaseBuffer(buffer);
238 if (copy != nullptr) {
239 (void)input->extraBuffers.assignSlot(copy);
240 if (!input->extraBuffers.releaseSlot(copy, &c2buffer, false)) {
241 return UNKNOWN_ERROR;
242 }
243 bool released = input->buffers->releaseBuffer(buffer, nullptr, true);
244 ALOGV("[%s] queueInputBuffer: buffer copied; %sreleased",
245 mName, released ? "" : "not ");
246 buffer.clear();
247 } else {
248 ALOGW("[%s] queueInputBuffer: failed to copy a buffer; this may cause input "
249 "buffer starvation on component.", mName);
250 }
251 }
Byeongjo Park2eef13e2020-06-12 17:24:21 +0900252 int32_t cvo = 0;
253 if (buffer->meta()->findInt32("cvo", &cvo)) {
254 int32_t rotation = cvo % 360;
255 // change rotation to counter-clock wise.
256 rotation = ((rotation <= 0) ? 0 : 360) - rotation;
257 Mutexed<OutputSurface>::Locked output(mOutputSurface);
258 output->rotation[queuedFrameIndex] = rotation;
259 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800260 work->input.buffers.push_back(c2buffer);
Wonsik Kimab34ed62019-01-31 15:28:46 -0800261 queuedBuffers.push_back(c2buffer);
262 } else if (eos) {
263 flags |= C2FrameData::FLAG_END_OF_STREAM;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800264 }
265 work->input.flags = (C2FrameData::flags_t)flags;
266 // TODO: fill info's
267
268 work->input.configUpdate = std::move(mParamsToBeSet);
269 work->worklets.clear();
270 work->worklets.emplace_back(new C2Worklet);
271
272 std::list<std::unique_ptr<C2Work>> items;
273 items.push_back(std::move(work));
Wonsik Kimab34ed62019-01-31 15:28:46 -0800274 mPipelineWatcher.lock()->onWorkQueued(
275 queuedFrameIndex,
276 std::move(queuedBuffers),
277 PipelineWatcher::Clock::now());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800278 c2_status_t err = mComponent->queue(&items);
Wonsik Kimab34ed62019-01-31 15:28:46 -0800279 if (err != C2_OK) {
280 mPipelineWatcher.lock()->onWorkDone(queuedFrameIndex);
281 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800282
283 if (err == C2_OK && eos && buffer->size() > 0u) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800284 work.reset(new C2Work);
285 work->input.ordinal.timestamp = timeUs;
286 work->input.ordinal.frameIndex = mFrameIndex++;
287 // WORKAROUND: keep client timestamp in customOrdinal
288 work->input.ordinal.customOrdinal = timeUs;
289 work->input.buffers.clear();
290 work->input.flags = C2FrameData::FLAG_END_OF_STREAM;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800291 work->worklets.emplace_back(new C2Worklet);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800292
Wonsik Kimab34ed62019-01-31 15:28:46 -0800293 queuedFrameIndex = work->input.ordinal.frameIndex.peeku();
294 queuedBuffers.clear();
295
Pawin Vongmasa36653902018-11-15 00:10:25 -0800296 items.clear();
297 items.push_back(std::move(work));
Wonsik Kimab34ed62019-01-31 15:28:46 -0800298
299 mPipelineWatcher.lock()->onWorkQueued(
300 queuedFrameIndex,
301 std::move(queuedBuffers),
302 PipelineWatcher::Clock::now());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800303 err = mComponent->queue(&items);
Wonsik Kimab34ed62019-01-31 15:28:46 -0800304 if (err != C2_OK) {
305 mPipelineWatcher.lock()->onWorkDone(queuedFrameIndex);
306 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800307 }
308 if (err == C2_OK) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700309 Mutexed<Input>::Locked input(mInput);
310 bool released = false;
311 if (buffer) {
312 released = input->buffers->releaseBuffer(buffer, nullptr, true);
313 } else if (copy) {
314 released = input->extraBuffers.releaseSlot(copy, nullptr, true);
315 }
316 ALOGV("[%s] queueInputBuffer: buffer%s %sreleased",
317 mName, (buffer == nullptr) ? "(copy)" : "", released ? "" : "not ");
Pawin Vongmasa36653902018-11-15 00:10:25 -0800318 }
319
320 feedInputBufferIfAvailableInternal();
321 return err;
322}
323
324status_t CCodecBufferChannel::setParameters(std::vector<std::unique_ptr<C2Param>> &params) {
325 QueueGuard guard(mSync);
326 if (!guard.isRunning()) {
327 ALOGD("[%s] setParameters is only supported in the running state.", mName);
328 return -ENOSYS;
329 }
330 mParamsToBeSet.insert(mParamsToBeSet.end(),
331 std::make_move_iterator(params.begin()),
332 std::make_move_iterator(params.end()));
333 params.clear();
334 return OK;
335}
336
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800337status_t CCodecBufferChannel::attachBuffer(
338 const std::shared_ptr<C2Buffer> &c2Buffer,
339 const sp<MediaCodecBuffer> &buffer) {
340 if (!buffer->copy(c2Buffer)) {
341 return -ENOSYS;
342 }
343 return OK;
344}
345
346void CCodecBufferChannel::ensureDecryptDestination(size_t size) {
347 if (!mDecryptDestination || mDecryptDestination->size() < size) {
348 sp<IMemoryHeap> heap{new MemoryHeapBase(size * 2)};
349 if (mDecryptDestination && mCrypto && mHeapSeqNum >= 0) {
350 mCrypto->unsetHeap(mHeapSeqNum);
351 }
352 mDecryptDestination = new MemoryBase(heap, 0, size * 2);
353 if (mCrypto) {
354 mHeapSeqNum = mCrypto->setHeap(hardware::fromHeap(heap));
355 }
356 }
357}
358
359int32_t CCodecBufferChannel::getHeapSeqNum(const sp<HidlMemory> &memory) {
360 CHECK(mCrypto);
361 auto it = mHeapSeqNumMap.find(memory);
362 int32_t heapSeqNum = -1;
363 if (it == mHeapSeqNumMap.end()) {
364 heapSeqNum = mCrypto->setHeap(memory);
365 mHeapSeqNumMap.emplace(memory, heapSeqNum);
366 } else {
367 heapSeqNum = it->second;
368 }
369 return heapSeqNum;
370}
371
372status_t CCodecBufferChannel::attachEncryptedBuffer(
373 const sp<hardware::HidlMemory> &memory,
374 bool secure,
375 const uint8_t *key,
376 const uint8_t *iv,
377 CryptoPlugin::Mode mode,
378 CryptoPlugin::Pattern pattern,
379 size_t offset,
380 const CryptoPlugin::SubSample *subSamples,
381 size_t numSubSamples,
382 const sp<MediaCodecBuffer> &buffer) {
383 static const C2MemoryUsage kSecureUsage{C2MemoryUsage::READ_PROTECTED, 0};
384 static const C2MemoryUsage kDefaultReadWriteUsage{
385 C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE};
386
387 size_t size = 0;
388 for (size_t i = 0; i < numSubSamples; ++i) {
389 size += subSamples[i].mNumBytesOfClearData + subSamples[i].mNumBytesOfEncryptedData;
390 }
391 std::shared_ptr<C2BlockPool> pool = mBlockPools.lock()->inputPool;
392 std::shared_ptr<C2LinearBlock> block;
393 c2_status_t err = pool->fetchLinearBlock(
394 size,
395 secure ? kSecureUsage : kDefaultReadWriteUsage,
396 &block);
397 if (err != C2_OK) {
398 return NO_MEMORY;
399 }
400 if (!secure) {
401 ensureDecryptDestination(size);
402 }
403 ssize_t result = -1;
404 ssize_t codecDataOffset = 0;
405 if (mCrypto) {
406 AString errorDetailMsg;
407 int32_t heapSeqNum = getHeapSeqNum(memory);
408 hardware::drm::V1_0::SharedBuffer src{(uint32_t)heapSeqNum, offset, size};
409 hardware::drm::V1_0::DestinationBuffer dst;
410 if (secure) {
411 dst.type = DrmBufferType::NATIVE_HANDLE;
412 dst.secureMemory = hardware::hidl_handle(block->handle());
413 } else {
414 dst.type = DrmBufferType::SHARED_MEMORY;
415 IMemoryToSharedBuffer(
416 mDecryptDestination, mHeapSeqNum, &dst.nonsecureMemory);
417 }
418 result = mCrypto->decrypt(
419 key, iv, mode, pattern, src, 0, subSamples, numSubSamples,
420 dst, &errorDetailMsg);
421 if (result < 0) {
422 return result;
423 }
424 if (dst.type == DrmBufferType::SHARED_MEMORY) {
425 C2WriteView view = block->map().get();
426 if (view.error() != C2_OK) {
427 return false;
428 }
429 if (view.size() < result) {
430 return false;
431 }
432 memcpy(view.data(), mDecryptDestination->unsecurePointer(), result);
433 }
434 } else {
435 // Here we cast CryptoPlugin::SubSample to hardware::cas::native::V1_0::SubSample
436 // directly, the structure definitions should match as checked in DescramblerImpl.cpp.
437 hidl_vec<SubSample> hidlSubSamples;
438 hidlSubSamples.setToExternal((SubSample *)subSamples, numSubSamples, false /*own*/);
439
440 hardware::cas::native::V1_0::SharedBuffer src{*memory, offset, size};
441 hardware::cas::native::V1_0::DestinationBuffer dst;
442 if (secure) {
443 dst.type = BufferType::NATIVE_HANDLE;
444 dst.secureMemory = hardware::hidl_handle(block->handle());
445 } else {
446 dst.type = BufferType::SHARED_MEMORY;
447 dst.nonsecureMemory = src;
448 }
449
450 CasStatus status = CasStatus::OK;
451 hidl_string detailedError;
452 ScramblingControl sctrl = ScramblingControl::UNSCRAMBLED;
453
454 if (key != nullptr) {
455 sctrl = (ScramblingControl)key[0];
456 // Adjust for the PES offset
457 codecDataOffset = key[2] | (key[3] << 8);
458 }
459
460 auto returnVoid = mDescrambler->descramble(
461 sctrl,
462 hidlSubSamples,
463 src,
464 0,
465 dst,
466 0,
467 [&status, &result, &detailedError] (
468 CasStatus _status, uint32_t _bytesWritten,
469 const hidl_string& _detailedError) {
470 status = _status;
471 result = (ssize_t)_bytesWritten;
472 detailedError = _detailedError;
473 });
474
475 if (!returnVoid.isOk() || status != CasStatus::OK || result < 0) {
476 ALOGI("[%s] descramble failed, trans=%s, status=%d, result=%zd",
477 mName, returnVoid.description().c_str(), status, result);
478 return UNKNOWN_ERROR;
479 }
480
481 if (result < codecDataOffset) {
482 ALOGD("invalid codec data offset: %zd, result %zd", codecDataOffset, result);
483 return BAD_VALUE;
484 }
485 }
486 if (!secure) {
487 C2WriteView view = block->map().get();
488 if (view.error() != C2_OK) {
489 return UNKNOWN_ERROR;
490 }
491 if (view.size() < result) {
492 return UNKNOWN_ERROR;
493 }
494 memcpy(view.data(), mDecryptDestination->unsecurePointer(), result);
495 }
496 std::shared_ptr<C2Buffer> c2Buffer{C2Buffer::CreateLinearBuffer(
497 block->share(codecDataOffset, result - codecDataOffset, C2Fence{}))};
498 if (!buffer->copy(c2Buffer)) {
499 return -ENOSYS;
500 }
501 return OK;
502}
503
Pawin Vongmasa36653902018-11-15 00:10:25 -0800504status_t CCodecBufferChannel::queueInputBuffer(const sp<MediaCodecBuffer> &buffer) {
505 QueueGuard guard(mSync);
506 if (!guard.isRunning()) {
507 ALOGD("[%s] No more buffers should be queued at current state.", mName);
508 return -ENOSYS;
509 }
510 return queueInputBufferInternal(buffer);
511}
512
513status_t CCodecBufferChannel::queueSecureInputBuffer(
514 const sp<MediaCodecBuffer> &buffer, bool secure, const uint8_t *key,
515 const uint8_t *iv, CryptoPlugin::Mode mode, CryptoPlugin::Pattern pattern,
516 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
517 AString *errorDetailMsg) {
518 QueueGuard guard(mSync);
519 if (!guard.isRunning()) {
520 ALOGD("[%s] No more buffers should be queued at current state.", mName);
521 return -ENOSYS;
522 }
523
524 if (!hasCryptoOrDescrambler()) {
525 return -ENOSYS;
526 }
527 sp<EncryptedLinearBlockBuffer> encryptedBuffer((EncryptedLinearBlockBuffer *)buffer.get());
528
529 ssize_t result = -1;
530 ssize_t codecDataOffset = 0;
Wonsik Kim557c88c2020-03-13 11:03:52 -0700531 if (numSubSamples == 1
532 && subSamples[0].mNumBytesOfClearData == 0
533 && subSamples[0].mNumBytesOfEncryptedData == 0) {
534 // We don't need to go through crypto or descrambler if the input is empty.
535 result = 0;
536 } else if (mCrypto != nullptr) {
Robert Shih895fba92019-07-16 16:29:44 -0700537 hardware::drm::V1_0::DestinationBuffer destination;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800538 if (secure) {
Robert Shih895fba92019-07-16 16:29:44 -0700539 destination.type = DrmBufferType::NATIVE_HANDLE;
540 destination.secureMemory = hidl_handle(encryptedBuffer->handle());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800541 } else {
Robert Shih895fba92019-07-16 16:29:44 -0700542 destination.type = DrmBufferType::SHARED_MEMORY;
543 IMemoryToSharedBuffer(
544 mDecryptDestination, mHeapSeqNum, &destination.nonsecureMemory);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800545 }
Robert Shih895fba92019-07-16 16:29:44 -0700546 hardware::drm::V1_0::SharedBuffer source;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800547 encryptedBuffer->fillSourceBuffer(&source);
548 result = mCrypto->decrypt(
549 key, iv, mode, pattern, source, buffer->offset(),
550 subSamples, numSubSamples, destination, errorDetailMsg);
551 if (result < 0) {
Wonsik Kim557c88c2020-03-13 11:03:52 -0700552 ALOGI("[%s] decrypt failed: result=%zd", mName, result);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800553 return result;
554 }
Robert Shih895fba92019-07-16 16:29:44 -0700555 if (destination.type == DrmBufferType::SHARED_MEMORY) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800556 encryptedBuffer->copyDecryptedContent(mDecryptDestination, result);
557 }
558 } else {
559 // Here we cast CryptoPlugin::SubSample to hardware::cas::native::V1_0::SubSample
560 // directly, the structure definitions should match as checked in DescramblerImpl.cpp.
561 hidl_vec<SubSample> hidlSubSamples;
562 hidlSubSamples.setToExternal((SubSample *)subSamples, numSubSamples, false /*own*/);
563
564 hardware::cas::native::V1_0::SharedBuffer srcBuffer;
565 encryptedBuffer->fillSourceBuffer(&srcBuffer);
566
567 DestinationBuffer dstBuffer;
568 if (secure) {
569 dstBuffer.type = BufferType::NATIVE_HANDLE;
570 dstBuffer.secureMemory = hidl_handle(encryptedBuffer->handle());
571 } else {
572 dstBuffer.type = BufferType::SHARED_MEMORY;
573 dstBuffer.nonsecureMemory = srcBuffer;
574 }
575
576 CasStatus status = CasStatus::OK;
577 hidl_string detailedError;
578 ScramblingControl sctrl = ScramblingControl::UNSCRAMBLED;
579
580 if (key != nullptr) {
581 sctrl = (ScramblingControl)key[0];
582 // Adjust for the PES offset
583 codecDataOffset = key[2] | (key[3] << 8);
584 }
585
586 auto returnVoid = mDescrambler->descramble(
587 sctrl,
588 hidlSubSamples,
589 srcBuffer,
590 0,
591 dstBuffer,
592 0,
593 [&status, &result, &detailedError] (
594 CasStatus _status, uint32_t _bytesWritten,
595 const hidl_string& _detailedError) {
596 status = _status;
597 result = (ssize_t)_bytesWritten;
598 detailedError = _detailedError;
599 });
600
601 if (!returnVoid.isOk() || status != CasStatus::OK || result < 0) {
602 ALOGI("[%s] descramble failed, trans=%s, status=%d, result=%zd",
603 mName, returnVoid.description().c_str(), status, result);
604 return UNKNOWN_ERROR;
605 }
606
607 if (result < codecDataOffset) {
608 ALOGD("invalid codec data offset: %zd, result %zd", codecDataOffset, result);
609 return BAD_VALUE;
610 }
611
612 ALOGV("[%s] descramble succeeded, %zd bytes", mName, result);
613
614 if (dstBuffer.type == BufferType::SHARED_MEMORY) {
615 encryptedBuffer->copyDecryptedContentFromMemory(result);
616 }
617 }
618
619 buffer->setRange(codecDataOffset, result - codecDataOffset);
620 return queueInputBufferInternal(buffer);
621}
622
623void CCodecBufferChannel::feedInputBufferIfAvailable() {
624 QueueGuard guard(mSync);
625 if (!guard.isRunning()) {
626 ALOGV("[%s] We're not running --- no input buffer reported", mName);
627 return;
628 }
629 feedInputBufferIfAvailableInternal();
630}
631
632void CCodecBufferChannel::feedInputBufferIfAvailableInternal() {
Taehwan Kimda0517d2020-09-16 17:29:37 +0900633 if (mInputMetEos) {
Wonsik Kimdf5dd142019-02-06 10:15:46 -0800634 return;
Pawin Vongmasac3c536d2020-06-12 04:00:04 -0700635 }
636 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700637 Mutexed<Output>::Locked output(mOutput);
Pawin Vongmasac3c536d2020-06-12 04:00:04 -0700638 if (!output->buffers ||
639 output->buffers->hasPending() ||
Wonsik Kim0487b782020-10-28 11:45:50 -0700640 output->buffers->numActiveSlots() >= output->numSlots) {
Wonsik Kimdf5dd142019-02-06 10:15:46 -0800641 return;
642 }
643 }
Wonsik Kim0487b782020-10-28 11:45:50 -0700644 size_t numActiveSlots = 0;
645 while (!mPipelineWatcher.lock()->pipelineFull()) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800646 sp<MediaCodecBuffer> inBuffer;
647 size_t index;
648 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700649 Mutexed<Input>::Locked input(mInput);
Wonsik Kim0487b782020-10-28 11:45:50 -0700650 numActiveSlots = input->buffers->numActiveSlots();
651 if (numActiveSlots >= input->numSlots) {
652 break;
Wonsik Kimab34ed62019-01-31 15:28:46 -0800653 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700654 if (!input->buffers->requestNewBuffer(&index, &inBuffer)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800655 ALOGV("[%s] no new buffer available", mName);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800656 break;
657 }
658 }
659 ALOGV("[%s] new input index = %zu [%p]", mName, index, inBuffer.get());
660 mCallback->onInputBufferAvailable(index, inBuffer);
661 }
Wonsik Kim0487b782020-10-28 11:45:50 -0700662 ALOGV("[%s] # active slots after feedInputBufferIfAvailable = %zu", mName, numActiveSlots);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800663}
664
665status_t CCodecBufferChannel::renderOutputBuffer(
666 const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) {
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800667 ALOGV("[%s] renderOutputBuffer: %p", mName, buffer.get());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800668 std::shared_ptr<C2Buffer> c2Buffer;
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800669 bool released = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800670 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700671 Mutexed<Output>::Locked output(mOutput);
672 if (output->buffers) {
673 released = output->buffers->releaseBuffer(buffer, &c2Buffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800674 }
675 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800676 // NOTE: some apps try to releaseOutputBuffer() with timestamp and/or render
677 // set to true.
678 sendOutputBuffers();
679 // input buffer feeding may have been gated by pending output buffers
680 feedInputBufferIfAvailable();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800681 if (!c2Buffer) {
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800682 if (released) {
Wonsik Kimf7529dd2019-04-18 17:35:53 -0700683 std::call_once(mRenderWarningFlag, [this] {
684 ALOGW("[%s] The app is calling releaseOutputBuffer() with "
685 "timestamp or render=true with non-video buffers. Apps should "
686 "call releaseOutputBuffer() with render=false for those.",
687 mName);
688 });
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800689 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800690 return INVALID_OPERATION;
691 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800692
693#if 0
694 const std::vector<std::shared_ptr<const C2Info>> infoParams = c2Buffer->info();
695 ALOGV("[%s] queuing gfx buffer with %zu infos", mName, infoParams.size());
696 for (const std::shared_ptr<const C2Info> &info : infoParams) {
697 AString res;
698 for (size_t ix = 0; ix + 3 < info->size(); ix += 4) {
699 if (ix) res.append(", ");
700 res.append(*((int32_t*)info.get() + (ix / 4)));
701 }
702 ALOGV(" [%s]", res.c_str());
703 }
704#endif
705 std::shared_ptr<const C2StreamRotationInfo::output> rotation =
706 std::static_pointer_cast<const C2StreamRotationInfo::output>(
707 c2Buffer->getInfo(C2StreamRotationInfo::output::PARAM_TYPE));
708 bool flip = rotation && (rotation->flip & 1);
709 uint32_t quarters = ((rotation ? rotation->value : 0) / 90) & 3;
Byeongjo Park2eef13e2020-06-12 17:24:21 +0900710
711 {
712 Mutexed<OutputSurface>::Locked output(mOutputSurface);
713 if (output->surface == nullptr) {
714 ALOGI("[%s] cannot render buffer without surface", mName);
715 return OK;
716 }
717 int64_t frameIndex;
718 buffer->meta()->findInt64("frameIndex", &frameIndex);
719 if (output->rotation.count(frameIndex) != 0) {
720 auto it = output->rotation.find(frameIndex);
721 quarters = (it->second / 90) & 3;
722 output->rotation.erase(it);
723 }
724 }
725
Pawin Vongmasa36653902018-11-15 00:10:25 -0800726 uint32_t transform = 0;
727 switch (quarters) {
728 case 0: // no rotation
729 transform = flip ? HAL_TRANSFORM_FLIP_H : 0;
730 break;
731 case 1: // 90 degrees counter-clockwise
732 transform = flip ? (HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90)
733 : HAL_TRANSFORM_ROT_270;
734 break;
735 case 2: // 180 degrees
736 transform = flip ? HAL_TRANSFORM_FLIP_V : HAL_TRANSFORM_ROT_180;
737 break;
738 case 3: // 90 degrees clockwise
739 transform = flip ? (HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90)
740 : HAL_TRANSFORM_ROT_90;
741 break;
742 }
743
744 std::shared_ptr<const C2StreamSurfaceScalingInfo::output> surfaceScaling =
745 std::static_pointer_cast<const C2StreamSurfaceScalingInfo::output>(
746 c2Buffer->getInfo(C2StreamSurfaceScalingInfo::output::PARAM_TYPE));
747 uint32_t videoScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
748 if (surfaceScaling) {
749 videoScalingMode = surfaceScaling->value;
750 }
751
752 // Use dataspace from format as it has the default aspects already applied
753 android_dataspace_t dataSpace = HAL_DATASPACE_UNKNOWN; // this is 0
754 (void)buffer->format()->findInt32("android._dataspace", (int32_t *)&dataSpace);
755
756 // HDR static info
757 std::shared_ptr<const C2StreamHdrStaticInfo::output> hdrStaticInfo =
758 std::static_pointer_cast<const C2StreamHdrStaticInfo::output>(
759 c2Buffer->getInfo(C2StreamHdrStaticInfo::output::PARAM_TYPE));
760
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800761 // HDR10 plus info
762 std::shared_ptr<const C2StreamHdr10PlusInfo::output> hdr10PlusInfo =
763 std::static_pointer_cast<const C2StreamHdr10PlusInfo::output>(
764 c2Buffer->getInfo(C2StreamHdr10PlusInfo::output::PARAM_TYPE));
Yichi Chen54be23c2020-06-15 14:30:53 +0800765 if (hdr10PlusInfo && hdr10PlusInfo->flexCount() == 0) {
766 hdr10PlusInfo.reset();
767 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800768
Pawin Vongmasa36653902018-11-15 00:10:25 -0800769 std::vector<C2ConstGraphicBlock> blocks = c2Buffer->data().graphicBlocks();
770 if (blocks.size() != 1u) {
771 ALOGD("[%s] expected 1 graphic block, but got %zu", mName, blocks.size());
772 return UNKNOWN_ERROR;
773 }
774 const C2ConstGraphicBlock &block = blocks.front();
775
776 // TODO: revisit this after C2Fence implementation.
777 android::IGraphicBufferProducer::QueueBufferInput qbi(
778 timestampNs,
779 false, // droppable
780 dataSpace,
781 Rect(blocks.front().crop().left,
782 blocks.front().crop().top,
783 blocks.front().crop().right(),
784 blocks.front().crop().bottom()),
785 videoScalingMode,
786 transform,
787 Fence::NO_FENCE, 0);
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800788 if (hdrStaticInfo || hdr10PlusInfo) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800789 HdrMetadata hdr;
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800790 if (hdrStaticInfo) {
wenchangliuf3f92882020-05-14 00:02:01 +0800791 // If mastering max and min luminance fields are 0, do not use them.
792 // It indicates the value may not be present in the stream.
793 if (hdrStaticInfo->mastering.maxLuminance > 0.0f &&
794 hdrStaticInfo->mastering.minLuminance > 0.0f) {
795 struct android_smpte2086_metadata smpte2086_meta = {
796 .displayPrimaryRed = {
797 hdrStaticInfo->mastering.red.x, hdrStaticInfo->mastering.red.y
798 },
799 .displayPrimaryGreen = {
800 hdrStaticInfo->mastering.green.x, hdrStaticInfo->mastering.green.y
801 },
802 .displayPrimaryBlue = {
803 hdrStaticInfo->mastering.blue.x, hdrStaticInfo->mastering.blue.y
804 },
805 .whitePoint = {
806 hdrStaticInfo->mastering.white.x, hdrStaticInfo->mastering.white.y
807 },
808 .maxLuminance = hdrStaticInfo->mastering.maxLuminance,
809 .minLuminance = hdrStaticInfo->mastering.minLuminance,
810 };
Yichi Chen54be23c2020-06-15 14:30:53 +0800811 hdr.validTypes |= HdrMetadata::SMPTE2086;
wenchangliuf3f92882020-05-14 00:02:01 +0800812 hdr.smpte2086 = smpte2086_meta;
813 }
Chong Zhang3bb2a7f2020-04-21 10:35:12 -0700814 // If the content light level fields are 0, do not use them, it
815 // indicates the value may not be present in the stream.
816 if (hdrStaticInfo->maxCll > 0.0f && hdrStaticInfo->maxFall > 0.0f) {
817 struct android_cta861_3_metadata cta861_meta = {
818 .maxContentLightLevel = hdrStaticInfo->maxCll,
819 .maxFrameAverageLightLevel = hdrStaticInfo->maxFall,
820 };
821 hdr.validTypes |= HdrMetadata::CTA861_3;
822 hdr.cta8613 = cta861_meta;
823 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800824 }
825 if (hdr10PlusInfo) {
826 hdr.validTypes |= HdrMetadata::HDR10PLUS;
827 hdr.hdr10plus.assign(
828 hdr10PlusInfo->m.value,
829 hdr10PlusInfo->m.value + hdr10PlusInfo->flexCount());
830 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800831 qbi.setHdrMetadata(hdr);
832 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800833 // we don't have dirty regions
834 qbi.setSurfaceDamage(Region::INVALID_REGION);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800835 android::IGraphicBufferProducer::QueueBufferOutput qbo;
836 status_t result = mComponent->queueToOutputSurface(block, qbi, &qbo);
837 if (result != OK) {
838 ALOGI("[%s] queueBuffer failed: %d", mName, result);
Sungtak Lee47c018a2020-11-07 01:02:49 -0800839 if (result == NO_INIT) {
840 mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
841 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800842 return result;
843 }
844 ALOGV("[%s] queue buffer successful", mName);
845
846 int64_t mediaTimeUs = 0;
847 (void)buffer->meta()->findInt64("timeUs", &mediaTimeUs);
848 mCCodecCallback->onOutputFramesRendered(mediaTimeUs, timestampNs);
849
850 return OK;
851}
852
853status_t CCodecBufferChannel::discardBuffer(const sp<MediaCodecBuffer> &buffer) {
854 ALOGV("[%s] discardBuffer: %p", mName, buffer.get());
855 bool released = false;
856 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700857 Mutexed<Input>::Locked input(mInput);
858 if (input->buffers && input->buffers->releaseBuffer(buffer, nullptr, true)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800859 released = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800860 }
861 }
862 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700863 Mutexed<Output>::Locked output(mOutput);
864 if (output->buffers && output->buffers->releaseBuffer(buffer, nullptr)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800865 released = true;
866 }
867 }
868 if (released) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800869 sendOutputBuffers();
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800870 feedInputBufferIfAvailable();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800871 } else {
872 ALOGD("[%s] MediaCodec discarded an unknown buffer", mName);
873 }
874 return OK;
875}
876
877void CCodecBufferChannel::getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) {
878 array->clear();
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700879 Mutexed<Input>::Locked input(mInput);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800880
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700881 if (!input->buffers->isArrayMode()) {
882 input->buffers = input->buffers->toArrayMode(input->numSlots);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800883 }
884
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700885 input->buffers->getArray(array);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800886}
887
888void CCodecBufferChannel::getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) {
889 array->clear();
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700890 Mutexed<Output>::Locked output(mOutput);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800891
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700892 if (!output->buffers->isArrayMode()) {
893 output->buffers = output->buffers->toArrayMode(output->numSlots);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800894 }
895
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700896 output->buffers->getArray(array);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800897}
898
899status_t CCodecBufferChannel::start(
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800900 const sp<AMessage> &inputFormat,
901 const sp<AMessage> &outputFormat,
902 bool buffersBoundToCodec) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800903 C2StreamBufferTypeSetting::input iStreamFormat(0u);
904 C2StreamBufferTypeSetting::output oStreamFormat(0u);
905 C2PortReorderBufferDepthTuning::output reorderDepth;
906 C2PortReorderKeySetting::output reorderKey;
Wonsik Kim078b58e2019-01-09 15:08:06 -0800907 C2PortActualDelayTuning::input inputDelay(0);
908 C2PortActualDelayTuning::output outputDelay(0);
909 C2ActualPipelineDelayTuning pipelineDelay(0);
910
Pawin Vongmasa36653902018-11-15 00:10:25 -0800911 c2_status_t err = mComponent->query(
912 {
913 &iStreamFormat,
914 &oStreamFormat,
915 &reorderDepth,
916 &reorderKey,
Wonsik Kim078b58e2019-01-09 15:08:06 -0800917 &inputDelay,
918 &pipelineDelay,
919 &outputDelay,
Pawin Vongmasa36653902018-11-15 00:10:25 -0800920 },
921 {},
922 C2_DONT_BLOCK,
923 nullptr);
924 if (err == C2_BAD_INDEX) {
925 if (!iStreamFormat || !oStreamFormat) {
926 return UNKNOWN_ERROR;
927 }
928 } else if (err != C2_OK) {
929 return UNKNOWN_ERROR;
930 }
931
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -0800932 uint32_t inputDelayValue = inputDelay ? inputDelay.value : 0;
933 uint32_t pipelineDelayValue = pipelineDelay ? pipelineDelay.value : 0;
934 uint32_t outputDelayValue = outputDelay ? outputDelay.value : 0;
935
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700936 size_t numInputSlots = inputDelayValue + pipelineDelayValue + kSmoothnessFactor;
937 size_t numOutputSlots = outputDelayValue + kSmoothnessFactor;
Wonsik Kim078b58e2019-01-09 15:08:06 -0800938
Pawin Vongmasa36653902018-11-15 00:10:25 -0800939 // TODO: get this from input format
940 bool secure = mComponent->getName().find(".secure") != std::string::npos;
941
942 std::shared_ptr<C2AllocatorStore> allocatorStore = GetCodec2PlatformAllocatorStore();
Pin-chih Linaa18ea52019-11-19 18:48:50 +0800943 int poolMask = GetCodec2PoolMask();
944 C2PlatformAllocatorStore::id_t preferredLinearId = GetPreferredLinearAllocatorId(poolMask);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800945
946 if (inputFormat != nullptr) {
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800947 bool graphic = (iStreamFormat.value == C2BufferData::GRAPHIC);
Wonsik Kimffb889a2020-05-28 11:32:25 -0700948 C2Config::api_feature_t apiFeatures = C2Config::api_feature_t(
949 API_REFLECTION |
950 API_VALUES |
951 API_CURRENT_VALUES |
952 API_DEPENDENCY |
953 API_SAME_INPUT_BUFFER);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800954 std::shared_ptr<C2BlockPool> pool;
955 {
956 Mutexed<BlockPools>::Locked pools(mBlockPools);
957
958 // set default allocator ID.
959 pools->inputAllocatorId = (graphic) ? C2PlatformAllocatorStore::GRALLOC
Pin-chih Linaa18ea52019-11-19 18:48:50 +0800960 : preferredLinearId;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800961
962 // query C2PortAllocatorsTuning::input from component. If an allocator ID is obtained
963 // from component, create the input block pool with given ID. Otherwise, use default IDs.
964 std::vector<std::unique_ptr<C2Param>> params;
Wonsik Kimffb889a2020-05-28 11:32:25 -0700965 C2ApiFeaturesSetting featuresSetting{apiFeatures};
966 err = mComponent->query({ &featuresSetting },
Pawin Vongmasa36653902018-11-15 00:10:25 -0800967 { C2PortAllocatorsTuning::input::PARAM_TYPE },
968 C2_DONT_BLOCK,
969 &params);
970 if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
971 ALOGD("[%s] Query input allocators returned %zu params => %s (%u)",
972 mName, params.size(), asString(err), err);
Wonsik Kimffb889a2020-05-28 11:32:25 -0700973 } else if (params.size() == 1) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800974 C2PortAllocatorsTuning::input *inputAllocators =
975 C2PortAllocatorsTuning::input::From(params[0].get());
976 if (inputAllocators && inputAllocators->flexCount() > 0) {
977 std::shared_ptr<C2Allocator> allocator;
978 // verify allocator IDs and resolve default allocator
979 allocatorStore->fetchAllocator(inputAllocators->m.values[0], &allocator);
980 if (allocator) {
981 pools->inputAllocatorId = allocator->getId();
982 } else {
983 ALOGD("[%s] component requested invalid input allocator ID %u",
984 mName, inputAllocators->m.values[0]);
985 }
986 }
987 }
Wonsik Kimffb889a2020-05-28 11:32:25 -0700988 if (featuresSetting) {
989 apiFeatures = featuresSetting.value;
990 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800991
992 // TODO: use C2Component wrapper to associate this pool with ourselves
993 if ((poolMask >> pools->inputAllocatorId) & 1) {
994 err = CreateCodec2BlockPool(pools->inputAllocatorId, nullptr, &pool);
995 ALOGD("[%s] Created input block pool with allocatorID %u => poolID %llu - %s (%d)",
996 mName, pools->inputAllocatorId,
997 (unsigned long long)(pool ? pool->getLocalId() : 111000111),
998 asString(err), err);
999 } else {
1000 err = C2_NOT_FOUND;
1001 }
1002 if (err != C2_OK) {
1003 C2BlockPool::local_id_t inputPoolId =
1004 graphic ? C2BlockPool::BASIC_GRAPHIC : C2BlockPool::BASIC_LINEAR;
1005 err = GetCodec2BlockPool(inputPoolId, nullptr, &pool);
1006 ALOGD("[%s] Using basic input block pool with poolID %llu => got %llu - %s (%d)",
1007 mName, (unsigned long long)inputPoolId,
1008 (unsigned long long)(pool ? pool->getLocalId() : 111000111),
1009 asString(err), err);
1010 if (err != C2_OK) {
1011 return NO_MEMORY;
1012 }
1013 }
1014 pools->inputPool = pool;
1015 }
1016
Wonsik Kim51051262018-11-28 13:59:05 -08001017 bool forceArrayMode = false;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001018 Mutexed<Input>::Locked input(mInput);
Wonsik Kimbdffead2019-07-01 12:00:07 -07001019 input->inputDelay = inputDelayValue;
1020 input->pipelineDelay = pipelineDelayValue;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001021 input->numSlots = numInputSlots;
1022 input->extraBuffers.flush();
1023 input->numExtraSlots = 0u;
Wonsik Kimffb889a2020-05-28 11:32:25 -07001024 bool conforming = (apiFeatures & API_SAME_INPUT_BUFFER);
1025 // For encrypted content, framework decrypts source buffer (ashmem) into
1026 // C2Buffers. Thus non-conforming codecs can process these.
1027 if (!buffersBoundToCodec && (hasCryptoOrDescrambler() || conforming)) {
Wonsik Kimfb7a7672019-12-27 17:13:33 -08001028 input->buffers.reset(new SlotInputBuffers(mName));
1029 } else if (graphic) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001030 if (mInputSurface) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001031 input->buffers.reset(new DummyInputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001032 } else if (mMetaMode == MODE_ANW) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001033 input->buffers.reset(new GraphicMetadataInputBuffers(mName));
Wonsik Kim1221fd12019-07-12 12:52:05 -07001034 // This is to ensure buffers do not get released prematurely.
1035 // TODO: handle this without going into array mode
1036 forceArrayMode = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001037 } else {
Wonsik Kim41d83432020-04-27 16:40:49 -07001038 input->buffers.reset(new GraphicInputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001039 }
1040 } else {
1041 if (hasCryptoOrDescrambler()) {
1042 int32_t capacity = kLinearBufferSize;
1043 (void)inputFormat->findInt32(KEY_MAX_INPUT_SIZE, &capacity);
1044 if ((size_t)capacity > kMaxLinearBufferSize) {
1045 ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize);
1046 capacity = kMaxLinearBufferSize;
1047 }
1048 if (mDealer == nullptr) {
1049 mDealer = new MemoryDealer(
1050 align(capacity, MemoryDealer::getAllocationAlignment())
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001051 * (numInputSlots + 1),
Pawin Vongmasa36653902018-11-15 00:10:25 -08001052 "EncryptedLinearInputBuffers");
1053 mDecryptDestination = mDealer->allocate((size_t)capacity);
1054 }
1055 if (mCrypto != nullptr && mHeapSeqNum < 0) {
Robert Shih895fba92019-07-16 16:29:44 -07001056 sp<HidlMemory> heap = fromHeap(mDealer->getMemoryHeap());
1057 mHeapSeqNum = mCrypto->setHeap(heap);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001058 } else {
1059 mHeapSeqNum = -1;
1060 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001061 input->buffers.reset(new EncryptedLinearInputBuffers(
Wonsik Kim078b58e2019-01-09 15:08:06 -08001062 secure, mDealer, mCrypto, mHeapSeqNum, (size_t)capacity,
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001063 numInputSlots, mName));
Wonsik Kim51051262018-11-28 13:59:05 -08001064 forceArrayMode = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001065 } else {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001066 input->buffers.reset(new LinearInputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001067 }
1068 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001069 input->buffers->setFormat(inputFormat);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001070
1071 if (err == C2_OK) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001072 input->buffers->setPool(pool);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001073 } else {
1074 // TODO: error
1075 }
Wonsik Kim51051262018-11-28 13:59:05 -08001076
1077 if (forceArrayMode) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001078 input->buffers = input->buffers->toArrayMode(numInputSlots);
Wonsik Kim51051262018-11-28 13:59:05 -08001079 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001080 }
1081
1082 if (outputFormat != nullptr) {
1083 sp<IGraphicBufferProducer> outputSurface;
1084 uint32_t outputGeneration;
1085 {
1086 Mutexed<OutputSurface>::Locked output(mOutputSurface);
Sungtak Leed7463d12019-09-04 16:01:00 -07001087 output->maxDequeueBuffers = numOutputSlots +
Sungtak Lee7a7b7422019-07-16 17:40:40 -07001088 reorderDepth.value + kRenderingDepth;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001089 outputSurface = output->surface ?
1090 output->surface->getIGraphicBufferProducer() : nullptr;
Wonsik Kimf5e5c832019-02-21 11:36:05 -08001091 if (outputSurface) {
1092 output->surface->setMaxDequeuedBufferCount(output->maxDequeueBuffers);
1093 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001094 outputGeneration = output->generation;
1095 }
1096
Lajos Molnar3bb81cd2019-02-20 15:10:30 -08001097 bool graphic = (oStreamFormat.value == C2BufferData::GRAPHIC);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001098 C2BlockPool::local_id_t outputPoolId_;
David Stevensc3fbb282021-01-18 18:11:20 +09001099 C2BlockPool::local_id_t prevOutputPoolId;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001100
1101 {
1102 Mutexed<BlockPools>::Locked pools(mBlockPools);
1103
David Stevensc3fbb282021-01-18 18:11:20 +09001104 prevOutputPoolId = pools->outputPoolId;
1105
Pawin Vongmasa36653902018-11-15 00:10:25 -08001106 // set default allocator ID.
1107 pools->outputAllocatorId = (graphic) ? C2PlatformAllocatorStore::GRALLOC
Pin-chih Linaa18ea52019-11-19 18:48:50 +08001108 : preferredLinearId;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001109
1110 // query C2PortAllocatorsTuning::output from component, or use default allocator if
1111 // unsuccessful.
1112 std::vector<std::unique_ptr<C2Param>> params;
1113 err = mComponent->query({ },
1114 { C2PortAllocatorsTuning::output::PARAM_TYPE },
1115 C2_DONT_BLOCK,
1116 &params);
1117 if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
1118 ALOGD("[%s] Query output allocators returned %zu params => %s (%u)",
1119 mName, params.size(), asString(err), err);
1120 } else if (err == C2_OK && params.size() == 1) {
1121 C2PortAllocatorsTuning::output *outputAllocators =
1122 C2PortAllocatorsTuning::output::From(params[0].get());
1123 if (outputAllocators && outputAllocators->flexCount() > 0) {
1124 std::shared_ptr<C2Allocator> allocator;
1125 // verify allocator IDs and resolve default allocator
1126 allocatorStore->fetchAllocator(outputAllocators->m.values[0], &allocator);
1127 if (allocator) {
1128 pools->outputAllocatorId = allocator->getId();
1129 } else {
1130 ALOGD("[%s] component requested invalid output allocator ID %u",
1131 mName, outputAllocators->m.values[0]);
1132 }
1133 }
1134 }
1135
1136 // use bufferqueue if outputting to a surface.
1137 // query C2PortSurfaceAllocatorTuning::output from component, or use default allocator
1138 // if unsuccessful.
1139 if (outputSurface) {
1140 params.clear();
1141 err = mComponent->query({ },
1142 { C2PortSurfaceAllocatorTuning::output::PARAM_TYPE },
1143 C2_DONT_BLOCK,
1144 &params);
1145 if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
1146 ALOGD("[%s] Query output surface allocator returned %zu params => %s (%u)",
1147 mName, params.size(), asString(err), err);
1148 } else if (err == C2_OK && params.size() == 1) {
1149 C2PortSurfaceAllocatorTuning::output *surfaceAllocator =
1150 C2PortSurfaceAllocatorTuning::output::From(params[0].get());
1151 if (surfaceAllocator) {
1152 std::shared_ptr<C2Allocator> allocator;
1153 // verify allocator IDs and resolve default allocator
1154 allocatorStore->fetchAllocator(surfaceAllocator->value, &allocator);
1155 if (allocator) {
1156 pools->outputAllocatorId = allocator->getId();
1157 } else {
1158 ALOGD("[%s] component requested invalid surface output allocator ID %u",
1159 mName, surfaceAllocator->value);
1160 err = C2_BAD_VALUE;
1161 }
1162 }
1163 }
1164 if (pools->outputAllocatorId == C2PlatformAllocatorStore::GRALLOC
1165 && err != C2_OK
1166 && ((poolMask >> C2PlatformAllocatorStore::BUFFERQUEUE) & 1)) {
1167 pools->outputAllocatorId = C2PlatformAllocatorStore::BUFFERQUEUE;
1168 }
1169 }
1170
1171 if ((poolMask >> pools->outputAllocatorId) & 1) {
1172 err = mComponent->createBlockPool(
1173 pools->outputAllocatorId, &pools->outputPoolId, &pools->outputPoolIntf);
1174 ALOGI("[%s] Created output block pool with allocatorID %u => poolID %llu - %s",
1175 mName, pools->outputAllocatorId,
1176 (unsigned long long)pools->outputPoolId,
1177 asString(err));
1178 } else {
1179 err = C2_NOT_FOUND;
1180 }
1181 if (err != C2_OK) {
1182 // use basic pool instead
1183 pools->outputPoolId =
1184 graphic ? C2BlockPool::BASIC_GRAPHIC : C2BlockPool::BASIC_LINEAR;
1185 }
1186
1187 // Configure output block pool ID as parameter C2PortBlockPoolsTuning::output to
1188 // component.
1189 std::unique_ptr<C2PortBlockPoolsTuning::output> poolIdsTuning =
1190 C2PortBlockPoolsTuning::output::AllocUnique({ pools->outputPoolId });
1191
1192 std::vector<std::unique_ptr<C2SettingResult>> failures;
1193 err = mComponent->config({ poolIdsTuning.get() }, C2_MAY_BLOCK, &failures);
1194 ALOGD("[%s] Configured output block pool ids %llu => %s",
1195 mName, (unsigned long long)poolIdsTuning->m.values[0], asString(err));
1196 outputPoolId_ = pools->outputPoolId;
1197 }
1198
David Stevensc3fbb282021-01-18 18:11:20 +09001199 if (prevOutputPoolId != C2BlockPool::BASIC_LINEAR
1200 && prevOutputPoolId != C2BlockPool::BASIC_GRAPHIC) {
1201 c2_status_t err = mComponent->destroyBlockPool(prevOutputPoolId);
1202 if (err != C2_OK) {
1203 ALOGW("Failed to clean up previous block pool %llu - %s (%d)\n",
1204 (unsigned long long) prevOutputPoolId, asString(err), err);
1205 }
1206 }
1207
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001208 Mutexed<Output>::Locked output(mOutput);
Wonsik Kimbdffead2019-07-01 12:00:07 -07001209 output->outputDelay = outputDelayValue;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001210 output->numSlots = numOutputSlots;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001211 if (graphic) {
Wonsik Kimfb7a7672019-12-27 17:13:33 -08001212 if (outputSurface || !buffersBoundToCodec) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001213 output->buffers.reset(new GraphicOutputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001214 } else {
Wonsik Kim41d83432020-04-27 16:40:49 -07001215 output->buffers.reset(new RawGraphicOutputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001216 }
1217 } else {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001218 output->buffers.reset(new LinearOutputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001219 }
Wonsik Kime4716c02020-02-28 10:42:21 -08001220 output->buffers->setFormat(outputFormat);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001221
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001222 output->buffers->clearStash();
1223 if (reorderDepth) {
1224 output->buffers->setReorderDepth(reorderDepth.value);
1225 }
1226 if (reorderKey) {
1227 output->buffers->setReorderKey(reorderKey.value);
1228 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001229
1230 // Try to set output surface to created block pool if given.
1231 if (outputSurface) {
1232 mComponent->setOutputSurface(
1233 outputPoolId_,
1234 outputSurface,
1235 outputGeneration);
1236 }
1237
Wonsik Kim8ab25aa2019-06-24 16:37:37 -07001238 if (oStreamFormat.value == C2BufferData::LINEAR) {
Wonsik Kim58713302020-01-29 22:25:23 -08001239 if (buffersBoundToCodec) {
1240 // WORKAROUND: if we're using early CSD workaround we convert to
1241 // array mode, to appease apps assuming the output
1242 // buffers to be of the same size.
1243 output->buffers = output->buffers->toArrayMode(numOutputSlots);
1244 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001245
1246 int32_t channelCount;
1247 int32_t sampleRate;
1248 if (outputFormat->findInt32(KEY_CHANNEL_COUNT, &channelCount)
1249 && outputFormat->findInt32(KEY_SAMPLE_RATE, &sampleRate)) {
1250 int32_t delay = 0;
1251 int32_t padding = 0;;
1252 if (!outputFormat->findInt32("encoder-delay", &delay)) {
1253 delay = 0;
1254 }
1255 if (!outputFormat->findInt32("encoder-padding", &padding)) {
1256 padding = 0;
1257 }
1258 if (delay || padding) {
1259 // We need write access to the buffers, and we're already in
1260 // array mode.
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001261 output->buffers->initSkipCutBuffer(delay, padding, sampleRate, channelCount);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001262 }
1263 }
1264 }
1265 }
1266
1267 // Set up pipeline control. This has to be done after mInputBuffers and
1268 // mOutputBuffers are initialized to make sure that lingering callbacks
1269 // about buffers from the previous generation do not interfere with the
1270 // newly initialized pipeline capacity.
1271
Wonsik Kimab34ed62019-01-31 15:28:46 -08001272 {
1273 Mutexed<PipelineWatcher>::Locked watcher(mPipelineWatcher);
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -08001274 watcher->inputDelay(inputDelayValue)
1275 .pipelineDelay(pipelineDelayValue)
1276 .outputDelay(outputDelayValue)
Wonsik Kimab34ed62019-01-31 15:28:46 -08001277 .smoothnessFactor(kSmoothnessFactor);
1278 watcher->flush();
1279 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001280
1281 mInputMetEos = false;
1282 mSync.start();
1283 return OK;
1284}
1285
1286status_t CCodecBufferChannel::requestInitialInputBuffers() {
1287 if (mInputSurface) {
1288 return OK;
1289 }
1290
Lajos Molnar3bb81cd2019-02-20 15:10:30 -08001291 C2StreamBufferTypeSetting::output oStreamFormat(0u);
Wonsik Kim8ab25aa2019-06-24 16:37:37 -07001292 C2PrependHeaderModeSetting prepend(PREPEND_HEADER_TO_NONE);
1293 c2_status_t err = mComponent->query({ &oStreamFormat, &prepend }, {}, C2_DONT_BLOCK, nullptr);
1294 if (err != C2_OK && err != C2_BAD_INDEX) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001295 return UNKNOWN_ERROR;
1296 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001297 size_t numInputSlots = mInput.lock()->numSlots;
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001298
1299 struct ClientInputBuffer {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001300 size_t index;
1301 sp<MediaCodecBuffer> buffer;
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001302 size_t capacity;
1303 };
1304 std::list<ClientInputBuffer> clientInputBuffers;
1305
1306 {
1307 Mutexed<Input>::Locked input(mInput);
1308 while (clientInputBuffers.size() < numInputSlots) {
1309 ClientInputBuffer clientInputBuffer;
1310 if (!input->buffers->requestNewBuffer(&clientInputBuffer.index,
1311 &clientInputBuffer.buffer)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001312 break;
1313 }
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001314 clientInputBuffer.capacity = clientInputBuffer.buffer->capacity();
1315 clientInputBuffers.emplace_back(std::move(clientInputBuffer));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001316 }
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001317 }
1318 if (clientInputBuffers.empty()) {
1319 ALOGW("[%s] start: cannot allocate memory at all", mName);
1320 return NO_MEMORY;
1321 } else if (clientInputBuffers.size() < numInputSlots) {
1322 ALOGD("[%s] start: cannot allocate memory for all slots, "
1323 "only %zu buffers allocated",
1324 mName, clientInputBuffers.size());
1325 } else {
1326 ALOGV("[%s] %zu initial input buffers available",
1327 mName, clientInputBuffers.size());
1328 }
1329 // Sort input buffers by their capacities in increasing order.
1330 clientInputBuffers.sort(
1331 [](const ClientInputBuffer& a, const ClientInputBuffer& b) {
1332 return a.capacity < b.capacity;
1333 });
1334
Wonsik Kimf34e4e02021-01-05 18:58:15 -08001335 std::list<std::unique_ptr<C2Work>> flushedConfigs;
1336 mFlushedConfigs.lock()->swap(flushedConfigs);
1337 if (!flushedConfigs.empty()) {
1338 err = mComponent->queue(&flushedConfigs);
1339 if (err != C2_OK) {
1340 ALOGW("[%s] Error while queueing a flushed config", mName);
1341 return UNKNOWN_ERROR;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001342 }
1343 }
Wonsik Kimf34e4e02021-01-05 18:58:15 -08001344 if (oStreamFormat.value == C2BufferData::LINEAR &&
1345 (!prepend || prepend.value == PREPEND_HEADER_TO_NONE)) {
1346 sp<MediaCodecBuffer> buffer = clientInputBuffers.front().buffer;
1347 // WORKAROUND: Some apps expect CSD available without queueing
1348 // any input. Queue an empty buffer to get the CSD.
1349 buffer->setRange(0, 0);
1350 buffer->meta()->clear();
1351 buffer->meta()->setInt64("timeUs", 0);
1352 if (queueInputBufferInternal(buffer) != OK) {
1353 ALOGW("[%s] Error while queueing an empty buffer to get CSD",
1354 mName);
1355 return UNKNOWN_ERROR;
1356 }
1357 clientInputBuffers.pop_front();
1358 }
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001359
1360 for (const ClientInputBuffer& clientInputBuffer: clientInputBuffers) {
1361 mCallback->onInputBufferAvailable(
1362 clientInputBuffer.index,
1363 clientInputBuffer.buffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001364 }
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001365
Pawin Vongmasa36653902018-11-15 00:10:25 -08001366 return OK;
1367}
1368
1369void CCodecBufferChannel::stop() {
1370 mSync.stop();
1371 mFirstValidFrameIndex = mFrameIndex.load(std::memory_order_relaxed);
1372 if (mInputSurface != nullptr) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001373 mInputSurface.reset();
1374 }
Wonsik Kima2e3cdd2020-05-20 15:14:42 -07001375 mPipelineWatcher.lock()->flush();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001376}
1377
Wonsik Kim936a89c2020-05-08 16:07:50 -07001378void CCodecBufferChannel::reset() {
1379 stop();
1380 {
1381 Mutexed<Input>::Locked input(mInput);
1382 input->buffers.reset(new DummyInputBuffers(""));
Wonsik Kima2e3cdd2020-05-20 15:14:42 -07001383 input->extraBuffers.flush();
Wonsik Kim936a89c2020-05-08 16:07:50 -07001384 }
1385 {
1386 Mutexed<Output>::Locked output(mOutput);
1387 output->buffers.reset();
1388 }
1389}
1390
1391void CCodecBufferChannel::release() {
1392 mComponent.reset();
1393 mInputAllocator.reset();
1394 mOutputSurface.lock()->surface.clear();
1395 {
1396 Mutexed<BlockPools>::Locked blockPools{mBlockPools};
1397 blockPools->inputPool.reset();
1398 blockPools->outputPoolIntf.reset();
1399 }
Wonsik Kima2e3cdd2020-05-20 15:14:42 -07001400 setCrypto(nullptr);
1401 setDescrambler(nullptr);
Wonsik Kim936a89c2020-05-08 16:07:50 -07001402}
1403
1404
Pawin Vongmasa36653902018-11-15 00:10:25 -08001405void CCodecBufferChannel::flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) {
1406 ALOGV("[%s] flush", mName);
Wonsik Kimf34e4e02021-01-05 18:58:15 -08001407 std::list<std::unique_ptr<C2Work>> configs;
1408 for (const std::unique_ptr<C2Work> &work : flushedWork) {
1409 if (!(work->input.flags & C2FrameData::FLAG_CODEC_CONFIG)) {
1410 continue;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001411 }
Wonsik Kimf34e4e02021-01-05 18:58:15 -08001412 if (work->input.buffers.empty()
1413 || work->input.buffers.front() == nullptr
1414 || work->input.buffers.front()->data().linearBlocks().empty()) {
1415 ALOGD("[%s] no linear codec config data found", mName);
1416 continue;
1417 }
1418 std::unique_ptr<C2Work> copy(new C2Work);
1419 copy->input.flags = C2FrameData::flags_t(work->input.flags | C2FrameData::FLAG_DROP_FRAME);
1420 copy->input.ordinal = work->input.ordinal;
1421 copy->input.buffers.insert(
1422 copy->input.buffers.begin(),
1423 work->input.buffers.begin(),
1424 work->input.buffers.end());
1425 for (const std::unique_ptr<C2Param> &param : work->input.configUpdate) {
1426 copy->input.configUpdate.push_back(C2Param::Copy(*param));
1427 }
1428 copy->input.infoBuffers.insert(
1429 copy->input.infoBuffers.begin(),
1430 work->input.infoBuffers.begin(),
1431 work->input.infoBuffers.end());
1432 copy->worklets.emplace_back(new C2Worklet);
1433 configs.push_back(std::move(copy));
1434 ALOGV("[%s] stashed flushed codec config data", mName);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001435 }
Wonsik Kimf34e4e02021-01-05 18:58:15 -08001436 mFlushedConfigs.lock()->swap(configs);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001437 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001438 Mutexed<Input>::Locked input(mInput);
1439 input->buffers->flush();
1440 input->extraBuffers.flush();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001441 }
1442 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001443 Mutexed<Output>::Locked output(mOutput);
Wonsik Kim936a89c2020-05-08 16:07:50 -07001444 if (output->buffers) {
1445 output->buffers->flush(flushedWork);
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001446 output->buffers->flushStash();
Wonsik Kim936a89c2020-05-08 16:07:50 -07001447 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001448 }
Wonsik Kimab34ed62019-01-31 15:28:46 -08001449 mPipelineWatcher.lock()->flush();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001450}
1451
1452void CCodecBufferChannel::onWorkDone(
1453 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
Wonsik Kimab34ed62019-01-31 15:28:46 -08001454 const C2StreamInitDataInfo::output *initData) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001455 if (handleWork(std::move(work), outputFormat, initData)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001456 feedInputBufferIfAvailable();
1457 }
1458}
1459
1460void CCodecBufferChannel::onInputBufferDone(
Wonsik Kimab34ed62019-01-31 15:28:46 -08001461 uint64_t frameIndex, size_t arrayIndex) {
Pawin Vongmasa8e2cfb52019-05-15 05:20:52 -07001462 if (mInputSurface) {
1463 return;
1464 }
Wonsik Kimab34ed62019-01-31 15:28:46 -08001465 std::shared_ptr<C2Buffer> buffer =
1466 mPipelineWatcher.lock()->onInputBufferReleased(frameIndex, arrayIndex);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001467 bool newInputSlotAvailable;
1468 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001469 Mutexed<Input>::Locked input(mInput);
1470 newInputSlotAvailable = input->buffers->expireComponentBuffer(buffer);
1471 if (!newInputSlotAvailable) {
1472 (void)input->extraBuffers.expireComponentBuffer(buffer);
1473 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001474 }
1475 if (newInputSlotAvailable) {
1476 feedInputBufferIfAvailable();
1477 }
1478}
1479
1480bool CCodecBufferChannel::handleWork(
1481 std::unique_ptr<C2Work> work,
1482 const sp<AMessage> &outputFormat,
1483 const C2StreamInitDataInfo::output *initData) {
Wonsik Kim936a89c2020-05-08 16:07:50 -07001484 {
Wonsik Kima4e049d2020-04-28 19:42:23 +00001485 Mutexed<Output>::Locked output(mOutput);
Wonsik Kim936a89c2020-05-08 16:07:50 -07001486 if (!output->buffers) {
1487 return false;
1488 }
Wonsik Kime75a5da2020-02-14 17:29:03 -08001489 }
1490
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001491 // Whether the output buffer should be reported to the client or not.
1492 bool notifyClient = false;
1493
1494 if (work->result == C2_OK){
1495 notifyClient = true;
1496 } else if (work->result == C2_NOT_FOUND) {
1497 ALOGD("[%s] flushed work; ignored.", mName);
1498 } else {
1499 // C2_OK and C2_NOT_FOUND are the only results that we accept for processing
1500 // the config update.
1501 ALOGD("[%s] work failed to complete: %d", mName, work->result);
1502 mCCodecCallback->onError(work->result, ACTION_CODE_FATAL);
1503 return false;
1504 }
1505
1506 if ((work->input.ordinal.frameIndex -
1507 mFirstValidFrameIndex.load()).peek() < 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001508 // Discard frames from previous generation.
1509 ALOGD("[%s] Discard frames from previous generation.", mName);
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001510 notifyClient = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001511 }
1512
Wonsik Kim524b0582019-03-12 11:28:57 -07001513 if (mInputSurface == nullptr && (work->worklets.size() != 1u
Pawin Vongmasa36653902018-11-15 00:10:25 -08001514 || !work->worklets.front()
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001515 || !(work->worklets.front()->output.flags &
1516 C2FrameData::FLAG_INCOMPLETE))) {
1517 mPipelineWatcher.lock()->onWorkDone(
1518 work->input.ordinal.frameIndex.peeku());
Pawin Vongmasa36653902018-11-15 00:10:25 -08001519 }
1520
1521 // NOTE: MediaCodec usage supposedly have only one worklet
1522 if (work->worklets.size() != 1u) {
1523 ALOGI("[%s] onWorkDone: incorrect number of worklets: %zu",
1524 mName, work->worklets.size());
1525 mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1526 return false;
1527 }
1528
1529 const std::unique_ptr<C2Worklet> &worklet = work->worklets.front();
1530
1531 std::shared_ptr<C2Buffer> buffer;
1532 // NOTE: MediaCodec usage supposedly have only one output stream.
1533 if (worklet->output.buffers.size() > 1u) {
1534 ALOGI("[%s] onWorkDone: incorrect number of output buffers: %zu",
1535 mName, worklet->output.buffers.size());
1536 mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1537 return false;
1538 } else if (worklet->output.buffers.size() == 1u) {
1539 buffer = worklet->output.buffers[0];
1540 if (!buffer) {
1541 ALOGD("[%s] onWorkDone: nullptr found in buffers; ignored.", mName);
1542 }
1543 }
1544
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001545 std::optional<uint32_t> newInputDelay, newPipelineDelay;
Wonsik Kim315e40a2020-09-09 14:11:50 -07001546 bool needMaxDequeueBufferCountUpdate = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001547 while (!worklet->output.configUpdate.empty()) {
1548 std::unique_ptr<C2Param> param;
1549 worklet->output.configUpdate.back().swap(param);
1550 worklet->output.configUpdate.pop_back();
1551 switch (param->coreIndex().coreIndex()) {
1552 case C2PortReorderBufferDepthTuning::CORE_INDEX: {
1553 C2PortReorderBufferDepthTuning::output reorderDepth;
1554 if (reorderDepth.updateFrom(*param)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001555 ALOGV("[%s] onWorkDone: updated reorder depth to %u",
1556 mName, reorderDepth.value);
Wonsik Kim315e40a2020-09-09 14:11:50 -07001557 mOutput.lock()->buffers->setReorderDepth(reorderDepth.value);
1558 needMaxDequeueBufferCountUpdate = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001559 } else {
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001560 ALOGD("[%s] onWorkDone: failed to read reorder depth",
1561 mName);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001562 }
1563 break;
1564 }
1565 case C2PortReorderKeySetting::CORE_INDEX: {
1566 C2PortReorderKeySetting::output reorderKey;
1567 if (reorderKey.updateFrom(*param)) {
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001568 mOutput.lock()->buffers->setReorderKey(reorderKey.value);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001569 ALOGV("[%s] onWorkDone: updated reorder key to %u",
1570 mName, reorderKey.value);
1571 } else {
1572 ALOGD("[%s] onWorkDone: failed to read reorder key", mName);
1573 }
1574 break;
1575 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001576 case C2PortActualDelayTuning::CORE_INDEX: {
1577 if (param->isGlobal()) {
1578 C2ActualPipelineDelayTuning pipelineDelay;
1579 if (pipelineDelay.updateFrom(*param)) {
1580 ALOGV("[%s] onWorkDone: updating pipeline delay %u",
1581 mName, pipelineDelay.value);
1582 newPipelineDelay = pipelineDelay.value;
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001583 (void)mPipelineWatcher.lock()->pipelineDelay(
1584 pipelineDelay.value);
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001585 }
1586 }
1587 if (param->forInput()) {
1588 C2PortActualDelayTuning::input inputDelay;
1589 if (inputDelay.updateFrom(*param)) {
1590 ALOGV("[%s] onWorkDone: updating input delay %u",
1591 mName, inputDelay.value);
1592 newInputDelay = inputDelay.value;
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001593 (void)mPipelineWatcher.lock()->inputDelay(
1594 inputDelay.value);
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001595 }
1596 }
1597 if (param->forOutput()) {
1598 C2PortActualDelayTuning::output outputDelay;
1599 if (outputDelay.updateFrom(*param)) {
1600 ALOGV("[%s] onWorkDone: updating output delay %u",
1601 mName, outputDelay.value);
Wonsik Kim315e40a2020-09-09 14:11:50 -07001602 (void)mPipelineWatcher.lock()->outputDelay(outputDelay.value);
1603 needMaxDequeueBufferCountUpdate = true;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001604
1605 bool outputBuffersChanged = false;
Wonsik Kimf0e7d222019-06-28 12:33:16 -07001606 size_t numOutputSlots = 0;
1607 {
1608 Mutexed<Output>::Locked output(mOutput);
Wonsik Kim936a89c2020-05-08 16:07:50 -07001609 if (!output->buffers) {
1610 return false;
1611 }
Wonsik Kimf0e7d222019-06-28 12:33:16 -07001612 output->outputDelay = outputDelay.value;
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001613 numOutputSlots = outputDelay.value +
1614 kSmoothnessFactor;
Wonsik Kimf0e7d222019-06-28 12:33:16 -07001615 if (output->numSlots < numOutputSlots) {
1616 output->numSlots = numOutputSlots;
1617 if (output->buffers->isArrayMode()) {
1618 OutputBuffersArray *array =
1619 (OutputBuffersArray *)output->buffers.get();
1620 ALOGV("[%s] onWorkDone: growing output buffer array to %zu",
1621 mName, numOutputSlots);
1622 array->grow(numOutputSlots);
1623 outputBuffersChanged = true;
1624 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001625 }
Wonsik Kimf0e7d222019-06-28 12:33:16 -07001626 numOutputSlots = output->numSlots;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001627 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001628
1629 if (outputBuffersChanged) {
1630 mCCodecCallback->onOutputBuffersChanged();
1631 }
1632 }
1633 }
1634 break;
1635 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001636 default:
1637 ALOGV("[%s] onWorkDone: unrecognized config update (%08X)",
1638 mName, param->index());
1639 break;
1640 }
1641 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001642 if (newInputDelay || newPipelineDelay) {
1643 Mutexed<Input>::Locked input(mInput);
1644 size_t newNumSlots =
1645 newInputDelay.value_or(input->inputDelay) +
1646 newPipelineDelay.value_or(input->pipelineDelay) +
1647 kSmoothnessFactor;
1648 if (input->buffers->isArrayMode()) {
1649 if (input->numSlots >= newNumSlots) {
1650 input->numExtraSlots = 0;
1651 } else {
1652 input->numExtraSlots = newNumSlots - input->numSlots;
1653 }
1654 ALOGV("[%s] onWorkDone: updated number of extra slots to %zu (input array mode)",
1655 mName, input->numExtraSlots);
1656 } else {
1657 input->numSlots = newNumSlots;
1658 }
1659 }
Wonsik Kim315e40a2020-09-09 14:11:50 -07001660 if (needMaxDequeueBufferCountUpdate) {
1661 size_t numOutputSlots = 0;
1662 uint32_t reorderDepth = 0;
1663 {
1664 Mutexed<Output>::Locked output(mOutput);
1665 numOutputSlots = output->numSlots;
1666 reorderDepth = output->buffers->getReorderDepth();
1667 }
1668 Mutexed<OutputSurface>::Locked output(mOutputSurface);
1669 output->maxDequeueBuffers = numOutputSlots + reorderDepth + kRenderingDepth;
1670 if (output->surface) {
1671 output->surface->setMaxDequeuedBufferCount(output->maxDequeueBuffers);
1672 }
1673 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001674
Pawin Vongmasa36653902018-11-15 00:10:25 -08001675 int32_t flags = 0;
1676 if (worklet->output.flags & C2FrameData::FLAG_END_OF_STREAM) {
1677 flags |= MediaCodec::BUFFER_FLAG_EOS;
1678 ALOGV("[%s] onWorkDone: output EOS", mName);
1679 }
1680
Pawin Vongmasa36653902018-11-15 00:10:25 -08001681 // WORKAROUND: adjust output timestamp based on client input timestamp and codec
1682 // input timestamp. Codec output timestamp (in the timestamp field) shall correspond to
1683 // the codec input timestamp, but client output timestamp should (reported in timeUs)
1684 // shall correspond to the client input timesamp (in customOrdinal). By using the
1685 // delta between the two, this allows for some timestamp deviation - e.g. if one input
1686 // produces multiple output.
1687 c2_cntr64_t timestamp =
1688 worklet->output.ordinal.timestamp + work->input.ordinal.customOrdinal
1689 - work->input.ordinal.timestamp;
Wonsik Kim95ba0162019-03-19 15:51:54 -07001690 if (mInputSurface != nullptr) {
1691 // When using input surface we need to restore the original input timestamp.
1692 timestamp = work->input.ordinal.customOrdinal;
1693 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001694 ALOGV("[%s] onWorkDone: input %lld, codec %lld => output %lld => %lld",
1695 mName,
1696 work->input.ordinal.customOrdinal.peekll(),
1697 work->input.ordinal.timestamp.peekll(),
1698 worklet->output.ordinal.timestamp.peekll(),
1699 timestamp.peekll());
1700
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001701 // csd cannot be re-ordered and will always arrive first.
Pawin Vongmasa36653902018-11-15 00:10:25 -08001702 if (initData != nullptr) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001703 Mutexed<Output>::Locked output(mOutput);
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001704 if (output->buffers && outputFormat) {
1705 output->buffers->updateSkipCutBuffer(outputFormat);
1706 output->buffers->setFormat(outputFormat);
1707 }
1708 if (!notifyClient) {
1709 return false;
1710 }
1711 size_t index;
1712 sp<MediaCodecBuffer> outBuffer;
Wonsik Kim936a89c2020-05-08 16:07:50 -07001713 if (output->buffers && output->buffers->registerCsd(initData, &index, &outBuffer) == OK) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001714 outBuffer->meta()->setInt64("timeUs", timestamp.peek());
1715 outBuffer->meta()->setInt32("flags", MediaCodec::BUFFER_FLAG_CODECCONFIG);
1716 ALOGV("[%s] onWorkDone: csd index = %zu [%p]", mName, index, outBuffer.get());
1717
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001718 output.unlock();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001719 mCallback->onOutputBufferAvailable(index, outBuffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001720 } else {
1721 ALOGD("[%s] onWorkDone: unable to register csd", mName);
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001722 output.unlock();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001723 mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001724 return false;
1725 }
1726 }
1727
ted.sunb8fe01e2020-06-23 14:03:41 +08001728 bool drop = false;
1729 if (worklet->output.flags & C2FrameData::FLAG_DROP_FRAME) {
1730 ALOGV("[%s] onWorkDone: drop buffer but keep metadata", mName);
1731 drop = true;
1732 }
1733
ted.sun04698a32020-06-23 14:03:41 +08001734 if (notifyClient && !buffer && !flags && !(drop && outputFormat)) {
Wonsik Kim35bf5732020-05-14 17:40:29 +00001735 ALOGV("[%s] onWorkDone: Not reporting output buffer (%lld)",
Pawin Vongmasa36653902018-11-15 00:10:25 -08001736 mName, work->input.ordinal.frameIndex.peekull());
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001737 notifyClient = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001738 }
1739
1740 if (buffer) {
1741 for (const std::shared_ptr<const C2Info> &info : buffer->info()) {
1742 // TODO: properly translate these to metadata
1743 switch (info->coreIndex().coreIndex()) {
1744 case C2StreamPictureTypeMaskInfo::CORE_INDEX:
Lajos Molnar3bb81cd2019-02-20 15:10:30 -08001745 if (((C2StreamPictureTypeMaskInfo *)info.get())->value & C2Config::SYNC_FRAME) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001746 flags |= MediaCodec::BUFFER_FLAG_SYNCFRAME;
1747 }
1748 break;
1749 default:
1750 break;
1751 }
1752 }
1753 }
1754
1755 {
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001756 Mutexed<Output>::Locked output(mOutput);
Wonsik Kimc23cc402020-05-28 14:53:40 -07001757 if (!output->buffers) {
1758 return false;
1759 }
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001760 output->buffers->pushToStash(
ted.sun04698a32020-06-23 14:03:41 +08001761 drop ? nullptr : buffer,
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001762 notifyClient,
1763 timestamp.peek(),
1764 flags,
1765 outputFormat,
1766 worklet->output.ordinal);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001767 }
1768 sendOutputBuffers();
1769 return true;
1770}
1771
1772void CCodecBufferChannel::sendOutputBuffers() {
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001773 OutputBuffers::BufferAction action;
Wonsik Kima4e049d2020-04-28 19:42:23 +00001774 size_t index;
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001775 sp<MediaCodecBuffer> outBuffer;
1776 std::shared_ptr<C2Buffer> c2Buffer;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001777
1778 while (true) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001779 Mutexed<Output>::Locked output(mOutput);
Wonsik Kim936a89c2020-05-08 16:07:50 -07001780 if (!output->buffers) {
1781 return;
1782 }
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001783 action = output->buffers->popFromStashAndRegister(
1784 &c2Buffer, &index, &outBuffer);
1785 switch (action) {
1786 case OutputBuffers::SKIP:
1787 return;
1788 case OutputBuffers::DISCARD:
1789 break;
1790 case OutputBuffers::NOTIFY_CLIENT:
Wonsik Kima4e049d2020-04-28 19:42:23 +00001791 output.unlock();
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001792 mCallback->onOutputBufferAvailable(index, outBuffer);
1793 break;
1794 case OutputBuffers::REALLOCATE:
1795 if (!output->buffers->isArrayMode()) {
1796 output->buffers =
1797 output->buffers->toArrayMode(output->numSlots);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001798 }
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001799 static_cast<OutputBuffersArray*>(output->buffers.get())->
1800 realloc(c2Buffer);
1801 output.unlock();
1802 mCCodecCallback->onOutputBuffersChanged();
Wonsik Kim4ada73d2020-05-26 14:58:07 -07001803 break;
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001804 case OutputBuffers::RETRY:
1805 ALOGV("[%s] sendOutputBuffers: unable to register output buffer",
1806 mName);
1807 return;
1808 default:
1809 LOG_ALWAYS_FATAL("[%s] sendOutputBuffers: "
1810 "corrupted BufferAction value (%d) "
1811 "returned from popFromStashAndRegister.",
1812 mName, int(action));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001813 return;
1814 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001815 }
1816}
1817
1818status_t CCodecBufferChannel::setSurface(const sp<Surface> &newSurface) {
1819 static std::atomic_uint32_t surfaceGeneration{0};
1820 uint32_t generation = (getpid() << 10) |
1821 ((surfaceGeneration.fetch_add(1, std::memory_order_relaxed) + 1)
1822 & ((1 << 10) - 1));
1823
1824 sp<IGraphicBufferProducer> producer;
1825 if (newSurface) {
1826 newSurface->setScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Sungtak Leeab6f2f32019-02-15 14:43:51 -08001827 newSurface->setDequeueTimeout(kDequeueTimeoutNs);
Sungtak Lee08515812019-06-05 11:16:32 -07001828 newSurface->setMaxDequeuedBufferCount(mOutputSurface.lock()->maxDequeueBuffers);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001829 producer = newSurface->getIGraphicBufferProducer();
1830 producer->setGenerationNumber(generation);
1831 } else {
1832 ALOGE("[%s] setting output surface to null", mName);
1833 return INVALID_OPERATION;
1834 }
1835
1836 std::shared_ptr<Codec2Client::Configurable> outputPoolIntf;
1837 C2BlockPool::local_id_t outputPoolId;
1838 {
1839 Mutexed<BlockPools>::Locked pools(mBlockPools);
1840 outputPoolId = pools->outputPoolId;
1841 outputPoolIntf = pools->outputPoolIntf;
1842 }
1843
1844 if (outputPoolIntf) {
1845 if (mComponent->setOutputSurface(
1846 outputPoolId,
1847 producer,
1848 generation) != C2_OK) {
1849 ALOGI("[%s] setSurface: component setOutputSurface failed", mName);
1850 return INVALID_OPERATION;
1851 }
1852 }
1853
1854 {
1855 Mutexed<OutputSurface>::Locked output(mOutputSurface);
1856 output->surface = newSurface;
1857 output->generation = generation;
1858 }
1859
1860 return OK;
1861}
1862
Wonsik Kimab34ed62019-01-31 15:28:46 -08001863PipelineWatcher::Clock::duration CCodecBufferChannel::elapsed() {
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -08001864 // When client pushed EOS, we want all the work to be done quickly.
1865 // Otherwise, component may have stalled work due to input starvation up to
1866 // the sum of the delay in the pipeline.
Wonsik Kimf0e7d222019-06-28 12:33:16 -07001867 size_t n = 0;
1868 if (!mInputMetEos) {
1869 size_t outputDelay = mOutput.lock()->outputDelay;
1870 Mutexed<Input>::Locked input(mInput);
1871 n = input->inputDelay + input->pipelineDelay + outputDelay;
1872 }
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -08001873 return mPipelineWatcher.lock()->elapsed(PipelineWatcher::Clock::now(), n);
Wonsik Kimab34ed62019-01-31 15:28:46 -08001874}
1875
Pawin Vongmasa36653902018-11-15 00:10:25 -08001876void CCodecBufferChannel::setMetaMode(MetaMode mode) {
1877 mMetaMode = mode;
1878}
1879
Wonsik Kim596187e2019-10-25 12:44:10 -07001880void CCodecBufferChannel::setCrypto(const sp<ICrypto> &crypto) {
Wonsik Kimfb7a7672019-12-27 17:13:33 -08001881 if (mCrypto != nullptr) {
1882 for (std::pair<wp<HidlMemory>, int32_t> entry : mHeapSeqNumMap) {
1883 mCrypto->unsetHeap(entry.second);
1884 }
1885 mHeapSeqNumMap.clear();
1886 if (mHeapSeqNum >= 0) {
1887 mCrypto->unsetHeap(mHeapSeqNum);
1888 mHeapSeqNum = -1;
1889 }
1890 }
Wonsik Kim596187e2019-10-25 12:44:10 -07001891 mCrypto = crypto;
1892}
1893
1894void CCodecBufferChannel::setDescrambler(const sp<IDescrambler> &descrambler) {
1895 mDescrambler = descrambler;
1896}
1897
Pawin Vongmasa36653902018-11-15 00:10:25 -08001898status_t toStatusT(c2_status_t c2s, c2_operation_t c2op) {
1899 // C2_OK is always translated to OK.
1900 if (c2s == C2_OK) {
1901 return OK;
1902 }
1903
1904 // Operation-dependent translation
1905 // TODO: Add as necessary
1906 switch (c2op) {
1907 case C2_OPERATION_Component_start:
1908 switch (c2s) {
1909 case C2_NO_MEMORY:
1910 return NO_MEMORY;
1911 default:
1912 return UNKNOWN_ERROR;
1913 }
1914 default:
1915 break;
1916 }
1917
1918 // Backup operation-agnostic translation
1919 switch (c2s) {
1920 case C2_BAD_INDEX:
1921 return BAD_INDEX;
1922 case C2_BAD_VALUE:
1923 return BAD_VALUE;
1924 case C2_BLOCKING:
1925 return WOULD_BLOCK;
1926 case C2_DUPLICATE:
1927 return ALREADY_EXISTS;
1928 case C2_NO_INIT:
1929 return NO_INIT;
1930 case C2_NO_MEMORY:
1931 return NO_MEMORY;
1932 case C2_NOT_FOUND:
1933 return NAME_NOT_FOUND;
1934 case C2_TIMED_OUT:
1935 return TIMED_OUT;
1936 case C2_BAD_STATE:
1937 case C2_CANCELED:
1938 case C2_CANNOT_DO:
1939 case C2_CORRUPTED:
1940 case C2_OMITTED:
1941 case C2_REFUSED:
1942 return UNKNOWN_ERROR;
1943 default:
1944 return -static_cast<status_t>(c2s);
1945 }
1946}
1947
1948} // namespace android