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() { |
Taehwan Kim | da0517d | 2020-09-16 17:29:37 +0900 | [diff] [blame] | 621 | if (mInputMetEos) { |
Wonsik Kim | df5dd14 | 2019-02-06 10:15:46 -0800 | [diff] [blame] | 622 | return; |
Pawin Vongmasa | c3c536d | 2020-06-12 04:00:04 -0700 | [diff] [blame] | 623 | } |
| 624 | { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 625 | Mutexed<Output>::Locked output(mOutput); |
Pawin Vongmasa | c3c536d | 2020-06-12 04:00:04 -0700 | [diff] [blame] | 626 | if (!output->buffers || |
| 627 | output->buffers->hasPending() || |
| 628 | output->buffers->numClientBuffers() >= output->numSlots) { |
Wonsik Kim | df5dd14 | 2019-02-06 10:15:46 -0800 | [diff] [blame] | 629 | return; |
| 630 | } |
| 631 | } |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 632 | size_t numInputSlots = mInput.lock()->numSlots; |
| 633 | for (size_t i = 0; i < numInputSlots; ++i) { |
Taehwan Kim | da0517d | 2020-09-16 17:29:37 +0900 | [diff] [blame] | 634 | if (mPipelineWatcher.lock()->pipelineFull()) { |
| 635 | return; |
| 636 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 637 | sp<MediaCodecBuffer> inBuffer; |
| 638 | size_t index; |
| 639 | { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 640 | Mutexed<Input>::Locked input(mInput); |
| 641 | if (input->buffers->numClientBuffers() >= input->numSlots) { |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 642 | return; |
| 643 | } |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 644 | if (!input->buffers->requestNewBuffer(&index, &inBuffer)) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 645 | ALOGV("[%s] no new buffer available", mName); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 646 | break; |
| 647 | } |
| 648 | } |
| 649 | ALOGV("[%s] new input index = %zu [%p]", mName, index, inBuffer.get()); |
| 650 | mCallback->onInputBufferAvailable(index, inBuffer); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | status_t CCodecBufferChannel::renderOutputBuffer( |
| 655 | const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) { |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 656 | ALOGV("[%s] renderOutputBuffer: %p", mName, buffer.get()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 657 | std::shared_ptr<C2Buffer> c2Buffer; |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 658 | bool released = false; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 659 | { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 660 | Mutexed<Output>::Locked output(mOutput); |
| 661 | if (output->buffers) { |
| 662 | released = output->buffers->releaseBuffer(buffer, &c2Buffer); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 663 | } |
| 664 | } |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 665 | // NOTE: some apps try to releaseOutputBuffer() with timestamp and/or render |
| 666 | // set to true. |
| 667 | sendOutputBuffers(); |
| 668 | // input buffer feeding may have been gated by pending output buffers |
| 669 | feedInputBufferIfAvailable(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 670 | if (!c2Buffer) { |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 671 | if (released) { |
Wonsik Kim | f7529dd | 2019-04-18 17:35:53 -0700 | [diff] [blame] | 672 | std::call_once(mRenderWarningFlag, [this] { |
| 673 | ALOGW("[%s] The app is calling releaseOutputBuffer() with " |
| 674 | "timestamp or render=true with non-video buffers. Apps should " |
| 675 | "call releaseOutputBuffer() with render=false for those.", |
| 676 | mName); |
| 677 | }); |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 678 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 679 | return INVALID_OPERATION; |
| 680 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 681 | |
| 682 | #if 0 |
| 683 | const std::vector<std::shared_ptr<const C2Info>> infoParams = c2Buffer->info(); |
| 684 | ALOGV("[%s] queuing gfx buffer with %zu infos", mName, infoParams.size()); |
| 685 | for (const std::shared_ptr<const C2Info> &info : infoParams) { |
| 686 | AString res; |
| 687 | for (size_t ix = 0; ix + 3 < info->size(); ix += 4) { |
| 688 | if (ix) res.append(", "); |
| 689 | res.append(*((int32_t*)info.get() + (ix / 4))); |
| 690 | } |
| 691 | ALOGV(" [%s]", res.c_str()); |
| 692 | } |
| 693 | #endif |
| 694 | std::shared_ptr<const C2StreamRotationInfo::output> rotation = |
| 695 | std::static_pointer_cast<const C2StreamRotationInfo::output>( |
| 696 | c2Buffer->getInfo(C2StreamRotationInfo::output::PARAM_TYPE)); |
| 697 | bool flip = rotation && (rotation->flip & 1); |
| 698 | uint32_t quarters = ((rotation ? rotation->value : 0) / 90) & 3; |
| 699 | uint32_t transform = 0; |
| 700 | switch (quarters) { |
| 701 | case 0: // no rotation |
| 702 | transform = flip ? HAL_TRANSFORM_FLIP_H : 0; |
| 703 | break; |
| 704 | case 1: // 90 degrees counter-clockwise |
| 705 | transform = flip ? (HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90) |
| 706 | : HAL_TRANSFORM_ROT_270; |
| 707 | break; |
| 708 | case 2: // 180 degrees |
| 709 | transform = flip ? HAL_TRANSFORM_FLIP_V : HAL_TRANSFORM_ROT_180; |
| 710 | break; |
| 711 | case 3: // 90 degrees clockwise |
| 712 | transform = flip ? (HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90) |
| 713 | : HAL_TRANSFORM_ROT_90; |
| 714 | break; |
| 715 | } |
| 716 | |
| 717 | std::shared_ptr<const C2StreamSurfaceScalingInfo::output> surfaceScaling = |
| 718 | std::static_pointer_cast<const C2StreamSurfaceScalingInfo::output>( |
| 719 | c2Buffer->getInfo(C2StreamSurfaceScalingInfo::output::PARAM_TYPE)); |
| 720 | uint32_t videoScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW; |
| 721 | if (surfaceScaling) { |
| 722 | videoScalingMode = surfaceScaling->value; |
| 723 | } |
| 724 | |
| 725 | // Use dataspace from format as it has the default aspects already applied |
| 726 | android_dataspace_t dataSpace = HAL_DATASPACE_UNKNOWN; // this is 0 |
| 727 | (void)buffer->format()->findInt32("android._dataspace", (int32_t *)&dataSpace); |
| 728 | |
| 729 | // HDR static info |
| 730 | std::shared_ptr<const C2StreamHdrStaticInfo::output> hdrStaticInfo = |
| 731 | std::static_pointer_cast<const C2StreamHdrStaticInfo::output>( |
| 732 | c2Buffer->getInfo(C2StreamHdrStaticInfo::output::PARAM_TYPE)); |
| 733 | |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 734 | // HDR10 plus info |
| 735 | std::shared_ptr<const C2StreamHdr10PlusInfo::output> hdr10PlusInfo = |
| 736 | std::static_pointer_cast<const C2StreamHdr10PlusInfo::output>( |
| 737 | c2Buffer->getInfo(C2StreamHdr10PlusInfo::output::PARAM_TYPE)); |
Yichi Chen | 54be23c | 2020-06-15 14:30:53 +0800 | [diff] [blame] | 738 | if (hdr10PlusInfo && hdr10PlusInfo->flexCount() == 0) { |
| 739 | hdr10PlusInfo.reset(); |
| 740 | } |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 741 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 742 | { |
| 743 | Mutexed<OutputSurface>::Locked output(mOutputSurface); |
| 744 | if (output->surface == nullptr) { |
| 745 | ALOGI("[%s] cannot render buffer without surface", mName); |
| 746 | return OK; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | std::vector<C2ConstGraphicBlock> blocks = c2Buffer->data().graphicBlocks(); |
| 751 | if (blocks.size() != 1u) { |
| 752 | ALOGD("[%s] expected 1 graphic block, but got %zu", mName, blocks.size()); |
| 753 | return UNKNOWN_ERROR; |
| 754 | } |
| 755 | const C2ConstGraphicBlock &block = blocks.front(); |
| 756 | |
| 757 | // TODO: revisit this after C2Fence implementation. |
| 758 | android::IGraphicBufferProducer::QueueBufferInput qbi( |
| 759 | timestampNs, |
| 760 | false, // droppable |
| 761 | dataSpace, |
| 762 | Rect(blocks.front().crop().left, |
| 763 | blocks.front().crop().top, |
| 764 | blocks.front().crop().right(), |
| 765 | blocks.front().crop().bottom()), |
| 766 | videoScalingMode, |
| 767 | transform, |
| 768 | Fence::NO_FENCE, 0); |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 769 | if (hdrStaticInfo || hdr10PlusInfo) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 770 | HdrMetadata hdr; |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 771 | if (hdrStaticInfo) { |
wenchangliu | f3f9288 | 2020-05-14 00:02:01 +0800 | [diff] [blame] | 772 | // If mastering max and min luminance fields are 0, do not use them. |
| 773 | // It indicates the value may not be present in the stream. |
| 774 | if (hdrStaticInfo->mastering.maxLuminance > 0.0f && |
| 775 | hdrStaticInfo->mastering.minLuminance > 0.0f) { |
| 776 | struct android_smpte2086_metadata smpte2086_meta = { |
| 777 | .displayPrimaryRed = { |
| 778 | hdrStaticInfo->mastering.red.x, hdrStaticInfo->mastering.red.y |
| 779 | }, |
| 780 | .displayPrimaryGreen = { |
| 781 | hdrStaticInfo->mastering.green.x, hdrStaticInfo->mastering.green.y |
| 782 | }, |
| 783 | .displayPrimaryBlue = { |
| 784 | hdrStaticInfo->mastering.blue.x, hdrStaticInfo->mastering.blue.y |
| 785 | }, |
| 786 | .whitePoint = { |
| 787 | hdrStaticInfo->mastering.white.x, hdrStaticInfo->mastering.white.y |
| 788 | }, |
| 789 | .maxLuminance = hdrStaticInfo->mastering.maxLuminance, |
| 790 | .minLuminance = hdrStaticInfo->mastering.minLuminance, |
| 791 | }; |
Yichi Chen | 54be23c | 2020-06-15 14:30:53 +0800 | [diff] [blame] | 792 | hdr.validTypes |= HdrMetadata::SMPTE2086; |
wenchangliu | f3f9288 | 2020-05-14 00:02:01 +0800 | [diff] [blame] | 793 | hdr.smpte2086 = smpte2086_meta; |
| 794 | } |
Chong Zhang | 3bb2a7f | 2020-04-21 10:35:12 -0700 | [diff] [blame] | 795 | // If the content light level fields are 0, do not use them, it |
| 796 | // indicates the value may not be present in the stream. |
| 797 | if (hdrStaticInfo->maxCll > 0.0f && hdrStaticInfo->maxFall > 0.0f) { |
| 798 | struct android_cta861_3_metadata cta861_meta = { |
| 799 | .maxContentLightLevel = hdrStaticInfo->maxCll, |
| 800 | .maxFrameAverageLightLevel = hdrStaticInfo->maxFall, |
| 801 | }; |
| 802 | hdr.validTypes |= HdrMetadata::CTA861_3; |
| 803 | hdr.cta8613 = cta861_meta; |
| 804 | } |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 805 | } |
| 806 | if (hdr10PlusInfo) { |
| 807 | hdr.validTypes |= HdrMetadata::HDR10PLUS; |
| 808 | hdr.hdr10plus.assign( |
| 809 | hdr10PlusInfo->m.value, |
| 810 | hdr10PlusInfo->m.value + hdr10PlusInfo->flexCount()); |
| 811 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 812 | qbi.setHdrMetadata(hdr); |
| 813 | } |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 814 | // we don't have dirty regions |
| 815 | qbi.setSurfaceDamage(Region::INVALID_REGION); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 816 | android::IGraphicBufferProducer::QueueBufferOutput qbo; |
| 817 | status_t result = mComponent->queueToOutputSurface(block, qbi, &qbo); |
| 818 | if (result != OK) { |
| 819 | ALOGI("[%s] queueBuffer failed: %d", mName, result); |
| 820 | return result; |
| 821 | } |
| 822 | ALOGV("[%s] queue buffer successful", mName); |
| 823 | |
| 824 | int64_t mediaTimeUs = 0; |
| 825 | (void)buffer->meta()->findInt64("timeUs", &mediaTimeUs); |
| 826 | mCCodecCallback->onOutputFramesRendered(mediaTimeUs, timestampNs); |
| 827 | |
| 828 | return OK; |
| 829 | } |
| 830 | |
| 831 | status_t CCodecBufferChannel::discardBuffer(const sp<MediaCodecBuffer> &buffer) { |
| 832 | ALOGV("[%s] discardBuffer: %p", mName, buffer.get()); |
| 833 | bool released = false; |
| 834 | { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 835 | Mutexed<Input>::Locked input(mInput); |
| 836 | if (input->buffers && input->buffers->releaseBuffer(buffer, nullptr, true)) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 837 | released = true; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 841 | Mutexed<Output>::Locked output(mOutput); |
| 842 | if (output->buffers && output->buffers->releaseBuffer(buffer, nullptr)) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 843 | released = true; |
| 844 | } |
| 845 | } |
| 846 | if (released) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 847 | sendOutputBuffers(); |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 848 | feedInputBufferIfAvailable(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 849 | } else { |
| 850 | ALOGD("[%s] MediaCodec discarded an unknown buffer", mName); |
| 851 | } |
| 852 | return OK; |
| 853 | } |
| 854 | |
| 855 | void CCodecBufferChannel::getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) { |
| 856 | array->clear(); |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 857 | Mutexed<Input>::Locked input(mInput); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 858 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 859 | if (!input->buffers->isArrayMode()) { |
| 860 | input->buffers = input->buffers->toArrayMode(input->numSlots); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 861 | } |
| 862 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 863 | input->buffers->getArray(array); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | void CCodecBufferChannel::getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) { |
| 867 | array->clear(); |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 868 | Mutexed<Output>::Locked output(mOutput); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 869 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 870 | if (!output->buffers->isArrayMode()) { |
| 871 | output->buffers = output->buffers->toArrayMode(output->numSlots); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 872 | } |
| 873 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 874 | output->buffers->getArray(array); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | status_t CCodecBufferChannel::start( |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 878 | const sp<AMessage> &inputFormat, |
| 879 | const sp<AMessage> &outputFormat, |
| 880 | bool buffersBoundToCodec) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 881 | C2StreamBufferTypeSetting::input iStreamFormat(0u); |
| 882 | C2StreamBufferTypeSetting::output oStreamFormat(0u); |
| 883 | C2PortReorderBufferDepthTuning::output reorderDepth; |
| 884 | C2PortReorderKeySetting::output reorderKey; |
Wonsik Kim | 078b58e | 2019-01-09 15:08:06 -0800 | [diff] [blame] | 885 | C2PortActualDelayTuning::input inputDelay(0); |
| 886 | C2PortActualDelayTuning::output outputDelay(0); |
| 887 | C2ActualPipelineDelayTuning pipelineDelay(0); |
| 888 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 889 | c2_status_t err = mComponent->query( |
| 890 | { |
| 891 | &iStreamFormat, |
| 892 | &oStreamFormat, |
| 893 | &reorderDepth, |
| 894 | &reorderKey, |
Wonsik Kim | 078b58e | 2019-01-09 15:08:06 -0800 | [diff] [blame] | 895 | &inputDelay, |
| 896 | &pipelineDelay, |
| 897 | &outputDelay, |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 898 | }, |
| 899 | {}, |
| 900 | C2_DONT_BLOCK, |
| 901 | nullptr); |
| 902 | if (err == C2_BAD_INDEX) { |
| 903 | if (!iStreamFormat || !oStreamFormat) { |
| 904 | return UNKNOWN_ERROR; |
| 905 | } |
| 906 | } else if (err != C2_OK) { |
| 907 | return UNKNOWN_ERROR; |
| 908 | } |
| 909 | |
Wonsik Kim | 4fa4f2b | 2019-02-13 11:02:58 -0800 | [diff] [blame] | 910 | uint32_t inputDelayValue = inputDelay ? inputDelay.value : 0; |
| 911 | uint32_t pipelineDelayValue = pipelineDelay ? pipelineDelay.value : 0; |
| 912 | uint32_t outputDelayValue = outputDelay ? outputDelay.value : 0; |
| 913 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 914 | size_t numInputSlots = inputDelayValue + pipelineDelayValue + kSmoothnessFactor; |
| 915 | size_t numOutputSlots = outputDelayValue + kSmoothnessFactor; |
Wonsik Kim | 078b58e | 2019-01-09 15:08:06 -0800 | [diff] [blame] | 916 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 917 | // TODO: get this from input format |
| 918 | bool secure = mComponent->getName().find(".secure") != std::string::npos; |
| 919 | |
| 920 | std::shared_ptr<C2AllocatorStore> allocatorStore = GetCodec2PlatformAllocatorStore(); |
Pin-chih Lin | aa18ea5 | 2019-11-19 18:48:50 +0800 | [diff] [blame] | 921 | int poolMask = GetCodec2PoolMask(); |
| 922 | C2PlatformAllocatorStore::id_t preferredLinearId = GetPreferredLinearAllocatorId(poolMask); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 923 | |
| 924 | if (inputFormat != nullptr) { |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 925 | bool graphic = (iStreamFormat.value == C2BufferData::GRAPHIC); |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 926 | C2Config::api_feature_t apiFeatures = C2Config::api_feature_t( |
| 927 | API_REFLECTION | |
| 928 | API_VALUES | |
| 929 | API_CURRENT_VALUES | |
| 930 | API_DEPENDENCY | |
| 931 | API_SAME_INPUT_BUFFER); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 932 | std::shared_ptr<C2BlockPool> pool; |
| 933 | { |
| 934 | Mutexed<BlockPools>::Locked pools(mBlockPools); |
| 935 | |
| 936 | // set default allocator ID. |
| 937 | pools->inputAllocatorId = (graphic) ? C2PlatformAllocatorStore::GRALLOC |
Pin-chih Lin | aa18ea5 | 2019-11-19 18:48:50 +0800 | [diff] [blame] | 938 | : preferredLinearId; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 939 | |
| 940 | // query C2PortAllocatorsTuning::input from component. If an allocator ID is obtained |
| 941 | // from component, create the input block pool with given ID. Otherwise, use default IDs. |
| 942 | std::vector<std::unique_ptr<C2Param>> params; |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 943 | C2ApiFeaturesSetting featuresSetting{apiFeatures}; |
| 944 | err = mComponent->query({ &featuresSetting }, |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 945 | { C2PortAllocatorsTuning::input::PARAM_TYPE }, |
| 946 | C2_DONT_BLOCK, |
| 947 | ¶ms); |
| 948 | if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) { |
| 949 | ALOGD("[%s] Query input allocators returned %zu params => %s (%u)", |
| 950 | mName, params.size(), asString(err), err); |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 951 | } else if (params.size() == 1) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 952 | C2PortAllocatorsTuning::input *inputAllocators = |
| 953 | C2PortAllocatorsTuning::input::From(params[0].get()); |
| 954 | if (inputAllocators && inputAllocators->flexCount() > 0) { |
| 955 | std::shared_ptr<C2Allocator> allocator; |
| 956 | // verify allocator IDs and resolve default allocator |
| 957 | allocatorStore->fetchAllocator(inputAllocators->m.values[0], &allocator); |
| 958 | if (allocator) { |
| 959 | pools->inputAllocatorId = allocator->getId(); |
| 960 | } else { |
| 961 | ALOGD("[%s] component requested invalid input allocator ID %u", |
| 962 | mName, inputAllocators->m.values[0]); |
| 963 | } |
| 964 | } |
| 965 | } |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 966 | if (featuresSetting) { |
| 967 | apiFeatures = featuresSetting.value; |
| 968 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 969 | |
| 970 | // TODO: use C2Component wrapper to associate this pool with ourselves |
| 971 | if ((poolMask >> pools->inputAllocatorId) & 1) { |
| 972 | err = CreateCodec2BlockPool(pools->inputAllocatorId, nullptr, &pool); |
| 973 | ALOGD("[%s] Created input block pool with allocatorID %u => poolID %llu - %s (%d)", |
| 974 | mName, pools->inputAllocatorId, |
| 975 | (unsigned long long)(pool ? pool->getLocalId() : 111000111), |
| 976 | asString(err), err); |
| 977 | } else { |
| 978 | err = C2_NOT_FOUND; |
| 979 | } |
| 980 | if (err != C2_OK) { |
| 981 | C2BlockPool::local_id_t inputPoolId = |
| 982 | graphic ? C2BlockPool::BASIC_GRAPHIC : C2BlockPool::BASIC_LINEAR; |
| 983 | err = GetCodec2BlockPool(inputPoolId, nullptr, &pool); |
| 984 | ALOGD("[%s] Using basic input block pool with poolID %llu => got %llu - %s (%d)", |
| 985 | mName, (unsigned long long)inputPoolId, |
| 986 | (unsigned long long)(pool ? pool->getLocalId() : 111000111), |
| 987 | asString(err), err); |
| 988 | if (err != C2_OK) { |
| 989 | return NO_MEMORY; |
| 990 | } |
| 991 | } |
| 992 | pools->inputPool = pool; |
| 993 | } |
| 994 | |
Wonsik Kim | 5105126 | 2018-11-28 13:59:05 -0800 | [diff] [blame] | 995 | bool forceArrayMode = false; |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 996 | Mutexed<Input>::Locked input(mInput); |
Wonsik Kim | bdffead | 2019-07-01 12:00:07 -0700 | [diff] [blame] | 997 | input->inputDelay = inputDelayValue; |
| 998 | input->pipelineDelay = pipelineDelayValue; |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 999 | input->numSlots = numInputSlots; |
| 1000 | input->extraBuffers.flush(); |
| 1001 | input->numExtraSlots = 0u; |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 1002 | bool conforming = (apiFeatures & API_SAME_INPUT_BUFFER); |
| 1003 | // For encrypted content, framework decrypts source buffer (ashmem) into |
| 1004 | // C2Buffers. Thus non-conforming codecs can process these. |
| 1005 | if (!buffersBoundToCodec && (hasCryptoOrDescrambler() || conforming)) { |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 1006 | input->buffers.reset(new SlotInputBuffers(mName)); |
| 1007 | } else if (graphic) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1008 | if (mInputSurface) { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1009 | input->buffers.reset(new DummyInputBuffers(mName)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1010 | } else if (mMetaMode == MODE_ANW) { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1011 | input->buffers.reset(new GraphicMetadataInputBuffers(mName)); |
Wonsik Kim | 1221fd1 | 2019-07-12 12:52:05 -0700 | [diff] [blame] | 1012 | // This is to ensure buffers do not get released prematurely. |
| 1013 | // TODO: handle this without going into array mode |
| 1014 | forceArrayMode = true; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1015 | } else { |
Wonsik Kim | 41d8343 | 2020-04-27 16:40:49 -0700 | [diff] [blame] | 1016 | input->buffers.reset(new GraphicInputBuffers(mName)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1017 | } |
| 1018 | } else { |
| 1019 | if (hasCryptoOrDescrambler()) { |
| 1020 | int32_t capacity = kLinearBufferSize; |
| 1021 | (void)inputFormat->findInt32(KEY_MAX_INPUT_SIZE, &capacity); |
| 1022 | if ((size_t)capacity > kMaxLinearBufferSize) { |
| 1023 | ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize); |
| 1024 | capacity = kMaxLinearBufferSize; |
| 1025 | } |
| 1026 | if (mDealer == nullptr) { |
| 1027 | mDealer = new MemoryDealer( |
| 1028 | align(capacity, MemoryDealer::getAllocationAlignment()) |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1029 | * (numInputSlots + 1), |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1030 | "EncryptedLinearInputBuffers"); |
| 1031 | mDecryptDestination = mDealer->allocate((size_t)capacity); |
| 1032 | } |
| 1033 | if (mCrypto != nullptr && mHeapSeqNum < 0) { |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 1034 | sp<HidlMemory> heap = fromHeap(mDealer->getMemoryHeap()); |
| 1035 | mHeapSeqNum = mCrypto->setHeap(heap); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1036 | } else { |
| 1037 | mHeapSeqNum = -1; |
| 1038 | } |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1039 | input->buffers.reset(new EncryptedLinearInputBuffers( |
Wonsik Kim | 078b58e | 2019-01-09 15:08:06 -0800 | [diff] [blame] | 1040 | secure, mDealer, mCrypto, mHeapSeqNum, (size_t)capacity, |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1041 | numInputSlots, mName)); |
Wonsik Kim | 5105126 | 2018-11-28 13:59:05 -0800 | [diff] [blame] | 1042 | forceArrayMode = true; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1043 | } else { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1044 | input->buffers.reset(new LinearInputBuffers(mName)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1045 | } |
| 1046 | } |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1047 | input->buffers->setFormat(inputFormat); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1048 | |
| 1049 | if (err == C2_OK) { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1050 | input->buffers->setPool(pool); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1051 | } else { |
| 1052 | // TODO: error |
| 1053 | } |
Wonsik Kim | 5105126 | 2018-11-28 13:59:05 -0800 | [diff] [blame] | 1054 | |
| 1055 | if (forceArrayMode) { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1056 | input->buffers = input->buffers->toArrayMode(numInputSlots); |
Wonsik Kim | 5105126 | 2018-11-28 13:59:05 -0800 | [diff] [blame] | 1057 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | if (outputFormat != nullptr) { |
| 1061 | sp<IGraphicBufferProducer> outputSurface; |
| 1062 | uint32_t outputGeneration; |
| 1063 | { |
| 1064 | Mutexed<OutputSurface>::Locked output(mOutputSurface); |
Sungtak Lee | d7463d1 | 2019-09-04 16:01:00 -0700 | [diff] [blame] | 1065 | output->maxDequeueBuffers = numOutputSlots + |
Sungtak Lee | 7a7b742 | 2019-07-16 17:40:40 -0700 | [diff] [blame] | 1066 | reorderDepth.value + kRenderingDepth; |
Sungtak Lee | d7463d1 | 2019-09-04 16:01:00 -0700 | [diff] [blame] | 1067 | if (!secure) { |
| 1068 | output->maxDequeueBuffers += numInputSlots; |
| 1069 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1070 | outputSurface = output->surface ? |
| 1071 | output->surface->getIGraphicBufferProducer() : nullptr; |
Wonsik Kim | f5e5c83 | 2019-02-21 11:36:05 -0800 | [diff] [blame] | 1072 | if (outputSurface) { |
| 1073 | output->surface->setMaxDequeuedBufferCount(output->maxDequeueBuffers); |
| 1074 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1075 | outputGeneration = output->generation; |
| 1076 | } |
| 1077 | |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 1078 | bool graphic = (oStreamFormat.value == C2BufferData::GRAPHIC); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1079 | C2BlockPool::local_id_t outputPoolId_; |
| 1080 | |
| 1081 | { |
| 1082 | Mutexed<BlockPools>::Locked pools(mBlockPools); |
| 1083 | |
| 1084 | // set default allocator ID. |
| 1085 | pools->outputAllocatorId = (graphic) ? C2PlatformAllocatorStore::GRALLOC |
Pin-chih Lin | aa18ea5 | 2019-11-19 18:48:50 +0800 | [diff] [blame] | 1086 | : preferredLinearId; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1087 | |
| 1088 | // query C2PortAllocatorsTuning::output from component, or use default allocator if |
| 1089 | // unsuccessful. |
| 1090 | std::vector<std::unique_ptr<C2Param>> params; |
| 1091 | err = mComponent->query({ }, |
| 1092 | { C2PortAllocatorsTuning::output::PARAM_TYPE }, |
| 1093 | C2_DONT_BLOCK, |
| 1094 | ¶ms); |
| 1095 | if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) { |
| 1096 | ALOGD("[%s] Query output allocators returned %zu params => %s (%u)", |
| 1097 | mName, params.size(), asString(err), err); |
| 1098 | } else if (err == C2_OK && params.size() == 1) { |
| 1099 | C2PortAllocatorsTuning::output *outputAllocators = |
| 1100 | C2PortAllocatorsTuning::output::From(params[0].get()); |
| 1101 | if (outputAllocators && outputAllocators->flexCount() > 0) { |
| 1102 | std::shared_ptr<C2Allocator> allocator; |
| 1103 | // verify allocator IDs and resolve default allocator |
| 1104 | allocatorStore->fetchAllocator(outputAllocators->m.values[0], &allocator); |
| 1105 | if (allocator) { |
| 1106 | pools->outputAllocatorId = allocator->getId(); |
| 1107 | } else { |
| 1108 | ALOGD("[%s] component requested invalid output allocator ID %u", |
| 1109 | mName, outputAllocators->m.values[0]); |
| 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | // use bufferqueue if outputting to a surface. |
| 1115 | // query C2PortSurfaceAllocatorTuning::output from component, or use default allocator |
| 1116 | // if unsuccessful. |
| 1117 | if (outputSurface) { |
| 1118 | params.clear(); |
| 1119 | err = mComponent->query({ }, |
| 1120 | { C2PortSurfaceAllocatorTuning::output::PARAM_TYPE }, |
| 1121 | C2_DONT_BLOCK, |
| 1122 | ¶ms); |
| 1123 | if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) { |
| 1124 | ALOGD("[%s] Query output surface allocator returned %zu params => %s (%u)", |
| 1125 | mName, params.size(), asString(err), err); |
| 1126 | } else if (err == C2_OK && params.size() == 1) { |
| 1127 | C2PortSurfaceAllocatorTuning::output *surfaceAllocator = |
| 1128 | C2PortSurfaceAllocatorTuning::output::From(params[0].get()); |
| 1129 | if (surfaceAllocator) { |
| 1130 | std::shared_ptr<C2Allocator> allocator; |
| 1131 | // verify allocator IDs and resolve default allocator |
| 1132 | allocatorStore->fetchAllocator(surfaceAllocator->value, &allocator); |
| 1133 | if (allocator) { |
| 1134 | pools->outputAllocatorId = allocator->getId(); |
| 1135 | } else { |
| 1136 | ALOGD("[%s] component requested invalid surface output allocator ID %u", |
| 1137 | mName, surfaceAllocator->value); |
| 1138 | err = C2_BAD_VALUE; |
| 1139 | } |
| 1140 | } |
| 1141 | } |
| 1142 | if (pools->outputAllocatorId == C2PlatformAllocatorStore::GRALLOC |
| 1143 | && err != C2_OK |
| 1144 | && ((poolMask >> C2PlatformAllocatorStore::BUFFERQUEUE) & 1)) { |
| 1145 | pools->outputAllocatorId = C2PlatformAllocatorStore::BUFFERQUEUE; |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | if ((poolMask >> pools->outputAllocatorId) & 1) { |
| 1150 | err = mComponent->createBlockPool( |
| 1151 | pools->outputAllocatorId, &pools->outputPoolId, &pools->outputPoolIntf); |
| 1152 | ALOGI("[%s] Created output block pool with allocatorID %u => poolID %llu - %s", |
| 1153 | mName, pools->outputAllocatorId, |
| 1154 | (unsigned long long)pools->outputPoolId, |
| 1155 | asString(err)); |
| 1156 | } else { |
| 1157 | err = C2_NOT_FOUND; |
| 1158 | } |
| 1159 | if (err != C2_OK) { |
| 1160 | // use basic pool instead |
| 1161 | pools->outputPoolId = |
| 1162 | graphic ? C2BlockPool::BASIC_GRAPHIC : C2BlockPool::BASIC_LINEAR; |
| 1163 | } |
| 1164 | |
| 1165 | // Configure output block pool ID as parameter C2PortBlockPoolsTuning::output to |
| 1166 | // component. |
| 1167 | std::unique_ptr<C2PortBlockPoolsTuning::output> poolIdsTuning = |
| 1168 | C2PortBlockPoolsTuning::output::AllocUnique({ pools->outputPoolId }); |
| 1169 | |
| 1170 | std::vector<std::unique_ptr<C2SettingResult>> failures; |
| 1171 | err = mComponent->config({ poolIdsTuning.get() }, C2_MAY_BLOCK, &failures); |
| 1172 | ALOGD("[%s] Configured output block pool ids %llu => %s", |
| 1173 | mName, (unsigned long long)poolIdsTuning->m.values[0], asString(err)); |
| 1174 | outputPoolId_ = pools->outputPoolId; |
| 1175 | } |
| 1176 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1177 | Mutexed<Output>::Locked output(mOutput); |
Wonsik Kim | bdffead | 2019-07-01 12:00:07 -0700 | [diff] [blame] | 1178 | output->outputDelay = outputDelayValue; |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1179 | output->numSlots = numOutputSlots; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1180 | if (graphic) { |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 1181 | if (outputSurface || !buffersBoundToCodec) { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1182 | output->buffers.reset(new GraphicOutputBuffers(mName)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1183 | } else { |
Wonsik Kim | 41d8343 | 2020-04-27 16:40:49 -0700 | [diff] [blame] | 1184 | output->buffers.reset(new RawGraphicOutputBuffers(mName)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1185 | } |
| 1186 | } else { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1187 | output->buffers.reset(new LinearOutputBuffers(mName)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1188 | } |
Wonsik Kim | e4716c0 | 2020-02-28 10:42:21 -0800 | [diff] [blame] | 1189 | output->buffers->setFormat(outputFormat); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1190 | |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1191 | output->buffers->clearStash(); |
| 1192 | if (reorderDepth) { |
| 1193 | output->buffers->setReorderDepth(reorderDepth.value); |
| 1194 | } |
| 1195 | if (reorderKey) { |
| 1196 | output->buffers->setReorderKey(reorderKey.value); |
| 1197 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1198 | |
| 1199 | // Try to set output surface to created block pool if given. |
| 1200 | if (outputSurface) { |
| 1201 | mComponent->setOutputSurface( |
| 1202 | outputPoolId_, |
| 1203 | outputSurface, |
| 1204 | outputGeneration); |
| 1205 | } |
| 1206 | |
Wonsik Kim | 8ab25aa | 2019-06-24 16:37:37 -0700 | [diff] [blame] | 1207 | if (oStreamFormat.value == C2BufferData::LINEAR) { |
Wonsik Kim | 5871330 | 2020-01-29 22:25:23 -0800 | [diff] [blame] | 1208 | if (buffersBoundToCodec) { |
| 1209 | // WORKAROUND: if we're using early CSD workaround we convert to |
| 1210 | // array mode, to appease apps assuming the output |
| 1211 | // buffers to be of the same size. |
| 1212 | output->buffers = output->buffers->toArrayMode(numOutputSlots); |
| 1213 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1214 | |
| 1215 | int32_t channelCount; |
| 1216 | int32_t sampleRate; |
| 1217 | if (outputFormat->findInt32(KEY_CHANNEL_COUNT, &channelCount) |
| 1218 | && outputFormat->findInt32(KEY_SAMPLE_RATE, &sampleRate)) { |
| 1219 | int32_t delay = 0; |
| 1220 | int32_t padding = 0;; |
| 1221 | if (!outputFormat->findInt32("encoder-delay", &delay)) { |
| 1222 | delay = 0; |
| 1223 | } |
| 1224 | if (!outputFormat->findInt32("encoder-padding", &padding)) { |
| 1225 | padding = 0; |
| 1226 | } |
| 1227 | if (delay || padding) { |
| 1228 | // We need write access to the buffers, and we're already in |
| 1229 | // array mode. |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1230 | output->buffers->initSkipCutBuffer(delay, padding, sampleRate, channelCount); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1231 | } |
| 1232 | } |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | // Set up pipeline control. This has to be done after mInputBuffers and |
| 1237 | // mOutputBuffers are initialized to make sure that lingering callbacks |
| 1238 | // about buffers from the previous generation do not interfere with the |
| 1239 | // newly initialized pipeline capacity. |
| 1240 | |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1241 | { |
| 1242 | Mutexed<PipelineWatcher>::Locked watcher(mPipelineWatcher); |
Wonsik Kim | 4fa4f2b | 2019-02-13 11:02:58 -0800 | [diff] [blame] | 1243 | watcher->inputDelay(inputDelayValue) |
| 1244 | .pipelineDelay(pipelineDelayValue) |
| 1245 | .outputDelay(outputDelayValue) |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1246 | .smoothnessFactor(kSmoothnessFactor); |
| 1247 | watcher->flush(); |
| 1248 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1249 | |
| 1250 | mInputMetEos = false; |
| 1251 | mSync.start(); |
| 1252 | return OK; |
| 1253 | } |
| 1254 | |
| 1255 | status_t CCodecBufferChannel::requestInitialInputBuffers() { |
| 1256 | if (mInputSurface) { |
| 1257 | return OK; |
| 1258 | } |
| 1259 | |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 1260 | C2StreamBufferTypeSetting::output oStreamFormat(0u); |
Wonsik Kim | 8ab25aa | 2019-06-24 16:37:37 -0700 | [diff] [blame] | 1261 | C2PrependHeaderModeSetting prepend(PREPEND_HEADER_TO_NONE); |
| 1262 | c2_status_t err = mComponent->query({ &oStreamFormat, &prepend }, {}, C2_DONT_BLOCK, nullptr); |
| 1263 | if (err != C2_OK && err != C2_BAD_INDEX) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1264 | return UNKNOWN_ERROR; |
| 1265 | } |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1266 | size_t numInputSlots = mInput.lock()->numSlots; |
Pawin Vongmasa | e7bb861 | 2020-06-04 06:15:22 -0700 | [diff] [blame] | 1267 | |
| 1268 | struct ClientInputBuffer { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1269 | size_t index; |
| 1270 | sp<MediaCodecBuffer> buffer; |
Pawin Vongmasa | e7bb861 | 2020-06-04 06:15:22 -0700 | [diff] [blame] | 1271 | size_t capacity; |
| 1272 | }; |
| 1273 | std::list<ClientInputBuffer> clientInputBuffers; |
| 1274 | |
| 1275 | { |
| 1276 | Mutexed<Input>::Locked input(mInput); |
| 1277 | while (clientInputBuffers.size() < numInputSlots) { |
| 1278 | ClientInputBuffer clientInputBuffer; |
| 1279 | if (!input->buffers->requestNewBuffer(&clientInputBuffer.index, |
| 1280 | &clientInputBuffer.buffer)) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1281 | break; |
| 1282 | } |
Pawin Vongmasa | e7bb861 | 2020-06-04 06:15:22 -0700 | [diff] [blame] | 1283 | clientInputBuffer.capacity = clientInputBuffer.buffer->capacity(); |
| 1284 | clientInputBuffers.emplace_back(std::move(clientInputBuffer)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1285 | } |
Pawin Vongmasa | e7bb861 | 2020-06-04 06:15:22 -0700 | [diff] [blame] | 1286 | } |
| 1287 | if (clientInputBuffers.empty()) { |
| 1288 | ALOGW("[%s] start: cannot allocate memory at all", mName); |
| 1289 | return NO_MEMORY; |
| 1290 | } else if (clientInputBuffers.size() < numInputSlots) { |
| 1291 | ALOGD("[%s] start: cannot allocate memory for all slots, " |
| 1292 | "only %zu buffers allocated", |
| 1293 | mName, clientInputBuffers.size()); |
| 1294 | } else { |
| 1295 | ALOGV("[%s] %zu initial input buffers available", |
| 1296 | mName, clientInputBuffers.size()); |
| 1297 | } |
| 1298 | // Sort input buffers by their capacities in increasing order. |
| 1299 | clientInputBuffers.sort( |
| 1300 | [](const ClientInputBuffer& a, const ClientInputBuffer& b) { |
| 1301 | return a.capacity < b.capacity; |
| 1302 | }); |
| 1303 | |
| 1304 | { |
| 1305 | Mutexed<std::list<sp<ABuffer>>>::Locked configs(mFlushedConfigs); |
| 1306 | if (!configs->empty()) { |
| 1307 | while (!configs->empty()) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1308 | sp<ABuffer> config = configs->front(); |
Pawin Vongmasa | 472c738 | 2019-03-26 18:13:58 -0700 | [diff] [blame] | 1309 | configs->pop_front(); |
Pawin Vongmasa | e7bb861 | 2020-06-04 06:15:22 -0700 | [diff] [blame] | 1310 | // Find the smallest input buffer that can fit the config. |
| 1311 | auto i = std::find_if( |
| 1312 | clientInputBuffers.begin(), |
| 1313 | clientInputBuffers.end(), |
| 1314 | [cfgSize = config->size()](const ClientInputBuffer& b) { |
| 1315 | return b.capacity >= cfgSize; |
| 1316 | }); |
| 1317 | if (i == clientInputBuffers.end()) { |
| 1318 | ALOGW("[%s] no input buffer large enough for the config " |
| 1319 | "(%zu bytes)", |
| 1320 | mName, config->size()); |
| 1321 | return NO_MEMORY; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1322 | } |
Pawin Vongmasa | e7bb861 | 2020-06-04 06:15:22 -0700 | [diff] [blame] | 1323 | sp<MediaCodecBuffer> buffer = i->buffer; |
| 1324 | memcpy(buffer->base(), config->data(), config->size()); |
| 1325 | buffer->setRange(0, config->size()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1326 | buffer->meta()->clear(); |
| 1327 | buffer->meta()->setInt64("timeUs", 0); |
Pawin Vongmasa | e7bb861 | 2020-06-04 06:15:22 -0700 | [diff] [blame] | 1328 | buffer->meta()->setInt32("csd", 1); |
| 1329 | if (queueInputBufferInternal(buffer) != OK) { |
| 1330 | ALOGW("[%s] Error while queueing a flushed config", |
| 1331 | mName); |
| 1332 | return UNKNOWN_ERROR; |
| 1333 | } |
| 1334 | clientInputBuffers.erase(i); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1335 | } |
Pawin Vongmasa | e7bb861 | 2020-06-04 06:15:22 -0700 | [diff] [blame] | 1336 | } else if (oStreamFormat.value == C2BufferData::LINEAR && |
| 1337 | (!prepend || prepend.value == PREPEND_HEADER_TO_NONE)) { |
| 1338 | sp<MediaCodecBuffer> buffer = clientInputBuffers.front().buffer; |
| 1339 | // WORKAROUND: Some apps expect CSD available without queueing |
| 1340 | // any input. Queue an empty buffer to get the CSD. |
| 1341 | buffer->setRange(0, 0); |
| 1342 | buffer->meta()->clear(); |
| 1343 | buffer->meta()->setInt64("timeUs", 0); |
| 1344 | if (queueInputBufferInternal(buffer) != OK) { |
| 1345 | ALOGW("[%s] Error while queueing an empty buffer to get CSD", |
| 1346 | mName); |
| 1347 | return UNKNOWN_ERROR; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1348 | } |
Pawin Vongmasa | e7bb861 | 2020-06-04 06:15:22 -0700 | [diff] [blame] | 1349 | clientInputBuffers.pop_front(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1350 | } |
| 1351 | } |
Pawin Vongmasa | e7bb861 | 2020-06-04 06:15:22 -0700 | [diff] [blame] | 1352 | |
| 1353 | for (const ClientInputBuffer& clientInputBuffer: clientInputBuffers) { |
| 1354 | mCallback->onInputBufferAvailable( |
| 1355 | clientInputBuffer.index, |
| 1356 | clientInputBuffer.buffer); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1357 | } |
Pawin Vongmasa | e7bb861 | 2020-06-04 06:15:22 -0700 | [diff] [blame] | 1358 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1359 | return OK; |
| 1360 | } |
| 1361 | |
| 1362 | void CCodecBufferChannel::stop() { |
| 1363 | mSync.stop(); |
| 1364 | mFirstValidFrameIndex = mFrameIndex.load(std::memory_order_relaxed); |
| 1365 | if (mInputSurface != nullptr) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1366 | mInputSurface.reset(); |
| 1367 | } |
Wonsik Kim | a2e3cdd | 2020-05-20 15:14:42 -0700 | [diff] [blame] | 1368 | mPipelineWatcher.lock()->flush(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1369 | } |
| 1370 | |
Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1371 | void CCodecBufferChannel::reset() { |
| 1372 | stop(); |
| 1373 | { |
| 1374 | Mutexed<Input>::Locked input(mInput); |
| 1375 | input->buffers.reset(new DummyInputBuffers("")); |
Wonsik Kim | a2e3cdd | 2020-05-20 15:14:42 -0700 | [diff] [blame] | 1376 | input->extraBuffers.flush(); |
Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1377 | } |
| 1378 | { |
| 1379 | Mutexed<Output>::Locked output(mOutput); |
| 1380 | output->buffers.reset(); |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | void CCodecBufferChannel::release() { |
| 1385 | mComponent.reset(); |
| 1386 | mInputAllocator.reset(); |
| 1387 | mOutputSurface.lock()->surface.clear(); |
| 1388 | { |
| 1389 | Mutexed<BlockPools>::Locked blockPools{mBlockPools}; |
| 1390 | blockPools->inputPool.reset(); |
| 1391 | blockPools->outputPoolIntf.reset(); |
| 1392 | } |
Wonsik Kim | a2e3cdd | 2020-05-20 15:14:42 -0700 | [diff] [blame] | 1393 | setCrypto(nullptr); |
| 1394 | setDescrambler(nullptr); |
Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1398 | void CCodecBufferChannel::flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) { |
| 1399 | ALOGV("[%s] flush", mName); |
| 1400 | { |
| 1401 | Mutexed<std::list<sp<ABuffer>>>::Locked configs(mFlushedConfigs); |
| 1402 | for (const std::unique_ptr<C2Work> &work : flushedWork) { |
| 1403 | if (!(work->input.flags & C2FrameData::FLAG_CODEC_CONFIG)) { |
| 1404 | continue; |
| 1405 | } |
| 1406 | if (work->input.buffers.empty() |
| 1407 | || work->input.buffers.front()->data().linearBlocks().empty()) { |
| 1408 | ALOGD("[%s] no linear codec config data found", mName); |
| 1409 | continue; |
| 1410 | } |
| 1411 | C2ReadView view = |
| 1412 | work->input.buffers.front()->data().linearBlocks().front().map().get(); |
| 1413 | if (view.error() != C2_OK) { |
| 1414 | ALOGD("[%s] failed to map flushed codec config data: %d", mName, view.error()); |
| 1415 | continue; |
| 1416 | } |
| 1417 | configs->push_back(ABuffer::CreateAsCopy(view.data(), view.capacity())); |
| 1418 | ALOGV("[%s] stashed flushed codec config data (size=%u)", mName, view.capacity()); |
| 1419 | } |
| 1420 | } |
| 1421 | { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1422 | Mutexed<Input>::Locked input(mInput); |
| 1423 | input->buffers->flush(); |
| 1424 | input->extraBuffers.flush(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1425 | } |
| 1426 | { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1427 | Mutexed<Output>::Locked output(mOutput); |
Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1428 | if (output->buffers) { |
| 1429 | output->buffers->flush(flushedWork); |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1430 | output->buffers->flushStash(); |
Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1431 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1432 | } |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1433 | mPipelineWatcher.lock()->flush(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | void CCodecBufferChannel::onWorkDone( |
| 1437 | std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat, |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1438 | const C2StreamInitDataInfo::output *initData) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1439 | if (handleWork(std::move(work), outputFormat, initData)) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1440 | feedInputBufferIfAvailable(); |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | void CCodecBufferChannel::onInputBufferDone( |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1445 | uint64_t frameIndex, size_t arrayIndex) { |
Pawin Vongmasa | 8e2cfb5 | 2019-05-15 05:20:52 -0700 | [diff] [blame] | 1446 | if (mInputSurface) { |
| 1447 | return; |
| 1448 | } |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1449 | std::shared_ptr<C2Buffer> buffer = |
| 1450 | mPipelineWatcher.lock()->onInputBufferReleased(frameIndex, arrayIndex); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1451 | bool newInputSlotAvailable; |
| 1452 | { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1453 | Mutexed<Input>::Locked input(mInput); |
| 1454 | newInputSlotAvailable = input->buffers->expireComponentBuffer(buffer); |
| 1455 | if (!newInputSlotAvailable) { |
| 1456 | (void)input->extraBuffers.expireComponentBuffer(buffer); |
| 1457 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1458 | } |
| 1459 | if (newInputSlotAvailable) { |
| 1460 | feedInputBufferIfAvailable(); |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | bool CCodecBufferChannel::handleWork( |
| 1465 | std::unique_ptr<C2Work> work, |
| 1466 | const sp<AMessage> &outputFormat, |
| 1467 | const C2StreamInitDataInfo::output *initData) { |
Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1468 | { |
Wonsik Kim | a4e049d | 2020-04-28 19:42:23 +0000 | [diff] [blame] | 1469 | Mutexed<Output>::Locked output(mOutput); |
Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1470 | if (!output->buffers) { |
| 1471 | return false; |
| 1472 | } |
Wonsik Kim | e75a5da | 2020-02-14 17:29:03 -0800 | [diff] [blame] | 1473 | } |
| 1474 | |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1475 | // Whether the output buffer should be reported to the client or not. |
| 1476 | bool notifyClient = false; |
| 1477 | |
| 1478 | if (work->result == C2_OK){ |
| 1479 | notifyClient = true; |
| 1480 | } else if (work->result == C2_NOT_FOUND) { |
| 1481 | ALOGD("[%s] flushed work; ignored.", mName); |
| 1482 | } else { |
| 1483 | // C2_OK and C2_NOT_FOUND are the only results that we accept for processing |
| 1484 | // the config update. |
| 1485 | ALOGD("[%s] work failed to complete: %d", mName, work->result); |
| 1486 | mCCodecCallback->onError(work->result, ACTION_CODE_FATAL); |
| 1487 | return false; |
| 1488 | } |
| 1489 | |
| 1490 | if ((work->input.ordinal.frameIndex - |
| 1491 | mFirstValidFrameIndex.load()).peek() < 0) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1492 | // Discard frames from previous generation. |
| 1493 | ALOGD("[%s] Discard frames from previous generation.", mName); |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1494 | notifyClient = false; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1495 | } |
| 1496 | |
Wonsik Kim | 524b058 | 2019-03-12 11:28:57 -0700 | [diff] [blame] | 1497 | if (mInputSurface == nullptr && (work->worklets.size() != 1u |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1498 | || !work->worklets.front() |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1499 | || !(work->worklets.front()->output.flags & |
| 1500 | C2FrameData::FLAG_INCOMPLETE))) { |
| 1501 | mPipelineWatcher.lock()->onWorkDone( |
| 1502 | work->input.ordinal.frameIndex.peeku()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | // NOTE: MediaCodec usage supposedly have only one worklet |
| 1506 | if (work->worklets.size() != 1u) { |
| 1507 | ALOGI("[%s] onWorkDone: incorrect number of worklets: %zu", |
| 1508 | mName, work->worklets.size()); |
| 1509 | mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 1510 | return false; |
| 1511 | } |
| 1512 | |
| 1513 | const std::unique_ptr<C2Worklet> &worklet = work->worklets.front(); |
| 1514 | |
| 1515 | std::shared_ptr<C2Buffer> buffer; |
| 1516 | // NOTE: MediaCodec usage supposedly have only one output stream. |
| 1517 | if (worklet->output.buffers.size() > 1u) { |
| 1518 | ALOGI("[%s] onWorkDone: incorrect number of output buffers: %zu", |
| 1519 | mName, worklet->output.buffers.size()); |
| 1520 | mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 1521 | return false; |
| 1522 | } else if (worklet->output.buffers.size() == 1u) { |
| 1523 | buffer = worklet->output.buffers[0]; |
| 1524 | if (!buffer) { |
| 1525 | ALOGD("[%s] onWorkDone: nullptr found in buffers; ignored.", mName); |
| 1526 | } |
| 1527 | } |
| 1528 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1529 | std::optional<uint32_t> newInputDelay, newPipelineDelay; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1530 | while (!worklet->output.configUpdate.empty()) { |
| 1531 | std::unique_ptr<C2Param> param; |
| 1532 | worklet->output.configUpdate.back().swap(param); |
| 1533 | worklet->output.configUpdate.pop_back(); |
| 1534 | switch (param->coreIndex().coreIndex()) { |
| 1535 | case C2PortReorderBufferDepthTuning::CORE_INDEX: { |
| 1536 | C2PortReorderBufferDepthTuning::output reorderDepth; |
| 1537 | if (reorderDepth.updateFrom(*param)) { |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1538 | bool secure = mComponent->getName().find(".secure") != |
| 1539 | std::string::npos; |
| 1540 | mOutput.lock()->buffers->setReorderDepth( |
| 1541 | reorderDepth.value); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1542 | ALOGV("[%s] onWorkDone: updated reorder depth to %u", |
| 1543 | mName, reorderDepth.value); |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1544 | size_t numOutputSlots = mOutput.lock()->numSlots; |
Sungtak Lee | 7a7b742 | 2019-07-16 17:40:40 -0700 | [diff] [blame] | 1545 | size_t numInputSlots = mInput.lock()->numSlots; |
Wonsik Kim | f5e5c83 | 2019-02-21 11:36:05 -0800 | [diff] [blame] | 1546 | Mutexed<OutputSurface>::Locked output(mOutputSurface); |
Sungtak Lee | d7463d1 | 2019-09-04 16:01:00 -0700 | [diff] [blame] | 1547 | output->maxDequeueBuffers = numOutputSlots + |
Sungtak Lee | 7a7b742 | 2019-07-16 17:40:40 -0700 | [diff] [blame] | 1548 | reorderDepth.value + kRenderingDepth; |
Sungtak Lee | d7463d1 | 2019-09-04 16:01:00 -0700 | [diff] [blame] | 1549 | if (!secure) { |
| 1550 | output->maxDequeueBuffers += numInputSlots; |
| 1551 | } |
Wonsik Kim | f5e5c83 | 2019-02-21 11:36:05 -0800 | [diff] [blame] | 1552 | if (output->surface) { |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1553 | output->surface->setMaxDequeuedBufferCount( |
| 1554 | output->maxDequeueBuffers); |
Wonsik Kim | f5e5c83 | 2019-02-21 11:36:05 -0800 | [diff] [blame] | 1555 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1556 | } else { |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1557 | ALOGD("[%s] onWorkDone: failed to read reorder depth", |
| 1558 | mName); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1559 | } |
| 1560 | break; |
| 1561 | } |
| 1562 | case C2PortReorderKeySetting::CORE_INDEX: { |
| 1563 | C2PortReorderKeySetting::output reorderKey; |
| 1564 | if (reorderKey.updateFrom(*param)) { |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1565 | mOutput.lock()->buffers->setReorderKey(reorderKey.value); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1566 | ALOGV("[%s] onWorkDone: updated reorder key to %u", |
| 1567 | mName, reorderKey.value); |
| 1568 | } else { |
| 1569 | ALOGD("[%s] onWorkDone: failed to read reorder key", mName); |
| 1570 | } |
| 1571 | break; |
| 1572 | } |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1573 | case C2PortActualDelayTuning::CORE_INDEX: { |
| 1574 | if (param->isGlobal()) { |
| 1575 | C2ActualPipelineDelayTuning pipelineDelay; |
| 1576 | if (pipelineDelay.updateFrom(*param)) { |
| 1577 | ALOGV("[%s] onWorkDone: updating pipeline delay %u", |
| 1578 | mName, pipelineDelay.value); |
| 1579 | newPipelineDelay = pipelineDelay.value; |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1580 | (void)mPipelineWatcher.lock()->pipelineDelay( |
| 1581 | pipelineDelay.value); |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1582 | } |
| 1583 | } |
| 1584 | if (param->forInput()) { |
| 1585 | C2PortActualDelayTuning::input inputDelay; |
| 1586 | if (inputDelay.updateFrom(*param)) { |
| 1587 | ALOGV("[%s] onWorkDone: updating input delay %u", |
| 1588 | mName, inputDelay.value); |
| 1589 | newInputDelay = inputDelay.value; |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1590 | (void)mPipelineWatcher.lock()->inputDelay( |
| 1591 | inputDelay.value); |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1592 | } |
| 1593 | } |
| 1594 | if (param->forOutput()) { |
| 1595 | C2PortActualDelayTuning::output outputDelay; |
| 1596 | if (outputDelay.updateFrom(*param)) { |
| 1597 | ALOGV("[%s] onWorkDone: updating output delay %u", |
| 1598 | mName, outputDelay.value); |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1599 | bool secure = mComponent->getName().find(".secure") != |
| 1600 | std::string::npos; |
| 1601 | (void)mPipelineWatcher.lock()->outputDelay( |
| 1602 | outputDelay.value); |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1603 | |
| 1604 | bool outputBuffersChanged = false; |
Wonsik Kim | f0e7d22 | 2019-06-28 12:33:16 -0700 | [diff] [blame] | 1605 | size_t numOutputSlots = 0; |
Sungtak Lee | 7a7b742 | 2019-07-16 17:40:40 -0700 | [diff] [blame] | 1606 | size_t numInputSlots = mInput.lock()->numSlots; |
Wonsik Kim | f0e7d22 | 2019-06-28 12:33:16 -0700 | [diff] [blame] | 1607 | { |
| 1608 | Mutexed<Output>::Locked output(mOutput); |
Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1609 | if (!output->buffers) { |
| 1610 | return false; |
| 1611 | } |
Wonsik Kim | f0e7d22 | 2019-06-28 12:33:16 -0700 | [diff] [blame] | 1612 | output->outputDelay = outputDelay.value; |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1613 | numOutputSlots = outputDelay.value + |
| 1614 | kSmoothnessFactor; |
Wonsik Kim | f0e7d22 | 2019-06-28 12:33:16 -0700 | [diff] [blame] | 1615 | if (output->numSlots < numOutputSlots) { |
| 1616 | output->numSlots = numOutputSlots; |
| 1617 | if (output->buffers->isArrayMode()) { |
| 1618 | OutputBuffersArray *array = |
| 1619 | (OutputBuffersArray *)output->buffers.get(); |
| 1620 | ALOGV("[%s] onWorkDone: growing output buffer array to %zu", |
| 1621 | mName, numOutputSlots); |
| 1622 | array->grow(numOutputSlots); |
| 1623 | outputBuffersChanged = true; |
| 1624 | } |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1625 | } |
Wonsik Kim | f0e7d22 | 2019-06-28 12:33:16 -0700 | [diff] [blame] | 1626 | numOutputSlots = output->numSlots; |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1627 | } |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1628 | |
| 1629 | if (outputBuffersChanged) { |
| 1630 | mCCodecCallback->onOutputBuffersChanged(); |
| 1631 | } |
Wonsik Kim | f0e7d22 | 2019-06-28 12:33:16 -0700 | [diff] [blame] | 1632 | |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1633 | uint32_t depth = mOutput.lock()->buffers->getReorderDepth(); |
Wonsik Kim | f0e7d22 | 2019-06-28 12:33:16 -0700 | [diff] [blame] | 1634 | Mutexed<OutputSurface>::Locked output(mOutputSurface); |
Sungtak Lee | d7463d1 | 2019-09-04 16:01:00 -0700 | [diff] [blame] | 1635 | output->maxDequeueBuffers = numOutputSlots + depth + kRenderingDepth; |
| 1636 | if (!secure) { |
| 1637 | output->maxDequeueBuffers += numInputSlots; |
| 1638 | } |
Wonsik Kim | f0e7d22 | 2019-06-28 12:33:16 -0700 | [diff] [blame] | 1639 | if (output->surface) { |
| 1640 | output->surface->setMaxDequeuedBufferCount(output->maxDequeueBuffers); |
| 1641 | } |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1642 | } |
| 1643 | } |
| 1644 | break; |
| 1645 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1646 | default: |
| 1647 | ALOGV("[%s] onWorkDone: unrecognized config update (%08X)", |
| 1648 | mName, param->index()); |
| 1649 | break; |
| 1650 | } |
| 1651 | } |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1652 | if (newInputDelay || newPipelineDelay) { |
| 1653 | Mutexed<Input>::Locked input(mInput); |
| 1654 | size_t newNumSlots = |
| 1655 | newInputDelay.value_or(input->inputDelay) + |
| 1656 | newPipelineDelay.value_or(input->pipelineDelay) + |
| 1657 | kSmoothnessFactor; |
| 1658 | if (input->buffers->isArrayMode()) { |
| 1659 | if (input->numSlots >= newNumSlots) { |
| 1660 | input->numExtraSlots = 0; |
| 1661 | } else { |
| 1662 | input->numExtraSlots = newNumSlots - input->numSlots; |
| 1663 | } |
| 1664 | ALOGV("[%s] onWorkDone: updated number of extra slots to %zu (input array mode)", |
| 1665 | mName, input->numExtraSlots); |
| 1666 | } else { |
| 1667 | input->numSlots = newNumSlots; |
| 1668 | } |
| 1669 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1670 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1671 | int32_t flags = 0; |
| 1672 | if (worklet->output.flags & C2FrameData::FLAG_END_OF_STREAM) { |
| 1673 | flags |= MediaCodec::BUFFER_FLAG_EOS; |
| 1674 | ALOGV("[%s] onWorkDone: output EOS", mName); |
| 1675 | } |
| 1676 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1677 | // WORKAROUND: adjust output timestamp based on client input timestamp and codec |
| 1678 | // input timestamp. Codec output timestamp (in the timestamp field) shall correspond to |
| 1679 | // the codec input timestamp, but client output timestamp should (reported in timeUs) |
| 1680 | // shall correspond to the client input timesamp (in customOrdinal). By using the |
| 1681 | // delta between the two, this allows for some timestamp deviation - e.g. if one input |
| 1682 | // produces multiple output. |
| 1683 | c2_cntr64_t timestamp = |
| 1684 | worklet->output.ordinal.timestamp + work->input.ordinal.customOrdinal |
| 1685 | - work->input.ordinal.timestamp; |
Wonsik Kim | 95ba016 | 2019-03-19 15:51:54 -0700 | [diff] [blame] | 1686 | if (mInputSurface != nullptr) { |
| 1687 | // When using input surface we need to restore the original input timestamp. |
| 1688 | timestamp = work->input.ordinal.customOrdinal; |
| 1689 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1690 | ALOGV("[%s] onWorkDone: input %lld, codec %lld => output %lld => %lld", |
| 1691 | mName, |
| 1692 | work->input.ordinal.customOrdinal.peekll(), |
| 1693 | work->input.ordinal.timestamp.peekll(), |
| 1694 | worklet->output.ordinal.timestamp.peekll(), |
| 1695 | timestamp.peekll()); |
| 1696 | |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1697 | // csd cannot be re-ordered and will always arrive first. |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1698 | if (initData != nullptr) { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1699 | Mutexed<Output>::Locked output(mOutput); |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1700 | if (output->buffers && outputFormat) { |
| 1701 | output->buffers->updateSkipCutBuffer(outputFormat); |
| 1702 | output->buffers->setFormat(outputFormat); |
| 1703 | } |
| 1704 | if (!notifyClient) { |
| 1705 | return false; |
| 1706 | } |
| 1707 | size_t index; |
| 1708 | sp<MediaCodecBuffer> outBuffer; |
Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1709 | if (output->buffers && output->buffers->registerCsd(initData, &index, &outBuffer) == OK) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1710 | outBuffer->meta()->setInt64("timeUs", timestamp.peek()); |
| 1711 | outBuffer->meta()->setInt32("flags", MediaCodec::BUFFER_FLAG_CODECCONFIG); |
| 1712 | ALOGV("[%s] onWorkDone: csd index = %zu [%p]", mName, index, outBuffer.get()); |
| 1713 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1714 | output.unlock(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1715 | mCallback->onOutputBufferAvailable(index, outBuffer); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1716 | } else { |
| 1717 | ALOGD("[%s] onWorkDone: unable to register csd", mName); |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1718 | output.unlock(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1719 | mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1720 | return false; |
| 1721 | } |
| 1722 | } |
| 1723 | |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1724 | if (notifyClient && !buffer && !flags) { |
Wonsik Kim | 35bf573 | 2020-05-14 17:40:29 +0000 | [diff] [blame] | 1725 | ALOGV("[%s] onWorkDone: Not reporting output buffer (%lld)", |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1726 | mName, work->input.ordinal.frameIndex.peekull()); |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1727 | notifyClient = false; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | if (buffer) { |
| 1731 | for (const std::shared_ptr<const C2Info> &info : buffer->info()) { |
| 1732 | // TODO: properly translate these to metadata |
| 1733 | switch (info->coreIndex().coreIndex()) { |
| 1734 | case C2StreamPictureTypeMaskInfo::CORE_INDEX: |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 1735 | if (((C2StreamPictureTypeMaskInfo *)info.get())->value & C2Config::SYNC_FRAME) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1736 | flags |= MediaCodec::BUFFER_FLAG_SYNCFRAME; |
| 1737 | } |
| 1738 | break; |
| 1739 | default: |
| 1740 | break; |
| 1741 | } |
| 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | { |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1746 | Mutexed<Output>::Locked output(mOutput); |
Wonsik Kim | c23cc40 | 2020-05-28 14:53:40 -0700 | [diff] [blame] | 1747 | if (!output->buffers) { |
| 1748 | return false; |
| 1749 | } |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1750 | output->buffers->pushToStash( |
| 1751 | buffer, |
| 1752 | notifyClient, |
| 1753 | timestamp.peek(), |
| 1754 | flags, |
| 1755 | outputFormat, |
| 1756 | worklet->output.ordinal); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1757 | } |
| 1758 | sendOutputBuffers(); |
| 1759 | return true; |
| 1760 | } |
| 1761 | |
| 1762 | void CCodecBufferChannel::sendOutputBuffers() { |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1763 | OutputBuffers::BufferAction action; |
Wonsik Kim | a4e049d | 2020-04-28 19:42:23 +0000 | [diff] [blame] | 1764 | size_t index; |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1765 | sp<MediaCodecBuffer> outBuffer; |
| 1766 | std::shared_ptr<C2Buffer> c2Buffer; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1767 | |
| 1768 | while (true) { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1769 | Mutexed<Output>::Locked output(mOutput); |
Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1770 | if (!output->buffers) { |
| 1771 | return; |
| 1772 | } |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1773 | action = output->buffers->popFromStashAndRegister( |
| 1774 | &c2Buffer, &index, &outBuffer); |
| 1775 | switch (action) { |
| 1776 | case OutputBuffers::SKIP: |
| 1777 | return; |
| 1778 | case OutputBuffers::DISCARD: |
| 1779 | break; |
| 1780 | case OutputBuffers::NOTIFY_CLIENT: |
Wonsik Kim | a4e049d | 2020-04-28 19:42:23 +0000 | [diff] [blame] | 1781 | output.unlock(); |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1782 | mCallback->onOutputBufferAvailable(index, outBuffer); |
| 1783 | break; |
| 1784 | case OutputBuffers::REALLOCATE: |
| 1785 | if (!output->buffers->isArrayMode()) { |
| 1786 | output->buffers = |
| 1787 | output->buffers->toArrayMode(output->numSlots); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1788 | } |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1789 | static_cast<OutputBuffersArray*>(output->buffers.get())-> |
| 1790 | realloc(c2Buffer); |
| 1791 | output.unlock(); |
| 1792 | mCCodecCallback->onOutputBuffersChanged(); |
Wonsik Kim | 4ada73d | 2020-05-26 14:58:07 -0700 | [diff] [blame] | 1793 | break; |
Pawin Vongmasa | 9b90698 | 2020-04-11 05:07:15 -0700 | [diff] [blame] | 1794 | case OutputBuffers::RETRY: |
| 1795 | ALOGV("[%s] sendOutputBuffers: unable to register output buffer", |
| 1796 | mName); |
| 1797 | return; |
| 1798 | default: |
| 1799 | LOG_ALWAYS_FATAL("[%s] sendOutputBuffers: " |
| 1800 | "corrupted BufferAction value (%d) " |
| 1801 | "returned from popFromStashAndRegister.", |
| 1802 | mName, int(action)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1803 | return; |
| 1804 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | status_t CCodecBufferChannel::setSurface(const sp<Surface> &newSurface) { |
| 1809 | static std::atomic_uint32_t surfaceGeneration{0}; |
| 1810 | uint32_t generation = (getpid() << 10) | |
| 1811 | ((surfaceGeneration.fetch_add(1, std::memory_order_relaxed) + 1) |
| 1812 | & ((1 << 10) - 1)); |
| 1813 | |
| 1814 | sp<IGraphicBufferProducer> producer; |
| 1815 | if (newSurface) { |
| 1816 | newSurface->setScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); |
Sungtak Lee | ab6f2f3 | 2019-02-15 14:43:51 -0800 | [diff] [blame] | 1817 | newSurface->setDequeueTimeout(kDequeueTimeoutNs); |
Sungtak Lee | 0851581 | 2019-06-05 11:16:32 -0700 | [diff] [blame] | 1818 | newSurface->setMaxDequeuedBufferCount(mOutputSurface.lock()->maxDequeueBuffers); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1819 | producer = newSurface->getIGraphicBufferProducer(); |
| 1820 | producer->setGenerationNumber(generation); |
| 1821 | } else { |
| 1822 | ALOGE("[%s] setting output surface to null", mName); |
| 1823 | return INVALID_OPERATION; |
| 1824 | } |
| 1825 | |
| 1826 | std::shared_ptr<Codec2Client::Configurable> outputPoolIntf; |
| 1827 | C2BlockPool::local_id_t outputPoolId; |
| 1828 | { |
| 1829 | Mutexed<BlockPools>::Locked pools(mBlockPools); |
| 1830 | outputPoolId = pools->outputPoolId; |
| 1831 | outputPoolIntf = pools->outputPoolIntf; |
| 1832 | } |
| 1833 | |
| 1834 | if (outputPoolIntf) { |
| 1835 | if (mComponent->setOutputSurface( |
| 1836 | outputPoolId, |
| 1837 | producer, |
| 1838 | generation) != C2_OK) { |
| 1839 | ALOGI("[%s] setSurface: component setOutputSurface failed", mName); |
| 1840 | return INVALID_OPERATION; |
| 1841 | } |
| 1842 | } |
| 1843 | |
| 1844 | { |
| 1845 | Mutexed<OutputSurface>::Locked output(mOutputSurface); |
| 1846 | output->surface = newSurface; |
| 1847 | output->generation = generation; |
| 1848 | } |
| 1849 | |
| 1850 | return OK; |
| 1851 | } |
| 1852 | |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1853 | PipelineWatcher::Clock::duration CCodecBufferChannel::elapsed() { |
Wonsik Kim | 4fa4f2b | 2019-02-13 11:02:58 -0800 | [diff] [blame] | 1854 | // When client pushed EOS, we want all the work to be done quickly. |
| 1855 | // Otherwise, component may have stalled work due to input starvation up to |
| 1856 | // the sum of the delay in the pipeline. |
Wonsik Kim | f0e7d22 | 2019-06-28 12:33:16 -0700 | [diff] [blame] | 1857 | size_t n = 0; |
| 1858 | if (!mInputMetEos) { |
| 1859 | size_t outputDelay = mOutput.lock()->outputDelay; |
| 1860 | Mutexed<Input>::Locked input(mInput); |
| 1861 | n = input->inputDelay + input->pipelineDelay + outputDelay; |
| 1862 | } |
Wonsik Kim | 4fa4f2b | 2019-02-13 11:02:58 -0800 | [diff] [blame] | 1863 | return mPipelineWatcher.lock()->elapsed(PipelineWatcher::Clock::now(), n); |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1864 | } |
| 1865 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1866 | void CCodecBufferChannel::setMetaMode(MetaMode mode) { |
| 1867 | mMetaMode = mode; |
| 1868 | } |
| 1869 | |
Wonsik Kim | 596187e | 2019-10-25 12:44:10 -0700 | [diff] [blame] | 1870 | void CCodecBufferChannel::setCrypto(const sp<ICrypto> &crypto) { |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 1871 | if (mCrypto != nullptr) { |
| 1872 | for (std::pair<wp<HidlMemory>, int32_t> entry : mHeapSeqNumMap) { |
| 1873 | mCrypto->unsetHeap(entry.second); |
| 1874 | } |
| 1875 | mHeapSeqNumMap.clear(); |
| 1876 | if (mHeapSeqNum >= 0) { |
| 1877 | mCrypto->unsetHeap(mHeapSeqNum); |
| 1878 | mHeapSeqNum = -1; |
| 1879 | } |
| 1880 | } |
Wonsik Kim | 596187e | 2019-10-25 12:44:10 -0700 | [diff] [blame] | 1881 | mCrypto = crypto; |
| 1882 | } |
| 1883 | |
| 1884 | void CCodecBufferChannel::setDescrambler(const sp<IDescrambler> &descrambler) { |
| 1885 | mDescrambler = descrambler; |
| 1886 | } |
| 1887 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1888 | status_t toStatusT(c2_status_t c2s, c2_operation_t c2op) { |
| 1889 | // C2_OK is always translated to OK. |
| 1890 | if (c2s == C2_OK) { |
| 1891 | return OK; |
| 1892 | } |
| 1893 | |
| 1894 | // Operation-dependent translation |
| 1895 | // TODO: Add as necessary |
| 1896 | switch (c2op) { |
| 1897 | case C2_OPERATION_Component_start: |
| 1898 | switch (c2s) { |
| 1899 | case C2_NO_MEMORY: |
| 1900 | return NO_MEMORY; |
| 1901 | default: |
| 1902 | return UNKNOWN_ERROR; |
| 1903 | } |
| 1904 | default: |
| 1905 | break; |
| 1906 | } |
| 1907 | |
| 1908 | // Backup operation-agnostic translation |
| 1909 | switch (c2s) { |
| 1910 | case C2_BAD_INDEX: |
| 1911 | return BAD_INDEX; |
| 1912 | case C2_BAD_VALUE: |
| 1913 | return BAD_VALUE; |
| 1914 | case C2_BLOCKING: |
| 1915 | return WOULD_BLOCK; |
| 1916 | case C2_DUPLICATE: |
| 1917 | return ALREADY_EXISTS; |
| 1918 | case C2_NO_INIT: |
| 1919 | return NO_INIT; |
| 1920 | case C2_NO_MEMORY: |
| 1921 | return NO_MEMORY; |
| 1922 | case C2_NOT_FOUND: |
| 1923 | return NAME_NOT_FOUND; |
| 1924 | case C2_TIMED_OUT: |
| 1925 | return TIMED_OUT; |
| 1926 | case C2_BAD_STATE: |
| 1927 | case C2_CANCELED: |
| 1928 | case C2_CANNOT_DO: |
| 1929 | case C2_CORRUPTED: |
| 1930 | case C2_OMITTED: |
| 1931 | case C2_REFUSED: |
| 1932 | return UNKNOWN_ERROR; |
| 1933 | default: |
| 1934 | return -static_cast<status_t>(c2s); |
| 1935 | } |
| 1936 | } |
| 1937 | |
| 1938 | } // namespace android |