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