gstv4l2object.h 12.6 KB
Newer Older
1 2 3
/* GStreamer
 *
 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 5 6
 *               2006 Edgard Lima <edgard.lima@indt.org.br>
 *
 * gstv4l2object.h: base class for V4L2 elements
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
20 21
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
22 23
 */

24 25
#ifndef __GST_V4L2_OBJECT_H__
#define __GST_V4L2_OBJECT_H__
26 27 28 29 30 31 32 33 34 35 36 37 38

/* Because of some really cool feature in video4linux1, also known as
 * 'not including sys/types.h and sys/time.h', we had to include it
 * ourselves. In all their intelligence, these people decided to fix
 * this in the next version (video4linux2) in such a cool way that it
 * breaks all compilations of old stuff...
 * The real problem is actually that linux/time.h doesn't use proper
 * macro checks before defining types like struct timeval. The proper
 * fix here is to either fuck the kernel header (which is what we do
 * by defining _LINUX_TIME_H, an innocent little hack) or by fixing it
 * upstream, which I'll consider doing later on. If you get compiler
 * errors here, check your linux/time.h && sys/time.h header setup.
 */
39
#include <sys/ioctl.h>
40
#include <sys/types.h>
41 42 43 44 45
#ifdef __sun
#include <sys/videodev2.h>
#elif defined(__FreeBSD__)
#include <linux/videodev2.h>
#else /* linux */
46 47 48 49
#include <linux/types.h>
#define _LINUX_TIME_H
#define __user
#include <linux/videodev2.h>
50
#endif
51 52 53 54

#include <gst/gst.h>
#include <gst/base/gstpushsrc.h>

Wim Taymans's avatar
Wim Taymans committed
55
#include <gst/video/video.h>
56

57 58 59 60 61
typedef struct _GstV4l2Object GstV4l2Object;
typedef struct _GstV4l2ObjectClassHelper GstV4l2ObjectClassHelper;
typedef struct _GstV4l2Xv GstV4l2Xv;

#include <gstv4l2bufferpool.h>
Rob Clark's avatar
Rob Clark committed
62 63 64 65 66 67 68 69

/* size of v4l2 buffer pool in streaming case */
#define GST_V4L2_MAX_BUFFERS 16
#define GST_V4L2_MIN_BUFFERS 1

/* max frame width/height */
#define GST_V4L2_MAX_SIZE (1<<15) /* 2^15 == 32768 */

70 71
G_BEGIN_DECLS

72
#define GST_V4L2_OBJECT(obj) (GstV4l2Object *)(obj)
Stefan Kost's avatar
Stefan Kost committed
73

Wim Taymans's avatar
Wim Taymans committed
74
typedef enum {
Wim Taymans's avatar
Wim Taymans committed
75
  GST_V4L2_IO_AUTO    = 0,
Wim Taymans's avatar
Wim Taymans committed
76 77
  GST_V4L2_IO_RW      = 1,
  GST_V4L2_IO_MMAP    = 2,
78 79
  GST_V4L2_IO_USERPTR = 3,
  GST_V4L2_IO_DMABUF  = 4
Wim Taymans's avatar
Wim Taymans committed
80
} GstV4l2IOMode;
81

82 83
typedef gboolean  (*GstV4l2GetInOutFunction)  (GstV4l2Object * v4l2object, gint * input);
typedef gboolean  (*GstV4l2SetInOutFunction)  (GstV4l2Object * v4l2object, gint input);
84
typedef gboolean  (*GstV4l2UpdateFpsFunction) (GstV4l2Object * v4l2object);
85

Wim Taymans's avatar
Wim Taymans committed
86 87 88 89 90
#define GST_V4L2_WIDTH(o)        (GST_VIDEO_INFO_WIDTH (&(o)->info))
#define GST_V4L2_HEIGHT(o)       (GST_VIDEO_INFO_HEIGHT (&(o)->info))
#define GST_V4L2_PIXELFORMAT(o)  ((o)->fmtdesc->pixelformat)
#define GST_V4L2_FPS_N(o)        (GST_VIDEO_INFO_FPS_N (&(o)->info))
#define GST_V4L2_FPS_D(o)        (GST_VIDEO_INFO_FPS_D (&(o)->info))
91 92 93 94 95 96 97 98 99

/* simple check whether the device is open */
#define GST_V4L2_IS_OPEN(o)      ((o)->video_fd > 0)

/* check whether the device is 'active' */
#define GST_V4L2_IS_ACTIVE(o)    ((o)->active)
#define GST_V4L2_SET_ACTIVE(o)   ((o)->active = TRUE)
#define GST_V4L2_SET_INACTIVE(o) ((o)->active = FALSE)

100 101 102
struct _GstV4l2Object {
  GstElement * element;

103 104
  enum v4l2_buf_type type;   /* V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_BUF_TYPE_VIDEO_OUTPUT */

105 106 107 108 109
  /* the video device */
  char *videodev;

  /* the video-device's file descriptor */
  gint video_fd;
Wim Taymans's avatar
Wim Taymans committed
110
  GstV4l2IOMode mode;
111
  GstPoll * poll;
112
  gboolean can_poll_device;
113

114
  gboolean active;
115
  gboolean streaming;
Rob Clark's avatar
Rob Clark committed
116

117
  /* the current format */
Wim Taymans's avatar
Wim Taymans committed
118 119 120
  struct v4l2_fmtdesc *fmtdesc;
  GstVideoInfo info;

121 122 123 124 125 126 127
  /* only used if the device supports MPLANE
   * nb planes is meaning of v4l2 planes
   * the gstreamer equivalent is gst_buffer_n_memory
   */
  gint n_v4l2_planes;

  guint32 bytesperline[GST_VIDEO_MAX_PLANES];
Wim Taymans's avatar
Wim Taymans committed
128
  guint32 sizeimage;
129
  GstClockTime duration;
130

131 132 133 134 135 136
  /* if the MPLANE device support both contiguous and non contiguous 
   * it allows to select which one we want. But we prefered_non_contiguous
   * non contiguous mode.
   */
  gboolean prefered_non_contiguous;

Wim Taymans's avatar
Wim Taymans committed
137 138 139
  /* wanted mode */
  GstV4l2IOMode req_mode;

140
  /* optional pool */
141
  GstBufferPool *pool;
142

143 144 145 146 147 148 149 150 151 152
  /* the video device's capabilities */
  struct v4l2_capability vcap;

  /* the video device's window properties */
  struct v4l2_window vwin;

  /* some more info about the current input's capabilities */
  struct v4l2_input vinput;

  /* lists... */
Rob Clark's avatar
Rob Clark committed
153
  GSList *formats;              /* list of available capture formats */
154
  GstCaps *probed_caps;
Rob Clark's avatar
Rob Clark committed
155

156
  GList *colors;
157 158
  GList *norms;
  GList *channels;
159
  GData *controls;
160 161

  /* properties */
Rob Clark's avatar
Rob Clark committed
162
  v4l2_std_id tv_norm;
163
  gchar *channel;
164
  gulong frequency;
165
  GstStructure *extra_controls;
166 167
  gboolean keep_aspect;
  GValue *par;
168 169 170 171 172 173

  /* X-overlay */
  GstV4l2Xv *xv;
  gulong xwindow_id;

  /* funcs */
174 175
  GstV4l2GetInOutFunction  get_in_out_func;
  GstV4l2SetInOutFunction  set_in_out_func;
176
  GstV4l2UpdateFpsFunction update_fps_func;
177 178 179 180

  /* Quirks */
  /* Skips interlacing probes */
  gboolean never_interlaced;
181 182 183 184 185 186 187
};

struct _GstV4l2ObjectClassHelper {
  /* probed devices */
  GList *devices;
};

188
GType gst_v4l2_object_get_type (void);
189

190 191 192 193 194 195 196 197 198 199 200 201 202
#define V4L2_STD_OBJECT_PROPS \
    PROP_DEVICE,              \
    PROP_DEVICE_NAME,         \
    PROP_DEVICE_FD,           \
    PROP_FLAGS,               \
    PROP_BRIGHTNESS,          \
    PROP_CONTRAST,            \
    PROP_SATURATION,          \
    PROP_HUE,                 \
    PROP_TV_NORM,             \
    PROP_IO_MODE,             \
    PROP_EXTRA_CONTROLS,      \
    PROP_PIXEL_ASPECT_RATIO,  \
203
    PROP_FORCE_ASPECT_RATIO
204

205
/* create/destroy */
206 207 208 209 210 211 212 213
GstV4l2Object*  gst_v4l2_object_new       (GstElement * element,
                                           enum v4l2_buf_type  type,
                                           const char * default_device,
                                           GstV4l2GetInOutFunction get_in_out_func,
                                           GstV4l2SetInOutFunction set_in_out_func,
                                           GstV4l2UpdateFpsFunction update_fps_func);

void            gst_v4l2_object_destroy   (GstV4l2Object * v4l2object);
214 215

/* properties */
Rob Clark's avatar
Rob Clark committed
216

217 218
void         gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
                                                        const char * default_device);
219

220 221 222 223 224 225 226
gboolean     gst_v4l2_object_set_property_helper       (GstV4l2Object * v4l2object,
                                                        guint prop_id,
                                                        const GValue * value,
                                                        GParamSpec * pspec);
gboolean     gst_v4l2_object_get_property_helper       (GstV4l2Object *v4l2object,
                                                        guint prop_id, GValue * value,
                                                        GParamSpec * pspec);
227
/* open/close */
228 229
gboolean     gst_v4l2_object_open            (GstV4l2Object *v4l2object);
gboolean     gst_v4l2_object_close           (GstV4l2Object *v4l2object);
230 231

/* probing */
232
#if 0
233 234 235 236 237 238 239 240 241 242 243
const GList* gst_v4l2_probe_get_properties  (GstPropertyProbe * probe);

void         gst_v4l2_probe_probe_property  (GstPropertyProbe * probe, guint prop_id,
                                             const GParamSpec * pspec,
                                             GList ** klass_devices);
gboolean     gst_v4l2_probe_needs_probe     (GstPropertyProbe * probe, guint prop_id,
                                             const GParamSpec * pspec,
                                             GList ** klass_devices);
GValueArray* gst_v4l2_probe_get_values      (GstPropertyProbe * probe, guint prop_id,
                                             const GParamSpec * pspec,
                                             GList ** klass_devices);
244
#endif
245

Rob Clark's avatar
Rob Clark committed
246 247
GstCaps*      gst_v4l2_object_get_all_caps (void);

248 249 250 251
GstCaps*      gst_v4l2_object_get_raw_caps (void);

GstCaps*      gst_v4l2_object_get_codec_caps (void);

Rob Clark's avatar
Rob Clark committed
252 253
GstStructure* gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc);

254
gboolean      gst_v4l2_object_set_format  (GstV4l2Object * v4l2object, GstCaps * caps);
Rob Clark's avatar
Rob Clark committed
255

256 257
gboolean      gst_v4l2_object_caps_equal  (GstV4l2Object * v4l2object, GstCaps * caps);

258 259
gboolean      gst_v4l2_object_unlock      (GstV4l2Object * v4l2object);
gboolean      gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object);
Wim Taymans's avatar
Wim Taymans committed
260

261
gboolean      gst_v4l2_object_stop        (GstV4l2Object * v4l2object);
262 263


Wim Taymans's avatar
Wim Taymans committed
264
gboolean      gst_v4l2_object_copy        (GstV4l2Object * v4l2object,
265
                                           GstBuffer * dest, GstBuffer * src);
Wim Taymans's avatar
Wim Taymans committed
266

267 268 269
GstCaps *     gst_v4l2_object_get_caps    (GstV4l2Object * v4l2object,
                                           GstCaps * filter);

Rob Clark's avatar
Rob Clark committed
270

271 272 273 274
#define GST_IMPLEMENT_V4L2_PROBE_METHODS(Type_Class, interface_as_function)                 \
                                                                                            \
static void                                                                                 \
interface_as_function ## _probe_probe_property (GstPropertyProbe * probe,                   \
275
                                                guint prop_id,                              \
276 277
                                                const GParamSpec * pspec)                   \
{                                                                                           \
278
  Type_Class *this_class = (Type_Class*) G_OBJECT_GET_CLASS (probe);                        \
279
  gst_v4l2_probe_probe_property (probe, prop_id, pspec,                                     \
280
                                 &this_class->v4l2_class_devices);                          \
281 282 283 284
}                                                                                           \
                                                                                            \
static gboolean                                                                             \
interface_as_function ## _probe_needs_probe (GstPropertyProbe * probe,                      \
285
                                             guint prop_id,                                 \
286 287
                                             const GParamSpec * pspec)                      \
{                                                                                           \
288
  Type_Class *this_class = (Type_Class*) G_OBJECT_GET_CLASS (probe);                        \
289
  return gst_v4l2_probe_needs_probe (probe, prop_id, pspec,                                 \
290
                                     &this_class->v4l2_class_devices);                      \
291 292 293 294
}                                                                                           \
                                                                                            \
static GValueArray *                                                                        \
interface_as_function ## _probe_get_values (GstPropertyProbe * probe,                       \
295
                                            guint prop_id,                                  \
296 297
                                            const GParamSpec * pspec)                       \
{                                                                                           \
298
  Type_Class *this_class = (Type_Class*) G_OBJECT_GET_CLASS (probe);                        \
299
  return gst_v4l2_probe_get_values (probe, prop_id, pspec,                                  \
300
                                    &this_class->v4l2_class_devices);                       \
301 302
}                                                                                           \
                                                                                            \
303
static void                                                                                 \
304 305 306 307 308
interface_as_function ## _property_probe_interface_init (GstPropertyProbeInterface * iface) \
{                                                                                           \
  iface->get_properties = gst_v4l2_probe_get_properties;                                    \
  iface->probe_property = interface_as_function ## _probe_probe_property;                   \
  iface->needs_probe = interface_as_function ## _probe_needs_probe;                         \
309
  iface->get_values = interface_as_function ## _probe_get_values;                           \
310 311 312 313
}

G_END_DECLS

314
#endif /* __GST_V4L2_OBJECT_H__ */