3 * Copyright (C) 2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@indt.org.br>
6 * v4l2_calls.c - generic V4L2 calls handling
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.
28 #include <sys/types.h>
31 #include <sys/ioctl.h>
37 /* Needed on older Solaris Nevada builds (72 at least) */
39 #include <sys/ioccom.h>
41 #include "v4l2_calls.h"
42 #include "gstv4l2tuner.h"
44 #include "gstv4l2xoverlay.h"
46 #include "gstv4l2colorbalance.h"
48 #include "gstv4l2src.h"
49 #include "gstv4l2sink.h"
50 #include "gstv4l2videodec.h"
52 #include "gst/gst-i18n-plugin.h"
54 /* Those are ioctl calls */
55 #ifndef V4L2_CID_HCENTER
56 #define V4L2_CID_HCENTER V4L2_CID_HCENTER_DEPRECATED
58 #ifndef V4L2_CID_VCENTER
59 #define V4L2_CID_VCENTER V4L2_CID_VCENTER_DEPRECATED
62 #ifndef V4L2_CAP_VIDEO_M2M
63 #define V4L2_CAP_VIDEO_M2M 0x00008000
65 #ifndef V4L2_CAP_VIDEO_M2M_MPLANE
66 #define V4L2_CAP_VIDEO_M2M_MPLANE 0x00004000
69 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
70 #define GST_CAT_DEFAULT v4l2_debug
72 /******************************************************
73 * gst_v4l2_get_capabilities():
74 * get the device's capturing capabilities
75 * return value: TRUE on success, FALSE on error
76 ******************************************************/
78 gst_v4l2_get_capabilities (GstV4l2Object * v4l2object)
82 e = v4l2object->element;
84 GST_DEBUG_OBJECT (e, "getting capabilities");
86 if (!GST_V4L2_IS_OPEN (v4l2object))
89 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYCAP, &v4l2object->vcap) < 0)
92 GST_LOG_OBJECT (e, "driver: '%s'", v4l2object->vcap.driver);
93 GST_LOG_OBJECT (e, "card: '%s'", v4l2object->vcap.card);
94 GST_LOG_OBJECT (e, "bus_info: '%s'", v4l2object->vcap.bus_info);
95 GST_LOG_OBJECT (e, "version: %08x", v4l2object->vcap.version);
96 GST_LOG_OBJECT (e, "capabilites: %08x", v4l2object->vcap.capabilities);
103 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
104 (_("Error getting capabilities for device '%s': "
105 "It isn't a v4l2 driver. Check if it is a v4l1 driver."),
106 v4l2object->videodev), GST_ERROR_SYSTEM);
111 /******************************************************
112 * The video4linux command line tool v4l2-ctrl
113 * normalises the names of the controls received from
116 * "Exposure (absolute)" -> "exposure_absolute"
118 * We follow their lead here. @name is modified
120 ******************************************************/
122 gst_v4l2_normalise_control_name (gchar * name)
125 for (i = 0, j = 0; name[j]; ++j) {
126 if (g_ascii_isalnum (name[j])) {
127 if (i > 0 && !g_ascii_isalnum (name[j - 1]))
129 name[i++] = g_ascii_tolower (name[j]);
135 /******************************************************
136 * gst_v4l2_empty_lists() and gst_v4l2_fill_lists():
137 * fill/empty the lists of enumerations
138 * return value: TRUE on success, FALSE on error
139 ******************************************************/
141 gst_v4l2_fill_lists (GstV4l2Object * v4l2object)
144 struct v4l2_queryctrl control = { 0, };
148 e = v4l2object->element;
150 GST_DEBUG_OBJECT (e, "getting enumerations");
151 GST_V4L2_CHECK_OPEN (v4l2object);
153 GST_DEBUG_OBJECT (e, " channels");
154 /* and now, the channels */
156 struct v4l2_input input;
157 GstV4l2TunerChannel *v4l2channel;
158 GstTunerChannel *channel;
160 memset (&input, 0, sizeof (input));
163 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUMINPUT, &input) < 0) {
164 if (errno == EINVAL || errno == ENOTTY)
165 break; /* end of enumeration */
167 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
168 (_("Failed to query attributes of input %d in device %s"),
169 n, v4l2object->videodev),
170 ("Failed to get %d in input enumeration for %s. (%d - %s)",
171 n, v4l2object->videodev, errno, strerror (errno)));
176 GST_LOG_OBJECT (e, " index: %d", input.index);
177 GST_LOG_OBJECT (e, " name: '%s'", input.name);
178 GST_LOG_OBJECT (e, " type: %08x", input.type);
179 GST_LOG_OBJECT (e, " audioset: %08x", input.audioset);
180 GST_LOG_OBJECT (e, " std: %016" G_GINT64_MODIFIER "x",
181 (guint64) input.std);
182 GST_LOG_OBJECT (e, " status: %08x", input.status);
184 v4l2channel = g_object_new (GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
185 channel = GST_TUNER_CHANNEL (v4l2channel);
186 channel->label = g_strdup ((const gchar *) input.name);
187 channel->flags = GST_TUNER_CHANNEL_INPUT;
188 v4l2channel->index = n;
190 if (input.type == V4L2_INPUT_TYPE_TUNER) {
191 struct v4l2_tuner vtun;
193 v4l2channel->tuner = input.tuner;
194 channel->flags |= GST_TUNER_CHANNEL_FREQUENCY;
196 vtun.index = input.tuner;
197 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &vtun) < 0) {
198 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
199 (_("Failed to get setting of tuner %d on device '%s'."),
200 input.tuner, v4l2object->videodev), GST_ERROR_SYSTEM);
201 g_object_unref (G_OBJECT (channel));
205 channel->freq_multiplicator =
206 62.5 * ((vtun.capability & V4L2_TUNER_CAP_LOW) ? 1 : 1000);
207 channel->min_frequency = vtun.rangelow * channel->freq_multiplicator;
208 channel->max_frequency = vtun.rangehigh * channel->freq_multiplicator;
209 channel->min_signal = 0;
210 channel->max_signal = 0xffff;
212 if (input.audioset) {
213 /* we take the first. We don't care for
214 * the others for now */
215 while (!(input.audioset & (1 << v4l2channel->audio)))
216 v4l2channel->audio++;
217 channel->flags |= GST_TUNER_CHANNEL_AUDIO;
220 v4l2object->channels =
221 g_list_prepend (v4l2object->channels, (gpointer) channel);
223 v4l2object->channels = g_list_reverse (v4l2object->channels);
225 GST_DEBUG_OBJECT (e, " norms");
228 struct v4l2_standard standard = { 0, };
229 GstV4l2TunerNorm *v4l2norm;
233 /* fill in defaults */
234 standard.frameperiod.numerator = 1;
235 standard.frameperiod.denominator = 0;
238 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUMSTD, &standard) < 0) {
239 if (errno == EINVAL || errno == ENOTTY)
240 break; /* end of enumeration */
242 else if (errno == ENODATA)
243 break; /* end of enumeration, as of Linux 3.7-rc1 */
246 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
247 (_("Failed to query norm on device '%s'."),
248 v4l2object->videodev),
249 ("Failed to get attributes for norm %d on devide '%s'. (%d - %s)",
250 n, v4l2object->videodev, errno, strerror (errno)));
255 GST_DEBUG_OBJECT (e, " '%s', fps: %d / %d",
256 standard.name, standard.frameperiod.denominator,
257 standard.frameperiod.numerator);
259 v4l2norm = g_object_new (GST_TYPE_V4L2_TUNER_NORM, NULL);
260 norm = GST_TUNER_NORM (v4l2norm);
261 norm->label = g_strdup ((const gchar *) standard.name);
262 gst_value_set_fraction (&norm->framerate,
263 standard.frameperiod.denominator, standard.frameperiod.numerator);
264 v4l2norm->index = standard.id;
266 GST_DEBUG_OBJECT (v4l2object->element, "index=%08x, label=%s",
267 (unsigned int) v4l2norm->index, norm->label);
269 v4l2object->norms = g_list_prepend (v4l2object->norms, (gpointer) norm);
271 v4l2object->norms = g_list_reverse (v4l2object->norms);
273 GST_DEBUG_OBJECT (e, " controls+menus");
275 /* and lastly, controls+menus (if appropriate) */
276 #ifdef V4L2_CTRL_FLAG_NEXT_CTRL
277 next = V4L2_CTRL_FLAG_NEXT_CTRL;
285 GstV4l2ColorBalanceChannel *v4l2channel;
286 GstColorBalanceChannel *channel;
292 /* when we reached the last official CID, continue with private CIDs */
293 if (n == V4L2_CID_LASTP1) {
294 GST_DEBUG_OBJECT (e, "checking private CIDs");
295 n = V4L2_CID_PRIVATE_BASE;
297 GST_DEBUG_OBJECT (e, "checking control %08x", n);
299 control.id = n | next;
300 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
303 GST_DEBUG_OBJECT (e, "controls finished");
306 GST_DEBUG_OBJECT (e, "V4L2_CTRL_FLAG_NEXT_CTRL not supported.");
312 if (errno == EINVAL || errno == ENOTTY || errno == EIO || errno == ENOENT) {
313 if (n < V4L2_CID_PRIVATE_BASE) {
314 GST_DEBUG_OBJECT (e, "skipping control %08x", n);
315 /* continue so that we also check private controls */
318 GST_DEBUG_OBJECT (e, "controls finished");
322 GST_WARNING_OBJECT (e, "Failed querying control %d on device '%s'. "
323 "(%d - %s)", n, v4l2object->videodev, errno, strerror (errno));
327 /* bogus driver might mess with id in unexpected ways (e.g. set to 0), so
328 * make sure to simply try all if V4L2_CTRL_FLAG_NEXT_CTRL not supported */
331 if (control.flags & V4L2_CTRL_FLAG_DISABLED) {
332 GST_DEBUG_OBJECT (e, "skipping disabled control");
335 #ifdef V4L2_CTRL_TYPE_CTRL_CLASS
336 if (control.type == V4L2_CTRL_TYPE_CTRL_CLASS) {
337 GST_DEBUG_OBJECT (e, "starting control class '%s'", control.name);
341 switch (control.type) {
342 case V4L2_CTRL_TYPE_INTEGER:
343 case V4L2_CTRL_TYPE_BOOLEAN:
344 case V4L2_CTRL_TYPE_MENU:
345 #ifdef V4L2_CTRL_TYPE_INTEGER_MENU
346 case V4L2_CTRL_TYPE_INTEGER_MENU:
348 #ifdef V4L2_CTRL_TYPE_BITMASK
349 case V4L2_CTRL_TYPE_BITMASK:
351 case V4L2_CTRL_TYPE_BUTTON:{
352 control.name[31] = '\0';
353 gst_v4l2_normalise_control_name ((gchar *) control.name);
354 g_datalist_id_set_data (&v4l2object->controls,
355 g_quark_from_string ((const gchar *) control.name),
356 GINT_TO_POINTER (n));
361 "Control type for '%s' not suppored for extra controls.",
367 case V4L2_CID_BRIGHTNESS:
368 case V4L2_CID_CONTRAST:
369 case V4L2_CID_SATURATION:
371 case V4L2_CID_BLACK_LEVEL:
372 case V4L2_CID_AUTO_WHITE_BALANCE:
373 case V4L2_CID_DO_WHITE_BALANCE:
374 case V4L2_CID_RED_BALANCE:
375 case V4L2_CID_BLUE_BALANCE:
377 case V4L2_CID_EXPOSURE:
378 case V4L2_CID_AUTOGAIN:
380 #ifdef V4L2_CID_SHARPNESS
381 case V4L2_CID_SHARPNESS:
383 /* we only handle these for now (why?) */
387 #ifndef V4L2_CID_PAN_RESET
388 case V4L2_CID_HCENTER:
390 #ifndef V4L2_CID_TILT_RESET
391 case V4L2_CID_VCENTER:
393 #ifdef V4L2_CID_PAN_RESET
394 case V4L2_CID_PAN_RESET:
396 #ifdef V4L2_CID_TILT_RESET
397 case V4L2_CID_TILT_RESET:
399 /* not handled here, handled by VideoOrientation interface */
402 case V4L2_CID_AUDIO_VOLUME:
403 case V4L2_CID_AUDIO_BALANCE:
404 case V4L2_CID_AUDIO_BASS:
405 case V4L2_CID_AUDIO_TREBLE:
406 case V4L2_CID_AUDIO_MUTE:
407 case V4L2_CID_AUDIO_LOUDNESS:
408 /* FIXME: We should implement GstMixer interface instead */
409 /* but let's not be pedantic and make element more useful for now */
413 "ControlID %s (%x) unhandled, FIXME", control.name, n);
420 GST_DEBUG_OBJECT (e, "Adding ControlID %s (%x)", control.name, n);
421 v4l2channel = g_object_new (GST_TYPE_V4L2_COLOR_BALANCE_CHANNEL, NULL);
422 channel = GST_COLOR_BALANCE_CHANNEL (v4l2channel);
423 channel->label = g_strdup ((const gchar *) control.name);
427 /* FIXME: it will be need just when handling private controls
428 *(currently none of base controls are of this type) */
429 if (control.type == V4L2_CTRL_TYPE_MENU) {
430 struct v4l2_querymenu menu, *mptr;
437 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYMENU, &menu) < 0) {
439 break; /* end of enumeration */
441 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
442 (_("Failed getting controls attributes on device '%s'."),
443 v4l2object->videodev),
444 ("Failed to get %d in menu enumeration for %s. (%d - %s)",
445 n, v4l2object->videodev, errno, strerror (errno)));
449 mptr = g_malloc (sizeof (menu));
450 memcpy (mptr, &menu, sizeof (menu));
451 menus = g_list_append (menus, mptr);
454 v4l2object->menus = g_list_append (v4l2object->menus, menus);
457 switch (control.type) {
458 case V4L2_CTRL_TYPE_INTEGER:
459 channel->min_value = control.minimum;
460 channel->max_value = control.maximum;
462 case V4L2_CTRL_TYPE_BOOLEAN:
463 channel->min_value = FALSE;
464 channel->max_value = TRUE;
467 /* FIXME we should find out how to handle V4L2_CTRL_TYPE_BUTTON.
468 BUTTON controls like V4L2_CID_DO_WHITE_BALANCE can just be set (1) or
469 unset (0), but can't be queried */
471 "Control with non supported type %s (%x), type=%d",
472 control.name, n, control.type);
473 channel->min_value = channel->max_value = 0;
478 g_list_prepend (v4l2object->colors, (gpointer) channel);
480 v4l2object->colors = g_list_reverse (v4l2object->colors);
482 GST_DEBUG_OBJECT (e, "done");
488 gst_v4l2_empty_lists (GstV4l2Object * v4l2object)
490 GST_DEBUG_OBJECT (v4l2object->element, "deleting enumerations");
492 g_list_foreach (v4l2object->channels, (GFunc) g_object_unref, NULL);
493 g_list_free (v4l2object->channels);
494 v4l2object->channels = NULL;
496 g_list_foreach (v4l2object->norms, (GFunc) g_object_unref, NULL);
497 g_list_free (v4l2object->norms);
498 v4l2object->norms = NULL;
500 g_list_foreach (v4l2object->colors, (GFunc) g_object_unref, NULL);
501 g_list_free (v4l2object->colors);
502 v4l2object->colors = NULL;
504 g_datalist_clear (&v4l2object->controls);
508 gst_v4l2_adjust_buf_type (GstV4l2Object * v4l2object)
510 /* when calling gst_v4l2_object_new the user decides the initial type
511 * so adjust it if multi-planar is supported
512 * the driver should make it exclusive. So the driver should
513 * not support both MPLANE and non-PLANE.
514 * Because even when using MPLANE it still possibles to use it
515 * in a contiguous manner. In this case the first v4l2 plane
516 * contains all the gst planes.
518 #ifdef V4L2_CAP_VIDEO_M2M_MPLANE
519 #define CHECK_CAPS (V4L2_CAP_VIDEO_OUTPUT_MPLANE | V4L2_CAP_VIDEO_M2M_MPLANE)
521 #define CHECK_CAPS (V4L2_CAP_VIDEO_OUTPUT_MPLANE)
524 switch (v4l2object->type) {
525 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
526 if (v4l2object->vcap.capabilities & CHECK_CAPS) {
527 GST_DEBUG ("adjust type to multi-planar output");
528 v4l2object->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
531 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
532 if (v4l2object->vcap.capabilities & CHECK_CAPS) {
533 GST_DEBUG ("adjust type to multi-planar capture");
534 v4l2object->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
542 /******************************************************
544 * open the video device (v4l2object->videodev)
545 * return value: TRUE on success, FALSE on error
546 ******************************************************/
548 gst_v4l2_open (GstV4l2Object * v4l2object)
552 GstPollFD pollfd = GST_POLL_FD_INIT;
554 GST_DEBUG_OBJECT (v4l2object->element, "Trying to open device %s",
555 v4l2object->videodev);
557 GST_V4L2_CHECK_NOT_OPEN (v4l2object);
558 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
560 /* be sure we have a device */
561 if (!v4l2object->videodev)
562 v4l2object->videodev = g_strdup ("/dev/video");
564 /* check if it is a device */
565 if (stat (v4l2object->videodev, &st) == -1)
568 if (!S_ISCHR (st.st_mode))
571 /* open the device */
572 v4l2object->video_fd =
573 open (v4l2object->videodev, O_RDWR /* | O_NONBLOCK */ );
575 if (!GST_V4L2_IS_OPEN (v4l2object))
578 libv4l2_fd = v4l2_fd_open (v4l2object->video_fd,
579 V4L2_ENABLE_ENUM_FMT_EMULATION);
580 /* Note the v4l2_xxx functions are designed so that if they get passed an
581 unknown fd, the will behave exactly as their regular xxx counterparts, so
582 if v4l2_fd_open fails, we continue as normal (missing the libv4l2 custom
583 cam format to normal formats conversion). Chances are big we will still
584 fail then though, as normally v4l2_fd_open only fails if the device is not
586 if (libv4l2_fd != -1)
587 v4l2object->video_fd = libv4l2_fd;
589 v4l2object->can_poll_device = TRUE;
591 /* get capabilities, error will be posted */
592 if (!gst_v4l2_get_capabilities (v4l2object))
595 /* do we need to be a capture device? */
596 if (GST_IS_V4L2SRC (v4l2object->element) &&
597 !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_CAPTURE |
598 V4L2_CAP_VIDEO_CAPTURE_MPLANE)))
601 if (GST_IS_V4L2SINK (v4l2object->element) &&
602 !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_OUTPUT |
603 V4L2_CAP_VIDEO_OUTPUT_MPLANE)))
606 if (GST_IS_V4L2_VIDEO_DEC (v4l2object->element) &&
607 /* Today's M2M device only expose M2M */
608 !((v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_M2M |
609 V4L2_CAP_VIDEO_M2M_MPLANE)) ||
610 /* But legacy driver may expose both CAPTURE and OUTPUT */
611 ((v4l2object->vcap.capabilities &
612 (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) &&
613 (v4l2object->vcap.capabilities &
614 (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE)))))
617 gst_v4l2_adjust_buf_type (v4l2object);
619 /* create enumerations, posts errors. */
620 if (!gst_v4l2_fill_lists (v4l2object))
623 GST_INFO_OBJECT (v4l2object->element,
624 "Opened device '%s' (%s) successfully",
625 v4l2object->vcap.card, v4l2object->videodev);
627 pollfd.fd = v4l2object->video_fd;
628 gst_poll_add_fd (v4l2object->poll, &pollfd);
629 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
630 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
631 gst_poll_fd_ctl_read (v4l2object->poll, &pollfd, TRUE);
633 gst_poll_fd_ctl_write (v4l2object->poll, &pollfd, TRUE);
635 if (v4l2object->extra_controls)
636 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
638 /* UVC devices are never interlaced, and doing VIDIOC_TRY_FMT on them
639 * causes expensive and slow USB IO, so don't probe them for interlaced
641 if (!strcmp ((char *) v4l2object->vcap.driver, "uvcusb")) {
642 v4l2object->never_interlaced = TRUE;
650 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
651 (_("Cannot identify device '%s'."), v4l2object->videodev),
657 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
658 (_("This isn't a device '%s'."), v4l2object->videodev),
664 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
665 (_("Could not open device '%s' for reading and writing."),
666 v4l2object->videodev), GST_ERROR_SYSTEM);
671 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
672 (_("Device '%s' is not a capture device."),
673 v4l2object->videodev),
674 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
679 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
680 (_("Device '%s' is not a output device."),
681 v4l2object->videodev),
682 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
687 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
688 (_("Device '%s' is not a M2M device."),
689 v4l2object->videodev),
690 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
695 if (GST_V4L2_IS_OPEN (v4l2object)) {
697 v4l2_close (v4l2object->video_fd);
698 v4l2object->video_fd = -1;
701 gst_v4l2_empty_lists (v4l2object);
708 gst_v4l2_dup (GstV4l2Object * v4l2object, GstV4l2Object * other)
710 GstPollFD pollfd = GST_POLL_FD_INIT;
712 GST_DEBUG_OBJECT (v4l2object->element, "Trying to dup device %s",
715 GST_V4L2_CHECK_OPEN (other);
716 GST_V4L2_CHECK_NOT_OPEN (v4l2object);
717 GST_V4L2_CHECK_NOT_ACTIVE (other);
718 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
720 v4l2object->vcap = other->vcap;
721 gst_v4l2_adjust_buf_type (v4l2object);
723 v4l2object->video_fd = v4l2_dup (other->video_fd);
724 if (!GST_V4L2_IS_OPEN (v4l2object))
727 g_free (v4l2object->videodev);
728 v4l2object->videodev = g_strdup (other->videodev);
730 GST_INFO_OBJECT (v4l2object->element,
731 "Cloned device '%s' (%s) successfully",
732 v4l2object->vcap.card, v4l2object->videodev);
734 pollfd.fd = v4l2object->video_fd;
735 gst_poll_add_fd (v4l2object->poll, &pollfd);
736 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
737 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
738 gst_poll_fd_ctl_read (v4l2object->poll, &pollfd, TRUE);
740 gst_poll_fd_ctl_write (v4l2object->poll, &pollfd, TRUE);
742 v4l2object->never_interlaced = other->never_interlaced;
743 v4l2object->can_poll_device = TRUE;
749 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
750 (_("Could not dup device '%s' for reading and writing."),
751 v4l2object->videodev), GST_ERROR_SYSTEM);
758 /******************************************************
760 * close the video device (v4l2object->video_fd)
761 * return value: TRUE on success, FALSE on error
762 ******************************************************/
764 gst_v4l2_close (GstV4l2Object * v4l2object)
766 GstPollFD pollfd = GST_POLL_FD_INIT;
767 GST_DEBUG_OBJECT (v4l2object->element, "Trying to close %s",
768 v4l2object->videodev);
770 GST_V4L2_CHECK_OPEN (v4l2object);
771 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
774 v4l2_close (v4l2object->video_fd);
775 pollfd.fd = v4l2object->video_fd;
776 gst_poll_remove_fd (v4l2object->poll, &pollfd);
777 v4l2object->video_fd = -1;
780 gst_v4l2_empty_lists (v4l2object);
786 /******************************************************
787 * gst_v4l2_get_norm()
788 * Get the norm of the current device
789 * return value: TRUE on success, FALSE on error
790 ******************************************************/
792 gst_v4l2_get_norm (GstV4l2Object * v4l2object, v4l2_std_id * norm)
794 GST_DEBUG_OBJECT (v4l2object->element, "getting norm");
796 if (!GST_V4L2_IS_OPEN (v4l2object))
799 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_STD, norm) < 0)
807 GST_DEBUG ("Failed to get the current norm for device %s",
808 v4l2object->videodev);
814 /******************************************************
815 * gst_v4l2_set_norm()
816 * Set the norm of the current device
817 * return value: TRUE on success, FALSE on error
818 ******************************************************/
820 gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm)
822 GST_DEBUG_OBJECT (v4l2object->element, "trying to set norm to "
823 "%" G_GINT64_MODIFIER "x", (guint64) norm);
825 if (!GST_V4L2_IS_OPEN (v4l2object))
828 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_STD, &norm) < 0)
836 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
837 (_("Failed to set norm for device '%s'."),
838 v4l2object->videodev), GST_ERROR_SYSTEM);
843 /******************************************************
844 * gst_v4l2_get_frequency():
845 * get the current frequency
846 * return value: TRUE on success, FALSE on error
847 ******************************************************/
849 gst_v4l2_get_frequency (GstV4l2Object * v4l2object,
850 gint tunernum, gulong * frequency)
852 struct v4l2_frequency freq = { 0, };
854 GstTunerChannel *channel;
856 GST_DEBUG_OBJECT (v4l2object->element, "getting current tuner frequency");
858 if (!GST_V4L2_IS_OPEN (v4l2object))
861 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
863 freq.tuner = tunernum;
864 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0)
867 *frequency = freq.frequency * channel->freq_multiplicator;
874 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
875 (_("Failed to get current tuner frequency for device '%s'."),
876 v4l2object->videodev), GST_ERROR_SYSTEM);
882 /******************************************************
883 * gst_v4l2_set_frequency():
885 * return value: TRUE on success, FALSE on error
886 ******************************************************/
888 gst_v4l2_set_frequency (GstV4l2Object * v4l2object,
889 gint tunernum, gulong frequency)
891 struct v4l2_frequency freq = { 0, };
893 GstTunerChannel *channel;
895 GST_DEBUG_OBJECT (v4l2object->element,
896 "setting current tuner frequency to %lu", frequency);
898 if (!GST_V4L2_IS_OPEN (v4l2object))
901 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
903 freq.tuner = tunernum;
904 /* fill in type - ignore error */
905 v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq);
906 freq.frequency = frequency / channel->freq_multiplicator;
908 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0)
916 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
917 (_("Failed to set current tuner frequency for device '%s' to %lu Hz."),
918 v4l2object->videodev, frequency), GST_ERROR_SYSTEM);
923 /******************************************************
924 * gst_v4l2_signal_strength():
925 * get the strength of the signal on the current input
926 * return value: TRUE on success, FALSE on error
927 ******************************************************/
929 gst_v4l2_signal_strength (GstV4l2Object * v4l2object,
930 gint tunernum, gulong * signal_strength)
932 struct v4l2_tuner tuner = { 0, };
934 GST_DEBUG_OBJECT (v4l2object->element, "trying to get signal strength");
936 if (!GST_V4L2_IS_OPEN (v4l2object))
939 tuner.index = tunernum;
940 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &tuner) < 0)
943 *signal_strength = tuner.signal;
950 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
951 (_("Failed to get signal strength for device '%s'."),
952 v4l2object->videodev), GST_ERROR_SYSTEM);
957 /******************************************************
958 * gst_v4l2_get_attribute():
959 * try to get the value of one specific attribute
960 * return value: TRUE on success, FALSE on error
961 ******************************************************/
963 gst_v4l2_get_attribute (GstV4l2Object * v4l2object,
964 int attribute_num, int *value)
966 struct v4l2_control control = { 0, };
968 GST_DEBUG_OBJECT (v4l2object->element, "getting value of attribute %d",
971 if (!GST_V4L2_IS_OPEN (v4l2object))
974 control.id = attribute_num;
976 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0)
979 *value = control.value;
986 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
987 (_("Failed to get value for control %d on device '%s'."),
988 attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
994 /******************************************************
995 * gst_v4l2_set_attribute():
996 * try to set the value of one specific attribute
997 * return value: TRUE on success, FALSE on error
998 ******************************************************/
1000 gst_v4l2_set_attribute (GstV4l2Object * v4l2object,
1001 int attribute_num, const int value)
1003 struct v4l2_control control = { 0, };
1005 GST_DEBUG_OBJECT (v4l2object->element, "setting value of attribute %d to %d",
1006 attribute_num, value);
1008 if (!GST_V4L2_IS_OPEN (v4l2object))
1011 control.id = attribute_num;
1012 control.value = value;
1013 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0)
1021 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1022 (_("Failed to set value %d for control %d on device '%s'."),
1023 value, attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
1029 set_control (GQuark field_id, const GValue * value, gpointer user_data)
1031 GstV4l2Object *v4l2object = user_data;
1032 GQuark normalised_field_id;
1035 /* 32 bytes is the maximum size for a control name according to v4l2 */
1038 /* Backwards compatibility: in the past GStreamer would normalise strings in
1039 a subtly different way to v4l2-ctl. e.g. the kernel's "Focus (absolute)"
1040 would become "focus__absolute_" whereas now it becomes "focus_absolute".
1041 Please remove the following in GStreamer 1.5 for 1.6 */
1042 strncpy (name, g_quark_to_string (field_id), sizeof (name));
1044 gst_v4l2_normalise_control_name (name);
1045 normalised_field_id = g_quark_from_string (name);
1046 if (normalised_field_id != field_id)
1047 g_warning ("In GStreamer 1.4 the way V4L2 control names were normalised "
1048 "changed. Instead of setting \"%s\" please use \"%s\". The former is "
1049 "deprecated and will be removed in a future version of GStreamer",
1050 g_quark_to_string (field_id), name);
1051 field_id = normalised_field_id;
1053 d = g_datalist_id_get_data (&v4l2object->controls, field_id);
1055 GST_WARNING_OBJECT (v4l2object,
1056 "Control '%s' does not exist or has an unsupported type.",
1057 g_quark_to_string (field_id));
1060 if (!G_VALUE_HOLDS (value, G_TYPE_INT)) {
1061 GST_WARNING_OBJECT (v4l2object,
1062 "'int' value expected for control '%s'.", g_quark_to_string (field_id));
1065 gst_v4l2_set_attribute (v4l2object, GPOINTER_TO_INT (d),
1066 g_value_get_int (value));
1071 gst_v4l2_set_controls (GstV4l2Object * v4l2object, GstStructure * controls)
1073 return gst_structure_foreach (controls, set_control, v4l2object);
1077 gst_v4l2_get_input (GstV4l2Object * v4l2object, gint * input)
1081 GST_DEBUG_OBJECT (v4l2object->element, "trying to get input");
1083 if (!GST_V4L2_IS_OPEN (v4l2object))
1086 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_INPUT, &n) < 0)
1091 GST_DEBUG_OBJECT (v4l2object->element, "input: %d", n);
1097 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1098 /* only give a warning message if driver actually claims to have tuner
1101 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1102 (_("Failed to get current input on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1108 gst_v4l2_set_input (GstV4l2Object * v4l2object, gint input)
1110 GST_DEBUG_OBJECT (v4l2object->element, "trying to set input to %d", input);
1112 if (!GST_V4L2_IS_OPEN (v4l2object))
1115 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_INPUT, &input) < 0)
1122 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1123 /* only give a warning message if driver actually claims to have tuner
1126 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1127 (_("Failed to set input %d on device %s."),
1128 input, v4l2object->videodev), GST_ERROR_SYSTEM);
1134 gst_v4l2_get_output (GstV4l2Object * v4l2object, gint * output)
1138 GST_DEBUG_OBJECT (v4l2object->element, "trying to get output");
1140 if (!GST_V4L2_IS_OPEN (v4l2object))
1143 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_OUTPUT, &n) < 0)
1148 GST_DEBUG_OBJECT (v4l2object->element, "output: %d", n);
1154 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1155 /* only give a warning message if driver actually claims to have tuner
1158 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1159 (_("Failed to get current output on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1165 gst_v4l2_set_output (GstV4l2Object * v4l2object, gint output)
1167 GST_DEBUG_OBJECT (v4l2object->element, "trying to set output to %d", output);
1169 if (!GST_V4L2_IS_OPEN (v4l2object))
1172 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_OUTPUT, &output) < 0)
1179 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1180 /* only give a warning message if driver actually claims to have tuner
1183 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1184 (_("Failed to set output %d on device %s."),
1185 output, v4l2object->videodev), GST_ERROR_SYSTEM);