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