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