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>
37 /* Needed on older Solaris Nevada builds (72 at least) */
39 #include <sys/ioccom.h>
41 #include "v4l2_calls.h"
42 #include "gstv4l2tuner.h"
43 #include "gstv4l2xoverlay.h"
44 #include "gstv4l2colorbalance.h"
46 #include "gstv4l2src.h"
48 /* Those are ioctl calls */
49 #ifndef V4L2_CID_HCENTER
50 #define V4L2_CID_HCENTER V4L2_CID_HCENTER_DEPRECATED
52 #ifndef V4L2_CID_VCENTER
53 #define V4L2_CID_VCENTER V4L2_CID_VCENTER_DEPRECATED
56 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
57 #define GST_CAT_DEFAULT v4l2_debug
59 /******************************************************
60 * gst_v4l2_get_capabilities():
61 * get the device's capturing capabilities
62 * return value: TRUE on success, FALSE on error
63 ******************************************************/
65 gst_v4l2_get_capabilities (GstV4l2Object * v4l2object)
69 e = v4l2object->element;
71 GST_DEBUG_OBJECT (e, "getting capabilities");
73 if (!GST_V4L2_IS_OPEN (v4l2object))
76 if (ioctl (v4l2object->video_fd, VIDIOC_QUERYCAP, &v4l2object->vcap) < 0)
79 GST_LOG_OBJECT (e, "driver: '%s'", v4l2object->vcap.driver);
80 GST_LOG_OBJECT (e, "card: '%s'", v4l2object->vcap.card);
81 GST_LOG_OBJECT (e, "bus_info: '%s'", v4l2object->vcap.bus_info);
82 GST_LOG_OBJECT (e, "version: %08x", v4l2object->vcap.version);
83 GST_LOG_OBJECT (e, "capabilites: %08x", v4l2object->vcap.capabilities);
90 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
91 (_("Error getting capabilities for device '%s': "
92 "It isn't a v4l2 driver. Check if it is a v4l1 driver."),
93 v4l2object->videodev), GST_ERROR_SYSTEM);
99 /******************************************************
100 * gst_v4l2_empty_lists() and gst_v4l2_fill_lists():
101 * fill/empty the lists of enumerations
102 * return value: TRUE on success, FALSE on error
103 ******************************************************/
105 gst_v4l2_fill_lists (GstV4l2Object * v4l2object)
111 e = v4l2object->element;
113 GST_DEBUG_OBJECT (e, "getting enumerations");
114 GST_V4L2_CHECK_OPEN (v4l2object);
116 GST_DEBUG_OBJECT (e, " channels");
117 /* and now, the channels */
119 struct v4l2_input input = { 0, };
120 GstV4l2TunerChannel *v4l2channel;
122 GstTunerChannel *channel;
125 if (ioctl (v4l2object->video_fd, VIDIOC_ENUMINPUT, &input) < 0) {
127 break; /* end of enumeration */
129 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
130 (_("Failed to query attributes of input %d in device %s"),
131 n, v4l2object->videodev),
132 ("Failed to get %d in input enumeration for %s. (%d - %s)",
133 n, v4l2object->videodev, errno, strerror (errno)));
138 GST_LOG_OBJECT (e, " index: %d", input.index);
139 GST_LOG_OBJECT (e, " name: '%s'", input.name);
140 GST_LOG_OBJECT (e, " type: %08x", input.type);
141 GST_LOG_OBJECT (e, " audioset: %08x", input.audioset);
142 GST_LOG_OBJECT (e, " std: %016x", input.std);
143 GST_LOG_OBJECT (e, " status: %08x", input.status);
145 v4l2channel = g_object_new (GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
146 channel = GST_TUNER_CHANNEL (v4l2channel);
147 channel->label = g_strdup ((const gchar *) input.name);
148 channel->flags = GST_TUNER_CHANNEL_INPUT;
149 v4l2channel->index = n;
151 if (input.type == V4L2_INPUT_TYPE_TUNER) {
152 struct v4l2_tuner vtun;
154 v4l2channel->tuner = input.tuner;
155 channel->flags |= GST_TUNER_CHANNEL_FREQUENCY;
157 vtun.index = input.tuner;
158 if (ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &vtun) < 0) {
159 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
160 (_("Failed to get setting of tuner %d on device '%s'."),
161 input.tuner, v4l2object->videodev), GST_ERROR_SYSTEM);
162 g_object_unref (G_OBJECT (channel));
166 channel->freq_multiplicator =
167 62.5 * ((vtun.capability & V4L2_TUNER_CAP_LOW) ? 1 : 1000);
168 channel->min_frequency = vtun.rangelow * channel->freq_multiplicator;
169 channel->max_frequency = vtun.rangehigh * channel->freq_multiplicator;
170 channel->min_signal = 0;
171 channel->max_signal = 0xffff;
173 if (input.audioset) {
174 /* we take the first. We don't care for
175 * the others for now */
176 while (!(input.audioset & (1 << v4l2channel->audio)))
177 v4l2channel->audio++;
178 channel->flags |= GST_TUNER_CHANNEL_AUDIO;
181 v4l2object->channels =
182 g_list_append (v4l2object->channels, (gpointer) channel);
185 GST_DEBUG_OBJECT (e, " norms");
188 struct v4l2_standard standard = { 0, };
189 GstV4l2TunerNorm *v4l2norm;
193 /* fill in defaults */
194 standard.frameperiod.numerator = 1;
195 standard.frameperiod.denominator = 0;
198 if (ioctl (v4l2object->video_fd, VIDIOC_ENUMSTD, &standard) < 0) {
199 if (errno == EINVAL || errno == ENOTTY)
200 break; /* end of enumeration */
202 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
203 (_("Failed to query norm on device '%s'."),
204 v4l2object->videodev),
205 ("Failed to get attributes for norm %d on devide '%s'. (%d - %s)",
206 n, v4l2object->videodev, errno, strerror (errno)));
211 GST_DEBUG_OBJECT (e, " '%s', fps: %d / %d",
212 standard.name, standard.frameperiod.denominator,
213 standard.frameperiod.numerator);
215 v4l2norm = g_object_new (GST_TYPE_V4L2_TUNER_NORM, NULL);
216 norm = GST_TUNER_NORM (v4l2norm);
217 norm->label = g_strdup ((const gchar *) standard.name);
218 gst_value_set_fraction (&norm->framerate,
219 standard.frameperiod.denominator, standard.frameperiod.numerator);
220 v4l2norm->index = standard.id;
222 v4l2object->norms = g_list_append (v4l2object->norms, (gpointer) norm);
225 GST_DEBUG_OBJECT (e, " controls+menus");
226 /* and lastly, controls+menus (if appropriate) */
227 for (n = V4L2_CID_BASE;; n++) {
228 struct v4l2_queryctrl control = { 0, };
229 GstV4l2ColorBalanceChannel *v4l2channel;
231 GstColorBalanceChannel *channel;
233 /* when we reached the last official CID, continue with private CIDs */
234 if (n == V4L2_CID_LASTP1) {
235 GST_DEBUG_OBJECT (e, "checking private CIDs");
236 n = V4L2_CID_PRIVATE_BASE;
237 /* FIXME: We are still not handling private controls. We need a new GstInterface
238 to export those controls */
243 if (ioctl (v4l2object->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
244 if (errno == EINVAL) {
245 if (n < V4L2_CID_PRIVATE_BASE)
246 /* continue so that we also check private controls */
251 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
252 (_("Failed getting controls attributes on device '%s.'"),
253 v4l2object->videodev),
254 ("Failed querying control %d on device '%s'. (%d - %s)",
255 n, v4l2object->videodev, errno, strerror (errno)));
259 if (control.flags & V4L2_CTRL_FLAG_DISABLED)
263 case V4L2_CID_BRIGHTNESS:
264 case V4L2_CID_CONTRAST:
265 case V4L2_CID_SATURATION:
267 case V4L2_CID_BLACK_LEVEL:
268 case V4L2_CID_AUTO_WHITE_BALANCE:
269 case V4L2_CID_DO_WHITE_BALANCE:
270 case V4L2_CID_RED_BALANCE:
271 case V4L2_CID_BLUE_BALANCE:
273 case V4L2_CID_EXPOSURE:
274 case V4L2_CID_AUTOGAIN:
276 /* we only handle these for now (why?) */
280 case V4L2_CID_HCENTER:
281 case V4L2_CID_VCENTER:
282 #ifdef V4L2_CID_PAN_RESET
283 case V4L2_CID_PAN_RESET:
285 #ifdef V4L2_CID_TILT_RESET
286 case V4L2_CID_TILT_RESET:
288 /* not handled here, handled by VideoOrientation interface */
291 case V4L2_CID_AUDIO_VOLUME:
292 case V4L2_CID_AUDIO_BALANCE:
293 case V4L2_CID_AUDIO_BASS:
294 case V4L2_CID_AUDIO_TREBLE:
295 case V4L2_CID_AUDIO_MUTE:
296 case V4L2_CID_AUDIO_LOUDNESS:
297 /* FIXME: We should implement GstMixer interface */
301 "ControlID %s (%x) unhandled, FIXME", control.name, n);
308 GST_DEBUG_OBJECT (e, "Adding ControlID %s (%x)", control.name, n);
309 v4l2channel = g_object_new (GST_TYPE_V4L2_COLOR_BALANCE_CHANNEL, NULL);
310 channel = GST_COLOR_BALANCE_CHANNEL (v4l2channel);
311 channel->label = g_strdup ((const gchar *) control.name);
314 #if 0 /* FIXME: it will be need just when handling private controls
315 (currently none of base controls are of this type) */
316 if (control.type == V4L2_CTRL_TYPE_MENU) {
317 struct v4l2_querymenu menu, *mptr;
324 if (ioctl (v4l2object->video_fd, VIDIOC_QUERYMENU, &menu) < 0) {
326 break; /* end of enumeration */
328 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
329 (_("Failed getting controls attributes on device '%s'."),
330 v4l2object->videodev),
331 ("Failed to get %d in menu enumeration for %s. (%d - %s)",
332 n, v4l2object->videodev, errno, strerror (errno)));
336 mptr = g_malloc (sizeof (menu));
337 memcpy (mptr, &menu, sizeof (menu));
338 menus = g_list_append (menus, mptr);
341 v4l2object->menus = g_list_append (v4l2object->menus, menus);
344 switch (control.type) {
345 case V4L2_CTRL_TYPE_INTEGER:
346 channel->min_value = control.minimum;
347 channel->max_value = control.maximum;
349 case V4L2_CTRL_TYPE_BOOLEAN:
350 channel->min_value = FALSE;
351 channel->max_value = TRUE;
354 /* FIXME we should find out how to handle V4L2_CTRL_TYPE_BUTTON.
355 BUTTON controls like V4L2_CID_DO_WHITE_BALANCE can just be set (1) or
356 unset (0), but can't be queried */
358 "Control with non supported type %s (%x), type=%d",
359 control.name, n, control.type);
360 channel->min_value = channel->max_value = 0;
364 v4l2object->colors = g_list_append (v4l2object->colors, (gpointer) channel);
367 GST_DEBUG_OBJECT (e, "done");
373 gst_v4l2_empty_lists (GstV4l2Object * v4l2object)
375 GST_DEBUG_OBJECT (v4l2object->element, "deleting enumerations");
377 g_list_foreach (v4l2object->channels, (GFunc) g_object_unref, NULL);
378 g_list_free (v4l2object->channels);
379 v4l2object->channels = NULL;
381 g_list_foreach (v4l2object->norms, (GFunc) g_object_unref, NULL);
382 g_list_free (v4l2object->norms);
383 v4l2object->norms = NULL;
385 g_list_foreach (v4l2object->colors, (GFunc) g_object_unref, NULL);
386 g_list_free (v4l2object->colors);
387 v4l2object->colors = NULL;
390 /******************************************************
392 * open the video device (v4l2object->videodev)
393 * return value: TRUE on success, FALSE on error
394 ******************************************************/
396 gst_v4l2_open (GstV4l2Object * v4l2object)
400 GST_DEBUG_OBJECT (v4l2object->element, "Trying to open device %s",
401 v4l2object->videodev);
403 GST_V4L2_CHECK_NOT_OPEN (v4l2object);
404 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
406 /* be sure we have a device */
407 if (!v4l2object->videodev)
408 v4l2object->videodev = g_strdup ("/dev/video");
410 /* check if it is a device */
411 if (stat (v4l2object->videodev, &st) == -1)
414 if (!S_ISCHR (st.st_mode))
417 /* open the device */
418 v4l2object->video_fd =
419 open (v4l2object->videodev, O_RDWR /* | O_NONBLOCK */ );
421 if (!GST_V4L2_IS_OPEN (v4l2object))
424 /* get capabilities, error will be posted */
425 if (!gst_v4l2_get_capabilities (v4l2object))
428 /* do we need to be a capture device? */
429 if (GST_IS_V4L2SRC (v4l2object) &&
430 !(v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
433 /* create enumerations, posts errors. */
434 if (!gst_v4l2_fill_lists (v4l2object))
437 GST_INFO_OBJECT (v4l2object->element,
438 "Opened device '%s' (%s) successfully",
439 v4l2object->vcap.card, v4l2object->videodev);
446 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
447 (_("Cannot identify device '%s'."), v4l2object->videodev),
453 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
454 (_("This isn't a device '%s'."), v4l2object->videodev),
460 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
461 (_("Could not open device '%s' for reading and writing."),
462 v4l2object->videodev), GST_ERROR_SYSTEM);
467 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
468 (_("Device '%s' is not a capture device."),
469 v4l2object->videodev),
470 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
475 if (GST_V4L2_IS_OPEN (v4l2object)) {
477 close (v4l2object->video_fd);
478 v4l2object->video_fd = -1;
481 gst_v4l2_empty_lists (v4l2object);
488 /******************************************************
490 * close the video device (v4l2object->video_fd)
491 * return value: TRUE on success, FALSE on error
492 ******************************************************/
494 gst_v4l2_close (GstV4l2Object * v4l2object)
496 GST_DEBUG_OBJECT (v4l2object->element, "Trying to close %s",
497 v4l2object->videodev);
499 GST_V4L2_CHECK_OPEN (v4l2object);
500 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
503 close (v4l2object->video_fd);
504 v4l2object->video_fd = -1;
507 gst_v4l2_empty_lists (v4l2object);
513 /******************************************************
514 * gst_v4l2_get_norm()
515 * Get the norm of the current device
516 * return value: TRUE on success, FALSE on error
517 ******************************************************/
519 gst_v4l2_get_norm (GstV4l2Object * v4l2object, v4l2_std_id * norm)
521 GST_DEBUG_OBJECT (v4l2object->element, "getting norm");
523 if (!GST_V4L2_IS_OPEN (v4l2object))
526 if (ioctl (v4l2object->video_fd, VIDIOC_G_STD, norm) < 0)
534 GST_DEBUG ("Failed to get the current norm for device %s",
535 v4l2object->videodev);
541 /******************************************************
542 * gst_v4l2_set_norm()
543 * Set the norm of the current device
544 * return value: TRUE on success, FALSE on error
545 ******************************************************/
547 gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm)
549 GST_DEBUG_OBJECT (v4l2object->element, "trying to set norm to %llx", norm);
551 if (!GST_V4L2_IS_OPEN (v4l2object))
554 if (ioctl (v4l2object->video_fd, VIDIOC_S_STD, &norm) < 0)
562 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
563 (_("Failed to set norm for device '%s'."),
564 v4l2object->videodev), GST_ERROR_SYSTEM);
569 /******************************************************
570 * gst_v4l2_get_frequency():
571 * get the current frequency
572 * return value: TRUE on success, FALSE on error
573 ******************************************************/
575 gst_v4l2_get_frequency (GstV4l2Object * v4l2object,
576 gint tunernum, gulong * frequency)
578 struct v4l2_frequency freq;
580 GstTunerChannel *channel;
582 GST_DEBUG_OBJECT (v4l2object->element, "getting current tuner frequency");
584 if (!GST_V4L2_IS_OPEN (v4l2object))
587 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
589 freq.tuner = tunernum;
590 if (ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0)
593 *frequency = freq.frequency * channel->freq_multiplicator;
600 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
601 (_("Failed to get current tuner frequency for device '%s'."),
602 v4l2object->videodev), GST_ERROR_SYSTEM);
608 /******************************************************
609 * gst_v4l2_set_frequency():
611 * return value: TRUE on success, FALSE on error
612 ******************************************************/
614 gst_v4l2_set_frequency (GstV4l2Object * v4l2object,
615 gint tunernum, gulong frequency)
617 struct v4l2_frequency freq;
619 GstTunerChannel *channel;
621 GST_DEBUG_OBJECT (v4l2object->element,
622 "setting current tuner frequency to %lu", frequency);
624 if (!GST_V4L2_IS_OPEN (v4l2object))
627 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
629 freq.tuner = tunernum;
630 /* fill in type - ignore error */
631 ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq);
632 freq.frequency = frequency / channel->freq_multiplicator;
634 if (ioctl (v4l2object->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0)
642 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
643 (_("Failed to set current tuner frequency for device '%s' to %lu Hz."),
644 v4l2object->videodev, frequency), GST_ERROR_SYSTEM);
649 /******************************************************
650 * gst_v4l2_signal_strength():
651 * get the strength of the signal on the current input
652 * return value: TRUE on success, FALSE on error
653 ******************************************************/
655 gst_v4l2_signal_strength (GstV4l2Object * v4l2object,
656 gint tunernum, gulong * signal_strength)
658 struct v4l2_tuner tuner;
660 GST_DEBUG_OBJECT (v4l2object->element, "trying to get signal strength");
662 if (!GST_V4L2_IS_OPEN (v4l2object))
665 tuner.index = tunernum;
666 if (ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &tuner) < 0)
669 *signal_strength = tuner.signal;
676 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
677 (_("Failed to get signal strength for device '%s'."),
678 v4l2object->videodev), GST_ERROR_SYSTEM);
683 /******************************************************
684 * gst_v4l2_get_attribute():
685 * try to get the value of one specific attribute
686 * return value: TRUE on success, FALSE on error
687 ******************************************************/
689 gst_v4l2_get_attribute (GstV4l2Object * v4l2object,
690 int attribute_num, int *value)
692 struct v4l2_control control;
694 GST_DEBUG_OBJECT (v4l2object->element, "getting value of attribute %d",
697 if (!GST_V4L2_IS_OPEN (v4l2object))
700 control.id = attribute_num;
702 if (ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0)
705 *value = control.value;
712 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
713 (_("Failed to get value for control %d on device '%s'."),
714 attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
720 /******************************************************
721 * gst_v4l2_set_attribute():
722 * try to set the value of one specific attribute
723 * return value: TRUE on success, FALSE on error
724 ******************************************************/
726 gst_v4l2_set_attribute (GstV4l2Object * v4l2object,
727 int attribute_num, const int value)
729 struct v4l2_control control;
731 GST_DEBUG_OBJECT (v4l2object->element, "setting value of attribute %d to %d",
732 attribute_num, value);
734 if (!GST_V4L2_IS_OPEN (v4l2object))
737 control.id = attribute_num;
738 control.value = value;
739 if (ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0)
747 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
748 (_("Failed to set value %d for control %d on device '%s'."),
749 value, attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
755 gst_v4l2_get_input (GstV4l2Object * v4l2object, gint * input)
759 GST_DEBUG_OBJECT (v4l2object->element, "trying to get input");
761 if (!GST_V4L2_IS_OPEN (v4l2object))
764 if (ioctl (v4l2object->video_fd, VIDIOC_G_INPUT, &n) < 0)
769 GST_DEBUG_OBJECT (v4l2object->element, "input: %d", n);
776 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
777 (_("Failed to get current input on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
783 gst_v4l2_set_input (GstV4l2Object * v4l2object, gint input)
785 GST_DEBUG_OBJECT (v4l2object->element, "trying to set input to %d", input);
787 if (!GST_V4L2_IS_OPEN (v4l2object))
790 if (ioctl (v4l2object->video_fd, VIDIOC_S_INPUT, &input) < 0)
798 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
799 (_("Failed to set input %d on device %s."),
800 input, v4l2object->videodev), GST_ERROR_SYSTEM);