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