1 /* G-Streamer generic V4L2 element - generic V4L2 calls handling
2 * Copyright (C) 2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include <sys/types.h>
27 #include <sys/ioctl.h>
32 #include "v4l2_calls.h"
33 #include "gstv4l2tuner.h"
34 #include "gstv4l2xoverlay.h"
35 #include "gstv4l2colorbalance.h"
37 #define DEBUG(format, args...) \
39 GST_ELEMENT(v4l2element), \
40 "V4L2: " format, ##args)
43 /******************************************************
44 * gst_v4l2_get_capabilities():
45 * get the device's capturing capabilities
46 * return value: TRUE on success, FALSE on error
47 ******************************************************/
50 gst_v4l2_get_capabilities (GstV4l2Element *v4l2element)
52 DEBUG("getting capabilities");
53 GST_V4L2_CHECK_OPEN(v4l2element);
55 if (ioctl(v4l2element->video_fd, VIDIOC_QUERYCAP, &(v4l2element->vcap)) < 0) {
56 gst_element_error(GST_ELEMENT(v4l2element),
57 "Error getting %s capabilities: %s",
58 v4l2element->device, g_strerror(errno));
66 /******************************************************
67 * gst_v4l2_empty_lists() and gst_v4l2_fill_lists():
68 * fill/empty the lists of enumerations
69 * return value: TRUE on success, FALSE on error
70 ******************************************************/
73 gst_v4l2_fill_lists (GstV4l2Element *v4l2element)
77 gst_element_get_pad_list (GST_ELEMENT (v4l2element));
78 GstPadDirection dir = GST_PAD_UNKNOWN;
80 DEBUG("getting enumerations");
81 GST_V4L2_CHECK_OPEN(v4l2element);
83 /* sinks have outputs, all others have inputs */
84 if (pads && g_list_length ((GList *) pads) == 1)
85 dir = GST_PAD_DIRECTION (GST_PAD (pads->data));
87 if (dir != GST_PAD_SINK) {
88 /* and now, the inputs */
90 struct v4l2_input input;
91 GstV4l2TunerChannel *v4l2channel;
92 GstTunerChannel *channel;
95 if (ioctl(v4l2element->video_fd, VIDIOC_ENUMINPUT,
98 break; /* end of enumeration */
100 gst_element_error(GST_ELEMENT(v4l2element),
101 "Failed to get no. %d in input enumeration for %s: %s",
102 n, v4l2element->device,
109 g_object_new(GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
110 channel = GST_TUNER_CHANNEL(v4l2channel);
111 channel->label = g_strdup(input.name);
112 channel->flags = GST_TUNER_CHANNEL_INPUT;
113 v4l2channel->index = n;
114 if (input.type == V4L2_INPUT_TYPE_TUNER) {
115 struct v4l2_tuner vtun;
117 v4l2channel->tuner = input.tuner;
118 channel->flags |= GST_TUNER_CHANNEL_FREQUENCY;
120 vtun.index = input.tuner;
121 if (ioctl(v4l2element->video_fd, VIDIOC_G_TUNER,
123 gst_element_error(GST_ELEMENT(v4l2element),
124 "Failed to get tuner %d settings on %s: %s",
128 g_object_unref(G_OBJECT(channel));
131 channel->min_frequency = vtun.rangelow;
132 channel->max_frequency = vtun.rangehigh;
133 channel->min_signal = 0;
134 channel->max_signal = 0xffff;
136 if (input.audioset) {
137 /* we take the first. We don't care for
138 * the others for now */
139 while (!(input.audioset &
140 (1<<v4l2channel->audio)))
141 v4l2channel->audio++;
142 channel->flags |= GST_TUNER_CHANNEL_AUDIO;
145 v4l2element->channels =
146 g_list_append(v4l2element->channels,
152 struct v4l2_output output;
153 GstV4l2TunerChannel *v4l2channel;
154 GstTunerChannel *channel;
157 if (ioctl(v4l2element->video_fd, VIDIOC_ENUMOUTPUT,
160 break; /* end of enumeration */
162 gst_element_error(GST_ELEMENT(v4l2element),
163 "Failed to get no. %d in output enumeration for %s: %s",
164 n, v4l2element->device,
170 v4l2channel = g_object_new(GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
171 channel = GST_TUNER_CHANNEL(v4l2channel);
172 channel->label = g_strdup(output.name);
173 channel->flags = GST_TUNER_CHANNEL_OUTPUT;
174 v4l2channel->index = n;
175 if (output.audioset) {
176 /* we take the first. We don't care for
177 * the others for now */
178 while (!(output.audioset &
179 (1<<v4l2channel->audio)))
180 v4l2channel->audio++;
181 channel->flags |= GST_TUNER_CHANNEL_AUDIO;
184 v4l2element->channels =
185 g_list_append(v4l2element->channels,
192 struct v4l2_standard standard;
193 GstV4l2TunerNorm *v4l2norm;
197 if (ioctl(v4l2element->video_fd, VIDIOC_ENUMSTD, &standard) < 0) {
199 break; /* end of enumeration */
201 gst_element_error(GST_ELEMENT(v4l2element),
202 "Failed to get no. %d in norm enumeration for %s: %s",
203 n, v4l2element->device, g_strerror(errno));
208 v4l2norm = g_object_new(GST_TYPE_V4L2_TUNER_NORM, NULL);
209 norm = GST_TUNER_NORM (v4l2norm);
210 norm->label = g_strdup(standard.name);
211 norm->fps = (gfloat) standard.frameperiod.denominator /
212 standard.frameperiod.numerator;
213 v4l2norm->index = standard.id;
215 v4l2element->norms = g_list_append(v4l2element->norms,
219 /* and lastly, controls+menus (if appropriate) */
220 for (n=V4L2_CID_BASE;;n++) {
221 struct v4l2_queryctrl control;
222 GstV4l2ColorBalanceChannel *v4l2channel;
223 GstColorBalanceChannel *channel;
226 if (n == V4L2_CID_LASTP1)
227 n = V4L2_CID_PRIVATE_BASE;
230 if (ioctl(v4l2element->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
231 if (errno == EINVAL) {
232 if (n < V4L2_CID_PRIVATE_BASE)
237 gst_element_error(GST_ELEMENT(v4l2element),
238 "Failed to get no. %d in control enumeration for %s: %s",
239 n, v4l2element->device, g_strerror(errno));
243 if (control.flags & V4L2_CTRL_FLAG_DISABLED)
247 case V4L2_CID_BRIGHTNESS:
248 case V4L2_CID_CONTRAST:
249 case V4L2_CID_SATURATION:
251 case V4L2_CID_BLACK_LEVEL:
252 case V4L2_CID_AUTO_WHITE_BALANCE:
253 case V4L2_CID_DO_WHITE_BALANCE:
254 case V4L2_CID_RED_BALANCE:
255 case V4L2_CID_BLUE_BALANCE:
257 case V4L2_CID_EXPOSURE:
258 case V4L2_CID_AUTOGAIN:
260 /* we only handle these for now */
263 DEBUG("ControlID %s (%d) unhandled, FIXME",
271 v4l2channel = g_object_new(GST_TYPE_V4L2_COLOR_BALANCE_CHANNEL,
273 channel = GST_COLOR_BALANCE_CHANNEL(v4l2channel);
274 channel->label = g_strdup(control.name);
275 v4l2channel->index = n;
278 if (control.type == V4L2_CTRL_TYPE_MENU) {
279 struct v4l2_querymenu menu, *mptr;
284 if (ioctl(v4l2element->video_fd, VIDIOC_QUERYMENU, &menu) < 0) {
286 break; /* end of enumeration */
288 gst_element_error(GST_ELEMENT(v4l2element),
289 "Failed to get no. %d in menu %d enumeration for %s: %s",
290 i, n, v4l2element->device, g_strerror(errno));
294 mptr = g_malloc(sizeof(menu));
295 memcpy(mptr, &menu, sizeof(menu));
296 menus = g_list_append(menus, mptr);
299 v4l2element->menus = g_list_append(v4l2element->menus, menus);
302 switch (control.type) {
303 case V4L2_CTRL_TYPE_INTEGER:
304 channel->min_value = control.minimum;
305 channel->max_value = control.maximum;
307 case V4L2_CTRL_TYPE_BOOLEAN:
308 channel->min_value = FALSE;
309 channel->max_value = TRUE;
313 channel->max_value = 0;
317 v4l2element->colors = g_list_append(v4l2element->colors,
326 gst_v4l2_empty_lists (GstV4l2Element *v4l2element)
328 DEBUG("deleting enumerations");
330 g_list_foreach (v4l2element->channels, (GFunc) g_object_unref, NULL);
331 g_list_free (v4l2element->channels);
332 v4l2element->channels = NULL;
334 g_list_foreach (v4l2element->norms, (GFunc) g_object_unref, NULL);
335 g_list_free (v4l2element->norms);
336 v4l2element->norms = NULL;
338 g_list_foreach (v4l2element->colors, (GFunc) g_object_unref, NULL);
339 g_list_free (v4l2element->colors);
340 v4l2element->colors = NULL;
344 /******************************************************
346 * open the video device (v4l2element->device)
347 * return value: TRUE on success, FALSE on error
348 ******************************************************/
351 gst_v4l2_open (GstV4l2Element *v4l2element)
353 DEBUG("Trying to open device %s", v4l2element->device);
354 GST_V4L2_CHECK_NOT_OPEN(v4l2element);
355 GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
357 /* be sure we have a device */
358 if (!v4l2element->device)
359 v4l2element->device = g_strdup("/dev/video");
361 /* open the device */
362 v4l2element->video_fd = open(v4l2element->device, O_RDWR);
363 if (!GST_V4L2_IS_OPEN(v4l2element)) {
364 gst_element_error(GST_ELEMENT(v4l2element),
365 "Failed to open device %s: %s",
366 v4l2element->device, g_strerror(errno));
370 /* get capabilities */
371 if (!gst_v4l2_get_capabilities(v4l2element)) {
375 /* create enumerations */
376 if (!gst_v4l2_fill_lists(v4l2element))
379 gst_info("Opened device '%s' (%s) successfully\n",
380 v4l2element->vcap.card, v4l2element->device);
385 if (GST_V4L2_IS_OPEN(v4l2element)) {
387 close(v4l2element->video_fd);
388 v4l2element->video_fd = -1;
391 gst_v4l2_empty_lists(v4l2element);
397 /******************************************************
399 * close the video device (v4l2element->video_fd)
400 * return value: TRUE on success, FALSE on error
401 ******************************************************/
404 gst_v4l2_close (GstV4l2Element *v4l2element)
406 DEBUG("Trying to close %s", v4l2element->device);
407 GST_V4L2_CHECK_OPEN(v4l2element);
408 GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
411 close(v4l2element->video_fd);
412 v4l2element->video_fd = -1;
415 gst_v4l2_empty_lists(v4l2element);
421 /******************************************************
422 * gst_v4l2_get_norm()
423 * Get the norm of the current device
424 * return value: TRUE on success, FALSE on error
425 ******************************************************/
428 gst_v4l2_get_norm (GstV4l2Element *v4l2element,
431 DEBUG("getting norm");
432 GST_V4L2_CHECK_OPEN(v4l2element);
434 if (ioctl(v4l2element->video_fd, VIDIOC_G_STD, norm) < 0) {
435 gst_element_error(GST_ELEMENT(v4l2element),
436 "Failed to get the current norm for device %s: %s",
437 v4l2element->device, g_strerror(errno));
445 /******************************************************
446 * gst_v4l2_set_norm()
447 * Set the norm of the current device
448 * return value: TRUE on success, FALSE on error
449 ******************************************************/
452 gst_v4l2_set_norm (GstV4l2Element *v4l2element,
455 DEBUG("trying to set norm to %llx", norm);
456 GST_V4L2_CHECK_OPEN(v4l2element);
457 GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
459 if (ioctl(v4l2element->video_fd, VIDIOC_S_STD, &norm) < 0) {
460 gst_element_error(GST_ELEMENT(v4l2element),
461 "Failed to set norm 0x%llx for device %s: %s",
462 norm, v4l2element->device, g_strerror(errno));
470 /******************************************************
471 * gst_v4l2_get_input()
472 * Get the input of the current device
473 * return value: TRUE on success, FALSE on error
474 ******************************************************/
477 gst_v4l2_get_input (GstV4l2Element *v4l2element,
482 DEBUG("trying to get input");
483 GST_V4L2_CHECK_OPEN(v4l2element);
485 if (ioctl(v4l2element->video_fd, VIDIOC_G_INPUT, &n) < 0) {
486 gst_element_error(GST_ELEMENT(v4l2element),
487 "Failed to get current input on device %s: %s",
488 v4l2element->device, g_strerror(errno));
498 /******************************************************
499 * gst_v4l2_set_input()
500 * Set the input of the current device
501 * return value: TRUE on success, FALSE on error
502 ******************************************************/
505 gst_v4l2_set_input (GstV4l2Element *v4l2element,
508 DEBUG("trying to set input to %d", input);
509 GST_V4L2_CHECK_OPEN(v4l2element);
510 GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
512 if (ioctl(v4l2element->video_fd, VIDIOC_S_INPUT, &input) < 0) {
513 gst_element_error(GST_ELEMENT(v4l2element),
514 "Failed to set input %d on device %s: %s",
515 input, v4l2element->device, g_strerror(errno));
523 /******************************************************
524 * gst_v4l2_get_output()
525 * Get the output of the current device
526 * return value: TRUE on success, FALSE on error
527 ******************************************************/
530 gst_v4l2_get_output (GstV4l2Element *v4l2element,
535 DEBUG("trying to get output");
536 GST_V4L2_CHECK_OPEN(v4l2element);
538 if (ioctl(v4l2element->video_fd, VIDIOC_G_OUTPUT, &n) < 0) {
539 gst_element_error(GST_ELEMENT(v4l2element),
540 "Failed to get current output on device %s: %s",
541 v4l2element->device, g_strerror(errno));
551 /******************************************************
552 * gst_v4l2_set_output()
553 * Set the output of the current device
554 * return value: TRUE on success, FALSE on error
555 ******************************************************/
558 gst_v4l2_set_output (GstV4l2Element *v4l2element,
561 DEBUG("trying to set output to %d", output);
562 GST_V4L2_CHECK_OPEN(v4l2element);
563 GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
565 if (ioctl(v4l2element->video_fd, VIDIOC_S_OUTPUT, &output) < 0) {
566 gst_element_error(GST_ELEMENT(v4l2element),
567 "Failed to set output %d on device %s: %s",
568 output, v4l2element->device, g_strerror(errno));
576 /******************************************************
577 * gst_v4l2_get_frequency():
578 * get the current frequency
579 * return value: TRUE on success, FALSE on error
580 ******************************************************/
583 gst_v4l2_get_frequency (GstV4l2Element *v4l2element,
587 struct v4l2_frequency freq;
589 DEBUG("getting current tuner frequency");
590 GST_V4L2_CHECK_OPEN(v4l2element);
592 freq.tuner = tunernum;
593 if (ioctl(v4l2element->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0) {
594 gst_element_error(GST_ELEMENT(v4l2element),
595 "Failed to get current tuner frequency for device %s: %s",
596 v4l2element->device, g_strerror(errno));
600 *frequency = freq.frequency;
606 /******************************************************
607 * gst_v4l2_set_frequency():
609 * return value: TRUE on success, FALSE on error
610 ******************************************************/
613 gst_v4l2_set_frequency (GstV4l2Element *v4l2element,
617 struct v4l2_frequency freq;
619 DEBUG("setting current tuner frequency to %lu", frequency);
620 GST_V4L2_CHECK_OPEN(v4l2element);
621 GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
623 freq.tuner = tunernum;
624 /* fill in type - ignore error */
625 ioctl(v4l2element->video_fd, VIDIOC_G_FREQUENCY, &freq);
626 freq.frequency = frequency;
628 if (ioctl(v4l2element->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0) {
629 gst_element_error(GST_ELEMENT(v4l2element),
630 "Failed to set tuner frequency to %lu for device %s: %s",
631 frequency, v4l2element->device, g_strerror(errno));
639 /******************************************************
640 * gst_v4l2_signal_strength():
641 * get the strength of the signal on the current input
642 * return value: TRUE on success, FALSE on error
643 ******************************************************/
646 gst_v4l2_signal_strength (GstV4l2Element *v4l2element,
648 gulong *signal_strength)
650 struct v4l2_tuner tuner;
652 DEBUG("trying to get signal strength");
653 GST_V4L2_CHECK_OPEN(v4l2element);
655 tuner.index = tunernum;
656 if (ioctl(v4l2element->video_fd, VIDIOC_G_TUNER, &tuner) < 0) {
657 gst_element_error(GST_ELEMENT(v4l2element),
658 "Failed to get signal strength for device %s: %s",
659 v4l2element->device, g_strerror(errno));
663 *signal_strength = tuner.signal;
669 /******************************************************
670 * gst_v4l2_get_attribute():
671 * try to get the value of one specific attribute
672 * return value: TRUE on success, FALSE on error
673 ******************************************************/
676 gst_v4l2_get_attribute (GstV4l2Element *v4l2element,
680 struct v4l2_control control;
682 GST_V4L2_CHECK_OPEN(v4l2element);
684 DEBUG("getting value of attribute %d", attribute_num);
686 control.id = attribute_num;
688 if (ioctl(v4l2element->video_fd, VIDIOC_G_CTRL, &control) < 0) {
689 gst_element_error(GST_ELEMENT(v4l2element),
690 "Failed to get value for control %d on device %s: %s",
691 attribute_num, v4l2element->device, g_strerror(errno));
695 *value = control.value;
701 /******************************************************
702 * gst_v4l2_set_attribute():
703 * try to set the value of one specific attribute
704 * return value: TRUE on success, FALSE on error
705 ******************************************************/
708 gst_v4l2_set_attribute (GstV4l2Element *v4l2element,
712 struct v4l2_control control;
714 GST_V4L2_CHECK_OPEN(v4l2element);
716 DEBUG("setting value of attribute %d to %d", attribute_num, value);
718 control.id = attribute_num;
719 control.value = value;
721 if (ioctl(v4l2element->video_fd, VIDIOC_S_CTRL, &control) < 0) {
722 gst_element_error(GST_ELEMENT(v4l2element),
723 "Failed to set value %d for control %d on device %s: %s",
724 value, attribute_num, v4l2element->device, g_strerror(errno));