Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 "CCodec" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <sstream> |
| 22 | #include <thread> |
| 23 | |
| 24 | #include <C2Config.h> |
| 25 | #include <C2Debug.h> |
| 26 | #include <C2ParamInternal.h> |
| 27 | #include <C2PlatformSupport.h> |
| 28 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 29 | #include <android/IOMXBufferSource.h> |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 30 | #include <android/hardware/media/c2/1.0/IInputSurface.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 31 | #include <android/hardware/media/omx/1.0/IGraphicBufferSource.h> |
| 32 | #include <android/hardware/media/omx/1.0/IOmx.h> |
| 33 | #include <android-base/stringprintf.h> |
| 34 | #include <cutils/properties.h> |
| 35 | #include <gui/IGraphicBufferProducer.h> |
| 36 | #include <gui/Surface.h> |
| 37 | #include <gui/bufferqueue/1.0/H2BGraphicBufferProducer.h> |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 38 | #include <media/omx/1.0/WOmxNode.h> |
| 39 | #include <media/openmax/OMX_Core.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 40 | #include <media/openmax/OMX_IndexExt.h> |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 41 | #include <media/stagefright/omx/1.0/WGraphicBufferSource.h> |
| 42 | #include <media/stagefright/omx/OmxGraphicBufferSource.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 43 | #include <media/stagefright/BufferProducerWrapper.h> |
| 44 | #include <media/stagefright/MediaCodecConstants.h> |
| 45 | #include <media/stagefright/PersistentSurface.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 46 | |
| 47 | #include "C2OMXNode.h" |
| 48 | #include "CCodec.h" |
| 49 | #include "CCodecBufferChannel.h" |
| 50 | #include "InputSurfaceWrapper.h" |
| 51 | |
| 52 | extern "C" android::PersistentSurface *CreateInputSurface(); |
| 53 | |
| 54 | namespace android { |
| 55 | |
| 56 | using namespace std::chrono_literals; |
| 57 | using ::android::hardware::graphics::bufferqueue::V1_0::utils::H2BGraphicBufferProducer; |
| 58 | using android::base::StringPrintf; |
Pawin Vongmasa | d0f0e14 | 2018-11-15 03:36:28 -0800 | [diff] [blame] | 59 | using ::android::hardware::media::c2::V1_0::IInputSurface; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 60 | |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 61 | typedef hardware::media::omx::V1_0::IGraphicBufferSource HGraphicBufferSource; |
| 62 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 63 | namespace { |
| 64 | |
| 65 | class CCodecWatchdog : public AHandler { |
| 66 | private: |
| 67 | enum { |
| 68 | kWhatWatch, |
| 69 | }; |
| 70 | constexpr static int64_t kWatchIntervalUs = 3300000; // 3.3 secs |
| 71 | |
| 72 | public: |
| 73 | static sp<CCodecWatchdog> getInstance() { |
| 74 | static sp<CCodecWatchdog> instance(new CCodecWatchdog); |
| 75 | static std::once_flag flag; |
| 76 | // Call Init() only once. |
| 77 | std::call_once(flag, Init, instance); |
| 78 | return instance; |
| 79 | } |
| 80 | |
| 81 | ~CCodecWatchdog() = default; |
| 82 | |
| 83 | void watch(sp<CCodec> codec) { |
| 84 | bool shouldPost = false; |
| 85 | { |
| 86 | Mutexed<std::set<wp<CCodec>>>::Locked codecs(mCodecsToWatch); |
| 87 | // If a watch message is in flight, piggy-back this instance as well. |
| 88 | // Otherwise, post a new watch message. |
| 89 | shouldPost = codecs->empty(); |
| 90 | codecs->emplace(codec); |
| 91 | } |
| 92 | if (shouldPost) { |
| 93 | ALOGV("posting watch message"); |
| 94 | (new AMessage(kWhatWatch, this))->post(kWatchIntervalUs); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | protected: |
| 99 | void onMessageReceived(const sp<AMessage> &msg) { |
| 100 | switch (msg->what()) { |
| 101 | case kWhatWatch: { |
| 102 | Mutexed<std::set<wp<CCodec>>>::Locked codecs(mCodecsToWatch); |
| 103 | ALOGV("watch for %zu codecs", codecs->size()); |
| 104 | for (auto it = codecs->begin(); it != codecs->end(); ++it) { |
| 105 | sp<CCodec> codec = it->promote(); |
| 106 | if (codec == nullptr) { |
| 107 | continue; |
| 108 | } |
| 109 | codec->initiateReleaseIfStuck(); |
| 110 | } |
| 111 | codecs->clear(); |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | default: { |
| 116 | TRESPASS("CCodecWatchdog: unrecognized message"); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | private: |
| 122 | CCodecWatchdog() : mLooper(new ALooper) {} |
| 123 | |
| 124 | static void Init(const sp<CCodecWatchdog> &thiz) { |
| 125 | ALOGV("Init"); |
| 126 | thiz->mLooper->setName("CCodecWatchdog"); |
| 127 | thiz->mLooper->registerHandler(thiz); |
| 128 | thiz->mLooper->start(); |
| 129 | } |
| 130 | |
| 131 | sp<ALooper> mLooper; |
| 132 | |
| 133 | Mutexed<std::set<wp<CCodec>>> mCodecsToWatch; |
| 134 | }; |
| 135 | |
| 136 | class C2InputSurfaceWrapper : public InputSurfaceWrapper { |
| 137 | public: |
| 138 | explicit C2InputSurfaceWrapper( |
| 139 | const std::shared_ptr<Codec2Client::InputSurface> &surface) : |
| 140 | mSurface(surface) { |
| 141 | } |
| 142 | |
| 143 | ~C2InputSurfaceWrapper() override = default; |
| 144 | |
| 145 | status_t connect(const std::shared_ptr<Codec2Client::Component> &comp) override { |
| 146 | if (mConnection != nullptr) { |
| 147 | return ALREADY_EXISTS; |
| 148 | } |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 149 | return toStatusT(comp->connectToInputSurface(mSurface, &mConnection)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void disconnect() override { |
| 153 | if (mConnection != nullptr) { |
| 154 | mConnection->disconnect(); |
| 155 | mConnection = nullptr; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | status_t start() override { |
| 160 | // InputSurface does not distinguish started state |
| 161 | return OK; |
| 162 | } |
| 163 | |
| 164 | status_t signalEndOfInputStream() override { |
| 165 | C2InputSurfaceEosTuning eos(true); |
| 166 | std::vector<std::unique_ptr<C2SettingResult>> failures; |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 167 | c2_status_t err = mSurface->config({&eos}, C2_MAY_BLOCK, &failures); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 168 | if (err != C2_OK) { |
| 169 | return UNKNOWN_ERROR; |
| 170 | } |
| 171 | return OK; |
| 172 | } |
| 173 | |
| 174 | status_t configure(Config &config __unused) { |
| 175 | // TODO |
| 176 | return OK; |
| 177 | } |
| 178 | |
| 179 | private: |
| 180 | std::shared_ptr<Codec2Client::InputSurface> mSurface; |
| 181 | std::shared_ptr<Codec2Client::InputSurfaceConnection> mConnection; |
| 182 | }; |
| 183 | |
| 184 | class GraphicBufferSourceWrapper : public InputSurfaceWrapper { |
| 185 | public: |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 186 | typedef hardware::media::omx::V1_0::Status OmxStatus; |
| 187 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 188 | GraphicBufferSourceWrapper( |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 189 | const sp<HGraphicBufferSource> &source, |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 190 | uint32_t width, |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 191 | uint32_t height, |
| 192 | uint64_t usage) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 193 | : mSource(source), mWidth(width), mHeight(height) { |
| 194 | mDataSpace = HAL_DATASPACE_BT709; |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 195 | mConfig.mUsage = usage; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 196 | } |
| 197 | ~GraphicBufferSourceWrapper() override = default; |
| 198 | |
| 199 | status_t connect(const std::shared_ptr<Codec2Client::Component> &comp) override { |
| 200 | mNode = new C2OMXNode(comp); |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 201 | mOmxNode = new hardware::media::omx::V1_0::utils::TWOmxNode(mNode); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 202 | mNode->setFrameSize(mWidth, mHeight); |
| 203 | |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 204 | // Usage is queried during configure(), so setting it beforehand. |
| 205 | OMX_U32 usage = mConfig.mUsage & 0xFFFFFFFF; |
| 206 | (void)mNode->setParameter( |
| 207 | (OMX_INDEXTYPE)OMX_IndexParamConsumerUsageBits, |
| 208 | &usage, sizeof(usage)); |
| 209 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 210 | // NOTE: we do not use/pass through color aspects from GraphicBufferSource as we |
| 211 | // communicate that directly to the component. |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 212 | mSource->configure( |
| 213 | mOmxNode, static_cast<hardware::graphics::common::V1_0::Dataspace>(mDataSpace)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 214 | return OK; |
| 215 | } |
| 216 | |
| 217 | void disconnect() override { |
| 218 | if (mNode == nullptr) { |
| 219 | return; |
| 220 | } |
| 221 | sp<IOMXBufferSource> source = mNode->getSource(); |
| 222 | if (source == nullptr) { |
| 223 | ALOGD("GBSWrapper::disconnect: node is not configured with OMXBufferSource."); |
| 224 | return; |
| 225 | } |
| 226 | source->onOmxIdle(); |
| 227 | source->onOmxLoaded(); |
| 228 | mNode.clear(); |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 229 | mOmxNode.clear(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 232 | status_t GetStatus(hardware::Return<OmxStatus> &&status) { |
| 233 | if (status.isOk()) { |
| 234 | return static_cast<status_t>(status.withDefault(OmxStatus::UNKNOWN_ERROR)); |
| 235 | } else if (status.isDeadObject()) { |
| 236 | return DEAD_OBJECT; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 237 | } |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 238 | return UNKNOWN_ERROR; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | status_t start() override { |
| 242 | sp<IOMXBufferSource> source = mNode->getSource(); |
| 243 | if (source == nullptr) { |
| 244 | return NO_INIT; |
| 245 | } |
| 246 | constexpr size_t kNumSlots = 16; |
| 247 | for (size_t i = 0; i < kNumSlots; ++i) { |
| 248 | source->onInputBufferAdded(i); |
| 249 | } |
| 250 | |
| 251 | source->onOmxExecuting(); |
| 252 | return OK; |
| 253 | } |
| 254 | |
| 255 | status_t signalEndOfInputStream() override { |
| 256 | return GetStatus(mSource->signalEndOfInputStream()); |
| 257 | } |
| 258 | |
| 259 | status_t configure(Config &config) { |
| 260 | std::stringstream status; |
| 261 | status_t err = OK; |
| 262 | |
| 263 | // handle each configuration granually, in case we need to handle part of the configuration |
| 264 | // elsewhere |
| 265 | |
| 266 | // TRICKY: we do not unset frame delay repeating |
| 267 | if (config.mMinFps > 0 && config.mMinFps != mConfig.mMinFps) { |
| 268 | int64_t us = 1e6 / config.mMinFps + 0.5; |
| 269 | status_t res = GetStatus(mSource->setRepeatPreviousFrameDelayUs(us)); |
| 270 | status << " minFps=" << config.mMinFps << " => repeatDelayUs=" << us; |
| 271 | if (res != OK) { |
| 272 | status << " (=> " << asString(res) << ")"; |
| 273 | err = res; |
| 274 | } |
| 275 | mConfig.mMinFps = config.mMinFps; |
| 276 | } |
| 277 | |
| 278 | // pts gap |
| 279 | if (config.mMinAdjustedFps > 0 || config.mFixedAdjustedFps > 0) { |
| 280 | if (mNode != nullptr) { |
| 281 | OMX_PARAM_U32TYPE ptrGapParam = {}; |
| 282 | ptrGapParam.nSize = sizeof(OMX_PARAM_U32TYPE); |
Wonsik Kim | 95ba016 | 2019-03-19 15:51:54 -0700 | [diff] [blame] | 283 | float gap = (config.mMinAdjustedFps > 0) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 284 | ? c2_min(INT32_MAX + 0., 1e6 / config.mMinAdjustedFps + 0.5) |
| 285 | : c2_max(0. - INT32_MAX, -1e6 / config.mFixedAdjustedFps - 0.5); |
Wonsik Kim | 95ba016 | 2019-03-19 15:51:54 -0700 | [diff] [blame] | 286 | // float -> uint32_t is undefined if the value is negative. |
| 287 | // First convert to int32_t to ensure the expected behavior. |
| 288 | ptrGapParam.nU32 = int32_t(gap); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 289 | (void)mNode->setParameter( |
| 290 | (OMX_INDEXTYPE)OMX_IndexParamMaxFrameDurationForBitrateControl, |
| 291 | &ptrGapParam, sizeof(ptrGapParam)); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // max fps |
| 296 | // TRICKY: we do not unset max fps to 0 unless using fixed fps |
Wonsik Kim | 95ba016 | 2019-03-19 15:51:54 -0700 | [diff] [blame] | 297 | if ((config.mMaxFps > 0 || (config.mFixedAdjustedFps > 0 && config.mMaxFps == -1)) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 298 | && config.mMaxFps != mConfig.mMaxFps) { |
| 299 | status_t res = GetStatus(mSource->setMaxFps(config.mMaxFps)); |
| 300 | status << " maxFps=" << config.mMaxFps; |
| 301 | if (res != OK) { |
| 302 | status << " (=> " << asString(res) << ")"; |
| 303 | err = res; |
| 304 | } |
| 305 | mConfig.mMaxFps = config.mMaxFps; |
| 306 | } |
| 307 | |
| 308 | if (config.mTimeOffsetUs != mConfig.mTimeOffsetUs) { |
| 309 | status_t res = GetStatus(mSource->setTimeOffsetUs(config.mTimeOffsetUs)); |
| 310 | status << " timeOffset " << config.mTimeOffsetUs << "us"; |
| 311 | if (res != OK) { |
| 312 | status << " (=> " << asString(res) << ")"; |
| 313 | err = res; |
| 314 | } |
| 315 | mConfig.mTimeOffsetUs = config.mTimeOffsetUs; |
| 316 | } |
| 317 | |
| 318 | if (config.mCaptureFps != mConfig.mCaptureFps || config.mCodedFps != mConfig.mCodedFps) { |
| 319 | status_t res = |
| 320 | GetStatus(mSource->setTimeLapseConfig(config.mCodedFps, config.mCaptureFps)); |
| 321 | status << " timeLapse " << config.mCaptureFps << "fps as " << config.mCodedFps << "fps"; |
| 322 | if (res != OK) { |
| 323 | status << " (=> " << asString(res) << ")"; |
| 324 | err = res; |
| 325 | } |
| 326 | mConfig.mCaptureFps = config.mCaptureFps; |
| 327 | mConfig.mCodedFps = config.mCodedFps; |
| 328 | } |
| 329 | |
| 330 | if (config.mStartAtUs != mConfig.mStartAtUs |
| 331 | || (config.mStopped != mConfig.mStopped && !config.mStopped)) { |
| 332 | status_t res = GetStatus(mSource->setStartTimeUs(config.mStartAtUs)); |
| 333 | status << " start at " << config.mStartAtUs << "us"; |
| 334 | if (res != OK) { |
| 335 | status << " (=> " << asString(res) << ")"; |
| 336 | err = res; |
| 337 | } |
| 338 | mConfig.mStartAtUs = config.mStartAtUs; |
| 339 | mConfig.mStopped = config.mStopped; |
| 340 | } |
| 341 | |
| 342 | // suspend-resume |
| 343 | if (config.mSuspended != mConfig.mSuspended) { |
| 344 | status_t res = GetStatus(mSource->setSuspend(config.mSuspended, config.mSuspendAtUs)); |
| 345 | status << " " << (config.mSuspended ? "suspend" : "resume") |
| 346 | << " at " << config.mSuspendAtUs << "us"; |
| 347 | if (res != OK) { |
| 348 | status << " (=> " << asString(res) << ")"; |
| 349 | err = res; |
| 350 | } |
| 351 | mConfig.mSuspended = config.mSuspended; |
| 352 | mConfig.mSuspendAtUs = config.mSuspendAtUs; |
| 353 | } |
| 354 | |
| 355 | if (config.mStopped != mConfig.mStopped && config.mStopped) { |
| 356 | status_t res = GetStatus(mSource->setStopTimeUs(config.mStopAtUs)); |
| 357 | status << " stop at " << config.mStopAtUs << "us"; |
| 358 | if (res != OK) { |
| 359 | status << " (=> " << asString(res) << ")"; |
| 360 | err = res; |
| 361 | } else { |
| 362 | status << " delayUs"; |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 363 | hardware::Return<void> trans = mSource->getStopTimeOffsetUs( |
| 364 | [&res, &delayUs = config.mInputDelayUs]( |
| 365 | auto status, auto stopTimeOffsetUs) { |
| 366 | res = static_cast<status_t>(status); |
| 367 | delayUs = stopTimeOffsetUs; |
| 368 | }); |
| 369 | if (!trans.isOk()) { |
| 370 | res = trans.isDeadObject() ? DEAD_OBJECT : UNKNOWN_ERROR; |
| 371 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 372 | if (res != OK) { |
| 373 | status << " (=> " << asString(res) << ")"; |
| 374 | } else { |
| 375 | status << "=" << config.mInputDelayUs << "us"; |
| 376 | } |
| 377 | mConfig.mInputDelayUs = config.mInputDelayUs; |
| 378 | } |
| 379 | mConfig.mStopAtUs = config.mStopAtUs; |
| 380 | mConfig.mStopped = config.mStopped; |
| 381 | } |
| 382 | |
| 383 | // color aspects (android._color-aspects) |
| 384 | |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 385 | // consumer usage is queried earlier. |
| 386 | |
Wonsik Kim | bd55793 | 2019-07-02 15:51:20 -0700 | [diff] [blame] | 387 | if (status.str().empty()) { |
| 388 | ALOGD("ISConfig not changed"); |
| 389 | } else { |
| 390 | ALOGD("ISConfig%s", status.str().c_str()); |
| 391 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 392 | return err; |
| 393 | } |
| 394 | |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 395 | void onInputBufferDone(c2_cntr64_t index) override { |
| 396 | mNode->onInputBufferDone(index); |
| 397 | } |
| 398 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 399 | private: |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 400 | sp<HGraphicBufferSource> mSource; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 401 | sp<C2OMXNode> mNode; |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 402 | sp<hardware::media::omx::V1_0::IOmxNode> mOmxNode; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 403 | uint32_t mWidth; |
| 404 | uint32_t mHeight; |
| 405 | Config mConfig; |
| 406 | }; |
| 407 | |
| 408 | class Codec2ClientInterfaceWrapper : public C2ComponentStore { |
| 409 | std::shared_ptr<Codec2Client> mClient; |
| 410 | |
| 411 | public: |
| 412 | Codec2ClientInterfaceWrapper(std::shared_ptr<Codec2Client> client) |
| 413 | : mClient(client) { } |
| 414 | |
| 415 | virtual ~Codec2ClientInterfaceWrapper() = default; |
| 416 | |
| 417 | virtual c2_status_t config_sm( |
| 418 | const std::vector<C2Param *> ¶ms, |
| 419 | std::vector<std::unique_ptr<C2SettingResult>> *const failures) { |
| 420 | return mClient->config(params, C2_MAY_BLOCK, failures); |
| 421 | }; |
| 422 | |
| 423 | virtual c2_status_t copyBuffer( |
| 424 | std::shared_ptr<C2GraphicBuffer>, |
| 425 | std::shared_ptr<C2GraphicBuffer>) { |
| 426 | return C2_OMITTED; |
| 427 | } |
| 428 | |
| 429 | virtual c2_status_t createComponent( |
| 430 | C2String, std::shared_ptr<C2Component> *const component) { |
| 431 | component->reset(); |
| 432 | return C2_OMITTED; |
| 433 | } |
| 434 | |
| 435 | virtual c2_status_t createInterface( |
| 436 | C2String, std::shared_ptr<C2ComponentInterface> *const interface) { |
| 437 | interface->reset(); |
| 438 | return C2_OMITTED; |
| 439 | } |
| 440 | |
| 441 | virtual c2_status_t query_sm( |
| 442 | const std::vector<C2Param *> &stackParams, |
| 443 | const std::vector<C2Param::Index> &heapParamIndices, |
| 444 | std::vector<std::unique_ptr<C2Param>> *const heapParams) const { |
| 445 | return mClient->query(stackParams, heapParamIndices, C2_MAY_BLOCK, heapParams); |
| 446 | } |
| 447 | |
| 448 | virtual c2_status_t querySupportedParams_nb( |
| 449 | std::vector<std::shared_ptr<C2ParamDescriptor>> *const params) const { |
| 450 | return mClient->querySupportedParams(params); |
| 451 | } |
| 452 | |
| 453 | virtual c2_status_t querySupportedValues_sm( |
| 454 | std::vector<C2FieldSupportedValuesQuery> &fields) const { |
| 455 | return mClient->querySupportedValues(fields, C2_MAY_BLOCK); |
| 456 | } |
| 457 | |
| 458 | virtual C2String getName() const { |
| 459 | return mClient->getName(); |
| 460 | } |
| 461 | |
| 462 | virtual std::shared_ptr<C2ParamReflector> getParamReflector() const { |
| 463 | return mClient->getParamReflector(); |
| 464 | } |
| 465 | |
| 466 | virtual std::vector<std::shared_ptr<const C2Component::Traits>> listComponents() { |
| 467 | return std::vector<std::shared_ptr<const C2Component::Traits>>(); |
| 468 | } |
| 469 | }; |
| 470 | |
| 471 | } // namespace |
| 472 | |
| 473 | // CCodec::ClientListener |
| 474 | |
| 475 | struct CCodec::ClientListener : public Codec2Client::Listener { |
| 476 | |
| 477 | explicit ClientListener(const wp<CCodec> &codec) : mCodec(codec) {} |
| 478 | |
| 479 | virtual void onWorkDone( |
| 480 | const std::weak_ptr<Codec2Client::Component>& component, |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 481 | std::list<std::unique_ptr<C2Work>>& workItems) override { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 482 | (void)component; |
| 483 | sp<CCodec> codec(mCodec.promote()); |
| 484 | if (!codec) { |
| 485 | return; |
| 486 | } |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 487 | codec->onWorkDone(workItems); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | virtual void onTripped( |
| 491 | const std::weak_ptr<Codec2Client::Component>& component, |
| 492 | const std::vector<std::shared_ptr<C2SettingResult>>& settingResult |
| 493 | ) override { |
| 494 | // TODO |
| 495 | (void)component; |
| 496 | (void)settingResult; |
| 497 | } |
| 498 | |
| 499 | virtual void onError( |
| 500 | const std::weak_ptr<Codec2Client::Component>& component, |
| 501 | uint32_t errorCode) override { |
| 502 | // TODO |
| 503 | (void)component; |
| 504 | (void)errorCode; |
| 505 | } |
| 506 | |
| 507 | virtual void onDeath( |
| 508 | const std::weak_ptr<Codec2Client::Component>& component) override { |
| 509 | { // Log the death of the component. |
| 510 | std::shared_ptr<Codec2Client::Component> comp = component.lock(); |
| 511 | if (!comp) { |
| 512 | ALOGE("Codec2 component died."); |
| 513 | } else { |
| 514 | ALOGE("Codec2 component \"%s\" died.", comp->getName().c_str()); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | // Report to MediaCodec. |
| 519 | sp<CCodec> codec(mCodec.promote()); |
| 520 | if (!codec || !codec->mCallback) { |
| 521 | return; |
| 522 | } |
| 523 | codec->mCallback->onError(DEAD_OBJECT, ACTION_CODE_FATAL); |
| 524 | } |
| 525 | |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 526 | virtual void onFrameRendered(uint64_t bufferQueueId, |
| 527 | int32_t slotId, |
| 528 | int64_t timestampNs) override { |
| 529 | // TODO: implement |
| 530 | (void)bufferQueueId; |
| 531 | (void)slotId; |
| 532 | (void)timestampNs; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | virtual void onInputBufferDone( |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 536 | uint64_t frameIndex, size_t arrayIndex) override { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 537 | sp<CCodec> codec(mCodec.promote()); |
| 538 | if (codec) { |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 539 | codec->onInputBufferDone(frameIndex, arrayIndex); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
| 543 | private: |
| 544 | wp<CCodec> mCodec; |
| 545 | }; |
| 546 | |
| 547 | // CCodecCallbackImpl |
| 548 | |
| 549 | class CCodecCallbackImpl : public CCodecCallback { |
| 550 | public: |
| 551 | explicit CCodecCallbackImpl(CCodec *codec) : mCodec(codec) {} |
| 552 | ~CCodecCallbackImpl() override = default; |
| 553 | |
| 554 | void onError(status_t err, enum ActionCode actionCode) override { |
| 555 | mCodec->mCallback->onError(err, actionCode); |
| 556 | } |
| 557 | |
| 558 | void onOutputFramesRendered(int64_t mediaTimeUs, nsecs_t renderTimeNs) override { |
| 559 | mCodec->mCallback->onOutputFramesRendered( |
| 560 | {RenderedFrameInfo(mediaTimeUs, renderTimeNs)}); |
| 561 | } |
| 562 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 563 | void onOutputBuffersChanged() override { |
| 564 | mCodec->mCallback->onOutputBuffersChanged(); |
| 565 | } |
| 566 | |
| 567 | private: |
| 568 | CCodec *mCodec; |
| 569 | }; |
| 570 | |
| 571 | // CCodec |
| 572 | |
| 573 | CCodec::CCodec() |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 574 | : mChannel(new CCodecBufferChannel(std::make_shared<CCodecCallbackImpl>(this))) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | CCodec::~CCodec() { |
| 578 | } |
| 579 | |
| 580 | std::shared_ptr<BufferChannelBase> CCodec::getBufferChannel() { |
| 581 | return mChannel; |
| 582 | } |
| 583 | |
| 584 | status_t CCodec::tryAndReportOnError(std::function<status_t()> job) { |
| 585 | status_t err = job(); |
| 586 | if (err != C2_OK) { |
| 587 | mCallback->onError(err, ACTION_CODE_FATAL); |
| 588 | } |
| 589 | return err; |
| 590 | } |
| 591 | |
| 592 | void CCodec::initiateAllocateComponent(const sp<AMessage> &msg) { |
| 593 | auto setAllocating = [this] { |
| 594 | Mutexed<State>::Locked state(mState); |
| 595 | if (state->get() != RELEASED) { |
| 596 | return INVALID_OPERATION; |
| 597 | } |
| 598 | state->set(ALLOCATING); |
| 599 | return OK; |
| 600 | }; |
| 601 | if (tryAndReportOnError(setAllocating) != OK) { |
| 602 | return; |
| 603 | } |
| 604 | |
| 605 | sp<RefBase> codecInfo; |
| 606 | CHECK(msg->findObject("codecInfo", &codecInfo)); |
| 607 | // For Codec 2.0 components, componentName == codecInfo->getCodecName(). |
| 608 | |
| 609 | sp<AMessage> allocMsg(new AMessage(kWhatAllocate, this)); |
| 610 | allocMsg->setObject("codecInfo", codecInfo); |
| 611 | allocMsg->post(); |
| 612 | } |
| 613 | |
| 614 | void CCodec::allocate(const sp<MediaCodecInfo> &codecInfo) { |
| 615 | if (codecInfo == nullptr) { |
| 616 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 617 | return; |
| 618 | } |
| 619 | ALOGD("allocate(%s)", codecInfo->getCodecName()); |
| 620 | mClientListener.reset(new ClientListener(this)); |
| 621 | |
| 622 | AString componentName = codecInfo->getCodecName(); |
| 623 | std::shared_ptr<Codec2Client> client; |
| 624 | |
| 625 | // set up preferred component store to access vendor store parameters |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 626 | client = Codec2Client::CreateFromService("default"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 627 | if (client) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 628 | ALOGI("setting up '%s' as default (vendor) store", client->getServiceName().c_str()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 629 | SetPreferredCodec2ComponentStore( |
| 630 | std::make_shared<Codec2ClientInterfaceWrapper>(client)); |
| 631 | } |
| 632 | |
| 633 | std::shared_ptr<Codec2Client::Component> comp = |
| 634 | Codec2Client::CreateComponentByName( |
| 635 | componentName.c_str(), |
| 636 | mClientListener, |
| 637 | &client); |
| 638 | if (!comp) { |
| 639 | ALOGE("Failed Create component: %s", componentName.c_str()); |
| 640 | Mutexed<State>::Locked state(mState); |
| 641 | state->set(RELEASED); |
| 642 | state.unlock(); |
| 643 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 644 | state.lock(); |
| 645 | return; |
| 646 | } |
| 647 | ALOGI("Created component [%s]", componentName.c_str()); |
| 648 | mChannel->setComponent(comp); |
| 649 | auto setAllocated = [this, comp, client] { |
| 650 | Mutexed<State>::Locked state(mState); |
| 651 | if (state->get() != ALLOCATING) { |
| 652 | state->set(RELEASED); |
| 653 | return UNKNOWN_ERROR; |
| 654 | } |
| 655 | state->set(ALLOCATED); |
| 656 | state->comp = comp; |
| 657 | mClient = client; |
| 658 | return OK; |
| 659 | }; |
| 660 | if (tryAndReportOnError(setAllocated) != OK) { |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | // initialize config here in case setParameters is called prior to configure |
| 665 | Mutexed<Config>::Locked config(mConfig); |
| 666 | status_t err = config->initialize(mClient, comp); |
| 667 | if (err != OK) { |
| 668 | ALOGW("Failed to initialize configuration support"); |
| 669 | // TODO: report error once we complete implementation. |
| 670 | } |
| 671 | config->queryConfiguration(comp); |
| 672 | |
| 673 | mCallback->onComponentAllocated(componentName.c_str()); |
| 674 | } |
| 675 | |
| 676 | void CCodec::initiateConfigureComponent(const sp<AMessage> &format) { |
| 677 | auto checkAllocated = [this] { |
| 678 | Mutexed<State>::Locked state(mState); |
| 679 | return (state->get() != ALLOCATED) ? UNKNOWN_ERROR : OK; |
| 680 | }; |
| 681 | if (tryAndReportOnError(checkAllocated) != OK) { |
| 682 | return; |
| 683 | } |
| 684 | |
| 685 | sp<AMessage> msg(new AMessage(kWhatConfigure, this)); |
| 686 | msg->setMessage("format", format); |
| 687 | msg->post(); |
| 688 | } |
| 689 | |
| 690 | void CCodec::configure(const sp<AMessage> &msg) { |
| 691 | std::shared_ptr<Codec2Client::Component> comp; |
| 692 | auto checkAllocated = [this, &comp] { |
| 693 | Mutexed<State>::Locked state(mState); |
| 694 | if (state->get() != ALLOCATED) { |
| 695 | state->set(RELEASED); |
| 696 | return UNKNOWN_ERROR; |
| 697 | } |
| 698 | comp = state->comp; |
| 699 | return OK; |
| 700 | }; |
| 701 | if (tryAndReportOnError(checkAllocated) != OK) { |
| 702 | return; |
| 703 | } |
| 704 | |
| 705 | auto doConfig = [msg, comp, this]() -> status_t { |
| 706 | AString mime; |
| 707 | if (!msg->findString("mime", &mime)) { |
| 708 | return BAD_VALUE; |
| 709 | } |
| 710 | |
| 711 | int32_t encoder; |
| 712 | if (!msg->findInt32("encoder", &encoder)) { |
| 713 | encoder = false; |
| 714 | } |
| 715 | |
| 716 | // TODO: read from intf() |
| 717 | if ((!encoder) != (comp->getName().find("encoder") == std::string::npos)) { |
| 718 | return UNKNOWN_ERROR; |
| 719 | } |
| 720 | |
| 721 | int32_t storeMeta; |
| 722 | if (encoder |
| 723 | && msg->findInt32("android._input-metadata-buffer-type", &storeMeta) |
| 724 | && storeMeta != kMetadataBufferTypeInvalid) { |
| 725 | if (storeMeta != kMetadataBufferTypeANWBuffer) { |
| 726 | ALOGD("Only ANW buffers are supported for legacy metadata mode"); |
| 727 | return BAD_VALUE; |
| 728 | } |
| 729 | mChannel->setMetaMode(CCodecBufferChannel::MODE_ANW); |
| 730 | } |
| 731 | |
| 732 | sp<RefBase> obj; |
| 733 | sp<Surface> surface; |
| 734 | if (msg->findObject("native-window", &obj)) { |
| 735 | surface = static_cast<Surface *>(obj.get()); |
| 736 | setSurface(surface); |
| 737 | } |
| 738 | |
| 739 | Mutexed<Config>::Locked config(mConfig); |
| 740 | config->mUsingSurface = surface != nullptr; |
| 741 | |
Wonsik Kim | 1114eea | 2019-02-25 14:35:24 -0800 | [diff] [blame] | 742 | // Enforce required parameters |
| 743 | int32_t i32; |
| 744 | float flt; |
| 745 | if (config->mDomain & Config::IS_AUDIO) { |
| 746 | if (!msg->findInt32(KEY_SAMPLE_RATE, &i32)) { |
| 747 | ALOGD("sample rate is missing, which is required for audio components."); |
| 748 | return BAD_VALUE; |
| 749 | } |
| 750 | if (!msg->findInt32(KEY_CHANNEL_COUNT, &i32)) { |
| 751 | ALOGD("channel count is missing, which is required for audio components."); |
| 752 | return BAD_VALUE; |
| 753 | } |
| 754 | if ((config->mDomain & Config::IS_ENCODER) |
| 755 | && !mime.equalsIgnoreCase(MEDIA_MIMETYPE_AUDIO_FLAC) |
| 756 | && !msg->findInt32(KEY_BIT_RATE, &i32) |
| 757 | && !msg->findFloat(KEY_BIT_RATE, &flt)) { |
| 758 | ALOGD("bitrate is missing, which is required for audio encoders."); |
| 759 | return BAD_VALUE; |
| 760 | } |
| 761 | } |
| 762 | if (config->mDomain & (Config::IS_IMAGE | Config::IS_VIDEO)) { |
| 763 | if (!msg->findInt32(KEY_WIDTH, &i32)) { |
| 764 | ALOGD("width is missing, which is required for image/video components."); |
| 765 | return BAD_VALUE; |
| 766 | } |
| 767 | if (!msg->findInt32(KEY_HEIGHT, &i32)) { |
| 768 | ALOGD("height is missing, which is required for image/video components."); |
| 769 | return BAD_VALUE; |
| 770 | } |
| 771 | if ((config->mDomain & Config::IS_ENCODER) && (config->mDomain & Config::IS_VIDEO)) { |
Harish Mahendrakar | 71cbb9d | 2019-05-21 11:21:27 -0700 | [diff] [blame] | 772 | int32_t mode = BITRATE_MODE_VBR; |
| 773 | if (msg->findInt32(KEY_BITRATE_MODE, &mode) && mode == BITRATE_MODE_CQ) { |
Harish Mahendrakar | 817d318 | 2019-03-11 16:37:47 -0700 | [diff] [blame] | 774 | if (!msg->findInt32(KEY_QUALITY, &i32)) { |
| 775 | ALOGD("quality is missing, which is required for video encoders in CQ."); |
| 776 | return BAD_VALUE; |
| 777 | } |
| 778 | } else { |
| 779 | if (!msg->findInt32(KEY_BIT_RATE, &i32) |
| 780 | && !msg->findFloat(KEY_BIT_RATE, &flt)) { |
| 781 | ALOGD("bitrate is missing, which is required for video encoders."); |
| 782 | return BAD_VALUE; |
| 783 | } |
Wonsik Kim | 1114eea | 2019-02-25 14:35:24 -0800 | [diff] [blame] | 784 | } |
| 785 | if (!msg->findInt32(KEY_I_FRAME_INTERVAL, &i32) |
| 786 | && !msg->findFloat(KEY_I_FRAME_INTERVAL, &flt)) { |
| 787 | ALOGD("I frame interval is missing, which is required for video encoders."); |
| 788 | return BAD_VALUE; |
| 789 | } |
Wonsik Kim | aab2eea | 2019-05-22 10:37:58 -0700 | [diff] [blame] | 790 | if (!msg->findInt32(KEY_FRAME_RATE, &i32) |
| 791 | && !msg->findFloat(KEY_FRAME_RATE, &flt)) { |
| 792 | ALOGD("frame rate is missing, which is required for video encoders."); |
| 793 | return BAD_VALUE; |
| 794 | } |
Wonsik Kim | 1114eea | 2019-02-25 14:35:24 -0800 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 798 | /* |
| 799 | * Handle input surface configuration |
| 800 | */ |
| 801 | if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE)) |
| 802 | && (config->mDomain & Config::IS_ENCODER)) { |
| 803 | config->mISConfig.reset(new InputSurfaceWrapper::Config{}); |
| 804 | { |
| 805 | config->mISConfig->mMinFps = 0; |
| 806 | int64_t value; |
Chong Zhang | 038e8f8 | 2019-02-06 19:05:14 -0800 | [diff] [blame] | 807 | if (msg->findInt64(KEY_REPEAT_PREVIOUS_FRAME_AFTER, &value) && value > 0) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 808 | config->mISConfig->mMinFps = 1e6 / value; |
| 809 | } |
Wonsik Kim | 95ba016 | 2019-03-19 15:51:54 -0700 | [diff] [blame] | 810 | if (!msg->findFloat( |
| 811 | KEY_MAX_FPS_TO_ENCODER, &config->mISConfig->mMaxFps)) { |
| 812 | config->mISConfig->mMaxFps = -1; |
| 813 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 814 | config->mISConfig->mMinAdjustedFps = 0; |
| 815 | config->mISConfig->mFixedAdjustedFps = 0; |
Chong Zhang | 038e8f8 | 2019-02-06 19:05:14 -0800 | [diff] [blame] | 816 | if (msg->findInt64(KEY_MAX_PTS_GAP_TO_ENCODER, &value)) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 817 | if (value < 0 && value >= INT32_MIN) { |
| 818 | config->mISConfig->mFixedAdjustedFps = -1e6 / value; |
Wonsik Kim | 95ba016 | 2019-03-19 15:51:54 -0700 | [diff] [blame] | 819 | config->mISConfig->mMaxFps = -1; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 820 | } else if (value > 0 && value <= INT32_MAX) { |
| 821 | config->mISConfig->mMinAdjustedFps = 1e6 / value; |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | { |
Wonsik Kim | 8e55f3a | 2019-09-03 14:10:37 -0700 | [diff] [blame] | 827 | bool captureFpsFound = false; |
| 828 | double timeLapseFps; |
| 829 | float captureRate; |
| 830 | if (msg->findDouble("time-lapse-fps", &timeLapseFps)) { |
| 831 | config->mISConfig->mCaptureFps = timeLapseFps; |
| 832 | captureFpsFound = true; |
| 833 | } else if (msg->findAsFloat(KEY_CAPTURE_RATE, &captureRate)) { |
| 834 | config->mISConfig->mCaptureFps = captureRate; |
| 835 | captureFpsFound = true; |
| 836 | } |
| 837 | if (captureFpsFound) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 838 | (void)msg->findAsFloat(KEY_FRAME_RATE, &config->mISConfig->mCodedFps); |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | { |
| 843 | config->mISConfig->mSuspended = false; |
| 844 | config->mISConfig->mSuspendAtUs = -1; |
| 845 | int32_t value; |
Chong Zhang | 038e8f8 | 2019-02-06 19:05:14 -0800 | [diff] [blame] | 846 | if (msg->findInt32(KEY_CREATE_INPUT_SURFACE_SUSPENDED, &value) && value) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 847 | config->mISConfig->mSuspended = true; |
| 848 | } |
| 849 | } |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 850 | config->mISConfig->mUsage = 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | /* |
| 854 | * Handle desired color format. |
| 855 | */ |
| 856 | if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))) { |
| 857 | int32_t format = -1; |
| 858 | if (!msg->findInt32(KEY_COLOR_FORMAT, &format)) { |
| 859 | /* |
| 860 | * Also handle default color format (encoders require color format, so this is only |
| 861 | * needed for decoders. |
| 862 | */ |
| 863 | if (!(config->mDomain & Config::IS_ENCODER)) { |
| 864 | format = (surface == nullptr) ? COLOR_FormatYUV420Planar : COLOR_FormatSurface; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | if (format >= 0) { |
| 869 | msg->setInt32("android._color-format", format); |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | std::vector<std::unique_ptr<C2Param>> configUpdate; |
Wonsik Kim | aa484ac | 2019-02-13 16:54:02 -0800 | [diff] [blame] | 874 | // NOTE: We used to ignore "video-bitrate" at configure; replicate |
| 875 | // the behavior here. |
| 876 | sp<AMessage> sdkParams = msg; |
| 877 | int32_t videoBitrate; |
| 878 | if (sdkParams->findInt32(PARAMETER_KEY_VIDEO_BITRATE, &videoBitrate)) { |
| 879 | sdkParams = msg->dup(); |
| 880 | sdkParams->removeEntryAt(sdkParams->findEntryByName(PARAMETER_KEY_VIDEO_BITRATE)); |
| 881 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 882 | status_t err = config->getConfigUpdateFromSdkParams( |
Wonsik Kim | aa484ac | 2019-02-13 16:54:02 -0800 | [diff] [blame] | 883 | comp, sdkParams, Config::IS_CONFIG, C2_DONT_BLOCK, &configUpdate); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 884 | if (err != OK) { |
| 885 | ALOGW("failed to convert configuration to c2 params"); |
| 886 | } |
Wonsik Kim | aab2eea | 2019-05-22 10:37:58 -0700 | [diff] [blame] | 887 | |
| 888 | int32_t maxBframes = 0; |
| 889 | if ((config->mDomain & Config::IS_ENCODER) |
| 890 | && (config->mDomain & Config::IS_VIDEO) |
| 891 | && sdkParams->findInt32(KEY_MAX_B_FRAMES, &maxBframes) |
| 892 | && maxBframes > 0) { |
| 893 | std::unique_ptr<C2StreamGopTuning::output> gop = |
| 894 | C2StreamGopTuning::output::AllocUnique(2 /* flexCount */, 0u /* stream */); |
| 895 | gop->m.values[0] = { P_FRAME, UINT32_MAX }; |
| 896 | gop->m.values[1] = { |
| 897 | C2Config::picture_type_t(P_FRAME | B_FRAME), |
| 898 | uint32_t(maxBframes) |
| 899 | }; |
| 900 | configUpdate.push_back(std::move(gop)); |
| 901 | } |
| 902 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 903 | err = config->setParameters(comp, configUpdate, C2_DONT_BLOCK); |
| 904 | if (err != OK) { |
| 905 | ALOGW("failed to configure c2 params"); |
| 906 | return err; |
| 907 | } |
| 908 | |
| 909 | std::vector<std::unique_ptr<C2Param>> params; |
| 910 | C2StreamUsageTuning::input usage(0u, 0u); |
| 911 | C2StreamMaxBufferSizeInfo::input maxInputSize(0u, 0u); |
Wonsik Kim | 9ca01d3 | 2019-04-01 14:45:47 -0700 | [diff] [blame] | 912 | C2PrependHeaderModeSetting prepend(PREPEND_HEADER_TO_NONE); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 913 | |
| 914 | std::initializer_list<C2Param::Index> indices { |
| 915 | }; |
| 916 | c2_status_t c2err = comp->query( |
Wonsik Kim | 9ca01d3 | 2019-04-01 14:45:47 -0700 | [diff] [blame] | 917 | { &usage, &maxInputSize, &prepend }, |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 918 | indices, |
| 919 | C2_DONT_BLOCK, |
| 920 | ¶ms); |
| 921 | if (c2err != C2_OK && c2err != C2_BAD_INDEX) { |
| 922 | ALOGE("Failed to query component interface: %d", c2err); |
| 923 | return UNKNOWN_ERROR; |
| 924 | } |
| 925 | if (params.size() != indices.size()) { |
| 926 | ALOGE("Component returns wrong number of params: expected %zu actual %zu", |
| 927 | indices.size(), params.size()); |
| 928 | return UNKNOWN_ERROR; |
| 929 | } |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 930 | if (usage) { |
| 931 | if (usage.value & C2MemoryUsage::CPU_READ) { |
| 932 | config->mInputFormat->setInt32("using-sw-read-often", true); |
| 933 | } |
| 934 | if (config->mISConfig) { |
| 935 | C2AndroidMemoryUsage androidUsage(C2MemoryUsage(usage.value)); |
| 936 | config->mISConfig->mUsage = androidUsage.asGrallocUsage(); |
| 937 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | // NOTE: we don't blindly use client specified input size if specified as clients |
| 941 | // at times specify too small size. Instead, mimic the behavior from OMX, where the |
| 942 | // client specified size is only used to ask for bigger buffers than component suggested |
| 943 | // size. |
| 944 | int32_t clientInputSize = 0; |
| 945 | bool clientSpecifiedInputSize = |
| 946 | msg->findInt32(KEY_MAX_INPUT_SIZE, &clientInputSize) && clientInputSize > 0; |
| 947 | // TEMP: enforce minimum buffer size of 1MB for video decoders |
| 948 | // and 16K / 4K for audio encoders/decoders |
| 949 | if (maxInputSize.value == 0) { |
| 950 | if (config->mDomain & Config::IS_AUDIO) { |
| 951 | maxInputSize.value = encoder ? 16384 : 4096; |
| 952 | } else if (!encoder) { |
| 953 | maxInputSize.value = 1048576u; |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | // verify that CSD fits into this size (if defined) |
| 958 | if ((config->mDomain & Config::IS_DECODER) && maxInputSize.value > 0) { |
| 959 | sp<ABuffer> csd; |
| 960 | for (size_t ix = 0; msg->findBuffer(StringPrintf("csd-%zu", ix).c_str(), &csd); ++ix) { |
| 961 | if (csd && csd->size() > maxInputSize.value) { |
| 962 | maxInputSize.value = csd->size(); |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | // TODO: do this based on component requiring linear allocator for input |
| 968 | if ((config->mDomain & Config::IS_DECODER) || (config->mDomain & Config::IS_AUDIO)) { |
| 969 | if (clientSpecifiedInputSize) { |
| 970 | // Warn that we're overriding client's max input size if necessary. |
| 971 | if ((uint32_t)clientInputSize < maxInputSize.value) { |
| 972 | ALOGD("client requested max input size %d, which is smaller than " |
| 973 | "what component recommended (%u); overriding with component " |
| 974 | "recommendation.", clientInputSize, maxInputSize.value); |
| 975 | ALOGW("This behavior is subject to change. It is recommended that " |
| 976 | "app developers double check whether the requested " |
| 977 | "max input size is in reasonable range."); |
| 978 | } else { |
| 979 | maxInputSize.value = clientInputSize; |
| 980 | } |
| 981 | } |
| 982 | // Pass max input size on input format to the buffer channel (if supplied by the |
| 983 | // component or by a default) |
| 984 | if (maxInputSize.value) { |
| 985 | config->mInputFormat->setInt32( |
| 986 | KEY_MAX_INPUT_SIZE, |
| 987 | (int32_t)(c2_min(maxInputSize.value, uint32_t(INT32_MAX)))); |
| 988 | } |
| 989 | } |
| 990 | |
Wonsik Kim | 9ca01d3 | 2019-04-01 14:45:47 -0700 | [diff] [blame] | 991 | int32_t clientPrepend; |
| 992 | if ((config->mDomain & Config::IS_VIDEO) |
| 993 | && (config->mDomain & Config::IS_ENCODER) |
| 994 | && msg->findInt32(KEY_PREPEND_HEADERS_TO_SYNC_FRAMES, &clientPrepend) |
| 995 | && clientPrepend |
| 996 | && (!prepend || prepend.value != PREPEND_HEADER_TO_ALL_SYNC)) { |
| 997 | ALOGE("Failed to set KEY_PREPEND_HEADERS_TO_SYNC_FRAMES"); |
| 998 | return BAD_VALUE; |
| 999 | } |
| 1000 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1001 | if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))) { |
| 1002 | // propagate HDR static info to output format for both encoders and decoders |
| 1003 | // if component supports this info, we will update from component, but only the raw port, |
| 1004 | // so don't propagate if component already filled it in. |
| 1005 | sp<ABuffer> hdrInfo; |
| 1006 | if (msg->findBuffer(KEY_HDR_STATIC_INFO, &hdrInfo) |
| 1007 | && !config->mOutputFormat->findBuffer(KEY_HDR_STATIC_INFO, &hdrInfo)) { |
| 1008 | config->mOutputFormat->setBuffer(KEY_HDR_STATIC_INFO, hdrInfo); |
| 1009 | } |
| 1010 | |
| 1011 | // Set desired color format from configuration parameter |
| 1012 | int32_t format; |
| 1013 | if (msg->findInt32("android._color-format", &format)) { |
| 1014 | if (config->mDomain & Config::IS_ENCODER) { |
| 1015 | config->mInputFormat->setInt32(KEY_COLOR_FORMAT, format); |
| 1016 | } else { |
| 1017 | config->mOutputFormat->setInt32(KEY_COLOR_FORMAT, format); |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | // propagate encoder delay and padding to output format |
| 1023 | if ((config->mDomain & Config::IS_DECODER) && (config->mDomain & Config::IS_AUDIO)) { |
| 1024 | int delay = 0; |
| 1025 | if (msg->findInt32("encoder-delay", &delay)) { |
| 1026 | config->mOutputFormat->setInt32("encoder-delay", delay); |
| 1027 | } |
| 1028 | int padding = 0; |
| 1029 | if (msg->findInt32("encoder-padding", &padding)) { |
| 1030 | config->mOutputFormat->setInt32("encoder-padding", padding); |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | // set channel-mask |
| 1035 | if (config->mDomain & Config::IS_AUDIO) { |
| 1036 | int32_t mask; |
| 1037 | if (msg->findInt32(KEY_CHANNEL_MASK, &mask)) { |
| 1038 | if (config->mDomain & Config::IS_ENCODER) { |
| 1039 | config->mInputFormat->setInt32(KEY_CHANNEL_MASK, mask); |
| 1040 | } else { |
| 1041 | config->mOutputFormat->setInt32(KEY_CHANNEL_MASK, mask); |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | ALOGD("setup formats input: %s and output: %s", |
| 1047 | config->mInputFormat->debugString().c_str(), |
| 1048 | config->mOutputFormat->debugString().c_str()); |
| 1049 | return OK; |
| 1050 | }; |
| 1051 | if (tryAndReportOnError(doConfig) != OK) { |
| 1052 | return; |
| 1053 | } |
| 1054 | |
| 1055 | Mutexed<Config>::Locked config(mConfig); |
| 1056 | |
| 1057 | mCallback->onComponentConfigured(config->mInputFormat, config->mOutputFormat); |
| 1058 | } |
| 1059 | |
| 1060 | void CCodec::initiateCreateInputSurface() { |
| 1061 | status_t err = [this] { |
| 1062 | Mutexed<State>::Locked state(mState); |
| 1063 | if (state->get() != ALLOCATED) { |
| 1064 | return UNKNOWN_ERROR; |
| 1065 | } |
| 1066 | // TODO: read it from intf() properly. |
| 1067 | if (state->comp->getName().find("encoder") == std::string::npos) { |
| 1068 | return INVALID_OPERATION; |
| 1069 | } |
| 1070 | return OK; |
| 1071 | }(); |
| 1072 | if (err != OK) { |
| 1073 | mCallback->onInputSurfaceCreationFailed(err); |
| 1074 | return; |
| 1075 | } |
| 1076 | |
| 1077 | (new AMessage(kWhatCreateInputSurface, this))->post(); |
| 1078 | } |
| 1079 | |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 1080 | sp<PersistentSurface> CCodec::CreateOmxInputSurface() { |
| 1081 | using namespace android::hardware::media::omx::V1_0; |
| 1082 | using namespace android::hardware::media::omx::V1_0::utils; |
| 1083 | using namespace android::hardware::graphics::bufferqueue::V1_0::utils; |
| 1084 | typedef android::hardware::media::omx::V1_0::Status OmxStatus; |
| 1085 | android::sp<IOmx> omx = IOmx::getService(); |
| 1086 | typedef android::hardware::graphics::bufferqueue::V1_0:: |
| 1087 | IGraphicBufferProducer HGraphicBufferProducer; |
| 1088 | typedef android::hardware::media::omx::V1_0:: |
| 1089 | IGraphicBufferSource HGraphicBufferSource; |
| 1090 | OmxStatus s; |
| 1091 | android::sp<HGraphicBufferProducer> gbp; |
| 1092 | android::sp<HGraphicBufferSource> gbs; |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 1093 | |
Chong Zhang | c8ce1d8 | 2019-03-27 10:18:38 -0700 | [diff] [blame] | 1094 | using ::android::hardware::Return; |
| 1095 | Return<void> transStatus = omx->createInputSurface( |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 1096 | [&s, &gbp, &gbs]( |
| 1097 | OmxStatus status, |
| 1098 | const android::sp<HGraphicBufferProducer>& producer, |
| 1099 | const android::sp<HGraphicBufferSource>& source) { |
| 1100 | s = status; |
| 1101 | gbp = producer; |
| 1102 | gbs = source; |
| 1103 | }); |
| 1104 | if (transStatus.isOk() && s == OmxStatus::OK) { |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1105 | return new PersistentSurface(new H2BGraphicBufferProducer(gbp), gbs); |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | return nullptr; |
| 1109 | } |
| 1110 | |
| 1111 | sp<PersistentSurface> CCodec::CreateCompatibleInputSurface() { |
| 1112 | sp<PersistentSurface> surface(CreateInputSurface()); |
| 1113 | |
| 1114 | if (surface == nullptr) { |
| 1115 | surface = CreateOmxInputSurface(); |
| 1116 | } |
| 1117 | |
| 1118 | return surface; |
| 1119 | } |
| 1120 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1121 | void CCodec::createInputSurface() { |
| 1122 | status_t err; |
| 1123 | sp<IGraphicBufferProducer> bufferProducer; |
| 1124 | |
| 1125 | sp<AMessage> inputFormat; |
| 1126 | sp<AMessage> outputFormat; |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 1127 | uint64_t usage = 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1128 | { |
| 1129 | Mutexed<Config>::Locked config(mConfig); |
| 1130 | inputFormat = config->mInputFormat; |
| 1131 | outputFormat = config->mOutputFormat; |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 1132 | usage = config->mISConfig ? config->mISConfig->mUsage : 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1133 | } |
| 1134 | |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 1135 | sp<PersistentSurface> persistentSurface = CreateCompatibleInputSurface(); |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1136 | sp<hidl::base::V1_0::IBase> hidlTarget = persistentSurface->getHidlTarget(); |
| 1137 | sp<IInputSurface> hidlInputSurface = IInputSurface::castFrom(hidlTarget); |
| 1138 | sp<HGraphicBufferSource> gbs = HGraphicBufferSource::castFrom(hidlTarget); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1139 | |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1140 | if (hidlInputSurface) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1141 | std::shared_ptr<Codec2Client::InputSurface> inputSurface = |
| 1142 | std::make_shared<Codec2Client::InputSurface>(hidlInputSurface); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1143 | err = setupInputSurface(std::make_shared<C2InputSurfaceWrapper>( |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1144 | inputSurface)); |
| 1145 | bufferProducer = inputSurface->getGraphicBufferProducer(); |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1146 | } else if (gbs) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1147 | int32_t width = 0; |
| 1148 | (void)outputFormat->findInt32("width", &width); |
| 1149 | int32_t height = 0; |
| 1150 | (void)outputFormat->findInt32("height", &height); |
| 1151 | err = setupInputSurface(std::make_shared<GraphicBufferSourceWrapper>( |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1152 | gbs, width, height, usage)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1153 | bufferProducer = persistentSurface->getBufferProducer(); |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1154 | } else { |
| 1155 | ALOGE("Corrupted input surface"); |
| 1156 | mCallback->onInputSurfaceCreationFailed(UNKNOWN_ERROR); |
| 1157 | return; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | if (err != OK) { |
| 1161 | ALOGE("Failed to set up input surface: %d", err); |
| 1162 | mCallback->onInputSurfaceCreationFailed(err); |
| 1163 | return; |
| 1164 | } |
| 1165 | |
| 1166 | mCallback->onInputSurfaceCreated( |
| 1167 | inputFormat, |
| 1168 | outputFormat, |
| 1169 | new BufferProducerWrapper(bufferProducer)); |
| 1170 | } |
| 1171 | |
| 1172 | status_t CCodec::setupInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface) { |
| 1173 | Mutexed<Config>::Locked config(mConfig); |
| 1174 | config->mUsingSurface = true; |
| 1175 | |
| 1176 | // we are now using surface - apply default color aspects to input format - as well as |
| 1177 | // get dataspace |
| 1178 | bool inputFormatChanged = config->updateFormats(config->IS_INPUT); |
| 1179 | ALOGD("input format %s to %s", |
| 1180 | inputFormatChanged ? "changed" : "unchanged", |
| 1181 | config->mInputFormat->debugString().c_str()); |
| 1182 | |
| 1183 | // configure dataspace |
| 1184 | static_assert(sizeof(int32_t) == sizeof(android_dataspace), "dataspace size mismatch"); |
| 1185 | android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN; |
| 1186 | (void)config->mInputFormat->findInt32("android._dataspace", (int32_t*)&dataSpace); |
| 1187 | surface->setDataSpace(dataSpace); |
| 1188 | |
| 1189 | status_t err = mChannel->setInputSurface(surface); |
| 1190 | if (err != OK) { |
| 1191 | // undo input format update |
| 1192 | config->mUsingSurface = false; |
| 1193 | (void)config->updateFormats(config->IS_INPUT); |
| 1194 | return err; |
| 1195 | } |
| 1196 | config->mInputSurface = surface; |
| 1197 | |
| 1198 | if (config->mISConfig) { |
| 1199 | surface->configure(*config->mISConfig); |
| 1200 | } else { |
| 1201 | ALOGD("ISConfig: no configuration"); |
| 1202 | } |
| 1203 | |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1204 | return OK; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | void CCodec::initiateSetInputSurface(const sp<PersistentSurface> &surface) { |
| 1208 | sp<AMessage> msg = new AMessage(kWhatSetInputSurface, this); |
| 1209 | msg->setObject("surface", surface); |
| 1210 | msg->post(); |
| 1211 | } |
| 1212 | |
| 1213 | void CCodec::setInputSurface(const sp<PersistentSurface> &surface) { |
| 1214 | sp<AMessage> inputFormat; |
| 1215 | sp<AMessage> outputFormat; |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 1216 | uint64_t usage = 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1217 | { |
| 1218 | Mutexed<Config>::Locked config(mConfig); |
| 1219 | inputFormat = config->mInputFormat; |
| 1220 | outputFormat = config->mOutputFormat; |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 1221 | usage = config->mISConfig ? config->mISConfig->mUsage : 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1222 | } |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1223 | sp<hidl::base::V1_0::IBase> hidlTarget = surface->getHidlTarget(); |
| 1224 | sp<IInputSurface> inputSurface = IInputSurface::castFrom(hidlTarget); |
| 1225 | sp<HGraphicBufferSource> gbs = HGraphicBufferSource::castFrom(hidlTarget); |
| 1226 | if (inputSurface) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1227 | status_t err = setupInputSurface(std::make_shared<C2InputSurfaceWrapper>( |
| 1228 | std::make_shared<Codec2Client::InputSurface>(inputSurface))); |
| 1229 | if (err != OK) { |
| 1230 | ALOGE("Failed to set up input surface: %d", err); |
| 1231 | mCallback->onInputSurfaceDeclined(err); |
| 1232 | return; |
| 1233 | } |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1234 | } else if (gbs) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1235 | int32_t width = 0; |
| 1236 | (void)outputFormat->findInt32("width", &width); |
| 1237 | int32_t height = 0; |
| 1238 | (void)outputFormat->findInt32("height", &height); |
| 1239 | status_t err = setupInputSurface(std::make_shared<GraphicBufferSourceWrapper>( |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1240 | gbs, width, height, usage)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1241 | if (err != OK) { |
| 1242 | ALOGE("Failed to set up input surface: %d", err); |
| 1243 | mCallback->onInputSurfaceDeclined(err); |
| 1244 | return; |
| 1245 | } |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1246 | } else { |
| 1247 | ALOGE("Failed to set input surface: Corrupted surface."); |
| 1248 | mCallback->onInputSurfaceDeclined(UNKNOWN_ERROR); |
| 1249 | return; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1250 | } |
| 1251 | mCallback->onInputSurfaceAccepted(inputFormat, outputFormat); |
| 1252 | } |
| 1253 | |
| 1254 | void CCodec::initiateStart() { |
| 1255 | auto setStarting = [this] { |
| 1256 | Mutexed<State>::Locked state(mState); |
| 1257 | if (state->get() != ALLOCATED) { |
| 1258 | return UNKNOWN_ERROR; |
| 1259 | } |
| 1260 | state->set(STARTING); |
| 1261 | return OK; |
| 1262 | }; |
| 1263 | if (tryAndReportOnError(setStarting) != OK) { |
| 1264 | return; |
| 1265 | } |
| 1266 | |
| 1267 | (new AMessage(kWhatStart, this))->post(); |
| 1268 | } |
| 1269 | |
| 1270 | void CCodec::start() { |
| 1271 | std::shared_ptr<Codec2Client::Component> comp; |
| 1272 | auto checkStarting = [this, &comp] { |
| 1273 | Mutexed<State>::Locked state(mState); |
| 1274 | if (state->get() != STARTING) { |
| 1275 | return UNKNOWN_ERROR; |
| 1276 | } |
| 1277 | comp = state->comp; |
| 1278 | return OK; |
| 1279 | }; |
| 1280 | if (tryAndReportOnError(checkStarting) != OK) { |
| 1281 | return; |
| 1282 | } |
| 1283 | |
| 1284 | c2_status_t err = comp->start(); |
| 1285 | if (err != C2_OK) { |
| 1286 | mCallback->onError(toStatusT(err, C2_OPERATION_Component_start), |
| 1287 | ACTION_CODE_FATAL); |
| 1288 | return; |
| 1289 | } |
| 1290 | sp<AMessage> inputFormat; |
| 1291 | sp<AMessage> outputFormat; |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1292 | status_t err2 = OK; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1293 | { |
| 1294 | Mutexed<Config>::Locked config(mConfig); |
| 1295 | inputFormat = config->mInputFormat; |
| 1296 | outputFormat = config->mOutputFormat; |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1297 | if (config->mInputSurface) { |
| 1298 | err2 = config->mInputSurface->start(); |
| 1299 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1300 | } |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1301 | if (err2 != OK) { |
| 1302 | mCallback->onError(err2, ACTION_CODE_FATAL); |
| 1303 | return; |
| 1304 | } |
| 1305 | err2 = mChannel->start(inputFormat, outputFormat); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1306 | if (err2 != OK) { |
| 1307 | mCallback->onError(err2, ACTION_CODE_FATAL); |
| 1308 | return; |
| 1309 | } |
| 1310 | |
| 1311 | auto setRunning = [this] { |
| 1312 | Mutexed<State>::Locked state(mState); |
| 1313 | if (state->get() != STARTING) { |
| 1314 | return UNKNOWN_ERROR; |
| 1315 | } |
| 1316 | state->set(RUNNING); |
| 1317 | return OK; |
| 1318 | }; |
| 1319 | if (tryAndReportOnError(setRunning) != OK) { |
| 1320 | return; |
| 1321 | } |
| 1322 | mCallback->onStartCompleted(); |
| 1323 | |
| 1324 | (void)mChannel->requestInitialInputBuffers(); |
| 1325 | } |
| 1326 | |
| 1327 | void CCodec::initiateShutdown(bool keepComponentAllocated) { |
| 1328 | if (keepComponentAllocated) { |
| 1329 | initiateStop(); |
| 1330 | } else { |
| 1331 | initiateRelease(); |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | void CCodec::initiateStop() { |
| 1336 | { |
| 1337 | Mutexed<State>::Locked state(mState); |
| 1338 | if (state->get() == ALLOCATED |
| 1339 | || state->get() == RELEASED |
| 1340 | || state->get() == STOPPING |
| 1341 | || state->get() == RELEASING) { |
| 1342 | // We're already stopped, released, or doing it right now. |
| 1343 | state.unlock(); |
| 1344 | mCallback->onStopCompleted(); |
| 1345 | state.lock(); |
| 1346 | return; |
| 1347 | } |
| 1348 | state->set(STOPPING); |
| 1349 | } |
| 1350 | |
| 1351 | mChannel->stop(); |
| 1352 | (new AMessage(kWhatStop, this))->post(); |
| 1353 | } |
| 1354 | |
| 1355 | void CCodec::stop() { |
| 1356 | std::shared_ptr<Codec2Client::Component> comp; |
| 1357 | { |
| 1358 | Mutexed<State>::Locked state(mState); |
| 1359 | if (state->get() == RELEASING) { |
| 1360 | state.unlock(); |
| 1361 | // We're already stopped or release is in progress. |
| 1362 | mCallback->onStopCompleted(); |
| 1363 | state.lock(); |
| 1364 | return; |
| 1365 | } else if (state->get() != STOPPING) { |
| 1366 | state.unlock(); |
| 1367 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 1368 | state.lock(); |
| 1369 | return; |
| 1370 | } |
| 1371 | comp = state->comp; |
| 1372 | } |
| 1373 | status_t err = comp->stop(); |
| 1374 | if (err != C2_OK) { |
| 1375 | // TODO: convert err into status_t |
| 1376 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 1377 | } |
| 1378 | |
| 1379 | { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1380 | Mutexed<Config>::Locked config(mConfig); |
| 1381 | if (config->mInputSurface) { |
| 1382 | config->mInputSurface->disconnect(); |
| 1383 | config->mInputSurface = nullptr; |
| 1384 | } |
| 1385 | } |
| 1386 | { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1387 | Mutexed<State>::Locked state(mState); |
| 1388 | if (state->get() == STOPPING) { |
| 1389 | state->set(ALLOCATED); |
| 1390 | } |
| 1391 | } |
| 1392 | mCallback->onStopCompleted(); |
| 1393 | } |
| 1394 | |
| 1395 | void CCodec::initiateRelease(bool sendCallback /* = true */) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1396 | bool clearInputSurfaceIfNeeded = false; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1397 | { |
| 1398 | Mutexed<State>::Locked state(mState); |
| 1399 | if (state->get() == RELEASED || state->get() == RELEASING) { |
| 1400 | // We're already released or doing it right now. |
| 1401 | if (sendCallback) { |
| 1402 | state.unlock(); |
| 1403 | mCallback->onReleaseCompleted(); |
| 1404 | state.lock(); |
| 1405 | } |
| 1406 | return; |
| 1407 | } |
| 1408 | if (state->get() == ALLOCATING) { |
| 1409 | state->set(RELEASING); |
| 1410 | // With the altered state allocate() would fail and clean up. |
| 1411 | if (sendCallback) { |
| 1412 | state.unlock(); |
| 1413 | mCallback->onReleaseCompleted(); |
| 1414 | state.lock(); |
| 1415 | } |
| 1416 | return; |
| 1417 | } |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1418 | if (state->get() == STARTING |
| 1419 | || state->get() == RUNNING |
| 1420 | || state->get() == STOPPING) { |
| 1421 | // Input surface may have been started, so clean up is needed. |
| 1422 | clearInputSurfaceIfNeeded = true; |
| 1423 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1424 | state->set(RELEASING); |
| 1425 | } |
| 1426 | |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1427 | if (clearInputSurfaceIfNeeded) { |
| 1428 | Mutexed<Config>::Locked config(mConfig); |
| 1429 | if (config->mInputSurface) { |
| 1430 | config->mInputSurface->disconnect(); |
| 1431 | config->mInputSurface = nullptr; |
| 1432 | } |
| 1433 | } |
| 1434 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1435 | mChannel->stop(); |
| 1436 | // thiz holds strong ref to this while the thread is running. |
| 1437 | sp<CCodec> thiz(this); |
| 1438 | std::thread([thiz, sendCallback] { thiz->release(sendCallback); }).detach(); |
| 1439 | } |
| 1440 | |
| 1441 | void CCodec::release(bool sendCallback) { |
| 1442 | std::shared_ptr<Codec2Client::Component> comp; |
| 1443 | { |
| 1444 | Mutexed<State>::Locked state(mState); |
| 1445 | if (state->get() == RELEASED) { |
| 1446 | if (sendCallback) { |
| 1447 | state.unlock(); |
| 1448 | mCallback->onReleaseCompleted(); |
| 1449 | state.lock(); |
| 1450 | } |
| 1451 | return; |
| 1452 | } |
| 1453 | comp = state->comp; |
| 1454 | } |
| 1455 | comp->release(); |
| 1456 | |
| 1457 | { |
| 1458 | Mutexed<State>::Locked state(mState); |
| 1459 | state->set(RELEASED); |
| 1460 | state->comp.reset(); |
| 1461 | } |
| 1462 | if (sendCallback) { |
| 1463 | mCallback->onReleaseCompleted(); |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | status_t CCodec::setSurface(const sp<Surface> &surface) { |
| 1468 | return mChannel->setSurface(surface); |
| 1469 | } |
| 1470 | |
| 1471 | void CCodec::signalFlush() { |
| 1472 | status_t err = [this] { |
| 1473 | Mutexed<State>::Locked state(mState); |
| 1474 | if (state->get() == FLUSHED) { |
| 1475 | return ALREADY_EXISTS; |
| 1476 | } |
| 1477 | if (state->get() != RUNNING) { |
| 1478 | return UNKNOWN_ERROR; |
| 1479 | } |
| 1480 | state->set(FLUSHING); |
| 1481 | return OK; |
| 1482 | }(); |
| 1483 | switch (err) { |
| 1484 | case ALREADY_EXISTS: |
| 1485 | mCallback->onFlushCompleted(); |
| 1486 | return; |
| 1487 | case OK: |
| 1488 | break; |
| 1489 | default: |
| 1490 | mCallback->onError(err, ACTION_CODE_FATAL); |
| 1491 | return; |
| 1492 | } |
| 1493 | |
| 1494 | mChannel->stop(); |
| 1495 | (new AMessage(kWhatFlush, this))->post(); |
| 1496 | } |
| 1497 | |
| 1498 | void CCodec::flush() { |
| 1499 | std::shared_ptr<Codec2Client::Component> comp; |
| 1500 | auto checkFlushing = [this, &comp] { |
| 1501 | Mutexed<State>::Locked state(mState); |
| 1502 | if (state->get() != FLUSHING) { |
| 1503 | return UNKNOWN_ERROR; |
| 1504 | } |
| 1505 | comp = state->comp; |
| 1506 | return OK; |
| 1507 | }; |
| 1508 | if (tryAndReportOnError(checkFlushing) != OK) { |
| 1509 | return; |
| 1510 | } |
| 1511 | |
| 1512 | std::list<std::unique_ptr<C2Work>> flushedWork; |
| 1513 | c2_status_t err = comp->flush(C2Component::FLUSH_COMPONENT, &flushedWork); |
| 1514 | { |
| 1515 | Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue); |
| 1516 | flushedWork.splice(flushedWork.end(), *queue); |
| 1517 | } |
| 1518 | if (err != C2_OK) { |
| 1519 | // TODO: convert err into status_t |
| 1520 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 1521 | } |
| 1522 | |
| 1523 | mChannel->flush(flushedWork); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1524 | |
| 1525 | { |
| 1526 | Mutexed<State>::Locked state(mState); |
Iris Chang | ce521ee | 2019-07-05 16:18:54 +0800 | [diff] [blame] | 1527 | if (state->get() == FLUSHING) { |
| 1528 | state->set(FLUSHED); |
| 1529 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1530 | } |
| 1531 | mCallback->onFlushCompleted(); |
| 1532 | } |
| 1533 | |
| 1534 | void CCodec::signalResume() { |
| 1535 | auto setResuming = [this] { |
| 1536 | Mutexed<State>::Locked state(mState); |
| 1537 | if (state->get() != FLUSHED) { |
| 1538 | return UNKNOWN_ERROR; |
| 1539 | } |
| 1540 | state->set(RESUMING); |
| 1541 | return OK; |
| 1542 | }; |
| 1543 | if (tryAndReportOnError(setResuming) != OK) { |
| 1544 | return; |
| 1545 | } |
| 1546 | |
| 1547 | (void)mChannel->start(nullptr, nullptr); |
| 1548 | |
| 1549 | { |
| 1550 | Mutexed<State>::Locked state(mState); |
| 1551 | if (state->get() != RESUMING) { |
| 1552 | state.unlock(); |
| 1553 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 1554 | state.lock(); |
| 1555 | return; |
| 1556 | } |
| 1557 | state->set(RUNNING); |
| 1558 | } |
| 1559 | |
| 1560 | (void)mChannel->requestInitialInputBuffers(); |
| 1561 | } |
| 1562 | |
Wonsik Kim | aa484ac | 2019-02-13 16:54:02 -0800 | [diff] [blame] | 1563 | void CCodec::signalSetParameters(const sp<AMessage> &msg) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1564 | std::shared_ptr<Codec2Client::Component> comp; |
| 1565 | auto checkState = [this, &comp] { |
| 1566 | Mutexed<State>::Locked state(mState); |
| 1567 | if (state->get() == RELEASED) { |
| 1568 | return INVALID_OPERATION; |
| 1569 | } |
| 1570 | comp = state->comp; |
| 1571 | return OK; |
| 1572 | }; |
| 1573 | if (tryAndReportOnError(checkState) != OK) { |
| 1574 | return; |
| 1575 | } |
| 1576 | |
Wonsik Kim | aa484ac | 2019-02-13 16:54:02 -0800 | [diff] [blame] | 1577 | // NOTE: We used to ignore "bitrate" at setParameters; replicate |
| 1578 | // the behavior here. |
| 1579 | sp<AMessage> params = msg; |
| 1580 | int32_t bitrate; |
| 1581 | if (params->findInt32(KEY_BIT_RATE, &bitrate)) { |
| 1582 | params = msg->dup(); |
| 1583 | params->removeEntryAt(params->findEntryByName(KEY_BIT_RATE)); |
| 1584 | } |
| 1585 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1586 | Mutexed<Config>::Locked config(mConfig); |
| 1587 | |
| 1588 | /** |
| 1589 | * Handle input surface parameters |
| 1590 | */ |
| 1591 | if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE)) |
| 1592 | && (config->mDomain & Config::IS_ENCODER) && config->mInputSurface && config->mISConfig) { |
Chong Zhang | 038e8f8 | 2019-02-06 19:05:14 -0800 | [diff] [blame] | 1593 | (void)params->findInt64(PARAMETER_KEY_OFFSET_TIME, &config->mISConfig->mTimeOffsetUs); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1594 | |
| 1595 | if (params->findInt64("skip-frames-before", &config->mISConfig->mStartAtUs)) { |
| 1596 | config->mISConfig->mStopped = false; |
| 1597 | } else if (params->findInt64("stop-time-us", &config->mISConfig->mStopAtUs)) { |
| 1598 | config->mISConfig->mStopped = true; |
| 1599 | } |
| 1600 | |
| 1601 | int32_t value; |
Chong Zhang | 038e8f8 | 2019-02-06 19:05:14 -0800 | [diff] [blame] | 1602 | if (params->findInt32(PARAMETER_KEY_SUSPEND, &value)) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1603 | config->mISConfig->mSuspended = value; |
| 1604 | config->mISConfig->mSuspendAtUs = -1; |
Chong Zhang | 038e8f8 | 2019-02-06 19:05:14 -0800 | [diff] [blame] | 1605 | (void)params->findInt64(PARAMETER_KEY_SUSPEND_TIME, &config->mISConfig->mSuspendAtUs); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | (void)config->mInputSurface->configure(*config->mISConfig); |
| 1609 | if (config->mISConfig->mStopped) { |
| 1610 | config->mInputFormat->setInt64( |
| 1611 | "android._stop-time-offset-us", config->mISConfig->mInputDelayUs); |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | std::vector<std::unique_ptr<C2Param>> configUpdate; |
| 1616 | (void)config->getConfigUpdateFromSdkParams( |
| 1617 | comp, params, Config::IS_PARAM, C2_MAY_BLOCK, &configUpdate); |
| 1618 | // Prefer to pass parameters to the buffer channel, so they can be synchronized with the frames. |
| 1619 | // Parameter synchronization is not defined when using input surface. For now, route |
| 1620 | // these directly to the component. |
| 1621 | if (config->mInputSurface == nullptr |
| 1622 | && (property_get_bool("debug.stagefright.ccodec_delayed_params", false) |
| 1623 | || comp->getName().find("c2.android.") == 0)) { |
| 1624 | mChannel->setParameters(configUpdate); |
| 1625 | } else { |
| 1626 | (void)config->setParameters(comp, configUpdate, C2_MAY_BLOCK); |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | void CCodec::signalEndOfInputStream() { |
| 1631 | mCallback->onSignaledInputEOS(mChannel->signalEndOfInputStream()); |
| 1632 | } |
| 1633 | |
| 1634 | void CCodec::signalRequestIDRFrame() { |
| 1635 | std::shared_ptr<Codec2Client::Component> comp; |
| 1636 | { |
| 1637 | Mutexed<State>::Locked state(mState); |
| 1638 | if (state->get() == RELEASED) { |
| 1639 | ALOGD("no IDR request sent since component is released"); |
| 1640 | return; |
| 1641 | } |
| 1642 | comp = state->comp; |
| 1643 | } |
| 1644 | ALOGV("request IDR"); |
| 1645 | Mutexed<Config>::Locked config(mConfig); |
| 1646 | std::vector<std::unique_ptr<C2Param>> params; |
| 1647 | params.push_back( |
| 1648 | std::make_unique<C2StreamRequestSyncFrameTuning::output>(0u, true)); |
| 1649 | config->setParameters(comp, params, C2_MAY_BLOCK); |
| 1650 | } |
| 1651 | |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1652 | void CCodec::onWorkDone(std::list<std::unique_ptr<C2Work>> &workItems) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1653 | if (!workItems.empty()) { |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1654 | Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue); |
| 1655 | queue->splice(queue->end(), workItems); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1656 | } |
| 1657 | (new AMessage(kWhatWorkDone, this))->post(); |
| 1658 | } |
| 1659 | |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1660 | void CCodec::onInputBufferDone(uint64_t frameIndex, size_t arrayIndex) { |
| 1661 | mChannel->onInputBufferDone(frameIndex, arrayIndex); |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 1662 | if (arrayIndex == 0) { |
| 1663 | // We always put no more than one buffer per work, if we use an input surface. |
| 1664 | Mutexed<Config>::Locked config(mConfig); |
| 1665 | if (config->mInputSurface) { |
| 1666 | config->mInputSurface->onInputBufferDone(frameIndex); |
| 1667 | } |
| 1668 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | void CCodec::onMessageReceived(const sp<AMessage> &msg) { |
| 1672 | TimePoint now = std::chrono::steady_clock::now(); |
| 1673 | CCodecWatchdog::getInstance()->watch(this); |
| 1674 | switch (msg->what()) { |
| 1675 | case kWhatAllocate: { |
| 1676 | // C2ComponentStore::createComponent() should return within 100ms. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1677 | setDeadline(now, 1500ms, "allocate"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1678 | sp<RefBase> obj; |
| 1679 | CHECK(msg->findObject("codecInfo", &obj)); |
| 1680 | allocate((MediaCodecInfo *)obj.get()); |
| 1681 | break; |
| 1682 | } |
| 1683 | case kWhatConfigure: { |
| 1684 | // C2Component::commit_sm() should return within 5ms. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1685 | setDeadline(now, 1500ms, "configure"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1686 | sp<AMessage> format; |
| 1687 | CHECK(msg->findMessage("format", &format)); |
| 1688 | configure(format); |
| 1689 | break; |
| 1690 | } |
| 1691 | case kWhatStart: { |
| 1692 | // C2Component::start() should return within 500ms. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1693 | setDeadline(now, 1500ms, "start"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1694 | start(); |
| 1695 | break; |
| 1696 | } |
| 1697 | case kWhatStop: { |
| 1698 | // C2Component::stop() should return within 500ms. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1699 | setDeadline(now, 1500ms, "stop"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1700 | stop(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1701 | break; |
| 1702 | } |
| 1703 | case kWhatFlush: { |
| 1704 | // C2Component::flush_sm() should return within 5ms. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1705 | setDeadline(now, 1500ms, "flush"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1706 | flush(); |
| 1707 | break; |
| 1708 | } |
| 1709 | case kWhatCreateInputSurface: { |
| 1710 | // Surface operations may be briefly blocking. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1711 | setDeadline(now, 1500ms, "createInputSurface"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1712 | createInputSurface(); |
| 1713 | break; |
| 1714 | } |
| 1715 | case kWhatSetInputSurface: { |
| 1716 | // Surface operations may be briefly blocking. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1717 | setDeadline(now, 1500ms, "setInputSurface"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1718 | sp<RefBase> obj; |
| 1719 | CHECK(msg->findObject("surface", &obj)); |
| 1720 | sp<PersistentSurface> surface(static_cast<PersistentSurface *>(obj.get())); |
| 1721 | setInputSurface(surface); |
| 1722 | break; |
| 1723 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1724 | case kWhatWorkDone: { |
| 1725 | std::unique_ptr<C2Work> work; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1726 | bool shouldPost = false; |
| 1727 | { |
| 1728 | Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue); |
| 1729 | if (queue->empty()) { |
| 1730 | break; |
| 1731 | } |
| 1732 | work.swap(queue->front()); |
| 1733 | queue->pop_front(); |
| 1734 | shouldPost = !queue->empty(); |
| 1735 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1736 | if (shouldPost) { |
| 1737 | (new AMessage(kWhatWorkDone, this))->post(); |
| 1738 | } |
| 1739 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1740 | // handle configuration changes in work done |
| 1741 | Mutexed<Config>::Locked config(mConfig); |
| 1742 | bool changed = false; |
| 1743 | Config::Watcher<C2StreamInitDataInfo::output> initData = |
| 1744 | config->watch<C2StreamInitDataInfo::output>(); |
| 1745 | if (!work->worklets.empty() |
| 1746 | && (work->worklets.front()->output.flags |
| 1747 | & C2FrameData::FLAG_DISCARD_FRAME) == 0) { |
| 1748 | |
| 1749 | // copy buffer info to config |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1750 | std::vector<std::unique_ptr<C2Param>> updates; |
| 1751 | for (const std::unique_ptr<C2Param> ¶m |
| 1752 | : work->worklets.front()->output.configUpdate) { |
| 1753 | updates.push_back(C2Param::Copy(*param)); |
| 1754 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1755 | unsigned stream = 0; |
| 1756 | for (const std::shared_ptr<C2Buffer> &buf : work->worklets.front()->output.buffers) { |
| 1757 | for (const std::shared_ptr<const C2Info> &info : buf->info()) { |
| 1758 | // move all info into output-stream #0 domain |
| 1759 | updates.emplace_back(C2Param::CopyAsStream(*info, true /* output */, stream)); |
| 1760 | } |
| 1761 | for (const C2ConstGraphicBlock &block : buf->data().graphicBlocks()) { |
| 1762 | // ALOGV("got output buffer with crop %u,%u+%u,%u and size %u,%u", |
| 1763 | // block.crop().left, block.crop().top, |
| 1764 | // block.crop().width, block.crop().height, |
| 1765 | // block.width(), block.height()); |
| 1766 | updates.emplace_back(new C2StreamCropRectInfo::output(stream, block.crop())); |
| 1767 | updates.emplace_back(new C2StreamPictureSizeInfo::output( |
Harish Mahendrakar | f7c49e2 | 2019-05-24 14:19:16 -0700 | [diff] [blame] | 1768 | stream, block.crop().width, block.crop().height)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1769 | break; // for now only do the first block |
| 1770 | } |
| 1771 | ++stream; |
| 1772 | } |
| 1773 | |
| 1774 | changed = config->updateConfiguration(updates, config->mOutputDomain); |
| 1775 | |
| 1776 | // copy standard infos to graphic buffers if not already present (otherwise, we |
| 1777 | // may overwrite the actual intermediate value with a final value) |
| 1778 | stream = 0; |
| 1779 | const static std::vector<C2Param::Index> stdGfxInfos = { |
| 1780 | C2StreamRotationInfo::output::PARAM_TYPE, |
| 1781 | C2StreamColorAspectsInfo::output::PARAM_TYPE, |
| 1782 | C2StreamDataSpaceInfo::output::PARAM_TYPE, |
| 1783 | C2StreamHdrStaticInfo::output::PARAM_TYPE, |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 1784 | C2StreamHdr10PlusInfo::output::PARAM_TYPE, |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1785 | C2StreamPixelAspectRatioInfo::output::PARAM_TYPE, |
| 1786 | C2StreamSurfaceScalingInfo::output::PARAM_TYPE |
| 1787 | }; |
| 1788 | for (const std::shared_ptr<C2Buffer> &buf : work->worklets.front()->output.buffers) { |
| 1789 | if (buf->data().graphicBlocks().size()) { |
| 1790 | for (C2Param::Index ix : stdGfxInfos) { |
| 1791 | if (!buf->hasInfo(ix)) { |
| 1792 | const C2Param *param = |
| 1793 | config->getConfigParameterValue(ix.withStream(stream)); |
| 1794 | if (param) { |
| 1795 | std::shared_ptr<C2Param> info(C2Param::Copy(*param)); |
| 1796 | buf->setInfo(std::static_pointer_cast<C2Info>(info)); |
| 1797 | } |
| 1798 | } |
| 1799 | } |
| 1800 | } |
| 1801 | ++stream; |
| 1802 | } |
| 1803 | } |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 1804 | if (config->mInputSurface) { |
| 1805 | config->mInputSurface->onInputBufferDone(work->input.ordinal.frameIndex); |
| 1806 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1807 | mChannel->onWorkDone( |
| 1808 | std::move(work), changed ? config->mOutputFormat : nullptr, |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1809 | initData.hasChanged() ? initData.update().get() : nullptr); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1810 | break; |
| 1811 | } |
| 1812 | case kWhatWatch: { |
| 1813 | // watch message already posted; no-op. |
| 1814 | break; |
| 1815 | } |
| 1816 | default: { |
| 1817 | ALOGE("unrecognized message"); |
| 1818 | break; |
| 1819 | } |
| 1820 | } |
| 1821 | setDeadline(TimePoint::max(), 0ms, "none"); |
| 1822 | } |
| 1823 | |
| 1824 | void CCodec::setDeadline( |
| 1825 | const TimePoint &now, |
| 1826 | const std::chrono::milliseconds &timeout, |
| 1827 | const char *name) { |
| 1828 | int32_t mult = std::max(1, property_get_int32("debug.stagefright.ccodec_timeout_mult", 1)); |
| 1829 | Mutexed<NamedTimePoint>::Locked deadline(mDeadline); |
| 1830 | deadline->set(now + (timeout * mult), name); |
| 1831 | } |
| 1832 | |
| 1833 | void CCodec::initiateReleaseIfStuck() { |
| 1834 | std::string name; |
| 1835 | bool pendingDeadline = false; |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1836 | { |
| 1837 | Mutexed<NamedTimePoint>::Locked deadline(mDeadline); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1838 | if (deadline->get() < std::chrono::steady_clock::now()) { |
| 1839 | name = deadline->getName(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1840 | } |
| 1841 | if (deadline->get() != TimePoint::max()) { |
| 1842 | pendingDeadline = true; |
| 1843 | } |
| 1844 | } |
| 1845 | if (name.empty()) { |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1846 | constexpr std::chrono::steady_clock::duration kWorkDurationThreshold = 3s; |
| 1847 | std::chrono::steady_clock::duration elapsed = mChannel->elapsed(); |
| 1848 | if (elapsed >= kWorkDurationThreshold) { |
| 1849 | name = "queue"; |
| 1850 | } |
| 1851 | if (elapsed > 0s) { |
| 1852 | pendingDeadline = true; |
| 1853 | } |
| 1854 | } |
| 1855 | if (name.empty()) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1856 | // We're not stuck. |
| 1857 | if (pendingDeadline) { |
| 1858 | // If we are not stuck yet but still has deadline coming up, |
| 1859 | // post watch message to check back later. |
| 1860 | (new AMessage(kWhatWatch, this))->post(); |
| 1861 | } |
| 1862 | return; |
| 1863 | } |
| 1864 | |
| 1865 | ALOGW("previous call to %s exceeded timeout", name.c_str()); |
| 1866 | initiateRelease(false); |
| 1867 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 1868 | } |
| 1869 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1870 | } // namespace android |
| 1871 | |
| 1872 | extern "C" android::CodecBase *CreateCodec() { |
| 1873 | return new android::CCodec; |
| 1874 | } |
| 1875 | |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 1876 | // Create Codec 2.0 input surface |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1877 | extern "C" android::PersistentSurface *CreateInputSurface() { |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 1878 | using namespace android; |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1879 | using ::android::hardware::media::omx::V1_0::implementation::TWGraphicBufferSource; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1880 | // Attempt to create a Codec2's input surface. |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 1881 | std::shared_ptr<Codec2Client::InputSurface> inputSurface = |
| 1882 | Codec2Client::CreateInputSurface(); |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 1883 | if (!inputSurface) { |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 1884 | if (property_get_int32("debug.stagefright.c2inputsurface", 0) == -1) { |
| 1885 | sp<IGraphicBufferProducer> gbp; |
| 1886 | sp<OmxGraphicBufferSource> gbs = new OmxGraphicBufferSource(); |
| 1887 | status_t err = gbs->initCheck(); |
| 1888 | if (err != OK) { |
| 1889 | ALOGE("Failed to create persistent input surface: error %d", err); |
| 1890 | return nullptr; |
| 1891 | } |
| 1892 | return new PersistentSurface( |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1893 | gbs->getIGraphicBufferProducer(), new TWGraphicBufferSource(gbs)); |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 1894 | } else { |
| 1895 | return nullptr; |
| 1896 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1897 | } |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 1898 | return new PersistentSurface( |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 1899 | inputSurface->getGraphicBufferProducer(), |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 1900 | static_cast<sp<android::hidl::base::V1_0::IBase>>( |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 1901 | inputSurface->getHalInterface())); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1902 | } |
| 1903 | |