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