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