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