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 type tracking
147     MetadataBufferType mMetadataType[2];
148     enum SecureBufferType {
149         kSecureBufferTypeUnknown,
150         kSecureBufferTypeOpaque,
151         kSecureBufferTypeNativeHandle,
152     };
153     SecureBufferType mSecureBufferType[2];
154 
155     // Following are OMX parameters managed by us (instead of the component)
156     // OMX_IndexParamMaxFrameDurationForBitrateControl
157     KeyedVector<int64_t, int64_t> mOriginalTimeUs;
158     bool mRestorePtsFailed;
159     int64_t mMaxTimestampGapUs;
160     int64_t mPrevOriginalTimeUs;
161     int64_t mPrevModifiedTimeUs;
162 
163     // For debug support
164     char *mName;
165     int DEBUG;
166     size_t mNumPortBuffers[2];  // modified under mLock, read outside for debug
167     Mutex mDebugLock;
168     // following are modified and read under mDebugLock
169     int DEBUG_BUMP;
170     SortedVector<OMX_BUFFERHEADERTYPE *> mInputBuffersWithCodec, mOutputBuffersWithCodec;
171     size_t mDebugLevelBumpPendingBuffers[2];
172     void bumpDebugLevel_l(size_t numInputBuffers, size_t numOutputBuffers);
173     void unbumpDebugLevel_l(size_t portIndex);
174 
175     ~OMXNodeInstance();
176 
177     void addActiveBuffer(OMX_U32 portIndex, IOMX::buffer_id id);
178     void removeActiveBuffer(OMX_U32 portIndex, IOMX::buffer_id id);
179     void freeActiveBuffers();
180 
181     // For buffer id management
182     IOMX::buffer_id makeBufferID(OMX_BUFFERHEADERTYPE *bufferHeader);
183     OMX_BUFFERHEADERTYPE *findBufferHeader(IOMX::buffer_id buffer, OMX_U32 portIndex);
184     IOMX::buffer_id findBufferID(OMX_BUFFERHEADERTYPE *bufferHeader);
185     void invalidateBufferID(IOMX::buffer_id buffer);
186 
187     bool isProhibitedIndex_l(OMX_INDEXTYPE index);
188 
189     status_t useBuffer_l(
190             OMX_U32 portIndex, const sp<IMemory> &params,
191             const sp<IHidlMemory> &hParams, IOMX::buffer_id *buffer);
192 
193     status_t useGraphicBuffer_l(
194             OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer,
195             IOMX::buffer_id *buffer);
196 
197     status_t useGraphicBufferWithMetadata_l(
198             OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer,
199             IOMX::buffer_id *buffer);
200 
201     status_t useGraphicBuffer2_l(
202             OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer,
203             IOMX::buffer_id *buffer);
204 
205     status_t emptyBuffer_l(
206             IOMX::buffer_id buffer,
207             OMX_U32 rangeOffset, OMX_U32 rangeLength,
208             OMX_U32 flags, OMX_TICKS timestamp, int fenceFd);
209 
210     status_t emptyGraphicBuffer_l(
211             IOMX::buffer_id buffer, const sp<GraphicBuffer> &graphicBuffer,
212             OMX_U32 flags, OMX_TICKS timestamp, int fenceFd);
213 
214     status_t emptyNativeHandleBuffer_l(
215             IOMX::buffer_id buffer, const sp<NativeHandle> &nativeHandle,
216             OMX_U32 flags, OMX_TICKS timestamp, int fenceFd);
217 
218     status_t emptyBuffer_l(
219             OMX_BUFFERHEADERTYPE *header,
220             OMX_U32 flags, OMX_TICKS timestamp, intptr_t debugAddr, int fenceFd);
221 
222     static OMX_ERRORTYPE OnEvent(
223             OMX_IN OMX_HANDLETYPE hComponent,
224             OMX_IN OMX_PTR pAppData,
225             OMX_IN OMX_EVENTTYPE eEvent,
226             OMX_IN OMX_U32 nData1,
227             OMX_IN OMX_U32 nData2,
228             OMX_IN OMX_PTR pEventData);
229 
230     static OMX_ERRORTYPE OnEmptyBufferDone(
231             OMX_IN OMX_HANDLETYPE hComponent,
232             OMX_IN OMX_PTR pAppData,
233             OMX_IN OMX_BUFFERHEADERTYPE *pBuffer);
234 
235     static OMX_ERRORTYPE OnFillBufferDone(
236             OMX_IN OMX_HANDLETYPE hComponent,
237             OMX_IN OMX_PTR pAppData,
238             OMX_IN OMX_BUFFERHEADERTYPE *pBuffer);
239 
240     status_t enableNativeBuffers_l(
241             OMX_U32 portIndex, OMX_BOOL graphic, OMX_BOOL enable);
242 
243     status_t storeMetaDataInBuffers_l(
244             OMX_U32 portIndex, OMX_BOOL enable, MetadataBufferType *type);
245 
246     // Stores fence into buffer if it is ANWBuffer type and has enough space.
247     // otherwise, waits for the fence to signal.  Takes ownership of |fenceFd|.
248     status_t storeFenceInMeta_l(
249             OMX_BUFFERHEADERTYPE *header, int fenceFd, OMX_U32 portIndex);
250 
251     // Retrieves the fence from buffer if ANWBuffer type and has enough space. Otherwise, returns -1
252     int retrieveFenceFromMeta_l(
253             OMX_BUFFERHEADERTYPE *header, OMX_U32 portIndex);
254 
255     // Updates the graphic buffer handle in the metadata buffer for |buffer| and |header| to
256     // |graphicBuffer|'s handle. If |updateCodecBuffer| is true, the update will happen in
257     // the actual codec buffer (use this if not using emptyBuffer (with no _l) later to
258     // pass the buffer to the codec, as only emptyBuffer copies the backup buffer to the codec
259     // buffer.)
260     status_t updateGraphicBufferInMeta_l(
261             OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer,
262             IOMX::buffer_id buffer, OMX_BUFFERHEADERTYPE *header);
263 
264     status_t updateNativeHandleInMeta_l(
265             OMX_U32 portIndex, const sp<NativeHandle> &nativeHandle,
266             IOMX::buffer_id buffer, OMX_BUFFERHEADERTYPE *header);
267 
268     sp<IOMXBufferSource> getBufferSource();
269     void setBufferSource(const sp<IOMXBufferSource> &bufferSource);
270     // Called when omx_message::FILL_BUFFER_DONE is received. (Currently the
271     // buffer source will fix timestamp in the header if needed.)
272     void codecBufferFilled(omx_message &msg);
273 
274     // Handles |msg|, and may modify it. Returns true iff completely handled it and
275     // |msg| does not need to be sent to the event listener.
276     bool handleMessage(omx_message &msg);
277 
278     bool handleDataSpaceChanged(omx_message &msg);
279 
280     status_t setMaxPtsGapUs(const void *params, size_t size);
281     int64_t getCodecTimestamp(OMX_TICKS timestamp);
282 
283     OMXNodeInstance(const OMXNodeInstance &);
284     OMXNodeInstance &operator=(const OMXNodeInstance &);
285 };
286 
287 }  // namespace android
288 
289 #endif  // OMX_NODE_INSTANCE_H_
290