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