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