blob: 813d85bd659d1cffc5e12dff7566f79e5b1de8aa [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),
Sungtak Lee04b30352020-07-27 13:57:25 -0700146 mInputMetEos(false),
147 mSendEncryptedInfoBuffer(false) {
Sungtak Leed7463d12019-09-04 16:01:00 -0700148 mOutputSurface.lock()->maxDequeueBuffers = kSmoothnessFactor + kRenderingDepth;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700149 {
150 Mutexed<Input>::Locked input(mInput);
151 input->buffers.reset(new DummyInputBuffers(""));
152 input->extraBuffers.flush();
153 input->inputDelay = 0u;
154 input->pipelineDelay = 0u;
155 input->numSlots = kSmoothnessFactor;
156 input->numExtraSlots = 0u;
157 }
158 {
159 Mutexed<Output>::Locked output(mOutput);
160 output->outputDelay = 0u;
161 output->numSlots = kSmoothnessFactor;
162 }
David Stevensc3fbb282021-01-18 18:11:20 +0900163 {
164 Mutexed<BlockPools>::Locked pools(mBlockPools);
165 pools->outputPoolId = C2BlockPool::BASIC_LINEAR;
166 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800167}
168
169CCodecBufferChannel::~CCodecBufferChannel() {
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800170 if (mCrypto != nullptr && mHeapSeqNum >= 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800171 mCrypto->unsetHeap(mHeapSeqNum);
172 }
173}
174
175void CCodecBufferChannel::setComponent(
176 const std::shared_ptr<Codec2Client::Component> &component) {
177 mComponent = component;
178 mComponentName = component->getName() + StringPrintf("#%d", int(uintptr_t(component.get()) % 997));
179 mName = mComponentName.c_str();
180}
181
182status_t CCodecBufferChannel::setInputSurface(
183 const std::shared_ptr<InputSurfaceWrapper> &surface) {
184 ALOGV("[%s] setInputSurface", mName);
185 mInputSurface = surface;
186 return mInputSurface->connect(mComponent);
187}
188
189status_t CCodecBufferChannel::signalEndOfInputStream() {
190 if (mInputSurface == nullptr) {
191 return INVALID_OPERATION;
192 }
193 return mInputSurface->signalEndOfInputStream();
194}
195
Sungtak Lee04b30352020-07-27 13:57:25 -0700196status_t CCodecBufferChannel::queueInputBufferInternal(
197 sp<MediaCodecBuffer> buffer,
198 std::shared_ptr<C2LinearBlock> encryptedBlock,
199 size_t blockSize) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800200 int64_t timeUs;
201 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
202
203 if (mInputMetEos) {
204 ALOGD("[%s] buffers after EOS ignored (%lld us)", mName, (long long)timeUs);
205 return OK;
206 }
207
208 int32_t flags = 0;
209 int32_t tmp = 0;
210 bool eos = false;
211 if (buffer->meta()->findInt32("eos", &tmp) && tmp) {
212 eos = true;
213 mInputMetEos = true;
214 ALOGV("[%s] input EOS", mName);
215 }
216 if (buffer->meta()->findInt32("csd", &tmp) && tmp) {
217 flags |= C2FrameData::FLAG_CODEC_CONFIG;
218 }
219 ALOGV("[%s] queueInputBuffer: buffer->size() = %zu", mName, buffer->size());
220 std::unique_ptr<C2Work> work(new C2Work);
221 work->input.ordinal.timestamp = timeUs;
222 work->input.ordinal.frameIndex = mFrameIndex++;
223 // WORKAROUND: until codecs support handling work after EOS and max output sizing, use timestamp
224 // manipulation to achieve image encoding via video codec, and to constrain encoded output.
225 // Keep client timestamp in customOrdinal
226 work->input.ordinal.customOrdinal = timeUs;
227 work->input.buffers.clear();
228
Wonsik Kimab34ed62019-01-31 15:28:46 -0800229 uint64_t queuedFrameIndex = work->input.ordinal.frameIndex.peeku();
230 std::vector<std::shared_ptr<C2Buffer>> queuedBuffers;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700231 sp<Codec2Buffer> copy;
Wonsik Kimab34ed62019-01-31 15:28:46 -0800232
Pawin Vongmasa36653902018-11-15 00:10:25 -0800233 if (buffer->size() > 0u) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700234 Mutexed<Input>::Locked input(mInput);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800235 std::shared_ptr<C2Buffer> c2buffer;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700236 if (!input->buffers->releaseBuffer(buffer, &c2buffer, false)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800237 return -ENOENT;
238 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700239 // TODO: we want to delay copying buffers.
240 if (input->extraBuffers.numComponentBuffers() < input->numExtraSlots) {
241 copy = input->buffers->cloneAndReleaseBuffer(buffer);
242 if (copy != nullptr) {
243 (void)input->extraBuffers.assignSlot(copy);
244 if (!input->extraBuffers.releaseSlot(copy, &c2buffer, false)) {
245 return UNKNOWN_ERROR;
246 }
247 bool released = input->buffers->releaseBuffer(buffer, nullptr, true);
248 ALOGV("[%s] queueInputBuffer: buffer copied; %sreleased",
249 mName, released ? "" : "not ");
250 buffer.clear();
251 } else {
252 ALOGW("[%s] queueInputBuffer: failed to copy a buffer; this may cause input "
253 "buffer starvation on component.", mName);
254 }
255 }
Byeongjo Park2eef13e2020-06-12 17:24:21 +0900256 int32_t cvo = 0;
257 if (buffer->meta()->findInt32("cvo", &cvo)) {
258 int32_t rotation = cvo % 360;
259 // change rotation to counter-clock wise.
260 rotation = ((rotation <= 0) ? 0 : 360) - rotation;
261 Mutexed<OutputSurface>::Locked output(mOutputSurface);
262 output->rotation[queuedFrameIndex] = rotation;
263 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800264 work->input.buffers.push_back(c2buffer);
Sungtak Lee04b30352020-07-27 13:57:25 -0700265 if (encryptedBlock) {
266 work->input.infoBuffers.emplace_back(C2InfoBuffer::CreateLinearBuffer(
267 kParamIndexEncryptedBuffer,
268 encryptedBlock->share(0, blockSize, C2Fence())));
269 }
Wonsik Kimab34ed62019-01-31 15:28:46 -0800270 queuedBuffers.push_back(c2buffer);
271 } else if (eos) {
272 flags |= C2FrameData::FLAG_END_OF_STREAM;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800273 }
274 work->input.flags = (C2FrameData::flags_t)flags;
275 // TODO: fill info's
276
277 work->input.configUpdate = std::move(mParamsToBeSet);
278 work->worklets.clear();
279 work->worklets.emplace_back(new C2Worklet);
280
281 std::list<std::unique_ptr<C2Work>> items;
282 items.push_back(std::move(work));
Wonsik Kimab34ed62019-01-31 15:28:46 -0800283 mPipelineWatcher.lock()->onWorkQueued(
284 queuedFrameIndex,
285 std::move(queuedBuffers),
286 PipelineWatcher::Clock::now());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800287 c2_status_t err = mComponent->queue(&items);
Wonsik Kimab34ed62019-01-31 15:28:46 -0800288 if (err != C2_OK) {
289 mPipelineWatcher.lock()->onWorkDone(queuedFrameIndex);
290 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800291
292 if (err == C2_OK && eos && buffer->size() > 0u) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800293 work.reset(new C2Work);
294 work->input.ordinal.timestamp = timeUs;
295 work->input.ordinal.frameIndex = mFrameIndex++;
296 // WORKAROUND: keep client timestamp in customOrdinal
297 work->input.ordinal.customOrdinal = timeUs;
298 work->input.buffers.clear();
299 work->input.flags = C2FrameData::FLAG_END_OF_STREAM;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800300 work->worklets.emplace_back(new C2Worklet);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800301
Wonsik Kimab34ed62019-01-31 15:28:46 -0800302 queuedFrameIndex = work->input.ordinal.frameIndex.peeku();
303 queuedBuffers.clear();
304
Pawin Vongmasa36653902018-11-15 00:10:25 -0800305 items.clear();
306 items.push_back(std::move(work));
Wonsik Kimab34ed62019-01-31 15:28:46 -0800307
308 mPipelineWatcher.lock()->onWorkQueued(
309 queuedFrameIndex,
310 std::move(queuedBuffers),
311 PipelineWatcher::Clock::now());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800312 err = mComponent->queue(&items);
Wonsik Kimab34ed62019-01-31 15:28:46 -0800313 if (err != C2_OK) {
314 mPipelineWatcher.lock()->onWorkDone(queuedFrameIndex);
315 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800316 }
317 if (err == C2_OK) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700318 Mutexed<Input>::Locked input(mInput);
319 bool released = false;
320 if (buffer) {
321 released = input->buffers->releaseBuffer(buffer, nullptr, true);
322 } else if (copy) {
323 released = input->extraBuffers.releaseSlot(copy, nullptr, true);
324 }
325 ALOGV("[%s] queueInputBuffer: buffer%s %sreleased",
326 mName, (buffer == nullptr) ? "(copy)" : "", released ? "" : "not ");
Pawin Vongmasa36653902018-11-15 00:10:25 -0800327 }
328
329 feedInputBufferIfAvailableInternal();
330 return err;
331}
332
333status_t CCodecBufferChannel::setParameters(std::vector<std::unique_ptr<C2Param>> &params) {
334 QueueGuard guard(mSync);
335 if (!guard.isRunning()) {
336 ALOGD("[%s] setParameters is only supported in the running state.", mName);
337 return -ENOSYS;
338 }
339 mParamsToBeSet.insert(mParamsToBeSet.end(),
340 std::make_move_iterator(params.begin()),
341 std::make_move_iterator(params.end()));
342 params.clear();
343 return OK;
344}
345
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800346status_t CCodecBufferChannel::attachBuffer(
347 const std::shared_ptr<C2Buffer> &c2Buffer,
348 const sp<MediaCodecBuffer> &buffer) {
349 if (!buffer->copy(c2Buffer)) {
350 return -ENOSYS;
351 }
352 return OK;
353}
354
355void CCodecBufferChannel::ensureDecryptDestination(size_t size) {
356 if (!mDecryptDestination || mDecryptDestination->size() < size) {
357 sp<IMemoryHeap> heap{new MemoryHeapBase(size * 2)};
358 if (mDecryptDestination && mCrypto && mHeapSeqNum >= 0) {
359 mCrypto->unsetHeap(mHeapSeqNum);
360 }
361 mDecryptDestination = new MemoryBase(heap, 0, size * 2);
362 if (mCrypto) {
363 mHeapSeqNum = mCrypto->setHeap(hardware::fromHeap(heap));
364 }
365 }
366}
367
368int32_t CCodecBufferChannel::getHeapSeqNum(const sp<HidlMemory> &memory) {
369 CHECK(mCrypto);
370 auto it = mHeapSeqNumMap.find(memory);
371 int32_t heapSeqNum = -1;
372 if (it == mHeapSeqNumMap.end()) {
373 heapSeqNum = mCrypto->setHeap(memory);
374 mHeapSeqNumMap.emplace(memory, heapSeqNum);
375 } else {
376 heapSeqNum = it->second;
377 }
378 return heapSeqNum;
379}
380
381status_t CCodecBufferChannel::attachEncryptedBuffer(
382 const sp<hardware::HidlMemory> &memory,
383 bool secure,
384 const uint8_t *key,
385 const uint8_t *iv,
386 CryptoPlugin::Mode mode,
387 CryptoPlugin::Pattern pattern,
388 size_t offset,
389 const CryptoPlugin::SubSample *subSamples,
390 size_t numSubSamples,
391 const sp<MediaCodecBuffer> &buffer) {
392 static const C2MemoryUsage kSecureUsage{C2MemoryUsage::READ_PROTECTED, 0};
393 static const C2MemoryUsage kDefaultReadWriteUsage{
394 C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE};
395
396 size_t size = 0;
397 for (size_t i = 0; i < numSubSamples; ++i) {
398 size += subSamples[i].mNumBytesOfClearData + subSamples[i].mNumBytesOfEncryptedData;
399 }
400 std::shared_ptr<C2BlockPool> pool = mBlockPools.lock()->inputPool;
401 std::shared_ptr<C2LinearBlock> block;
402 c2_status_t err = pool->fetchLinearBlock(
403 size,
404 secure ? kSecureUsage : kDefaultReadWriteUsage,
405 &block);
406 if (err != C2_OK) {
407 return NO_MEMORY;
408 }
409 if (!secure) {
410 ensureDecryptDestination(size);
411 }
412 ssize_t result = -1;
413 ssize_t codecDataOffset = 0;
414 if (mCrypto) {
415 AString errorDetailMsg;
416 int32_t heapSeqNum = getHeapSeqNum(memory);
417 hardware::drm::V1_0::SharedBuffer src{(uint32_t)heapSeqNum, offset, size};
418 hardware::drm::V1_0::DestinationBuffer dst;
419 if (secure) {
420 dst.type = DrmBufferType::NATIVE_HANDLE;
421 dst.secureMemory = hardware::hidl_handle(block->handle());
422 } else {
423 dst.type = DrmBufferType::SHARED_MEMORY;
424 IMemoryToSharedBuffer(
425 mDecryptDestination, mHeapSeqNum, &dst.nonsecureMemory);
426 }
427 result = mCrypto->decrypt(
428 key, iv, mode, pattern, src, 0, subSamples, numSubSamples,
429 dst, &errorDetailMsg);
430 if (result < 0) {
431 return result;
432 }
433 if (dst.type == DrmBufferType::SHARED_MEMORY) {
434 C2WriteView view = block->map().get();
435 if (view.error() != C2_OK) {
436 return false;
437 }
438 if (view.size() < result) {
439 return false;
440 }
441 memcpy(view.data(), mDecryptDestination->unsecurePointer(), result);
442 }
443 } else {
444 // Here we cast CryptoPlugin::SubSample to hardware::cas::native::V1_0::SubSample
445 // directly, the structure definitions should match as checked in DescramblerImpl.cpp.
446 hidl_vec<SubSample> hidlSubSamples;
447 hidlSubSamples.setToExternal((SubSample *)subSamples, numSubSamples, false /*own*/);
448
449 hardware::cas::native::V1_0::SharedBuffer src{*memory, offset, size};
450 hardware::cas::native::V1_0::DestinationBuffer dst;
451 if (secure) {
452 dst.type = BufferType::NATIVE_HANDLE;
453 dst.secureMemory = hardware::hidl_handle(block->handle());
454 } else {
455 dst.type = BufferType::SHARED_MEMORY;
456 dst.nonsecureMemory = src;
457 }
458
459 CasStatus status = CasStatus::OK;
460 hidl_string detailedError;
461 ScramblingControl sctrl = ScramblingControl::UNSCRAMBLED;
462
463 if (key != nullptr) {
464 sctrl = (ScramblingControl)key[0];
465 // Adjust for the PES offset
466 codecDataOffset = key[2] | (key[3] << 8);
467 }
468
469 auto returnVoid = mDescrambler->descramble(
470 sctrl,
471 hidlSubSamples,
472 src,
473 0,
474 dst,
475 0,
476 [&status, &result, &detailedError] (
477 CasStatus _status, uint32_t _bytesWritten,
478 const hidl_string& _detailedError) {
479 status = _status;
480 result = (ssize_t)_bytesWritten;
481 detailedError = _detailedError;
482 });
483
484 if (!returnVoid.isOk() || status != CasStatus::OK || result < 0) {
485 ALOGI("[%s] descramble failed, trans=%s, status=%d, result=%zd",
486 mName, returnVoid.description().c_str(), status, result);
487 return UNKNOWN_ERROR;
488 }
489
490 if (result < codecDataOffset) {
491 ALOGD("invalid codec data offset: %zd, result %zd", codecDataOffset, result);
492 return BAD_VALUE;
493 }
494 }
495 if (!secure) {
496 C2WriteView view = block->map().get();
497 if (view.error() != C2_OK) {
498 return UNKNOWN_ERROR;
499 }
500 if (view.size() < result) {
501 return UNKNOWN_ERROR;
502 }
503 memcpy(view.data(), mDecryptDestination->unsecurePointer(), result);
504 }
505 std::shared_ptr<C2Buffer> c2Buffer{C2Buffer::CreateLinearBuffer(
506 block->share(codecDataOffset, result - codecDataOffset, C2Fence{}))};
507 if (!buffer->copy(c2Buffer)) {
508 return -ENOSYS;
509 }
510 return OK;
511}
512
Pawin Vongmasa36653902018-11-15 00:10:25 -0800513status_t CCodecBufferChannel::queueInputBuffer(const sp<MediaCodecBuffer> &buffer) {
514 QueueGuard guard(mSync);
515 if (!guard.isRunning()) {
516 ALOGD("[%s] No more buffers should be queued at current state.", mName);
517 return -ENOSYS;
518 }
519 return queueInputBufferInternal(buffer);
520}
521
522status_t CCodecBufferChannel::queueSecureInputBuffer(
523 const sp<MediaCodecBuffer> &buffer, bool secure, const uint8_t *key,
524 const uint8_t *iv, CryptoPlugin::Mode mode, CryptoPlugin::Pattern pattern,
525 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
526 AString *errorDetailMsg) {
527 QueueGuard guard(mSync);
528 if (!guard.isRunning()) {
529 ALOGD("[%s] No more buffers should be queued at current state.", mName);
530 return -ENOSYS;
531 }
532
533 if (!hasCryptoOrDescrambler()) {
534 return -ENOSYS;
535 }
536 sp<EncryptedLinearBlockBuffer> encryptedBuffer((EncryptedLinearBlockBuffer *)buffer.get());
537
Sungtak Lee04b30352020-07-27 13:57:25 -0700538 std::shared_ptr<C2LinearBlock> block;
539 size_t allocSize = buffer->size();
540 size_t bufferSize = 0;
541 c2_status_t blockRes = C2_OK;
542 bool copied = false;
543 if (mSendEncryptedInfoBuffer) {
544 static const C2MemoryUsage kDefaultReadWriteUsage{
545 C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE};
546 constexpr int kAllocGranule0 = 1024 * 64;
547 constexpr int kAllocGranule1 = 1024 * 1024;
548 std::shared_ptr<C2BlockPool> pool = mBlockPools.lock()->inputPool;
549 // round up encrypted sizes to limit fragmentation and encourage buffer reuse
550 if (allocSize <= kAllocGranule1) {
551 bufferSize = align(allocSize, kAllocGranule0);
552 } else {
553 bufferSize = align(allocSize, kAllocGranule1);
554 }
555 blockRes = pool->fetchLinearBlock(
556 bufferSize, kDefaultReadWriteUsage, &block);
557
558 if (blockRes == C2_OK) {
559 C2WriteView view = block->map().get();
560 if (view.error() == C2_OK && view.size() == bufferSize) {
561 copied = true;
562 // TODO: only copy clear sections
563 memcpy(view.data(), buffer->data(), allocSize);
564 }
565 }
566 }
567
568 if (!copied) {
569 block.reset();
570 }
571
Pawin Vongmasa36653902018-11-15 00:10:25 -0800572 ssize_t result = -1;
573 ssize_t codecDataOffset = 0;
Wonsik Kim557c88c2020-03-13 11:03:52 -0700574 if (numSubSamples == 1
575 && subSamples[0].mNumBytesOfClearData == 0
576 && subSamples[0].mNumBytesOfEncryptedData == 0) {
577 // We don't need to go through crypto or descrambler if the input is empty.
578 result = 0;
579 } else if (mCrypto != nullptr) {
Robert Shih895fba92019-07-16 16:29:44 -0700580 hardware::drm::V1_0::DestinationBuffer destination;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800581 if (secure) {
Robert Shih895fba92019-07-16 16:29:44 -0700582 destination.type = DrmBufferType::NATIVE_HANDLE;
583 destination.secureMemory = hidl_handle(encryptedBuffer->handle());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800584 } else {
Robert Shih895fba92019-07-16 16:29:44 -0700585 destination.type = DrmBufferType::SHARED_MEMORY;
586 IMemoryToSharedBuffer(
587 mDecryptDestination, mHeapSeqNum, &destination.nonsecureMemory);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800588 }
Robert Shih895fba92019-07-16 16:29:44 -0700589 hardware::drm::V1_0::SharedBuffer source;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800590 encryptedBuffer->fillSourceBuffer(&source);
591 result = mCrypto->decrypt(
592 key, iv, mode, pattern, source, buffer->offset(),
593 subSamples, numSubSamples, destination, errorDetailMsg);
594 if (result < 0) {
Wonsik Kim557c88c2020-03-13 11:03:52 -0700595 ALOGI("[%s] decrypt failed: result=%zd", mName, result);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800596 return result;
597 }
Robert Shih895fba92019-07-16 16:29:44 -0700598 if (destination.type == DrmBufferType::SHARED_MEMORY) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800599 encryptedBuffer->copyDecryptedContent(mDecryptDestination, result);
600 }
601 } else {
602 // Here we cast CryptoPlugin::SubSample to hardware::cas::native::V1_0::SubSample
603 // directly, the structure definitions should match as checked in DescramblerImpl.cpp.
604 hidl_vec<SubSample> hidlSubSamples;
605 hidlSubSamples.setToExternal((SubSample *)subSamples, numSubSamples, false /*own*/);
606
607 hardware::cas::native::V1_0::SharedBuffer srcBuffer;
608 encryptedBuffer->fillSourceBuffer(&srcBuffer);
609
610 DestinationBuffer dstBuffer;
611 if (secure) {
612 dstBuffer.type = BufferType::NATIVE_HANDLE;
613 dstBuffer.secureMemory = hidl_handle(encryptedBuffer->handle());
614 } else {
615 dstBuffer.type = BufferType::SHARED_MEMORY;
616 dstBuffer.nonsecureMemory = srcBuffer;
617 }
618
619 CasStatus status = CasStatus::OK;
620 hidl_string detailedError;
621 ScramblingControl sctrl = ScramblingControl::UNSCRAMBLED;
622
623 if (key != nullptr) {
624 sctrl = (ScramblingControl)key[0];
625 // Adjust for the PES offset
626 codecDataOffset = key[2] | (key[3] << 8);
627 }
628
629 auto returnVoid = mDescrambler->descramble(
630 sctrl,
631 hidlSubSamples,
632 srcBuffer,
633 0,
634 dstBuffer,
635 0,
636 [&status, &result, &detailedError] (
637 CasStatus _status, uint32_t _bytesWritten,
638 const hidl_string& _detailedError) {
639 status = _status;
640 result = (ssize_t)_bytesWritten;
641 detailedError = _detailedError;
642 });
643
644 if (!returnVoid.isOk() || status != CasStatus::OK || result < 0) {
645 ALOGI("[%s] descramble failed, trans=%s, status=%d, result=%zd",
646 mName, returnVoid.description().c_str(), status, result);
647 return UNKNOWN_ERROR;
648 }
649
650 if (result < codecDataOffset) {
651 ALOGD("invalid codec data offset: %zd, result %zd", codecDataOffset, result);
652 return BAD_VALUE;
653 }
654
655 ALOGV("[%s] descramble succeeded, %zd bytes", mName, result);
656
657 if (dstBuffer.type == BufferType::SHARED_MEMORY) {
658 encryptedBuffer->copyDecryptedContentFromMemory(result);
659 }
660 }
661
662 buffer->setRange(codecDataOffset, result - codecDataOffset);
Sungtak Lee04b30352020-07-27 13:57:25 -0700663
664 return queueInputBufferInternal(buffer, block, bufferSize);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800665}
666
667void CCodecBufferChannel::feedInputBufferIfAvailable() {
668 QueueGuard guard(mSync);
669 if (!guard.isRunning()) {
670 ALOGV("[%s] We're not running --- no input buffer reported", mName);
671 return;
672 }
673 feedInputBufferIfAvailableInternal();
674}
675
676void CCodecBufferChannel::feedInputBufferIfAvailableInternal() {
Taehwan Kimda0517d2020-09-16 17:29:37 +0900677 if (mInputMetEos) {
Wonsik Kimdf5dd142019-02-06 10:15:46 -0800678 return;
Pawin Vongmasac3c536d2020-06-12 04:00:04 -0700679 }
680 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700681 Mutexed<Output>::Locked output(mOutput);
Pawin Vongmasac3c536d2020-06-12 04:00:04 -0700682 if (!output->buffers ||
683 output->buffers->hasPending() ||
Wonsik Kim0487b782020-10-28 11:45:50 -0700684 output->buffers->numActiveSlots() >= output->numSlots) {
Wonsik Kimdf5dd142019-02-06 10:15:46 -0800685 return;
686 }
687 }
Wonsik Kim0487b782020-10-28 11:45:50 -0700688 size_t numActiveSlots = 0;
689 while (!mPipelineWatcher.lock()->pipelineFull()) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800690 sp<MediaCodecBuffer> inBuffer;
691 size_t index;
692 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700693 Mutexed<Input>::Locked input(mInput);
Wonsik Kim0487b782020-10-28 11:45:50 -0700694 numActiveSlots = input->buffers->numActiveSlots();
695 if (numActiveSlots >= input->numSlots) {
696 break;
Wonsik Kimab34ed62019-01-31 15:28:46 -0800697 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700698 if (!input->buffers->requestNewBuffer(&index, &inBuffer)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800699 ALOGV("[%s] no new buffer available", mName);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800700 break;
701 }
702 }
703 ALOGV("[%s] new input index = %zu [%p]", mName, index, inBuffer.get());
704 mCallback->onInputBufferAvailable(index, inBuffer);
705 }
Wonsik Kim0487b782020-10-28 11:45:50 -0700706 ALOGV("[%s] # active slots after feedInputBufferIfAvailable = %zu", mName, numActiveSlots);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800707}
708
709status_t CCodecBufferChannel::renderOutputBuffer(
710 const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) {
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800711 ALOGV("[%s] renderOutputBuffer: %p", mName, buffer.get());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800712 std::shared_ptr<C2Buffer> c2Buffer;
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800713 bool released = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800714 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700715 Mutexed<Output>::Locked output(mOutput);
716 if (output->buffers) {
717 released = output->buffers->releaseBuffer(buffer, &c2Buffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800718 }
719 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800720 // NOTE: some apps try to releaseOutputBuffer() with timestamp and/or render
721 // set to true.
722 sendOutputBuffers();
723 // input buffer feeding may have been gated by pending output buffers
724 feedInputBufferIfAvailable();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800725 if (!c2Buffer) {
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800726 if (released) {
Wonsik Kimf7529dd2019-04-18 17:35:53 -0700727 std::call_once(mRenderWarningFlag, [this] {
728 ALOGW("[%s] The app is calling releaseOutputBuffer() with "
729 "timestamp or render=true with non-video buffers. Apps should "
730 "call releaseOutputBuffer() with render=false for those.",
731 mName);
732 });
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800733 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800734 return INVALID_OPERATION;
735 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800736
737#if 0
738 const std::vector<std::shared_ptr<const C2Info>> infoParams = c2Buffer->info();
739 ALOGV("[%s] queuing gfx buffer with %zu infos", mName, infoParams.size());
740 for (const std::shared_ptr<const C2Info> &info : infoParams) {
741 AString res;
742 for (size_t ix = 0; ix + 3 < info->size(); ix += 4) {
743 if (ix) res.append(", ");
744 res.append(*((int32_t*)info.get() + (ix / 4)));
745 }
746 ALOGV(" [%s]", res.c_str());
747 }
748#endif
749 std::shared_ptr<const C2StreamRotationInfo::output> rotation =
750 std::static_pointer_cast<const C2StreamRotationInfo::output>(
751 c2Buffer->getInfo(C2StreamRotationInfo::output::PARAM_TYPE));
752 bool flip = rotation && (rotation->flip & 1);
753 uint32_t quarters = ((rotation ? rotation->value : 0) / 90) & 3;
Byeongjo Park2eef13e2020-06-12 17:24:21 +0900754
755 {
756 Mutexed<OutputSurface>::Locked output(mOutputSurface);
757 if (output->surface == nullptr) {
758 ALOGI("[%s] cannot render buffer without surface", mName);
759 return OK;
760 }
761 int64_t frameIndex;
762 buffer->meta()->findInt64("frameIndex", &frameIndex);
763 if (output->rotation.count(frameIndex) != 0) {
764 auto it = output->rotation.find(frameIndex);
765 quarters = (it->second / 90) & 3;
766 output->rotation.erase(it);
767 }
768 }
769
Pawin Vongmasa36653902018-11-15 00:10:25 -0800770 uint32_t transform = 0;
771 switch (quarters) {
772 case 0: // no rotation
773 transform = flip ? HAL_TRANSFORM_FLIP_H : 0;
774 break;
775 case 1: // 90 degrees counter-clockwise
776 transform = flip ? (HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90)
777 : HAL_TRANSFORM_ROT_270;
778 break;
779 case 2: // 180 degrees
780 transform = flip ? HAL_TRANSFORM_FLIP_V : HAL_TRANSFORM_ROT_180;
781 break;
782 case 3: // 90 degrees clockwise
783 transform = flip ? (HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90)
784 : HAL_TRANSFORM_ROT_90;
785 break;
786 }
787
788 std::shared_ptr<const C2StreamSurfaceScalingInfo::output> surfaceScaling =
789 std::static_pointer_cast<const C2StreamSurfaceScalingInfo::output>(
790 c2Buffer->getInfo(C2StreamSurfaceScalingInfo::output::PARAM_TYPE));
791 uint32_t videoScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
792 if (surfaceScaling) {
793 videoScalingMode = surfaceScaling->value;
794 }
795
796 // Use dataspace from format as it has the default aspects already applied
797 android_dataspace_t dataSpace = HAL_DATASPACE_UNKNOWN; // this is 0
798 (void)buffer->format()->findInt32("android._dataspace", (int32_t *)&dataSpace);
799
800 // HDR static info
801 std::shared_ptr<const C2StreamHdrStaticInfo::output> hdrStaticInfo =
802 std::static_pointer_cast<const C2StreamHdrStaticInfo::output>(
803 c2Buffer->getInfo(C2StreamHdrStaticInfo::output::PARAM_TYPE));
804
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800805 // HDR10 plus info
806 std::shared_ptr<const C2StreamHdr10PlusInfo::output> hdr10PlusInfo =
807 std::static_pointer_cast<const C2StreamHdr10PlusInfo::output>(
808 c2Buffer->getInfo(C2StreamHdr10PlusInfo::output::PARAM_TYPE));
Yichi Chen54be23c2020-06-15 14:30:53 +0800809 if (hdr10PlusInfo && hdr10PlusInfo->flexCount() == 0) {
810 hdr10PlusInfo.reset();
811 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800812
Pawin Vongmasa36653902018-11-15 00:10:25 -0800813 std::vector<C2ConstGraphicBlock> blocks = c2Buffer->data().graphicBlocks();
814 if (blocks.size() != 1u) {
815 ALOGD("[%s] expected 1 graphic block, but got %zu", mName, blocks.size());
816 return UNKNOWN_ERROR;
817 }
818 const C2ConstGraphicBlock &block = blocks.front();
819
820 // TODO: revisit this after C2Fence implementation.
821 android::IGraphicBufferProducer::QueueBufferInput qbi(
822 timestampNs,
823 false, // droppable
824 dataSpace,
825 Rect(blocks.front().crop().left,
826 blocks.front().crop().top,
827 blocks.front().crop().right(),
828 blocks.front().crop().bottom()),
829 videoScalingMode,
830 transform,
831 Fence::NO_FENCE, 0);
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800832 if (hdrStaticInfo || hdr10PlusInfo) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800833 HdrMetadata hdr;
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800834 if (hdrStaticInfo) {
wenchangliuf3f92882020-05-14 00:02:01 +0800835 // If mastering max and min luminance fields are 0, do not use them.
836 // It indicates the value may not be present in the stream.
837 if (hdrStaticInfo->mastering.maxLuminance > 0.0f &&
838 hdrStaticInfo->mastering.minLuminance > 0.0f) {
839 struct android_smpte2086_metadata smpte2086_meta = {
840 .displayPrimaryRed = {
841 hdrStaticInfo->mastering.red.x, hdrStaticInfo->mastering.red.y
842 },
843 .displayPrimaryGreen = {
844 hdrStaticInfo->mastering.green.x, hdrStaticInfo->mastering.green.y
845 },
846 .displayPrimaryBlue = {
847 hdrStaticInfo->mastering.blue.x, hdrStaticInfo->mastering.blue.y
848 },
849 .whitePoint = {
850 hdrStaticInfo->mastering.white.x, hdrStaticInfo->mastering.white.y
851 },
852 .maxLuminance = hdrStaticInfo->mastering.maxLuminance,
853 .minLuminance = hdrStaticInfo->mastering.minLuminance,
854 };
Yichi Chen54be23c2020-06-15 14:30:53 +0800855 hdr.validTypes |= HdrMetadata::SMPTE2086;
wenchangliuf3f92882020-05-14 00:02:01 +0800856 hdr.smpte2086 = smpte2086_meta;
857 }
Chong Zhang3bb2a7f2020-04-21 10:35:12 -0700858 // If the content light level fields are 0, do not use them, it
859 // indicates the value may not be present in the stream.
860 if (hdrStaticInfo->maxCll > 0.0f && hdrStaticInfo->maxFall > 0.0f) {
861 struct android_cta861_3_metadata cta861_meta = {
862 .maxContentLightLevel = hdrStaticInfo->maxCll,
863 .maxFrameAverageLightLevel = hdrStaticInfo->maxFall,
864 };
865 hdr.validTypes |= HdrMetadata::CTA861_3;
866 hdr.cta8613 = cta861_meta;
867 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800868 }
869 if (hdr10PlusInfo) {
870 hdr.validTypes |= HdrMetadata::HDR10PLUS;
871 hdr.hdr10plus.assign(
872 hdr10PlusInfo->m.value,
873 hdr10PlusInfo->m.value + hdr10PlusInfo->flexCount());
874 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800875 qbi.setHdrMetadata(hdr);
876 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800877 // we don't have dirty regions
878 qbi.setSurfaceDamage(Region::INVALID_REGION);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800879 android::IGraphicBufferProducer::QueueBufferOutput qbo;
880 status_t result = mComponent->queueToOutputSurface(block, qbi, &qbo);
881 if (result != OK) {
882 ALOGI("[%s] queueBuffer failed: %d", mName, result);
Sungtak Lee47c018a2020-11-07 01:02:49 -0800883 if (result == NO_INIT) {
884 mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
885 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800886 return result;
887 }
888 ALOGV("[%s] queue buffer successful", mName);
889
890 int64_t mediaTimeUs = 0;
891 (void)buffer->meta()->findInt64("timeUs", &mediaTimeUs);
892 mCCodecCallback->onOutputFramesRendered(mediaTimeUs, timestampNs);
893
894 return OK;
895}
896
897status_t CCodecBufferChannel::discardBuffer(const sp<MediaCodecBuffer> &buffer) {
898 ALOGV("[%s] discardBuffer: %p", mName, buffer.get());
899 bool released = false;
900 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700901 Mutexed<Input>::Locked input(mInput);
902 if (input->buffers && input->buffers->releaseBuffer(buffer, nullptr, true)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800903 released = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800904 }
905 }
906 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700907 Mutexed<Output>::Locked output(mOutput);
908 if (output->buffers && output->buffers->releaseBuffer(buffer, nullptr)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800909 released = true;
910 }
911 }
912 if (released) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800913 sendOutputBuffers();
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800914 feedInputBufferIfAvailable();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800915 } else {
916 ALOGD("[%s] MediaCodec discarded an unknown buffer", mName);
917 }
918 return OK;
919}
920
921void CCodecBufferChannel::getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) {
922 array->clear();
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700923 Mutexed<Input>::Locked input(mInput);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800924
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700925 if (!input->buffers->isArrayMode()) {
926 input->buffers = input->buffers->toArrayMode(input->numSlots);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800927 }
928
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700929 input->buffers->getArray(array);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800930}
931
932void CCodecBufferChannel::getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) {
933 array->clear();
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700934 Mutexed<Output>::Locked output(mOutput);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800935
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700936 if (!output->buffers->isArrayMode()) {
937 output->buffers = output->buffers->toArrayMode(output->numSlots);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800938 }
939
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700940 output->buffers->getArray(array);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800941}
942
943status_t CCodecBufferChannel::start(
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800944 const sp<AMessage> &inputFormat,
945 const sp<AMessage> &outputFormat,
946 bool buffersBoundToCodec) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800947 C2StreamBufferTypeSetting::input iStreamFormat(0u);
948 C2StreamBufferTypeSetting::output oStreamFormat(0u);
949 C2PortReorderBufferDepthTuning::output reorderDepth;
950 C2PortReorderKeySetting::output reorderKey;
Wonsik Kim078b58e2019-01-09 15:08:06 -0800951 C2PortActualDelayTuning::input inputDelay(0);
952 C2PortActualDelayTuning::output outputDelay(0);
953 C2ActualPipelineDelayTuning pipelineDelay(0);
Sungtak Lee04b30352020-07-27 13:57:25 -0700954 C2SecureModeTuning secureMode(C2Config::SM_UNPROTECTED);
Wonsik Kim078b58e2019-01-09 15:08:06 -0800955
Pawin Vongmasa36653902018-11-15 00:10:25 -0800956 c2_status_t err = mComponent->query(
957 {
958 &iStreamFormat,
959 &oStreamFormat,
960 &reorderDepth,
961 &reorderKey,
Wonsik Kim078b58e2019-01-09 15:08:06 -0800962 &inputDelay,
963 &pipelineDelay,
964 &outputDelay,
Sungtak Lee04b30352020-07-27 13:57:25 -0700965 &secureMode,
Pawin Vongmasa36653902018-11-15 00:10:25 -0800966 },
967 {},
968 C2_DONT_BLOCK,
969 nullptr);
970 if (err == C2_BAD_INDEX) {
971 if (!iStreamFormat || !oStreamFormat) {
972 return UNKNOWN_ERROR;
973 }
974 } else if (err != C2_OK) {
975 return UNKNOWN_ERROR;
976 }
977
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -0800978 uint32_t inputDelayValue = inputDelay ? inputDelay.value : 0;
979 uint32_t pipelineDelayValue = pipelineDelay ? pipelineDelay.value : 0;
980 uint32_t outputDelayValue = outputDelay ? outputDelay.value : 0;
981
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700982 size_t numInputSlots = inputDelayValue + pipelineDelayValue + kSmoothnessFactor;
983 size_t numOutputSlots = outputDelayValue + kSmoothnessFactor;
Wonsik Kim078b58e2019-01-09 15:08:06 -0800984
Pawin Vongmasa36653902018-11-15 00:10:25 -0800985 // TODO: get this from input format
986 bool secure = mComponent->getName().find(".secure") != std::string::npos;
987
Sungtak Lee04b30352020-07-27 13:57:25 -0700988 // secure mode is a static parameter (shall not change in the executing state)
989 mSendEncryptedInfoBuffer = secureMode.value == C2Config::SM_READ_PROTECTED_WITH_ENCRYPTED;
990
Pawin Vongmasa36653902018-11-15 00:10:25 -0800991 std::shared_ptr<C2AllocatorStore> allocatorStore = GetCodec2PlatformAllocatorStore();
Pin-chih Linaa18ea52019-11-19 18:48:50 +0800992 int poolMask = GetCodec2PoolMask();
993 C2PlatformAllocatorStore::id_t preferredLinearId = GetPreferredLinearAllocatorId(poolMask);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800994
995 if (inputFormat != nullptr) {
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800996 bool graphic = (iStreamFormat.value == C2BufferData::GRAPHIC);
Wonsik Kimffb889a2020-05-28 11:32:25 -0700997 C2Config::api_feature_t apiFeatures = C2Config::api_feature_t(
998 API_REFLECTION |
999 API_VALUES |
1000 API_CURRENT_VALUES |
1001 API_DEPENDENCY |
1002 API_SAME_INPUT_BUFFER);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001003 std::shared_ptr<C2BlockPool> pool;
1004 {
1005 Mutexed<BlockPools>::Locked pools(mBlockPools);
1006
1007 // set default allocator ID.
1008 pools->inputAllocatorId = (graphic) ? C2PlatformAllocatorStore::GRALLOC
Pin-chih Linaa18ea52019-11-19 18:48:50 +08001009 : preferredLinearId;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001010
1011 // query C2PortAllocatorsTuning::input from component. If an allocator ID is obtained
1012 // from component, create the input block pool with given ID. Otherwise, use default IDs.
1013 std::vector<std::unique_ptr<C2Param>> params;
Wonsik Kimffb889a2020-05-28 11:32:25 -07001014 C2ApiFeaturesSetting featuresSetting{apiFeatures};
1015 err = mComponent->query({ &featuresSetting },
Pawin Vongmasa36653902018-11-15 00:10:25 -08001016 { C2PortAllocatorsTuning::input::PARAM_TYPE },
1017 C2_DONT_BLOCK,
1018 &params);
1019 if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
1020 ALOGD("[%s] Query input allocators returned %zu params => %s (%u)",
1021 mName, params.size(), asString(err), err);
Wonsik Kimffb889a2020-05-28 11:32:25 -07001022 } else if (params.size() == 1) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001023 C2PortAllocatorsTuning::input *inputAllocators =
1024 C2PortAllocatorsTuning::input::From(params[0].get());
1025 if (inputAllocators && inputAllocators->flexCount() > 0) {
1026 std::shared_ptr<C2Allocator> allocator;
1027 // verify allocator IDs and resolve default allocator
1028 allocatorStore->fetchAllocator(inputAllocators->m.values[0], &allocator);
1029 if (allocator) {
1030 pools->inputAllocatorId = allocator->getId();
1031 } else {
1032 ALOGD("[%s] component requested invalid input allocator ID %u",
1033 mName, inputAllocators->m.values[0]);
1034 }
1035 }
1036 }
Wonsik Kimffb889a2020-05-28 11:32:25 -07001037 if (featuresSetting) {
1038 apiFeatures = featuresSetting.value;
1039 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001040
1041 // TODO: use C2Component wrapper to associate this pool with ourselves
1042 if ((poolMask >> pools->inputAllocatorId) & 1) {
1043 err = CreateCodec2BlockPool(pools->inputAllocatorId, nullptr, &pool);
1044 ALOGD("[%s] Created input block pool with allocatorID %u => poolID %llu - %s (%d)",
1045 mName, pools->inputAllocatorId,
1046 (unsigned long long)(pool ? pool->getLocalId() : 111000111),
1047 asString(err), err);
1048 } else {
1049 err = C2_NOT_FOUND;
1050 }
1051 if (err != C2_OK) {
1052 C2BlockPool::local_id_t inputPoolId =
1053 graphic ? C2BlockPool::BASIC_GRAPHIC : C2BlockPool::BASIC_LINEAR;
1054 err = GetCodec2BlockPool(inputPoolId, nullptr, &pool);
1055 ALOGD("[%s] Using basic input block pool with poolID %llu => got %llu - %s (%d)",
1056 mName, (unsigned long long)inputPoolId,
1057 (unsigned long long)(pool ? pool->getLocalId() : 111000111),
1058 asString(err), err);
1059 if (err != C2_OK) {
1060 return NO_MEMORY;
1061 }
1062 }
1063 pools->inputPool = pool;
1064 }
1065
Wonsik Kim51051262018-11-28 13:59:05 -08001066 bool forceArrayMode = false;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001067 Mutexed<Input>::Locked input(mInput);
Wonsik Kimbdffead2019-07-01 12:00:07 -07001068 input->inputDelay = inputDelayValue;
1069 input->pipelineDelay = pipelineDelayValue;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001070 input->numSlots = numInputSlots;
1071 input->extraBuffers.flush();
1072 input->numExtraSlots = 0u;
Wonsik Kimffb889a2020-05-28 11:32:25 -07001073 bool conforming = (apiFeatures & API_SAME_INPUT_BUFFER);
1074 // For encrypted content, framework decrypts source buffer (ashmem) into
1075 // C2Buffers. Thus non-conforming codecs can process these.
1076 if (!buffersBoundToCodec && (hasCryptoOrDescrambler() || conforming)) {
Wonsik Kimfb7a7672019-12-27 17:13:33 -08001077 input->buffers.reset(new SlotInputBuffers(mName));
1078 } else if (graphic) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001079 if (mInputSurface) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001080 input->buffers.reset(new DummyInputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001081 } else if (mMetaMode == MODE_ANW) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001082 input->buffers.reset(new GraphicMetadataInputBuffers(mName));
Wonsik Kim1221fd12019-07-12 12:52:05 -07001083 // This is to ensure buffers do not get released prematurely.
1084 // TODO: handle this without going into array mode
1085 forceArrayMode = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001086 } else {
Wonsik Kim41d83432020-04-27 16:40:49 -07001087 input->buffers.reset(new GraphicInputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001088 }
1089 } else {
1090 if (hasCryptoOrDescrambler()) {
1091 int32_t capacity = kLinearBufferSize;
1092 (void)inputFormat->findInt32(KEY_MAX_INPUT_SIZE, &capacity);
1093 if ((size_t)capacity > kMaxLinearBufferSize) {
1094 ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize);
1095 capacity = kMaxLinearBufferSize;
1096 }
1097 if (mDealer == nullptr) {
1098 mDealer = new MemoryDealer(
1099 align(capacity, MemoryDealer::getAllocationAlignment())
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001100 * (numInputSlots + 1),
Pawin Vongmasa36653902018-11-15 00:10:25 -08001101 "EncryptedLinearInputBuffers");
1102 mDecryptDestination = mDealer->allocate((size_t)capacity);
1103 }
1104 if (mCrypto != nullptr && mHeapSeqNum < 0) {
Robert Shih895fba92019-07-16 16:29:44 -07001105 sp<HidlMemory> heap = fromHeap(mDealer->getMemoryHeap());
1106 mHeapSeqNum = mCrypto->setHeap(heap);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001107 } else {
1108 mHeapSeqNum = -1;
1109 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001110 input->buffers.reset(new EncryptedLinearInputBuffers(
Wonsik Kim078b58e2019-01-09 15:08:06 -08001111 secure, mDealer, mCrypto, mHeapSeqNum, (size_t)capacity,
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001112 numInputSlots, mName));
Wonsik Kim51051262018-11-28 13:59:05 -08001113 forceArrayMode = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001114 } else {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001115 input->buffers.reset(new LinearInputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001116 }
1117 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001118 input->buffers->setFormat(inputFormat);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001119
1120 if (err == C2_OK) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001121 input->buffers->setPool(pool);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001122 } else {
1123 // TODO: error
1124 }
Wonsik Kim51051262018-11-28 13:59:05 -08001125
1126 if (forceArrayMode) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001127 input->buffers = input->buffers->toArrayMode(numInputSlots);
Wonsik Kim51051262018-11-28 13:59:05 -08001128 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001129 }
1130
1131 if (outputFormat != nullptr) {
1132 sp<IGraphicBufferProducer> outputSurface;
1133 uint32_t outputGeneration;
1134 {
1135 Mutexed<OutputSurface>::Locked output(mOutputSurface);
Sungtak Leed7463d12019-09-04 16:01:00 -07001136 output->maxDequeueBuffers = numOutputSlots +
Sungtak Lee7a7b7422019-07-16 17:40:40 -07001137 reorderDepth.value + kRenderingDepth;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001138 outputSurface = output->surface ?
1139 output->surface->getIGraphicBufferProducer() : nullptr;
Wonsik Kimf5e5c832019-02-21 11:36:05 -08001140 if (outputSurface) {
1141 output->surface->setMaxDequeuedBufferCount(output->maxDequeueBuffers);
1142 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001143 outputGeneration = output->generation;
1144 }
1145
Lajos Molnar3bb81cd2019-02-20 15:10:30 -08001146 bool graphic = (oStreamFormat.value == C2BufferData::GRAPHIC);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001147 C2BlockPool::local_id_t outputPoolId_;
David Stevensc3fbb282021-01-18 18:11:20 +09001148 C2BlockPool::local_id_t prevOutputPoolId;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001149
1150 {
1151 Mutexed<BlockPools>::Locked pools(mBlockPools);
1152
David Stevensc3fbb282021-01-18 18:11:20 +09001153 prevOutputPoolId = pools->outputPoolId;
1154
Pawin Vongmasa36653902018-11-15 00:10:25 -08001155 // set default allocator ID.
1156 pools->outputAllocatorId = (graphic) ? C2PlatformAllocatorStore::GRALLOC
Pin-chih Linaa18ea52019-11-19 18:48:50 +08001157 : preferredLinearId;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001158
1159 // query C2PortAllocatorsTuning::output from component, or use default allocator if
1160 // unsuccessful.
1161 std::vector<std::unique_ptr<C2Param>> params;
1162 err = mComponent->query({ },
1163 { C2PortAllocatorsTuning::output::PARAM_TYPE },
1164 C2_DONT_BLOCK,
1165 &params);
1166 if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
1167 ALOGD("[%s] Query output allocators returned %zu params => %s (%u)",
1168 mName, params.size(), asString(err), err);
1169 } else if (err == C2_OK && params.size() == 1) {
1170 C2PortAllocatorsTuning::output *outputAllocators =
1171 C2PortAllocatorsTuning::output::From(params[0].get());
1172 if (outputAllocators && outputAllocators->flexCount() > 0) {
1173 std::shared_ptr<C2Allocator> allocator;
1174 // verify allocator IDs and resolve default allocator
1175 allocatorStore->fetchAllocator(outputAllocators->m.values[0], &allocator);
1176 if (allocator) {
1177 pools->outputAllocatorId = allocator->getId();
1178 } else {
1179 ALOGD("[%s] component requested invalid output allocator ID %u",
1180 mName, outputAllocators->m.values[0]);
1181 }
1182 }
1183 }
1184
1185 // use bufferqueue if outputting to a surface.
1186 // query C2PortSurfaceAllocatorTuning::output from component, or use default allocator
1187 // if unsuccessful.
1188 if (outputSurface) {
1189 params.clear();
1190 err = mComponent->query({ },
1191 { C2PortSurfaceAllocatorTuning::output::PARAM_TYPE },
1192 C2_DONT_BLOCK,
1193 &params);
1194 if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
1195 ALOGD("[%s] Query output surface allocator returned %zu params => %s (%u)",
1196 mName, params.size(), asString(err), err);
1197 } else if (err == C2_OK && params.size() == 1) {
1198 C2PortSurfaceAllocatorTuning::output *surfaceAllocator =
1199 C2PortSurfaceAllocatorTuning::output::From(params[0].get());
1200 if (surfaceAllocator) {
1201 std::shared_ptr<C2Allocator> allocator;
1202 // verify allocator IDs and resolve default allocator
1203 allocatorStore->fetchAllocator(surfaceAllocator->value, &allocator);
1204 if (allocator) {
1205 pools->outputAllocatorId = allocator->getId();
1206 } else {
1207 ALOGD("[%s] component requested invalid surface output allocator ID %u",
1208 mName, surfaceAllocator->value);
1209 err = C2_BAD_VALUE;
1210 }
1211 }
1212 }
1213 if (pools->outputAllocatorId == C2PlatformAllocatorStore::GRALLOC
1214 && err != C2_OK
1215 && ((poolMask >> C2PlatformAllocatorStore::BUFFERQUEUE) & 1)) {
1216 pools->outputAllocatorId = C2PlatformAllocatorStore::BUFFERQUEUE;
1217 }
1218 }
1219
1220 if ((poolMask >> pools->outputAllocatorId) & 1) {
1221 err = mComponent->createBlockPool(
1222 pools->outputAllocatorId, &pools->outputPoolId, &pools->outputPoolIntf);
1223 ALOGI("[%s] Created output block pool with allocatorID %u => poolID %llu - %s",
1224 mName, pools->outputAllocatorId,
1225 (unsigned long long)pools->outputPoolId,
1226 asString(err));
1227 } else {
1228 err = C2_NOT_FOUND;
1229 }
1230 if (err != C2_OK) {
1231 // use basic pool instead
1232 pools->outputPoolId =
1233 graphic ? C2BlockPool::BASIC_GRAPHIC : C2BlockPool::BASIC_LINEAR;
1234 }
1235
1236 // Configure output block pool ID as parameter C2PortBlockPoolsTuning::output to
1237 // component.
1238 std::unique_ptr<C2PortBlockPoolsTuning::output> poolIdsTuning =
1239 C2PortBlockPoolsTuning::output::AllocUnique({ pools->outputPoolId });
1240
1241 std::vector<std::unique_ptr<C2SettingResult>> failures;
1242 err = mComponent->config({ poolIdsTuning.get() }, C2_MAY_BLOCK, &failures);
1243 ALOGD("[%s] Configured output block pool ids %llu => %s",
1244 mName, (unsigned long long)poolIdsTuning->m.values[0], asString(err));
1245 outputPoolId_ = pools->outputPoolId;
1246 }
1247
David Stevensc3fbb282021-01-18 18:11:20 +09001248 if (prevOutputPoolId != C2BlockPool::BASIC_LINEAR
1249 && prevOutputPoolId != C2BlockPool::BASIC_GRAPHIC) {
1250 c2_status_t err = mComponent->destroyBlockPool(prevOutputPoolId);
1251 if (err != C2_OK) {
1252 ALOGW("Failed to clean up previous block pool %llu - %s (%d)\n",
1253 (unsigned long long) prevOutputPoolId, asString(err), err);
1254 }
1255 }
1256
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001257 Mutexed<Output>::Locked output(mOutput);
Wonsik Kimbdffead2019-07-01 12:00:07 -07001258 output->outputDelay = outputDelayValue;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001259 output->numSlots = numOutputSlots;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001260 if (graphic) {
Wonsik Kimfb7a7672019-12-27 17:13:33 -08001261 if (outputSurface || !buffersBoundToCodec) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001262 output->buffers.reset(new GraphicOutputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001263 } else {
Wonsik Kim41d83432020-04-27 16:40:49 -07001264 output->buffers.reset(new RawGraphicOutputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001265 }
1266 } else {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001267 output->buffers.reset(new LinearOutputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001268 }
Wonsik Kime4716c02020-02-28 10:42:21 -08001269 output->buffers->setFormat(outputFormat);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001270
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001271 output->buffers->clearStash();
1272 if (reorderDepth) {
1273 output->buffers->setReorderDepth(reorderDepth.value);
1274 }
1275 if (reorderKey) {
1276 output->buffers->setReorderKey(reorderKey.value);
1277 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001278
1279 // Try to set output surface to created block pool if given.
1280 if (outputSurface) {
1281 mComponent->setOutputSurface(
1282 outputPoolId_,
1283 outputSurface,
1284 outputGeneration);
1285 }
1286
Wonsik Kim8ab25aa2019-06-24 16:37:37 -07001287 if (oStreamFormat.value == C2BufferData::LINEAR) {
Wonsik Kim58713302020-01-29 22:25:23 -08001288 if (buffersBoundToCodec) {
1289 // WORKAROUND: if we're using early CSD workaround we convert to
1290 // array mode, to appease apps assuming the output
1291 // buffers to be of the same size.
1292 output->buffers = output->buffers->toArrayMode(numOutputSlots);
1293 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001294
1295 int32_t channelCount;
1296 int32_t sampleRate;
1297 if (outputFormat->findInt32(KEY_CHANNEL_COUNT, &channelCount)
1298 && outputFormat->findInt32(KEY_SAMPLE_RATE, &sampleRate)) {
1299 int32_t delay = 0;
1300 int32_t padding = 0;;
1301 if (!outputFormat->findInt32("encoder-delay", &delay)) {
1302 delay = 0;
1303 }
1304 if (!outputFormat->findInt32("encoder-padding", &padding)) {
1305 padding = 0;
1306 }
1307 if (delay || padding) {
1308 // We need write access to the buffers, and we're already in
1309 // array mode.
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001310 output->buffers->initSkipCutBuffer(delay, padding, sampleRate, channelCount);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001311 }
1312 }
1313 }
1314 }
1315
1316 // Set up pipeline control. This has to be done after mInputBuffers and
1317 // mOutputBuffers are initialized to make sure that lingering callbacks
1318 // about buffers from the previous generation do not interfere with the
1319 // newly initialized pipeline capacity.
1320
Wonsik Kimab34ed62019-01-31 15:28:46 -08001321 {
1322 Mutexed<PipelineWatcher>::Locked watcher(mPipelineWatcher);
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -08001323 watcher->inputDelay(inputDelayValue)
1324 .pipelineDelay(pipelineDelayValue)
1325 .outputDelay(outputDelayValue)
Wonsik Kimab34ed62019-01-31 15:28:46 -08001326 .smoothnessFactor(kSmoothnessFactor);
1327 watcher->flush();
1328 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001329
1330 mInputMetEos = false;
1331 mSync.start();
1332 return OK;
1333}
1334
1335status_t CCodecBufferChannel::requestInitialInputBuffers() {
1336 if (mInputSurface) {
1337 return OK;
1338 }
1339
Lajos Molnar3bb81cd2019-02-20 15:10:30 -08001340 C2StreamBufferTypeSetting::output oStreamFormat(0u);
Wonsik Kim8ab25aa2019-06-24 16:37:37 -07001341 C2PrependHeaderModeSetting prepend(PREPEND_HEADER_TO_NONE);
1342 c2_status_t err = mComponent->query({ &oStreamFormat, &prepend }, {}, C2_DONT_BLOCK, nullptr);
1343 if (err != C2_OK && err != C2_BAD_INDEX) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001344 return UNKNOWN_ERROR;
1345 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001346 size_t numInputSlots = mInput.lock()->numSlots;
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001347
1348 struct ClientInputBuffer {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001349 size_t index;
1350 sp<MediaCodecBuffer> buffer;
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001351 size_t capacity;
1352 };
1353 std::list<ClientInputBuffer> clientInputBuffers;
1354
1355 {
1356 Mutexed<Input>::Locked input(mInput);
1357 while (clientInputBuffers.size() < numInputSlots) {
1358 ClientInputBuffer clientInputBuffer;
1359 if (!input->buffers->requestNewBuffer(&clientInputBuffer.index,
1360 &clientInputBuffer.buffer)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001361 break;
1362 }
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001363 clientInputBuffer.capacity = clientInputBuffer.buffer->capacity();
1364 clientInputBuffers.emplace_back(std::move(clientInputBuffer));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001365 }
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001366 }
1367 if (clientInputBuffers.empty()) {
1368 ALOGW("[%s] start: cannot allocate memory at all", mName);
1369 return NO_MEMORY;
1370 } else if (clientInputBuffers.size() < numInputSlots) {
1371 ALOGD("[%s] start: cannot allocate memory for all slots, "
1372 "only %zu buffers allocated",
1373 mName, clientInputBuffers.size());
1374 } else {
1375 ALOGV("[%s] %zu initial input buffers available",
1376 mName, clientInputBuffers.size());
1377 }
1378 // Sort input buffers by their capacities in increasing order.
1379 clientInputBuffers.sort(
1380 [](const ClientInputBuffer& a, const ClientInputBuffer& b) {
1381 return a.capacity < b.capacity;
1382 });
1383
Wonsik Kimf34e4e02021-01-05 18:58:15 -08001384 std::list<std::unique_ptr<C2Work>> flushedConfigs;
1385 mFlushedConfigs.lock()->swap(flushedConfigs);
1386 if (!flushedConfigs.empty()) {
1387 err = mComponent->queue(&flushedConfigs);
1388 if (err != C2_OK) {
1389 ALOGW("[%s] Error while queueing a flushed config", mName);
1390 return UNKNOWN_ERROR;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001391 }
1392 }
Wonsik Kimf34e4e02021-01-05 18:58:15 -08001393 if (oStreamFormat.value == C2BufferData::LINEAR &&
1394 (!prepend || prepend.value == PREPEND_HEADER_TO_NONE)) {
1395 sp<MediaCodecBuffer> buffer = clientInputBuffers.front().buffer;
1396 // WORKAROUND: Some apps expect CSD available without queueing
1397 // any input. Queue an empty buffer to get the CSD.
1398 buffer->setRange(0, 0);
1399 buffer->meta()->clear();
1400 buffer->meta()->setInt64("timeUs", 0);
1401 if (queueInputBufferInternal(buffer) != OK) {
1402 ALOGW("[%s] Error while queueing an empty buffer to get CSD",
1403 mName);
1404 return UNKNOWN_ERROR;
1405 }
1406 clientInputBuffers.pop_front();
1407 }
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001408
1409 for (const ClientInputBuffer& clientInputBuffer: clientInputBuffers) {
1410 mCallback->onInputBufferAvailable(
1411 clientInputBuffer.index,
1412 clientInputBuffer.buffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001413 }
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001414
Pawin Vongmasa36653902018-11-15 00:10:25 -08001415 return OK;
1416}
1417
1418void CCodecBufferChannel::stop() {
1419 mSync.stop();
1420 mFirstValidFrameIndex = mFrameIndex.load(std::memory_order_relaxed);
1421 if (mInputSurface != nullptr) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001422 mInputSurface.reset();
1423 }
Wonsik Kima2e3cdd2020-05-20 15:14:42 -07001424 mPipelineWatcher.lock()->flush();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001425}
1426
Wonsik Kim936a89c2020-05-08 16:07:50 -07001427void CCodecBufferChannel::reset() {
1428 stop();
1429 {
1430 Mutexed<Input>::Locked input(mInput);
1431 input->buffers.reset(new DummyInputBuffers(""));
Wonsik Kima2e3cdd2020-05-20 15:14:42 -07001432 input->extraBuffers.flush();
Wonsik Kim936a89c2020-05-08 16:07:50 -07001433 }
1434 {
1435 Mutexed<Output>::Locked output(mOutput);
1436 output->buffers.reset();
1437 }
1438}
1439
1440void CCodecBufferChannel::release() {
1441 mComponent.reset();
1442 mInputAllocator.reset();
1443 mOutputSurface.lock()->surface.clear();
1444 {
1445 Mutexed<BlockPools>::Locked blockPools{mBlockPools};
1446 blockPools->inputPool.reset();
1447 blockPools->outputPoolIntf.reset();
1448 }
Wonsik Kima2e3cdd2020-05-20 15:14:42 -07001449 setCrypto(nullptr);
1450 setDescrambler(nullptr);
Wonsik Kim936a89c2020-05-08 16:07:50 -07001451}
1452
1453
Pawin Vongmasa36653902018-11-15 00:10:25 -08001454void CCodecBufferChannel::flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) {
1455 ALOGV("[%s] flush", mName);
Wonsik Kimf34e4e02021-01-05 18:58:15 -08001456 std::list<std::unique_ptr<C2Work>> configs;
1457 for (const std::unique_ptr<C2Work> &work : flushedWork) {
1458 if (!(work->input.flags & C2FrameData::FLAG_CODEC_CONFIG)) {
1459 continue;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001460 }
Wonsik Kimf34e4e02021-01-05 18:58:15 -08001461 if (work->input.buffers.empty()
1462 || work->input.buffers.front() == nullptr
1463 || work->input.buffers.front()->data().linearBlocks().empty()) {
1464 ALOGD("[%s] no linear codec config data found", mName);
1465 continue;
1466 }
1467 std::unique_ptr<C2Work> copy(new C2Work);
1468 copy->input.flags = C2FrameData::flags_t(work->input.flags | C2FrameData::FLAG_DROP_FRAME);
1469 copy->input.ordinal = work->input.ordinal;
1470 copy->input.buffers.insert(
1471 copy->input.buffers.begin(),
1472 work->input.buffers.begin(),
1473 work->input.buffers.end());
1474 for (const std::unique_ptr<C2Param> &param : work->input.configUpdate) {
1475 copy->input.configUpdate.push_back(C2Param::Copy(*param));
1476 }
1477 copy->input.infoBuffers.insert(
1478 copy->input.infoBuffers.begin(),
1479 work->input.infoBuffers.begin(),
1480 work->input.infoBuffers.end());
1481 copy->worklets.emplace_back(new C2Worklet);
1482 configs.push_back(std::move(copy));
1483 ALOGV("[%s] stashed flushed codec config data", mName);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001484 }
Wonsik Kimf34e4e02021-01-05 18:58:15 -08001485 mFlushedConfigs.lock()->swap(configs);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001486 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001487 Mutexed<Input>::Locked input(mInput);
1488 input->buffers->flush();
1489 input->extraBuffers.flush();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001490 }
1491 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001492 Mutexed<Output>::Locked output(mOutput);
Wonsik Kim936a89c2020-05-08 16:07:50 -07001493 if (output->buffers) {
1494 output->buffers->flush(flushedWork);
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001495 output->buffers->flushStash();
Wonsik Kim936a89c2020-05-08 16:07:50 -07001496 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001497 }
Wonsik Kimab34ed62019-01-31 15:28:46 -08001498 mPipelineWatcher.lock()->flush();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001499}
1500
1501void CCodecBufferChannel::onWorkDone(
1502 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
Wonsik Kimab34ed62019-01-31 15:28:46 -08001503 const C2StreamInitDataInfo::output *initData) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001504 if (handleWork(std::move(work), outputFormat, initData)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001505 feedInputBufferIfAvailable();
1506 }
1507}
1508
1509void CCodecBufferChannel::onInputBufferDone(
Wonsik Kimab34ed62019-01-31 15:28:46 -08001510 uint64_t frameIndex, size_t arrayIndex) {
Pawin Vongmasa8e2cfb52019-05-15 05:20:52 -07001511 if (mInputSurface) {
1512 return;
1513 }
Wonsik Kimab34ed62019-01-31 15:28:46 -08001514 std::shared_ptr<C2Buffer> buffer =
1515 mPipelineWatcher.lock()->onInputBufferReleased(frameIndex, arrayIndex);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001516 bool newInputSlotAvailable;
1517 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001518 Mutexed<Input>::Locked input(mInput);
1519 newInputSlotAvailable = input->buffers->expireComponentBuffer(buffer);
1520 if (!newInputSlotAvailable) {
1521 (void)input->extraBuffers.expireComponentBuffer(buffer);
1522 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001523 }
1524 if (newInputSlotAvailable) {
1525 feedInputBufferIfAvailable();
1526 }
1527}
1528
1529bool CCodecBufferChannel::handleWork(
1530 std::unique_ptr<C2Work> work,
1531 const sp<AMessage> &outputFormat,
1532 const C2StreamInitDataInfo::output *initData) {
Wonsik Kim936a89c2020-05-08 16:07:50 -07001533 {
Wonsik Kima4e049d2020-04-28 19:42:23 +00001534 Mutexed<Output>::Locked output(mOutput);
Wonsik Kim936a89c2020-05-08 16:07:50 -07001535 if (!output->buffers) {
1536 return false;
1537 }
Wonsik Kime75a5da2020-02-14 17:29:03 -08001538 }
1539
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001540 // Whether the output buffer should be reported to the client or not.
1541 bool notifyClient = false;
1542
1543 if (work->result == C2_OK){
1544 notifyClient = true;
1545 } else if (work->result == C2_NOT_FOUND) {
1546 ALOGD("[%s] flushed work; ignored.", mName);
1547 } else {
1548 // C2_OK and C2_NOT_FOUND are the only results that we accept for processing
1549 // the config update.
1550 ALOGD("[%s] work failed to complete: %d", mName, work->result);
1551 mCCodecCallback->onError(work->result, ACTION_CODE_FATAL);
1552 return false;
1553 }
1554
1555 if ((work->input.ordinal.frameIndex -
1556 mFirstValidFrameIndex.load()).peek() < 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001557 // Discard frames from previous generation.
1558 ALOGD("[%s] Discard frames from previous generation.", mName);
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001559 notifyClient = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001560 }
1561
Wonsik Kim524b0582019-03-12 11:28:57 -07001562 if (mInputSurface == nullptr && (work->worklets.size() != 1u
Pawin Vongmasa36653902018-11-15 00:10:25 -08001563 || !work->worklets.front()
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001564 || !(work->worklets.front()->output.flags &
1565 C2FrameData::FLAG_INCOMPLETE))) {
1566 mPipelineWatcher.lock()->onWorkDone(
1567 work->input.ordinal.frameIndex.peeku());
Pawin Vongmasa36653902018-11-15 00:10:25 -08001568 }
1569
1570 // NOTE: MediaCodec usage supposedly have only one worklet
1571 if (work->worklets.size() != 1u) {
1572 ALOGI("[%s] onWorkDone: incorrect number of worklets: %zu",
1573 mName, work->worklets.size());
1574 mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1575 return false;
1576 }
1577
1578 const std::unique_ptr<C2Worklet> &worklet = work->worklets.front();
1579
1580 std::shared_ptr<C2Buffer> buffer;
1581 // NOTE: MediaCodec usage supposedly have only one output stream.
1582 if (worklet->output.buffers.size() > 1u) {
1583 ALOGI("[%s] onWorkDone: incorrect number of output buffers: %zu",
1584 mName, worklet->output.buffers.size());
1585 mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1586 return false;
1587 } else if (worklet->output.buffers.size() == 1u) {
1588 buffer = worklet->output.buffers[0];
1589 if (!buffer) {
1590 ALOGD("[%s] onWorkDone: nullptr found in buffers; ignored.", mName);
1591 }
1592 }
1593
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001594 std::optional<uint32_t> newInputDelay, newPipelineDelay;
Wonsik Kim315e40a2020-09-09 14:11:50 -07001595 bool needMaxDequeueBufferCountUpdate = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001596 while (!worklet->output.configUpdate.empty()) {
1597 std::unique_ptr<C2Param> param;
1598 worklet->output.configUpdate.back().swap(param);
1599 worklet->output.configUpdate.pop_back();
1600 switch (param->coreIndex().coreIndex()) {
1601 case C2PortReorderBufferDepthTuning::CORE_INDEX: {
1602 C2PortReorderBufferDepthTuning::output reorderDepth;
1603 if (reorderDepth.updateFrom(*param)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001604 ALOGV("[%s] onWorkDone: updated reorder depth to %u",
1605 mName, reorderDepth.value);
Wonsik Kim315e40a2020-09-09 14:11:50 -07001606 mOutput.lock()->buffers->setReorderDepth(reorderDepth.value);
1607 needMaxDequeueBufferCountUpdate = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001608 } else {
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001609 ALOGD("[%s] onWorkDone: failed to read reorder depth",
1610 mName);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001611 }
1612 break;
1613 }
1614 case C2PortReorderKeySetting::CORE_INDEX: {
1615 C2PortReorderKeySetting::output reorderKey;
1616 if (reorderKey.updateFrom(*param)) {
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001617 mOutput.lock()->buffers->setReorderKey(reorderKey.value);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001618 ALOGV("[%s] onWorkDone: updated reorder key to %u",
1619 mName, reorderKey.value);
1620 } else {
1621 ALOGD("[%s] onWorkDone: failed to read reorder key", mName);
1622 }
1623 break;
1624 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001625 case C2PortActualDelayTuning::CORE_INDEX: {
1626 if (param->isGlobal()) {
1627 C2ActualPipelineDelayTuning pipelineDelay;
1628 if (pipelineDelay.updateFrom(*param)) {
1629 ALOGV("[%s] onWorkDone: updating pipeline delay %u",
1630 mName, pipelineDelay.value);
1631 newPipelineDelay = pipelineDelay.value;
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001632 (void)mPipelineWatcher.lock()->pipelineDelay(
1633 pipelineDelay.value);
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001634 }
1635 }
1636 if (param->forInput()) {
1637 C2PortActualDelayTuning::input inputDelay;
1638 if (inputDelay.updateFrom(*param)) {
1639 ALOGV("[%s] onWorkDone: updating input delay %u",
1640 mName, inputDelay.value);
1641 newInputDelay = inputDelay.value;
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001642 (void)mPipelineWatcher.lock()->inputDelay(
1643 inputDelay.value);
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001644 }
1645 }
1646 if (param->forOutput()) {
1647 C2PortActualDelayTuning::output outputDelay;
1648 if (outputDelay.updateFrom(*param)) {
1649 ALOGV("[%s] onWorkDone: updating output delay %u",
1650 mName, outputDelay.value);
Wonsik Kim315e40a2020-09-09 14:11:50 -07001651 (void)mPipelineWatcher.lock()->outputDelay(outputDelay.value);
1652 needMaxDequeueBufferCountUpdate = true;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001653
1654 bool outputBuffersChanged = false;
Wonsik Kimf0e7d222019-06-28 12:33:16 -07001655 size_t numOutputSlots = 0;
1656 {
1657 Mutexed<Output>::Locked output(mOutput);
Wonsik Kim936a89c2020-05-08 16:07:50 -07001658 if (!output->buffers) {
1659 return false;
1660 }
Wonsik Kimf0e7d222019-06-28 12:33:16 -07001661 output->outputDelay = outputDelay.value;
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001662 numOutputSlots = outputDelay.value +
1663 kSmoothnessFactor;
Wonsik Kimf0e7d222019-06-28 12:33:16 -07001664 if (output->numSlots < numOutputSlots) {
1665 output->numSlots = numOutputSlots;
1666 if (output->buffers->isArrayMode()) {
1667 OutputBuffersArray *array =
1668 (OutputBuffersArray *)output->buffers.get();
1669 ALOGV("[%s] onWorkDone: growing output buffer array to %zu",
1670 mName, numOutputSlots);
1671 array->grow(numOutputSlots);
1672 outputBuffersChanged = true;
1673 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001674 }
Wonsik Kimf0e7d222019-06-28 12:33:16 -07001675 numOutputSlots = output->numSlots;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001676 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001677
1678 if (outputBuffersChanged) {
1679 mCCodecCallback->onOutputBuffersChanged();
1680 }
1681 }
1682 }
1683 break;
1684 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001685 default:
1686 ALOGV("[%s] onWorkDone: unrecognized config update (%08X)",
1687 mName, param->index());
1688 break;
1689 }
1690 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001691 if (newInputDelay || newPipelineDelay) {
1692 Mutexed<Input>::Locked input(mInput);
1693 size_t newNumSlots =
1694 newInputDelay.value_or(input->inputDelay) +
1695 newPipelineDelay.value_or(input->pipelineDelay) +
1696 kSmoothnessFactor;
1697 if (input->buffers->isArrayMode()) {
1698 if (input->numSlots >= newNumSlots) {
1699 input->numExtraSlots = 0;
1700 } else {
1701 input->numExtraSlots = newNumSlots - input->numSlots;
1702 }
1703 ALOGV("[%s] onWorkDone: updated number of extra slots to %zu (input array mode)",
1704 mName, input->numExtraSlots);
1705 } else {
1706 input->numSlots = newNumSlots;
1707 }
1708 }
Wonsik Kim315e40a2020-09-09 14:11:50 -07001709 if (needMaxDequeueBufferCountUpdate) {
1710 size_t numOutputSlots = 0;
1711 uint32_t reorderDepth = 0;
1712 {
1713 Mutexed<Output>::Locked output(mOutput);
1714 numOutputSlots = output->numSlots;
1715 reorderDepth = output->buffers->getReorderDepth();
1716 }
1717 Mutexed<OutputSurface>::Locked output(mOutputSurface);
1718 output->maxDequeueBuffers = numOutputSlots + reorderDepth + kRenderingDepth;
1719 if (output->surface) {
1720 output->surface->setMaxDequeuedBufferCount(output->maxDequeueBuffers);
1721 }
1722 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001723
Pawin Vongmasa36653902018-11-15 00:10:25 -08001724 int32_t flags = 0;
1725 if (worklet->output.flags & C2FrameData::FLAG_END_OF_STREAM) {
1726 flags |= MediaCodec::BUFFER_FLAG_EOS;
1727 ALOGV("[%s] onWorkDone: output EOS", mName);
1728 }
1729
Pawin Vongmasa36653902018-11-15 00:10:25 -08001730 // WORKAROUND: adjust output timestamp based on client input timestamp and codec
1731 // input timestamp. Codec output timestamp (in the timestamp field) shall correspond to
1732 // the codec input timestamp, but client output timestamp should (reported in timeUs)
1733 // shall correspond to the client input timesamp (in customOrdinal). By using the
1734 // delta between the two, this allows for some timestamp deviation - e.g. if one input
1735 // produces multiple output.
1736 c2_cntr64_t timestamp =
1737 worklet->output.ordinal.timestamp + work->input.ordinal.customOrdinal
1738 - work->input.ordinal.timestamp;
Wonsik Kim95ba0162019-03-19 15:51:54 -07001739 if (mInputSurface != nullptr) {
1740 // When using input surface we need to restore the original input timestamp.
1741 timestamp = work->input.ordinal.customOrdinal;
1742 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001743 ALOGV("[%s] onWorkDone: input %lld, codec %lld => output %lld => %lld",
1744 mName,
1745 work->input.ordinal.customOrdinal.peekll(),
1746 work->input.ordinal.timestamp.peekll(),
1747 worklet->output.ordinal.timestamp.peekll(),
1748 timestamp.peekll());
1749
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001750 // csd cannot be re-ordered and will always arrive first.
Pawin Vongmasa36653902018-11-15 00:10:25 -08001751 if (initData != nullptr) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001752 Mutexed<Output>::Locked output(mOutput);
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001753 if (output->buffers && outputFormat) {
1754 output->buffers->updateSkipCutBuffer(outputFormat);
1755 output->buffers->setFormat(outputFormat);
1756 }
1757 if (!notifyClient) {
1758 return false;
1759 }
1760 size_t index;
1761 sp<MediaCodecBuffer> outBuffer;
Wonsik Kim936a89c2020-05-08 16:07:50 -07001762 if (output->buffers && output->buffers->registerCsd(initData, &index, &outBuffer) == OK) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001763 outBuffer->meta()->setInt64("timeUs", timestamp.peek());
1764 outBuffer->meta()->setInt32("flags", MediaCodec::BUFFER_FLAG_CODECCONFIG);
1765 ALOGV("[%s] onWorkDone: csd index = %zu [%p]", mName, index, outBuffer.get());
1766
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001767 output.unlock();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001768 mCallback->onOutputBufferAvailable(index, outBuffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001769 } else {
1770 ALOGD("[%s] onWorkDone: unable to register csd", mName);
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001771 output.unlock();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001772 mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001773 return false;
1774 }
1775 }
1776
ted.sunb8fe01e2020-06-23 14:03:41 +08001777 bool drop = false;
1778 if (worklet->output.flags & C2FrameData::FLAG_DROP_FRAME) {
1779 ALOGV("[%s] onWorkDone: drop buffer but keep metadata", mName);
1780 drop = true;
1781 }
1782
ted.sun04698a32020-06-23 14:03:41 +08001783 if (notifyClient && !buffer && !flags && !(drop && outputFormat)) {
Wonsik Kim35bf5732020-05-14 17:40:29 +00001784 ALOGV("[%s] onWorkDone: Not reporting output buffer (%lld)",
Pawin Vongmasa36653902018-11-15 00:10:25 -08001785 mName, work->input.ordinal.frameIndex.peekull());
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001786 notifyClient = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001787 }
1788
1789 if (buffer) {
1790 for (const std::shared_ptr<const C2Info> &info : buffer->info()) {
1791 // TODO: properly translate these to metadata
1792 switch (info->coreIndex().coreIndex()) {
1793 case C2StreamPictureTypeMaskInfo::CORE_INDEX:
Lajos Molnar3bb81cd2019-02-20 15:10:30 -08001794 if (((C2StreamPictureTypeMaskInfo *)info.get())->value & C2Config::SYNC_FRAME) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001795 flags |= MediaCodec::BUFFER_FLAG_SYNCFRAME;
1796 }
1797 break;
1798 default:
1799 break;
1800 }
1801 }
1802 }
1803
1804 {
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001805 Mutexed<Output>::Locked output(mOutput);
Wonsik Kimc23cc402020-05-28 14:53:40 -07001806 if (!output->buffers) {
1807 return false;
1808 }
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001809 output->buffers->pushToStash(
ted.sun04698a32020-06-23 14:03:41 +08001810 drop ? nullptr : buffer,
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001811 notifyClient,
1812 timestamp.peek(),
1813 flags,
1814 outputFormat,
1815 worklet->output.ordinal);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001816 }
1817 sendOutputBuffers();
1818 return true;
1819}
1820
1821void CCodecBufferChannel::sendOutputBuffers() {
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001822 OutputBuffers::BufferAction action;
Wonsik Kima4e049d2020-04-28 19:42:23 +00001823 size_t index;
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001824 sp<MediaCodecBuffer> outBuffer;
1825 std::shared_ptr<C2Buffer> c2Buffer;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001826
1827 while (true) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001828 Mutexed<Output>::Locked output(mOutput);
Wonsik Kim936a89c2020-05-08 16:07:50 -07001829 if (!output->buffers) {
1830 return;
1831 }
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001832 action = output->buffers->popFromStashAndRegister(
1833 &c2Buffer, &index, &outBuffer);
1834 switch (action) {
1835 case OutputBuffers::SKIP:
1836 return;
1837 case OutputBuffers::DISCARD:
1838 break;
1839 case OutputBuffers::NOTIFY_CLIENT:
Wonsik Kima4e049d2020-04-28 19:42:23 +00001840 output.unlock();
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001841 mCallback->onOutputBufferAvailable(index, outBuffer);
1842 break;
1843 case OutputBuffers::REALLOCATE:
1844 if (!output->buffers->isArrayMode()) {
1845 output->buffers =
1846 output->buffers->toArrayMode(output->numSlots);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001847 }
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001848 static_cast<OutputBuffersArray*>(output->buffers.get())->
1849 realloc(c2Buffer);
1850 output.unlock();
1851 mCCodecCallback->onOutputBuffersChanged();
Wonsik Kim4ada73d2020-05-26 14:58:07 -07001852 break;
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001853 case OutputBuffers::RETRY:
1854 ALOGV("[%s] sendOutputBuffers: unable to register output buffer",
1855 mName);
1856 return;
1857 default:
1858 LOG_ALWAYS_FATAL("[%s] sendOutputBuffers: "
1859 "corrupted BufferAction value (%d) "
1860 "returned from popFromStashAndRegister.",
1861 mName, int(action));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001862 return;
1863 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001864 }
1865}
1866
1867status_t CCodecBufferChannel::setSurface(const sp<Surface> &newSurface) {
1868 static std::atomic_uint32_t surfaceGeneration{0};
1869 uint32_t generation = (getpid() << 10) |
1870 ((surfaceGeneration.fetch_add(1, std::memory_order_relaxed) + 1)
1871 & ((1 << 10) - 1));
1872
1873 sp<IGraphicBufferProducer> producer;
1874 if (newSurface) {
1875 newSurface->setScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Sungtak Leeab6f2f32019-02-15 14:43:51 -08001876 newSurface->setDequeueTimeout(kDequeueTimeoutNs);
Sungtak Lee08515812019-06-05 11:16:32 -07001877 newSurface->setMaxDequeuedBufferCount(mOutputSurface.lock()->maxDequeueBuffers);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001878 producer = newSurface->getIGraphicBufferProducer();
1879 producer->setGenerationNumber(generation);
1880 } else {
1881 ALOGE("[%s] setting output surface to null", mName);
1882 return INVALID_OPERATION;
1883 }
1884
1885 std::shared_ptr<Codec2Client::Configurable> outputPoolIntf;
1886 C2BlockPool::local_id_t outputPoolId;
1887 {
1888 Mutexed<BlockPools>::Locked pools(mBlockPools);
1889 outputPoolId = pools->outputPoolId;
1890 outputPoolIntf = pools->outputPoolIntf;
1891 }
1892
1893 if (outputPoolIntf) {
1894 if (mComponent->setOutputSurface(
1895 outputPoolId,
1896 producer,
1897 generation) != C2_OK) {
1898 ALOGI("[%s] setSurface: component setOutputSurface failed", mName);
1899 return INVALID_OPERATION;
1900 }
1901 }
1902
1903 {
1904 Mutexed<OutputSurface>::Locked output(mOutputSurface);
1905 output->surface = newSurface;
1906 output->generation = generation;
1907 }
1908
1909 return OK;
1910}
1911
Wonsik Kimab34ed62019-01-31 15:28:46 -08001912PipelineWatcher::Clock::duration CCodecBufferChannel::elapsed() {
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -08001913 // When client pushed EOS, we want all the work to be done quickly.
1914 // Otherwise, component may have stalled work due to input starvation up to
1915 // the sum of the delay in the pipeline.
Wonsik Kimf0e7d222019-06-28 12:33:16 -07001916 size_t n = 0;
1917 if (!mInputMetEos) {
1918 size_t outputDelay = mOutput.lock()->outputDelay;
1919 Mutexed<Input>::Locked input(mInput);
1920 n = input->inputDelay + input->pipelineDelay + outputDelay;
1921 }
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -08001922 return mPipelineWatcher.lock()->elapsed(PipelineWatcher::Clock::now(), n);
Wonsik Kimab34ed62019-01-31 15:28:46 -08001923}
1924
Pawin Vongmasa36653902018-11-15 00:10:25 -08001925void CCodecBufferChannel::setMetaMode(MetaMode mode) {
1926 mMetaMode = mode;
1927}
1928
Wonsik Kim596187e2019-10-25 12:44:10 -07001929void CCodecBufferChannel::setCrypto(const sp<ICrypto> &crypto) {
Wonsik Kimfb7a7672019-12-27 17:13:33 -08001930 if (mCrypto != nullptr) {
1931 for (std::pair<wp<HidlMemory>, int32_t> entry : mHeapSeqNumMap) {
1932 mCrypto->unsetHeap(entry.second);
1933 }
1934 mHeapSeqNumMap.clear();
1935 if (mHeapSeqNum >= 0) {
1936 mCrypto->unsetHeap(mHeapSeqNum);
1937 mHeapSeqNum = -1;
1938 }
1939 }
Wonsik Kim596187e2019-10-25 12:44:10 -07001940 mCrypto = crypto;
1941}
1942
1943void CCodecBufferChannel::setDescrambler(const sp<IDescrambler> &descrambler) {
1944 mDescrambler = descrambler;
1945}
1946
Pawin Vongmasa36653902018-11-15 00:10:25 -08001947status_t toStatusT(c2_status_t c2s, c2_operation_t c2op) {
1948 // C2_OK is always translated to OK.
1949 if (c2s == C2_OK) {
1950 return OK;
1951 }
1952
1953 // Operation-dependent translation
1954 // TODO: Add as necessary
1955 switch (c2op) {
1956 case C2_OPERATION_Component_start:
1957 switch (c2s) {
1958 case C2_NO_MEMORY:
1959 return NO_MEMORY;
1960 default:
1961 return UNKNOWN_ERROR;
1962 }
1963 default:
1964 break;
1965 }
1966
1967 // Backup operation-agnostic translation
1968 switch (c2s) {
1969 case C2_BAD_INDEX:
1970 return BAD_INDEX;
1971 case C2_BAD_VALUE:
1972 return BAD_VALUE;
1973 case C2_BLOCKING:
1974 return WOULD_BLOCK;
1975 case C2_DUPLICATE:
1976 return ALREADY_EXISTS;
1977 case C2_NO_INIT:
1978 return NO_INIT;
1979 case C2_NO_MEMORY:
1980 return NO_MEMORY;
1981 case C2_NOT_FOUND:
1982 return NAME_NOT_FOUND;
1983 case C2_TIMED_OUT:
1984 return TIMED_OUT;
1985 case C2_BAD_STATE:
1986 case C2_CANCELED:
1987 case C2_CANNOT_DO:
1988 case C2_CORRUPTED:
1989 case C2_OMITTED:
1990 case C2_REFUSED:
1991 return UNKNOWN_ERROR;
1992 default:
1993 return -static_cast<status_t>(c2s);
1994 }
1995}
1996
1997} // namespace android