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