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