v4l2: convert to GstBufferPool
[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., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, 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 #include <gst/controller/gstcontroller.h>
53
54 #include <gst/video/video.h>
55 #include <gst/interfaces/propertyprobe.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
75 typedef gboolean  (*GstV4l2GetInOutFunction)  (GstV4l2Object * v4l2object, gint * input);
76 typedef gboolean  (*GstV4l2SetInOutFunction)  (GstV4l2Object * v4l2object, gint input);
77 typedef gboolean  (*GstV4l2UpdateFpsFunction) (GstV4l2Object * v4l2object);
78
79 #define GST_V4L2_WIDTH(o)        (GST_VIDEO_INFO_WIDTH (&(o)->info))
80 #define GST_V4L2_HEIGHT(o)       (GST_VIDEO_INFO_HEIGHT (&(o)->info))
81 #define GST_V4L2_PIXELFORMAT(o)  ((o)->fmtdesc->pixelformat)
82 #define GST_V4L2_FPS_N(o)        (GST_VIDEO_INFO_FPS_N (&(o)->info))
83 #define GST_V4L2_FPS_D(o)        (GST_VIDEO_INFO_FPS_D (&(o)->info))
84
85 /* simple check whether the device is open */
86 #define GST_V4L2_IS_OPEN(o)      ((o)->video_fd > 0)
87
88 /* check whether the device is 'active' */
89 #define GST_V4L2_IS_ACTIVE(o)    ((o)->active)
90 #define GST_V4L2_SET_ACTIVE(o)   ((o)->active = TRUE)
91 #define GST_V4L2_SET_INACTIVE(o) ((o)->active = FALSE)
92
93 struct _GstV4l2Object {
94   GstElement * element;
95
96   enum v4l2_buf_type type;   /* V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_BUF_TYPE_VIDEO_OUTPUT */
97
98   /* the video device */
99   char *videodev;
100
101   /* the video-device's file descriptor */
102   gint video_fd;
103   GstPoll * poll;
104   gboolean can_poll_device;
105
106   gboolean active;
107   gboolean streaming;
108
109   /* the current format */
110   struct v4l2_fmtdesc *fmtdesc;
111   GstVideoInfo info;
112
113   guint32 bytesperline;
114   guint size;
115   GstClockTime duration;
116
117   /* optional pool */
118   guint32 num_buffers;
119   guint32 min_queued_bufs;
120   gboolean always_copy;
121   gboolean use_mmap;
122   GstBufferPool *pool;
123
124   /* the video device's capabilities */
125   struct v4l2_capability vcap;
126
127   /* the video device's window properties */
128   struct v4l2_window vwin;
129
130   /* some more info about the current input's capabilities */
131   struct v4l2_input vinput;
132
133   /* lists... */
134   GSList *formats;              /* list of available capture formats */
135
136   GList *colors;
137   GList *norms;
138   GList *channels;
139
140   /* properties */
141   v4l2_std_id tv_norm;
142   gchar *channel;
143   gulong frequency;
144
145   /* X-overlay */
146   GstV4l2Xv *xv;
147   gulong xwindow_id;
148
149   /* funcs */
150   GstV4l2GetInOutFunction  get_in_out_func;
151   GstV4l2SetInOutFunction  set_in_out_func;
152   GstV4l2UpdateFpsFunction update_fps_func;
153 };
154
155 struct _GstV4l2ObjectClassHelper {
156   /* probed devices */
157   GList *devices;
158 };
159
160 GType gst_v4l2_object_get_type (void);
161
162 #define V4L2_STD_OBJECT_PROPS           \
163     PROP_DEVICE,                        \
164     PROP_DEVICE_NAME,                   \
165     PROP_DEVICE_FD,                     \
166     PROP_FLAGS,                         \
167     PROP_BRIGHTNESS,                    \
168     PROP_CONTRAST,                      \
169     PROP_SATURATION,                    \
170     PROP_HUE,                           \
171     PROP_TV_NORM
172
173 /* create/destroy */
174 GstV4l2Object * gst_v4l2_object_new              (GstElement * element,
175                                                   enum v4l2_buf_type  type,
176                                                   const char *default_device,
177                                                   GstV4l2GetInOutFunction get_in_out_func,
178                                                   GstV4l2SetInOutFunction set_in_out_func,
179                                                   GstV4l2UpdateFpsFunction   update_fps_func);
180 void            gst_v4l2_object_destroy          (GstV4l2Object * v4l2object);
181
182 /* properties */
183
184 void      gst_v4l2_object_install_properties_helper (GObjectClass *gobject_class, const char *default_device);
185
186 gboolean  gst_v4l2_object_set_property_helper       (GstV4l2Object *v4l2object,
187                                                      guint prop_id, const GValue * value,
188                                                      GParamSpec * pspec);
189 gboolean  gst_v4l2_object_get_property_helper       (GstV4l2Object *v4l2object,
190                                                      guint prop_id, GValue * value,
191                                                      GParamSpec * pspec);
192 /* open/close */
193 gboolean  gst_v4l2_object_open               (GstV4l2Object *v4l2object);
194 gboolean  gst_v4l2_object_close              (GstV4l2Object *v4l2object);
195
196 /* probing */
197 const GList* gst_v4l2_probe_get_properties  (GstPropertyProbe * probe);
198
199 void         gst_v4l2_probe_probe_property  (GstPropertyProbe * probe, guint prop_id,
200                                              const GParamSpec * pspec,
201                                              GList ** klass_devices);
202 gboolean     gst_v4l2_probe_needs_probe     (GstPropertyProbe * probe, guint prop_id,
203                                              const GParamSpec * pspec,
204                                              GList ** klass_devices);
205 GValueArray* gst_v4l2_probe_get_values      (GstPropertyProbe * probe, guint prop_id,
206                                              const GParamSpec * pspec,
207                                              GList ** klass_devices);
208
209 GstCaps*      gst_v4l2_object_probe_caps_for_format (GstV4l2Object *v4l2object, guint32 pixelformat,
210                                              const GstStructure * template);
211
212 GSList*       gst_v4l2_object_get_format_list  (GstV4l2Object *v4l2object);
213
214 GstCaps*      gst_v4l2_object_get_all_caps (void);
215
216 GstStructure* gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc);
217
218 gboolean      gst_v4l2_object_set_format (GstV4l2Object *v4l2object, GstCaps * caps);
219
220 gboolean      gst_v4l2_object_start       (GstV4l2Object *v4l2object);
221 gboolean      gst_v4l2_object_stop        (GstV4l2Object *v4l2object);
222
223
224 /* capture returns a filled buffer, output returns an empty buffer */
225 GstFlowReturn gst_v4l2_object_get_buffer     (GstV4l2Object * v4l2object, GstBuffer ** buf);
226 /* output the filled buffer */
227 GstFlowReturn gst_v4l2_object_output_buffer  (GstV4l2Object * v4l2object, GstBuffer * buf);
228
229
230 #define GST_IMPLEMENT_V4L2_PROBE_METHODS(Type_Class, interface_as_function)                 \
231                                                                                             \
232 static void                                                                                 \
233 interface_as_function ## _probe_probe_property (GstPropertyProbe * probe,                   \
234                                                 guint prop_id,                              \
235                                                 const GParamSpec * pspec)                   \
236 {                                                                                           \
237   Type_Class *this_class = (Type_Class*) G_OBJECT_GET_CLASS (probe);                        \
238   gst_v4l2_probe_probe_property (probe, prop_id, pspec,                                     \
239                                         &this_class->v4l2_class_devices);                   \
240 }                                                                                           \
241                                                                                             \
242 static gboolean                                                                             \
243 interface_as_function ## _probe_needs_probe (GstPropertyProbe * probe,                      \
244                                              guint prop_id,                                 \
245                                              const GParamSpec * pspec)                      \
246 {                                                                                           \
247   Type_Class *this_class = (Type_Class*) G_OBJECT_GET_CLASS (probe);                        \
248   return gst_v4l2_probe_needs_probe (probe, prop_id, pspec,                                 \
249                                         &this_class->v4l2_class_devices);                   \
250 }                                                                                           \
251                                                                                             \
252 static GValueArray *                                                                        \
253 interface_as_function ## _probe_get_values (GstPropertyProbe * probe,                       \
254                                             guint prop_id,                                  \
255                                             const GParamSpec * pspec)                       \
256 {                                                                                           \
257   Type_Class *this_class = (Type_Class*) G_OBJECT_GET_CLASS (probe);                        \
258   return gst_v4l2_probe_get_values (probe, prop_id, pspec,                                  \
259                                     &this_class->v4l2_class_devices);                       \
260 }                                                                                           \
261                                                                                             \
262 static void                                                                                 \
263 interface_as_function ## _property_probe_interface_init (GstPropertyProbeInterface * iface) \
264 {                                                                                           \
265   iface->get_properties = gst_v4l2_probe_get_properties;                                    \
266   iface->probe_property = interface_as_function ## _probe_probe_property;                   \
267   iface->needs_probe = interface_as_function ## _probe_needs_probe;                         \
268   iface->get_values = interface_as_function ## _probe_get_values;                                            \
269 }
270
271 G_END_DECLS
272
273 #endif /* __GST_V4L2_OBJECT_H__ */