1 /* 2 * Copyright (C) 2009 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 OMX_NODE_INSTANCE_H_ 18 19 #define OMX_NODE_INSTANCE_H_ 20 21 #include <atomic> 22 23 #include <media/IOMX.h> 24 #include <utils/RefBase.h> 25 #include <utils/threads.h> 26 #include <utils/KeyedVector.h> 27 #include <utils/SortedVector.h> 28 29 #include <android/hidl/memory/1.0/IMemory.h> 30 #include <media/stagefright/omx/1.0/Omx.h> 31 32 namespace android { 33 class GraphicBuffer; 34 class IOMXBufferSource; 35 class IOMXObserver; 36 struct OMXStore; 37 class OMXBuffer; 38 using IHidlMemory = hidl::memory::V1_0::IMemory; 39 using hardware::media::omx::V1_0::implementation::Omx; 40 41 struct OMXNodeInstance : public BnOMXNode { 42 OMXNodeInstance( 43 Omx *owner, const sp<IOMXObserver> &observer, const char *name); 44 45 void setHandle(OMX_HANDLETYPE handle); 46 47 OMX_HANDLETYPE handle(); 48 sp<IOMXObserver> observer(); 49 50 status_t freeNode() override; 51 52 status_t sendCommand(OMX_COMMANDTYPE cmd, OMX_S32 param); 53 status_t getParameter(OMX_INDEXTYPE index, void *params, size_t size); 54 55 status_t setParameter( 56 OMX_INDEXTYPE index, const void *params, size_t size); 57 58 status_t getConfig(OMX_INDEXTYPE index, void *params, size_t size); 59 status_t setConfig(OMX_INDEXTYPE index, const void *params, size_t size); 60 61 status_t setPortMode(OMX_U32 port_index, IOMX::PortMode mode); 62 63 status_t getGraphicBufferUsage(OMX_U32 portIndex, OMX_U32* usage); 64 65 status_t prepareForAdaptivePlayback( 66 OMX_U32 portIndex, OMX_BOOL enable, 67 OMX_U32 maxFrameWidth, OMX_U32 maxFrameHeight); 68 69 status_t configureVideoTunnelMode( 70 OMX_U32 portIndex, OMX_BOOL tunneled, 71 OMX_U32 audioHwSync, native_handle_t **sidebandHandle); 72 73 status_t setInputSurface( 74 const sp<IOMXBufferSource> &bufferSource); 75 76 status_t allocateSecureBuffer( 77 OMX_U32 portIndex, size_t size, IOMX::buffer_id *buffer, 78 void **buffer_data, sp<NativeHandle> *native_handle); 79 80 status_t useBuffer( 81 OMX_U32 portIndex, const OMXBuffer &omxBuf, buffer_id *buffer); 82 83 status_t freeBuffer( 84 OMX_U32 portIndex, buffer_id buffer); 85 86 status_t fillBuffer( 87 buffer_id buffer, const OMXBuffer &omxBuf, int fenceFd = -1); 88 89 status_t emptyBuffer( 90 buffer_id buffer, const OMXBuffer &omxBuf, 91 OMX_U32 flags, OMX_TICKS timestamp, int fenceFd = -1); 92 93 status_t getExtensionIndex( 94 const char *parameterName, OMX_INDEXTYPE *index); 95 96 // Quirk still supported, even though deprecated 97 enum Quirks { 98 kRequiresAllocateBufferOnInputPorts = 1, 99 kRequiresAllocateBufferOnOutputPorts = 2, 100 101 kQuirksMask = kRequiresAllocateBufferOnInputPorts 102 | kRequiresAllocateBufferOnOutputPorts, 103 }; 104 105 status_t setQuirks(OMX_U32 quirks); 106 isSecureOMXNodeInstance107 bool isSecure() const { 108 return mIsSecure; 109 } 110 111 status_t dispatchMessage(const omx_message &msg) override; 112 113 // handles messages and removes them from the list 114 void onMessages(std::list<omx_message> &messages); 115 void onObserverDied(); 116 void onEvent(OMX_EVENTTYPE event, OMX_U32 arg1, OMX_U32 arg2); 117 118 static OMX_CALLBACKTYPE kCallbacks; 119 120 private: 121 struct CallbackDispatcherThread; 122 struct CallbackDispatcher; 123 124 Mutex mLock; 125 126 Omx *mOwner; 127 OMX_HANDLETYPE mHandle; 128 sp<IOMXObserver> mObserver; 129 sp<CallbackDispatcher> mDispatcher; 130 std::atomic_bool mDying; 131 bool mSailed; // configuration is set (no more meta-mode changes) 132 bool mQueriedProhibitedExtensions; 133 SortedVector<OMX_INDEXTYPE> mProhibitedExtensions; 134 bool mIsSecure; 135 uint32_t mQuirks; 136 137 // Lock only covers mOMXBufferSource and mOMXOutputListener. We can't always 138 // use mLock because of rare instances where we'd end up locking it recursively. 139 Mutex mOMXBufferSourceLock; 140 // Access these through getBufferSource(). 141 sp<IOMXBufferSource> mOMXBufferSource; 142 143 struct ActiveBuffer { 144 OMX_U32 mPortIndex; 145 IOMX::buffer_id mID; 146 }; 147 Vector<ActiveBuffer> mActiveBuffers; 148 // for buffer ptr to buffer id translation 149 Mutex mBufferIDLock; 150 uint32_t mBufferIDCount; 151 KeyedVector<IOMX::buffer_id, OMX_BUFFERHEADERTYPE *> mBufferIDToBufferHeader; 152 KeyedVector<OMX_BUFFERHEADERTYPE *, IOMX::buffer_id> mBufferHeaderToBufferID; 153 154 bool mLegacyAdaptiveExperiment; 155 IOMX::PortMode mPortMode[2]; 156 // metadata and secure buffer types and graphic buffer mode tracking 157 MetadataBufferType mMetadataType[2]; 158 enum SecureBufferType { 159 kSecureBufferTypeUnknown, 160 kSecureBufferTypeOpaque, 161 kSecureBufferTypeNativeHandle, 162 }; 163 SecureBufferType mSecureBufferType[2]; 164 bool mGraphicBufferEnabled[2]; 165 166 // Following are OMX parameters managed by us (instead of the component) 167 // OMX_IndexParamMaxFrameDurationForBitrateControl 168 KeyedVector<int64_t, int64_t> mOriginalTimeUs; 169 bool mRestorePtsFailed; 170 int64_t mMaxTimestampGapUs; 171 int64_t mPrevOriginalTimeUs; 172 int64_t mPrevModifiedTimeUs; 173 174 // For debug support 175 char *mName; 176 int DEBUG; 177 size_t mNumPortBuffers[2]; // modified under mLock, read outside for debug 178 Mutex mDebugLock; 179 // following are modified and read under mDebugLock 180 int DEBUG_BUMP; 181 SortedVector<OMX_BUFFERHEADERTYPE *> mInputBuffersWithCodec, mOutputBuffersWithCodec; 182 size_t mDebugLevelBumpPendingBuffers[2]; 183 void bumpDebugLevel_l(size_t numInputBuffers, size_t numOutputBuffers); 184 void unbumpDebugLevel_l(size_t portIndex); 185 186 ~OMXNodeInstance(); 187 188 void addActiveBuffer(OMX_U32 portIndex, IOMX::buffer_id id); 189 void removeActiveBuffer(OMX_U32 portIndex, IOMX::buffer_id id); 190 void freeActiveBuffers(); 191 192 // For buffer id management 193 IOMX::buffer_id makeBufferID(OMX_BUFFERHEADERTYPE *bufferHeader); 194 OMX_BUFFERHEADERTYPE *findBufferHeader(IOMX::buffer_id buffer, OMX_U32 portIndex); 195 IOMX::buffer_id findBufferID(OMX_BUFFERHEADERTYPE *bufferHeader); 196 void invalidateBufferID(IOMX::buffer_id buffer); 197 198 bool isProhibitedIndex_l(OMX_INDEXTYPE index); 199 200 status_t useBuffer_l( 201 OMX_U32 portIndex, const sp<IMemory> ¶ms, 202 const sp<IHidlMemory> &hParams, IOMX::buffer_id *buffer); 203 204 status_t useGraphicBuffer_l( 205 OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer, 206 IOMX::buffer_id *buffer); 207 208 status_t useGraphicBufferWithMetadata_l( 209 OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer, 210 IOMX::buffer_id *buffer); 211 212 status_t useGraphicBuffer2_l( 213 OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer, 214 IOMX::buffer_id *buffer); 215 216 status_t emptyBuffer_l( 217 IOMX::buffer_id buffer, 218 OMX_U32 rangeOffset, OMX_U32 rangeLength, 219 OMX_U32 flags, OMX_TICKS timestamp, int fenceFd); 220 221 status_t emptyGraphicBuffer_l( 222 IOMX::buffer_id buffer, const sp<GraphicBuffer> &graphicBuffer, 223 OMX_U32 flags, OMX_TICKS timestamp, int fenceFd); 224 225 status_t emptyNativeHandleBuffer_l( 226 IOMX::buffer_id buffer, const sp<NativeHandle> &nativeHandle, 227 OMX_U32 flags, OMX_TICKS timestamp, int fenceFd); 228 229 status_t emptyBuffer_l( 230 OMX_BUFFERHEADERTYPE *header, 231 OMX_U32 flags, OMX_TICKS timestamp, intptr_t debugAddr, int fenceFd); 232 233 static OMX_ERRORTYPE OnEvent( 234 OMX_IN OMX_HANDLETYPE hComponent, 235 OMX_IN OMX_PTR pAppData, 236 OMX_IN OMX_EVENTTYPE eEvent, 237 OMX_IN OMX_U32 nData1, 238 OMX_IN OMX_U32 nData2, 239 OMX_IN OMX_PTR pEventData); 240 241 static OMX_ERRORTYPE OnEmptyBufferDone( 242 OMX_IN OMX_HANDLETYPE hComponent, 243 OMX_IN OMX_PTR pAppData, 244 OMX_IN OMX_BUFFERHEADERTYPE *pBuffer); 245 246 static OMX_ERRORTYPE OnFillBufferDone( 247 OMX_IN OMX_HANDLETYPE hComponent, 248 OMX_IN OMX_PTR pAppData, 249 OMX_IN OMX_BUFFERHEADERTYPE *pBuffer); 250 251 status_t enableNativeBuffers_l( 252 OMX_U32 portIndex, OMX_BOOL graphic, OMX_BOOL enable); 253 254 status_t storeMetaDataInBuffers_l( 255 OMX_U32 portIndex, OMX_BOOL enable, MetadataBufferType *type); 256 257 // Stores fence into buffer if it is ANWBuffer type and has enough space. 258 // otherwise, waits for the fence to signal. Takes ownership of |fenceFd|. 259 status_t storeFenceInMeta_l( 260 OMX_BUFFERHEADERTYPE *header, int fenceFd, OMX_U32 portIndex); 261 262 // Retrieves the fence from buffer if ANWBuffer type and has enough space. Otherwise, returns -1 263 int retrieveFenceFromMeta_l( 264 OMX_BUFFERHEADERTYPE *header, OMX_U32 portIndex); 265 266 // Updates the graphic buffer handle in the metadata buffer for |buffer| and |header| to 267 // |graphicBuffer|'s handle. If |updateCodecBuffer| is true, the update will happen in 268 // the actual codec buffer (use this if not using emptyBuffer (with no _l) later to 269 // pass the buffer to the codec, as only emptyBuffer copies the backup buffer to the codec 270 // buffer.) 271 status_t updateGraphicBufferInMeta_l( 272 OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer, 273 IOMX::buffer_id buffer, OMX_BUFFERHEADERTYPE *header); 274 275 status_t updateNativeHandleInMeta_l( 276 OMX_U32 portIndex, const sp<NativeHandle> &nativeHandle, 277 IOMX::buffer_id buffer, OMX_BUFFERHEADERTYPE *header); 278 279 sp<IOMXBufferSource> getBufferSource(); 280 void setBufferSource(const sp<IOMXBufferSource> &bufferSource); 281 // Called when omx_message::FILL_BUFFER_DONE is received. (Currently the 282 // buffer source will fix timestamp in the header if needed.) 283 void codecBufferFilled(omx_message &msg); 284 285 // Handles |msg|, and may modify it. Returns true iff completely handled it and 286 // |msg| does not need to be sent to the event listener. 287 bool handleMessage(omx_message &msg); 288 289 bool handleDataSpaceChanged(omx_message &msg); 290 291 /* 292 * Set the max pts gap between frames. 293 * 294 * When the pts gap number is positive, it indicates the maximum pts gap between 295 * two adjacent frames. If two frames are further apart, timestamps will be modified 296 * to meet this requirement before the frames are sent to the encoder. 297 * 298 * When the pts gap number is negative, it indicates that the original timestamp 299 * should always be modified such that all adjacent frames have the same pts gap 300 * equal to the absolute value of the passed in number. This option is typically 301 * used when client wants to make sure all frames are captured even when source 302 * potentially sends out-of-order frames. 303 * 304 * Timestamps will be restored to the original when the output is sent back to the client. 305 */ 306 status_t setMaxPtsGapUs(const void *params, size_t size); 307 int64_t getCodecTimestamp(OMX_TICKS timestamp); 308 309 OMXNodeInstance(const OMXNodeInstance &); 310 OMXNodeInstance &operator=(const OMXNodeInstance &); 311 }; 312 313 } // namespace android 314 315 #endif // OMX_NODE_INSTANCE_H_ 316