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