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 ANDROID_IOMX_H_
18 
19 #define ANDROID_IOMX_H_
20 
21 #include <binder/IInterface.h>
22 #include <gui/IGraphicBufferProducer.h>
23 #include <gui/IGraphicBufferConsumer.h>
24 #include <ui/GraphicBuffer.h>
25 #include <utils/List.h>
26 #include <utils/String8.h>
27 
28 #include <list>
29 
30 #include <media/hardware/MetadataBufferType.h>
31 
32 #include <OMX_Core.h>
33 #include <OMX_Video.h>
34 
35 namespace android {
36 
37 class IMemory;
38 class IOMXObserver;
39 class IOMXRenderer;
40 class Surface;
41 
42 class IOMX : public IInterface {
43 public:
44     DECLARE_META_INTERFACE(OMX);
45 
46     typedef uint32_t buffer_id;
47     typedef uint32_t node_id;
48 
49     // Given a node_id and the calling process' pid, returns true iff
50     // the implementation of the OMX interface lives in the same
51     // process.
52     virtual bool livesLocally(node_id node, pid_t pid) = 0;
53 
54     struct ComponentInfo {
55         String8 mName;
56         List<String8> mRoles;
57     };
58     virtual status_t listNodes(List<ComponentInfo> *list) = 0;
59 
60     virtual status_t allocateNode(
61             const char *name, const sp<IOMXObserver> &observer,
62             node_id *node) = 0;
63 
64     virtual status_t freeNode(node_id node) = 0;
65 
66     virtual status_t sendCommand(
67             node_id node, OMX_COMMANDTYPE cmd, OMX_S32 param) = 0;
68 
69     virtual status_t getParameter(
70             node_id node, OMX_INDEXTYPE index,
71             void *params, size_t size) = 0;
72 
73     virtual status_t setParameter(
74             node_id node, OMX_INDEXTYPE index,
75             const void *params, size_t size) = 0;
76 
77     virtual status_t getConfig(
78             node_id node, OMX_INDEXTYPE index,
79             void *params, size_t size) = 0;
80 
81     virtual status_t setConfig(
82             node_id node, OMX_INDEXTYPE index,
83             const void *params, size_t size) = 0;
84 
85     virtual status_t getState(
86             node_id node, OMX_STATETYPE* state) = 0;
87 
88     // This will set *type to previous metadata buffer type on OMX error (not on binder error), and
89     // new metadata buffer type on success.
90     virtual status_t storeMetaDataInBuffers(
91             node_id node, OMX_U32 port_index, OMX_BOOL enable, MetadataBufferType *type = NULL) = 0;
92 
93     virtual status_t prepareForAdaptivePlayback(
94             node_id node, OMX_U32 portIndex, OMX_BOOL enable,
95             OMX_U32 maxFrameWidth, OMX_U32 maxFrameHeight) = 0;
96 
97     virtual status_t configureVideoTunnelMode(
98             node_id node, OMX_U32 portIndex, OMX_BOOL tunneled,
99             OMX_U32 audioHwSync, native_handle_t **sidebandHandle) = 0;
100 
101     virtual status_t enableGraphicBuffers(
102             node_id node, OMX_U32 port_index, OMX_BOOL enable) = 0;
103 
104     virtual status_t getGraphicBufferUsage(
105             node_id node, OMX_U32 port_index, OMX_U32* usage) = 0;
106 
107     // Use |params| as an OMX buffer, but limit the size of the OMX buffer to |allottedSize|.
108     virtual status_t useBuffer(
109             node_id node, OMX_U32 port_index, const sp<IMemory> &params,
110             buffer_id *buffer, OMX_U32 allottedSize) = 0;
111 
112     virtual status_t useGraphicBuffer(
113             node_id node, OMX_U32 port_index,
114             const sp<GraphicBuffer> &graphicBuffer, buffer_id *buffer) = 0;
115 
116     virtual status_t updateGraphicBufferInMeta(
117             node_id node, OMX_U32 port_index,
118             const sp<GraphicBuffer> &graphicBuffer, buffer_id buffer) = 0;
119 
120     // This will set *type to resulting metadata buffer type on OMX error (not on binder error) as
121     // well as on success.
122     virtual status_t createInputSurface(
123             node_id node, OMX_U32 port_index,
124             sp<IGraphicBufferProducer> *bufferProducer,
125             MetadataBufferType *type = NULL) = 0;
126 
127     virtual status_t createPersistentInputSurface(
128             sp<IGraphicBufferProducer> *bufferProducer,
129             sp<IGraphicBufferConsumer> *bufferConsumer) = 0;
130 
131     // This will set *type to resulting metadata buffer type on OMX error (not on binder error) as
132     // well as on success.
133     virtual status_t setInputSurface(
134             node_id node, OMX_U32 port_index,
135             const sp<IGraphicBufferConsumer> &bufferConsumer,
136             MetadataBufferType *type) = 0;
137 
138     virtual status_t signalEndOfInputStream(node_id node) = 0;
139 
140     // This API clearly only makes sense if the caller lives in the
141     // same process as the callee, i.e. is the media_server, as the
142     // returned "buffer_data" pointer is just that, a pointer into local
143     // address space.
144     virtual status_t allocateBuffer(
145             node_id node, OMX_U32 port_index, size_t size,
146             buffer_id *buffer, void **buffer_data) = 0;
147 
148     // Allocate an OMX buffer of size |allotedSize|. Use |params| as the backup buffer, which
149     // may be larger.
150     virtual status_t allocateBufferWithBackup(
151             node_id node, OMX_U32 port_index, const sp<IMemory> &params,
152             buffer_id *buffer, OMX_U32 allottedSize) = 0;
153 
154     virtual status_t freeBuffer(
155             node_id node, OMX_U32 port_index, buffer_id buffer) = 0;
156 
157     enum {
158         kFenceTimeoutMs = 1000
159     };
160     // Calls OMX_FillBuffer on buffer, and passes |fenceFd| to component if it supports
161     // fences. Otherwise, it waits on |fenceFd| before calling OMX_FillBuffer.
162     // Takes ownership of |fenceFd| even if this call fails.
163     virtual status_t fillBuffer(node_id node, buffer_id buffer, int fenceFd = -1) = 0;
164 
165     // Calls OMX_EmptyBuffer on buffer (after updating buffer header with |range_offset|,
166     // |range_length|, |flags| and |timestamp|). Passes |fenceFd| to component if it
167     // supports fences. Otherwise, it waits on |fenceFd| before calling OMX_EmptyBuffer.
168     // Takes ownership of |fenceFd| even if this call fails.
169     virtual status_t emptyBuffer(
170             node_id node,
171             buffer_id buffer,
172             OMX_U32 range_offset, OMX_U32 range_length,
173             OMX_U32 flags, OMX_TICKS timestamp, int fenceFd = -1) = 0;
174 
175     virtual status_t getExtensionIndex(
176             node_id node,
177             const char *parameter_name,
178             OMX_INDEXTYPE *index) = 0;
179 
180     enum InternalOptionType {
181         INTERNAL_OPTION_SUSPEND,  // data is a bool
182         INTERNAL_OPTION_REPEAT_PREVIOUS_FRAME_DELAY,  // data is an int64_t
183         INTERNAL_OPTION_MAX_TIMESTAMP_GAP, // data is int64_t
184         INTERNAL_OPTION_MAX_FPS, // data is float
185         INTERNAL_OPTION_START_TIME, // data is an int64_t
186         INTERNAL_OPTION_TIME_LAPSE, // data is an int64_t[2]
187     };
188     virtual status_t setInternalOption(
189             node_id node,
190             OMX_U32 port_index,
191             InternalOptionType type,
192             const void *data,
193             size_t size) = 0;
194 };
195 
196 struct omx_message {
197     enum {
198         EVENT,
199         EMPTY_BUFFER_DONE,
200         FILL_BUFFER_DONE,
201         FRAME_RENDERED,
202     } type;
203 
204     IOMX::node_id node;
205     int fenceFd; // used for EMPTY_BUFFER_DONE and FILL_BUFFER_DONE; client must close this
206 
207     union {
208         // if type == EVENT
209         struct {
210             OMX_EVENTTYPE event;
211             OMX_U32 data1;
212             OMX_U32 data2;
213         } event_data;
214 
215         // if type == EMPTY_BUFFER_DONE
216         struct {
217             IOMX::buffer_id buffer;
218         } buffer_data;
219 
220         // if type == FILL_BUFFER_DONE
221         struct {
222             IOMX::buffer_id buffer;
223             OMX_U32 range_offset;
224             OMX_U32 range_length;
225             OMX_U32 flags;
226             OMX_TICKS timestamp;
227         } extended_buffer_data;
228 
229         // if type == FRAME_RENDERED
230         struct {
231             OMX_TICKS timestamp;
232             OMX_S64 nanoTime;
233         } render_data;
234     } u;
235 };
236 
237 class IOMXObserver : public IInterface {
238 public:
239     DECLARE_META_INTERFACE(OMXObserver);
240 
241     // Handle (list of) messages.
242     virtual void onMessages(const std::list<omx_message> &messages) = 0;
243 };
244 
245 ////////////////////////////////////////////////////////////////////////////////
246 
247 class BnOMX : public BnInterface<IOMX> {
248 public:
249     virtual status_t onTransact(
250             uint32_t code, const Parcel &data, Parcel *reply,
251             uint32_t flags = 0);
252 };
253 
254 class BnOMXObserver : public BnInterface<IOMXObserver> {
255 public:
256     virtual status_t onTransact(
257             uint32_t code, const Parcel &data, Parcel *reply,
258             uint32_t flags = 0);
259 };
260 
261 struct CodecProfileLevel {
262     OMX_U32 mProfile;
263     OMX_U32 mLevel;
264 };
265 
266 }  // namespace android
267 
268 inline static const char *asString(android::MetadataBufferType i, const char *def = "??") {
269     using namespace android;
270     switch (i) {
271         case kMetadataBufferTypeCameraSource:   return "CameraSource";
272         case kMetadataBufferTypeGrallocSource:  return "GrallocSource";
273         case kMetadataBufferTypeANWBuffer:      return "ANWBuffer";
274         case kMetadataBufferTypeInvalid:        return "Invalid";
275         default:                                return def;
276     }
277 }
278 
279 #endif  // ANDROID_IOMX_H_
280