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