Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | #ifndef CCODEC_BUFFER_CHANNEL_H_ |
| 18 | |
| 19 | #define CCODEC_BUFFER_CHANNEL_H_ |
| 20 | |
| 21 | #include <map> |
| 22 | #include <memory> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include <C2Buffer.h> |
| 26 | #include <C2Component.h> |
| 27 | #include <Codec2Mapper.h> |
| 28 | |
| 29 | #include <codec2/hidl/client.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 30 | #include <media/stagefright/foundation/Mutexed.h> |
| 31 | #include <media/stagefright/CodecBase.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 32 | |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 33 | #include "CCodecBuffers.h" |
Wonsik Kim | e1104ca | 2020-11-24 15:01:33 -0800 | [diff] [blame] | 34 | #include "FrameReassembler.h" |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 35 | #include "InputSurfaceWrapper.h" |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 36 | #include "PipelineWatcher.h" |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 37 | |
| 38 | namespace android { |
| 39 | |
Wonsik Kim | 078b58e | 2019-01-09 15:08:06 -0800 | [diff] [blame] | 40 | class MemoryDealer; |
| 41 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 42 | class CCodecCallback { |
| 43 | public: |
| 44 | virtual ~CCodecCallback() = default; |
| 45 | virtual void onError(status_t err, enum ActionCode actionCode) = 0; |
| 46 | virtual void onOutputFramesRendered(int64_t mediaTimeUs, nsecs_t renderTimeNs) = 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 47 | virtual void onOutputBuffersChanged() = 0; |
Guillaume Chelfi | 867d4dd | 2021-07-01 18:38:45 +0200 | [diff] [blame] | 48 | virtual void onFirstTunnelFrameReady() = 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | /** |
| 52 | * BufferChannelBase implementation for CCodec. |
| 53 | */ |
| 54 | class CCodecBufferChannel |
| 55 | : public BufferChannelBase, public std::enable_shared_from_this<CCodecBufferChannel> { |
| 56 | public: |
| 57 | explicit CCodecBufferChannel(const std::shared_ptr<CCodecCallback> &callback); |
| 58 | virtual ~CCodecBufferChannel(); |
| 59 | |
| 60 | // BufferChannelBase interface |
Wonsik Kim | 596187e | 2019-10-25 12:44:10 -0700 | [diff] [blame] | 61 | void setCrypto(const sp<ICrypto> &crypto) override; |
| 62 | void setDescrambler(const sp<IDescrambler> &descrambler) override; |
| 63 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 64 | virtual status_t queueInputBuffer(const sp<MediaCodecBuffer> &buffer) override; |
| 65 | virtual status_t queueSecureInputBuffer( |
| 66 | const sp<MediaCodecBuffer> &buffer, |
| 67 | bool secure, |
| 68 | const uint8_t *key, |
| 69 | const uint8_t *iv, |
| 70 | CryptoPlugin::Mode mode, |
| 71 | CryptoPlugin::Pattern pattern, |
| 72 | const CryptoPlugin::SubSample *subSamples, |
| 73 | size_t numSubSamples, |
| 74 | AString *errorDetailMsg) override; |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 75 | virtual status_t attachBuffer( |
| 76 | const std::shared_ptr<C2Buffer> &c2Buffer, |
| 77 | const sp<MediaCodecBuffer> &buffer) override; |
| 78 | virtual status_t attachEncryptedBuffer( |
| 79 | const sp<hardware::HidlMemory> &memory, |
| 80 | bool secure, |
| 81 | const uint8_t *key, |
| 82 | const uint8_t *iv, |
| 83 | CryptoPlugin::Mode mode, |
| 84 | CryptoPlugin::Pattern pattern, |
| 85 | size_t offset, |
| 86 | const CryptoPlugin::SubSample *subSamples, |
| 87 | size_t numSubSamples, |
| 88 | const sp<MediaCodecBuffer> &buffer) override; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 89 | virtual status_t renderOutputBuffer( |
| 90 | const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) override; |
| 91 | virtual status_t discardBuffer(const sp<MediaCodecBuffer> &buffer) override; |
| 92 | virtual void getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override; |
| 93 | virtual void getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override; |
| 94 | |
| 95 | // Methods below are interface for CCodec to use. |
| 96 | |
| 97 | /** |
| 98 | * Set the component object for buffer processing. |
| 99 | */ |
| 100 | void setComponent(const std::shared_ptr<Codec2Client::Component> &component); |
| 101 | |
| 102 | /** |
| 103 | * Set output graphic surface for rendering. |
| 104 | */ |
Sungtak Lee | 80c8e1e | 2023-01-26 11:03:14 +0000 | [diff] [blame^] | 105 | status_t setSurface(const sp<Surface> &surface, bool pushBlankBuffer); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * Set GraphicBufferSource object from which the component extracts input |
| 109 | * buffers. |
| 110 | */ |
| 111 | status_t setInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface); |
| 112 | |
| 113 | /** |
| 114 | * Signal EOS to input surface. |
| 115 | */ |
| 116 | status_t signalEndOfInputStream(); |
| 117 | |
| 118 | /** |
| 119 | * Set parameters. |
| 120 | */ |
| 121 | status_t setParameters(std::vector<std::unique_ptr<C2Param>> ¶ms); |
| 122 | |
| 123 | /** |
| 124 | * Start queueing buffers to the component. This object should never queue |
| 125 | * buffers before this call has completed. |
| 126 | */ |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 127 | status_t start( |
| 128 | const sp<AMessage> &inputFormat, |
| 129 | const sp<AMessage> &outputFormat, |
| 130 | bool buffersBoundToCodec); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 131 | |
| 132 | /** |
Wonsik Kim | 34b28b4 | 2022-05-20 15:49:32 -0700 | [diff] [blame] | 133 | * Prepare initial input buffers to be filled by client. |
| 134 | * |
| 135 | * \param clientInputBuffers[out] pointer to slot index -> buffer map. |
| 136 | * On success, it contains prepared |
| 137 | * initial input buffers. |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 138 | */ |
Wonsik Kim | 34b28b4 | 2022-05-20 15:49:32 -0700 | [diff] [blame] | 139 | status_t prepareInitialInputBuffers( |
| 140 | std::map<size_t, sp<MediaCodecBuffer>> *clientInputBuffers); |
| 141 | |
| 142 | /** |
| 143 | * Request initial input buffers as prepared in clientInputBuffers. |
| 144 | * |
| 145 | * \param clientInputBuffers[in] slot index -> buffer map with prepared |
| 146 | * initial input buffers. |
| 147 | */ |
| 148 | status_t requestInitialInputBuffers( |
| 149 | std::map<size_t, sp<MediaCodecBuffer>> &&clientInputBuffers); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 150 | |
| 151 | /** |
Sungtak Lee | d964e2e | 2022-07-30 08:43:58 +0000 | [diff] [blame] | 152 | * Stop using buffers of the current output surface for other Codec |
| 153 | * instances to use the surface safely. |
Sungtak Lee | 80c8e1e | 2023-01-26 11:03:14 +0000 | [diff] [blame^] | 154 | * |
| 155 | * \param pushBlankBuffer[in] push a blank buffer at the end if true |
Sungtak Lee | d964e2e | 2022-07-30 08:43:58 +0000 | [diff] [blame] | 156 | */ |
Sungtak Lee | 80c8e1e | 2023-01-26 11:03:14 +0000 | [diff] [blame^] | 157 | void stopUseOutputSurface(bool pushBlankBuffer); |
Sungtak Lee | d964e2e | 2022-07-30 08:43:58 +0000 | [diff] [blame] | 158 | |
| 159 | /** |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 160 | * Stop queueing buffers to the component. This object should never queue |
| 161 | * buffers after this call, until start() is called. |
| 162 | */ |
| 163 | void stop(); |
| 164 | |
Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 165 | /** |
| 166 | * Stop queueing buffers to the component and release all buffers. |
| 167 | */ |
| 168 | void reset(); |
| 169 | |
| 170 | /** |
| 171 | * Release all resources. |
| 172 | */ |
| 173 | void release(); |
| 174 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 175 | void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork); |
| 176 | |
| 177 | /** |
| 178 | * Notify input client about work done. |
| 179 | * |
| 180 | * @param workItems finished work item. |
| 181 | * @param outputFormat new output format if it has changed, otherwise nullptr |
| 182 | * @param initData new init data (CSD) if it has changed, otherwise nullptr |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 183 | */ |
| 184 | void onWorkDone( |
| 185 | std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat, |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 186 | const C2StreamInitDataInfo::output *initData); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 187 | |
| 188 | /** |
| 189 | * Make an input buffer available for the client as it is no longer needed |
| 190 | * by the codec. |
| 191 | * |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 192 | * @param frameIndex The index of input work |
| 193 | * @param arrayIndex The index of buffer in the input work buffers. |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 194 | */ |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 195 | void onInputBufferDone(uint64_t frameIndex, size_t arrayIndex); |
| 196 | |
| 197 | PipelineWatcher::Clock::duration elapsed(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 198 | |
| 199 | enum MetaMode { |
| 200 | MODE_NONE, |
| 201 | MODE_ANW, |
| 202 | }; |
| 203 | |
| 204 | void setMetaMode(MetaMode mode); |
| 205 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 206 | private: |
| 207 | class QueueGuard; |
| 208 | |
| 209 | /** |
| 210 | * Special mutex-like object with the following properties: |
| 211 | * |
| 212 | * - At STOPPED state (initial, or after stop()) |
| 213 | * - QueueGuard object gets created at STOPPED state, and the client is |
| 214 | * supposed to return immediately. |
| 215 | * - At RUNNING state (after start()) |
| 216 | * - Each QueueGuard object |
| 217 | */ |
| 218 | class QueueSync { |
| 219 | public: |
| 220 | /** |
| 221 | * At construction the sync object is in STOPPED state. |
| 222 | */ |
| 223 | inline QueueSync() {} |
| 224 | ~QueueSync() = default; |
| 225 | |
| 226 | /** |
| 227 | * Transition to RUNNING state when stopped. No-op if already in RUNNING |
| 228 | * state. |
| 229 | */ |
| 230 | void start(); |
| 231 | |
| 232 | /** |
| 233 | * At RUNNING state, wait until all QueueGuard object created during |
| 234 | * RUNNING state are destroyed, and then transition to STOPPED state. |
| 235 | * No-op if already in STOPPED state. |
| 236 | */ |
| 237 | void stop(); |
| 238 | |
| 239 | private: |
| 240 | Mutex mGuardLock; |
| 241 | |
| 242 | struct Counter { |
| 243 | inline Counter() : value(-1) {} |
| 244 | int32_t value; |
| 245 | Condition cond; |
| 246 | }; |
| 247 | Mutexed<Counter> mCount; |
| 248 | |
| 249 | friend class CCodecBufferChannel::QueueGuard; |
| 250 | }; |
| 251 | |
| 252 | class QueueGuard { |
| 253 | public: |
| 254 | QueueGuard(QueueSync &sync); |
| 255 | ~QueueGuard(); |
| 256 | inline bool isRunning() { return mRunning; } |
| 257 | |
| 258 | private: |
| 259 | QueueSync &mSync; |
| 260 | bool mRunning; |
| 261 | }; |
| 262 | |
| 263 | void feedInputBufferIfAvailable(); |
| 264 | void feedInputBufferIfAvailableInternal(); |
Sungtak Lee | 04b3035 | 2020-07-27 13:57:25 -0700 | [diff] [blame] | 265 | status_t queueInputBufferInternal(sp<MediaCodecBuffer> buffer, |
| 266 | std::shared_ptr<C2LinearBlock> encryptedBlock = nullptr, |
| 267 | size_t blockSize = 0); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 268 | bool handleWork( |
| 269 | std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat, |
| 270 | const C2StreamInitDataInfo::output *initData); |
| 271 | void sendOutputBuffers(); |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 272 | void ensureDecryptDestination(size_t size); |
| 273 | int32_t getHeapSeqNum(const sp<hardware::HidlMemory> &memory); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 274 | |
| 275 | QueueSync mSync; |
| 276 | sp<MemoryDealer> mDealer; |
| 277 | sp<IMemory> mDecryptDestination; |
| 278 | int32_t mHeapSeqNum; |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 279 | std::map<wp<hardware::HidlMemory>, int32_t> mHeapSeqNumMap; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 280 | |
| 281 | std::shared_ptr<Codec2Client::Component> mComponent; |
| 282 | std::string mComponentName; ///< component name for debugging |
| 283 | const char *mName; ///< C-string version of component name |
| 284 | std::shared_ptr<CCodecCallback> mCCodecCallback; |
| 285 | std::shared_ptr<C2BlockPool> mInputAllocator; |
| 286 | QueueSync mQueueSync; |
| 287 | std::vector<std::unique_ptr<C2Param>> mParamsToBeSet; |
| 288 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 289 | struct Input { |
| 290 | Input(); |
| 291 | |
| 292 | std::unique_ptr<InputBuffers> buffers; |
| 293 | size_t numSlots; |
| 294 | FlexBuffersImpl extraBuffers; |
| 295 | size_t numExtraSlots; |
| 296 | uint32_t inputDelay; |
| 297 | uint32_t pipelineDelay; |
Wonsik Kim | 6b2c8be | 2021-09-28 05:11:04 -0700 | [diff] [blame] | 298 | c2_cntr64_t lastFlushIndex; |
Wonsik Kim | e1104ca | 2020-11-24 15:01:33 -0800 | [diff] [blame] | 299 | |
| 300 | FrameReassembler frameReassembler; |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 301 | }; |
| 302 | Mutexed<Input> mInput; |
| 303 | struct Output { |
| 304 | std::unique_ptr<OutputBuffers> buffers; |
| 305 | size_t numSlots; |
| 306 | uint32_t outputDelay; |
| 307 | }; |
| 308 | Mutexed<Output> mOutput; |
Wonsik Kim | 5ebfcb2 | 2021-01-05 18:58:15 -0800 | [diff] [blame] | 309 | Mutexed<std::list<std::unique_ptr<C2Work>>> mFlushedConfigs; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 310 | |
| 311 | std::atomic_uint64_t mFrameIndex; |
| 312 | std::atomic_uint64_t mFirstValidFrameIndex; |
| 313 | |
| 314 | sp<MemoryDealer> makeMemoryDealer(size_t heapSize); |
| 315 | |
| 316 | struct OutputSurface { |
| 317 | sp<Surface> surface; |
| 318 | uint32_t generation; |
Wonsik Kim | f5e5c83 | 2019-02-21 11:36:05 -0800 | [diff] [blame] | 319 | int maxDequeueBuffers; |
Byeongjo Park | 25c3a3d | 2020-06-12 17:24:21 +0900 | [diff] [blame] | 320 | std::map<uint64_t, int> rotation; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 321 | }; |
| 322 | Mutexed<OutputSurface> mOutputSurface; |
| 323 | |
| 324 | struct BlockPools { |
| 325 | C2Allocator::id_t inputAllocatorId; |
| 326 | std::shared_ptr<C2BlockPool> inputPool; |
| 327 | C2Allocator::id_t outputAllocatorId; |
| 328 | C2BlockPool::local_id_t outputPoolId; |
| 329 | std::shared_ptr<Codec2Client::Configurable> outputPoolIntf; |
| 330 | }; |
| 331 | Mutexed<BlockPools> mBlockPools; |
| 332 | |
| 333 | std::shared_ptr<InputSurfaceWrapper> mInputSurface; |
| 334 | |
| 335 | MetaMode mMetaMode; |
| 336 | |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 337 | Mutexed<PipelineWatcher> mPipelineWatcher; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 338 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 339 | std::atomic_bool mInputMetEos; |
Wonsik Kim | f7529dd | 2019-04-18 17:35:53 -0700 | [diff] [blame] | 340 | std::once_flag mRenderWarningFlag; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 341 | |
Wonsik Kim | 596187e | 2019-10-25 12:44:10 -0700 | [diff] [blame] | 342 | sp<ICrypto> mCrypto; |
| 343 | sp<IDescrambler> mDescrambler; |
| 344 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 345 | inline bool hasCryptoOrDescrambler() { |
| 346 | return mCrypto != nullptr || mDescrambler != nullptr; |
| 347 | } |
Sungtak Lee | 04b3035 | 2020-07-27 13:57:25 -0700 | [diff] [blame] | 348 | std::atomic_bool mSendEncryptedInfoBuffer; |
Wonsik Kim | ec585c3 | 2021-10-01 01:11:00 -0700 | [diff] [blame] | 349 | |
| 350 | std::atomic_bool mTunneled; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 351 | }; |
| 352 | |
| 353 | // Conversion of a c2_status_t value to a status_t value may depend on the |
| 354 | // operation that returns the c2_status_t value. |
| 355 | enum c2_operation_t { |
| 356 | C2_OPERATION_NONE, |
| 357 | C2_OPERATION_Component_connectToOmxInputSurface, |
| 358 | C2_OPERATION_Component_createBlockPool, |
| 359 | C2_OPERATION_Component_destroyBlockPool, |
| 360 | C2_OPERATION_Component_disconnectFromInputSurface, |
| 361 | C2_OPERATION_Component_drain, |
| 362 | C2_OPERATION_Component_flush, |
| 363 | C2_OPERATION_Component_queue, |
| 364 | C2_OPERATION_Component_release, |
| 365 | C2_OPERATION_Component_reset, |
| 366 | C2_OPERATION_Component_setOutputSurface, |
| 367 | C2_OPERATION_Component_start, |
| 368 | C2_OPERATION_Component_stop, |
| 369 | C2_OPERATION_ComponentStore_copyBuffer, |
| 370 | C2_OPERATION_ComponentStore_createComponent, |
| 371 | C2_OPERATION_ComponentStore_createInputSurface, |
| 372 | C2_OPERATION_ComponentStore_createInterface, |
| 373 | C2_OPERATION_Configurable_config, |
| 374 | C2_OPERATION_Configurable_query, |
| 375 | C2_OPERATION_Configurable_querySupportedParams, |
| 376 | C2_OPERATION_Configurable_querySupportedValues, |
| 377 | C2_OPERATION_InputSurface_connectToComponent, |
| 378 | C2_OPERATION_InputSurfaceConnection_disconnect, |
| 379 | }; |
| 380 | |
| 381 | status_t toStatusT(c2_status_t c2s, c2_operation_t c2op = C2_OPERATION_NONE); |
| 382 | |
| 383 | } // namespace android |
| 384 | |
| 385 | #endif // CCODEC_BUFFER_CHANNEL_H_ |