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