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