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