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