Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "CCodec" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <sstream> |
| 22 | #include <thread> |
| 23 | |
| 24 | #include <C2Config.h> |
| 25 | #include <C2Debug.h> |
| 26 | #include <C2ParamInternal.h> |
| 27 | #include <C2PlatformSupport.h> |
| 28 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 29 | #include <android/IOMXBufferSource.h> |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 30 | #include <android/hardware/media/c2/1.0/IInputSurface.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 31 | #include <android/hardware/media/omx/1.0/IGraphicBufferSource.h> |
| 32 | #include <android/hardware/media/omx/1.0/IOmx.h> |
| 33 | #include <android-base/stringprintf.h> |
| 34 | #include <cutils/properties.h> |
| 35 | #include <gui/IGraphicBufferProducer.h> |
| 36 | #include <gui/Surface.h> |
| 37 | #include <gui/bufferqueue/1.0/H2BGraphicBufferProducer.h> |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 38 | #include <media/omx/1.0/WOmxNode.h> |
| 39 | #include <media/openmax/OMX_Core.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 40 | #include <media/openmax/OMX_IndexExt.h> |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 41 | #include <media/stagefright/omx/1.0/WGraphicBufferSource.h> |
| 42 | #include <media/stagefright/omx/OmxGraphicBufferSource.h> |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 43 | #include <media/stagefright/CCodec.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 44 | #include <media/stagefright/BufferProducerWrapper.h> |
| 45 | #include <media/stagefright/MediaCodecConstants.h> |
| 46 | #include <media/stagefright/PersistentSurface.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 47 | |
| 48 | #include "C2OMXNode.h" |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 49 | #include "CCodecBufferChannel.h" |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 50 | #include "CCodecConfig.h" |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 51 | #include "Codec2Mapper.h" |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 52 | #include "InputSurfaceWrapper.h" |
| 53 | |
| 54 | extern "C" android::PersistentSurface *CreateInputSurface(); |
| 55 | |
| 56 | namespace android { |
| 57 | |
| 58 | using namespace std::chrono_literals; |
| 59 | using ::android::hardware::graphics::bufferqueue::V1_0::utils::H2BGraphicBufferProducer; |
| 60 | using android::base::StringPrintf; |
Pawin Vongmasa | d0f0e14 | 2018-11-15 03:36:28 -0800 | [diff] [blame] | 61 | using ::android::hardware::media::c2::V1_0::IInputSurface; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 62 | |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 63 | typedef hardware::media::omx::V1_0::IGraphicBufferSource HGraphicBufferSource; |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 64 | typedef CCodecConfig Config; |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 65 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 66 | namespace { |
| 67 | |
| 68 | class CCodecWatchdog : public AHandler { |
| 69 | private: |
| 70 | enum { |
| 71 | kWhatWatch, |
| 72 | }; |
| 73 | constexpr static int64_t kWatchIntervalUs = 3300000; // 3.3 secs |
| 74 | |
| 75 | public: |
| 76 | static sp<CCodecWatchdog> getInstance() { |
| 77 | static sp<CCodecWatchdog> instance(new CCodecWatchdog); |
| 78 | static std::once_flag flag; |
| 79 | // Call Init() only once. |
| 80 | std::call_once(flag, Init, instance); |
| 81 | return instance; |
| 82 | } |
| 83 | |
| 84 | ~CCodecWatchdog() = default; |
| 85 | |
| 86 | void watch(sp<CCodec> codec) { |
| 87 | bool shouldPost = false; |
| 88 | { |
| 89 | Mutexed<std::set<wp<CCodec>>>::Locked codecs(mCodecsToWatch); |
| 90 | // If a watch message is in flight, piggy-back this instance as well. |
| 91 | // Otherwise, post a new watch message. |
| 92 | shouldPost = codecs->empty(); |
| 93 | codecs->emplace(codec); |
| 94 | } |
| 95 | if (shouldPost) { |
| 96 | ALOGV("posting watch message"); |
| 97 | (new AMessage(kWhatWatch, this))->post(kWatchIntervalUs); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | protected: |
| 102 | void onMessageReceived(const sp<AMessage> &msg) { |
| 103 | switch (msg->what()) { |
| 104 | case kWhatWatch: { |
| 105 | Mutexed<std::set<wp<CCodec>>>::Locked codecs(mCodecsToWatch); |
| 106 | ALOGV("watch for %zu codecs", codecs->size()); |
| 107 | for (auto it = codecs->begin(); it != codecs->end(); ++it) { |
| 108 | sp<CCodec> codec = it->promote(); |
| 109 | if (codec == nullptr) { |
| 110 | continue; |
| 111 | } |
| 112 | codec->initiateReleaseIfStuck(); |
| 113 | } |
| 114 | codecs->clear(); |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | default: { |
| 119 | TRESPASS("CCodecWatchdog: unrecognized message"); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | private: |
| 125 | CCodecWatchdog() : mLooper(new ALooper) {} |
| 126 | |
| 127 | static void Init(const sp<CCodecWatchdog> &thiz) { |
| 128 | ALOGV("Init"); |
| 129 | thiz->mLooper->setName("CCodecWatchdog"); |
| 130 | thiz->mLooper->registerHandler(thiz); |
| 131 | thiz->mLooper->start(); |
| 132 | } |
| 133 | |
| 134 | sp<ALooper> mLooper; |
| 135 | |
| 136 | Mutexed<std::set<wp<CCodec>>> mCodecsToWatch; |
| 137 | }; |
| 138 | |
| 139 | class C2InputSurfaceWrapper : public InputSurfaceWrapper { |
| 140 | public: |
| 141 | explicit C2InputSurfaceWrapper( |
| 142 | const std::shared_ptr<Codec2Client::InputSurface> &surface) : |
| 143 | mSurface(surface) { |
| 144 | } |
| 145 | |
| 146 | ~C2InputSurfaceWrapper() override = default; |
| 147 | |
| 148 | status_t connect(const std::shared_ptr<Codec2Client::Component> &comp) override { |
| 149 | if (mConnection != nullptr) { |
| 150 | return ALREADY_EXISTS; |
| 151 | } |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 152 | return toStatusT(comp->connectToInputSurface(mSurface, &mConnection)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void disconnect() override { |
| 156 | if (mConnection != nullptr) { |
| 157 | mConnection->disconnect(); |
| 158 | mConnection = nullptr; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | status_t start() override { |
| 163 | // InputSurface does not distinguish started state |
| 164 | return OK; |
| 165 | } |
| 166 | |
| 167 | status_t signalEndOfInputStream() override { |
| 168 | C2InputSurfaceEosTuning eos(true); |
| 169 | std::vector<std::unique_ptr<C2SettingResult>> failures; |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 170 | c2_status_t err = mSurface->config({&eos}, C2_MAY_BLOCK, &failures); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 171 | if (err != C2_OK) { |
| 172 | return UNKNOWN_ERROR; |
| 173 | } |
| 174 | return OK; |
| 175 | } |
| 176 | |
| 177 | status_t configure(Config &config __unused) { |
| 178 | // TODO |
| 179 | return OK; |
| 180 | } |
| 181 | |
| 182 | private: |
| 183 | std::shared_ptr<Codec2Client::InputSurface> mSurface; |
| 184 | std::shared_ptr<Codec2Client::InputSurfaceConnection> mConnection; |
| 185 | }; |
| 186 | |
| 187 | class GraphicBufferSourceWrapper : public InputSurfaceWrapper { |
| 188 | public: |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 189 | typedef hardware::media::omx::V1_0::Status OmxStatus; |
| 190 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 191 | GraphicBufferSourceWrapper( |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 192 | const sp<HGraphicBufferSource> &source, |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 193 | uint32_t width, |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 194 | uint32_t height, |
| 195 | uint64_t usage) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 196 | : mSource(source), mWidth(width), mHeight(height) { |
| 197 | mDataSpace = HAL_DATASPACE_BT709; |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 198 | mConfig.mUsage = usage; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 199 | } |
| 200 | ~GraphicBufferSourceWrapper() override = default; |
| 201 | |
| 202 | status_t connect(const std::shared_ptr<Codec2Client::Component> &comp) override { |
| 203 | mNode = new C2OMXNode(comp); |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 204 | mOmxNode = new hardware::media::omx::V1_0::utils::TWOmxNode(mNode); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 205 | mNode->setFrameSize(mWidth, mHeight); |
| 206 | |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 207 | // Usage is queried during configure(), so setting it beforehand. |
| 208 | OMX_U32 usage = mConfig.mUsage & 0xFFFFFFFF; |
| 209 | (void)mNode->setParameter( |
| 210 | (OMX_INDEXTYPE)OMX_IndexParamConsumerUsageBits, |
| 211 | &usage, sizeof(usage)); |
| 212 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 213 | // NOTE: we do not use/pass through color aspects from GraphicBufferSource as we |
| 214 | // communicate that directly to the component. |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 215 | mSource->configure( |
| 216 | mOmxNode, static_cast<hardware::graphics::common::V1_0::Dataspace>(mDataSpace)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 217 | return OK; |
| 218 | } |
| 219 | |
| 220 | void disconnect() override { |
| 221 | if (mNode == nullptr) { |
| 222 | return; |
| 223 | } |
| 224 | sp<IOMXBufferSource> source = mNode->getSource(); |
| 225 | if (source == nullptr) { |
| 226 | ALOGD("GBSWrapper::disconnect: node is not configured with OMXBufferSource."); |
| 227 | return; |
| 228 | } |
| 229 | source->onOmxIdle(); |
| 230 | source->onOmxLoaded(); |
| 231 | mNode.clear(); |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 232 | mOmxNode.clear(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 235 | status_t GetStatus(hardware::Return<OmxStatus> &&status) { |
| 236 | if (status.isOk()) { |
| 237 | return static_cast<status_t>(status.withDefault(OmxStatus::UNKNOWN_ERROR)); |
| 238 | } else if (status.isDeadObject()) { |
| 239 | return DEAD_OBJECT; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 240 | } |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 241 | return UNKNOWN_ERROR; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | status_t start() override { |
| 245 | sp<IOMXBufferSource> source = mNode->getSource(); |
| 246 | if (source == nullptr) { |
| 247 | return NO_INIT; |
| 248 | } |
Taehwan Kim | 8b3bcdd | 2020-11-26 22:40:40 +0900 | [diff] [blame] | 249 | |
Wonsik Kim | 0f6b61d | 2021-01-05 18:55:22 -0800 | [diff] [blame] | 250 | size_t numSlots = 16; |
| 251 | // WORKAROUND: having more slots improve performance while consuming |
| 252 | // more memory. This is a temporary workaround to reduce memory for |
| 253 | // larger-than-4K scenario. |
| 254 | if (mWidth * mHeight > 4096 * 2340) { |
| 255 | constexpr OMX_U32 kPortIndexInput = 0; |
Taehwan Kim | 8b3bcdd | 2020-11-26 22:40:40 +0900 | [diff] [blame] | 256 | |
Wonsik Kim | 0f6b61d | 2021-01-05 18:55:22 -0800 | [diff] [blame] | 257 | OMX_PARAM_PORTDEFINITIONTYPE param; |
| 258 | param.nPortIndex = kPortIndexInput; |
| 259 | status_t err = mNode->getParameter(OMX_IndexParamPortDefinition, |
| 260 | ¶m, sizeof(param)); |
| 261 | if (err == OK) { |
| 262 | numSlots = param.nBufferCountActual; |
| 263 | } |
Taehwan Kim | 8b3bcdd | 2020-11-26 22:40:40 +0900 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | for (size_t i = 0; i < numSlots; ++i) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 267 | source->onInputBufferAdded(i); |
| 268 | } |
| 269 | |
| 270 | source->onOmxExecuting(); |
| 271 | return OK; |
| 272 | } |
| 273 | |
| 274 | status_t signalEndOfInputStream() override { |
| 275 | return GetStatus(mSource->signalEndOfInputStream()); |
| 276 | } |
| 277 | |
| 278 | status_t configure(Config &config) { |
| 279 | std::stringstream status; |
| 280 | status_t err = OK; |
| 281 | |
| 282 | // handle each configuration granually, in case we need to handle part of the configuration |
| 283 | // elsewhere |
| 284 | |
| 285 | // TRICKY: we do not unset frame delay repeating |
| 286 | if (config.mMinFps > 0 && config.mMinFps != mConfig.mMinFps) { |
| 287 | int64_t us = 1e6 / config.mMinFps + 0.5; |
| 288 | status_t res = GetStatus(mSource->setRepeatPreviousFrameDelayUs(us)); |
| 289 | status << " minFps=" << config.mMinFps << " => repeatDelayUs=" << us; |
| 290 | if (res != OK) { |
| 291 | status << " (=> " << asString(res) << ")"; |
| 292 | err = res; |
| 293 | } |
| 294 | mConfig.mMinFps = config.mMinFps; |
| 295 | } |
| 296 | |
| 297 | // pts gap |
| 298 | if (config.mMinAdjustedFps > 0 || config.mFixedAdjustedFps > 0) { |
| 299 | if (mNode != nullptr) { |
| 300 | OMX_PARAM_U32TYPE ptrGapParam = {}; |
| 301 | ptrGapParam.nSize = sizeof(OMX_PARAM_U32TYPE); |
Wonsik Kim | 95ba016 | 2019-03-19 15:51:54 -0700 | [diff] [blame] | 302 | float gap = (config.mMinAdjustedFps > 0) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 303 | ? c2_min(INT32_MAX + 0., 1e6 / config.mMinAdjustedFps + 0.5) |
| 304 | : c2_max(0. - INT32_MAX, -1e6 / config.mFixedAdjustedFps - 0.5); |
Wonsik Kim | 95ba016 | 2019-03-19 15:51:54 -0700 | [diff] [blame] | 305 | // float -> uint32_t is undefined if the value is negative. |
| 306 | // First convert to int32_t to ensure the expected behavior. |
| 307 | ptrGapParam.nU32 = int32_t(gap); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 308 | (void)mNode->setParameter( |
| 309 | (OMX_INDEXTYPE)OMX_IndexParamMaxFrameDurationForBitrateControl, |
| 310 | &ptrGapParam, sizeof(ptrGapParam)); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // max fps |
| 315 | // 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] | 316 | if ((config.mMaxFps > 0 || (config.mFixedAdjustedFps > 0 && config.mMaxFps == -1)) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 317 | && config.mMaxFps != mConfig.mMaxFps) { |
| 318 | status_t res = GetStatus(mSource->setMaxFps(config.mMaxFps)); |
| 319 | status << " maxFps=" << config.mMaxFps; |
| 320 | if (res != OK) { |
| 321 | status << " (=> " << asString(res) << ")"; |
| 322 | err = res; |
| 323 | } |
| 324 | mConfig.mMaxFps = config.mMaxFps; |
| 325 | } |
| 326 | |
| 327 | if (config.mTimeOffsetUs != mConfig.mTimeOffsetUs) { |
| 328 | status_t res = GetStatus(mSource->setTimeOffsetUs(config.mTimeOffsetUs)); |
| 329 | status << " timeOffset " << config.mTimeOffsetUs << "us"; |
| 330 | if (res != OK) { |
| 331 | status << " (=> " << asString(res) << ")"; |
| 332 | err = res; |
| 333 | } |
| 334 | mConfig.mTimeOffsetUs = config.mTimeOffsetUs; |
| 335 | } |
| 336 | |
| 337 | if (config.mCaptureFps != mConfig.mCaptureFps || config.mCodedFps != mConfig.mCodedFps) { |
| 338 | status_t res = |
| 339 | GetStatus(mSource->setTimeLapseConfig(config.mCodedFps, config.mCaptureFps)); |
| 340 | status << " timeLapse " << config.mCaptureFps << "fps as " << config.mCodedFps << "fps"; |
| 341 | if (res != OK) { |
| 342 | status << " (=> " << asString(res) << ")"; |
| 343 | err = res; |
| 344 | } |
| 345 | mConfig.mCaptureFps = config.mCaptureFps; |
| 346 | mConfig.mCodedFps = config.mCodedFps; |
| 347 | } |
| 348 | |
| 349 | if (config.mStartAtUs != mConfig.mStartAtUs |
| 350 | || (config.mStopped != mConfig.mStopped && !config.mStopped)) { |
| 351 | status_t res = GetStatus(mSource->setStartTimeUs(config.mStartAtUs)); |
| 352 | status << " start at " << config.mStartAtUs << "us"; |
| 353 | if (res != OK) { |
| 354 | status << " (=> " << asString(res) << ")"; |
| 355 | err = res; |
| 356 | } |
| 357 | mConfig.mStartAtUs = config.mStartAtUs; |
| 358 | mConfig.mStopped = config.mStopped; |
| 359 | } |
| 360 | |
| 361 | // suspend-resume |
| 362 | if (config.mSuspended != mConfig.mSuspended) { |
| 363 | status_t res = GetStatus(mSource->setSuspend(config.mSuspended, config.mSuspendAtUs)); |
| 364 | status << " " << (config.mSuspended ? "suspend" : "resume") |
| 365 | << " at " << config.mSuspendAtUs << "us"; |
| 366 | if (res != OK) { |
| 367 | status << " (=> " << asString(res) << ")"; |
| 368 | err = res; |
| 369 | } |
| 370 | mConfig.mSuspended = config.mSuspended; |
| 371 | mConfig.mSuspendAtUs = config.mSuspendAtUs; |
| 372 | } |
| 373 | |
| 374 | if (config.mStopped != mConfig.mStopped && config.mStopped) { |
| 375 | status_t res = GetStatus(mSource->setStopTimeUs(config.mStopAtUs)); |
| 376 | status << " stop at " << config.mStopAtUs << "us"; |
| 377 | if (res != OK) { |
| 378 | status << " (=> " << asString(res) << ")"; |
| 379 | err = res; |
| 380 | } else { |
| 381 | status << " delayUs"; |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 382 | hardware::Return<void> trans = mSource->getStopTimeOffsetUs( |
| 383 | [&res, &delayUs = config.mInputDelayUs]( |
| 384 | auto status, auto stopTimeOffsetUs) { |
| 385 | res = static_cast<status_t>(status); |
| 386 | delayUs = stopTimeOffsetUs; |
| 387 | }); |
| 388 | if (!trans.isOk()) { |
| 389 | res = trans.isDeadObject() ? DEAD_OBJECT : UNKNOWN_ERROR; |
| 390 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 391 | if (res != OK) { |
| 392 | status << " (=> " << asString(res) << ")"; |
| 393 | } else { |
| 394 | status << "=" << config.mInputDelayUs << "us"; |
| 395 | } |
| 396 | mConfig.mInputDelayUs = config.mInputDelayUs; |
| 397 | } |
| 398 | mConfig.mStopAtUs = config.mStopAtUs; |
| 399 | mConfig.mStopped = config.mStopped; |
| 400 | } |
| 401 | |
| 402 | // color aspects (android._color-aspects) |
| 403 | |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 404 | // consumer usage is queried earlier. |
| 405 | |
Wonsik Kim | bd55793 | 2019-07-02 15:51:20 -0700 | [diff] [blame] | 406 | if (status.str().empty()) { |
| 407 | ALOGD("ISConfig not changed"); |
| 408 | } else { |
| 409 | ALOGD("ISConfig%s", status.str().c_str()); |
| 410 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 411 | return err; |
| 412 | } |
| 413 | |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 414 | void onInputBufferDone(c2_cntr64_t index) override { |
| 415 | mNode->onInputBufferDone(index); |
| 416 | } |
| 417 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 418 | private: |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 419 | sp<HGraphicBufferSource> mSource; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 420 | sp<C2OMXNode> mNode; |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 421 | sp<hardware::media::omx::V1_0::IOmxNode> mOmxNode; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 422 | uint32_t mWidth; |
| 423 | uint32_t mHeight; |
| 424 | Config mConfig; |
| 425 | }; |
| 426 | |
| 427 | class Codec2ClientInterfaceWrapper : public C2ComponentStore { |
| 428 | std::shared_ptr<Codec2Client> mClient; |
| 429 | |
| 430 | public: |
| 431 | Codec2ClientInterfaceWrapper(std::shared_ptr<Codec2Client> client) |
| 432 | : mClient(client) { } |
| 433 | |
| 434 | virtual ~Codec2ClientInterfaceWrapper() = default; |
| 435 | |
| 436 | virtual c2_status_t config_sm( |
| 437 | const std::vector<C2Param *> ¶ms, |
| 438 | std::vector<std::unique_ptr<C2SettingResult>> *const failures) { |
| 439 | return mClient->config(params, C2_MAY_BLOCK, failures); |
| 440 | }; |
| 441 | |
| 442 | virtual c2_status_t copyBuffer( |
| 443 | std::shared_ptr<C2GraphicBuffer>, |
| 444 | std::shared_ptr<C2GraphicBuffer>) { |
| 445 | return C2_OMITTED; |
| 446 | } |
| 447 | |
| 448 | virtual c2_status_t createComponent( |
| 449 | C2String, std::shared_ptr<C2Component> *const component) { |
| 450 | component->reset(); |
| 451 | return C2_OMITTED; |
| 452 | } |
| 453 | |
| 454 | virtual c2_status_t createInterface( |
| 455 | C2String, std::shared_ptr<C2ComponentInterface> *const interface) { |
| 456 | interface->reset(); |
| 457 | return C2_OMITTED; |
| 458 | } |
| 459 | |
| 460 | virtual c2_status_t query_sm( |
| 461 | const std::vector<C2Param *> &stackParams, |
| 462 | const std::vector<C2Param::Index> &heapParamIndices, |
| 463 | std::vector<std::unique_ptr<C2Param>> *const heapParams) const { |
| 464 | return mClient->query(stackParams, heapParamIndices, C2_MAY_BLOCK, heapParams); |
| 465 | } |
| 466 | |
| 467 | virtual c2_status_t querySupportedParams_nb( |
| 468 | std::vector<std::shared_ptr<C2ParamDescriptor>> *const params) const { |
| 469 | return mClient->querySupportedParams(params); |
| 470 | } |
| 471 | |
| 472 | virtual c2_status_t querySupportedValues_sm( |
| 473 | std::vector<C2FieldSupportedValuesQuery> &fields) const { |
| 474 | return mClient->querySupportedValues(fields, C2_MAY_BLOCK); |
| 475 | } |
| 476 | |
| 477 | virtual C2String getName() const { |
| 478 | return mClient->getName(); |
| 479 | } |
| 480 | |
| 481 | virtual std::shared_ptr<C2ParamReflector> getParamReflector() const { |
| 482 | return mClient->getParamReflector(); |
| 483 | } |
| 484 | |
| 485 | virtual std::vector<std::shared_ptr<const C2Component::Traits>> listComponents() { |
| 486 | return std::vector<std::shared_ptr<const C2Component::Traits>>(); |
| 487 | } |
| 488 | }; |
| 489 | |
Wonsik Kim | 970bf0b | 2020-11-10 11:54:15 -0800 | [diff] [blame] | 490 | void RevertOutputFormatIfNeeded( |
| 491 | const sp<AMessage> &oldFormat, sp<AMessage> ¤tFormat) { |
| 492 | // We used to not report changes to these keys to the client. |
| 493 | const static std::set<std::string> sIgnoredKeys({ |
| 494 | KEY_BIT_RATE, |
| 495 | KEY_MAX_BIT_RATE, |
| 496 | "csd-0", |
| 497 | "csd-1", |
| 498 | "csd-2", |
| 499 | }); |
| 500 | if (currentFormat == oldFormat) { |
| 501 | return; |
| 502 | } |
| 503 | sp<AMessage> diff = currentFormat->changesFrom(oldFormat); |
| 504 | AMessage::Type type; |
| 505 | for (size_t i = diff->countEntries(); i > 0; --i) { |
| 506 | if (sIgnoredKeys.count(diff->getEntryNameAt(i - 1, &type)) > 0) { |
| 507 | diff->removeEntryAt(i - 1); |
| 508 | } |
| 509 | } |
| 510 | if (diff->countEntries() == 0) { |
| 511 | currentFormat = oldFormat; |
| 512 | } |
| 513 | } |
| 514 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 515 | } // namespace |
| 516 | |
| 517 | // CCodec::ClientListener |
| 518 | |
| 519 | struct CCodec::ClientListener : public Codec2Client::Listener { |
| 520 | |
| 521 | explicit ClientListener(const wp<CCodec> &codec) : mCodec(codec) {} |
| 522 | |
| 523 | virtual void onWorkDone( |
| 524 | const std::weak_ptr<Codec2Client::Component>& component, |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 525 | std::list<std::unique_ptr<C2Work>>& workItems) override { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 526 | (void)component; |
| 527 | sp<CCodec> codec(mCodec.promote()); |
| 528 | if (!codec) { |
| 529 | return; |
| 530 | } |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 531 | codec->onWorkDone(workItems); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | virtual void onTripped( |
| 535 | const std::weak_ptr<Codec2Client::Component>& component, |
| 536 | const std::vector<std::shared_ptr<C2SettingResult>>& settingResult |
| 537 | ) override { |
| 538 | // TODO |
| 539 | (void)component; |
| 540 | (void)settingResult; |
| 541 | } |
| 542 | |
| 543 | virtual void onError( |
| 544 | const std::weak_ptr<Codec2Client::Component>& component, |
| 545 | uint32_t errorCode) override { |
Praveen Chavan | 72eff01 | 2020-11-20 23:20:28 -0800 | [diff] [blame] | 546 | { |
| 547 | // Component is only used for reporting as we use a separate listener for each instance |
| 548 | std::shared_ptr<Codec2Client::Component> comp = component.lock(); |
| 549 | if (!comp) { |
| 550 | ALOGD("Component died with error: 0x%x", errorCode); |
| 551 | } else { |
| 552 | ALOGD("Component \"%s\" returned error: 0x%x", comp->getName().c_str(), errorCode); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | // Report to MediaCodec |
| 557 | // Note: for now we do not propagate the error code to MediaCodec as we would need |
| 558 | // to translate to a MediaCodec error. |
| 559 | sp<CCodec> codec(mCodec.promote()); |
| 560 | if (!codec || !codec->mCallback) { |
| 561 | return; |
| 562 | } |
| 563 | codec->mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | virtual void onDeath( |
| 567 | const std::weak_ptr<Codec2Client::Component>& component) override { |
| 568 | { // Log the death of the component. |
| 569 | std::shared_ptr<Codec2Client::Component> comp = component.lock(); |
| 570 | if (!comp) { |
| 571 | ALOGE("Codec2 component died."); |
| 572 | } else { |
| 573 | ALOGE("Codec2 component \"%s\" died.", comp->getName().c_str()); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | // Report to MediaCodec. |
| 578 | sp<CCodec> codec(mCodec.promote()); |
| 579 | if (!codec || !codec->mCallback) { |
| 580 | return; |
| 581 | } |
| 582 | codec->mCallback->onError(DEAD_OBJECT, ACTION_CODE_FATAL); |
| 583 | } |
| 584 | |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 585 | virtual void onFrameRendered(uint64_t bufferQueueId, |
| 586 | int32_t slotId, |
| 587 | int64_t timestampNs) override { |
| 588 | // TODO: implement |
| 589 | (void)bufferQueueId; |
| 590 | (void)slotId; |
| 591 | (void)timestampNs; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | virtual void onInputBufferDone( |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 595 | uint64_t frameIndex, size_t arrayIndex) override { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 596 | sp<CCodec> codec(mCodec.promote()); |
| 597 | if (codec) { |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 598 | codec->onInputBufferDone(frameIndex, arrayIndex); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
| 602 | private: |
| 603 | wp<CCodec> mCodec; |
| 604 | }; |
| 605 | |
| 606 | // CCodecCallbackImpl |
| 607 | |
| 608 | class CCodecCallbackImpl : public CCodecCallback { |
| 609 | public: |
| 610 | explicit CCodecCallbackImpl(CCodec *codec) : mCodec(codec) {} |
| 611 | ~CCodecCallbackImpl() override = default; |
| 612 | |
| 613 | void onError(status_t err, enum ActionCode actionCode) override { |
| 614 | mCodec->mCallback->onError(err, actionCode); |
| 615 | } |
| 616 | |
| 617 | void onOutputFramesRendered(int64_t mediaTimeUs, nsecs_t renderTimeNs) override { |
| 618 | mCodec->mCallback->onOutputFramesRendered( |
| 619 | {RenderedFrameInfo(mediaTimeUs, renderTimeNs)}); |
| 620 | } |
| 621 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 622 | void onOutputBuffersChanged() override { |
| 623 | mCodec->mCallback->onOutputBuffersChanged(); |
| 624 | } |
| 625 | |
| 626 | private: |
| 627 | CCodec *mCodec; |
| 628 | }; |
| 629 | |
| 630 | // CCodec |
| 631 | |
| 632 | CCodec::CCodec() |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 633 | : mChannel(new CCodecBufferChannel(std::make_shared<CCodecCallbackImpl>(this))), |
| 634 | mConfig(new CCodecConfig) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | CCodec::~CCodec() { |
| 638 | } |
| 639 | |
| 640 | std::shared_ptr<BufferChannelBase> CCodec::getBufferChannel() { |
| 641 | return mChannel; |
| 642 | } |
| 643 | |
| 644 | status_t CCodec::tryAndReportOnError(std::function<status_t()> job) { |
| 645 | status_t err = job(); |
| 646 | if (err != C2_OK) { |
| 647 | mCallback->onError(err, ACTION_CODE_FATAL); |
| 648 | } |
| 649 | return err; |
| 650 | } |
| 651 | |
| 652 | void CCodec::initiateAllocateComponent(const sp<AMessage> &msg) { |
| 653 | auto setAllocating = [this] { |
| 654 | Mutexed<State>::Locked state(mState); |
| 655 | if (state->get() != RELEASED) { |
| 656 | return INVALID_OPERATION; |
| 657 | } |
| 658 | state->set(ALLOCATING); |
| 659 | return OK; |
| 660 | }; |
| 661 | if (tryAndReportOnError(setAllocating) != OK) { |
| 662 | return; |
| 663 | } |
| 664 | |
| 665 | sp<RefBase> codecInfo; |
| 666 | CHECK(msg->findObject("codecInfo", &codecInfo)); |
| 667 | // For Codec 2.0 components, componentName == codecInfo->getCodecName(). |
| 668 | |
| 669 | sp<AMessage> allocMsg(new AMessage(kWhatAllocate, this)); |
| 670 | allocMsg->setObject("codecInfo", codecInfo); |
| 671 | allocMsg->post(); |
| 672 | } |
| 673 | |
| 674 | void CCodec::allocate(const sp<MediaCodecInfo> &codecInfo) { |
| 675 | if (codecInfo == nullptr) { |
| 676 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 677 | return; |
| 678 | } |
| 679 | ALOGD("allocate(%s)", codecInfo->getCodecName()); |
| 680 | mClientListener.reset(new ClientListener(this)); |
| 681 | |
| 682 | AString componentName = codecInfo->getCodecName(); |
| 683 | std::shared_ptr<Codec2Client> client; |
| 684 | |
| 685 | // set up preferred component store to access vendor store parameters |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 686 | client = Codec2Client::CreateFromService("default"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 687 | if (client) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 688 | ALOGI("setting up '%s' as default (vendor) store", client->getServiceName().c_str()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 689 | SetPreferredCodec2ComponentStore( |
| 690 | std::make_shared<Codec2ClientInterfaceWrapper>(client)); |
| 691 | } |
| 692 | |
| 693 | std::shared_ptr<Codec2Client::Component> comp = |
| 694 | Codec2Client::CreateComponentByName( |
| 695 | componentName.c_str(), |
| 696 | mClientListener, |
| 697 | &client); |
| 698 | if (!comp) { |
| 699 | ALOGE("Failed Create component: %s", componentName.c_str()); |
| 700 | Mutexed<State>::Locked state(mState); |
| 701 | state->set(RELEASED); |
| 702 | state.unlock(); |
| 703 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 704 | state.lock(); |
| 705 | return; |
| 706 | } |
| 707 | ALOGI("Created component [%s]", componentName.c_str()); |
| 708 | mChannel->setComponent(comp); |
| 709 | auto setAllocated = [this, comp, client] { |
| 710 | Mutexed<State>::Locked state(mState); |
| 711 | if (state->get() != ALLOCATING) { |
| 712 | state->set(RELEASED); |
| 713 | return UNKNOWN_ERROR; |
| 714 | } |
| 715 | state->set(ALLOCATED); |
| 716 | state->comp = comp; |
| 717 | mClient = client; |
| 718 | return OK; |
| 719 | }; |
| 720 | if (tryAndReportOnError(setAllocated) != OK) { |
| 721 | return; |
| 722 | } |
| 723 | |
| 724 | // initialize config here in case setParameters is called prior to configure |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 725 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 726 | const std::unique_ptr<Config> &config = *configLocked; |
Wonsik Kim | 8a6ed37 | 2019-12-03 16:05:51 -0800 | [diff] [blame] | 727 | status_t err = config->initialize(mClient->getParamReflector(), comp); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 728 | if (err != OK) { |
| 729 | ALOGW("Failed to initialize configuration support"); |
| 730 | // TODO: report error once we complete implementation. |
| 731 | } |
| 732 | config->queryConfiguration(comp); |
| 733 | |
| 734 | mCallback->onComponentAllocated(componentName.c_str()); |
| 735 | } |
| 736 | |
| 737 | void CCodec::initiateConfigureComponent(const sp<AMessage> &format) { |
| 738 | auto checkAllocated = [this] { |
| 739 | Mutexed<State>::Locked state(mState); |
| 740 | return (state->get() != ALLOCATED) ? UNKNOWN_ERROR : OK; |
| 741 | }; |
| 742 | if (tryAndReportOnError(checkAllocated) != OK) { |
| 743 | return; |
| 744 | } |
| 745 | |
| 746 | sp<AMessage> msg(new AMessage(kWhatConfigure, this)); |
| 747 | msg->setMessage("format", format); |
| 748 | msg->post(); |
| 749 | } |
| 750 | |
| 751 | void CCodec::configure(const sp<AMessage> &msg) { |
| 752 | std::shared_ptr<Codec2Client::Component> comp; |
| 753 | auto checkAllocated = [this, &comp] { |
| 754 | Mutexed<State>::Locked state(mState); |
| 755 | if (state->get() != ALLOCATED) { |
| 756 | state->set(RELEASED); |
| 757 | return UNKNOWN_ERROR; |
| 758 | } |
| 759 | comp = state->comp; |
| 760 | return OK; |
| 761 | }; |
| 762 | if (tryAndReportOnError(checkAllocated) != OK) { |
| 763 | return; |
| 764 | } |
| 765 | |
| 766 | auto doConfig = [msg, comp, this]() -> status_t { |
| 767 | AString mime; |
| 768 | if (!msg->findString("mime", &mime)) { |
| 769 | return BAD_VALUE; |
| 770 | } |
| 771 | |
| 772 | int32_t encoder; |
| 773 | if (!msg->findInt32("encoder", &encoder)) { |
| 774 | encoder = false; |
| 775 | } |
| 776 | |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 777 | int32_t flags; |
| 778 | if (!msg->findInt32("flags", &flags)) { |
| 779 | return BAD_VALUE; |
| 780 | } |
| 781 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 782 | // TODO: read from intf() |
| 783 | if ((!encoder) != (comp->getName().find("encoder") == std::string::npos)) { |
| 784 | return UNKNOWN_ERROR; |
| 785 | } |
| 786 | |
| 787 | int32_t storeMeta; |
| 788 | if (encoder |
| 789 | && msg->findInt32("android._input-metadata-buffer-type", &storeMeta) |
| 790 | && storeMeta != kMetadataBufferTypeInvalid) { |
| 791 | if (storeMeta != kMetadataBufferTypeANWBuffer) { |
| 792 | ALOGD("Only ANW buffers are supported for legacy metadata mode"); |
| 793 | return BAD_VALUE; |
| 794 | } |
| 795 | mChannel->setMetaMode(CCodecBufferChannel::MODE_ANW); |
| 796 | } |
| 797 | |
| 798 | sp<RefBase> obj; |
| 799 | sp<Surface> surface; |
| 800 | if (msg->findObject("native-window", &obj)) { |
| 801 | surface = static_cast<Surface *>(obj.get()); |
| 802 | setSurface(surface); |
| 803 | } |
| 804 | |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 805 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 806 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 807 | config->mUsingSurface = surface != nullptr; |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 808 | config->mBuffersBoundToCodec = ((flags & CONFIGURE_FLAG_USE_BLOCK_MODEL) == 0); |
| 809 | ALOGD("[%s] buffers are %sbound to CCodec for this session", |
| 810 | comp->getName().c_str(), config->mBuffersBoundToCodec ? "" : "not "); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 811 | |
Wonsik Kim | 1114eea | 2019-02-25 14:35:24 -0800 | [diff] [blame] | 812 | // Enforce required parameters |
| 813 | int32_t i32; |
| 814 | float flt; |
| 815 | if (config->mDomain & Config::IS_AUDIO) { |
| 816 | if (!msg->findInt32(KEY_SAMPLE_RATE, &i32)) { |
| 817 | ALOGD("sample rate is missing, which is required for audio components."); |
| 818 | return BAD_VALUE; |
| 819 | } |
| 820 | if (!msg->findInt32(KEY_CHANNEL_COUNT, &i32)) { |
| 821 | ALOGD("channel count is missing, which is required for audio components."); |
| 822 | return BAD_VALUE; |
| 823 | } |
| 824 | if ((config->mDomain & Config::IS_ENCODER) |
| 825 | && !mime.equalsIgnoreCase(MEDIA_MIMETYPE_AUDIO_FLAC) |
| 826 | && !msg->findInt32(KEY_BIT_RATE, &i32) |
| 827 | && !msg->findFloat(KEY_BIT_RATE, &flt)) { |
| 828 | ALOGD("bitrate is missing, which is required for audio encoders."); |
| 829 | return BAD_VALUE; |
| 830 | } |
| 831 | } |
| 832 | if (config->mDomain & (Config::IS_IMAGE | Config::IS_VIDEO)) { |
| 833 | if (!msg->findInt32(KEY_WIDTH, &i32)) { |
| 834 | ALOGD("width is missing, which is required for image/video components."); |
| 835 | return BAD_VALUE; |
| 836 | } |
| 837 | if (!msg->findInt32(KEY_HEIGHT, &i32)) { |
| 838 | ALOGD("height is missing, which is required for image/video components."); |
| 839 | return BAD_VALUE; |
| 840 | } |
| 841 | if ((config->mDomain & Config::IS_ENCODER) && (config->mDomain & Config::IS_VIDEO)) { |
Harish Mahendrakar | 71cbb9d | 2019-05-21 11:21:27 -0700 | [diff] [blame] | 842 | int32_t mode = BITRATE_MODE_VBR; |
| 843 | if (msg->findInt32(KEY_BITRATE_MODE, &mode) && mode == BITRATE_MODE_CQ) { |
Harish Mahendrakar | 817d318 | 2019-03-11 16:37:47 -0700 | [diff] [blame] | 844 | if (!msg->findInt32(KEY_QUALITY, &i32)) { |
| 845 | ALOGD("quality is missing, which is required for video encoders in CQ."); |
| 846 | return BAD_VALUE; |
| 847 | } |
| 848 | } else { |
| 849 | if (!msg->findInt32(KEY_BIT_RATE, &i32) |
| 850 | && !msg->findFloat(KEY_BIT_RATE, &flt)) { |
| 851 | ALOGD("bitrate is missing, which is required for video encoders."); |
| 852 | return BAD_VALUE; |
| 853 | } |
Wonsik Kim | 1114eea | 2019-02-25 14:35:24 -0800 | [diff] [blame] | 854 | } |
| 855 | if (!msg->findInt32(KEY_I_FRAME_INTERVAL, &i32) |
| 856 | && !msg->findFloat(KEY_I_FRAME_INTERVAL, &flt)) { |
| 857 | ALOGD("I frame interval is missing, which is required for video encoders."); |
| 858 | return BAD_VALUE; |
| 859 | } |
Wonsik Kim | aab2eea | 2019-05-22 10:37:58 -0700 | [diff] [blame] | 860 | if (!msg->findInt32(KEY_FRAME_RATE, &i32) |
| 861 | && !msg->findFloat(KEY_FRAME_RATE, &flt)) { |
| 862 | ALOGD("frame rate is missing, which is required for video encoders."); |
| 863 | return BAD_VALUE; |
| 864 | } |
Wonsik Kim | 1114eea | 2019-02-25 14:35:24 -0800 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 868 | /* |
| 869 | * Handle input surface configuration |
| 870 | */ |
| 871 | if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE)) |
| 872 | && (config->mDomain & Config::IS_ENCODER)) { |
| 873 | config->mISConfig.reset(new InputSurfaceWrapper::Config{}); |
| 874 | { |
| 875 | config->mISConfig->mMinFps = 0; |
| 876 | int64_t value; |
Chong Zhang | 038e8f8 | 2019-02-06 19:05:14 -0800 | [diff] [blame] | 877 | if (msg->findInt64(KEY_REPEAT_PREVIOUS_FRAME_AFTER, &value) && value > 0) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 878 | config->mISConfig->mMinFps = 1e6 / value; |
| 879 | } |
Wonsik Kim | 95ba016 | 2019-03-19 15:51:54 -0700 | [diff] [blame] | 880 | if (!msg->findFloat( |
| 881 | KEY_MAX_FPS_TO_ENCODER, &config->mISConfig->mMaxFps)) { |
| 882 | config->mISConfig->mMaxFps = -1; |
| 883 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 884 | config->mISConfig->mMinAdjustedFps = 0; |
| 885 | config->mISConfig->mFixedAdjustedFps = 0; |
Chong Zhang | 038e8f8 | 2019-02-06 19:05:14 -0800 | [diff] [blame] | 886 | if (msg->findInt64(KEY_MAX_PTS_GAP_TO_ENCODER, &value)) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 887 | if (value < 0 && value >= INT32_MIN) { |
| 888 | config->mISConfig->mFixedAdjustedFps = -1e6 / value; |
Wonsik Kim | 95ba016 | 2019-03-19 15:51:54 -0700 | [diff] [blame] | 889 | config->mISConfig->mMaxFps = -1; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 890 | } else if (value > 0 && value <= INT32_MAX) { |
| 891 | config->mISConfig->mMinAdjustedFps = 1e6 / value; |
| 892 | } |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | { |
Wonsik Kim | 8e55f3a | 2019-09-03 14:10:37 -0700 | [diff] [blame] | 897 | bool captureFpsFound = false; |
| 898 | double timeLapseFps; |
| 899 | float captureRate; |
| 900 | if (msg->findDouble("time-lapse-fps", &timeLapseFps)) { |
| 901 | config->mISConfig->mCaptureFps = timeLapseFps; |
| 902 | captureFpsFound = true; |
| 903 | } else if (msg->findAsFloat(KEY_CAPTURE_RATE, &captureRate)) { |
| 904 | config->mISConfig->mCaptureFps = captureRate; |
| 905 | captureFpsFound = true; |
| 906 | } |
| 907 | if (captureFpsFound) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 908 | (void)msg->findAsFloat(KEY_FRAME_RATE, &config->mISConfig->mCodedFps); |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | { |
| 913 | config->mISConfig->mSuspended = false; |
| 914 | config->mISConfig->mSuspendAtUs = -1; |
| 915 | int32_t value; |
Chong Zhang | 038e8f8 | 2019-02-06 19:05:14 -0800 | [diff] [blame] | 916 | if (msg->findInt32(KEY_CREATE_INPUT_SURFACE_SUSPENDED, &value) && value) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 917 | config->mISConfig->mSuspended = true; |
| 918 | } |
| 919 | } |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 920 | config->mISConfig->mUsage = 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | /* |
| 924 | * Handle desired color format. |
| 925 | */ |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 926 | int32_t defaultColorFormat = COLOR_FormatYUV420Flexible; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 927 | if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))) { |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 928 | int32_t format = 0; |
| 929 | // Query vendor format for Flexible YUV |
| 930 | std::vector<std::unique_ptr<C2Param>> heapParams; |
| 931 | C2StoreFlexiblePixelFormatDescriptorsInfo *pixelFormatInfo = nullptr; |
| 932 | if (mClient->query( |
| 933 | {}, |
| 934 | {C2StoreFlexiblePixelFormatDescriptorsInfo::PARAM_TYPE}, |
| 935 | C2_MAY_BLOCK, |
| 936 | &heapParams) == C2_OK |
| 937 | && heapParams.size() == 1u) { |
| 938 | pixelFormatInfo = C2StoreFlexiblePixelFormatDescriptorsInfo::From( |
| 939 | heapParams[0].get()); |
| 940 | } else { |
| 941 | pixelFormatInfo = nullptr; |
| 942 | } |
| 943 | std::optional<uint32_t> flexPixelFormat{}; |
| 944 | std::optional<uint32_t> flexPlanarPixelFormat{}; |
| 945 | std::optional<uint32_t> flexSemiPlanarPixelFormat{}; |
| 946 | if (pixelFormatInfo && *pixelFormatInfo) { |
| 947 | for (size_t i = 0; i < pixelFormatInfo->flexCount(); ++i) { |
| 948 | const C2FlexiblePixelFormatDescriptorStruct &desc = |
| 949 | pixelFormatInfo->m.values[i]; |
| 950 | if (desc.bitDepth != 8 |
| 951 | || desc.subsampling != C2Color::YUV_420 |
| 952 | // TODO(b/180076105): some device report wrong layout |
| 953 | // || desc.layout == C2Color::INTERLEAVED_PACKED |
| 954 | // || desc.layout == C2Color::INTERLEAVED_ALIGNED |
| 955 | || desc.layout == C2Color::UNKNOWN_LAYOUT) { |
| 956 | continue; |
| 957 | } |
| 958 | if (!flexPixelFormat) { |
| 959 | flexPixelFormat = desc.pixelFormat; |
| 960 | } |
| 961 | if (desc.layout == C2Color::PLANAR_PACKED && !flexPlanarPixelFormat) { |
| 962 | flexPlanarPixelFormat = desc.pixelFormat; |
| 963 | } |
| 964 | if (desc.layout == C2Color::SEMIPLANAR_PACKED && !flexSemiPlanarPixelFormat) { |
| 965 | flexSemiPlanarPixelFormat = desc.pixelFormat; |
| 966 | } |
| 967 | } |
| 968 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 969 | if (!msg->findInt32(KEY_COLOR_FORMAT, &format)) { |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 970 | // Also handle default color format (encoders require color format, so this is only |
| 971 | // needed for decoders. |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 972 | if (!(config->mDomain & Config::IS_ENCODER)) { |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 973 | if (surface == nullptr) { |
| 974 | format = flexPixelFormat.value_or(COLOR_FormatYUV420Flexible); |
| 975 | } else { |
| 976 | format = COLOR_FormatSurface; |
| 977 | } |
| 978 | defaultColorFormat = format; |
| 979 | } |
| 980 | } else { |
| 981 | if ((config->mDomain & Config::IS_ENCODER) || !surface) { |
| 982 | switch (format) { |
| 983 | case COLOR_FormatYUV420Flexible: |
| 984 | format = flexPixelFormat.value_or(COLOR_FormatYUV420Planar); |
| 985 | break; |
| 986 | case COLOR_FormatYUV420Planar: |
| 987 | case COLOR_FormatYUV420PackedPlanar: |
| 988 | format = flexPlanarPixelFormat.value_or( |
| 989 | flexPixelFormat.value_or(format)); |
| 990 | break; |
| 991 | case COLOR_FormatYUV420SemiPlanar: |
| 992 | case COLOR_FormatYUV420PackedSemiPlanar: |
| 993 | format = flexSemiPlanarPixelFormat.value_or( |
| 994 | flexPixelFormat.value_or(format)); |
| 995 | break; |
| 996 | default: |
| 997 | // No-op |
| 998 | break; |
| 999 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 1003 | if (format != 0) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1004 | msg->setInt32("android._color-format", format); |
| 1005 | } |
| 1006 | } |
| 1007 | |
Wonsik Kim | 8a6ed37 | 2019-12-03 16:05:51 -0800 | [diff] [blame] | 1008 | int32_t subscribeToAllVendorParams; |
| 1009 | if (msg->findInt32("x-*", &subscribeToAllVendorParams) && subscribeToAllVendorParams) { |
| 1010 | if (config->subscribeToAllVendorParams(comp, C2_MAY_BLOCK) != OK) { |
| 1011 | ALOGD("[%s] Failed to subscribe to all vendor params", comp->getName().c_str()); |
| 1012 | } |
| 1013 | } |
| 1014 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1015 | std::vector<std::unique_ptr<C2Param>> configUpdate; |
Wonsik Kim | aa484ac | 2019-02-13 16:54:02 -0800 | [diff] [blame] | 1016 | // NOTE: We used to ignore "video-bitrate" at configure; replicate |
| 1017 | // the behavior here. |
| 1018 | sp<AMessage> sdkParams = msg; |
| 1019 | int32_t videoBitrate; |
| 1020 | if (sdkParams->findInt32(PARAMETER_KEY_VIDEO_BITRATE, &videoBitrate)) { |
| 1021 | sdkParams = msg->dup(); |
| 1022 | sdkParams->removeEntryAt(sdkParams->findEntryByName(PARAMETER_KEY_VIDEO_BITRATE)); |
| 1023 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1024 | status_t err = config->getConfigUpdateFromSdkParams( |
Wonsik Kim | aa484ac | 2019-02-13 16:54:02 -0800 | [diff] [blame] | 1025 | comp, sdkParams, Config::IS_CONFIG, C2_DONT_BLOCK, &configUpdate); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1026 | if (err != OK) { |
| 1027 | ALOGW("failed to convert configuration to c2 params"); |
| 1028 | } |
Wonsik Kim | aab2eea | 2019-05-22 10:37:58 -0700 | [diff] [blame] | 1029 | |
| 1030 | int32_t maxBframes = 0; |
| 1031 | if ((config->mDomain & Config::IS_ENCODER) |
| 1032 | && (config->mDomain & Config::IS_VIDEO) |
| 1033 | && sdkParams->findInt32(KEY_MAX_B_FRAMES, &maxBframes) |
| 1034 | && maxBframes > 0) { |
| 1035 | std::unique_ptr<C2StreamGopTuning::output> gop = |
| 1036 | C2StreamGopTuning::output::AllocUnique(2 /* flexCount */, 0u /* stream */); |
| 1037 | gop->m.values[0] = { P_FRAME, UINT32_MAX }; |
| 1038 | gop->m.values[1] = { |
| 1039 | C2Config::picture_type_t(P_FRAME | B_FRAME), |
| 1040 | uint32_t(maxBframes) |
| 1041 | }; |
| 1042 | configUpdate.push_back(std::move(gop)); |
| 1043 | } |
| 1044 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1045 | err = config->setParameters(comp, configUpdate, C2_DONT_BLOCK); |
| 1046 | if (err != OK) { |
| 1047 | ALOGW("failed to configure c2 params"); |
| 1048 | return err; |
| 1049 | } |
| 1050 | |
| 1051 | std::vector<std::unique_ptr<C2Param>> params; |
| 1052 | C2StreamUsageTuning::input usage(0u, 0u); |
| 1053 | C2StreamMaxBufferSizeInfo::input maxInputSize(0u, 0u); |
Wonsik Kim | 9ca01d3 | 2019-04-01 14:45:47 -0700 | [diff] [blame] | 1054 | C2PrependHeaderModeSetting prepend(PREPEND_HEADER_TO_NONE); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1055 | |
| 1056 | std::initializer_list<C2Param::Index> indices { |
| 1057 | }; |
| 1058 | c2_status_t c2err = comp->query( |
Wonsik Kim | 9ca01d3 | 2019-04-01 14:45:47 -0700 | [diff] [blame] | 1059 | { &usage, &maxInputSize, &prepend }, |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1060 | indices, |
| 1061 | C2_DONT_BLOCK, |
| 1062 | ¶ms); |
| 1063 | if (c2err != C2_OK && c2err != C2_BAD_INDEX) { |
| 1064 | ALOGE("Failed to query component interface: %d", c2err); |
| 1065 | return UNKNOWN_ERROR; |
| 1066 | } |
| 1067 | if (params.size() != indices.size()) { |
| 1068 | ALOGE("Component returns wrong number of params: expected %zu actual %zu", |
| 1069 | indices.size(), params.size()); |
| 1070 | return UNKNOWN_ERROR; |
| 1071 | } |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 1072 | if (usage) { |
| 1073 | if (usage.value & C2MemoryUsage::CPU_READ) { |
| 1074 | config->mInputFormat->setInt32("using-sw-read-often", true); |
| 1075 | } |
| 1076 | if (config->mISConfig) { |
| 1077 | C2AndroidMemoryUsage androidUsage(C2MemoryUsage(usage.value)); |
| 1078 | config->mISConfig->mUsage = androidUsage.asGrallocUsage(); |
| 1079 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | // NOTE: we don't blindly use client specified input size if specified as clients |
| 1083 | // at times specify too small size. Instead, mimic the behavior from OMX, where the |
| 1084 | // client specified size is only used to ask for bigger buffers than component suggested |
| 1085 | // size. |
| 1086 | int32_t clientInputSize = 0; |
| 1087 | bool clientSpecifiedInputSize = |
| 1088 | msg->findInt32(KEY_MAX_INPUT_SIZE, &clientInputSize) && clientInputSize > 0; |
| 1089 | // TEMP: enforce minimum buffer size of 1MB for video decoders |
| 1090 | // and 16K / 4K for audio encoders/decoders |
| 1091 | if (maxInputSize.value == 0) { |
| 1092 | if (config->mDomain & Config::IS_AUDIO) { |
| 1093 | maxInputSize.value = encoder ? 16384 : 4096; |
| 1094 | } else if (!encoder) { |
| 1095 | maxInputSize.value = 1048576u; |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | // verify that CSD fits into this size (if defined) |
| 1100 | if ((config->mDomain & Config::IS_DECODER) && maxInputSize.value > 0) { |
| 1101 | sp<ABuffer> csd; |
| 1102 | for (size_t ix = 0; msg->findBuffer(StringPrintf("csd-%zu", ix).c_str(), &csd); ++ix) { |
| 1103 | if (csd && csd->size() > maxInputSize.value) { |
| 1104 | maxInputSize.value = csd->size(); |
| 1105 | } |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | // TODO: do this based on component requiring linear allocator for input |
| 1110 | if ((config->mDomain & Config::IS_DECODER) || (config->mDomain & Config::IS_AUDIO)) { |
| 1111 | if (clientSpecifiedInputSize) { |
| 1112 | // Warn that we're overriding client's max input size if necessary. |
| 1113 | if ((uint32_t)clientInputSize < maxInputSize.value) { |
| 1114 | ALOGD("client requested max input size %d, which is smaller than " |
| 1115 | "what component recommended (%u); overriding with component " |
| 1116 | "recommendation.", clientInputSize, maxInputSize.value); |
| 1117 | ALOGW("This behavior is subject to change. It is recommended that " |
| 1118 | "app developers double check whether the requested " |
| 1119 | "max input size is in reasonable range."); |
| 1120 | } else { |
| 1121 | maxInputSize.value = clientInputSize; |
| 1122 | } |
| 1123 | } |
| 1124 | // Pass max input size on input format to the buffer channel (if supplied by the |
| 1125 | // component or by a default) |
| 1126 | if (maxInputSize.value) { |
| 1127 | config->mInputFormat->setInt32( |
| 1128 | KEY_MAX_INPUT_SIZE, |
| 1129 | (int32_t)(c2_min(maxInputSize.value, uint32_t(INT32_MAX)))); |
| 1130 | } |
| 1131 | } |
| 1132 | |
Wonsik Kim | 9ca01d3 | 2019-04-01 14:45:47 -0700 | [diff] [blame] | 1133 | int32_t clientPrepend; |
| 1134 | if ((config->mDomain & Config::IS_VIDEO) |
| 1135 | && (config->mDomain & Config::IS_ENCODER) |
| 1136 | && msg->findInt32(KEY_PREPEND_HEADERS_TO_SYNC_FRAMES, &clientPrepend) |
| 1137 | && clientPrepend |
| 1138 | && (!prepend || prepend.value != PREPEND_HEADER_TO_ALL_SYNC)) { |
| 1139 | ALOGE("Failed to set KEY_PREPEND_HEADERS_TO_SYNC_FRAMES"); |
| 1140 | return BAD_VALUE; |
| 1141 | } |
| 1142 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1143 | if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))) { |
| 1144 | // propagate HDR static info to output format for both encoders and decoders |
| 1145 | // if component supports this info, we will update from component, but only the raw port, |
| 1146 | // so don't propagate if component already filled it in. |
| 1147 | sp<ABuffer> hdrInfo; |
| 1148 | if (msg->findBuffer(KEY_HDR_STATIC_INFO, &hdrInfo) |
| 1149 | && !config->mOutputFormat->findBuffer(KEY_HDR_STATIC_INFO, &hdrInfo)) { |
| 1150 | config->mOutputFormat->setBuffer(KEY_HDR_STATIC_INFO, hdrInfo); |
| 1151 | } |
| 1152 | |
| 1153 | // Set desired color format from configuration parameter |
| 1154 | int32_t format; |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 1155 | if (!msg->findInt32(KEY_COLOR_FORMAT, &format)) { |
| 1156 | format = defaultColorFormat; |
| 1157 | } |
| 1158 | if (config->mDomain & Config::IS_ENCODER) { |
| 1159 | config->mInputFormat->setInt32(KEY_COLOR_FORMAT, format); |
| 1160 | if (msg->findInt32("android._color-format", &format)) { |
| 1161 | config->mInputFormat->setInt32("android._color-format", format); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1162 | } |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 1163 | } else { |
| 1164 | config->mOutputFormat->setInt32(KEY_COLOR_FORMAT, format); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | // propagate encoder delay and padding to output format |
| 1169 | if ((config->mDomain & Config::IS_DECODER) && (config->mDomain & Config::IS_AUDIO)) { |
| 1170 | int delay = 0; |
| 1171 | if (msg->findInt32("encoder-delay", &delay)) { |
| 1172 | config->mOutputFormat->setInt32("encoder-delay", delay); |
| 1173 | } |
| 1174 | int padding = 0; |
| 1175 | if (msg->findInt32("encoder-padding", &padding)) { |
| 1176 | config->mOutputFormat->setInt32("encoder-padding", padding); |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | // set channel-mask |
| 1181 | if (config->mDomain & Config::IS_AUDIO) { |
| 1182 | int32_t mask; |
| 1183 | if (msg->findInt32(KEY_CHANNEL_MASK, &mask)) { |
| 1184 | if (config->mDomain & Config::IS_ENCODER) { |
| 1185 | config->mInputFormat->setInt32(KEY_CHANNEL_MASK, mask); |
| 1186 | } else { |
| 1187 | config->mOutputFormat->setInt32(KEY_CHANNEL_MASK, mask); |
| 1188 | } |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | ALOGD("setup formats input: %s and output: %s", |
| 1193 | config->mInputFormat->debugString().c_str(), |
| 1194 | config->mOutputFormat->debugString().c_str()); |
| 1195 | return OK; |
| 1196 | }; |
| 1197 | if (tryAndReportOnError(doConfig) != OK) { |
| 1198 | return; |
| 1199 | } |
| 1200 | |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1201 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1202 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1203 | |
| 1204 | mCallback->onComponentConfigured(config->mInputFormat, config->mOutputFormat); |
| 1205 | } |
| 1206 | |
| 1207 | void CCodec::initiateCreateInputSurface() { |
| 1208 | status_t err = [this] { |
| 1209 | Mutexed<State>::Locked state(mState); |
| 1210 | if (state->get() != ALLOCATED) { |
| 1211 | return UNKNOWN_ERROR; |
| 1212 | } |
| 1213 | // TODO: read it from intf() properly. |
| 1214 | if (state->comp->getName().find("encoder") == std::string::npos) { |
| 1215 | return INVALID_OPERATION; |
| 1216 | } |
| 1217 | return OK; |
| 1218 | }(); |
| 1219 | if (err != OK) { |
| 1220 | mCallback->onInputSurfaceCreationFailed(err); |
| 1221 | return; |
| 1222 | } |
| 1223 | |
| 1224 | (new AMessage(kWhatCreateInputSurface, this))->post(); |
| 1225 | } |
| 1226 | |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 1227 | sp<PersistentSurface> CCodec::CreateOmxInputSurface() { |
| 1228 | using namespace android::hardware::media::omx::V1_0; |
| 1229 | using namespace android::hardware::media::omx::V1_0::utils; |
| 1230 | using namespace android::hardware::graphics::bufferqueue::V1_0::utils; |
| 1231 | typedef android::hardware::media::omx::V1_0::Status OmxStatus; |
| 1232 | android::sp<IOmx> omx = IOmx::getService(); |
| 1233 | typedef android::hardware::graphics::bufferqueue::V1_0:: |
| 1234 | IGraphicBufferProducer HGraphicBufferProducer; |
| 1235 | typedef android::hardware::media::omx::V1_0:: |
| 1236 | IGraphicBufferSource HGraphicBufferSource; |
| 1237 | OmxStatus s; |
| 1238 | android::sp<HGraphicBufferProducer> gbp; |
| 1239 | android::sp<HGraphicBufferSource> gbs; |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 1240 | |
Chong Zhang | c8ce1d8 | 2019-03-27 10:18:38 -0700 | [diff] [blame] | 1241 | using ::android::hardware::Return; |
| 1242 | Return<void> transStatus = omx->createInputSurface( |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 1243 | [&s, &gbp, &gbs]( |
| 1244 | OmxStatus status, |
| 1245 | const android::sp<HGraphicBufferProducer>& producer, |
| 1246 | const android::sp<HGraphicBufferSource>& source) { |
| 1247 | s = status; |
| 1248 | gbp = producer; |
| 1249 | gbs = source; |
| 1250 | }); |
| 1251 | if (transStatus.isOk() && s == OmxStatus::OK) { |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1252 | return new PersistentSurface(new H2BGraphicBufferProducer(gbp), gbs); |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | return nullptr; |
| 1256 | } |
| 1257 | |
| 1258 | sp<PersistentSurface> CCodec::CreateCompatibleInputSurface() { |
| 1259 | sp<PersistentSurface> surface(CreateInputSurface()); |
| 1260 | |
| 1261 | if (surface == nullptr) { |
| 1262 | surface = CreateOmxInputSurface(); |
| 1263 | } |
| 1264 | |
| 1265 | return surface; |
| 1266 | } |
| 1267 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1268 | void CCodec::createInputSurface() { |
| 1269 | status_t err; |
| 1270 | sp<IGraphicBufferProducer> bufferProducer; |
| 1271 | |
| 1272 | sp<AMessage> inputFormat; |
| 1273 | sp<AMessage> outputFormat; |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 1274 | uint64_t usage = 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1275 | { |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1276 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1277 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1278 | inputFormat = config->mInputFormat; |
| 1279 | outputFormat = config->mOutputFormat; |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 1280 | usage = config->mISConfig ? config->mISConfig->mUsage : 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1281 | } |
| 1282 | |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 1283 | sp<PersistentSurface> persistentSurface = CreateCompatibleInputSurface(); |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1284 | sp<hidl::base::V1_0::IBase> hidlTarget = persistentSurface->getHidlTarget(); |
| 1285 | sp<IInputSurface> hidlInputSurface = IInputSurface::castFrom(hidlTarget); |
| 1286 | sp<HGraphicBufferSource> gbs = HGraphicBufferSource::castFrom(hidlTarget); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1287 | |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1288 | if (hidlInputSurface) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1289 | std::shared_ptr<Codec2Client::InputSurface> inputSurface = |
| 1290 | std::make_shared<Codec2Client::InputSurface>(hidlInputSurface); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1291 | err = setupInputSurface(std::make_shared<C2InputSurfaceWrapper>( |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1292 | inputSurface)); |
| 1293 | bufferProducer = inputSurface->getGraphicBufferProducer(); |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1294 | } else if (gbs) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1295 | int32_t width = 0; |
| 1296 | (void)outputFormat->findInt32("width", &width); |
| 1297 | int32_t height = 0; |
| 1298 | (void)outputFormat->findInt32("height", &height); |
| 1299 | err = setupInputSurface(std::make_shared<GraphicBufferSourceWrapper>( |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1300 | gbs, width, height, usage)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1301 | bufferProducer = persistentSurface->getBufferProducer(); |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1302 | } else { |
| 1303 | ALOGE("Corrupted input surface"); |
| 1304 | mCallback->onInputSurfaceCreationFailed(UNKNOWN_ERROR); |
| 1305 | return; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | if (err != OK) { |
| 1309 | ALOGE("Failed to set up input surface: %d", err); |
| 1310 | mCallback->onInputSurfaceCreationFailed(err); |
| 1311 | return; |
| 1312 | } |
| 1313 | |
| 1314 | mCallback->onInputSurfaceCreated( |
| 1315 | inputFormat, |
| 1316 | outputFormat, |
| 1317 | new BufferProducerWrapper(bufferProducer)); |
| 1318 | } |
| 1319 | |
| 1320 | status_t CCodec::setupInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface) { |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1321 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1322 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1323 | config->mUsingSurface = true; |
| 1324 | |
| 1325 | // we are now using surface - apply default color aspects to input format - as well as |
| 1326 | // get dataspace |
Wonsik Kim | 8a6ed37 | 2019-12-03 16:05:51 -0800 | [diff] [blame] | 1327 | bool inputFormatChanged = config->updateFormats(Config::IS_INPUT); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1328 | ALOGD("input format %s to %s", |
| 1329 | inputFormatChanged ? "changed" : "unchanged", |
| 1330 | config->mInputFormat->debugString().c_str()); |
| 1331 | |
| 1332 | // configure dataspace |
| 1333 | static_assert(sizeof(int32_t) == sizeof(android_dataspace), "dataspace size mismatch"); |
| 1334 | android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN; |
| 1335 | (void)config->mInputFormat->findInt32("android._dataspace", (int32_t*)&dataSpace); |
| 1336 | surface->setDataSpace(dataSpace); |
| 1337 | |
| 1338 | status_t err = mChannel->setInputSurface(surface); |
| 1339 | if (err != OK) { |
| 1340 | // undo input format update |
| 1341 | config->mUsingSurface = false; |
Wonsik Kim | 8a6ed37 | 2019-12-03 16:05:51 -0800 | [diff] [blame] | 1342 | (void)config->updateFormats(Config::IS_INPUT); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1343 | return err; |
| 1344 | } |
| 1345 | config->mInputSurface = surface; |
| 1346 | |
| 1347 | if (config->mISConfig) { |
| 1348 | surface->configure(*config->mISConfig); |
| 1349 | } else { |
| 1350 | ALOGD("ISConfig: no configuration"); |
| 1351 | } |
| 1352 | |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1353 | return OK; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | void CCodec::initiateSetInputSurface(const sp<PersistentSurface> &surface) { |
| 1357 | sp<AMessage> msg = new AMessage(kWhatSetInputSurface, this); |
| 1358 | msg->setObject("surface", surface); |
| 1359 | msg->post(); |
| 1360 | } |
| 1361 | |
| 1362 | void CCodec::setInputSurface(const sp<PersistentSurface> &surface) { |
| 1363 | sp<AMessage> inputFormat; |
| 1364 | sp<AMessage> outputFormat; |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 1365 | uint64_t usage = 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1366 | { |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1367 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1368 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1369 | inputFormat = config->mInputFormat; |
| 1370 | outputFormat = config->mOutputFormat; |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 1371 | usage = config->mISConfig ? config->mISConfig->mUsage : 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1372 | } |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1373 | sp<hidl::base::V1_0::IBase> hidlTarget = surface->getHidlTarget(); |
| 1374 | sp<IInputSurface> inputSurface = IInputSurface::castFrom(hidlTarget); |
| 1375 | sp<HGraphicBufferSource> gbs = HGraphicBufferSource::castFrom(hidlTarget); |
| 1376 | if (inputSurface) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1377 | status_t err = setupInputSurface(std::make_shared<C2InputSurfaceWrapper>( |
| 1378 | std::make_shared<Codec2Client::InputSurface>(inputSurface))); |
| 1379 | if (err != OK) { |
| 1380 | ALOGE("Failed to set up input surface: %d", err); |
| 1381 | mCallback->onInputSurfaceDeclined(err); |
| 1382 | return; |
| 1383 | } |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1384 | } else if (gbs) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1385 | int32_t width = 0; |
| 1386 | (void)outputFormat->findInt32("width", &width); |
| 1387 | int32_t height = 0; |
| 1388 | (void)outputFormat->findInt32("height", &height); |
| 1389 | status_t err = setupInputSurface(std::make_shared<GraphicBufferSourceWrapper>( |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1390 | gbs, width, height, usage)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1391 | if (err != OK) { |
| 1392 | ALOGE("Failed to set up input surface: %d", err); |
| 1393 | mCallback->onInputSurfaceDeclined(err); |
| 1394 | return; |
| 1395 | } |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1396 | } else { |
| 1397 | ALOGE("Failed to set input surface: Corrupted surface."); |
| 1398 | mCallback->onInputSurfaceDeclined(UNKNOWN_ERROR); |
| 1399 | return; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1400 | } |
| 1401 | mCallback->onInputSurfaceAccepted(inputFormat, outputFormat); |
| 1402 | } |
| 1403 | |
| 1404 | void CCodec::initiateStart() { |
| 1405 | auto setStarting = [this] { |
| 1406 | Mutexed<State>::Locked state(mState); |
| 1407 | if (state->get() != ALLOCATED) { |
| 1408 | return UNKNOWN_ERROR; |
| 1409 | } |
| 1410 | state->set(STARTING); |
| 1411 | return OK; |
| 1412 | }; |
| 1413 | if (tryAndReportOnError(setStarting) != OK) { |
| 1414 | return; |
| 1415 | } |
| 1416 | |
| 1417 | (new AMessage(kWhatStart, this))->post(); |
| 1418 | } |
| 1419 | |
| 1420 | void CCodec::start() { |
| 1421 | std::shared_ptr<Codec2Client::Component> comp; |
| 1422 | auto checkStarting = [this, &comp] { |
| 1423 | Mutexed<State>::Locked state(mState); |
| 1424 | if (state->get() != STARTING) { |
| 1425 | return UNKNOWN_ERROR; |
| 1426 | } |
| 1427 | comp = state->comp; |
| 1428 | return OK; |
| 1429 | }; |
| 1430 | if (tryAndReportOnError(checkStarting) != OK) { |
| 1431 | return; |
| 1432 | } |
| 1433 | |
| 1434 | c2_status_t err = comp->start(); |
| 1435 | if (err != C2_OK) { |
| 1436 | mCallback->onError(toStatusT(err, C2_OPERATION_Component_start), |
| 1437 | ACTION_CODE_FATAL); |
| 1438 | return; |
| 1439 | } |
| 1440 | sp<AMessage> inputFormat; |
| 1441 | sp<AMessage> outputFormat; |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1442 | status_t err2 = OK; |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 1443 | bool buffersBoundToCodec = false; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1444 | { |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1445 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1446 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1447 | inputFormat = config->mInputFormat; |
Wonsik Kim | 274c832 | 2020-02-28 10:42:21 -0800 | [diff] [blame] | 1448 | // start triggers format dup |
| 1449 | outputFormat = config->mOutputFormat = config->mOutputFormat->dup(); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1450 | if (config->mInputSurface) { |
| 1451 | err2 = config->mInputSurface->start(); |
| 1452 | } |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 1453 | buffersBoundToCodec = config->mBuffersBoundToCodec; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1454 | } |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1455 | if (err2 != OK) { |
| 1456 | mCallback->onError(err2, ACTION_CODE_FATAL); |
| 1457 | return; |
| 1458 | } |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 1459 | err2 = mChannel->start(inputFormat, outputFormat, buffersBoundToCodec); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1460 | if (err2 != OK) { |
| 1461 | mCallback->onError(err2, ACTION_CODE_FATAL); |
| 1462 | return; |
| 1463 | } |
| 1464 | |
| 1465 | auto setRunning = [this] { |
| 1466 | Mutexed<State>::Locked state(mState); |
| 1467 | if (state->get() != STARTING) { |
| 1468 | return UNKNOWN_ERROR; |
| 1469 | } |
| 1470 | state->set(RUNNING); |
| 1471 | return OK; |
| 1472 | }; |
| 1473 | if (tryAndReportOnError(setRunning) != OK) { |
| 1474 | return; |
| 1475 | } |
| 1476 | mCallback->onStartCompleted(); |
| 1477 | |
| 1478 | (void)mChannel->requestInitialInputBuffers(); |
| 1479 | } |
| 1480 | |
| 1481 | void CCodec::initiateShutdown(bool keepComponentAllocated) { |
| 1482 | if (keepComponentAllocated) { |
| 1483 | initiateStop(); |
| 1484 | } else { |
| 1485 | initiateRelease(); |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | void CCodec::initiateStop() { |
| 1490 | { |
| 1491 | Mutexed<State>::Locked state(mState); |
| 1492 | if (state->get() == ALLOCATED |
| 1493 | || state->get() == RELEASED |
| 1494 | || state->get() == STOPPING |
| 1495 | || state->get() == RELEASING) { |
| 1496 | // We're already stopped, released, or doing it right now. |
| 1497 | state.unlock(); |
| 1498 | mCallback->onStopCompleted(); |
| 1499 | state.lock(); |
| 1500 | return; |
| 1501 | } |
| 1502 | state->set(STOPPING); |
| 1503 | } |
| 1504 | |
Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1505 | mChannel->reset(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1506 | (new AMessage(kWhatStop, this))->post(); |
| 1507 | } |
| 1508 | |
| 1509 | void CCodec::stop() { |
| 1510 | std::shared_ptr<Codec2Client::Component> comp; |
| 1511 | { |
| 1512 | Mutexed<State>::Locked state(mState); |
| 1513 | if (state->get() == RELEASING) { |
| 1514 | state.unlock(); |
| 1515 | // We're already stopped or release is in progress. |
| 1516 | mCallback->onStopCompleted(); |
| 1517 | state.lock(); |
| 1518 | return; |
| 1519 | } else if (state->get() != STOPPING) { |
| 1520 | state.unlock(); |
| 1521 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 1522 | state.lock(); |
| 1523 | return; |
| 1524 | } |
| 1525 | comp = state->comp; |
| 1526 | } |
| 1527 | status_t err = comp->stop(); |
| 1528 | if (err != C2_OK) { |
| 1529 | // TODO: convert err into status_t |
| 1530 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 1531 | } |
| 1532 | |
| 1533 | { |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1534 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1535 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1536 | if (config->mInputSurface) { |
| 1537 | config->mInputSurface->disconnect(); |
| 1538 | config->mInputSurface = nullptr; |
| 1539 | } |
| 1540 | } |
| 1541 | { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1542 | Mutexed<State>::Locked state(mState); |
| 1543 | if (state->get() == STOPPING) { |
| 1544 | state->set(ALLOCATED); |
| 1545 | } |
| 1546 | } |
| 1547 | mCallback->onStopCompleted(); |
| 1548 | } |
| 1549 | |
| 1550 | void CCodec::initiateRelease(bool sendCallback /* = true */) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1551 | bool clearInputSurfaceIfNeeded = false; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1552 | { |
| 1553 | Mutexed<State>::Locked state(mState); |
| 1554 | if (state->get() == RELEASED || state->get() == RELEASING) { |
| 1555 | // We're already released or doing it right now. |
| 1556 | if (sendCallback) { |
| 1557 | state.unlock(); |
| 1558 | mCallback->onReleaseCompleted(); |
| 1559 | state.lock(); |
| 1560 | } |
| 1561 | return; |
| 1562 | } |
| 1563 | if (state->get() == ALLOCATING) { |
| 1564 | state->set(RELEASING); |
| 1565 | // With the altered state allocate() would fail and clean up. |
| 1566 | if (sendCallback) { |
| 1567 | state.unlock(); |
| 1568 | mCallback->onReleaseCompleted(); |
| 1569 | state.lock(); |
| 1570 | } |
| 1571 | return; |
| 1572 | } |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1573 | if (state->get() == STARTING |
| 1574 | || state->get() == RUNNING |
| 1575 | || state->get() == STOPPING) { |
| 1576 | // Input surface may have been started, so clean up is needed. |
| 1577 | clearInputSurfaceIfNeeded = true; |
| 1578 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1579 | state->set(RELEASING); |
| 1580 | } |
| 1581 | |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1582 | if (clearInputSurfaceIfNeeded) { |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1583 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1584 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1585 | if (config->mInputSurface) { |
| 1586 | config->mInputSurface->disconnect(); |
| 1587 | config->mInputSurface = nullptr; |
| 1588 | } |
| 1589 | } |
| 1590 | |
Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1591 | mChannel->reset(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1592 | // thiz holds strong ref to this while the thread is running. |
| 1593 | sp<CCodec> thiz(this); |
| 1594 | std::thread([thiz, sendCallback] { thiz->release(sendCallback); }).detach(); |
| 1595 | } |
| 1596 | |
| 1597 | void CCodec::release(bool sendCallback) { |
| 1598 | std::shared_ptr<Codec2Client::Component> comp; |
| 1599 | { |
| 1600 | Mutexed<State>::Locked state(mState); |
| 1601 | if (state->get() == RELEASED) { |
| 1602 | if (sendCallback) { |
| 1603 | state.unlock(); |
| 1604 | mCallback->onReleaseCompleted(); |
| 1605 | state.lock(); |
| 1606 | } |
| 1607 | return; |
| 1608 | } |
| 1609 | comp = state->comp; |
| 1610 | } |
| 1611 | comp->release(); |
| 1612 | |
| 1613 | { |
| 1614 | Mutexed<State>::Locked state(mState); |
| 1615 | state->set(RELEASED); |
| 1616 | state->comp.reset(); |
| 1617 | } |
Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1618 | (new AMessage(kWhatRelease, this))->post(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1619 | if (sendCallback) { |
| 1620 | mCallback->onReleaseCompleted(); |
| 1621 | } |
| 1622 | } |
| 1623 | |
| 1624 | status_t CCodec::setSurface(const sp<Surface> &surface) { |
| 1625 | return mChannel->setSurface(surface); |
| 1626 | } |
| 1627 | |
| 1628 | void CCodec::signalFlush() { |
| 1629 | status_t err = [this] { |
| 1630 | Mutexed<State>::Locked state(mState); |
| 1631 | if (state->get() == FLUSHED) { |
| 1632 | return ALREADY_EXISTS; |
| 1633 | } |
| 1634 | if (state->get() != RUNNING) { |
| 1635 | return UNKNOWN_ERROR; |
| 1636 | } |
| 1637 | state->set(FLUSHING); |
| 1638 | return OK; |
| 1639 | }(); |
| 1640 | switch (err) { |
| 1641 | case ALREADY_EXISTS: |
| 1642 | mCallback->onFlushCompleted(); |
| 1643 | return; |
| 1644 | case OK: |
| 1645 | break; |
| 1646 | default: |
| 1647 | mCallback->onError(err, ACTION_CODE_FATAL); |
| 1648 | return; |
| 1649 | } |
| 1650 | |
| 1651 | mChannel->stop(); |
| 1652 | (new AMessage(kWhatFlush, this))->post(); |
| 1653 | } |
| 1654 | |
| 1655 | void CCodec::flush() { |
| 1656 | std::shared_ptr<Codec2Client::Component> comp; |
| 1657 | auto checkFlushing = [this, &comp] { |
| 1658 | Mutexed<State>::Locked state(mState); |
| 1659 | if (state->get() != FLUSHING) { |
| 1660 | return UNKNOWN_ERROR; |
| 1661 | } |
| 1662 | comp = state->comp; |
| 1663 | return OK; |
| 1664 | }; |
| 1665 | if (tryAndReportOnError(checkFlushing) != OK) { |
| 1666 | return; |
| 1667 | } |
| 1668 | |
| 1669 | std::list<std::unique_ptr<C2Work>> flushedWork; |
| 1670 | c2_status_t err = comp->flush(C2Component::FLUSH_COMPONENT, &flushedWork); |
| 1671 | { |
| 1672 | Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue); |
| 1673 | flushedWork.splice(flushedWork.end(), *queue); |
| 1674 | } |
| 1675 | if (err != C2_OK) { |
| 1676 | // TODO: convert err into status_t |
| 1677 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 1678 | } |
| 1679 | |
| 1680 | mChannel->flush(flushedWork); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1681 | |
| 1682 | { |
| 1683 | Mutexed<State>::Locked state(mState); |
Iris Chang | ce521ee | 2019-07-05 16:18:54 +0800 | [diff] [blame] | 1684 | if (state->get() == FLUSHING) { |
| 1685 | state->set(FLUSHED); |
| 1686 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1687 | } |
| 1688 | mCallback->onFlushCompleted(); |
| 1689 | } |
| 1690 | |
| 1691 | void CCodec::signalResume() { |
Wonsik Kim | e75a5da | 2020-02-14 17:29:03 -0800 | [diff] [blame] | 1692 | std::shared_ptr<Codec2Client::Component> comp; |
| 1693 | auto setResuming = [this, &comp] { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1694 | Mutexed<State>::Locked state(mState); |
| 1695 | if (state->get() != FLUSHED) { |
| 1696 | return UNKNOWN_ERROR; |
| 1697 | } |
| 1698 | state->set(RESUMING); |
Wonsik Kim | e75a5da | 2020-02-14 17:29:03 -0800 | [diff] [blame] | 1699 | comp = state->comp; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1700 | return OK; |
| 1701 | }; |
| 1702 | if (tryAndReportOnError(setResuming) != OK) { |
| 1703 | return; |
| 1704 | } |
| 1705 | |
Wonsik Kim | e75a5da | 2020-02-14 17:29:03 -0800 | [diff] [blame] | 1706 | { |
| 1707 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1708 | const std::unique_ptr<Config> &config = *configLocked; |
| 1709 | config->queryConfiguration(comp); |
| 1710 | } |
| 1711 | |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 1712 | (void)mChannel->start(nullptr, nullptr, [&]{ |
| 1713 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1714 | const std::unique_ptr<Config> &config = *configLocked; |
| 1715 | return config->mBuffersBoundToCodec; |
| 1716 | }()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1717 | |
| 1718 | { |
| 1719 | Mutexed<State>::Locked state(mState); |
| 1720 | if (state->get() != RESUMING) { |
| 1721 | state.unlock(); |
| 1722 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 1723 | state.lock(); |
| 1724 | return; |
| 1725 | } |
| 1726 | state->set(RUNNING); |
| 1727 | } |
| 1728 | |
| 1729 | (void)mChannel->requestInitialInputBuffers(); |
| 1730 | } |
| 1731 | |
Wonsik Kim | aa484ac | 2019-02-13 16:54:02 -0800 | [diff] [blame] | 1732 | void CCodec::signalSetParameters(const sp<AMessage> &msg) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1733 | std::shared_ptr<Codec2Client::Component> comp; |
| 1734 | auto checkState = [this, &comp] { |
| 1735 | Mutexed<State>::Locked state(mState); |
| 1736 | if (state->get() == RELEASED) { |
| 1737 | return INVALID_OPERATION; |
| 1738 | } |
| 1739 | comp = state->comp; |
| 1740 | return OK; |
| 1741 | }; |
| 1742 | if (tryAndReportOnError(checkState) != OK) { |
| 1743 | return; |
| 1744 | } |
| 1745 | |
Wonsik Kim | aa484ac | 2019-02-13 16:54:02 -0800 | [diff] [blame] | 1746 | // NOTE: We used to ignore "bitrate" at setParameters; replicate |
| 1747 | // the behavior here. |
| 1748 | sp<AMessage> params = msg; |
| 1749 | int32_t bitrate; |
| 1750 | if (params->findInt32(KEY_BIT_RATE, &bitrate)) { |
| 1751 | params = msg->dup(); |
| 1752 | params->removeEntryAt(params->findEntryByName(KEY_BIT_RATE)); |
| 1753 | } |
| 1754 | |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1755 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1756 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1757 | |
| 1758 | /** |
| 1759 | * Handle input surface parameters |
| 1760 | */ |
| 1761 | if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE)) |
Wonsik Kim | 8a6ed37 | 2019-12-03 16:05:51 -0800 | [diff] [blame] | 1762 | && (config->mDomain & Config::IS_ENCODER) |
| 1763 | && config->mInputSurface && config->mISConfig) { |
Chong Zhang | 038e8f8 | 2019-02-06 19:05:14 -0800 | [diff] [blame] | 1764 | (void)params->findInt64(PARAMETER_KEY_OFFSET_TIME, &config->mISConfig->mTimeOffsetUs); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1765 | |
| 1766 | if (params->findInt64("skip-frames-before", &config->mISConfig->mStartAtUs)) { |
| 1767 | config->mISConfig->mStopped = false; |
| 1768 | } else if (params->findInt64("stop-time-us", &config->mISConfig->mStopAtUs)) { |
| 1769 | config->mISConfig->mStopped = true; |
| 1770 | } |
| 1771 | |
| 1772 | int32_t value; |
Chong Zhang | 038e8f8 | 2019-02-06 19:05:14 -0800 | [diff] [blame] | 1773 | if (params->findInt32(PARAMETER_KEY_SUSPEND, &value)) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1774 | config->mISConfig->mSuspended = value; |
| 1775 | config->mISConfig->mSuspendAtUs = -1; |
Chong Zhang | 038e8f8 | 2019-02-06 19:05:14 -0800 | [diff] [blame] | 1776 | (void)params->findInt64(PARAMETER_KEY_SUSPEND_TIME, &config->mISConfig->mSuspendAtUs); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | (void)config->mInputSurface->configure(*config->mISConfig); |
| 1780 | if (config->mISConfig->mStopped) { |
| 1781 | config->mInputFormat->setInt64( |
| 1782 | "android._stop-time-offset-us", config->mISConfig->mInputDelayUs); |
| 1783 | } |
| 1784 | } |
| 1785 | |
| 1786 | std::vector<std::unique_ptr<C2Param>> configUpdate; |
| 1787 | (void)config->getConfigUpdateFromSdkParams( |
| 1788 | comp, params, Config::IS_PARAM, C2_MAY_BLOCK, &configUpdate); |
| 1789 | // Prefer to pass parameters to the buffer channel, so they can be synchronized with the frames. |
| 1790 | // Parameter synchronization is not defined when using input surface. For now, route |
| 1791 | // these directly to the component. |
| 1792 | if (config->mInputSurface == nullptr |
| 1793 | && (property_get_bool("debug.stagefright.ccodec_delayed_params", false) |
| 1794 | || comp->getName().find("c2.android.") == 0)) { |
| 1795 | mChannel->setParameters(configUpdate); |
| 1796 | } else { |
Wonsik Kim | 970bf0b | 2020-11-10 11:54:15 -0800 | [diff] [blame] | 1797 | sp<AMessage> outputFormat = config->mOutputFormat; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1798 | (void)config->setParameters(comp, configUpdate, C2_MAY_BLOCK); |
Wonsik Kim | 970bf0b | 2020-11-10 11:54:15 -0800 | [diff] [blame] | 1799 | RevertOutputFormatIfNeeded(outputFormat, config->mOutputFormat); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | void CCodec::signalEndOfInputStream() { |
| 1804 | mCallback->onSignaledInputEOS(mChannel->signalEndOfInputStream()); |
| 1805 | } |
| 1806 | |
| 1807 | void CCodec::signalRequestIDRFrame() { |
| 1808 | std::shared_ptr<Codec2Client::Component> comp; |
| 1809 | { |
| 1810 | Mutexed<State>::Locked state(mState); |
| 1811 | if (state->get() == RELEASED) { |
| 1812 | ALOGD("no IDR request sent since component is released"); |
| 1813 | return; |
| 1814 | } |
| 1815 | comp = state->comp; |
| 1816 | } |
| 1817 | ALOGV("request IDR"); |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1818 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1819 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1820 | std::vector<std::unique_ptr<C2Param>> params; |
| 1821 | params.push_back( |
| 1822 | std::make_unique<C2StreamRequestSyncFrameTuning::output>(0u, true)); |
| 1823 | config->setParameters(comp, params, C2_MAY_BLOCK); |
| 1824 | } |
| 1825 | |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1826 | void CCodec::onWorkDone(std::list<std::unique_ptr<C2Work>> &workItems) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1827 | if (!workItems.empty()) { |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1828 | Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue); |
| 1829 | queue->splice(queue->end(), workItems); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1830 | } |
| 1831 | (new AMessage(kWhatWorkDone, this))->post(); |
| 1832 | } |
| 1833 | |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1834 | void CCodec::onInputBufferDone(uint64_t frameIndex, size_t arrayIndex) { |
| 1835 | mChannel->onInputBufferDone(frameIndex, arrayIndex); |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 1836 | if (arrayIndex == 0) { |
| 1837 | // We always put no more than one buffer per work, if we use an input surface. |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1838 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1839 | const std::unique_ptr<Config> &config = *configLocked; |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 1840 | if (config->mInputSurface) { |
| 1841 | config->mInputSurface->onInputBufferDone(frameIndex); |
| 1842 | } |
| 1843 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | void CCodec::onMessageReceived(const sp<AMessage> &msg) { |
| 1847 | TimePoint now = std::chrono::steady_clock::now(); |
| 1848 | CCodecWatchdog::getInstance()->watch(this); |
| 1849 | switch (msg->what()) { |
| 1850 | case kWhatAllocate: { |
| 1851 | // C2ComponentStore::createComponent() should return within 100ms. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1852 | setDeadline(now, 1500ms, "allocate"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1853 | sp<RefBase> obj; |
| 1854 | CHECK(msg->findObject("codecInfo", &obj)); |
| 1855 | allocate((MediaCodecInfo *)obj.get()); |
| 1856 | break; |
| 1857 | } |
| 1858 | case kWhatConfigure: { |
| 1859 | // C2Component::commit_sm() should return within 5ms. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1860 | setDeadline(now, 1500ms, "configure"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1861 | sp<AMessage> format; |
| 1862 | CHECK(msg->findMessage("format", &format)); |
| 1863 | configure(format); |
| 1864 | break; |
| 1865 | } |
| 1866 | case kWhatStart: { |
| 1867 | // C2Component::start() should return within 500ms. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1868 | setDeadline(now, 1500ms, "start"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1869 | start(); |
| 1870 | break; |
| 1871 | } |
| 1872 | case kWhatStop: { |
| 1873 | // C2Component::stop() should return within 500ms. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1874 | setDeadline(now, 1500ms, "stop"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1875 | stop(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1876 | break; |
| 1877 | } |
| 1878 | case kWhatFlush: { |
| 1879 | // C2Component::flush_sm() should return within 5ms. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1880 | setDeadline(now, 1500ms, "flush"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1881 | flush(); |
| 1882 | break; |
| 1883 | } |
Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1884 | case kWhatRelease: { |
| 1885 | mChannel->release(); |
| 1886 | mClient.reset(); |
| 1887 | mClientListener.reset(); |
| 1888 | break; |
| 1889 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1890 | case kWhatCreateInputSurface: { |
| 1891 | // Surface operations may be briefly blocking. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1892 | setDeadline(now, 1500ms, "createInputSurface"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1893 | createInputSurface(); |
| 1894 | break; |
| 1895 | } |
| 1896 | case kWhatSetInputSurface: { |
| 1897 | // Surface operations may be briefly blocking. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1898 | setDeadline(now, 1500ms, "setInputSurface"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1899 | sp<RefBase> obj; |
| 1900 | CHECK(msg->findObject("surface", &obj)); |
| 1901 | sp<PersistentSurface> surface(static_cast<PersistentSurface *>(obj.get())); |
| 1902 | setInputSurface(surface); |
| 1903 | break; |
| 1904 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1905 | case kWhatWorkDone: { |
| 1906 | std::unique_ptr<C2Work> work; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1907 | bool shouldPost = false; |
| 1908 | { |
| 1909 | Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue); |
| 1910 | if (queue->empty()) { |
| 1911 | break; |
| 1912 | } |
| 1913 | work.swap(queue->front()); |
| 1914 | queue->pop_front(); |
| 1915 | shouldPost = !queue->empty(); |
| 1916 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1917 | if (shouldPost) { |
| 1918 | (new AMessage(kWhatWorkDone, this))->post(); |
| 1919 | } |
| 1920 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1921 | // handle configuration changes in work done |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1922 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1923 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1924 | Config::Watcher<C2StreamInitDataInfo::output> initData = |
| 1925 | config->watch<C2StreamInitDataInfo::output>(); |
| 1926 | if (!work->worklets.empty() |
| 1927 | && (work->worklets.front()->output.flags |
| 1928 | & C2FrameData::FLAG_DISCARD_FRAME) == 0) { |
| 1929 | |
| 1930 | // copy buffer info to config |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1931 | std::vector<std::unique_ptr<C2Param>> updates; |
| 1932 | for (const std::unique_ptr<C2Param> ¶m |
| 1933 | : work->worklets.front()->output.configUpdate) { |
| 1934 | updates.push_back(C2Param::Copy(*param)); |
| 1935 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1936 | unsigned stream = 0; |
| 1937 | for (const std::shared_ptr<C2Buffer> &buf : work->worklets.front()->output.buffers) { |
| 1938 | for (const std::shared_ptr<const C2Info> &info : buf->info()) { |
| 1939 | // move all info into output-stream #0 domain |
| 1940 | updates.emplace_back(C2Param::CopyAsStream(*info, true /* output */, stream)); |
| 1941 | } |
George Burgess IV | c813a59 | 2020-02-22 22:54:44 -0800 | [diff] [blame] | 1942 | |
| 1943 | const std::vector<C2ConstGraphicBlock> blocks = buf->data().graphicBlocks(); |
| 1944 | // for now only do the first block |
| 1945 | if (!blocks.empty()) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1946 | // ALOGV("got output buffer with crop %u,%u+%u,%u and size %u,%u", |
| 1947 | // block.crop().left, block.crop().top, |
| 1948 | // block.crop().width, block.crop().height, |
| 1949 | // block.width(), block.height()); |
George Burgess IV | c813a59 | 2020-02-22 22:54:44 -0800 | [diff] [blame] | 1950 | const C2ConstGraphicBlock &block = blocks[0]; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1951 | updates.emplace_back(new C2StreamCropRectInfo::output(stream, block.crop())); |
| 1952 | updates.emplace_back(new C2StreamPictureSizeInfo::output( |
Harish Mahendrakar | f7c49e2 | 2019-05-24 14:19:16 -0700 | [diff] [blame] | 1953 | stream, block.crop().width, block.crop().height)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1954 | } |
| 1955 | ++stream; |
| 1956 | } |
| 1957 | |
Wonsik Kim | 970bf0b | 2020-11-10 11:54:15 -0800 | [diff] [blame] | 1958 | sp<AMessage> outputFormat = config->mOutputFormat; |
| 1959 | config->updateConfiguration(updates, config->mOutputDomain); |
| 1960 | RevertOutputFormatIfNeeded(outputFormat, config->mOutputFormat); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1961 | |
| 1962 | // copy standard infos to graphic buffers if not already present (otherwise, we |
| 1963 | // may overwrite the actual intermediate value with a final value) |
| 1964 | stream = 0; |
George Burgess IV | 3f1a090 | 2020-03-18 12:58:32 -0700 | [diff] [blame] | 1965 | const static C2Param::Index stdGfxInfos[] = { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1966 | C2StreamRotationInfo::output::PARAM_TYPE, |
| 1967 | C2StreamColorAspectsInfo::output::PARAM_TYPE, |
| 1968 | C2StreamDataSpaceInfo::output::PARAM_TYPE, |
| 1969 | C2StreamHdrStaticInfo::output::PARAM_TYPE, |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 1970 | C2StreamHdr10PlusInfo::output::PARAM_TYPE, |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1971 | C2StreamPixelAspectRatioInfo::output::PARAM_TYPE, |
| 1972 | C2StreamSurfaceScalingInfo::output::PARAM_TYPE |
| 1973 | }; |
| 1974 | for (const std::shared_ptr<C2Buffer> &buf : work->worklets.front()->output.buffers) { |
| 1975 | if (buf->data().graphicBlocks().size()) { |
| 1976 | for (C2Param::Index ix : stdGfxInfos) { |
| 1977 | if (!buf->hasInfo(ix)) { |
| 1978 | const C2Param *param = |
| 1979 | config->getConfigParameterValue(ix.withStream(stream)); |
| 1980 | if (param) { |
| 1981 | std::shared_ptr<C2Param> info(C2Param::Copy(*param)); |
| 1982 | buf->setInfo(std::static_pointer_cast<C2Info>(info)); |
| 1983 | } |
| 1984 | } |
| 1985 | } |
| 1986 | } |
| 1987 | ++stream; |
| 1988 | } |
| 1989 | } |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 1990 | if (config->mInputSurface) { |
| 1991 | config->mInputSurface->onInputBufferDone(work->input.ordinal.frameIndex); |
| 1992 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1993 | mChannel->onWorkDone( |
Wonsik Kim | 970bf0b | 2020-11-10 11:54:15 -0800 | [diff] [blame] | 1994 | std::move(work), config->mOutputFormat, |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1995 | initData.hasChanged() ? initData.update().get() : nullptr); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1996 | break; |
| 1997 | } |
| 1998 | case kWhatWatch: { |
| 1999 | // watch message already posted; no-op. |
| 2000 | break; |
| 2001 | } |
| 2002 | default: { |
| 2003 | ALOGE("unrecognized message"); |
| 2004 | break; |
| 2005 | } |
| 2006 | } |
| 2007 | setDeadline(TimePoint::max(), 0ms, "none"); |
| 2008 | } |
| 2009 | |
| 2010 | void CCodec::setDeadline( |
| 2011 | const TimePoint &now, |
| 2012 | const std::chrono::milliseconds &timeout, |
| 2013 | const char *name) { |
| 2014 | int32_t mult = std::max(1, property_get_int32("debug.stagefright.ccodec_timeout_mult", 1)); |
| 2015 | Mutexed<NamedTimePoint>::Locked deadline(mDeadline); |
| 2016 | deadline->set(now + (timeout * mult), name); |
| 2017 | } |
| 2018 | |
| 2019 | void CCodec::initiateReleaseIfStuck() { |
| 2020 | std::string name; |
| 2021 | bool pendingDeadline = false; |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 2022 | { |
| 2023 | Mutexed<NamedTimePoint>::Locked deadline(mDeadline); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 2024 | if (deadline->get() < std::chrono::steady_clock::now()) { |
| 2025 | name = deadline->getName(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 2026 | } |
| 2027 | if (deadline->get() != TimePoint::max()) { |
| 2028 | pendingDeadline = true; |
| 2029 | } |
| 2030 | } |
| 2031 | if (name.empty()) { |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 2032 | constexpr std::chrono::steady_clock::duration kWorkDurationThreshold = 3s; |
| 2033 | std::chrono::steady_clock::duration elapsed = mChannel->elapsed(); |
| 2034 | if (elapsed >= kWorkDurationThreshold) { |
| 2035 | name = "queue"; |
| 2036 | } |
| 2037 | if (elapsed > 0s) { |
| 2038 | pendingDeadline = true; |
| 2039 | } |
| 2040 | } |
| 2041 | if (name.empty()) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 2042 | // We're not stuck. |
| 2043 | if (pendingDeadline) { |
| 2044 | // If we are not stuck yet but still has deadline coming up, |
| 2045 | // post watch message to check back later. |
| 2046 | (new AMessage(kWhatWatch, this))->post(); |
| 2047 | } |
| 2048 | return; |
| 2049 | } |
| 2050 | |
| 2051 | ALOGW("previous call to %s exceeded timeout", name.c_str()); |
| 2052 | initiateRelease(false); |
| 2053 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 2054 | } |
| 2055 | |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 2056 | // static |
| 2057 | PersistentSurface *CCodec::CreateInputSurface() { |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 2058 | using namespace android; |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 2059 | using ::android::hardware::media::omx::V1_0::implementation::TWGraphicBufferSource; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 2060 | // Attempt to create a Codec2's input surface. |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 2061 | std::shared_ptr<Codec2Client::InputSurface> inputSurface = |
| 2062 | Codec2Client::CreateInputSurface(); |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 2063 | if (!inputSurface) { |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 2064 | if (property_get_int32("debug.stagefright.c2inputsurface", 0) == -1) { |
| 2065 | sp<IGraphicBufferProducer> gbp; |
| 2066 | sp<OmxGraphicBufferSource> gbs = new OmxGraphicBufferSource(); |
| 2067 | status_t err = gbs->initCheck(); |
| 2068 | if (err != OK) { |
| 2069 | ALOGE("Failed to create persistent input surface: error %d", err); |
| 2070 | return nullptr; |
| 2071 | } |
| 2072 | return new PersistentSurface( |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 2073 | gbs->getIGraphicBufferProducer(), new TWGraphicBufferSource(gbs)); |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 2074 | } else { |
| 2075 | return nullptr; |
| 2076 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 2077 | } |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 2078 | return new PersistentSurface( |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 2079 | inputSurface->getGraphicBufferProducer(), |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 2080 | static_cast<sp<android::hidl::base::V1_0::IBase>>( |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 2081 | inputSurface->getHalInterface())); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 2082 | } |
| 2083 | |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2084 | class IntfCache { |
| 2085 | public: |
| 2086 | IntfCache() = default; |
| 2087 | |
| 2088 | status_t init(const std::string &name) { |
| 2089 | std::shared_ptr<Codec2Client::Interface> intf{ |
| 2090 | Codec2Client::CreateInterfaceByName(name.c_str())}; |
| 2091 | if (!intf) { |
| 2092 | ALOGW("IntfCache [%s]: Unrecognized interface name", name.c_str()); |
| 2093 | mInitStatus = NO_INIT; |
| 2094 | return NO_INIT; |
| 2095 | } |
| 2096 | const static C2StreamUsageTuning::input sUsage{0u /* stream id */}; |
| 2097 | mFields.push_back(C2FieldSupportedValuesQuery::Possible( |
| 2098 | C2ParamField{&sUsage, &sUsage.value})); |
| 2099 | c2_status_t err = intf->querySupportedValues(mFields, C2_MAY_BLOCK); |
| 2100 | if (err != C2_OK) { |
| 2101 | ALOGW("IntfCache [%s]: failed to query usage supported value (err=%d)", |
| 2102 | name.c_str(), err); |
| 2103 | mFields[0].status = err; |
| 2104 | } |
| 2105 | std::vector<std::unique_ptr<C2Param>> params; |
| 2106 | err = intf->query( |
| 2107 | {&mApiFeatures}, |
| 2108 | {C2PortAllocatorsTuning::input::PARAM_TYPE}, |
| 2109 | C2_MAY_BLOCK, |
| 2110 | ¶ms); |
| 2111 | if (err != C2_OK && err != C2_BAD_INDEX) { |
| 2112 | ALOGW("IntfCache [%s]: failed to query api features (err=%d)", |
| 2113 | name.c_str(), err); |
| 2114 | } |
| 2115 | while (!params.empty()) { |
| 2116 | C2Param *param = params.back().release(); |
| 2117 | params.pop_back(); |
| 2118 | if (!param) { |
| 2119 | continue; |
| 2120 | } |
| 2121 | if (param->type() == C2PortAllocatorsTuning::input::PARAM_TYPE) { |
| 2122 | mInputAllocators.reset( |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 2123 | C2PortAllocatorsTuning::input::From(param)); |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2124 | } |
| 2125 | } |
| 2126 | mInitStatus = OK; |
| 2127 | return OK; |
Wonsik Kim | fcf46c3 | 2020-04-22 13:50:45 -0700 | [diff] [blame] | 2128 | } |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2129 | |
| 2130 | status_t initCheck() const { return mInitStatus; } |
| 2131 | |
| 2132 | const C2FieldSupportedValuesQuery &getUsageSupportedValues() const { |
| 2133 | CHECK_EQ(1u, mFields.size()); |
| 2134 | return mFields[0]; |
| 2135 | } |
| 2136 | |
| 2137 | const C2ApiFeaturesSetting &getApiFeatures() const { |
| 2138 | return mApiFeatures; |
| 2139 | } |
| 2140 | |
| 2141 | const C2PortAllocatorsTuning::input &getInputAllocators() const { |
| 2142 | static std::unique_ptr<C2PortAllocatorsTuning::input> sInvalidated = []{ |
| 2143 | std::unique_ptr<C2PortAllocatorsTuning::input> param = |
| 2144 | C2PortAllocatorsTuning::input::AllocUnique(0); |
| 2145 | param->invalidate(); |
| 2146 | return param; |
| 2147 | }(); |
| 2148 | return mInputAllocators ? *mInputAllocators : *sInvalidated; |
| 2149 | } |
| 2150 | |
| 2151 | private: |
| 2152 | status_t mInitStatus{NO_INIT}; |
| 2153 | |
| 2154 | std::vector<C2FieldSupportedValuesQuery> mFields; |
| 2155 | C2ApiFeaturesSetting mApiFeatures; |
| 2156 | std::unique_ptr<C2PortAllocatorsTuning::input> mInputAllocators; |
| 2157 | }; |
| 2158 | |
| 2159 | static const IntfCache &GetIntfCache(const std::string &name) { |
| 2160 | static IntfCache sNullIntfCache; |
| 2161 | static std::mutex sMutex; |
| 2162 | static std::map<std::string, IntfCache> sCache; |
| 2163 | std::unique_lock<std::mutex> lock{sMutex}; |
| 2164 | auto it = sCache.find(name); |
| 2165 | if (it == sCache.end()) { |
| 2166 | lock.unlock(); |
| 2167 | IntfCache intfCache; |
| 2168 | status_t err = intfCache.init(name); |
| 2169 | if (err != OK) { |
| 2170 | return sNullIntfCache; |
| 2171 | } |
| 2172 | lock.lock(); |
| 2173 | it = sCache.insert({name, std::move(intfCache)}).first; |
| 2174 | } |
| 2175 | return it->second; |
Wonsik Kim | fcf46c3 | 2020-04-22 13:50:45 -0700 | [diff] [blame] | 2176 | } |
| 2177 | |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 2178 | static status_t GetCommonAllocatorIds( |
| 2179 | const std::vector<std::string> &names, |
| 2180 | C2Allocator::type_t type, |
| 2181 | std::set<C2Allocator::id_t> *ids) { |
| 2182 | int poolMask = GetCodec2PoolMask(); |
| 2183 | C2PlatformAllocatorStore::id_t preferredLinearId = GetPreferredLinearAllocatorId(poolMask); |
| 2184 | C2Allocator::id_t defaultAllocatorId = |
| 2185 | (type == C2Allocator::LINEAR) ? preferredLinearId : C2PlatformAllocatorStore::GRALLOC; |
| 2186 | |
| 2187 | ids->clear(); |
| 2188 | if (names.empty()) { |
| 2189 | return OK; |
| 2190 | } |
Wonsik Kim | fcf46c3 | 2020-04-22 13:50:45 -0700 | [diff] [blame] | 2191 | bool firstIteration = true; |
| 2192 | for (const std::string &name : names) { |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2193 | const IntfCache &intfCache = GetIntfCache(name); |
| 2194 | if (intfCache.initCheck() != OK) { |
Wonsik Kim | fcf46c3 | 2020-04-22 13:50:45 -0700 | [diff] [blame] | 2195 | continue; |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 2196 | } |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2197 | const C2PortAllocatorsTuning::input &allocators = intfCache.getInputAllocators(); |
Wonsik Kim | fcf46c3 | 2020-04-22 13:50:45 -0700 | [diff] [blame] | 2198 | if (firstIteration) { |
| 2199 | firstIteration = false; |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2200 | if (allocators && allocators.flexCount() > 0) { |
| 2201 | ids->insert(allocators.m.values, |
| 2202 | allocators.m.values + allocators.flexCount()); |
Wonsik Kim | fcf46c3 | 2020-04-22 13:50:45 -0700 | [diff] [blame] | 2203 | } |
| 2204 | if (ids->empty()) { |
| 2205 | // The component does not advertise allocators. Use default. |
| 2206 | ids->insert(defaultAllocatorId); |
| 2207 | } |
| 2208 | continue; |
| 2209 | } |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 2210 | bool filtered = false; |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2211 | if (allocators && allocators.flexCount() > 0) { |
| 2212 | filtered = true; |
| 2213 | for (auto it = ids->begin(); it != ids->end(); ) { |
| 2214 | bool found = false; |
| 2215 | for (size_t j = 0; j < allocators.flexCount(); ++j) { |
| 2216 | if (allocators.m.values[j] == *it) { |
| 2217 | found = true; |
| 2218 | break; |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 2219 | } |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2220 | } |
| 2221 | if (found) { |
| 2222 | ++it; |
| 2223 | } else { |
| 2224 | it = ids->erase(it); |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 2225 | } |
| 2226 | } |
| 2227 | } |
| 2228 | if (!filtered) { |
| 2229 | // The component does not advertise supported allocators. Use default. |
| 2230 | bool containsDefault = (ids->count(defaultAllocatorId) > 0u); |
| 2231 | if (ids->size() != (containsDefault ? 1 : 0)) { |
| 2232 | ids->clear(); |
| 2233 | if (containsDefault) { |
| 2234 | ids->insert(defaultAllocatorId); |
| 2235 | } |
| 2236 | } |
| 2237 | } |
| 2238 | } |
| 2239 | // Finally, filter with pool masks |
| 2240 | for (auto it = ids->begin(); it != ids->end(); ) { |
| 2241 | if ((poolMask >> *it) & 1) { |
| 2242 | ++it; |
| 2243 | } else { |
| 2244 | it = ids->erase(it); |
| 2245 | } |
| 2246 | } |
| 2247 | return OK; |
| 2248 | } |
| 2249 | |
| 2250 | static status_t CalculateMinMaxUsage( |
| 2251 | const std::vector<std::string> &names, uint64_t *minUsage, uint64_t *maxUsage) { |
| 2252 | static C2StreamUsageTuning::input sUsage{0u /* stream id */}; |
| 2253 | *minUsage = 0; |
| 2254 | *maxUsage = ~0ull; |
| 2255 | for (const std::string &name : names) { |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2256 | const IntfCache &intfCache = GetIntfCache(name); |
| 2257 | if (intfCache.initCheck() != OK) { |
Wonsik Kim | fcf46c3 | 2020-04-22 13:50:45 -0700 | [diff] [blame] | 2258 | continue; |
| 2259 | } |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2260 | const C2FieldSupportedValuesQuery &usageSupportedValues = |
| 2261 | intfCache.getUsageSupportedValues(); |
| 2262 | if (usageSupportedValues.status != C2_OK) { |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 2263 | continue; |
| 2264 | } |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2265 | const C2FieldSupportedValues &supported = usageSupportedValues.values; |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 2266 | if (supported.type != C2FieldSupportedValues::FLAGS) { |
| 2267 | continue; |
| 2268 | } |
| 2269 | if (supported.values.empty()) { |
| 2270 | *maxUsage = 0; |
| 2271 | continue; |
| 2272 | } |
| 2273 | *minUsage |= supported.values[0].u64; |
| 2274 | int64_t currentMaxUsage = 0; |
| 2275 | for (const C2Value::Primitive &flags : supported.values) { |
| 2276 | currentMaxUsage |= flags.u64; |
| 2277 | } |
| 2278 | *maxUsage &= currentMaxUsage; |
| 2279 | } |
| 2280 | return OK; |
| 2281 | } |
| 2282 | |
| 2283 | // static |
| 2284 | status_t CCodec::CanFetchLinearBlock( |
| 2285 | const std::vector<std::string> &names, const C2MemoryUsage &usage, bool *isCompatible) { |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2286 | for (const std::string &name : names) { |
| 2287 | const IntfCache &intfCache = GetIntfCache(name); |
| 2288 | if (intfCache.initCheck() != OK) { |
| 2289 | continue; |
| 2290 | } |
| 2291 | const C2ApiFeaturesSetting &features = intfCache.getApiFeatures(); |
| 2292 | if (features && !(features.value & API_SAME_INPUT_BUFFER)) { |
| 2293 | *isCompatible = false; |
| 2294 | return OK; |
| 2295 | } |
| 2296 | } |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 2297 | uint64_t minUsage = usage.expected; |
| 2298 | uint64_t maxUsage = ~0ull; |
| 2299 | std::set<C2Allocator::id_t> allocators; |
| 2300 | GetCommonAllocatorIds(names, C2Allocator::LINEAR, &allocators); |
| 2301 | if (allocators.empty()) { |
| 2302 | *isCompatible = false; |
| 2303 | return OK; |
| 2304 | } |
| 2305 | CalculateMinMaxUsage(names, &minUsage, &maxUsage); |
| 2306 | *isCompatible = ((maxUsage & minUsage) == minUsage); |
| 2307 | return OK; |
| 2308 | } |
| 2309 | |
| 2310 | static std::shared_ptr<C2BlockPool> GetPool(C2Allocator::id_t allocId) { |
| 2311 | static std::mutex sMutex{}; |
| 2312 | static std::map<C2Allocator::id_t, std::shared_ptr<C2BlockPool>> sPools; |
| 2313 | std::unique_lock<std::mutex> lock{sMutex}; |
| 2314 | std::shared_ptr<C2BlockPool> pool; |
| 2315 | auto it = sPools.find(allocId); |
| 2316 | if (it == sPools.end()) { |
| 2317 | c2_status_t err = CreateCodec2BlockPool(allocId, nullptr, &pool); |
| 2318 | if (err == OK) { |
| 2319 | sPools.emplace(allocId, pool); |
| 2320 | } else { |
| 2321 | pool.reset(); |
| 2322 | } |
| 2323 | } else { |
| 2324 | pool = it->second; |
| 2325 | } |
| 2326 | return pool; |
| 2327 | } |
| 2328 | |
| 2329 | // static |
| 2330 | std::shared_ptr<C2LinearBlock> CCodec::FetchLinearBlock( |
| 2331 | size_t capacity, const C2MemoryUsage &usage, const std::vector<std::string> &names) { |
| 2332 | uint64_t minUsage = usage.expected; |
| 2333 | uint64_t maxUsage = ~0ull; |
| 2334 | std::set<C2Allocator::id_t> allocators; |
| 2335 | GetCommonAllocatorIds(names, C2Allocator::LINEAR, &allocators); |
| 2336 | if (allocators.empty()) { |
| 2337 | allocators.insert(C2PlatformAllocatorStore::DEFAULT_LINEAR); |
| 2338 | } |
| 2339 | CalculateMinMaxUsage(names, &minUsage, &maxUsage); |
| 2340 | if ((maxUsage & minUsage) != minUsage) { |
| 2341 | allocators.clear(); |
| 2342 | allocators.insert(C2PlatformAllocatorStore::DEFAULT_LINEAR); |
| 2343 | } |
| 2344 | std::shared_ptr<C2LinearBlock> block; |
| 2345 | for (C2Allocator::id_t allocId : allocators) { |
| 2346 | std::shared_ptr<C2BlockPool> pool = GetPool(allocId); |
| 2347 | if (!pool) { |
| 2348 | continue; |
| 2349 | } |
| 2350 | c2_status_t err = pool->fetchLinearBlock(capacity, C2MemoryUsage{minUsage}, &block); |
| 2351 | if (err != C2_OK || !block) { |
| 2352 | block.reset(); |
| 2353 | continue; |
| 2354 | } |
| 2355 | break; |
| 2356 | } |
| 2357 | return block; |
| 2358 | } |
| 2359 | |
| 2360 | // static |
| 2361 | status_t CCodec::CanFetchGraphicBlock( |
| 2362 | const std::vector<std::string> &names, bool *isCompatible) { |
| 2363 | uint64_t minUsage = 0; |
| 2364 | uint64_t maxUsage = ~0ull; |
| 2365 | std::set<C2Allocator::id_t> allocators; |
| 2366 | GetCommonAllocatorIds(names, C2Allocator::GRAPHIC, &allocators); |
| 2367 | if (allocators.empty()) { |
| 2368 | *isCompatible = false; |
| 2369 | return OK; |
| 2370 | } |
| 2371 | CalculateMinMaxUsage(names, &minUsage, &maxUsage); |
| 2372 | *isCompatible = ((maxUsage & minUsage) == minUsage); |
| 2373 | return OK; |
| 2374 | } |
| 2375 | |
| 2376 | // static |
| 2377 | std::shared_ptr<C2GraphicBlock> CCodec::FetchGraphicBlock( |
| 2378 | int32_t width, |
| 2379 | int32_t height, |
| 2380 | int32_t format, |
| 2381 | uint64_t usage, |
| 2382 | const std::vector<std::string> &names) { |
| 2383 | uint32_t halPixelFormat = HAL_PIXEL_FORMAT_YCBCR_420_888; |
| 2384 | if (!C2Mapper::mapPixelFormatFrameworkToCodec(format, &halPixelFormat)) { |
| 2385 | ALOGD("Unrecognized pixel format: %d", format); |
| 2386 | return nullptr; |
| 2387 | } |
| 2388 | uint64_t minUsage = 0; |
| 2389 | uint64_t maxUsage = ~0ull; |
| 2390 | std::set<C2Allocator::id_t> allocators; |
| 2391 | GetCommonAllocatorIds(names, C2Allocator::GRAPHIC, &allocators); |
| 2392 | if (allocators.empty()) { |
| 2393 | allocators.insert(C2PlatformAllocatorStore::DEFAULT_GRAPHIC); |
| 2394 | } |
| 2395 | CalculateMinMaxUsage(names, &minUsage, &maxUsage); |
| 2396 | minUsage |= usage; |
| 2397 | if ((maxUsage & minUsage) != minUsage) { |
| 2398 | allocators.clear(); |
| 2399 | allocators.insert(C2PlatformAllocatorStore::DEFAULT_GRAPHIC); |
| 2400 | } |
| 2401 | std::shared_ptr<C2GraphicBlock> block; |
| 2402 | for (C2Allocator::id_t allocId : allocators) { |
| 2403 | std::shared_ptr<C2BlockPool> pool; |
| 2404 | c2_status_t err = CreateCodec2BlockPool(allocId, nullptr, &pool); |
| 2405 | if (err != C2_OK || !pool) { |
| 2406 | continue; |
| 2407 | } |
| 2408 | err = pool->fetchGraphicBlock( |
| 2409 | width, height, halPixelFormat, C2MemoryUsage{minUsage}, &block); |
| 2410 | if (err != C2_OK || !block) { |
| 2411 | block.reset(); |
| 2412 | continue; |
| 2413 | } |
| 2414 | break; |
| 2415 | } |
| 2416 | return block; |
| 2417 | } |
| 2418 | |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 2419 | } // namespace android |
| 2420 | |