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