sys/v4l2/: Fix compilation with newer GIT kernels that deprecated
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / v4l2_calls.c
1 /* GStreamer
2  *
3  * Copyright (C) 2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4  *               2006 Edgard Lima <edgard.lima@indt.org.br>
5  *
6  * v4l2_calls.c - generic V4L2 calls handling
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <sys/ioctl.h>
32 #include <sys/mman.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #ifdef __sun
37 /* Needed on older Solaris Nevada builds (72 at least) */
38 #include <stropts.h>
39 #include <sys/ioccom.h>
40 #endif
41 #include "v4l2_calls.h"
42 #include "gstv4l2tuner.h"
43 #include "gstv4l2xoverlay.h"
44 #include "gstv4l2colorbalance.h"
45
46 #include "gstv4l2src.h"
47
48 /* Those are ioctl calls */
49 #ifndef V4L2_CID_HCENTER
50 #define V4L2_CID_HCENTER V4L2_CID_HCENTER_DEPRECATED
51 #endif
52 #ifndef V4L2_CID_VCENTER
53 #define V4L2_CID_VCENTER V4L2_CID_VCENTER_DEPRECATED
54 #endif
55
56 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
57 #define GST_CAT_DEFAULT v4l2_debug
58
59 /******************************************************
60  * gst_v4l2_get_capabilities():
61  *   get the device's capturing capabilities
62  * return value: TRUE on success, FALSE on error
63  ******************************************************/
64 gboolean
65 gst_v4l2_get_capabilities (GstV4l2Object * v4l2object)
66 {
67   GstElement *e;
68
69   e = v4l2object->element;
70
71   GST_DEBUG_OBJECT (e, "getting capabilities");
72
73   if (!GST_V4L2_IS_OPEN (v4l2object))
74     return FALSE;
75
76   if (ioctl (v4l2object->video_fd, VIDIOC_QUERYCAP, &v4l2object->vcap) < 0)
77     goto cap_failed;
78
79   GST_LOG_OBJECT (e, "driver:      '%s'", v4l2object->vcap.driver);
80   GST_LOG_OBJECT (e, "card:        '%s'", v4l2object->vcap.card);
81   GST_LOG_OBJECT (e, "bus_info:    '%s'", v4l2object->vcap.bus_info);
82   GST_LOG_OBJECT (e, "version:     %08x", v4l2object->vcap.version);
83   GST_LOG_OBJECT (e, "capabilites: %08x", v4l2object->vcap.capabilities);
84
85   return TRUE;
86
87   /* ERRORS */
88 cap_failed:
89   {
90     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
91         (_("Error getting capabilities for device '%s': "
92                 "It isn't a v4l2 driver. Check if it is a v4l1 driver."),
93             v4l2object->videodev), GST_ERROR_SYSTEM);
94     return FALSE;
95   }
96 }
97
98
99 /******************************************************
100  * gst_v4l2_empty_lists() and gst_v4l2_fill_lists():
101  *   fill/empty the lists of enumerations
102  * return value: TRUE on success, FALSE on error
103  ******************************************************/
104 static gboolean
105 gst_v4l2_fill_lists (GstV4l2Object * v4l2object)
106 {
107   gint n;
108
109   GstElement *e;
110
111   e = v4l2object->element;
112
113   GST_DEBUG_OBJECT (e, "getting enumerations");
114   GST_V4L2_CHECK_OPEN (v4l2object);
115
116   GST_DEBUG_OBJECT (e, "  channels");
117   /* and now, the channels */
118   for (n = 0;; n++) {
119     struct v4l2_input input = { 0, };
120     GstV4l2TunerChannel *v4l2channel;
121
122     GstTunerChannel *channel;
123
124     input.index = n;
125     if (ioctl (v4l2object->video_fd, VIDIOC_ENUMINPUT, &input) < 0) {
126       if (errno == EINVAL)
127         break;                  /* end of enumeration */
128       else {
129         GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
130             (_("Failed to query attributes of input %d in device %s"),
131                 n, v4l2object->videodev),
132             ("Failed to get %d in input enumeration for %s. (%d - %s)",
133                 n, v4l2object->videodev, errno, strerror (errno)));
134         return FALSE;
135       }
136     }
137
138     GST_LOG_OBJECT (e, "   index:     %d", input.index);
139     GST_LOG_OBJECT (e, "   name:      '%s'", input.name);
140     GST_LOG_OBJECT (e, "   type:      %08x", input.type);
141     GST_LOG_OBJECT (e, "   audioset:  %08x", input.audioset);
142     GST_LOG_OBJECT (e, "   std:       %016x", input.std);
143     GST_LOG_OBJECT (e, "   status:    %08x", input.status);
144
145     v4l2channel = g_object_new (GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
146     channel = GST_TUNER_CHANNEL (v4l2channel);
147     channel->label = g_strdup ((const gchar *) input.name);
148     channel->flags = GST_TUNER_CHANNEL_INPUT;
149     v4l2channel->index = n;
150
151     if (input.type == V4L2_INPUT_TYPE_TUNER) {
152       struct v4l2_tuner vtun;
153
154       v4l2channel->tuner = input.tuner;
155       channel->flags |= GST_TUNER_CHANNEL_FREQUENCY;
156
157       vtun.index = input.tuner;
158       if (ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &vtun) < 0) {
159         GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
160             (_("Failed to get setting of tuner %d on device '%s'."),
161                 input.tuner, v4l2object->videodev), GST_ERROR_SYSTEM);
162         g_object_unref (G_OBJECT (channel));
163         return FALSE;
164       }
165
166       channel->freq_multiplicator =
167           62.5 * ((vtun.capability & V4L2_TUNER_CAP_LOW) ? 1 : 1000);
168       channel->min_frequency = vtun.rangelow * channel->freq_multiplicator;
169       channel->max_frequency = vtun.rangehigh * channel->freq_multiplicator;
170       channel->min_signal = 0;
171       channel->max_signal = 0xffff;
172     }
173     if (input.audioset) {
174       /* we take the first. We don't care for
175        * the others for now */
176       while (!(input.audioset & (1 << v4l2channel->audio)))
177         v4l2channel->audio++;
178       channel->flags |= GST_TUNER_CHANNEL_AUDIO;
179     }
180
181     v4l2object->channels =
182         g_list_append (v4l2object->channels, (gpointer) channel);
183   }
184
185   GST_DEBUG_OBJECT (e, "  norms");
186   /* norms... */
187   for (n = 0;; n++) {
188     struct v4l2_standard standard = { 0, };
189     GstV4l2TunerNorm *v4l2norm;
190
191     GstTunerNorm *norm;
192
193     /* fill in defaults */
194     standard.frameperiod.numerator = 1;
195     standard.frameperiod.denominator = 0;
196     standard.index = n;
197
198     if (ioctl (v4l2object->video_fd, VIDIOC_ENUMSTD, &standard) < 0) {
199       if (errno == EINVAL || errno == ENOTTY)
200         break;                  /* end of enumeration */
201       else {
202         GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
203             (_("Failed to query norm on device '%s'."),
204                 v4l2object->videodev),
205             ("Failed to get attributes for norm %d on devide '%s'. (%d - %s)",
206                 n, v4l2object->videodev, errno, strerror (errno)));
207         return FALSE;
208       }
209     }
210
211     GST_DEBUG_OBJECT (e, "    '%s', fps: %d / %d",
212         standard.name, standard.frameperiod.denominator,
213         standard.frameperiod.numerator);
214
215     v4l2norm = g_object_new (GST_TYPE_V4L2_TUNER_NORM, NULL);
216     norm = GST_TUNER_NORM (v4l2norm);
217     norm->label = g_strdup ((const gchar *) standard.name);
218     gst_value_set_fraction (&norm->framerate,
219         standard.frameperiod.denominator, standard.frameperiod.numerator);
220     v4l2norm->index = standard.id;
221
222     v4l2object->norms = g_list_append (v4l2object->norms, (gpointer) norm);
223   }
224
225   GST_DEBUG_OBJECT (e, "  controls+menus");
226   /* and lastly, controls+menus (if appropriate) */
227   for (n = V4L2_CID_BASE;; n++) {
228     struct v4l2_queryctrl control = { 0, };
229     GstV4l2ColorBalanceChannel *v4l2channel;
230
231     GstColorBalanceChannel *channel;
232
233     /* when we reached the last official CID, continue with private CIDs */
234     if (n == V4L2_CID_LASTP1) {
235       GST_DEBUG_OBJECT (e, "checking private CIDs");
236       n = V4L2_CID_PRIVATE_BASE;
237       /* FIXME: We are still not handling private controls. We need a new GstInterface
238          to export those controls */
239       break;
240     }
241
242     control.id = n;
243     if (ioctl (v4l2object->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
244       if (errno == EINVAL) {
245         if (n < V4L2_CID_PRIVATE_BASE)
246           /* continue so that we also check private controls */
247           continue;
248         else
249           break;
250       } else {
251         GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
252             (_("Failed getting controls attributes on device '%s.'"),
253                 v4l2object->videodev),
254             ("Failed querying control %d on device '%s'. (%d - %s)",
255                 n, v4l2object->videodev, errno, strerror (errno)));
256         return FALSE;
257       }
258     }
259     if (control.flags & V4L2_CTRL_FLAG_DISABLED)
260       continue;
261
262     switch (n) {
263       case V4L2_CID_BRIGHTNESS:
264       case V4L2_CID_CONTRAST:
265       case V4L2_CID_SATURATION:
266       case V4L2_CID_HUE:
267       case V4L2_CID_BLACK_LEVEL:
268       case V4L2_CID_AUTO_WHITE_BALANCE:
269       case V4L2_CID_DO_WHITE_BALANCE:
270       case V4L2_CID_RED_BALANCE:
271       case V4L2_CID_BLUE_BALANCE:
272       case V4L2_CID_GAMMA:
273       case V4L2_CID_EXPOSURE:
274       case V4L2_CID_AUTOGAIN:
275       case V4L2_CID_GAIN:
276         /* we only handle these for now (why?) */
277         break;
278       case V4L2_CID_HFLIP:
279       case V4L2_CID_VFLIP:
280       case V4L2_CID_HCENTER:
281       case V4L2_CID_VCENTER:
282 #ifdef V4L2_CID_PAN_RESET
283       case V4L2_CID_PAN_RESET:
284 #endif
285 #ifdef V4L2_CID_TILT_RESET
286       case V4L2_CID_TILT_RESET:
287 #endif
288         /* not handled here, handled by VideoOrientation interface */
289         control.id++;
290         break;
291       case V4L2_CID_AUDIO_VOLUME:
292       case V4L2_CID_AUDIO_BALANCE:
293       case V4L2_CID_AUDIO_BASS:
294       case V4L2_CID_AUDIO_TREBLE:
295       case V4L2_CID_AUDIO_MUTE:
296       case V4L2_CID_AUDIO_LOUDNESS:
297         /* FIXME: We should implement GstMixer interface */
298         /* fall through */
299       default:
300         GST_DEBUG_OBJECT (e,
301             "ControlID %s (%x) unhandled, FIXME", control.name, n);
302         control.id++;
303         break;
304     }
305     if (n != control.id)
306       continue;
307
308     GST_DEBUG_OBJECT (e, "Adding ControlID %s (%x)", control.name, n);
309     v4l2channel = g_object_new (GST_TYPE_V4L2_COLOR_BALANCE_CHANNEL, NULL);
310     channel = GST_COLOR_BALANCE_CHANNEL (v4l2channel);
311     channel->label = g_strdup ((const gchar *) control.name);
312     v4l2channel->id = n;
313
314 #if 0                           /* FIXME: it will be need just when handling private controls
315                                    (currently none of base controls are of this type) */
316     if (control.type == V4L2_CTRL_TYPE_MENU) {
317       struct v4l2_querymenu menu, *mptr;
318
319       int i;
320
321       menu.id = n;
322       for (i = 0;; i++) {
323         menu.index = i;
324         if (ioctl (v4l2object->video_fd, VIDIOC_QUERYMENU, &menu) < 0) {
325           if (errno == EINVAL)
326             break;              /* end of enumeration */
327           else {
328             GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
329                 (_("Failed getting controls attributes on device '%s'."),
330                     v4l2object->videodev),
331                 ("Failed to get %d in menu enumeration for %s. (%d - %s)",
332                     n, v4l2object->videodev, errno, strerror (errno)));
333             return FALSE;
334           }
335         }
336         mptr = g_malloc (sizeof (menu));
337         memcpy (mptr, &menu, sizeof (menu));
338         menus = g_list_append (menus, mptr);
339       }
340     }
341     v4l2object->menus = g_list_append (v4l2object->menus, menus);
342 #endif
343
344     switch (control.type) {
345       case V4L2_CTRL_TYPE_INTEGER:
346         channel->min_value = control.minimum;
347         channel->max_value = control.maximum;
348         break;
349       case V4L2_CTRL_TYPE_BOOLEAN:
350         channel->min_value = FALSE;
351         channel->max_value = TRUE;
352         break;
353       default:
354         /* FIXME we should find out how to handle V4L2_CTRL_TYPE_BUTTON.
355            BUTTON controls like V4L2_CID_DO_WHITE_BALANCE can just be set (1) or
356            unset (0), but can't be queried */
357         GST_DEBUG_OBJECT (e,
358             "Control with non supported type %s (%x), type=%d",
359             control.name, n, control.type);
360         channel->min_value = channel->max_value = 0;
361         break;
362     }
363
364     v4l2object->colors = g_list_append (v4l2object->colors, (gpointer) channel);
365   }
366
367   GST_DEBUG_OBJECT (e, "done");
368   return TRUE;
369 }
370
371
372 static void
373 gst_v4l2_empty_lists (GstV4l2Object * v4l2object)
374 {
375   GST_DEBUG_OBJECT (v4l2object->element, "deleting enumerations");
376
377   g_list_foreach (v4l2object->channels, (GFunc) g_object_unref, NULL);
378   g_list_free (v4l2object->channels);
379   v4l2object->channels = NULL;
380
381   g_list_foreach (v4l2object->norms, (GFunc) g_object_unref, NULL);
382   g_list_free (v4l2object->norms);
383   v4l2object->norms = NULL;
384
385   g_list_foreach (v4l2object->colors, (GFunc) g_object_unref, NULL);
386   g_list_free (v4l2object->colors);
387   v4l2object->colors = NULL;
388 }
389
390 /******************************************************
391  * gst_v4l2_open():
392  *   open the video device (v4l2object->videodev)
393  * return value: TRUE on success, FALSE on error
394  ******************************************************/
395 gboolean
396 gst_v4l2_open (GstV4l2Object * v4l2object)
397 {
398   struct stat st;
399
400   GST_DEBUG_OBJECT (v4l2object->element, "Trying to open device %s",
401       v4l2object->videodev);
402
403   GST_V4L2_CHECK_NOT_OPEN (v4l2object);
404   GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
405
406   /* be sure we have a device */
407   if (!v4l2object->videodev)
408     v4l2object->videodev = g_strdup ("/dev/video");
409
410   /* check if it is a device */
411   if (stat (v4l2object->videodev, &st) == -1)
412     goto stat_failed;
413
414   if (!S_ISCHR (st.st_mode))
415     goto no_device;
416
417   /* open the device */
418   v4l2object->video_fd =
419       open (v4l2object->videodev, O_RDWR /* | O_NONBLOCK */ );
420
421   if (!GST_V4L2_IS_OPEN (v4l2object))
422     goto not_open;
423
424   /* get capabilities, error will be posted */
425   if (!gst_v4l2_get_capabilities (v4l2object))
426     goto error;
427
428   /* do we need to be a capture device? */
429   if (GST_IS_V4L2SRC (v4l2object) &&
430       !(v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
431     goto not_capture;
432
433   /* create enumerations, posts errors. */
434   if (!gst_v4l2_fill_lists (v4l2object))
435     goto error;
436
437   GST_INFO_OBJECT (v4l2object->element,
438       "Opened device '%s' (%s) successfully",
439       v4l2object->vcap.card, v4l2object->videodev);
440
441   return TRUE;
442
443   /* ERRORS */
444 stat_failed:
445   {
446     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
447         (_("Cannot identify device '%s'."), v4l2object->videodev),
448         GST_ERROR_SYSTEM);
449     goto error;
450   }
451 no_device:
452   {
453     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
454         (_("This isn't a device '%s'."), v4l2object->videodev),
455         GST_ERROR_SYSTEM);
456     goto error;
457   }
458 not_open:
459   {
460     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
461         (_("Could not open device '%s' for reading and writing."),
462             v4l2object->videodev), GST_ERROR_SYSTEM);
463     goto error;
464   }
465 not_capture:
466   {
467     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
468         (_("Device '%s' is not a capture device."),
469             v4l2object->videodev),
470         ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
471     goto error;
472   }
473 error:
474   {
475     if (GST_V4L2_IS_OPEN (v4l2object)) {
476       /* close device */
477       close (v4l2object->video_fd);
478       v4l2object->video_fd = -1;
479     }
480     /* empty lists */
481     gst_v4l2_empty_lists (v4l2object);
482
483     return FALSE;
484   }
485 }
486
487
488 /******************************************************
489  * gst_v4l2_close():
490  *   close the video device (v4l2object->video_fd)
491  * return value: TRUE on success, FALSE on error
492  ******************************************************/
493 gboolean
494 gst_v4l2_close (GstV4l2Object * v4l2object)
495 {
496   GST_DEBUG_OBJECT (v4l2object->element, "Trying to close %s",
497       v4l2object->videodev);
498
499   GST_V4L2_CHECK_OPEN (v4l2object);
500   GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
501
502   /* close device */
503   close (v4l2object->video_fd);
504   v4l2object->video_fd = -1;
505
506   /* empty lists */
507   gst_v4l2_empty_lists (v4l2object);
508
509   return TRUE;
510 }
511
512
513 /******************************************************
514  * gst_v4l2_get_norm()
515  *   Get the norm of the current device
516  * return value: TRUE on success, FALSE on error
517  ******************************************************/
518 gboolean
519 gst_v4l2_get_norm (GstV4l2Object * v4l2object, v4l2_std_id * norm)
520 {
521   GST_DEBUG_OBJECT (v4l2object->element, "getting norm");
522
523   if (!GST_V4L2_IS_OPEN (v4l2object))
524     return FALSE;
525
526   if (ioctl (v4l2object->video_fd, VIDIOC_G_STD, norm) < 0)
527     goto std_failed;
528
529   return TRUE;
530
531   /* ERRORS */
532 std_failed:
533   {
534     GST_DEBUG ("Failed to get the current norm for device %s",
535         v4l2object->videodev);
536     return FALSE;
537   }
538 }
539
540
541 /******************************************************
542  * gst_v4l2_set_norm()
543  *   Set the norm of the current device
544  * return value: TRUE on success, FALSE on error
545  ******************************************************/
546 gboolean
547 gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm)
548 {
549   GST_DEBUG_OBJECT (v4l2object->element, "trying to set norm to %llx", norm);
550
551   if (!GST_V4L2_IS_OPEN (v4l2object))
552     return FALSE;
553
554   if (ioctl (v4l2object->video_fd, VIDIOC_S_STD, &norm) < 0)
555     goto std_failed;
556
557   return TRUE;
558
559   /* ERRORS */
560 std_failed:
561   {
562     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
563         (_("Failed to set norm for device '%s'."),
564             v4l2object->videodev), GST_ERROR_SYSTEM);
565     return FALSE;
566   }
567 }
568
569 /******************************************************
570  * gst_v4l2_get_frequency():
571  *   get the current frequency
572  * return value: TRUE on success, FALSE on error
573  ******************************************************/
574 gboolean
575 gst_v4l2_get_frequency (GstV4l2Object * v4l2object,
576     gint tunernum, gulong * frequency)
577 {
578   struct v4l2_frequency freq;
579
580   GstTunerChannel *channel;
581
582   GST_DEBUG_OBJECT (v4l2object->element, "getting current tuner frequency");
583
584   if (!GST_V4L2_IS_OPEN (v4l2object))
585     return FALSE;
586
587   channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
588
589   freq.tuner = tunernum;
590   if (ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0)
591     goto freq_failed;
592
593   *frequency = freq.frequency * channel->freq_multiplicator;
594
595   return TRUE;
596
597   /* ERRORS */
598 freq_failed:
599   {
600     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
601         (_("Failed to get current tuner frequency for device '%s'."),
602             v4l2object->videodev), GST_ERROR_SYSTEM);
603     return FALSE;
604   }
605 }
606
607
608 /******************************************************
609  * gst_v4l2_set_frequency():
610  *   set frequency
611  * return value: TRUE on success, FALSE on error
612  ******************************************************/
613 gboolean
614 gst_v4l2_set_frequency (GstV4l2Object * v4l2object,
615     gint tunernum, gulong frequency)
616 {
617   struct v4l2_frequency freq;
618
619   GstTunerChannel *channel;
620
621   GST_DEBUG_OBJECT (v4l2object->element,
622       "setting current tuner frequency to %lu", frequency);
623
624   if (!GST_V4L2_IS_OPEN (v4l2object))
625     return FALSE;
626
627   channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
628
629   freq.tuner = tunernum;
630   /* fill in type - ignore error */
631   ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq);
632   freq.frequency = frequency / channel->freq_multiplicator;
633
634   if (ioctl (v4l2object->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0)
635     goto freq_failed;
636
637   return TRUE;
638
639   /* ERRORS */
640 freq_failed:
641   {
642     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
643         (_("Failed to set current tuner frequency for device '%s' to %lu Hz."),
644             v4l2object->videodev, frequency), GST_ERROR_SYSTEM);
645     return FALSE;
646   }
647 }
648
649 /******************************************************
650  * gst_v4l2_signal_strength():
651  *   get the strength of the signal on the current input
652  * return value: TRUE on success, FALSE on error
653  ******************************************************/
654 gboolean
655 gst_v4l2_signal_strength (GstV4l2Object * v4l2object,
656     gint tunernum, gulong * signal_strength)
657 {
658   struct v4l2_tuner tuner;
659
660   GST_DEBUG_OBJECT (v4l2object->element, "trying to get signal strength");
661
662   if (!GST_V4L2_IS_OPEN (v4l2object))
663     return FALSE;
664
665   tuner.index = tunernum;
666   if (ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &tuner) < 0)
667     goto tuner_failed;
668
669   *signal_strength = tuner.signal;
670
671   return TRUE;
672
673   /* ERRORS */
674 tuner_failed:
675   {
676     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
677         (_("Failed to get signal strength for device '%s'."),
678             v4l2object->videodev), GST_ERROR_SYSTEM);
679     return FALSE;
680   }
681 }
682
683 /******************************************************
684  * gst_v4l2_get_attribute():
685  *   try to get the value of one specific attribute
686  * return value: TRUE on success, FALSE on error
687  ******************************************************/
688 gboolean
689 gst_v4l2_get_attribute (GstV4l2Object * v4l2object,
690     int attribute_num, int *value)
691 {
692   struct v4l2_control control;
693
694   GST_DEBUG_OBJECT (v4l2object->element, "getting value of attribute %d",
695       attribute_num);
696
697   if (!GST_V4L2_IS_OPEN (v4l2object))
698     return FALSE;
699
700   control.id = attribute_num;
701
702   if (ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0)
703     goto ctrl_failed;
704
705   *value = control.value;
706
707   return TRUE;
708
709   /* ERRORS */
710 ctrl_failed:
711   {
712     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
713         (_("Failed to get value for control %d on device '%s'."),
714             attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
715     return FALSE;
716   }
717 }
718
719
720 /******************************************************
721  * gst_v4l2_set_attribute():
722  *   try to set the value of one specific attribute
723  * return value: TRUE on success, FALSE on error
724  ******************************************************/
725 gboolean
726 gst_v4l2_set_attribute (GstV4l2Object * v4l2object,
727     int attribute_num, const int value)
728 {
729   struct v4l2_control control;
730
731   GST_DEBUG_OBJECT (v4l2object->element, "setting value of attribute %d to %d",
732       attribute_num, value);
733
734   if (!GST_V4L2_IS_OPEN (v4l2object))
735     return FALSE;
736
737   control.id = attribute_num;
738   control.value = value;
739   if (ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0)
740     goto ctrl_failed;
741
742   return TRUE;
743
744   /* ERRORS */
745 ctrl_failed:
746   {
747     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
748         (_("Failed to set value %d for control %d on device '%s'."),
749             value, attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
750     return FALSE;
751   }
752 }
753
754 gboolean
755 gst_v4l2_get_input (GstV4l2Object * v4l2object, gint * input)
756 {
757   gint n;
758
759   GST_DEBUG_OBJECT (v4l2object->element, "trying to get input");
760
761   if (!GST_V4L2_IS_OPEN (v4l2object))
762     return FALSE;
763
764   if (ioctl (v4l2object->video_fd, VIDIOC_G_INPUT, &n) < 0)
765     goto input_failed;
766
767   *input = n;
768
769   GST_DEBUG_OBJECT (v4l2object->element, "input: %d", n);
770
771   return TRUE;
772
773   /* ERRORS */
774 input_failed:
775   {
776     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
777         (_("Failed to get current input on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
778     return FALSE;
779   }
780 }
781
782 gboolean
783 gst_v4l2_set_input (GstV4l2Object * v4l2object, gint input)
784 {
785   GST_DEBUG_OBJECT (v4l2object->element, "trying to set input to %d", input);
786
787   if (!GST_V4L2_IS_OPEN (v4l2object))
788     return FALSE;
789
790   if (ioctl (v4l2object->video_fd, VIDIOC_S_INPUT, &input) < 0)
791     goto input_failed;
792
793   return TRUE;
794
795   /* ERRORS */
796 input_failed:
797   {
798     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
799         (_("Failed to set input %d on device %s."),
800             input, v4l2object->videodev), GST_ERROR_SYSTEM);
801     return FALSE;
802   }
803 }