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