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
25#include <OMX_Core.h>
26#include <OMX_Video.h>
27
28#include "jni.h"
29
30namespace android {
31
32class IMemory;
33class IOMXObserver;
34class IOMXRenderer;
35class ISurface;
36class Surface;
37
38class IOMX : public IInterface {
39public:
40    DECLARE_META_INTERFACE(OMX);
41
42    typedef void *buffer_id;
43    typedef void *node_id;
44
45    // Given the calling process' pid, returns true iff
46    // the implementation of the OMX interface lives in the same
47    // process.
48    virtual bool livesLocally(pid_t pid) = 0;
49
50    struct ComponentInfo {
51        String8 mName;
52        List<String8> mRoles;
53    };
54    virtual status_t listNodes(List<ComponentInfo> *list) = 0;
55
56    virtual status_t allocateNode(
57            const char *name, const sp<IOMXObserver> &observer,
58            node_id *node) = 0;
59
60    virtual status_t freeNode(node_id node) = 0;
61
62    virtual status_t sendCommand(
63            node_id node, OMX_COMMANDTYPE cmd, OMX_S32 param) = 0;
64
65    virtual status_t getParameter(
66            node_id node, OMX_INDEXTYPE index,
67            void *params, size_t size) = 0;
68
69    virtual status_t setParameter(
70            node_id node, OMX_INDEXTYPE index,
71            const void *params, size_t size) = 0;
72
73    virtual status_t getConfig(
74            node_id node, OMX_INDEXTYPE index,
75            void *params, size_t size) = 0;
76
77    virtual status_t setConfig(
78            node_id node, OMX_INDEXTYPE index,
79            const void *params, size_t size) = 0;
80
81    virtual status_t useBuffer(
82            node_id node, OMX_U32 port_index, const sp<IMemory> &params,
83            buffer_id *buffer) = 0;
84
85    // This API clearly only makes sense if the caller lives in the
86    // same process as the callee, i.e. is the media_server, as the
87    // returned "buffer_data" pointer is just that, a pointer into local
88    // address space.
89    virtual status_t allocateBuffer(
90            node_id node, OMX_U32 port_index, size_t size,
91            buffer_id *buffer, void **buffer_data) = 0;
92
93    virtual status_t allocateBufferWithBackup(
94            node_id node, OMX_U32 port_index, const sp<IMemory> &params,
95            buffer_id *buffer) = 0;
96
97    virtual status_t freeBuffer(
98            node_id node, OMX_U32 port_index, buffer_id buffer) = 0;
99
100    virtual status_t fillBuffer(node_id node, buffer_id buffer) = 0;
101
102    virtual status_t emptyBuffer(
103            node_id node,
104            buffer_id buffer,
105            OMX_U32 range_offset, OMX_U32 range_length,
106            OMX_U32 flags, OMX_TICKS timestamp) = 0;
107
108    virtual status_t getExtensionIndex(
109            node_id node,
110            const char *parameter_name,
111            OMX_INDEXTYPE *index) = 0;
112
113    virtual sp<IOMXRenderer> createRenderer(
114            const sp<ISurface> &surface,
115            const char *componentName,
116            OMX_COLOR_FORMATTYPE colorFormat,
117            size_t encodedWidth, size_t encodedHeight,
118            size_t displayWidth, size_t displayHeight,
119            int32_t rotationDegrees) = 0;
120
121    // Note: These methods are _not_ virtual, it exists as a wrapper around
122    // the virtual "createRenderer" method above facilitating extraction
123    // of the ISurface from a regular Surface or a java Surface object.
124    sp<IOMXRenderer> createRenderer(
125            const sp<Surface> &surface,
126            const char *componentName,
127            OMX_COLOR_FORMATTYPE colorFormat,
128            size_t encodedWidth, size_t encodedHeight,
129            size_t displayWidth, size_t displayHeight,
130            int32_t rotationDegrees);
131
132    sp<IOMXRenderer> createRendererFromJavaSurface(
133            JNIEnv *env, jobject javaSurface,
134            const char *componentName,
135            OMX_COLOR_FORMATTYPE colorFormat,
136            size_t encodedWidth, size_t encodedHeight,
137            size_t displayWidth, size_t displayHeight,
138            int32_t rotationDegrees);
139};
140
141struct omx_message {
142    enum {
143        EVENT,
144        EMPTY_BUFFER_DONE,
145        FILL_BUFFER_DONE,
146
147    } type;
148
149    IOMX::node_id node;
150
151    union {
152        // if type == EVENT
153        struct {
154            OMX_EVENTTYPE event;
155            OMX_U32 data1;
156            OMX_U32 data2;
157        } event_data;
158
159        // if type == EMPTY_BUFFER_DONE
160        struct {
161            IOMX::buffer_id buffer;
162        } buffer_data;
163
164        // if type == FILL_BUFFER_DONE
165        struct {
166            IOMX::buffer_id buffer;
167            OMX_U32 range_offset;
168            OMX_U32 range_length;
169            OMX_U32 flags;
170            OMX_TICKS timestamp;
171            OMX_PTR platform_private;
172            OMX_PTR data_ptr;
173        } extended_buffer_data;
174
175    } u;
176};
177
178class IOMXObserver : public IInterface {
179public:
180    DECLARE_META_INTERFACE(OMXObserver);
181
182    virtual void onMessage(const omx_message &msg) = 0;
183};
184
185class IOMXRenderer : public IInterface {
186public:
187    DECLARE_META_INTERFACE(OMXRenderer);
188
189    virtual void render(IOMX::buffer_id buffer) = 0;
190};
191
192////////////////////////////////////////////////////////////////////////////////
193
194class BnOMX : public BnInterface<IOMX> {
195public:
196    virtual status_t onTransact(
197            uint32_t code, const Parcel &data, Parcel *reply,
198            uint32_t flags = 0);
199};
200
201class BnOMXObserver : public BnInterface<IOMXObserver> {
202public:
203    virtual status_t onTransact(
204            uint32_t code, const Parcel &data, Parcel *reply,
205            uint32_t flags = 0);
206};
207
208class BnOMXRenderer : public BnInterface<IOMXRenderer> {
209public:
210    virtual status_t onTransact(
211            uint32_t code, const Parcel &data, Parcel *reply,
212            uint32_t flags = 0);
213};
214
215}  // namespace android
216
217#endif  // ANDROID_IOMX_H_
218