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>
36 #ifdef GST_EXT_V4L2SRC_MODIFIED
38 #endif /* GST_EXT_V4L2SRC_MODIFIED */
40 /* Needed on older Solaris Nevada builds (72 at least) */
42 #include <sys/ioccom.h>
44 #include "v4l2_calls.h"
45 #include "gstv4l2tuner.h"
47 #include "gstv4l2xoverlay.h"
49 #include "gstv4l2colorbalance.h"
51 #include "gstv4l2src.h"
52 #include "gstv4l2sink.h"
53 #include "gstv4l2videodec.h"
55 #include "gst/gst-i18n-plugin.h"
57 #ifdef GST_EXT_V4L2SRC_MODIFIED
60 V4L2_OPEN_ERROR_STAT_FAILED,
61 V4L2_OPEN_ERROR_NO_DEVICE,
62 V4L2_OPEN_ERROR_NOT_OPEN,
63 V4L2_OPEN_ERROR_NOT_CAPTURE,
64 V4L2_OPEN_ERROR_NOT_OUTPUT
66 #endif /* GST_EXT_V4L2SRC_MODIFIED */
68 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
69 #define GST_CAT_DEFAULT v4l2_debug
71 /******************************************************
72 * gst_v4l2_get_capabilities():
73 * get the device's capturing capabilities
74 * return value: TRUE on success, FALSE on error
75 ******************************************************/
77 gst_v4l2_get_capabilities (GstV4l2Object * v4l2object)
81 e = v4l2object->element;
83 GST_DEBUG_OBJECT (e, "getting capabilities");
85 if (!GST_V4L2_IS_OPEN (v4l2object))
88 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYCAP, &v4l2object->vcap) < 0)
91 GST_LOG_OBJECT (e, "driver: '%s'", v4l2object->vcap.driver);
92 GST_LOG_OBJECT (e, "card: '%s'", v4l2object->vcap.card);
93 GST_LOG_OBJECT (e, "bus_info: '%s'", v4l2object->vcap.bus_info);
94 GST_LOG_OBJECT (e, "version: %08x", v4l2object->vcap.version);
95 GST_LOG_OBJECT (e, "capabilites: %08x", v4l2object->vcap.capabilities);
102 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
103 (_("Error getting capabilities for device '%s': "
104 "It isn't a v4l2 driver. Check if it is a v4l1 driver."),
105 v4l2object->videodev), GST_ERROR_SYSTEM);
110 /******************************************************
111 * The video4linux command line tool v4l2-ctrl
112 * normalises the names of the controls received from
115 * "Exposure (absolute)" -> "exposure_absolute"
117 * We follow their lead here. @name is modified
119 ******************************************************/
121 gst_v4l2_normalise_control_name (gchar * name)
124 for (i = 0, j = 0; name[j]; ++j) {
125 if (g_ascii_isalnum (name[j])) {
126 if (i > 0 && !g_ascii_isalnum (name[j - 1]))
128 name[i++] = g_ascii_tolower (name[j]);
134 /******************************************************
135 * gst_v4l2_empty_lists() and gst_v4l2_fill_lists():
136 * fill/empty the lists of enumerations
137 * return value: TRUE on success, FALSE on error
138 ******************************************************/
140 gst_v4l2_fill_lists (GstV4l2Object * v4l2object)
143 struct v4l2_queryctrl control = { 0, };
147 e = v4l2object->element;
149 GST_DEBUG_OBJECT (e, "getting enumerations");
150 GST_V4L2_CHECK_OPEN (v4l2object);
152 GST_DEBUG_OBJECT (e, " channels");
153 /* and now, the channels */
155 struct v4l2_input input;
156 GstV4l2TunerChannel *v4l2channel;
157 GstTunerChannel *channel;
159 memset (&input, 0, sizeof (input));
162 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUMINPUT, &input) < 0) {
163 if (errno == EINVAL || errno == ENOTTY)
164 break; /* end of enumeration */
166 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
167 (_("Failed to query attributes of input %d in device %s"),
168 n, v4l2object->videodev),
169 ("Failed to get %d in input enumeration for %s. (%d - %s)",
170 n, v4l2object->videodev, errno, strerror (errno)));
175 GST_LOG_OBJECT (e, " index: %d", input.index);
176 GST_LOG_OBJECT (e, " name: '%s'", input.name);
177 GST_LOG_OBJECT (e, " type: %08x", input.type);
178 GST_LOG_OBJECT (e, " audioset: %08x", input.audioset);
179 GST_LOG_OBJECT (e, " std: %016" G_GINT64_MODIFIER "x",
180 (guint64) input.std);
181 GST_LOG_OBJECT (e, " status: %08x", input.status);
183 v4l2channel = g_object_new (GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
184 channel = GST_TUNER_CHANNEL (v4l2channel);
185 channel->label = g_strdup ((const gchar *) input.name);
186 channel->flags = GST_TUNER_CHANNEL_INPUT;
187 v4l2channel->index = n;
189 if (input.type == V4L2_INPUT_TYPE_TUNER) {
190 struct v4l2_tuner vtun;
192 v4l2channel->tuner = input.tuner;
193 channel->flags |= GST_TUNER_CHANNEL_FREQUENCY;
195 vtun.index = input.tuner;
196 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &vtun) < 0) {
197 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
198 (_("Failed to get setting of tuner %d on device '%s'."),
199 input.tuner, v4l2object->videodev), GST_ERROR_SYSTEM);
200 g_object_unref (G_OBJECT (channel));
204 channel->freq_multiplicator =
205 62.5 * ((vtun.capability & V4L2_TUNER_CAP_LOW) ? 1 : 1000);
206 channel->min_frequency = vtun.rangelow * channel->freq_multiplicator;
207 channel->max_frequency = vtun.rangehigh * channel->freq_multiplicator;
208 channel->min_signal = 0;
209 channel->max_signal = 0xffff;
211 if (input.audioset) {
212 /* we take the first. We don't care for
213 * the others for now */
214 while (!(input.audioset & (1 << v4l2channel->audio)))
215 v4l2channel->audio++;
216 channel->flags |= GST_TUNER_CHANNEL_AUDIO;
219 v4l2object->channels =
220 g_list_prepend (v4l2object->channels, (gpointer) channel);
222 v4l2object->channels = g_list_reverse (v4l2object->channels);
224 GST_DEBUG_OBJECT (e, " norms");
227 struct v4l2_standard standard = { 0, };
228 GstV4l2TunerNorm *v4l2norm;
232 /* fill in defaults */
233 standard.frameperiod.numerator = 1;
234 standard.frameperiod.denominator = 0;
237 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUMSTD, &standard) < 0) {
238 if (errno == EINVAL || errno == ENOTTY)
239 break; /* end of enumeration */
241 else if (errno == ENODATA)
242 break; /* end of enumeration, as of Linux 3.7-rc1 */
245 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
246 (_("Failed to query norm on device '%s'."),
247 v4l2object->videodev),
248 ("Failed to get attributes for norm %d on devide '%s'. (%d - %s)",
249 n, v4l2object->videodev, errno, strerror (errno)));
254 GST_DEBUG_OBJECT (e, " '%s', fps: %d / %d",
255 standard.name, standard.frameperiod.denominator,
256 standard.frameperiod.numerator);
258 v4l2norm = g_object_new (GST_TYPE_V4L2_TUNER_NORM, NULL);
259 norm = GST_TUNER_NORM (v4l2norm);
260 norm->label = g_strdup ((const gchar *) standard.name);
261 gst_value_set_fraction (&norm->framerate,
262 standard.frameperiod.denominator, standard.frameperiod.numerator);
263 v4l2norm->index = standard.id;
265 GST_DEBUG_OBJECT (v4l2object->element, "index=%08x, label=%s",
266 (unsigned int) v4l2norm->index, norm->label);
268 v4l2object->norms = g_list_prepend (v4l2object->norms, (gpointer) norm);
270 v4l2object->norms = g_list_reverse (v4l2object->norms);
272 GST_DEBUG_OBJECT (e, " controls+menus");
274 /* and lastly, controls+menus (if appropriate) */
275 next = V4L2_CTRL_FLAG_NEXT_CTRL;
280 GstV4l2ColorBalanceChannel *v4l2channel;
281 GstColorBalanceChannel *channel;
287 /* when we reached the last official CID, continue with private CIDs */
288 if (n == V4L2_CID_LASTP1) {
289 GST_DEBUG_OBJECT (e, "checking private CIDs");
290 n = V4L2_CID_PRIVATE_BASE;
292 GST_DEBUG_OBJECT (e, "checking control %08x", n);
294 control.id = n | next;
295 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
298 GST_DEBUG_OBJECT (e, "controls finished");
301 GST_DEBUG_OBJECT (e, "V4L2_CTRL_FLAG_NEXT_CTRL not supported.");
307 if (errno == EINVAL || errno == ENOTTY || errno == EIO || errno == ENOENT) {
308 if (n < V4L2_CID_PRIVATE_BASE) {
309 GST_DEBUG_OBJECT (e, "skipping control %08x", n);
310 /* continue so that we also check private controls */
311 n = V4L2_CID_PRIVATE_BASE - 1;
314 GST_DEBUG_OBJECT (e, "controls finished");
318 GST_WARNING_OBJECT (e, "Failed querying control %d on device '%s'. "
319 "(%d - %s)", n, v4l2object->videodev, errno, strerror (errno));
323 /* bogus driver might mess with id in unexpected ways (e.g. set to 0), so
324 * make sure to simply try all if V4L2_CTRL_FLAG_NEXT_CTRL not supported */
327 if (control.flags & V4L2_CTRL_FLAG_DISABLED) {
328 GST_DEBUG_OBJECT (e, "skipping disabled control");
332 if (control.type == V4L2_CTRL_TYPE_CTRL_CLASS) {
333 GST_DEBUG_OBJECT (e, "starting control class '%s'", control.name);
337 switch (control.type) {
338 case V4L2_CTRL_TYPE_INTEGER:
339 case V4L2_CTRL_TYPE_BOOLEAN:
340 case V4L2_CTRL_TYPE_MENU:
341 case V4L2_CTRL_TYPE_INTEGER_MENU:
342 case V4L2_CTRL_TYPE_BITMASK:
343 case V4L2_CTRL_TYPE_BUTTON:{
344 control.name[31] = '\0';
345 gst_v4l2_normalise_control_name ((gchar *) control.name);
346 g_datalist_id_set_data (&v4l2object->controls,
347 g_quark_from_string ((const gchar *) control.name),
348 GINT_TO_POINTER (n));
353 "Control type for '%s' not suppored for extra controls.",
359 case V4L2_CID_BRIGHTNESS:
360 case V4L2_CID_CONTRAST:
361 case V4L2_CID_SATURATION:
363 case V4L2_CID_BLACK_LEVEL:
364 case V4L2_CID_AUTO_WHITE_BALANCE:
365 case V4L2_CID_DO_WHITE_BALANCE:
366 case V4L2_CID_RED_BALANCE:
367 case V4L2_CID_BLUE_BALANCE:
369 case V4L2_CID_EXPOSURE:
370 case V4L2_CID_AUTOGAIN:
372 case V4L2_CID_SHARPNESS:
373 /* we only handle these for now (why?) */
377 case V4L2_CID_PAN_RESET:
378 case V4L2_CID_TILT_RESET:
379 /* not handled here, handled by VideoOrientation interface */
382 case V4L2_CID_AUDIO_VOLUME:
383 case V4L2_CID_AUDIO_BALANCE:
384 case V4L2_CID_AUDIO_BASS:
385 case V4L2_CID_AUDIO_TREBLE:
386 case V4L2_CID_AUDIO_MUTE:
387 case V4L2_CID_AUDIO_LOUDNESS:
388 /* FIXME: We should implement GstMixer interface instead */
389 /* but let's not be pedantic and make element more useful for now */
391 case V4L2_CID_ALPHA_COMPONENT:
392 v4l2object->has_alpha_component = TRUE;
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 #define CHECK_CAPS (V4L2_CAP_VIDEO_OUTPUT_MPLANE | V4L2_CAP_VIDEO_M2M_MPLANE)
502 switch (v4l2object->type) {
503 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
504 if (v4l2object->vcap.capabilities & CHECK_CAPS) {
505 GST_DEBUG ("adjust type to multi-planar output");
506 v4l2object->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
509 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
510 if (v4l2object->vcap.capabilities & CHECK_CAPS) {
511 GST_DEBUG ("adjust type to multi-planar capture");
512 v4l2object->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
521 /******************************************************
523 * open the video device (v4l2object->videodev)
524 * return value: TRUE on success, FALSE on error
525 ******************************************************/
527 gst_v4l2_open (GstV4l2Object * v4l2object)
531 #ifdef GST_EXT_V4L2SRC_MODIFIED
532 int error_type = V4L2_OPEN_ERROR_STAT_FAILED;
533 int device_index = 0;
536 memset(&glob_buf, 0x0, sizeof(glob_t));
537 #endif /* GST_EXT_V4L2SRC_MODIFIED */
539 GST_DEBUG_OBJECT (v4l2object->element, "Trying to open device %s",
540 v4l2object->videodev);
542 GST_V4L2_CHECK_NOT_OPEN (v4l2object);
543 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
545 /* be sure we have a device */
546 if (!v4l2object->videodev)
547 v4l2object->videodev = g_strdup ("/dev/video");
549 #ifdef GST_EXT_V4L2SRC_MODIFIED
551 #endif /* GST_EXT_V4L2SRC_MODIFIED */
552 /* check if it is a device */
553 #ifdef GST_EXT_V4L2SRC_MODIFIED
554 if (stat (v4l2object->videodev, &st) == -1) {
555 error_type = V4L2_OPEN_ERROR_STAT_FAILED;
556 goto pre_error_check;
558 #else /* GST_EXT_V4L2SRC_MODIFIED */
559 if (stat (v4l2object->videodev, &st) == -1)
561 #endif /* GST_EXT_V4L2SRC_MODIFIED */
563 #ifdef GST_EXT_V4L2SRC_MODIFIED
564 if (!S_ISCHR (st.st_mode)) {
565 error_type = V4L2_OPEN_ERROR_NO_DEVICE;
566 goto pre_error_check;
568 #else /* GST_EXT_V4L2SRC_MODIFIED */
569 if (!S_ISCHR (st.st_mode))
571 #endif /* GST_EXT_V4L2SRC_MODIFIED */
573 /* open the device */
574 v4l2object->video_fd =
575 open (v4l2object->videodev, O_RDWR /* | O_NONBLOCK */ );
577 #ifdef GST_EXT_V4L2SRC_MODIFIED
578 if (!GST_V4L2_IS_OPEN (v4l2object)) {
579 error_type = V4L2_OPEN_ERROR_NOT_OPEN;
580 goto pre_error_check;
582 #else /* GST_EXT_V4L2SRC_MODIFIED */
583 if (!GST_V4L2_IS_OPEN (v4l2object))
585 #endif /* GST_EXT_V4L2SRC_MODIFIED */
587 libv4l2_fd = v4l2_fd_open (v4l2object->video_fd,
588 V4L2_ENABLE_ENUM_FMT_EMULATION);
589 /* Note the v4l2_xxx functions are designed so that if they get passed an
590 unknown fd, the will behave exactly as their regular xxx counterparts, so
591 if v4l2_fd_open fails, we continue as normal (missing the libv4l2 custom
592 cam format to normal formats conversion). Chances are big we will still
593 fail then though, as normally v4l2_fd_open only fails if the device is not
595 if (libv4l2_fd != -1)
596 v4l2object->video_fd = libv4l2_fd;
598 /* get capabilities, error will be posted */
599 #ifdef GST_EXT_V4L2SRC_MODIFIED
600 if (!gst_v4l2_get_capabilities (v4l2object)) {
601 error_type = V4L2_OPEN_ERROR;
602 goto pre_error_check;
604 #else /* GST_EXT_V4L2SRC_MODIFIED */
605 if (!gst_v4l2_get_capabilities (v4l2object))
607 #endif /* GST_EXT_V4L2SRC_MODIFIED */
609 /* do we need to be a capture device? */
610 #ifdef GST_EXT_V4L2SRC_MODIFIED
611 GST_INFO_OBJECT(v4l2object->element, "capabilities 0x%x", v4l2object->vcap.capabilities);
612 if (GST_IS_V4L2SRC (v4l2object->element) &&
613 (!(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) ||
614 (v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE)))) {
615 error_type = V4L2_OPEN_ERROR_NOT_CAPTURE;
616 goto pre_error_check;
618 #else /* GST_EXT_V4L2SRC_MODIFIED */
619 if (GST_IS_V4L2SRC (v4l2object->element) &&
620 !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_CAPTURE |
621 V4L2_CAP_VIDEO_CAPTURE_MPLANE)))
623 #endif /* GST_EXT_V4L2SRC_MODIFIED */
625 #ifdef GST_EXT_V4L2SRC_MODIFIED
626 if (GST_IS_V4L2SINK (v4l2object->element) &&
627 !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_OUTPUT |
628 V4L2_CAP_VIDEO_OUTPUT_MPLANE))) {
629 error_type = V4L2_OPEN_ERROR_NOT_OUTPUT;
630 goto pre_error_check;
632 #else /* GST_EXT_V4L2SRC_MODIFIED */
633 if (GST_IS_V4L2SINK (v4l2object->element) &&
634 !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_OUTPUT |
635 V4L2_CAP_VIDEO_OUTPUT_MPLANE)))
637 #endif /* GST_EXT_V4L2SRC_MODIFIED */
639 if (GST_IS_V4L2_VIDEO_DEC (v4l2object->element) &&
640 /* Today's M2M device only expose M2M */
641 !((v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_M2M |
642 V4L2_CAP_VIDEO_M2M_MPLANE)) ||
643 /* But legacy driver may expose both CAPTURE and OUTPUT */
644 ((v4l2object->vcap.capabilities &
645 (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) &&
646 (v4l2object->vcap.capabilities &
647 (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE)))))
650 gst_v4l2_adjust_buf_type (v4l2object);
652 /* create enumerations, posts errors. */
653 #ifdef GST_EXT_V4L2SRC_MODIFIED
654 if (!gst_v4l2_fill_lists (v4l2object)) {
655 error_type = V4L2_OPEN_ERROR;
656 goto pre_error_check;
658 #else /* GST_EXT_V4L2SRC_MODIFIED */
659 if (!gst_v4l2_fill_lists (v4l2object))
661 #endif /* GST_EXT_V4L2SRC_MODIFIED */
663 GST_INFO_OBJECT (v4l2object->element,
664 "Opened device '%s' (%s) successfully",
665 v4l2object->vcap.card, v4l2object->videodev);
667 if (v4l2object->extra_controls)
668 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
670 #ifdef GST_EXT_V4L2SRC_MODIFIED
672 #endif /* GST_EXT_V4L2SRC_MODIFIED */
674 /* UVC devices are never interlaced, and doing VIDIOC_TRY_FMT on them
675 * causes expensive and slow USB IO, so don't probe them for interlaced
677 if (!strcmp ((char *) v4l2object->vcap.driver, "uvcusb")) {
678 v4l2object->never_interlaced = TRUE;
683 #ifdef GST_EXT_V4L2SRC_MODIFIED
686 if (GST_IS_V4L2SRC(v4l2object->element) && glob_buf.gl_pathc == 0) {
687 if (glob("/dev/video*", 0, 0, &glob_buf) != 0) {
688 GST_WARNING_OBJECT(v4l2object->element, "glob failed");
692 if (glob_buf.gl_pathc > 0 && device_index < glob_buf.gl_pathc) {
693 if (v4l2object->videodev) {
694 g_free(v4l2object->videodev);
696 v4l2object->videodev = g_strdup(glob_buf.gl_pathv[device_index]);
697 if (v4l2object->videodev) {
699 GST_INFO_OBJECT(v4l2object->element, "check device [%s]",
700 v4l2object->videodev);
702 if (GST_V4L2_IS_OPEN (v4l2object)) {
704 v4l2_close (v4l2object->video_fd);
705 v4l2object->video_fd = -1;
708 gst_v4l2_empty_lists (v4l2object);
712 GST_WARNING_OBJECT(v4l2object->element, "g_strdup failed [%s]",
713 glob_buf.gl_pathv[device_index]);
717 GST_WARNING_OBJECT(v4l2object->element, "error type : %d", error_type);
719 switch (error_type) {
720 case V4L2_OPEN_ERROR_STAT_FAILED:
722 case V4L2_OPEN_ERROR_NO_DEVICE:
724 case V4L2_OPEN_ERROR_NOT_OPEN:
726 case V4L2_OPEN_ERROR_NOT_CAPTURE:
728 case V4L2_OPEN_ERROR_NOT_OUTPUT:
734 #endif /* GST_EXT_V4L2SRC_MODIFIED */
739 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
740 (_("Cannot identify device '%s'."), v4l2object->videodev),
746 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
747 (_("This isn't a device '%s'."), v4l2object->videodev),
753 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
754 (_("Could not open device '%s' for reading and writing."),
755 v4l2object->videodev), GST_ERROR_SYSTEM);
760 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
761 (_("Device '%s' is not a capture device."),
762 v4l2object->videodev),
763 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
768 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
769 (_("Device '%s' is not a output device."),
770 v4l2object->videodev),
771 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
776 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
777 (_("Device '%s' is not a M2M device."),
778 v4l2object->videodev),
779 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
784 if (GST_V4L2_IS_OPEN (v4l2object)) {
786 v4l2_close (v4l2object->video_fd);
787 v4l2object->video_fd = -1;
790 gst_v4l2_empty_lists (v4l2object);
792 #ifdef GST_EXT_V4L2SRC_MODIFIED
794 #endif /* GST_EXT_V4L2SRC_MODIFIED */
801 gst_v4l2_dup (GstV4l2Object * v4l2object, GstV4l2Object * other)
803 GST_DEBUG_OBJECT (v4l2object->element, "Trying to dup device %s",
806 GST_V4L2_CHECK_OPEN (other);
807 GST_V4L2_CHECK_NOT_OPEN (v4l2object);
808 GST_V4L2_CHECK_NOT_ACTIVE (other);
809 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
811 v4l2object->vcap = other->vcap;
812 gst_v4l2_adjust_buf_type (v4l2object);
814 v4l2object->video_fd = v4l2_dup (other->video_fd);
815 if (!GST_V4L2_IS_OPEN (v4l2object))
818 g_free (v4l2object->videodev);
819 v4l2object->videodev = g_strdup (other->videodev);
821 GST_INFO_OBJECT (v4l2object->element,
822 "Cloned device '%s' (%s) successfully",
823 v4l2object->vcap.card, v4l2object->videodev);
825 v4l2object->never_interlaced = other->never_interlaced;
831 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
832 (_("Could not dup device '%s' for reading and writing."),
833 v4l2object->videodev), GST_ERROR_SYSTEM);
840 /******************************************************
842 * close the video device (v4l2object->video_fd)
843 * return value: TRUE on success, FALSE on error
844 ******************************************************/
846 gst_v4l2_close (GstV4l2Object * v4l2object)
848 GST_DEBUG_OBJECT (v4l2object->element, "Trying to close %s",
849 v4l2object->videodev);
851 GST_V4L2_CHECK_OPEN (v4l2object);
852 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
855 v4l2_close (v4l2object->video_fd);
856 v4l2object->video_fd = -1;
859 gst_v4l2_empty_lists (v4l2object);
865 /******************************************************
866 * gst_v4l2_get_norm()
867 * Get the norm of the current device
868 * return value: TRUE on success, FALSE on error
869 ******************************************************/
871 gst_v4l2_get_norm (GstV4l2Object * v4l2object, v4l2_std_id * norm)
873 GST_DEBUG_OBJECT (v4l2object->element, "getting norm");
875 if (!GST_V4L2_IS_OPEN (v4l2object))
878 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_STD, norm) < 0)
886 GST_DEBUG ("Failed to get the current norm for device %s",
887 v4l2object->videodev);
893 /******************************************************
894 * gst_v4l2_set_norm()
895 * Set the norm of the current device
896 * return value: TRUE on success, FALSE on error
897 ******************************************************/
899 gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm)
901 GST_DEBUG_OBJECT (v4l2object->element, "trying to set norm to "
902 "%" G_GINT64_MODIFIER "x", (guint64) norm);
904 if (!GST_V4L2_IS_OPEN (v4l2object))
907 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_STD, &norm) < 0)
915 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
916 (_("Failed to set norm for device '%s'."),
917 v4l2object->videodev), GST_ERROR_SYSTEM);
922 /******************************************************
923 * gst_v4l2_get_frequency():
924 * get the current frequency
925 * return value: TRUE on success, FALSE on error
926 ******************************************************/
928 gst_v4l2_get_frequency (GstV4l2Object * v4l2object,
929 gint tunernum, gulong * frequency)
931 struct v4l2_frequency freq = { 0, };
933 GstTunerChannel *channel;
935 GST_DEBUG_OBJECT (v4l2object->element, "getting current tuner frequency");
937 if (!GST_V4L2_IS_OPEN (v4l2object))
940 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
942 freq.tuner = tunernum;
943 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0)
946 *frequency = freq.frequency * channel->freq_multiplicator;
953 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
954 (_("Failed to get current tuner frequency for device '%s'."),
955 v4l2object->videodev), GST_ERROR_SYSTEM);
961 /******************************************************
962 * gst_v4l2_set_frequency():
964 * return value: TRUE on success, FALSE on error
965 ******************************************************/
967 gst_v4l2_set_frequency (GstV4l2Object * v4l2object,
968 gint tunernum, gulong frequency)
970 struct v4l2_frequency freq = { 0, };
972 GstTunerChannel *channel;
974 GST_DEBUG_OBJECT (v4l2object->element,
975 "setting current tuner frequency to %lu", frequency);
977 if (!GST_V4L2_IS_OPEN (v4l2object))
980 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
982 freq.tuner = tunernum;
983 /* fill in type - ignore error */
984 (void) v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq);
985 freq.frequency = frequency / channel->freq_multiplicator;
987 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0)
995 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
996 (_("Failed to set current tuner frequency for device '%s' to %lu Hz."),
997 v4l2object->videodev, frequency), GST_ERROR_SYSTEM);
1002 /******************************************************
1003 * gst_v4l2_signal_strength():
1004 * get the strength of the signal on the current input
1005 * return value: TRUE on success, FALSE on error
1006 ******************************************************/
1008 gst_v4l2_signal_strength (GstV4l2Object * v4l2object,
1009 gint tunernum, gulong * signal_strength)
1011 struct v4l2_tuner tuner = { 0, };
1013 GST_DEBUG_OBJECT (v4l2object->element, "trying to get signal strength");
1015 if (!GST_V4L2_IS_OPEN (v4l2object))
1018 tuner.index = tunernum;
1019 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &tuner) < 0)
1022 *signal_strength = tuner.signal;
1029 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1030 (_("Failed to get signal strength for device '%s'."),
1031 v4l2object->videodev), GST_ERROR_SYSTEM);
1036 /******************************************************
1037 * gst_v4l2_get_attribute():
1038 * try to get the value of one specific attribute
1039 * return value: TRUE on success, FALSE on error
1040 ******************************************************/
1042 gst_v4l2_get_attribute (GstV4l2Object * v4l2object,
1043 int attribute_num, int *value)
1045 struct v4l2_control control = { 0, };
1047 GST_DEBUG_OBJECT (v4l2object->element, "getting value of attribute %d",
1050 if (!GST_V4L2_IS_OPEN (v4l2object))
1053 control.id = attribute_num;
1055 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0)
1058 *value = control.value;
1065 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1066 (_("Failed to get value for control %d on device '%s'."),
1067 attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
1073 /******************************************************
1074 * gst_v4l2_set_attribute():
1075 * try to set the value of one specific attribute
1076 * return value: TRUE on success, FALSE on error
1077 ******************************************************/
1079 gst_v4l2_set_attribute (GstV4l2Object * v4l2object,
1080 int attribute_num, const int value)
1082 struct v4l2_control control = { 0, };
1084 GST_DEBUG_OBJECT (v4l2object->element, "setting value of attribute %d to %d",
1085 attribute_num, value);
1087 if (!GST_V4L2_IS_OPEN (v4l2object))
1090 control.id = attribute_num;
1091 control.value = value;
1092 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0)
1100 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1101 (_("Failed to set value %d for control %d on device '%s'."),
1102 value, attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
1108 set_control (GQuark field_id, const GValue * value, gpointer user_data)
1110 GstV4l2Object *v4l2object = user_data;
1111 GQuark normalised_field_id;
1114 /* 32 bytes is the maximum size for a control name according to v4l2 */
1117 /* Backwards compatibility: in the past GStreamer would normalise strings in
1118 a subtly different way to v4l2-ctl. e.g. the kernel's "Focus (absolute)"
1119 would become "focus__absolute_" whereas now it becomes "focus_absolute".
1120 Please remove the following in GStreamer 1.5 for 1.6 */
1121 strncpy (name, g_quark_to_string (field_id), sizeof (name));
1123 gst_v4l2_normalise_control_name (name);
1124 normalised_field_id = g_quark_from_string (name);
1125 if (normalised_field_id != field_id)
1126 g_warning ("In GStreamer 1.4 the way V4L2 control names were normalised "
1127 "changed. Instead of setting \"%s\" please use \"%s\". The former is "
1128 "deprecated and will be removed in a future version of GStreamer",
1129 g_quark_to_string (field_id), name);
1130 field_id = normalised_field_id;
1132 d = g_datalist_id_get_data (&v4l2object->controls, field_id);
1134 GST_WARNING_OBJECT (v4l2object,
1135 "Control '%s' does not exist or has an unsupported type.",
1136 g_quark_to_string (field_id));
1139 if (!G_VALUE_HOLDS (value, G_TYPE_INT)) {
1140 GST_WARNING_OBJECT (v4l2object,
1141 "'int' value expected for control '%s'.", g_quark_to_string (field_id));
1144 gst_v4l2_set_attribute (v4l2object, GPOINTER_TO_INT (d),
1145 g_value_get_int (value));
1150 gst_v4l2_set_controls (GstV4l2Object * v4l2object, GstStructure * controls)
1152 return gst_structure_foreach (controls, set_control, v4l2object);
1156 gst_v4l2_get_input (GstV4l2Object * v4l2object, gint * input)
1160 GST_DEBUG_OBJECT (v4l2object->element, "trying to get input");
1162 if (!GST_V4L2_IS_OPEN (v4l2object))
1165 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_INPUT, &n) < 0)
1170 GST_DEBUG_OBJECT (v4l2object->element, "input: %d", n);
1176 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1177 /* only give a warning message if driver actually claims to have tuner
1180 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1181 (_("Failed to get current input on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1187 gst_v4l2_set_input (GstV4l2Object * v4l2object, gint input)
1189 GST_DEBUG_OBJECT (v4l2object->element, "trying to set input to %d", input);
1191 if (!GST_V4L2_IS_OPEN (v4l2object))
1194 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_INPUT, &input) < 0)
1201 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1202 /* only give a warning message if driver actually claims to have tuner
1205 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1206 (_("Failed to set input %d on device %s."),
1207 input, v4l2object->videodev), GST_ERROR_SYSTEM);
1213 gst_v4l2_get_output (GstV4l2Object * v4l2object, gint * output)
1217 GST_DEBUG_OBJECT (v4l2object->element, "trying to get output");
1219 if (!GST_V4L2_IS_OPEN (v4l2object))
1222 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_OUTPUT, &n) < 0)
1227 GST_DEBUG_OBJECT (v4l2object->element, "output: %d", n);
1233 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1234 /* only give a warning message if driver actually claims to have tuner
1237 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1238 (_("Failed to get current output on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1244 gst_v4l2_set_output (GstV4l2Object * v4l2object, gint output)
1246 GST_DEBUG_OBJECT (v4l2object->element, "trying to set output to %d", output);
1248 if (!GST_V4L2_IS_OPEN (v4l2object))
1251 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_OUTPUT, &output) < 0)
1258 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1259 /* only give a warning message if driver actually claims to have tuner
1262 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1263 (_("Failed to set output %d on device %s."),
1264 output, v4l2object->videodev), GST_ERROR_SYSTEM);