Merge branch 'master' of ssh://thomasvs@git.freedesktop.org/git/gstreamer/gst-plugins...
[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   GstPollFD pollfd = GST_POLL_FD_INIT;
404
405   GST_DEBUG_OBJECT (v4l2object->element, "Trying to open device %s",
406       v4l2object->videodev);
407
408   GST_V4L2_CHECK_NOT_OPEN (v4l2object);
409   GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
410
411   /* be sure we have a device */
412   if (!v4l2object->videodev)
413     v4l2object->videodev = g_strdup ("/dev/video");
414
415   /* check if it is a device */
416   if (stat (v4l2object->videodev, &st) == -1)
417     goto stat_failed;
418
419   if (!S_ISCHR (st.st_mode))
420     goto no_device;
421
422   /* open the device */
423   v4l2object->video_fd =
424       open (v4l2object->videodev, O_RDWR /* | O_NONBLOCK */ );
425
426   if (!GST_V4L2_IS_OPEN (v4l2object))
427     goto not_open;
428
429   libv4l2_fd = v4l2_fd_open (v4l2object->video_fd,
430       V4L2_ENABLE_ENUM_FMT_EMULATION);
431   /* Note the v4l2_xxx functions are designed so that if they get passed an
432      unknown fd, the will behave exactly as their regular xxx counterparts, so
433      if v4l2_fd_open fails, we continue as normal (missing the libv4l2 custom
434      cam format to normal formats conversion). Chances are big we will still
435      fail then though, as normally v4l2_fd_open only fails if the device is not
436      a v4l2 device. */
437   if (libv4l2_fd != -1)
438     v4l2object->video_fd = libv4l2_fd;
439
440   /* get capabilities, error will be posted */
441   if (!gst_v4l2_get_capabilities (v4l2object))
442     goto error;
443
444   /* do we need to be a capture device? */
445   if (GST_IS_V4L2SRC (v4l2object) &&
446       !(v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
447     goto not_capture;
448
449   /* create enumerations, posts errors. */
450   if (!gst_v4l2_fill_lists (v4l2object))
451     goto error;
452
453   GST_INFO_OBJECT (v4l2object->element,
454       "Opened device '%s' (%s) successfully",
455       v4l2object->vcap.card, v4l2object->videodev);
456
457   pollfd.fd = v4l2object->video_fd;
458   gst_poll_add_fd (v4l2object->poll, &pollfd);
459   gst_poll_fd_ctl_read (v4l2object->poll, &pollfd, TRUE);
460
461   return TRUE;
462
463   /* ERRORS */
464 stat_failed:
465   {
466     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
467         (_("Cannot identify device '%s'."), v4l2object->videodev),
468         GST_ERROR_SYSTEM);
469     goto error;
470   }
471 no_device:
472   {
473     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
474         (_("This isn't a device '%s'."), v4l2object->videodev),
475         GST_ERROR_SYSTEM);
476     goto error;
477   }
478 not_open:
479   {
480     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
481         (_("Could not open device '%s' for reading and writing."),
482             v4l2object->videodev), GST_ERROR_SYSTEM);
483     goto error;
484   }
485 not_capture:
486   {
487     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
488         (_("Device '%s' is not a capture device."),
489             v4l2object->videodev),
490         ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
491     goto error;
492   }
493 error:
494   {
495     if (GST_V4L2_IS_OPEN (v4l2object)) {
496       /* close device */
497       v4l2_close (v4l2object->video_fd);
498       v4l2object->video_fd = -1;
499     }
500     /* empty lists */
501     gst_v4l2_empty_lists (v4l2object);
502
503     return FALSE;
504   }
505 }
506
507
508 /******************************************************
509  * gst_v4l2_close():
510  *   close the video device (v4l2object->video_fd)
511  * return value: TRUE on success, FALSE on error
512  ******************************************************/
513 gboolean
514 gst_v4l2_close (GstV4l2Object * v4l2object)
515 {
516   GstPollFD pollfd = GST_POLL_FD_INIT;
517   GST_DEBUG_OBJECT (v4l2object->element, "Trying to close %s",
518       v4l2object->videodev);
519
520   GST_V4L2_CHECK_OPEN (v4l2object);
521   GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
522
523   /* close device */
524   v4l2_close (v4l2object->video_fd);
525   pollfd.fd = v4l2object->video_fd;
526   gst_poll_remove_fd (v4l2object->poll, &pollfd);
527   v4l2object->video_fd = -1;
528
529   /* empty lists */
530   gst_v4l2_empty_lists (v4l2object);
531
532   return TRUE;
533 }
534
535
536 /******************************************************
537  * gst_v4l2_get_norm()
538  *   Get the norm of the current device
539  * return value: TRUE on success, FALSE on error
540  ******************************************************/
541 gboolean
542 gst_v4l2_get_norm (GstV4l2Object * v4l2object, v4l2_std_id * norm)
543 {
544   GST_DEBUG_OBJECT (v4l2object->element, "getting norm");
545
546   if (!GST_V4L2_IS_OPEN (v4l2object))
547     return FALSE;
548
549   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_STD, norm) < 0)
550     goto std_failed;
551
552   return TRUE;
553
554   /* ERRORS */
555 std_failed:
556   {
557     GST_DEBUG ("Failed to get the current norm for device %s",
558         v4l2object->videodev);
559     return FALSE;
560   }
561 }
562
563
564 /******************************************************
565  * gst_v4l2_set_norm()
566  *   Set the norm of the current device
567  * return value: TRUE on success, FALSE on error
568  ******************************************************/
569 gboolean
570 gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm)
571 {
572   GST_DEBUG_OBJECT (v4l2object->element, "trying to set norm to %llx", norm);
573
574   if (!GST_V4L2_IS_OPEN (v4l2object))
575     return FALSE;
576
577   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_STD, &norm) < 0)
578     goto std_failed;
579
580   return TRUE;
581
582   /* ERRORS */
583 std_failed:
584   {
585     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
586         (_("Failed to set norm for device '%s'."),
587             v4l2object->videodev), GST_ERROR_SYSTEM);
588     return FALSE;
589   }
590 }
591
592 /******************************************************
593  * gst_v4l2_get_frequency():
594  *   get the current frequency
595  * return value: TRUE on success, FALSE on error
596  ******************************************************/
597 gboolean
598 gst_v4l2_get_frequency (GstV4l2Object * v4l2object,
599     gint tunernum, gulong * frequency)
600 {
601   struct v4l2_frequency freq;
602
603   GstTunerChannel *channel;
604
605   GST_DEBUG_OBJECT (v4l2object->element, "getting current tuner frequency");
606
607   if (!GST_V4L2_IS_OPEN (v4l2object))
608     return FALSE;
609
610   channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
611
612   freq.tuner = tunernum;
613   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0)
614     goto freq_failed;
615
616   *frequency = freq.frequency * channel->freq_multiplicator;
617
618   return TRUE;
619
620   /* ERRORS */
621 freq_failed:
622   {
623     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
624         (_("Failed to get current tuner frequency for device '%s'."),
625             v4l2object->videodev), GST_ERROR_SYSTEM);
626     return FALSE;
627   }
628 }
629
630
631 /******************************************************
632  * gst_v4l2_set_frequency():
633  *   set frequency
634  * return value: TRUE on success, FALSE on error
635  ******************************************************/
636 gboolean
637 gst_v4l2_set_frequency (GstV4l2Object * v4l2object,
638     gint tunernum, gulong frequency)
639 {
640   struct v4l2_frequency freq;
641
642   GstTunerChannel *channel;
643
644   GST_DEBUG_OBJECT (v4l2object->element,
645       "setting current tuner frequency to %lu", frequency);
646
647   if (!GST_V4L2_IS_OPEN (v4l2object))
648     return FALSE;
649
650   channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
651
652   freq.tuner = tunernum;
653   /* fill in type - ignore error */
654   v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq);
655   freq.frequency = frequency / channel->freq_multiplicator;
656
657   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0)
658     goto freq_failed;
659
660   return TRUE;
661
662   /* ERRORS */
663 freq_failed:
664   {
665     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
666         (_("Failed to set current tuner frequency for device '%s' to %lu Hz."),
667             v4l2object->videodev, frequency), GST_ERROR_SYSTEM);
668     return FALSE;
669   }
670 }
671
672 /******************************************************
673  * gst_v4l2_signal_strength():
674  *   get the strength of the signal on the current input
675  * return value: TRUE on success, FALSE on error
676  ******************************************************/
677 gboolean
678 gst_v4l2_signal_strength (GstV4l2Object * v4l2object,
679     gint tunernum, gulong * signal_strength)
680 {
681   struct v4l2_tuner tuner;
682
683   GST_DEBUG_OBJECT (v4l2object->element, "trying to get signal strength");
684
685   if (!GST_V4L2_IS_OPEN (v4l2object))
686     return FALSE;
687
688   tuner.index = tunernum;
689   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &tuner) < 0)
690     goto tuner_failed;
691
692   *signal_strength = tuner.signal;
693
694   return TRUE;
695
696   /* ERRORS */
697 tuner_failed:
698   {
699     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
700         (_("Failed to get signal strength for device '%s'."),
701             v4l2object->videodev), GST_ERROR_SYSTEM);
702     return FALSE;
703   }
704 }
705
706 /******************************************************
707  * gst_v4l2_get_attribute():
708  *   try to get the value of one specific attribute
709  * return value: TRUE on success, FALSE on error
710  ******************************************************/
711 gboolean
712 gst_v4l2_get_attribute (GstV4l2Object * v4l2object,
713     int attribute_num, int *value)
714 {
715   struct v4l2_control control;
716
717   GST_DEBUG_OBJECT (v4l2object->element, "getting value of attribute %d",
718       attribute_num);
719
720   if (!GST_V4L2_IS_OPEN (v4l2object))
721     return FALSE;
722
723   control.id = attribute_num;
724
725   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0)
726     goto ctrl_failed;
727
728   *value = control.value;
729
730   return TRUE;
731
732   /* ERRORS */
733 ctrl_failed:
734   {
735     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
736         (_("Failed to get value for control %d on device '%s'."),
737             attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
738     return FALSE;
739   }
740 }
741
742
743 /******************************************************
744  * gst_v4l2_set_attribute():
745  *   try to set the value of one specific attribute
746  * return value: TRUE on success, FALSE on error
747  ******************************************************/
748 gboolean
749 gst_v4l2_set_attribute (GstV4l2Object * v4l2object,
750     int attribute_num, const int value)
751 {
752   struct v4l2_control control;
753
754   GST_DEBUG_OBJECT (v4l2object->element, "setting value of attribute %d to %d",
755       attribute_num, value);
756
757   if (!GST_V4L2_IS_OPEN (v4l2object))
758     return FALSE;
759
760   control.id = attribute_num;
761   control.value = value;
762   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0)
763     goto ctrl_failed;
764
765   return TRUE;
766
767   /* ERRORS */
768 ctrl_failed:
769   {
770     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
771         (_("Failed to set value %d for control %d on device '%s'."),
772             value, attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
773     return FALSE;
774   }
775 }
776
777 gboolean
778 gst_v4l2_get_input (GstV4l2Object * v4l2object, gint * input)
779 {
780   gint n;
781
782   GST_DEBUG_OBJECT (v4l2object->element, "trying to get input");
783
784   if (!GST_V4L2_IS_OPEN (v4l2object))
785     return FALSE;
786
787   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_INPUT, &n) < 0)
788     goto input_failed;
789
790   *input = n;
791
792   GST_DEBUG_OBJECT (v4l2object->element, "input: %d", n);
793
794   return TRUE;
795
796   /* ERRORS */
797 input_failed:
798   {
799     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
800         (_("Failed to get current input on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
801     return FALSE;
802   }
803 }
804
805 gboolean
806 gst_v4l2_set_input (GstV4l2Object * v4l2object, gint input)
807 {
808   GST_DEBUG_OBJECT (v4l2object->element, "trying to set input to %d", input);
809
810   if (!GST_V4L2_IS_OPEN (v4l2object))
811     return FALSE;
812
813   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_INPUT, &input) < 0)
814     goto input_failed;
815
816   return TRUE;
817
818   /* ERRORS */
819 input_failed:
820   {
821     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
822         (_("Failed to set input %d on device %s."),
823             input, v4l2object->videodev), GST_ERROR_SYSTEM);
824     return FALSE;
825   }
826 }