blob: a9c72fb902c27c48649b4cbe011c6a08155f33db [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 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800162}
163
164CCodecBufferChannel::~CCodecBufferChannel() {
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800165 if (mCrypto != nullptr && mHeapSeqNum >= 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800166 mCrypto->unsetHeap(mHeapSeqNum);
167 }
168}
169
170void CCodecBufferChannel::setComponent(
171 const std::shared_ptr<Codec2Client::Component> &component) {
172 mComponent = component;
173 mComponentName = component->getName() + StringPrintf("#%d", int(uintptr_t(component.get()) % 997));
174 mName = mComponentName.c_str();
175}
176
177status_t CCodecBufferChannel::setInputSurface(
178 const std::shared_ptr<InputSurfaceWrapper> &surface) {
179 ALOGV("[%s] setInputSurface", mName);
180 mInputSurface = surface;
181 return mInputSurface->connect(mComponent);
182}
183
184status_t CCodecBufferChannel::signalEndOfInputStream() {
185 if (mInputSurface == nullptr) {
186 return INVALID_OPERATION;
187 }
188 return mInputSurface->signalEndOfInputStream();
189}
190
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700191status_t CCodecBufferChannel::queueInputBufferInternal(sp<MediaCodecBuffer> buffer) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800192 int64_t timeUs;
193 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
194
195 if (mInputMetEos) {
196 ALOGD("[%s] buffers after EOS ignored (%lld us)", mName, (long long)timeUs);
197 return OK;
198 }
199
200 int32_t flags = 0;
201 int32_t tmp = 0;
202 bool eos = false;
203 if (buffer->meta()->findInt32("eos", &tmp) && tmp) {
204 eos = true;
205 mInputMetEos = true;
206 ALOGV("[%s] input EOS", mName);
207 }
208 if (buffer->meta()->findInt32("csd", &tmp) && tmp) {
209 flags |= C2FrameData::FLAG_CODEC_CONFIG;
210 }
211 ALOGV("[%s] queueInputBuffer: buffer->size() = %zu", mName, buffer->size());
212 std::unique_ptr<C2Work> work(new C2Work);
213 work->input.ordinal.timestamp = timeUs;
214 work->input.ordinal.frameIndex = mFrameIndex++;
215 // WORKAROUND: until codecs support handling work after EOS and max output sizing, use timestamp
216 // manipulation to achieve image encoding via video codec, and to constrain encoded output.
217 // Keep client timestamp in customOrdinal
218 work->input.ordinal.customOrdinal = timeUs;
219 work->input.buffers.clear();
220
Wonsik Kimab34ed62019-01-31 15:28:46 -0800221 uint64_t queuedFrameIndex = work->input.ordinal.frameIndex.peeku();
222 std::vector<std::shared_ptr<C2Buffer>> queuedBuffers;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700223 sp<Codec2Buffer> copy;
Wonsik Kimab34ed62019-01-31 15:28:46 -0800224
Pawin Vongmasa36653902018-11-15 00:10:25 -0800225 if (buffer->size() > 0u) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700226 Mutexed<Input>::Locked input(mInput);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800227 std::shared_ptr<C2Buffer> c2buffer;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700228 if (!input->buffers->releaseBuffer(buffer, &c2buffer, false)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800229 return -ENOENT;
230 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700231 // TODO: we want to delay copying buffers.
232 if (input->extraBuffers.numComponentBuffers() < input->numExtraSlots) {
233 copy = input->buffers->cloneAndReleaseBuffer(buffer);
234 if (copy != nullptr) {
235 (void)input->extraBuffers.assignSlot(copy);
236 if (!input->extraBuffers.releaseSlot(copy, &c2buffer, false)) {
237 return UNKNOWN_ERROR;
238 }
239 bool released = input->buffers->releaseBuffer(buffer, nullptr, true);
240 ALOGV("[%s] queueInputBuffer: buffer copied; %sreleased",
241 mName, released ? "" : "not ");
242 buffer.clear();
243 } else {
244 ALOGW("[%s] queueInputBuffer: failed to copy a buffer; this may cause input "
245 "buffer starvation on component.", mName);
246 }
247 }
Byeongjo Park2eef13e2020-06-12 17:24:21 +0900248 int32_t cvo = 0;
249 if (buffer->meta()->findInt32("cvo", &cvo)) {
250 int32_t rotation = cvo % 360;
251 // change rotation to counter-clock wise.
252 rotation = ((rotation <= 0) ? 0 : 360) - rotation;
253 Mutexed<OutputSurface>::Locked output(mOutputSurface);
254 output->rotation[queuedFrameIndex] = rotation;
255 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800256 work->input.buffers.push_back(c2buffer);
Wonsik Kimab34ed62019-01-31 15:28:46 -0800257 queuedBuffers.push_back(c2buffer);
258 } else if (eos) {
259 flags |= C2FrameData::FLAG_END_OF_STREAM;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800260 }
261 work->input.flags = (C2FrameData::flags_t)flags;
262 // TODO: fill info's
263
264 work->input.configUpdate = std::move(mParamsToBeSet);
265 work->worklets.clear();
266 work->worklets.emplace_back(new C2Worklet);
267
268 std::list<std::unique_ptr<C2Work>> items;
269 items.push_back(std::move(work));
Wonsik Kimab34ed62019-01-31 15:28:46 -0800270 mPipelineWatcher.lock()->onWorkQueued(
271 queuedFrameIndex,
272 std::move(queuedBuffers),
273 PipelineWatcher::Clock::now());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800274 c2_status_t err = mComponent->queue(&items);
Wonsik Kimab34ed62019-01-31 15:28:46 -0800275 if (err != C2_OK) {
276 mPipelineWatcher.lock()->onWorkDone(queuedFrameIndex);
277 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800278
279 if (err == C2_OK && eos && buffer->size() > 0u) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800280 work.reset(new C2Work);
281 work->input.ordinal.timestamp = timeUs;
282 work->input.ordinal.frameIndex = mFrameIndex++;
283 // WORKAROUND: keep client timestamp in customOrdinal
284 work->input.ordinal.customOrdinal = timeUs;
285 work->input.buffers.clear();
286 work->input.flags = C2FrameData::FLAG_END_OF_STREAM;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800287 work->worklets.emplace_back(new C2Worklet);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800288
Wonsik Kimab34ed62019-01-31 15:28:46 -0800289 queuedFrameIndex = work->input.ordinal.frameIndex.peeku();
290 queuedBuffers.clear();
291
Pawin Vongmasa36653902018-11-15 00:10:25 -0800292 items.clear();
293 items.push_back(std::move(work));
Wonsik Kimab34ed62019-01-31 15:28:46 -0800294
295 mPipelineWatcher.lock()->onWorkQueued(
296 queuedFrameIndex,
297 std::move(queuedBuffers),
298 PipelineWatcher::Clock::now());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800299 err = mComponent->queue(&items);
Wonsik Kimab34ed62019-01-31 15:28:46 -0800300 if (err != C2_OK) {
301 mPipelineWatcher.lock()->onWorkDone(queuedFrameIndex);
302 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800303 }
304 if (err == C2_OK) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700305 Mutexed<Input>::Locked input(mInput);
306 bool released = false;
307 if (buffer) {
308 released = input->buffers->releaseBuffer(buffer, nullptr, true);
309 } else if (copy) {
310 released = input->extraBuffers.releaseSlot(copy, nullptr, true);
311 }
312 ALOGV("[%s] queueInputBuffer: buffer%s %sreleased",
313 mName, (buffer == nullptr) ? "(copy)" : "", released ? "" : "not ");
Pawin Vongmasa36653902018-11-15 00:10:25 -0800314 }
315
316 feedInputBufferIfAvailableInternal();
317 return err;
318}
319
320status_t CCodecBufferChannel::setParameters(std::vector<std::unique_ptr<C2Param>> &params) {
321 QueueGuard guard(mSync);
322 if (!guard.isRunning()) {
323 ALOGD("[%s] setParameters is only supported in the running state.", mName);
324 return -ENOSYS;
325 }
326 mParamsToBeSet.insert(mParamsToBeSet.end(),
327 std::make_move_iterator(params.begin()),
328 std::make_move_iterator(params.end()));
329 params.clear();
330 return OK;
331}
332
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800333status_t CCodecBufferChannel::attachBuffer(
334 const std::shared_ptr<C2Buffer> &c2Buffer,
335 const sp<MediaCodecBuffer> &buffer) {
336 if (!buffer->copy(c2Buffer)) {
337 return -ENOSYS;
338 }
339 return OK;
340}
341
342void CCodecBufferChannel::ensureDecryptDestination(size_t size) {
343 if (!mDecryptDestination || mDecryptDestination->size() < size) {
344 sp<IMemoryHeap> heap{new MemoryHeapBase(size * 2)};
345 if (mDecryptDestination && mCrypto && mHeapSeqNum >= 0) {
346 mCrypto->unsetHeap(mHeapSeqNum);
347 }
348 mDecryptDestination = new MemoryBase(heap, 0, size * 2);
349 if (mCrypto) {
350 mHeapSeqNum = mCrypto->setHeap(hardware::fromHeap(heap));
351 }
352 }
353}
354
355int32_t CCodecBufferChannel::getHeapSeqNum(const sp<HidlMemory> &memory) {
356 CHECK(mCrypto);
357 auto it = mHeapSeqNumMap.find(memory);
358 int32_t heapSeqNum = -1;
359 if (it == mHeapSeqNumMap.end()) {
360 heapSeqNum = mCrypto->setHeap(memory);
361 mHeapSeqNumMap.emplace(memory, heapSeqNum);
362 } else {
363 heapSeqNum = it->second;
364 }
365 return heapSeqNum;
366}
367
368status_t CCodecBufferChannel::attachEncryptedBuffer(
369 const sp<hardware::HidlMemory> &memory,
370 bool secure,
371 const uint8_t *key,
372 const uint8_t *iv,
373 CryptoPlugin::Mode mode,
374 CryptoPlugin::Pattern pattern,
375 size_t offset,
376 const CryptoPlugin::SubSample *subSamples,
377 size_t numSubSamples,
378 const sp<MediaCodecBuffer> &buffer) {
379 static const C2MemoryUsage kSecureUsage{C2MemoryUsage::READ_PROTECTED, 0};
380 static const C2MemoryUsage kDefaultReadWriteUsage{
381 C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE};
382
383 size_t size = 0;
384 for (size_t i = 0; i < numSubSamples; ++i) {
385 size += subSamples[i].mNumBytesOfClearData + subSamples[i].mNumBytesOfEncryptedData;
386 }
387 std::shared_ptr<C2BlockPool> pool = mBlockPools.lock()->inputPool;
388 std::shared_ptr<C2LinearBlock> block;
389 c2_status_t err = pool->fetchLinearBlock(
390 size,
391 secure ? kSecureUsage : kDefaultReadWriteUsage,
392 &block);
393 if (err != C2_OK) {
394 return NO_MEMORY;
395 }
396 if (!secure) {
397 ensureDecryptDestination(size);
398 }
399 ssize_t result = -1;
400 ssize_t codecDataOffset = 0;
401 if (mCrypto) {
402 AString errorDetailMsg;
403 int32_t heapSeqNum = getHeapSeqNum(memory);
404 hardware::drm::V1_0::SharedBuffer src{(uint32_t)heapSeqNum, offset, size};
405 hardware::drm::V1_0::DestinationBuffer dst;
406 if (secure) {
407 dst.type = DrmBufferType::NATIVE_HANDLE;
408 dst.secureMemory = hardware::hidl_handle(block->handle());
409 } else {
410 dst.type = DrmBufferType::SHARED_MEMORY;
411 IMemoryToSharedBuffer(
412 mDecryptDestination, mHeapSeqNum, &dst.nonsecureMemory);
413 }
414 result = mCrypto->decrypt(
415 key, iv, mode, pattern, src, 0, subSamples, numSubSamples,
416 dst, &errorDetailMsg);
417 if (result < 0) {
418 return result;
419 }
420 if (dst.type == DrmBufferType::SHARED_MEMORY) {
421 C2WriteView view = block->map().get();
422 if (view.error() != C2_OK) {
423 return false;
424 }
425 if (view.size() < result) {
426 return false;
427 }
428 memcpy(view.data(), mDecryptDestination->unsecurePointer(), result);
429 }
430 } else {
431 // Here we cast CryptoPlugin::SubSample to hardware::cas::native::V1_0::SubSample
432 // directly, the structure definitions should match as checked in DescramblerImpl.cpp.
433 hidl_vec<SubSample> hidlSubSamples;
434 hidlSubSamples.setToExternal((SubSample *)subSamples, numSubSamples, false /*own*/);
435
436 hardware::cas::native::V1_0::SharedBuffer src{*memory, offset, size};
437 hardware::cas::native::V1_0::DestinationBuffer dst;
438 if (secure) {
439 dst.type = BufferType::NATIVE_HANDLE;
440 dst.secureMemory = hardware::hidl_handle(block->handle());
441 } else {
442 dst.type = BufferType::SHARED_MEMORY;
443 dst.nonsecureMemory = src;
444 }
445
446 CasStatus status = CasStatus::OK;
447 hidl_string detailedError;
448 ScramblingControl sctrl = ScramblingControl::UNSCRAMBLED;
449
450 if (key != nullptr) {
451 sctrl = (ScramblingControl)key[0];
452 // Adjust for the PES offset
453 codecDataOffset = key[2] | (key[3] << 8);
454 }
455
456 auto returnVoid = mDescrambler->descramble(
457 sctrl,
458 hidlSubSamples,
459 src,
460 0,
461 dst,
462 0,
463 [&status, &result, &detailedError] (
464 CasStatus _status, uint32_t _bytesWritten,
465 const hidl_string& _detailedError) {
466 status = _status;
467 result = (ssize_t)_bytesWritten;
468 detailedError = _detailedError;
469 });
470
471 if (!returnVoid.isOk() || status != CasStatus::OK || result < 0) {
472 ALOGI("[%s] descramble failed, trans=%s, status=%d, result=%zd",
473 mName, returnVoid.description().c_str(), status, result);
474 return UNKNOWN_ERROR;
475 }
476
477 if (result < codecDataOffset) {
478 ALOGD("invalid codec data offset: %zd, result %zd", codecDataOffset, result);
479 return BAD_VALUE;
480 }
481 }
482 if (!secure) {
483 C2WriteView view = block->map().get();
484 if (view.error() != C2_OK) {
485 return UNKNOWN_ERROR;
486 }
487 if (view.size() < result) {
488 return UNKNOWN_ERROR;
489 }
490 memcpy(view.data(), mDecryptDestination->unsecurePointer(), result);
491 }
492 std::shared_ptr<C2Buffer> c2Buffer{C2Buffer::CreateLinearBuffer(
493 block->share(codecDataOffset, result - codecDataOffset, C2Fence{}))};
494 if (!buffer->copy(c2Buffer)) {
495 return -ENOSYS;
496 }
497 return OK;
498}
499
Pawin Vongmasa36653902018-11-15 00:10:25 -0800500status_t CCodecBufferChannel::queueInputBuffer(const sp<MediaCodecBuffer> &buffer) {
501 QueueGuard guard(mSync);
502 if (!guard.isRunning()) {
503 ALOGD("[%s] No more buffers should be queued at current state.", mName);
504 return -ENOSYS;
505 }
506 return queueInputBufferInternal(buffer);
507}
508
509status_t CCodecBufferChannel::queueSecureInputBuffer(
510 const sp<MediaCodecBuffer> &buffer, bool secure, const uint8_t *key,
511 const uint8_t *iv, CryptoPlugin::Mode mode, CryptoPlugin::Pattern pattern,
512 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
513 AString *errorDetailMsg) {
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
520 if (!hasCryptoOrDescrambler()) {
521 return -ENOSYS;
522 }
523 sp<EncryptedLinearBlockBuffer> encryptedBuffer((EncryptedLinearBlockBuffer *)buffer.get());
524
525 ssize_t result = -1;
526 ssize_t codecDataOffset = 0;
Wonsik Kim557c88c2020-03-13 11:03:52 -0700527 if (numSubSamples == 1
528 && subSamples[0].mNumBytesOfClearData == 0
529 && subSamples[0].mNumBytesOfEncryptedData == 0) {
530 // We don't need to go through crypto or descrambler if the input is empty.
531 result = 0;
532 } else if (mCrypto != nullptr) {
Robert Shih895fba92019-07-16 16:29:44 -0700533 hardware::drm::V1_0::DestinationBuffer destination;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800534 if (secure) {
Robert Shih895fba92019-07-16 16:29:44 -0700535 destination.type = DrmBufferType::NATIVE_HANDLE;
536 destination.secureMemory = hidl_handle(encryptedBuffer->handle());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800537 } else {
Robert Shih895fba92019-07-16 16:29:44 -0700538 destination.type = DrmBufferType::SHARED_MEMORY;
539 IMemoryToSharedBuffer(
540 mDecryptDestination, mHeapSeqNum, &destination.nonsecureMemory);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800541 }
Robert Shih895fba92019-07-16 16:29:44 -0700542 hardware::drm::V1_0::SharedBuffer source;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800543 encryptedBuffer->fillSourceBuffer(&source);
544 result = mCrypto->decrypt(
545 key, iv, mode, pattern, source, buffer->offset(),
546 subSamples, numSubSamples, destination, errorDetailMsg);
547 if (result < 0) {
Wonsik Kim557c88c2020-03-13 11:03:52 -0700548 ALOGI("[%s] decrypt failed: result=%zd", mName, result);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800549 return result;
550 }
Robert Shih895fba92019-07-16 16:29:44 -0700551 if (destination.type == DrmBufferType::SHARED_MEMORY) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800552 encryptedBuffer->copyDecryptedContent(mDecryptDestination, result);
553 }
554 } else {
555 // Here we cast CryptoPlugin::SubSample to hardware::cas::native::V1_0::SubSample
556 // directly, the structure definitions should match as checked in DescramblerImpl.cpp.
557 hidl_vec<SubSample> hidlSubSamples;
558 hidlSubSamples.setToExternal((SubSample *)subSamples, numSubSamples, false /*own*/);
559
560 hardware::cas::native::V1_0::SharedBuffer srcBuffer;
561 encryptedBuffer->fillSourceBuffer(&srcBuffer);
562
563 DestinationBuffer dstBuffer;
564 if (secure) {
565 dstBuffer.type = BufferType::NATIVE_HANDLE;
566 dstBuffer.secureMemory = hidl_handle(encryptedBuffer->handle());
567 } else {
568 dstBuffer.type = BufferType::SHARED_MEMORY;
569 dstBuffer.nonsecureMemory = srcBuffer;
570 }
571
572 CasStatus status = CasStatus::OK;
573 hidl_string detailedError;
574 ScramblingControl sctrl = ScramblingControl::UNSCRAMBLED;
575
576 if (key != nullptr) {
577 sctrl = (ScramblingControl)key[0];
578 // Adjust for the PES offset
579 codecDataOffset = key[2] | (key[3] << 8);
580 }
581
582 auto returnVoid = mDescrambler->descramble(
583 sctrl,
584 hidlSubSamples,
585 srcBuffer,
586 0,
587 dstBuffer,
588 0,
589 [&status, &result, &detailedError] (
590 CasStatus _status, uint32_t _bytesWritten,
591 const hidl_string& _detailedError) {
592 status = _status;
593 result = (ssize_t)_bytesWritten;
594 detailedError = _detailedError;
595 });
596
597 if (!returnVoid.isOk() || status != CasStatus::OK || result < 0) {
598 ALOGI("[%s] descramble failed, trans=%s, status=%d, result=%zd",
599 mName, returnVoid.description().c_str(), status, result);
600 return UNKNOWN_ERROR;
601 }
602
603 if (result < codecDataOffset) {
604 ALOGD("invalid codec data offset: %zd, result %zd", codecDataOffset, result);
605 return BAD_VALUE;
606 }
607
608 ALOGV("[%s] descramble succeeded, %zd bytes", mName, result);
609
610 if (dstBuffer.type == BufferType::SHARED_MEMORY) {
611 encryptedBuffer->copyDecryptedContentFromMemory(result);
612 }
613 }
614
615 buffer->setRange(codecDataOffset, result - codecDataOffset);
616 return queueInputBufferInternal(buffer);
617}
618
619void CCodecBufferChannel::feedInputBufferIfAvailable() {
620 QueueGuard guard(mSync);
621 if (!guard.isRunning()) {
622 ALOGV("[%s] We're not running --- no input buffer reported", mName);
623 return;
624 }
625 feedInputBufferIfAvailableInternal();
626}
627
628void CCodecBufferChannel::feedInputBufferIfAvailableInternal() {
Taehwan Kimda0517d2020-09-16 17:29:37 +0900629 if (mInputMetEos) {
Wonsik Kimdf5dd142019-02-06 10:15:46 -0800630 return;
Pawin Vongmasac3c536d2020-06-12 04:00:04 -0700631 }
632 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700633 Mutexed<Output>::Locked output(mOutput);
Pawin Vongmasac3c536d2020-06-12 04:00:04 -0700634 if (!output->buffers ||
635 output->buffers->hasPending() ||
Wonsik Kim0487b782020-10-28 11:45:50 -0700636 output->buffers->numActiveSlots() >= output->numSlots) {
Wonsik Kimdf5dd142019-02-06 10:15:46 -0800637 return;
638 }
639 }
Wonsik Kim0487b782020-10-28 11:45:50 -0700640 size_t numActiveSlots = 0;
641 while (!mPipelineWatcher.lock()->pipelineFull()) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800642 sp<MediaCodecBuffer> inBuffer;
643 size_t index;
644 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700645 Mutexed<Input>::Locked input(mInput);
Wonsik Kim0487b782020-10-28 11:45:50 -0700646 numActiveSlots = input->buffers->numActiveSlots();
647 if (numActiveSlots >= input->numSlots) {
648 break;
Wonsik Kimab34ed62019-01-31 15:28:46 -0800649 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700650 if (!input->buffers->requestNewBuffer(&index, &inBuffer)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800651 ALOGV("[%s] no new buffer available", mName);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800652 break;
653 }
654 }
655 ALOGV("[%s] new input index = %zu [%p]", mName, index, inBuffer.get());
656 mCallback->onInputBufferAvailable(index, inBuffer);
657 }
Wonsik Kim0487b782020-10-28 11:45:50 -0700658 ALOGV("[%s] # active slots after feedInputBufferIfAvailable = %zu", mName, numActiveSlots);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800659}
660
661status_t CCodecBufferChannel::renderOutputBuffer(
662 const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) {
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800663 ALOGV("[%s] renderOutputBuffer: %p", mName, buffer.get());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800664 std::shared_ptr<C2Buffer> c2Buffer;
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800665 bool released = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800666 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700667 Mutexed<Output>::Locked output(mOutput);
668 if (output->buffers) {
669 released = output->buffers->releaseBuffer(buffer, &c2Buffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800670 }
671 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800672 // NOTE: some apps try to releaseOutputBuffer() with timestamp and/or render
673 // set to true.
674 sendOutputBuffers();
675 // input buffer feeding may have been gated by pending output buffers
676 feedInputBufferIfAvailable();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800677 if (!c2Buffer) {
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800678 if (released) {
Wonsik Kimf7529dd2019-04-18 17:35:53 -0700679 std::call_once(mRenderWarningFlag, [this] {
680 ALOGW("[%s] The app is calling releaseOutputBuffer() with "
681 "timestamp or render=true with non-video buffers. Apps should "
682 "call releaseOutputBuffer() with render=false for those.",
683 mName);
684 });
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800685 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800686 return INVALID_OPERATION;
687 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800688
689#if 0
690 const std::vector<std::shared_ptr<const C2Info>> infoParams = c2Buffer->info();
691 ALOGV("[%s] queuing gfx buffer with %zu infos", mName, infoParams.size());
692 for (const std::shared_ptr<const C2Info> &info : infoParams) {
693 AString res;
694 for (size_t ix = 0; ix + 3 < info->size(); ix += 4) {
695 if (ix) res.append(", ");
696 res.append(*((int32_t*)info.get() + (ix / 4)));
697 }
698 ALOGV(" [%s]", res.c_str());
699 }
700#endif
701 std::shared_ptr<const C2StreamRotationInfo::output> rotation =
702 std::static_pointer_cast<const C2StreamRotationInfo::output>(
703 c2Buffer->getInfo(C2StreamRotationInfo::output::PARAM_TYPE));
704 bool flip = rotation && (rotation->flip & 1);
705 uint32_t quarters = ((rotation ? rotation->value : 0) / 90) & 3;
Byeongjo Park2eef13e2020-06-12 17:24:21 +0900706
707 {
708 Mutexed<OutputSurface>::Locked output(mOutputSurface);
709 if (output->surface == nullptr) {
710 ALOGI("[%s] cannot render buffer without surface", mName);
711 return OK;
712 }
713 int64_t frameIndex;
714 buffer->meta()->findInt64("frameIndex", &frameIndex);
715 if (output->rotation.count(frameIndex) != 0) {
716 auto it = output->rotation.find(frameIndex);
717 quarters = (it->second / 90) & 3;
718 output->rotation.erase(it);
719 }
720 }
721
Pawin Vongmasa36653902018-11-15 00:10:25 -0800722 uint32_t transform = 0;
723 switch (quarters) {
724 case 0: // no rotation
725 transform = flip ? HAL_TRANSFORM_FLIP_H : 0;
726 break;
727 case 1: // 90 degrees counter-clockwise
728 transform = flip ? (HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90)
729 : HAL_TRANSFORM_ROT_270;
730 break;
731 case 2: // 180 degrees
732 transform = flip ? HAL_TRANSFORM_FLIP_V : HAL_TRANSFORM_ROT_180;
733 break;
734 case 3: // 90 degrees clockwise
735 transform = flip ? (HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90)
736 : HAL_TRANSFORM_ROT_90;
737 break;
738 }
739
740 std::shared_ptr<const C2StreamSurfaceScalingInfo::output> surfaceScaling =
741 std::static_pointer_cast<const C2StreamSurfaceScalingInfo::output>(
742 c2Buffer->getInfo(C2StreamSurfaceScalingInfo::output::PARAM_TYPE));
743 uint32_t videoScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
744 if (surfaceScaling) {
745 videoScalingMode = surfaceScaling->value;
746 }
747
748 // Use dataspace from format as it has the default aspects already applied
749 android_dataspace_t dataSpace = HAL_DATASPACE_UNKNOWN; // this is 0
750 (void)buffer->format()->findInt32("android._dataspace", (int32_t *)&dataSpace);
751
752 // HDR static info
753 std::shared_ptr<const C2StreamHdrStaticInfo::output> hdrStaticInfo =
754 std::static_pointer_cast<const C2StreamHdrStaticInfo::output>(
755 c2Buffer->getInfo(C2StreamHdrStaticInfo::output::PARAM_TYPE));
756
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800757 // HDR10 plus info
758 std::shared_ptr<const C2StreamHdr10PlusInfo::output> hdr10PlusInfo =
759 std::static_pointer_cast<const C2StreamHdr10PlusInfo::output>(
760 c2Buffer->getInfo(C2StreamHdr10PlusInfo::output::PARAM_TYPE));
Yichi Chen54be23c2020-06-15 14:30:53 +0800761 if (hdr10PlusInfo && hdr10PlusInfo->flexCount() == 0) {
762 hdr10PlusInfo.reset();
763 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800764
Pawin Vongmasa36653902018-11-15 00:10:25 -0800765 std::vector<C2ConstGraphicBlock> blocks = c2Buffer->data().graphicBlocks();
766 if (blocks.size() != 1u) {
767 ALOGD("[%s] expected 1 graphic block, but got %zu", mName, blocks.size());
768 return UNKNOWN_ERROR;
769 }
770 const C2ConstGraphicBlock &block = blocks.front();
771
772 // TODO: revisit this after C2Fence implementation.
773 android::IGraphicBufferProducer::QueueBufferInput qbi(
774 timestampNs,
775 false, // droppable
776 dataSpace,
777 Rect(blocks.front().crop().left,
778 blocks.front().crop().top,
779 blocks.front().crop().right(),
780 blocks.front().crop().bottom()),
781 videoScalingMode,
782 transform,
783 Fence::NO_FENCE, 0);
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800784 if (hdrStaticInfo || hdr10PlusInfo) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800785 HdrMetadata hdr;
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800786 if (hdrStaticInfo) {
wenchangliuf3f92882020-05-14 00:02:01 +0800787 // If mastering max and min luminance fields are 0, do not use them.
788 // It indicates the value may not be present in the stream.
789 if (hdrStaticInfo->mastering.maxLuminance > 0.0f &&
790 hdrStaticInfo->mastering.minLuminance > 0.0f) {
791 struct android_smpte2086_metadata smpte2086_meta = {
792 .displayPrimaryRed = {
793 hdrStaticInfo->mastering.red.x, hdrStaticInfo->mastering.red.y
794 },
795 .displayPrimaryGreen = {
796 hdrStaticInfo->mastering.green.x, hdrStaticInfo->mastering.green.y
797 },
798 .displayPrimaryBlue = {
799 hdrStaticInfo->mastering.blue.x, hdrStaticInfo->mastering.blue.y
800 },
801 .whitePoint = {
802 hdrStaticInfo->mastering.white.x, hdrStaticInfo->mastering.white.y
803 },
804 .maxLuminance = hdrStaticInfo->mastering.maxLuminance,
805 .minLuminance = hdrStaticInfo->mastering.minLuminance,
806 };
Yichi Chen54be23c2020-06-15 14:30:53 +0800807 hdr.validTypes |= HdrMetadata::SMPTE2086;
wenchangliuf3f92882020-05-14 00:02:01 +0800808 hdr.smpte2086 = smpte2086_meta;
809 }
Chong Zhang3bb2a7f2020-04-21 10:35:12 -0700810 // If the content light level fields are 0, do not use them, it
811 // indicates the value may not be present in the stream.
812 if (hdrStaticInfo->maxCll > 0.0f && hdrStaticInfo->maxFall > 0.0f) {
813 struct android_cta861_3_metadata cta861_meta = {
814 .maxContentLightLevel = hdrStaticInfo->maxCll,
815 .maxFrameAverageLightLevel = hdrStaticInfo->maxFall,
816 };
817 hdr.validTypes |= HdrMetadata::CTA861_3;
818 hdr.cta8613 = cta861_meta;
819 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800820 }
821 if (hdr10PlusInfo) {
822 hdr.validTypes |= HdrMetadata::HDR10PLUS;
823 hdr.hdr10plus.assign(
824 hdr10PlusInfo->m.value,
825 hdr10PlusInfo->m.value + hdr10PlusInfo->flexCount());
826 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800827 qbi.setHdrMetadata(hdr);
828 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800829 // we don't have dirty regions
830 qbi.setSurfaceDamage(Region::INVALID_REGION);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800831 android::IGraphicBufferProducer::QueueBufferOutput qbo;
832 status_t result = mComponent->queueToOutputSurface(block, qbi, &qbo);
833 if (result != OK) {
834 ALOGI("[%s] queueBuffer failed: %d", mName, result);
Sungtak Lee47c018a2020-11-07 01:02:49 -0800835 if (result == NO_INIT) {
836 mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
837 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800838 return result;
839 }
840 ALOGV("[%s] queue buffer successful", mName);
841
842 int64_t mediaTimeUs = 0;
843 (void)buffer->meta()->findInt64("timeUs", &mediaTimeUs);
844 mCCodecCallback->onOutputFramesRendered(mediaTimeUs, timestampNs);
845
846 return OK;
847}
848
849status_t CCodecBufferChannel::discardBuffer(const sp<MediaCodecBuffer> &buffer) {
850 ALOGV("[%s] discardBuffer: %p", mName, buffer.get());
851 bool released = false;
852 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700853 Mutexed<Input>::Locked input(mInput);
854 if (input->buffers && input->buffers->releaseBuffer(buffer, nullptr, true)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800855 released = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800856 }
857 }
858 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700859 Mutexed<Output>::Locked output(mOutput);
860 if (output->buffers && output->buffers->releaseBuffer(buffer, nullptr)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800861 released = true;
862 }
863 }
864 if (released) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800865 sendOutputBuffers();
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800866 feedInputBufferIfAvailable();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800867 } else {
868 ALOGD("[%s] MediaCodec discarded an unknown buffer", mName);
869 }
870 return OK;
871}
872
873void CCodecBufferChannel::getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) {
874 array->clear();
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700875 Mutexed<Input>::Locked input(mInput);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800876
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700877 if (!input->buffers->isArrayMode()) {
878 input->buffers = input->buffers->toArrayMode(input->numSlots);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800879 }
880
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700881 input->buffers->getArray(array);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800882}
883
884void CCodecBufferChannel::getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) {
885 array->clear();
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700886 Mutexed<Output>::Locked output(mOutput);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800887
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700888 if (!output->buffers->isArrayMode()) {
889 output->buffers = output->buffers->toArrayMode(output->numSlots);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800890 }
891
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700892 output->buffers->getArray(array);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800893}
894
895status_t CCodecBufferChannel::start(
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800896 const sp<AMessage> &inputFormat,
897 const sp<AMessage> &outputFormat,
898 bool buffersBoundToCodec) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800899 C2StreamBufferTypeSetting::input iStreamFormat(0u);
900 C2StreamBufferTypeSetting::output oStreamFormat(0u);
901 C2PortReorderBufferDepthTuning::output reorderDepth;
902 C2PortReorderKeySetting::output reorderKey;
Wonsik Kim078b58e2019-01-09 15:08:06 -0800903 C2PortActualDelayTuning::input inputDelay(0);
904 C2PortActualDelayTuning::output outputDelay(0);
905 C2ActualPipelineDelayTuning pipelineDelay(0);
906
Pawin Vongmasa36653902018-11-15 00:10:25 -0800907 c2_status_t err = mComponent->query(
908 {
909 &iStreamFormat,
910 &oStreamFormat,
911 &reorderDepth,
912 &reorderKey,
Wonsik Kim078b58e2019-01-09 15:08:06 -0800913 &inputDelay,
914 &pipelineDelay,
915 &outputDelay,
Pawin Vongmasa36653902018-11-15 00:10:25 -0800916 },
917 {},
918 C2_DONT_BLOCK,
919 nullptr);
920 if (err == C2_BAD_INDEX) {
921 if (!iStreamFormat || !oStreamFormat) {
922 return UNKNOWN_ERROR;
923 }
924 } else if (err != C2_OK) {
925 return UNKNOWN_ERROR;
926 }
927
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -0800928 uint32_t inputDelayValue = inputDelay ? inputDelay.value : 0;
929 uint32_t pipelineDelayValue = pipelineDelay ? pipelineDelay.value : 0;
930 uint32_t outputDelayValue = outputDelay ? outputDelay.value : 0;
931
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700932 size_t numInputSlots = inputDelayValue + pipelineDelayValue + kSmoothnessFactor;
933 size_t numOutputSlots = outputDelayValue + kSmoothnessFactor;
Wonsik Kim078b58e2019-01-09 15:08:06 -0800934
Pawin Vongmasa36653902018-11-15 00:10:25 -0800935 // TODO: get this from input format
936 bool secure = mComponent->getName().find(".secure") != std::string::npos;
937
938 std::shared_ptr<C2AllocatorStore> allocatorStore = GetCodec2PlatformAllocatorStore();
Pin-chih Linaa18ea52019-11-19 18:48:50 +0800939 int poolMask = GetCodec2PoolMask();
940 C2PlatformAllocatorStore::id_t preferredLinearId = GetPreferredLinearAllocatorId(poolMask);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800941
942 if (inputFormat != nullptr) {
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800943 bool graphic = (iStreamFormat.value == C2BufferData::GRAPHIC);
Wonsik Kimffb889a2020-05-28 11:32:25 -0700944 C2Config::api_feature_t apiFeatures = C2Config::api_feature_t(
945 API_REFLECTION |
946 API_VALUES |
947 API_CURRENT_VALUES |
948 API_DEPENDENCY |
949 API_SAME_INPUT_BUFFER);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800950 std::shared_ptr<C2BlockPool> pool;
951 {
952 Mutexed<BlockPools>::Locked pools(mBlockPools);
953
954 // set default allocator ID.
955 pools->inputAllocatorId = (graphic) ? C2PlatformAllocatorStore::GRALLOC
Pin-chih Linaa18ea52019-11-19 18:48:50 +0800956 : preferredLinearId;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800957
958 // query C2PortAllocatorsTuning::input from component. If an allocator ID is obtained
959 // from component, create the input block pool with given ID. Otherwise, use default IDs.
960 std::vector<std::unique_ptr<C2Param>> params;
Wonsik Kimffb889a2020-05-28 11:32:25 -0700961 C2ApiFeaturesSetting featuresSetting{apiFeatures};
962 err = mComponent->query({ &featuresSetting },
Pawin Vongmasa36653902018-11-15 00:10:25 -0800963 { C2PortAllocatorsTuning::input::PARAM_TYPE },
964 C2_DONT_BLOCK,
965 &params);
966 if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
967 ALOGD("[%s] Query input allocators returned %zu params => %s (%u)",
968 mName, params.size(), asString(err), err);
Wonsik Kimffb889a2020-05-28 11:32:25 -0700969 } else if (params.size() == 1) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800970 C2PortAllocatorsTuning::input *inputAllocators =
971 C2PortAllocatorsTuning::input::From(params[0].get());
972 if (inputAllocators && inputAllocators->flexCount() > 0) {
973 std::shared_ptr<C2Allocator> allocator;
974 // verify allocator IDs and resolve default allocator
975 allocatorStore->fetchAllocator(inputAllocators->m.values[0], &allocator);
976 if (allocator) {
977 pools->inputAllocatorId = allocator->getId();
978 } else {
979 ALOGD("[%s] component requested invalid input allocator ID %u",
980 mName, inputAllocators->m.values[0]);
981 }
982 }
983 }
Wonsik Kimffb889a2020-05-28 11:32:25 -0700984 if (featuresSetting) {
985 apiFeatures = featuresSetting.value;
986 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800987
988 // TODO: use C2Component wrapper to associate this pool with ourselves
989 if ((poolMask >> pools->inputAllocatorId) & 1) {
990 err = CreateCodec2BlockPool(pools->inputAllocatorId, nullptr, &pool);
991 ALOGD("[%s] Created input block pool with allocatorID %u => poolID %llu - %s (%d)",
992 mName, pools->inputAllocatorId,
993 (unsigned long long)(pool ? pool->getLocalId() : 111000111),
994 asString(err), err);
995 } else {
996 err = C2_NOT_FOUND;
997 }
998 if (err != C2_OK) {
999 C2BlockPool::local_id_t inputPoolId =
1000 graphic ? C2BlockPool::BASIC_GRAPHIC : C2BlockPool::BASIC_LINEAR;
1001 err = GetCodec2BlockPool(inputPoolId, nullptr, &pool);
1002 ALOGD("[%s] Using basic input block pool with poolID %llu => got %llu - %s (%d)",
1003 mName, (unsigned long long)inputPoolId,
1004 (unsigned long long)(pool ? pool->getLocalId() : 111000111),
1005 asString(err), err);
1006 if (err != C2_OK) {
1007 return NO_MEMORY;
1008 }
1009 }
1010 pools->inputPool = pool;
1011 }
1012
Wonsik Kim51051262018-11-28 13:59:05 -08001013 bool forceArrayMode = false;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001014 Mutexed<Input>::Locked input(mInput);
Wonsik Kimbdffead2019-07-01 12:00:07 -07001015 input->inputDelay = inputDelayValue;
1016 input->pipelineDelay = pipelineDelayValue;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001017 input->numSlots = numInputSlots;
1018 input->extraBuffers.flush();
1019 input->numExtraSlots = 0u;
Wonsik Kimffb889a2020-05-28 11:32:25 -07001020 bool conforming = (apiFeatures & API_SAME_INPUT_BUFFER);
1021 // For encrypted content, framework decrypts source buffer (ashmem) into
1022 // C2Buffers. Thus non-conforming codecs can process these.
1023 if (!buffersBoundToCodec && (hasCryptoOrDescrambler() || conforming)) {
Wonsik Kimfb7a7672019-12-27 17:13:33 -08001024 input->buffers.reset(new SlotInputBuffers(mName));
1025 } else if (graphic) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001026 if (mInputSurface) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001027 input->buffers.reset(new DummyInputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001028 } else if (mMetaMode == MODE_ANW) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001029 input->buffers.reset(new GraphicMetadataInputBuffers(mName));
Wonsik Kim1221fd12019-07-12 12:52:05 -07001030 // This is to ensure buffers do not get released prematurely.
1031 // TODO: handle this without going into array mode
1032 forceArrayMode = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001033 } else {
Wonsik Kim41d83432020-04-27 16:40:49 -07001034 input->buffers.reset(new GraphicInputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001035 }
1036 } else {
1037 if (hasCryptoOrDescrambler()) {
1038 int32_t capacity = kLinearBufferSize;
1039 (void)inputFormat->findInt32(KEY_MAX_INPUT_SIZE, &capacity);
1040 if ((size_t)capacity > kMaxLinearBufferSize) {
1041 ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize);
1042 capacity = kMaxLinearBufferSize;
1043 }
1044 if (mDealer == nullptr) {
1045 mDealer = new MemoryDealer(
1046 align(capacity, MemoryDealer::getAllocationAlignment())
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001047 * (numInputSlots + 1),
Pawin Vongmasa36653902018-11-15 00:10:25 -08001048 "EncryptedLinearInputBuffers");
1049 mDecryptDestination = mDealer->allocate((size_t)capacity);
1050 }
1051 if (mCrypto != nullptr && mHeapSeqNum < 0) {
Robert Shih895fba92019-07-16 16:29:44 -07001052 sp<HidlMemory> heap = fromHeap(mDealer->getMemoryHeap());
1053 mHeapSeqNum = mCrypto->setHeap(heap);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001054 } else {
1055 mHeapSeqNum = -1;
1056 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001057 input->buffers.reset(new EncryptedLinearInputBuffers(
Wonsik Kim078b58e2019-01-09 15:08:06 -08001058 secure, mDealer, mCrypto, mHeapSeqNum, (size_t)capacity,
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001059 numInputSlots, mName));
Wonsik Kim51051262018-11-28 13:59:05 -08001060 forceArrayMode = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001061 } else {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001062 input->buffers.reset(new LinearInputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001063 }
1064 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001065 input->buffers->setFormat(inputFormat);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001066
1067 if (err == C2_OK) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001068 input->buffers->setPool(pool);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001069 } else {
1070 // TODO: error
1071 }
Wonsik Kim51051262018-11-28 13:59:05 -08001072
1073 if (forceArrayMode) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001074 input->buffers = input->buffers->toArrayMode(numInputSlots);
Wonsik Kim51051262018-11-28 13:59:05 -08001075 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001076 }
1077
1078 if (outputFormat != nullptr) {
1079 sp<IGraphicBufferProducer> outputSurface;
1080 uint32_t outputGeneration;
1081 {
1082 Mutexed<OutputSurface>::Locked output(mOutputSurface);
Sungtak Leed7463d12019-09-04 16:01:00 -07001083 output->maxDequeueBuffers = numOutputSlots +
Sungtak Lee7a7b7422019-07-16 17:40:40 -07001084 reorderDepth.value + kRenderingDepth;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001085 outputSurface = output->surface ?
1086 output->surface->getIGraphicBufferProducer() : nullptr;
Wonsik Kimf5e5c832019-02-21 11:36:05 -08001087 if (outputSurface) {
1088 output->surface->setMaxDequeuedBufferCount(output->maxDequeueBuffers);
1089 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001090 outputGeneration = output->generation;
1091 }
1092
Lajos Molnar3bb81cd2019-02-20 15:10:30 -08001093 bool graphic = (oStreamFormat.value == C2BufferData::GRAPHIC);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001094 C2BlockPool::local_id_t outputPoolId_;
1095
1096 {
1097 Mutexed<BlockPools>::Locked pools(mBlockPools);
1098
1099 // set default allocator ID.
1100 pools->outputAllocatorId = (graphic) ? C2PlatformAllocatorStore::GRALLOC
Pin-chih Linaa18ea52019-11-19 18:48:50 +08001101 : preferredLinearId;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001102
1103 // query C2PortAllocatorsTuning::output from component, or use default allocator if
1104 // unsuccessful.
1105 std::vector<std::unique_ptr<C2Param>> params;
1106 err = mComponent->query({ },
1107 { C2PortAllocatorsTuning::output::PARAM_TYPE },
1108 C2_DONT_BLOCK,
1109 &params);
1110 if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
1111 ALOGD("[%s] Query output allocators returned %zu params => %s (%u)",
1112 mName, params.size(), asString(err), err);
1113 } else if (err == C2_OK && params.size() == 1) {
1114 C2PortAllocatorsTuning::output *outputAllocators =
1115 C2PortAllocatorsTuning::output::From(params[0].get());
1116 if (outputAllocators && outputAllocators->flexCount() > 0) {
1117 std::shared_ptr<C2Allocator> allocator;
1118 // verify allocator IDs and resolve default allocator
1119 allocatorStore->fetchAllocator(outputAllocators->m.values[0], &allocator);
1120 if (allocator) {
1121 pools->outputAllocatorId = allocator->getId();
1122 } else {
1123 ALOGD("[%s] component requested invalid output allocator ID %u",
1124 mName, outputAllocators->m.values[0]);
1125 }
1126 }
1127 }
1128
1129 // use bufferqueue if outputting to a surface.
1130 // query C2PortSurfaceAllocatorTuning::output from component, or use default allocator
1131 // if unsuccessful.
1132 if (outputSurface) {
1133 params.clear();
1134 err = mComponent->query({ },
1135 { C2PortSurfaceAllocatorTuning::output::PARAM_TYPE },
1136 C2_DONT_BLOCK,
1137 &params);
1138 if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
1139 ALOGD("[%s] Query output surface allocator returned %zu params => %s (%u)",
1140 mName, params.size(), asString(err), err);
1141 } else if (err == C2_OK && params.size() == 1) {
1142 C2PortSurfaceAllocatorTuning::output *surfaceAllocator =
1143 C2PortSurfaceAllocatorTuning::output::From(params[0].get());
1144 if (surfaceAllocator) {
1145 std::shared_ptr<C2Allocator> allocator;
1146 // verify allocator IDs and resolve default allocator
1147 allocatorStore->fetchAllocator(surfaceAllocator->value, &allocator);
1148 if (allocator) {
1149 pools->outputAllocatorId = allocator->getId();
1150 } else {
1151 ALOGD("[%s] component requested invalid surface output allocator ID %u",
1152 mName, surfaceAllocator->value);
1153 err = C2_BAD_VALUE;
1154 }
1155 }
1156 }
1157 if (pools->outputAllocatorId == C2PlatformAllocatorStore::GRALLOC
1158 && err != C2_OK
1159 && ((poolMask >> C2PlatformAllocatorStore::BUFFERQUEUE) & 1)) {
1160 pools->outputAllocatorId = C2PlatformAllocatorStore::BUFFERQUEUE;
1161 }
1162 }
1163
1164 if ((poolMask >> pools->outputAllocatorId) & 1) {
1165 err = mComponent->createBlockPool(
1166 pools->outputAllocatorId, &pools->outputPoolId, &pools->outputPoolIntf);
1167 ALOGI("[%s] Created output block pool with allocatorID %u => poolID %llu - %s",
1168 mName, pools->outputAllocatorId,
1169 (unsigned long long)pools->outputPoolId,
1170 asString(err));
1171 } else {
1172 err = C2_NOT_FOUND;
1173 }
1174 if (err != C2_OK) {
1175 // use basic pool instead
1176 pools->outputPoolId =
1177 graphic ? C2BlockPool::BASIC_GRAPHIC : C2BlockPool::BASIC_LINEAR;
1178 }
1179
1180 // Configure output block pool ID as parameter C2PortBlockPoolsTuning::output to
1181 // component.
1182 std::unique_ptr<C2PortBlockPoolsTuning::output> poolIdsTuning =
1183 C2PortBlockPoolsTuning::output::AllocUnique({ pools->outputPoolId });
1184
1185 std::vector<std::unique_ptr<C2SettingResult>> failures;
1186 err = mComponent->config({ poolIdsTuning.get() }, C2_MAY_BLOCK, &failures);
1187 ALOGD("[%s] Configured output block pool ids %llu => %s",
1188 mName, (unsigned long long)poolIdsTuning->m.values[0], asString(err));
1189 outputPoolId_ = pools->outputPoolId;
1190 }
1191
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001192 Mutexed<Output>::Locked output(mOutput);
Wonsik Kimbdffead2019-07-01 12:00:07 -07001193 output->outputDelay = outputDelayValue;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001194 output->numSlots = numOutputSlots;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001195 if (graphic) {
Wonsik Kimfb7a7672019-12-27 17:13:33 -08001196 if (outputSurface || !buffersBoundToCodec) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001197 output->buffers.reset(new GraphicOutputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001198 } else {
Wonsik Kim41d83432020-04-27 16:40:49 -07001199 output->buffers.reset(new RawGraphicOutputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001200 }
1201 } else {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001202 output->buffers.reset(new LinearOutputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001203 }
Wonsik Kime4716c02020-02-28 10:42:21 -08001204 output->buffers->setFormat(outputFormat);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001205
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001206 output->buffers->clearStash();
1207 if (reorderDepth) {
1208 output->buffers->setReorderDepth(reorderDepth.value);
1209 }
1210 if (reorderKey) {
1211 output->buffers->setReorderKey(reorderKey.value);
1212 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001213
1214 // Try to set output surface to created block pool if given.
1215 if (outputSurface) {
1216 mComponent->setOutputSurface(
1217 outputPoolId_,
1218 outputSurface,
1219 outputGeneration);
1220 }
1221
Wonsik Kim8ab25aa2019-06-24 16:37:37 -07001222 if (oStreamFormat.value == C2BufferData::LINEAR) {
Wonsik Kim58713302020-01-29 22:25:23 -08001223 if (buffersBoundToCodec) {
1224 // WORKAROUND: if we're using early CSD workaround we convert to
1225 // array mode, to appease apps assuming the output
1226 // buffers to be of the same size.
1227 output->buffers = output->buffers->toArrayMode(numOutputSlots);
1228 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001229
1230 int32_t channelCount;
1231 int32_t sampleRate;
1232 if (outputFormat->findInt32(KEY_CHANNEL_COUNT, &channelCount)
1233 && outputFormat->findInt32(KEY_SAMPLE_RATE, &sampleRate)) {
1234 int32_t delay = 0;
1235 int32_t padding = 0;;
1236 if (!outputFormat->findInt32("encoder-delay", &delay)) {
1237 delay = 0;
1238 }
1239 if (!outputFormat->findInt32("encoder-padding", &padding)) {
1240 padding = 0;
1241 }
1242 if (delay || padding) {
1243 // We need write access to the buffers, and we're already in
1244 // array mode.
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001245 output->buffers->initSkipCutBuffer(delay, padding, sampleRate, channelCount);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001246 }
1247 }
1248 }
1249 }
1250
1251 // Set up pipeline control. This has to be done after mInputBuffers and
1252 // mOutputBuffers are initialized to make sure that lingering callbacks
1253 // about buffers from the previous generation do not interfere with the
1254 // newly initialized pipeline capacity.
1255
Wonsik Kimab34ed62019-01-31 15:28:46 -08001256 {
1257 Mutexed<PipelineWatcher>::Locked watcher(mPipelineWatcher);
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -08001258 watcher->inputDelay(inputDelayValue)
1259 .pipelineDelay(pipelineDelayValue)
1260 .outputDelay(outputDelayValue)
Wonsik Kimab34ed62019-01-31 15:28:46 -08001261 .smoothnessFactor(kSmoothnessFactor);
1262 watcher->flush();
1263 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001264
1265 mInputMetEos = false;
1266 mSync.start();
1267 return OK;
1268}
1269
1270status_t CCodecBufferChannel::requestInitialInputBuffers() {
1271 if (mInputSurface) {
1272 return OK;
1273 }
1274
Lajos Molnar3bb81cd2019-02-20 15:10:30 -08001275 C2StreamBufferTypeSetting::output oStreamFormat(0u);
Wonsik Kim8ab25aa2019-06-24 16:37:37 -07001276 C2PrependHeaderModeSetting prepend(PREPEND_HEADER_TO_NONE);
1277 c2_status_t err = mComponent->query({ &oStreamFormat, &prepend }, {}, C2_DONT_BLOCK, nullptr);
1278 if (err != C2_OK && err != C2_BAD_INDEX) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001279 return UNKNOWN_ERROR;
1280 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001281 size_t numInputSlots = mInput.lock()->numSlots;
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001282
1283 struct ClientInputBuffer {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001284 size_t index;
1285 sp<MediaCodecBuffer> buffer;
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001286 size_t capacity;
1287 };
1288 std::list<ClientInputBuffer> clientInputBuffers;
1289
1290 {
1291 Mutexed<Input>::Locked input(mInput);
1292 while (clientInputBuffers.size() < numInputSlots) {
1293 ClientInputBuffer clientInputBuffer;
1294 if (!input->buffers->requestNewBuffer(&clientInputBuffer.index,
1295 &clientInputBuffer.buffer)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001296 break;
1297 }
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001298 clientInputBuffer.capacity = clientInputBuffer.buffer->capacity();
1299 clientInputBuffers.emplace_back(std::move(clientInputBuffer));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001300 }
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001301 }
1302 if (clientInputBuffers.empty()) {
1303 ALOGW("[%s] start: cannot allocate memory at all", mName);
1304 return NO_MEMORY;
1305 } else if (clientInputBuffers.size() < numInputSlots) {
1306 ALOGD("[%s] start: cannot allocate memory for all slots, "
1307 "only %zu buffers allocated",
1308 mName, clientInputBuffers.size());
1309 } else {
1310 ALOGV("[%s] %zu initial input buffers available",
1311 mName, clientInputBuffers.size());
1312 }
1313 // Sort input buffers by their capacities in increasing order.
1314 clientInputBuffers.sort(
1315 [](const ClientInputBuffer& a, const ClientInputBuffer& b) {
1316 return a.capacity < b.capacity;
1317 });
1318
1319 {
1320 Mutexed<std::list<sp<ABuffer>>>::Locked configs(mFlushedConfigs);
1321 if (!configs->empty()) {
1322 while (!configs->empty()) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001323 sp<ABuffer> config = configs->front();
Pawin Vongmasa472c7382019-03-26 18:13:58 -07001324 configs->pop_front();
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001325 // Find the smallest input buffer that can fit the config.
1326 auto i = std::find_if(
1327 clientInputBuffers.begin(),
1328 clientInputBuffers.end(),
1329 [cfgSize = config->size()](const ClientInputBuffer& b) {
1330 return b.capacity >= cfgSize;
1331 });
1332 if (i == clientInputBuffers.end()) {
1333 ALOGW("[%s] no input buffer large enough for the config "
1334 "(%zu bytes)",
1335 mName, config->size());
1336 return NO_MEMORY;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001337 }
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001338 sp<MediaCodecBuffer> buffer = i->buffer;
1339 memcpy(buffer->base(), config->data(), config->size());
1340 buffer->setRange(0, config->size());
Pawin Vongmasa36653902018-11-15 00:10:25 -08001341 buffer->meta()->clear();
1342 buffer->meta()->setInt64("timeUs", 0);
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001343 buffer->meta()->setInt32("csd", 1);
1344 if (queueInputBufferInternal(buffer) != OK) {
1345 ALOGW("[%s] Error while queueing a flushed config",
1346 mName);
1347 return UNKNOWN_ERROR;
1348 }
1349 clientInputBuffers.erase(i);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001350 }
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001351 } else if (oStreamFormat.value == C2BufferData::LINEAR &&
1352 (!prepend || prepend.value == PREPEND_HEADER_TO_NONE)) {
1353 sp<MediaCodecBuffer> buffer = clientInputBuffers.front().buffer;
1354 // WORKAROUND: Some apps expect CSD available without queueing
1355 // any input. Queue an empty buffer to get the CSD.
1356 buffer->setRange(0, 0);
1357 buffer->meta()->clear();
1358 buffer->meta()->setInt64("timeUs", 0);
1359 if (queueInputBufferInternal(buffer) != OK) {
1360 ALOGW("[%s] Error while queueing an empty buffer to get CSD",
1361 mName);
1362 return UNKNOWN_ERROR;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001363 }
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001364 clientInputBuffers.pop_front();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001365 }
1366 }
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001367
1368 for (const ClientInputBuffer& clientInputBuffer: clientInputBuffers) {
1369 mCallback->onInputBufferAvailable(
1370 clientInputBuffer.index,
1371 clientInputBuffer.buffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001372 }
Pawin Vongmasae7bb8612020-06-04 06:15:22 -07001373
Pawin Vongmasa36653902018-11-15 00:10:25 -08001374 return OK;
1375}
1376
1377void CCodecBufferChannel::stop() {
1378 mSync.stop();
1379 mFirstValidFrameIndex = mFrameIndex.load(std::memory_order_relaxed);
1380 if (mInputSurface != nullptr) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001381 mInputSurface.reset();
1382 }
Wonsik Kima2e3cdd2020-05-20 15:14:42 -07001383 mPipelineWatcher.lock()->flush();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001384}
1385
Wonsik Kim936a89c2020-05-08 16:07:50 -07001386void CCodecBufferChannel::reset() {
1387 stop();
1388 {
1389 Mutexed<Input>::Locked input(mInput);
1390 input->buffers.reset(new DummyInputBuffers(""));
Wonsik Kima2e3cdd2020-05-20 15:14:42 -07001391 input->extraBuffers.flush();
Wonsik Kim936a89c2020-05-08 16:07:50 -07001392 }
1393 {
1394 Mutexed<Output>::Locked output(mOutput);
1395 output->buffers.reset();
1396 }
1397}
1398
1399void CCodecBufferChannel::release() {
1400 mComponent.reset();
1401 mInputAllocator.reset();
1402 mOutputSurface.lock()->surface.clear();
1403 {
1404 Mutexed<BlockPools>::Locked blockPools{mBlockPools};
1405 blockPools->inputPool.reset();
1406 blockPools->outputPoolIntf.reset();
1407 }
Wonsik Kima2e3cdd2020-05-20 15:14:42 -07001408 setCrypto(nullptr);
1409 setDescrambler(nullptr);
Wonsik Kim936a89c2020-05-08 16:07:50 -07001410}
1411
1412
Pawin Vongmasa36653902018-11-15 00:10:25 -08001413void CCodecBufferChannel::flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) {
1414 ALOGV("[%s] flush", mName);
1415 {
1416 Mutexed<std::list<sp<ABuffer>>>::Locked configs(mFlushedConfigs);
1417 for (const std::unique_ptr<C2Work> &work : flushedWork) {
1418 if (!(work->input.flags & C2FrameData::FLAG_CODEC_CONFIG)) {
1419 continue;
1420 }
1421 if (work->input.buffers.empty()
Chih-Yu Huang7427d372020-12-02 16:16:57 +09001422 || work->input.buffers.front() == nullptr
Pawin Vongmasa36653902018-11-15 00:10:25 -08001423 || work->input.buffers.front()->data().linearBlocks().empty()) {
1424 ALOGD("[%s] no linear codec config data found", mName);
1425 continue;
1426 }
1427 C2ReadView view =
1428 work->input.buffers.front()->data().linearBlocks().front().map().get();
1429 if (view.error() != C2_OK) {
1430 ALOGD("[%s] failed to map flushed codec config data: %d", mName, view.error());
1431 continue;
1432 }
1433 configs->push_back(ABuffer::CreateAsCopy(view.data(), view.capacity()));
1434 ALOGV("[%s] stashed flushed codec config data (size=%u)", mName, view.capacity());
1435 }
1436 }
1437 {
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