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