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);
112 /******************************************************
113 * gst_v4l2_empty_lists() and gst_v4l2_fill_lists():
114 * fill/empty the lists of enumerations
115 * return value: TRUE on success, FALSE on error
116 ******************************************************/
118 gst_v4l2_fill_lists (GstV4l2Object * v4l2object)
121 struct v4l2_queryctrl control = { 0, };
125 e = v4l2object->element;
127 GST_DEBUG_OBJECT (e, "getting enumerations");
128 GST_V4L2_CHECK_OPEN (v4l2object);
130 GST_DEBUG_OBJECT (e, " channels");
131 /* and now, the channels */
133 struct v4l2_input input;
134 GstV4l2TunerChannel *v4l2channel;
135 GstTunerChannel *channel;
137 memset (&input, 0, sizeof (input));
140 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUMINPUT, &input) < 0) {
141 if (errno == EINVAL || errno == ENOTTY)
142 break; /* end of enumeration */
144 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
145 (_("Failed to query attributes of input %d in device %s"),
146 n, v4l2object->videodev),
147 ("Failed to get %d in input enumeration for %s. (%d - %s)",
148 n, v4l2object->videodev, errno, strerror (errno)));
153 GST_LOG_OBJECT (e, " index: %d", input.index);
154 GST_LOG_OBJECT (e, " name: '%s'", input.name);
155 GST_LOG_OBJECT (e, " type: %08x", input.type);
156 GST_LOG_OBJECT (e, " audioset: %08x", input.audioset);
157 GST_LOG_OBJECT (e, " std: %016" G_GINT64_MODIFIER "x",
158 (guint64) input.std);
159 GST_LOG_OBJECT (e, " status: %08x", input.status);
161 v4l2channel = g_object_new (GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
162 channel = GST_TUNER_CHANNEL (v4l2channel);
163 channel->label = g_strdup ((const gchar *) input.name);
164 channel->flags = GST_TUNER_CHANNEL_INPUT;
165 v4l2channel->index = n;
167 if (input.type == V4L2_INPUT_TYPE_TUNER) {
168 struct v4l2_tuner vtun;
170 v4l2channel->tuner = input.tuner;
171 channel->flags |= GST_TUNER_CHANNEL_FREQUENCY;
173 vtun.index = input.tuner;
174 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &vtun) < 0) {
175 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
176 (_("Failed to get setting of tuner %d on device '%s'."),
177 input.tuner, v4l2object->videodev), GST_ERROR_SYSTEM);
178 g_object_unref (G_OBJECT (channel));
182 channel->freq_multiplicator =
183 62.5 * ((vtun.capability & V4L2_TUNER_CAP_LOW) ? 1 : 1000);
184 channel->min_frequency = vtun.rangelow * channel->freq_multiplicator;
185 channel->max_frequency = vtun.rangehigh * channel->freq_multiplicator;
186 channel->min_signal = 0;
187 channel->max_signal = 0xffff;
189 if (input.audioset) {
190 /* we take the first. We don't care for
191 * the others for now */
192 while (!(input.audioset & (1 << v4l2channel->audio)))
193 v4l2channel->audio++;
194 channel->flags |= GST_TUNER_CHANNEL_AUDIO;
197 v4l2object->channels =
198 g_list_prepend (v4l2object->channels, (gpointer) channel);
200 v4l2object->channels = g_list_reverse (v4l2object->channels);
202 GST_DEBUG_OBJECT (e, " norms");
205 struct v4l2_standard standard = { 0, };
206 GstV4l2TunerNorm *v4l2norm;
210 /* fill in defaults */
211 standard.frameperiod.numerator = 1;
212 standard.frameperiod.denominator = 0;
215 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUMSTD, &standard) < 0) {
216 if (errno == EINVAL || errno == ENOTTY)
217 break; /* end of enumeration */
219 else if (errno == ENODATA)
220 break; /* end of enumeration, as of Linux 3.7-rc1 */
223 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
224 (_("Failed to query norm on device '%s'."),
225 v4l2object->videodev),
226 ("Failed to get attributes for norm %d on devide '%s'. (%d - %s)",
227 n, v4l2object->videodev, errno, strerror (errno)));
232 GST_DEBUG_OBJECT (e, " '%s', fps: %d / %d",
233 standard.name, standard.frameperiod.denominator,
234 standard.frameperiod.numerator);
236 v4l2norm = g_object_new (GST_TYPE_V4L2_TUNER_NORM, NULL);
237 norm = GST_TUNER_NORM (v4l2norm);
238 norm->label = g_strdup ((const gchar *) standard.name);
239 gst_value_set_fraction (&norm->framerate,
240 standard.frameperiod.denominator, standard.frameperiod.numerator);
241 v4l2norm->index = standard.id;
243 GST_DEBUG_OBJECT (v4l2object->element, "index=%08x, label=%s",
244 (unsigned int) v4l2norm->index, norm->label);
246 v4l2object->norms = g_list_prepend (v4l2object->norms, (gpointer) norm);
248 v4l2object->norms = g_list_reverse (v4l2object->norms);
250 GST_DEBUG_OBJECT (e, " controls+menus");
252 /* and lastly, controls+menus (if appropriate) */
253 #ifdef V4L2_CTRL_FLAG_NEXT_CTRL
254 next = V4L2_CTRL_FLAG_NEXT_CTRL;
262 GstV4l2ColorBalanceChannel *v4l2channel;
263 GstColorBalanceChannel *channel;
269 /* when we reached the last official CID, continue with private CIDs */
270 if (n == V4L2_CID_LASTP1) {
271 GST_DEBUG_OBJECT (e, "checking private CIDs");
272 n = V4L2_CID_PRIVATE_BASE;
274 GST_DEBUG_OBJECT (e, "checking control %08x", n);
276 control.id = n | next;
277 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
280 GST_DEBUG_OBJECT (e, "controls finished");
283 GST_DEBUG_OBJECT (e, "V4L2_CTRL_FLAG_NEXT_CTRL not supported.");
289 if (errno == EINVAL || errno == ENOTTY || errno == EIO || errno == ENOENT) {
290 if (n < V4L2_CID_PRIVATE_BASE) {
291 GST_DEBUG_OBJECT (e, "skipping control %08x", n);
292 /* continue so that we also check private controls */
295 GST_DEBUG_OBJECT (e, "controls finished");
299 GST_WARNING_OBJECT (e, "Failed querying control %d on device '%s'. "
300 "(%d - %s)", n, v4l2object->videodev, errno, strerror (errno));
304 /* bogus driver might mess with id in unexpected ways (e.g. set to 0), so
305 * make sure to simply try all if V4L2_CTRL_FLAG_NEXT_CTRL not supported */
308 if (control.flags & V4L2_CTRL_FLAG_DISABLED) {
309 GST_DEBUG_OBJECT (e, "skipping disabled control");
312 #ifdef V4L2_CTRL_TYPE_CTRL_CLASS
313 if (control.type == V4L2_CTRL_TYPE_CTRL_CLASS) {
314 GST_DEBUG_OBJECT (e, "starting control class '%s'", control.name);
318 switch (control.type) {
319 case V4L2_CTRL_TYPE_INTEGER:
320 case V4L2_CTRL_TYPE_BOOLEAN:
321 case V4L2_CTRL_TYPE_MENU:
322 #ifdef V4L2_CTRL_TYPE_INTEGER_MENU
323 case V4L2_CTRL_TYPE_INTEGER_MENU:
325 #ifdef V4L2_CTRL_TYPE_BITMASK
326 case V4L2_CTRL_TYPE_BITMASK:
328 case V4L2_CTRL_TYPE_BUTTON:{
330 control.name[31] = '\0';
331 for (i = 0; control.name[i]; ++i) {
332 control.name[i] = g_ascii_tolower (control.name[i]);
333 if (!g_ascii_isalnum (control.name[i]))
334 control.name[i] = '_';
336 GST_INFO_OBJECT (e, "adding generic controls '%s'", control.name);
337 g_datalist_id_set_data (&v4l2object->controls,
338 g_quark_from_string ((const gchar *) control.name),
339 GINT_TO_POINTER (n));
344 "Control type for '%s' not suppored for extra controls.",
350 case V4L2_CID_BRIGHTNESS:
351 case V4L2_CID_CONTRAST:
352 case V4L2_CID_SATURATION:
354 case V4L2_CID_BLACK_LEVEL:
355 case V4L2_CID_AUTO_WHITE_BALANCE:
356 case V4L2_CID_DO_WHITE_BALANCE:
357 case V4L2_CID_RED_BALANCE:
358 case V4L2_CID_BLUE_BALANCE:
360 case V4L2_CID_EXPOSURE:
361 case V4L2_CID_AUTOGAIN:
363 #ifdef V4L2_CID_SHARPNESS
364 case V4L2_CID_SHARPNESS:
366 /* we only handle these for now (why?) */
370 #ifndef V4L2_CID_PAN_RESET
371 case V4L2_CID_HCENTER:
373 #ifndef V4L2_CID_TILT_RESET
374 case V4L2_CID_VCENTER:
376 #ifdef V4L2_CID_PAN_RESET
377 case V4L2_CID_PAN_RESET:
379 #ifdef V4L2_CID_TILT_RESET
380 case V4L2_CID_TILT_RESET:
382 /* not handled here, handled by VideoOrientation interface */
385 case V4L2_CID_AUDIO_VOLUME:
386 case V4L2_CID_AUDIO_BALANCE:
387 case V4L2_CID_AUDIO_BASS:
388 case V4L2_CID_AUDIO_TREBLE:
389 case V4L2_CID_AUDIO_MUTE:
390 case V4L2_CID_AUDIO_LOUDNESS:
391 /* FIXME: We should implement GstMixer interface instead */
392 /* but let's not be pedantic and make element more useful for now */
396 "ControlID %s (%x) unhandled, FIXME", control.name, n);
403 GST_DEBUG_OBJECT (e, "Adding ControlID %s (%x)", control.name, n);
404 v4l2channel = g_object_new (GST_TYPE_V4L2_COLOR_BALANCE_CHANNEL, NULL);
405 channel = GST_COLOR_BALANCE_CHANNEL (v4l2channel);
406 channel->label = g_strdup ((const gchar *) control.name);
410 /* FIXME: it will be need just when handling private controls
411 *(currently none of base controls are of this type) */
412 if (control.type == V4L2_CTRL_TYPE_MENU) {
413 struct v4l2_querymenu menu, *mptr;
420 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYMENU, &menu) < 0) {
422 break; /* end of enumeration */
424 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
425 (_("Failed getting controls attributes on device '%s'."),
426 v4l2object->videodev),
427 ("Failed to get %d in menu enumeration for %s. (%d - %s)",
428 n, v4l2object->videodev, errno, strerror (errno)));
432 mptr = g_malloc (sizeof (menu));
433 memcpy (mptr, &menu, sizeof (menu));
434 menus = g_list_append (menus, mptr);
437 v4l2object->menus = g_list_append (v4l2object->menus, menus);
440 switch (control.type) {
441 case V4L2_CTRL_TYPE_INTEGER:
442 channel->min_value = control.minimum;
443 channel->max_value = control.maximum;
445 case V4L2_CTRL_TYPE_BOOLEAN:
446 channel->min_value = FALSE;
447 channel->max_value = TRUE;
450 /* FIXME we should find out how to handle V4L2_CTRL_TYPE_BUTTON.
451 BUTTON controls like V4L2_CID_DO_WHITE_BALANCE can just be set (1) or
452 unset (0), but can't be queried */
454 "Control with non supported type %s (%x), type=%d",
455 control.name, n, control.type);
456 channel->min_value = channel->max_value = 0;
461 g_list_prepend (v4l2object->colors, (gpointer) channel);
463 v4l2object->colors = g_list_reverse (v4l2object->colors);
465 GST_DEBUG_OBJECT (e, "done");
471 gst_v4l2_empty_lists (GstV4l2Object * v4l2object)
473 GST_DEBUG_OBJECT (v4l2object->element, "deleting enumerations");
475 g_list_foreach (v4l2object->channels, (GFunc) g_object_unref, NULL);
476 g_list_free (v4l2object->channels);
477 v4l2object->channels = NULL;
479 g_list_foreach (v4l2object->norms, (GFunc) g_object_unref, NULL);
480 g_list_free (v4l2object->norms);
481 v4l2object->norms = NULL;
483 g_list_foreach (v4l2object->colors, (GFunc) g_object_unref, NULL);
484 g_list_free (v4l2object->colors);
485 v4l2object->colors = NULL;
487 g_datalist_clear (&v4l2object->controls);
491 gst_v4l2_adjust_buf_type (GstV4l2Object * v4l2object)
493 /* when calling gst_v4l2_object_new the user decides the initial type
494 * so adjust it if multi-planar is supported
495 * the driver should make it exclusive. So the driver should
496 * not support both MPLANE and non-PLANE.
497 * Because even when using MPLANE it still possibles to use it
498 * in a contiguous manner. In this case the first v4l2 plane
499 * contains all the gst planes.
501 #ifdef V4L2_CAP_VIDEO_M2M_MPLANE
502 #define CHECK_CAPS (V4L2_CAP_VIDEO_OUTPUT_MPLANE | V4L2_CAP_VIDEO_M2M_MPLANE)
504 #define CHECK_CAPS (V4L2_CAP_VIDEO_OUTPUT_MPLANE)
507 switch (v4l2object->type) {
508 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
509 if (v4l2object->vcap.capabilities & CHECK_CAPS) {
510 GST_DEBUG ("adjust type to multi-planar output");
511 v4l2object->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
514 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
515 if (v4l2object->vcap.capabilities & CHECK_CAPS) {
516 /* FIXME: for now it's an untested case so just put a warning */
517 GST_WARNING ("untested V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE");
519 GST_DEBUG ("adjust type to multi-planar capture");
520 v4l2object->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
528 /******************************************************
530 * open the video device (v4l2object->videodev)
531 * return value: TRUE on success, FALSE on error
532 ******************************************************/
534 gst_v4l2_open (GstV4l2Object * v4l2object)
538 GstPollFD pollfd = GST_POLL_FD_INIT;
540 GST_DEBUG_OBJECT (v4l2object->element, "Trying to open device %s",
541 v4l2object->videodev);
543 GST_V4L2_CHECK_NOT_OPEN (v4l2object);
544 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
546 /* be sure we have a device */
547 if (!v4l2object->videodev)
548 v4l2object->videodev = g_strdup ("/dev/video");
550 /* check if it is a device */
551 if (stat (v4l2object->videodev, &st) == -1)
554 if (!S_ISCHR (st.st_mode))
557 /* open the device */
558 v4l2object->video_fd =
559 open (v4l2object->videodev, O_RDWR /* | O_NONBLOCK */ );
561 if (!GST_V4L2_IS_OPEN (v4l2object))
564 libv4l2_fd = v4l2_fd_open (v4l2object->video_fd,
565 V4L2_ENABLE_ENUM_FMT_EMULATION);
566 /* Note the v4l2_xxx functions are designed so that if they get passed an
567 unknown fd, the will behave exactly as their regular xxx counterparts, so
568 if v4l2_fd_open fails, we continue as normal (missing the libv4l2 custom
569 cam format to normal formats conversion). Chances are big we will still
570 fail then though, as normally v4l2_fd_open only fails if the device is not
572 if (libv4l2_fd != -1)
573 v4l2object->video_fd = libv4l2_fd;
575 v4l2object->can_poll_device = TRUE;
577 /* get capabilities, error will be posted */
578 if (!gst_v4l2_get_capabilities (v4l2object))
581 /* do we need to be a capture device? */
582 if (GST_IS_V4L2SRC (v4l2object->element) &&
583 !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_CAPTURE |
584 V4L2_CAP_VIDEO_CAPTURE_MPLANE)))
587 if (GST_IS_V4L2SINK (v4l2object->element) &&
588 !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_OUTPUT |
589 V4L2_CAP_VIDEO_OUTPUT_MPLANE)))
592 if (GST_IS_V4L2_VIDEO_DEC (v4l2object->element) &&
593 /* Today's M2M device only expose M2M */
594 !((v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_M2M |
595 V4L2_CAP_VIDEO_M2M_MPLANE)) ||
596 /* But legacy driver may expose both CAPTURE and OUTPUT */
597 ((v4l2object->vcap.capabilities &
598 (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) &&
599 (v4l2object->vcap.capabilities &
600 (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE)))))
603 gst_v4l2_adjust_buf_type (v4l2object);
605 /* create enumerations, posts errors. */
606 if (!gst_v4l2_fill_lists (v4l2object))
609 GST_INFO_OBJECT (v4l2object->element,
610 "Opened device '%s' (%s) successfully",
611 v4l2object->vcap.card, v4l2object->videodev);
613 pollfd.fd = v4l2object->video_fd;
614 gst_poll_add_fd (v4l2object->poll, &pollfd);
615 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
616 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
617 gst_poll_fd_ctl_read (v4l2object->poll, &pollfd, TRUE);
619 gst_poll_fd_ctl_write (v4l2object->poll, &pollfd, TRUE);
621 if (v4l2object->extra_controls)
622 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
624 /* UVC devices are never interlaced, and doing VIDIOC_TRY_FMT on them
625 * causes expensive and slow USB IO, so don't probe them for interlaced
627 if (!strcmp ((char *) v4l2object->vcap.driver, "uvcusb")) {
628 v4l2object->never_interlaced = TRUE;
636 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
637 (_("Cannot identify device '%s'."), v4l2object->videodev),
643 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
644 (_("This isn't a device '%s'."), v4l2object->videodev),
650 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
651 (_("Could not open device '%s' for reading and writing."),
652 v4l2object->videodev), GST_ERROR_SYSTEM);
657 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
658 (_("Device '%s' is not a capture device."),
659 v4l2object->videodev),
660 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
665 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
666 (_("Device '%s' is not a output device."),
667 v4l2object->videodev),
668 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
673 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
674 (_("Device '%s' is not a M2M device."),
675 v4l2object->videodev),
676 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
681 if (GST_V4L2_IS_OPEN (v4l2object)) {
683 v4l2_close (v4l2object->video_fd);
684 v4l2object->video_fd = -1;
687 gst_v4l2_empty_lists (v4l2object);
694 gst_v4l2_dup (GstV4l2Object * v4l2object, GstV4l2Object * other)
696 GstPollFD pollfd = GST_POLL_FD_INIT;
698 GST_DEBUG_OBJECT (v4l2object->element, "Trying to dup device %s",
701 GST_V4L2_CHECK_OPEN (other);
702 GST_V4L2_CHECK_NOT_OPEN (v4l2object);
703 GST_V4L2_CHECK_NOT_ACTIVE (other);
704 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
706 v4l2object->vcap = other->vcap;
707 gst_v4l2_adjust_buf_type (v4l2object);
709 v4l2object->video_fd = v4l2_dup (other->video_fd);
710 if (!GST_V4L2_IS_OPEN (v4l2object))
713 g_free (v4l2object->videodev);
714 v4l2object->videodev = g_strdup (other->videodev);
716 GST_INFO_OBJECT (v4l2object->element,
717 "Cloned device '%s' (%s) successfully",
718 v4l2object->vcap.card, v4l2object->videodev);
720 pollfd.fd = v4l2object->video_fd;
721 gst_poll_add_fd (v4l2object->poll, &pollfd);
722 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
723 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
724 gst_poll_fd_ctl_read (v4l2object->poll, &pollfd, TRUE);
726 gst_poll_fd_ctl_write (v4l2object->poll, &pollfd, TRUE);
728 v4l2object->never_interlaced = other->never_interlaced;
729 v4l2object->can_poll_device = TRUE;
735 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
736 (_("Could not dup device '%s' for reading and writing."),
737 v4l2object->videodev), GST_ERROR_SYSTEM);
744 /******************************************************
746 * close the video device (v4l2object->video_fd)
747 * return value: TRUE on success, FALSE on error
748 ******************************************************/
750 gst_v4l2_close (GstV4l2Object * v4l2object)
752 GstPollFD pollfd = GST_POLL_FD_INIT;
753 GST_DEBUG_OBJECT (v4l2object->element, "Trying to close %s",
754 v4l2object->videodev);
756 GST_V4L2_CHECK_OPEN (v4l2object);
757 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
760 v4l2_close (v4l2object->video_fd);
761 pollfd.fd = v4l2object->video_fd;
762 gst_poll_remove_fd (v4l2object->poll, &pollfd);
763 v4l2object->video_fd = -1;
766 gst_v4l2_empty_lists (v4l2object);
772 /******************************************************
773 * gst_v4l2_get_norm()
774 * Get the norm of the current device
775 * return value: TRUE on success, FALSE on error
776 ******************************************************/
778 gst_v4l2_get_norm (GstV4l2Object * v4l2object, v4l2_std_id * norm)
780 GST_DEBUG_OBJECT (v4l2object->element, "getting norm");
782 if (!GST_V4L2_IS_OPEN (v4l2object))
785 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_STD, norm) < 0)
793 GST_DEBUG ("Failed to get the current norm for device %s",
794 v4l2object->videodev);
800 /******************************************************
801 * gst_v4l2_set_norm()
802 * Set the norm of the current device
803 * return value: TRUE on success, FALSE on error
804 ******************************************************/
806 gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm)
808 GST_DEBUG_OBJECT (v4l2object->element, "trying to set norm to "
809 "%" G_GINT64_MODIFIER "x", (guint64) norm);
811 if (!GST_V4L2_IS_OPEN (v4l2object))
814 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_STD, &norm) < 0)
822 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
823 (_("Failed to set norm for device '%s'."),
824 v4l2object->videodev), GST_ERROR_SYSTEM);
829 /******************************************************
830 * gst_v4l2_get_frequency():
831 * get the current frequency
832 * return value: TRUE on success, FALSE on error
833 ******************************************************/
835 gst_v4l2_get_frequency (GstV4l2Object * v4l2object,
836 gint tunernum, gulong * frequency)
838 struct v4l2_frequency freq = { 0, };
840 GstTunerChannel *channel;
842 GST_DEBUG_OBJECT (v4l2object->element, "getting current tuner frequency");
844 if (!GST_V4L2_IS_OPEN (v4l2object))
847 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
849 freq.tuner = tunernum;
850 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0)
853 *frequency = freq.frequency * channel->freq_multiplicator;
860 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
861 (_("Failed to get current tuner frequency for device '%s'."),
862 v4l2object->videodev), GST_ERROR_SYSTEM);
868 /******************************************************
869 * gst_v4l2_set_frequency():
871 * return value: TRUE on success, FALSE on error
872 ******************************************************/
874 gst_v4l2_set_frequency (GstV4l2Object * v4l2object,
875 gint tunernum, gulong frequency)
877 struct v4l2_frequency freq = { 0, };
879 GstTunerChannel *channel;
881 GST_DEBUG_OBJECT (v4l2object->element,
882 "setting current tuner frequency to %lu", frequency);
884 if (!GST_V4L2_IS_OPEN (v4l2object))
887 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
889 freq.tuner = tunernum;
890 /* fill in type - ignore error */
891 v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq);
892 freq.frequency = frequency / channel->freq_multiplicator;
894 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0)
902 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
903 (_("Failed to set current tuner frequency for device '%s' to %lu Hz."),
904 v4l2object->videodev, frequency), GST_ERROR_SYSTEM);
909 /******************************************************
910 * gst_v4l2_signal_strength():
911 * get the strength of the signal on the current input
912 * return value: TRUE on success, FALSE on error
913 ******************************************************/
915 gst_v4l2_signal_strength (GstV4l2Object * v4l2object,
916 gint tunernum, gulong * signal_strength)
918 struct v4l2_tuner tuner = { 0, };
920 GST_DEBUG_OBJECT (v4l2object->element, "trying to get signal strength");
922 if (!GST_V4L2_IS_OPEN (v4l2object))
925 tuner.index = tunernum;
926 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &tuner) < 0)
929 *signal_strength = tuner.signal;
936 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
937 (_("Failed to get signal strength for device '%s'."),
938 v4l2object->videodev), GST_ERROR_SYSTEM);
943 /******************************************************
944 * gst_v4l2_get_attribute():
945 * try to get the value of one specific attribute
946 * return value: TRUE on success, FALSE on error
947 ******************************************************/
949 gst_v4l2_get_attribute (GstV4l2Object * v4l2object,
950 int attribute_num, int *value)
952 struct v4l2_control control = { 0, };
954 GST_DEBUG_OBJECT (v4l2object->element, "getting value of attribute %d",
957 if (!GST_V4L2_IS_OPEN (v4l2object))
960 control.id = attribute_num;
962 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0)
965 *value = control.value;
972 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
973 (_("Failed to get value for control %d on device '%s'."),
974 attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
980 /******************************************************
981 * gst_v4l2_set_attribute():
982 * try to set the value of one specific attribute
983 * return value: TRUE on success, FALSE on error
984 ******************************************************/
986 gst_v4l2_set_attribute (GstV4l2Object * v4l2object,
987 int attribute_num, const int value)
989 struct v4l2_control control = { 0, };
991 GST_DEBUG_OBJECT (v4l2object->element, "setting value of attribute %d to %d",
992 attribute_num, value);
994 if (!GST_V4L2_IS_OPEN (v4l2object))
997 control.id = attribute_num;
998 control.value = value;
999 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0)
1007 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1008 (_("Failed to set value %d for control %d on device '%s'."),
1009 value, attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
1015 set_contol (GQuark field_id, const GValue * value, gpointer user_data)
1017 GstV4l2Object *v4l2object = user_data;
1018 gpointer *d = g_datalist_id_get_data (&v4l2object->controls, field_id);
1020 GST_WARNING_OBJECT (v4l2object,
1021 "Control '%s' does not exist or has an unsupported type.",
1022 g_quark_to_string (field_id));
1025 if (!G_VALUE_HOLDS (value, G_TYPE_INT)) {
1026 GST_WARNING_OBJECT (v4l2object,
1027 "'int' value expected for control '%s'.", g_quark_to_string (field_id));
1030 gst_v4l2_set_attribute (v4l2object, GPOINTER_TO_INT (d),
1031 g_value_get_int (value));
1036 gst_v4l2_set_controls (GstV4l2Object * v4l2object, GstStructure * controls)
1038 return gst_structure_foreach (controls, set_contol, v4l2object);
1042 gst_v4l2_get_input (GstV4l2Object * v4l2object, gint * input)
1046 GST_DEBUG_OBJECT (v4l2object->element, "trying to get input");
1048 if (!GST_V4L2_IS_OPEN (v4l2object))
1051 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_INPUT, &n) < 0)
1056 GST_DEBUG_OBJECT (v4l2object->element, "input: %d", n);
1062 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1063 /* only give a warning message if driver actually claims to have tuner
1066 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1067 (_("Failed to get current input on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1073 gst_v4l2_set_input (GstV4l2Object * v4l2object, gint input)
1075 GST_DEBUG_OBJECT (v4l2object->element, "trying to set input to %d", input);
1077 if (!GST_V4L2_IS_OPEN (v4l2object))
1080 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_INPUT, &input) < 0)
1087 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1088 /* only give a warning message if driver actually claims to have tuner
1091 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1092 (_("Failed to set input %d on device %s."),
1093 input, v4l2object->videodev), GST_ERROR_SYSTEM);
1099 gst_v4l2_get_output (GstV4l2Object * v4l2object, gint * output)
1103 GST_DEBUG_OBJECT (v4l2object->element, "trying to get output");
1105 if (!GST_V4L2_IS_OPEN (v4l2object))
1108 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_OUTPUT, &n) < 0)
1113 GST_DEBUG_OBJECT (v4l2object->element, "output: %d", n);
1119 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1120 /* only give a warning message if driver actually claims to have tuner
1123 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1124 (_("Failed to get current output on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1130 gst_v4l2_set_output (GstV4l2Object * v4l2object, gint output)
1132 GST_DEBUG_OBJECT (v4l2object->element, "trying to set output to %d", output);
1134 if (!GST_V4L2_IS_OPEN (v4l2object))
1137 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_OUTPUT, &output) < 0)
1144 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1145 /* only give a warning message if driver actually claims to have tuner
1148 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1149 (_("Failed to set output %d on device %s."),
1150 output, v4l2object->videodev), GST_ERROR_SYSTEM);