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"
51 #include "gst/gst-i18n-plugin.h"
53 /* Those are ioctl calls */
54 #ifndef V4L2_CID_HCENTER
55 #define V4L2_CID_HCENTER V4L2_CID_HCENTER_DEPRECATED
57 #ifndef V4L2_CID_VCENTER
58 #define V4L2_CID_VCENTER V4L2_CID_VCENTER_DEPRECATED
61 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
62 #define GST_CAT_DEFAULT v4l2_debug
64 /******************************************************
65 * gst_v4l2_get_capabilities():
66 * get the device's capturing capabilities
67 * return value: TRUE on success, FALSE on error
68 ******************************************************/
70 gst_v4l2_get_capabilities (GstV4l2Object * v4l2object)
74 e = v4l2object->element;
76 GST_DEBUG_OBJECT (e, "getting capabilities");
78 if (!GST_V4L2_IS_OPEN (v4l2object))
81 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYCAP, &v4l2object->vcap) < 0)
84 GST_LOG_OBJECT (e, "driver: '%s'", v4l2object->vcap.driver);
85 GST_LOG_OBJECT (e, "card: '%s'", v4l2object->vcap.card);
86 GST_LOG_OBJECT (e, "bus_info: '%s'", v4l2object->vcap.bus_info);
87 GST_LOG_OBJECT (e, "version: %08x", v4l2object->vcap.version);
88 GST_LOG_OBJECT (e, "capabilites: %08x", v4l2object->vcap.capabilities);
95 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
96 (_("Error getting capabilities for device '%s': "
97 "It isn't a v4l2 driver. Check if it is a v4l1 driver."),
98 v4l2object->videodev), GST_ERROR_SYSTEM);
104 /******************************************************
105 * gst_v4l2_empty_lists() and gst_v4l2_fill_lists():
106 * fill/empty the lists of enumerations
107 * return value: TRUE on success, FALSE on error
108 ******************************************************/
110 gst_v4l2_fill_lists (GstV4l2Object * v4l2object)
113 struct v4l2_queryctrl control = { 0, };
117 e = v4l2object->element;
119 GST_DEBUG_OBJECT (e, "getting enumerations");
120 GST_V4L2_CHECK_OPEN (v4l2object);
122 GST_DEBUG_OBJECT (e, " channels");
123 /* and now, the channels */
125 struct v4l2_input input;
126 GstV4l2TunerChannel *v4l2channel;
127 GstTunerChannel *channel;
129 memset (&input, 0, sizeof (input));
132 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUMINPUT, &input) < 0) {
133 if (errno == EINVAL || errno == ENOTTY)
134 break; /* end of enumeration */
136 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
137 (_("Failed to query attributes of input %d in device %s"),
138 n, v4l2object->videodev),
139 ("Failed to get %d in input enumeration for %s. (%d - %s)",
140 n, v4l2object->videodev, errno, strerror (errno)));
145 GST_LOG_OBJECT (e, " index: %d", input.index);
146 GST_LOG_OBJECT (e, " name: '%s'", input.name);
147 GST_LOG_OBJECT (e, " type: %08x", input.type);
148 GST_LOG_OBJECT (e, " audioset: %08x", input.audioset);
149 GST_LOG_OBJECT (e, " std: %016" G_GINT64_MODIFIER "x",
150 (guint64) input.std);
151 GST_LOG_OBJECT (e, " status: %08x", input.status);
153 v4l2channel = g_object_new (GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
154 channel = GST_TUNER_CHANNEL (v4l2channel);
155 channel->label = g_strdup ((const gchar *) input.name);
156 channel->flags = GST_TUNER_CHANNEL_INPUT;
157 v4l2channel->index = n;
159 if (input.type == V4L2_INPUT_TYPE_TUNER) {
160 struct v4l2_tuner vtun;
162 v4l2channel->tuner = input.tuner;
163 channel->flags |= GST_TUNER_CHANNEL_FREQUENCY;
165 vtun.index = input.tuner;
166 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &vtun) < 0) {
167 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
168 (_("Failed to get setting of tuner %d on device '%s'."),
169 input.tuner, v4l2object->videodev), GST_ERROR_SYSTEM);
170 g_object_unref (G_OBJECT (channel));
174 channel->freq_multiplicator =
175 62.5 * ((vtun.capability & V4L2_TUNER_CAP_LOW) ? 1 : 1000);
176 channel->min_frequency = vtun.rangelow * channel->freq_multiplicator;
177 channel->max_frequency = vtun.rangehigh * channel->freq_multiplicator;
178 channel->min_signal = 0;
179 channel->max_signal = 0xffff;
181 if (input.audioset) {
182 /* we take the first. We don't care for
183 * the others for now */
184 while (!(input.audioset & (1 << v4l2channel->audio)))
185 v4l2channel->audio++;
186 channel->flags |= GST_TUNER_CHANNEL_AUDIO;
189 v4l2object->channels =
190 g_list_prepend (v4l2object->channels, (gpointer) channel);
192 v4l2object->channels = g_list_reverse (v4l2object->channels);
194 GST_DEBUG_OBJECT (e, " norms");
197 struct v4l2_standard standard = { 0, };
198 GstV4l2TunerNorm *v4l2norm;
202 /* fill in defaults */
203 standard.frameperiod.numerator = 1;
204 standard.frameperiod.denominator = 0;
207 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUMSTD, &standard) < 0) {
208 if (errno == EINVAL || errno == ENOTTY || errno == ENODATA)
209 break; /* end of enumeration */
211 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
212 (_("Failed to query norm on device '%s'."),
213 v4l2object->videodev),
214 ("Failed to get attributes for norm %d on devide '%s'. (%d - %s)",
215 n, v4l2object->videodev, errno, strerror (errno)));
220 GST_DEBUG_OBJECT (e, " '%s', fps: %d / %d",
221 standard.name, standard.frameperiod.denominator,
222 standard.frameperiod.numerator);
224 v4l2norm = g_object_new (GST_TYPE_V4L2_TUNER_NORM, NULL);
225 norm = GST_TUNER_NORM (v4l2norm);
226 norm->label = g_strdup ((const gchar *) standard.name);
227 gst_value_set_fraction (&norm->framerate,
228 standard.frameperiod.denominator, standard.frameperiod.numerator);
229 v4l2norm->index = standard.id;
231 GST_DEBUG_OBJECT (v4l2object->element, "index=%08x, label=%s",
232 (unsigned int) v4l2norm->index, norm->label);
234 v4l2object->norms = g_list_prepend (v4l2object->norms, (gpointer) norm);
236 v4l2object->norms = g_list_reverse (v4l2object->norms);
238 GST_DEBUG_OBJECT (e, " controls+menus");
240 /* and lastly, controls+menus (if appropriate) */
241 #ifdef V4L2_CTRL_FLAG_NEXT_CTRL
242 next = V4L2_CTRL_FLAG_NEXT_CTRL;
250 GstV4l2ColorBalanceChannel *v4l2channel;
251 GstColorBalanceChannel *channel;
256 /* when we reached the last official CID, continue with private CIDs */
257 if (n == V4L2_CID_LASTP1) {
258 GST_DEBUG_OBJECT (e, "checking private CIDs");
259 n = V4L2_CID_PRIVATE_BASE;
261 GST_DEBUG_OBJECT (e, "checking control %08x", n);
263 control.id = n | next;
264 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
267 GST_DEBUG_OBJECT (e, "controls finished");
270 GST_DEBUG_OBJECT (e, "V4L2_CTRL_FLAG_NEXT_CTRL not supported.");
276 if (errno == EINVAL || errno == ENOTTY || errno == EIO || errno == ENOENT) {
277 if (n < V4L2_CID_PRIVATE_BASE) {
278 GST_DEBUG_OBJECT (e, "skipping control %08x", n);
279 /* continue so that we also check private controls */
282 GST_DEBUG_OBJECT (e, "controls finished");
286 GST_WARNING_OBJECT (e, "Failed querying control %d on device '%s'. "
287 "(%d - %s)", n, v4l2object->videodev, errno, strerror (errno));
292 if (control.flags & V4L2_CTRL_FLAG_DISABLED) {
293 GST_DEBUG_OBJECT (e, "skipping disabled control");
296 #ifdef V4L2_CTRL_TYPE_CTRL_CLASS
297 if (control.type == V4L2_CTRL_TYPE_CTRL_CLASS) {
298 GST_DEBUG_OBJECT (e, "starting control class '%s'", control.name);
302 switch (control.type) {
303 case V4L2_CTRL_TYPE_INTEGER:
304 case V4L2_CTRL_TYPE_BOOLEAN:
305 case V4L2_CTRL_TYPE_MENU:
306 #ifdef V4L2_CTRL_TYPE_INTEGER_MENU
307 case V4L2_CTRL_TYPE_INTEGER_MENU:
309 #ifdef V4L2_CTRL_TYPE_BITMASK
310 case V4L2_CTRL_TYPE_BITMASK:
312 case V4L2_CTRL_TYPE_BUTTON:{
314 control.name[31] = '\0';
315 for (i = 0; control.name[i]; ++i) {
316 control.name[i] = g_ascii_tolower (control.name[i]);
317 if (!g_ascii_isalnum (control.name[i]))
318 control.name[i] = '_';
320 GST_INFO_OBJECT (e, "adding generic controls '%s'", control.name);
321 g_datalist_id_set_data (&v4l2object->controls,
322 g_quark_from_string ((const gchar *) control.name),
323 GINT_TO_POINTER (n));
328 "Control type for '%s' not suppored for extra controls.",
334 case V4L2_CID_BRIGHTNESS:
335 case V4L2_CID_CONTRAST:
336 case V4L2_CID_SATURATION:
338 case V4L2_CID_BLACK_LEVEL:
339 case V4L2_CID_AUTO_WHITE_BALANCE:
340 case V4L2_CID_DO_WHITE_BALANCE:
341 case V4L2_CID_RED_BALANCE:
342 case V4L2_CID_BLUE_BALANCE:
344 case V4L2_CID_EXPOSURE:
345 case V4L2_CID_AUTOGAIN:
347 #ifdef V4L2_CID_SHARPNESS
348 case V4L2_CID_SHARPNESS:
350 /* we only handle these for now (why?) */
354 #ifndef V4L2_CID_PAN_RESET
355 case V4L2_CID_HCENTER:
357 #ifndef V4L2_CID_TILT_RESET
358 case V4L2_CID_VCENTER:
360 #ifdef V4L2_CID_PAN_RESET
361 case V4L2_CID_PAN_RESET:
363 #ifdef V4L2_CID_TILT_RESET
364 case V4L2_CID_TILT_RESET:
366 /* not handled here, handled by VideoOrientation interface */
369 case V4L2_CID_AUDIO_VOLUME:
370 case V4L2_CID_AUDIO_BALANCE:
371 case V4L2_CID_AUDIO_BASS:
372 case V4L2_CID_AUDIO_TREBLE:
373 case V4L2_CID_AUDIO_MUTE:
374 case V4L2_CID_AUDIO_LOUDNESS:
375 /* FIXME: We should implement GstMixer interface */
379 "ControlID %s (%x) unhandled, FIXME", control.name, n);
386 GST_DEBUG_OBJECT (e, "Adding ControlID %s (%x)", control.name, n);
387 v4l2channel = g_object_new (GST_TYPE_V4L2_COLOR_BALANCE_CHANNEL, NULL);
388 channel = GST_COLOR_BALANCE_CHANNEL (v4l2channel);
389 channel->label = g_strdup ((const gchar *) control.name);
393 /* FIXME: it will be need just when handling private controls
394 *(currently none of base controls are of this type) */
395 if (control.type == V4L2_CTRL_TYPE_MENU) {
396 struct v4l2_querymenu menu, *mptr;
403 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYMENU, &menu) < 0) {
405 break; /* end of enumeration */
407 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
408 (_("Failed getting controls attributes on device '%s'."),
409 v4l2object->videodev),
410 ("Failed to get %d in menu enumeration for %s. (%d - %s)",
411 n, v4l2object->videodev, errno, strerror (errno)));
415 mptr = g_malloc (sizeof (menu));
416 memcpy (mptr, &menu, sizeof (menu));
417 menus = g_list_append (menus, mptr);
420 v4l2object->menus = g_list_append (v4l2object->menus, menus);
423 switch (control.type) {
424 case V4L2_CTRL_TYPE_INTEGER:
425 channel->min_value = control.minimum;
426 channel->max_value = control.maximum;
428 case V4L2_CTRL_TYPE_BOOLEAN:
429 channel->min_value = FALSE;
430 channel->max_value = TRUE;
433 /* FIXME we should find out how to handle V4L2_CTRL_TYPE_BUTTON.
434 BUTTON controls like V4L2_CID_DO_WHITE_BALANCE can just be set (1) or
435 unset (0), but can't be queried */
437 "Control with non supported type %s (%x), type=%d",
438 control.name, n, control.type);
439 channel->min_value = channel->max_value = 0;
444 g_list_prepend (v4l2object->colors, (gpointer) channel);
446 v4l2object->colors = g_list_reverse (v4l2object->colors);
448 GST_DEBUG_OBJECT (e, "done");
454 gst_v4l2_empty_lists (GstV4l2Object * v4l2object)
456 GST_DEBUG_OBJECT (v4l2object->element, "deleting enumerations");
458 g_list_foreach (v4l2object->channels, (GFunc) g_object_unref, NULL);
459 g_list_free (v4l2object->channels);
460 v4l2object->channels = NULL;
462 g_list_foreach (v4l2object->norms, (GFunc) g_object_unref, NULL);
463 g_list_free (v4l2object->norms);
464 v4l2object->norms = NULL;
466 g_list_foreach (v4l2object->colors, (GFunc) g_object_unref, NULL);
467 g_list_free (v4l2object->colors);
468 v4l2object->colors = NULL;
470 g_datalist_clear (&v4l2object->controls);
474 gst_v4l2_adjust_buf_type (GstV4l2Object * v4l2object)
476 /* when calling gst_v4l2_object_new the user decides the initial type
477 * so adjust it if multi-planar is supported
478 * the driver should make it exclusive. So the driver should
479 * not support both MPLANE and non-PLANE.
480 * Because even when using MPLANE it still possibles to use it
481 * in a contiguous manner. In this case the first v4l2 plane
482 * contains all the gst planes.
484 #ifdef V4L2_CAP_VIDEO_M2M_MPLANE
485 #define CHECK_CAPS (V4L2_CAP_VIDEO_OUTPUT_MPLANE | V4L2_CAP_VIDEO_M2M_MPLANE)
487 #define CHECK_CAPS (V4L2_CAP_VIDEO_OUTPUT_MPLANE)
490 switch (v4l2object->type) {
491 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
492 if (v4l2object->vcap.capabilities & CHECK_CAPS) {
493 GST_DEBUG ("adjust type to multi-planar output");
494 v4l2object->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
497 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
498 if (v4l2object->vcap.capabilities & CHECK_CAPS) {
499 /* FIXME: for now it's an untested case so just put a warning */
500 GST_WARNING ("untested V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE");
502 GST_DEBUG ("adjust type to multi-planar capture");
503 v4l2object->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
511 /******************************************************
513 * open the video device (v4l2object->videodev)
514 * return value: TRUE on success, FALSE on error
515 ******************************************************/
517 gst_v4l2_open (GstV4l2Object * v4l2object)
521 GstPollFD pollfd = GST_POLL_FD_INIT;
523 GST_DEBUG_OBJECT (v4l2object->element, "Trying to open device %s",
524 v4l2object->videodev);
526 GST_V4L2_CHECK_NOT_OPEN (v4l2object);
527 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
529 /* be sure we have a device */
530 if (!v4l2object->videodev)
531 v4l2object->videodev = g_strdup ("/dev/video");
533 /* check if it is a device */
534 if (stat (v4l2object->videodev, &st) == -1)
537 if (!S_ISCHR (st.st_mode))
540 /* open the device */
541 v4l2object->video_fd =
542 open (v4l2object->videodev, O_RDWR /* | O_NONBLOCK */ );
544 if (!GST_V4L2_IS_OPEN (v4l2object))
547 libv4l2_fd = v4l2_fd_open (v4l2object->video_fd,
548 V4L2_ENABLE_ENUM_FMT_EMULATION);
549 /* Note the v4l2_xxx functions are designed so that if they get passed an
550 unknown fd, the will behave exactly as their regular xxx counterparts, so
551 if v4l2_fd_open fails, we continue as normal (missing the libv4l2 custom
552 cam format to normal formats conversion). Chances are big we will still
553 fail then though, as normally v4l2_fd_open only fails if the device is not
555 if (libv4l2_fd != -1)
556 v4l2object->video_fd = libv4l2_fd;
558 v4l2object->can_poll_device = TRUE;
560 /* get capabilities, error will be posted */
561 if (!gst_v4l2_get_capabilities (v4l2object))
564 /* do we need to be a capture device? */
565 if (GST_IS_V4L2SRC (v4l2object->element) &&
566 !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_CAPTURE |
567 V4L2_CAP_VIDEO_CAPTURE_MPLANE)))
570 if (GST_IS_V4L2SINK (v4l2object->element) &&
571 !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_OUTPUT |
572 V4L2_CAP_VIDEO_OUTPUT_MPLANE)))
576 gst_v4l2_adjust_buf_type (v4l2object);
578 /* create enumerations, posts errors. */
579 if (!gst_v4l2_fill_lists (v4l2object))
582 GST_INFO_OBJECT (v4l2object->element,
583 "Opened device '%s' (%s) successfully",
584 v4l2object->vcap.card, v4l2object->videodev);
586 pollfd.fd = v4l2object->video_fd;
587 gst_poll_add_fd (v4l2object->poll, &pollfd);
588 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
589 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
590 gst_poll_fd_ctl_read (v4l2object->poll, &pollfd, TRUE);
592 gst_poll_fd_ctl_write (v4l2object->poll, &pollfd, TRUE);
594 if (v4l2object->extra_controls)
595 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
597 /* UVC devices are never interlaced, and doing VIDIOC_TRY_FMT on them
598 * causes expensive and slow USB IO, so don't probe them for interlaced
600 if (!strcmp ((char *) v4l2object->vcap.driver, "uvcusb")) {
601 v4l2object->never_interlaced = TRUE;
609 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
610 (_("Cannot identify device '%s'."), v4l2object->videodev),
616 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
617 (_("This isn't a device '%s'."), v4l2object->videodev),
623 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
624 (_("Could not open device '%s' for reading and writing."),
625 v4l2object->videodev), GST_ERROR_SYSTEM);
630 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
631 (_("Device '%s' is not a capture device."),
632 v4l2object->videodev),
633 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
638 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
639 (_("Device '%s' is not a output device."),
640 v4l2object->videodev),
641 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
646 if (GST_V4L2_IS_OPEN (v4l2object)) {
648 v4l2_close (v4l2object->video_fd);
649 v4l2object->video_fd = -1;
652 gst_v4l2_empty_lists (v4l2object);
659 gst_v4l2_dup (GstV4l2Object * v4l2object, GstV4l2Object * other)
661 GstPollFD pollfd = GST_POLL_FD_INIT;
663 GST_DEBUG_OBJECT (v4l2object->element, "Trying to dup device %s",
666 GST_V4L2_CHECK_OPEN (other);
667 GST_V4L2_CHECK_NOT_OPEN (v4l2object);
668 GST_V4L2_CHECK_NOT_ACTIVE (other);
669 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
671 v4l2object->vcap = other->vcap;
672 gst_v4l2_adjust_buf_type (v4l2object);
674 v4l2object->video_fd = v4l2_dup (other->video_fd);
675 if (!GST_V4L2_IS_OPEN (v4l2object))
678 g_free (v4l2object->videodev);
679 v4l2object->videodev = g_strdup (other->videodev);
681 GST_INFO_OBJECT (v4l2object->element,
682 "Cloned device '%s' (%s) successfully",
683 v4l2object->vcap.card, v4l2object->videodev);
685 pollfd.fd = v4l2object->video_fd;
686 gst_poll_add_fd (v4l2object->poll, &pollfd);
687 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
688 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
689 gst_poll_fd_ctl_read (v4l2object->poll, &pollfd, TRUE);
691 gst_poll_fd_ctl_write (v4l2object->poll, &pollfd, TRUE);
693 v4l2object->never_interlaced = other->never_interlaced;
694 v4l2object->can_poll_device = TRUE;
700 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
701 (_("Could not dup device '%s' for reading and writing."),
702 v4l2object->videodev), GST_ERROR_SYSTEM);
709 /******************************************************
711 * close the video device (v4l2object->video_fd)
712 * return value: TRUE on success, FALSE on error
713 ******************************************************/
715 gst_v4l2_close (GstV4l2Object * v4l2object)
717 GstPollFD pollfd = GST_POLL_FD_INIT;
718 GST_DEBUG_OBJECT (v4l2object->element, "Trying to close %s",
719 v4l2object->videodev);
721 GST_V4L2_CHECK_OPEN (v4l2object);
722 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
725 v4l2_close (v4l2object->video_fd);
726 pollfd.fd = v4l2object->video_fd;
727 gst_poll_remove_fd (v4l2object->poll, &pollfd);
728 v4l2object->video_fd = -1;
731 gst_v4l2_empty_lists (v4l2object);
737 /******************************************************
738 * gst_v4l2_get_norm()
739 * Get the norm of the current device
740 * return value: TRUE on success, FALSE on error
741 ******************************************************/
743 gst_v4l2_get_norm (GstV4l2Object * v4l2object, v4l2_std_id * norm)
745 GST_DEBUG_OBJECT (v4l2object->element, "getting norm");
747 if (!GST_V4L2_IS_OPEN (v4l2object))
750 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_STD, norm) < 0)
758 GST_DEBUG ("Failed to get the current norm for device %s",
759 v4l2object->videodev);
765 /******************************************************
766 * gst_v4l2_set_norm()
767 * Set the norm of the current device
768 * return value: TRUE on success, FALSE on error
769 ******************************************************/
771 gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm)
773 GST_DEBUG_OBJECT (v4l2object->element, "trying to set norm to "
774 "%" G_GINT64_MODIFIER "x", (guint64) norm);
776 if (!GST_V4L2_IS_OPEN (v4l2object))
779 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_STD, &norm) < 0)
787 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
788 (_("Failed to set norm for device '%s'."),
789 v4l2object->videodev), GST_ERROR_SYSTEM);
794 /******************************************************
795 * gst_v4l2_get_frequency():
796 * get the current frequency
797 * return value: TRUE on success, FALSE on error
798 ******************************************************/
800 gst_v4l2_get_frequency (GstV4l2Object * v4l2object,
801 gint tunernum, gulong * frequency)
803 struct v4l2_frequency freq = { 0, };
805 GstTunerChannel *channel;
807 GST_DEBUG_OBJECT (v4l2object->element, "getting current tuner frequency");
809 if (!GST_V4L2_IS_OPEN (v4l2object))
812 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
814 freq.tuner = tunernum;
815 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0)
818 *frequency = freq.frequency * channel->freq_multiplicator;
825 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
826 (_("Failed to get current tuner frequency for device '%s'."),
827 v4l2object->videodev), GST_ERROR_SYSTEM);
833 /******************************************************
834 * gst_v4l2_set_frequency():
836 * return value: TRUE on success, FALSE on error
837 ******************************************************/
839 gst_v4l2_set_frequency (GstV4l2Object * v4l2object,
840 gint tunernum, gulong frequency)
842 struct v4l2_frequency freq = { 0, };
844 GstTunerChannel *channel;
846 GST_DEBUG_OBJECT (v4l2object->element,
847 "setting current tuner frequency to %lu", frequency);
849 if (!GST_V4L2_IS_OPEN (v4l2object))
852 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
854 freq.tuner = tunernum;
855 /* fill in type - ignore error */
856 v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq);
857 freq.frequency = frequency / channel->freq_multiplicator;
859 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0)
867 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
868 (_("Failed to set current tuner frequency for device '%s' to %lu Hz."),
869 v4l2object->videodev, frequency), GST_ERROR_SYSTEM);
874 /******************************************************
875 * gst_v4l2_signal_strength():
876 * get the strength of the signal on the current input
877 * return value: TRUE on success, FALSE on error
878 ******************************************************/
880 gst_v4l2_signal_strength (GstV4l2Object * v4l2object,
881 gint tunernum, gulong * signal_strength)
883 struct v4l2_tuner tuner = { 0, };
885 GST_DEBUG_OBJECT (v4l2object->element, "trying to get signal strength");
887 if (!GST_V4L2_IS_OPEN (v4l2object))
890 tuner.index = tunernum;
891 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &tuner) < 0)
894 *signal_strength = tuner.signal;
901 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
902 (_("Failed to get signal strength for device '%s'."),
903 v4l2object->videodev), GST_ERROR_SYSTEM);
908 /******************************************************
909 * gst_v4l2_get_attribute():
910 * try to get the value of one specific attribute
911 * return value: TRUE on success, FALSE on error
912 ******************************************************/
914 gst_v4l2_get_attribute (GstV4l2Object * v4l2object,
915 int attribute_num, int *value)
917 struct v4l2_control control = { 0, };
919 GST_DEBUG_OBJECT (v4l2object->element, "getting value of attribute %d",
922 if (!GST_V4L2_IS_OPEN (v4l2object))
925 control.id = attribute_num;
927 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0)
930 *value = control.value;
937 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
938 (_("Failed to get value for control %d on device '%s'."),
939 attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
945 /******************************************************
946 * gst_v4l2_set_attribute():
947 * try to set the value of one specific attribute
948 * return value: TRUE on success, FALSE on error
949 ******************************************************/
951 gst_v4l2_set_attribute (GstV4l2Object * v4l2object,
952 int attribute_num, const int value)
954 struct v4l2_control control = { 0, };
956 GST_DEBUG_OBJECT (v4l2object->element, "setting value of attribute %d to %d",
957 attribute_num, value);
959 if (!GST_V4L2_IS_OPEN (v4l2object))
962 control.id = attribute_num;
963 control.value = value;
964 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0)
972 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
973 (_("Failed to set value %d for control %d on device '%s'."),
974 value, attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
980 set_contol (GQuark field_id, const GValue * value, gpointer user_data)
982 GstV4l2Object *v4l2object = user_data;
983 gpointer *d = g_datalist_id_get_data (&v4l2object->controls, field_id);
985 GST_WARNING_OBJECT (v4l2object,
986 "Control '%s' does not exist or has an unsupported type.",
987 g_quark_to_string (field_id));
990 if (!G_VALUE_HOLDS (value, G_TYPE_INT)) {
991 GST_WARNING_OBJECT (v4l2object,
992 "'int' value expected for control '%s'.", g_quark_to_string (field_id));
995 gst_v4l2_set_attribute (v4l2object, GPOINTER_TO_INT (d),
996 g_value_get_int (value));
1001 gst_v4l2_set_controls (GstV4l2Object * v4l2object, GstStructure * controls)
1003 return gst_structure_foreach (controls, set_contol, v4l2object);
1007 gst_v4l2_get_input (GstV4l2Object * v4l2object, gint * input)
1011 GST_DEBUG_OBJECT (v4l2object->element, "trying to get input");
1013 if (!GST_V4L2_IS_OPEN (v4l2object))
1016 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_INPUT, &n) < 0)
1021 GST_DEBUG_OBJECT (v4l2object->element, "input: %d", n);
1027 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1028 /* only give a warning message if driver actually claims to have tuner
1031 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1032 (_("Failed to get current input on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1038 gst_v4l2_set_input (GstV4l2Object * v4l2object, gint input)
1040 GST_DEBUG_OBJECT (v4l2object->element, "trying to set input to %d", input);
1042 if (!GST_V4L2_IS_OPEN (v4l2object))
1045 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_INPUT, &input) < 0)
1052 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1053 /* only give a warning message if driver actually claims to have tuner
1056 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1057 (_("Failed to set input %d on device %s."),
1058 input, v4l2object->videodev), GST_ERROR_SYSTEM);
1064 gst_v4l2_get_output (GstV4l2Object * v4l2object, gint * output)
1068 GST_DEBUG_OBJECT (v4l2object->element, "trying to get output");
1070 if (!GST_V4L2_IS_OPEN (v4l2object))
1073 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_OUTPUT, &n) < 0)
1078 GST_DEBUG_OBJECT (v4l2object->element, "output: %d", n);
1084 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1085 /* only give a warning message if driver actually claims to have tuner
1088 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1089 (_("Failed to get current output on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1095 gst_v4l2_set_output (GstV4l2Object * v4l2object, gint output)
1097 GST_DEBUG_OBJECT (v4l2object->element, "trying to set output to %d", output);
1099 if (!GST_V4L2_IS_OPEN (v4l2object))
1102 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_OUTPUT, &output) < 0)
1109 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1110 /* only give a warning message if driver actually claims to have tuner
1113 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1114 (_("Failed to set output %d on device %s."),
1115 output, v4l2object->videodev), GST_ERROR_SYSTEM);