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 <utils/List.h>
23 #include <utils/String8.h>
24 #include <cutils/native_handle.h>
25 
26 #include <list>
27 
28 #include <hidl/HybridInterface.h>
29 #include <media/hardware/MetadataBufferType.h>
30 #include <android/hardware/media/omx/1.0/IOmxNode.h>
31 
32 #include <media/openmax/OMX_Core.h>
33 #include <media/openmax/OMX_Video.h>
34 #include <media/openmax/OMX_VideoExt.h>
35 
36 namespace android {
37 
38 class IGraphicBufferProducer;
39 class IGraphicBufferSource;
40 class IMemory;
41 class IOMXBufferSource;
42 class IOMXNode;
43 class IOMXObserver;
44 class NativeHandle;
45 class OMXBuffer;
46 struct omx_message;
47 
48 using hardware::media::omx::V1_0::IOmxNode;
49 
50 class IOMX : public RefBase {
51 public:
52 
53     typedef uint32_t buffer_id;
54 
55     enum {
56         kFenceTimeoutMs = 1000
57     };
58 
59     enum PortMode {
60         kPortModePresetStart = 0,
61         kPortModePresetByteBuffer,
62         kPortModePresetANWBuffer,
63         kPortModePresetSecureBuffer,
64         kPortModePresetEnd,
65 
66         kPortModeDynamicStart = 100,
67         kPortModeDynamicANWBuffer,      // uses metadata mode kMetadataBufferTypeANWBuffer
68                                         // or kMetadataBufferTypeGrallocSource
69         kPortModeDynamicNativeHandle,   // uses metadata mode kMetadataBufferTypeNativeHandleSource
70         kPortModeDynamicEnd,
71     };
72 
73     struct ComponentInfo {
74         String8 mName;
75         List<String8> mRoles;
76     };
77     virtual status_t listNodes(List<ComponentInfo> *list) = 0;
78 
79     virtual status_t allocateNode(
80             const char *name, const sp<IOMXObserver> &observer,
81             sp<IOMXNode> *omxNode) = 0;
82 
83     virtual status_t createInputSurface(
84             sp<IGraphicBufferProducer> *bufferProducer,
85             sp<IGraphicBufferSource> *bufferSource) = 0;
86 };
87 
88 class IOMXNode : public IInterface {
89 public:
90     DECLARE_HYBRID_META_INTERFACE(OMXNode, IOmxNode);
91 
92     typedef IOMX::buffer_id buffer_id;
93 
94     virtual status_t freeNode() = 0;
95 
96     virtual status_t sendCommand(
97             OMX_COMMANDTYPE cmd, OMX_S32 param) = 0;
98 
99     virtual status_t getParameter(
100             OMX_INDEXTYPE index, void *params, size_t size) = 0;
101 
102     virtual status_t setParameter(
103             OMX_INDEXTYPE index, const void *params, size_t size) = 0;
104 
105     virtual status_t getConfig(
106             OMX_INDEXTYPE index, void *params, size_t size) = 0;
107 
108     virtual status_t setConfig(
109             OMX_INDEXTYPE index, const void *params, size_t size) = 0;
110 
111     virtual status_t setPortMode(
112             OMX_U32 port_index, IOMX::PortMode mode) = 0;
113 
114     virtual status_t prepareForAdaptivePlayback(
115             OMX_U32 portIndex, OMX_BOOL enable,
116             OMX_U32 maxFrameWidth, OMX_U32 maxFrameHeight) = 0;
117 
118     virtual status_t configureVideoTunnelMode(
119             OMX_U32 portIndex, OMX_BOOL tunneled,
120             OMX_U32 audioHwSync, native_handle_t **sidebandHandle) = 0;
121 
122     virtual status_t getGraphicBufferUsage(
123             OMX_U32 port_index, OMX_U32* usage) = 0;
124 
125     virtual status_t setInputSurface(
126             const sp<IOMXBufferSource> &bufferSource) = 0;
127 
128     // Allocate an opaque buffer as a native handle. If component supports returning native
129     // handles, those are returned in *native_handle. Otherwise, the allocated buffer is
130     // returned in *buffer_data. This clearly only makes sense if the caller lives in the
131     // same process as the callee, i.e. is the media_server, as the returned "buffer_data"
132     // pointer is just that, a pointer into local address space.
133     virtual status_t allocateSecureBuffer(
134             OMX_U32 port_index, size_t size, buffer_id *buffer,
135             void **buffer_data, sp<NativeHandle> *native_handle) = 0;
136 
137     // Instructs the component to use the buffer passed in via |omxBuf| on the
138     // specified port. Returns in |*buffer| the buffer id that the component
139     // assigns to this buffer. |omxBuf| must be one of:
140     // 1) OMXBuffer::sPreset for meta-mode,
141     // 2) type kBufferTypeANWBuffer for non-meta-graphic buffer mode,
142     // 3) type kBufferTypeSharedMem for bytebuffer mode.
143     virtual status_t useBuffer(
144             OMX_U32 port_index, const OMXBuffer &omxBuf, buffer_id *buffer) = 0;
145 
146     // Frees the buffer on the specified port with buffer id |buffer|.
147     virtual status_t freeBuffer(
148             OMX_U32 port_index, buffer_id buffer) = 0;
149 
150     // Calls OMX_FillBuffer on buffer. Passes |fenceFd| to component if it
151     // supports fences. Otherwise, it waits on |fenceFd| before calling
152     // OMX_FillBuffer. Takes ownership of |fenceFd| even if this call fails.
153     // If the port is in metadata mode, the buffer will be updated to point
154     // to the new buffer passed in via |omxBuf| before OMX_FillBuffer is called.
155     // Otherwise info in the |omxBuf| is not used.
156     virtual status_t fillBuffer(
157             buffer_id buffer, const OMXBuffer &omxBuf, int fenceFd = -1) = 0;
158 
159     // Calls OMX_EmptyBuffer on buffer. Passes |fenceFd| to component if it
160     // supports fences. Otherwise, it waits on |fenceFd| before calling
161     // OMX_EmptyBuffer. Takes ownership of |fenceFd| even if this call fails.
162     // If the port is in metadata mode, the buffer will be updated to point
163     // to the new buffer passed in via |omxBuf| before OMX_EmptyBuffer is called.
164     virtual status_t emptyBuffer(
165             buffer_id buffer, const OMXBuffer &omxBuf,
166             OMX_U32 flags, OMX_TICKS timestamp, int fenceFd = -1) = 0;
167 
168     virtual status_t getExtensionIndex(
169             const char *parameter_name,
170             OMX_INDEXTYPE *index) = 0;
171 
172     virtual status_t dispatchMessage(const omx_message &msg) = 0;
173 };
174 
175 struct omx_message {
176     enum {
177         EVENT,
178         EMPTY_BUFFER_DONE,
179         FILL_BUFFER_DONE,
180         FRAME_RENDERED,
181     } type;
182 
183     int fenceFd; // used for EMPTY_BUFFER_DONE and FILL_BUFFER_DONE; client must close this
184 
185     union {
186         // if type == EVENT
187         struct {
188             OMX_EVENTTYPE event;
189             OMX_U32 data1;
190             OMX_U32 data2;
191             OMX_U32 data3;
192             OMX_U32 data4;
193         } event_data;
194 
195         // if type == EMPTY_BUFFER_DONE
196         struct {
197             IOMX::buffer_id buffer;
198         } buffer_data;
199 
200         // if type == FILL_BUFFER_DONE
201         struct {
202             IOMX::buffer_id buffer;
203             OMX_U32 range_offset;
204             OMX_U32 range_length;
205             OMX_U32 flags;
206             OMX_TICKS timestamp;
207         } extended_buffer_data;
208 
209         // if type == FRAME_RENDERED
210         struct {
211             OMX_TICKS timestamp;
212             OMX_S64 nanoTime;
213         } render_data;
214     } u;
215 };
216 
217 class IOMXObserver : public IInterface {
218 public:
219     DECLARE_META_INTERFACE(OMXObserver);
220 
221     // Handle (list of) messages.
222     virtual void onMessages(const std::list<omx_message> &messages) = 0;
223 };
224 
225 ////////////////////////////////////////////////////////////////////////////////
226 class BnOMXNode : public BnInterface<IOMXNode> {
227 public:
228     virtual status_t onTransact(
229             uint32_t code, const Parcel &data, Parcel *reply,
230             uint32_t flags = 0);
231 
232 protected:
233     // check if the codec is secure.
isSecure()234     virtual bool isSecure() const {
235         return false;
236     }
237 };
238 
239 class BnOMXObserver : public BnInterface<IOMXObserver> {
240 public:
241     virtual status_t onTransact(
242             uint32_t code, const Parcel &data, Parcel *reply,
243             uint32_t flags = 0);
244 };
245 
246 }  // namespace android
247 
248 #endif  // ANDROID_IOMX_H_
249