d4fa9fe6b57ef038bc1e75c318e514cfb5dabec7
[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@indt.org.br>
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 /* Because of some really cool feature in video4linux1, also known as
28  * 'not including sys/types.h and sys/time.h', we had to include it
29  * ourselves. In all their intelligence, these people decided to fix
30  * this in the next version (video4linux2) in such a cool way that it
31  * breaks all compilations of old stuff...
32  * The real problem is actually that linux/time.h doesn't use proper
33  * macro checks before defining types like struct timeval. The proper
34  * fix here is to either fuck the kernel header (which is what we do
35  * by defining _LINUX_TIME_H, an innocent little hack) or by fixing it
36  * upstream, which I'll consider doing later on. If you get compiler
37  * errors here, check your linux/time.h && sys/time.h header setup.
38  */
39 #include <sys/ioctl.h>
40 #include <sys/types.h>
41 #ifdef __sun
42 #include <sys/videodev2.h>
43 #elif defined(__FreeBSD__)
44 #include <linux/videodev2.h>
45 #else /* linux */
46 #include <linux/types.h>
47 #define _LINUX_TIME_H
48 #define __user
49 #include <linux/videodev2.h>
50 #endif
51
52 #include <gst/gst.h>
53 #include <gst/base/gstpushsrc.h>
54
55 #include <gst/video/video.h>
56
57 typedef struct _GstV4l2Object GstV4l2Object;
58 typedef struct _GstV4l2ObjectClassHelper GstV4l2ObjectClassHelper;
59 typedef struct _GstV4l2Xv GstV4l2Xv;
60
61 #include <gstv4l2bufferpool.h>
62
63 /* size of v4l2 buffer pool in streaming case */
64 #define GST_V4L2_MAX_BUFFERS 16
65 #define GST_V4L2_MIN_BUFFERS 1
66
67 /* max frame width/height */
68 #define GST_V4L2_MAX_SIZE (1<<15) /* 2^15 == 32768 */
69
70 G_BEGIN_DECLS
71
72 #define GST_V4L2_OBJECT(obj) (GstV4l2Object *)(obj)
73
74 typedef enum {
75   GST_V4L2_IO_AUTO    = 0,
76   GST_V4L2_IO_RW      = 1,
77   GST_V4L2_IO_MMAP    = 2,
78   GST_V4L2_IO_USERPTR = 3,
79   GST_V4L2_IO_DMABUF  = 4
80 } GstV4l2IOMode;
81
82 typedef gboolean  (*GstV4l2GetInOutFunction)  (GstV4l2Object * v4l2object, gint * input);
83 typedef gboolean  (*GstV4l2SetInOutFunction)  (GstV4l2Object * v4l2object, gint input);
84 typedef gboolean  (*GstV4l2UpdateFpsFunction) (GstV4l2Object * v4l2object);
85
86 #define GST_V4L2_WIDTH(o)        (GST_VIDEO_INFO_WIDTH (&(o)->info))
87 #define GST_V4L2_HEIGHT(o)       (GST_VIDEO_INFO_HEIGHT (&(o)->info))
88 #define GST_V4L2_PIXELFORMAT(o)  ((o)->fmtdesc->pixelformat)
89 #define GST_V4L2_FPS_N(o)        (GST_VIDEO_INFO_FPS_N (&(o)->info))
90 #define GST_V4L2_FPS_D(o)        (GST_VIDEO_INFO_FPS_D (&(o)->info))
91
92 /* simple check whether the device is open */
93 #define GST_V4L2_IS_OPEN(o)      ((o)->video_fd > 0)
94
95 /* check whether the device is 'active' */
96 #define GST_V4L2_IS_ACTIVE(o)    ((o)->active)
97 #define GST_V4L2_SET_ACTIVE(o)   ((o)->active = TRUE)
98 #define GST_V4L2_SET_INACTIVE(o) ((o)->active = FALSE)
99
100 struct _GstV4l2Object {
101   GstElement * element;
102
103   enum v4l2_buf_type type;   /* V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_BUF_TYPE_VIDEO_OUTPUT */
104
105   /* the video device */
106   char *videodev;
107
108   /* the video-device's file descriptor */
109   gint video_fd;
110   GstV4l2IOMode mode;
111   GstPoll * poll;
112   gboolean can_poll_device;
113
114   gboolean active;
115   gboolean streaming;
116
117   /* the current format */
118   struct v4l2_fmtdesc *fmtdesc;
119   GstVideoInfo info;
120
121   guint32 bytesperline;
122   guint32 sizeimage;
123   GstClockTime duration;
124
125   /* wanted mode */
126   GstV4l2IOMode req_mode;
127
128   /* optional pool */
129   GstBufferPool *pool;
130
131   /* the video device's capabilities */
132   struct v4l2_capability vcap;
133
134   /* the video device's window properties */
135   struct v4l2_window vwin;
136
137   /* some more info about the current input's capabilities */
138   struct v4l2_input vinput;
139
140   /* lists... */
141   GSList *formats;              /* list of available capture formats */
142
143   GList *colors;
144   GList *norms;
145   GList *channels;
146   GData *controls;
147
148   /* properties */
149   v4l2_std_id tv_norm;
150   gchar *channel;
151   gulong frequency;
152   GstStructure *extra_controls;
153
154   /* X-overlay */
155   GstV4l2Xv *xv;
156   gulong xwindow_id;
157
158   /* funcs */
159   GstV4l2GetInOutFunction  get_in_out_func;
160   GstV4l2SetInOutFunction  set_in_out_func;
161   GstV4l2UpdateFpsFunction update_fps_func;
162 };
163
164 struct _GstV4l2ObjectClassHelper {
165   /* probed devices */
166   GList *devices;
167 };
168
169 GType gst_v4l2_object_get_type (void);
170
171 #define V4L2_STD_OBJECT_PROPS           \
172     PROP_DEVICE,                        \
173     PROP_DEVICE_NAME,                   \
174     PROP_DEVICE_FD,                     \
175     PROP_FLAGS,                         \
176     PROP_BRIGHTNESS,                    \
177     PROP_CONTRAST,                      \
178     PROP_SATURATION,                    \
179     PROP_HUE,                           \
180     PROP_TV_NORM,                       \
181     PROP_IO_MODE,                       \
182     PROP_EXTRA_CONTROLS
183
184 /* create/destroy */
185 GstV4l2Object * gst_v4l2_object_new              (GstElement * element,
186                                                   enum v4l2_buf_type  type,
187                                                   const char *default_device,
188                                                   GstV4l2GetInOutFunction get_in_out_func,
189                                                   GstV4l2SetInOutFunction set_in_out_func,
190                                                   GstV4l2UpdateFpsFunction   update_fps_func);
191 void            gst_v4l2_object_destroy          (GstV4l2Object * v4l2object);
192
193 /* properties */
194
195 void      gst_v4l2_object_install_properties_helper (GObjectClass *gobject_class, const char *default_device);
196
197 gboolean  gst_v4l2_object_set_property_helper       (GstV4l2Object *v4l2object,
198                                                      guint prop_id, const GValue * value,
199                                                      GParamSpec * pspec);
200 gboolean  gst_v4l2_object_get_property_helper       (GstV4l2Object *v4l2object,
201                                                      guint prop_id, GValue * value,
202                                                      GParamSpec * pspec);
203 /* open/close */
204 gboolean  gst_v4l2_object_open               (GstV4l2Object *v4l2object);
205 gboolean  gst_v4l2_object_close              (GstV4l2Object *v4l2object);
206
207 /* probing */
208 #if 0
209 const GList* gst_v4l2_probe_get_properties  (GstPropertyProbe * probe);
210
211 void         gst_v4l2_probe_probe_property  (GstPropertyProbe * probe, guint prop_id,
212                                              const GParamSpec * pspec,
213                                              GList ** klass_devices);
214 gboolean     gst_v4l2_probe_needs_probe     (GstPropertyProbe * probe, guint prop_id,
215                                              const GParamSpec * pspec,
216                                              GList ** klass_devices);
217 GValueArray* gst_v4l2_probe_get_values      (GstPropertyProbe * probe, guint prop_id,
218                                              const GParamSpec * pspec,
219                                              GList ** klass_devices);
220 #endif
221
222 GstCaps*      gst_v4l2_object_probe_caps_for_format (GstV4l2Object *v4l2object, guint32 pixelformat,
223                                              const GstStructure * template);
224
225 GSList*       gst_v4l2_object_get_format_list  (GstV4l2Object *v4l2object);
226
227 GstCaps*      gst_v4l2_object_get_all_caps (void);
228
229 GstStructure* gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc);
230
231 gboolean      gst_v4l2_object_set_format (GstV4l2Object *v4l2object, GstCaps * caps);
232
233 gboolean      gst_v4l2_object_caps_equal  (GstV4l2Object * v4l2object, GstCaps * caps);
234
235 gboolean      gst_v4l2_object_unlock      (GstV4l2Object *v4l2object);
236 gboolean      gst_v4l2_object_unlock_stop (GstV4l2Object *v4l2object);
237
238 gboolean      gst_v4l2_object_stop        (GstV4l2Object *v4l2object);
239
240
241 gboolean      gst_v4l2_object_copy        (GstV4l2Object * v4l2object,
242                                            GstBuffer * dest, GstBuffer *src);
243
244
245 #define GST_IMPLEMENT_V4L2_PROBE_METHODS(Type_Class, interface_as_function)                 \
246                                                                                             \
247 static void                                                                                 \
248 interface_as_function ## _probe_probe_property (GstPropertyProbe * probe,                   \
249                                                 guint prop_id,                              \
250                                                 const GParamSpec * pspec)                   \
251 {                                                                                           \
252   Type_Class *this_class = (Type_Class*) G_OBJECT_GET_CLASS (probe);                        \
253   gst_v4l2_probe_probe_property (probe, prop_id, pspec,                                     \
254                                         &this_class->v4l2_class_devices);                   \
255 }                                                                                           \
256                                                                                             \
257 static gboolean                                                                             \
258 interface_as_function ## _probe_needs_probe (GstPropertyProbe * probe,                      \
259                                              guint prop_id,                                 \
260                                              const GParamSpec * pspec)                      \
261 {                                                                                           \
262   Type_Class *this_class = (Type_Class*) G_OBJECT_GET_CLASS (probe);                        \
263   return gst_v4l2_probe_needs_probe (probe, prop_id, pspec,                                 \
264                                         &this_class->v4l2_class_devices);                   \
265 }                                                                                           \
266                                                                                             \
267 static GValueArray *                                                                        \
268 interface_as_function ## _probe_get_values (GstPropertyProbe * probe,                       \
269                                             guint prop_id,                                  \
270                                             const GParamSpec * pspec)                       \
271 {                                                                                           \
272   Type_Class *this_class = (Type_Class*) G_OBJECT_GET_CLASS (probe);                        \
273   return gst_v4l2_probe_get_values (probe, prop_id, pspec,                                  \
274                                     &this_class->v4l2_class_devices);                       \
275 }                                                                                           \
276                                                                                             \
277 static void                                                                                 \
278 interface_as_function ## _property_probe_interface_init (GstPropertyProbeInterface * iface) \
279 {                                                                                           \
280   iface->get_properties = gst_v4l2_probe_get_properties;                                    \
281   iface->probe_property = interface_as_function ## _probe_probe_property;                   \
282   iface->needs_probe = interface_as_function ## _probe_needs_probe;                         \
283   iface->get_values = interface_as_function ## _probe_get_values;                                            \
284 }
285
286 G_END_DECLS
287
288 #endif /* __GST_V4L2_OBJECT_H__ */