blob: a278a81d5b4d1a038e2006daf97150fa35a1fc05 [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>
30#include <android-base/stringprintf.h>
31#include <binder/MemoryDealer.h>
32#include <gui/Surface.h>
33#include <media/openmax/OMX_Core.h>
34#include <media/stagefright/foundation/ABuffer.h>
35#include <media/stagefright/foundation/ALookup.h>
36#include <media/stagefright/foundation/AMessage.h>
37#include <media/stagefright/foundation/AUtils.h>
38#include <media/stagefright/foundation/hexdump.h>
39#include <media/stagefright/MediaCodec.h>
40#include <media/stagefright/MediaCodecConstants.h>
41#include <media/MediaCodecBuffer.h>
42#include <system/window.h>
43
44#include "CCodecBufferChannel.h"
45#include "Codec2Buffer.h"
46#include "SkipCutBuffer.h"
47
48namespace android {
49
50using android::base::StringPrintf;
51using hardware::hidl_handle;
52using hardware::hidl_string;
53using hardware::hidl_vec;
54using namespace hardware::cas::V1_0;
55using namespace hardware::cas::native::V1_0;
56
57using CasStatus = hardware::cas::V1_0::Status;
58
59/**
60 * Base class for representation of buffers at one port.
61 */
62class CCodecBufferChannel::Buffers {
63public:
64 Buffers(const char *componentName, const char *name = "Buffers")
65 : mComponentName(componentName),
66 mChannelName(std::string(componentName) + ":" + name),
67 mName(mChannelName.c_str()) {
68 }
69 virtual ~Buffers() = default;
70
71 /**
72 * Set format for MediaCodec-facing buffers.
73 */
74 void setFormat(const sp<AMessage> &format) {
75 CHECK(format != nullptr);
76 mFormat = format;
77 }
78
79 /**
80 * Return a copy of current format.
81 */
82 sp<AMessage> dupFormat() {
83 return mFormat != nullptr ? mFormat->dup() : nullptr;
84 }
85
86 /**
87 * Returns true if the buffers are operating under array mode.
88 */
89 virtual bool isArrayMode() const { return false; }
90
91 /**
92 * Fills the vector with MediaCodecBuffer's if in array mode; otherwise,
93 * no-op.
94 */
95 virtual void getArray(Vector<sp<MediaCodecBuffer>> *) const {}
96
Wonsik Kimdf5dd142019-02-06 10:15:46 -080097 /**
98 * Return number of buffers the client owns.
99 */
100 virtual size_t numClientBuffers() const = 0;
101
Pawin Vongmasa36653902018-11-15 00:10:25 -0800102protected:
103 std::string mComponentName; ///< name of component for debugging
104 std::string mChannelName; ///< name of channel for debugging
105 const char *mName; ///< C-string version of channel name
106 // Format to be used for creating MediaCodec-facing buffers.
107 sp<AMessage> mFormat;
108
109private:
110 DISALLOW_EVIL_CONSTRUCTORS(Buffers);
111};
112
113class CCodecBufferChannel::InputBuffers : public CCodecBufferChannel::Buffers {
114public:
115 InputBuffers(const char *componentName, const char *name = "Input[]")
116 : Buffers(componentName, name) { }
117 virtual ~InputBuffers() = default;
118
119 /**
120 * Set a block pool to obtain input memory blocks.
121 */
122 void setPool(const std::shared_ptr<C2BlockPool> &pool) { mPool = pool; }
123
124 /**
125 * Get a new MediaCodecBuffer for input and its corresponding index.
126 * Returns false if no new buffer can be obtained at the moment.
127 */
128 virtual bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) = 0;
129
130 /**
131 * Release the buffer obtained from requestNewBuffer() and get the
132 * associated C2Buffer object back. Returns true if the buffer was on file
133 * and released successfully.
134 */
135 virtual bool releaseBuffer(
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800136 const sp<MediaCodecBuffer> &buffer,
137 std::shared_ptr<C2Buffer> *c2buffer,
138 bool release) = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800139
140 /**
141 * Release the buffer that is no longer used by the codec process. Return
142 * true if and only if the buffer was on file and released successfully.
143 */
144 virtual bool expireComponentBuffer(
145 const std::shared_ptr<C2Buffer> &c2buffer) = 0;
146
147 /**
148 * Flush internal state. After this call, no index or buffer previously
149 * returned from requestNewBuffer() is valid.
150 */
151 virtual void flush() = 0;
152
153 /**
154 * Return array-backed version of input buffers. The returned object
155 * shall retain the internal state so that it will honor index and
156 * buffer from previous calls of requestNewBuffer().
157 */
158 virtual std::unique_ptr<InputBuffers> toArrayMode(size_t size) = 0;
159
160protected:
161 // Pool to obtain blocks for input buffers.
162 std::shared_ptr<C2BlockPool> mPool;
163
164private:
165 DISALLOW_EVIL_CONSTRUCTORS(InputBuffers);
166};
167
168class CCodecBufferChannel::OutputBuffers : public CCodecBufferChannel::Buffers {
169public:
170 OutputBuffers(const char *componentName, const char *name = "Output")
171 : Buffers(componentName, name) { }
172 virtual ~OutputBuffers() = default;
173
174 /**
175 * Register output C2Buffer from the component and obtain corresponding
176 * index and MediaCodecBuffer object. Returns false if registration
177 * fails.
178 */
179 virtual status_t registerBuffer(
180 const std::shared_ptr<C2Buffer> &buffer,
181 size_t *index,
182 sp<MediaCodecBuffer> *clientBuffer) = 0;
183
184 /**
185 * Register codec specific data as a buffer to be consistent with
186 * MediaCodec behavior.
187 */
188 virtual status_t registerCsd(
189 const C2StreamCsdInfo::output * /* csd */,
190 size_t * /* index */,
191 sp<MediaCodecBuffer> * /* clientBuffer */) = 0;
192
193 /**
194 * Release the buffer obtained from registerBuffer() and get the
195 * associated C2Buffer object back. Returns true if the buffer was on file
196 * and released successfully.
197 */
198 virtual bool releaseBuffer(
199 const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) = 0;
200
201 /**
202 * Flush internal state. After this call, no index or buffer previously
203 * returned from registerBuffer() is valid.
204 */
205 virtual void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) = 0;
206
207 /**
208 * Return array-backed version of output buffers. The returned object
209 * shall retain the internal state so that it will honor index and
210 * buffer from previous calls of registerBuffer().
211 */
212 virtual std::unique_ptr<OutputBuffers> toArrayMode(size_t size) = 0;
213
214 /**
215 * Initialize SkipCutBuffer object.
216 */
217 void initSkipCutBuffer(
218 int32_t delay, int32_t padding, int32_t sampleRate, int32_t channelCount) {
219 CHECK(mSkipCutBuffer == nullptr);
220 mDelay = delay;
221 mPadding = padding;
222 mSampleRate = sampleRate;
223 setSkipCutBuffer(delay, padding, channelCount);
224 }
225
226 /**
227 * Update the SkipCutBuffer object. No-op if it's never initialized.
228 */
229 void updateSkipCutBuffer(int32_t sampleRate, int32_t channelCount) {
230 if (mSkipCutBuffer == nullptr) {
231 return;
232 }
233 int32_t delay = mDelay;
234 int32_t padding = mPadding;
235 if (sampleRate != mSampleRate) {
236 delay = ((int64_t)delay * sampleRate) / mSampleRate;
237 padding = ((int64_t)padding * sampleRate) / mSampleRate;
238 }
239 setSkipCutBuffer(delay, padding, channelCount);
240 }
241
242 /**
243 * Submit buffer to SkipCutBuffer object, if initialized.
244 */
245 void submit(const sp<MediaCodecBuffer> &buffer) {
246 if (mSkipCutBuffer != nullptr) {
247 mSkipCutBuffer->submit(buffer);
248 }
249 }
250
251 /**
252 * Transfer SkipCutBuffer object to the other Buffers object.
253 */
254 void transferSkipCutBuffer(const sp<SkipCutBuffer> &scb) {
255 mSkipCutBuffer = scb;
256 }
257
258protected:
259 sp<SkipCutBuffer> mSkipCutBuffer;
260
261private:
262 int32_t mDelay;
263 int32_t mPadding;
264 int32_t mSampleRate;
265
266 void setSkipCutBuffer(int32_t skip, int32_t cut, int32_t channelCount) {
267 if (mSkipCutBuffer != nullptr) {
268 size_t prevSize = mSkipCutBuffer->size();
269 if (prevSize != 0u) {
270 ALOGD("[%s] Replacing SkipCutBuffer holding %zu bytes", mName, prevSize);
271 }
272 }
273 mSkipCutBuffer = new SkipCutBuffer(skip, cut, channelCount);
274 }
275
276 DISALLOW_EVIL_CONSTRUCTORS(OutputBuffers);
277};
278
279namespace {
280
Wonsik Kim078b58e2019-01-09 15:08:06 -0800281const static size_t kSmoothnessFactor = 4;
282const static size_t kRenderingDepth = 3;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800283const static size_t kLinearBufferSize = 1048576;
284// This can fit 4K RGBA frame, and most likely client won't need more than this.
285const static size_t kMaxLinearBufferSize = 3840 * 2160 * 4;
286
287/**
288 * Simple local buffer pool backed by std::vector.
289 */
290class LocalBufferPool : public std::enable_shared_from_this<LocalBufferPool> {
291public:
292 /**
293 * Create a new LocalBufferPool object.
294 *
295 * \param poolCapacity max total size of buffers managed by this pool.
296 *
297 * \return a newly created pool object.
298 */
299 static std::shared_ptr<LocalBufferPool> Create(size_t poolCapacity) {
300 return std::shared_ptr<LocalBufferPool>(new LocalBufferPool(poolCapacity));
301 }
302
303 /**
304 * Return an ABuffer object whose size is at least |capacity|.
305 *
306 * \param capacity requested capacity
307 * \return nullptr if the pool capacity is reached
308 * an ABuffer object otherwise.
309 */
310 sp<ABuffer> newBuffer(size_t capacity) {
311 Mutex::Autolock lock(mMutex);
312 auto it = std::find_if(
313 mPool.begin(), mPool.end(),
314 [capacity](const std::vector<uint8_t> &vec) {
315 return vec.capacity() >= capacity;
316 });
317 if (it != mPool.end()) {
318 sp<ABuffer> buffer = new VectorBuffer(std::move(*it), shared_from_this());
319 mPool.erase(it);
320 return buffer;
321 }
322 if (mUsedSize + capacity > mPoolCapacity) {
323 while (!mPool.empty()) {
324 mUsedSize -= mPool.back().capacity();
325 mPool.pop_back();
326 }
327 if (mUsedSize + capacity > mPoolCapacity) {
328 ALOGD("mUsedSize = %zu, capacity = %zu, mPoolCapacity = %zu",
329 mUsedSize, capacity, mPoolCapacity);
330 return nullptr;
331 }
332 }
333 std::vector<uint8_t> vec(capacity);
334 mUsedSize += vec.capacity();
335 return new VectorBuffer(std::move(vec), shared_from_this());
336 }
337
338private:
339 /**
340 * ABuffer backed by std::vector.
341 */
342 class VectorBuffer : public ::android::ABuffer {
343 public:
344 /**
345 * Construct a VectorBuffer by taking the ownership of supplied vector.
346 *
347 * \param vec backing vector of the buffer. this object takes
348 * ownership at construction.
349 * \param pool a LocalBufferPool object to return the vector at
350 * destruction.
351 */
352 VectorBuffer(std::vector<uint8_t> &&vec, const std::shared_ptr<LocalBufferPool> &pool)
353 : ABuffer(vec.data(), vec.capacity()),
354 mVec(std::move(vec)),
355 mPool(pool) {
356 }
357
358 ~VectorBuffer() override {
359 std::shared_ptr<LocalBufferPool> pool = mPool.lock();
360 if (pool) {
361 // If pool is alive, return the vector back to the pool so that
362 // it can be recycled.
363 pool->returnVector(std::move(mVec));
364 }
365 }
366
367 private:
368 std::vector<uint8_t> mVec;
369 std::weak_ptr<LocalBufferPool> mPool;
370 };
371
372 Mutex mMutex;
373 size_t mPoolCapacity;
374 size_t mUsedSize;
375 std::list<std::vector<uint8_t>> mPool;
376
377 /**
378 * Private constructor to prevent constructing non-managed LocalBufferPool.
379 */
380 explicit LocalBufferPool(size_t poolCapacity)
381 : mPoolCapacity(poolCapacity), mUsedSize(0) {
382 }
383
384 /**
385 * Take back the ownership of vec from the destructed VectorBuffer and put
386 * it in front of the pool.
387 */
388 void returnVector(std::vector<uint8_t> &&vec) {
389 Mutex::Autolock lock(mMutex);
390 mPool.push_front(std::move(vec));
391 }
392
393 DISALLOW_EVIL_CONSTRUCTORS(LocalBufferPool);
394};
395
396sp<GraphicBlockBuffer> AllocateGraphicBuffer(
397 const std::shared_ptr<C2BlockPool> &pool,
398 const sp<AMessage> &format,
399 uint32_t pixelFormat,
400 const C2MemoryUsage &usage,
401 const std::shared_ptr<LocalBufferPool> &localBufferPool) {
402 int32_t width, height;
403 if (!format->findInt32("width", &width) || !format->findInt32("height", &height)) {
404 ALOGD("format lacks width or height");
405 return nullptr;
406 }
407
408 std::shared_ptr<C2GraphicBlock> block;
409 c2_status_t err = pool->fetchGraphicBlock(
410 width, height, pixelFormat, usage, &block);
411 if (err != C2_OK) {
412 ALOGD("fetch graphic block failed: %d", err);
413 return nullptr;
414 }
415
416 return GraphicBlockBuffer::Allocate(
417 format,
418 block,
419 [localBufferPool](size_t capacity) {
420 return localBufferPool->newBuffer(capacity);
421 });
422}
423
424class BuffersArrayImpl;
425
426/**
427 * Flexible buffer slots implementation.
428 */
429class FlexBuffersImpl {
430public:
431 FlexBuffersImpl(const char *name)
432 : mImplName(std::string(name) + ".Impl"),
433 mName(mImplName.c_str()) { }
434
435 /**
436 * Assign an empty slot for a buffer and return the index. If there's no
437 * empty slot, just add one at the end and return it.
438 *
439 * \param buffer[in] a new buffer to assign a slot.
440 * \return index of the assigned slot.
441 */
442 size_t assignSlot(const sp<Codec2Buffer> &buffer) {
443 for (size_t i = 0; i < mBuffers.size(); ++i) {
444 if (mBuffers[i].clientBuffer == nullptr
445 && mBuffers[i].compBuffer.expired()) {
446 mBuffers[i].clientBuffer = buffer;
447 return i;
448 }
449 }
450 mBuffers.push_back({ buffer, std::weak_ptr<C2Buffer>() });
451 return mBuffers.size() - 1;
452 }
453
454 /**
455 * Release the slot from the client, and get the C2Buffer object back from
456 * the previously assigned buffer. Note that the slot is not completely free
457 * until the returned C2Buffer object is freed.
458 *
459 * \param buffer[in] the buffer previously assigned a slot.
460 * \param c2buffer[in,out] pointer to C2Buffer to be populated. Ignored
461 * if null.
462 * \return true if the buffer is successfully released from a slot
463 * false otherwise
464 */
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800465 bool releaseSlot(
466 const sp<MediaCodecBuffer> &buffer,
467 std::shared_ptr<C2Buffer> *c2buffer,
468 bool release) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800469 sp<Codec2Buffer> clientBuffer;
470 size_t index = mBuffers.size();
471 for (size_t i = 0; i < mBuffers.size(); ++i) {
472 if (mBuffers[i].clientBuffer == buffer) {
473 clientBuffer = mBuffers[i].clientBuffer;
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800474 if (release) {
475 mBuffers[i].clientBuffer.clear();
476 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800477 index = i;
478 break;
479 }
480 }
481 if (clientBuffer == nullptr) {
482 ALOGV("[%s] %s: No matching buffer found", mName, __func__);
483 return false;
484 }
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800485 std::shared_ptr<C2Buffer> result = mBuffers[index].compBuffer.lock();
486 if (!result) {
487 result = clientBuffer->asC2Buffer();
488 mBuffers[index].compBuffer = result;
489 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800490 if (c2buffer) {
491 *c2buffer = result;
492 }
493 return true;
494 }
495
496 bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer) {
497 for (size_t i = 0; i < mBuffers.size(); ++i) {
498 std::shared_ptr<C2Buffer> compBuffer =
499 mBuffers[i].compBuffer.lock();
500 if (!compBuffer || compBuffer != c2buffer) {
501 continue;
502 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800503 mBuffers[i].compBuffer.reset();
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800504 ALOGV("[%s] codec released buffer #%zu", mName, i);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800505 return true;
506 }
507 ALOGV("[%s] codec released an unknown buffer", mName);
508 return false;
509 }
510
511 void flush() {
512 ALOGV("[%s] buffers are flushed %zu", mName, mBuffers.size());
513 mBuffers.clear();
514 }
515
Wonsik Kimab34ed62019-01-31 15:28:46 -0800516 size_t numClientBuffers() const {
517 return std::count_if(
518 mBuffers.begin(), mBuffers.end(),
519 [](const Entry &entry) {
520 return (entry.clientBuffer != nullptr);
521 });
522 }
523
Pawin Vongmasa36653902018-11-15 00:10:25 -0800524private:
525 friend class BuffersArrayImpl;
526
527 std::string mImplName; ///< name for debugging
528 const char *mName; ///< C-string version of name
529
530 struct Entry {
531 sp<Codec2Buffer> clientBuffer;
532 std::weak_ptr<C2Buffer> compBuffer;
533 };
534 std::vector<Entry> mBuffers;
535};
536
537/**
538 * Static buffer slots implementation based on a fixed-size array.
539 */
540class BuffersArrayImpl {
541public:
542 BuffersArrayImpl()
543 : mImplName("BuffersArrayImpl"),
544 mName(mImplName.c_str()) { }
545
546 /**
547 * Initialize buffer array from the original |impl|. The buffers known by
548 * the client is preserved, and the empty slots are populated so that the
549 * array size is at least |minSize|.
550 *
551 * \param impl[in] FlexBuffersImpl object used so far.
552 * \param minSize[in] minimum size of the buffer array.
553 * \param allocate[in] function to allocate a client buffer for an empty slot.
554 */
555 void initialize(
556 const FlexBuffersImpl &impl,
557 size_t minSize,
558 std::function<sp<Codec2Buffer>()> allocate) {
559 mImplName = impl.mImplName + "[N]";
560 mName = mImplName.c_str();
561 for (size_t i = 0; i < impl.mBuffers.size(); ++i) {
562 sp<Codec2Buffer> clientBuffer = impl.mBuffers[i].clientBuffer;
563 bool ownedByClient = (clientBuffer != nullptr);
564 if (!ownedByClient) {
565 clientBuffer = allocate();
566 }
567 mBuffers.push_back({ clientBuffer, impl.mBuffers[i].compBuffer, ownedByClient });
568 }
569 ALOGV("[%s] converted %zu buffers to array mode of %zu", mName, mBuffers.size(), minSize);
570 for (size_t i = impl.mBuffers.size(); i < minSize; ++i) {
571 mBuffers.push_back({ allocate(), std::weak_ptr<C2Buffer>(), false });
572 }
573 }
574
575 /**
576 * Grab a buffer from the underlying array which matches the criteria.
577 *
578 * \param index[out] index of the slot.
579 * \param buffer[out] the matching buffer.
580 * \param match[in] a function to test whether the buffer matches the
581 * criteria or not.
582 * \return OK if successful,
583 * WOULD_BLOCK if slots are being used,
584 * NO_MEMORY if no slot matches the criteria, even though it's
585 * available
586 */
587 status_t grabBuffer(
588 size_t *index,
589 sp<Codec2Buffer> *buffer,
590 std::function<bool(const sp<Codec2Buffer> &)> match =
591 [](const sp<Codec2Buffer> &) { return true; }) {
592 // allBuffersDontMatch remains true if all buffers are available but
593 // match() returns false for every buffer.
594 bool allBuffersDontMatch = true;
595 for (size_t i = 0; i < mBuffers.size(); ++i) {
596 if (!mBuffers[i].ownedByClient && mBuffers[i].compBuffer.expired()) {
597 if (match(mBuffers[i].clientBuffer)) {
598 mBuffers[i].ownedByClient = true;
599 *buffer = mBuffers[i].clientBuffer;
600 (*buffer)->meta()->clear();
601 (*buffer)->setRange(0, (*buffer)->capacity());
602 *index = i;
603 return OK;
604 }
605 } else {
606 allBuffersDontMatch = false;
607 }
608 }
609 return allBuffersDontMatch ? NO_MEMORY : WOULD_BLOCK;
610 }
611
612 /**
613 * Return the buffer from the client, and get the C2Buffer object back from
614 * the buffer. Note that the slot is not completely free until the returned
615 * C2Buffer object is freed.
616 *
617 * \param buffer[in] the buffer previously grabbed.
618 * \param c2buffer[in,out] pointer to C2Buffer to be populated. Ignored
619 * if null.
620 * \return true if the buffer is successfully returned
621 * false otherwise
622 */
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800623 bool returnBuffer(
624 const sp<MediaCodecBuffer> &buffer,
625 std::shared_ptr<C2Buffer> *c2buffer,
626 bool release) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800627 sp<Codec2Buffer> clientBuffer;
628 size_t index = mBuffers.size();
629 for (size_t i = 0; i < mBuffers.size(); ++i) {
630 if (mBuffers[i].clientBuffer == buffer) {
631 if (!mBuffers[i].ownedByClient) {
632 ALOGD("[%s] Client returned a buffer it does not own according to our record: %zu", mName, i);
633 }
634 clientBuffer = mBuffers[i].clientBuffer;
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800635 if (release) {
636 mBuffers[i].ownedByClient = false;
637 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800638 index = i;
639 break;
640 }
641 }
642 if (clientBuffer == nullptr) {
643 ALOGV("[%s] %s: No matching buffer found", mName, __func__);
644 return false;
645 }
646 ALOGV("[%s] %s: matching buffer found (index=%zu)", mName, __func__, index);
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800647 std::shared_ptr<C2Buffer> result = mBuffers[index].compBuffer.lock();
648 if (!result) {
649 result = clientBuffer->asC2Buffer();
650 mBuffers[index].compBuffer = result;
651 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800652 if (c2buffer) {
653 *c2buffer = result;
654 }
655 return true;
656 }
657
658 bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer) {
659 for (size_t i = 0; i < mBuffers.size(); ++i) {
660 std::shared_ptr<C2Buffer> compBuffer =
661 mBuffers[i].compBuffer.lock();
662 if (!compBuffer) {
663 continue;
664 }
665 if (c2buffer == compBuffer) {
666 if (mBuffers[i].ownedByClient) {
667 // This should not happen.
668 ALOGD("[%s] codec released a buffer owned by client "
669 "(index %zu)", mName, i);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800670 }
671 mBuffers[i].compBuffer.reset();
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800672 ALOGV("[%s] codec released buffer #%zu(array mode)", mName, i);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800673 return true;
674 }
675 }
676 ALOGV("[%s] codec released an unknown buffer (array mode)", mName);
677 return false;
678 }
679
680 /**
681 * Populate |array| with the underlying buffer array.
682 *
683 * \param array[out] an array to be filled with the underlying buffer array.
684 */
685 void getArray(Vector<sp<MediaCodecBuffer>> *array) const {
686 array->clear();
687 for (const Entry &entry : mBuffers) {
688 array->push(entry.clientBuffer);
689 }
690 }
691
692 /**
693 * The client abandoned all known buffers, so reclaim the ownership.
694 */
695 void flush() {
696 for (Entry &entry : mBuffers) {
697 entry.ownedByClient = false;
698 }
699 }
700
701 void realloc(std::function<sp<Codec2Buffer>()> alloc) {
702 size_t size = mBuffers.size();
703 mBuffers.clear();
704 for (size_t i = 0; i < size; ++i) {
705 mBuffers.push_back({ alloc(), std::weak_ptr<C2Buffer>(), false });
706 }
707 }
708
Wonsik Kimab34ed62019-01-31 15:28:46 -0800709 size_t numClientBuffers() const {
710 return std::count_if(
711 mBuffers.begin(), mBuffers.end(),
712 [](const Entry &entry) {
713 return entry.ownedByClient;
714 });
715 }
716
Pawin Vongmasa36653902018-11-15 00:10:25 -0800717private:
718 std::string mImplName; ///< name for debugging
719 const char *mName; ///< C-string version of name
720
721 struct Entry {
722 const sp<Codec2Buffer> clientBuffer;
723 std::weak_ptr<C2Buffer> compBuffer;
724 bool ownedByClient;
725 };
726 std::vector<Entry> mBuffers;
727};
728
729class InputBuffersArray : public CCodecBufferChannel::InputBuffers {
730public:
731 InputBuffersArray(const char *componentName, const char *name = "Input[N]")
732 : InputBuffers(componentName, name) { }
733 ~InputBuffersArray() override = default;
734
735 void initialize(
736 const FlexBuffersImpl &impl,
737 size_t minSize,
738 std::function<sp<Codec2Buffer>()> allocate) {
739 mImpl.initialize(impl, minSize, allocate);
740 }
741
742 bool isArrayMode() const final { return true; }
743
744 std::unique_ptr<CCodecBufferChannel::InputBuffers> toArrayMode(
745 size_t) final {
746 return nullptr;
747 }
748
749 void getArray(Vector<sp<MediaCodecBuffer>> *array) const final {
750 mImpl.getArray(array);
751 }
752
753 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override {
754 sp<Codec2Buffer> c2Buffer;
755 status_t err = mImpl.grabBuffer(index, &c2Buffer);
756 if (err == OK) {
757 c2Buffer->setFormat(mFormat);
758 *buffer = c2Buffer;
759 return true;
760 }
761 return false;
762 }
763
764 bool releaseBuffer(
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800765 const sp<MediaCodecBuffer> &buffer,
766 std::shared_ptr<C2Buffer> *c2buffer,
767 bool release) override {
768 return mImpl.returnBuffer(buffer, c2buffer, release);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800769 }
770
771 bool expireComponentBuffer(
772 const std::shared_ptr<C2Buffer> &c2buffer) override {
773 return mImpl.expireComponentBuffer(c2buffer);
774 }
775
776 void flush() override {
777 mImpl.flush();
778 }
779
Wonsik Kimab34ed62019-01-31 15:28:46 -0800780 size_t numClientBuffers() const final {
781 return mImpl.numClientBuffers();
782 }
783
Pawin Vongmasa36653902018-11-15 00:10:25 -0800784private:
785 BuffersArrayImpl mImpl;
786};
787
788class LinearInputBuffers : public CCodecBufferChannel::InputBuffers {
789public:
790 LinearInputBuffers(const char *componentName, const char *name = "1D-Input")
791 : InputBuffers(componentName, name),
792 mImpl(mName) { }
793
794 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override {
795 int32_t capacity = kLinearBufferSize;
796 (void)mFormat->findInt32(KEY_MAX_INPUT_SIZE, &capacity);
797 if ((size_t)capacity > kMaxLinearBufferSize) {
798 ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize);
799 capacity = kMaxLinearBufferSize;
800 }
801 // TODO: proper max input size
802 // TODO: read usage from intf
803 sp<Codec2Buffer> newBuffer = alloc((size_t)capacity);
804 if (newBuffer == nullptr) {
805 return false;
806 }
807 *index = mImpl.assignSlot(newBuffer);
808 *buffer = newBuffer;
809 return true;
810 }
811
812 bool releaseBuffer(
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800813 const sp<MediaCodecBuffer> &buffer,
814 std::shared_ptr<C2Buffer> *c2buffer,
815 bool release) override {
816 return mImpl.releaseSlot(buffer, c2buffer, release);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800817 }
818
819 bool expireComponentBuffer(
820 const std::shared_ptr<C2Buffer> &c2buffer) override {
821 return mImpl.expireComponentBuffer(c2buffer);
822 }
823
824 void flush() override {
825 // This is no-op by default unless we're in array mode where we need to keep
826 // track of the flushed work.
827 mImpl.flush();
828 }
829
830 std::unique_ptr<CCodecBufferChannel::InputBuffers> toArrayMode(
831 size_t size) final {
832 int32_t capacity = kLinearBufferSize;
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800833 (void)mFormat->findInt32(KEY_MAX_INPUT_SIZE, &capacity);
834 if ((size_t)capacity > kMaxLinearBufferSize) {
835 ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize);
836 capacity = kMaxLinearBufferSize;
837 }
838 // TODO: proper max input size
839 // TODO: read usage from intf
Pawin Vongmasa36653902018-11-15 00:10:25 -0800840 std::unique_ptr<InputBuffersArray> array(
841 new InputBuffersArray(mComponentName.c_str(), "1D-Input[N]"));
842 array->setPool(mPool);
843 array->setFormat(mFormat);
844 array->initialize(
845 mImpl,
846 size,
847 [this, capacity] () -> sp<Codec2Buffer> { return alloc(capacity); });
848 return std::move(array);
849 }
850
Wonsik Kimab34ed62019-01-31 15:28:46 -0800851 size_t numClientBuffers() const final {
852 return mImpl.numClientBuffers();
853 }
854
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800855 virtual sp<Codec2Buffer> alloc(size_t size) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800856 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
857 std::shared_ptr<C2LinearBlock> block;
858
859 c2_status_t err = mPool->fetchLinearBlock(size, usage, &block);
860 if (err != C2_OK) {
861 return nullptr;
862 }
863
864 return LinearBlockBuffer::Allocate(mFormat, block);
865 }
866
867private:
868 FlexBuffersImpl mImpl;
869};
870
871class EncryptedLinearInputBuffers : public LinearInputBuffers {
872public:
873 EncryptedLinearInputBuffers(
874 bool secure,
875 const sp<MemoryDealer> &dealer,
876 const sp<ICrypto> &crypto,
877 int32_t heapSeqNum,
878 size_t capacity,
Wonsik Kim078b58e2019-01-09 15:08:06 -0800879 size_t numInputSlots,
Pawin Vongmasa36653902018-11-15 00:10:25 -0800880 const char *componentName, const char *name = "EncryptedInput")
881 : LinearInputBuffers(componentName, name),
882 mUsage({0, 0}),
883 mDealer(dealer),
884 mCrypto(crypto),
885 mHeapSeqNum(heapSeqNum) {
886 if (secure) {
887 mUsage = { C2MemoryUsage::READ_PROTECTED, 0 };
888 } else {
889 mUsage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
890 }
Wonsik Kim078b58e2019-01-09 15:08:06 -0800891 for (size_t i = 0; i < numInputSlots; ++i) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800892 sp<IMemory> memory = mDealer->allocate(capacity);
893 if (memory == nullptr) {
894 ALOGD("[%s] Failed to allocate memory from dealer: only %zu slots allocated", mName, i);
895 break;
896 }
897 mMemoryVector.push_back({std::weak_ptr<C2LinearBlock>(), memory});
898 }
899 }
900
901 ~EncryptedLinearInputBuffers() override {
902 }
903
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800904 sp<Codec2Buffer> alloc(size_t size) override {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800905 sp<IMemory> memory;
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800906 size_t slot = 0;
907 for (; slot < mMemoryVector.size(); ++slot) {
908 if (mMemoryVector[slot].block.expired()) {
909 memory = mMemoryVector[slot].memory;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800910 break;
911 }
912 }
913 if (memory == nullptr) {
914 return nullptr;
915 }
916
917 std::shared_ptr<C2LinearBlock> block;
918 c2_status_t err = mPool->fetchLinearBlock(size, mUsage, &block);
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800919 if (err != C2_OK || block == nullptr) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800920 return nullptr;
921 }
922
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800923 mMemoryVector[slot].block = block;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800924 return new EncryptedLinearBlockBuffer(mFormat, block, memory, mHeapSeqNum);
925 }
926
927private:
928 C2MemoryUsage mUsage;
929 sp<MemoryDealer> mDealer;
930 sp<ICrypto> mCrypto;
931 int32_t mHeapSeqNum;
932 struct Entry {
933 std::weak_ptr<C2LinearBlock> block;
934 sp<IMemory> memory;
935 };
936 std::vector<Entry> mMemoryVector;
937};
938
939class GraphicMetadataInputBuffers : public CCodecBufferChannel::InputBuffers {
940public:
941 GraphicMetadataInputBuffers(const char *componentName, const char *name = "2D-MetaInput")
942 : InputBuffers(componentName, name),
943 mImpl(mName),
944 mStore(GetCodec2PlatformAllocatorStore()) { }
945 ~GraphicMetadataInputBuffers() override = default;
946
947 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override {
948 std::shared_ptr<C2Allocator> alloc;
949 c2_status_t err = mStore->fetchAllocator(mPool->getAllocatorId(), &alloc);
950 if (err != C2_OK) {
951 return false;
952 }
953 sp<GraphicMetadataBuffer> newBuffer = new GraphicMetadataBuffer(mFormat, alloc);
954 if (newBuffer == nullptr) {
955 return false;
956 }
957 *index = mImpl.assignSlot(newBuffer);
958 *buffer = newBuffer;
959 return true;
960 }
961
962 bool releaseBuffer(
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800963 const sp<MediaCodecBuffer> &buffer,
964 std::shared_ptr<C2Buffer> *c2buffer,
965 bool release) override {
966 return mImpl.releaseSlot(buffer, c2buffer, release);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800967 }
968
969 bool expireComponentBuffer(
970 const std::shared_ptr<C2Buffer> &c2buffer) override {
971 return mImpl.expireComponentBuffer(c2buffer);
972 }
973
974 void flush() override {
975 // This is no-op by default unless we're in array mode where we need to keep
976 // track of the flushed work.
977 }
978
979 std::unique_ptr<CCodecBufferChannel::InputBuffers> toArrayMode(
980 size_t size) final {
981 std::shared_ptr<C2Allocator> alloc;
982 c2_status_t err = mStore->fetchAllocator(mPool->getAllocatorId(), &alloc);
983 if (err != C2_OK) {
984 return nullptr;
985 }
986 std::unique_ptr<InputBuffersArray> array(
987 new InputBuffersArray(mComponentName.c_str(), "2D-MetaInput[N]"));
988 array->setPool(mPool);
989 array->setFormat(mFormat);
990 array->initialize(
991 mImpl,
992 size,
993 [format = mFormat, alloc]() -> sp<Codec2Buffer> {
994 return new GraphicMetadataBuffer(format, alloc);
995 });
996 return std::move(array);
997 }
998
Wonsik Kimab34ed62019-01-31 15:28:46 -0800999 size_t numClientBuffers() const final {
1000 return mImpl.numClientBuffers();
1001 }
1002
Pawin Vongmasa36653902018-11-15 00:10:25 -08001003private:
1004 FlexBuffersImpl mImpl;
1005 std::shared_ptr<C2AllocatorStore> mStore;
1006};
1007
1008class GraphicInputBuffers : public CCodecBufferChannel::InputBuffers {
1009public:
Wonsik Kim078b58e2019-01-09 15:08:06 -08001010 GraphicInputBuffers(
1011 size_t numInputSlots, const char *componentName, const char *name = "2D-BB-Input")
Pawin Vongmasa36653902018-11-15 00:10:25 -08001012 : InputBuffers(componentName, name),
1013 mImpl(mName),
1014 mLocalBufferPool(LocalBufferPool::Create(
Wonsik Kim078b58e2019-01-09 15:08:06 -08001015 kMaxLinearBufferSize * numInputSlots)) { }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001016 ~GraphicInputBuffers() override = default;
1017
1018 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override {
1019 // TODO: proper max input size
1020 // TODO: read usage from intf
1021 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
1022 sp<GraphicBlockBuffer> newBuffer = AllocateGraphicBuffer(
1023 mPool, mFormat, HAL_PIXEL_FORMAT_YV12, usage, mLocalBufferPool);
1024 if (newBuffer == nullptr) {
1025 return false;
1026 }
1027 *index = mImpl.assignSlot(newBuffer);
1028 *buffer = newBuffer;
1029 return true;
1030 }
1031
1032 bool releaseBuffer(
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001033 const sp<MediaCodecBuffer> &buffer,
1034 std::shared_ptr<C2Buffer> *c2buffer,
1035 bool release) override {
1036 return mImpl.releaseSlot(buffer, c2buffer, release);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001037 }
1038
1039 bool expireComponentBuffer(
1040 const std::shared_ptr<C2Buffer> &c2buffer) override {
1041 return mImpl.expireComponentBuffer(c2buffer);
1042 }
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001043
Pawin Vongmasa36653902018-11-15 00:10:25 -08001044 void flush() override {
1045 // This is no-op by default unless we're in array mode where we need to keep
1046 // track of the flushed work.
1047 }
1048
1049 std::unique_ptr<CCodecBufferChannel::InputBuffers> toArrayMode(
1050 size_t size) final {
1051 std::unique_ptr<InputBuffersArray> array(
1052 new InputBuffersArray(mComponentName.c_str(), "2D-BB-Input[N]"));
1053 array->setPool(mPool);
1054 array->setFormat(mFormat);
1055 array->initialize(
1056 mImpl,
1057 size,
1058 [pool = mPool, format = mFormat, lbp = mLocalBufferPool]() -> sp<Codec2Buffer> {
1059 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
1060 return AllocateGraphicBuffer(
1061 pool, format, HAL_PIXEL_FORMAT_YV12, usage, lbp);
1062 });
1063 return std::move(array);
1064 }
1065
Wonsik Kimab34ed62019-01-31 15:28:46 -08001066 size_t numClientBuffers() const final {
1067 return mImpl.numClientBuffers();
1068 }
1069
Pawin Vongmasa36653902018-11-15 00:10:25 -08001070private:
1071 FlexBuffersImpl mImpl;
1072 std::shared_ptr<LocalBufferPool> mLocalBufferPool;
1073};
1074
1075class DummyInputBuffers : public CCodecBufferChannel::InputBuffers {
1076public:
1077 DummyInputBuffers(const char *componentName, const char *name = "2D-Input")
1078 : InputBuffers(componentName, name) { }
1079
1080 bool requestNewBuffer(size_t *, sp<MediaCodecBuffer> *) override {
1081 return false;
1082 }
1083
1084 bool releaseBuffer(
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001085 const sp<MediaCodecBuffer> &, std::shared_ptr<C2Buffer> *, bool) override {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001086 return false;
1087 }
1088
1089 bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &) override {
1090 return false;
1091 }
1092 void flush() override {
1093 }
1094
1095 std::unique_ptr<CCodecBufferChannel::InputBuffers> toArrayMode(
1096 size_t) final {
1097 return nullptr;
1098 }
1099
1100 bool isArrayMode() const final { return true; }
1101
1102 void getArray(Vector<sp<MediaCodecBuffer>> *array) const final {
1103 array->clear();
1104 }
Wonsik Kimab34ed62019-01-31 15:28:46 -08001105
1106 size_t numClientBuffers() const final {
1107 return 0u;
1108 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001109};
1110
1111class OutputBuffersArray : public CCodecBufferChannel::OutputBuffers {
1112public:
1113 OutputBuffersArray(const char *componentName, const char *name = "Output[N]")
1114 : OutputBuffers(componentName, name) { }
1115 ~OutputBuffersArray() override = default;
1116
1117 void initialize(
1118 const FlexBuffersImpl &impl,
1119 size_t minSize,
1120 std::function<sp<Codec2Buffer>()> allocate) {
1121 mImpl.initialize(impl, minSize, allocate);
1122 }
1123
1124 bool isArrayMode() const final { return true; }
1125
1126 std::unique_ptr<CCodecBufferChannel::OutputBuffers> toArrayMode(
1127 size_t) final {
1128 return nullptr;
1129 }
1130
1131 status_t registerBuffer(
1132 const std::shared_ptr<C2Buffer> &buffer,
1133 size_t *index,
1134 sp<MediaCodecBuffer> *clientBuffer) final {
1135 sp<Codec2Buffer> c2Buffer;
1136 status_t err = mImpl.grabBuffer(
1137 index,
1138 &c2Buffer,
1139 [buffer](const sp<Codec2Buffer> &clientBuffer) {
1140 return clientBuffer->canCopy(buffer);
1141 });
1142 if (err == WOULD_BLOCK) {
1143 ALOGV("[%s] buffers temporarily not available", mName);
1144 return err;
1145 } else if (err != OK) {
1146 ALOGD("[%s] grabBuffer failed: %d", mName, err);
1147 return err;
1148 }
1149 c2Buffer->setFormat(mFormat);
1150 if (!c2Buffer->copy(buffer)) {
1151 ALOGD("[%s] copy buffer failed", mName);
1152 return WOULD_BLOCK;
1153 }
1154 submit(c2Buffer);
1155 *clientBuffer = c2Buffer;
1156 ALOGV("[%s] grabbed buffer %zu", mName, *index);
1157 return OK;
1158 }
1159
1160 status_t registerCsd(
1161 const C2StreamCsdInfo::output *csd,
1162 size_t *index,
1163 sp<MediaCodecBuffer> *clientBuffer) final {
1164 sp<Codec2Buffer> c2Buffer;
1165 status_t err = mImpl.grabBuffer(
1166 index,
1167 &c2Buffer,
1168 [csd](const sp<Codec2Buffer> &clientBuffer) {
1169 return clientBuffer->base() != nullptr
1170 && clientBuffer->capacity() >= csd->flexCount();
1171 });
1172 if (err != OK) {
1173 return err;
1174 }
1175 memcpy(c2Buffer->base(), csd->m.value, csd->flexCount());
1176 c2Buffer->setRange(0, csd->flexCount());
1177 c2Buffer->setFormat(mFormat);
1178 *clientBuffer = c2Buffer;
1179 return OK;
1180 }
1181
1182 bool releaseBuffer(
1183 const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) override {
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001184 return mImpl.returnBuffer(buffer, c2buffer, true);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001185 }
1186
1187 void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) override {
1188 (void)flushedWork;
1189 mImpl.flush();
1190 if (mSkipCutBuffer != nullptr) {
1191 mSkipCutBuffer->clear();
1192 }
1193 }
1194
1195 void getArray(Vector<sp<MediaCodecBuffer>> *array) const final {
1196 mImpl.getArray(array);
1197 }
1198
1199 void realloc(const std::shared_ptr<C2Buffer> &c2buffer) {
1200 std::function<sp<Codec2Buffer>()> alloc;
1201 switch (c2buffer->data().type()) {
1202 case C2BufferData::LINEAR: {
1203 uint32_t size = kLinearBufferSize;
1204 const C2ConstLinearBlock &block = c2buffer->data().linearBlocks().front();
1205 if (block.size() < kMaxLinearBufferSize / 2) {
1206 size = block.size() * 2;
1207 } else {
1208 size = kMaxLinearBufferSize;
1209 }
1210 alloc = [format = mFormat, size] {
1211 return new LocalLinearBuffer(format, new ABuffer(size));
1212 };
1213 break;
1214 }
1215
1216 // TODO: add support
1217 case C2BufferData::GRAPHIC: FALLTHROUGH_INTENDED;
1218
1219 case C2BufferData::INVALID: FALLTHROUGH_INTENDED;
1220 case C2BufferData::LINEAR_CHUNKS: FALLTHROUGH_INTENDED;
1221 case C2BufferData::GRAPHIC_CHUNKS: FALLTHROUGH_INTENDED;
1222 default:
1223 ALOGD("Unsupported type: %d", (int)c2buffer->data().type());
1224 return;
1225 }
1226 mImpl.realloc(alloc);
1227 }
1228
Wonsik Kimdf5dd142019-02-06 10:15:46 -08001229 size_t numClientBuffers() const final {
1230 return mImpl.numClientBuffers();
1231 }
1232
Pawin Vongmasa36653902018-11-15 00:10:25 -08001233private:
1234 BuffersArrayImpl mImpl;
1235};
1236
1237class FlexOutputBuffers : public CCodecBufferChannel::OutputBuffers {
1238public:
1239 FlexOutputBuffers(const char *componentName, const char *name = "Output[]")
1240 : OutputBuffers(componentName, name),
1241 mImpl(mName) { }
1242
1243 status_t registerBuffer(
1244 const std::shared_ptr<C2Buffer> &buffer,
1245 size_t *index,
1246 sp<MediaCodecBuffer> *clientBuffer) override {
1247 sp<Codec2Buffer> newBuffer = wrap(buffer);
Wonsik Kim186fdbf2019-01-29 13:30:01 -08001248 if (newBuffer == nullptr) {
1249 return NO_MEMORY;
1250 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001251 newBuffer->setFormat(mFormat);
1252 *index = mImpl.assignSlot(newBuffer);
1253 *clientBuffer = newBuffer;
1254 ALOGV("[%s] registered buffer %zu", mName, *index);
1255 return OK;
1256 }
1257
1258 status_t registerCsd(
1259 const C2StreamCsdInfo::output *csd,
1260 size_t *index,
1261 sp<MediaCodecBuffer> *clientBuffer) final {
1262 sp<Codec2Buffer> newBuffer = new LocalLinearBuffer(
1263 mFormat, ABuffer::CreateAsCopy(csd->m.value, csd->flexCount()));
1264 *index = mImpl.assignSlot(newBuffer);
1265 *clientBuffer = newBuffer;
1266 return OK;
1267 }
1268
1269 bool releaseBuffer(
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001270 const sp<MediaCodecBuffer> &buffer,
1271 std::shared_ptr<C2Buffer> *c2buffer) override {
1272 return mImpl.releaseSlot(buffer, c2buffer, true);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001273 }
1274
1275 void flush(
1276 const std::list<std::unique_ptr<C2Work>> &flushedWork) override {
1277 (void) flushedWork;
1278 // This is no-op by default unless we're in array mode where we need to keep
1279 // track of the flushed work.
1280 }
1281
1282 std::unique_ptr<CCodecBufferChannel::OutputBuffers> toArrayMode(
1283 size_t size) override {
1284 std::unique_ptr<OutputBuffersArray> array(new OutputBuffersArray(mComponentName.c_str()));
1285 array->setFormat(mFormat);
1286 array->transferSkipCutBuffer(mSkipCutBuffer);
1287 array->initialize(
1288 mImpl,
1289 size,
1290 [this]() { return allocateArrayBuffer(); });
1291 return std::move(array);
1292 }
1293
Wonsik Kimdf5dd142019-02-06 10:15:46 -08001294 size_t numClientBuffers() const final {
1295 return mImpl.numClientBuffers();
1296 }
1297
Pawin Vongmasa36653902018-11-15 00:10:25 -08001298 /**
1299 * Return an appropriate Codec2Buffer object for the type of buffers.
1300 *
1301 * \param buffer C2Buffer object to wrap.
1302 *
1303 * \return appropriate Codec2Buffer object to wrap |buffer|.
1304 */
1305 virtual sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) = 0;
1306
1307 /**
1308 * Return an appropriate Codec2Buffer object for the type of buffers, to be
1309 * used as an empty array buffer.
1310 *
1311 * \return appropriate Codec2Buffer object which can copy() from C2Buffers.
1312 */
1313 virtual sp<Codec2Buffer> allocateArrayBuffer() = 0;
1314
1315private:
1316 FlexBuffersImpl mImpl;
1317};
1318
1319class LinearOutputBuffers : public FlexOutputBuffers {
1320public:
1321 LinearOutputBuffers(const char *componentName, const char *name = "1D-Output")
1322 : FlexOutputBuffers(componentName, name) { }
1323
1324 void flush(
1325 const std::list<std::unique_ptr<C2Work>> &flushedWork) override {
1326 if (mSkipCutBuffer != nullptr) {
1327 mSkipCutBuffer->clear();
1328 }
1329 FlexOutputBuffers::flush(flushedWork);
1330 }
1331
1332 sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override {
1333 if (buffer == nullptr) {
1334 ALOGV("[%s] using a dummy buffer", mName);
1335 return new LocalLinearBuffer(mFormat, new ABuffer(0));
1336 }
1337 if (buffer->data().type() != C2BufferData::LINEAR) {
1338 ALOGV("[%s] non-linear buffer %d", mName, buffer->data().type());
1339 // We expect linear output buffers from the component.
1340 return nullptr;
1341 }
1342 if (buffer->data().linearBlocks().size() != 1u) {
1343 ALOGV("[%s] no linear buffers", mName);
1344 // We expect one and only one linear block from the component.
1345 return nullptr;
1346 }
1347 sp<Codec2Buffer> clientBuffer = ConstLinearBlockBuffer::Allocate(mFormat, buffer);
Wonsik Kim186fdbf2019-01-29 13:30:01 -08001348 if (clientBuffer == nullptr) {
1349 ALOGD("[%s] ConstLinearBlockBuffer::Allocate failed", mName);
1350 return nullptr;
1351 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001352 submit(clientBuffer);
1353 return clientBuffer;
1354 }
1355
1356 sp<Codec2Buffer> allocateArrayBuffer() override {
1357 // TODO: proper max output size
1358 return new LocalLinearBuffer(mFormat, new ABuffer(kLinearBufferSize));
1359 }
1360};
1361
1362class GraphicOutputBuffers : public FlexOutputBuffers {
1363public:
1364 GraphicOutputBuffers(const char *componentName, const char *name = "2D-Output")
1365 : FlexOutputBuffers(componentName, name) { }
1366
1367 sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override {
1368 return new DummyContainerBuffer(mFormat, buffer);
1369 }
1370
1371 sp<Codec2Buffer> allocateArrayBuffer() override {
1372 return new DummyContainerBuffer(mFormat);
1373 }
1374};
1375
1376class RawGraphicOutputBuffers : public FlexOutputBuffers {
1377public:
Wonsik Kim078b58e2019-01-09 15:08:06 -08001378 RawGraphicOutputBuffers(
1379 size_t numOutputSlots, const char *componentName, const char *name = "2D-BB-Output")
Pawin Vongmasa36653902018-11-15 00:10:25 -08001380 : FlexOutputBuffers(componentName, name),
1381 mLocalBufferPool(LocalBufferPool::Create(
Wonsik Kim078b58e2019-01-09 15:08:06 -08001382 kMaxLinearBufferSize * numOutputSlots)) { }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001383 ~RawGraphicOutputBuffers() override = default;
1384
1385 sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override {
1386 if (buffer == nullptr) {
1387 sp<Codec2Buffer> c2buffer = ConstGraphicBlockBuffer::AllocateEmpty(
1388 mFormat,
1389 [lbp = mLocalBufferPool](size_t capacity) {
1390 return lbp->newBuffer(capacity);
1391 });
Wonsik Kim186fdbf2019-01-29 13:30:01 -08001392 if (c2buffer == nullptr) {
1393 ALOGD("[%s] ConstGraphicBlockBuffer::AllocateEmpty failed", mName);
1394 return nullptr;
1395 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001396 c2buffer->setRange(0, 0);
1397 return c2buffer;
1398 } else {
1399 return ConstGraphicBlockBuffer::Allocate(
1400 mFormat,
1401 buffer,
1402 [lbp = mLocalBufferPool](size_t capacity) {
1403 return lbp->newBuffer(capacity);
1404 });
1405 }
1406 }
1407
1408 sp<Codec2Buffer> allocateArrayBuffer() override {
1409 return ConstGraphicBlockBuffer::AllocateEmpty(
1410 mFormat,
1411 [lbp = mLocalBufferPool](size_t capacity) {
1412 return lbp->newBuffer(capacity);
1413 });
1414 }
1415
1416private:
1417 std::shared_ptr<LocalBufferPool> mLocalBufferPool;
1418};
1419
1420} // namespace
1421
1422CCodecBufferChannel::QueueGuard::QueueGuard(
1423 CCodecBufferChannel::QueueSync &sync) : mSync(sync) {
1424 Mutex::Autolock l(mSync.mGuardLock);
1425 // At this point it's guaranteed that mSync is not under state transition,
1426 // as we are holding its mutex.
1427
1428 Mutexed<CCodecBufferChannel::QueueSync::Counter>::Locked count(mSync.mCount);
1429 if (count->value == -1) {
1430 mRunning = false;
1431 } else {
1432 ++count->value;
1433 mRunning = true;
1434 }
1435}
1436
1437CCodecBufferChannel::QueueGuard::~QueueGuard() {
1438 if (mRunning) {
1439 // We are not holding mGuardLock at this point so that QueueSync::stop() can
1440 // keep holding the lock until mCount reaches zero.
1441 Mutexed<CCodecBufferChannel::QueueSync::Counter>::Locked count(mSync.mCount);
1442 --count->value;
1443 count->cond.broadcast();
1444 }
1445}
1446
1447void CCodecBufferChannel::QueueSync::start() {
1448 Mutex::Autolock l(mGuardLock);
1449 // If stopped, it goes to running state; otherwise no-op.
1450 Mutexed<Counter>::Locked count(mCount);
1451 if (count->value == -1) {
1452 count->value = 0;
1453 }
1454}
1455
1456void CCodecBufferChannel::QueueSync::stop() {
1457 Mutex::Autolock l(mGuardLock);
1458 Mutexed<Counter>::Locked count(mCount);
1459 if (count->value == -1) {
1460 // no-op
1461 return;
1462 }
1463 // Holding mGuardLock here blocks creation of additional QueueGuard objects, so
1464 // mCount can only decrement. In other words, threads that acquired the lock
1465 // are allowed to finish execution but additional threads trying to acquire
1466 // the lock at this point will block, and then get QueueGuard at STOPPED
1467 // state.
1468 while (count->value != 0) {
1469 count.waitForCondition(count->cond);
1470 }
1471 count->value = -1;
1472}
1473
Pawin Vongmasa36653902018-11-15 00:10:25 -08001474// CCodecBufferChannel::ReorderStash
1475
1476CCodecBufferChannel::ReorderStash::ReorderStash() {
1477 clear();
1478}
1479
1480void CCodecBufferChannel::ReorderStash::clear() {
1481 mPending.clear();
1482 mStash.clear();
1483 mDepth = 0;
1484 mKey = C2Config::ORDINAL;
1485}
1486
Wonsik Kim6897f222019-01-30 13:29:24 -08001487void CCodecBufferChannel::ReorderStash::flush() {
1488 mPending.clear();
1489 mStash.clear();
1490}
1491
Pawin Vongmasa36653902018-11-15 00:10:25 -08001492void CCodecBufferChannel::ReorderStash::setDepth(uint32_t depth) {
1493 mPending.splice(mPending.end(), mStash);
1494 mDepth = depth;
1495}
1496void CCodecBufferChannel::ReorderStash::setKey(C2Config::ordinal_key_t key) {
1497 mPending.splice(mPending.end(), mStash);
1498 mKey = key;
1499}
1500
1501bool CCodecBufferChannel::ReorderStash::pop(Entry *entry) {
1502 if (mPending.empty()) {
1503 return false;
1504 }
1505 entry->buffer = mPending.front().buffer;
1506 entry->timestamp = mPending.front().timestamp;
1507 entry->flags = mPending.front().flags;
1508 entry->ordinal = mPending.front().ordinal;
1509 mPending.pop_front();
1510 return true;
1511}
1512
1513void CCodecBufferChannel::ReorderStash::emplace(
1514 const std::shared_ptr<C2Buffer> &buffer,
1515 int64_t timestamp,
1516 int32_t flags,
1517 const C2WorkOrdinalStruct &ordinal) {
Wonsik Kim38ad3412019-02-01 15:13:23 -08001518 auto it = mStash.begin();
1519 for (; it != mStash.end(); ++it) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001520 if (less(ordinal, it->ordinal)) {
Wonsik Kim38ad3412019-02-01 15:13:23 -08001521 break;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001522 }
1523 }
Wonsik Kim38ad3412019-02-01 15:13:23 -08001524 mStash.emplace(it, buffer, timestamp, flags, ordinal);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001525 while (!mStash.empty() && mStash.size() > mDepth) {
1526 mPending.push_back(mStash.front());
1527 mStash.pop_front();
1528 }
1529}
1530
1531void CCodecBufferChannel::ReorderStash::defer(
1532 const CCodecBufferChannel::ReorderStash::Entry &entry) {
1533 mPending.push_front(entry);
1534}
1535
1536bool CCodecBufferChannel::ReorderStash::hasPending() const {
1537 return !mPending.empty();
1538}
1539
1540bool CCodecBufferChannel::ReorderStash::less(
1541 const C2WorkOrdinalStruct &o1, const C2WorkOrdinalStruct &o2) {
1542 switch (mKey) {
1543 case C2Config::ORDINAL: return o1.frameIndex < o2.frameIndex;
1544 case C2Config::TIMESTAMP: return o1.timestamp < o2.timestamp;
1545 case C2Config::CUSTOM: return o1.customOrdinal < o2.customOrdinal;
1546 default:
1547 ALOGD("Unrecognized key; default to timestamp");
1548 return o1.frameIndex < o2.frameIndex;
1549 }
1550}
1551
1552// CCodecBufferChannel
1553
1554CCodecBufferChannel::CCodecBufferChannel(
1555 const std::shared_ptr<CCodecCallback> &callback)
1556 : mHeapSeqNum(-1),
1557 mCCodecCallback(callback),
Wonsik Kim078b58e2019-01-09 15:08:06 -08001558 mNumInputSlots(kSmoothnessFactor),
1559 mNumOutputSlots(kSmoothnessFactor),
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -08001560 mDelay(0),
Pawin Vongmasa36653902018-11-15 00:10:25 -08001561 mFrameIndex(0u),
1562 mFirstValidFrameIndex(0u),
1563 mMetaMode(MODE_NONE),
Pawin Vongmasa36653902018-11-15 00:10:25 -08001564 mInputMetEos(false) {
1565 Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
1566 buffers->reset(new DummyInputBuffers(""));
1567}
1568
1569CCodecBufferChannel::~CCodecBufferChannel() {
1570 if (mCrypto != nullptr && mDealer != nullptr && mHeapSeqNum >= 0) {
1571 mCrypto->unsetHeap(mHeapSeqNum);
1572 }
1573}
1574
1575void CCodecBufferChannel::setComponent(
1576 const std::shared_ptr<Codec2Client::Component> &component) {
1577 mComponent = component;
1578 mComponentName = component->getName() + StringPrintf("#%d", int(uintptr_t(component.get()) % 997));
1579 mName = mComponentName.c_str();
1580}
1581
1582status_t CCodecBufferChannel::setInputSurface(
1583 const std::shared_ptr<InputSurfaceWrapper> &surface) {
1584 ALOGV("[%s] setInputSurface", mName);
1585 mInputSurface = surface;
1586 return mInputSurface->connect(mComponent);
1587}
1588
1589status_t CCodecBufferChannel::signalEndOfInputStream() {
1590 if (mInputSurface == nullptr) {
1591 return INVALID_OPERATION;
1592 }
1593 return mInputSurface->signalEndOfInputStream();
1594}
1595
1596status_t CCodecBufferChannel::queueInputBufferInternal(const sp<MediaCodecBuffer> &buffer) {
1597 int64_t timeUs;
1598 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1599
1600 if (mInputMetEos) {
1601 ALOGD("[%s] buffers after EOS ignored (%lld us)", mName, (long long)timeUs);
1602 return OK;
1603 }
1604
1605 int32_t flags = 0;
1606 int32_t tmp = 0;
1607 bool eos = false;
1608 if (buffer->meta()->findInt32("eos", &tmp) && tmp) {
1609 eos = true;
1610 mInputMetEos = true;
1611 ALOGV("[%s] input EOS", mName);
1612 }
1613 if (buffer->meta()->findInt32("csd", &tmp) && tmp) {
1614 flags |= C2FrameData::FLAG_CODEC_CONFIG;
1615 }
1616 ALOGV("[%s] queueInputBuffer: buffer->size() = %zu", mName, buffer->size());
1617 std::unique_ptr<C2Work> work(new C2Work);
1618 work->input.ordinal.timestamp = timeUs;
1619 work->input.ordinal.frameIndex = mFrameIndex++;
1620 // WORKAROUND: until codecs support handling work after EOS and max output sizing, use timestamp
1621 // manipulation to achieve image encoding via video codec, and to constrain encoded output.
1622 // Keep client timestamp in customOrdinal
1623 work->input.ordinal.customOrdinal = timeUs;
1624 work->input.buffers.clear();
1625
Wonsik Kimab34ed62019-01-31 15:28:46 -08001626 uint64_t queuedFrameIndex = work->input.ordinal.frameIndex.peeku();
1627 std::vector<std::shared_ptr<C2Buffer>> queuedBuffers;
1628
Pawin Vongmasa36653902018-11-15 00:10:25 -08001629 if (buffer->size() > 0u) {
1630 Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
1631 std::shared_ptr<C2Buffer> c2buffer;
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001632 if (!(*buffers)->releaseBuffer(buffer, &c2buffer, false)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001633 return -ENOENT;
1634 }
1635 work->input.buffers.push_back(c2buffer);
Wonsik Kimab34ed62019-01-31 15:28:46 -08001636 queuedBuffers.push_back(c2buffer);
1637 } else if (eos) {
1638 flags |= C2FrameData::FLAG_END_OF_STREAM;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001639 }
1640 work->input.flags = (C2FrameData::flags_t)flags;
1641 // TODO: fill info's
1642
1643 work->input.configUpdate = std::move(mParamsToBeSet);
1644 work->worklets.clear();
1645 work->worklets.emplace_back(new C2Worklet);
1646
1647 std::list<std::unique_ptr<C2Work>> items;
1648 items.push_back(std::move(work));
Wonsik Kimab34ed62019-01-31 15:28:46 -08001649 mPipelineWatcher.lock()->onWorkQueued(
1650 queuedFrameIndex,
1651 std::move(queuedBuffers),
1652 PipelineWatcher::Clock::now());
Pawin Vongmasa36653902018-11-15 00:10:25 -08001653 c2_status_t err = mComponent->queue(&items);
Wonsik Kimab34ed62019-01-31 15:28:46 -08001654 if (err != C2_OK) {
1655 mPipelineWatcher.lock()->onWorkDone(queuedFrameIndex);
1656 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001657
1658 if (err == C2_OK && eos && buffer->size() > 0u) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001659 work.reset(new C2Work);
1660 work->input.ordinal.timestamp = timeUs;
1661 work->input.ordinal.frameIndex = mFrameIndex++;
1662 // WORKAROUND: keep client timestamp in customOrdinal
1663 work->input.ordinal.customOrdinal = timeUs;
1664 work->input.buffers.clear();
1665 work->input.flags = C2FrameData::FLAG_END_OF_STREAM;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001666 work->worklets.emplace_back(new C2Worklet);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001667
Wonsik Kimab34ed62019-01-31 15:28:46 -08001668 queuedFrameIndex = work->input.ordinal.frameIndex.peeku();
1669 queuedBuffers.clear();
1670
Pawin Vongmasa36653902018-11-15 00:10:25 -08001671 items.clear();
1672 items.push_back(std::move(work));
Wonsik Kimab34ed62019-01-31 15:28:46 -08001673
1674 mPipelineWatcher.lock()->onWorkQueued(
1675 queuedFrameIndex,
1676 std::move(queuedBuffers),
1677 PipelineWatcher::Clock::now());
Pawin Vongmasa36653902018-11-15 00:10:25 -08001678 err = mComponent->queue(&items);
Wonsik Kimab34ed62019-01-31 15:28:46 -08001679 if (err != C2_OK) {
1680 mPipelineWatcher.lock()->onWorkDone(queuedFrameIndex);
1681 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001682 }
1683 if (err == C2_OK) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001684 Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
1685 bool released = (*buffers)->releaseBuffer(buffer, nullptr, true);
1686 ALOGV("[%s] queueInputBuffer: buffer %sreleased", mName, released ? "" : "not ");
Pawin Vongmasa36653902018-11-15 00:10:25 -08001687 }
1688
1689 feedInputBufferIfAvailableInternal();
1690 return err;
1691}
1692
1693status_t CCodecBufferChannel::setParameters(std::vector<std::unique_ptr<C2Param>> &params) {
1694 QueueGuard guard(mSync);
1695 if (!guard.isRunning()) {
1696 ALOGD("[%s] setParameters is only supported in the running state.", mName);
1697 return -ENOSYS;
1698 }
1699 mParamsToBeSet.insert(mParamsToBeSet.end(),
1700 std::make_move_iterator(params.begin()),
1701 std::make_move_iterator(params.end()));
1702 params.clear();
1703 return OK;
1704}
1705
1706status_t CCodecBufferChannel::queueInputBuffer(const sp<MediaCodecBuffer> &buffer) {
1707 QueueGuard guard(mSync);
1708 if (!guard.isRunning()) {
1709 ALOGD("[%s] No more buffers should be queued at current state.", mName);
1710 return -ENOSYS;
1711 }
1712 return queueInputBufferInternal(buffer);
1713}
1714
1715status_t CCodecBufferChannel::queueSecureInputBuffer(
1716 const sp<MediaCodecBuffer> &buffer, bool secure, const uint8_t *key,
1717 const uint8_t *iv, CryptoPlugin::Mode mode, CryptoPlugin::Pattern pattern,
1718 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
1719 AString *errorDetailMsg) {
1720 QueueGuard guard(mSync);
1721 if (!guard.isRunning()) {
1722 ALOGD("[%s] No more buffers should be queued at current state.", mName);
1723 return -ENOSYS;
1724 }
1725
1726 if (!hasCryptoOrDescrambler()) {
1727 return -ENOSYS;
1728 }
1729 sp<EncryptedLinearBlockBuffer> encryptedBuffer((EncryptedLinearBlockBuffer *)buffer.get());
1730
1731 ssize_t result = -1;
1732 ssize_t codecDataOffset = 0;
1733 if (mCrypto != nullptr) {
1734 ICrypto::DestinationBuffer destination;
1735 if (secure) {
1736 destination.mType = ICrypto::kDestinationTypeNativeHandle;
1737 destination.mHandle = encryptedBuffer->handle();
1738 } else {
1739 destination.mType = ICrypto::kDestinationTypeSharedMemory;
1740 destination.mSharedMemory = mDecryptDestination;
1741 }
1742 ICrypto::SourceBuffer source;
1743 encryptedBuffer->fillSourceBuffer(&source);
1744 result = mCrypto->decrypt(
1745 key, iv, mode, pattern, source, buffer->offset(),
1746 subSamples, numSubSamples, destination, errorDetailMsg);
1747 if (result < 0) {
1748 return result;
1749 }
1750 if (destination.mType == ICrypto::kDestinationTypeSharedMemory) {
1751 encryptedBuffer->copyDecryptedContent(mDecryptDestination, result);
1752 }
1753 } else {
1754 // Here we cast CryptoPlugin::SubSample to hardware::cas::native::V1_0::SubSample
1755 // directly, the structure definitions should match as checked in DescramblerImpl.cpp.
1756 hidl_vec<SubSample> hidlSubSamples;
1757 hidlSubSamples.setToExternal((SubSample *)subSamples, numSubSamples, false /*own*/);
1758
1759 hardware::cas::native::V1_0::SharedBuffer srcBuffer;
1760 encryptedBuffer->fillSourceBuffer(&srcBuffer);
1761
1762 DestinationBuffer dstBuffer;
1763 if (secure) {
1764 dstBuffer.type = BufferType::NATIVE_HANDLE;
1765 dstBuffer.secureMemory = hidl_handle(encryptedBuffer->handle());
1766 } else {
1767 dstBuffer.type = BufferType::SHARED_MEMORY;
1768 dstBuffer.nonsecureMemory = srcBuffer;
1769 }
1770
1771 CasStatus status = CasStatus::OK;
1772 hidl_string detailedError;
1773 ScramblingControl sctrl = ScramblingControl::UNSCRAMBLED;
1774
1775 if (key != nullptr) {
1776 sctrl = (ScramblingControl)key[0];
1777 // Adjust for the PES offset
1778 codecDataOffset = key[2] | (key[3] << 8);
1779 }
1780
1781 auto returnVoid = mDescrambler->descramble(
1782 sctrl,
1783 hidlSubSamples,
1784 srcBuffer,
1785 0,
1786 dstBuffer,
1787 0,
1788 [&status, &result, &detailedError] (
1789 CasStatus _status, uint32_t _bytesWritten,
1790 const hidl_string& _detailedError) {
1791 status = _status;
1792 result = (ssize_t)_bytesWritten;
1793 detailedError = _detailedError;
1794 });
1795
1796 if (!returnVoid.isOk() || status != CasStatus::OK || result < 0) {
1797 ALOGI("[%s] descramble failed, trans=%s, status=%d, result=%zd",
1798 mName, returnVoid.description().c_str(), status, result);
1799 return UNKNOWN_ERROR;
1800 }
1801
1802 if (result < codecDataOffset) {
1803 ALOGD("invalid codec data offset: %zd, result %zd", codecDataOffset, result);
1804 return BAD_VALUE;
1805 }
1806
1807 ALOGV("[%s] descramble succeeded, %zd bytes", mName, result);
1808
1809 if (dstBuffer.type == BufferType::SHARED_MEMORY) {
1810 encryptedBuffer->copyDecryptedContentFromMemory(result);
1811 }
1812 }
1813
1814 buffer->setRange(codecDataOffset, result - codecDataOffset);
1815 return queueInputBufferInternal(buffer);
1816}
1817
1818void CCodecBufferChannel::feedInputBufferIfAvailable() {
1819 QueueGuard guard(mSync);
1820 if (!guard.isRunning()) {
1821 ALOGV("[%s] We're not running --- no input buffer reported", mName);
1822 return;
1823 }
1824 feedInputBufferIfAvailableInternal();
1825}
1826
1827void CCodecBufferChannel::feedInputBufferIfAvailableInternal() {
Wonsik Kimdf5dd142019-02-06 10:15:46 -08001828 if (mInputMetEos ||
1829 mReorderStash.lock()->hasPending() ||
1830 mPipelineWatcher.lock()->pipelineFull()) {
1831 return;
1832 } else {
1833 Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
1834 if ((*buffers)->numClientBuffers() >= mNumOutputSlots) {
1835 return;
1836 }
1837 }
1838 for (size_t i = 0; i < mNumInputSlots; ++i) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001839 sp<MediaCodecBuffer> inBuffer;
1840 size_t index;
1841 {
1842 Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
Wonsik Kimab34ed62019-01-31 15:28:46 -08001843 if ((*buffers)->numClientBuffers() >= mNumInputSlots) {
1844 return;
1845 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001846 if (!(*buffers)->requestNewBuffer(&index, &inBuffer)) {
1847 ALOGV("[%s] no new buffer available", mName);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001848 break;
1849 }
1850 }
1851 ALOGV("[%s] new input index = %zu [%p]", mName, index, inBuffer.get());
1852 mCallback->onInputBufferAvailable(index, inBuffer);
1853 }
1854}
1855
1856status_t CCodecBufferChannel::renderOutputBuffer(
1857 const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) {
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001858 ALOGV("[%s] renderOutputBuffer: %p", mName, buffer.get());
Pawin Vongmasa36653902018-11-15 00:10:25 -08001859 std::shared_ptr<C2Buffer> c2Buffer;
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001860 bool released = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001861 {
1862 Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
1863 if (*buffers) {
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001864 released = (*buffers)->releaseBuffer(buffer, &c2Buffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001865 }
1866 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001867 // NOTE: some apps try to releaseOutputBuffer() with timestamp and/or render
1868 // set to true.
1869 sendOutputBuffers();
1870 // input buffer feeding may have been gated by pending output buffers
1871 feedInputBufferIfAvailable();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001872 if (!c2Buffer) {
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001873 if (released) {
1874 ALOGD("[%s] The app is calling releaseOutputBuffer() with "
1875 "timestamp or render=true with non-video buffers. Apps should "
1876 "call releaseOutputBuffer() with render=false for those.",
1877 mName);
1878 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001879 return INVALID_OPERATION;
1880 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001881
1882#if 0
1883 const std::vector<std::shared_ptr<const C2Info>> infoParams = c2Buffer->info();
1884 ALOGV("[%s] queuing gfx buffer with %zu infos", mName, infoParams.size());
1885 for (const std::shared_ptr<const C2Info> &info : infoParams) {
1886 AString res;
1887 for (size_t ix = 0; ix + 3 < info->size(); ix += 4) {
1888 if (ix) res.append(", ");
1889 res.append(*((int32_t*)info.get() + (ix / 4)));
1890 }
1891 ALOGV(" [%s]", res.c_str());
1892 }
1893#endif
1894 std::shared_ptr<const C2StreamRotationInfo::output> rotation =
1895 std::static_pointer_cast<const C2StreamRotationInfo::output>(
1896 c2Buffer->getInfo(C2StreamRotationInfo::output::PARAM_TYPE));
1897 bool flip = rotation && (rotation->flip & 1);
1898 uint32_t quarters = ((rotation ? rotation->value : 0) / 90) & 3;
1899 uint32_t transform = 0;
1900 switch (quarters) {
1901 case 0: // no rotation
1902 transform = flip ? HAL_TRANSFORM_FLIP_H : 0;
1903 break;
1904 case 1: // 90 degrees counter-clockwise
1905 transform = flip ? (HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90)
1906 : HAL_TRANSFORM_ROT_270;
1907 break;
1908 case 2: // 180 degrees
1909 transform = flip ? HAL_TRANSFORM_FLIP_V : HAL_TRANSFORM_ROT_180;
1910 break;
1911 case 3: // 90 degrees clockwise
1912 transform = flip ? (HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90)
1913 : HAL_TRANSFORM_ROT_90;
1914 break;
1915 }
1916
1917 std::shared_ptr<const C2StreamSurfaceScalingInfo::output> surfaceScaling =
1918 std::static_pointer_cast<const C2StreamSurfaceScalingInfo::output>(
1919 c2Buffer->getInfo(C2StreamSurfaceScalingInfo::output::PARAM_TYPE));
1920 uint32_t videoScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
1921 if (surfaceScaling) {
1922 videoScalingMode = surfaceScaling->value;
1923 }
1924
1925 // Use dataspace from format as it has the default aspects already applied
1926 android_dataspace_t dataSpace = HAL_DATASPACE_UNKNOWN; // this is 0
1927 (void)buffer->format()->findInt32("android._dataspace", (int32_t *)&dataSpace);
1928
1929 // HDR static info
1930 std::shared_ptr<const C2StreamHdrStaticInfo::output> hdrStaticInfo =
1931 std::static_pointer_cast<const C2StreamHdrStaticInfo::output>(
1932 c2Buffer->getInfo(C2StreamHdrStaticInfo::output::PARAM_TYPE));
1933
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001934 // HDR10 plus info
1935 std::shared_ptr<const C2StreamHdr10PlusInfo::output> hdr10PlusInfo =
1936 std::static_pointer_cast<const C2StreamHdr10PlusInfo::output>(
1937 c2Buffer->getInfo(C2StreamHdr10PlusInfo::output::PARAM_TYPE));
1938
Pawin Vongmasa36653902018-11-15 00:10:25 -08001939 {
1940 Mutexed<OutputSurface>::Locked output(mOutputSurface);
1941 if (output->surface == nullptr) {
1942 ALOGI("[%s] cannot render buffer without surface", mName);
1943 return OK;
1944 }
1945 }
1946
1947 std::vector<C2ConstGraphicBlock> blocks = c2Buffer->data().graphicBlocks();
1948 if (blocks.size() != 1u) {
1949 ALOGD("[%s] expected 1 graphic block, but got %zu", mName, blocks.size());
1950 return UNKNOWN_ERROR;
1951 }
1952 const C2ConstGraphicBlock &block = blocks.front();
1953
1954 // TODO: revisit this after C2Fence implementation.
1955 android::IGraphicBufferProducer::QueueBufferInput qbi(
1956 timestampNs,
1957 false, // droppable
1958 dataSpace,
1959 Rect(blocks.front().crop().left,
1960 blocks.front().crop().top,
1961 blocks.front().crop().right(),
1962 blocks.front().crop().bottom()),
1963 videoScalingMode,
1964 transform,
1965 Fence::NO_FENCE, 0);
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001966 if (hdrStaticInfo || hdr10PlusInfo) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001967 HdrMetadata hdr;
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001968 if (hdrStaticInfo) {
1969 struct android_smpte2086_metadata smpte2086_meta = {
1970 .displayPrimaryRed = {
1971 hdrStaticInfo->mastering.red.x, hdrStaticInfo->mastering.red.y
1972 },
1973 .displayPrimaryGreen = {
1974 hdrStaticInfo->mastering.green.x, hdrStaticInfo->mastering.green.y
1975 },
1976 .displayPrimaryBlue = {
1977 hdrStaticInfo->mastering.blue.x, hdrStaticInfo->mastering.blue.y
1978 },
1979 .whitePoint = {
1980 hdrStaticInfo->mastering.white.x, hdrStaticInfo->mastering.white.y
1981 },
1982 .maxLuminance = hdrStaticInfo->mastering.maxLuminance,
1983 .minLuminance = hdrStaticInfo->mastering.minLuminance,
1984 };
1985
1986 struct android_cta861_3_metadata cta861_meta = {
1987 .maxContentLightLevel = hdrStaticInfo->maxCll,
1988 .maxFrameAverageLightLevel = hdrStaticInfo->maxFall,
1989 };
1990
1991 hdr.validTypes = HdrMetadata::SMPTE2086 | HdrMetadata::CTA861_3;
1992 hdr.smpte2086 = smpte2086_meta;
1993 hdr.cta8613 = cta861_meta;
1994 }
1995 if (hdr10PlusInfo) {
1996 hdr.validTypes |= HdrMetadata::HDR10PLUS;
1997 hdr.hdr10plus.assign(
1998 hdr10PlusInfo->m.value,
1999 hdr10PlusInfo->m.value + hdr10PlusInfo->flexCount());
2000 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002001 qbi.setHdrMetadata(hdr);
2002 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -08002003 // we don't have dirty regions
2004 qbi.setSurfaceDamage(Region::INVALID_REGION);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002005 android::IGraphicBufferProducer::QueueBufferOutput qbo;
2006 status_t result = mComponent->queueToOutputSurface(block, qbi, &qbo);
2007 if (result != OK) {
2008 ALOGI("[%s] queueBuffer failed: %d", mName, result);
2009 return result;
2010 }
2011 ALOGV("[%s] queue buffer successful", mName);
2012
2013 int64_t mediaTimeUs = 0;
2014 (void)buffer->meta()->findInt64("timeUs", &mediaTimeUs);
2015 mCCodecCallback->onOutputFramesRendered(mediaTimeUs, timestampNs);
2016
2017 return OK;
2018}
2019
2020status_t CCodecBufferChannel::discardBuffer(const sp<MediaCodecBuffer> &buffer) {
2021 ALOGV("[%s] discardBuffer: %p", mName, buffer.get());
2022 bool released = false;
2023 {
2024 Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
Pawin Vongmasa1f213362019-01-24 06:59:16 -08002025 if (*buffers && (*buffers)->releaseBuffer(buffer, nullptr, true)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002026 released = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002027 }
2028 }
2029 {
2030 Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
2031 if (*buffers && (*buffers)->releaseBuffer(buffer, nullptr)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002032 released = true;
2033 }
2034 }
2035 if (released) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002036 sendOutputBuffers();
Pawin Vongmasa8be93112018-12-11 14:01:42 -08002037 feedInputBufferIfAvailable();
Pawin Vongmasa36653902018-11-15 00:10:25 -08002038 } else {
2039 ALOGD("[%s] MediaCodec discarded an unknown buffer", mName);
2040 }
2041 return OK;
2042}
2043
2044void CCodecBufferChannel::getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) {
2045 array->clear();
2046 Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
2047
2048 if (!(*buffers)->isArrayMode()) {
Wonsik Kim078b58e2019-01-09 15:08:06 -08002049 *buffers = (*buffers)->toArrayMode(mNumInputSlots);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002050 }
2051
2052 (*buffers)->getArray(array);
2053}
2054
2055void CCodecBufferChannel::getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) {
2056 array->clear();
2057 Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
2058
2059 if (!(*buffers)->isArrayMode()) {
Wonsik Kim078b58e2019-01-09 15:08:06 -08002060 *buffers = (*buffers)->toArrayMode(mNumOutputSlots);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002061 }
2062
2063 (*buffers)->getArray(array);
2064}
2065
2066status_t CCodecBufferChannel::start(
2067 const sp<AMessage> &inputFormat, const sp<AMessage> &outputFormat) {
2068 C2StreamBufferTypeSetting::input iStreamFormat(0u);
2069 C2StreamBufferTypeSetting::output oStreamFormat(0u);
2070 C2PortReorderBufferDepthTuning::output reorderDepth;
2071 C2PortReorderKeySetting::output reorderKey;
Wonsik Kim078b58e2019-01-09 15:08:06 -08002072 C2PortActualDelayTuning::input inputDelay(0);
2073 C2PortActualDelayTuning::output outputDelay(0);
2074 C2ActualPipelineDelayTuning pipelineDelay(0);
2075
Pawin Vongmasa36653902018-11-15 00:10:25 -08002076 c2_status_t err = mComponent->query(
2077 {
2078 &iStreamFormat,
2079 &oStreamFormat,
2080 &reorderDepth,
2081 &reorderKey,
Wonsik Kim078b58e2019-01-09 15:08:06 -08002082 &inputDelay,
2083 &pipelineDelay,
2084 &outputDelay,
Pawin Vongmasa36653902018-11-15 00:10:25 -08002085 },
2086 {},
2087 C2_DONT_BLOCK,
2088 nullptr);
2089 if (err == C2_BAD_INDEX) {
2090 if (!iStreamFormat || !oStreamFormat) {
2091 return UNKNOWN_ERROR;
2092 }
2093 } else if (err != C2_OK) {
2094 return UNKNOWN_ERROR;
2095 }
2096
2097 {
2098 Mutexed<ReorderStash>::Locked reorder(mReorderStash);
2099 reorder->clear();
2100 if (reorderDepth) {
2101 reorder->setDepth(reorderDepth.value);
2102 }
2103 if (reorderKey) {
2104 reorder->setKey(reorderKey.value);
2105 }
2106 }
Wonsik Kim078b58e2019-01-09 15:08:06 -08002107
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -08002108 uint32_t inputDelayValue = inputDelay ? inputDelay.value : 0;
2109 uint32_t pipelineDelayValue = pipelineDelay ? pipelineDelay.value : 0;
2110 uint32_t outputDelayValue = outputDelay ? outputDelay.value : 0;
2111
2112 mNumInputSlots = inputDelayValue + pipelineDelayValue + kSmoothnessFactor;
2113 mNumOutputSlots = outputDelayValue + kSmoothnessFactor;
2114 mDelay = inputDelayValue + pipelineDelayValue + outputDelayValue;
Wonsik Kim078b58e2019-01-09 15:08:06 -08002115
Pawin Vongmasa36653902018-11-15 00:10:25 -08002116 // TODO: get this from input format
2117 bool secure = mComponent->getName().find(".secure") != std::string::npos;
2118
2119 std::shared_ptr<C2AllocatorStore> allocatorStore = GetCodec2PlatformAllocatorStore();
2120 int poolMask = property_get_int32(
2121 "debug.stagefright.c2-poolmask",
2122 1 << C2PlatformAllocatorStore::ION |
2123 1 << C2PlatformAllocatorStore::BUFFERQUEUE);
2124
2125 if (inputFormat != nullptr) {
2126 bool graphic = (iStreamFormat.value == C2FormatVideo);
2127 std::shared_ptr<C2BlockPool> pool;
2128 {
2129 Mutexed<BlockPools>::Locked pools(mBlockPools);
2130
2131 // set default allocator ID.
2132 pools->inputAllocatorId = (graphic) ? C2PlatformAllocatorStore::GRALLOC
2133 : C2PlatformAllocatorStore::ION;
2134
2135 // query C2PortAllocatorsTuning::input from component. If an allocator ID is obtained
2136 // from component, create the input block pool with given ID. Otherwise, use default IDs.
2137 std::vector<std::unique_ptr<C2Param>> params;
2138 err = mComponent->query({ },
2139 { C2PortAllocatorsTuning::input::PARAM_TYPE },
2140 C2_DONT_BLOCK,
2141 &params);
2142 if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
2143 ALOGD("[%s] Query input allocators returned %zu params => %s (%u)",
2144 mName, params.size(), asString(err), err);
2145 } else if (err == C2_OK && params.size() == 1) {
2146 C2PortAllocatorsTuning::input *inputAllocators =
2147 C2PortAllocatorsTuning::input::From(params[0].get());
2148 if (inputAllocators && inputAllocators->flexCount() > 0) {
2149 std::shared_ptr<C2Allocator> allocator;
2150 // verify allocator IDs and resolve default allocator
2151 allocatorStore->fetchAllocator(inputAllocators->m.values[0], &allocator);
2152 if (allocator) {
2153 pools->inputAllocatorId = allocator->getId();
2154 } else {
2155 ALOGD("[%s] component requested invalid input allocator ID %u",
2156 mName, inputAllocators->m.values[0]);
2157 }
2158 }
2159 }
2160
2161 // TODO: use C2Component wrapper to associate this pool with ourselves
2162 if ((poolMask >> pools->inputAllocatorId) & 1) {
2163 err = CreateCodec2BlockPool(pools->inputAllocatorId, nullptr, &pool);
2164 ALOGD("[%s] Created input block pool with allocatorID %u => poolID %llu - %s (%d)",
2165 mName, pools->inputAllocatorId,
2166 (unsigned long long)(pool ? pool->getLocalId() : 111000111),
2167 asString(err), err);
2168 } else {
2169 err = C2_NOT_FOUND;
2170 }
2171 if (err != C2_OK) {
2172 C2BlockPool::local_id_t inputPoolId =
2173 graphic ? C2BlockPool::BASIC_GRAPHIC : C2BlockPool::BASIC_LINEAR;
2174 err = GetCodec2BlockPool(inputPoolId, nullptr, &pool);
2175 ALOGD("[%s] Using basic input block pool with poolID %llu => got %llu - %s (%d)",
2176 mName, (unsigned long long)inputPoolId,
2177 (unsigned long long)(pool ? pool->getLocalId() : 111000111),
2178 asString(err), err);
2179 if (err != C2_OK) {
2180 return NO_MEMORY;
2181 }
2182 }
2183 pools->inputPool = pool;
2184 }
2185
Wonsik Kim51051262018-11-28 13:59:05 -08002186 bool forceArrayMode = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002187 Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
2188 if (graphic) {
2189 if (mInputSurface) {
2190 buffers->reset(new DummyInputBuffers(mName));
2191 } else if (mMetaMode == MODE_ANW) {
2192 buffers->reset(new GraphicMetadataInputBuffers(mName));
2193 } else {
Wonsik Kim078b58e2019-01-09 15:08:06 -08002194 buffers->reset(new GraphicInputBuffers(mNumInputSlots, mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002195 }
2196 } else {
2197 if (hasCryptoOrDescrambler()) {
2198 int32_t capacity = kLinearBufferSize;
2199 (void)inputFormat->findInt32(KEY_MAX_INPUT_SIZE, &capacity);
2200 if ((size_t)capacity > kMaxLinearBufferSize) {
2201 ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize);
2202 capacity = kMaxLinearBufferSize;
2203 }
2204 if (mDealer == nullptr) {
2205 mDealer = new MemoryDealer(
2206 align(capacity, MemoryDealer::getAllocationAlignment())
Wonsik Kim078b58e2019-01-09 15:08:06 -08002207 * (mNumInputSlots + 1),
Pawin Vongmasa36653902018-11-15 00:10:25 -08002208 "EncryptedLinearInputBuffers");
2209 mDecryptDestination = mDealer->allocate((size_t)capacity);
2210 }
2211 if (mCrypto != nullptr && mHeapSeqNum < 0) {
2212 mHeapSeqNum = mCrypto->setHeap(mDealer->getMemoryHeap());
2213 } else {
2214 mHeapSeqNum = -1;
2215 }
2216 buffers->reset(new EncryptedLinearInputBuffers(
Wonsik Kim078b58e2019-01-09 15:08:06 -08002217 secure, mDealer, mCrypto, mHeapSeqNum, (size_t)capacity,
2218 mNumInputSlots, mName));
Wonsik Kim51051262018-11-28 13:59:05 -08002219 forceArrayMode = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002220 } else {
2221 buffers->reset(new LinearInputBuffers(mName));
2222 }
2223 }
2224 (*buffers)->setFormat(inputFormat);
2225
2226 if (err == C2_OK) {
2227 (*buffers)->setPool(pool);
2228 } else {
2229 // TODO: error
2230 }
Wonsik Kim51051262018-11-28 13:59:05 -08002231
2232 if (forceArrayMode) {
Wonsik Kim078b58e2019-01-09 15:08:06 -08002233 *buffers = (*buffers)->toArrayMode(mNumInputSlots);
Wonsik Kim51051262018-11-28 13:59:05 -08002234 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002235 }
2236
2237 if (outputFormat != nullptr) {
2238 sp<IGraphicBufferProducer> outputSurface;
2239 uint32_t outputGeneration;
2240 {
2241 Mutexed<OutputSurface>::Locked output(mOutputSurface);
2242 outputSurface = output->surface ?
2243 output->surface->getIGraphicBufferProducer() : nullptr;
2244 outputGeneration = output->generation;
2245 }
2246
2247 bool graphic = (oStreamFormat.value == C2FormatVideo);
2248 C2BlockPool::local_id_t outputPoolId_;
2249
2250 {
2251 Mutexed<BlockPools>::Locked pools(mBlockPools);
2252
2253 // set default allocator ID.
2254 pools->outputAllocatorId = (graphic) ? C2PlatformAllocatorStore::GRALLOC
2255 : C2PlatformAllocatorStore::ION;
2256
2257 // query C2PortAllocatorsTuning::output from component, or use default allocator if
2258 // unsuccessful.
2259 std::vector<std::unique_ptr<C2Param>> params;
2260 err = mComponent->query({ },
2261 { C2PortAllocatorsTuning::output::PARAM_TYPE },
2262 C2_DONT_BLOCK,
2263 &params);
2264 if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
2265 ALOGD("[%s] Query output allocators returned %zu params => %s (%u)",
2266 mName, params.size(), asString(err), err);
2267 } else if (err == C2_OK && params.size() == 1) {
2268 C2PortAllocatorsTuning::output *outputAllocators =
2269 C2PortAllocatorsTuning::output::From(params[0].get());
2270 if (outputAllocators && outputAllocators->flexCount() > 0) {
2271 std::shared_ptr<C2Allocator> allocator;
2272 // verify allocator IDs and resolve default allocator
2273 allocatorStore->fetchAllocator(outputAllocators->m.values[0], &allocator);
2274 if (allocator) {
2275 pools->outputAllocatorId = allocator->getId();
2276 } else {
2277 ALOGD("[%s] component requested invalid output allocator ID %u",
2278 mName, outputAllocators->m.values[0]);
2279 }
2280 }
2281 }
2282
2283 // use bufferqueue if outputting to a surface.
2284 // query C2PortSurfaceAllocatorTuning::output from component, or use default allocator
2285 // if unsuccessful.
2286 if (outputSurface) {
2287 params.clear();
2288 err = mComponent->query({ },
2289 { C2PortSurfaceAllocatorTuning::output::PARAM_TYPE },
2290 C2_DONT_BLOCK,
2291 &params);
2292 if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
2293 ALOGD("[%s] Query output surface allocator returned %zu params => %s (%u)",
2294 mName, params.size(), asString(err), err);
2295 } else if (err == C2_OK && params.size() == 1) {
2296 C2PortSurfaceAllocatorTuning::output *surfaceAllocator =
2297 C2PortSurfaceAllocatorTuning::output::From(params[0].get());
2298 if (surfaceAllocator) {
2299 std::shared_ptr<C2Allocator> allocator;
2300 // verify allocator IDs and resolve default allocator
2301 allocatorStore->fetchAllocator(surfaceAllocator->value, &allocator);
2302 if (allocator) {
2303 pools->outputAllocatorId = allocator->getId();
2304 } else {
2305 ALOGD("[%s] component requested invalid surface output allocator ID %u",
2306 mName, surfaceAllocator->value);
2307 err = C2_BAD_VALUE;
2308 }
2309 }
2310 }
2311 if (pools->outputAllocatorId == C2PlatformAllocatorStore::GRALLOC
2312 && err != C2_OK
2313 && ((poolMask >> C2PlatformAllocatorStore::BUFFERQUEUE) & 1)) {
2314 pools->outputAllocatorId = C2PlatformAllocatorStore::BUFFERQUEUE;
2315 }
2316 }
2317
2318 if ((poolMask >> pools->outputAllocatorId) & 1) {
2319 err = mComponent->createBlockPool(
2320 pools->outputAllocatorId, &pools->outputPoolId, &pools->outputPoolIntf);
2321 ALOGI("[%s] Created output block pool with allocatorID %u => poolID %llu - %s",
2322 mName, pools->outputAllocatorId,
2323 (unsigned long long)pools->outputPoolId,
2324 asString(err));
2325 } else {
2326 err = C2_NOT_FOUND;
2327 }
2328 if (err != C2_OK) {
2329 // use basic pool instead
2330 pools->outputPoolId =
2331 graphic ? C2BlockPool::BASIC_GRAPHIC : C2BlockPool::BASIC_LINEAR;
2332 }
2333
2334 // Configure output block pool ID as parameter C2PortBlockPoolsTuning::output to
2335 // component.
2336 std::unique_ptr<C2PortBlockPoolsTuning::output> poolIdsTuning =
2337 C2PortBlockPoolsTuning::output::AllocUnique({ pools->outputPoolId });
2338
2339 std::vector<std::unique_ptr<C2SettingResult>> failures;
2340 err = mComponent->config({ poolIdsTuning.get() }, C2_MAY_BLOCK, &failures);
2341 ALOGD("[%s] Configured output block pool ids %llu => %s",
2342 mName, (unsigned long long)poolIdsTuning->m.values[0], asString(err));
2343 outputPoolId_ = pools->outputPoolId;
2344 }
2345
2346 Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
2347
2348 if (graphic) {
2349 if (outputSurface) {
2350 buffers->reset(new GraphicOutputBuffers(mName));
2351 } else {
Wonsik Kim078b58e2019-01-09 15:08:06 -08002352 buffers->reset(new RawGraphicOutputBuffers(mNumOutputSlots, mName));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002353 }
2354 } else {
2355 buffers->reset(new LinearOutputBuffers(mName));
2356 }
2357 (*buffers)->setFormat(outputFormat->dup());
2358
2359
2360 // Try to set output surface to created block pool if given.
2361 if (outputSurface) {
2362 mComponent->setOutputSurface(
2363 outputPoolId_,
2364 outputSurface,
2365 outputGeneration);
2366 }
2367
2368 if (oStreamFormat.value == C2BufferData::LINEAR
2369 && mComponentName.find("c2.qti.") == std::string::npos) {
2370 // WORKAROUND: if we're using early CSD workaround we convert to
2371 // array mode, to appease apps assuming the output
2372 // buffers to be of the same size.
Wonsik Kim078b58e2019-01-09 15:08:06 -08002373 (*buffers) = (*buffers)->toArrayMode(mNumOutputSlots);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002374
2375 int32_t channelCount;
2376 int32_t sampleRate;
2377 if (outputFormat->findInt32(KEY_CHANNEL_COUNT, &channelCount)
2378 && outputFormat->findInt32(KEY_SAMPLE_RATE, &sampleRate)) {
2379 int32_t delay = 0;
2380 int32_t padding = 0;;
2381 if (!outputFormat->findInt32("encoder-delay", &delay)) {
2382 delay = 0;
2383 }
2384 if (!outputFormat->findInt32("encoder-padding", &padding)) {
2385 padding = 0;
2386 }
2387 if (delay || padding) {
2388 // We need write access to the buffers, and we're already in
2389 // array mode.
2390 (*buffers)->initSkipCutBuffer(delay, padding, sampleRate, channelCount);
2391 }
2392 }
2393 }
2394 }
2395
2396 // Set up pipeline control. This has to be done after mInputBuffers and
2397 // mOutputBuffers are initialized to make sure that lingering callbacks
2398 // about buffers from the previous generation do not interfere with the
2399 // newly initialized pipeline capacity.
2400
Wonsik Kimab34ed62019-01-31 15:28:46 -08002401 {
2402 Mutexed<PipelineWatcher>::Locked watcher(mPipelineWatcher);
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -08002403 watcher->inputDelay(inputDelayValue)
2404 .pipelineDelay(pipelineDelayValue)
2405 .outputDelay(outputDelayValue)
Wonsik Kimab34ed62019-01-31 15:28:46 -08002406 .smoothnessFactor(kSmoothnessFactor);
2407 watcher->flush();
2408 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002409
2410 mInputMetEos = false;
2411 mSync.start();
2412 return OK;
2413}
2414
2415status_t CCodecBufferChannel::requestInitialInputBuffers() {
2416 if (mInputSurface) {
2417 return OK;
2418 }
2419
2420 C2StreamFormatConfig::output oStreamFormat(0u);
2421 c2_status_t err = mComponent->query({ &oStreamFormat }, {}, C2_DONT_BLOCK, nullptr);
2422 if (err != C2_OK) {
2423 return UNKNOWN_ERROR;
2424 }
2425 std::vector<sp<MediaCodecBuffer>> toBeQueued;
2426 // TODO: use proper buffer depth instead of this random value
Wonsik Kim078b58e2019-01-09 15:08:06 -08002427 for (size_t i = 0; i < mNumInputSlots; ++i) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002428 size_t index;
2429 sp<MediaCodecBuffer> buffer;
2430 {
2431 Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
2432 if (!(*buffers)->requestNewBuffer(&index, &buffer)) {
2433 if (i == 0) {
2434 ALOGW("[%s] start: cannot allocate memory at all", mName);
2435 return NO_MEMORY;
2436 } else {
2437 ALOGV("[%s] start: cannot allocate memory, only %zu buffers allocated",
2438 mName, i);
2439 }
2440 break;
2441 }
2442 }
2443 if (buffer) {
2444 Mutexed<std::list<sp<ABuffer>>>::Locked configs(mFlushedConfigs);
2445 ALOGV("[%s] input buffer %zu available", mName, index);
2446 bool post = true;
2447 if (!configs->empty()) {
2448 sp<ABuffer> config = configs->front();
2449 if (buffer->capacity() >= config->size()) {
2450 memcpy(buffer->base(), config->data(), config->size());
2451 buffer->setRange(0, config->size());
2452 buffer->meta()->clear();
2453 buffer->meta()->setInt64("timeUs", 0);
2454 buffer->meta()->setInt32("csd", 1);
2455 post = false;
2456 } else {
2457 ALOGD("[%s] buffer capacity too small for the config (%zu < %zu)",
2458 mName, buffer->capacity(), config->size());
2459 }
2460 } else if (oStreamFormat.value == C2BufferData::LINEAR && i == 0
2461 && mComponentName.find("c2.qti.") == std::string::npos) {
2462 // WORKAROUND: Some apps expect CSD available without queueing
2463 // any input. Queue an empty buffer to get the CSD.
2464 buffer->setRange(0, 0);
2465 buffer->meta()->clear();
2466 buffer->meta()->setInt64("timeUs", 0);
2467 post = false;
2468 }
Wonsik Kimab34ed62019-01-31 15:28:46 -08002469 if (post) {
2470 mCallback->onInputBufferAvailable(index, buffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002471 } else {
Wonsik Kimab34ed62019-01-31 15:28:46 -08002472 toBeQueued.emplace_back(buffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002473 }
2474 }
2475 }
2476 for (const sp<MediaCodecBuffer> &buffer : toBeQueued) {
2477 if (queueInputBufferInternal(buffer) != OK) {
Wonsik Kimab34ed62019-01-31 15:28:46 -08002478 ALOGV("[%s] Error while queueing initial buffers", mName);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002479 }
2480 }
2481 return OK;
2482}
2483
2484void CCodecBufferChannel::stop() {
2485 mSync.stop();
2486 mFirstValidFrameIndex = mFrameIndex.load(std::memory_order_relaxed);
2487 if (mInputSurface != nullptr) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002488 mInputSurface.reset();
2489 }
2490}
2491
2492void CCodecBufferChannel::flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) {
2493 ALOGV("[%s] flush", mName);
2494 {
2495 Mutexed<std::list<sp<ABuffer>>>::Locked configs(mFlushedConfigs);
2496 for (const std::unique_ptr<C2Work> &work : flushedWork) {
2497 if (!(work->input.flags & C2FrameData::FLAG_CODEC_CONFIG)) {
2498 continue;
2499 }
2500 if (work->input.buffers.empty()
2501 || work->input.buffers.front()->data().linearBlocks().empty()) {
2502 ALOGD("[%s] no linear codec config data found", mName);
2503 continue;
2504 }
2505 C2ReadView view =
2506 work->input.buffers.front()->data().linearBlocks().front().map().get();
2507 if (view.error() != C2_OK) {
2508 ALOGD("[%s] failed to map flushed codec config data: %d", mName, view.error());
2509 continue;
2510 }
2511 configs->push_back(ABuffer::CreateAsCopy(view.data(), view.capacity()));
2512 ALOGV("[%s] stashed flushed codec config data (size=%u)", mName, view.capacity());
2513 }
2514 }
2515 {
2516 Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
2517 (*buffers)->flush();
2518 }
2519 {
2520 Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
2521 (*buffers)->flush(flushedWork);
2522 }
Wonsik Kim6897f222019-01-30 13:29:24 -08002523 mReorderStash.lock()->flush();
Wonsik Kimab34ed62019-01-31 15:28:46 -08002524 mPipelineWatcher.lock()->flush();
Pawin Vongmasa36653902018-11-15 00:10:25 -08002525}
2526
2527void CCodecBufferChannel::onWorkDone(
2528 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
Wonsik Kimab34ed62019-01-31 15:28:46 -08002529 const C2StreamInitDataInfo::output *initData) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002530 if (handleWork(std::move(work), outputFormat, initData)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002531 feedInputBufferIfAvailable();
2532 }
2533}
2534
2535void CCodecBufferChannel::onInputBufferDone(
Wonsik Kimab34ed62019-01-31 15:28:46 -08002536 uint64_t frameIndex, size_t arrayIndex) {
2537 std::shared_ptr<C2Buffer> buffer =
2538 mPipelineWatcher.lock()->onInputBufferReleased(frameIndex, arrayIndex);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002539 bool newInputSlotAvailable;
2540 {
2541 Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
2542 newInputSlotAvailable = (*buffers)->expireComponentBuffer(buffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002543 }
2544 if (newInputSlotAvailable) {
2545 feedInputBufferIfAvailable();
2546 }
2547}
2548
2549bool CCodecBufferChannel::handleWork(
2550 std::unique_ptr<C2Work> work,
2551 const sp<AMessage> &outputFormat,
2552 const C2StreamInitDataInfo::output *initData) {
2553 if ((work->input.ordinal.frameIndex - mFirstValidFrameIndex.load()).peek() < 0) {
2554 // Discard frames from previous generation.
2555 ALOGD("[%s] Discard frames from previous generation.", mName);
2556 return false;
2557 }
2558
2559 if (work->worklets.size() != 1u
2560 || !work->worklets.front()
2561 || !(work->worklets.front()->output.flags & C2FrameData::FLAG_INCOMPLETE)) {
Wonsik Kimab34ed62019-01-31 15:28:46 -08002562 mPipelineWatcher.lock()->onWorkDone(work->input.ordinal.frameIndex.peeku());
Pawin Vongmasa36653902018-11-15 00:10:25 -08002563 }
2564
2565 if (work->result == C2_NOT_FOUND) {
2566 ALOGD("[%s] flushed work; ignored.", mName);
2567 return true;
2568 }
2569
2570 if (work->result != C2_OK) {
2571 ALOGD("[%s] work failed to complete: %d", mName, work->result);
2572 mCCodecCallback->onError(work->result, ACTION_CODE_FATAL);
2573 return false;
2574 }
2575
2576 // NOTE: MediaCodec usage supposedly have only one worklet
2577 if (work->worklets.size() != 1u) {
2578 ALOGI("[%s] onWorkDone: incorrect number of worklets: %zu",
2579 mName, work->worklets.size());
2580 mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
2581 return false;
2582 }
2583
2584 const std::unique_ptr<C2Worklet> &worklet = work->worklets.front();
2585
2586 std::shared_ptr<C2Buffer> buffer;
2587 // NOTE: MediaCodec usage supposedly have only one output stream.
2588 if (worklet->output.buffers.size() > 1u) {
2589 ALOGI("[%s] onWorkDone: incorrect number of output buffers: %zu",
2590 mName, worklet->output.buffers.size());
2591 mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
2592 return false;
2593 } else if (worklet->output.buffers.size() == 1u) {
2594 buffer = worklet->output.buffers[0];
2595 if (!buffer) {
2596 ALOGD("[%s] onWorkDone: nullptr found in buffers; ignored.", mName);
2597 }
2598 }
2599
2600 while (!worklet->output.configUpdate.empty()) {
2601 std::unique_ptr<C2Param> param;
2602 worklet->output.configUpdate.back().swap(param);
2603 worklet->output.configUpdate.pop_back();
2604 switch (param->coreIndex().coreIndex()) {
2605 case C2PortReorderBufferDepthTuning::CORE_INDEX: {
2606 C2PortReorderBufferDepthTuning::output reorderDepth;
2607 if (reorderDepth.updateFrom(*param)) {
2608 mReorderStash.lock()->setDepth(reorderDepth.value);
2609 ALOGV("[%s] onWorkDone: updated reorder depth to %u",
2610 mName, reorderDepth.value);
2611 } else {
2612 ALOGD("[%s] onWorkDone: failed to read reorder depth", mName);
2613 }
2614 break;
2615 }
2616 case C2PortReorderKeySetting::CORE_INDEX: {
2617 C2PortReorderKeySetting::output reorderKey;
2618 if (reorderKey.updateFrom(*param)) {
2619 mReorderStash.lock()->setKey(reorderKey.value);
2620 ALOGV("[%s] onWorkDone: updated reorder key to %u",
2621 mName, reorderKey.value);
2622 } else {
2623 ALOGD("[%s] onWorkDone: failed to read reorder key", mName);
2624 }
2625 break;
2626 }
2627 default:
2628 ALOGV("[%s] onWorkDone: unrecognized config update (%08X)",
2629 mName, param->index());
2630 break;
2631 }
2632 }
2633
2634 if (outputFormat != nullptr) {
2635 Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
2636 ALOGD("[%s] onWorkDone: output format changed to %s",
2637 mName, outputFormat->debugString().c_str());
2638 (*buffers)->setFormat(outputFormat);
2639
2640 AString mediaType;
2641 if (outputFormat->findString(KEY_MIME, &mediaType)
2642 && mediaType == MIMETYPE_AUDIO_RAW) {
2643 int32_t channelCount;
2644 int32_t sampleRate;
2645 if (outputFormat->findInt32(KEY_CHANNEL_COUNT, &channelCount)
2646 && outputFormat->findInt32(KEY_SAMPLE_RATE, &sampleRate)) {
2647 (*buffers)->updateSkipCutBuffer(sampleRate, channelCount);
2648 }
2649 }
2650 }
2651
2652 int32_t flags = 0;
2653 if (worklet->output.flags & C2FrameData::FLAG_END_OF_STREAM) {
2654 flags |= MediaCodec::BUFFER_FLAG_EOS;
2655 ALOGV("[%s] onWorkDone: output EOS", mName);
2656 }
2657
2658 sp<MediaCodecBuffer> outBuffer;
2659 size_t index;
2660
2661 // WORKAROUND: adjust output timestamp based on client input timestamp and codec
2662 // input timestamp. Codec output timestamp (in the timestamp field) shall correspond to
2663 // the codec input timestamp, but client output timestamp should (reported in timeUs)
2664 // shall correspond to the client input timesamp (in customOrdinal). By using the
2665 // delta between the two, this allows for some timestamp deviation - e.g. if one input
2666 // produces multiple output.
2667 c2_cntr64_t timestamp =
2668 worklet->output.ordinal.timestamp + work->input.ordinal.customOrdinal
2669 - work->input.ordinal.timestamp;
2670 ALOGV("[%s] onWorkDone: input %lld, codec %lld => output %lld => %lld",
2671 mName,
2672 work->input.ordinal.customOrdinal.peekll(),
2673 work->input.ordinal.timestamp.peekll(),
2674 worklet->output.ordinal.timestamp.peekll(),
2675 timestamp.peekll());
2676
2677 if (initData != nullptr) {
2678 Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
2679 if ((*buffers)->registerCsd(initData, &index, &outBuffer) == OK) {
2680 outBuffer->meta()->setInt64("timeUs", timestamp.peek());
2681 outBuffer->meta()->setInt32("flags", MediaCodec::BUFFER_FLAG_CODECCONFIG);
2682 ALOGV("[%s] onWorkDone: csd index = %zu [%p]", mName, index, outBuffer.get());
2683
2684 buffers.unlock();
2685 mCallback->onOutputBufferAvailable(index, outBuffer);
2686 buffers.lock();
2687 } else {
2688 ALOGD("[%s] onWorkDone: unable to register csd", mName);
2689 buffers.unlock();
2690 mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
2691 buffers.lock();
2692 return false;
2693 }
2694 }
2695
2696 if (!buffer && !flags) {
2697 ALOGV("[%s] onWorkDone: Not reporting output buffer (%lld)",
2698 mName, work->input.ordinal.frameIndex.peekull());
2699 return true;
2700 }
2701
2702 if (buffer) {
2703 for (const std::shared_ptr<const C2Info> &info : buffer->info()) {
2704 // TODO: properly translate these to metadata
2705 switch (info->coreIndex().coreIndex()) {
2706 case C2StreamPictureTypeMaskInfo::CORE_INDEX:
2707 if (((C2StreamPictureTypeMaskInfo *)info.get())->value & C2PictureTypeKeyFrame) {
2708 flags |= MediaCodec::BUFFER_FLAG_SYNCFRAME;
2709 }
2710 break;
2711 default:
2712 break;
2713 }
2714 }
2715 }
2716
2717 {
2718 Mutexed<ReorderStash>::Locked reorder(mReorderStash);
2719 reorder->emplace(buffer, timestamp.peek(), flags, worklet->output.ordinal);
2720 if (flags & MediaCodec::BUFFER_FLAG_EOS) {
2721 // Flush reorder stash
2722 reorder->setDepth(0);
2723 }
2724 }
2725 sendOutputBuffers();
2726 return true;
2727}
2728
2729void CCodecBufferChannel::sendOutputBuffers() {
2730 ReorderStash::Entry entry;
2731 sp<MediaCodecBuffer> outBuffer;
2732 size_t index;
2733
2734 while (true) {
Wonsik Kim38ad3412019-02-01 15:13:23 -08002735 Mutexed<ReorderStash>::Locked reorder(mReorderStash);
2736 if (!reorder->hasPending()) {
2737 break;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002738 }
Wonsik Kim38ad3412019-02-01 15:13:23 -08002739 if (!reorder->pop(&entry)) {
2740 break;
2741 }
2742
Pawin Vongmasa36653902018-11-15 00:10:25 -08002743 Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
2744 status_t err = (*buffers)->registerBuffer(entry.buffer, &index, &outBuffer);
2745 if (err != OK) {
Wonsik Kim38ad3412019-02-01 15:13:23 -08002746 bool outputBuffersChanged = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002747 if (err != WOULD_BLOCK) {
Wonsik Kim186fdbf2019-01-29 13:30:01 -08002748 if (!(*buffers)->isArrayMode()) {
2749 *buffers = (*buffers)->toArrayMode(mNumOutputSlots);
2750 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002751 OutputBuffersArray *array = (OutputBuffersArray *)buffers->get();
2752 array->realloc(entry.buffer);
Wonsik Kim38ad3412019-02-01 15:13:23 -08002753 outputBuffersChanged = true;
2754 }
2755 ALOGV("[%s] sendOutputBuffers: unable to register output buffer", mName);
2756 reorder->defer(entry);
2757
2758 buffers.unlock();
2759 reorder.unlock();
2760
2761 if (outputBuffersChanged) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002762 mCCodecCallback->onOutputBuffersChanged();
2763 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002764 return;
2765 }
2766 buffers.unlock();
Wonsik Kim38ad3412019-02-01 15:13:23 -08002767 reorder.unlock();
Pawin Vongmasa36653902018-11-15 00:10:25 -08002768
2769 outBuffer->meta()->setInt64("timeUs", entry.timestamp);
2770 outBuffer->meta()->setInt32("flags", entry.flags);
2771 ALOGV("[%s] sendOutputBuffers: out buffer index = %zu [%p] => %p + %zu",
2772 mName, index, outBuffer.get(), outBuffer->data(), outBuffer->size());
2773 mCallback->onOutputBufferAvailable(index, outBuffer);
2774 }
2775}
2776
2777status_t CCodecBufferChannel::setSurface(const sp<Surface> &newSurface) {
2778 static std::atomic_uint32_t surfaceGeneration{0};
2779 uint32_t generation = (getpid() << 10) |
2780 ((surfaceGeneration.fetch_add(1, std::memory_order_relaxed) + 1)
2781 & ((1 << 10) - 1));
2782
2783 sp<IGraphicBufferProducer> producer;
2784 if (newSurface) {
2785 newSurface->setScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Wonsik Kim078b58e2019-01-09 15:08:06 -08002786 newSurface->setMaxDequeuedBufferCount(mNumOutputSlots + kRenderingDepth);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002787 producer = newSurface->getIGraphicBufferProducer();
2788 producer->setGenerationNumber(generation);
2789 } else {
2790 ALOGE("[%s] setting output surface to null", mName);
2791 return INVALID_OPERATION;
2792 }
2793
2794 std::shared_ptr<Codec2Client::Configurable> outputPoolIntf;
2795 C2BlockPool::local_id_t outputPoolId;
2796 {
2797 Mutexed<BlockPools>::Locked pools(mBlockPools);
2798 outputPoolId = pools->outputPoolId;
2799 outputPoolIntf = pools->outputPoolIntf;
2800 }
2801
2802 if (outputPoolIntf) {
2803 if (mComponent->setOutputSurface(
2804 outputPoolId,
2805 producer,
2806 generation) != C2_OK) {
2807 ALOGI("[%s] setSurface: component setOutputSurface failed", mName);
2808 return INVALID_OPERATION;
2809 }
2810 }
2811
2812 {
2813 Mutexed<OutputSurface>::Locked output(mOutputSurface);
2814 output->surface = newSurface;
2815 output->generation = generation;
2816 }
2817
2818 return OK;
2819}
2820
Wonsik Kimab34ed62019-01-31 15:28:46 -08002821PipelineWatcher::Clock::duration CCodecBufferChannel::elapsed() {
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -08002822 // When client pushed EOS, we want all the work to be done quickly.
2823 // Otherwise, component may have stalled work due to input starvation up to
2824 // the sum of the delay in the pipeline.
2825 size_t n = mInputMetEos ? 0 : mDelay;
2826 return mPipelineWatcher.lock()->elapsed(PipelineWatcher::Clock::now(), n);
Wonsik Kimab34ed62019-01-31 15:28:46 -08002827}
2828
Pawin Vongmasa36653902018-11-15 00:10:25 -08002829void CCodecBufferChannel::setMetaMode(MetaMode mode) {
2830 mMetaMode = mode;
2831}
2832
2833status_t toStatusT(c2_status_t c2s, c2_operation_t c2op) {
2834 // C2_OK is always translated to OK.
2835 if (c2s == C2_OK) {
2836 return OK;
2837 }
2838
2839 // Operation-dependent translation
2840 // TODO: Add as necessary
2841 switch (c2op) {
2842 case C2_OPERATION_Component_start:
2843 switch (c2s) {
2844 case C2_NO_MEMORY:
2845 return NO_MEMORY;
2846 default:
2847 return UNKNOWN_ERROR;
2848 }
2849 default:
2850 break;
2851 }
2852
2853 // Backup operation-agnostic translation
2854 switch (c2s) {
2855 case C2_BAD_INDEX:
2856 return BAD_INDEX;
2857 case C2_BAD_VALUE:
2858 return BAD_VALUE;
2859 case C2_BLOCKING:
2860 return WOULD_BLOCK;
2861 case C2_DUPLICATE:
2862 return ALREADY_EXISTS;
2863 case C2_NO_INIT:
2864 return NO_INIT;
2865 case C2_NO_MEMORY:
2866 return NO_MEMORY;
2867 case C2_NOT_FOUND:
2868 return NAME_NOT_FOUND;
2869 case C2_TIMED_OUT:
2870 return TIMED_OUT;
2871 case C2_BAD_STATE:
2872 case C2_CANCELED:
2873 case C2_CANNOT_DO:
2874 case C2_CORRUPTED:
2875 case C2_OMITTED:
2876 case C2_REFUSED:
2877 return UNKNOWN_ERROR;
2878 default:
2879 return -static_cast<status_t>(c2s);
2880 }
2881}
2882
2883} // namespace android