v4l2: Add run-time environment to enable libv4l2
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2object.h
1 /* GStreamer
2  *
3  * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4  *               2006 Edgard Lima <edgard.lima@gmail.com>
5  *
6  * gstv4l2object.h: base class for V4L2 elements
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 #ifndef __GST_V4L2_OBJECT_H__
25 #define __GST_V4L2_OBJECT_H__
26
27 #include "ext/videodev2.h"
28 #ifdef HAVE_LIBV4L2
29 #  include <libv4l2.h>
30 #endif
31
32 #include "v4l2-utils.h"
33
34 #include <gst/gst.h>
35 #include <gst/base/gstpushsrc.h>
36
37 #include <gst/video/video.h>
38
39 typedef struct _GstV4l2Object GstV4l2Object;
40 typedef struct _GstV4l2ObjectClassHelper GstV4l2ObjectClassHelper;
41 typedef struct _GstV4l2Xv GstV4l2Xv;
42
43 #include <gstv4l2bufferpool.h>
44
45 /* size of v4l2 buffer pool in streaming case */
46 #define GST_V4L2_MIN_BUFFERS 2
47
48 /* max frame width/height */
49 #define GST_V4L2_MAX_SIZE (1<<15) /* 2^15 == 32768 */
50
51 G_BEGIN_DECLS
52
53 #define GST_TYPE_V4L2_IO_MODE (gst_v4l2_io_mode_get_type ())
54 GType gst_v4l2_io_mode_get_type (void);
55
56 #define GST_V4L2_OBJECT(obj) (GstV4l2Object *)(obj)
57
58 typedef enum {
59   GST_V4L2_IO_AUTO          = 0,
60   GST_V4L2_IO_RW            = 1,
61   GST_V4L2_IO_MMAP          = 2,
62   GST_V4L2_IO_USERPTR       = 3,
63   GST_V4L2_IO_DMABUF        = 4,
64   GST_V4L2_IO_DMABUF_IMPORT = 5
65 } GstV4l2IOMode;
66
67 typedef gboolean  (*GstV4l2GetInOutFunction)  (GstV4l2Object * v4l2object, gint * input);
68 typedef gboolean  (*GstV4l2SetInOutFunction)  (GstV4l2Object * v4l2object, gint input);
69 typedef gboolean  (*GstV4l2UpdateFpsFunction) (GstV4l2Object * v4l2object);
70
71 #define GST_V4L2_WIDTH(o)        (GST_VIDEO_INFO_WIDTH (&(o)->info))
72 #define GST_V4L2_HEIGHT(o)       (GST_VIDEO_INFO_HEIGHT (&(o)->info))
73 #define GST_V4L2_PIXELFORMAT(o)  ((o)->fmtdesc->pixelformat)
74 #define GST_V4L2_FPS_N(o)        (GST_VIDEO_INFO_FPS_N (&(o)->info))
75 #define GST_V4L2_FPS_D(o)        (GST_VIDEO_INFO_FPS_D (&(o)->info))
76
77 /* simple check whether the device is open */
78 #define GST_V4L2_IS_OPEN(o)      ((o)->video_fd > 0)
79
80 /* check whether the device is 'active' */
81 #define GST_V4L2_IS_ACTIVE(o)    ((o)->active)
82 #define GST_V4L2_SET_ACTIVE(o)   ((o)->active = TRUE)
83 #define GST_V4L2_SET_INACTIVE(o) ((o)->active = FALSE)
84
85 /* checks whether the current v4lv4l2object has already been open()'ed or not */
86 #define GST_V4L2_CHECK_OPEN(v4l2object)                         \
87   if (!GST_V4L2_IS_OPEN(v4l2object))                            \
88   {                                                             \
89     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS, \
90       (_("Device is not open.")), (NULL));                      \
91     return FALSE;                                               \
92   }
93
94 /* checks whether the current v4lv4l2object is close()'ed or whether it is still open */
95 #define GST_V4L2_CHECK_NOT_OPEN(v4l2object)                     \
96   if (GST_V4L2_IS_OPEN(v4l2object))                             \
97   {                                                             \
98     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS, \
99       (_("Device is open.")), (NULL));                          \
100     return FALSE;                                               \
101   }
102
103 /* checks whether we're out of capture mode or not */
104 #define GST_V4L2_CHECK_NOT_ACTIVE(v4l2object)                   \
105   if (GST_V4L2_IS_ACTIVE(v4l2object))                           \
106   {                                                             \
107     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS, \
108       (NULL), ("Device is in streaming mode"));                 \
109     return FALSE;                                               \
110   }
111
112
113 struct _GstV4l2Object {
114   GstElement * element;
115
116   enum v4l2_buf_type type;   /* V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_BUF_TYPE_VIDEO_OUTPUT */
117
118   /* the video device */
119   char *videodev;
120
121   /* the video-device's file descriptor */
122   gint video_fd;
123   GstV4l2IOMode mode;
124
125   gboolean active;
126   gboolean streaming;
127
128   /* the current format */
129   struct v4l2_fmtdesc *fmtdesc;
130   struct v4l2_format format;
131   GstVideoInfo info;
132   GstVideoAlignment align;
133
134   /* Features */
135   gboolean need_video_meta;
136   gboolean has_alpha_component;
137
138   /* only used if the device supports MPLANE
139    * nb planes is meaning of v4l2 planes
140    * the gstreamer equivalent is gst_buffer_n_memory
141    */
142   gint n_v4l2_planes;
143
144   /* We cache the frame duration if known */
145   GstClockTime duration;
146
147   /* if the MPLANE device support both contiguous and non contiguous
148    * it allows to select which one we want. But we prefered_non_contiguous
149    * non contiguous mode.
150    */
151   gboolean prefered_non_contiguous;
152
153   /* This will be set if supported in decide_allocation. It can be used to
154    * calculate the minimum latency. */
155   guint32 min_buffers;
156
157   /* This will be set if supported in propose allocation. */
158   guint32 min_buffers_for_output;
159
160   /* wanted mode */
161   GstV4l2IOMode req_mode;
162
163   /* optional pool */
164   GstBufferPool *pool;
165
166   /* the video device's capabilities */
167   struct v4l2_capability vcap;
168   /* opened device specific capabilities */
169   guint32 device_caps;
170
171   /* the video device's window properties */
172   struct v4l2_window vwin;
173
174   /* some more info about the current input's capabilities */
175   struct v4l2_input vinput;
176
177   /* lists... */
178   GSList *formats;              /* list of available capture formats */
179   GstCaps *probed_caps;
180
181   GList *colors;
182   GList *norms;
183   GList *channels;
184   GData *controls;
185
186   /* properties */
187   v4l2_std_id tv_norm;
188   gchar *channel;
189   gulong frequency;
190   GstStructure *extra_controls;
191   gboolean keep_aspect;
192   GValue *par;
193
194   /* X-overlay */
195   GstV4l2Xv *xv;
196   gulong xwindow_id;
197
198   /* funcs */
199   GstV4l2GetInOutFunction  get_in_out_func;
200   GstV4l2SetInOutFunction  set_in_out_func;
201   GstV4l2UpdateFpsFunction update_fps_func;
202
203   /* syscalls */
204   int (*fd_open) (int fd, int v4l2_flags);
205   int (*close) (int fd);
206   int (*dup) (int fd);
207   int (*ioctl) (int fd, unsigned long int request, ...);
208   ssize_t (*read) (int fd, void *buffer, size_t n);
209   void * (*mmap) (void *start, size_t length, int prot, int flags,
210       int fd, int64_t offset);
211   int (*munmap) (void *_start, size_t length);
212
213   /* Quirks */
214   /* Skips interlacing probes */
215   gboolean never_interlaced;
216   /* Allow to skip reading initial format through G_FMT. Some devices
217    * just fails if you don't call S_FMT first. (ex: M2M decoders) */
218   gboolean no_initial_format;
219   /* Avoid any try_fmt probe. This is used by v4l2src to speedup start up time
220    * on slow USB firmwares. When this is set, gst_v4l2_set_format() will modify
221    * the caps to reflect what was negotiated during fixation */
222   gboolean skip_try_fmt_probes;
223 };
224
225 struct _GstV4l2ObjectClassHelper {
226   /* probed devices */
227   GList *devices;
228 };
229
230 GType gst_v4l2_object_get_type (void);
231
232 #define V4L2_STD_OBJECT_PROPS \
233     PROP_DEVICE,              \
234     PROP_DEVICE_NAME,         \
235     PROP_DEVICE_FD,           \
236     PROP_FLAGS,               \
237     PROP_BRIGHTNESS,          \
238     PROP_CONTRAST,            \
239     PROP_SATURATION,          \
240     PROP_HUE,                 \
241     PROP_TV_NORM,             \
242     PROP_IO_MODE,             \
243     PROP_OUTPUT_IO_MODE,      \
244     PROP_CAPTURE_IO_MODE,     \
245     PROP_EXTRA_CONTROLS,      \
246     PROP_PIXEL_ASPECT_RATIO,  \
247     PROP_FORCE_ASPECT_RATIO
248
249 /* create/destroy */
250 GstV4l2Object*  gst_v4l2_object_new       (GstElement * element,
251                                            enum v4l2_buf_type  type,
252                                            const char * default_device,
253                                            GstV4l2GetInOutFunction get_in_out_func,
254                                            GstV4l2SetInOutFunction set_in_out_func,
255                                            GstV4l2UpdateFpsFunction update_fps_func);
256
257 void         gst_v4l2_object_destroy   (GstV4l2Object * v4l2object);
258
259 /* properties */
260
261 void         gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
262                                                         const char * default_device);
263
264 void         gst_v4l2_object_install_m2m_properties_helper (GObjectClass * gobject_class);
265
266 gboolean     gst_v4l2_object_set_property_helper       (GstV4l2Object * v4l2object,
267                                                         guint prop_id,
268                                                         const GValue * value,
269                                                         GParamSpec * pspec);
270 gboolean     gst_v4l2_object_get_property_helper       (GstV4l2Object *v4l2object,
271                                                         guint prop_id, GValue * value,
272                                                         GParamSpec * pspec);
273 /* open/close */
274 gboolean     gst_v4l2_object_open            (GstV4l2Object * v4l2object);
275 gboolean     gst_v4l2_object_open_shared     (GstV4l2Object * v4l2object, GstV4l2Object * other);
276 gboolean     gst_v4l2_object_close           (GstV4l2Object * v4l2object);
277
278 /* probing */
279
280 GstCaps*     gst_v4l2_object_get_all_caps (void);
281
282 GstCaps*     gst_v4l2_object_get_raw_caps (void);
283
284 GstCaps*     gst_v4l2_object_get_codec_caps (void);
285
286 gint         gst_v4l2_object_extrapolate_stride (const GstVideoFormatInfo * finfo,
287                                                   gint plane, gint stride);
288
289 gboolean     gst_v4l2_object_set_format  (GstV4l2Object * v4l2object, GstCaps * caps, GstV4l2Error * error);
290 gboolean     gst_v4l2_object_try_format  (GstV4l2Object * v4l2object, GstCaps * caps, GstV4l2Error * error);
291
292 gboolean     gst_v4l2_object_caps_equal  (GstV4l2Object * v4l2object, GstCaps * caps);
293
294 gboolean     gst_v4l2_object_unlock      (GstV4l2Object * v4l2object);
295 gboolean     gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object);
296
297 gboolean     gst_v4l2_object_stop        (GstV4l2Object * v4l2object);
298
299 GstCaps *    gst_v4l2_object_probe_caps  (GstV4l2Object * v4l2object, GstCaps * filter);
300 GstCaps *    gst_v4l2_object_get_caps    (GstV4l2Object * v4l2object, GstCaps * filter);
301
302 gboolean     gst_v4l2_object_acquire_format (GstV4l2Object * v4l2object, GstVideoInfo * info);
303
304 gboolean     gst_v4l2_object_set_crop    (GstV4l2Object * obj);
305
306 gboolean     gst_v4l2_object_decide_allocation (GstV4l2Object * v4l2object, GstQuery * query);
307
308 gboolean     gst_v4l2_object_propose_allocation (GstV4l2Object * obj, GstQuery * query);
309
310 GstStructure * gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc);
311
312 /* TODO Move to proper namespace */
313 /* open/close the device */
314 gboolean     gst_v4l2_open           (GstV4l2Object * v4l2object);
315 gboolean     gst_v4l2_dup            (GstV4l2Object * v4l2object, GstV4l2Object * other);
316 gboolean     gst_v4l2_close          (GstV4l2Object * v4l2object);
317
318 /* norm/input/output */
319 gboolean     gst_v4l2_get_norm       (GstV4l2Object * v4l2object, v4l2_std_id * norm);
320 gboolean     gst_v4l2_set_norm       (GstV4l2Object * v4l2object, v4l2_std_id norm);
321 gboolean     gst_v4l2_get_input      (GstV4l2Object * v4l2object, gint * input);
322 gboolean     gst_v4l2_set_input      (GstV4l2Object * v4l2object, gint input);
323 gboolean     gst_v4l2_get_output     (GstV4l2Object * v4l2object, gint * output);
324 gboolean     gst_v4l2_set_output     (GstV4l2Object * v4l2object, gint output);
325
326 /* frequency control */
327 gboolean     gst_v4l2_get_frequency   (GstV4l2Object * v4l2object, gint tunernum, gulong * frequency);
328 gboolean     gst_v4l2_set_frequency   (GstV4l2Object * v4l2object, gint tunernum, gulong frequency);
329 gboolean     gst_v4l2_signal_strength (GstV4l2Object * v4l2object, gint tunernum, gulong * signal);
330
331 /* attribute control */
332 gboolean     gst_v4l2_get_attribute   (GstV4l2Object * v4l2object, int attribute, int * value);
333 gboolean     gst_v4l2_set_attribute   (GstV4l2Object * v4l2object, int attribute, const int value);
334 gboolean     gst_v4l2_set_controls    (GstV4l2Object * v4l2object, GstStructure * controls);
335
336 G_END_DECLS
337
338 #endif /* __GST_V4L2_OBJECT_H__ */