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., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
28 #include <sys/types.h>
31 #include <sys/ioctl.h>
36 #include "v4l2_calls.h"
37 #include "gstv4l2tuner.h"
38 #include "gstv4l2xoverlay.h"
39 #include "gstv4l2colorbalance.h"
41 #include "gstv4l2src.h"
43 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
44 #define GST_CAT_DEFAULT v4l2_debug
46 /******************************************************
47 * gst_v4l2_get_capabilities():
48 * get the device's capturing capabilities
49 * return value: TRUE on success, FALSE on error
50 ******************************************************/
52 gst_v4l2_get_capabilities (GstV4l2Object * v4l2object)
54 GST_DEBUG_OBJECT (v4l2object->element, "getting capabilities");
56 if (!GST_V4L2_IS_OPEN (v4l2object))
59 if (ioctl (v4l2object->video_fd, VIDIOC_QUERYCAP, &v4l2object->vcap) < 0)
67 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
68 (_("Error getting capabilities for device '%s': "
69 "It isn't a v4l2 driver. Check if it is a v4l1 driver."),
70 v4l2object->videodev), GST_ERROR_SYSTEM);
76 /******************************************************
77 * gst_v4l2_empty_lists() and gst_v4l2_fill_lists():
78 * fill/empty the lists of enumerations
79 * return value: TRUE on success, FALSE on error
80 ******************************************************/
82 gst_v4l2_fill_lists (GstV4l2Object * v4l2object)
86 GST_DEBUG_OBJECT (v4l2object->element, "getting enumerations");
87 GST_V4L2_CHECK_OPEN (v4l2object);
89 GST_DEBUG_OBJECT (v4l2object->element, " channels");
90 /* and now, the channels */
92 struct v4l2_input input;
93 GstV4l2TunerChannel *v4l2channel;
94 GstTunerChannel *channel;
97 if (ioctl (v4l2object->video_fd, VIDIOC_ENUMINPUT, &input) < 0) {
99 break; /* end of enumeration */
101 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
102 (_("Failed to query attributes of input %d in device %s"),
103 n, v4l2object->videodev),
104 ("Failed to get %d in input enumeration for %s. (%d - %s)",
105 n, v4l2object->videodev, errno, strerror (errno)));
110 GST_DEBUG_OBJECT (v4l2object->element, " '%s'", input.name);
112 v4l2channel = g_object_new (GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
113 channel = GST_TUNER_CHANNEL (v4l2channel);
114 channel->label = g_strdup ((const gchar *) input.name);
115 channel->flags = GST_TUNER_CHANNEL_INPUT;
116 v4l2channel->index = n;
117 if (input.type == V4L2_INPUT_TYPE_TUNER) {
118 struct v4l2_tuner vtun;
120 v4l2channel->tuner = input.tuner;
121 channel->flags |= GST_TUNER_CHANNEL_FREQUENCY;
123 vtun.index = input.tuner;
124 if (ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &vtun) < 0) {
125 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
126 (_("Failed to get setting of tuner &d on device '%s'."),
127 input.tuner, v4l2object->videodev), GST_ERROR_SYSTEM);
128 g_object_unref (G_OBJECT (channel));
131 channel->freq_multiplicator =
132 62.5 * ((vtun.capability & V4L2_TUNER_CAP_LOW) ? 1 : 1000);
133 channel->min_frequency = vtun.rangelow * channel->freq_multiplicator;
134 channel->max_frequency = vtun.rangehigh * channel->freq_multiplicator;
135 channel->min_signal = 0;
136 channel->max_signal = 0xffff;
138 if (input.audioset) {
139 /* we take the first. We don't care for
140 * the others for now */
141 while (!(input.audioset & (1 << v4l2channel->audio)))
142 v4l2channel->audio++;
143 channel->flags |= GST_TUNER_CHANNEL_AUDIO;
146 v4l2object->channels =
147 g_list_append (v4l2object->channels, (gpointer) channel);
150 GST_DEBUG_OBJECT (v4l2object->element, " norms");
153 struct v4l2_standard standard;
154 GstV4l2TunerNorm *v4l2norm;
157 /* fill in defaults */
158 standard.frameperiod.numerator = 1;
159 standard.frameperiod.denominator = 0;
161 if (ioctl (v4l2object->video_fd, VIDIOC_ENUMSTD, &standard) < 0) {
163 break; /* end of enumeration */
165 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
166 (_("Failed to query norm on device '%s'."),
167 v4l2object->videodev),
168 ("Failed to get attributes for norm %d on devide '%s'. (%d - %s)",
169 n, v4l2object->videodev, errno, strerror (errno)));
174 GST_DEBUG_OBJECT (v4l2object->element, " '%s', fps: %d / %d",
175 standard.name, standard.frameperiod.denominator,
176 standard.frameperiod.numerator);
178 v4l2norm = g_object_new (GST_TYPE_V4L2_TUNER_NORM, NULL);
179 norm = GST_TUNER_NORM (v4l2norm);
180 norm->label = g_strdup ((const gchar *) standard.name);
181 gst_value_set_fraction (&norm->framerate,
182 standard.frameperiod.denominator, standard.frameperiod.numerator);
183 v4l2norm->index = standard.id;
185 v4l2object->norms = g_list_append (v4l2object->norms, (gpointer) norm);
188 GST_DEBUG_OBJECT (v4l2object->element, " controls+menus");
189 /* and lastly, controls+menus (if appropriate) */
190 for (n = V4L2_CID_BASE;; n++) {
191 struct v4l2_queryctrl control;
192 GstV4l2ColorBalanceChannel *v4l2channel;
193 GstColorBalanceChannel *channel;
195 /* when we reached the last official CID, continue with private CIDs */
196 if (n == V4L2_CID_LASTP1) {
197 GST_DEBUG_OBJECT (v4l2object->element, "checking private CIDs");
198 n = V4L2_CID_PRIVATE_BASE;
199 /* FIXME: We are still not handling private controls. We need a new GstInterface
200 to export those controls */
205 if (ioctl (v4l2object->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
206 if (errno == EINVAL) {
207 if (n < V4L2_CID_PRIVATE_BASE)
208 /* continue so that we also check private controls */
213 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
214 (_("Failed getting controls attributes on device '%s."),
215 v4l2object->videodev),
216 ("Failed querying control %d on device '%s'. (%d - %s)",
217 n, v4l2object->videodev, errno, strerror (errno)));
221 if (control.flags & V4L2_CTRL_FLAG_DISABLED)
225 case V4L2_CID_BRIGHTNESS:
226 case V4L2_CID_CONTRAST:
227 case V4L2_CID_SATURATION:
229 case V4L2_CID_BLACK_LEVEL:
230 case V4L2_CID_AUTO_WHITE_BALANCE:
231 case V4L2_CID_DO_WHITE_BALANCE:
232 case V4L2_CID_RED_BALANCE:
233 case V4L2_CID_BLUE_BALANCE:
235 case V4L2_CID_EXPOSURE:
236 case V4L2_CID_AUTOGAIN:
238 /* we only handle these for now (why?) */
242 case V4L2_CID_HCENTER:
243 case V4L2_CID_VCENTER:
244 /* not handled here, handled by VideoOrientation interface */
247 case V4L2_CID_AUDIO_VOLUME:
248 case V4L2_CID_AUDIO_BALANCE:
249 case V4L2_CID_AUDIO_BASS:
250 case V4L2_CID_AUDIO_TREBLE:
251 case V4L2_CID_AUDIO_MUTE:
252 case V4L2_CID_AUDIO_LOUDNESS:
253 /* FIXME: We should implement GstMixer interface */
256 GST_DEBUG_OBJECT (v4l2object->element,
257 "ControlID %s (%x) unhandled, FIXME", control.name, n);
264 GST_DEBUG_OBJECT (v4l2object->element,
265 "Adding ControlID %s (%x)", control.name, n);
266 v4l2channel = g_object_new (GST_TYPE_V4L2_COLOR_BALANCE_CHANNEL, NULL);
267 channel = GST_COLOR_BALANCE_CHANNEL (v4l2channel);
268 channel->label = g_strdup ((const gchar *) control.name);
271 #if 0 /* FIXME: it will be need just when handling private controls
272 (currently none of base controls are of this type) */
273 if (control.type == V4L2_CTRL_TYPE_MENU) {
274 struct v4l2_querymenu menu, *mptr;
280 if (ioctl (v4l2object->video_fd, VIDIOC_QUERYMENU, &menu) < 0) {
282 break; /* end of enumeration */
284 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
285 (_("Failed getting controls attributes on device '%s."),
286 v4l2object->videodev),
287 ("Failed to get %d in menu enumeration for %s. (%d - %s)",
288 n, v4l2object->videodev, errno, strerror (errno)));
292 mptr = g_malloc (sizeof (menu));
293 memcpy (mptr, &menu, sizeof (menu));
294 menus = g_list_append (menus, mptr);
297 v4l2object->menus = g_list_append (v4l2object->menus, menus);
300 switch (control.type) {
301 case V4L2_CTRL_TYPE_INTEGER:
302 channel->min_value = control.minimum;
303 channel->max_value = control.maximum;
305 case V4L2_CTRL_TYPE_BOOLEAN:
306 channel->min_value = FALSE;
307 channel->max_value = TRUE;
310 /* FIXME we should find out how to handle V4L2_CTRL_TYPE_BUTTON.
311 BUTTON controls like V4L2_CID_DO_WHITE_BALANCE can just be set (1) or
312 unset (0), but can't be queried */
313 GST_DEBUG_OBJECT (v4l2object->element,
314 "Control with non supported type %s (%x), type=%d",
315 control.name, n, control.type);
316 channel->min_value = channel->max_value = 0;
320 v4l2object->colors = g_list_append (v4l2object->colors, (gpointer) channel);
323 GST_DEBUG_OBJECT (v4l2object->element, "done");
329 gst_v4l2_empty_lists (GstV4l2Object * v4l2object)
331 GST_DEBUG_OBJECT (v4l2object->element, "deleting enumerations");
333 g_list_foreach (v4l2object->channels, (GFunc) g_object_unref, NULL);
334 g_list_free (v4l2object->channels);
335 v4l2object->channels = NULL;
337 g_list_foreach (v4l2object->norms, (GFunc) g_object_unref, NULL);
338 g_list_free (v4l2object->norms);
339 v4l2object->norms = NULL;
341 g_list_foreach (v4l2object->colors, (GFunc) g_object_unref, NULL);
342 g_list_free (v4l2object->colors);
343 v4l2object->colors = NULL;
346 /******************************************************
348 * open the video device (v4l2object->videodev)
349 * return value: TRUE on success, FALSE on error
350 ******************************************************/
352 gst_v4l2_open (GstV4l2Object * v4l2object)
356 GST_DEBUG_OBJECT (v4l2object->element, "Trying to open device %s",
357 v4l2object->videodev);
359 GST_V4L2_CHECK_NOT_OPEN (v4l2object);
360 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
362 /* be sure we have a device */
363 if (!v4l2object->videodev)
364 v4l2object->videodev = g_strdup ("/dev/video");
366 /* check if it is a device */
367 if (stat (v4l2object->videodev, &st) == -1)
370 if (!S_ISCHR (st.st_mode))
373 /* open the device */
374 v4l2object->video_fd =
375 open (v4l2object->videodev, O_RDWR /* | O_NONBLOCK */ );
377 if (!GST_V4L2_IS_OPEN (v4l2object))
380 /* get capabilities, error will be posted */
381 if (!gst_v4l2_get_capabilities (v4l2object))
384 /* do we need to be a capture device? */
385 if (GST_IS_V4L2SRC (v4l2object) &&
386 !(v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
389 /* create enumerations, posts errors. */
390 if (!gst_v4l2_fill_lists (v4l2object))
393 GST_INFO_OBJECT (v4l2object->element,
394 "Opened device '%s' (%s) successfully",
395 v4l2object->vcap.card, v4l2object->videodev);
402 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
403 (_("Cannot identify device '%s'."), v4l2object->videodev),
409 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
410 (_("This isn't a device '%s'."), v4l2object->videodev),
416 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
417 (_("Could not open device '%s' for reading and writing."),
418 v4l2object->videodev), GST_ERROR_SYSTEM);
423 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
424 (_("Device '%s' is not a capture device."),
425 v4l2object->videodev),
426 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
431 if (GST_V4L2_IS_OPEN (v4l2object)) {
433 close (v4l2object->video_fd);
434 v4l2object->video_fd = -1;
437 gst_v4l2_empty_lists (v4l2object);
444 /******************************************************
446 * close the video device (v4l2object->video_fd)
447 * return value: TRUE on success, FALSE on error
448 ******************************************************/
450 gst_v4l2_close (GstV4l2Object * v4l2object)
452 GST_DEBUG_OBJECT (v4l2object->element, "Trying to close %s",
453 v4l2object->videodev);
455 GST_V4L2_CHECK_OPEN (v4l2object);
456 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
459 close (v4l2object->video_fd);
460 v4l2object->video_fd = -1;
463 gst_v4l2_empty_lists (v4l2object);
469 /******************************************************
470 * gst_v4l2_get_norm()
471 * Get the norm of the current device
472 * return value: TRUE on success, FALSE on error
473 ******************************************************/
475 gst_v4l2_get_norm (GstV4l2Object * v4l2object, v4l2_std_id * norm)
477 GST_DEBUG_OBJECT (v4l2object->element, "getting norm");
479 if (!GST_V4L2_IS_OPEN (v4l2object))
482 if (ioctl (v4l2object->video_fd, VIDIOC_G_STD, norm) < 0)
490 GST_DEBUG ("Failed to get the current norm for device %s",
491 v4l2object->videodev);
497 /******************************************************
498 * gst_v4l2_set_norm()
499 * Set the norm of the current device
500 * return value: TRUE on success, FALSE on error
501 ******************************************************/
503 gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm)
505 GST_DEBUG_OBJECT (v4l2object->element, "trying to set norm to %llx", norm);
507 if (!GST_V4L2_IS_OPEN (v4l2object))
510 if (ioctl (v4l2object->video_fd, VIDIOC_S_STD, &norm) < 0)
518 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
519 (_("Failed to set norm for device '%s'."),
520 v4l2object->videodev), GST_ERROR_SYSTEM);
525 /******************************************************
526 * gst_v4l2_get_frequency():
527 * get the current frequency
528 * return value: TRUE on success, FALSE on error
529 ******************************************************/
531 gst_v4l2_get_frequency (GstV4l2Object * v4l2object,
532 gint tunernum, gulong * frequency)
534 struct v4l2_frequency freq;
535 GstTunerChannel *channel;
537 GST_DEBUG_OBJECT (v4l2object->element, "getting current tuner frequency");
539 if (!GST_V4L2_IS_OPEN (v4l2object))
542 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
544 freq.tuner = tunernum;
545 if (ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0)
548 *frequency = freq.frequency * channel->freq_multiplicator;
555 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
556 (_("Failed to get current tuner frequency for device '%s'."),
557 v4l2object->videodev), GST_ERROR_SYSTEM);
563 /******************************************************
564 * gst_v4l2_set_frequency():
566 * return value: TRUE on success, FALSE on error
567 ******************************************************/
569 gst_v4l2_set_frequency (GstV4l2Object * v4l2object,
570 gint tunernum, gulong frequency)
572 struct v4l2_frequency freq;
573 GstTunerChannel *channel;
575 GST_DEBUG_OBJECT (v4l2object->element,
576 "setting current tuner frequency to %lu", frequency);
578 if (!GST_V4L2_IS_OPEN (v4l2object))
581 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
583 freq.tuner = tunernum;
584 /* fill in type - ignore error */
585 ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq);
586 freq.frequency = frequency / channel->freq_multiplicator;
588 if (ioctl (v4l2object->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0)
596 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
597 (_("Failed to set current tuner frequency for device '%s' to %lu Hz."),
598 v4l2object->videodev, frequency), GST_ERROR_SYSTEM);
603 /******************************************************
604 * gst_v4l2_signal_strength():
605 * get the strength of the signal on the current input
606 * return value: TRUE on success, FALSE on error
607 ******************************************************/
609 gst_v4l2_signal_strength (GstV4l2Object * v4l2object,
610 gint tunernum, gulong * signal_strength)
612 struct v4l2_tuner tuner;
614 GST_DEBUG_OBJECT (v4l2object->element, "trying to get signal strength");
616 if (!GST_V4L2_IS_OPEN (v4l2object))
619 tuner.index = tunernum;
620 if (ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &tuner) < 0)
623 *signal_strength = tuner.signal;
630 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
631 (_("Failed to get signal strength for device '%s'."),
632 v4l2object->videodev), GST_ERROR_SYSTEM);
637 /******************************************************
638 * gst_v4l2_get_attribute():
639 * try to get the value of one specific attribute
640 * return value: TRUE on success, FALSE on error
641 ******************************************************/
643 gst_v4l2_get_attribute (GstV4l2Object * v4l2object,
644 int attribute_num, int *value)
646 struct v4l2_control control;
648 GST_DEBUG_OBJECT (v4l2object->element, "getting value of attribute %d",
651 if (!GST_V4L2_IS_OPEN (v4l2object))
654 control.id = attribute_num;
656 if (ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0)
659 *value = control.value;
666 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
667 (_("Failed to get value for control %d on device '%s'."),
668 attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
674 /******************************************************
675 * gst_v4l2_set_attribute():
676 * try to set the value of one specific attribute
677 * return value: TRUE on success, FALSE on error
678 ******************************************************/
680 gst_v4l2_set_attribute (GstV4l2Object * v4l2object,
681 int attribute_num, const int value)
683 struct v4l2_control control;
685 GST_DEBUG_OBJECT (v4l2object->element, "setting value of attribute %d to %d",
686 attribute_num, value);
688 if (!GST_V4L2_IS_OPEN (v4l2object))
691 control.id = attribute_num;
692 control.value = value;
693 if (ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0)
701 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
702 (_("Failed to set value %d for control %d on device '%s'."),
703 value, attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
709 gst_v4l2_get_input (GstV4l2Object * v4l2object, gint * input)
713 GST_DEBUG_OBJECT (v4l2object->element, "trying to get input");
715 if (!GST_V4L2_IS_OPEN (v4l2object))
718 if (ioctl (v4l2object->video_fd, VIDIOC_G_INPUT, &n) < 0)
728 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
729 (_("Failed to get current input on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
735 gst_v4l2_set_input (GstV4l2Object * v4l2object, gint input)
737 GST_DEBUG_OBJECT (v4l2object->element, "trying to set input to %d", input);
739 if (!GST_V4L2_IS_OPEN (v4l2object))
742 if (ioctl (v4l2object->video_fd, VIDIOC_S_INPUT, &input) < 0)
750 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
751 (_("Failed to set input %d on device %s."),
752 input, v4l2object->videodev), GST_ERROR_SYSTEM);