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