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> 30 #include <media/stagefright/foundation/Mutexed.h> 31 #include <media/stagefright/CodecBase.h> 32 33 #include "CCodecBuffers.h" 34 #include "FrameReassembler.h" 35 #include "InputSurfaceWrapper.h" 36 #include "PipelineWatcher.h" 37 38 namespace android { 39 40 class MemoryDealer; 41 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; 47 virtual void onOutputBuffersChanged() = 0; 48 virtual void onFirstTunnelFrameReady() = 0; 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 61 void setCrypto(const sp<ICrypto> &crypto) override; 62 void setDescrambler(const sp<IDescrambler> &descrambler) override; 63 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; 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; 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 */ 105 status_t setSurface(const sp<Surface> &surface); 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 */ 127 status_t start( 128 const sp<AMessage> &inputFormat, 129 const sp<AMessage> &outputFormat, 130 bool buffersBoundToCodec); 131 132 /** 133 * Request initial input buffers to be filled by client. 134 */ 135 status_t requestInitialInputBuffers(); 136 137 /** 138 * Stop queueing buffers to the component. This object should never queue 139 * buffers after this call, until start() is called. 140 */ 141 void stop(); 142 143 /** 144 * Stop queueing buffers to the component and release all buffers. 145 */ 146 void reset(); 147 148 /** 149 * Release all resources. 150 */ 151 void release(); 152 153 void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork); 154 155 /** 156 * Notify input client about work done. 157 * 158 * @param workItems finished work item. 159 * @param outputFormat new output format if it has changed, otherwise nullptr 160 * @param initData new init data (CSD) if it has changed, otherwise nullptr 161 */ 162 void onWorkDone( 163 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat, 164 const C2StreamInitDataInfo::output *initData); 165 166 /** 167 * Make an input buffer available for the client as it is no longer needed 168 * by the codec. 169 * 170 * @param frameIndex The index of input work 171 * @param arrayIndex The index of buffer in the input work buffers. 172 */ 173 void onInputBufferDone(uint64_t frameIndex, size_t arrayIndex); 174 175 PipelineWatcher::Clock::duration elapsed(); 176 177 enum MetaMode { 178 MODE_NONE, 179 MODE_ANW, 180 }; 181 182 void setMetaMode(MetaMode mode); 183 184 private: 185 class QueueGuard; 186 187 /** 188 * Special mutex-like object with the following properties: 189 * 190 * - At STOPPED state (initial, or after stop()) 191 * - QueueGuard object gets created at STOPPED state, and the client is 192 * supposed to return immediately. 193 * - At RUNNING state (after start()) 194 * - Each QueueGuard object 195 */ 196 class QueueSync { 197 public: 198 /** 199 * At construction the sync object is in STOPPED state. 200 */ QueueSync()201 inline QueueSync() {} 202 ~QueueSync() = default; 203 204 /** 205 * Transition to RUNNING state when stopped. No-op if already in RUNNING 206 * state. 207 */ 208 void start(); 209 210 /** 211 * At RUNNING state, wait until all QueueGuard object created during 212 * RUNNING state are destroyed, and then transition to STOPPED state. 213 * No-op if already in STOPPED state. 214 */ 215 void stop(); 216 217 private: 218 Mutex mGuardLock; 219 220 struct Counter { CounterCounter221 inline Counter() : value(-1) {} 222 int32_t value; 223 Condition cond; 224 }; 225 Mutexed<Counter> mCount; 226 227 friend class CCodecBufferChannel::QueueGuard; 228 }; 229 230 class QueueGuard { 231 public: 232 QueueGuard(QueueSync &sync); 233 ~QueueGuard(); isRunning()234 inline bool isRunning() { return mRunning; } 235 236 private: 237 QueueSync &mSync; 238 bool mRunning; 239 }; 240 241 void feedInputBufferIfAvailable(); 242 void feedInputBufferIfAvailableInternal(); 243 status_t queueInputBufferInternal(sp<MediaCodecBuffer> buffer, 244 std::shared_ptr<C2LinearBlock> encryptedBlock = nullptr, 245 size_t blockSize = 0); 246 bool handleWork( 247 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat, 248 const C2StreamInitDataInfo::output *initData); 249 void sendOutputBuffers(); 250 void ensureDecryptDestination(size_t size); 251 int32_t getHeapSeqNum(const sp<hardware::HidlMemory> &memory); 252 253 QueueSync mSync; 254 sp<MemoryDealer> mDealer; 255 sp<IMemory> mDecryptDestination; 256 int32_t mHeapSeqNum; 257 std::map<wp<hardware::HidlMemory>, int32_t> mHeapSeqNumMap; 258 259 std::shared_ptr<Codec2Client::Component> mComponent; 260 std::string mComponentName; ///< component name for debugging 261 const char *mName; ///< C-string version of component name 262 std::shared_ptr<CCodecCallback> mCCodecCallback; 263 std::shared_ptr<C2BlockPool> mInputAllocator; 264 QueueSync mQueueSync; 265 std::vector<std::unique_ptr<C2Param>> mParamsToBeSet; 266 267 struct Input { 268 Input(); 269 270 std::unique_ptr<InputBuffers> buffers; 271 size_t numSlots; 272 FlexBuffersImpl extraBuffers; 273 size_t numExtraSlots; 274 uint32_t inputDelay; 275 uint32_t pipelineDelay; 276 277 FrameReassembler frameReassembler; 278 }; 279 Mutexed<Input> mInput; 280 struct Output { 281 std::unique_ptr<OutputBuffers> buffers; 282 size_t numSlots; 283 uint32_t outputDelay; 284 }; 285 Mutexed<Output> mOutput; 286 Mutexed<std::list<std::unique_ptr<C2Work>>> mFlushedConfigs; 287 288 std::atomic_uint64_t mFrameIndex; 289 std::atomic_uint64_t mFirstValidFrameIndex; 290 291 sp<MemoryDealer> makeMemoryDealer(size_t heapSize); 292 293 struct OutputSurface { 294 sp<Surface> surface; 295 uint32_t generation; 296 int maxDequeueBuffers; 297 std::map<uint64_t, int> rotation; 298 }; 299 Mutexed<OutputSurface> mOutputSurface; 300 301 struct BlockPools { 302 C2Allocator::id_t inputAllocatorId; 303 std::shared_ptr<C2BlockPool> inputPool; 304 C2Allocator::id_t outputAllocatorId; 305 C2BlockPool::local_id_t outputPoolId; 306 std::shared_ptr<Codec2Client::Configurable> outputPoolIntf; 307 }; 308 Mutexed<BlockPools> mBlockPools; 309 310 std::shared_ptr<InputSurfaceWrapper> mInputSurface; 311 312 MetaMode mMetaMode; 313 314 Mutexed<PipelineWatcher> mPipelineWatcher; 315 316 std::atomic_bool mInputMetEos; 317 std::once_flag mRenderWarningFlag; 318 319 sp<ICrypto> mCrypto; 320 sp<IDescrambler> mDescrambler; 321 hasCryptoOrDescrambler()322 inline bool hasCryptoOrDescrambler() { 323 return mCrypto != nullptr || mDescrambler != nullptr; 324 } 325 std::atomic_bool mSendEncryptedInfoBuffer; 326 }; 327 328 // Conversion of a c2_status_t value to a status_t value may depend on the 329 // operation that returns the c2_status_t value. 330 enum c2_operation_t { 331 C2_OPERATION_NONE, 332 C2_OPERATION_Component_connectToOmxInputSurface, 333 C2_OPERATION_Component_createBlockPool, 334 C2_OPERATION_Component_destroyBlockPool, 335 C2_OPERATION_Component_disconnectFromInputSurface, 336 C2_OPERATION_Component_drain, 337 C2_OPERATION_Component_flush, 338 C2_OPERATION_Component_queue, 339 C2_OPERATION_Component_release, 340 C2_OPERATION_Component_reset, 341 C2_OPERATION_Component_setOutputSurface, 342 C2_OPERATION_Component_start, 343 C2_OPERATION_Component_stop, 344 C2_OPERATION_ComponentStore_copyBuffer, 345 C2_OPERATION_ComponentStore_createComponent, 346 C2_OPERATION_ComponentStore_createInputSurface, 347 C2_OPERATION_ComponentStore_createInterface, 348 C2_OPERATION_Configurable_config, 349 C2_OPERATION_Configurable_query, 350 C2_OPERATION_Configurable_querySupportedParams, 351 C2_OPERATION_Configurable_querySupportedValues, 352 C2_OPERATION_InputSurface_connectToComponent, 353 C2_OPERATION_InputSurfaceConnection_disconnect, 354 }; 355 356 status_t toStatusT(c2_status_t c2s, c2_operation_t c2op = C2_OPERATION_NONE); 357 358 } // namespace android 359 360 #endif // CCODEC_BUFFER_CHANNEL_H_ 361