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 TIZEN_FEATURE_V4L2SRC_MODIFICATION
38 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
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 TIZEN_FEATURE_V4L2SRC_MODIFICATION
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 /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
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 TIZEN_FEATURE_V4L2SRC_MODIFICATION
532 int error_type = V4L2_OPEN_ERROR_STAT_FAILED;
533 int device_index = 0;
536 memset(&glob_buf, 0x0, sizeof(glob_t));
539 GST_ERROR("v4l2object is NULL");
542 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
544 GST_DEBUG_OBJECT (v4l2object->element, "Trying to open device %s",
545 v4l2object->videodev);
547 GST_V4L2_CHECK_NOT_OPEN (v4l2object);
548 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
550 /* be sure we have a device */
551 if (!v4l2object->videodev)
552 v4l2object->videodev = g_strdup ("/dev/video");
554 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
555 if (!v4l2object->videodev) {
556 GST_ERROR_OBJECT(v4l2object->element, "videodev is NULL");
561 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
562 /* check if it is a device */
563 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
564 if (stat (v4l2object->videodev, &st) == -1) {
565 error_type = V4L2_OPEN_ERROR_STAT_FAILED;
566 goto pre_error_check;
568 #else /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
569 if (stat (v4l2object->videodev, &st) == -1)
571 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
573 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
574 if (!S_ISCHR (st.st_mode)) {
575 error_type = V4L2_OPEN_ERROR_NO_DEVICE;
576 goto pre_error_check;
578 #else /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
579 if (!S_ISCHR (st.st_mode))
581 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
583 /* open the device */
584 v4l2object->video_fd =
585 open (v4l2object->videodev, O_RDWR /* | O_NONBLOCK */ );
587 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
588 if (!GST_V4L2_IS_OPEN (v4l2object)) {
589 error_type = V4L2_OPEN_ERROR_NOT_OPEN;
590 goto pre_error_check;
592 #else /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
593 if (!GST_V4L2_IS_OPEN (v4l2object))
595 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
597 libv4l2_fd = v4l2_fd_open (v4l2object->video_fd,
598 V4L2_ENABLE_ENUM_FMT_EMULATION);
599 /* Note the v4l2_xxx functions are designed so that if they get passed an
600 unknown fd, the will behave exactly as their regular xxx counterparts, so
601 if v4l2_fd_open fails, we continue as normal (missing the libv4l2 custom
602 cam format to normal formats conversion). Chances are big we will still
603 fail then though, as normally v4l2_fd_open only fails if the device is not
605 if (libv4l2_fd != -1)
606 v4l2object->video_fd = libv4l2_fd;
608 /* get capabilities, error will be posted */
609 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
610 if (!gst_v4l2_get_capabilities (v4l2object)) {
611 error_type = V4L2_OPEN_ERROR;
612 goto pre_error_check;
614 #else /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
615 if (!gst_v4l2_get_capabilities (v4l2object))
617 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
619 /* do we need to be a capture device? */
620 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
621 GST_INFO_OBJECT(v4l2object->element, "capabilities 0x%x", v4l2object->vcap.capabilities);
622 if (GST_IS_V4L2SRC (v4l2object->element) &&
623 (!(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) ||
624 (v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE)))) {
625 error_type = V4L2_OPEN_ERROR_NOT_CAPTURE;
626 goto pre_error_check;
628 #else /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
629 if (GST_IS_V4L2SRC (v4l2object->element) &&
630 !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_CAPTURE |
631 V4L2_CAP_VIDEO_CAPTURE_MPLANE)))
633 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
635 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
636 if (GST_IS_V4L2SINK (v4l2object->element) &&
637 !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_OUTPUT |
638 V4L2_CAP_VIDEO_OUTPUT_MPLANE))) {
639 error_type = V4L2_OPEN_ERROR_NOT_OUTPUT;
640 goto pre_error_check;
642 #else /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
643 if (GST_IS_V4L2SINK (v4l2object->element) &&
644 !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_OUTPUT |
645 V4L2_CAP_VIDEO_OUTPUT_MPLANE)))
647 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
649 if (GST_IS_V4L2_VIDEO_DEC (v4l2object->element) &&
650 /* Today's M2M device only expose M2M */
651 !((v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_M2M |
652 V4L2_CAP_VIDEO_M2M_MPLANE)) ||
653 /* But legacy driver may expose both CAPTURE and OUTPUT */
654 ((v4l2object->vcap.capabilities &
655 (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) &&
656 (v4l2object->vcap.capabilities &
657 (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE)))))
660 gst_v4l2_adjust_buf_type (v4l2object);
662 /* create enumerations, posts errors. */
663 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
664 if (!gst_v4l2_fill_lists (v4l2object)) {
665 error_type = V4L2_OPEN_ERROR;
666 goto pre_error_check;
668 #else /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
669 if (!gst_v4l2_fill_lists (v4l2object))
671 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
673 GST_INFO_OBJECT (v4l2object->element,
674 "Opened device '%s' (%s) successfully",
675 v4l2object->vcap.card, v4l2object->videodev);
677 if (v4l2object->extra_controls)
678 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
680 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
682 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
684 /* UVC devices are never interlaced, and doing VIDIOC_TRY_FMT on them
685 * causes expensive and slow USB IO, so don't probe them for interlaced
687 if (!strcmp ((char *) v4l2object->vcap.driver, "uvcusb")) {
688 v4l2object->never_interlaced = TRUE;
693 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
696 if (GST_IS_V4L2SRC(v4l2object->element) && glob_buf.gl_pathc == 0) {
697 if (glob("/dev/video*", 0, 0, &glob_buf) != 0) {
698 GST_WARNING_OBJECT(v4l2object->element, "glob failed");
702 if (glob_buf.gl_pathc > 0 && device_index < glob_buf.gl_pathc) {
703 if (v4l2object->videodev) {
704 g_free(v4l2object->videodev);
705 v4l2object->videodev = NULL;
707 v4l2object->videodev = g_strdup(glob_buf.gl_pathv[device_index]);
708 if (v4l2object->videodev) {
710 GST_INFO_OBJECT(v4l2object->element, "check device [%s]",
711 v4l2object->videodev);
713 if (GST_V4L2_IS_OPEN (v4l2object)) {
715 v4l2_close (v4l2object->video_fd);
716 v4l2object->video_fd = -1;
719 gst_v4l2_empty_lists (v4l2object);
723 GST_WARNING_OBJECT(v4l2object->element, "g_strdup failed [%s]",
724 glob_buf.gl_pathv[device_index]);
728 GST_WARNING_OBJECT(v4l2object->element, "error type : %d", error_type);
730 switch (error_type) {
731 case V4L2_OPEN_ERROR_STAT_FAILED:
733 case V4L2_OPEN_ERROR_NO_DEVICE:
735 case V4L2_OPEN_ERROR_NOT_OPEN:
737 case V4L2_OPEN_ERROR_NOT_CAPTURE:
739 case V4L2_OPEN_ERROR_NOT_OUTPUT:
745 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
750 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
751 (_("Cannot identify device '%s'."), v4l2object->videodev),
757 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
758 (_("This isn't a device '%s'."), v4l2object->videodev),
764 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
765 (_("Could not open device '%s' for reading and writing."),
766 v4l2object->videodev), GST_ERROR_SYSTEM);
771 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
772 (_("Device '%s' is not a capture device."),
773 v4l2object->videodev),
774 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
779 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
780 (_("Device '%s' is not a output device."),
781 v4l2object->videodev),
782 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
787 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
788 (_("Device '%s' is not a M2M device."),
789 v4l2object->videodev),
790 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
795 if (GST_V4L2_IS_OPEN (v4l2object)) {
797 v4l2_close (v4l2object->video_fd);
798 v4l2object->video_fd = -1;
801 gst_v4l2_empty_lists (v4l2object);
803 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
805 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
812 gst_v4l2_dup (GstV4l2Object * v4l2object, GstV4l2Object * other)
814 GST_DEBUG_OBJECT (v4l2object->element, "Trying to dup device %s",
817 GST_V4L2_CHECK_OPEN (other);
818 GST_V4L2_CHECK_NOT_OPEN (v4l2object);
819 GST_V4L2_CHECK_NOT_ACTIVE (other);
820 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
822 v4l2object->vcap = other->vcap;
823 gst_v4l2_adjust_buf_type (v4l2object);
825 v4l2object->video_fd = v4l2_dup (other->video_fd);
826 if (!GST_V4L2_IS_OPEN (v4l2object))
829 g_free (v4l2object->videodev);
830 v4l2object->videodev = g_strdup (other->videodev);
832 GST_INFO_OBJECT (v4l2object->element,
833 "Cloned device '%s' (%s) successfully",
834 v4l2object->vcap.card, v4l2object->videodev);
836 v4l2object->never_interlaced = other->never_interlaced;
842 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
843 (_("Could not dup device '%s' for reading and writing."),
844 v4l2object->videodev), GST_ERROR_SYSTEM);
851 /******************************************************
853 * close the video device (v4l2object->video_fd)
854 * return value: TRUE on success, FALSE on error
855 ******************************************************/
857 gst_v4l2_close (GstV4l2Object * v4l2object)
859 GST_DEBUG_OBJECT (v4l2object->element, "Trying to close %s",
860 v4l2object->videodev);
862 GST_V4L2_CHECK_OPEN (v4l2object);
863 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
866 v4l2_close (v4l2object->video_fd);
867 v4l2object->video_fd = -1;
870 gst_v4l2_empty_lists (v4l2object);
876 /******************************************************
877 * gst_v4l2_get_norm()
878 * Get the norm of the current device
879 * return value: TRUE on success, FALSE on error
880 ******************************************************/
882 gst_v4l2_get_norm (GstV4l2Object * v4l2object, v4l2_std_id * norm)
884 GST_DEBUG_OBJECT (v4l2object->element, "getting norm");
886 if (!GST_V4L2_IS_OPEN (v4l2object))
889 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_STD, norm) < 0)
897 GST_DEBUG ("Failed to get the current norm for device %s",
898 v4l2object->videodev);
904 /******************************************************
905 * gst_v4l2_set_norm()
906 * Set the norm of the current device
907 * return value: TRUE on success, FALSE on error
908 ******************************************************/
910 gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm)
912 GST_DEBUG_OBJECT (v4l2object->element, "trying to set norm to "
913 "%" G_GINT64_MODIFIER "x", (guint64) norm);
915 if (!GST_V4L2_IS_OPEN (v4l2object))
918 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_STD, &norm) < 0)
926 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
927 (_("Failed to set norm for device '%s'."),
928 v4l2object->videodev), GST_ERROR_SYSTEM);
933 /******************************************************
934 * gst_v4l2_get_frequency():
935 * get the current frequency
936 * return value: TRUE on success, FALSE on error
937 ******************************************************/
939 gst_v4l2_get_frequency (GstV4l2Object * v4l2object,
940 gint tunernum, gulong * frequency)
942 struct v4l2_frequency freq = { 0, };
944 GstTunerChannel *channel;
946 GST_DEBUG_OBJECT (v4l2object->element, "getting current tuner frequency");
948 if (!GST_V4L2_IS_OPEN (v4l2object))
951 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
953 freq.tuner = tunernum;
954 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0)
957 *frequency = freq.frequency * channel->freq_multiplicator;
964 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
965 (_("Failed to get current tuner frequency for device '%s'."),
966 v4l2object->videodev), GST_ERROR_SYSTEM);
972 /******************************************************
973 * gst_v4l2_set_frequency():
975 * return value: TRUE on success, FALSE on error
976 ******************************************************/
978 gst_v4l2_set_frequency (GstV4l2Object * v4l2object,
979 gint tunernum, gulong frequency)
981 struct v4l2_frequency freq = { 0, };
983 GstTunerChannel *channel;
985 GST_DEBUG_OBJECT (v4l2object->element,
986 "setting current tuner frequency to %lu", frequency);
988 if (!GST_V4L2_IS_OPEN (v4l2object))
991 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
993 freq.tuner = tunernum;
994 /* fill in type - ignore error */
995 (void) v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq);
996 freq.frequency = frequency / channel->freq_multiplicator;
998 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0)
1006 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1007 (_("Failed to set current tuner frequency for device '%s' to %lu Hz."),
1008 v4l2object->videodev, frequency), GST_ERROR_SYSTEM);
1013 /******************************************************
1014 * gst_v4l2_signal_strength():
1015 * get the strength of the signal on the current input
1016 * return value: TRUE on success, FALSE on error
1017 ******************************************************/
1019 gst_v4l2_signal_strength (GstV4l2Object * v4l2object,
1020 gint tunernum, gulong * signal_strength)
1022 struct v4l2_tuner tuner = { 0, };
1024 GST_DEBUG_OBJECT (v4l2object->element, "trying to get signal strength");
1026 if (!GST_V4L2_IS_OPEN (v4l2object))
1029 tuner.index = tunernum;
1030 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &tuner) < 0)
1033 *signal_strength = tuner.signal;
1040 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1041 (_("Failed to get signal strength for device '%s'."),
1042 v4l2object->videodev), GST_ERROR_SYSTEM);
1047 /******************************************************
1048 * gst_v4l2_get_attribute():
1049 * try to get the value of one specific attribute
1050 * return value: TRUE on success, FALSE on error
1051 ******************************************************/
1053 gst_v4l2_get_attribute (GstV4l2Object * v4l2object,
1054 int attribute_num, int *value)
1056 struct v4l2_control control = { 0, };
1058 GST_DEBUG_OBJECT (v4l2object->element, "getting value of attribute %d",
1061 if (!GST_V4L2_IS_OPEN (v4l2object))
1064 control.id = attribute_num;
1066 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0)
1069 *value = control.value;
1076 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1077 (_("Failed to get value for control %d on device '%s'."),
1078 attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
1084 /******************************************************
1085 * gst_v4l2_set_attribute():
1086 * try to set the value of one specific attribute
1087 * return value: TRUE on success, FALSE on error
1088 ******************************************************/
1090 gst_v4l2_set_attribute (GstV4l2Object * v4l2object,
1091 int attribute_num, const int value)
1093 struct v4l2_control control = { 0, };
1095 GST_DEBUG_OBJECT (v4l2object->element, "setting value of attribute %d to %d",
1096 attribute_num, value);
1098 if (!GST_V4L2_IS_OPEN (v4l2object))
1101 control.id = attribute_num;
1102 control.value = value;
1103 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0)
1111 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1112 (_("Failed to set value %d for control %d on device '%s'."),
1113 value, attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
1119 set_control (GQuark field_id, const GValue * value, gpointer user_data)
1121 GstV4l2Object *v4l2object = user_data;
1122 GQuark normalised_field_id;
1125 /* 32 bytes is the maximum size for a control name according to v4l2 */
1128 /* Backwards compatibility: in the past GStreamer would normalise strings in
1129 a subtly different way to v4l2-ctl. e.g. the kernel's "Focus (absolute)"
1130 would become "focus__absolute_" whereas now it becomes "focus_absolute".
1131 Please remove the following in GStreamer 1.5 for 1.6 */
1132 strncpy (name, g_quark_to_string (field_id), sizeof (name));
1134 gst_v4l2_normalise_control_name (name);
1135 normalised_field_id = g_quark_from_string (name);
1136 if (normalised_field_id != field_id)
1137 g_warning ("In GStreamer 1.4 the way V4L2 control names were normalised "
1138 "changed. Instead of setting \"%s\" please use \"%s\". The former is "
1139 "deprecated and will be removed in a future version of GStreamer",
1140 g_quark_to_string (field_id), name);
1141 field_id = normalised_field_id;
1143 d = g_datalist_id_get_data (&v4l2object->controls, field_id);
1145 GST_WARNING_OBJECT (v4l2object,
1146 "Control '%s' does not exist or has an unsupported type.",
1147 g_quark_to_string (field_id));
1150 if (!G_VALUE_HOLDS (value, G_TYPE_INT)) {
1151 GST_WARNING_OBJECT (v4l2object,
1152 "'int' value expected for control '%s'.", g_quark_to_string (field_id));
1155 gst_v4l2_set_attribute (v4l2object, GPOINTER_TO_INT (d),
1156 g_value_get_int (value));
1161 gst_v4l2_set_controls (GstV4l2Object * v4l2object, GstStructure * controls)
1163 return gst_structure_foreach (controls, set_control, v4l2object);
1167 gst_v4l2_get_input (GstV4l2Object * v4l2object, gint * input)
1171 GST_DEBUG_OBJECT (v4l2object->element, "trying to get input");
1173 if (!GST_V4L2_IS_OPEN (v4l2object))
1176 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_INPUT, &n) < 0)
1181 GST_DEBUG_OBJECT (v4l2object->element, "input: %d", n);
1187 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1188 /* only give a warning message if driver actually claims to have tuner
1191 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1192 (_("Failed to get current input on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1198 gst_v4l2_set_input (GstV4l2Object * v4l2object, gint input)
1200 GST_DEBUG_OBJECT (v4l2object->element, "trying to set input to %d", input);
1202 if (!GST_V4L2_IS_OPEN (v4l2object))
1205 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_INPUT, &input) < 0)
1212 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1213 /* only give a warning message if driver actually claims to have tuner
1216 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1217 (_("Failed to set input %d on device %s."),
1218 input, v4l2object->videodev), GST_ERROR_SYSTEM);
1224 gst_v4l2_get_output (GstV4l2Object * v4l2object, gint * output)
1228 GST_DEBUG_OBJECT (v4l2object->element, "trying to get output");
1230 if (!GST_V4L2_IS_OPEN (v4l2object))
1233 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_OUTPUT, &n) < 0)
1238 GST_DEBUG_OBJECT (v4l2object->element, "output: %d", n);
1244 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1245 /* only give a warning message if driver actually claims to have tuner
1248 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1249 (_("Failed to get current output on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1255 gst_v4l2_set_output (GstV4l2Object * v4l2object, gint output)
1257 GST_DEBUG_OBJECT (v4l2object->element, "trying to set output to %d", output);
1259 if (!GST_V4L2_IS_OPEN (v4l2object))
1262 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_OUTPUT, &output) < 0)
1269 if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1270 /* only give a warning message if driver actually claims to have tuner
1273 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1274 (_("Failed to set output %d on device %s."),
1275 output, v4l2object->videodev), GST_ERROR_SYSTEM);