3 * Copyright (C) 2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@gmail.com>
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_AUTO_SCAN_DEVICE_NODE
38 #endif /* TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE */
40 /* Needed on older Solaris Nevada builds (72 at least) */
42 #include <sys/ioccom.h>
44 #include "gstv4l2object.h"
45 #include "gstv4l2tuner.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 #ifdef TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE
57 V4L2_OPEN_ERROR_STAT_FAILED,
58 V4L2_OPEN_ERROR_NO_DEVICE,
59 V4L2_OPEN_ERROR_NOT_OPEN,
60 V4L2_OPEN_ERROR_NOT_CAPTURE,
61 V4L2_OPEN_ERROR_NOT_OUTPUT
63 #endif /* TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE */
65 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
66 #define GST_CAT_DEFAULT v4l2_debug
68 /******************************************************
69 * gst_v4l2_get_capabilities():
70 * get the device's capturing capabilities
71 * return value: TRUE on success, FALSE on error
72 ******************************************************/
74 gst_v4l2_get_capabilities (GstV4l2Object * v4l2object)
78 e = v4l2object->element;
80 GST_DEBUG_OBJECT (e, "getting capabilities");
82 if (!GST_V4L2_IS_OPEN (v4l2object))
85 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_QUERYCAP,
86 &v4l2object->vcap) < 0)
89 if (v4l2object->vcap.capabilities & V4L2_CAP_DEVICE_CAPS)
90 v4l2object->device_caps = v4l2object->vcap.device_caps;
92 v4l2object->device_caps = v4l2object->vcap.capabilities;
94 GST_LOG_OBJECT (e, "driver: '%s'", v4l2object->vcap.driver);
95 GST_LOG_OBJECT (e, "card: '%s'", v4l2object->vcap.card);
96 GST_LOG_OBJECT (e, "bus_info: '%s'", v4l2object->vcap.bus_info);
97 GST_LOG_OBJECT (e, "version: %08x", v4l2object->vcap.version);
98 GST_LOG_OBJECT (e, "capabilites: %08x", v4l2object->device_caps);
105 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
106 (_("Error getting capabilities for device '%s': "
107 "It isn't a v4l2 driver. Check if it is a v4l1 driver."),
108 v4l2object->videodev), GST_ERROR_SYSTEM);
113 /******************************************************
114 * The video4linux command line tool v4l2-ctrl
115 * normalises the names of the controls received from
118 * "Exposure (absolute)" -> "exposure_absolute"
120 * We follow their lead here. @name is modified
122 ******************************************************/
124 gst_v4l2_normalise_control_name (gchar * name)
127 for (i = 0, j = 0; name[j]; ++j) {
128 if (g_ascii_isalnum (name[j])) {
129 if (i > 0 && !g_ascii_isalnum (name[j - 1]))
131 name[i++] = g_ascii_tolower (name[j]);
137 /******************************************************
138 * gst_v4l2_empty_lists() and gst_v4l2_fill_lists():
139 * fill/empty the lists of enumerations
140 * return value: TRUE on success, FALSE on error
141 ******************************************************/
143 gst_v4l2_fill_lists (GstV4l2Object * v4l2object)
146 struct v4l2_queryctrl control = { 0, };
150 e = v4l2object->element;
152 GST_DEBUG_OBJECT (e, "getting enumerations");
153 GST_V4L2_CHECK_OPEN (v4l2object);
155 GST_DEBUG_OBJECT (e, " channels");
156 /* and now, the channels */
158 struct v4l2_input input;
159 GstV4l2TunerChannel *v4l2channel;
160 GstTunerChannel *channel;
162 memset (&input, 0, sizeof (input));
165 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_ENUMINPUT, &input) < 0) {
166 if (errno == EINVAL || errno == ENOTTY)
167 break; /* end of enumeration */
169 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
170 (_("Failed to query attributes of input %d in device %s"),
171 n, v4l2object->videodev),
172 ("Failed to get %d in input enumeration for %s. (%d - %s)",
173 n, v4l2object->videodev, errno, strerror (errno)));
178 GST_LOG_OBJECT (e, " index: %d", input.index);
179 GST_LOG_OBJECT (e, " name: '%s'", input.name);
180 GST_LOG_OBJECT (e, " type: %08x", input.type);
181 GST_LOG_OBJECT (e, " audioset: %08x", input.audioset);
182 GST_LOG_OBJECT (e, " std: %016" G_GINT64_MODIFIER "x",
183 (guint64) input.std);
184 GST_LOG_OBJECT (e, " status: %08x", input.status);
186 v4l2channel = g_object_new (GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
187 channel = GST_TUNER_CHANNEL (v4l2channel);
188 channel->label = g_strdup ((const gchar *) input.name);
189 channel->flags = GST_TUNER_CHANNEL_INPUT;
190 v4l2channel->index = n;
192 if (input.type == V4L2_INPUT_TYPE_TUNER) {
193 struct v4l2_tuner vtun;
195 v4l2channel->tuner = input.tuner;
196 channel->flags |= GST_TUNER_CHANNEL_FREQUENCY;
198 vtun.index = input.tuner;
199 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &vtun) < 0) {
200 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
201 (_("Failed to get setting of tuner %d on device '%s'."),
202 input.tuner, v4l2object->videodev), GST_ERROR_SYSTEM);
203 g_object_unref (G_OBJECT (channel));
207 channel->freq_multiplicator =
208 62.5 * ((vtun.capability & V4L2_TUNER_CAP_LOW) ? 1 : 1000);
209 channel->min_frequency = vtun.rangelow * channel->freq_multiplicator;
210 channel->max_frequency = vtun.rangehigh * channel->freq_multiplicator;
211 channel->min_signal = 0;
212 channel->max_signal = 0xffff;
214 if (input.audioset) {
215 /* we take the first. We don't care for
216 * the others for now */
217 while (!(input.audioset & (1 << v4l2channel->audio)))
218 v4l2channel->audio++;
219 channel->flags |= GST_TUNER_CHANNEL_AUDIO;
222 v4l2object->channels =
223 g_list_prepend (v4l2object->channels, (gpointer) channel);
225 v4l2object->channels = g_list_reverse (v4l2object->channels);
227 GST_DEBUG_OBJECT (e, " norms");
230 struct v4l2_standard standard = { 0, };
231 GstV4l2TunerNorm *v4l2norm;
235 /* fill in defaults */
236 standard.frameperiod.numerator = 1;
237 standard.frameperiod.denominator = 0;
240 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_ENUMSTD, &standard) < 0) {
241 if (errno == EINVAL || errno == ENOTTY)
242 break; /* end of enumeration */
244 else if (errno == ENODATA)
245 break; /* end of enumeration, as of Linux 3.7-rc1 */
248 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
249 (_("Failed to query norm on device '%s'."),
250 v4l2object->videodev),
251 ("Failed to get attributes for norm %d on devide '%s'. (%d - %s)",
252 n, v4l2object->videodev, errno, strerror (errno)));
257 GST_DEBUG_OBJECT (e, " '%s', fps: %d / %d",
258 standard.name, standard.frameperiod.denominator,
259 standard.frameperiod.numerator);
261 v4l2norm = g_object_new (GST_TYPE_V4L2_TUNER_NORM, NULL);
262 norm = GST_TUNER_NORM (v4l2norm);
263 norm->label = g_strdup ((const gchar *) standard.name);
264 gst_value_set_fraction (&norm->framerate,
265 standard.frameperiod.denominator, standard.frameperiod.numerator);
266 v4l2norm->index = standard.id;
268 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "index=%08x, label=%s",
269 (unsigned int) v4l2norm->index, norm->label);
271 v4l2object->norms = g_list_prepend (v4l2object->norms, (gpointer) norm);
273 v4l2object->norms = g_list_reverse (v4l2object->norms);
275 GST_DEBUG_OBJECT (e, " controls+menus");
277 /* and lastly, controls+menus (if appropriate) */
278 next = V4L2_CTRL_FLAG_NEXT_CTRL;
283 GstV4l2ColorBalanceChannel *v4l2channel;
284 GstColorBalanceChannel *channel;
290 /* when we reached the last official CID, continue with private CIDs */
291 if (n == V4L2_CID_LASTP1) {
292 GST_DEBUG_OBJECT (e, "checking private CIDs");
293 n = V4L2_CID_PRIVATE_BASE;
295 GST_DEBUG_OBJECT (e, "checking control %08x", n);
297 control.id = n | next;
298 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_QUERYCTRL,
302 GST_DEBUG_OBJECT (e, "controls finished");
305 GST_DEBUG_OBJECT (e, "V4L2_CTRL_FLAG_NEXT_CTRL not supported.");
311 if (errno == EINVAL || errno == ENOTTY || errno == EIO || errno == ENOENT) {
312 if (n < V4L2_CID_PRIVATE_BASE) {
313 GST_DEBUG_OBJECT (e, "skipping control %08x", n);
314 /* continue so that we also check private controls */
315 n = V4L2_CID_PRIVATE_BASE - 1;
318 GST_DEBUG_OBJECT (e, "controls finished");
322 GST_WARNING_OBJECT (e, "Failed querying control %d on device '%s'. "
323 "(%d - %s)", n, v4l2object->videodev, errno, strerror (errno));
327 /* bogus driver might mess with id in unexpected ways (e.g. set to 0), so
328 * make sure to simply try all if V4L2_CTRL_FLAG_NEXT_CTRL not supported */
331 if (control.flags & V4L2_CTRL_FLAG_DISABLED) {
332 GST_DEBUG_OBJECT (e, "skipping disabled control");
336 if (control.type == V4L2_CTRL_TYPE_CTRL_CLASS) {
337 GST_DEBUG_OBJECT (e, "starting control class '%s'", control.name);
341 switch (control.type) {
342 case V4L2_CTRL_TYPE_INTEGER:
343 case V4L2_CTRL_TYPE_BOOLEAN:
344 case V4L2_CTRL_TYPE_MENU:
345 case V4L2_CTRL_TYPE_INTEGER_MENU:
346 case V4L2_CTRL_TYPE_BITMASK:
347 case V4L2_CTRL_TYPE_BUTTON:{
348 control.name[31] = '\0';
349 gst_v4l2_normalise_control_name ((gchar *) control.name);
350 g_datalist_id_set_data (&v4l2object->controls,
351 g_quark_from_string ((const gchar *) control.name),
352 GINT_TO_POINTER (n));
357 "Control type for '%s' not supported for extra controls.",
363 case V4L2_CID_BRIGHTNESS:
364 case V4L2_CID_CONTRAST:
365 case V4L2_CID_SATURATION:
367 case V4L2_CID_BLACK_LEVEL:
368 case V4L2_CID_AUTO_WHITE_BALANCE:
369 case V4L2_CID_DO_WHITE_BALANCE:
370 case V4L2_CID_RED_BALANCE:
371 case V4L2_CID_BLUE_BALANCE:
373 case V4L2_CID_EXPOSURE:
374 case V4L2_CID_AUTOGAIN:
376 case V4L2_CID_SHARPNESS:
377 #ifdef TIZEN_FEATURE_V4L2_ADDITIONAL_CID_SUPPORT
378 case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
379 case V4L2_CID_EXPOSURE_AUTO:
380 case V4L2_CID_EXPOSURE_ABSOLUTE:
381 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
382 #endif /* TIZEN_FEATURE_V4L2_ADDITIONAL_CID_SUPPORT */
383 /* we only handle these for now (why?) */
387 case V4L2_CID_PAN_RESET:
388 case V4L2_CID_TILT_RESET:
389 /* not handled here, handled by VideoOrientation interface */
392 case V4L2_CID_AUDIO_VOLUME:
393 case V4L2_CID_AUDIO_BALANCE:
394 case V4L2_CID_AUDIO_BASS:
395 case V4L2_CID_AUDIO_TREBLE:
396 case V4L2_CID_AUDIO_MUTE:
397 case V4L2_CID_AUDIO_LOUDNESS:
398 /* FIXME: We should implement GstMixer interface instead */
399 /* but let's not be pedantic and make element more useful for now */
401 case V4L2_CID_ALPHA_COMPONENT:
402 v4l2object->has_alpha_component = TRUE;
406 "ControlID %s (%x) unhandled, FIXME", control.name, n);
413 GST_DEBUG_OBJECT (e, "Adding ControlID %s (%x)", control.name, n);
414 v4l2channel = g_object_new (GST_TYPE_V4L2_COLOR_BALANCE_CHANNEL, NULL);
415 channel = GST_COLOR_BALANCE_CHANNEL (v4l2channel);
416 channel->label = g_strdup ((const gchar *) control.name);
420 /* FIXME: it will be need just when handling private controls
421 *(currently none of base controls are of this type) */
422 if (control.type == V4L2_CTRL_TYPE_MENU) {
423 struct v4l2_querymenu menu, *mptr;
430 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_QUERYMENU,
433 break; /* end of enumeration */
435 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
436 (_("Failed getting controls attributes on device '%s'."),
437 v4l2object->videodev),
438 ("Failed to get %d in menu enumeration for %s. (%d - %s)",
439 n, v4l2object->videodev, errno, strerror (errno)));
443 mptr = g_malloc (sizeof (menu));
444 memcpy (mptr, &menu, sizeof (menu));
445 menus = g_list_append (menus, mptr);
448 v4l2object->menus = g_list_append (v4l2object->menus, menus);
451 switch (control.type) {
452 case V4L2_CTRL_TYPE_INTEGER:
453 channel->min_value = control.minimum;
454 channel->max_value = control.maximum;
456 case V4L2_CTRL_TYPE_BOOLEAN:
457 channel->min_value = FALSE;
458 channel->max_value = TRUE;
461 /* FIXME we should find out how to handle V4L2_CTRL_TYPE_BUTTON.
462 BUTTON controls like V4L2_CID_DO_WHITE_BALANCE can just be set (1) or
463 unset (0), but can't be queried */
465 "Control with non supported type %s (%x), type=%d",
466 control.name, n, control.type);
467 channel->min_value = channel->max_value = 0;
472 g_list_prepend (v4l2object->colors, (gpointer) channel);
474 v4l2object->colors = g_list_reverse (v4l2object->colors);
476 GST_DEBUG_OBJECT (e, "done");
482 gst_v4l2_empty_lists (GstV4l2Object * v4l2object)
484 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "deleting enumerations");
486 g_list_foreach (v4l2object->channels, (GFunc) g_object_unref, NULL);
487 g_list_free (v4l2object->channels);
488 v4l2object->channels = NULL;
490 g_list_foreach (v4l2object->norms, (GFunc) g_object_unref, NULL);
491 g_list_free (v4l2object->norms);
492 v4l2object->norms = NULL;
494 g_list_foreach (v4l2object->colors, (GFunc) g_object_unref, NULL);
495 g_list_free (v4l2object->colors);
496 v4l2object->colors = NULL;
498 g_datalist_clear (&v4l2object->controls);
502 gst_v4l2_adjust_buf_type (GstV4l2Object * v4l2object)
504 /* when calling gst_v4l2_object_new the user decides the initial type
505 * so adjust it if multi-planar is supported
506 * the driver should make it exclusive. So the driver should
507 * not support both MPLANE and non-PLANE.
508 * Because even when using MPLANE it still possibles to use it
509 * in a contiguous manner. In this case the first v4l2 plane
510 * contains all the gst planes.
512 switch (v4l2object->type) {
513 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
514 if (v4l2object->device_caps &
515 (V4L2_CAP_VIDEO_OUTPUT_MPLANE | V4L2_CAP_VIDEO_M2M_MPLANE)) {
516 GST_DEBUG ("adjust type to multi-planar output");
517 v4l2object->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
520 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
521 if (v4l2object->device_caps &
522 (V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_M2M_MPLANE)) {
523 GST_DEBUG ("adjust type to multi-planar capture");
524 v4l2object->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
532 /******************************************************
534 * open the video device (v4l2object->videodev)
535 * return value: TRUE on success, FALSE on error
536 ******************************************************/
538 gst_v4l2_open (GstV4l2Object * v4l2object)
542 #ifdef TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE
543 int error_type = V4L2_OPEN_ERROR_STAT_FAILED;
544 int device_index = 0;
547 memset (&glob_buf, 0x0, sizeof(glob_t));
550 GST_ERROR ("v4l2object is NULL");
553 #endif /* TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE */
554 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Trying to open device %s",
555 v4l2object->videodev);
557 GST_V4L2_CHECK_NOT_OPEN (v4l2object);
558 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
560 /* be sure we have a device */
561 if (!v4l2object->videodev)
562 v4l2object->videodev = g_strdup ("/dev/video");
564 #ifdef TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE
565 if (!v4l2object->videodev) {
566 GST_ERROR_OBJECT (v4l2object->element, "videodev is NULL");
571 /* check if it is a device */
572 if (stat (v4l2object->videodev, &st) == -1) {
573 error_type = V4L2_OPEN_ERROR_STAT_FAILED;
574 goto pre_error_check;
577 if (!S_ISCHR (st.st_mode)) {
578 error_type = V4L2_OPEN_ERROR_NO_DEVICE;
579 goto pre_error_check;
581 #else /* TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE */
582 /* check if it is a device */
583 if (stat (v4l2object->videodev, &st) == -1)
586 if (!S_ISCHR (st.st_mode))
588 #endif /* TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE */
590 /* open the device */
591 v4l2object->video_fd =
592 open (v4l2object->videodev, O_RDWR /* | O_NONBLOCK */ );
594 #ifdef TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE
595 if (!GST_V4L2_IS_OPEN (v4l2object)) {
596 error_type = V4L2_OPEN_ERROR_NOT_OPEN;
597 goto pre_error_check;
599 #else /* TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE */
600 if (!GST_V4L2_IS_OPEN (v4l2object))
602 #endif /* TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE */
605 if (v4l2object->fd_open)
606 libv4l2_fd = v4l2object->fd_open (v4l2object->video_fd,
607 V4L2_ENABLE_ENUM_FMT_EMULATION);
610 /* Note the v4l2_xxx functions are designed so that if they get passed an
611 unknown fd, the will behave exactly as their regular xxx counterparts, so
612 if v4l2_fd_open fails, we continue as normal (missing the libv4l2 custom
613 cam format to normal formats conversion). Chances are big we will still
614 fail then though, as normally v4l2_fd_open only fails if the device is not
616 if (libv4l2_fd != -1)
617 v4l2object->video_fd = libv4l2_fd;
619 /* get capabilities, error will be posted */
620 #ifdef TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE
621 if (!gst_v4l2_get_capabilities (v4l2object)) {
622 error_type = V4L2_OPEN_ERROR;
623 goto pre_error_check;
626 GST_INFO_OBJECT (v4l2object->element, "device_caps 0x%x", v4l2object->device_caps);
628 if (GST_IS_V4L2SRC (v4l2object->element) &&
629 (!(v4l2object->device_caps & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) ||
630 (v4l2object->device_caps & (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE)))) {
631 error_type = V4L2_OPEN_ERROR_NOT_CAPTURE;
632 goto pre_error_check;
635 if (GST_IS_V4L2SINK (v4l2object->element) &&
636 !(v4l2object->device_caps & (V4L2_CAP_VIDEO_OUTPUT |
637 V4L2_CAP_VIDEO_OUTPUT_MPLANE))) {
638 error_type = V4L2_OPEN_ERROR_NOT_OUTPUT;
639 goto pre_error_check;
641 #else /* TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE */
642 if (!gst_v4l2_get_capabilities (v4l2object))
645 /* do we need to be a capture device? */
646 if (GST_IS_V4L2SRC (v4l2object->element) &&
647 !(v4l2object->device_caps & (V4L2_CAP_VIDEO_CAPTURE |
648 V4L2_CAP_VIDEO_CAPTURE_MPLANE)))
651 if (GST_IS_V4L2SINK (v4l2object->element) &&
652 !(v4l2object->device_caps & (V4L2_CAP_VIDEO_OUTPUT |
653 V4L2_CAP_VIDEO_OUTPUT_MPLANE)))
655 #endif /* TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE */
657 if (GST_IS_V4L2_VIDEO_DEC (v4l2object->element) &&
658 !GST_V4L2_IS_M2M (v4l2object->device_caps))
661 gst_v4l2_adjust_buf_type (v4l2object);
663 /* create enumerations, posts errors. */
664 #ifdef TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE
665 if (!gst_v4l2_fill_lists (v4l2object)) {
666 error_type = V4L2_OPEN_ERROR;
667 goto pre_error_check;
669 #else /* TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE */
670 if (!gst_v4l2_fill_lists (v4l2object))
672 #endif /* TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE */
674 GST_INFO_OBJECT (v4l2object->dbg_obj,
675 "Opened device '%s' (%s) successfully",
676 v4l2object->vcap.card, v4l2object->videodev);
678 if (v4l2object->extra_controls)
679 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
681 #ifdef TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE
682 globfree (&glob_buf);
683 #endif /* TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE */
685 /* UVC devices are never interlaced, and doing VIDIOC_TRY_FMT on them
686 * causes expensive and slow USB IO, so don't probe them for interlaced
688 if (!strcmp ((char *) v4l2object->vcap.driver, "uvcusb") ||
689 !strcmp ((char *) v4l2object->vcap.driver, "uvcvideo")) {
690 v4l2object->never_interlaced = TRUE;
695 #ifdef TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE
698 if (v4l2object->auto_scan_device == FALSE) {
699 GST_WARNING_OBJECT (v4l2object->element, "auto scan device disabled");
703 GST_ERROR_OBJECT (v4l2object->element, "device[%s] failed, error[%d]",
704 v4l2object->videodev, error_type);
706 if (GST_IS_V4L2SRC (v4l2object->element) && glob_buf.gl_pathc == 0) {
707 if (glob("/dev/video*", 0, 0, &glob_buf) != 0) {
708 GST_WARNING_OBJECT (v4l2object->element, "glob failed");
712 if (glob_buf.gl_pathc > 0 && device_index < glob_buf.gl_pathc) {
713 if (v4l2object->videodev)
714 g_free (v4l2object->videodev);
716 v4l2object->videodev = g_strdup (glob_buf.gl_pathv[device_index]);
717 if (v4l2object->videodev) {
720 GST_INFO_OBJECT (v4l2object->element, "check device [%s]",
721 v4l2object->videodev);
723 if (GST_V4L2_IS_OPEN (v4l2object)) {
725 v4l2object->close (v4l2object->video_fd);
726 v4l2object->video_fd = -1;
729 gst_v4l2_empty_lists (v4l2object);
733 GST_WARNING_OBJECT (v4l2object->element, "g_strdup failed [%s]",
734 glob_buf.gl_pathv[device_index]);
738 switch (error_type) {
739 case V4L2_OPEN_ERROR_STAT_FAILED:
741 case V4L2_OPEN_ERROR_NO_DEVICE:
743 case V4L2_OPEN_ERROR_NOT_OPEN:
745 case V4L2_OPEN_ERROR_NOT_CAPTURE:
747 case V4L2_OPEN_ERROR_NOT_OUTPUT:
753 #endif /* TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE */
758 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
759 (_("Cannot identify device '%s'."), v4l2object->videodev),
765 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
766 (_("This isn't a device '%s'."), v4l2object->videodev),
772 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
773 (_("Could not open device '%s' for reading and writing."),
774 v4l2object->videodev), GST_ERROR_SYSTEM);
779 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
780 (_("Device '%s' is not a capture device."),
781 v4l2object->videodev),
782 ("Capabilities: 0x%x", v4l2object->device_caps));
787 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
788 (_("Device '%s' is not a output device."),
789 v4l2object->videodev),
790 ("Capabilities: 0x%x", v4l2object->device_caps));
795 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
796 (_("Device '%s' is not a M2M device."),
797 v4l2object->videodev),
798 ("Capabilities: 0x%x", v4l2object->device_caps));
803 if (GST_V4L2_IS_OPEN (v4l2object)) {
805 v4l2object->close (v4l2object->video_fd);
806 v4l2object->video_fd = -1;
809 gst_v4l2_empty_lists (v4l2object);
811 #ifdef TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE
812 globfree (&glob_buf);
813 #endif /* TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE */
820 gst_v4l2_dup (GstV4l2Object * v4l2object, GstV4l2Object * other)
822 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Trying to dup device %s",
825 GST_V4L2_CHECK_OPEN (other);
826 GST_V4L2_CHECK_NOT_OPEN (v4l2object);
827 GST_V4L2_CHECK_NOT_ACTIVE (other);
828 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
830 v4l2object->vcap = other->vcap;
831 v4l2object->device_caps = other->device_caps;
832 gst_v4l2_adjust_buf_type (v4l2object);
834 v4l2object->video_fd = v4l2object->dup (other->video_fd);
835 if (!GST_V4L2_IS_OPEN (v4l2object))
838 g_free (v4l2object->videodev);
839 v4l2object->videodev = g_strdup (other->videodev);
841 GST_INFO_OBJECT (v4l2object->dbg_obj,
842 "Cloned device '%s' (%s) successfully",
843 v4l2object->vcap.card, v4l2object->videodev);
845 v4l2object->never_interlaced = other->never_interlaced;
846 v4l2object->no_initial_format = other->no_initial_format;
852 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
853 (_("Could not dup device '%s' for reading and writing."),
854 v4l2object->videodev), GST_ERROR_SYSTEM);
861 /******************************************************
863 * close the video device (v4l2object->video_fd)
864 * return value: TRUE on success, FALSE on error
865 ******************************************************/
867 gst_v4l2_close (GstV4l2Object * v4l2object)
869 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Trying to close %s",
870 v4l2object->videodev);
872 GST_V4L2_CHECK_OPEN (v4l2object);
873 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
876 v4l2object->close (v4l2object->video_fd);
877 v4l2object->video_fd = -1;
880 gst_v4l2_empty_lists (v4l2object);
886 /******************************************************
887 * gst_v4l2_get_norm()
888 * Get the norm of the current device
889 * return value: TRUE on success, FALSE on error
890 ******************************************************/
892 gst_v4l2_get_norm (GstV4l2Object * v4l2object, v4l2_std_id * norm)
894 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "getting norm");
896 if (!GST_V4L2_IS_OPEN (v4l2object))
899 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_STD, norm) < 0)
907 GST_DEBUG ("Failed to get the current norm for device %s",
908 v4l2object->videodev);
914 /******************************************************
915 * gst_v4l2_set_norm()
916 * Set the norm of the current device
917 * return value: TRUE on success, FALSE on error
918 ******************************************************/
920 gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm)
922 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "trying to set norm to "
923 "%" G_GINT64_MODIFIER "x", (guint64) norm);
925 if (!GST_V4L2_IS_OPEN (v4l2object))
928 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_S_STD, &norm) < 0)
936 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
937 (_("Failed to set norm for device '%s'."),
938 v4l2object->videodev), GST_ERROR_SYSTEM);
943 /******************************************************
944 * gst_v4l2_get_frequency():
945 * get the current frequency
946 * return value: TRUE on success, FALSE on error
947 ******************************************************/
949 gst_v4l2_get_frequency (GstV4l2Object * v4l2object,
950 gint tunernum, gulong * frequency)
952 struct v4l2_frequency freq = { 0, };
954 GstTunerChannel *channel;
956 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "getting current tuner frequency");
958 if (!GST_V4L2_IS_OPEN (v4l2object))
961 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
963 freq.tuner = tunernum;
964 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0)
967 *frequency = freq.frequency * channel->freq_multiplicator;
974 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
975 (_("Failed to get current tuner frequency for device '%s'."),
976 v4l2object->videodev), GST_ERROR_SYSTEM);
982 /******************************************************
983 * gst_v4l2_set_frequency():
985 * return value: TRUE on success, FALSE on error
986 ******************************************************/
988 gst_v4l2_set_frequency (GstV4l2Object * v4l2object,
989 gint tunernum, gulong frequency)
991 struct v4l2_frequency freq = { 0, };
993 GstTunerChannel *channel;
995 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
996 "setting current tuner frequency to %lu", frequency);
998 if (!GST_V4L2_IS_OPEN (v4l2object))
1001 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
1003 freq.tuner = tunernum;
1004 /* fill in type - ignore error */
1005 (void) v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq);
1006 freq.frequency = frequency / channel->freq_multiplicator;
1008 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0)
1016 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1017 (_("Failed to set current tuner frequency for device '%s' to %lu Hz."),
1018 v4l2object->videodev, frequency), GST_ERROR_SYSTEM);
1023 /******************************************************
1024 * gst_v4l2_signal_strength():
1025 * get the strength of the signal on the current input
1026 * return value: TRUE on success, FALSE on error
1027 ******************************************************/
1029 gst_v4l2_signal_strength (GstV4l2Object * v4l2object,
1030 gint tunernum, gulong * signal_strength)
1032 struct v4l2_tuner tuner = { 0, };
1034 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "trying to get signal strength");
1036 if (!GST_V4L2_IS_OPEN (v4l2object))
1039 tuner.index = tunernum;
1040 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &tuner) < 0)
1043 *signal_strength = tuner.signal;
1050 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1051 (_("Failed to get signal strength for device '%s'."),
1052 v4l2object->videodev), GST_ERROR_SYSTEM);
1057 /******************************************************
1058 * gst_v4l2_get_attribute():
1059 * try to get the value of one specific attribute
1060 * return value: TRUE on success, FALSE on error
1061 ******************************************************/
1063 gst_v4l2_get_attribute (GstV4l2Object * v4l2object,
1064 int attribute_num, int *value)
1066 struct v4l2_control control = { 0, };
1068 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "getting value of attribute %d",
1071 if (!GST_V4L2_IS_OPEN (v4l2object))
1074 control.id = attribute_num;
1076 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0)
1079 *value = control.value;
1086 GST_WARNING_OBJECT (v4l2object,
1087 _("Failed to get value for control %d on device '%s'."),
1088 attribute_num, v4l2object->videodev);
1094 /******************************************************
1095 * gst_v4l2_set_attribute():
1096 * try to set the value of one specific attribute
1097 * return value: TRUE on success, FALSE on error
1098 ******************************************************/
1100 gst_v4l2_set_attribute (GstV4l2Object * v4l2object,
1101 int attribute_num, const int value)
1103 struct v4l2_control control = { 0, };
1105 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "setting value of attribute %d to %d",
1106 attribute_num, value);
1108 if (!GST_V4L2_IS_OPEN (v4l2object))
1111 control.id = attribute_num;
1112 control.value = value;
1113 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0)
1121 GST_WARNING_OBJECT (v4l2object,
1122 _("Failed to set value %d for control %d on device '%s'."),
1123 value, attribute_num, v4l2object->videodev);
1129 set_control (GQuark field_id, const GValue * value, gpointer user_data)
1131 GstV4l2Object *v4l2object = user_data;
1132 GQuark normalised_field_id;
1135 /* 32 bytes is the maximum size for a control name according to v4l2 */
1138 /* Backwards compatibility: in the past GStreamer would normalise strings in
1139 a subtly different way to v4l2-ctl. e.g. the kernel's "Focus (absolute)"
1140 would become "focus__absolute_" whereas now it becomes "focus_absolute".
1141 Please remove the following in GStreamer 1.5 for 1.6 */
1142 strncpy (name, g_quark_to_string (field_id), sizeof (name));
1144 gst_v4l2_normalise_control_name (name);
1145 normalised_field_id = g_quark_from_string (name);
1146 if (normalised_field_id != field_id)
1147 g_warning ("In GStreamer 1.4 the way V4L2 control names were normalised "
1148 "changed. Instead of setting \"%s\" please use \"%s\". The former is "
1149 "deprecated and will be removed in a future version of GStreamer",
1150 g_quark_to_string (field_id), name);
1151 field_id = normalised_field_id;
1153 d = g_datalist_id_get_data (&v4l2object->controls, field_id);
1155 GST_WARNING_OBJECT (v4l2object,
1156 "Control '%s' does not exist or has an unsupported type.",
1157 g_quark_to_string (field_id));
1160 if (!G_VALUE_HOLDS (value, G_TYPE_INT)) {
1161 GST_WARNING_OBJECT (v4l2object,
1162 "'int' value expected for control '%s'.", g_quark_to_string (field_id));
1165 gst_v4l2_set_attribute (v4l2object, GPOINTER_TO_INT (d),
1166 g_value_get_int (value));
1171 gst_v4l2_set_controls (GstV4l2Object * v4l2object, GstStructure * controls)
1173 return gst_structure_foreach (controls, set_control, v4l2object);
1177 gst_v4l2_get_input (GstV4l2Object * v4l2object, gint * input)
1181 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "trying to get input");
1183 if (!GST_V4L2_IS_OPEN (v4l2object))
1186 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_INPUT, &n) < 0)
1191 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "input: %d", n);
1197 if (v4l2object->device_caps & V4L2_CAP_TUNER) {
1198 /* only give a warning message if driver actually claims to have tuner
1201 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1202 (_("Failed to get current input on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1208 gst_v4l2_set_input (GstV4l2Object * v4l2object, gint input)
1210 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "trying to set input to %d", input);
1212 if (!GST_V4L2_IS_OPEN (v4l2object))
1215 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_S_INPUT, &input) < 0)
1222 if (v4l2object->device_caps & V4L2_CAP_TUNER) {
1223 /* only give a warning message if driver actually claims to have tuner
1226 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1227 (_("Failed to set input %d on device %s."),
1228 input, v4l2object->videodev), GST_ERROR_SYSTEM);
1234 gst_v4l2_get_output (GstV4l2Object * v4l2object, gint * output)
1238 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "trying to get output");
1240 if (!GST_V4L2_IS_OPEN (v4l2object))
1243 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_OUTPUT, &n) < 0)
1248 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "output: %d", n);
1254 if (v4l2object->device_caps & V4L2_CAP_TUNER) {
1255 /* only give a warning message if driver actually claims to have tuner
1258 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1259 (_("Failed to get current output on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1265 gst_v4l2_set_output (GstV4l2Object * v4l2object, gint output)
1267 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "trying to set output to %d", output);
1269 if (!GST_V4L2_IS_OPEN (v4l2object))
1272 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_S_OUTPUT, &output) < 0)
1279 if (v4l2object->device_caps & V4L2_CAP_TUNER) {
1280 /* only give a warning message if driver actually claims to have tuner
1283 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1284 (_("Failed to set output %d on device %s."),
1285 output, v4l2object->videodev), GST_ERROR_SYSTEM);