Home | History | Annotate | Download | only in media
      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         INTERNAL_OPTION_TIME_OFFSET, // data is an int64_t
    196     };
    197     virtual status_t setInternalOption(
    198             node_id node,
    199             OMX_U32 port_index,
    200             InternalOptionType type,
    201             const void *data,
    202             size_t size) = 0;
    203 };
    204 
    205 struct omx_message {
    206     enum {
    207         EVENT,
    208         EMPTY_BUFFER_DONE,
    209         FILL_BUFFER_DONE,
    210         FRAME_RENDERED,
    211     } type;
    212 
    213     IOMX::node_id node;
    214     int fenceFd; // used for EMPTY_BUFFER_DONE and FILL_BUFFER_DONE; client must close this
    215 
    216     union {
    217         // if type == EVENT
    218         struct {
    219             OMX_EVENTTYPE event;
    220             OMX_U32 data1;
    221             OMX_U32 data2;
    222         } event_data;
    223 
    224         // if type == EMPTY_BUFFER_DONE
    225         struct {
    226             IOMX::buffer_id buffer;
    227         } buffer_data;
    228 
    229         // if type == FILL_BUFFER_DONE
    230         struct {
    231             IOMX::buffer_id buffer;
    232             OMX_U32 range_offset;
    233             OMX_U32 range_length;
    234             OMX_U32 flags;
    235             OMX_TICKS timestamp;
    236         } extended_buffer_data;
    237 
    238         // if type == FRAME_RENDERED
    239         struct {
    240             OMX_TICKS timestamp;
    241             OMX_S64 nanoTime;
    242         } render_data;
    243     } u;
    244 };
    245 
    246 class IOMXObserver : public IInterface {
    247 public:
    248     DECLARE_META_INTERFACE(OMXObserver);
    249 
    250     // Handle (list of) messages.
    251     virtual void onMessages(const std::list<omx_message> &messages) = 0;
    252 };
    253 
    254 ////////////////////////////////////////////////////////////////////////////////
    255 
    256 class BnOMX : public BnInterface<IOMX> {
    257 public:
    258     virtual status_t onTransact(
    259             uint32_t code, const Parcel &data, Parcel *reply,
    260             uint32_t flags = 0);
    261 
    262 protected:
    263     // check if the codec is secure.
    264     virtual bool isSecure(IOMX::node_id node) {
    265         return false;
    266     }
    267 };
    268 
    269 class BnOMXObserver : public BnInterface<IOMXObserver> {
    270 public:
    271     virtual status_t onTransact(
    272             uint32_t code, const Parcel &data, Parcel *reply,
    273             uint32_t flags = 0);
    274 };
    275 
    276 struct CodecProfileLevel {
    277     OMX_U32 mProfile;
    278     OMX_U32 mLevel;
    279 };
    280 
    281 inline static const char *asString(MetadataBufferType i, const char *def = "??") {
    282     using namespace android;
    283     switch (i) {
    284         case kMetadataBufferTypeCameraSource:   return "CameraSource";
    285         case kMetadataBufferTypeGrallocSource:  return "GrallocSource";
    286         case kMetadataBufferTypeANWBuffer:      return "ANWBuffer";
    287         case kMetadataBufferTypeNativeHandleSource: return "NativeHandleSource";
    288         case kMetadataBufferTypeInvalid:        return "Invalid";
    289         default:                                return def;
    290     }
    291 }
    292 
    293 }  // namespace android
    294 
    295 #endif  // ANDROID_IOMX_H_
    296