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