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