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