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)
209 break; /* end of enumeration */
211 else if (errno == ENODATA)
212 break; /* end of enumeration, as of Linux 3.7-rc1 */
215 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
216 (_("Failed to query norm on device '%s'."),
217 v4l2object->videodev),
218 ("Failed to get attributes for norm %d on devide '%s'. (%d - %s)",
219 n, v4l2object->videodev, errno, strerror (errno)));
224 GST_DEBUG_OBJECT (e, " '%s', fps: %d / %d",
225 standard.name, standard.frameperiod.denominator,
226 standard.frameperiod.numerator);
228 v4l2norm = g_object_new (GST_TYPE_V4L2_TUNER_NORM, NULL);
229 norm = GST_TUNER_NORM (v4l2norm);
230 norm->label = g_strdup ((const gchar *) standard.name);
231 gst_value_set_fraction (&norm->framerate,
232 standard.frameperiod.denominator, standard.frameperiod.numerator);
233 v4l2norm->index = standard.id;
235 GST_DEBUG_OBJECT (v4l2object->element, "index=%08x, label=%s",
236 (unsigned int) v4l2norm->index, norm->label);
238 v4l2object->norms = g_list_prepend (v4l2object->norms, (gpointer) norm);
240 v4l2object->norms = g_list_reverse (v4l2object->norms);
242 GST_DEBUG_OBJECT (e, " controls+menus");
244 /* and lastly, controls+menus (if appropriate) */
245 #ifdef V4L2_CTRL_FLAG_NEXT_CTRL
246 next = V4L2_CTRL_FLAG_NEXT_CTRL;
254 GstV4l2ColorBalanceChannel *v4l2channel;
255 GstColorBalanceChannel *channel;
260 /* when we reached the last official CID, continue with private CIDs */
261 if (n == V4L2_CID_LASTP1) {
262 GST_DEBUG_OBJECT (e, "checking private CIDs");
263 n = V4L2_CID_PRIVATE_BASE;
265 GST_DEBUG_OBJECT (e, "checking control %08x", n);
267 control.id = n | next;
268 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
271 GST_DEBUG_OBJECT (e, "controls finished");
274 GST_DEBUG_OBJECT (e, "V4L2_CTRL_FLAG_NEXT_CTRL not supported.");
280 if (errno == EINVAL || errno == ENOTTY || errno == EIO || errno == ENOENT) {
281 if (n < V4L2_CID_PRIVATE_BASE) {
282 GST_DEBUG_OBJECT (e, "skipping control %08x", n);
283 /* continue so that we also check private controls */
286 GST_DEBUG_OBJECT (e, "controls finished");
290 GST_WARNING_OBJECT (e, "Failed querying control %d on device '%s'. "
291 "(%d - %s)", n, v4l2object->videodev, errno, strerror (errno));
296 if (control.flags & V4L2_CTRL_FLAG_DISABLED) {
297 GST_DEBUG_OBJECT (e, "skipping disabled control");
300 #ifdef V4L2_CTRL_TYPE_CTRL_CLASS
301 if (control.type == V4L2_CTRL_TYPE_CTRL_CLASS) {
302 GST_DEBUG_OBJECT (e, "starting control class '%s'", control.name);
306 switch (control.type) {
307 case V4L2_CTRL_TYPE_INTEGER:
308 case V4L2_CTRL_TYPE_BOOLEAN:
309 case V4L2_CTRL_TYPE_MENU:
310 #ifdef V4L2_CTRL_TYPE_INTEGER_MENU
311 case V4L2_CTRL_TYPE_INTEGER_MENU:
313 #ifdef V4L2_CTRL_TYPE_BITMASK
314 case V4L2_CTRL_TYPE_BITMASK:
316 case V4L2_CTRL_TYPE_BUTTON:{
318 control.name[31] = '\0';
319 for (i = 0; control.name[i]; ++i) {
320 control.name[i] = g_ascii_tolower (control.name[i]);
321 if (!g_ascii_isalnum (control.name[i]))
322 control.name[i] = '_';
324 GST_INFO_OBJECT (e, "adding generic controls '%s'", control.name);
325 g_datalist_id_set_data (&v4l2object->controls,
326 g_quark_from_string ((const gchar *) control.name),
327 GINT_TO_POINTER (n));
332 "Control type for '%s' not suppored for extra controls.",
338 case V4L2_CID_BRIGHTNESS:
339 case V4L2_CID_CONTRAST:
340 case V4L2_CID_SATURATION:
342 case V4L2_CID_BLACK_LEVEL:
343 case V4L2_CID_AUTO_WHITE_BALANCE:
344 case V4L2_CID_DO_WHITE_BALANCE:
345 case V4L2_CID_RED_BALANCE:
346 case V4L2_CID_BLUE_BALANCE:
348 case V4L2_CID_EXPOSURE:
349 case V4L2_CID_AUTOGAIN:
351 #ifdef V4L2_CID_SHARPNESS
352 case V4L2_CID_SHARPNESS:
354 /* we only handle these for now (why?) */
358 #ifndef V4L2_CID_PAN_RESET
359 case V4L2_CID_HCENTER:
361 #ifndef V4L2_CID_TILT_RESET
362 case V4L2_CID_VCENTER:
364 #ifdef V4L2_CID_PAN_RESET
365 case V4L2_CID_PAN_RESET:
367 #ifdef V4L2_CID_TILT_RESET
368 case V4L2_CID_TILT_RESET:
370 /* not handled here, handled by VideoOrientation interface */
373 case V4L2_CID_AUDIO_VOLUME:
374 case V4L2_CID_AUDIO_BALANCE:
375 case V4L2_CID_AUDIO_BASS:
376 case V4L2_CID_AUDIO_TREBLE:
377 case V4L2_CID_AUDIO_MUTE:
378 case V4L2_CID_AUDIO_LOUDNESS:
379 /* FIXME: We should implement GstMixer interface */
383 "ControlID %s (%x) unhandled, FIXME", control.name, n);
390 GST_DEBUG_OBJECT (e, "Adding ControlID %s (%x)", control.name, n);
391 v4l2channel = g_object_new (GST_TYPE_V4L2_COLOR_BALANCE_CHANNEL, NULL);
392 channel = GST_COLOR_BALANCE_CHANNEL (v4l2channel);
393 channel->label = g_strdup ((const gchar *) control.name);
397 /* FIXME: it will be need just when handling private controls
398 *(currently none of base controls are of this type) */
399 if (control.type == V4L2_CTRL_TYPE_MENU) {
400 struct v4l2_querymenu menu, *mptr;
407 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYMENU, &menu) < 0) {
409 break; /* end of enumeration */
411 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
412 (_("Failed getting controls attributes on device '%s'."),
413 v4l2object->videodev),
414 ("Failed to get %d in menu enumeration for %s. (%d - %s)",
415 n, v4l2object->videodev, errno, strerror (errno)));
419 mptr = g_malloc (sizeof (menu));
420 memcpy (mptr, &menu, sizeof (menu));
421 menus = g_list_append (menus, mptr);
424 v4l2object->menus = g_list_append (v4l2object->menus, menus);
427 switch (control.type) {
428 case V4L2_CTRL_TYPE_INTEGER:
429 channel->min_value = control.minimum;
430 channel->max_value = control.maximum;
432 case V4L2_CTRL_TYPE_BOOLEAN:
433 channel->min_value = FALSE;
434 channel->max_value = TRUE;
437 /* FIXME we should find out how to handle V4L2_CTRL_TYPE_BUTTON.
438 BUTTON controls like V4L2_CID_DO_WHITE_BALANCE can just be set (1) or
439 unset (0), but can't be queried */
441 "Control with non supported type %s (%x), type=%d",
442 control.name, n, control.type);
443 channel->min_value = channel->max_value = 0;
448 g_list_prepend (v4l2object->colors, (gpointer) channel);
450 v4l2object->colors = g_list_reverse (v4l2object->colors);
452 GST_DEBUG_OBJECT (e, "done");
458 gst_v4l2_empty_lists (GstV4l2Object * v4l2object)
460 GST_DEBUG_OBJECT (v4l2object->element, "deleting enumerations");
462 g_list_foreach (v4l2object->channels, (GFunc) g_object_unref, NULL);
463 g_list_free (v4l2object->channels);
464 v4l2object->channels = NULL;
466 g_list_foreach (v4l2object->norms, (GFunc) g_object_unref, NULL);
467 g_list_free (v4l2object->norms);
468 v4l2object->norms = NULL;
470 g_list_foreach (v4l2object->colors, (GFunc) g_object_unref, NULL);
471 g_list_free (v4l2object->colors);
472 v4l2object->colors = NULL;
474 g_datalist_clear (&v4l2object->controls);
478 gst_v4l2_adjust_buf_type (GstV4l2Object * v4l2object)
480 /* when calling gst_v4l2_object_new the user decides the initial type
481 * so adjust it if multi-planar is supported
482 * the driver should make it exclusive. So the driver should
483 * not support both MPLANE and non-PLANE.
484 * Because even when using MPLANE it still possibles to use it
485 * in a contiguous manner. In this case the first v4l2 plane
486 * contains all the gst planes.
488 #ifdef V4L2_CAP_VIDEO_M2M_MPLANE
489 #define CHECK_CAPS (V4L2_CAP_VIDEO_OUTPUT_MPLANE | V4L2_CAP_VIDEO_M2M_MPLANE)
491 #define CHECK_CAPS (V4L2_CAP_VIDEO_OUTPUT_MPLANE)
494 switch (v4l2object->type) {
495 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
496 if (v4l2object->vcap.capabilities & CHECK_CAPS) {
497 GST_DEBUG ("adjust type to multi-planar output");
498 v4l2object->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
501 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
502 if (v4l2object->vcap.capabilities & CHECK_CAPS) {
503 /* FIXME: for now it's an untested case so just put a warning */
504 GST_WARNING ("untested V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE");
506 GST_DEBUG ("adjust type to multi-planar capture");
507 v4l2object->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
515 /******************************************************
517 * open the video device (v4l2object->videodev)
518 * return value: TRUE on success, FALSE on error
519 ******************************************************/
521 gst_v4l2_open (GstV4l2Object * v4l2object)
525 GstPollFD pollfd = GST_POLL_FD_INIT;
527 GST_DEBUG_OBJECT (v4l2object->element, "Trying to open device %s",
528 v4l2object->videodev);
530 GST_V4L2_CHECK_NOT_OPEN (v4l2object);
531 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
533 /* be sure we have a device */
534 if (!v4l2object->videodev)
535 v4l2object->videodev = g_strdup ("/dev/video");
537 /* check if it is a device */
538 if (stat (v4l2object->videodev, &st) == -1)
541 if (!S_ISCHR (st.st_mode))
544 /* open the device */
545 v4l2object->video_fd =
546 open (v4l2object->videodev, O_RDWR /* | O_NONBLOCK */ );
548 if (!GST_V4L2_IS_OPEN (v4l2object))
551 libv4l2_fd = v4l2_fd_open (v4l2object->video_fd,
552 V4L2_ENABLE_ENUM_FMT_EMULATION);
553 /* Note the v4l2_xxx functions are designed so that if they get passed an
554 unknown fd, the will behave exactly as their regular xxx counterparts, so
555 if v4l2_fd_open fails, we continue as normal (missing the libv4l2 custom
556 cam format to normal formats conversion). Chances are big we will still
557 fail then though, as normally v4l2_fd_open only fails if the device is not
559 if (libv4l2_fd != -1)
560 v4l2object->video_fd = libv4l2_fd;
562 v4l2object->can_poll_device = TRUE;
564 /* get capabilities, error will be posted */
565 if (!gst_v4l2_get_capabilities (v4l2object))
568 /* do we need to be a capture device? */
569 if (GST_IS_V4L2SRC (v4l2object->element) &&
570 !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_CAPTURE |
571 V4L2_CAP_VIDEO_CAPTURE_MPLANE)))
574 if (GST_IS_V4L2SINK (v4l2object->element) &&
575 !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_OUTPUT |
576 V4L2_CAP_VIDEO_OUTPUT_MPLANE)))
580 gst_v4l2_adjust_buf_type (v4l2object);
582 /* create enumerations, posts errors. */
583 if (!gst_v4l2_fill_lists (v4l2object))
586 GST_INFO_OBJECT (v4l2object->element,
587 "Opened device '%s' (%s) successfully",
588 v4l2object->vcap.card, v4l2object->videodev);
590 pollfd.fd = v4l2object->video_fd;
591 gst_poll_add_fd (v4l2object->poll, &pollfd);
592 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
593 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
594 gst_poll_fd_ctl_read (v4l2object->poll, &pollfd, TRUE);
596 gst_poll_fd_ctl_write (v4l2object->poll, &pollfd, TRUE);
598 if (v4l2object->extra_controls)
599 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
601 /* UVC devices are never interlaced, and doing VIDIOC_TRY_FMT on them
602 * causes expensive and slow USB IO, so don't probe them for interlaced
604 if (!strcmp ((char *) v4l2object->vcap.driver, "uvcusb")) {
605 v4l2object->never_interlaced = TRUE;
613 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
614 (_("Cannot identify device '%s'."), v4l2object->videodev),
620 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
621 (_("This isn't a device '%s'."), v4l2object->videodev),
627 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
628 (_("Could not open device '%s' for reading and writing."),
629 v4l2object->videodev), GST_ERROR_SYSTEM);
634 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
635 (_("Device '%s' is not a capture device."),
636 v4l2object->videodev),
637 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
642 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
643 (_("Device '%s' is not a output device."),
644 v4l2object->videodev),
645 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
650 if (GST_V4L2_IS_OPEN (v4l2object)) {
652 v4l2_close (v4l2object->video_fd);
653 v4l2object->video_fd = -1;
656 gst_v4l2_empty_lists (v4l2object);
663 gst_v4l2_dup (GstV4l2Object * v4l2object, GstV4l2Object * other)
665 GstPollFD pollfd = GST_POLL_FD_INIT;
667 GST_DEBUG_OBJECT (v4l2object->element, "Trying to dup device %s",
670 GST_V4L2_CHECK_OPEN (other);
671 GST_V4L2_CHECK_NOT_OPEN (v4l2object);
672 GST_V4L2_CHECK_NOT_ACTIVE (other);
673 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
675 v4l2object->vcap = other->vcap;
676 gst_v4l2_adjust_buf_type (v4l2object);
678 v4l2object->video_fd = v4l2_dup (other->video_fd);
679 if (!GST_V4L2_IS_OPEN (v4l2object))
682 g_free (v4l2object->videodev);
683 v4l2object->videodev = g_strdup (other->videodev);
685 GST_INFO_OBJECT (v4l2object->element,
686 "Cloned device '%s' (%s) successfully",
687 v4l2object->vcap.card, v4l2object->videodev);
689 pollfd.fd = v4l2object->video_fd;
690 gst_poll_add_fd (v4l2object->poll, &pollfd);
691 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
692 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
693 gst_poll_fd_ctl_read (v4l2object->poll, &pollfd, TRUE);
695 gst_poll_fd_ctl_write (v4l2object->poll, &pollfd, TRUE);
697 v4l2object->never_interlaced = other->never_interlaced;
698 v4l2object->can_poll_device = TRUE;
704 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
705 (_("Could not dup device '%s' for reading and writing."),
706 v4l2object->videodev), GST_ERROR_SYSTEM);
713 /******************************************************
715 * close the video device (v4l2object->video_fd)
716 * return value: TRUE on success, FALSE on error
717 ******************************************************/
719 gst_v4l2_close (GstV4l2Object * v4l2object)
721 GstPollFD pollfd = GST_POLL_FD_INIT;
722 GST_DEBUG_OBJECT (v4l2object->element, "Trying to close %s",
723 v4l2object->videodev);
725 GST_V4L2_CHECK_OPEN (v4l2object);
726 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
729 v4l2_close (v4l2object->video_fd);
730 pollfd.fd = v4l2object->video_fd;
731 gst_poll_remove_fd (v4l2object->poll, &pollfd);
732 v4l2object->video_fd = -1;
735 gst_v4l2_empty_lists (v4l2object);
741 /******************************************************
742 * gst_v4l2_get_norm()
743 * Get the norm of the current device
744 * return value: TRUE on success, FALSE on error
745 ******************************************************/
747 gst_v4l2_get_norm (GstV4l2Object * v4l2object, v4l2_std_id * norm)
749 GST_DEBUG_OBJECT (v4l2object->element, "getting norm");
751 if (!GST_V4L2_IS_OPEN (v4l2object))
754 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_STD, norm) < 0)
762 GST_DEBUG ("Failed to get the current norm for device %s",
763 v4l2object->videodev);
769 /******************************************************
770 * gst_v4l2_set_norm()
771 * Set the norm of the current device
772 * return value: TRUE on success, FALSE on error
773 ******************************************************/
775 gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm)
777 GST_DEBUG_OBJECT (v4l2object->element, "trying to set norm to "
778 "%" G_GINT64_MODIFIER "x", (guint64) norm);
780 if (!GST_V4L2_IS_OPEN (v4l2object))
783 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_STD, &norm) < 0)
791 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
792 (_("Failed to set norm for device '%s'."),
793 v4l2object->videodev), GST_ERROR_SYSTEM);
798 /******************************************************
799 * gst_v4l2_get_frequency():
800 * get the current frequency
801 * return value: TRUE on success, FALSE on error
802 ******************************************************/
804 gst_v4l2_get_frequency (GstV4l2Object * v4l2object,
805 gint tunernum, gulong * frequency)
807 struct v4l2_frequency freq = { 0, };
809 GstTunerChannel *channel;
811 GST_DEBUG_OBJECT (v4l2object->element, "getting current tuner frequency");
813 if (!GST_V4L2_IS_OPEN (v4l2object))
816 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
818 freq.tuner = tunernum;
819 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0)
822 *frequency = freq.frequency * channel->freq_multiplicator;
829 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
830 (_("Failed to get current tuner frequency for device '%s'."),
831 v4l2object->videodev), GST_ERROR_SYSTEM);
837 /******************************************************
838 * gst_v4l2_set_frequency():
840 * return value: TRUE on success, FALSE on error
841 ******************************************************/
843 gst_v4l2_set_frequency (GstV4l2Object * v4l2object,
844 gint tunernum, gulong frequency)
846 struct v4l2_frequency freq = { 0, };
848 GstTunerChannel *channel;
850 GST_DEBUG_OBJECT (v4l2object->element,
851 "setting current tuner frequency to %lu", frequency);
853 if (!GST_V4L2_IS_OPEN (v4l2object))
856 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
858 freq.tuner = tunernum;
859 /* fill in type - ignore error */
860 v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq);
861 freq.frequency = frequency / channel->freq_multiplicator;
863 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0)
871 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
872 (_("Failed to set current tuner frequency for device '%s' to %lu Hz."),
873 v4l2object->videodev, frequency), GST_ERROR_SYSTEM);
878 /******************************************************
879 * gst_v4l2_signal_strength():
880 * get the strength of the signal on the current input
881 * return value: TRUE on success, FALSE on error
882 ******************************************************/
884 gst_v4l2_signal_strength (GstV4l2Object * v4l2object,
885 gint tunernum, gulong * signal_strength)
887 struct v4l2_tuner tuner = { 0, };
889 GST_DEBUG_OBJECT (v4l2object->element, "trying to get signal strength");
891 if (!GST_V4L2_IS_OPEN (v4l2object))
894 tuner.index = tunernum;
895 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &tuner) < 0)
898 *signal_strength = tuner.signal;
905 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
906 (_("Failed to get signal strength for device '%s'."),
907 v4l2object->videodev), GST_ERROR_SYSTEM);
912 /******************************************************
913 * gst_v4l2_get_attribute():
914 * try to get the value of one specific attribute
915 * return value: TRUE on success, FALSE on error
916 ******************************************************/
918 gst_v4l2_get_attribute (GstV4l2Object * v4l2object,
919 int attribute_num, int *value)
921 struct v4l2_control control = { 0, };
923 GST_DEBUG_OBJECT (v4l2object->element, "getting value of attribute %d",
926 if (!GST_V4L2_IS_OPEN (v4l2object))
929 control.id = attribute_num;
931 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0)
934 *value = control.value;
941 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
942 (_("Failed to get value for control %d on device '%s'."),
943 attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
949 /******************************************************
950 * gst_v4l2_set_attribute():
951 * try to set the value of one specific attribute
952 * return value: TRUE on success, FALSE on error
953 ******************************************************/
955 gst_v4l2_set_attribute (GstV4l2Object * v4l2object,
956 int attribute_num, const int value)
958 struct v4l2_control control = { 0, };
960 GST_DEBUG_OBJECT (v4l2object->element, "setting value of attribute %d to %d",
961 attribute_num, value);
963 if (!GST_V4L2_IS_OPEN (v4l2object))
966 control.id = attribute_num;
967 control.value = value;
968 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0)
976 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
977 (_("Failed to set value %d for control %d on device '%s'."),
978 value, attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
984 set_contol (GQuark field_id, const GValue * value, gpointer user_data)
986 GstV4l2Object *v4l2object = user_data;
987 gpointer *d = g_datalist_id_get_data (&v4l2object->controls, field_id);
989 GST_WARNING_OBJECT (v4l2object,
990 "Control '%s' does not exist or has an unsupported type.",
991 g_quark_to_string (field_id));
994 if (!G_VALUE_HOLDS (value, G_TYPE_INT)) {
995 GST_WARNING_OBJECT (v4l2object,
996 "'int' value expected for control '%s'.", g_quark_to_string (field_id));
999 gst_v4l2_set_attribute (v4l2object, GPOINTER_TO_INT (d),
1000 g_value_get_int (value));
1005 gst_v4l2_set_controls (GstV4l2Object * v4l2object, GstStructure * controls)
1007 return gst_structure_foreach (controls, set_contol, v4l2object);
1011 gst_v4l2_get_input (GstV4l2Object * v4l2object, gint * input)
1015 GST_DEBUG_OBJECT (v4l2object->element, "trying to get input");
1017 if (!GST_V4L2_IS_OPEN (v4l2object))
1020 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_INPUT, &n) < 0)
1025 GST_DEBUG_OBJECT (v4l2object->element, "input: %d", n);
1031 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1032 /* only give a warning message if driver actually claims to have tuner
1035 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1036 (_("Failed to get current input on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1042 gst_v4l2_set_input (GstV4l2Object * v4l2object, gint input)
1044 GST_DEBUG_OBJECT (v4l2object->element, "trying to set input to %d", input);
1046 if (!GST_V4L2_IS_OPEN (v4l2object))
1049 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_INPUT, &input) < 0)
1056 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1057 /* only give a warning message if driver actually claims to have tuner
1060 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1061 (_("Failed to set input %d on device %s."),
1062 input, v4l2object->videodev), GST_ERROR_SYSTEM);
1068 gst_v4l2_get_output (GstV4l2Object * v4l2object, gint * output)
1072 GST_DEBUG_OBJECT (v4l2object->element, "trying to get output");
1074 if (!GST_V4L2_IS_OPEN (v4l2object))
1077 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_OUTPUT, &n) < 0)
1082 GST_DEBUG_OBJECT (v4l2object->element, "output: %d", n);
1088 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1089 /* only give a warning message if driver actually claims to have tuner
1092 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1093 (_("Failed to get current output on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1099 gst_v4l2_set_output (GstV4l2Object * v4l2object, gint output)
1101 GST_DEBUG_OBJECT (v4l2object->element, "trying to set output to %d", output);
1103 if (!GST_V4L2_IS_OPEN (v4l2object))
1106 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_OUTPUT, &output) < 0)
1113 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1114 /* only give a warning message if driver actually claims to have tuner
1117 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1118 (_("Failed to set output %d on device %s."),
1119 output, v4l2object->videodev), GST_ERROR_SYSTEM);