3 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@gmail.com>
6 * gstv4l2object.h: base class for V4L2 elements
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.
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.
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.
24 #ifndef __GST_V4L2_OBJECT_H__
25 #define __GST_V4L2_OBJECT_H__
27 #include "ext/videodev2.h"
32 #include "v4l2-utils.h"
35 #include <gst/base/gstpushsrc.h>
37 #include <gst/video/video.h>
40 typedef struct _GstV4l2Object GstV4l2Object;
41 typedef struct _GstV4l2ObjectClassHelper GstV4l2ObjectClassHelper;
43 #include <gstv4l2bufferpool.h>
45 /* size of v4l2 buffer pool in streaming case, obj->info needs to be valid */
46 #define GST_V4L2_MIN_BUFFERS(obj) \
47 ((GST_VIDEO_INFO_INTERLACE_MODE (&obj->info) == \
48 GST_VIDEO_INTERLACE_MODE_ALTERNATE) ? \
49 /* 2x buffers needed with each field in its own buffer */ \
52 /* max frame width/height */
53 #define GST_V4L2_MAX_SIZE (1<<15) /* 2^15 == 32768 */
57 #define GST_TYPE_V4L2_IO_MODE (gst_v4l2_io_mode_get_type ())
58 GType gst_v4l2_io_mode_get_type (void);
60 #define GST_V4L2_OBJECT(obj) (GstV4l2Object *)(obj)
66 GST_V4L2_IO_USERPTR = 3,
67 GST_V4L2_IO_DMABUF = 4,
68 GST_V4L2_IO_DMABUF_IMPORT = 5
71 typedef gboolean (*GstV4l2GetInOutFunction) (GstV4l2Object * v4l2object, guint32 * input);
72 typedef gboolean (*GstV4l2SetInOutFunction) (GstV4l2Object * v4l2object, guint32 input);
73 typedef gboolean (*GstV4l2UpdateFpsFunction) (GstV4l2Object * v4l2object);
75 /* On Android NDK r18b the ioctl() signature uses 'unsigned' instead of
76 * 'unsigned long' for the 2nd parameter */
78 typedef unsigned ioctl_req_t;
80 typedef gulong ioctl_req_t;
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))
89 /* simple check whether the device is open */
90 #define GST_V4L2_IS_OPEN(o) ((o)->video_fd > 0)
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)
97 /* checks whether the current v4lv4l2object has already been open()'ed or not */
98 #define GST_V4L2_CHECK_OPEN(v4l2object) \
99 if (!GST_V4L2_IS_OPEN(v4l2object)) \
101 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS, \
102 (_("Device is not open.")), (NULL)); \
106 /* checks whether the current v4lv4l2object is close()'ed or whether it is still open */
107 #define GST_V4L2_CHECK_NOT_OPEN(v4l2object) \
108 if (GST_V4L2_IS_OPEN(v4l2object)) \
110 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS, \
111 (_("Device is open.")), (NULL)); \
115 /* checks whether we're out of capture mode or not */
116 #define GST_V4L2_CHECK_NOT_ACTIVE(v4l2object) \
117 if (GST_V4L2_IS_ACTIVE(v4l2object)) \
119 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS, \
120 (NULL), ("Device is in streaming mode")); \
125 struct _GstV4l2Object {
126 GstElement * element;
129 enum v4l2_buf_type type; /* V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_BUF_TYPE_VIDEO_OUTPUT */
131 /* the video device */
134 /* the video-device's file descriptor */
139 gboolean can_poll_device;
143 /* the current format */
144 struct v4l2_fmtdesc *fmtdesc;
145 struct v4l2_format format;
147 GstVideoAlignment align;
148 GstVideoTransferFunction transfer;
151 gboolean need_video_meta;
152 gboolean has_alpha_component;
154 /* only used if the device supports MPLANE
155 * nb planes is meaning of v4l2 planes
156 * the gstreamer equivalent is gst_buffer_n_memory
160 /* We cache the frame duration if known */
161 GstClockTime duration;
163 /* if the MPLANE device support both contiguous and non contiguous
164 * it allows to select which one we want. But we prefered_non_contiguous
165 * non contiguous mode.
167 gboolean prefered_non_contiguous;
169 /* This will be set if supported in decide_allocation. It can be used to
170 * calculate the minimum latency. */
174 GstV4l2IOMode req_mode;
178 /* the sequence of pool to identify (for debugging) */
181 /* the video device's capabilities */
182 struct v4l2_capability vcap;
183 /* opened device specific capabilities */
187 GSList *formats; /* list of available capture formats */
188 GstCaps *probed_caps;
199 GstStructure *extra_controls;
200 gboolean keep_aspect;
204 GstV4l2GetInOutFunction get_in_out_func;
205 GstV4l2SetInOutFunction set_in_out_func;
206 GstV4l2UpdateFpsFunction update_fps_func;
209 gint (*fd_open) (gint fd, gint v4l2_flags);
210 gint (*close) (gint fd);
211 gint (*dup) (gint fd);
212 gint (*ioctl) (gint fd, ioctl_req_t request, ...);
213 gssize (*read) (gint fd, gpointer buffer, gsize n);
214 gpointer (*mmap) (gpointer start, gsize length, gint prot, gint flags,
215 gint fd, off_t offset);
216 gint (*munmap) (gpointer _start, gsize length);
219 /* Skips interlacing probes */
220 gboolean never_interlaced;
221 /* Allow to skip reading initial format through G_FMT. Some devices
222 * just fails if you don't call S_FMT first. (ex: M2M decoders) */
223 gboolean no_initial_format;
224 /* Avoid any try_fmt probe. This is used by v4l2src to speedup start up time
225 * on slow USB firmwares. When this is set, gst_v4l2_set_format() will modify
226 * the caps to reflect what was negotiated during fixation */
227 gboolean skip_try_fmt_probes;
230 struct _GstV4l2ObjectClassHelper {
235 GType gst_v4l2_object_get_type (void);
237 #define V4L2_STD_OBJECT_PROPS \
248 PROP_OUTPUT_IO_MODE, \
249 PROP_CAPTURE_IO_MODE, \
250 PROP_EXTRA_CONTROLS, \
251 PROP_PIXEL_ASPECT_RATIO, \
252 PROP_FORCE_ASPECT_RATIO
255 GstV4l2Object* gst_v4l2_object_new (GstElement * element,
257 enum v4l2_buf_type type,
258 const char * default_device,
259 GstV4l2GetInOutFunction get_in_out_func,
260 GstV4l2SetInOutFunction set_in_out_func,
261 GstV4l2UpdateFpsFunction update_fps_func);
263 void gst_v4l2_object_destroy (GstV4l2Object * v4l2object);
267 void gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
268 const char * default_device);
270 void gst_v4l2_object_install_m2m_properties_helper (GObjectClass * gobject_class);
272 gboolean gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
274 const GValue * value,
276 gboolean gst_v4l2_object_get_property_helper (GstV4l2Object *v4l2object,
277 guint prop_id, GValue * value,
280 gboolean gst_v4l2_object_open (GstV4l2Object * v4l2object, GstV4l2Error * error);
281 gboolean gst_v4l2_object_open_shared (GstV4l2Object * v4l2object, GstV4l2Object * other);
282 gboolean gst_v4l2_object_close (GstV4l2Object * v4l2object);
286 GstCaps* gst_v4l2_object_get_all_caps (void);
288 GstCaps* gst_v4l2_object_get_raw_caps (void);
290 GstCaps* gst_v4l2_object_get_codec_caps (void);
292 gboolean gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps, GstV4l2Error * error);
293 gboolean gst_v4l2_object_try_format (GstV4l2Object * v4l2object, GstCaps * caps, GstV4l2Error * error);
294 gboolean gst_v4l2_object_try_import (GstV4l2Object * v4l2object, GstBuffer * buffer);
296 gboolean gst_v4l2_object_caps_equal (GstV4l2Object * v4l2object, GstCaps * caps);
297 gboolean gst_v4l2_object_caps_is_subset (GstV4l2Object * v4l2object, GstCaps * caps);
298 GstCaps * gst_v4l2_object_get_current_caps (GstV4l2Object * v4l2object);
300 gboolean gst_v4l2_object_unlock (GstV4l2Object * v4l2object);
301 gboolean gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object);
303 gboolean gst_v4l2_object_stop (GstV4l2Object * v4l2object);
305 GstCaps * gst_v4l2_object_probe_caps (GstV4l2Object * v4l2object, GstCaps * filter);
306 GstCaps * gst_v4l2_object_get_caps (GstV4l2Object * v4l2object, GstCaps * filter);
308 gboolean gst_v4l2_object_acquire_format (GstV4l2Object * v4l2object, GstVideoInfo * info);
310 gboolean gst_v4l2_object_setup_padding (GstV4l2Object * obj);
312 gboolean gst_v4l2_object_decide_allocation (GstV4l2Object * v4l2object, GstQuery * query);
314 gboolean gst_v4l2_object_propose_allocation (GstV4l2Object * obj, GstQuery * query);
316 GstBufferPool * gst_v4l2_object_get_buffer_pool (GstV4l2Object * v4l2object);
318 GstStructure * gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc);
320 GstFlowReturn gst_v4l2_object_poll (GstV4l2Object * v4l2object, GstClockTime timeout);
321 gboolean gst_v4l2_object_subscribe_event (GstV4l2Object * v4l2object, guint32 event);
324 gboolean gst_v4l2_object_set_crop (GstV4l2Object * obj, struct v4l2_rect *result);
325 gboolean gst_v4l2_object_get_crop_bounds (GstV4l2Object * obj, struct v4l2_rect *bounds);
326 gboolean gst_v4l2_object_get_crop_default (GstV4l2Object * obj, struct v4l2_rect *bounds);
328 /* TODO Move to proper namespace */
329 /* open/close the device */
330 gboolean gst_v4l2_open (GstV4l2Object * v4l2object, GstV4l2Error * error);
331 gboolean gst_v4l2_dup (GstV4l2Object * v4l2object, GstV4l2Object * other);
332 gboolean gst_v4l2_close (GstV4l2Object * v4l2object);
334 /* norm/input/output */
335 gboolean gst_v4l2_get_norm (GstV4l2Object * v4l2object, v4l2_std_id * norm);
336 gboolean gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm);
337 gboolean gst_v4l2_get_input (GstV4l2Object * v4l2object, guint32 * input);
338 gboolean gst_v4l2_set_input (GstV4l2Object * v4l2object, guint32 input);
339 gboolean gst_v4l2_query_input (GstV4l2Object * v4l2object, struct v4l2_input * input);
340 gboolean gst_v4l2_get_output (GstV4l2Object * v4l2object, guint32 * output);
341 gboolean gst_v4l2_set_output (GstV4l2Object * v4l2object, guint32 output);
344 gboolean gst_v4l2_set_dv_timings (GstV4l2Object * v4l2object, struct v4l2_dv_timings *timings);
345 gboolean gst_v4l2_query_dv_timings (GstV4l2Object * v4l2object, struct v4l2_dv_timings *timings);
347 /* frequency control */
348 gboolean gst_v4l2_get_frequency (GstV4l2Object * v4l2object, gint tunernum, gulong * frequency);
349 gboolean gst_v4l2_set_frequency (GstV4l2Object * v4l2object, gint tunernum, gulong frequency);
350 gboolean gst_v4l2_signal_strength (GstV4l2Object * v4l2object, gint tunernum, gulong * signal);
352 /* attribute control */
353 gboolean gst_v4l2_get_attribute (GstV4l2Object * v4l2object, int attribute, int * value);
354 gboolean gst_v4l2_set_attribute (GstV4l2Object * v4l2object, int attribute, const int value);
355 gboolean gst_v4l2_set_string_attribute (GstV4l2Object * v4l2object, int attribute_num, const char *value);
356 gboolean gst_v4l2_set_controls (GstV4l2Object * v4l2object, GstStructure * controls);
359 gboolean gst_v4l2_subscribe_event (GstV4l2Object * v4l2object, guint32 event, guint32 id);
360 gboolean gst_v4l2_dequeue_event (GstV4l2Object * v4l2object, struct v4l2_event *event);
364 #endif /* __GST_V4L2_OBJECT_H__ */