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