sys/: Fix some spelling mistakes. Fixes #556802.
[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 #ifdef __sun
37 /* Needed on older Solaris Nevada builds (72 at least) */
38 #include <stropts.h>
39 #include <sys/ioccom.h>
40 #endif
41 #include "v4l2_calls.h"
42 #include "gstv4l2tuner.h"
43 #if 0
44 #include "gstv4l2xoverlay.h"
45 #endif
46 #include "gstv4l2colorbalance.h"
47
48 #include "gstv4l2src.h"
49
50 /* Those are ioctl calls */
51 #ifndef V4L2_CID_HCENTER
52 #define V4L2_CID_HCENTER V4L2_CID_HCENTER_DEPRECATED
53 #endif
54 #ifndef V4L2_CID_VCENTER
55 #define V4L2_CID_VCENTER V4L2_CID_VCENTER_DEPRECATED
56 #endif
57
58 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
59 #define GST_CAT_DEFAULT v4l2_debug
60
61 /******************************************************
62  * gst_v4l2_get_capabilities():
63  *   get the device's capturing capabilities
64  * return value: TRUE on success, FALSE on error
65  ******************************************************/
66 gboolean
67 gst_v4l2_get_capabilities (GstV4l2Object * v4l2object)
68 {
69   GstElement *e;
70
71   e = v4l2object->element;
72
73   GST_DEBUG_OBJECT (e, "getting capabilities");
74
75   if (!GST_V4L2_IS_OPEN (v4l2object))
76     return FALSE;
77
78   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYCAP, &v4l2object->vcap) < 0)
79     goto cap_failed;
80
81   GST_LOG_OBJECT (e, "driver:      '%s'", v4l2object->vcap.driver);
82   GST_LOG_OBJECT (e, "card:        '%s'", v4l2object->vcap.card);
83   GST_LOG_OBJECT (e, "bus_info:    '%s'", v4l2object->vcap.bus_info);
84   GST_LOG_OBJECT (e, "version:     %08x", v4l2object->vcap.version);
85   GST_LOG_OBJECT (e, "capabilites: %08x", v4l2object->vcap.capabilities);
86
87   return TRUE;
88
89   /* ERRORS */
90 cap_failed:
91   {
92     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
93         (_("Error getting capabilities for device '%s': "
94                 "It isn't a v4l2 driver. Check if it is a v4l1 driver."),
95             v4l2object->videodev), GST_ERROR_SYSTEM);
96     return FALSE;
97   }
98 }
99
100
101 /******************************************************
102  * gst_v4l2_empty_lists() and gst_v4l2_fill_lists():
103  *   fill/empty the lists of enumerations
104  * return value: TRUE on success, FALSE on error
105  ******************************************************/
106 static gboolean
107 gst_v4l2_fill_lists (GstV4l2Object * v4l2object)
108 {
109   gint n;
110
111   GstElement *e;
112
113   e = v4l2object->element;
114
115   GST_DEBUG_OBJECT (e, "getting enumerations");
116   GST_V4L2_CHECK_OPEN (v4l2object);
117
118   GST_DEBUG_OBJECT (e, "  channels");
119   /* and now, the channels */
120   for (n = 0;; n++) {
121     struct v4l2_input input = { 0, };
122     GstV4l2TunerChannel *v4l2channel;
123
124     GstTunerChannel *channel;
125
126     input.index = n;
127     if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUMINPUT, &input) < 0) {
128       if (errno == EINVAL)
129         break;                  /* end of enumeration */
130       else {
131         GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
132             (_("Failed to query attributes of input %d in device %s"),
133                 n, v4l2object->videodev),
134             ("Failed to get %d in input enumeration for %s. (%d - %s)",
135                 n, v4l2object->videodev, errno, strerror (errno)));
136         return FALSE;
137       }
138     }
139
140     GST_LOG_OBJECT (e, "   index:     %d", input.index);
141     GST_LOG_OBJECT (e, "   name:      '%s'", input.name);
142     GST_LOG_OBJECT (e, "   type:      %08x", input.type);
143     GST_LOG_OBJECT (e, "   audioset:  %08x", input.audioset);
144     GST_LOG_OBJECT (e, "   std:       %016x", input.std);
145     GST_LOG_OBJECT (e, "   status:    %08x", input.status);
146
147     v4l2channel = g_object_new (GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
148     channel = GST_TUNER_CHANNEL (v4l2channel);
149     channel->label = g_strdup ((const gchar *) input.name);
150     channel->flags = GST_TUNER_CHANNEL_INPUT;
151     v4l2channel->index = n;
152
153     if (input.type == V4L2_INPUT_TYPE_TUNER) {
154       struct v4l2_tuner vtun;
155
156       v4l2channel->tuner = input.tuner;
157       channel->flags |= GST_TUNER_CHANNEL_FREQUENCY;
158
159       vtun.index = input.tuner;
160       if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &vtun) < 0) {
161         GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
162             (_("Failed to get setting of tuner %d on device '%s'."),
163                 input.tuner, v4l2object->videodev), GST_ERROR_SYSTEM);
164         g_object_unref (G_OBJECT (channel));
165         return FALSE;
166       }
167
168       channel->freq_multiplicator =
169           62.5 * ((vtun.capability & V4L2_TUNER_CAP_LOW) ? 1 : 1000);
170       channel->min_frequency = vtun.rangelow * channel->freq_multiplicator;
171       channel->max_frequency = vtun.rangehigh * channel->freq_multiplicator;
172       channel->min_signal = 0;
173       channel->max_signal = 0xffff;
174     }
175     if (input.audioset) {
176       /* we take the first. We don't care for
177        * the others for now */
178       while (!(input.audioset & (1 << v4l2channel->audio)))
179         v4l2channel->audio++;
180       channel->flags |= GST_TUNER_CHANNEL_AUDIO;
181     }
182
183     v4l2object->channels =
184         g_list_append (v4l2object->channels, (gpointer) channel);
185   }
186
187   GST_DEBUG_OBJECT (e, "  norms");
188   /* norms... */
189   for (n = 0;; n++) {
190     struct v4l2_standard standard = { 0, };
191     GstV4l2TunerNorm *v4l2norm;
192
193     GstTunerNorm *norm;
194
195     /* fill in defaults */
196     standard.frameperiod.numerator = 1;
197     standard.frameperiod.denominator = 0;
198     standard.index = n;
199
200     if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUMSTD, &standard) < 0) {
201       if (errno == EINVAL || errno == ENOTTY)
202         break;                  /* end of enumeration */
203       else {
204         GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
205             (_("Failed to query norm on device '%s'."),
206                 v4l2object->videodev),
207             ("Failed to get attributes for norm %d on devide '%s'. (%d - %s)",
208                 n, v4l2object->videodev, errno, strerror (errno)));
209         return FALSE;
210       }
211     }
212
213     GST_DEBUG_OBJECT (e, "    '%s', fps: %d / %d",
214         standard.name, standard.frameperiod.denominator,
215         standard.frameperiod.numerator);
216
217     v4l2norm = g_object_new (GST_TYPE_V4L2_TUNER_NORM, NULL);
218     norm = GST_TUNER_NORM (v4l2norm);
219     norm->label = g_strdup ((const gchar *) standard.name);
220     gst_value_set_fraction (&norm->framerate,
221         standard.frameperiod.denominator, standard.frameperiod.numerator);
222     v4l2norm->index = standard.id;
223
224     v4l2object->norms = g_list_append (v4l2object->norms, (gpointer) norm);
225   }
226
227   GST_DEBUG_OBJECT (e, "  controls+menus");
228   /* and lastly, controls+menus (if appropriate) */
229   for (n = V4L2_CID_BASE;; n++) {
230     struct v4l2_queryctrl control = { 0, };
231     GstV4l2ColorBalanceChannel *v4l2channel;
232
233     GstColorBalanceChannel *channel;
234
235     /* when we reached the last official CID, continue with private CIDs */
236     if (n == V4L2_CID_LASTP1) {
237       GST_DEBUG_OBJECT (e, "checking private CIDs");
238       n = V4L2_CID_PRIVATE_BASE;
239       /* FIXME: We are still not handling private controls. We need a new GstInterface
240          to export those controls */
241       break;
242     }
243
244     control.id = n;
245     if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
246       if (errno == EINVAL) {
247         if (n < V4L2_CID_PRIVATE_BASE)
248           /* continue so that we also check private controls */
249           continue;
250         else
251           break;
252       } else {
253         GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
254             (_("Failed getting controls attributes on device '%s'."),
255                 v4l2object->videodev),
256             ("Failed querying control %d on device '%s'. (%d - %s)",
257                 n, v4l2object->videodev, errno, strerror (errno)));
258         return FALSE;
259       }
260     }
261     if (control.flags & V4L2_CTRL_FLAG_DISABLED)
262       continue;
263
264     switch (n) {
265       case V4L2_CID_BRIGHTNESS:
266       case V4L2_CID_CONTRAST:
267       case V4L2_CID_SATURATION:
268       case V4L2_CID_HUE:
269       case V4L2_CID_BLACK_LEVEL:
270       case V4L2_CID_AUTO_WHITE_BALANCE:
271       case V4L2_CID_DO_WHITE_BALANCE:
272       case V4L2_CID_RED_BALANCE:
273       case V4L2_CID_BLUE_BALANCE:
274       case V4L2_CID_GAMMA:
275       case V4L2_CID_EXPOSURE:
276       case V4L2_CID_AUTOGAIN:
277       case V4L2_CID_GAIN:
278         /* we only handle these for now (why?) */
279         break;
280       case V4L2_CID_HFLIP:
281       case V4L2_CID_VFLIP:
282       case V4L2_CID_HCENTER:
283       case V4L2_CID_VCENTER:
284 #ifdef V4L2_CID_PAN_RESET
285       case V4L2_CID_PAN_RESET:
286 #endif
287 #ifdef V4L2_CID_TILT_RESET
288       case V4L2_CID_TILT_RESET:
289 #endif
290         /* not handled here, handled by VideoOrientation interface */
291         control.id++;
292         break;
293       case V4L2_CID_AUDIO_VOLUME:
294       case V4L2_CID_AUDIO_BALANCE:
295       case V4L2_CID_AUDIO_BASS:
296       case V4L2_CID_AUDIO_TREBLE:
297       case V4L2_CID_AUDIO_MUTE:
298       case V4L2_CID_AUDIO_LOUDNESS:
299         /* FIXME: We should implement GstMixer interface */
300         /* fall through */
301       default:
302         GST_DEBUG_OBJECT (e,
303             "ControlID %s (%x) unhandled, FIXME", control.name, n);
304         control.id++;
305         break;
306     }
307     if (n != control.id)
308       continue;
309
310     GST_DEBUG_OBJECT (e, "Adding ControlID %s (%x)", control.name, n);
311     v4l2channel = g_object_new (GST_TYPE_V4L2_COLOR_BALANCE_CHANNEL, NULL);
312     channel = GST_COLOR_BALANCE_CHANNEL (v4l2channel);
313     channel->label = g_strdup ((const gchar *) control.name);
314     v4l2channel->id = n;
315
316 #if 0
317     /* FIXME: it will be need just when handling private controls
318      *(currently none of base controls are of this type) */
319     if (control.type == V4L2_CTRL_TYPE_MENU) {
320       struct v4l2_querymenu menu, *mptr;
321
322       int i;
323
324       menu.id = n;
325       for (i = 0;; i++) {
326         menu.index = i;
327         if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYMENU, &menu) < 0) {
328           if (errno == EINVAL)
329             break;              /* end of enumeration */
330           else {
331             GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
332                 (_("Failed getting controls attributes on device '%s'."),
333                     v4l2object->videodev),
334                 ("Failed to get %d in menu enumeration for %s. (%d - %s)",
335                     n, v4l2object->videodev, errno, strerror (errno)));
336             return FALSE;
337           }
338         }
339         mptr = g_malloc (sizeof (menu));
340         memcpy (mptr, &menu, sizeof (menu));
341         menus = g_list_append (menus, mptr);
342       }
343     }
344     v4l2object->menus = g_list_append (v4l2object->menus, menus);
345 #endif
346
347     switch (control.type) {
348       case V4L2_CTRL_TYPE_INTEGER:
349         channel->min_value = control.minimum;
350         channel->max_value = control.maximum;
351         break;
352       case V4L2_CTRL_TYPE_BOOLEAN:
353         channel->min_value = FALSE;
354         channel->max_value = TRUE;
355         break;
356       default:
357         /* FIXME we should find out how to handle V4L2_CTRL_TYPE_BUTTON.
358            BUTTON controls like V4L2_CID_DO_WHITE_BALANCE can just be set (1) or
359            unset (0), but can't be queried */
360         GST_DEBUG_OBJECT (e,
361             "Control with non supported type %s (%x), type=%d",
362             control.name, n, control.type);
363         channel->min_value = channel->max_value = 0;
364         break;
365     }
366
367     v4l2object->colors = g_list_append (v4l2object->colors, (gpointer) channel);
368   }
369
370   GST_DEBUG_OBJECT (e, "done");
371   return TRUE;
372 }
373
374
375 static void
376 gst_v4l2_empty_lists (GstV4l2Object * v4l2object)
377 {
378   GST_DEBUG_OBJECT (v4l2object->element, "deleting enumerations");
379
380   g_list_foreach (v4l2object->channels, (GFunc) g_object_unref, NULL);
381   g_list_free (v4l2object->channels);
382   v4l2object->channels = NULL;
383
384   g_list_foreach (v4l2object->norms, (GFunc) g_object_unref, NULL);
385   g_list_free (v4l2object->norms);
386   v4l2object->norms = NULL;
387
388   g_list_foreach (v4l2object->colors, (GFunc) g_object_unref, NULL);
389   g_list_free (v4l2object->colors);
390   v4l2object->colors = NULL;
391 }
392
393 /******************************************************
394  * gst_v4l2_open():
395  *   open the video device (v4l2object->videodev)
396  * return value: TRUE on success, FALSE on error
397  ******************************************************/
398 gboolean
399 gst_v4l2_open (GstV4l2Object * v4l2object)
400 {
401   struct stat st;
402   int libv4l2_fd;
403
404   GST_DEBUG_OBJECT (v4l2object->element, "Trying to open device %s",
405       v4l2object->videodev);
406
407   GST_V4L2_CHECK_NOT_OPEN (v4l2object);
408   GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
409
410   /* be sure we have a device */
411   if (!v4l2object->videodev)
412     v4l2object->videodev = g_strdup ("/dev/video");
413
414   /* check if it is a device */
415   if (stat (v4l2object->videodev, &st) == -1)
416     goto stat_failed;
417
418   if (!S_ISCHR (st.st_mode))
419     goto no_device;
420
421   /* open the device */
422   v4l2object->video_fd =
423       open (v4l2object->videodev, O_RDWR /* | O_NONBLOCK */ );
424
425   if (!GST_V4L2_IS_OPEN (v4l2object))
426     goto not_open;
427
428   libv4l2_fd = v4l2_fd_open (v4l2object->video_fd,
429       V4L2_ENABLE_ENUM_FMT_EMULATION);
430   /* Note the v4l2_xxx functions are designed so that if they get passed an
431      unknown fd, the will behave exactly as their regular xxx counterparts, so
432      if v4l2_fd_open fails, we continue as normal (missing the libv4l2 custom
433      cam format to normal formats conversion). Chances are big we will still
434      fail then though, as normally v4l2_fd_open only fails if the device is not
435      a v4l2 device. */
436   if (libv4l2_fd != -1)
437     v4l2object->video_fd = libv4l2_fd;
438
439   /* get capabilities, error will be posted */
440   if (!gst_v4l2_get_capabilities (v4l2object))
441     goto error;
442
443   /* do we need to be a capture device? */
444   if (GST_IS_V4L2SRC (v4l2object) &&
445       !(v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
446     goto not_capture;
447
448   /* create enumerations, posts errors. */
449   if (!gst_v4l2_fill_lists (v4l2object))
450     goto error;
451
452   GST_INFO_OBJECT (v4l2object->element,
453       "Opened device '%s' (%s) successfully",
454       v4l2object->vcap.card, v4l2object->videodev);
455
456   return TRUE;
457
458   /* ERRORS */
459 stat_failed:
460   {
461     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
462         (_("Cannot identify device '%s'."), v4l2object->videodev),
463         GST_ERROR_SYSTEM);
464     goto error;
465   }
466 no_device:
467   {
468     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
469         (_("This isn't a device '%s'."), v4l2object->videodev),
470         GST_ERROR_SYSTEM);
471     goto error;
472   }
473 not_open:
474   {
475     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
476         (_("Could not open device '%s' for reading and writing."),
477             v4l2object->videodev), GST_ERROR_SYSTEM);
478     goto error;
479   }
480 not_capture:
481   {
482     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
483         (_("Device '%s' is not a capture device."),
484             v4l2object->videodev),
485         ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
486     goto error;
487   }
488 error:
489   {
490     if (GST_V4L2_IS_OPEN (v4l2object)) {
491       /* close device */
492       v4l2_close (v4l2object->video_fd);
493       v4l2object->video_fd = -1;
494     }
495     /* empty lists */
496     gst_v4l2_empty_lists (v4l2object);
497
498     return FALSE;
499   }
500 }
501
502
503 /******************************************************
504  * gst_v4l2_close():
505  *   close the video device (v4l2object->video_fd)
506  * return value: TRUE on success, FALSE on error
507  ******************************************************/
508 gboolean
509 gst_v4l2_close (GstV4l2Object * v4l2object)
510 {
511   GST_DEBUG_OBJECT (v4l2object->element, "Trying to close %s",
512       v4l2object->videodev);
513
514   GST_V4L2_CHECK_OPEN (v4l2object);
515   GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
516
517   /* close device */
518   v4l2_close (v4l2object->video_fd);
519   v4l2object->video_fd = -1;
520
521   /* empty lists */
522   gst_v4l2_empty_lists (v4l2object);
523
524   return TRUE;
525 }
526
527
528 /******************************************************
529  * gst_v4l2_get_norm()
530  *   Get the norm of the current device
531  * return value: TRUE on success, FALSE on error
532  ******************************************************/
533 gboolean
534 gst_v4l2_get_norm (GstV4l2Object * v4l2object, v4l2_std_id * norm)
535 {
536   GST_DEBUG_OBJECT (v4l2object->element, "getting norm");
537
538   if (!GST_V4L2_IS_OPEN (v4l2object))
539     return FALSE;
540
541   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_STD, norm) < 0)
542     goto std_failed;
543
544   return TRUE;
545
546   /* ERRORS */
547 std_failed:
548   {
549     GST_DEBUG ("Failed to get the current norm for device %s",
550         v4l2object->videodev);
551     return FALSE;
552   }
553 }
554
555
556 /******************************************************
557  * gst_v4l2_set_norm()
558  *   Set the norm of the current device
559  * return value: TRUE on success, FALSE on error
560  ******************************************************/
561 gboolean
562 gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm)
563 {
564   GST_DEBUG_OBJECT (v4l2object->element, "trying to set norm to %llx", norm);
565
566   if (!GST_V4L2_IS_OPEN (v4l2object))
567     return FALSE;
568
569   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_STD, &norm) < 0)
570     goto std_failed;
571
572   return TRUE;
573
574   /* ERRORS */
575 std_failed:
576   {
577     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
578         (_("Failed to set norm for device '%s'."),
579             v4l2object->videodev), GST_ERROR_SYSTEM);
580     return FALSE;
581   }
582 }
583
584 /******************************************************
585  * gst_v4l2_get_frequency():
586  *   get the current frequency
587  * return value: TRUE on success, FALSE on error
588  ******************************************************/
589 gboolean
590 gst_v4l2_get_frequency (GstV4l2Object * v4l2object,
591     gint tunernum, gulong * frequency)
592 {
593   struct v4l2_frequency freq;
594
595   GstTunerChannel *channel;
596
597   GST_DEBUG_OBJECT (v4l2object->element, "getting current tuner frequency");
598
599   if (!GST_V4L2_IS_OPEN (v4l2object))
600     return FALSE;
601
602   channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
603
604   freq.tuner = tunernum;
605   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0)
606     goto freq_failed;
607
608   *frequency = freq.frequency * channel->freq_multiplicator;
609
610   return TRUE;
611
612   /* ERRORS */
613 freq_failed:
614   {
615     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
616         (_("Failed to get current tuner frequency for device '%s'."),
617             v4l2object->videodev), GST_ERROR_SYSTEM);
618     return FALSE;
619   }
620 }
621
622
623 /******************************************************
624  * gst_v4l2_set_frequency():
625  *   set frequency
626  * return value: TRUE on success, FALSE on error
627  ******************************************************/
628 gboolean
629 gst_v4l2_set_frequency (GstV4l2Object * v4l2object,
630     gint tunernum, gulong frequency)
631 {
632   struct v4l2_frequency freq;
633
634   GstTunerChannel *channel;
635
636   GST_DEBUG_OBJECT (v4l2object->element,
637       "setting current tuner frequency to %lu", frequency);
638
639   if (!GST_V4L2_IS_OPEN (v4l2object))
640     return FALSE;
641
642   channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
643
644   freq.tuner = tunernum;
645   /* fill in type - ignore error */
646   v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq);
647   freq.frequency = frequency / channel->freq_multiplicator;
648
649   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0)
650     goto freq_failed;
651
652   return TRUE;
653
654   /* ERRORS */
655 freq_failed:
656   {
657     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
658         (_("Failed to set current tuner frequency for device '%s' to %lu Hz."),
659             v4l2object->videodev, frequency), GST_ERROR_SYSTEM);
660     return FALSE;
661   }
662 }
663
664 /******************************************************
665  * gst_v4l2_signal_strength():
666  *   get the strength of the signal on the current input
667  * return value: TRUE on success, FALSE on error
668  ******************************************************/
669 gboolean
670 gst_v4l2_signal_strength (GstV4l2Object * v4l2object,
671     gint tunernum, gulong * signal_strength)
672 {
673   struct v4l2_tuner tuner;
674
675   GST_DEBUG_OBJECT (v4l2object->element, "trying to get signal strength");
676
677   if (!GST_V4L2_IS_OPEN (v4l2object))
678     return FALSE;
679
680   tuner.index = tunernum;
681   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &tuner) < 0)
682     goto tuner_failed;
683
684   *signal_strength = tuner.signal;
685
686   return TRUE;
687
688   /* ERRORS */
689 tuner_failed:
690   {
691     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
692         (_("Failed to get signal strength for device '%s'."),
693             v4l2object->videodev), GST_ERROR_SYSTEM);
694     return FALSE;
695   }
696 }
697
698 /******************************************************
699  * gst_v4l2_get_attribute():
700  *   try to get the value of one specific attribute
701  * return value: TRUE on success, FALSE on error
702  ******************************************************/
703 gboolean
704 gst_v4l2_get_attribute (GstV4l2Object * v4l2object,
705     int attribute_num, int *value)
706 {
707   struct v4l2_control control;
708
709   GST_DEBUG_OBJECT (v4l2object->element, "getting value of attribute %d",
710       attribute_num);
711
712   if (!GST_V4L2_IS_OPEN (v4l2object))
713     return FALSE;
714
715   control.id = attribute_num;
716
717   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0)
718     goto ctrl_failed;
719
720   *value = control.value;
721
722   return TRUE;
723
724   /* ERRORS */
725 ctrl_failed:
726   {
727     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
728         (_("Failed to get value for control %d on device '%s'."),
729             attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
730     return FALSE;
731   }
732 }
733
734
735 /******************************************************
736  * gst_v4l2_set_attribute():
737  *   try to set the value of one specific attribute
738  * return value: TRUE on success, FALSE on error
739  ******************************************************/
740 gboolean
741 gst_v4l2_set_attribute (GstV4l2Object * v4l2object,
742     int attribute_num, const int value)
743 {
744   struct v4l2_control control;
745
746   GST_DEBUG_OBJECT (v4l2object->element, "setting value of attribute %d to %d",
747       attribute_num, value);
748
749   if (!GST_V4L2_IS_OPEN (v4l2object))
750     return FALSE;
751
752   control.id = attribute_num;
753   control.value = value;
754   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0)
755     goto ctrl_failed;
756
757   return TRUE;
758
759   /* ERRORS */
760 ctrl_failed:
761   {
762     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
763         (_("Failed to set value %d for control %d on device '%s'."),
764             value, attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
765     return FALSE;
766   }
767 }
768
769 gboolean
770 gst_v4l2_get_input (GstV4l2Object * v4l2object, gint * input)
771 {
772   gint n;
773
774   GST_DEBUG_OBJECT (v4l2object->element, "trying to get input");
775
776   if (!GST_V4L2_IS_OPEN (v4l2object))
777     return FALSE;
778
779   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_INPUT, &n) < 0)
780     goto input_failed;
781
782   *input = n;
783
784   GST_DEBUG_OBJECT (v4l2object->element, "input: %d", n);
785
786   return TRUE;
787
788   /* ERRORS */
789 input_failed:
790   {
791     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
792         (_("Failed to get current input on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
793     return FALSE;
794   }
795 }
796
797 gboolean
798 gst_v4l2_set_input (GstV4l2Object * v4l2object, gint input)
799 {
800   GST_DEBUG_OBJECT (v4l2object->element, "trying to set input to %d", input);
801
802   if (!GST_V4L2_IS_OPEN (v4l2object))
803     return FALSE;
804
805   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_INPUT, &input) < 0)
806     goto input_failed;
807
808   return TRUE;
809
810   /* ERRORS */
811 input_failed:
812   {
813     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
814         (_("Failed to set input %d on device %s."),
815             input, v4l2object->videodev), GST_ERROR_SYSTEM);
816     return FALSE;
817   }
818 }