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 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
49 #define GST_CAT_DEFAULT v4l2_debug
51 /******************************************************
52 * gst_v4l2_get_capabilities():
53 * get the device's capturing capabilities
54 * return value: TRUE on success, FALSE on error
55 ******************************************************/
57 gst_v4l2_get_capabilities (GstV4l2Object * v4l2object)
61 e = v4l2object->element;
63 GST_DEBUG_OBJECT (e, "getting capabilities");
65 if (!GST_V4L2_IS_OPEN (v4l2object))
68 if (ioctl (v4l2object->video_fd, VIDIOC_QUERYCAP, &v4l2object->vcap) < 0)
71 GST_LOG_OBJECT (e, "driver: '%s'", v4l2object->vcap.driver);
72 GST_LOG_OBJECT (e, "card: '%s'", v4l2object->vcap.card);
73 GST_LOG_OBJECT (e, "bus_info: '%s'", v4l2object->vcap.bus_info);
74 GST_LOG_OBJECT (e, "version: %08x", v4l2object->vcap.version);
75 GST_LOG_OBJECT (e, "capabilites: %08x", v4l2object->vcap.capabilities);
82 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
83 (_("Error getting capabilities for device '%s': "
84 "It isn't a v4l2 driver. Check if it is a v4l1 driver."),
85 v4l2object->videodev), GST_ERROR_SYSTEM);
91 /******************************************************
92 * gst_v4l2_empty_lists() and gst_v4l2_fill_lists():
93 * fill/empty the lists of enumerations
94 * return value: TRUE on success, FALSE on error
95 ******************************************************/
97 gst_v4l2_fill_lists (GstV4l2Object * v4l2object)
102 e = v4l2object->element;
104 GST_DEBUG_OBJECT (e, "getting enumerations");
105 GST_V4L2_CHECK_OPEN (v4l2object);
107 GST_DEBUG_OBJECT (e, " channels");
108 /* and now, the channels */
110 struct v4l2_input input = { 0, };
111 GstV4l2TunerChannel *v4l2channel;
112 GstTunerChannel *channel;
115 if (ioctl (v4l2object->video_fd, VIDIOC_ENUMINPUT, &input) < 0) {
117 break; /* end of enumeration */
119 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
120 (_("Failed to query attributes of input %d in device %s"),
121 n, v4l2object->videodev),
122 ("Failed to get %d in input enumeration for %s. (%d - %s)",
123 n, v4l2object->videodev, errno, strerror (errno)));
128 GST_LOG_OBJECT (e, " index: %d", input.index);
129 GST_LOG_OBJECT (e, " name: '%s'", input.name);
130 GST_LOG_OBJECT (e, " type: %08x", input.type);
131 GST_LOG_OBJECT (e, " audioset: %08x", input.audioset);
132 GST_LOG_OBJECT (e, " std: %016x", input.std);
133 GST_LOG_OBJECT (e, " status: %08x", input.status);
135 v4l2channel = g_object_new (GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
136 channel = GST_TUNER_CHANNEL (v4l2channel);
137 channel->label = g_strdup ((const gchar *) input.name);
138 channel->flags = GST_TUNER_CHANNEL_INPUT;
139 v4l2channel->index = n;
141 if (input.type == V4L2_INPUT_TYPE_TUNER) {
142 struct v4l2_tuner vtun;
144 v4l2channel->tuner = input.tuner;
145 channel->flags |= GST_TUNER_CHANNEL_FREQUENCY;
147 vtun.index = input.tuner;
148 if (ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &vtun) < 0) {
149 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
150 (_("Failed to get setting of tuner %d on device '%s'."),
151 input.tuner, v4l2object->videodev), GST_ERROR_SYSTEM);
152 g_object_unref (G_OBJECT (channel));
156 channel->freq_multiplicator =
157 62.5 * ((vtun.capability & V4L2_TUNER_CAP_LOW) ? 1 : 1000);
158 channel->min_frequency = vtun.rangelow * channel->freq_multiplicator;
159 channel->max_frequency = vtun.rangehigh * channel->freq_multiplicator;
160 channel->min_signal = 0;
161 channel->max_signal = 0xffff;
163 if (input.audioset) {
164 /* we take the first. We don't care for
165 * the others for now */
166 while (!(input.audioset & (1 << v4l2channel->audio)))
167 v4l2channel->audio++;
168 channel->flags |= GST_TUNER_CHANNEL_AUDIO;
171 v4l2object->channels =
172 g_list_append (v4l2object->channels, (gpointer) channel);
175 GST_DEBUG_OBJECT (e, " norms");
178 struct v4l2_standard standard = { 0, };
179 GstV4l2TunerNorm *v4l2norm;
182 /* fill in defaults */
183 standard.frameperiod.numerator = 1;
184 standard.frameperiod.denominator = 0;
187 if (ioctl (v4l2object->video_fd, VIDIOC_ENUMSTD, &standard) < 0) {
188 if (errno == EINVAL || errno == ENOTTY)
189 break; /* end of enumeration */
191 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
192 (_("Failed to query norm on device '%s'."),
193 v4l2object->videodev),
194 ("Failed to get attributes for norm %d on devide '%s'. (%d - %s)",
195 n, v4l2object->videodev, errno, strerror (errno)));
200 GST_DEBUG_OBJECT (e, " '%s', fps: %d / %d",
201 standard.name, standard.frameperiod.denominator,
202 standard.frameperiod.numerator);
204 v4l2norm = g_object_new (GST_TYPE_V4L2_TUNER_NORM, NULL);
205 norm = GST_TUNER_NORM (v4l2norm);
206 norm->label = g_strdup ((const gchar *) standard.name);
207 gst_value_set_fraction (&norm->framerate,
208 standard.frameperiod.denominator, standard.frameperiod.numerator);
209 v4l2norm->index = standard.id;
211 v4l2object->norms = g_list_append (v4l2object->norms, (gpointer) norm);
214 GST_DEBUG_OBJECT (e, " controls+menus");
215 /* and lastly, controls+menus (if appropriate) */
216 for (n = V4L2_CID_BASE;; n++) {
217 struct v4l2_queryctrl control = { 0, };
218 GstV4l2ColorBalanceChannel *v4l2channel;
219 GstColorBalanceChannel *channel;
221 /* when we reached the last official CID, continue with private CIDs */
222 if (n == V4L2_CID_LASTP1) {
223 GST_DEBUG_OBJECT (e, "checking private CIDs");
224 n = V4L2_CID_PRIVATE_BASE;
225 /* FIXME: We are still not handling private controls. We need a new GstInterface
226 to export those controls */
231 if (ioctl (v4l2object->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
232 if (errno == EINVAL) {
233 if (n < V4L2_CID_PRIVATE_BASE)
234 /* continue so that we also check private controls */
239 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
240 (_("Failed getting controls attributes on device '%s.'"),
241 v4l2object->videodev),
242 ("Failed querying control %d on device '%s'. (%d - %s)",
243 n, v4l2object->videodev, errno, strerror (errno)));
247 if (control.flags & V4L2_CTRL_FLAG_DISABLED)
251 case V4L2_CID_BRIGHTNESS:
252 case V4L2_CID_CONTRAST:
253 case V4L2_CID_SATURATION:
255 case V4L2_CID_BLACK_LEVEL:
256 case V4L2_CID_AUTO_WHITE_BALANCE:
257 case V4L2_CID_DO_WHITE_BALANCE:
258 case V4L2_CID_RED_BALANCE:
259 case V4L2_CID_BLUE_BALANCE:
261 case V4L2_CID_EXPOSURE:
262 case V4L2_CID_AUTOGAIN:
264 /* we only handle these for now (why?) */
268 case V4L2_CID_HCENTER:
269 case V4L2_CID_VCENTER:
270 /* not handled here, handled by VideoOrientation interface */
273 case V4L2_CID_AUDIO_VOLUME:
274 case V4L2_CID_AUDIO_BALANCE:
275 case V4L2_CID_AUDIO_BASS:
276 case V4L2_CID_AUDIO_TREBLE:
277 case V4L2_CID_AUDIO_MUTE:
278 case V4L2_CID_AUDIO_LOUDNESS:
279 /* FIXME: We should implement GstMixer interface */
283 "ControlID %s (%x) unhandled, FIXME", control.name, n);
290 GST_DEBUG_OBJECT (e, "Adding ControlID %s (%x)", control.name, n);
291 v4l2channel = g_object_new (GST_TYPE_V4L2_COLOR_BALANCE_CHANNEL, NULL);
292 channel = GST_COLOR_BALANCE_CHANNEL (v4l2channel);
293 channel->label = g_strdup ((const gchar *) control.name);
296 #if 0 /* FIXME: it will be need just when handling private controls
297 (currently none of base controls are of this type) */
298 if (control.type == V4L2_CTRL_TYPE_MENU) {
299 struct v4l2_querymenu menu, *mptr;
305 if (ioctl (v4l2object->video_fd, VIDIOC_QUERYMENU, &menu) < 0) {
307 break; /* end of enumeration */
309 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
310 (_("Failed getting controls attributes on device '%s'."),
311 v4l2object->videodev),
312 ("Failed to get %d in menu enumeration for %s. (%d - %s)",
313 n, v4l2object->videodev, errno, strerror (errno)));
317 mptr = g_malloc (sizeof (menu));
318 memcpy (mptr, &menu, sizeof (menu));
319 menus = g_list_append (menus, mptr);
322 v4l2object->menus = g_list_append (v4l2object->menus, menus);
325 switch (control.type) {
326 case V4L2_CTRL_TYPE_INTEGER:
327 channel->min_value = control.minimum;
328 channel->max_value = control.maximum;
330 case V4L2_CTRL_TYPE_BOOLEAN:
331 channel->min_value = FALSE;
332 channel->max_value = TRUE;
335 /* FIXME we should find out how to handle V4L2_CTRL_TYPE_BUTTON.
336 BUTTON controls like V4L2_CID_DO_WHITE_BALANCE can just be set (1) or
337 unset (0), but can't be queried */
339 "Control with non supported type %s (%x), type=%d",
340 control.name, n, control.type);
341 channel->min_value = channel->max_value = 0;
345 v4l2object->colors = g_list_append (v4l2object->colors, (gpointer) channel);
348 GST_DEBUG_OBJECT (e, "done");
354 gst_v4l2_empty_lists (GstV4l2Object * v4l2object)
356 GST_DEBUG_OBJECT (v4l2object->element, "deleting enumerations");
358 g_list_foreach (v4l2object->channels, (GFunc) g_object_unref, NULL);
359 g_list_free (v4l2object->channels);
360 v4l2object->channels = NULL;
362 g_list_foreach (v4l2object->norms, (GFunc) g_object_unref, NULL);
363 g_list_free (v4l2object->norms);
364 v4l2object->norms = NULL;
366 g_list_foreach (v4l2object->colors, (GFunc) g_object_unref, NULL);
367 g_list_free (v4l2object->colors);
368 v4l2object->colors = NULL;
371 /******************************************************
373 * open the video device (v4l2object->videodev)
374 * return value: TRUE on success, FALSE on error
375 ******************************************************/
377 gst_v4l2_open (GstV4l2Object * v4l2object)
381 GST_DEBUG_OBJECT (v4l2object->element, "Trying to open device %s",
382 v4l2object->videodev);
384 GST_V4L2_CHECK_NOT_OPEN (v4l2object);
385 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
387 /* be sure we have a device */
388 if (!v4l2object->videodev)
389 v4l2object->videodev = g_strdup ("/dev/video");
391 /* check if it is a device */
392 if (stat (v4l2object->videodev, &st) == -1)
395 if (!S_ISCHR (st.st_mode))
398 /* open the device */
399 v4l2object->video_fd =
400 open (v4l2object->videodev, O_RDWR /* | O_NONBLOCK */ );
402 if (!GST_V4L2_IS_OPEN (v4l2object))
405 /* get capabilities, error will be posted */
406 if (!gst_v4l2_get_capabilities (v4l2object))
409 /* do we need to be a capture device? */
410 if (GST_IS_V4L2SRC (v4l2object) &&
411 !(v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
414 /* create enumerations, posts errors. */
415 if (!gst_v4l2_fill_lists (v4l2object))
418 GST_INFO_OBJECT (v4l2object->element,
419 "Opened device '%s' (%s) successfully",
420 v4l2object->vcap.card, v4l2object->videodev);
427 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
428 (_("Cannot identify device '%s'."), v4l2object->videodev),
434 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
435 (_("This isn't a device '%s'."), v4l2object->videodev),
441 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
442 (_("Could not open device '%s' for reading and writing."),
443 v4l2object->videodev), GST_ERROR_SYSTEM);
448 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
449 (_("Device '%s' is not a capture device."),
450 v4l2object->videodev),
451 ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
456 if (GST_V4L2_IS_OPEN (v4l2object)) {
458 close (v4l2object->video_fd);
459 v4l2object->video_fd = -1;
462 gst_v4l2_empty_lists (v4l2object);
469 /******************************************************
471 * close the video device (v4l2object->video_fd)
472 * return value: TRUE on success, FALSE on error
473 ******************************************************/
475 gst_v4l2_close (GstV4l2Object * v4l2object)
477 GST_DEBUG_OBJECT (v4l2object->element, "Trying to close %s",
478 v4l2object->videodev);
480 GST_V4L2_CHECK_OPEN (v4l2object);
481 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
484 close (v4l2object->video_fd);
485 v4l2object->video_fd = -1;
488 gst_v4l2_empty_lists (v4l2object);
494 /******************************************************
495 * gst_v4l2_get_norm()
496 * Get the norm of the current device
497 * return value: TRUE on success, FALSE on error
498 ******************************************************/
500 gst_v4l2_get_norm (GstV4l2Object * v4l2object, v4l2_std_id * norm)
502 GST_DEBUG_OBJECT (v4l2object->element, "getting norm");
504 if (!GST_V4L2_IS_OPEN (v4l2object))
507 if (ioctl (v4l2object->video_fd, VIDIOC_G_STD, norm) < 0)
515 GST_DEBUG ("Failed to get the current norm for device %s",
516 v4l2object->videodev);
522 /******************************************************
523 * gst_v4l2_set_norm()
524 * Set the norm of the current device
525 * return value: TRUE on success, FALSE on error
526 ******************************************************/
528 gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm)
530 GST_DEBUG_OBJECT (v4l2object->element, "trying to set norm to %llx", norm);
532 if (!GST_V4L2_IS_OPEN (v4l2object))
535 if (ioctl (v4l2object->video_fd, VIDIOC_S_STD, &norm) < 0)
543 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
544 (_("Failed to set norm for device '%s'."),
545 v4l2object->videodev), GST_ERROR_SYSTEM);
550 /******************************************************
551 * gst_v4l2_get_frequency():
552 * get the current frequency
553 * return value: TRUE on success, FALSE on error
554 ******************************************************/
556 gst_v4l2_get_frequency (GstV4l2Object * v4l2object,
557 gint tunernum, gulong * frequency)
559 struct v4l2_frequency freq;
560 GstTunerChannel *channel;
562 GST_DEBUG_OBJECT (v4l2object->element, "getting current tuner frequency");
564 if (!GST_V4L2_IS_OPEN (v4l2object))
567 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
569 freq.tuner = tunernum;
570 if (ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0)
573 *frequency = freq.frequency * channel->freq_multiplicator;
580 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
581 (_("Failed to get current tuner frequency for device '%s'."),
582 v4l2object->videodev), GST_ERROR_SYSTEM);
588 /******************************************************
589 * gst_v4l2_set_frequency():
591 * return value: TRUE on success, FALSE on error
592 ******************************************************/
594 gst_v4l2_set_frequency (GstV4l2Object * v4l2object,
595 gint tunernum, gulong frequency)
597 struct v4l2_frequency freq;
598 GstTunerChannel *channel;
600 GST_DEBUG_OBJECT (v4l2object->element,
601 "setting current tuner frequency to %lu", frequency);
603 if (!GST_V4L2_IS_OPEN (v4l2object))
606 channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
608 freq.tuner = tunernum;
609 /* fill in type - ignore error */
610 ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq);
611 freq.frequency = frequency / channel->freq_multiplicator;
613 if (ioctl (v4l2object->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0)
621 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
622 (_("Failed to set current tuner frequency for device '%s' to %lu Hz."),
623 v4l2object->videodev, frequency), GST_ERROR_SYSTEM);
628 /******************************************************
629 * gst_v4l2_signal_strength():
630 * get the strength of the signal on the current input
631 * return value: TRUE on success, FALSE on error
632 ******************************************************/
634 gst_v4l2_signal_strength (GstV4l2Object * v4l2object,
635 gint tunernum, gulong * signal_strength)
637 struct v4l2_tuner tuner;
639 GST_DEBUG_OBJECT (v4l2object->element, "trying to get signal strength");
641 if (!GST_V4L2_IS_OPEN (v4l2object))
644 tuner.index = tunernum;
645 if (ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &tuner) < 0)
648 *signal_strength = tuner.signal;
655 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
656 (_("Failed to get signal strength for device '%s'."),
657 v4l2object->videodev), GST_ERROR_SYSTEM);
662 /******************************************************
663 * gst_v4l2_get_attribute():
664 * try to get the value of one specific attribute
665 * return value: TRUE on success, FALSE on error
666 ******************************************************/
668 gst_v4l2_get_attribute (GstV4l2Object * v4l2object,
669 int attribute_num, int *value)
671 struct v4l2_control control;
673 GST_DEBUG_OBJECT (v4l2object->element, "getting value of attribute %d",
676 if (!GST_V4L2_IS_OPEN (v4l2object))
679 control.id = attribute_num;
681 if (ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0)
684 *value = control.value;
691 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
692 (_("Failed to get value for control %d on device '%s'."),
693 attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
699 /******************************************************
700 * gst_v4l2_set_attribute():
701 * try to set the value of one specific attribute
702 * return value: TRUE on success, FALSE on error
703 ******************************************************/
705 gst_v4l2_set_attribute (GstV4l2Object * v4l2object,
706 int attribute_num, const int value)
708 struct v4l2_control control;
710 GST_DEBUG_OBJECT (v4l2object->element, "setting value of attribute %d to %d",
711 attribute_num, value);
713 if (!GST_V4L2_IS_OPEN (v4l2object))
716 control.id = attribute_num;
717 control.value = value;
718 if (ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0)
726 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
727 (_("Failed to set value %d for control %d on device '%s'."),
728 value, attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
734 gst_v4l2_get_input (GstV4l2Object * v4l2object, gint * input)
738 GST_DEBUG_OBJECT (v4l2object->element, "trying to get input");
740 if (!GST_V4L2_IS_OPEN (v4l2object))
743 if (ioctl (v4l2object->video_fd, VIDIOC_G_INPUT, &n) < 0)
748 GST_DEBUG_OBJECT (v4l2object->element, "input: %d", n);
755 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
756 (_("Failed to get current input on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
762 gst_v4l2_set_input (GstV4l2Object * v4l2object, gint input)
764 GST_DEBUG_OBJECT (v4l2object->element, "trying to set input to %d", input);
766 if (!GST_V4L2_IS_OPEN (v4l2object))
769 if (ioctl (v4l2object->video_fd, VIDIOC_S_INPUT, &input) < 0)
777 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
778 (_("Failed to set input %d on device %s."),
779 input, v4l2object->videodev), GST_ERROR_SYSTEM);