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