| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2017, The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | //#define LOG_NDEBUG 0 | 
|  | 18 | #define LOG_TAG "CCodecBufferChannel" | 
|  | 19 | #include <utils/Log.h> | 
|  | 20 |  | 
| Pawin Vongmasa | e7bb861 | 2020-06-04 06:15:22 -0700 | [diff] [blame] | 21 | #include <algorithm> | 
|  | 22 | #include <list> | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 23 | #include <numeric> | 
|  | 24 |  | 
|  | 25 | #include <C2AllocatorGralloc.h> | 
|  | 26 | #include <C2PlatformSupport.h> | 
|  | 27 | #include <C2BlockInternal.h> | 
|  | 28 | #include <C2Config.h> | 
|  | 29 | #include <C2Debug.h> | 
|  | 30 |  | 
|  | 31 | #include <android/hardware/cas/native/1.0/IDescrambler.h> | 
| Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 32 | #include <android/hardware/drm/1.0/types.h> | 
| Josh Hou | 8eddf4b | 2021-02-02 16:26:53 +0800 | [diff] [blame] | 33 | #include <android-base/properties.h> | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 34 | #include <android-base/stringprintf.h> | 
| Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 35 | #include <binder/MemoryBase.h> | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 36 | #include <binder/MemoryDealer.h> | 
| Ray Essick | 18ea045 | 2019-08-27 16:07:27 -0700 | [diff] [blame] | 37 | #include <cutils/properties.h> | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 38 | #include <gui/Surface.h> | 
| Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 39 | #include <hidlmemory/FrameworkUtils.h> | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 40 | #include <media/openmax/OMX_Core.h> | 
|  | 41 | #include <media/stagefright/foundation/ABuffer.h> | 
|  | 42 | #include <media/stagefright/foundation/ALookup.h> | 
|  | 43 | #include <media/stagefright/foundation/AMessage.h> | 
|  | 44 | #include <media/stagefright/foundation/AUtils.h> | 
|  | 45 | #include <media/stagefright/foundation/hexdump.h> | 
|  | 46 | #include <media/stagefright/MediaCodec.h> | 
|  | 47 | #include <media/stagefright/MediaCodecConstants.h> | 
| Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 48 | #include <media/stagefright/SkipCutBuffer.h> | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 49 | #include <media/MediaCodecBuffer.h> | 
| Wonsik Kim | 41d8343 | 2020-04-27 16:40:49 -0700 | [diff] [blame] | 50 | #include <mediadrm/ICrypto.h> | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 51 | #include <system/window.h> | 
|  | 52 |  | 
|  | 53 | #include "CCodecBufferChannel.h" | 
|  | 54 | #include "Codec2Buffer.h" | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 55 |  | 
|  | 56 | namespace android { | 
|  | 57 |  | 
|  | 58 | using android::base::StringPrintf; | 
|  | 59 | using hardware::hidl_handle; | 
|  | 60 | using hardware::hidl_string; | 
|  | 61 | using hardware::hidl_vec; | 
| Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 62 | using hardware::fromHeap; | 
|  | 63 | using hardware::HidlMemory; | 
|  | 64 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 65 | using namespace hardware::cas::V1_0; | 
|  | 66 | using namespace hardware::cas::native::V1_0; | 
|  | 67 |  | 
|  | 68 | using CasStatus = hardware::cas::V1_0::Status; | 
| Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 69 | using DrmBufferType = hardware::drm::V1_0::BufferType; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 70 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 71 | namespace { | 
|  | 72 |  | 
| Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 73 | constexpr size_t kSmoothnessFactor = 4; | 
|  | 74 | constexpr size_t kRenderingDepth = 3; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 75 |  | 
| Sungtak Lee | ab6f2f3 | 2019-02-15 14:43:51 -0800 | [diff] [blame] | 76 | // This is for keeping IGBP's buffer dropping logic in legacy mode other | 
|  | 77 | // than making it non-blocking. Do not change this value. | 
|  | 78 | const static size_t kDequeueTimeoutNs = 0; | 
|  | 79 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 80 | }  // namespace | 
|  | 81 |  | 
|  | 82 | CCodecBufferChannel::QueueGuard::QueueGuard( | 
|  | 83 | CCodecBufferChannel::QueueSync &sync) : mSync(sync) { | 
|  | 84 | Mutex::Autolock l(mSync.mGuardLock); | 
|  | 85 | // At this point it's guaranteed that mSync is not under state transition, | 
|  | 86 | // as we are holding its mutex. | 
|  | 87 |  | 
|  | 88 | Mutexed<CCodecBufferChannel::QueueSync::Counter>::Locked count(mSync.mCount); | 
|  | 89 | if (count->value == -1) { | 
|  | 90 | mRunning = false; | 
|  | 91 | } else { | 
|  | 92 | ++count->value; | 
|  | 93 | mRunning = true; | 
|  | 94 | } | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | CCodecBufferChannel::QueueGuard::~QueueGuard() { | 
|  | 98 | if (mRunning) { | 
|  | 99 | // We are not holding mGuardLock at this point so that QueueSync::stop() can | 
|  | 100 | // keep holding the lock until mCount reaches zero. | 
|  | 101 | Mutexed<CCodecBufferChannel::QueueSync::Counter>::Locked count(mSync.mCount); | 
|  | 102 | --count->value; | 
|  | 103 | count->cond.broadcast(); | 
|  | 104 | } | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | void CCodecBufferChannel::QueueSync::start() { | 
|  | 108 | Mutex::Autolock l(mGuardLock); | 
|  | 109 | // If stopped, it goes to running state; otherwise no-op. | 
|  | 110 | Mutexed<Counter>::Locked count(mCount); | 
|  | 111 | if (count->value == -1) { | 
|  | 112 | count->value = 0; | 
|  | 113 | } | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | void CCodecBufferChannel::QueueSync::stop() { | 
|  | 117 | Mutex::Autolock l(mGuardLock); | 
|  | 118 | Mutexed<Counter>::Locked count(mCount); | 
|  | 119 | if (count->value == -1) { | 
|  | 120 | // no-op | 
|  | 121 | return; | 
|  | 122 | } | 
|  | 123 | // Holding mGuardLock here blocks creation of additional QueueGuard objects, so | 
|  | 124 | // mCount can only decrement. In other words, threads that acquired the lock | 
|  | 125 | // are allowed to finish execution but additional threads trying to acquire | 
|  | 126 | // the lock at this point will block, and then get QueueGuard at STOPPED | 
|  | 127 | // state. | 
|  | 128 | while (count->value != 0) { | 
|  | 129 | count.waitForCondition(count->cond); | 
|  | 130 | } | 
|  | 131 | count->value = -1; | 
|  | 132 | } | 
|  | 133 |  | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 134 | // Input | 
|  | 135 |  | 
|  | 136 | CCodecBufferChannel::Input::Input() : extraBuffers("extra") {} | 
|  | 137 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 138 | // CCodecBufferChannel | 
|  | 139 |  | 
|  | 140 | CCodecBufferChannel::CCodecBufferChannel( | 
|  | 141 | const std::shared_ptr<CCodecCallback> &callback) | 
|  | 142 | : mHeapSeqNum(-1), | 
|  | 143 | mCCodecCallback(callback), | 
|  | 144 | mFrameIndex(0u), | 
|  | 145 | mFirstValidFrameIndex(0u), | 
|  | 146 | mMetaMode(MODE_NONE), | 
| Sungtak Lee | 04b3035 | 2020-07-27 13:57:25 -0700 | [diff] [blame] | 147 | mInputMetEos(false), | 
|  | 148 | mSendEncryptedInfoBuffer(false) { | 
| Sungtak Lee | d7463d1 | 2019-09-04 16:01:00 -0700 | [diff] [blame] | 149 | mOutputSurface.lock()->maxDequeueBuffers = kSmoothnessFactor + kRenderingDepth; | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 150 | { | 
|  | 151 | Mutexed<Input>::Locked input(mInput); | 
|  | 152 | input->buffers.reset(new DummyInputBuffers("")); | 
|  | 153 | input->extraBuffers.flush(); | 
|  | 154 | input->inputDelay = 0u; | 
|  | 155 | input->pipelineDelay = 0u; | 
|  | 156 | input->numSlots = kSmoothnessFactor; | 
|  | 157 | input->numExtraSlots = 0u; | 
|  | 158 | } | 
|  | 159 | { | 
|  | 160 | Mutexed<Output>::Locked output(mOutput); | 
|  | 161 | output->outputDelay = 0u; | 
|  | 162 | output->numSlots = kSmoothnessFactor; | 
|  | 163 | } | 
| David Stevens | c3fbb28 | 2021-01-18 18:11:20 +0900 | [diff] [blame] | 164 | { | 
|  | 165 | Mutexed<BlockPools>::Locked pools(mBlockPools); | 
|  | 166 | pools->outputPoolId = C2BlockPool::BASIC_LINEAR; | 
|  | 167 | } | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 168 | } | 
|  | 169 |  | 
|  | 170 | CCodecBufferChannel::~CCodecBufferChannel() { | 
| Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 171 | if (mCrypto != nullptr && mHeapSeqNum >= 0) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 172 | mCrypto->unsetHeap(mHeapSeqNum); | 
|  | 173 | } | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | void CCodecBufferChannel::setComponent( | 
|  | 177 | const std::shared_ptr<Codec2Client::Component> &component) { | 
|  | 178 | mComponent = component; | 
|  | 179 | mComponentName = component->getName() + StringPrintf("#%d", int(uintptr_t(component.get()) % 997)); | 
|  | 180 | mName = mComponentName.c_str(); | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | status_t CCodecBufferChannel::setInputSurface( | 
|  | 184 | const std::shared_ptr<InputSurfaceWrapper> &surface) { | 
|  | 185 | ALOGV("[%s] setInputSurface", mName); | 
|  | 186 | mInputSurface = surface; | 
|  | 187 | return mInputSurface->connect(mComponent); | 
|  | 188 | } | 
|  | 189 |  | 
|  | 190 | status_t CCodecBufferChannel::signalEndOfInputStream() { | 
|  | 191 | if (mInputSurface == nullptr) { | 
|  | 192 | return INVALID_OPERATION; | 
|  | 193 | } | 
|  | 194 | return mInputSurface->signalEndOfInputStream(); | 
|  | 195 | } | 
|  | 196 |  | 
| Sungtak Lee | 04b3035 | 2020-07-27 13:57:25 -0700 | [diff] [blame] | 197 | status_t CCodecBufferChannel::queueInputBufferInternal( | 
|  | 198 | sp<MediaCodecBuffer> buffer, | 
|  | 199 | std::shared_ptr<C2LinearBlock> encryptedBlock, | 
|  | 200 | size_t blockSize) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 201 | int64_t timeUs; | 
|  | 202 | CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); | 
|  | 203 |  | 
|  | 204 | if (mInputMetEos) { | 
|  | 205 | ALOGD("[%s] buffers after EOS ignored (%lld us)", mName, (long long)timeUs); | 
|  | 206 | return OK; | 
|  | 207 | } | 
|  | 208 |  | 
|  | 209 | int32_t flags = 0; | 
|  | 210 | int32_t tmp = 0; | 
|  | 211 | bool eos = false; | 
| Guillaume Chelfi | 867d4dd | 2021-07-01 18:38:45 +0200 | [diff] [blame] | 212 | bool tunnelFirstFrame = false; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 213 | if (buffer->meta()->findInt32("eos", &tmp) && tmp) { | 
|  | 214 | eos = true; | 
|  | 215 | mInputMetEos = true; | 
|  | 216 | ALOGV("[%s] input EOS", mName); | 
|  | 217 | } | 
|  | 218 | if (buffer->meta()->findInt32("csd", &tmp) && tmp) { | 
|  | 219 | flags |= C2FrameData::FLAG_CODEC_CONFIG; | 
|  | 220 | } | 
| Guillaume Chelfi | 867d4dd | 2021-07-01 18:38:45 +0200 | [diff] [blame] | 221 | if (buffer->meta()->findInt32("tunnel-first-frame", &tmp) && tmp) { | 
|  | 222 | tunnelFirstFrame = true; | 
|  | 223 | } | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 224 | ALOGV("[%s] queueInputBuffer: buffer->size() = %zu", mName, buffer->size()); | 
| Wonsik Kim | e1104ca | 2020-11-24 15:01:33 -0800 | [diff] [blame] | 225 | std::list<std::unique_ptr<C2Work>> items; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 226 | std::unique_ptr<C2Work> work(new C2Work); | 
|  | 227 | work->input.ordinal.timestamp = timeUs; | 
|  | 228 | work->input.ordinal.frameIndex = mFrameIndex++; | 
|  | 229 | // WORKAROUND: until codecs support handling work after EOS and max output sizing, use timestamp | 
|  | 230 | // manipulation to achieve image encoding via video codec, and to constrain encoded output. | 
|  | 231 | // Keep client timestamp in customOrdinal | 
|  | 232 | work->input.ordinal.customOrdinal = timeUs; | 
|  | 233 | work->input.buffers.clear(); | 
|  | 234 |  | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 235 | sp<Codec2Buffer> copy; | 
| Wonsik Kim | e1104ca | 2020-11-24 15:01:33 -0800 | [diff] [blame] | 236 | bool usesFrameReassembler = false; | 
| Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 237 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 238 | if (buffer->size() > 0u) { | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 239 | Mutexed<Input>::Locked input(mInput); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 240 | std::shared_ptr<C2Buffer> c2buffer; | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 241 | if (!input->buffers->releaseBuffer(buffer, &c2buffer, false)) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 242 | return -ENOENT; | 
|  | 243 | } | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 244 | // TODO: we want to delay copying buffers. | 
|  | 245 | if (input->extraBuffers.numComponentBuffers() < input->numExtraSlots) { | 
|  | 246 | copy = input->buffers->cloneAndReleaseBuffer(buffer); | 
|  | 247 | if (copy != nullptr) { | 
|  | 248 | (void)input->extraBuffers.assignSlot(copy); | 
|  | 249 | if (!input->extraBuffers.releaseSlot(copy, &c2buffer, false)) { | 
|  | 250 | return UNKNOWN_ERROR; | 
|  | 251 | } | 
|  | 252 | bool released = input->buffers->releaseBuffer(buffer, nullptr, true); | 
|  | 253 | ALOGV("[%s] queueInputBuffer: buffer copied; %sreleased", | 
|  | 254 | mName, released ? "" : "not "); | 
| Wonsik Kim | fb5ca49 | 2021-08-11 14:18:19 -0700 | [diff] [blame] | 255 | buffer = copy; | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 256 | } else { | 
|  | 257 | ALOGW("[%s] queueInputBuffer: failed to copy a buffer; this may cause input " | 
|  | 258 | "buffer starvation on component.", mName); | 
|  | 259 | } | 
|  | 260 | } | 
| Wonsik Kim | e1104ca | 2020-11-24 15:01:33 -0800 | [diff] [blame] | 261 | if (input->frameReassembler) { | 
|  | 262 | usesFrameReassembler = true; | 
|  | 263 | input->frameReassembler.process(buffer, &items); | 
|  | 264 | } else { | 
| Byeongjo Park | 25c3a3d | 2020-06-12 17:24:21 +0900 | [diff] [blame] | 265 | int32_t cvo = 0; | 
|  | 266 | if (buffer->meta()->findInt32("cvo", &cvo)) { | 
|  | 267 | int32_t rotation = cvo % 360; | 
|  | 268 | // change rotation to counter-clock wise. | 
|  | 269 | rotation = ((rotation <= 0) ? 0 : 360) - rotation; | 
|  | 270 |  | 
|  | 271 | Mutexed<OutputSurface>::Locked output(mOutputSurface); | 
|  | 272 | uint64_t frameIndex = work->input.ordinal.frameIndex.peeku(); | 
|  | 273 | output->rotation[frameIndex] = rotation; | 
|  | 274 | } | 
| Wonsik Kim | e1104ca | 2020-11-24 15:01:33 -0800 | [diff] [blame] | 275 | work->input.buffers.push_back(c2buffer); | 
|  | 276 | if (encryptedBlock) { | 
|  | 277 | work->input.infoBuffers.emplace_back(C2InfoBuffer::CreateLinearBuffer( | 
|  | 278 | kParamIndexEncryptedBuffer, | 
|  | 279 | encryptedBlock->share(0, blockSize, C2Fence()))); | 
|  | 280 | } | 
| Sungtak Lee | 04b3035 | 2020-07-27 13:57:25 -0700 | [diff] [blame] | 281 | } | 
| Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 282 | } else if (eos) { | 
| Wonsik Kim | cc59ad8 | 2021-08-11 18:15:19 -0700 | [diff] [blame] | 283 | Mutexed<Input>::Locked input(mInput); | 
|  | 284 | if (input->frameReassembler) { | 
|  | 285 | usesFrameReassembler = true; | 
|  | 286 | // drain any pending items with eos | 
|  | 287 | input->frameReassembler.process(buffer, &items); | 
|  | 288 | } | 
| Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 289 | flags |= C2FrameData::FLAG_END_OF_STREAM; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 290 | } | 
| Wonsik Kim | e1104ca | 2020-11-24 15:01:33 -0800 | [diff] [blame] | 291 | if (usesFrameReassembler) { | 
|  | 292 | if (!items.empty()) { | 
|  | 293 | items.front()->input.configUpdate = std::move(mParamsToBeSet); | 
|  | 294 | mFrameIndex = (items.back()->input.ordinal.frameIndex + 1).peek(); | 
|  | 295 | } | 
|  | 296 | } else { | 
|  | 297 | work->input.flags = (C2FrameData::flags_t)flags; | 
|  | 298 | // TODO: fill info's | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 299 |  | 
| Wonsik Kim | e1104ca | 2020-11-24 15:01:33 -0800 | [diff] [blame] | 300 | work->input.configUpdate = std::move(mParamsToBeSet); | 
| Guillaume Chelfi | 867d4dd | 2021-07-01 18:38:45 +0200 | [diff] [blame] | 301 | if (tunnelFirstFrame) { | 
|  | 302 | C2StreamTunnelHoldRender::input tunnelHoldRender{ | 
|  | 303 | 0u /* stream */, | 
|  | 304 | C2_TRUE /* value */ | 
|  | 305 | }; | 
|  | 306 | work->input.configUpdate.push_back(C2Param::Copy(tunnelHoldRender)); | 
|  | 307 | } | 
| Wonsik Kim | e1104ca | 2020-11-24 15:01:33 -0800 | [diff] [blame] | 308 | work->worklets.clear(); | 
|  | 309 | work->worklets.emplace_back(new C2Worklet); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 310 |  | 
| Wonsik Kim | e1104ca | 2020-11-24 15:01:33 -0800 | [diff] [blame] | 311 | items.push_back(std::move(work)); | 
|  | 312 |  | 
|  | 313 | eos = eos && buffer->size() > 0u; | 
| Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 314 | } | 
| Wonsik Kim | e1104ca | 2020-11-24 15:01:33 -0800 | [diff] [blame] | 315 | if (eos) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 316 | work.reset(new C2Work); | 
|  | 317 | work->input.ordinal.timestamp = timeUs; | 
|  | 318 | work->input.ordinal.frameIndex = mFrameIndex++; | 
|  | 319 | // WORKAROUND: keep client timestamp in customOrdinal | 
|  | 320 | work->input.ordinal.customOrdinal = timeUs; | 
|  | 321 | work->input.buffers.clear(); | 
|  | 322 | work->input.flags = C2FrameData::FLAG_END_OF_STREAM; | 
| Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 323 | work->worklets.emplace_back(new C2Worklet); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 324 | items.push_back(std::move(work)); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 325 | } | 
| Wonsik Kim | e1104ca | 2020-11-24 15:01:33 -0800 | [diff] [blame] | 326 | c2_status_t err = C2_OK; | 
|  | 327 | if (!items.empty()) { | 
|  | 328 | { | 
|  | 329 | Mutexed<PipelineWatcher>::Locked watcher(mPipelineWatcher); | 
|  | 330 | PipelineWatcher::Clock::time_point now = PipelineWatcher::Clock::now(); | 
|  | 331 | for (const std::unique_ptr<C2Work> &work : items) { | 
|  | 332 | watcher->onWorkQueued( | 
|  | 333 | work->input.ordinal.frameIndex.peeku(), | 
|  | 334 | std::vector(work->input.buffers), | 
|  | 335 | now); | 
|  | 336 | } | 
|  | 337 | } | 
|  | 338 | err = mComponent->queue(&items); | 
|  | 339 | } | 
|  | 340 | if (err != C2_OK) { | 
|  | 341 | Mutexed<PipelineWatcher>::Locked watcher(mPipelineWatcher); | 
|  | 342 | for (const std::unique_ptr<C2Work> &work : items) { | 
|  | 343 | watcher->onWorkDone(work->input.ordinal.frameIndex.peeku()); | 
|  | 344 | } | 
|  | 345 | } else { | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 346 | Mutexed<Input>::Locked input(mInput); | 
|  | 347 | bool released = false; | 
| Wonsik Kim | fb5ca49 | 2021-08-11 14:18:19 -0700 | [diff] [blame] | 348 | if (copy) { | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 349 | released = input->extraBuffers.releaseSlot(copy, nullptr, true); | 
| Wonsik Kim | fb5ca49 | 2021-08-11 14:18:19 -0700 | [diff] [blame] | 350 | } else if (buffer) { | 
|  | 351 | released = input->buffers->releaseBuffer(buffer, nullptr, true); | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 352 | } | 
|  | 353 | ALOGV("[%s] queueInputBuffer: buffer%s %sreleased", | 
|  | 354 | mName, (buffer == nullptr) ? "(copy)" : "", released ? "" : "not "); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 355 | } | 
|  | 356 |  | 
|  | 357 | feedInputBufferIfAvailableInternal(); | 
|  | 358 | return err; | 
|  | 359 | } | 
|  | 360 |  | 
|  | 361 | status_t CCodecBufferChannel::setParameters(std::vector<std::unique_ptr<C2Param>> ¶ms) { | 
|  | 362 | QueueGuard guard(mSync); | 
|  | 363 | if (!guard.isRunning()) { | 
|  | 364 | ALOGD("[%s] setParameters is only supported in the running state.", mName); | 
|  | 365 | return -ENOSYS; | 
|  | 366 | } | 
|  | 367 | mParamsToBeSet.insert(mParamsToBeSet.end(), | 
|  | 368 | std::make_move_iterator(params.begin()), | 
|  | 369 | std::make_move_iterator(params.end())); | 
|  | 370 | params.clear(); | 
|  | 371 | return OK; | 
|  | 372 | } | 
|  | 373 |  | 
| Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 374 | status_t CCodecBufferChannel::attachBuffer( | 
|  | 375 | const std::shared_ptr<C2Buffer> &c2Buffer, | 
|  | 376 | const sp<MediaCodecBuffer> &buffer) { | 
|  | 377 | if (!buffer->copy(c2Buffer)) { | 
|  | 378 | return -ENOSYS; | 
|  | 379 | } | 
|  | 380 | return OK; | 
|  | 381 | } | 
|  | 382 |  | 
|  | 383 | void CCodecBufferChannel::ensureDecryptDestination(size_t size) { | 
|  | 384 | if (!mDecryptDestination || mDecryptDestination->size() < size) { | 
|  | 385 | sp<IMemoryHeap> heap{new MemoryHeapBase(size * 2)}; | 
|  | 386 | if (mDecryptDestination && mCrypto && mHeapSeqNum >= 0) { | 
|  | 387 | mCrypto->unsetHeap(mHeapSeqNum); | 
|  | 388 | } | 
|  | 389 | mDecryptDestination = new MemoryBase(heap, 0, size * 2); | 
|  | 390 | if (mCrypto) { | 
|  | 391 | mHeapSeqNum = mCrypto->setHeap(hardware::fromHeap(heap)); | 
|  | 392 | } | 
|  | 393 | } | 
|  | 394 | } | 
|  | 395 |  | 
|  | 396 | int32_t CCodecBufferChannel::getHeapSeqNum(const sp<HidlMemory> &memory) { | 
|  | 397 | CHECK(mCrypto); | 
|  | 398 | auto it = mHeapSeqNumMap.find(memory); | 
|  | 399 | int32_t heapSeqNum = -1; | 
|  | 400 | if (it == mHeapSeqNumMap.end()) { | 
|  | 401 | heapSeqNum = mCrypto->setHeap(memory); | 
|  | 402 | mHeapSeqNumMap.emplace(memory, heapSeqNum); | 
|  | 403 | } else { | 
|  | 404 | heapSeqNum = it->second; | 
|  | 405 | } | 
|  | 406 | return heapSeqNum; | 
|  | 407 | } | 
|  | 408 |  | 
|  | 409 | status_t CCodecBufferChannel::attachEncryptedBuffer( | 
|  | 410 | const sp<hardware::HidlMemory> &memory, | 
|  | 411 | bool secure, | 
|  | 412 | const uint8_t *key, | 
|  | 413 | const uint8_t *iv, | 
|  | 414 | CryptoPlugin::Mode mode, | 
|  | 415 | CryptoPlugin::Pattern pattern, | 
|  | 416 | size_t offset, | 
|  | 417 | const CryptoPlugin::SubSample *subSamples, | 
|  | 418 | size_t numSubSamples, | 
|  | 419 | const sp<MediaCodecBuffer> &buffer) { | 
|  | 420 | static const C2MemoryUsage kSecureUsage{C2MemoryUsage::READ_PROTECTED, 0}; | 
|  | 421 | static const C2MemoryUsage kDefaultReadWriteUsage{ | 
|  | 422 | C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE}; | 
|  | 423 |  | 
|  | 424 | size_t size = 0; | 
|  | 425 | for (size_t i = 0; i < numSubSamples; ++i) { | 
|  | 426 | size += subSamples[i].mNumBytesOfClearData + subSamples[i].mNumBytesOfEncryptedData; | 
|  | 427 | } | 
|  | 428 | std::shared_ptr<C2BlockPool> pool = mBlockPools.lock()->inputPool; | 
|  | 429 | std::shared_ptr<C2LinearBlock> block; | 
|  | 430 | c2_status_t err = pool->fetchLinearBlock( | 
|  | 431 | size, | 
|  | 432 | secure ? kSecureUsage : kDefaultReadWriteUsage, | 
|  | 433 | &block); | 
|  | 434 | if (err != C2_OK) { | 
|  | 435 | return NO_MEMORY; | 
|  | 436 | } | 
|  | 437 | if (!secure) { | 
|  | 438 | ensureDecryptDestination(size); | 
|  | 439 | } | 
|  | 440 | ssize_t result = -1; | 
|  | 441 | ssize_t codecDataOffset = 0; | 
|  | 442 | if (mCrypto) { | 
|  | 443 | AString errorDetailMsg; | 
|  | 444 | int32_t heapSeqNum = getHeapSeqNum(memory); | 
|  | 445 | hardware::drm::V1_0::SharedBuffer src{(uint32_t)heapSeqNum, offset, size}; | 
|  | 446 | hardware::drm::V1_0::DestinationBuffer dst; | 
|  | 447 | if (secure) { | 
|  | 448 | dst.type = DrmBufferType::NATIVE_HANDLE; | 
|  | 449 | dst.secureMemory = hardware::hidl_handle(block->handle()); | 
|  | 450 | } else { | 
|  | 451 | dst.type = DrmBufferType::SHARED_MEMORY; | 
|  | 452 | IMemoryToSharedBuffer( | 
|  | 453 | mDecryptDestination, mHeapSeqNum, &dst.nonsecureMemory); | 
|  | 454 | } | 
|  | 455 | result = mCrypto->decrypt( | 
|  | 456 | key, iv, mode, pattern, src, 0, subSamples, numSubSamples, | 
|  | 457 | dst, &errorDetailMsg); | 
|  | 458 | if (result < 0) { | 
|  | 459 | return result; | 
|  | 460 | } | 
|  | 461 | if (dst.type == DrmBufferType::SHARED_MEMORY) { | 
|  | 462 | C2WriteView view = block->map().get(); | 
|  | 463 | if (view.error() != C2_OK) { | 
|  | 464 | return false; | 
|  | 465 | } | 
|  | 466 | if (view.size() < result) { | 
|  | 467 | return false; | 
|  | 468 | } | 
|  | 469 | memcpy(view.data(), mDecryptDestination->unsecurePointer(), result); | 
|  | 470 | } | 
|  | 471 | } else { | 
|  | 472 | // Here we cast CryptoPlugin::SubSample to hardware::cas::native::V1_0::SubSample | 
|  | 473 | // directly, the structure definitions should match as checked in DescramblerImpl.cpp. | 
|  | 474 | hidl_vec<SubSample> hidlSubSamples; | 
|  | 475 | hidlSubSamples.setToExternal((SubSample *)subSamples, numSubSamples, false /*own*/); | 
|  | 476 |  | 
|  | 477 | hardware::cas::native::V1_0::SharedBuffer src{*memory, offset, size}; | 
|  | 478 | hardware::cas::native::V1_0::DestinationBuffer dst; | 
|  | 479 | if (secure) { | 
|  | 480 | dst.type = BufferType::NATIVE_HANDLE; | 
|  | 481 | dst.secureMemory = hardware::hidl_handle(block->handle()); | 
|  | 482 | } else { | 
|  | 483 | dst.type = BufferType::SHARED_MEMORY; | 
|  | 484 | dst.nonsecureMemory = src; | 
|  | 485 | } | 
|  | 486 |  | 
|  | 487 | CasStatus status = CasStatus::OK; | 
|  | 488 | hidl_string detailedError; | 
|  | 489 | ScramblingControl sctrl = ScramblingControl::UNSCRAMBLED; | 
|  | 490 |  | 
|  | 491 | if (key != nullptr) { | 
|  | 492 | sctrl = (ScramblingControl)key[0]; | 
|  | 493 | // Adjust for the PES offset | 
|  | 494 | codecDataOffset = key[2] | (key[3] << 8); | 
|  | 495 | } | 
|  | 496 |  | 
|  | 497 | auto returnVoid = mDescrambler->descramble( | 
|  | 498 | sctrl, | 
|  | 499 | hidlSubSamples, | 
|  | 500 | src, | 
|  | 501 | 0, | 
|  | 502 | dst, | 
|  | 503 | 0, | 
|  | 504 | [&status, &result, &detailedError] ( | 
|  | 505 | CasStatus _status, uint32_t _bytesWritten, | 
|  | 506 | const hidl_string& _detailedError) { | 
|  | 507 | status = _status; | 
|  | 508 | result = (ssize_t)_bytesWritten; | 
|  | 509 | detailedError = _detailedError; | 
|  | 510 | }); | 
|  | 511 |  | 
|  | 512 | if (!returnVoid.isOk() || status != CasStatus::OK || result < 0) { | 
|  | 513 | ALOGI("[%s] descramble failed, trans=%s, status=%d, result=%zd", | 
|  | 514 | mName, returnVoid.description().c_str(), status, result); | 
|  | 515 | return UNKNOWN_ERROR; | 
|  | 516 | } | 
|  | 517 |  | 
|  | 518 | if (result < codecDataOffset) { | 
|  | 519 | ALOGD("invalid codec data offset: %zd, result %zd", codecDataOffset, result); | 
|  | 520 | return BAD_VALUE; | 
|  | 521 | } | 
|  | 522 | } | 
|  | 523 | if (!secure) { | 
|  | 524 | C2WriteView view = block->map().get(); | 
|  | 525 | if (view.error() != C2_OK) { | 
|  | 526 | return UNKNOWN_ERROR; | 
|  | 527 | } | 
|  | 528 | if (view.size() < result) { | 
|  | 529 | return UNKNOWN_ERROR; | 
|  | 530 | } | 
|  | 531 | memcpy(view.data(), mDecryptDestination->unsecurePointer(), result); | 
|  | 532 | } | 
|  | 533 | std::shared_ptr<C2Buffer> c2Buffer{C2Buffer::CreateLinearBuffer( | 
|  | 534 | block->share(codecDataOffset, result - codecDataOffset, C2Fence{}))}; | 
|  | 535 | if (!buffer->copy(c2Buffer)) { | 
|  | 536 | return -ENOSYS; | 
|  | 537 | } | 
|  | 538 | return OK; | 
|  | 539 | } | 
|  | 540 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 541 | status_t CCodecBufferChannel::queueInputBuffer(const sp<MediaCodecBuffer> &buffer) { | 
|  | 542 | QueueGuard guard(mSync); | 
|  | 543 | if (!guard.isRunning()) { | 
|  | 544 | ALOGD("[%s] No more buffers should be queued at current state.", mName); | 
|  | 545 | return -ENOSYS; | 
|  | 546 | } | 
|  | 547 | return queueInputBufferInternal(buffer); | 
|  | 548 | } | 
|  | 549 |  | 
|  | 550 | status_t CCodecBufferChannel::queueSecureInputBuffer( | 
|  | 551 | const sp<MediaCodecBuffer> &buffer, bool secure, const uint8_t *key, | 
|  | 552 | const uint8_t *iv, CryptoPlugin::Mode mode, CryptoPlugin::Pattern pattern, | 
|  | 553 | const CryptoPlugin::SubSample *subSamples, size_t numSubSamples, | 
|  | 554 | AString *errorDetailMsg) { | 
|  | 555 | QueueGuard guard(mSync); | 
|  | 556 | if (!guard.isRunning()) { | 
|  | 557 | ALOGD("[%s] No more buffers should be queued at current state.", mName); | 
|  | 558 | return -ENOSYS; | 
|  | 559 | } | 
|  | 560 |  | 
|  | 561 | if (!hasCryptoOrDescrambler()) { | 
|  | 562 | return -ENOSYS; | 
|  | 563 | } | 
|  | 564 | sp<EncryptedLinearBlockBuffer> encryptedBuffer((EncryptedLinearBlockBuffer *)buffer.get()); | 
|  | 565 |  | 
| Sungtak Lee | 04b3035 | 2020-07-27 13:57:25 -0700 | [diff] [blame] | 566 | std::shared_ptr<C2LinearBlock> block; | 
|  | 567 | size_t allocSize = buffer->size(); | 
|  | 568 | size_t bufferSize = 0; | 
|  | 569 | c2_status_t blockRes = C2_OK; | 
|  | 570 | bool copied = false; | 
|  | 571 | if (mSendEncryptedInfoBuffer) { | 
|  | 572 | static const C2MemoryUsage kDefaultReadWriteUsage{ | 
|  | 573 | C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE}; | 
|  | 574 | constexpr int kAllocGranule0 = 1024 * 64; | 
|  | 575 | constexpr int kAllocGranule1 = 1024 * 1024; | 
|  | 576 | std::shared_ptr<C2BlockPool> pool = mBlockPools.lock()->inputPool; | 
|  | 577 | // round up encrypted sizes to limit fragmentation and encourage buffer reuse | 
|  | 578 | if (allocSize <= kAllocGranule1) { | 
|  | 579 | bufferSize = align(allocSize, kAllocGranule0); | 
|  | 580 | } else { | 
|  | 581 | bufferSize = align(allocSize, kAllocGranule1); | 
|  | 582 | } | 
|  | 583 | blockRes = pool->fetchLinearBlock( | 
|  | 584 | bufferSize, kDefaultReadWriteUsage, &block); | 
|  | 585 |  | 
|  | 586 | if (blockRes == C2_OK) { | 
|  | 587 | C2WriteView view = block->map().get(); | 
|  | 588 | if (view.error() == C2_OK && view.size() == bufferSize) { | 
|  | 589 | copied = true; | 
|  | 590 | // TODO: only copy clear sections | 
|  | 591 | memcpy(view.data(), buffer->data(), allocSize); | 
|  | 592 | } | 
|  | 593 | } | 
|  | 594 | } | 
|  | 595 |  | 
|  | 596 | if (!copied) { | 
|  | 597 | block.reset(); | 
|  | 598 | } | 
|  | 599 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 600 | ssize_t result = -1; | 
|  | 601 | ssize_t codecDataOffset = 0; | 
| Wonsik Kim | 557c88c | 2020-03-13 11:03:52 -0700 | [diff] [blame] | 602 | if (numSubSamples == 1 | 
|  | 603 | && subSamples[0].mNumBytesOfClearData == 0 | 
|  | 604 | && subSamples[0].mNumBytesOfEncryptedData == 0) { | 
|  | 605 | // We don't need to go through crypto or descrambler if the input is empty. | 
|  | 606 | result = 0; | 
|  | 607 | } else if (mCrypto != nullptr) { | 
| Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 608 | hardware::drm::V1_0::DestinationBuffer destination; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 609 | if (secure) { | 
| Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 610 | destination.type = DrmBufferType::NATIVE_HANDLE; | 
|  | 611 | destination.secureMemory = hidl_handle(encryptedBuffer->handle()); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 612 | } else { | 
| Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 613 | destination.type = DrmBufferType::SHARED_MEMORY; | 
|  | 614 | IMemoryToSharedBuffer( | 
|  | 615 | mDecryptDestination, mHeapSeqNum, &destination.nonsecureMemory); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 616 | } | 
| Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 617 | hardware::drm::V1_0::SharedBuffer source; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 618 | encryptedBuffer->fillSourceBuffer(&source); | 
|  | 619 | result = mCrypto->decrypt( | 
|  | 620 | key, iv, mode, pattern, source, buffer->offset(), | 
|  | 621 | subSamples, numSubSamples, destination, errorDetailMsg); | 
|  | 622 | if (result < 0) { | 
| Wonsik Kim | 557c88c | 2020-03-13 11:03:52 -0700 | [diff] [blame] | 623 | ALOGI("[%s] decrypt failed: result=%zd", mName, result); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 624 | return result; | 
|  | 625 | } | 
| Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 626 | if (destination.type == DrmBufferType::SHARED_MEMORY) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 627 | encryptedBuffer->copyDecryptedContent(mDecryptDestination, result); | 
|  | 628 | } | 
|  | 629 | } else { | 
|  | 630 | // Here we cast CryptoPlugin::SubSample to hardware::cas::native::V1_0::SubSample | 
|  | 631 | // directly, the structure definitions should match as checked in DescramblerImpl.cpp. | 
|  | 632 | hidl_vec<SubSample> hidlSubSamples; | 
|  | 633 | hidlSubSamples.setToExternal((SubSample *)subSamples, numSubSamples, false /*own*/); | 
|  | 634 |  | 
|  | 635 | hardware::cas::native::V1_0::SharedBuffer srcBuffer; | 
|  | 636 | encryptedBuffer->fillSourceBuffer(&srcBuffer); | 
|  | 637 |  | 
|  | 638 | DestinationBuffer dstBuffer; | 
|  | 639 | if (secure) { | 
|  | 640 | dstBuffer.type = BufferType::NATIVE_HANDLE; | 
|  | 641 | dstBuffer.secureMemory = hidl_handle(encryptedBuffer->handle()); | 
|  | 642 | } else { | 
|  | 643 | dstBuffer.type = BufferType::SHARED_MEMORY; | 
|  | 644 | dstBuffer.nonsecureMemory = srcBuffer; | 
|  | 645 | } | 
|  | 646 |  | 
|  | 647 | CasStatus status = CasStatus::OK; | 
|  | 648 | hidl_string detailedError; | 
|  | 649 | ScramblingControl sctrl = ScramblingControl::UNSCRAMBLED; | 
|  | 650 |  | 
|  | 651 | if (key != nullptr) { | 
|  | 652 | sctrl = (ScramblingControl)key[0]; | 
|  | 653 | // Adjust for the PES offset | 
|  | 654 | codecDataOffset = key[2] | (key[3] << 8); | 
|  | 655 | } | 
|  | 656 |  | 
|  | 657 | auto returnVoid = mDescrambler->descramble( | 
|  | 658 | sctrl, | 
|  | 659 | hidlSubSamples, | 
|  | 660 | srcBuffer, | 
|  | 661 | 0, | 
|  | 662 | dstBuffer, | 
|  | 663 | 0, | 
|  | 664 | [&status, &result, &detailedError] ( | 
|  | 665 | CasStatus _status, uint32_t _bytesWritten, | 
|  | 666 | const hidl_string& _detailedError) { | 
|  | 667 | status = _status; | 
|  | 668 | result = (ssize_t)_bytesWritten; | 
|  | 669 | detailedError = _detailedError; | 
|  | 670 | }); | 
|  | 671 |  | 
|  | 672 | if (!returnVoid.isOk() || status != CasStatus::OK || result < 0) { | 
|  | 673 | ALOGI("[%s] descramble failed, trans=%s, status=%d, result=%zd", | 
|  | 674 | mName, returnVoid.description().c_str(), status, result); | 
|  | 675 | return UNKNOWN_ERROR; | 
|  | 676 | } | 
|  | 677 |  | 
|  | 678 | if (result < codecDataOffset) { | 
|  | 679 | ALOGD("invalid codec data offset: %zd, result %zd", codecDataOffset, result); | 
|  | 680 | return BAD_VALUE; | 
|  | 681 | } | 
|  | 682 |  | 
|  | 683 | ALOGV("[%s] descramble succeeded, %zd bytes", mName, result); | 
|  | 684 |  | 
|  | 685 | if (dstBuffer.type == BufferType::SHARED_MEMORY) { | 
|  | 686 | encryptedBuffer->copyDecryptedContentFromMemory(result); | 
|  | 687 | } | 
|  | 688 | } | 
|  | 689 |  | 
|  | 690 | buffer->setRange(codecDataOffset, result - codecDataOffset); | 
| Sungtak Lee | 04b3035 | 2020-07-27 13:57:25 -0700 | [diff] [blame] | 691 |  | 
|  | 692 | return queueInputBufferInternal(buffer, block, bufferSize); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 693 | } | 
|  | 694 |  | 
|  | 695 | void CCodecBufferChannel::feedInputBufferIfAvailable() { | 
|  | 696 | QueueGuard guard(mSync); | 
|  | 697 | if (!guard.isRunning()) { | 
|  | 698 | ALOGV("[%s] We're not running --- no input buffer reported", mName); | 
|  | 699 | return; | 
|  | 700 | } | 
|  | 701 | feedInputBufferIfAvailableInternal(); | 
|  | 702 | } | 
|  | 703 |  | 
|  | 704 | void CCodecBufferChannel::feedInputBufferIfAvailableInternal() { | 
| Taehwan Kim | da0517d | 2020-09-16 17:29:37 +0900 | [diff] [blame] | 705 | if (mInputMetEos) { | 
| Wonsik Kim | df5dd14 | 2019-02-06 10:15:46 -0800 | [diff] [blame] | 706 | return; | 
| Pawin Vongmasa | c3c536d | 2020-06-12 04:00:04 -0700 | [diff] [blame] | 707 | } | 
|  | 708 | { | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 709 | Mutexed<Output>::Locked output(mOutput); | 
| Pawin Vongmasa | c3c536d | 2020-06-12 04:00:04 -0700 | [diff] [blame] | 710 | if (!output->buffers || | 
|  | 711 | output->buffers->hasPending() || | 
| Wonsik Kim | 0487b78 | 2020-10-28 11:45:50 -0700 | [diff] [blame] | 712 | output->buffers->numActiveSlots() >= output->numSlots) { | 
| Wonsik Kim | df5dd14 | 2019-02-06 10:15:46 -0800 | [diff] [blame] | 713 | return; | 
|  | 714 | } | 
|  | 715 | } | 
| Wonsik Kim | 0487b78 | 2020-10-28 11:45:50 -0700 | [diff] [blame] | 716 | size_t numActiveSlots = 0; | 
|  | 717 | while (!mPipelineWatcher.lock()->pipelineFull()) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 718 | sp<MediaCodecBuffer> inBuffer; | 
|  | 719 | size_t index; | 
|  | 720 | { | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 721 | Mutexed<Input>::Locked input(mInput); | 
| Wonsik Kim | 0487b78 | 2020-10-28 11:45:50 -0700 | [diff] [blame] | 722 | numActiveSlots = input->buffers->numActiveSlots(); | 
|  | 723 | if (numActiveSlots >= input->numSlots) { | 
|  | 724 | break; | 
| Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 725 | } | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 726 | if (!input->buffers->requestNewBuffer(&index, &inBuffer)) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 727 | ALOGV("[%s] no new buffer available", mName); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 728 | break; | 
|  | 729 | } | 
|  | 730 | } | 
|  | 731 | ALOGV("[%s] new input index = %zu [%p]", mName, index, inBuffer.get()); | 
|  | 732 | mCallback->onInputBufferAvailable(index, inBuffer); | 
|  | 733 | } | 
| Wonsik Kim | 0487b78 | 2020-10-28 11:45:50 -0700 | [diff] [blame] | 734 | ALOGV("[%s] # active slots after feedInputBufferIfAvailable = %zu", mName, numActiveSlots); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 735 | } | 
|  | 736 |  | 
|  | 737 | status_t CCodecBufferChannel::renderOutputBuffer( | 
|  | 738 | const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) { | 
| Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 739 | ALOGV("[%s] renderOutputBuffer: %p", mName, buffer.get()); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 740 | std::shared_ptr<C2Buffer> c2Buffer; | 
| Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 741 | bool released = false; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 742 | { | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 743 | Mutexed<Output>::Locked output(mOutput); | 
|  | 744 | if (output->buffers) { | 
|  | 745 | released = output->buffers->releaseBuffer(buffer, &c2Buffer); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 746 | } | 
|  | 747 | } | 
| Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 748 | // NOTE: some apps try to releaseOutputBuffer() with timestamp and/or render | 
|  | 749 | //       set to true. | 
|  | 750 | sendOutputBuffers(); | 
|  | 751 | // input buffer feeding may have been gated by pending output buffers | 
|  | 752 | feedInputBufferIfAvailable(); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 753 | if (!c2Buffer) { | 
| Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 754 | if (released) { | 
| Wonsik Kim | f7529dd | 2019-04-18 17:35:53 -0700 | [diff] [blame] | 755 | std::call_once(mRenderWarningFlag, [this] { | 
|  | 756 | ALOGW("[%s] The app is calling releaseOutputBuffer() with " | 
|  | 757 | "timestamp or render=true with non-video buffers. Apps should " | 
|  | 758 | "call releaseOutputBuffer() with render=false for those.", | 
|  | 759 | mName); | 
|  | 760 | }); | 
| Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 761 | } | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 762 | return INVALID_OPERATION; | 
|  | 763 | } | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 764 |  | 
|  | 765 | #if 0 | 
|  | 766 | const std::vector<std::shared_ptr<const C2Info>> infoParams = c2Buffer->info(); | 
|  | 767 | ALOGV("[%s] queuing gfx buffer with %zu infos", mName, infoParams.size()); | 
|  | 768 | for (const std::shared_ptr<const C2Info> &info : infoParams) { | 
|  | 769 | AString res; | 
|  | 770 | for (size_t ix = 0; ix + 3 < info->size(); ix += 4) { | 
|  | 771 | if (ix) res.append(", "); | 
|  | 772 | res.append(*((int32_t*)info.get() + (ix / 4))); | 
|  | 773 | } | 
|  | 774 | ALOGV("  [%s]", res.c_str()); | 
|  | 775 | } | 
|  | 776 | #endif | 
|  | 777 | std::shared_ptr<const C2StreamRotationInfo::output> rotation = | 
|  | 778 | std::static_pointer_cast<const C2StreamRotationInfo::output>( | 
|  | 779 | c2Buffer->getInfo(C2StreamRotationInfo::output::PARAM_TYPE)); | 
|  | 780 | bool flip = rotation && (rotation->flip & 1); | 
|  | 781 | uint32_t quarters = ((rotation ? rotation->value : 0) / 90) & 3; | 
| Byeongjo Park | 25c3a3d | 2020-06-12 17:24:21 +0900 | [diff] [blame] | 782 |  | 
|  | 783 | { | 
|  | 784 | Mutexed<OutputSurface>::Locked output(mOutputSurface); | 
|  | 785 | if (output->surface == nullptr) { | 
|  | 786 | ALOGI("[%s] cannot render buffer without surface", mName); | 
|  | 787 | return OK; | 
|  | 788 | } | 
|  | 789 | int64_t frameIndex; | 
|  | 790 | buffer->meta()->findInt64("frameIndex", &frameIndex); | 
|  | 791 | if (output->rotation.count(frameIndex) != 0) { | 
|  | 792 | auto it = output->rotation.find(frameIndex); | 
|  | 793 | quarters = (it->second / 90) & 3; | 
|  | 794 | output->rotation.erase(it); | 
|  | 795 | } | 
|  | 796 | } | 
|  | 797 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 798 | uint32_t transform = 0; | 
|  | 799 | switch (quarters) { | 
|  | 800 | case 0: // no rotation | 
|  | 801 | transform = flip ? HAL_TRANSFORM_FLIP_H : 0; | 
|  | 802 | break; | 
|  | 803 | case 1: // 90 degrees counter-clockwise | 
|  | 804 | transform = flip ? (HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90) | 
|  | 805 | : HAL_TRANSFORM_ROT_270; | 
|  | 806 | break; | 
|  | 807 | case 2: // 180 degrees | 
|  | 808 | transform = flip ? HAL_TRANSFORM_FLIP_V : HAL_TRANSFORM_ROT_180; | 
|  | 809 | break; | 
|  | 810 | case 3: // 90 degrees clockwise | 
|  | 811 | transform = flip ? (HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90) | 
|  | 812 | : HAL_TRANSFORM_ROT_90; | 
|  | 813 | break; | 
|  | 814 | } | 
|  | 815 |  | 
|  | 816 | std::shared_ptr<const C2StreamSurfaceScalingInfo::output> surfaceScaling = | 
|  | 817 | std::static_pointer_cast<const C2StreamSurfaceScalingInfo::output>( | 
|  | 818 | c2Buffer->getInfo(C2StreamSurfaceScalingInfo::output::PARAM_TYPE)); | 
|  | 819 | uint32_t videoScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW; | 
|  | 820 | if (surfaceScaling) { | 
|  | 821 | videoScalingMode = surfaceScaling->value; | 
|  | 822 | } | 
|  | 823 |  | 
|  | 824 | // Use dataspace from format as it has the default aspects already applied | 
|  | 825 | android_dataspace_t dataSpace = HAL_DATASPACE_UNKNOWN; // this is 0 | 
|  | 826 | (void)buffer->format()->findInt32("android._dataspace", (int32_t *)&dataSpace); | 
|  | 827 |  | 
|  | 828 | // HDR static info | 
|  | 829 | std::shared_ptr<const C2StreamHdrStaticInfo::output> hdrStaticInfo = | 
|  | 830 | std::static_pointer_cast<const C2StreamHdrStaticInfo::output>( | 
|  | 831 | c2Buffer->getInfo(C2StreamHdrStaticInfo::output::PARAM_TYPE)); | 
|  | 832 |  | 
| Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 833 | // HDR10 plus info | 
|  | 834 | std::shared_ptr<const C2StreamHdr10PlusInfo::output> hdr10PlusInfo = | 
|  | 835 | std::static_pointer_cast<const C2StreamHdr10PlusInfo::output>( | 
|  | 836 | c2Buffer->getInfo(C2StreamHdr10PlusInfo::output::PARAM_TYPE)); | 
| Yichi Chen | 54be23c | 2020-06-15 14:30:53 +0800 | [diff] [blame] | 837 | if (hdr10PlusInfo && hdr10PlusInfo->flexCount() == 0) { | 
|  | 838 | hdr10PlusInfo.reset(); | 
|  | 839 | } | 
| Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 840 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 841 | std::vector<C2ConstGraphicBlock> blocks = c2Buffer->data().graphicBlocks(); | 
|  | 842 | if (blocks.size() != 1u) { | 
|  | 843 | ALOGD("[%s] expected 1 graphic block, but got %zu", mName, blocks.size()); | 
|  | 844 | return UNKNOWN_ERROR; | 
|  | 845 | } | 
|  | 846 | const C2ConstGraphicBlock &block = blocks.front(); | 
|  | 847 |  | 
|  | 848 | // TODO: revisit this after C2Fence implementation. | 
|  | 849 | android::IGraphicBufferProducer::QueueBufferInput qbi( | 
|  | 850 | timestampNs, | 
|  | 851 | false, // droppable | 
|  | 852 | dataSpace, | 
|  | 853 | Rect(blocks.front().crop().left, | 
|  | 854 | blocks.front().crop().top, | 
|  | 855 | blocks.front().crop().right(), | 
|  | 856 | blocks.front().crop().bottom()), | 
|  | 857 | videoScalingMode, | 
|  | 858 | transform, | 
|  | 859 | Fence::NO_FENCE, 0); | 
| Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 860 | if (hdrStaticInfo || hdr10PlusInfo) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 861 | HdrMetadata hdr; | 
| Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 862 | if (hdrStaticInfo) { | 
| wenchangliu | f3f9288 | 2020-05-14 00:02:01 +0800 | [diff] [blame] | 863 | // If mastering max and min luminance fields are 0, do not use them. | 
|  | 864 | // It indicates the value may not be present in the stream. | 
|  | 865 | if (hdrStaticInfo->mastering.maxLuminance > 0.0f && | 
|  | 866 | hdrStaticInfo->mastering.minLuminance > 0.0f) { | 
|  | 867 | struct android_smpte2086_metadata smpte2086_meta = { | 
|  | 868 | .displayPrimaryRed = { | 
|  | 869 | hdrStaticInfo->mastering.red.x, hdrStaticInfo->mastering.red.y | 
|  | 870 | }, | 
|  | 871 | .displayPrimaryGreen = { | 
|  | 872 | hdrStaticInfo->mastering.green.x, hdrStaticInfo->mastering.green.y | 
|  | 873 | }, | 
|  | 874 | .displayPrimaryBlue = { | 
|  | 875 | hdrStaticInfo->mastering.blue.x, hdrStaticInfo->mastering.blue.y | 
|  | 876 | }, | 
|  | 877 | .whitePoint = { | 
|  | 878 | hdrStaticInfo->mastering.white.x, hdrStaticInfo->mastering.white.y | 
|  | 879 | }, | 
|  | 880 | .maxLuminance = hdrStaticInfo->mastering.maxLuminance, | 
|  | 881 | .minLuminance = hdrStaticInfo->mastering.minLuminance, | 
|  | 882 | }; | 
| Yichi Chen | 54be23c | 2020-06-15 14:30:53 +0800 | [diff] [blame] | 883 | hdr.validTypes |= HdrMetadata::SMPTE2086; | 
| wenchangliu | f3f9288 | 2020-05-14 00:02:01 +0800 | [diff] [blame] | 884 | hdr.smpte2086 = smpte2086_meta; | 
|  | 885 | } | 
| Chong Zhang | 3bb2a7f | 2020-04-21 10:35:12 -0700 | [diff] [blame] | 886 | // If the content light level fields are 0, do not use them, it | 
|  | 887 | // indicates the value may not be present in the stream. | 
|  | 888 | if (hdrStaticInfo->maxCll > 0.0f && hdrStaticInfo->maxFall > 0.0f) { | 
|  | 889 | struct android_cta861_3_metadata cta861_meta = { | 
|  | 890 | .maxContentLightLevel = hdrStaticInfo->maxCll, | 
|  | 891 | .maxFrameAverageLightLevel = hdrStaticInfo->maxFall, | 
|  | 892 | }; | 
|  | 893 | hdr.validTypes |= HdrMetadata::CTA861_3; | 
|  | 894 | hdr.cta8613 = cta861_meta; | 
|  | 895 | } | 
| Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 896 | } | 
|  | 897 | if (hdr10PlusInfo) { | 
|  | 898 | hdr.validTypes |= HdrMetadata::HDR10PLUS; | 
|  | 899 | hdr.hdr10plus.assign( | 
|  | 900 | hdr10PlusInfo->m.value, | 
|  | 901 | hdr10PlusInfo->m.value + hdr10PlusInfo->flexCount()); | 
|  | 902 | } | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 903 | qbi.setHdrMetadata(hdr); | 
|  | 904 | } | 
| Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 905 | // we don't have dirty regions | 
|  | 906 | qbi.setSurfaceDamage(Region::INVALID_REGION); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 907 | android::IGraphicBufferProducer::QueueBufferOutput qbo; | 
|  | 908 | status_t result = mComponent->queueToOutputSurface(block, qbi, &qbo); | 
|  | 909 | if (result != OK) { | 
|  | 910 | ALOGI("[%s] queueBuffer failed: %d", mName, result); | 
| Sungtak Lee | 47c018a | 2020-11-07 01:02:49 -0800 | [diff] [blame] | 911 | if (result == NO_INIT) { | 
|  | 912 | mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); | 
|  | 913 | } | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 914 | return result; | 
|  | 915 | } | 
| Josh Hou | 8eddf4b | 2021-02-02 16:26:53 +0800 | [diff] [blame] | 916 |  | 
|  | 917 | if(android::base::GetBoolProperty("debug.stagefright.fps", false)) { | 
|  | 918 | ALOGD("[%s] queue buffer successful", mName); | 
|  | 919 | } else { | 
|  | 920 | ALOGV("[%s] queue buffer successful", mName); | 
|  | 921 | } | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 922 |  | 
|  | 923 | int64_t mediaTimeUs = 0; | 
|  | 924 | (void)buffer->meta()->findInt64("timeUs", &mediaTimeUs); | 
|  | 925 | mCCodecCallback->onOutputFramesRendered(mediaTimeUs, timestampNs); | 
|  | 926 |  | 
|  | 927 | return OK; | 
|  | 928 | } | 
|  | 929 |  | 
|  | 930 | status_t CCodecBufferChannel::discardBuffer(const sp<MediaCodecBuffer> &buffer) { | 
|  | 931 | ALOGV("[%s] discardBuffer: %p", mName, buffer.get()); | 
|  | 932 | bool released = false; | 
|  | 933 | { | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 934 | Mutexed<Input>::Locked input(mInput); | 
|  | 935 | if (input->buffers && input->buffers->releaseBuffer(buffer, nullptr, true)) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 936 | released = true; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 937 | } | 
|  | 938 | } | 
|  | 939 | { | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 940 | Mutexed<Output>::Locked output(mOutput); | 
|  | 941 | if (output->buffers && output->buffers->releaseBuffer(buffer, nullptr)) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 942 | released = true; | 
|  | 943 | } | 
|  | 944 | } | 
|  | 945 | if (released) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 946 | sendOutputBuffers(); | 
| Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 947 | feedInputBufferIfAvailable(); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 948 | } else { | 
|  | 949 | ALOGD("[%s] MediaCodec discarded an unknown buffer", mName); | 
|  | 950 | } | 
|  | 951 | return OK; | 
|  | 952 | } | 
|  | 953 |  | 
|  | 954 | void CCodecBufferChannel::getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) { | 
|  | 955 | array->clear(); | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 956 | Mutexed<Input>::Locked input(mInput); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 957 |  | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 958 | if (!input->buffers->isArrayMode()) { | 
|  | 959 | input->buffers = input->buffers->toArrayMode(input->numSlots); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 960 | } | 
|  | 961 |  | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 962 | input->buffers->getArray(array); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 963 | } | 
|  | 964 |  | 
|  | 965 | void CCodecBufferChannel::getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) { | 
|  | 966 | array->clear(); | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 967 | Mutexed<Output>::Locked output(mOutput); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 968 |  | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 969 | if (!output->buffers->isArrayMode()) { | 
|  | 970 | output->buffers = output->buffers->toArrayMode(output->numSlots); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 971 | } | 
|  | 972 |  | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 973 | output->buffers->getArray(array); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 974 | } | 
|  | 975 |  | 
|  | 976 | status_t CCodecBufferChannel::start( | 
| Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 977 | const sp<AMessage> &inputFormat, | 
|  | 978 | const sp<AMessage> &outputFormat, | 
|  | 979 | bool buffersBoundToCodec) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 980 | C2StreamBufferTypeSetting::input iStreamFormat(0u); | 
|  | 981 | C2StreamBufferTypeSetting::output oStreamFormat(0u); | 
| Wonsik Kim | e1104ca | 2020-11-24 15:01:33 -0800 | [diff] [blame] | 982 | C2ComponentKindSetting kind; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 983 | C2PortReorderBufferDepthTuning::output reorderDepth; | 
|  | 984 | C2PortReorderKeySetting::output reorderKey; | 
| Wonsik Kim | 078b58e | 2019-01-09 15:08:06 -0800 | [diff] [blame] | 985 | C2PortActualDelayTuning::input inputDelay(0); | 
|  | 986 | C2PortActualDelayTuning::output outputDelay(0); | 
|  | 987 | C2ActualPipelineDelayTuning pipelineDelay(0); | 
| Sungtak Lee | 04b3035 | 2020-07-27 13:57:25 -0700 | [diff] [blame] | 988 | C2SecureModeTuning secureMode(C2Config::SM_UNPROTECTED); | 
| Wonsik Kim | 078b58e | 2019-01-09 15:08:06 -0800 | [diff] [blame] | 989 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 990 | c2_status_t err = mComponent->query( | 
|  | 991 | { | 
|  | 992 | &iStreamFormat, | 
|  | 993 | &oStreamFormat, | 
| Wonsik Kim | e1104ca | 2020-11-24 15:01:33 -0800 | [diff] [blame] | 994 | &kind, | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 995 | &reorderDepth, | 
|  | 996 | &reorderKey, | 
| Wonsik Kim | 078b58e | 2019-01-09 15:08:06 -0800 | [diff] [blame] | 997 | &inputDelay, | 
|  | 998 | &pipelineDelay, | 
|  | 999 | &outputDelay, | 
| Sungtak Lee | 04b3035 | 2020-07-27 13:57:25 -0700 | [diff] [blame] | 1000 | &secureMode, | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1001 | }, | 
|  | 1002 | {}, | 
|  | 1003 | C2_DONT_BLOCK, | 
|  | 1004 | nullptr); | 
|  | 1005 | if (err == C2_BAD_INDEX) { | 
| Wonsik Kim | e1104ca | 2020-11-24 15:01:33 -0800 | [diff] [blame] | 1006 | if (!iStreamFormat || !oStreamFormat || !kind) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1007 | return UNKNOWN_ERROR; | 
|  | 1008 | } | 
|  | 1009 | } else if (err != C2_OK) { | 
|  | 1010 | return UNKNOWN_ERROR; | 
|  | 1011 | } | 
|  | 1012 |  | 
| Wonsik Kim | 4fa4f2b | 2019-02-13 11:02:58 -0800 | [diff] [blame] | 1013 | uint32_t inputDelayValue = inputDelay ? inputDelay.value : 0; | 
|  | 1014 | uint32_t pipelineDelayValue = pipelineDelay ? pipelineDelay.value : 0; | 
|  | 1015 | uint32_t outputDelayValue = outputDelay ? outputDelay.value : 0; | 
|  | 1016 |  | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1017 | size_t numInputSlots = inputDelayValue + pipelineDelayValue + kSmoothnessFactor; | 
|  | 1018 | size_t numOutputSlots = outputDelayValue + kSmoothnessFactor; | 
| Wonsik Kim | 078b58e | 2019-01-09 15:08:06 -0800 | [diff] [blame] | 1019 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1020 | // TODO: get this from input format | 
|  | 1021 | bool secure = mComponent->getName().find(".secure") != std::string::npos; | 
|  | 1022 |  | 
| Sungtak Lee | 04b3035 | 2020-07-27 13:57:25 -0700 | [diff] [blame] | 1023 | // secure mode is a static parameter (shall not change in the executing state) | 
|  | 1024 | mSendEncryptedInfoBuffer = secureMode.value == C2Config::SM_READ_PROTECTED_WITH_ENCRYPTED; | 
|  | 1025 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1026 | std::shared_ptr<C2AllocatorStore> allocatorStore = GetCodec2PlatformAllocatorStore(); | 
| Pin-chih Lin | aa18ea5 | 2019-11-19 18:48:50 +0800 | [diff] [blame] | 1027 | int poolMask = GetCodec2PoolMask(); | 
|  | 1028 | C2PlatformAllocatorStore::id_t preferredLinearId = GetPreferredLinearAllocatorId(poolMask); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1029 |  | 
|  | 1030 | if (inputFormat != nullptr) { | 
| Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 1031 | bool graphic = (iStreamFormat.value == C2BufferData::GRAPHIC); | 
| Wonsik Kim | e1104ca | 2020-11-24 15:01:33 -0800 | [diff] [blame] | 1032 | bool audioEncoder = !graphic && (kind.value == C2Component::KIND_ENCODER); | 
| Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 1033 | C2Config::api_feature_t apiFeatures = C2Config::api_feature_t( | 
|  | 1034 | API_REFLECTION | | 
|  | 1035 | API_VALUES | | 
|  | 1036 | API_CURRENT_VALUES | | 
|  | 1037 | API_DEPENDENCY | | 
|  | 1038 | API_SAME_INPUT_BUFFER); | 
| Wonsik Kim | e1104ca | 2020-11-24 15:01:33 -0800 | [diff] [blame] | 1039 | C2StreamAudioFrameSizeInfo::input encoderFrameSize(0u); | 
|  | 1040 | C2StreamSampleRateInfo::input sampleRate(0u); | 
|  | 1041 | C2StreamChannelCountInfo::input channelCount(0u); | 
|  | 1042 | C2StreamPcmEncodingInfo::input pcmEncoding(0u); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1043 | std::shared_ptr<C2BlockPool> pool; | 
|  | 1044 | { | 
|  | 1045 | Mutexed<BlockPools>::Locked pools(mBlockPools); | 
|  | 1046 |  | 
|  | 1047 | // set default allocator ID. | 
|  | 1048 | pools->inputAllocatorId = (graphic) ? C2PlatformAllocatorStore::GRALLOC | 
| Pin-chih Lin | aa18ea5 | 2019-11-19 18:48:50 +0800 | [diff] [blame] | 1049 | : preferredLinearId; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1050 |  | 
|  | 1051 | // query C2PortAllocatorsTuning::input from component. If an allocator ID is obtained | 
|  | 1052 | // from component, create the input block pool with given ID. Otherwise, use default IDs. | 
|  | 1053 | std::vector<std::unique_ptr<C2Param>> params; | 
| Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 1054 | C2ApiFeaturesSetting featuresSetting{apiFeatures}; | 
| Wonsik Kim | e1104ca | 2020-11-24 15:01:33 -0800 | [diff] [blame] | 1055 | std::vector<C2Param *> stackParams({&featuresSetting}); | 
|  | 1056 | if (audioEncoder) { | 
|  | 1057 | stackParams.push_back(&encoderFrameSize); | 
|  | 1058 | stackParams.push_back(&sampleRate); | 
|  | 1059 | stackParams.push_back(&channelCount); | 
|  | 1060 | stackParams.push_back(&pcmEncoding); | 
|  | 1061 | } else { | 
|  | 1062 | encoderFrameSize.invalidate(); | 
|  | 1063 | sampleRate.invalidate(); | 
|  | 1064 | channelCount.invalidate(); | 
|  | 1065 | pcmEncoding.invalidate(); | 
|  | 1066 | } | 
|  | 1067 | err = mComponent->query(stackParams, | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1068 | { C2PortAllocatorsTuning::input::PARAM_TYPE }, | 
|  | 1069 | C2_DONT_BLOCK, | 
|  | 1070 | ¶ms); | 
|  | 1071 | if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) { | 
|  | 1072 | ALOGD("[%s] Query input allocators returned %zu params => %s (%u)", | 
|  | 1073 | mName, params.size(), asString(err), err); | 
| Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 1074 | } else if (params.size() == 1) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1075 | C2PortAllocatorsTuning::input *inputAllocators = | 
|  | 1076 | C2PortAllocatorsTuning::input::From(params[0].get()); | 
|  | 1077 | if (inputAllocators && inputAllocators->flexCount() > 0) { | 
|  | 1078 | std::shared_ptr<C2Allocator> allocator; | 
|  | 1079 | // verify allocator IDs and resolve default allocator | 
|  | 1080 | allocatorStore->fetchAllocator(inputAllocators->m.values[0], &allocator); | 
|  | 1081 | if (allocator) { | 
|  | 1082 | pools->inputAllocatorId = allocator->getId(); | 
|  | 1083 | } else { | 
|  | 1084 | ALOGD("[%s] component requested invalid input allocator ID %u", | 
|  | 1085 | mName, inputAllocators->m.values[0]); | 
|  | 1086 | } | 
|  | 1087 | } | 
|  | 1088 | } | 
| Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 1089 | if (featuresSetting) { | 
|  | 1090 | apiFeatures = featuresSetting.value; | 
|  | 1091 | } | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1092 |  | 
|  | 1093 | // TODO: use C2Component wrapper to associate this pool with ourselves | 
|  | 1094 | if ((poolMask >> pools->inputAllocatorId) & 1) { | 
|  | 1095 | err = CreateCodec2BlockPool(pools->inputAllocatorId, nullptr, &pool); | 
|  | 1096 | ALOGD("[%s] Created input block pool with allocatorID %u => poolID %llu - %s (%d)", | 
|  | 1097 | mName, pools->inputAllocatorId, | 
|  | 1098 | (unsigned long long)(pool ? pool->getLocalId() : 111000111), | 
|  | 1099 | asString(err), err); | 
|  | 1100 | } else { | 
|  | 1101 | err = C2_NOT_FOUND; | 
|  | 1102 | } | 
|  | 1103 | if (err != C2_OK) { | 
|  | 1104 | C2BlockPool::local_id_t inputPoolId = | 
|  | 1105 | graphic ? C2BlockPool::BASIC_GRAPHIC : C2BlockPool::BASIC_LINEAR; | 
|  | 1106 | err = GetCodec2BlockPool(inputPoolId, nullptr, &pool); | 
|  | 1107 | ALOGD("[%s] Using basic input block pool with poolID %llu => got %llu - %s (%d)", | 
|  | 1108 | mName, (unsigned long long)inputPoolId, | 
|  | 1109 | (unsigned long long)(pool ? pool->getLocalId() : 111000111), | 
|  | 1110 | asString(err), err); | 
|  | 1111 | if (err != C2_OK) { | 
|  | 1112 | return NO_MEMORY; | 
|  | 1113 | } | 
|  | 1114 | } | 
|  | 1115 | pools->inputPool = pool; | 
|  | 1116 | } | 
|  | 1117 |  | 
| Wonsik Kim | 5105126 | 2018-11-28 13:59:05 -0800 | [diff] [blame] | 1118 | bool forceArrayMode = false; | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1119 | Mutexed<Input>::Locked input(mInput); | 
| Wonsik Kim | bdffead | 2019-07-01 12:00:07 -0700 | [diff] [blame] | 1120 | input->inputDelay = inputDelayValue; | 
|  | 1121 | input->pipelineDelay = pipelineDelayValue; | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1122 | input->numSlots = numInputSlots; | 
|  | 1123 | input->extraBuffers.flush(); | 
|  | 1124 | input->numExtraSlots = 0u; | 
| Wonsik Kim | e1104ca | 2020-11-24 15:01:33 -0800 | [diff] [blame] | 1125 | if (audioEncoder && encoderFrameSize && sampleRate && channelCount) { | 
|  | 1126 | input->frameReassembler.init( | 
|  | 1127 | pool, | 
|  | 1128 | {C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE}, | 
|  | 1129 | encoderFrameSize.value, | 
|  | 1130 | sampleRate.value, | 
|  | 1131 | channelCount.value, | 
|  | 1132 | pcmEncoding ? pcmEncoding.value : C2Config::PCM_16); | 
|  | 1133 | } | 
| Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 1134 | bool conforming = (apiFeatures & API_SAME_INPUT_BUFFER); | 
|  | 1135 | // For encrypted content, framework decrypts source buffer (ashmem) into | 
|  | 1136 | // C2Buffers. Thus non-conforming codecs can process these. | 
| Wonsik Kim | e1104ca | 2020-11-24 15:01:33 -0800 | [diff] [blame] | 1137 | if (!buffersBoundToCodec | 
|  | 1138 | && !input->frameReassembler | 
|  | 1139 | && (hasCryptoOrDescrambler() || conforming)) { | 
| Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 1140 | input->buffers.reset(new SlotInputBuffers(mName)); | 
|  | 1141 | } else if (graphic) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1142 | if (mInputSurface) { | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1143 | input->buffers.reset(new DummyInputBuffers(mName)); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1144 | } else if (mMetaMode == MODE_ANW) { | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1145 | input->buffers.reset(new GraphicMetadataInputBuffers(mName)); | 
| Wonsik Kim | 1221fd1 | 2019-07-12 12:52:05 -0700 | [diff] [blame] | 1146 | // This is to ensure buffers do not get released prematurely. | 
|  | 1147 | // TODO: handle this without going into array mode | 
|  | 1148 | forceArrayMode = true; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1149 | } else { | 
| Wonsik Kim | 41d8343 | 2020-04-27 16:40:49 -0700 | [diff] [blame] | 1150 | input->buffers.reset(new GraphicInputBuffers(mName)); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1151 | } | 
|  | 1152 | } else { | 
|  | 1153 | if (hasCryptoOrDescrambler()) { | 
|  | 1154 | int32_t capacity = kLinearBufferSize; | 
|  | 1155 | (void)inputFormat->findInt32(KEY_MAX_INPUT_SIZE, &capacity); | 
|  | 1156 | if ((size_t)capacity > kMaxLinearBufferSize) { | 
|  | 1157 | ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize); | 
|  | 1158 | capacity = kMaxLinearBufferSize; | 
|  | 1159 | } | 
|  | 1160 | if (mDealer == nullptr) { | 
|  | 1161 | mDealer = new MemoryDealer( | 
|  | 1162 | align(capacity, MemoryDealer::getAllocationAlignment()) | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1163 | * (numInputSlots + 1), | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1164 | "EncryptedLinearInputBuffers"); | 
|  | 1165 | mDecryptDestination = mDealer->allocate((size_t)capacity); | 
|  | 1166 | } | 
|  | 1167 | if (mCrypto != nullptr && mHeapSeqNum < 0) { | 
| Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 1168 | sp<HidlMemory> heap = fromHeap(mDealer->getMemoryHeap()); | 
|  | 1169 | mHeapSeqNum = mCrypto->setHeap(heap); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1170 | } else { | 
|  | 1171 | mHeapSeqNum = -1; | 
|  | 1172 | } | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1173 | input->buffers.reset(new EncryptedLinearInputBuffers( | 
| Wonsik Kim | 078b58e | 2019-01-09 15:08:06 -0800 | [diff] [blame] | 1174 | secure, mDealer, mCrypto, mHeapSeqNum, (size_t)capacity, | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1175 | numInputSlots, mName)); | 
| Wonsik Kim | 5105126 | 2018-11-28 13:59:05 -0800 | [diff] [blame] | 1176 | forceArrayMode = true; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1177 | } else { | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1178 | input->buffers.reset(new LinearInputBuffers(mName)); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1179 | } | 
|  | 1180 | } | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1181 | input->buffers->setFormat(inputFormat); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1182 |  | 
|  | 1183 | if (err == C2_OK) { | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1184 | input->buffers->setPool(pool); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1185 | } else { | 
|  | 1186 | // TODO: error | 
|  | 1187 | } | 
| Wonsik Kim | 5105126 | 2018-11-28 13:59:05 -0800 | [diff] [blame] | 1188 |  | 
|  | 1189 | if (forceArrayMode) { | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1190 | input->buffers = input->buffers->toArrayMode(numInputSlots); | 
| Wonsik Kim | 5105126 | 2018-11-28 13:59:05 -0800 | [diff] [blame] | 1191 | } | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1192 | } | 
|  | 1193 |  | 
|  | 1194 | if (outputFormat != nullptr) { | 
|  | 1195 | sp<IGraphicBufferProducer> outputSurface; | 
|  | 1196 | uint32_t outputGeneration; | 
| Sungtak Lee | a714f11 | 2021-03-16 05:40:03 -0700 | [diff] [blame] | 1197 | int maxDequeueCount = 0; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1198 | { | 
|  | 1199 | Mutexed<OutputSurface>::Locked output(mOutputSurface); | 
| Sungtak Lee | a714f11 | 2021-03-16 05:40:03 -0700 | [diff] [blame] | 1200 | maxDequeueCount = output->maxDequeueBuffers = numOutputSlots + | 
| Sungtak Lee | 7a7b742 | 2019-07-16 17:40:40 -0700 | [diff] [blame] | 1201 | reorderDepth.value + kRenderingDepth; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1202 | outputSurface = output->surface ? | 
|  | 1203 | output->surface->getIGraphicBufferProducer() : nullptr; | 
| Wonsik Kim | f5e5c83 | 2019-02-21 11:36:05 -0800 | [diff] [blame] | 1204 | if (outputSurface) { | 
|  | 1205 | output->surface->setMaxDequeuedBufferCount(output->maxDequeueBuffers); | 
|  | 1206 | } | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1207 | outputGeneration = output->generation; | 
|  | 1208 | } | 
|  | 1209 |  | 
| Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 1210 | bool graphic = (oStreamFormat.value == C2BufferData::GRAPHIC); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1211 | C2BlockPool::local_id_t outputPoolId_; | 
| David Stevens | c3fbb28 | 2021-01-18 18:11:20 +0900 | [diff] [blame] | 1212 | C2BlockPool::local_id_t prevOutputPoolId; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1213 |  | 
|  | 1214 | { | 
|  | 1215 | Mutexed<BlockPools>::Locked pools(mBlockPools); | 
|  | 1216 |  | 
| David Stevens | c3fbb28 | 2021-01-18 18:11:20 +0900 | [diff] [blame] | 1217 | prevOutputPoolId = pools->outputPoolId; | 
|  | 1218 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1219 | // set default allocator ID. | 
|  | 1220 | pools->outputAllocatorId = (graphic) ? C2PlatformAllocatorStore::GRALLOC | 
| Pin-chih Lin | aa18ea5 | 2019-11-19 18:48:50 +0800 | [diff] [blame] | 1221 | : preferredLinearId; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1222 |  | 
|  | 1223 | // query C2PortAllocatorsTuning::output from component, or use default allocator if | 
|  | 1224 | // unsuccessful. | 
|  | 1225 | std::vector<std::unique_ptr<C2Param>> params; | 
|  | 1226 | err = mComponent->query({ }, | 
|  | 1227 | { C2PortAllocatorsTuning::output::PARAM_TYPE }, | 
|  | 1228 | C2_DONT_BLOCK, | 
|  | 1229 | ¶ms); | 
|  | 1230 | if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) { | 
|  | 1231 | ALOGD("[%s] Query output allocators returned %zu params => %s (%u)", | 
|  | 1232 | mName, params.size(), asString(err), err); | 
|  | 1233 | } else if (err == C2_OK && params.size() == 1) { | 
|  | 1234 | C2PortAllocatorsTuning::output *outputAllocators = | 
|  | 1235 | C2PortAllocatorsTuning::output::From(params[0].get()); | 
|  | 1236 | if (outputAllocators && outputAllocators->flexCount() > 0) { | 
|  | 1237 | std::shared_ptr<C2Allocator> allocator; | 
|  | 1238 | // verify allocator IDs and resolve default allocator | 
|  | 1239 | allocatorStore->fetchAllocator(outputAllocators->m.values[0], &allocator); | 
|  | 1240 | if (allocator) { | 
|  | 1241 | pools->outputAllocatorId = allocator->getId(); | 
|  | 1242 | } else { | 
|  | 1243 | ALOGD("[%s] component requested invalid output allocator ID %u", | 
|  | 1244 | mName, outputAllocators->m.values[0]); | 
|  | 1245 | } | 
|  | 1246 | } | 
|  | 1247 | } | 
|  | 1248 |  | 
|  | 1249 | // use bufferqueue if outputting to a surface. | 
|  | 1250 | // query C2PortSurfaceAllocatorTuning::output from component, or use default allocator | 
|  | 1251 | // if unsuccessful. | 
|  | 1252 | if (outputSurface) { | 
|  | 1253 | params.clear(); | 
|  | 1254 | err = mComponent->query({ }, | 
|  | 1255 | { C2PortSurfaceAllocatorTuning::output::PARAM_TYPE }, | 
|  | 1256 | C2_DONT_BLOCK, | 
|  | 1257 | ¶ms); | 
|  | 1258 | if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) { | 
|  | 1259 | ALOGD("[%s] Query output surface allocator returned %zu params => %s (%u)", | 
|  | 1260 | mName, params.size(), asString(err), err); | 
|  | 1261 | } else if (err == C2_OK && params.size() == 1) { | 
|  | 1262 | C2PortSurfaceAllocatorTuning::output *surfaceAllocator = | 
|  | 1263 | C2PortSurfaceAllocatorTuning::output::From(params[0].get()); | 
|  | 1264 | if (surfaceAllocator) { | 
|  | 1265 | std::shared_ptr<C2Allocator> allocator; | 
|  | 1266 | // verify allocator IDs and resolve default allocator | 
|  | 1267 | allocatorStore->fetchAllocator(surfaceAllocator->value, &allocator); | 
|  | 1268 | if (allocator) { | 
|  | 1269 | pools->outputAllocatorId = allocator->getId(); | 
|  | 1270 | } else { | 
|  | 1271 | ALOGD("[%s] component requested invalid surface output allocator ID %u", | 
|  | 1272 | mName, surfaceAllocator->value); | 
|  | 1273 | err = C2_BAD_VALUE; | 
|  | 1274 | } | 
|  | 1275 | } | 
|  | 1276 | } | 
|  | 1277 | if (pools->outputAllocatorId == C2PlatformAllocatorStore::GRALLOC | 
|  | 1278 | && err != C2_OK | 
|  | 1279 | && ((poolMask >> C2PlatformAllocatorStore::BUFFERQUEUE) & 1)) { | 
|  | 1280 | pools->outputAllocatorId = C2PlatformAllocatorStore::BUFFERQUEUE; | 
|  | 1281 | } | 
|  | 1282 | } | 
|  | 1283 |  | 
|  | 1284 | if ((poolMask >> pools->outputAllocatorId) & 1) { | 
|  | 1285 | err = mComponent->createBlockPool( | 
|  | 1286 | pools->outputAllocatorId, &pools->outputPoolId, &pools->outputPoolIntf); | 
|  | 1287 | ALOGI("[%s] Created output block pool with allocatorID %u => poolID %llu - %s", | 
|  | 1288 | mName, pools->outputAllocatorId, | 
|  | 1289 | (unsigned long long)pools->outputPoolId, | 
|  | 1290 | asString(err)); | 
|  | 1291 | } else { | 
|  | 1292 | err = C2_NOT_FOUND; | 
|  | 1293 | } | 
|  | 1294 | if (err != C2_OK) { | 
|  | 1295 | // use basic pool instead | 
|  | 1296 | pools->outputPoolId = | 
|  | 1297 | graphic ? C2BlockPool::BASIC_GRAPHIC : C2BlockPool::BASIC_LINEAR; | 
|  | 1298 | } | 
|  | 1299 |  | 
|  | 1300 | // Configure output block pool ID as parameter C2PortBlockPoolsTuning::output to | 
|  | 1301 | // component. | 
|  | 1302 | std::unique_ptr<C2PortBlockPoolsTuning::output> poolIdsTuning = | 
|  | 1303 | C2PortBlockPoolsTuning::output::AllocUnique({ pools->outputPoolId }); | 
|  | 1304 |  | 
|  | 1305 | std::vector<std::unique_ptr<C2SettingResult>> failures; | 
|  | 1306 | err = mComponent->config({ poolIdsTuning.get() }, C2_MAY_BLOCK, &failures); | 
|  | 1307 | ALOGD("[%s] Configured output block pool ids %llu => %s", | 
|  | 1308 | mName, (unsigned long long)poolIdsTuning->m.values[0], asString(err)); | 
|  | 1309 | outputPoolId_ = pools->outputPoolId; | 
|  | 1310 | } | 
|  | 1311 |  | 
| David Stevens | c3fbb28 | 2021-01-18 18:11:20 +0900 | [diff] [blame] | 1312 | if (prevOutputPoolId != C2BlockPool::BASIC_LINEAR | 
|  | 1313 | && prevOutputPoolId != C2BlockPool::BASIC_GRAPHIC) { | 
|  | 1314 | c2_status_t err = mComponent->destroyBlockPool(prevOutputPoolId); | 
|  | 1315 | if (err != C2_OK) { | 
|  | 1316 | ALOGW("Failed to clean up previous block pool %llu - %s (%d)\n", | 
|  | 1317 | (unsigned long long) prevOutputPoolId, asString(err), err); | 
|  | 1318 | } | 
|  | 1319 | } | 
|  | 1320 |  | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1321 | Mutexed<Output>::Locked output(mOutput); | 
| Wonsik Kim | bdffead | 2019-07-01 12:00:07 -0700 | [diff] [blame] | 1322 | output->outputDelay = outputDelayValue; | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1323 | output->numSlots = numOutputSlots; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1324 | if (graphic) { | 
| Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 1325 | if (outputSurface || !buffersBoundToCodec) { | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1326 | output->buffers.reset(new GraphicOutputBuffers(mName)); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1327 | } else { | 
| Wonsik Kim | 41d8343 | 2020-04-27 16:40:49 -0700 | [diff] [blame] | 1328 | output->buffers.reset(new RawGraphicOutputBuffers(mName)); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1329 | } | 
|  | 1330 | } else { | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1331 | output->buffers.reset(new LinearOutputBuffers(mName)); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1332 | } | 
| Wonsik Kim | e4716c0 | 2020-02-28 10:42:21 -0800 | [diff] [blame] | 1333 | output->buffers->setFormat(outputFormat); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1334 |  | 
| Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1335 | output->buffers->clearStash(); | 
|  | 1336 | if (reorderDepth) { | 
|  | 1337 | output->buffers->setReorderDepth(reorderDepth.value); | 
|  | 1338 | } | 
|  | 1339 | if (reorderKey) { | 
|  | 1340 | output->buffers->setReorderKey(reorderKey.value); | 
|  | 1341 | } | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1342 |  | 
|  | 1343 | // Try to set output surface to created block pool if given. | 
|  | 1344 | if (outputSurface) { | 
|  | 1345 | mComponent->setOutputSurface( | 
|  | 1346 | outputPoolId_, | 
|  | 1347 | outputSurface, | 
| Sungtak Lee | db14cba | 2021-04-10 00:50:23 -0700 | [diff] [blame] | 1348 | outputGeneration, | 
|  | 1349 | maxDequeueCount); | 
| Lajos Molnar | 78aa7c9 | 2021-02-18 21:39:01 -0800 | [diff] [blame] | 1350 | } else { | 
|  | 1351 | // configure CPU read consumer usage | 
|  | 1352 | C2StreamUsageTuning::output outputUsage{0u, C2MemoryUsage::CPU_READ}; | 
|  | 1353 | std::vector<std::unique_ptr<C2SettingResult>> failures; | 
|  | 1354 | err = mComponent->config({ &outputUsage }, C2_MAY_BLOCK, &failures); | 
|  | 1355 | // do not print error message for now as most components may not yet | 
|  | 1356 | // support this setting | 
|  | 1357 | ALOGD_IF(err != C2_BAD_INDEX, "[%s] Configured output usage [%#llx]", | 
|  | 1358 | mName, (long long)outputUsage.value); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1359 | } | 
|  | 1360 |  | 
| Wonsik Kim | 8ab25aa | 2019-06-24 16:37:37 -0700 | [diff] [blame] | 1361 | if (oStreamFormat.value == C2BufferData::LINEAR) { | 
| Wonsik Kim | 5871330 | 2020-01-29 22:25:23 -0800 | [diff] [blame] | 1362 | if (buffersBoundToCodec) { | 
|  | 1363 | // WORKAROUND: if we're using early CSD workaround we convert to | 
|  | 1364 | //             array mode, to appease apps assuming the output | 
|  | 1365 | //             buffers to be of the same size. | 
|  | 1366 | output->buffers = output->buffers->toArrayMode(numOutputSlots); | 
|  | 1367 | } | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1368 |  | 
|  | 1369 | int32_t channelCount; | 
|  | 1370 | int32_t sampleRate; | 
|  | 1371 | if (outputFormat->findInt32(KEY_CHANNEL_COUNT, &channelCount) | 
|  | 1372 | && outputFormat->findInt32(KEY_SAMPLE_RATE, &sampleRate)) { | 
|  | 1373 | int32_t delay = 0; | 
|  | 1374 | int32_t padding = 0;; | 
|  | 1375 | if (!outputFormat->findInt32("encoder-delay", &delay)) { | 
|  | 1376 | delay = 0; | 
|  | 1377 | } | 
|  | 1378 | if (!outputFormat->findInt32("encoder-padding", &padding)) { | 
|  | 1379 | padding = 0; | 
|  | 1380 | } | 
|  | 1381 | if (delay || padding) { | 
|  | 1382 | // We need write access to the buffers, and we're already in | 
|  | 1383 | // array mode. | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1384 | output->buffers->initSkipCutBuffer(delay, padding, sampleRate, channelCount); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1385 | } | 
|  | 1386 | } | 
|  | 1387 | } | 
|  | 1388 | } | 
|  | 1389 |  | 
|  | 1390 | // Set up pipeline control. This has to be done after mInputBuffers and | 
|  | 1391 | // mOutputBuffers are initialized to make sure that lingering callbacks | 
|  | 1392 | // about buffers from the previous generation do not interfere with the | 
|  | 1393 | // newly initialized pipeline capacity. | 
|  | 1394 |  | 
| Wonsik Kim | 6254525 | 2021-01-20 11:25:41 -0800 | [diff] [blame] | 1395 | if (inputFormat || outputFormat) { | 
| Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1396 | Mutexed<PipelineWatcher>::Locked watcher(mPipelineWatcher); | 
| Wonsik Kim | 4fa4f2b | 2019-02-13 11:02:58 -0800 | [diff] [blame] | 1397 | watcher->inputDelay(inputDelayValue) | 
|  | 1398 | .pipelineDelay(pipelineDelayValue) | 
|  | 1399 | .outputDelay(outputDelayValue) | 
| Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1400 | .smoothnessFactor(kSmoothnessFactor); | 
|  | 1401 | watcher->flush(); | 
|  | 1402 | } | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1403 |  | 
|  | 1404 | mInputMetEos = false; | 
|  | 1405 | mSync.start(); | 
|  | 1406 | return OK; | 
|  | 1407 | } | 
|  | 1408 |  | 
|  | 1409 | status_t CCodecBufferChannel::requestInitialInputBuffers() { | 
|  | 1410 | if (mInputSurface) { | 
|  | 1411 | return OK; | 
|  | 1412 | } | 
|  | 1413 |  | 
| Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 1414 | C2StreamBufferTypeSetting::output oStreamFormat(0u); | 
| Wonsik Kim | 8ab25aa | 2019-06-24 16:37:37 -0700 | [diff] [blame] | 1415 | C2PrependHeaderModeSetting prepend(PREPEND_HEADER_TO_NONE); | 
|  | 1416 | c2_status_t err = mComponent->query({ &oStreamFormat, &prepend }, {}, C2_DONT_BLOCK, nullptr); | 
|  | 1417 | if (err != C2_OK && err != C2_BAD_INDEX) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1418 | return UNKNOWN_ERROR; | 
|  | 1419 | } | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1420 | size_t numInputSlots = mInput.lock()->numSlots; | 
| Pawin Vongmasa | e7bb861 | 2020-06-04 06:15:22 -0700 | [diff] [blame] | 1421 |  | 
|  | 1422 | struct ClientInputBuffer { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1423 | size_t index; | 
|  | 1424 | sp<MediaCodecBuffer> buffer; | 
| Pawin Vongmasa | e7bb861 | 2020-06-04 06:15:22 -0700 | [diff] [blame] | 1425 | size_t capacity; | 
|  | 1426 | }; | 
|  | 1427 | std::list<ClientInputBuffer> clientInputBuffers; | 
|  | 1428 |  | 
|  | 1429 | { | 
|  | 1430 | Mutexed<Input>::Locked input(mInput); | 
|  | 1431 | while (clientInputBuffers.size() < numInputSlots) { | 
|  | 1432 | ClientInputBuffer clientInputBuffer; | 
|  | 1433 | if (!input->buffers->requestNewBuffer(&clientInputBuffer.index, | 
|  | 1434 | &clientInputBuffer.buffer)) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1435 | break; | 
|  | 1436 | } | 
| Pawin Vongmasa | e7bb861 | 2020-06-04 06:15:22 -0700 | [diff] [blame] | 1437 | clientInputBuffer.capacity = clientInputBuffer.buffer->capacity(); | 
|  | 1438 | clientInputBuffers.emplace_back(std::move(clientInputBuffer)); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1439 | } | 
| Pawin Vongmasa | e7bb861 | 2020-06-04 06:15:22 -0700 | [diff] [blame] | 1440 | } | 
|  | 1441 | if (clientInputBuffers.empty()) { | 
|  | 1442 | ALOGW("[%s] start: cannot allocate memory at all", mName); | 
|  | 1443 | return NO_MEMORY; | 
|  | 1444 | } else if (clientInputBuffers.size() < numInputSlots) { | 
|  | 1445 | ALOGD("[%s] start: cannot allocate memory for all slots, " | 
|  | 1446 | "only %zu buffers allocated", | 
|  | 1447 | mName, clientInputBuffers.size()); | 
|  | 1448 | } else { | 
|  | 1449 | ALOGV("[%s] %zu initial input buffers available", | 
|  | 1450 | mName, clientInputBuffers.size()); | 
|  | 1451 | } | 
|  | 1452 | // Sort input buffers by their capacities in increasing order. | 
|  | 1453 | clientInputBuffers.sort( | 
|  | 1454 | [](const ClientInputBuffer& a, const ClientInputBuffer& b) { | 
|  | 1455 | return a.capacity < b.capacity; | 
|  | 1456 | }); | 
|  | 1457 |  | 
| Wonsik Kim | 5ebfcb2 | 2021-01-05 18:58:15 -0800 | [diff] [blame] | 1458 | std::list<std::unique_ptr<C2Work>> flushedConfigs; | 
|  | 1459 | mFlushedConfigs.lock()->swap(flushedConfigs); | 
|  | 1460 | if (!flushedConfigs.empty()) { | 
|  | 1461 | err = mComponent->queue(&flushedConfigs); | 
|  | 1462 | if (err != C2_OK) { | 
|  | 1463 | ALOGW("[%s] Error while queueing a flushed config", mName); | 
|  | 1464 | return UNKNOWN_ERROR; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1465 | } | 
|  | 1466 | } | 
| Wonsik Kim | 5ebfcb2 | 2021-01-05 18:58:15 -0800 | [diff] [blame] | 1467 | if (oStreamFormat.value == C2BufferData::LINEAR && | 
|  | 1468 | (!prepend || prepend.value == PREPEND_HEADER_TO_NONE)) { | 
|  | 1469 | sp<MediaCodecBuffer> buffer = clientInputBuffers.front().buffer; | 
|  | 1470 | // WORKAROUND: Some apps expect CSD available without queueing | 
|  | 1471 | //             any input. Queue an empty buffer to get the CSD. | 
|  | 1472 | buffer->setRange(0, 0); | 
|  | 1473 | buffer->meta()->clear(); | 
|  | 1474 | buffer->meta()->setInt64("timeUs", 0); | 
|  | 1475 | if (queueInputBufferInternal(buffer) != OK) { | 
|  | 1476 | ALOGW("[%s] Error while queueing an empty buffer to get CSD", | 
|  | 1477 | mName); | 
|  | 1478 | return UNKNOWN_ERROR; | 
|  | 1479 | } | 
|  | 1480 | clientInputBuffers.pop_front(); | 
|  | 1481 | } | 
| Pawin Vongmasa | e7bb861 | 2020-06-04 06:15:22 -0700 | [diff] [blame] | 1482 |  | 
|  | 1483 | for (const ClientInputBuffer& clientInputBuffer: clientInputBuffers) { | 
|  | 1484 | mCallback->onInputBufferAvailable( | 
|  | 1485 | clientInputBuffer.index, | 
|  | 1486 | clientInputBuffer.buffer); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1487 | } | 
| Pawin Vongmasa | e7bb861 | 2020-06-04 06:15:22 -0700 | [diff] [blame] | 1488 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1489 | return OK; | 
|  | 1490 | } | 
|  | 1491 |  | 
|  | 1492 | void CCodecBufferChannel::stop() { | 
|  | 1493 | mSync.stop(); | 
|  | 1494 | mFirstValidFrameIndex = mFrameIndex.load(std::memory_order_relaxed); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1495 | } | 
|  | 1496 |  | 
| Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1497 | void CCodecBufferChannel::reset() { | 
|  | 1498 | stop(); | 
| Wonsik Kim | 6254525 | 2021-01-20 11:25:41 -0800 | [diff] [blame] | 1499 | if (mInputSurface != nullptr) { | 
|  | 1500 | mInputSurface.reset(); | 
|  | 1501 | } | 
|  | 1502 | mPipelineWatcher.lock()->flush(); | 
| Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1503 | { | 
|  | 1504 | Mutexed<Input>::Locked input(mInput); | 
|  | 1505 | input->buffers.reset(new DummyInputBuffers("")); | 
| Wonsik Kim | a2e3cdd | 2020-05-20 15:14:42 -0700 | [diff] [blame] | 1506 | input->extraBuffers.flush(); | 
| Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1507 | } | 
|  | 1508 | { | 
|  | 1509 | Mutexed<Output>::Locked output(mOutput); | 
|  | 1510 | output->buffers.reset(); | 
|  | 1511 | } | 
|  | 1512 | } | 
|  | 1513 |  | 
|  | 1514 | void CCodecBufferChannel::release() { | 
|  | 1515 | mComponent.reset(); | 
|  | 1516 | mInputAllocator.reset(); | 
|  | 1517 | mOutputSurface.lock()->surface.clear(); | 
|  | 1518 | { | 
|  | 1519 | Mutexed<BlockPools>::Locked blockPools{mBlockPools}; | 
|  | 1520 | blockPools->inputPool.reset(); | 
|  | 1521 | blockPools->outputPoolIntf.reset(); | 
|  | 1522 | } | 
| Wonsik Kim | a2e3cdd | 2020-05-20 15:14:42 -0700 | [diff] [blame] | 1523 | setCrypto(nullptr); | 
|  | 1524 | setDescrambler(nullptr); | 
| Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1525 | } | 
|  | 1526 |  | 
|  | 1527 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1528 | void CCodecBufferChannel::flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) { | 
|  | 1529 | ALOGV("[%s] flush", mName); | 
| Wonsik Kim | 6254525 | 2021-01-20 11:25:41 -0800 | [diff] [blame] | 1530 | std::vector<uint64_t> indices; | 
| Wonsik Kim | 5ebfcb2 | 2021-01-05 18:58:15 -0800 | [diff] [blame] | 1531 | std::list<std::unique_ptr<C2Work>> configs; | 
|  | 1532 | for (const std::unique_ptr<C2Work> &work : flushedWork) { | 
| Wonsik Kim | 6254525 | 2021-01-20 11:25:41 -0800 | [diff] [blame] | 1533 | indices.push_back(work->input.ordinal.frameIndex.peeku()); | 
| Wonsik Kim | 5ebfcb2 | 2021-01-05 18:58:15 -0800 | [diff] [blame] | 1534 | if (!(work->input.flags & C2FrameData::FLAG_CODEC_CONFIG)) { | 
|  | 1535 | continue; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1536 | } | 
| Wonsik Kim | 5ebfcb2 | 2021-01-05 18:58:15 -0800 | [diff] [blame] | 1537 | if (work->input.buffers.empty() | 
|  | 1538 | || work->input.buffers.front() == nullptr | 
|  | 1539 | || work->input.buffers.front()->data().linearBlocks().empty()) { | 
|  | 1540 | ALOGD("[%s] no linear codec config data found", mName); | 
|  | 1541 | continue; | 
|  | 1542 | } | 
|  | 1543 | std::unique_ptr<C2Work> copy(new C2Work); | 
|  | 1544 | copy->input.flags = C2FrameData::flags_t(work->input.flags | C2FrameData::FLAG_DROP_FRAME); | 
|  | 1545 | copy->input.ordinal = work->input.ordinal; | 
| Wonsik Kim | 6254525 | 2021-01-20 11:25:41 -0800 | [diff] [blame] | 1546 | copy->input.ordinal.frameIndex = mFrameIndex++; | 
| Wonsik Kim | 5ebfcb2 | 2021-01-05 18:58:15 -0800 | [diff] [blame] | 1547 | copy->input.buffers.insert( | 
|  | 1548 | copy->input.buffers.begin(), | 
|  | 1549 | work->input.buffers.begin(), | 
|  | 1550 | work->input.buffers.end()); | 
|  | 1551 | for (const std::unique_ptr<C2Param> ¶m : work->input.configUpdate) { | 
|  | 1552 | copy->input.configUpdate.push_back(C2Param::Copy(*param)); | 
|  | 1553 | } | 
|  | 1554 | copy->input.infoBuffers.insert( | 
|  | 1555 | copy->input.infoBuffers.begin(), | 
|  | 1556 | work->input.infoBuffers.begin(), | 
|  | 1557 | work->input.infoBuffers.end()); | 
|  | 1558 | copy->worklets.emplace_back(new C2Worklet); | 
|  | 1559 | configs.push_back(std::move(copy)); | 
|  | 1560 | ALOGV("[%s] stashed flushed codec config data", mName); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1561 | } | 
| Wonsik Kim | 5ebfcb2 | 2021-01-05 18:58:15 -0800 | [diff] [blame] | 1562 | mFlushedConfigs.lock()->swap(configs); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1563 | { | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1564 | Mutexed<Input>::Locked input(mInput); | 
|  | 1565 | input->buffers->flush(); | 
|  | 1566 | input->extraBuffers.flush(); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1567 | } | 
|  | 1568 | { | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1569 | Mutexed<Output>::Locked output(mOutput); | 
| Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1570 | if (output->buffers) { | 
|  | 1571 | output->buffers->flush(flushedWork); | 
| Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1572 | output->buffers->flushStash(); | 
| Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1573 | } | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1574 | } | 
| Wonsik Kim | 6254525 | 2021-01-20 11:25:41 -0800 | [diff] [blame] | 1575 | { | 
|  | 1576 | Mutexed<PipelineWatcher>::Locked watcher(mPipelineWatcher); | 
|  | 1577 | for (uint64_t index : indices) { | 
|  | 1578 | watcher->onWorkDone(index); | 
|  | 1579 | } | 
|  | 1580 | } | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1581 | } | 
|  | 1582 |  | 
|  | 1583 | void CCodecBufferChannel::onWorkDone( | 
|  | 1584 | std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat, | 
| Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1585 | const C2StreamInitDataInfo::output *initData) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1586 | if (handleWork(std::move(work), outputFormat, initData)) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1587 | feedInputBufferIfAvailable(); | 
|  | 1588 | } | 
|  | 1589 | } | 
|  | 1590 |  | 
|  | 1591 | void CCodecBufferChannel::onInputBufferDone( | 
| Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1592 | uint64_t frameIndex, size_t arrayIndex) { | 
| Pawin Vongmasa | 8e2cfb5 | 2019-05-15 05:20:52 -0700 | [diff] [blame] | 1593 | if (mInputSurface) { | 
|  | 1594 | return; | 
|  | 1595 | } | 
| Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1596 | std::shared_ptr<C2Buffer> buffer = | 
|  | 1597 | mPipelineWatcher.lock()->onInputBufferReleased(frameIndex, arrayIndex); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1598 | bool newInputSlotAvailable; | 
|  | 1599 | { | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1600 | Mutexed<Input>::Locked input(mInput); | 
|  | 1601 | newInputSlotAvailable = input->buffers->expireComponentBuffer(buffer); | 
|  | 1602 | if (!newInputSlotAvailable) { | 
|  | 1603 | (void)input->extraBuffers.expireComponentBuffer(buffer); | 
|  | 1604 | } | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1605 | } | 
|  | 1606 | if (newInputSlotAvailable) { | 
|  | 1607 | feedInputBufferIfAvailable(); | 
|  | 1608 | } | 
|  | 1609 | } | 
|  | 1610 |  | 
|  | 1611 | bool CCodecBufferChannel::handleWork( | 
|  | 1612 | std::unique_ptr<C2Work> work, | 
|  | 1613 | const sp<AMessage> &outputFormat, | 
|  | 1614 | const C2StreamInitDataInfo::output *initData) { | 
| Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1615 | { | 
| Wonsik Kim | a4e049d | 2020-04-28 19:42:23 +0000 | [diff] [blame] | 1616 | Mutexed<Output>::Locked output(mOutput); | 
| Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1617 | if (!output->buffers) { | 
|  | 1618 | return false; | 
|  | 1619 | } | 
| Wonsik Kim | e75a5da | 2020-02-14 17:29:03 -0800 | [diff] [blame] | 1620 | } | 
|  | 1621 |  | 
| Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1622 | // Whether the output buffer should be reported to the client or not. | 
|  | 1623 | bool notifyClient = false; | 
|  | 1624 |  | 
|  | 1625 | if (work->result == C2_OK){ | 
|  | 1626 | notifyClient = true; | 
|  | 1627 | } else if (work->result == C2_NOT_FOUND) { | 
|  | 1628 | ALOGD("[%s] flushed work; ignored.", mName); | 
|  | 1629 | } else { | 
|  | 1630 | // C2_OK and C2_NOT_FOUND are the only results that we accept for processing | 
|  | 1631 | // the config update. | 
|  | 1632 | ALOGD("[%s] work failed to complete: %d", mName, work->result); | 
|  | 1633 | mCCodecCallback->onError(work->result, ACTION_CODE_FATAL); | 
|  | 1634 | return false; | 
|  | 1635 | } | 
|  | 1636 |  | 
|  | 1637 | if ((work->input.ordinal.frameIndex - | 
|  | 1638 | mFirstValidFrameIndex.load()).peek() < 0) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1639 | // Discard frames from previous generation. | 
|  | 1640 | ALOGD("[%s] Discard frames from previous generation.", mName); | 
| Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1641 | notifyClient = false; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1642 | } | 
|  | 1643 |  | 
| Wonsik Kim | 524b058 | 2019-03-12 11:28:57 -0700 | [diff] [blame] | 1644 | if (mInputSurface == nullptr && (work->worklets.size() != 1u | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1645 | || !work->worklets.front() | 
| Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1646 | || !(work->worklets.front()->output.flags & | 
|  | 1647 | C2FrameData::FLAG_INCOMPLETE))) { | 
|  | 1648 | mPipelineWatcher.lock()->onWorkDone( | 
|  | 1649 | work->input.ordinal.frameIndex.peeku()); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1650 | } | 
|  | 1651 |  | 
|  | 1652 | // NOTE: MediaCodec usage supposedly have only one worklet | 
|  | 1653 | if (work->worklets.size() != 1u) { | 
|  | 1654 | ALOGI("[%s] onWorkDone: incorrect number of worklets: %zu", | 
|  | 1655 | mName, work->worklets.size()); | 
|  | 1656 | mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); | 
|  | 1657 | return false; | 
|  | 1658 | } | 
|  | 1659 |  | 
|  | 1660 | const std::unique_ptr<C2Worklet> &worklet = work->worklets.front(); | 
|  | 1661 |  | 
|  | 1662 | std::shared_ptr<C2Buffer> buffer; | 
|  | 1663 | // NOTE: MediaCodec usage supposedly have only one output stream. | 
|  | 1664 | if (worklet->output.buffers.size() > 1u) { | 
|  | 1665 | ALOGI("[%s] onWorkDone: incorrect number of output buffers: %zu", | 
|  | 1666 | mName, worklet->output.buffers.size()); | 
|  | 1667 | mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); | 
|  | 1668 | return false; | 
|  | 1669 | } else if (worklet->output.buffers.size() == 1u) { | 
|  | 1670 | buffer = worklet->output.buffers[0]; | 
|  | 1671 | if (!buffer) { | 
|  | 1672 | ALOGD("[%s] onWorkDone: nullptr found in buffers; ignored.", mName); | 
|  | 1673 | } | 
|  | 1674 | } | 
|  | 1675 |  | 
| Wonsik Kim | 3dedf68 | 2021-05-03 10:57:09 -0700 | [diff] [blame] | 1676 | std::optional<uint32_t> newInputDelay, newPipelineDelay, newOutputDelay, newReorderDepth; | 
|  | 1677 | std::optional<C2Config::ordinal_key_t> newReorderKey; | 
| Wonsik Kim | 315e40a | 2020-09-09 14:11:50 -0700 | [diff] [blame] | 1678 | bool needMaxDequeueBufferCountUpdate = false; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1679 | while (!worklet->output.configUpdate.empty()) { | 
|  | 1680 | std::unique_ptr<C2Param> param; | 
|  | 1681 | worklet->output.configUpdate.back().swap(param); | 
|  | 1682 | worklet->output.configUpdate.pop_back(); | 
|  | 1683 | switch (param->coreIndex().coreIndex()) { | 
|  | 1684 | case C2PortReorderBufferDepthTuning::CORE_INDEX: { | 
|  | 1685 | C2PortReorderBufferDepthTuning::output reorderDepth; | 
|  | 1686 | if (reorderDepth.updateFrom(*param)) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1687 | ALOGV("[%s] onWorkDone: updated reorder depth to %u", | 
|  | 1688 | mName, reorderDepth.value); | 
| Wonsik Kim | 3dedf68 | 2021-05-03 10:57:09 -0700 | [diff] [blame] | 1689 | newReorderDepth = reorderDepth.value; | 
| Wonsik Kim | 315e40a | 2020-09-09 14:11:50 -0700 | [diff] [blame] | 1690 | needMaxDequeueBufferCountUpdate = true; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1691 | } else { | 
| Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1692 | ALOGD("[%s] onWorkDone: failed to read reorder depth", | 
|  | 1693 | mName); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1694 | } | 
|  | 1695 | break; | 
|  | 1696 | } | 
|  | 1697 | case C2PortReorderKeySetting::CORE_INDEX: { | 
|  | 1698 | C2PortReorderKeySetting::output reorderKey; | 
|  | 1699 | if (reorderKey.updateFrom(*param)) { | 
| Wonsik Kim | 3dedf68 | 2021-05-03 10:57:09 -0700 | [diff] [blame] | 1700 | newReorderKey = reorderKey.value; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1701 | ALOGV("[%s] onWorkDone: updated reorder key to %u", | 
|  | 1702 | mName, reorderKey.value); | 
|  | 1703 | } else { | 
|  | 1704 | ALOGD("[%s] onWorkDone: failed to read reorder key", mName); | 
|  | 1705 | } | 
|  | 1706 | break; | 
|  | 1707 | } | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1708 | case C2PortActualDelayTuning::CORE_INDEX: { | 
|  | 1709 | if (param->isGlobal()) { | 
|  | 1710 | C2ActualPipelineDelayTuning pipelineDelay; | 
|  | 1711 | if (pipelineDelay.updateFrom(*param)) { | 
|  | 1712 | ALOGV("[%s] onWorkDone: updating pipeline delay %u", | 
|  | 1713 | mName, pipelineDelay.value); | 
|  | 1714 | newPipelineDelay = pipelineDelay.value; | 
| Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1715 | (void)mPipelineWatcher.lock()->pipelineDelay( | 
|  | 1716 | pipelineDelay.value); | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1717 | } | 
|  | 1718 | } | 
|  | 1719 | if (param->forInput()) { | 
|  | 1720 | C2PortActualDelayTuning::input inputDelay; | 
|  | 1721 | if (inputDelay.updateFrom(*param)) { | 
|  | 1722 | ALOGV("[%s] onWorkDone: updating input delay %u", | 
|  | 1723 | mName, inputDelay.value); | 
|  | 1724 | newInputDelay = inputDelay.value; | 
| Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1725 | (void)mPipelineWatcher.lock()->inputDelay( | 
|  | 1726 | inputDelay.value); | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1727 | } | 
|  | 1728 | } | 
|  | 1729 | if (param->forOutput()) { | 
|  | 1730 | C2PortActualDelayTuning::output outputDelay; | 
|  | 1731 | if (outputDelay.updateFrom(*param)) { | 
|  | 1732 | ALOGV("[%s] onWorkDone: updating output delay %u", | 
|  | 1733 | mName, outputDelay.value); | 
| Wonsik Kim | 315e40a | 2020-09-09 14:11:50 -0700 | [diff] [blame] | 1734 | (void)mPipelineWatcher.lock()->outputDelay(outputDelay.value); | 
| Wonsik Kim | 3dedf68 | 2021-05-03 10:57:09 -0700 | [diff] [blame] | 1735 | newOutputDelay = outputDelay.value; | 
| Wonsik Kim | 315e40a | 2020-09-09 14:11:50 -0700 | [diff] [blame] | 1736 | needMaxDequeueBufferCountUpdate = true; | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1737 |  | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1738 | } | 
|  | 1739 | } | 
|  | 1740 | break; | 
|  | 1741 | } | 
| ted.sun | b1fbfdb | 2020-06-23 14:03:41 +0800 | [diff] [blame] | 1742 | case C2PortTunnelSystemTime::CORE_INDEX: { | 
|  | 1743 | C2PortTunnelSystemTime::output frameRenderTime; | 
|  | 1744 | if (frameRenderTime.updateFrom(*param)) { | 
|  | 1745 | ALOGV("[%s] onWorkDone: frame rendered (sys:%lld ns, media:%lld us)", | 
|  | 1746 | mName, (long long)frameRenderTime.value, | 
|  | 1747 | (long long)worklet->output.ordinal.timestamp.peekll()); | 
|  | 1748 | mCCodecCallback->onOutputFramesRendered( | 
|  | 1749 | worklet->output.ordinal.timestamp.peek(), frameRenderTime.value); | 
|  | 1750 | } | 
|  | 1751 | break; | 
|  | 1752 | } | 
| Guillaume Chelfi | 867d4dd | 2021-07-01 18:38:45 +0200 | [diff] [blame] | 1753 | case C2StreamTunnelHoldRender::CORE_INDEX: { | 
|  | 1754 | C2StreamTunnelHoldRender::output firstTunnelFrameHoldRender; | 
|  | 1755 | if (!(worklet->output.flags & C2FrameData::FLAG_INCOMPLETE)) break; | 
|  | 1756 | if (!firstTunnelFrameHoldRender.updateFrom(*param)) break; | 
|  | 1757 | if (firstTunnelFrameHoldRender.value != C2_TRUE) break; | 
|  | 1758 | ALOGV("[%s] onWorkDone: first tunnel frame ready", mName); | 
|  | 1759 | mCCodecCallback->onFirstTunnelFrameReady(); | 
|  | 1760 | break; | 
|  | 1761 | } | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1762 | default: | 
|  | 1763 | ALOGV("[%s] onWorkDone: unrecognized config update (%08X)", | 
|  | 1764 | mName, param->index()); | 
|  | 1765 | break; | 
|  | 1766 | } | 
|  | 1767 | } | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1768 | if (newInputDelay || newPipelineDelay) { | 
|  | 1769 | Mutexed<Input>::Locked input(mInput); | 
|  | 1770 | size_t newNumSlots = | 
|  | 1771 | newInputDelay.value_or(input->inputDelay) + | 
|  | 1772 | newPipelineDelay.value_or(input->pipelineDelay) + | 
|  | 1773 | kSmoothnessFactor; | 
|  | 1774 | if (input->buffers->isArrayMode()) { | 
|  | 1775 | if (input->numSlots >= newNumSlots) { | 
|  | 1776 | input->numExtraSlots = 0; | 
|  | 1777 | } else { | 
|  | 1778 | input->numExtraSlots = newNumSlots - input->numSlots; | 
|  | 1779 | } | 
|  | 1780 | ALOGV("[%s] onWorkDone: updated number of extra slots to %zu (input array mode)", | 
|  | 1781 | mName, input->numExtraSlots); | 
|  | 1782 | } else { | 
|  | 1783 | input->numSlots = newNumSlots; | 
|  | 1784 | } | 
|  | 1785 | } | 
| Wonsik Kim | 3dedf68 | 2021-05-03 10:57:09 -0700 | [diff] [blame] | 1786 | size_t numOutputSlots = 0; | 
|  | 1787 | uint32_t reorderDepth = 0; | 
|  | 1788 | bool outputBuffersChanged = false; | 
|  | 1789 | if (newReorderKey || newReorderDepth || needMaxDequeueBufferCountUpdate) { | 
|  | 1790 | Mutexed<Output>::Locked output(mOutput); | 
|  | 1791 | if (!output->buffers) { | 
|  | 1792 | return false; | 
| Wonsik Kim | 315e40a | 2020-09-09 14:11:50 -0700 | [diff] [blame] | 1793 | } | 
| Wonsik Kim | 3dedf68 | 2021-05-03 10:57:09 -0700 | [diff] [blame] | 1794 | numOutputSlots = output->numSlots; | 
|  | 1795 | if (newReorderKey) { | 
|  | 1796 | output->buffers->setReorderKey(newReorderKey.value()); | 
|  | 1797 | } | 
|  | 1798 | if (newReorderDepth) { | 
|  | 1799 | output->buffers->setReorderDepth(newReorderDepth.value()); | 
|  | 1800 | } | 
|  | 1801 | reorderDepth = output->buffers->getReorderDepth(); | 
|  | 1802 | if (newOutputDelay) { | 
|  | 1803 | output->outputDelay = newOutputDelay.value(); | 
|  | 1804 | numOutputSlots = newOutputDelay.value() + kSmoothnessFactor; | 
|  | 1805 | if (output->numSlots < numOutputSlots) { | 
|  | 1806 | output->numSlots = numOutputSlots; | 
|  | 1807 | if (output->buffers->isArrayMode()) { | 
|  | 1808 | OutputBuffersArray *array = | 
|  | 1809 | (OutputBuffersArray *)output->buffers.get(); | 
|  | 1810 | ALOGV("[%s] onWorkDone: growing output buffer array to %zu", | 
|  | 1811 | mName, numOutputSlots); | 
|  | 1812 | array->grow(numOutputSlots); | 
|  | 1813 | outputBuffersChanged = true; | 
|  | 1814 | } | 
|  | 1815 | } | 
|  | 1816 | } | 
|  | 1817 | numOutputSlots = output->numSlots; | 
|  | 1818 | } | 
|  | 1819 | if (outputBuffersChanged) { | 
|  | 1820 | mCCodecCallback->onOutputBuffersChanged(); | 
|  | 1821 | } | 
|  | 1822 | if (needMaxDequeueBufferCountUpdate) { | 
| Wonsik Kim | 84f439f | 2021-05-03 10:57:09 -0700 | [diff] [blame] | 1823 | int maxDequeueCount = 0; | 
| Sungtak Lee | a714f11 | 2021-03-16 05:40:03 -0700 | [diff] [blame] | 1824 | { | 
|  | 1825 | Mutexed<OutputSurface>::Locked output(mOutputSurface); | 
|  | 1826 | maxDequeueCount = output->maxDequeueBuffers = | 
|  | 1827 | numOutputSlots + reorderDepth + kRenderingDepth; | 
|  | 1828 | if (output->surface) { | 
|  | 1829 | output->surface->setMaxDequeuedBufferCount(output->maxDequeueBuffers); | 
|  | 1830 | } | 
|  | 1831 | } | 
|  | 1832 | if (maxDequeueCount > 0) { | 
|  | 1833 | mComponent->setOutputSurfaceMaxDequeueCount(maxDequeueCount); | 
| Wonsik Kim | 315e40a | 2020-09-09 14:11:50 -0700 | [diff] [blame] | 1834 | } | 
|  | 1835 | } | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1836 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1837 | int32_t flags = 0; | 
|  | 1838 | if (worklet->output.flags & C2FrameData::FLAG_END_OF_STREAM) { | 
|  | 1839 | flags |= MediaCodec::BUFFER_FLAG_EOS; | 
|  | 1840 | ALOGV("[%s] onWorkDone: output EOS", mName); | 
|  | 1841 | } | 
|  | 1842 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1843 | // WORKAROUND: adjust output timestamp based on client input timestamp and codec | 
|  | 1844 | // input timestamp. Codec output timestamp (in the timestamp field) shall correspond to | 
|  | 1845 | // the codec input timestamp, but client output timestamp should (reported in timeUs) | 
|  | 1846 | // shall correspond to the client input timesamp (in customOrdinal). By using the | 
|  | 1847 | // delta between the two, this allows for some timestamp deviation - e.g. if one input | 
|  | 1848 | // produces multiple output. | 
|  | 1849 | c2_cntr64_t timestamp = | 
|  | 1850 | worklet->output.ordinal.timestamp + work->input.ordinal.customOrdinal | 
|  | 1851 | - work->input.ordinal.timestamp; | 
| Wonsik Kim | 95ba016 | 2019-03-19 15:51:54 -0700 | [diff] [blame] | 1852 | if (mInputSurface != nullptr) { | 
|  | 1853 | // When using input surface we need to restore the original input timestamp. | 
|  | 1854 | timestamp = work->input.ordinal.customOrdinal; | 
|  | 1855 | } | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1856 | ALOGV("[%s] onWorkDone: input %lld, codec %lld => output %lld => %lld", | 
|  | 1857 | mName, | 
|  | 1858 | work->input.ordinal.customOrdinal.peekll(), | 
|  | 1859 | work->input.ordinal.timestamp.peekll(), | 
|  | 1860 | worklet->output.ordinal.timestamp.peekll(), | 
|  | 1861 | timestamp.peekll()); | 
|  | 1862 |  | 
| Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1863 | // csd cannot be re-ordered and will always arrive first. | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1864 | if (initData != nullptr) { | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1865 | Mutexed<Output>::Locked output(mOutput); | 
| Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1866 | if (output->buffers && outputFormat) { | 
|  | 1867 | output->buffers->updateSkipCutBuffer(outputFormat); | 
|  | 1868 | output->buffers->setFormat(outputFormat); | 
|  | 1869 | } | 
|  | 1870 | if (!notifyClient) { | 
|  | 1871 | return false; | 
|  | 1872 | } | 
|  | 1873 | size_t index; | 
|  | 1874 | sp<MediaCodecBuffer> outBuffer; | 
| Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1875 | if (output->buffers && output->buffers->registerCsd(initData, &index, &outBuffer) == OK) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1876 | outBuffer->meta()->setInt64("timeUs", timestamp.peek()); | 
|  | 1877 | outBuffer->meta()->setInt32("flags", MediaCodec::BUFFER_FLAG_CODECCONFIG); | 
|  | 1878 | ALOGV("[%s] onWorkDone: csd index = %zu [%p]", mName, index, outBuffer.get()); | 
|  | 1879 |  | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1880 | output.unlock(); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1881 | mCallback->onOutputBufferAvailable(index, outBuffer); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1882 | } else { | 
|  | 1883 | ALOGD("[%s] onWorkDone: unable to register csd", mName); | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1884 | output.unlock(); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1885 | mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1886 | return false; | 
|  | 1887 | } | 
|  | 1888 | } | 
|  | 1889 |  | 
| Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1890 | if (notifyClient && !buffer && !flags) { | 
| Wonsik Kim | 35bf573 | 2020-05-14 17:40:29 +0000 | [diff] [blame] | 1891 | ALOGV("[%s] onWorkDone: Not reporting output buffer (%lld)", | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1892 | mName, work->input.ordinal.frameIndex.peekull()); | 
| Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1893 | notifyClient = false; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1894 | } | 
|  | 1895 |  | 
|  | 1896 | if (buffer) { | 
|  | 1897 | for (const std::shared_ptr<const C2Info> &info : buffer->info()) { | 
|  | 1898 | // TODO: properly translate these to metadata | 
|  | 1899 | switch (info->coreIndex().coreIndex()) { | 
|  | 1900 | case C2StreamPictureTypeMaskInfo::CORE_INDEX: | 
| Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 1901 | if (((C2StreamPictureTypeMaskInfo *)info.get())->value & C2Config::SYNC_FRAME) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1902 | flags |= MediaCodec::BUFFER_FLAG_SYNCFRAME; | 
|  | 1903 | } | 
|  | 1904 | break; | 
|  | 1905 | default: | 
|  | 1906 | break; | 
|  | 1907 | } | 
|  | 1908 | } | 
|  | 1909 | } | 
|  | 1910 |  | 
|  | 1911 | { | 
| Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1912 | Mutexed<Output>::Locked output(mOutput); | 
| Wonsik Kim | c23cc40 | 2020-05-28 14:53:40 -0700 | [diff] [blame] | 1913 | if (!output->buffers) { | 
|  | 1914 | return false; | 
|  | 1915 | } | 
| Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1916 | output->buffers->pushToStash( | 
|  | 1917 | buffer, | 
|  | 1918 | notifyClient, | 
|  | 1919 | timestamp.peek(), | 
|  | 1920 | flags, | 
|  | 1921 | outputFormat, | 
|  | 1922 | worklet->output.ordinal); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1923 | } | 
|  | 1924 | sendOutputBuffers(); | 
|  | 1925 | return true; | 
|  | 1926 | } | 
|  | 1927 |  | 
|  | 1928 | void CCodecBufferChannel::sendOutputBuffers() { | 
| Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1929 | OutputBuffers::BufferAction action; | 
| Wonsik Kim | a4e049d | 2020-04-28 19:42:23 +0000 | [diff] [blame] | 1930 | size_t index; | 
| Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1931 | sp<MediaCodecBuffer> outBuffer; | 
|  | 1932 | std::shared_ptr<C2Buffer> c2Buffer; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1933 |  | 
|  | 1934 | while (true) { | 
| Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1935 | Mutexed<Output>::Locked output(mOutput); | 
| Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1936 | if (!output->buffers) { | 
|  | 1937 | return; | 
|  | 1938 | } | 
| Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1939 | action = output->buffers->popFromStashAndRegister( | 
|  | 1940 | &c2Buffer, &index, &outBuffer); | 
|  | 1941 | switch (action) { | 
|  | 1942 | case OutputBuffers::SKIP: | 
|  | 1943 | return; | 
|  | 1944 | case OutputBuffers::DISCARD: | 
|  | 1945 | break; | 
|  | 1946 | case OutputBuffers::NOTIFY_CLIENT: | 
| Wonsik Kim | a4e049d | 2020-04-28 19:42:23 +0000 | [diff] [blame] | 1947 | output.unlock(); | 
| Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1948 | mCallback->onOutputBufferAvailable(index, outBuffer); | 
|  | 1949 | break; | 
|  | 1950 | case OutputBuffers::REALLOCATE: | 
|  | 1951 | if (!output->buffers->isArrayMode()) { | 
|  | 1952 | output->buffers = | 
|  | 1953 | output->buffers->toArrayMode(output->numSlots); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1954 | } | 
| Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1955 | static_cast<OutputBuffersArray*>(output->buffers.get())-> | 
|  | 1956 | realloc(c2Buffer); | 
|  | 1957 | output.unlock(); | 
|  | 1958 | mCCodecCallback->onOutputBuffersChanged(); | 
| Wonsik Kim | 4ada73d | 2020-05-26 14:58:07 -0700 | [diff] [blame] | 1959 | break; | 
| Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1960 | case OutputBuffers::RETRY: | 
|  | 1961 | ALOGV("[%s] sendOutputBuffers: unable to register output buffer", | 
|  | 1962 | mName); | 
|  | 1963 | return; | 
|  | 1964 | default: | 
|  | 1965 | LOG_ALWAYS_FATAL("[%s] sendOutputBuffers: " | 
|  | 1966 | "corrupted BufferAction value (%d) " | 
|  | 1967 | "returned from popFromStashAndRegister.", | 
|  | 1968 | mName, int(action)); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1969 | return; | 
|  | 1970 | } | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1971 | } | 
|  | 1972 | } | 
|  | 1973 |  | 
|  | 1974 | status_t CCodecBufferChannel::setSurface(const sp<Surface> &newSurface) { | 
|  | 1975 | static std::atomic_uint32_t surfaceGeneration{0}; | 
|  | 1976 | uint32_t generation = (getpid() << 10) | | 
|  | 1977 | ((surfaceGeneration.fetch_add(1, std::memory_order_relaxed) + 1) | 
|  | 1978 | & ((1 << 10) - 1)); | 
|  | 1979 |  | 
|  | 1980 | sp<IGraphicBufferProducer> producer; | 
| Sungtak Lee | db14cba | 2021-04-10 00:50:23 -0700 | [diff] [blame] | 1981 | int maxDequeueCount = mOutputSurface.lock()->maxDequeueBuffers; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1982 | if (newSurface) { | 
|  | 1983 | newSurface->setScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); | 
| Sungtak Lee | ab6f2f3 | 2019-02-15 14:43:51 -0800 | [diff] [blame] | 1984 | newSurface->setDequeueTimeout(kDequeueTimeoutNs); | 
| Sungtak Lee | db14cba | 2021-04-10 00:50:23 -0700 | [diff] [blame] | 1985 | newSurface->setMaxDequeuedBufferCount(maxDequeueCount); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1986 | producer = newSurface->getIGraphicBufferProducer(); | 
|  | 1987 | producer->setGenerationNumber(generation); | 
|  | 1988 | } else { | 
|  | 1989 | ALOGE("[%s] setting output surface to null", mName); | 
|  | 1990 | return INVALID_OPERATION; | 
|  | 1991 | } | 
|  | 1992 |  | 
|  | 1993 | std::shared_ptr<Codec2Client::Configurable> outputPoolIntf; | 
|  | 1994 | C2BlockPool::local_id_t outputPoolId; | 
|  | 1995 | { | 
|  | 1996 | Mutexed<BlockPools>::Locked pools(mBlockPools); | 
|  | 1997 | outputPoolId = pools->outputPoolId; | 
|  | 1998 | outputPoolIntf = pools->outputPoolIntf; | 
|  | 1999 | } | 
|  | 2000 |  | 
|  | 2001 | if (outputPoolIntf) { | 
|  | 2002 | if (mComponent->setOutputSurface( | 
|  | 2003 | outputPoolId, | 
|  | 2004 | producer, | 
| Sungtak Lee | db14cba | 2021-04-10 00:50:23 -0700 | [diff] [blame] | 2005 | generation, | 
|  | 2006 | maxDequeueCount) != C2_OK) { | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 2007 | ALOGI("[%s] setSurface: component setOutputSurface failed", mName); | 
|  | 2008 | return INVALID_OPERATION; | 
|  | 2009 | } | 
|  | 2010 | } | 
|  | 2011 |  | 
|  | 2012 | { | 
|  | 2013 | Mutexed<OutputSurface>::Locked output(mOutputSurface); | 
|  | 2014 | output->surface = newSurface; | 
|  | 2015 | output->generation = generation; | 
|  | 2016 | } | 
|  | 2017 |  | 
|  | 2018 | return OK; | 
|  | 2019 | } | 
|  | 2020 |  | 
| Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 2021 | PipelineWatcher::Clock::duration CCodecBufferChannel::elapsed() { | 
| Wonsik Kim | 4fa4f2b | 2019-02-13 11:02:58 -0800 | [diff] [blame] | 2022 | // When client pushed EOS, we want all the work to be done quickly. | 
|  | 2023 | // Otherwise, component may have stalled work due to input starvation up to | 
|  | 2024 | // the sum of the delay in the pipeline. | 
| Wonsik Kim | f0e7d22 | 2019-06-28 12:33:16 -0700 | [diff] [blame] | 2025 | size_t n = 0; | 
|  | 2026 | if (!mInputMetEos) { | 
|  | 2027 | size_t outputDelay = mOutput.lock()->outputDelay; | 
|  | 2028 | Mutexed<Input>::Locked input(mInput); | 
|  | 2029 | n = input->inputDelay + input->pipelineDelay + outputDelay; | 
|  | 2030 | } | 
| Wonsik Kim | 4fa4f2b | 2019-02-13 11:02:58 -0800 | [diff] [blame] | 2031 | return mPipelineWatcher.lock()->elapsed(PipelineWatcher::Clock::now(), n); | 
| Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 2032 | } | 
|  | 2033 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 2034 | void CCodecBufferChannel::setMetaMode(MetaMode mode) { | 
|  | 2035 | mMetaMode = mode; | 
|  | 2036 | } | 
|  | 2037 |  | 
| Wonsik Kim | 596187e | 2019-10-25 12:44:10 -0700 | [diff] [blame] | 2038 | void CCodecBufferChannel::setCrypto(const sp<ICrypto> &crypto) { | 
| Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 2039 | if (mCrypto != nullptr) { | 
|  | 2040 | for (std::pair<wp<HidlMemory>, int32_t> entry : mHeapSeqNumMap) { | 
|  | 2041 | mCrypto->unsetHeap(entry.second); | 
|  | 2042 | } | 
|  | 2043 | mHeapSeqNumMap.clear(); | 
|  | 2044 | if (mHeapSeqNum >= 0) { | 
|  | 2045 | mCrypto->unsetHeap(mHeapSeqNum); | 
|  | 2046 | mHeapSeqNum = -1; | 
|  | 2047 | } | 
|  | 2048 | } | 
| Wonsik Kim | 596187e | 2019-10-25 12:44:10 -0700 | [diff] [blame] | 2049 | mCrypto = crypto; | 
|  | 2050 | } | 
|  | 2051 |  | 
|  | 2052 | void CCodecBufferChannel::setDescrambler(const sp<IDescrambler> &descrambler) { | 
|  | 2053 | mDescrambler = descrambler; | 
|  | 2054 | } | 
|  | 2055 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 2056 | status_t toStatusT(c2_status_t c2s, c2_operation_t c2op) { | 
|  | 2057 | // C2_OK is always translated to OK. | 
|  | 2058 | if (c2s == C2_OK) { | 
|  | 2059 | return OK; | 
|  | 2060 | } | 
|  | 2061 |  | 
|  | 2062 | // Operation-dependent translation | 
|  | 2063 | // TODO: Add as necessary | 
|  | 2064 | switch (c2op) { | 
|  | 2065 | case C2_OPERATION_Component_start: | 
|  | 2066 | switch (c2s) { | 
|  | 2067 | case C2_NO_MEMORY: | 
|  | 2068 | return NO_MEMORY; | 
|  | 2069 | default: | 
|  | 2070 | return UNKNOWN_ERROR; | 
|  | 2071 | } | 
|  | 2072 | default: | 
|  | 2073 | break; | 
|  | 2074 | } | 
|  | 2075 |  | 
|  | 2076 | // Backup operation-agnostic translation | 
|  | 2077 | switch (c2s) { | 
|  | 2078 | case C2_BAD_INDEX: | 
|  | 2079 | return BAD_INDEX; | 
|  | 2080 | case C2_BAD_VALUE: | 
|  | 2081 | return BAD_VALUE; | 
|  | 2082 | case C2_BLOCKING: | 
|  | 2083 | return WOULD_BLOCK; | 
|  | 2084 | case C2_DUPLICATE: | 
|  | 2085 | return ALREADY_EXISTS; | 
|  | 2086 | case C2_NO_INIT: | 
|  | 2087 | return NO_INIT; | 
|  | 2088 | case C2_NO_MEMORY: | 
|  | 2089 | return NO_MEMORY; | 
|  | 2090 | case C2_NOT_FOUND: | 
|  | 2091 | return NAME_NOT_FOUND; | 
|  | 2092 | case C2_TIMED_OUT: | 
|  | 2093 | return TIMED_OUT; | 
|  | 2094 | case C2_BAD_STATE: | 
|  | 2095 | case C2_CANCELED: | 
|  | 2096 | case C2_CANNOT_DO: | 
|  | 2097 | case C2_CORRUPTED: | 
|  | 2098 | case C2_OMITTED: | 
|  | 2099 | case C2_REFUSED: | 
|  | 2100 | return UNKNOWN_ERROR; | 
|  | 2101 | default: | 
|  | 2102 | return -static_cast<status_t>(c2s); | 
|  | 2103 | } | 
|  | 2104 | } | 
|  | 2105 |  | 
|  | 2106 | }  // namespace android |