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), |
Wonsik Kim | 4fa4f2b | 2019-02-13 11:02:58 -0800 | [diff] [blame] | 223 | mDelay(0), |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 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) { |
Wonsik Kim | f5e5c83 | 2019-02-21 11:36:05 -0800 | [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 | 4fa4f2b | 2019-02-13 11:02:58 -0800 | [diff] [blame] | 817 | mDelay = inputDelayValue + pipelineDelayValue + outputDelayValue; |
Wonsik Kim | 078b58e | 2019-01-09 15:08:06 -0800 | [diff] [blame] | 818 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 819 | // TODO: get this from input format |
| 820 | bool secure = mComponent->getName().find(".secure") != std::string::npos; |
| 821 | |
| 822 | std::shared_ptr<C2AllocatorStore> allocatorStore = GetCodec2PlatformAllocatorStore(); |
| 823 | int poolMask = property_get_int32( |
| 824 | "debug.stagefright.c2-poolmask", |
| 825 | 1 << C2PlatformAllocatorStore::ION | |
| 826 | 1 << C2PlatformAllocatorStore::BUFFERQUEUE); |
| 827 | |
| 828 | if (inputFormat != nullptr) { |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 829 | bool graphic = (iStreamFormat.value == C2BufferData::GRAPHIC); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 830 | std::shared_ptr<C2BlockPool> pool; |
| 831 | { |
| 832 | Mutexed<BlockPools>::Locked pools(mBlockPools); |
| 833 | |
| 834 | // set default allocator ID. |
| 835 | pools->inputAllocatorId = (graphic) ? C2PlatformAllocatorStore::GRALLOC |
| 836 | : C2PlatformAllocatorStore::ION; |
| 837 | |
| 838 | // query C2PortAllocatorsTuning::input from component. If an allocator ID is obtained |
| 839 | // from component, create the input block pool with given ID. Otherwise, use default IDs. |
| 840 | std::vector<std::unique_ptr<C2Param>> params; |
| 841 | err = mComponent->query({ }, |
| 842 | { C2PortAllocatorsTuning::input::PARAM_TYPE }, |
| 843 | C2_DONT_BLOCK, |
| 844 | ¶ms); |
| 845 | if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) { |
| 846 | ALOGD("[%s] Query input allocators returned %zu params => %s (%u)", |
| 847 | mName, params.size(), asString(err), err); |
| 848 | } else if (err == C2_OK && params.size() == 1) { |
| 849 | C2PortAllocatorsTuning::input *inputAllocators = |
| 850 | C2PortAllocatorsTuning::input::From(params[0].get()); |
| 851 | if (inputAllocators && inputAllocators->flexCount() > 0) { |
| 852 | std::shared_ptr<C2Allocator> allocator; |
| 853 | // verify allocator IDs and resolve default allocator |
| 854 | allocatorStore->fetchAllocator(inputAllocators->m.values[0], &allocator); |
| 855 | if (allocator) { |
| 856 | pools->inputAllocatorId = allocator->getId(); |
| 857 | } else { |
| 858 | ALOGD("[%s] component requested invalid input allocator ID %u", |
| 859 | mName, inputAllocators->m.values[0]); |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | // TODO: use C2Component wrapper to associate this pool with ourselves |
| 865 | if ((poolMask >> pools->inputAllocatorId) & 1) { |
| 866 | err = CreateCodec2BlockPool(pools->inputAllocatorId, nullptr, &pool); |
| 867 | ALOGD("[%s] Created input block pool with allocatorID %u => poolID %llu - %s (%d)", |
| 868 | mName, pools->inputAllocatorId, |
| 869 | (unsigned long long)(pool ? pool->getLocalId() : 111000111), |
| 870 | asString(err), err); |
| 871 | } else { |
| 872 | err = C2_NOT_FOUND; |
| 873 | } |
| 874 | if (err != C2_OK) { |
| 875 | C2BlockPool::local_id_t inputPoolId = |
| 876 | graphic ? C2BlockPool::BASIC_GRAPHIC : C2BlockPool::BASIC_LINEAR; |
| 877 | err = GetCodec2BlockPool(inputPoolId, nullptr, &pool); |
| 878 | ALOGD("[%s] Using basic input block pool with poolID %llu => got %llu - %s (%d)", |
| 879 | mName, (unsigned long long)inputPoolId, |
| 880 | (unsigned long long)(pool ? pool->getLocalId() : 111000111), |
| 881 | asString(err), err); |
| 882 | if (err != C2_OK) { |
| 883 | return NO_MEMORY; |
| 884 | } |
| 885 | } |
| 886 | pools->inputPool = pool; |
| 887 | } |
| 888 | |
Wonsik Kim | 5105126 | 2018-11-28 13:59:05 -0800 | [diff] [blame] | 889 | bool forceArrayMode = false; |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 890 | Mutexed<Input>::Locked input(mInput); |
| 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)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 899 | } else { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 900 | input->buffers.reset(new GraphicInputBuffers(numInputSlots, mName)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 901 | } |
| 902 | } else { |
| 903 | if (hasCryptoOrDescrambler()) { |
| 904 | int32_t capacity = kLinearBufferSize; |
| 905 | (void)inputFormat->findInt32(KEY_MAX_INPUT_SIZE, &capacity); |
| 906 | if ((size_t)capacity > kMaxLinearBufferSize) { |
| 907 | ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize); |
| 908 | capacity = kMaxLinearBufferSize; |
| 909 | } |
| 910 | if (mDealer == nullptr) { |
| 911 | mDealer = new MemoryDealer( |
| 912 | align(capacity, MemoryDealer::getAllocationAlignment()) |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 913 | * (numInputSlots + 1), |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 914 | "EncryptedLinearInputBuffers"); |
| 915 | mDecryptDestination = mDealer->allocate((size_t)capacity); |
| 916 | } |
| 917 | if (mCrypto != nullptr && mHeapSeqNum < 0) { |
| 918 | mHeapSeqNum = mCrypto->setHeap(mDealer->getMemoryHeap()); |
| 919 | } else { |
| 920 | mHeapSeqNum = -1; |
| 921 | } |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 922 | input->buffers.reset(new EncryptedLinearInputBuffers( |
Wonsik Kim | 078b58e | 2019-01-09 15:08:06 -0800 | [diff] [blame] | 923 | secure, mDealer, mCrypto, mHeapSeqNum, (size_t)capacity, |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 924 | numInputSlots, mName)); |
Wonsik Kim | 5105126 | 2018-11-28 13:59:05 -0800 | [diff] [blame] | 925 | forceArrayMode = true; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 926 | } else { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 927 | input->buffers.reset(new LinearInputBuffers(mName)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 928 | } |
| 929 | } |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 930 | input->buffers->setFormat(inputFormat); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 931 | |
| 932 | if (err == C2_OK) { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 933 | input->buffers->setPool(pool); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 934 | } else { |
| 935 | // TODO: error |
| 936 | } |
Wonsik Kim | 5105126 | 2018-11-28 13:59:05 -0800 | [diff] [blame] | 937 | |
| 938 | if (forceArrayMode) { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 939 | input->buffers = input->buffers->toArrayMode(numInputSlots); |
Wonsik Kim | 5105126 | 2018-11-28 13:59:05 -0800 | [diff] [blame] | 940 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | if (outputFormat != nullptr) { |
| 944 | sp<IGraphicBufferProducer> outputSurface; |
| 945 | uint32_t outputGeneration; |
| 946 | { |
| 947 | Mutexed<OutputSurface>::Locked output(mOutputSurface); |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 948 | output->maxDequeueBuffers = numOutputSlots + reorderDepth.value + kRenderingDepth; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 949 | outputSurface = output->surface ? |
| 950 | output->surface->getIGraphicBufferProducer() : nullptr; |
Wonsik Kim | f5e5c83 | 2019-02-21 11:36:05 -0800 | [diff] [blame] | 951 | if (outputSurface) { |
| 952 | output->surface->setMaxDequeuedBufferCount(output->maxDequeueBuffers); |
| 953 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 954 | outputGeneration = output->generation; |
| 955 | } |
| 956 | |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 957 | bool graphic = (oStreamFormat.value == C2BufferData::GRAPHIC); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 958 | C2BlockPool::local_id_t outputPoolId_; |
| 959 | |
| 960 | { |
| 961 | Mutexed<BlockPools>::Locked pools(mBlockPools); |
| 962 | |
| 963 | // set default allocator ID. |
| 964 | pools->outputAllocatorId = (graphic) ? C2PlatformAllocatorStore::GRALLOC |
| 965 | : C2PlatformAllocatorStore::ION; |
| 966 | |
| 967 | // query C2PortAllocatorsTuning::output from component, or use default allocator if |
| 968 | // unsuccessful. |
| 969 | std::vector<std::unique_ptr<C2Param>> params; |
| 970 | err = mComponent->query({ }, |
| 971 | { C2PortAllocatorsTuning::output::PARAM_TYPE }, |
| 972 | C2_DONT_BLOCK, |
| 973 | ¶ms); |
| 974 | if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) { |
| 975 | ALOGD("[%s] Query output allocators returned %zu params => %s (%u)", |
| 976 | mName, params.size(), asString(err), err); |
| 977 | } else if (err == C2_OK && params.size() == 1) { |
| 978 | C2PortAllocatorsTuning::output *outputAllocators = |
| 979 | C2PortAllocatorsTuning::output::From(params[0].get()); |
| 980 | if (outputAllocators && outputAllocators->flexCount() > 0) { |
| 981 | std::shared_ptr<C2Allocator> allocator; |
| 982 | // verify allocator IDs and resolve default allocator |
| 983 | allocatorStore->fetchAllocator(outputAllocators->m.values[0], &allocator); |
| 984 | if (allocator) { |
| 985 | pools->outputAllocatorId = allocator->getId(); |
| 986 | } else { |
| 987 | ALOGD("[%s] component requested invalid output allocator ID %u", |
| 988 | mName, outputAllocators->m.values[0]); |
| 989 | } |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | // use bufferqueue if outputting to a surface. |
| 994 | // query C2PortSurfaceAllocatorTuning::output from component, or use default allocator |
| 995 | // if unsuccessful. |
| 996 | if (outputSurface) { |
| 997 | params.clear(); |
| 998 | err = mComponent->query({ }, |
| 999 | { C2PortSurfaceAllocatorTuning::output::PARAM_TYPE }, |
| 1000 | C2_DONT_BLOCK, |
| 1001 | ¶ms); |
| 1002 | if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) { |
| 1003 | ALOGD("[%s] Query output surface allocator returned %zu params => %s (%u)", |
| 1004 | mName, params.size(), asString(err), err); |
| 1005 | } else if (err == C2_OK && params.size() == 1) { |
| 1006 | C2PortSurfaceAllocatorTuning::output *surfaceAllocator = |
| 1007 | C2PortSurfaceAllocatorTuning::output::From(params[0].get()); |
| 1008 | if (surfaceAllocator) { |
| 1009 | std::shared_ptr<C2Allocator> allocator; |
| 1010 | // verify allocator IDs and resolve default allocator |
| 1011 | allocatorStore->fetchAllocator(surfaceAllocator->value, &allocator); |
| 1012 | if (allocator) { |
| 1013 | pools->outputAllocatorId = allocator->getId(); |
| 1014 | } else { |
| 1015 | ALOGD("[%s] component requested invalid surface output allocator ID %u", |
| 1016 | mName, surfaceAllocator->value); |
| 1017 | err = C2_BAD_VALUE; |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | if (pools->outputAllocatorId == C2PlatformAllocatorStore::GRALLOC |
| 1022 | && err != C2_OK |
| 1023 | && ((poolMask >> C2PlatformAllocatorStore::BUFFERQUEUE) & 1)) { |
| 1024 | pools->outputAllocatorId = C2PlatformAllocatorStore::BUFFERQUEUE; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | if ((poolMask >> pools->outputAllocatorId) & 1) { |
| 1029 | err = mComponent->createBlockPool( |
| 1030 | pools->outputAllocatorId, &pools->outputPoolId, &pools->outputPoolIntf); |
| 1031 | ALOGI("[%s] Created output block pool with allocatorID %u => poolID %llu - %s", |
| 1032 | mName, pools->outputAllocatorId, |
| 1033 | (unsigned long long)pools->outputPoolId, |
| 1034 | asString(err)); |
| 1035 | } else { |
| 1036 | err = C2_NOT_FOUND; |
| 1037 | } |
| 1038 | if (err != C2_OK) { |
| 1039 | // use basic pool instead |
| 1040 | pools->outputPoolId = |
| 1041 | graphic ? C2BlockPool::BASIC_GRAPHIC : C2BlockPool::BASIC_LINEAR; |
| 1042 | } |
| 1043 | |
| 1044 | // Configure output block pool ID as parameter C2PortBlockPoolsTuning::output to |
| 1045 | // component. |
| 1046 | std::unique_ptr<C2PortBlockPoolsTuning::output> poolIdsTuning = |
| 1047 | C2PortBlockPoolsTuning::output::AllocUnique({ pools->outputPoolId }); |
| 1048 | |
| 1049 | std::vector<std::unique_ptr<C2SettingResult>> failures; |
| 1050 | err = mComponent->config({ poolIdsTuning.get() }, C2_MAY_BLOCK, &failures); |
| 1051 | ALOGD("[%s] Configured output block pool ids %llu => %s", |
| 1052 | mName, (unsigned long long)poolIdsTuning->m.values[0], asString(err)); |
| 1053 | outputPoolId_ = pools->outputPoolId; |
| 1054 | } |
| 1055 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1056 | Mutexed<Output>::Locked output(mOutput); |
| 1057 | output->numSlots = numOutputSlots; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1058 | if (graphic) { |
| 1059 | if (outputSurface) { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1060 | output->buffers.reset(new GraphicOutputBuffers(mName)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1061 | } else { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1062 | output->buffers.reset(new RawGraphicOutputBuffers(numOutputSlots, mName)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1063 | } |
| 1064 | } else { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1065 | output->buffers.reset(new LinearOutputBuffers(mName)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1066 | } |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1067 | output->buffers->setFormat(outputFormat->dup()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1068 | |
| 1069 | |
| 1070 | // Try to set output surface to created block pool if given. |
| 1071 | if (outputSurface) { |
| 1072 | mComponent->setOutputSurface( |
| 1073 | outputPoolId_, |
| 1074 | outputSurface, |
| 1075 | outputGeneration); |
| 1076 | } |
| 1077 | |
Wonsik Kim | 8ab25aa | 2019-06-24 16:37:37 -0700 | [diff] [blame^] | 1078 | if (oStreamFormat.value == C2BufferData::LINEAR) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1079 | // WORKAROUND: if we're using early CSD workaround we convert to |
| 1080 | // array mode, to appease apps assuming the output |
| 1081 | // buffers to be of the same size. |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1082 | output->buffers = output->buffers->toArrayMode(numOutputSlots); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1083 | |
| 1084 | int32_t channelCount; |
| 1085 | int32_t sampleRate; |
| 1086 | if (outputFormat->findInt32(KEY_CHANNEL_COUNT, &channelCount) |
| 1087 | && outputFormat->findInt32(KEY_SAMPLE_RATE, &sampleRate)) { |
| 1088 | int32_t delay = 0; |
| 1089 | int32_t padding = 0;; |
| 1090 | if (!outputFormat->findInt32("encoder-delay", &delay)) { |
| 1091 | delay = 0; |
| 1092 | } |
| 1093 | if (!outputFormat->findInt32("encoder-padding", &padding)) { |
| 1094 | padding = 0; |
| 1095 | } |
| 1096 | if (delay || padding) { |
| 1097 | // We need write access to the buffers, and we're already in |
| 1098 | // array mode. |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1099 | output->buffers->initSkipCutBuffer(delay, padding, sampleRate, channelCount); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | // Set up pipeline control. This has to be done after mInputBuffers and |
| 1106 | // mOutputBuffers are initialized to make sure that lingering callbacks |
| 1107 | // about buffers from the previous generation do not interfere with the |
| 1108 | // newly initialized pipeline capacity. |
| 1109 | |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1110 | { |
| 1111 | Mutexed<PipelineWatcher>::Locked watcher(mPipelineWatcher); |
Wonsik Kim | 4fa4f2b | 2019-02-13 11:02:58 -0800 | [diff] [blame] | 1112 | watcher->inputDelay(inputDelayValue) |
| 1113 | .pipelineDelay(pipelineDelayValue) |
| 1114 | .outputDelay(outputDelayValue) |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1115 | .smoothnessFactor(kSmoothnessFactor); |
| 1116 | watcher->flush(); |
| 1117 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1118 | |
| 1119 | mInputMetEos = false; |
| 1120 | mSync.start(); |
| 1121 | return OK; |
| 1122 | } |
| 1123 | |
| 1124 | status_t CCodecBufferChannel::requestInitialInputBuffers() { |
| 1125 | if (mInputSurface) { |
| 1126 | return OK; |
| 1127 | } |
| 1128 | |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 1129 | C2StreamBufferTypeSetting::output oStreamFormat(0u); |
Wonsik Kim | 8ab25aa | 2019-06-24 16:37:37 -0700 | [diff] [blame^] | 1130 | C2PrependHeaderModeSetting prepend(PREPEND_HEADER_TO_NONE); |
| 1131 | c2_status_t err = mComponent->query({ &oStreamFormat, &prepend }, {}, C2_DONT_BLOCK, nullptr); |
| 1132 | if (err != C2_OK && err != C2_BAD_INDEX) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1133 | return UNKNOWN_ERROR; |
| 1134 | } |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1135 | size_t numInputSlots = mInput.lock()->numSlots; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1136 | std::vector<sp<MediaCodecBuffer>> toBeQueued; |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1137 | for (size_t i = 0; i < numInputSlots; ++i) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1138 | size_t index; |
| 1139 | sp<MediaCodecBuffer> buffer; |
| 1140 | { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1141 | Mutexed<Input>::Locked input(mInput); |
| 1142 | if (!input->buffers->requestNewBuffer(&index, &buffer)) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1143 | if (i == 0) { |
| 1144 | ALOGW("[%s] start: cannot allocate memory at all", mName); |
| 1145 | return NO_MEMORY; |
| 1146 | } else { |
| 1147 | ALOGV("[%s] start: cannot allocate memory, only %zu buffers allocated", |
| 1148 | mName, i); |
| 1149 | } |
| 1150 | break; |
| 1151 | } |
| 1152 | } |
| 1153 | if (buffer) { |
| 1154 | Mutexed<std::list<sp<ABuffer>>>::Locked configs(mFlushedConfigs); |
| 1155 | ALOGV("[%s] input buffer %zu available", mName, index); |
| 1156 | bool post = true; |
| 1157 | if (!configs->empty()) { |
| 1158 | sp<ABuffer> config = configs->front(); |
Pawin Vongmasa | 472c738 | 2019-03-26 18:13:58 -0700 | [diff] [blame] | 1159 | configs->pop_front(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1160 | if (buffer->capacity() >= config->size()) { |
| 1161 | memcpy(buffer->base(), config->data(), config->size()); |
| 1162 | buffer->setRange(0, config->size()); |
| 1163 | buffer->meta()->clear(); |
| 1164 | buffer->meta()->setInt64("timeUs", 0); |
| 1165 | buffer->meta()->setInt32("csd", 1); |
| 1166 | post = false; |
| 1167 | } else { |
| 1168 | ALOGD("[%s] buffer capacity too small for the config (%zu < %zu)", |
| 1169 | mName, buffer->capacity(), config->size()); |
| 1170 | } |
| 1171 | } else if (oStreamFormat.value == C2BufferData::LINEAR && i == 0 |
Wonsik Kim | 8ab25aa | 2019-06-24 16:37:37 -0700 | [diff] [blame^] | 1172 | && (!prepend || prepend.value == PREPEND_HEADER_TO_NONE)) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1173 | // WORKAROUND: Some apps expect CSD available without queueing |
| 1174 | // any input. Queue an empty buffer to get the CSD. |
| 1175 | buffer->setRange(0, 0); |
| 1176 | buffer->meta()->clear(); |
| 1177 | buffer->meta()->setInt64("timeUs", 0); |
| 1178 | post = false; |
| 1179 | } |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1180 | if (post) { |
| 1181 | mCallback->onInputBufferAvailable(index, buffer); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1182 | } else { |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1183 | toBeQueued.emplace_back(buffer); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1184 | } |
| 1185 | } |
| 1186 | } |
| 1187 | for (const sp<MediaCodecBuffer> &buffer : toBeQueued) { |
| 1188 | if (queueInputBufferInternal(buffer) != OK) { |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1189 | ALOGV("[%s] Error while queueing initial buffers", mName); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1190 | } |
| 1191 | } |
| 1192 | return OK; |
| 1193 | } |
| 1194 | |
| 1195 | void CCodecBufferChannel::stop() { |
| 1196 | mSync.stop(); |
| 1197 | mFirstValidFrameIndex = mFrameIndex.load(std::memory_order_relaxed); |
| 1198 | if (mInputSurface != nullptr) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1199 | mInputSurface.reset(); |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | void CCodecBufferChannel::flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) { |
| 1204 | ALOGV("[%s] flush", mName); |
| 1205 | { |
| 1206 | Mutexed<std::list<sp<ABuffer>>>::Locked configs(mFlushedConfigs); |
| 1207 | for (const std::unique_ptr<C2Work> &work : flushedWork) { |
| 1208 | if (!(work->input.flags & C2FrameData::FLAG_CODEC_CONFIG)) { |
| 1209 | continue; |
| 1210 | } |
| 1211 | if (work->input.buffers.empty() |
| 1212 | || work->input.buffers.front()->data().linearBlocks().empty()) { |
| 1213 | ALOGD("[%s] no linear codec config data found", mName); |
| 1214 | continue; |
| 1215 | } |
| 1216 | C2ReadView view = |
| 1217 | work->input.buffers.front()->data().linearBlocks().front().map().get(); |
| 1218 | if (view.error() != C2_OK) { |
| 1219 | ALOGD("[%s] failed to map flushed codec config data: %d", mName, view.error()); |
| 1220 | continue; |
| 1221 | } |
| 1222 | configs->push_back(ABuffer::CreateAsCopy(view.data(), view.capacity())); |
| 1223 | ALOGV("[%s] stashed flushed codec config data (size=%u)", mName, view.capacity()); |
| 1224 | } |
| 1225 | } |
| 1226 | { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1227 | Mutexed<Input>::Locked input(mInput); |
| 1228 | input->buffers->flush(); |
| 1229 | input->extraBuffers.flush(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1230 | } |
| 1231 | { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1232 | Mutexed<Output>::Locked output(mOutput); |
| 1233 | output->buffers->flush(flushedWork); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1234 | } |
Wonsik Kim | 6897f22 | 2019-01-30 13:29:24 -0800 | [diff] [blame] | 1235 | mReorderStash.lock()->flush(); |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1236 | mPipelineWatcher.lock()->flush(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | void CCodecBufferChannel::onWorkDone( |
| 1240 | std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat, |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1241 | const C2StreamInitDataInfo::output *initData) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1242 | if (handleWork(std::move(work), outputFormat, initData)) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1243 | feedInputBufferIfAvailable(); |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | void CCodecBufferChannel::onInputBufferDone( |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1248 | uint64_t frameIndex, size_t arrayIndex) { |
Pawin Vongmasa | 8e2cfb5 | 2019-05-15 05:20:52 -0700 | [diff] [blame] | 1249 | if (mInputSurface) { |
| 1250 | return; |
| 1251 | } |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1252 | std::shared_ptr<C2Buffer> buffer = |
| 1253 | mPipelineWatcher.lock()->onInputBufferReleased(frameIndex, arrayIndex); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1254 | bool newInputSlotAvailable; |
| 1255 | { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1256 | Mutexed<Input>::Locked input(mInput); |
| 1257 | newInputSlotAvailable = input->buffers->expireComponentBuffer(buffer); |
| 1258 | if (!newInputSlotAvailable) { |
| 1259 | (void)input->extraBuffers.expireComponentBuffer(buffer); |
| 1260 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1261 | } |
| 1262 | if (newInputSlotAvailable) { |
| 1263 | feedInputBufferIfAvailable(); |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | bool CCodecBufferChannel::handleWork( |
| 1268 | std::unique_ptr<C2Work> work, |
| 1269 | const sp<AMessage> &outputFormat, |
| 1270 | const C2StreamInitDataInfo::output *initData) { |
| 1271 | if ((work->input.ordinal.frameIndex - mFirstValidFrameIndex.load()).peek() < 0) { |
| 1272 | // Discard frames from previous generation. |
| 1273 | ALOGD("[%s] Discard frames from previous generation.", mName); |
| 1274 | return false; |
| 1275 | } |
| 1276 | |
Wonsik Kim | 524b058 | 2019-03-12 11:28:57 -0700 | [diff] [blame] | 1277 | if (mInputSurface == nullptr && (work->worklets.size() != 1u |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1278 | || !work->worklets.front() |
Wonsik Kim | 524b058 | 2019-03-12 11:28:57 -0700 | [diff] [blame] | 1279 | || !(work->worklets.front()->output.flags & C2FrameData::FLAG_INCOMPLETE))) { |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1280 | mPipelineWatcher.lock()->onWorkDone(work->input.ordinal.frameIndex.peeku()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | if (work->result == C2_NOT_FOUND) { |
| 1284 | ALOGD("[%s] flushed work; ignored.", mName); |
| 1285 | return true; |
| 1286 | } |
| 1287 | |
| 1288 | if (work->result != C2_OK) { |
| 1289 | ALOGD("[%s] work failed to complete: %d", mName, work->result); |
| 1290 | mCCodecCallback->onError(work->result, ACTION_CODE_FATAL); |
| 1291 | return false; |
| 1292 | } |
| 1293 | |
| 1294 | // NOTE: MediaCodec usage supposedly have only one worklet |
| 1295 | if (work->worklets.size() != 1u) { |
| 1296 | ALOGI("[%s] onWorkDone: incorrect number of worklets: %zu", |
| 1297 | mName, work->worklets.size()); |
| 1298 | mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 1299 | return false; |
| 1300 | } |
| 1301 | |
| 1302 | const std::unique_ptr<C2Worklet> &worklet = work->worklets.front(); |
| 1303 | |
| 1304 | std::shared_ptr<C2Buffer> buffer; |
| 1305 | // NOTE: MediaCodec usage supposedly have only one output stream. |
| 1306 | if (worklet->output.buffers.size() > 1u) { |
| 1307 | ALOGI("[%s] onWorkDone: incorrect number of output buffers: %zu", |
| 1308 | mName, worklet->output.buffers.size()); |
| 1309 | mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 1310 | return false; |
| 1311 | } else if (worklet->output.buffers.size() == 1u) { |
| 1312 | buffer = worklet->output.buffers[0]; |
| 1313 | if (!buffer) { |
| 1314 | ALOGD("[%s] onWorkDone: nullptr found in buffers; ignored.", mName); |
| 1315 | } |
| 1316 | } |
| 1317 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1318 | std::optional<uint32_t> newInputDelay, newPipelineDelay; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1319 | while (!worklet->output.configUpdate.empty()) { |
| 1320 | std::unique_ptr<C2Param> param; |
| 1321 | worklet->output.configUpdate.back().swap(param); |
| 1322 | worklet->output.configUpdate.pop_back(); |
| 1323 | switch (param->coreIndex().coreIndex()) { |
| 1324 | case C2PortReorderBufferDepthTuning::CORE_INDEX: { |
| 1325 | C2PortReorderBufferDepthTuning::output reorderDepth; |
| 1326 | if (reorderDepth.updateFrom(*param)) { |
| 1327 | mReorderStash.lock()->setDepth(reorderDepth.value); |
| 1328 | ALOGV("[%s] onWorkDone: updated reorder depth to %u", |
| 1329 | mName, reorderDepth.value); |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1330 | size_t numOutputSlots = mOutput.lock()->numSlots; |
Wonsik Kim | f5e5c83 | 2019-02-21 11:36:05 -0800 | [diff] [blame] | 1331 | Mutexed<OutputSurface>::Locked output(mOutputSurface); |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1332 | output->maxDequeueBuffers = |
| 1333 | numOutputSlots + reorderDepth.value + kRenderingDepth; |
Wonsik Kim | f5e5c83 | 2019-02-21 11:36:05 -0800 | [diff] [blame] | 1334 | if (output->surface) { |
| 1335 | output->surface->setMaxDequeuedBufferCount(output->maxDequeueBuffers); |
| 1336 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1337 | } else { |
| 1338 | ALOGD("[%s] onWorkDone: failed to read reorder depth", mName); |
| 1339 | } |
| 1340 | break; |
| 1341 | } |
| 1342 | case C2PortReorderKeySetting::CORE_INDEX: { |
| 1343 | C2PortReorderKeySetting::output reorderKey; |
| 1344 | if (reorderKey.updateFrom(*param)) { |
| 1345 | mReorderStash.lock()->setKey(reorderKey.value); |
| 1346 | ALOGV("[%s] onWorkDone: updated reorder key to %u", |
| 1347 | mName, reorderKey.value); |
| 1348 | } else { |
| 1349 | ALOGD("[%s] onWorkDone: failed to read reorder key", mName); |
| 1350 | } |
| 1351 | break; |
| 1352 | } |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1353 | case C2PortActualDelayTuning::CORE_INDEX: { |
| 1354 | if (param->isGlobal()) { |
| 1355 | C2ActualPipelineDelayTuning pipelineDelay; |
| 1356 | if (pipelineDelay.updateFrom(*param)) { |
| 1357 | ALOGV("[%s] onWorkDone: updating pipeline delay %u", |
| 1358 | mName, pipelineDelay.value); |
| 1359 | newPipelineDelay = pipelineDelay.value; |
| 1360 | (void)mPipelineWatcher.lock()->pipelineDelay(pipelineDelay.value); |
| 1361 | } |
| 1362 | } |
| 1363 | if (param->forInput()) { |
| 1364 | C2PortActualDelayTuning::input inputDelay; |
| 1365 | if (inputDelay.updateFrom(*param)) { |
| 1366 | ALOGV("[%s] onWorkDone: updating input delay %u", |
| 1367 | mName, inputDelay.value); |
| 1368 | newInputDelay = inputDelay.value; |
| 1369 | (void)mPipelineWatcher.lock()->inputDelay(inputDelay.value); |
| 1370 | } |
| 1371 | } |
| 1372 | if (param->forOutput()) { |
| 1373 | C2PortActualDelayTuning::output outputDelay; |
| 1374 | if (outputDelay.updateFrom(*param)) { |
| 1375 | ALOGV("[%s] onWorkDone: updating output delay %u", |
| 1376 | mName, outputDelay.value); |
| 1377 | (void)mPipelineWatcher.lock()->outputDelay(outputDelay.value); |
| 1378 | |
| 1379 | bool outputBuffersChanged = false; |
| 1380 | Mutexed<Output>::Locked output(mOutput); |
| 1381 | output->outputDelay = outputDelay.value; |
| 1382 | size_t numOutputSlots = outputDelay.value + kSmoothnessFactor; |
| 1383 | if (output->numSlots < numOutputSlots) { |
| 1384 | output->numSlots = numOutputSlots; |
| 1385 | if (output->buffers->isArrayMode()) { |
| 1386 | OutputBuffersArray *array = |
| 1387 | (OutputBuffersArray *)output->buffers.get(); |
| 1388 | ALOGV("[%s] onWorkDone: growing output buffer array to %zu", |
| 1389 | mName, numOutputSlots); |
| 1390 | array->grow(numOutputSlots); |
| 1391 | outputBuffersChanged = true; |
| 1392 | } |
| 1393 | } |
| 1394 | output.unlock(); |
| 1395 | |
| 1396 | if (outputBuffersChanged) { |
| 1397 | mCCodecCallback->onOutputBuffersChanged(); |
| 1398 | } |
| 1399 | } |
| 1400 | } |
| 1401 | break; |
| 1402 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1403 | default: |
| 1404 | ALOGV("[%s] onWorkDone: unrecognized config update (%08X)", |
| 1405 | mName, param->index()); |
| 1406 | break; |
| 1407 | } |
| 1408 | } |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1409 | if (newInputDelay || newPipelineDelay) { |
| 1410 | Mutexed<Input>::Locked input(mInput); |
| 1411 | size_t newNumSlots = |
| 1412 | newInputDelay.value_or(input->inputDelay) + |
| 1413 | newPipelineDelay.value_or(input->pipelineDelay) + |
| 1414 | kSmoothnessFactor; |
| 1415 | if (input->buffers->isArrayMode()) { |
| 1416 | if (input->numSlots >= newNumSlots) { |
| 1417 | input->numExtraSlots = 0; |
| 1418 | } else { |
| 1419 | input->numExtraSlots = newNumSlots - input->numSlots; |
| 1420 | } |
| 1421 | ALOGV("[%s] onWorkDone: updated number of extra slots to %zu (input array mode)", |
| 1422 | mName, input->numExtraSlots); |
| 1423 | } else { |
| 1424 | input->numSlots = newNumSlots; |
| 1425 | } |
| 1426 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1427 | |
| 1428 | if (outputFormat != nullptr) { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1429 | Mutexed<Output>::Locked output(mOutput); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1430 | ALOGD("[%s] onWorkDone: output format changed to %s", |
| 1431 | mName, outputFormat->debugString().c_str()); |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1432 | output->buffers->setFormat(outputFormat); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1433 | |
| 1434 | AString mediaType; |
| 1435 | if (outputFormat->findString(KEY_MIME, &mediaType) |
| 1436 | && mediaType == MIMETYPE_AUDIO_RAW) { |
| 1437 | int32_t channelCount; |
| 1438 | int32_t sampleRate; |
| 1439 | if (outputFormat->findInt32(KEY_CHANNEL_COUNT, &channelCount) |
| 1440 | && outputFormat->findInt32(KEY_SAMPLE_RATE, &sampleRate)) { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1441 | output->buffers->updateSkipCutBuffer(sampleRate, channelCount); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1442 | } |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | int32_t flags = 0; |
| 1447 | if (worklet->output.flags & C2FrameData::FLAG_END_OF_STREAM) { |
| 1448 | flags |= MediaCodec::BUFFER_FLAG_EOS; |
| 1449 | ALOGV("[%s] onWorkDone: output EOS", mName); |
| 1450 | } |
| 1451 | |
| 1452 | sp<MediaCodecBuffer> outBuffer; |
| 1453 | size_t index; |
| 1454 | |
| 1455 | // WORKAROUND: adjust output timestamp based on client input timestamp and codec |
| 1456 | // input timestamp. Codec output timestamp (in the timestamp field) shall correspond to |
| 1457 | // the codec input timestamp, but client output timestamp should (reported in timeUs) |
| 1458 | // shall correspond to the client input timesamp (in customOrdinal). By using the |
| 1459 | // delta between the two, this allows for some timestamp deviation - e.g. if one input |
| 1460 | // produces multiple output. |
| 1461 | c2_cntr64_t timestamp = |
| 1462 | worklet->output.ordinal.timestamp + work->input.ordinal.customOrdinal |
| 1463 | - work->input.ordinal.timestamp; |
Wonsik Kim | 95ba016 | 2019-03-19 15:51:54 -0700 | [diff] [blame] | 1464 | if (mInputSurface != nullptr) { |
| 1465 | // When using input surface we need to restore the original input timestamp. |
| 1466 | timestamp = work->input.ordinal.customOrdinal; |
| 1467 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1468 | ALOGV("[%s] onWorkDone: input %lld, codec %lld => output %lld => %lld", |
| 1469 | mName, |
| 1470 | work->input.ordinal.customOrdinal.peekll(), |
| 1471 | work->input.ordinal.timestamp.peekll(), |
| 1472 | worklet->output.ordinal.timestamp.peekll(), |
| 1473 | timestamp.peekll()); |
| 1474 | |
| 1475 | if (initData != nullptr) { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1476 | Mutexed<Output>::Locked output(mOutput); |
| 1477 | if (output->buffers->registerCsd(initData, &index, &outBuffer) == OK) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1478 | outBuffer->meta()->setInt64("timeUs", timestamp.peek()); |
| 1479 | outBuffer->meta()->setInt32("flags", MediaCodec::BUFFER_FLAG_CODECCONFIG); |
| 1480 | ALOGV("[%s] onWorkDone: csd index = %zu [%p]", mName, index, outBuffer.get()); |
| 1481 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1482 | output.unlock(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1483 | mCallback->onOutputBufferAvailable(index, outBuffer); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1484 | } else { |
| 1485 | ALOGD("[%s] onWorkDone: unable to register csd", mName); |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1486 | output.unlock(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1487 | mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1488 | return false; |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | if (!buffer && !flags) { |
| 1493 | ALOGV("[%s] onWorkDone: Not reporting output buffer (%lld)", |
| 1494 | mName, work->input.ordinal.frameIndex.peekull()); |
| 1495 | return true; |
| 1496 | } |
| 1497 | |
| 1498 | if (buffer) { |
| 1499 | for (const std::shared_ptr<const C2Info> &info : buffer->info()) { |
| 1500 | // TODO: properly translate these to metadata |
| 1501 | switch (info->coreIndex().coreIndex()) { |
| 1502 | case C2StreamPictureTypeMaskInfo::CORE_INDEX: |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 1503 | if (((C2StreamPictureTypeMaskInfo *)info.get())->value & C2Config::SYNC_FRAME) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1504 | flags |= MediaCodec::BUFFER_FLAG_SYNCFRAME; |
| 1505 | } |
| 1506 | break; |
| 1507 | default: |
| 1508 | break; |
| 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | { |
| 1514 | Mutexed<ReorderStash>::Locked reorder(mReorderStash); |
| 1515 | reorder->emplace(buffer, timestamp.peek(), flags, worklet->output.ordinal); |
| 1516 | if (flags & MediaCodec::BUFFER_FLAG_EOS) { |
| 1517 | // Flush reorder stash |
| 1518 | reorder->setDepth(0); |
| 1519 | } |
| 1520 | } |
| 1521 | sendOutputBuffers(); |
| 1522 | return true; |
| 1523 | } |
| 1524 | |
| 1525 | void CCodecBufferChannel::sendOutputBuffers() { |
| 1526 | ReorderStash::Entry entry; |
| 1527 | sp<MediaCodecBuffer> outBuffer; |
| 1528 | size_t index; |
| 1529 | |
| 1530 | while (true) { |
Wonsik Kim | 38ad341 | 2019-02-01 15:13:23 -0800 | [diff] [blame] | 1531 | Mutexed<ReorderStash>::Locked reorder(mReorderStash); |
| 1532 | if (!reorder->hasPending()) { |
| 1533 | break; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1534 | } |
Wonsik Kim | 38ad341 | 2019-02-01 15:13:23 -0800 | [diff] [blame] | 1535 | if (!reorder->pop(&entry)) { |
| 1536 | break; |
| 1537 | } |
| 1538 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1539 | Mutexed<Output>::Locked output(mOutput); |
| 1540 | status_t err = output->buffers->registerBuffer(entry.buffer, &index, &outBuffer); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1541 | if (err != OK) { |
Wonsik Kim | 38ad341 | 2019-02-01 15:13:23 -0800 | [diff] [blame] | 1542 | bool outputBuffersChanged = false; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1543 | if (err != WOULD_BLOCK) { |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1544 | if (!output->buffers->isArrayMode()) { |
| 1545 | output->buffers = output->buffers->toArrayMode(output->numSlots); |
Wonsik Kim | 186fdbf | 2019-01-29 13:30:01 -0800 | [diff] [blame] | 1546 | } |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1547 | OutputBuffersArray *array = (OutputBuffersArray *)output->buffers.get(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1548 | array->realloc(entry.buffer); |
Wonsik Kim | 38ad341 | 2019-02-01 15:13:23 -0800 | [diff] [blame] | 1549 | outputBuffersChanged = true; |
| 1550 | } |
| 1551 | ALOGV("[%s] sendOutputBuffers: unable to register output buffer", mName); |
| 1552 | reorder->defer(entry); |
| 1553 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1554 | output.unlock(); |
Wonsik Kim | 38ad341 | 2019-02-01 15:13:23 -0800 | [diff] [blame] | 1555 | reorder.unlock(); |
| 1556 | |
| 1557 | if (outputBuffersChanged) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1558 | mCCodecCallback->onOutputBuffersChanged(); |
| 1559 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1560 | return; |
| 1561 | } |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1562 | output.unlock(); |
Wonsik Kim | 38ad341 | 2019-02-01 15:13:23 -0800 | [diff] [blame] | 1563 | reorder.unlock(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1564 | |
| 1565 | outBuffer->meta()->setInt64("timeUs", entry.timestamp); |
| 1566 | outBuffer->meta()->setInt32("flags", entry.flags); |
Wonsik Kim | 6642743 | 2019-03-21 15:06:22 -0700 | [diff] [blame] | 1567 | ALOGV("[%s] sendOutputBuffers: out buffer index = %zu [%p] => %p + %zu (%lld)", |
| 1568 | mName, index, outBuffer.get(), outBuffer->data(), outBuffer->size(), |
| 1569 | (long long)entry.timestamp); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1570 | mCallback->onOutputBufferAvailable(index, outBuffer); |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | status_t CCodecBufferChannel::setSurface(const sp<Surface> &newSurface) { |
| 1575 | static std::atomic_uint32_t surfaceGeneration{0}; |
| 1576 | uint32_t generation = (getpid() << 10) | |
| 1577 | ((surfaceGeneration.fetch_add(1, std::memory_order_relaxed) + 1) |
| 1578 | & ((1 << 10) - 1)); |
| 1579 | |
| 1580 | sp<IGraphicBufferProducer> producer; |
| 1581 | if (newSurface) { |
| 1582 | newSurface->setScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); |
Sungtak Lee | ab6f2f3 | 2019-02-15 14:43:51 -0800 | [diff] [blame] | 1583 | newSurface->setDequeueTimeout(kDequeueTimeoutNs); |
Sungtak Lee | 0851581 | 2019-06-05 11:16:32 -0700 | [diff] [blame] | 1584 | newSurface->setMaxDequeuedBufferCount(mOutputSurface.lock()->maxDequeueBuffers); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1585 | producer = newSurface->getIGraphicBufferProducer(); |
| 1586 | producer->setGenerationNumber(generation); |
| 1587 | } else { |
| 1588 | ALOGE("[%s] setting output surface to null", mName); |
| 1589 | return INVALID_OPERATION; |
| 1590 | } |
| 1591 | |
| 1592 | std::shared_ptr<Codec2Client::Configurable> outputPoolIntf; |
| 1593 | C2BlockPool::local_id_t outputPoolId; |
| 1594 | { |
| 1595 | Mutexed<BlockPools>::Locked pools(mBlockPools); |
| 1596 | outputPoolId = pools->outputPoolId; |
| 1597 | outputPoolIntf = pools->outputPoolIntf; |
| 1598 | } |
| 1599 | |
| 1600 | if (outputPoolIntf) { |
| 1601 | if (mComponent->setOutputSurface( |
| 1602 | outputPoolId, |
| 1603 | producer, |
| 1604 | generation) != C2_OK) { |
| 1605 | ALOGI("[%s] setSurface: component setOutputSurface failed", mName); |
| 1606 | return INVALID_OPERATION; |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | { |
| 1611 | Mutexed<OutputSurface>::Locked output(mOutputSurface); |
| 1612 | output->surface = newSurface; |
| 1613 | output->generation = generation; |
| 1614 | } |
| 1615 | |
| 1616 | return OK; |
| 1617 | } |
| 1618 | |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1619 | PipelineWatcher::Clock::duration CCodecBufferChannel::elapsed() { |
Wonsik Kim | 4fa4f2b | 2019-02-13 11:02:58 -0800 | [diff] [blame] | 1620 | // When client pushed EOS, we want all the work to be done quickly. |
| 1621 | // Otherwise, component may have stalled work due to input starvation up to |
| 1622 | // the sum of the delay in the pipeline. |
| 1623 | size_t n = mInputMetEos ? 0 : mDelay; |
| 1624 | return mPipelineWatcher.lock()->elapsed(PipelineWatcher::Clock::now(), n); |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1625 | } |
| 1626 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1627 | void CCodecBufferChannel::setMetaMode(MetaMode mode) { |
| 1628 | mMetaMode = mode; |
| 1629 | } |
| 1630 | |
| 1631 | status_t toStatusT(c2_status_t c2s, c2_operation_t c2op) { |
| 1632 | // C2_OK is always translated to OK. |
| 1633 | if (c2s == C2_OK) { |
| 1634 | return OK; |
| 1635 | } |
| 1636 | |
| 1637 | // Operation-dependent translation |
| 1638 | // TODO: Add as necessary |
| 1639 | switch (c2op) { |
| 1640 | case C2_OPERATION_Component_start: |
| 1641 | switch (c2s) { |
| 1642 | case C2_NO_MEMORY: |
| 1643 | return NO_MEMORY; |
| 1644 | default: |
| 1645 | return UNKNOWN_ERROR; |
| 1646 | } |
| 1647 | default: |
| 1648 | break; |
| 1649 | } |
| 1650 | |
| 1651 | // Backup operation-agnostic translation |
| 1652 | switch (c2s) { |
| 1653 | case C2_BAD_INDEX: |
| 1654 | return BAD_INDEX; |
| 1655 | case C2_BAD_VALUE: |
| 1656 | return BAD_VALUE; |
| 1657 | case C2_BLOCKING: |
| 1658 | return WOULD_BLOCK; |
| 1659 | case C2_DUPLICATE: |
| 1660 | return ALREADY_EXISTS; |
| 1661 | case C2_NO_INIT: |
| 1662 | return NO_INIT; |
| 1663 | case C2_NO_MEMORY: |
| 1664 | return NO_MEMORY; |
| 1665 | case C2_NOT_FOUND: |
| 1666 | return NAME_NOT_FOUND; |
| 1667 | case C2_TIMED_OUT: |
| 1668 | return TIMED_OUT; |
| 1669 | case C2_BAD_STATE: |
| 1670 | case C2_CANCELED: |
| 1671 | case C2_CANNOT_DO: |
| 1672 | case C2_CORRUPTED: |
| 1673 | case C2_OMITTED: |
| 1674 | case C2_REFUSED: |
| 1675 | return UNKNOWN_ERROR; |
| 1676 | default: |
| 1677 | return -static_cast<status_t>(c2s); |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | } // namespace android |