Fix segfault when oppening a radio device.
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / v4l2_calls.c
1 /* GStreamer
2  *
3  * Copyright (C) 2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4  *               2006 Edgard Lima <edgard.lima@indt.org.br>
5  *
6  * v4l2_calls.c - generic V4L2 calls handling
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <sys/ioctl.h>
32 #include <sys/mman.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include "v4l2_calls.h"
37 #include "gstv4l2tuner.h"
38 #include "gstv4l2xoverlay.h"
39 #include "gstv4l2colorbalance.h"
40
41 #include "gstv4l2src.h"
42
43 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
44 #define GST_CAT_DEFAULT v4l2_debug
45
46 /******************************************************
47  * gst_v4l2_get_capabilities():
48  *   get the device's capturing capabilities
49  * return value: TRUE on success, FALSE on error
50  ******************************************************/
51 gboolean
52 gst_v4l2_get_capabilities (GstV4l2Object * v4l2object)
53 {
54   GST_DEBUG_OBJECT (v4l2object->element, "getting capabilities");
55
56   if (!GST_V4L2_IS_OPEN (v4l2object))
57     return FALSE;
58
59   if (ioctl (v4l2object->video_fd, VIDIOC_QUERYCAP, &v4l2object->vcap) < 0)
60     goto cap_failed;
61
62   return TRUE;
63
64   /* ERRORS */
65 cap_failed:
66   {
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);
71     return FALSE;
72   }
73 }
74
75
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  ******************************************************/
81 static gboolean
82 gst_v4l2_fill_lists (GstV4l2Object * v4l2object)
83 {
84   gint n;
85
86   GST_DEBUG_OBJECT (v4l2object->element, "getting enumerations");
87   GST_V4L2_CHECK_OPEN (v4l2object);
88
89   GST_DEBUG_OBJECT (v4l2object->element, "  channels");
90   /* and now, the channels */
91   for (n = 0;; n++) {
92     struct v4l2_input input;
93     GstV4l2TunerChannel *v4l2channel;
94     GstTunerChannel *channel;
95
96     input.index = n;
97     if (ioctl (v4l2object->video_fd, VIDIOC_ENUMINPUT, &input) < 0) {
98       if (errno == EINVAL)
99         break;                  /* end of enumeration */
100       else {
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)));
106         return FALSE;
107       }
108     }
109
110     GST_DEBUG_OBJECT (v4l2object->element, "    '%s'", input.name);
111
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;
119
120       v4l2channel->tuner = input.tuner;
121       channel->flags |= GST_TUNER_CHANNEL_FREQUENCY;
122
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));
129         return FALSE;
130       }
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;
137     }
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;
144     }
145
146     v4l2object->channels =
147         g_list_append (v4l2object->channels, (gpointer) channel);
148   }
149
150   GST_DEBUG_OBJECT (v4l2object->element, "  norms");
151   /* norms... */
152   for (n = 0;; n++) {
153     struct v4l2_standard standard;
154     GstV4l2TunerNorm *v4l2norm;
155     GstTunerNorm *norm;
156
157     /* fill in defaults */
158     standard.frameperiod.numerator = 1;
159     standard.frameperiod.denominator = 0;
160     standard.index = n;
161     if (ioctl (v4l2object->video_fd, VIDIOC_ENUMSTD, &standard) < 0) {
162       if (errno == EINVAL)
163         break;                  /* end of enumeration */
164       else {
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)));
170         return FALSE;
171       }
172     }
173
174     GST_DEBUG_OBJECT (v4l2object->element, "    '%s', fps: %d / %d",
175         standard.name, standard.frameperiod.denominator,
176         standard.frameperiod.numerator);
177
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;
184
185     v4l2object->norms = g_list_append (v4l2object->norms, (gpointer) norm);
186   }
187
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;
194
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 */
201       break;
202     }
203
204     control.id = n;
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 */
209           continue;
210         else
211           break;
212       } else {
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)));
218         return FALSE;
219       }
220     }
221     if (control.flags & V4L2_CTRL_FLAG_DISABLED)
222       continue;
223
224     switch (n) {
225       case V4L2_CID_BRIGHTNESS:
226       case V4L2_CID_CONTRAST:
227       case V4L2_CID_SATURATION:
228       case V4L2_CID_HUE:
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:
234       case V4L2_CID_GAMMA:
235       case V4L2_CID_EXPOSURE:
236       case V4L2_CID_AUTOGAIN:
237       case V4L2_CID_GAIN:
238         /* we only handle these for now (why?) */
239         break;
240       case V4L2_CID_HFLIP:
241       case V4L2_CID_VFLIP:
242       case V4L2_CID_HCENTER:
243       case V4L2_CID_VCENTER:
244         /* not handled here, handled by VideoOrientation interface */
245         control.id++;
246         break;
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 */
254         /* fall through */
255       default:
256         GST_DEBUG_OBJECT (v4l2object->element,
257             "ControlID %s (%x) unhandled, FIXME", control.name, n);
258         control.id++;
259         break;
260     }
261     if (n != control.id)
262       continue;
263
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);
269     v4l2channel->id = n;
270
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;
275       int i;
276
277       menu.id = n;
278       for (i = 0;; i++) {
279         menu.index = i;
280         if (ioctl (v4l2object->video_fd, VIDIOC_QUERYMENU, &menu) < 0) {
281           if (errno == EINVAL)
282             break;              /* end of enumeration */
283           else {
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)));
289             return FALSE;
290           }
291         }
292         mptr = g_malloc (sizeof (menu));
293         memcpy (mptr, &menu, sizeof (menu));
294         menus = g_list_append (menus, mptr);
295       }
296     }
297     v4l2object->menus = g_list_append (v4l2object->menus, menus);
298 #endif
299
300     switch (control.type) {
301       case V4L2_CTRL_TYPE_INTEGER:
302         channel->min_value = control.minimum;
303         channel->max_value = control.maximum;
304         break;
305       case V4L2_CTRL_TYPE_BOOLEAN:
306         channel->min_value = FALSE;
307         channel->max_value = TRUE;
308         break;
309       default:
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;
317         break;
318     }
319
320     v4l2object->colors = g_list_append (v4l2object->colors, (gpointer) channel);
321   }
322
323   GST_DEBUG_OBJECT (v4l2object->element, "done");
324   return TRUE;
325 }
326
327
328 static void
329 gst_v4l2_empty_lists (GstV4l2Object * v4l2object)
330 {
331   GST_DEBUG_OBJECT (v4l2object->element, "deleting enumerations");
332
333   g_list_foreach (v4l2object->channels, (GFunc) g_object_unref, NULL);
334   g_list_free (v4l2object->channels);
335   v4l2object->channels = NULL;
336
337   g_list_foreach (v4l2object->norms, (GFunc) g_object_unref, NULL);
338   g_list_free (v4l2object->norms);
339   v4l2object->norms = NULL;
340
341   g_list_foreach (v4l2object->colors, (GFunc) g_object_unref, NULL);
342   g_list_free (v4l2object->colors);
343   v4l2object->colors = NULL;
344 }
345
346 /******************************************************
347  * gst_v4l2_open():
348  *   open the video device (v4l2object->videodev)
349  * return value: TRUE on success, FALSE on error
350  ******************************************************/
351 gboolean
352 gst_v4l2_open (GstV4l2Object * v4l2object)
353 {
354   struct stat st;
355
356   GST_DEBUG_OBJECT (v4l2object->element, "Trying to open device %s",
357       v4l2object->videodev);
358
359   GST_V4L2_CHECK_NOT_OPEN (v4l2object);
360   GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
361
362   /* be sure we have a device */
363   if (!v4l2object->videodev)
364     v4l2object->videodev = g_strdup ("/dev/video");
365
366   /* check if it is a device */
367   if (stat (v4l2object->videodev, &st) == -1)
368     goto stat_failed;
369
370   if (!S_ISCHR (st.st_mode))
371     goto no_device;
372
373   /* open the device */
374   v4l2object->video_fd =
375       open (v4l2object->videodev, O_RDWR /* | O_NONBLOCK */ );
376
377   if (!GST_V4L2_IS_OPEN (v4l2object))
378     goto not_open;
379
380   /* get capabilities, error will be posted */
381   if (!gst_v4l2_get_capabilities (v4l2object))
382     goto error;
383
384   /* do we need to be a capture device? */
385   if (GST_IS_V4L2SRC (v4l2object) &&
386       !(v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
387     goto not_capture;
388
389   /* create enumerations, posts errors. */
390   if (!gst_v4l2_fill_lists (v4l2object))
391     goto error;
392
393   GST_INFO_OBJECT (v4l2object->element,
394       "Opened device '%s' (%s) successfully",
395       v4l2object->vcap.card, v4l2object->videodev);
396
397   return TRUE;
398
399   /* ERRORS */
400 stat_failed:
401   {
402     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
403         (_("Cannot identify device '%s'."), v4l2object->videodev),
404         GST_ERROR_SYSTEM);
405     goto error;
406   }
407 no_device:
408   {
409     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
410         (_("This isn't a device '%s'."), v4l2object->videodev),
411         GST_ERROR_SYSTEM);
412     goto error;
413   }
414 not_open:
415   {
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);
419     goto error;
420   }
421 not_capture:
422   {
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));
427     goto error;
428   }
429 error:
430   {
431     if (GST_V4L2_IS_OPEN (v4l2object)) {
432       /* close device */
433       close (v4l2object->video_fd);
434       v4l2object->video_fd = -1;
435     }
436     /* empty lists */
437     gst_v4l2_empty_lists (v4l2object);
438
439     return FALSE;
440   }
441 }
442
443
444 /******************************************************
445  * gst_v4l2_close():
446  *   close the video device (v4l2object->video_fd)
447  * return value: TRUE on success, FALSE on error
448  ******************************************************/
449 gboolean
450 gst_v4l2_close (GstV4l2Object * v4l2object)
451 {
452   GST_DEBUG_OBJECT (v4l2object->element, "Trying to close %s",
453       v4l2object->videodev);
454
455   GST_V4L2_CHECK_OPEN (v4l2object);
456   GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
457
458   /* close device */
459   close (v4l2object->video_fd);
460   v4l2object->video_fd = -1;
461
462   /* empty lists */
463   gst_v4l2_empty_lists (v4l2object);
464
465   return TRUE;
466 }
467
468
469 /******************************************************
470  * gst_v4l2_get_norm()
471  *   Get the norm of the current device
472  * return value: TRUE on success, FALSE on error
473  ******************************************************/
474 gboolean
475 gst_v4l2_get_norm (GstV4l2Object * v4l2object, v4l2_std_id * norm)
476 {
477   GST_DEBUG_OBJECT (v4l2object->element, "getting norm");
478
479   if (!GST_V4L2_IS_OPEN (v4l2object))
480     return FALSE;
481
482   if (ioctl (v4l2object->video_fd, VIDIOC_G_STD, norm) < 0)
483     goto std_failed;
484
485   return TRUE;
486
487   /* ERRORS */
488 std_failed:
489   {
490     GST_DEBUG ("Failed to get the current norm for device %s",
491         v4l2object->videodev);
492     return FALSE;
493   }
494 }
495
496
497 /******************************************************
498  * gst_v4l2_set_norm()
499  *   Set the norm of the current device
500  * return value: TRUE on success, FALSE on error
501  ******************************************************/
502 gboolean
503 gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm)
504 {
505   GST_DEBUG_OBJECT (v4l2object->element, "trying to set norm to %llx", norm);
506
507   if (!GST_V4L2_IS_OPEN (v4l2object))
508     return FALSE;
509
510   if (ioctl (v4l2object->video_fd, VIDIOC_S_STD, &norm) < 0)
511     goto std_failed;
512
513   return TRUE;
514
515   /* ERRORS */
516 std_failed:
517   {
518     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
519         (_("Failed to set norm for device '%s'."),
520             v4l2object->videodev), GST_ERROR_SYSTEM);
521     return FALSE;
522   }
523 }
524
525 /******************************************************
526  * gst_v4l2_get_frequency():
527  *   get the current frequency
528  * return value: TRUE on success, FALSE on error
529  ******************************************************/
530 gboolean
531 gst_v4l2_get_frequency (GstV4l2Object * v4l2object,
532     gint tunernum, gulong * frequency)
533 {
534   struct v4l2_frequency freq;
535   GstTunerChannel *channel;
536
537   GST_DEBUG_OBJECT (v4l2object->element, "getting current tuner frequency");
538
539   if (!GST_V4L2_IS_OPEN (v4l2object))
540     return FALSE;
541
542   channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
543
544   freq.tuner = tunernum;
545   if (ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0)
546     goto freq_failed;
547
548   *frequency = freq.frequency * channel->freq_multiplicator;
549
550   return TRUE;
551
552   /* ERRORS */
553 freq_failed:
554   {
555     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
556         (_("Failed to get current tuner frequency for device '%s'."),
557             v4l2object->videodev), GST_ERROR_SYSTEM);
558     return FALSE;
559   }
560 }
561
562
563 /******************************************************
564  * gst_v4l2_set_frequency():
565  *   set frequency
566  * return value: TRUE on success, FALSE on error
567  ******************************************************/
568 gboolean
569 gst_v4l2_set_frequency (GstV4l2Object * v4l2object,
570     gint tunernum, gulong frequency)
571 {
572   struct v4l2_frequency freq;
573   GstTunerChannel *channel;
574
575   GST_DEBUG_OBJECT (v4l2object->element,
576       "setting current tuner frequency to %lu", frequency);
577
578   if (!GST_V4L2_IS_OPEN (v4l2object))
579     return FALSE;
580
581   channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
582
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;
587
588   if (ioctl (v4l2object->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0)
589     goto freq_failed;
590
591   return TRUE;
592
593   /* ERRORS */
594 freq_failed:
595   {
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);
599     return FALSE;
600   }
601 }
602
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  ******************************************************/
608 gboolean
609 gst_v4l2_signal_strength (GstV4l2Object * v4l2object,
610     gint tunernum, gulong * signal_strength)
611 {
612   struct v4l2_tuner tuner;
613
614   GST_DEBUG_OBJECT (v4l2object->element, "trying to get signal strength");
615
616   if (!GST_V4L2_IS_OPEN (v4l2object))
617     return FALSE;
618
619   tuner.index = tunernum;
620   if (ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &tuner) < 0)
621     goto tuner_failed;
622
623   *signal_strength = tuner.signal;
624
625   return TRUE;
626
627   /* ERRORS */
628 tuner_failed:
629   {
630     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
631         (_("Failed to get signal strength for device '%s'."),
632             v4l2object->videodev), GST_ERROR_SYSTEM);
633     return FALSE;
634   }
635 }
636
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  ******************************************************/
642 gboolean
643 gst_v4l2_get_attribute (GstV4l2Object * v4l2object,
644     int attribute_num, int *value)
645 {
646   struct v4l2_control control;
647
648   GST_DEBUG_OBJECT (v4l2object->element, "getting value of attribute %d",
649       attribute_num);
650
651   if (!GST_V4L2_IS_OPEN (v4l2object))
652     return FALSE;
653
654   control.id = attribute_num;
655
656   if (ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0)
657     goto ctrl_failed;
658
659   *value = control.value;
660
661   return TRUE;
662
663   /* ERRORS */
664 ctrl_failed:
665   {
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);
669     return FALSE;
670   }
671 }
672
673
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  ******************************************************/
679 gboolean
680 gst_v4l2_set_attribute (GstV4l2Object * v4l2object,
681     int attribute_num, const int value)
682 {
683   struct v4l2_control control;
684
685   GST_DEBUG_OBJECT (v4l2object->element, "setting value of attribute %d to %d",
686       attribute_num, value);
687
688   if (!GST_V4L2_IS_OPEN (v4l2object))
689     return FALSE;
690
691   control.id = attribute_num;
692   control.value = value;
693   if (ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0)
694     goto ctrl_failed;
695
696   return TRUE;
697
698   /* ERRORS */
699 ctrl_failed:
700   {
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);
704     return FALSE;
705   }
706 }
707
708 gboolean
709 gst_v4l2_get_input (GstV4l2Object * v4l2object, gint * input)
710 {
711   gint n;
712
713   GST_DEBUG_OBJECT (v4l2object->element, "trying to get input");
714
715   if (!GST_V4L2_IS_OPEN (v4l2object))
716     return FALSE;
717
718   if (ioctl (v4l2object->video_fd, VIDIOC_G_INPUT, &n) < 0)
719     goto input_failed;
720
721   *input = n;
722
723   return TRUE;
724
725   /* ERRORS */
726 input_failed:
727   {
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);
730     return FALSE;
731   }
732 }
733
734 gboolean
735 gst_v4l2_set_input (GstV4l2Object * v4l2object, gint input)
736 {
737   GST_DEBUG_OBJECT (v4l2object->element, "trying to set input to %d", input);
738
739   if (!GST_V4L2_IS_OPEN (v4l2object))
740     return FALSE;
741
742   if (ioctl (v4l2object->video_fd, VIDIOC_S_INPUT, &input) < 0)
743     goto input_failed;
744
745   return TRUE;
746
747   /* ERRORS */
748 input_failed:
749   {
750     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
751         (_("Failed to set input %d on device %s."),
752             input, v4l2object->videodev), GST_ERROR_SYSTEM);
753     return FALSE;
754   }
755 }