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>
31 #include "v4l2_calls.h"
33 #define DEBUG(format, args...) \
34 GST_DEBUG_ELEMENT(GST_CAT_PLUGIN_INFO, \
35 GST_ELEMENT(v4l2element), \
36 "V4L2: " format, ##args)
39 /******************************************************
40 * gst_v4l2_get_capabilities():
41 * get the device's capturing capabilities
42 * return value: TRUE on success, FALSE on error
43 ******************************************************/
46 gst_v4l2_get_capabilities (GstV4l2Element *v4l2element)
48 DEBUG("getting capabilities");
49 GST_V4L2_CHECK_OPEN(v4l2element);
51 if (ioctl(v4l2element->video_fd, VIDIOC_QUERYCAP, &(v4l2element->vcap)) < 0) {
52 gst_element_error(GST_ELEMENT(v4l2element),
53 "Error getting %s capabilities: %s",
54 v4l2element->device, g_strerror(errno));
62 /******************************************************
63 * gst_v4l2_empty_lists() and gst_v4l2_fill_lists():
64 * fill/empty the lists of enumerations
65 * return value: TRUE on success, FALSE on error
66 ******************************************************/
69 gst_v4l2_fill_lists (GstV4l2Element *v4l2element)
73 DEBUG("getting enumerations");
74 GST_V4L2_CHECK_OPEN(v4l2element);
76 /* and now, the inputs */
78 struct v4l2_input input, *inpptr;
80 if (ioctl(v4l2element->video_fd, VIDIOC_ENUMINPUT, &input) < 0) {
82 break; /* end of enumeration */
84 gst_element_error(GST_ELEMENT(v4l2element),
85 "Failed to get no. %d in input enumeration for %s: %s",
86 n, v4l2element->device, g_strerror(errno));
90 inpptr = g_malloc(sizeof(input));
91 memcpy(inpptr, &input, sizeof(input));
92 v4l2element->inputs = g_list_append(v4l2element->inputs, inpptr);
94 v4l2element->input_names = g_list_append(v4l2element->input_names, inpptr->name);
99 struct v4l2_output output, *outptr;
101 if (ioctl(v4l2element->video_fd, VIDIOC_ENUMOUTPUT, &output) < 0) {
103 break; /* end of enumeration */
105 gst_element_error(GST_ELEMENT(v4l2element),
106 "Failed to get no. %d in output enumeration for %s: %s",
107 n, v4l2element->device, g_strerror(errno));
111 outptr = g_malloc(sizeof(output));
112 memcpy(outptr, &output, sizeof(output));
113 v4l2element->outputs = g_list_append(v4l2element->outputs, outptr);
115 v4l2element->output_names = g_list_append(v4l2element->output_names, outptr->name);
120 struct v4l2_standard standard, *stdptr;
122 if (ioctl(v4l2element->video_fd, VIDIOC_ENUMSTD, &standard) < 0) {
124 break; /* end of enumeration */
126 gst_element_error(GST_ELEMENT(v4l2element),
127 "Failed to get no. %d in norm enumeration for %s: %s",
128 n, v4l2element->device, g_strerror(errno));
132 stdptr = g_malloc(sizeof(standard));
133 memcpy(stdptr, &standard, sizeof(standard));
134 v4l2element->norms = g_list_append(v4l2element->norms, stdptr);
136 v4l2element->norm_names = g_list_append(v4l2element->norm_names, stdptr->name);
139 /* and lastly, controls+menus (if appropriate) */
140 for (n=V4L2_CID_BASE;;n++) {
141 struct v4l2_queryctrl control, *ctrlptr;
143 GParamSpec *spec = NULL;
145 if (n == V4L2_CID_LASTP1)
146 n = V4L2_CID_PRIVATE_BASE;
148 if (ioctl(v4l2element->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
149 if (errno == EINVAL) {
150 if (n < V4L2_CID_PRIVATE_BASE)
155 gst_element_error(GST_ELEMENT(v4l2element),
156 "Failed to get no. %d in control enumeration for %s: %s",
157 n, v4l2element->device, g_strerror(errno));
161 if (control.flags & V4L2_CTRL_FLAG_DISABLED)
163 ctrlptr = g_malloc(sizeof(control));
164 memcpy(ctrlptr, &control, sizeof(control));
165 v4l2element->controls = g_list_append(v4l2element->controls, ctrlptr);
166 if (control.type == V4L2_CTRL_TYPE_MENU) {
167 struct v4l2_querymenu menu, *mptr;
172 if (ioctl(v4l2element->video_fd, VIDIOC_QUERYMENU, &menu) < 0) {
174 break; /* end of enumeration */
176 gst_element_error(GST_ELEMENT(v4l2element),
177 "Failed to get no. %d in menu %d enumeration for %s: %s",
178 i, n, v4l2element->device, g_strerror(errno));
182 mptr = g_malloc(sizeof(menu));
183 memcpy(mptr, &menu, sizeof(menu));
184 menus = g_list_append(menus, mptr);
187 v4l2element->menus = g_list_append(v4l2element->menus, menus);
189 switch (control.type) {
190 case V4L2_CTRL_TYPE_INTEGER:
191 spec = g_param_spec_int(ctrlptr->name, ctrlptr->name,
192 ctrlptr->name, ctrlptr->minimum, ctrlptr->maximum,
193 ctrlptr->default_value, G_PARAM_READWRITE);
195 case V4L2_CTRL_TYPE_BOOLEAN:
196 spec = g_param_spec_boolean(ctrlptr->name, ctrlptr->name,
197 ctrlptr->name, ctrlptr->default_value,
200 case V4L2_CTRL_TYPE_MENU:
201 /* hacky... we abuse pointer for 'no value' */
202 spec = g_param_spec_pointer(ctrlptr->name, ctrlptr->name,
203 ctrlptr->name, G_PARAM_WRITABLE);
205 case V4L2_CTRL_TYPE_BUTTON:
211 v4l2element->control_specs = g_list_append(v4l2element->control_specs, spec);
219 gst_v4l2_empty_lists (GstV4l2Element *v4l2element)
221 DEBUG("deleting enumerations");
224 while (g_list_length(v4l2element->inputs) > 0) {
225 gpointer data = g_list_nth_data(v4l2element->inputs, 0);
226 v4l2element->inputs = g_list_remove(v4l2element->inputs, data);
229 g_list_free(v4l2element->input_names);
230 v4l2element->input_names = NULL;
231 while (g_list_length(v4l2element->outputs) > 0) {
232 gpointer data = g_list_nth_data(v4l2element->outputs, 0);
233 v4l2element->outputs = g_list_remove(v4l2element->outputs, data);
236 g_list_free(v4l2element->output_names);
237 v4l2element->output_names = NULL;
238 while (g_list_length(v4l2element->norms) > 0) {
239 gpointer data = g_list_nth_data(v4l2element->norms, 0);
240 v4l2element->norms = g_list_remove(v4l2element->norms, data);
243 g_list_free(v4l2element->norm_names);
244 v4l2element->norm_names = NULL;
245 while (g_list_length(v4l2element->controls) > 0) {
246 gpointer data = g_list_nth_data(v4l2element->controls, 0);
247 v4l2element->controls = g_list_remove(v4l2element->controls, data);
250 v4l2element->menus = g_list_remove_all(v4l2element->menus, NULL);
251 while (g_list_length(v4l2element->menus) > 0) {
252 GList *items = (GList *) g_list_nth_data(v4l2element->menus, 0);
253 v4l2element->inputs = g_list_remove(v4l2element->inputs, items);
254 while (g_list_length(items) > 0) {
255 gpointer data = g_list_nth_data(v4l2element->menus, 0);
256 items = g_list_remove(items, data);
260 while (g_list_length(v4l2element->control_specs) > 0) {
261 gpointer data = g_list_nth_data(v4l2element->control_specs, 0);
262 v4l2element->control_specs = g_list_remove(v4l2element->control_specs, data);
263 g_param_spec_unref(G_PARAM_SPEC(data));
268 /******************************************************
270 * open the video device (v4l2element->device)
271 * return value: TRUE on success, FALSE on error
272 ******************************************************/
275 gst_v4l2_open (GstV4l2Element *v4l2element)
277 DEBUG("Trying to open device %s", v4l2element->device);
278 GST_V4L2_CHECK_NOT_OPEN(v4l2element);
279 GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
281 /* be sure we have a device */
282 if (!v4l2element->device)
283 v4l2element->device = g_strdup("/dev/video");
285 /* open the device */
286 v4l2element->video_fd = open(v4l2element->device, O_RDWR);
287 if (!GST_V4L2_IS_OPEN(v4l2element)) {
288 gst_element_error(GST_ELEMENT(v4l2element),
289 "Failed to open device %s: %s",
290 v4l2element->device, g_strerror(errno));
294 /* get capabilities */
295 if (!gst_v4l2_get_capabilities(v4l2element)) {
299 /* create enumerations */
300 if (!gst_v4l2_fill_lists(v4l2element))
303 gst_info("Opened device '%s' (%s) successfully\n",
304 v4l2element->vcap.card, v4l2element->device);
309 if (GST_V4L2_IS_OPEN(v4l2element)) {
311 close(v4l2element->video_fd);
312 v4l2element->video_fd = -1;
315 gst_v4l2_empty_lists(v4l2element);
321 /******************************************************
323 * close the video device (v4l2element->video_fd)
324 * return value: TRUE on success, FALSE on error
325 ******************************************************/
328 gst_v4l2_close (GstV4l2Element *v4l2element)
330 DEBUG("Trying to close %s", v4l2element->device);
331 GST_V4L2_CHECK_OPEN(v4l2element);
332 GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
335 close(v4l2element->video_fd);
336 v4l2element->video_fd = -1;
339 gst_v4l2_empty_lists(v4l2element);
345 /******************************************************
346 * gst_v4l2_get_norm()
347 * Get the norm of the current device
348 * return value: TRUE on success, FALSE on error
349 ******************************************************/
352 gst_v4l2_get_norm (GstV4l2Element *v4l2element,
358 DEBUG("getting norm");
359 GST_V4L2_CHECK_OPEN(v4l2element);
361 if (ioctl(v4l2element->video_fd, VIDIOC_G_STD, &std_id) < 0) {
362 gst_element_error(GST_ELEMENT(v4l2element),
363 "Failed to get the current norm for device %s: %s",
364 v4l2element->device, g_strerror(errno));
368 /* try to find out what norm number this actually is */
369 for (n=0;n<g_list_length(v4l2element->norms);n++) {
370 struct v4l2_standard *stdptr = (struct v4l2_standard *) g_list_nth_data(v4l2element->norms, n);
371 if (stdptr->id == std_id) {
377 gst_element_error(GST_ELEMENT(v4l2element),
378 "Failed to find norm '%llu' in our list of available norms for device %s",
379 std_id, v4l2element->device);
384 /******************************************************
385 * gst_v4l2_set_norm()
386 * Set the norm of the current device
387 * return value: TRUE on success, FALSE on error
388 ******************************************************/
391 gst_v4l2_set_norm (GstV4l2Element *v4l2element,
394 struct v4l2_standard *standard;
396 DEBUG("trying to set norm to %d", norm);
397 GST_V4L2_CHECK_OPEN(v4l2element);
398 GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
400 if (norm < 0 || norm >= g_list_length(v4l2element->norms)) {
401 gst_element_error(GST_ELEMENT(v4l2element),
402 "Invalid norm number %d (%d-%d)",
403 norm, 0, g_list_length(v4l2element->norms));
407 standard = (struct v4l2_standard *) g_list_nth_data(v4l2element->norms, norm);
409 if (ioctl(v4l2element->video_fd, VIDIOC_S_STD, &standard->id) < 0) {
410 gst_element_error(GST_ELEMENT(v4l2element),
411 "Failed to set norm '%s' (%llu) for device %s: %s",
412 standard->name, standard->id, v4l2element->device, g_strerror(errno));
420 /******************************************************
421 * gst_v4l2_get_input()
422 * Get the input of the current device
423 * return value: TRUE on success, FALSE on error
424 ******************************************************/
427 gst_v4l2_get_input (GstV4l2Element *v4l2element,
432 DEBUG("trying to get input");
433 GST_V4L2_CHECK_OPEN(v4l2element);
435 if (ioctl(v4l2element->video_fd, VIDIOC_G_INPUT, &n) < 0) {
436 gst_element_error(GST_ELEMENT(v4l2element),
437 "Failed to get current input on device %s: %s",
438 v4l2element->device, g_strerror(errno));
448 /******************************************************
449 * gst_v4l2_set_input()
450 * Set the input of the current device
451 * return value: TRUE on success, FALSE on error
452 ******************************************************/
455 gst_v4l2_set_input (GstV4l2Element *v4l2element,
458 DEBUG("trying to set input to %d", input);
459 GST_V4L2_CHECK_OPEN(v4l2element);
460 GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
462 if (input < 0 || input >= g_list_length(v4l2element->inputs)) {
463 gst_element_error(GST_ELEMENT(v4l2element),
464 "Invalid input number %d (%d-%d)",
465 input, 0, g_list_length(v4l2element->inputs));
469 if (ioctl(v4l2element->video_fd, VIDIOC_S_INPUT, &input) < 0) {
470 gst_element_error(GST_ELEMENT(v4l2element),
471 "Failed to set input %d on device %s: %s",
472 input, v4l2element->device, g_strerror(errno));
480 /******************************************************
481 * gst_v4l2_get_output()
482 * Get the output of the current device
483 * return value: TRUE on success, FALSE on error
484 ******************************************************/
487 gst_v4l2_get_output (GstV4l2Element *v4l2element,
492 DEBUG("trying to get output");
493 GST_V4L2_CHECK_OPEN(v4l2element);
495 if (ioctl(v4l2element->video_fd, VIDIOC_G_OUTPUT, &n) < 0) {
496 gst_element_error(GST_ELEMENT(v4l2element),
497 "Failed to get current output on device %s: %s",
498 v4l2element->device, g_strerror(errno));
508 /******************************************************
509 * gst_v4l2_set_output()
510 * Set the output of the current device
511 * return value: TRUE on success, FALSE on error
512 ******************************************************/
515 gst_v4l2_set_output (GstV4l2Element *v4l2element,
518 DEBUG("trying to set output to %d", output);
519 GST_V4L2_CHECK_OPEN(v4l2element);
520 GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
522 if (output < 0 || output >= g_list_length(v4l2element->outputs)) {
523 gst_element_error(GST_ELEMENT(v4l2element),
524 "Invalid output number %d (%d-%d)",
525 output, 0, g_list_length(v4l2element->outputs));
529 if (ioctl(v4l2element->video_fd, VIDIOC_S_OUTPUT, &output) < 0) {
530 gst_element_error(GST_ELEMENT(v4l2element),
531 "Failed to set output %d on device %s: %s",
532 output, v4l2element->device, g_strerror(errno));
540 /******************************************************
541 * gst_v4l2_has_tuner():
542 * Check whether the device has a tuner
543 * return value: TRUE if it has a tuner, else FALSE
544 ******************************************************/
547 gst_v4l2_has_tuner (GstV4l2Element *v4l2element,
551 struct v4l2_input *input;
553 DEBUG("detecting whether device has a tuner");
554 GST_V4L2_CHECK_OPEN(v4l2element);
556 if (!gst_v4l2_get_input(v4l2element, &input_num))
559 input = (struct v4l2_input *) g_list_nth_data(v4l2element->inputs, input_num);
561 if (input->type == V4L2_INPUT_TYPE_TUNER &&
562 v4l2element->vcap.capabilities & V4L2_CAP_TUNER) {
563 *tuner_num = input->tuner;
570 /******************************************************
571 * gst_v4l2_get_frequency():
572 * get the current frequency
573 * return value: TRUE on success, FALSE on error
574 ******************************************************/
577 gst_v4l2_get_frequency (GstV4l2Element *v4l2element,
580 struct v4l2_frequency freq;
582 DEBUG("getting current tuner frequency");
583 GST_V4L2_CHECK_OPEN(v4l2element);
585 if (!gst_v4l2_has_tuner(v4l2element, &freq.tuner))
590 if (ioctl(v4l2element->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0) {
591 gst_element_error(GST_ELEMENT(v4l2element),
592 "Failed to get current tuner frequency for device %s: %s",
593 v4l2element->device, g_strerror(errno));
597 *frequency = freq.frequency;
603 /******************************************************
604 * gst_v4l2_set_frequency():
606 * return value: TRUE on success, FALSE on error
607 ******************************************************/
610 gst_v4l2_set_frequency (GstV4l2Element *v4l2element,
613 struct v4l2_frequency freq;
615 DEBUG("setting current tuner frequency to %lu", frequency);
616 GST_V4L2_CHECK_OPEN(v4l2element);
617 GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
619 if (!gst_v4l2_has_tuner(v4l2element, &freq.tuner))
622 freq.frequency = frequency;
625 if (ioctl(v4l2element->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0) {
626 gst_element_error(GST_ELEMENT(v4l2element),
627 "Failed to set tuner frequency to %lu for device %s: %s",
628 frequency, v4l2element->device, g_strerror(errno));
636 /******************************************************
637 * gst_v4l2_signal_strength():
638 * get the strength of the signal on the current input
639 * return value: TRUE on success, FALSE on error
640 ******************************************************/
643 gst_v4l2_signal_strength (GstV4l2Element *v4l2element,
644 gulong *signal_strength)
646 struct v4l2_tuner tuner;
648 DEBUG("trying to get signal strength");
649 GST_V4L2_CHECK_OPEN(v4l2element);
651 if (ioctl(v4l2element->video_fd, VIDIOC_G_TUNER, &tuner) < 0) {
652 gst_element_error(GST_ELEMENT(v4l2element),
653 "Failed to get signal strength for device %s: %s",
654 v4l2element->device, g_strerror(errno));
658 *signal_strength = tuner.signal;
664 /******************************************************
665 * gst_v4l2_has_audio():
666 * Check whether the device has audio capabilities
667 * return value: TRUE if it has a tuner, else FALSE
668 ******************************************************/
671 gst_v4l2_has_audio (GstV4l2Element *v4l2element)
674 struct v4l2_input *input;
676 DEBUG("detecting whether device has audio");
677 GST_V4L2_CHECK_OPEN(v4l2element);
679 if (!gst_v4l2_get_input(v4l2element, &input_num))
682 input = (struct v4l2_input *) g_list_nth_data(v4l2element->inputs, input_num);
684 return (input->audioset != 0);
688 /******************************************************
689 * gst_v4l2_control_name_to_num():
690 * convert name to num (-1 if nothing)
691 ******************************************************/
694 gst_v4l2_control_name_to_num (GstV4l2Element *v4l2element,
699 for (item = v4l2element->controls; item != NULL; item = item->next) {
700 struct v4l2_queryctrl *ctrl = item->data;
701 if (!strcmp(ctrl->name, name))
709 /******************************************************
710 * gst_v4l2_get_attribute():
711 * try to get the value of one specific attribute
712 * return value: TRUE on success, FALSE on error
713 ******************************************************/
716 gst_v4l2_get_attribute (GstElement *element,
720 struct v4l2_control control;
721 GstV4l2Element *v4l2element;
722 gint attribute_num = -1;
724 g_return_val_if_fail(element != NULL && name != NULL && value != NULL, FALSE);
725 g_return_val_if_fail(GST_IS_V4L2ELEMENT(element), FALSE);
726 v4l2element = GST_V4L2ELEMENT(element);
728 DEBUG("getting value of attribute %d", attribute_num);
729 GST_V4L2_CHECK_OPEN(v4l2element);
731 attribute_num = gst_v4l2_control_name_to_num(v4l2element, name);
733 if (attribute_num < 0) {
734 gst_element_error(GST_ELEMENT(v4l2element),
735 "Invalid control %s", name);
739 control.id = attribute_num;
741 if (ioctl(v4l2element->video_fd, VIDIOC_G_CTRL, &control) < 0) {
742 gst_element_error(GST_ELEMENT(v4l2element),
743 "Failed to get value for control %s (%d) on device %s: %s",
744 name, attribute_num, v4l2element->device, g_strerror(errno));
748 *value = control.value;
754 /******************************************************
755 * gst_v4l2_set_attribute():
756 * try to set the value of one specific attribute
757 * return value: TRUE on success, FALSE on error
758 ******************************************************/
761 gst_v4l2_set_attribute (GstElement *element,
765 struct v4l2_control control;
766 GstV4l2Element *v4l2element;
767 gint attribute_num = -1;
769 g_return_val_if_fail(element != NULL && name != NULL, FALSE);
770 g_return_val_if_fail(GST_IS_V4L2ELEMENT(element), FALSE);
771 v4l2element = GST_V4L2ELEMENT(element);
773 DEBUG("setting value of attribute %d to %d", attribute_num, value);
774 GST_V4L2_CHECK_OPEN(v4l2element);
776 attribute_num = gst_v4l2_control_name_to_num(v4l2element, name);
778 if (attribute_num < 0) {
779 gst_element_error(GST_ELEMENT(v4l2element),
780 "Invalid control %s", name);
784 control.id = attribute_num;
785 control.value = value;
787 if (ioctl(v4l2element->video_fd, VIDIOC_S_CTRL, &control) < 0) {
788 gst_element_error(GST_ELEMENT(v4l2element),
789 "Failed to set value %d for control %s (%d) on device %s: %s",
790 value, name, attribute_num, v4l2element->device, g_strerror(errno));