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 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
55 #define GST_CAT_DEFAULT v4l2_debug
57 /******************************************************
58 * gst_v4l2_get_capabilities():
59 * get the device's capturing capabilities
60 * return value: TRUE on success, FALSE on error
61 ******************************************************/
63 gst_v4l2_get_capabilities (GstV4l2Object * v4l2object)
67 e = v4l2object->element;
69 GST_DEBUG_OBJECT (e, "getting capabilities");
71 if (!GST_V4L2_IS_OPEN (v4l2object))
74 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYCAP, &v4l2object->vcap) < 0)
77 GST_LOG_OBJECT (e, "driver: '%s'", v4l2object->vcap.driver);
78 GST_LOG_OBJECT (e, "card: '%s'", v4l2object->vcap.card);
79 GST_LOG_OBJECT (e, "bus_info: '%s'", v4l2object->vcap.bus_info);
80 GST_LOG_OBJECT (e, "version: %08x", v4l2object->vcap.version);
81 GST_LOG_OBJECT (e, "capabilites: %08x", v4l2object->vcap.capabilities);
88 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
89 (_("Error getting capabilities for device '%s': "
90 "It isn't a v4l2 driver. Check if it is a v4l1 driver."),
91 v4l2object->videodev), GST_ERROR_SYSTEM);
96 /******************************************************
97 * The video4linux command line tool v4l2-ctrl
98 * normalises the names of the controls received from
101 * "Exposure (absolute)" -> "exposure_absolute"
103 * We follow their lead here. @name is modified
105 ******************************************************/
107 gst_v4l2_normalise_control_name (gchar * name)
110 for (i = 0, j = 0; name[j]; ++j) {
111 if (g_ascii_isalnum (name[j])) {
112 if (i > 0 && !g_ascii_isalnum (name[j - 1]))
114 name[i++] = g_ascii_tolower (name[j]);
120 /******************************************************
121 * gst_v4l2_empty_lists() and gst_v4l2_fill_lists():
122 * fill/empty the lists of enumerations
123 * return value: TRUE on success, FALSE on error
124 ******************************************************/
126 gst_v4l2_fill_lists (GstV4l2Object * v4l2object)
129 struct v4l2_queryctrl control = { 0, };
133 e = v4l2object->element;
135 GST_DEBUG_OBJECT (e, "getting enumerations");
136 GST_V4L2_CHECK_OPEN (v4l2object);
138 GST_DEBUG_OBJECT (e, " channels");
139 /* and now, the channels */
141 struct v4l2_input input;
142 GstV4l2TunerChannel *v4l2channel;
143 GstTunerChannel *channel;
145 memset (&input, 0, sizeof (input));
148 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUMINPUT, &input) < 0) {
149 if (errno == EINVAL || errno == ENOTTY)
150 break; /* end of enumeration */
152 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
153 (_("Failed to query attributes of input %d in device %s"),
154 n, v4l2object->videodev),
155 ("Failed to get %d in input enumeration for %s. (%d - %s)",
156 n, v4l2object->videodev, errno, strerror (errno)));
161 GST_LOG_OBJECT (e, " index: %d", input.index);
162 GST_LOG_OBJECT (e, " name: '%s'", input.name);
163 GST_LOG_OBJECT (e, " type: %08x", input.type);
164 GST_LOG_OBJECT (e, " audioset: %08x", input.audioset);
165 GST_LOG_OBJECT (e, " std: %016" G_GINT64_MODIFIER "x",
166 (guint64) input.std);
167 GST_LOG_OBJECT (e, " status: %08x", input.status);
169 v4l2channel = g_object_new (GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
170 channel = GST_TUNER_CHANNEL (v4l2channel);
171 channel->label = g_strdup ((const gchar *) input.name);
172 channel->flags = GST_TUNER_CHANNEL_INPUT;
173 v4l2channel->index = n;
175 if (input.type == V4L2_INPUT_TYPE_TUNER) {
176 struct v4l2_tuner vtun;
178 v4l2channel->tuner = input.tuner;
179 channel->flags |= GST_TUNER_CHANNEL_FREQUENCY;
181 vtun.index = input.tuner;
182 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &vtun) < 0) {
183 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
184 (_("Failed to get setting of tuner %d on device '%s'."),
185 input.tuner, v4l2object->videodev), GST_ERROR_SYSTEM);
186 g_object_unref (G_OBJECT (channel));
190 channel->freq_multiplicator =
191 62.5 * ((vtun.capability & V4L2_TUNER_CAP_LOW) ? 1 : 1000);
192 channel->min_frequency = vtun.rangelow * channel->freq_multiplicator;
193 channel->max_frequency = vtun.rangehigh * channel->freq_multiplicator;
194 channel->min_signal = 0;
195 channel->max_signal = 0xffff;
197 if (input.audioset) {
198 /* we take the first. We don't care for
199 * the others for now */
200 while (!(input.audioset & (1 << v4l2channel->audio)))
201 v4l2channel->audio++;
202 channel->flags |= GST_TUNER_CHANNEL_AUDIO;
205 v4l2object->channels =
206 g_list_prepend (v4l2object->channels, (gpointer) channel);
208 v4l2object->channels = g_list_reverse (v4l2object->channels);
210 GST_DEBUG_OBJECT (e, " norms");
213 struct v4l2_standard standard = { 0, };
214 GstV4l2TunerNorm *v4l2norm;
218 /* fill in defaults */
219 standard.frameperiod.numerator = 1;
220 standard.frameperiod.denominator = 0;
223 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUMSTD, &standard) < 0) {
224 if (errno == EINVAL || errno == ENOTTY)
225 break; /* end of enumeration */
227 else if (errno == ENODATA)
228 break; /* end of enumeration, as of Linux 3.7-rc1 */
231 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
232 (_("Failed to query norm on device '%s'."),
233 v4l2object->videodev),
234 ("Failed to get attributes for norm %d on devide '%s'. (%d - %s)",
235 n, v4l2object->videodev, errno, strerror (errno)));
240 GST_DEBUG_OBJECT (e, " '%s', fps: %d / %d",
241 standard.name, standard.frameperiod.denominator,
242 standard.frameperiod.numerator);
244 v4l2norm = g_object_new (GST_TYPE_V4L2_TUNER_NORM, NULL);
245 norm = GST_TUNER_NORM (v4l2norm);
246 norm->label = g_strdup ((const gchar *) standard.name);
247 gst_value_set_fraction (&norm->framerate,
248 standard.frameperiod.denominator, standard.frameperiod.numerator);
249 v4l2norm->index = standard.id;
251 GST_DEBUG_OBJECT (v4l2object->element, "index=%08x, label=%s",
252 (unsigned int) v4l2norm->index, norm->label);
254 v4l2object->norms = g_list_prepend (v4l2object->norms, (gpointer) norm);
256 v4l2object->norms = g_list_reverse (v4l2object->norms);
258 GST_DEBUG_OBJECT (e, " controls+menus");
260 /* and lastly, controls+menus (if appropriate) */
261 next = V4L2_CTRL_FLAG_NEXT_CTRL;
266 GstV4l2ColorBalanceChannel *v4l2channel;
267 GstColorBalanceChannel *channel;
273 /* when we reached the last official CID, continue with private CIDs */
274 if (n == V4L2_CID_LASTP1) {
275 GST_DEBUG_OBJECT (e, "checking private CIDs");
276 n = V4L2_CID_PRIVATE_BASE;
278 GST_DEBUG_OBJECT (e, "checking control %08x", n);
280 control.id = n | next;
281 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
284 GST_DEBUG_OBJECT (e, "controls finished");
287 GST_DEBUG_OBJECT (e, "V4L2_CTRL_FLAG_NEXT_CTRL not supported.");
293 if (errno == EINVAL || errno == ENOTTY || errno == EIO || errno == ENOENT) {
294 if (n < V4L2_CID_PRIVATE_BASE) {
295 GST_DEBUG_OBJECT (e, "skipping control %08x", n);
296 /* continue so that we also check private controls */
299 GST_DEBUG_OBJECT (e, "controls finished");
303 GST_WARNING_OBJECT (e, "Failed querying control %d on device '%s'. "
304 "(%d - %s)", n, v4l2object->videodev, errno, strerror (errno));
308 /* bogus driver might mess with id in unexpected ways (e.g. set to 0), so
309 * make sure to simply try all if V4L2_CTRL_FLAG_NEXT_CTRL not supported */
312 if (control.flags & V4L2_CTRL_FLAG_DISABLED) {
313 GST_DEBUG_OBJECT (e, "skipping disabled control");
317 if (control.type == V4L2_CTRL_TYPE_CTRL_CLASS) {
318 GST_DEBUG_OBJECT (e, "starting control class '%s'", control.name);
322 switch (control.type) {
323 case V4L2_CTRL_TYPE_INTEGER:
324 case V4L2_CTRL_TYPE_BOOLEAN:
325 case V4L2_CTRL_TYPE_MENU:
326 case V4L2_CTRL_TYPE_INTEGER_MENU:
327 case V4L2_CTRL_TYPE_BITMASK:
328 case V4L2_CTRL_TYPE_BUTTON:{
329 control.name[31] = '\0';
330 gst_v4l2_normalise_control_name ((gchar *) control.name);
331 g_datalist_id_set_data (&v4l2object->controls,
332 g_quark_from_string ((const gchar *) control.name),
333 GINT_TO_POINTER (n));
338 "Control type for '%s' not suppored for extra controls.",
344 case V4L2_CID_BRIGHTNESS:
345 case V4L2_CID_CONTRAST:
346 case V4L2_CID_SATURATION:
348 case V4L2_CID_BLACK_LEVEL:
349 case V4L2_CID_AUTO_WHITE_BALANCE:
350 case V4L2_CID_DO_WHITE_BALANCE:
351 case V4L2_CID_RED_BALANCE:
352 case V4L2_CID_BLUE_BALANCE:
354 case V4L2_CID_EXPOSURE:
355 case V4L2_CID_AUTOGAIN:
357 case V4L2_CID_SHARPNESS:
358 /* we only handle these for now (why?) */
362 case V4L2_CID_PAN_RESET:
363 case V4L2_CID_TILT_RESET:
364 /* not handled here, handled by VideoOrientation interface */
367 case V4L2_CID_AUDIO_VOLUME:
368 case V4L2_CID_AUDIO_BALANCE:
369 case V4L2_CID_AUDIO_BASS:
370 case V4L2_CID_AUDIO_TREBLE:
371 case V4L2_CID_AUDIO_MUTE:
372 case V4L2_CID_AUDIO_LOUDNESS:
373 /* FIXME: We should implement GstMixer interface instead */
374 /* but let's not be pedantic and make element more useful for now */
378 "ControlID %s (%x) unhandled, FIXME", control.name, n);
385 GST_DEBUG_OBJECT (e, "Adding ControlID %s (%x)", control.name, n);
386 v4l2channel = g_object_new (GST_TYPE_V4L2_COLOR_BALANCE_CHANNEL, NULL);
387 channel = GST_COLOR_BALANCE_CHANNEL (v4l2channel);
388 channel->label = g_strdup ((const gchar *) control.name);
392 /* FIXME: it will be need just when handling private controls
393 *(currently none of base controls are of this type) */
394 if (control.type == V4L2_CTRL_TYPE_MENU) {
395 struct v4l2_querymenu menu, *mptr;
402 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYMENU, &menu) < 0) {
404 break; /* end of enumeration */
406 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
407 (_("Failed getting controls attributes on device '%s'."),
408 v4l2object->videodev),
409 ("Failed to get %d in menu enumeration for %s. (%d - %s)",
410 n, v4l2object->videodev, errno, strerror (errno)));
414 mptr = g_malloc (sizeof (menu));
415 memcpy (mptr, &menu, sizeof (menu));
416 menus = g_list_append (menus, mptr);
419 v4l2object->menus = g_list_append (v4l2object->menus, menus);
422 switch (control.type) {
423 case V4L2_CTRL_TYPE_INTEGER:
424 channel->min_value = control.minimum;
425 channel->max_value = control.maximum;
427 case V4L2_CTRL_TYPE_BOOLEAN:
428 channel->min_value = FALSE;
429 channel->max_value = TRUE;
432 /* FIXME we should find out how to handle V4L2_CTRL_TYPE_BUTTON.
433 BUTTON controls like V4L2_CID_DO_WHITE_BALANCE can just be set (1) or
434 unset (0), but can't be queried */
436 "Control with non supported type %s (%x), type=%d",
437 control.name, n, control.type);
438 channel->min_value = channel->max_value = 0;
443 g_list_prepend (v4l2object->colors, (gpointer) channel);
445 v4l2object->colors = g_list_reverse (v4l2object->colors);
447 GST_DEBUG_OBJECT (e, "done");
453 gst_v4l2_empty_lists (GstV4l2Object * v4l2object)
455 GST_DEBUG_OBJECT (v4l2object->element, "deleting enumerations");
457 g_list_foreach (v4l2object->channels, (GFunc) g_object_unref, NULL);
458 g_list_free (v4l2object->channels);
459 v4l2object->channels = NULL;
461 g_list_foreach (v4l2object->norms, (GFunc) g_object_unref, NULL);
462 g_list_free (v4l2object->norms);
463 v4l2object->norms = NULL;
465 g_list_foreach (v4l2object->colors, (GFunc) g_object_unref, NULL);
466 g_list_free (v4l2object->colors);
467 v4l2object->colors = NULL;
469 g_datalist_clear (&v4l2object->controls);
473 gst_v4l2_adjust_buf_type (GstV4l2Object * v4l2object)
475 /* when calling gst_v4l2_object_new the user decides the initial type
476 * so adjust it if multi-planar is supported
477 * the driver should make it exclusive. So the driver should
478 * not support both MPLANE and non-PLANE.
479 * Because even when using MPLANE it still possibles to use it
480 * in a contiguous manner. In this case the first v4l2 plane
481 * contains all the gst planes.
483 #define CHECK_CAPS (V4L2_CAP_VIDEO_OUTPUT_MPLANE | V4L2_CAP_VIDEO_M2M_MPLANE)
484 switch (v4l2object->type) {
485 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
486 if (v4l2object->vcap.capabilities & CHECK_CAPS) {
487 GST_DEBUG ("adjust type to multi-planar output");
488 v4l2object->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
491 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
492 if (v4l2object->vcap.capabilities & CHECK_CAPS) {
493 GST_DEBUG ("adjust type to multi-planar capture");
494 v4l2object->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
503 /******************************************************
505 * open the video device (v4l2object->videodev)
506 * return value: TRUE on success, FALSE on error
507 ******************************************************/
509 gst_v4l2_open (GstV4l2Object * v4l2object)
513 GstPollFD pollfd = GST_POLL_FD_INIT;
515 GST_DEBUG_OBJECT (v4l2object->element, "Trying to open device %s",
516 v4l2object->videodev);
518 GST_V4L2_CHECK_NOT_OPEN (v4l2object);
519 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
521 /* be sure we have a device */
522 if (!v4l2object->videodev)
523 v4l2object->videodev = g_strdup ("/dev/video");
525 /* check if it is a device */
526 if (stat (v4l2object->videodev, &st) == -1)
529 if (!S_ISCHR (st.st_mode))
532 /* open the device */
533 v4l2object->video_fd =
534 open (v4l2object->videodev, O_RDWR /* | O_NONBLOCK */ );
536 if (!GST_V4L2_IS_OPEN (v4l2object))
539 libv4l2_fd = v4l2_fd_open (v4l2object->video_fd,
540 V4L2_ENABLE_ENUM_FMT_EMULATION);
541 /* Note the v4l2_xxx functions are designed so that if they get passed an
542 unknown fd, the will behave exactly as their regular xxx counterparts, so
543 if v4l2_fd_open fails, we continue as normal (missing the libv4l2 custom
544 cam format to normal formats conversion). Chances are big we will still
545 fail then though, as normally v4l2_fd_open only fails if the device is not
547 if (libv4l2_fd != -1)
548 v4l2object->video_fd = libv4l2_fd;
550 v4l2object->can_poll_device = TRUE;
552 /* get capabilities, error will be posted */
553 if (!gst_v4l2_get_capabilities (v4l2object))
556 /* do we need to be a capture device? */
557 if (GST_IS_V4L2SRC (v4l2object->element) &&
558 !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_CAPTURE |
559 V4L2_CAP_VIDEO_CAPTURE_MPLANE)))
562 if (GST_IS_V4L2SINK (v4l2object->element) &&
563 !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_OUTPUT |
564 V4L2_CAP_VIDEO_OUTPUT_MPLANE)))
567 if (GST_IS_V4L2_VIDEO_DEC (v4l2object->element) &&
568 /* Today's M2M device only expose M2M */
569 !((v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_M2M |
570 V4L2_CAP_VIDEO_M2M_MPLANE)) ||
571 /* But legacy driver may expose both CAPTURE and OUTPUT */
572 ((v4l2object->vcap.capabilities &
573 (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) &&
574 (v4l2object->vcap.capabilities &
575 (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE)))))
578 gst_v4l2_adjust_buf_type (v4l2object);
580 /* create enumerations, posts errors. */
581 if (!gst_v4l2_fill_lists (v4l2object))
584 GST_INFO_OBJECT (v4l2object->element,
585 "Opened device '%s' (%s) successfully",
586 v4l2object->vcap.card, v4l2object->videodev);
588 pollfd.fd = v4l2object->video_fd;
589 gst_poll_add_fd (v4l2object->poll, &pollfd);
590 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
591 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
592 gst_poll_fd_ctl_read (v4l2object->poll, &pollfd, TRUE);
594 gst_poll_fd_ctl_write (v4l2object->poll, &pollfd, TRUE);
596 if (v4l2object->extra_controls)
597 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
599 /* UVC devices are never interlaced, and doing VIDIOC_TRY_FMT on them
600 * causes expensive and slow USB IO, so don't probe them for interlaced
602 if (!strcmp ((char *) v4l2object->vcap.driver, "uvcusb")) {
603 v4l2object->never_interlaced = TRUE;
611 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
612 (_("Cannot identify device '%s'."), v4l2object->videodev),
618 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
619 (_("This isn't a device '%s'."), v4l2object->videodev),
625 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
626 (_("Could not open device '%s' for reading and writing."),
627 v4l2object->videodev), GST_ERROR_SYSTEM);
632 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
633 (_("Device '%s' is not a capture device."),
634 v4l2object->videodev),
635 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
640 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
641 (_("Device '%s' is not a output device."),
642 v4l2object->videodev),
643 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
648 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
649 (_("Device '%s' is not a M2M device."),
650 v4l2object->videodev),
651 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
656 if (GST_V4L2_IS_OPEN (v4l2object)) {
658 v4l2_close (v4l2object->video_fd);
659 v4l2object->video_fd = -1;
662 gst_v4l2_empty_lists (v4l2object);
669 gst_v4l2_dup (GstV4l2Object * v4l2object, GstV4l2Object * other)
671 GstPollFD pollfd = GST_POLL_FD_INIT;
673 GST_DEBUG_OBJECT (v4l2object->element, "Trying to dup device %s",
676 GST_V4L2_CHECK_OPEN (other);
677 GST_V4L2_CHECK_NOT_OPEN (v4l2object);
678 GST_V4L2_CHECK_NOT_ACTIVE (other);
679 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
681 v4l2object->vcap = other->vcap;
682 gst_v4l2_adjust_buf_type (v4l2object);
684 v4l2object->video_fd = v4l2_dup (other->video_fd);
685 if (!GST_V4L2_IS_OPEN (v4l2object))
688 g_free (v4l2object->videodev);
689 v4l2object->videodev = g_strdup (other->videodev);
691 GST_INFO_OBJECT (v4l2object->element,
692 "Cloned device '%s' (%s) successfully",
693 v4l2object->vcap.card, v4l2object->videodev);
695 pollfd.fd = v4l2object->video_fd;
696 gst_poll_add_fd (v4l2object->poll, &pollfd);
697 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
698 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
699 gst_poll_fd_ctl_read (v4l2object->poll, &pollfd, TRUE);
701 gst_poll_fd_ctl_write (v4l2object->poll, &pollfd, TRUE);
703 v4l2object->never_interlaced = other->never_interlaced;
704 v4l2object->can_poll_device = TRUE;
710 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
711 (_("Could not dup device '%s' for reading and writing."),
712 v4l2object->videodev), GST_ERROR_SYSTEM);
719 /******************************************************
721 * close the video device (v4l2object->video_fd)
722 * return value: TRUE on success, FALSE on error
723 ******************************************************/
725 gst_v4l2_close (GstV4l2Object * v4l2object)
727 GstPollFD pollfd = GST_POLL_FD_INIT;
728 GST_DEBUG_OBJECT (v4l2object->element, "Trying to close %s",
729 v4l2object->videodev);
731 GST_V4L2_CHECK_OPEN (v4l2object);
732 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
735 v4l2_close (v4l2object->video_fd);
736 pollfd.fd = v4l2object->video_fd;
737 gst_poll_remove_fd (v4l2object->poll, &pollfd);
738 v4l2object->video_fd = -1;
741 gst_v4l2_empty_lists (v4l2object);
747 /******************************************************
748 * gst_v4l2_get_norm()
749 * Get the norm of the current device
750 * return value: TRUE on success, FALSE on error
751 ******************************************************/
753 gst_v4l2_get_norm (GstV4l2Object * v4l2object, v4l2_std_id * norm)
755 GST_DEBUG_OBJECT (v4l2object->element, "getting norm");
757 if (!GST_V4L2_IS_OPEN (v4l2object))
760 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_STD, norm) < 0)
768 GST_DEBUG ("Failed to get the current norm for device %s",
769 v4l2object->videodev);
775 /******************************************************
776 * gst_v4l2_set_norm()
777 * Set the norm of the current device
778 * return value: TRUE on success, FALSE on error
779 ******************************************************/
781 gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm)
783 GST_DEBUG_OBJECT (v4l2object->element, "trying to set norm to "
784 "%" G_GINT64_MODIFIER "x", (guint64) norm);
786 if (!GST_V4L2_IS_OPEN (v4l2object))
789 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_STD, &norm) < 0)
797 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
798 (_("Failed to set norm for device '%s'."),
799 v4l2object->videodev), GST_ERROR_SYSTEM);
804 /******************************************************
805 * gst_v4l2_get_frequency():
806 * get the current frequency
807 * return value: TRUE on success, FALSE on error
808 ******************************************************/
810 gst_v4l2_get_frequency (GstV4l2Object * v4l2object,
811 gint tunernum, gulong * frequency)
813 struct v4l2_frequency freq = { 0, };
815 GstTunerChannel *channel;
817 GST_DEBUG_OBJECT (v4l2object->element, "getting current tuner frequency");
819 if (!GST_V4L2_IS_OPEN (v4l2object))
822 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
824 freq.tuner = tunernum;
825 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0)
828 *frequency = freq.frequency * channel->freq_multiplicator;
835 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
836 (_("Failed to get current tuner frequency for device '%s'."),
837 v4l2object->videodev), GST_ERROR_SYSTEM);
843 /******************************************************
844 * gst_v4l2_set_frequency():
846 * return value: TRUE on success, FALSE on error
847 ******************************************************/
849 gst_v4l2_set_frequency (GstV4l2Object * v4l2object,
850 gint tunernum, gulong frequency)
852 struct v4l2_frequency freq = { 0, };
854 GstTunerChannel *channel;
856 GST_DEBUG_OBJECT (v4l2object->element,
857 "setting current tuner frequency to %lu", frequency);
859 if (!GST_V4L2_IS_OPEN (v4l2object))
862 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
864 freq.tuner = tunernum;
865 /* fill in type - ignore error */
866 v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq);
867 freq.frequency = frequency / channel->freq_multiplicator;
869 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0)
877 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
878 (_("Failed to set current tuner frequency for device '%s' to %lu Hz."),
879 v4l2object->videodev, frequency), GST_ERROR_SYSTEM);
884 /******************************************************
885 * gst_v4l2_signal_strength():
886 * get the strength of the signal on the current input
887 * return value: TRUE on success, FALSE on error
888 ******************************************************/
890 gst_v4l2_signal_strength (GstV4l2Object * v4l2object,
891 gint tunernum, gulong * signal_strength)
893 struct v4l2_tuner tuner = { 0, };
895 GST_DEBUG_OBJECT (v4l2object->element, "trying to get signal strength");
897 if (!GST_V4L2_IS_OPEN (v4l2object))
900 tuner.index = tunernum;
901 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &tuner) < 0)
904 *signal_strength = tuner.signal;
911 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
912 (_("Failed to get signal strength for device '%s'."),
913 v4l2object->videodev), GST_ERROR_SYSTEM);
918 /******************************************************
919 * gst_v4l2_get_attribute():
920 * try to get the value of one specific attribute
921 * return value: TRUE on success, FALSE on error
922 ******************************************************/
924 gst_v4l2_get_attribute (GstV4l2Object * v4l2object,
925 int attribute_num, int *value)
927 struct v4l2_control control = { 0, };
929 GST_DEBUG_OBJECT (v4l2object->element, "getting value of attribute %d",
932 if (!GST_V4L2_IS_OPEN (v4l2object))
935 control.id = attribute_num;
937 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0)
940 *value = control.value;
947 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
948 (_("Failed to get value for control %d on device '%s'."),
949 attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
955 /******************************************************
956 * gst_v4l2_set_attribute():
957 * try to set the value of one specific attribute
958 * return value: TRUE on success, FALSE on error
959 ******************************************************/
961 gst_v4l2_set_attribute (GstV4l2Object * v4l2object,
962 int attribute_num, const int value)
964 struct v4l2_control control = { 0, };
966 GST_DEBUG_OBJECT (v4l2object->element, "setting value of attribute %d to %d",
967 attribute_num, value);
969 if (!GST_V4L2_IS_OPEN (v4l2object))
972 control.id = attribute_num;
973 control.value = value;
974 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0)
982 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
983 (_("Failed to set value %d for control %d on device '%s'."),
984 value, attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
990 set_control (GQuark field_id, const GValue * value, gpointer user_data)
992 GstV4l2Object *v4l2object = user_data;
993 GQuark normalised_field_id;
996 /* 32 bytes is the maximum size for a control name according to v4l2 */
999 /* Backwards compatibility: in the past GStreamer would normalise strings in
1000 a subtly different way to v4l2-ctl. e.g. the kernel's "Focus (absolute)"
1001 would become "focus__absolute_" whereas now it becomes "focus_absolute".
1002 Please remove the following in GStreamer 1.5 for 1.6 */
1003 strncpy (name, g_quark_to_string (field_id), sizeof (name));
1005 gst_v4l2_normalise_control_name (name);
1006 normalised_field_id = g_quark_from_string (name);
1007 if (normalised_field_id != field_id)
1008 g_warning ("In GStreamer 1.4 the way V4L2 control names were normalised "
1009 "changed. Instead of setting \"%s\" please use \"%s\". The former is "
1010 "deprecated and will be removed in a future version of GStreamer",
1011 g_quark_to_string (field_id), name);
1012 field_id = normalised_field_id;
1014 d = g_datalist_id_get_data (&v4l2object->controls, field_id);
1016 GST_WARNING_OBJECT (v4l2object,
1017 "Control '%s' does not exist or has an unsupported type.",
1018 g_quark_to_string (field_id));
1021 if (!G_VALUE_HOLDS (value, G_TYPE_INT)) {
1022 GST_WARNING_OBJECT (v4l2object,
1023 "'int' value expected for control '%s'.", g_quark_to_string (field_id));
1026 gst_v4l2_set_attribute (v4l2object, GPOINTER_TO_INT (d),
1027 g_value_get_int (value));
1032 gst_v4l2_set_controls (GstV4l2Object * v4l2object, GstStructure * controls)
1034 return gst_structure_foreach (controls, set_control, v4l2object);
1038 gst_v4l2_get_input (GstV4l2Object * v4l2object, gint * input)
1042 GST_DEBUG_OBJECT (v4l2object->element, "trying to get input");
1044 if (!GST_V4L2_IS_OPEN (v4l2object))
1047 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_INPUT, &n) < 0)
1052 GST_DEBUG_OBJECT (v4l2object->element, "input: %d", n);
1058 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1059 /* only give a warning message if driver actually claims to have tuner
1062 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1063 (_("Failed to get current input on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1069 gst_v4l2_set_input (GstV4l2Object * v4l2object, gint input)
1071 GST_DEBUG_OBJECT (v4l2object->element, "trying to set input to %d", input);
1073 if (!GST_V4L2_IS_OPEN (v4l2object))
1076 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_INPUT, &input) < 0)
1083 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1084 /* only give a warning message if driver actually claims to have tuner
1087 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1088 (_("Failed to set input %d on device %s."),
1089 input, v4l2object->videodev), GST_ERROR_SYSTEM);
1095 gst_v4l2_get_output (GstV4l2Object * v4l2object, gint * output)
1099 GST_DEBUG_OBJECT (v4l2object->element, "trying to get output");
1101 if (!GST_V4L2_IS_OPEN (v4l2object))
1104 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_OUTPUT, &n) < 0)
1109 GST_DEBUG_OBJECT (v4l2object->element, "output: %d", n);
1115 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1116 /* only give a warning message if driver actually claims to have tuner
1119 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1120 (_("Failed to get current output on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1126 gst_v4l2_set_output (GstV4l2Object * v4l2object, gint output)
1128 GST_DEBUG_OBJECT (v4l2object->element, "trying to set output to %d", output);
1130 if (!GST_V4L2_IS_OPEN (v4l2object))
1133 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_OUTPUT, &output) < 0)
1140 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1141 /* only give a warning message if driver actually claims to have tuner
1144 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1145 (_("Failed to set output %d on device %s."),
1146 output, v4l2object->videodev), GST_ERROR_SYSTEM);