blob: 49c04a0a52e36f3b88c574cee3c03bea85ffb45a [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
21#include <numeric>
22
23#include <C2AllocatorGralloc.h>
24#include <C2PlatformSupport.h>
25#include <C2BlockInternal.h>
26#include <C2Config.h>
27#include <C2Debug.h>
28
29#include <android/hardware/cas/native/1.0/IDescrambler.h>
Robert Shih895fba92019-07-16 16:29:44 -070030#include <android/hardware/drm/1.0/types.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080031#include <android-base/stringprintf.h>
Wonsik Kimfb7a7672019-12-27 17:13:33 -080032#include <binder/MemoryBase.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080033#include <binder/MemoryDealer.h>
Ray Essick18ea0452019-08-27 16:07:27 -070034#include <cutils/properties.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080035#include <gui/Surface.h>
Robert Shih895fba92019-07-16 16:29:44 -070036#include <hidlmemory/FrameworkUtils.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080037#include <media/openmax/OMX_Core.h>
38#include <media/stagefright/foundation/ABuffer.h>
39#include <media/stagefright/foundation/ALookup.h>
40#include <media/stagefright/foundation/AMessage.h>
41#include <media/stagefright/foundation/AUtils.h>
42#include <media/stagefright/foundation/hexdump.h>
43#include <media/stagefright/MediaCodec.h>
44#include <media/stagefright/MediaCodecConstants.h>
Wonsik Kim155d5cb2019-10-09 12:49:49 -070045#include <media/stagefright/SkipCutBuffer.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080046#include <media/MediaCodecBuffer.h>
Wonsik Kim41d83432020-04-27 16:40:49 -070047#include <mediadrm/ICrypto.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080048#include <system/window.h>
49
50#include "CCodecBufferChannel.h"
51#include "Codec2Buffer.h"
Pawin Vongmasa36653902018-11-15 00:10:25 -080052
53namespace android {
54
55using android::base::StringPrintf;
56using hardware::hidl_handle;
57using hardware::hidl_string;
58using hardware::hidl_vec;
Robert Shih895fba92019-07-16 16:29:44 -070059using hardware::fromHeap;
60using hardware::HidlMemory;
61
Pawin Vongmasa36653902018-11-15 00:10:25 -080062using namespace hardware::cas::V1_0;
63using namespace hardware::cas::native::V1_0;
64
65using CasStatus = hardware::cas::V1_0::Status;
Robert Shih895fba92019-07-16 16:29:44 -070066using DrmBufferType = hardware::drm::V1_0::BufferType;
Pawin Vongmasa36653902018-11-15 00:10:25 -080067
Pawin Vongmasa36653902018-11-15 00:10:25 -080068namespace {
69
Wonsik Kim469c8342019-04-11 16:46:09 -070070constexpr size_t kSmoothnessFactor = 4;
71constexpr size_t kRenderingDepth = 3;
Pawin Vongmasa36653902018-11-15 00:10:25 -080072
Sungtak Leeab6f2f32019-02-15 14:43:51 -080073// This is for keeping IGBP's buffer dropping logic in legacy mode other
74// than making it non-blocking. Do not change this value.
75const static size_t kDequeueTimeoutNs = 0;
76
Pawin Vongmasa36653902018-11-15 00:10:25 -080077} // namespace
78
79CCodecBufferChannel::QueueGuard::QueueGuard(
80 CCodecBufferChannel::QueueSync &sync) : mSync(sync) {
81 Mutex::Autolock l(mSync.mGuardLock);
82 // At this point it's guaranteed that mSync is not under state transition,
83 // as we are holding its mutex.
84
85 Mutexed<CCodecBufferChannel::QueueSync::Counter>::Locked count(mSync.mCount);
86 if (count->value == -1) {
87 mRunning = false;
88 } else {
89 ++count->value;
90 mRunning = true;
91 }
92}
93
94CCodecBufferChannel::QueueGuard::~QueueGuard() {
95 if (mRunning) {
96 // We are not holding mGuardLock at this point so that QueueSync::stop() can
97 // keep holding the lock until mCount reaches zero.
98 Mutexed<CCodecBufferChannel::QueueSync::Counter>::Locked count(mSync.mCount);
99 --count->value;
100 count->cond.broadcast();
101 }
102}
103
104void CCodecBufferChannel::QueueSync::start() {
105 Mutex::Autolock l(mGuardLock);
106 // If stopped, it goes to running state; otherwise no-op.
107 Mutexed<Counter>::Locked count(mCount);
108 if (count->value == -1) {
109 count->value = 0;
110 }
111}
112
113void CCodecBufferChannel::QueueSync::stop() {
114 Mutex::Autolock l(mGuardLock);
115 Mutexed<Counter>::Locked count(mCount);
116 if (count->value == -1) {
117 // no-op
118 return;
119 }
120 // Holding mGuardLock here blocks creation of additional QueueGuard objects, so
121 // mCount can only decrement. In other words, threads that acquired the lock
122 // are allowed to finish execution but additional threads trying to acquire
123 // the lock at this point will block, and then get QueueGuard at STOPPED
124 // state.
125 while (count->value != 0) {
126 count.waitForCondition(count->cond);
127 }
128 count->value = -1;
129}
130
Wonsik Kima4e049d2020-04-28 19:42:23 +0000131// CCodecBufferChannel::ReorderStash
132
133CCodecBufferChannel::ReorderStash::ReorderStash() {
134 clear();
135}
136
137void CCodecBufferChannel::ReorderStash::clear() {
138 mPending.clear();
139 mStash.clear();
140 mDepth = 0;
141 mKey = C2Config::ORDINAL;
142}
143
144void CCodecBufferChannel::ReorderStash::flush() {
145 mPending.clear();
146 mStash.clear();
147}
148
149void CCodecBufferChannel::ReorderStash::setDepth(uint32_t depth) {
150 mPending.splice(mPending.end(), mStash);
151 mDepth = depth;
152}
153
154void CCodecBufferChannel::ReorderStash::setKey(C2Config::ordinal_key_t key) {
155 mPending.splice(mPending.end(), mStash);
156 mKey = key;
157}
158
159bool CCodecBufferChannel::ReorderStash::pop(Entry *entry) {
160 if (mPending.empty()) {
161 return false;
162 }
163 entry->buffer = mPending.front().buffer;
164 entry->timestamp = mPending.front().timestamp;
165 entry->flags = mPending.front().flags;
166 entry->ordinal = mPending.front().ordinal;
167 mPending.pop_front();
168 return true;
169}
170
171void CCodecBufferChannel::ReorderStash::emplace(
172 const std::shared_ptr<C2Buffer> &buffer,
173 int64_t timestamp,
174 int32_t flags,
175 const C2WorkOrdinalStruct &ordinal) {
176 bool eos = flags & MediaCodec::BUFFER_FLAG_EOS;
177 if (!buffer && eos) {
178 // TRICKY: we may be violating ordering of the stash here. Because we
179 // don't expect any more emplace() calls after this, the ordering should
180 // not matter.
181 mStash.emplace_back(buffer, timestamp, flags, ordinal);
182 } else {
183 flags = flags & ~MediaCodec::BUFFER_FLAG_EOS;
184 auto it = mStash.begin();
185 for (; it != mStash.end(); ++it) {
186 if (less(ordinal, it->ordinal)) {
187 break;
188 }
189 }
190 mStash.emplace(it, buffer, timestamp, flags, ordinal);
191 if (eos) {
192 mStash.back().flags = mStash.back().flags | MediaCodec::BUFFER_FLAG_EOS;
193 }
194 }
195 while (!mStash.empty() && mStash.size() > mDepth) {
196 mPending.push_back(mStash.front());
197 mStash.pop_front();
198 }
199}
200
201void CCodecBufferChannel::ReorderStash::defer(
202 const CCodecBufferChannel::ReorderStash::Entry &entry) {
203 mPending.push_front(entry);
204}
205
206bool CCodecBufferChannel::ReorderStash::hasPending() const {
207 return !mPending.empty();
208}
209
210bool CCodecBufferChannel::ReorderStash::less(
211 const C2WorkOrdinalStruct &o1, const C2WorkOrdinalStruct &o2) {
212 switch (mKey) {
213 case C2Config::ORDINAL: return o1.frameIndex < o2.frameIndex;
214 case C2Config::TIMESTAMP: return o1.timestamp < o2.timestamp;
215 case C2Config::CUSTOM: return o1.customOrdinal < o2.customOrdinal;
216 default:
217 ALOGD("Unrecognized key; default to timestamp");
218 return o1.frameIndex < o2.frameIndex;
219 }
220}
221
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700222// Input
223
224CCodecBufferChannel::Input::Input() : extraBuffers("extra") {}
225
Pawin Vongmasa36653902018-11-15 00:10:25 -0800226// CCodecBufferChannel
227
228CCodecBufferChannel::CCodecBufferChannel(
229 const std::shared_ptr<CCodecCallback> &callback)
230 : mHeapSeqNum(-1),
231 mCCodecCallback(callback),
232 mFrameIndex(0u),
233 mFirstValidFrameIndex(0u),
234 mMetaMode(MODE_NONE),
Pawin Vongmasa36653902018-11-15 00:10:25 -0800235 mInputMetEos(false) {
Sungtak Leed7463d12019-09-04 16:01:00 -0700236 mOutputSurface.lock()->maxDequeueBuffers = kSmoothnessFactor + kRenderingDepth;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700237 {
238 Mutexed<Input>::Locked input(mInput);
239 input->buffers.reset(new DummyInputBuffers(""));
240 input->extraBuffers.flush();
241 input->inputDelay = 0u;
242 input->pipelineDelay = 0u;
243 input->numSlots = kSmoothnessFactor;
244 input->numExtraSlots = 0u;
245 }
246 {
247 Mutexed<Output>::Locked output(mOutput);
248 output->outputDelay = 0u;
249 output->numSlots = kSmoothnessFactor;
250 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800251}
252
253CCodecBufferChannel::~CCodecBufferChannel() {
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800254 if (mCrypto != nullptr && mHeapSeqNum >= 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800255 mCrypto->unsetHeap(mHeapSeqNum);
256 }
257}
258
259void CCodecBufferChannel::setComponent(
260 const std::shared_ptr<Codec2Client::Component> &component) {
261 mComponent = component;
262 mComponentName = component->getName() + StringPrintf("#%d", int(uintptr_t(component.get()) % 997));
263 mName = mComponentName.c_str();
264}
265
266status_t CCodecBufferChannel::setInputSurface(
267 const std::shared_ptr<InputSurfaceWrapper> &surface) {
268 ALOGV("[%s] setInputSurface", mName);
269 mInputSurface = surface;
270 return mInputSurface->connect(mComponent);
271}
272
273status_t CCodecBufferChannel::signalEndOfInputStream() {
274 if (mInputSurface == nullptr) {
275 return INVALID_OPERATION;
276 }
277 return mInputSurface->signalEndOfInputStream();
278}
279
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700280status_t CCodecBufferChannel::queueInputBufferInternal(sp<MediaCodecBuffer> buffer) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800281 int64_t timeUs;
282 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
283
284 if (mInputMetEos) {
285 ALOGD("[%s] buffers after EOS ignored (%lld us)", mName, (long long)timeUs);
286 return OK;
287 }
288
289 int32_t flags = 0;
290 int32_t tmp = 0;
291 bool eos = false;
292 if (buffer->meta()->findInt32("eos", &tmp) && tmp) {
293 eos = true;
294 mInputMetEos = true;
295 ALOGV("[%s] input EOS", mName);
296 }
297 if (buffer->meta()->findInt32("csd", &tmp) && tmp) {
298 flags |= C2FrameData::FLAG_CODEC_CONFIG;
299 }
300 ALOGV("[%s] queueInputBuffer: buffer->size() = %zu", mName, buffer->size());
301 std::unique_ptr<C2Work> work(new C2Work);
302 work->input.ordinal.timestamp = timeUs;
303 work->input.ordinal.frameIndex = mFrameIndex++;
304 // WORKAROUND: until codecs support handling work after EOS and max output sizing, use timestamp
305 // manipulation to achieve image encoding via video codec, and to constrain encoded output.
306 // Keep client timestamp in customOrdinal
307 work->input.ordinal.customOrdinal = timeUs;
308 work->input.buffers.clear();
309
Wonsik Kimab34ed62019-01-31 15:28:46 -0800310 uint64_t queuedFrameIndex = work->input.ordinal.frameIndex.peeku();
311 std::vector<std::shared_ptr<C2Buffer>> queuedBuffers;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700312 sp<Codec2Buffer> copy;
Wonsik Kimab34ed62019-01-31 15:28:46 -0800313
Pawin Vongmasa36653902018-11-15 00:10:25 -0800314 if (buffer->size() > 0u) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700315 Mutexed<Input>::Locked input(mInput);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800316 std::shared_ptr<C2Buffer> c2buffer;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700317 if (!input->buffers->releaseBuffer(buffer, &c2buffer, false)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800318 return -ENOENT;
319 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700320 // TODO: we want to delay copying buffers.
321 if (input->extraBuffers.numComponentBuffers() < input->numExtraSlots) {
322 copy = input->buffers->cloneAndReleaseBuffer(buffer);
323 if (copy != nullptr) {
324 (void)input->extraBuffers.assignSlot(copy);
325 if (!input->extraBuffers.releaseSlot(copy, &c2buffer, false)) {
326 return UNKNOWN_ERROR;
327 }
328 bool released = input->buffers->releaseBuffer(buffer, nullptr, true);
329 ALOGV("[%s] queueInputBuffer: buffer copied; %sreleased",
330 mName, released ? "" : "not ");
331 buffer.clear();
332 } else {
333 ALOGW("[%s] queueInputBuffer: failed to copy a buffer; this may cause input "
334 "buffer starvation on component.", mName);
335 }
336 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800337 work->input.buffers.push_back(c2buffer);
Wonsik Kimab34ed62019-01-31 15:28:46 -0800338 queuedBuffers.push_back(c2buffer);
339 } else if (eos) {
340 flags |= C2FrameData::FLAG_END_OF_STREAM;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800341 }
342 work->input.flags = (C2FrameData::flags_t)flags;
343 // TODO: fill info's
344
345 work->input.configUpdate = std::move(mParamsToBeSet);
346 work->worklets.clear();
347 work->worklets.emplace_back(new C2Worklet);
348
349 std::list<std::unique_ptr<C2Work>> items;
350 items.push_back(std::move(work));
Wonsik Kimab34ed62019-01-31 15:28:46 -0800351 mPipelineWatcher.lock()->onWorkQueued(
352 queuedFrameIndex,
353 std::move(queuedBuffers),
354 PipelineWatcher::Clock::now());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800355 c2_status_t err = mComponent->queue(&items);
Wonsik Kimab34ed62019-01-31 15:28:46 -0800356 if (err != C2_OK) {
357 mPipelineWatcher.lock()->onWorkDone(queuedFrameIndex);
358 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800359
360 if (err == C2_OK && eos && buffer->size() > 0u) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800361 work.reset(new C2Work);
362 work->input.ordinal.timestamp = timeUs;
363 work->input.ordinal.frameIndex = mFrameIndex++;
364 // WORKAROUND: keep client timestamp in customOrdinal
365 work->input.ordinal.customOrdinal = timeUs;
366 work->input.buffers.clear();
367 work->input.flags = C2FrameData::FLAG_END_OF_STREAM;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800368 work->worklets.emplace_back(new C2Worklet);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800369
Wonsik Kimab34ed62019-01-31 15:28:46 -0800370 queuedFrameIndex = work->input.ordinal.frameIndex.peeku();
371 queuedBuffers.clear();
372
Pawin Vongmasa36653902018-11-15 00:10:25 -0800373 items.clear();
374 items.push_back(std::move(work));
Wonsik Kimab34ed62019-01-31 15:28:46 -0800375
376 mPipelineWatcher.lock()->onWorkQueued(
377 queuedFrameIndex,
378 std::move(queuedBuffers),
379 PipelineWatcher::Clock::now());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800380 err = mComponent->queue(&items);
Wonsik Kimab34ed62019-01-31 15:28:46 -0800381 if (err != C2_OK) {
382 mPipelineWatcher.lock()->onWorkDone(queuedFrameIndex);
383 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800384 }
385 if (err == C2_OK) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700386 Mutexed<Input>::Locked input(mInput);
387 bool released = false;
388 if (buffer) {
389 released = input->buffers->releaseBuffer(buffer, nullptr, true);
390 } else if (copy) {
391 released = input->extraBuffers.releaseSlot(copy, nullptr, true);
392 }
393 ALOGV("[%s] queueInputBuffer: buffer%s %sreleased",
394 mName, (buffer == nullptr) ? "(copy)" : "", released ? "" : "not ");
Pawin Vongmasa36653902018-11-15 00:10:25 -0800395 }
396
397 feedInputBufferIfAvailableInternal();
398 return err;
399}
400
401status_t CCodecBufferChannel::setParameters(std::vector<std::unique_ptr<C2Param>> &params) {
402 QueueGuard guard(mSync);
403 if (!guard.isRunning()) {
404 ALOGD("[%s] setParameters is only supported in the running state.", mName);
405 return -ENOSYS;
406 }
407 mParamsToBeSet.insert(mParamsToBeSet.end(),
408 std::make_move_iterator(params.begin()),
409 std::make_move_iterator(params.end()));
410 params.clear();
411 return OK;
412}
413
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800414status_t CCodecBufferChannel::attachBuffer(
415 const std::shared_ptr<C2Buffer> &c2Buffer,
416 const sp<MediaCodecBuffer> &buffer) {
417 if (!buffer->copy(c2Buffer)) {
418 return -ENOSYS;
419 }
420 return OK;
421}
422
423void CCodecBufferChannel::ensureDecryptDestination(size_t size) {
424 if (!mDecryptDestination || mDecryptDestination->size() < size) {
425 sp<IMemoryHeap> heap{new MemoryHeapBase(size * 2)};
426 if (mDecryptDestination && mCrypto && mHeapSeqNum >= 0) {
427 mCrypto->unsetHeap(mHeapSeqNum);
428 }
429 mDecryptDestination = new MemoryBase(heap, 0, size * 2);
430 if (mCrypto) {
431 mHeapSeqNum = mCrypto->setHeap(hardware::fromHeap(heap));
432 }
433 }
434}
435
436int32_t CCodecBufferChannel::getHeapSeqNum(const sp<HidlMemory> &memory) {
437 CHECK(mCrypto);
438 auto it = mHeapSeqNumMap.find(memory);
439 int32_t heapSeqNum = -1;
440 if (it == mHeapSeqNumMap.end()) {
441 heapSeqNum = mCrypto->setHeap(memory);
442 mHeapSeqNumMap.emplace(memory, heapSeqNum);
443 } else {
444 heapSeqNum = it->second;
445 }
446 return heapSeqNum;
447}
448
449status_t CCodecBufferChannel::attachEncryptedBuffer(
450 const sp<hardware::HidlMemory> &memory,
451 bool secure,
452 const uint8_t *key,
453 const uint8_t *iv,
454 CryptoPlugin::Mode mode,
455 CryptoPlugin::Pattern pattern,
456 size_t offset,
457 const CryptoPlugin::SubSample *subSamples,
458 size_t numSubSamples,
459 const sp<MediaCodecBuffer> &buffer) {
460 static const C2MemoryUsage kSecureUsage{C2MemoryUsage::READ_PROTECTED, 0};
461 static const C2MemoryUsage kDefaultReadWriteUsage{
462 C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE};
463
464 size_t size = 0;
465 for (size_t i = 0; i < numSubSamples; ++i) {
466 size += subSamples[i].mNumBytesOfClearData + subSamples[i].mNumBytesOfEncryptedData;
467 }
468 std::shared_ptr<C2BlockPool> pool = mBlockPools.lock()->inputPool;
469 std::shared_ptr<C2LinearBlock> block;
470 c2_status_t err = pool->fetchLinearBlock(
471 size,
472 secure ? kSecureUsage : kDefaultReadWriteUsage,
473 &block);
474 if (err != C2_OK) {
475 return NO_MEMORY;
476 }
477 if (!secure) {
478 ensureDecryptDestination(size);
479 }
480 ssize_t result = -1;
481 ssize_t codecDataOffset = 0;
482 if (mCrypto) {
483 AString errorDetailMsg;
484 int32_t heapSeqNum = getHeapSeqNum(memory);
485 hardware::drm::V1_0::SharedBuffer src{(uint32_t)heapSeqNum, offset, size};
486 hardware::drm::V1_0::DestinationBuffer dst;
487 if (secure) {
488 dst.type = DrmBufferType::NATIVE_HANDLE;
489 dst.secureMemory = hardware::hidl_handle(block->handle());
490 } else {
491 dst.type = DrmBufferType::SHARED_MEMORY;
492 IMemoryToSharedBuffer(
493 mDecryptDestination, mHeapSeqNum, &dst.nonsecureMemory);
494 }
495 result = mCrypto->decrypt(
496 key, iv, mode, pattern, src, 0, subSamples, numSubSamples,
497 dst, &errorDetailMsg);
498 if (result < 0) {
499 return result;
500 }
501 if (dst.type == DrmBufferType::SHARED_MEMORY) {
502 C2WriteView view = block->map().get();
503 if (view.error() != C2_OK) {
504 return false;
505 }
506 if (view.size() < result) {
507 return false;
508 }
509 memcpy(view.data(), mDecryptDestination->unsecurePointer(), result);
510 }
511 } else {
512 // Here we cast CryptoPlugin::SubSample to hardware::cas::native::V1_0::SubSample
513 // directly, the structure definitions should match as checked in DescramblerImpl.cpp.
514 hidl_vec<SubSample> hidlSubSamples;
515 hidlSubSamples.setToExternal((SubSample *)subSamples, numSubSamples, false /*own*/);
516
517 hardware::cas::native::V1_0::SharedBuffer src{*memory, offset, size};
518 hardware::cas::native::V1_0::DestinationBuffer dst;
519 if (secure) {
520 dst.type = BufferType::NATIVE_HANDLE;
521 dst.secureMemory = hardware::hidl_handle(block->handle());
522 } else {
523 dst.type = BufferType::SHARED_MEMORY;
524 dst.nonsecureMemory = src;
525 }
526
527 CasStatus status = CasStatus::OK;
528 hidl_string detailedError;
529 ScramblingControl sctrl = ScramblingControl::UNSCRAMBLED;
530
531 if (key != nullptr) {
532 sctrl = (ScramblingControl)key[0];
533 // Adjust for the PES offset
534 codecDataOffset = key[2] | (key[3] << 8);
535 }
536
537 auto returnVoid = mDescrambler->descramble(
538 sctrl,
539 hidlSubSamples,
540 src,
541 0,
542 dst,
543 0,
544 [&status, &result, &detailedError] (
545 CasStatus _status, uint32_t _bytesWritten,
546 const hidl_string& _detailedError) {
547 status = _status;
548 result = (ssize_t)_bytesWritten;
549 detailedError = _detailedError;
550 });
551
552 if (!returnVoid.isOk() || status != CasStatus::OK || result < 0) {
553 ALOGI("[%s] descramble failed, trans=%s, status=%d, result=%zd",
554 mName, returnVoid.description().c_str(), status, result);
555 return UNKNOWN_ERROR;
556 }
557
558 if (result < codecDataOffset) {
559 ALOGD("invalid codec data offset: %zd, result %zd", codecDataOffset, result);
560 return BAD_VALUE;
561 }
562 }
563 if (!secure) {
564 C2WriteView view = block->map().get();
565 if (view.error() != C2_OK) {
566 return UNKNOWN_ERROR;
567 }
568 if (view.size() < result) {
569 return UNKNOWN_ERROR;
570 }
571 memcpy(view.data(), mDecryptDestination->unsecurePointer(), result);
572 }
573 std::shared_ptr<C2Buffer> c2Buffer{C2Buffer::CreateLinearBuffer(
574 block->share(codecDataOffset, result - codecDataOffset, C2Fence{}))};
575 if (!buffer->copy(c2Buffer)) {
576 return -ENOSYS;
577 }
578 return OK;
579}
580
Pawin Vongmasa36653902018-11-15 00:10:25 -0800581status_t CCodecBufferChannel::queueInputBuffer(const sp<MediaCodecBuffer> &buffer) {
582 QueueGuard guard(mSync);
583 if (!guard.isRunning()) {
584 ALOGD("[%s] No more buffers should be queued at current state.", mName);
585 return -ENOSYS;
586 }
587 return queueInputBufferInternal(buffer);
588}
589
590status_t CCodecBufferChannel::queueSecureInputBuffer(
591 const sp<MediaCodecBuffer> &buffer, bool secure, const uint8_t *key,
592 const uint8_t *iv, CryptoPlugin::Mode mode, CryptoPlugin::Pattern pattern,
593 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
594 AString *errorDetailMsg) {
595 QueueGuard guard(mSync);
596 if (!guard.isRunning()) {
597 ALOGD("[%s] No more buffers should be queued at current state.", mName);
598 return -ENOSYS;
599 }
600
601 if (!hasCryptoOrDescrambler()) {
602 return -ENOSYS;
603 }
604 sp<EncryptedLinearBlockBuffer> encryptedBuffer((EncryptedLinearBlockBuffer *)buffer.get());
605
606 ssize_t result = -1;
607 ssize_t codecDataOffset = 0;
Wonsik Kim557c88c2020-03-13 11:03:52 -0700608 if (numSubSamples == 1
609 && subSamples[0].mNumBytesOfClearData == 0
610 && subSamples[0].mNumBytesOfEncryptedData == 0) {
611 // We don't need to go through crypto or descrambler if the input is empty.
612 result = 0;
613 } else if (mCrypto != nullptr) {
Robert Shih895fba92019-07-16 16:29:44 -0700614 hardware::drm::V1_0::DestinationBuffer destination;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800615 if (secure) {
Robert Shih895fba92019-07-16 16:29:44 -0700616 destination.type = DrmBufferType::NATIVE_HANDLE;
617 destination.secureMemory = hidl_handle(encryptedBuffer->handle());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800618 } else {
Robert Shih895fba92019-07-16 16:29:44 -0700619 destination.type = DrmBufferType::SHARED_MEMORY;
620 IMemoryToSharedBuffer(
621 mDecryptDestination, mHeapSeqNum, &destination.nonsecureMemory);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800622 }
Robert Shih895fba92019-07-16 16:29:44 -0700623 hardware::drm::V1_0::SharedBuffer source;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800624 encryptedBuffer->fillSourceBuffer(&source);
625 result = mCrypto->decrypt(
626 key, iv, mode, pattern, source, buffer->offset(),
627 subSamples, numSubSamples, destination, errorDetailMsg);
628 if (result < 0) {
Wonsik Kim557c88c2020-03-13 11:03:52 -0700629 ALOGI("[%s] decrypt failed: result=%zd", mName, result);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800630 return result;
631 }
Robert Shih895fba92019-07-16 16:29:44 -0700632 if (destination.type == DrmBufferType::SHARED_MEMORY) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800633 encryptedBuffer->copyDecryptedContent(mDecryptDestination, result);
634 }
635 } else {
636 // Here we cast CryptoPlugin::SubSample to hardware::cas::native::V1_0::SubSample
637 // directly, the structure definitions should match as checked in DescramblerImpl.cpp.
638 hidl_vec<SubSample> hidlSubSamples;
639 hidlSubSamples.setToExternal((SubSample *)subSamples, numSubSamples, false /*own*/);
640
641 hardware::cas::native::V1_0::SharedBuffer srcBuffer;
642 encryptedBuffer->fillSourceBuffer(&srcBuffer);
643
644 DestinationBuffer dstBuffer;
645 if (secure) {
646 dstBuffer.type = BufferType::NATIVE_HANDLE;
647 dstBuffer.secureMemory = hidl_handle(encryptedBuffer->handle());
648 } else {
649 dstBuffer.type = BufferType::SHARED_MEMORY;
650 dstBuffer.nonsecureMemory = srcBuffer;
651 }
652
653 CasStatus status = CasStatus::OK;
654 hidl_string detailedError;
655 ScramblingControl sctrl = ScramblingControl::UNSCRAMBLED;
656
657 if (key != nullptr) {
658 sctrl = (ScramblingControl)key[0];
659 // Adjust for the PES offset
660 codecDataOffset = key[2] | (key[3] << 8);
661 }
662
663 auto returnVoid = mDescrambler->descramble(
664 sctrl,
665 hidlSubSamples,
666 srcBuffer,
667 0,
668 dstBuffer,
669 0,
670 [&status, &result, &detailedError] (
671 CasStatus _status, uint32_t _bytesWritten,
672 const hidl_string& _detailedError) {
673 status = _status;
674 result = (ssize_t)_bytesWritten;
675 detailedError = _detailedError;
676 });
677
678 if (!returnVoid.isOk() || status != CasStatus::OK || result < 0) {
679 ALOGI("[%s] descramble failed, trans=%s, status=%d, result=%zd",
680 mName, returnVoid.description().c_str(), status, result);
681 return UNKNOWN_ERROR;
682 }
683
684 if (result < codecDataOffset) {
685 ALOGD("invalid codec data offset: %zd, result %zd", codecDataOffset, result);
686 return BAD_VALUE;
687 }
688
689 ALOGV("[%s] descramble succeeded, %zd bytes", mName, result);
690
691 if (dstBuffer.type == BufferType::SHARED_MEMORY) {
692 encryptedBuffer->copyDecryptedContentFromMemory(result);
693 }
694 }
695
696 buffer->setRange(codecDataOffset, result - codecDataOffset);
697 return queueInputBufferInternal(buffer);
698}
699
700void CCodecBufferChannel::feedInputBufferIfAvailable() {
701 QueueGuard guard(mSync);
702 if (!guard.isRunning()) {
703 ALOGV("[%s] We're not running --- no input buffer reported", mName);
704 return;
705 }
706 feedInputBufferIfAvailableInternal();
707}
708
709void CCodecBufferChannel::feedInputBufferIfAvailableInternal() {
Wonsik Kimdf5dd142019-02-06 10:15:46 -0800710 if (mInputMetEos ||
Wonsik Kima4e049d2020-04-28 19:42:23 +0000711 mReorderStash.lock()->hasPending() ||
Wonsik Kimdf5dd142019-02-06 10:15:46 -0800712 mPipelineWatcher.lock()->pipelineFull()) {
713 return;
714 } else {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700715 Mutexed<Output>::Locked output(mOutput);
Wonsik Kim936a89c2020-05-08 16:07:50 -0700716 if (!output->buffers || output->buffers->numClientBuffers() >= output->numSlots) {
Wonsik Kimdf5dd142019-02-06 10:15:46 -0800717 return;
718 }
719 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700720 size_t numInputSlots = mInput.lock()->numSlots;
721 for (size_t i = 0; i < numInputSlots; ++i) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800722 sp<MediaCodecBuffer> inBuffer;
723 size_t index;
724 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700725 Mutexed<Input>::Locked input(mInput);
726 if (input->buffers->numClientBuffers() >= input->numSlots) {
Wonsik Kimab34ed62019-01-31 15:28:46 -0800727 return;
728 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700729 if (!input->buffers->requestNewBuffer(&index, &inBuffer)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800730 ALOGV("[%s] no new buffer available", mName);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800731 break;
732 }
733 }
734 ALOGV("[%s] new input index = %zu [%p]", mName, index, inBuffer.get());
735 mCallback->onInputBufferAvailable(index, inBuffer);
736 }
737}
738
739status_t CCodecBufferChannel::renderOutputBuffer(
740 const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) {
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800741 ALOGV("[%s] renderOutputBuffer: %p", mName, buffer.get());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800742 std::shared_ptr<C2Buffer> c2Buffer;
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800743 bool released = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800744 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700745 Mutexed<Output>::Locked output(mOutput);
746 if (output->buffers) {
747 released = output->buffers->releaseBuffer(buffer, &c2Buffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800748 }
749 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800750 // NOTE: some apps try to releaseOutputBuffer() with timestamp and/or render
751 // set to true.
752 sendOutputBuffers();
753 // input buffer feeding may have been gated by pending output buffers
754 feedInputBufferIfAvailable();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800755 if (!c2Buffer) {
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800756 if (released) {
Wonsik Kimf7529dd2019-04-18 17:35:53 -0700757 std::call_once(mRenderWarningFlag, [this] {
758 ALOGW("[%s] The app is calling releaseOutputBuffer() with "
759 "timestamp or render=true with non-video buffers. Apps should "
760 "call releaseOutputBuffer() with render=false for those.",
761 mName);
762 });
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800763 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800764 return INVALID_OPERATION;
765 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800766
767#if 0
768 const std::vector<std::shared_ptr<const C2Info>> infoParams = c2Buffer->info();
769 ALOGV("[%s] queuing gfx buffer with %zu infos", mName, infoParams.size());
770 for (const std::shared_ptr<const C2Info> &info : infoParams) {
771 AString res;
772 for (size_t ix = 0; ix + 3 < info->size(); ix += 4) {
773 if (ix) res.append(", ");
774 res.append(*((int32_t*)info.get() + (ix / 4)));
775 }
776 ALOGV(" [%s]", res.c_str());
777 }
778#endif
779 std::shared_ptr<const C2StreamRotationInfo::output> rotation =
780 std::static_pointer_cast<const C2StreamRotationInfo::output>(
781 c2Buffer->getInfo(C2StreamRotationInfo::output::PARAM_TYPE));
782 bool flip = rotation && (rotation->flip & 1);
783 uint32_t quarters = ((rotation ? rotation->value : 0) / 90) & 3;
784 uint32_t transform = 0;
785 switch (quarters) {
786 case 0: // no rotation
787 transform = flip ? HAL_TRANSFORM_FLIP_H : 0;
788 break;
789 case 1: // 90 degrees counter-clockwise
790 transform = flip ? (HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90)
791 : HAL_TRANSFORM_ROT_270;
792 break;
793 case 2: // 180 degrees
794 transform = flip ? HAL_TRANSFORM_FLIP_V : HAL_TRANSFORM_ROT_180;
795 break;
796 case 3: // 90 degrees clockwise
797 transform = flip ? (HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90)
798 : HAL_TRANSFORM_ROT_90;
799 break;
800 }
801
802 std::shared_ptr<const C2StreamSurfaceScalingInfo::output> surfaceScaling =
803 std::static_pointer_cast<const C2StreamSurfaceScalingInfo::output>(
804 c2Buffer->getInfo(C2StreamSurfaceScalingInfo::output::PARAM_TYPE));
805 uint32_t videoScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
806 if (surfaceScaling) {
807 videoScalingMode = surfaceScaling->value;
808 }
809
810 // Use dataspace from format as it has the default aspects already applied
811 android_dataspace_t dataSpace = HAL_DATASPACE_UNKNOWN; // this is 0
812 (void)buffer->format()->findInt32("android._dataspace", (int32_t *)&dataSpace);
813
814 // HDR static info
815 std::shared_ptr<const C2StreamHdrStaticInfo::output> hdrStaticInfo =
816 std::static_pointer_cast<const C2StreamHdrStaticInfo::output>(
817 c2Buffer->getInfo(C2StreamHdrStaticInfo::output::PARAM_TYPE));
818
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800819 // HDR10 plus info
820 std::shared_ptr<const C2StreamHdr10PlusInfo::output> hdr10PlusInfo =
821 std::static_pointer_cast<const C2StreamHdr10PlusInfo::output>(
822 c2Buffer->getInfo(C2StreamHdr10PlusInfo::output::PARAM_TYPE));
823
Pawin Vongmasa36653902018-11-15 00:10:25 -0800824 {
825 Mutexed<OutputSurface>::Locked output(mOutputSurface);
826 if (output->surface == nullptr) {
827 ALOGI("[%s] cannot render buffer without surface", mName);
828 return OK;
829 }
830 }
831
832 std::vector<C2ConstGraphicBlock> blocks = c2Buffer->data().graphicBlocks();
833 if (blocks.size() != 1u) {
834 ALOGD("[%s] expected 1 graphic block, but got %zu", mName, blocks.size());
835 return UNKNOWN_ERROR;
836 }
837 const C2ConstGraphicBlock &block = blocks.front();
838
839 // TODO: revisit this after C2Fence implementation.
840 android::IGraphicBufferProducer::QueueBufferInput qbi(
841 timestampNs,
842 false, // droppable
843 dataSpace,
844 Rect(blocks.front().crop().left,
845 blocks.front().crop().top,
846 blocks.front().crop().right(),
847 blocks.front().crop().bottom()),
848 videoScalingMode,
849 transform,
850 Fence::NO_FENCE, 0);
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800851 if (hdrStaticInfo || hdr10PlusInfo) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800852 HdrMetadata hdr;
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800853 if (hdrStaticInfo) {
wenchangliuf3f92882020-05-14 00:02:01 +0800854 // If mastering max and min luminance fields are 0, do not use them.
855 // It indicates the value may not be present in the stream.
856 if (hdrStaticInfo->mastering.maxLuminance > 0.0f &&
857 hdrStaticInfo->mastering.minLuminance > 0.0f) {
858 struct android_smpte2086_metadata smpte2086_meta = {
859 .displayPrimaryRed = {
860 hdrStaticInfo->mastering.red.x, hdrStaticInfo->mastering.red.y
861 },
862 .displayPrimaryGreen = {
863 hdrStaticInfo->mastering.green.x, hdrStaticInfo->mastering.green.y
864 },
865 .displayPrimaryBlue = {
866 hdrStaticInfo->mastering.blue.x, hdrStaticInfo->mastering.blue.y
867 },
868 .whitePoint = {
869 hdrStaticInfo->mastering.white.x, hdrStaticInfo->mastering.white.y
870 },
871 .maxLuminance = hdrStaticInfo->mastering.maxLuminance,
872 .minLuminance = hdrStaticInfo->mastering.minLuminance,
873 };
874 hdr.validTypes = HdrMetadata::SMPTE2086;
875 hdr.smpte2086 = smpte2086_meta;
876 }
Chong Zhang3bb2a7f2020-04-21 10:35:12 -0700877 // If the content light level fields are 0, do not use them, it
878 // indicates the value may not be present in the stream.
879 if (hdrStaticInfo->maxCll > 0.0f && hdrStaticInfo->maxFall > 0.0f) {
880 struct android_cta861_3_metadata cta861_meta = {
881 .maxContentLightLevel = hdrStaticInfo->maxCll,
882 .maxFrameAverageLightLevel = hdrStaticInfo->maxFall,
883 };
884 hdr.validTypes |= HdrMetadata::CTA861_3;
885 hdr.cta8613 = cta861_meta;
886 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800887 }
888 if (hdr10PlusInfo) {
889 hdr.validTypes |= HdrMetadata::HDR10PLUS;
890 hdr.hdr10plus.assign(
891 hdr10PlusInfo->m.value,
892 hdr10PlusInfo->m.value + hdr10PlusInfo->flexCount());
893 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800894 qbi.setHdrMetadata(hdr);
895 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800896 // we don't have dirty regions
897 qbi.setSurfaceDamage(Region::INVALID_REGION);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800898 android::IGraphicBufferProducer::QueueBufferOutput qbo;
899 status_t result = mComponent->queueToOutputSurface(block, qbi, &qbo);
900 if (result != OK) {
901 ALOGI("[%s] queueBuffer failed: %d", mName, result);
902 return result;
903 }
904 ALOGV("[%s] queue buffer successful", mName);
905
906 int64_t mediaTimeUs = 0;
907 (void)buffer->meta()->findInt64("timeUs", &mediaTimeUs);
908 mCCodecCallback->onOutputFramesRendered(mediaTimeUs, timestampNs);
909
910 return OK;
911}
912
913status_t CCodecBufferChannel::discardBuffer(const sp<MediaCodecBuffer> &buffer) {
914 ALOGV("[%s] discardBuffer: %p", mName, buffer.get());
915 bool released = false;
916 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700917 Mutexed<Input>::Locked input(mInput);
918 if (input->buffers && input->buffers->releaseBuffer(buffer, nullptr, true)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800919 released = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800920 }
921 }
922 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700923 Mutexed<Output>::Locked output(mOutput);
924 if (output->buffers && output->buffers->releaseBuffer(buffer, nullptr)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800925 released = true;
926 }
927 }
928 if (released) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800929 sendOutputBuffers();
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800930 feedInputBufferIfAvailable();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800931 } else {
932 ALOGD("[%s] MediaCodec discarded an unknown buffer", mName);
933 }
934 return OK;
935}
936
937void CCodecBufferChannel::getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) {
938 array->clear();
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700939 Mutexed<Input>::Locked input(mInput);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800940
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700941 if (!input->buffers->isArrayMode()) {
942 input->buffers = input->buffers->toArrayMode(input->numSlots);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800943 }
944
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700945 input->buffers->getArray(array);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800946}
947
948void CCodecBufferChannel::getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) {
949 array->clear();
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700950 Mutexed<Output>::Locked output(mOutput);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800951
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700952 if (!output->buffers->isArrayMode()) {
953 output->buffers = output->buffers->toArrayMode(output->numSlots);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800954 }
955
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700956 output->buffers->getArray(array);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800957}
958
959status_t CCodecBufferChannel::start(
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800960 const sp<AMessage> &inputFormat,
961 const sp<AMessage> &outputFormat,
962 bool buffersBoundToCodec) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800963 C2StreamBufferTypeSetting::input iStreamFormat(0u);
964 C2StreamBufferTypeSetting::output oStreamFormat(0u);
965 C2PortReorderBufferDepthTuning::output reorderDepth;
966 C2PortReorderKeySetting::output reorderKey;
Wonsik Kim078b58e2019-01-09 15:08:06 -0800967 C2PortActualDelayTuning::input inputDelay(0);
968 C2PortActualDelayTuning::output outputDelay(0);
969 C2ActualPipelineDelayTuning pipelineDelay(0);
970
Pawin Vongmasa36653902018-11-15 00:10:25 -0800971 c2_status_t err = mComponent->query(
972 {
973 &iStreamFormat,
974 &oStreamFormat,
975 &reorderDepth,
976 &reorderKey,
Wonsik Kim078b58e2019-01-09 15:08:06 -0800977 &inputDelay,
978 &pipelineDelay,
979 &outputDelay,
Pawin Vongmasa36653902018-11-15 00:10:25 -0800980 },
981 {},
982 C2_DONT_BLOCK,
983 nullptr);
984 if (err == C2_BAD_INDEX) {
985 if (!iStreamFormat || !oStreamFormat) {
986 return UNKNOWN_ERROR;
987 }
988 } else if (err != C2_OK) {
989 return UNKNOWN_ERROR;
990 }
991
Wonsik Kima4e049d2020-04-28 19:42:23 +0000992 {
993 Mutexed<ReorderStash>::Locked reorder(mReorderStash);
994 reorder->clear();
995 if (reorderDepth) {
996 reorder->setDepth(reorderDepth.value);
997 }
998 if (reorderKey) {
999 reorder->setKey(reorderKey.value);
1000 }
1001 }
1002
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -08001003 uint32_t inputDelayValue = inputDelay ? inputDelay.value : 0;
1004 uint32_t pipelineDelayValue = pipelineDelay ? pipelineDelay.value : 0;
1005 uint32_t outputDelayValue = outputDelay ? outputDelay.value : 0;
1006
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001007 size_t numInputSlots = inputDelayValue + pipelineDelayValue + kSmoothnessFactor;
1008 size_t numOutputSlots = outputDelayValue + kSmoothnessFactor;
Wonsik Kim078b58e2019-01-09 15:08:06 -08001009
Pawin Vongmasa36653902018-11-15 00:10:25 -08001010 // TODO: get this from input format
1011 bool secure = mComponent->getName().find(".secure") != std::string::npos;
1012
1013 std::shared_ptr<C2AllocatorStore> allocatorStore = GetCodec2PlatformAllocatorStore();
Pin-chih Linaa18ea52019-11-19 18:48:50 +08001014 int poolMask = GetCodec2PoolMask();
1015 C2PlatformAllocatorStore::id_t preferredLinearId = GetPreferredLinearAllocatorId(poolMask);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001016
1017 if (inputFormat != nullptr) {
Lajos Molnar3bb81cd2019-02-20 15:10:30 -08001018 bool graphic = (iStreamFormat.value == C2BufferData::GRAPHIC);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001019 std::shared_ptr<C2BlockPool> pool;
1020 {
1021 Mutexed<BlockPools>::Locked pools(mBlockPools);
1022
1023 // set default allocator ID.
1024 pools->inputAllocatorId = (graphic) ? C2PlatformAllocatorStore::GRALLOC
Pin-chih Linaa18ea52019-11-19 18:48:50 +08001025 : preferredLinearId;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001026
1027 // query C2PortAllocatorsTuning::input from component. If an allocator ID is obtained
1028 // from component, create the input block pool with given ID. Otherwise, use default IDs.
1029 std::vector<std::unique_ptr<C2Param>> params;
1030 err = mComponent->query({ },
1031 { C2PortAllocatorsTuning::input::PARAM_TYPE },
1032 C2_DONT_BLOCK,
1033 &params);
1034 if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
1035 ALOGD("[%s] Query input allocators returned %zu params => %s (%u)",
1036 mName, params.size(), asString(err), err);
1037 } else if (err == C2_OK && params.size() == 1) {
1038 C2PortAllocatorsTuning::input *inputAllocators =
1039 C2PortAllocatorsTuning::input::From(params[0].get());
1040 if (inputAllocators && inputAllocators->flexCount() > 0) {
1041 std::shared_ptr<C2Allocator> allocator;
1042 // verify allocator IDs and resolve default allocator
1043 allocatorStore->fetchAllocator(inputAllocators->m.values[0], &allocator);
1044 if (allocator) {
1045 pools->inputAllocatorId = allocator->getId();
1046 } else {
1047 ALOGD("[%s] component requested invalid input allocator ID %u",
1048 mName, inputAllocators->m.values[0]);
1049 }
1050 }
1051 }
1052
1053 // TODO: use C2Component wrapper to associate this pool with ourselves
1054 if ((poolMask >> pools->inputAllocatorId) & 1) {
1055 err = CreateCodec2BlockPool(pools->inputAllocatorId, nullptr, &pool);
1056 ALOGD("[%s] Created input block pool with allocatorID %u => poolID %llu - %s (%d)",
1057 mName, pools->inputAllocatorId,
1058 (unsigned long long)(pool ? pool->getLocalId() : 111000111),
1059 asString(err), err);
1060 } else {
1061 err = C2_NOT_FOUND;
1062 }
1063 if (err != C2_OK) {
1064 C2BlockPool::local_id_t inputPoolId =
1065 graphic ? C2BlockPool::BASIC_GRAPHIC : C2BlockPool::BASIC_LINEAR;
1066 err = GetCodec2BlockPool(inputPoolId, nullptr, &pool);
1067 ALOGD("[%s] Using basic input block pool with poolID %llu => got %llu - %s (%d)",
1068 mName, (unsigned long long)inputPoolId,
1069 (unsigned long long)(pool ? pool->getLocalId() : 111000111),
1070 asString(err), err);
1071 if (err != C2_OK) {
1072 return NO_MEMORY;
1073 }
1074 }
1075 pools->inputPool = pool;
1076 }
1077
Wonsik Kim51051262018-11-28 13:59:05 -08001078 bool forceArrayMode = false;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001079 Mutexed<Input>::Locked input(mInput);
Wonsik Kimbdffead2019-07-01 12:00:07 -07001080 input->inputDelay = inputDelayValue;
1081 input->pipelineDelay = pipelineDelayValue;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001082 input->numSlots = numInputSlots;
1083 input->extraBuffers.flush();
1084 input->numExtraSlots = 0u;
Wonsik Kimfb7a7672019-12-27 17:13:33 -08001085 if (!buffersBoundToCodec) {
1086 input->buffers.reset(new SlotInputBuffers(mName));
1087 } else if (graphic) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001088 if (mInputSurface) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001089 input->buffers.reset(new DummyInputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001090 } else if (mMetaMode == MODE_ANW) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001091 input->buffers.reset(new GraphicMetadataInputBuffers(mName));
Wonsik Kim1221fd12019-07-12 12:52:05 -07001092 // This is to ensure buffers do not get released prematurely.
1093 // TODO: handle this without going into array mode
1094 forceArrayMode = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001095 } else {
Wonsik Kim41d83432020-04-27 16:40:49 -07001096 input->buffers.reset(new GraphicInputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001097 }
1098 } else {
1099 if (hasCryptoOrDescrambler()) {
1100 int32_t capacity = kLinearBufferSize;
1101 (void)inputFormat->findInt32(KEY_MAX_INPUT_SIZE, &capacity);
1102 if ((size_t)capacity > kMaxLinearBufferSize) {
1103 ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize);
1104 capacity = kMaxLinearBufferSize;
1105 }
1106 if (mDealer == nullptr) {
1107 mDealer = new MemoryDealer(
1108 align(capacity, MemoryDealer::getAllocationAlignment())
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001109 * (numInputSlots + 1),
Pawin Vongmasa36653902018-11-15 00:10:25 -08001110 "EncryptedLinearInputBuffers");
1111 mDecryptDestination = mDealer->allocate((size_t)capacity);
1112 }
1113 if (mCrypto != nullptr && mHeapSeqNum < 0) {
Robert Shih895fba92019-07-16 16:29:44 -07001114 sp<HidlMemory> heap = fromHeap(mDealer->getMemoryHeap());
1115 mHeapSeqNum = mCrypto->setHeap(heap);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001116 } else {
1117 mHeapSeqNum = -1;
1118 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001119 input->buffers.reset(new EncryptedLinearInputBuffers(
Wonsik Kim078b58e2019-01-09 15:08:06 -08001120 secure, mDealer, mCrypto, mHeapSeqNum, (size_t)capacity,
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001121 numInputSlots, mName));
Wonsik Kim51051262018-11-28 13:59:05 -08001122 forceArrayMode = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001123 } else {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001124 input->buffers.reset(new LinearInputBuffers(mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001125 }
1126 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001127 input->buffers->setFormat(inputFormat);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001128
1129 if (err == C2_OK) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001130 input->buffers->setPool(pool);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001131 } else {
1132 // TODO: error
1133 }
Wonsik Kim51051262018-11-28 13:59:05 -08001134
1135 if (forceArrayMode) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001136 input->buffers = input->buffers->toArrayMode(numInputSlots);
Wonsik Kim51051262018-11-28 13:59:05 -08001137 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001138 }
1139
1140 if (outputFormat != nullptr) {
1141 sp<IGraphicBufferProducer> outputSurface;
1142 uint32_t outputGeneration;
1143 {
1144 Mutexed<OutputSurface>::Locked output(mOutputSurface);
Sungtak Leed7463d12019-09-04 16:01:00 -07001145 output->maxDequeueBuffers = numOutputSlots +
Sungtak Lee7a7b7422019-07-16 17:40:40 -07001146 reorderDepth.value + kRenderingDepth;
Sungtak Leed7463d12019-09-04 16:01:00 -07001147 if (!secure) {
1148 output->maxDequeueBuffers += numInputSlots;
1149 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001150 outputSurface = output->surface ?
1151 output->surface->getIGraphicBufferProducer() : nullptr;
Wonsik Kimf5e5c832019-02-21 11:36:05 -08001152 if (outputSurface) {
1153 output->surface->setMaxDequeuedBufferCount(output->maxDequeueBuffers);
1154 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001155 outputGeneration = output->generation;
1156 }
1157
Lajos Molnar3bb81cd2019-02-20 15:10:30 -08001158 bool graphic = (oStreamFormat.value == C2BufferData::GRAPHIC);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001159 C2BlockPool::local_id_t outputPoolId_;
1160
1161 {
1162 Mutexed<BlockPools>::Locked pools(mBlockPools);
1163
1164 // set default allocator ID.
1165 pools->outputAllocatorId = (graphic) ? C2PlatformAllocatorStore::GRALLOC
Pin-chih Linaa18ea52019-11-19 18:48:50 +08001166 : preferredLinearId;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001167
1168 // query C2PortAllocatorsTuning::output from component, or use default allocator if
1169 // unsuccessful.
1170 std::vector<std::unique_ptr<C2Param>> params;
1171 err = mComponent->query({ },
1172 { C2PortAllocatorsTuning::output::PARAM_TYPE },
1173 C2_DONT_BLOCK,
1174 &params);
1175 if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
1176 ALOGD("[%s] Query output allocators returned %zu params => %s (%u)",
1177 mName, params.size(), asString(err), err);
1178 } else if (err == C2_OK && params.size() == 1) {
1179 C2PortAllocatorsTuning::output *outputAllocators =
1180 C2PortAllocatorsTuning::output::From(params[0].get());
1181 if (outputAllocators && outputAllocators->flexCount() > 0) {
1182 std::shared_ptr<C2Allocator> allocator;
1183 // verify allocator IDs and resolve default allocator
1184 allocatorStore->fetchAllocator(outputAllocators->m.values[0], &allocator);
1185 if (allocator) {
1186 pools->outputAllocatorId = allocator->getId();
1187 } else {
1188 ALOGD("[%s] component requested invalid output allocator ID %u",
1189 mName, outputAllocators->m.values[0]);
1190 }
1191 }
1192 }
1193
1194 // use bufferqueue if outputting to a surface.
1195 // query C2PortSurfaceAllocatorTuning::output from component, or use default allocator
1196 // if unsuccessful.
1197 if (outputSurface) {
1198 params.clear();
1199 err = mComponent->query({ },
1200 { C2PortSurfaceAllocatorTuning::output::PARAM_TYPE },
1201 C2_DONT_BLOCK,
1202 &params);
1203 if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
1204 ALOGD("[%s] Query output surface allocator returned %zu params => %s (%u)",
1205 mName, params.size(), asString(err), err);
1206 } else if (err == C2_OK && params.size() == 1) {
1207 C2PortSurfaceAllocatorTuning::output *surfaceAllocator =
1208 C2PortSurfaceAllocatorTuning::output::From(params[0].get());
1209 if (surfaceAllocator) {
1210 std::shared_ptr<C2Allocator> allocator;
1211 // verify allocator IDs and resolve default allocator
1212 allocatorStore->fetchAllocator(surfaceAllocator->value, &allocator);
1213 if (allocator) {
1214 pools->outputAllocatorId = allocator->getId();
1215 } else {
1216 ALOGD("[%s] component requested invalid surface output allocator ID %u",
1217 mName, surfaceAllocator->value);
1218 err = C2_BAD_VALUE;
1219 }
1220 }
1221 }
1222 if (pools->outputAllocatorId == C2PlatformAllocatorStore::GRALLOC
1223 && err != C2_OK
1224 && ((poolMask >> C2PlatformAllocatorStore::BUFFERQUEUE) & 1)) {
1225 pools->outputAllocatorId = C2PlatformAllocatorStore::BUFFERQUEUE;
1226 }
1227 }
1228
1229 if ((poolMask >> pools->outputAllocatorId) & 1) {
1230 err = mComponent->createBlockPool(
1231 pools->outputAllocatorId, &pools->outputPoolId, &pools->outputPoolIntf);
1232 ALOGI("[%s] Created output block pool with allocatorID %u => poolID %llu - %s",
1233 mName, pools->outputAllocatorId,
1234 (unsigned long long)pools->outputPoolId,
1235 asString(err));
1236 } else {
1237 err = C2_NOT_FOUND;
1238 }
1239 if (err != C2_OK) {
1240 // use basic pool instead
1241 pools->outputPoolId =
1242 graphic ? C2BlockPool::BASIC_GRAPHIC : C2BlockPool::BASIC_LINEAR;
1243 }
1244
1245 // Configure output block pool ID as parameter C2PortBlockPoolsTuning::output to
1246 // component.
1247 std::unique_ptr<C2PortBlockPoolsTuning::output> poolIdsTuning =
1248 C2PortBlockPoolsTuning::output::AllocUnique({ pools->outputPoolId });
1249
1250 std::vector<std::unique_ptr<C2SettingResult>> failures;
1251 err = mComponent->config({ poolIdsTuning.get() }, C2_MAY_BLOCK, &failures);
1252 ALOGD("[%s] Configured output block pool ids %llu => %s",
1253 mName, (unsigned long long)poolIdsTuning->m.values[0], asString(err));
1254 outputPoolId_ = pools->outputPoolId;
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
1271
1272 // Try to set output surface to created block pool if given.
1273 if (outputSurface) {
1274 mComponent->setOutputSurface(
1275 outputPoolId_,
1276 outputSurface,
1277 outputGeneration);
1278 }
1279
Wonsik Kim8ab25aa2019-06-24 16:37:37 -07001280 if (oStreamFormat.value == C2BufferData::LINEAR) {
Wonsik Kim58713302020-01-29 22:25:23 -08001281 if (buffersBoundToCodec) {
1282 // WORKAROUND: if we're using early CSD workaround we convert to
1283 // array mode, to appease apps assuming the output
1284 // buffers to be of the same size.
1285 output->buffers = output->buffers->toArrayMode(numOutputSlots);
1286 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001287
1288 int32_t channelCount;
1289 int32_t sampleRate;
1290 if (outputFormat->findInt32(KEY_CHANNEL_COUNT, &channelCount)
1291 && outputFormat->findInt32(KEY_SAMPLE_RATE, &sampleRate)) {
1292 int32_t delay = 0;
1293 int32_t padding = 0;;
1294 if (!outputFormat->findInt32("encoder-delay", &delay)) {
1295 delay = 0;
1296 }
1297 if (!outputFormat->findInt32("encoder-padding", &padding)) {
1298 padding = 0;
1299 }
1300 if (delay || padding) {
1301 // We need write access to the buffers, and we're already in
1302 // array mode.
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001303 output->buffers->initSkipCutBuffer(delay, padding, sampleRate, channelCount);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001304 }
1305 }
1306 }
1307 }
1308
1309 // Set up pipeline control. This has to be done after mInputBuffers and
1310 // mOutputBuffers are initialized to make sure that lingering callbacks
1311 // about buffers from the previous generation do not interfere with the
1312 // newly initialized pipeline capacity.
1313
Wonsik Kimab34ed62019-01-31 15:28:46 -08001314 {
1315 Mutexed<PipelineWatcher>::Locked watcher(mPipelineWatcher);
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -08001316 watcher->inputDelay(inputDelayValue)
1317 .pipelineDelay(pipelineDelayValue)
1318 .outputDelay(outputDelayValue)
Wonsik Kimab34ed62019-01-31 15:28:46 -08001319 .smoothnessFactor(kSmoothnessFactor);
1320 watcher->flush();
1321 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001322
1323 mInputMetEos = false;
1324 mSync.start();
1325 return OK;
1326}
1327
1328status_t CCodecBufferChannel::requestInitialInputBuffers() {
1329 if (mInputSurface) {
1330 return OK;
1331 }
1332
Lajos Molnar3bb81cd2019-02-20 15:10:30 -08001333 C2StreamBufferTypeSetting::output oStreamFormat(0u);
Wonsik Kim8ab25aa2019-06-24 16:37:37 -07001334 C2PrependHeaderModeSetting prepend(PREPEND_HEADER_TO_NONE);
1335 c2_status_t err = mComponent->query({ &oStreamFormat, &prepend }, {}, C2_DONT_BLOCK, nullptr);
1336 if (err != C2_OK && err != C2_BAD_INDEX) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001337 return UNKNOWN_ERROR;
1338 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001339 size_t numInputSlots = mInput.lock()->numSlots;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001340 std::vector<sp<MediaCodecBuffer>> toBeQueued;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001341 for (size_t i = 0; i < numInputSlots; ++i) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001342 size_t index;
1343 sp<MediaCodecBuffer> buffer;
1344 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001345 Mutexed<Input>::Locked input(mInput);
1346 if (!input->buffers->requestNewBuffer(&index, &buffer)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001347 if (i == 0) {
1348 ALOGW("[%s] start: cannot allocate memory at all", mName);
1349 return NO_MEMORY;
1350 } else {
1351 ALOGV("[%s] start: cannot allocate memory, only %zu buffers allocated",
1352 mName, i);
1353 }
1354 break;
1355 }
1356 }
1357 if (buffer) {
1358 Mutexed<std::list<sp<ABuffer>>>::Locked configs(mFlushedConfigs);
1359 ALOGV("[%s] input buffer %zu available", mName, index);
1360 bool post = true;
1361 if (!configs->empty()) {
1362 sp<ABuffer> config = configs->front();
Pawin Vongmasa472c7382019-03-26 18:13:58 -07001363 configs->pop_front();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001364 if (buffer->capacity() >= config->size()) {
1365 memcpy(buffer->base(), config->data(), config->size());
1366 buffer->setRange(0, config->size());
1367 buffer->meta()->clear();
1368 buffer->meta()->setInt64("timeUs", 0);
1369 buffer->meta()->setInt32("csd", 1);
1370 post = false;
1371 } else {
1372 ALOGD("[%s] buffer capacity too small for the config (%zu < %zu)",
1373 mName, buffer->capacity(), config->size());
1374 }
1375 } else if (oStreamFormat.value == C2BufferData::LINEAR && i == 0
Wonsik Kim8ab25aa2019-06-24 16:37:37 -07001376 && (!prepend || prepend.value == PREPEND_HEADER_TO_NONE)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001377 // WORKAROUND: Some apps expect CSD available without queueing
1378 // any input. Queue an empty buffer to get the CSD.
1379 buffer->setRange(0, 0);
1380 buffer->meta()->clear();
1381 buffer->meta()->setInt64("timeUs", 0);
1382 post = false;
1383 }
Wonsik Kimab34ed62019-01-31 15:28:46 -08001384 if (post) {
1385 mCallback->onInputBufferAvailable(index, buffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001386 } else {
Wonsik Kimab34ed62019-01-31 15:28:46 -08001387 toBeQueued.emplace_back(buffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001388 }
1389 }
1390 }
1391 for (const sp<MediaCodecBuffer> &buffer : toBeQueued) {
1392 if (queueInputBufferInternal(buffer) != OK) {
Wonsik Kimab34ed62019-01-31 15:28:46 -08001393 ALOGV("[%s] Error while queueing initial buffers", mName);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001394 }
1395 }
1396 return OK;
1397}
1398
1399void CCodecBufferChannel::stop() {
1400 mSync.stop();
1401 mFirstValidFrameIndex = mFrameIndex.load(std::memory_order_relaxed);
1402 if (mInputSurface != nullptr) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001403 mInputSurface.reset();
1404 }
1405}
1406
Wonsik Kim936a89c2020-05-08 16:07:50 -07001407void CCodecBufferChannel::reset() {
1408 stop();
1409 {
1410 Mutexed<Input>::Locked input(mInput);
1411 input->buffers.reset(new DummyInputBuffers(""));
1412 }
1413 {
1414 Mutexed<Output>::Locked output(mOutput);
1415 output->buffers.reset();
1416 }
1417}
1418
1419void CCodecBufferChannel::release() {
1420 mComponent.reset();
1421 mInputAllocator.reset();
1422 mOutputSurface.lock()->surface.clear();
1423 {
1424 Mutexed<BlockPools>::Locked blockPools{mBlockPools};
1425 blockPools->inputPool.reset();
1426 blockPools->outputPoolIntf.reset();
1427 }
1428}
1429
1430
Pawin Vongmasa36653902018-11-15 00:10:25 -08001431void CCodecBufferChannel::flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) {
1432 ALOGV("[%s] flush", mName);
1433 {
1434 Mutexed<std::list<sp<ABuffer>>>::Locked configs(mFlushedConfigs);
1435 for (const std::unique_ptr<C2Work> &work : flushedWork) {
1436 if (!(work->input.flags & C2FrameData::FLAG_CODEC_CONFIG)) {
1437 continue;
1438 }
1439 if (work->input.buffers.empty()
1440 || work->input.buffers.front()->data().linearBlocks().empty()) {
1441 ALOGD("[%s] no linear codec config data found", mName);
1442 continue;
1443 }
1444 C2ReadView view =
1445 work->input.buffers.front()->data().linearBlocks().front().map().get();
1446 if (view.error() != C2_OK) {
1447 ALOGD("[%s] failed to map flushed codec config data: %d", mName, view.error());
1448 continue;
1449 }
1450 configs->push_back(ABuffer::CreateAsCopy(view.data(), view.capacity()));
1451 ALOGV("[%s] stashed flushed codec config data (size=%u)", mName, view.capacity());
1452 }
1453 }
1454 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001455 Mutexed<Input>::Locked input(mInput);
1456 input->buffers->flush();
1457 input->extraBuffers.flush();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001458 }
1459 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001460 Mutexed<Output>::Locked output(mOutput);
Wonsik Kim936a89c2020-05-08 16:07:50 -07001461 if (output->buffers) {
1462 output->buffers->flush(flushedWork);
1463 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001464 }
Wonsik Kima4e049d2020-04-28 19:42:23 +00001465 mReorderStash.lock()->flush();
Wonsik Kimab34ed62019-01-31 15:28:46 -08001466 mPipelineWatcher.lock()->flush();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001467}
1468
1469void CCodecBufferChannel::onWorkDone(
1470 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
Wonsik Kimab34ed62019-01-31 15:28:46 -08001471 const C2StreamInitDataInfo::output *initData) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001472 if (handleWork(std::move(work), outputFormat, initData)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001473 feedInputBufferIfAvailable();
1474 }
1475}
1476
1477void CCodecBufferChannel::onInputBufferDone(
Wonsik Kimab34ed62019-01-31 15:28:46 -08001478 uint64_t frameIndex, size_t arrayIndex) {
Pawin Vongmasa8e2cfb52019-05-15 05:20:52 -07001479 if (mInputSurface) {
1480 return;
1481 }
Wonsik Kimab34ed62019-01-31 15:28:46 -08001482 std::shared_ptr<C2Buffer> buffer =
1483 mPipelineWatcher.lock()->onInputBufferReleased(frameIndex, arrayIndex);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001484 bool newInputSlotAvailable;
1485 {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001486 Mutexed<Input>::Locked input(mInput);
1487 newInputSlotAvailable = input->buffers->expireComponentBuffer(buffer);
1488 if (!newInputSlotAvailable) {
1489 (void)input->extraBuffers.expireComponentBuffer(buffer);
1490 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001491 }
1492 if (newInputSlotAvailable) {
1493 feedInputBufferIfAvailable();
1494 }
1495}
1496
1497bool CCodecBufferChannel::handleWork(
1498 std::unique_ptr<C2Work> work,
1499 const sp<AMessage> &outputFormat,
1500 const C2StreamInitDataInfo::output *initData) {
Wonsik Kim936a89c2020-05-08 16:07:50 -07001501 {
Wonsik Kima4e049d2020-04-28 19:42:23 +00001502 Mutexed<Output>::Locked output(mOutput);
Wonsik Kim936a89c2020-05-08 16:07:50 -07001503 if (!output->buffers) {
1504 return false;
1505 }
1506 if (outputFormat != nullptr) {
1507 ALOGD("[%s] onWorkDone: output format changed to %s",
1508 mName, outputFormat->debugString().c_str());
1509 output->buffers->setFormat(outputFormat);
Wonsik Kime75a5da2020-02-14 17:29:03 -08001510
Wonsik Kim936a89c2020-05-08 16:07:50 -07001511 AString mediaType;
1512 if (outputFormat->findString(KEY_MIME, &mediaType)
1513 && mediaType == MIMETYPE_AUDIO_RAW) {
1514 int32_t channelCount;
1515 int32_t sampleRate;
1516 if (outputFormat->findInt32(KEY_CHANNEL_COUNT, &channelCount)
1517 && outputFormat->findInt32(KEY_SAMPLE_RATE, &sampleRate)) {
1518 output->buffers->updateSkipCutBuffer(sampleRate, channelCount);
1519 }
Wonsik Kima4e049d2020-04-28 19:42:23 +00001520 }
1521 }
Wonsik Kime75a5da2020-02-14 17:29:03 -08001522 }
1523
Wonsik Kima4e049d2020-04-28 19:42:23 +00001524 if ((work->input.ordinal.frameIndex - mFirstValidFrameIndex.load()).peek() < 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001525 // Discard frames from previous generation.
1526 ALOGD("[%s] Discard frames from previous generation.", mName);
Wonsik Kima4e049d2020-04-28 19:42:23 +00001527 return false;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001528 }
1529
Wonsik Kim524b0582019-03-12 11:28:57 -07001530 if (mInputSurface == nullptr && (work->worklets.size() != 1u
Pawin Vongmasa36653902018-11-15 00:10:25 -08001531 || !work->worklets.front()
Wonsik Kima4e049d2020-04-28 19:42:23 +00001532 || !(work->worklets.front()->output.flags & C2FrameData::FLAG_INCOMPLETE))) {
1533 mPipelineWatcher.lock()->onWorkDone(work->input.ordinal.frameIndex.peeku());
1534 }
1535
1536 if (work->result == C2_NOT_FOUND) {
1537 ALOGD("[%s] flushed work; ignored.", mName);
1538 return true;
1539 }
1540
1541 if (work->result != C2_OK) {
1542 ALOGD("[%s] work failed to complete: %d", mName, work->result);
1543 mCCodecCallback->onError(work->result, ACTION_CODE_FATAL);
1544 return false;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001545 }
1546
1547 // NOTE: MediaCodec usage supposedly have only one worklet
1548 if (work->worklets.size() != 1u) {
1549 ALOGI("[%s] onWorkDone: incorrect number of worklets: %zu",
1550 mName, work->worklets.size());
1551 mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1552 return false;
1553 }
1554
1555 const std::unique_ptr<C2Worklet> &worklet = work->worklets.front();
1556
1557 std::shared_ptr<C2Buffer> buffer;
1558 // NOTE: MediaCodec usage supposedly have only one output stream.
1559 if (worklet->output.buffers.size() > 1u) {
1560 ALOGI("[%s] onWorkDone: incorrect number of output buffers: %zu",
1561 mName, worklet->output.buffers.size());
1562 mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1563 return false;
1564 } else if (worklet->output.buffers.size() == 1u) {
1565 buffer = worklet->output.buffers[0];
1566 if (!buffer) {
1567 ALOGD("[%s] onWorkDone: nullptr found in buffers; ignored.", mName);
1568 }
1569 }
1570
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001571 std::optional<uint32_t> newInputDelay, newPipelineDelay;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001572 while (!worklet->output.configUpdate.empty()) {
1573 std::unique_ptr<C2Param> param;
1574 worklet->output.configUpdate.back().swap(param);
1575 worklet->output.configUpdate.pop_back();
1576 switch (param->coreIndex().coreIndex()) {
1577 case C2PortReorderBufferDepthTuning::CORE_INDEX: {
1578 C2PortReorderBufferDepthTuning::output reorderDepth;
1579 if (reorderDepth.updateFrom(*param)) {
Wonsik Kima4e049d2020-04-28 19:42:23 +00001580 bool secure = mComponent->getName().find(".secure") != std::string::npos;
1581 mReorderStash.lock()->setDepth(reorderDepth.value);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001582 ALOGV("[%s] onWorkDone: updated reorder depth to %u",
1583 mName, reorderDepth.value);
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001584 size_t numOutputSlots = mOutput.lock()->numSlots;
Sungtak Lee7a7b7422019-07-16 17:40:40 -07001585 size_t numInputSlots = mInput.lock()->numSlots;
Wonsik Kimf5e5c832019-02-21 11:36:05 -08001586 Mutexed<OutputSurface>::Locked output(mOutputSurface);
Sungtak Leed7463d12019-09-04 16:01:00 -07001587 output->maxDequeueBuffers = numOutputSlots +
Sungtak Lee7a7b7422019-07-16 17:40:40 -07001588 reorderDepth.value + kRenderingDepth;
Sungtak Leed7463d12019-09-04 16:01:00 -07001589 if (!secure) {
1590 output->maxDequeueBuffers += numInputSlots;
1591 }
Wonsik Kimf5e5c832019-02-21 11:36:05 -08001592 if (output->surface) {
Wonsik Kima4e049d2020-04-28 19:42:23 +00001593 output->surface->setMaxDequeuedBufferCount(output->maxDequeueBuffers);
Wonsik Kimf5e5c832019-02-21 11:36:05 -08001594 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001595 } else {
Wonsik Kima4e049d2020-04-28 19:42:23 +00001596 ALOGD("[%s] onWorkDone: failed to read reorder depth", mName);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001597 }
1598 break;
1599 }
1600 case C2PortReorderKeySetting::CORE_INDEX: {
1601 C2PortReorderKeySetting::output reorderKey;
1602 if (reorderKey.updateFrom(*param)) {
Wonsik Kima4e049d2020-04-28 19:42:23 +00001603 mReorderStash.lock()->setKey(reorderKey.value);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001604 ALOGV("[%s] onWorkDone: updated reorder key to %u",
1605 mName, reorderKey.value);
1606 } else {
1607 ALOGD("[%s] onWorkDone: failed to read reorder key", mName);
1608 }
1609 break;
1610 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001611 case C2PortActualDelayTuning::CORE_INDEX: {
1612 if (param->isGlobal()) {
1613 C2ActualPipelineDelayTuning pipelineDelay;
1614 if (pipelineDelay.updateFrom(*param)) {
1615 ALOGV("[%s] onWorkDone: updating pipeline delay %u",
1616 mName, pipelineDelay.value);
1617 newPipelineDelay = pipelineDelay.value;
Wonsik Kima4e049d2020-04-28 19:42:23 +00001618 (void)mPipelineWatcher.lock()->pipelineDelay(pipelineDelay.value);
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001619 }
1620 }
1621 if (param->forInput()) {
1622 C2PortActualDelayTuning::input inputDelay;
1623 if (inputDelay.updateFrom(*param)) {
1624 ALOGV("[%s] onWorkDone: updating input delay %u",
1625 mName, inputDelay.value);
1626 newInputDelay = inputDelay.value;
Wonsik Kima4e049d2020-04-28 19:42:23 +00001627 (void)mPipelineWatcher.lock()->inputDelay(inputDelay.value);
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001628 }
1629 }
1630 if (param->forOutput()) {
1631 C2PortActualDelayTuning::output outputDelay;
1632 if (outputDelay.updateFrom(*param)) {
1633 ALOGV("[%s] onWorkDone: updating output delay %u",
1634 mName, outputDelay.value);
Wonsik Kima4e049d2020-04-28 19:42:23 +00001635 bool secure = mComponent->getName().find(".secure") != std::string::npos;
1636 (void)mPipelineWatcher.lock()->outputDelay(outputDelay.value);
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001637
1638 bool outputBuffersChanged = false;
Wonsik Kimf0e7d222019-06-28 12:33:16 -07001639 size_t numOutputSlots = 0;
Sungtak Lee7a7b7422019-07-16 17:40:40 -07001640 size_t numInputSlots = mInput.lock()->numSlots;
Wonsik Kimf0e7d222019-06-28 12:33:16 -07001641 {
1642 Mutexed<Output>::Locked output(mOutput);
Wonsik Kim936a89c2020-05-08 16:07:50 -07001643 if (!output->buffers) {
1644 return false;
1645 }
Wonsik Kimf0e7d222019-06-28 12:33:16 -07001646 output->outputDelay = outputDelay.value;
Wonsik Kima4e049d2020-04-28 19:42:23 +00001647 numOutputSlots = outputDelay.value + kSmoothnessFactor;
Wonsik Kimf0e7d222019-06-28 12:33:16 -07001648 if (output->numSlots < numOutputSlots) {
1649 output->numSlots = numOutputSlots;
1650 if (output->buffers->isArrayMode()) {
1651 OutputBuffersArray *array =
1652 (OutputBuffersArray *)output->buffers.get();
1653 ALOGV("[%s] onWorkDone: growing output buffer array to %zu",
1654 mName, numOutputSlots);
1655 array->grow(numOutputSlots);
1656 outputBuffersChanged = true;
1657 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001658 }
Wonsik Kimf0e7d222019-06-28 12:33:16 -07001659 numOutputSlots = output->numSlots;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001660 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001661
1662 if (outputBuffersChanged) {
1663 mCCodecCallback->onOutputBuffersChanged();
1664 }
Wonsik Kimf0e7d222019-06-28 12:33:16 -07001665
Wonsik Kima4e049d2020-04-28 19:42:23 +00001666 uint32_t depth = mReorderStash.lock()->depth();
Wonsik Kimf0e7d222019-06-28 12:33:16 -07001667 Mutexed<OutputSurface>::Locked output(mOutputSurface);
Sungtak Leed7463d12019-09-04 16:01:00 -07001668 output->maxDequeueBuffers = numOutputSlots + depth + kRenderingDepth;
1669 if (!secure) {
1670 output->maxDequeueBuffers += numInputSlots;
1671 }
Wonsik Kimf0e7d222019-06-28 12:33:16 -07001672 if (output->surface) {
1673 output->surface->setMaxDequeuedBufferCount(output->maxDequeueBuffers);
1674 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001675 }
1676 }
1677 break;
1678 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001679 default:
1680 ALOGV("[%s] onWorkDone: unrecognized config update (%08X)",
1681 mName, param->index());
1682 break;
1683 }
1684 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001685 if (newInputDelay || newPipelineDelay) {
1686 Mutexed<Input>::Locked input(mInput);
1687 size_t newNumSlots =
1688 newInputDelay.value_or(input->inputDelay) +
1689 newPipelineDelay.value_or(input->pipelineDelay) +
1690 kSmoothnessFactor;
1691 if (input->buffers->isArrayMode()) {
1692 if (input->numSlots >= newNumSlots) {
1693 input->numExtraSlots = 0;
1694 } else {
1695 input->numExtraSlots = newNumSlots - input->numSlots;
1696 }
1697 ALOGV("[%s] onWorkDone: updated number of extra slots to %zu (input array mode)",
1698 mName, input->numExtraSlots);
1699 } else {
1700 input->numSlots = newNumSlots;
1701 }
1702 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001703
Pawin Vongmasa36653902018-11-15 00:10:25 -08001704 int32_t flags = 0;
1705 if (worklet->output.flags & C2FrameData::FLAG_END_OF_STREAM) {
1706 flags |= MediaCodec::BUFFER_FLAG_EOS;
1707 ALOGV("[%s] onWorkDone: output EOS", mName);
1708 }
1709
Wonsik Kima4e049d2020-04-28 19:42:23 +00001710 sp<MediaCodecBuffer> outBuffer;
1711 size_t index;
1712
Pawin Vongmasa36653902018-11-15 00:10:25 -08001713 // WORKAROUND: adjust output timestamp based on client input timestamp and codec
1714 // input timestamp. Codec output timestamp (in the timestamp field) shall correspond to
1715 // the codec input timestamp, but client output timestamp should (reported in timeUs)
1716 // shall correspond to the client input timesamp (in customOrdinal). By using the
1717 // delta between the two, this allows for some timestamp deviation - e.g. if one input
1718 // produces multiple output.
1719 c2_cntr64_t timestamp =
1720 worklet->output.ordinal.timestamp + work->input.ordinal.customOrdinal
1721 - work->input.ordinal.timestamp;
Wonsik Kim95ba0162019-03-19 15:51:54 -07001722 if (mInputSurface != nullptr) {
1723 // When using input surface we need to restore the original input timestamp.
1724 timestamp = work->input.ordinal.customOrdinal;
1725 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001726 ALOGV("[%s] onWorkDone: input %lld, codec %lld => output %lld => %lld",
1727 mName,
1728 work->input.ordinal.customOrdinal.peekll(),
1729 work->input.ordinal.timestamp.peekll(),
1730 worklet->output.ordinal.timestamp.peekll(),
1731 timestamp.peekll());
1732
1733 if (initData != nullptr) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001734 Mutexed<Output>::Locked output(mOutput);
Wonsik Kim936a89c2020-05-08 16:07:50 -07001735 if (output->buffers && output->buffers->registerCsd(initData, &index, &outBuffer) == OK) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001736 outBuffer->meta()->setInt64("timeUs", timestamp.peek());
1737 outBuffer->meta()->setInt32("flags", MediaCodec::BUFFER_FLAG_CODECCONFIG);
1738 ALOGV("[%s] onWorkDone: csd index = %zu [%p]", mName, index, outBuffer.get());
1739
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001740 output.unlock();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001741 mCallback->onOutputBufferAvailable(index, outBuffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001742 } else {
1743 ALOGD("[%s] onWorkDone: unable to register csd", mName);
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001744 output.unlock();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001745 mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001746 return false;
1747 }
1748 }
1749
Wonsik Kim910a3172020-04-29 16:58:16 -07001750 if (!buffer && !flags && outputFormat == nullptr) {
1751 ALOGV("[%s] onWorkDone: nothing to report from the work (%lld)",
Pawin Vongmasa36653902018-11-15 00:10:25 -08001752 mName, work->input.ordinal.frameIndex.peekull());
Wonsik Kima4e049d2020-04-28 19:42:23 +00001753 return true;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001754 }
1755
1756 if (buffer) {
1757 for (const std::shared_ptr<const C2Info> &info : buffer->info()) {
1758 // TODO: properly translate these to metadata
1759 switch (info->coreIndex().coreIndex()) {
1760 case C2StreamPictureTypeMaskInfo::CORE_INDEX:
Lajos Molnar3bb81cd2019-02-20 15:10:30 -08001761 if (((C2StreamPictureTypeMaskInfo *)info.get())->value & C2Config::SYNC_FRAME) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001762 flags |= MediaCodec::BUFFER_FLAG_SYNCFRAME;
1763 }
1764 break;
1765 default:
1766 break;
1767 }
1768 }
1769 }
1770
1771 {
Wonsik Kima4e049d2020-04-28 19:42:23 +00001772 Mutexed<ReorderStash>::Locked reorder(mReorderStash);
1773 reorder->emplace(buffer, timestamp.peek(), flags, worklet->output.ordinal);
1774 if (flags & MediaCodec::BUFFER_FLAG_EOS) {
1775 // Flush reorder stash
1776 reorder->setDepth(0);
1777 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001778 }
1779 sendOutputBuffers();
1780 return true;
1781}
1782
1783void CCodecBufferChannel::sendOutputBuffers() {
Wonsik Kima4e049d2020-04-28 19:42:23 +00001784 ReorderStash::Entry entry;
Pawin Vongmasab18c1af2020-04-11 05:07:15 -07001785 sp<MediaCodecBuffer> outBuffer;
Wonsik Kima4e049d2020-04-28 19:42:23 +00001786 size_t index;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001787
1788 while (true) {
Wonsik Kima4e049d2020-04-28 19:42:23 +00001789 Mutexed<ReorderStash>::Locked reorder(mReorderStash);
1790 if (!reorder->hasPending()) {
1791 break;
1792 }
1793 if (!reorder->pop(&entry)) {
1794 break;
1795 }
1796
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001797 Mutexed<Output>::Locked output(mOutput);
Wonsik Kim936a89c2020-05-08 16:07:50 -07001798 if (!output->buffers) {
1799 return;
1800 }
Wonsik Kima4e049d2020-04-28 19:42:23 +00001801 status_t err = output->buffers->registerBuffer(entry.buffer, &index, &outBuffer);
1802 if (err != OK) {
1803 bool outputBuffersChanged = false;
1804 if (err != WOULD_BLOCK) {
Pawin Vongmasab18c1af2020-04-11 05:07:15 -07001805 if (!output->buffers->isArrayMode()) {
Wonsik Kima4e049d2020-04-28 19:42:23 +00001806 output->buffers = output->buffers->toArrayMode(output->numSlots);
Pawin Vongmasab18c1af2020-04-11 05:07:15 -07001807 }
Wonsik Kima4e049d2020-04-28 19:42:23 +00001808 OutputBuffersArray *array = (OutputBuffersArray *)output->buffers.get();
1809 array->realloc(entry.buffer);
1810 outputBuffersChanged = true;
1811 }
1812 ALOGV("[%s] sendOutputBuffers: unable to register output buffer", mName);
1813 reorder->defer(entry);
1814
1815 output.unlock();
1816 reorder.unlock();
1817
1818 if (outputBuffersChanged) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001819 mCCodecCallback->onOutputBuffersChanged();
1820 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001821 return;
1822 }
Wonsik Kima4e049d2020-04-28 19:42:23 +00001823 output.unlock();
1824 reorder.unlock();
1825
1826 outBuffer->meta()->setInt64("timeUs", entry.timestamp);
1827 outBuffer->meta()->setInt32("flags", entry.flags);
1828 ALOGV("[%s] sendOutputBuffers: out buffer index = %zu [%p] => %p + %zu (%lld)",
1829 mName, index, outBuffer.get(), outBuffer->data(), outBuffer->size(),
1830 (long long)entry.timestamp);
1831 mCallback->onOutputBufferAvailable(index, outBuffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001832 }
1833}
1834
1835status_t CCodecBufferChannel::setSurface(const sp<Surface> &newSurface) {
1836 static std::atomic_uint32_t surfaceGeneration{0};
1837 uint32_t generation = (getpid() << 10) |
1838 ((surfaceGeneration.fetch_add(1, std::memory_order_relaxed) + 1)
1839 & ((1 << 10) - 1));
1840
1841 sp<IGraphicBufferProducer> producer;
1842 if (newSurface) {
1843 newSurface->setScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Sungtak Leeab6f2f32019-02-15 14:43:51 -08001844 newSurface->setDequeueTimeout(kDequeueTimeoutNs);
Sungtak Lee08515812019-06-05 11:16:32 -07001845 newSurface->setMaxDequeuedBufferCount(mOutputSurface.lock()->maxDequeueBuffers);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001846 producer = newSurface->getIGraphicBufferProducer();
1847 producer->setGenerationNumber(generation);
1848 } else {
1849 ALOGE("[%s] setting output surface to null", mName);
1850 return INVALID_OPERATION;
1851 }
1852
1853 std::shared_ptr<Codec2Client::Configurable> outputPoolIntf;
1854 C2BlockPool::local_id_t outputPoolId;
1855 {
1856 Mutexed<BlockPools>::Locked pools(mBlockPools);
1857 outputPoolId = pools->outputPoolId;
1858 outputPoolIntf = pools->outputPoolIntf;
1859 }
1860
1861 if (outputPoolIntf) {
1862 if (mComponent->setOutputSurface(
1863 outputPoolId,
1864 producer,
1865 generation) != C2_OK) {
1866 ALOGI("[%s] setSurface: component setOutputSurface failed", mName);
1867 return INVALID_OPERATION;
1868 }
1869 }
1870
1871 {
1872 Mutexed<OutputSurface>::Locked output(mOutputSurface);
1873 output->surface = newSurface;
1874 output->generation = generation;
1875 }
1876
1877 return OK;
1878}
1879
Wonsik Kimab34ed62019-01-31 15:28:46 -08001880PipelineWatcher::Clock::duration CCodecBufferChannel::elapsed() {
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -08001881 // When client pushed EOS, we want all the work to be done quickly.
1882 // Otherwise, component may have stalled work due to input starvation up to
1883 // the sum of the delay in the pipeline.
Wonsik Kimf0e7d222019-06-28 12:33:16 -07001884 size_t n = 0;
1885 if (!mInputMetEos) {
1886 size_t outputDelay = mOutput.lock()->outputDelay;
1887 Mutexed<Input>::Locked input(mInput);
1888 n = input->inputDelay + input->pipelineDelay + outputDelay;
1889 }
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -08001890 return mPipelineWatcher.lock()->elapsed(PipelineWatcher::Clock::now(), n);
Wonsik Kimab34ed62019-01-31 15:28:46 -08001891}
1892
Pawin Vongmasa36653902018-11-15 00:10:25 -08001893void CCodecBufferChannel::setMetaMode(MetaMode mode) {
1894 mMetaMode = mode;
1895}
1896
Wonsik Kim596187e2019-10-25 12:44:10 -07001897void CCodecBufferChannel::setCrypto(const sp<ICrypto> &crypto) {
Wonsik Kimfb7a7672019-12-27 17:13:33 -08001898 if (mCrypto != nullptr) {
1899 for (std::pair<wp<HidlMemory>, int32_t> entry : mHeapSeqNumMap) {
1900 mCrypto->unsetHeap(entry.second);
1901 }
1902 mHeapSeqNumMap.clear();
1903 if (mHeapSeqNum >= 0) {
1904 mCrypto->unsetHeap(mHeapSeqNum);
1905 mHeapSeqNum = -1;
1906 }
1907 }
Wonsik Kim596187e2019-10-25 12:44:10 -07001908 mCrypto = crypto;
1909}
1910
1911void CCodecBufferChannel::setDescrambler(const sp<IDescrambler> &descrambler) {
1912 mDescrambler = descrambler;
1913}
1914
Pawin Vongmasa36653902018-11-15 00:10:25 -08001915status_t toStatusT(c2_status_t c2s, c2_operation_t c2op) {
1916 // C2_OK is always translated to OK.
1917 if (c2s == C2_OK) {
1918 return OK;
1919 }
1920
1921 // Operation-dependent translation
1922 // TODO: Add as necessary
1923 switch (c2op) {
1924 case C2_OPERATION_Component_start:
1925 switch (c2s) {
1926 case C2_NO_MEMORY:
1927 return NO_MEMORY;
1928 default:
1929 return UNKNOWN_ERROR;
1930 }
1931 default:
1932 break;
1933 }
1934
1935 // Backup operation-agnostic translation
1936 switch (c2s) {
1937 case C2_BAD_INDEX:
1938 return BAD_INDEX;
1939 case C2_BAD_VALUE:
1940 return BAD_VALUE;
1941 case C2_BLOCKING:
1942 return WOULD_BLOCK;
1943 case C2_DUPLICATE:
1944 return ALREADY_EXISTS;
1945 case C2_NO_INIT:
1946 return NO_INIT;
1947 case C2_NO_MEMORY:
1948 return NO_MEMORY;
1949 case C2_NOT_FOUND:
1950 return NAME_NOT_FOUND;
1951 case C2_TIMED_OUT:
1952 return TIMED_OUT;
1953 case C2_BAD_STATE:
1954 case C2_CANCELED:
1955 case C2_CANNOT_DO:
1956 case C2_CORRUPTED:
1957 case C2_OMITTED:
1958 case C2_REFUSED:
1959 return UNKNOWN_ERROR;
1960 default:
1961 return -static_cast<status_t>(c2s);
1962 }
1963}
1964
1965} // namespace android