[v4l2src] support USB web camera on odroid U3
[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 GST_EXT_V4L2SRC_MODIFIED
37 #include <glob.h>
38 #endif /* GST_EXT_V4L2SRC_MODIFIED */
39 #ifdef __sun
40 /* Needed on older Solaris Nevada builds (72 at least) */
41 #include <stropts.h>
42 #include <sys/ioccom.h>
43 #endif
44 #include "v4l2_calls.h"
45 #include "gstv4l2tuner.h"
46 #if 0
47 #include "gstv4l2xoverlay.h"
48 #endif
49 #include "gstv4l2colorbalance.h"
50
51 #include "gstv4l2src.h"
52 #include "gstv4l2sink.h"
53 #include "gstv4l2videodec.h"
54
55 #include "gst/gst-i18n-plugin.h"
56
57 #ifdef GST_EXT_V4L2SRC_MODIFIED
58 enum {
59   V4L2_OPEN_ERROR = 0,
60   V4L2_OPEN_ERROR_STAT_FAILED,
61   V4L2_OPEN_ERROR_NO_DEVICE,
62   V4L2_OPEN_ERROR_NOT_OPEN,
63   V4L2_OPEN_ERROR_NOT_CAPTURE,
64   V4L2_OPEN_ERROR_NOT_OUTPUT
65 };
66 #endif /* GST_EXT_V4L2SRC_MODIFIED */
67
68 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
69 #define GST_CAT_DEFAULT v4l2_debug
70
71 /******************************************************
72  * gst_v4l2_get_capabilities():
73  *   get the device's capturing capabilities
74  * return value: TRUE on success, FALSE on error
75  ******************************************************/
76 gboolean
77 gst_v4l2_get_capabilities (GstV4l2Object * v4l2object)
78 {
79   GstElement *e;
80
81   e = v4l2object->element;
82
83   GST_DEBUG_OBJECT (e, "getting capabilities");
84
85   if (!GST_V4L2_IS_OPEN (v4l2object))
86     return FALSE;
87
88   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYCAP, &v4l2object->vcap) < 0)
89     goto cap_failed;
90
91   GST_LOG_OBJECT (e, "driver:      '%s'", v4l2object->vcap.driver);
92   GST_LOG_OBJECT (e, "card:        '%s'", v4l2object->vcap.card);
93   GST_LOG_OBJECT (e, "bus_info:    '%s'", v4l2object->vcap.bus_info);
94   GST_LOG_OBJECT (e, "version:     %08x", v4l2object->vcap.version);
95   GST_LOG_OBJECT (e, "capabilites: %08x", v4l2object->vcap.capabilities);
96
97   return TRUE;
98
99   /* ERRORS */
100 cap_failed:
101   {
102     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
103         (_("Error getting capabilities for device '%s': "
104                 "It isn't a v4l2 driver. Check if it is a v4l1 driver."),
105             v4l2object->videodev), GST_ERROR_SYSTEM);
106     return FALSE;
107   }
108 }
109
110 /******************************************************
111  * The video4linux command line tool v4l2-ctrl
112  * normalises the names of the controls received from
113  * the kernel like:
114  *
115  *     "Exposure (absolute)" -> "exposure_absolute"
116  *
117  * We follow their lead here.  @name is modified
118  * in-place.
119  ******************************************************/
120 static void
121 gst_v4l2_normalise_control_name (gchar * name)
122 {
123   int i, j;
124   for (i = 0, j = 0; name[j]; ++j) {
125     if (g_ascii_isalnum (name[j])) {
126       if (i > 0 && !g_ascii_isalnum (name[j - 1]))
127         name[i++] = '_';
128       name[i++] = g_ascii_tolower (name[j]);
129     }
130   }
131   name[i++] = '\0';
132 }
133
134 /******************************************************
135  * gst_v4l2_empty_lists() and gst_v4l2_fill_lists():
136  *   fill/empty the lists of enumerations
137  * return value: TRUE on success, FALSE on error
138  ******************************************************/
139 static gboolean
140 gst_v4l2_fill_lists (GstV4l2Object * v4l2object)
141 {
142   gint n, next;
143   struct v4l2_queryctrl control = { 0, };
144
145   GstElement *e;
146
147   e = v4l2object->element;
148
149   GST_DEBUG_OBJECT (e, "getting enumerations");
150   GST_V4L2_CHECK_OPEN (v4l2object);
151
152   GST_DEBUG_OBJECT (e, "  channels");
153   /* and now, the channels */
154   for (n = 0;; n++) {
155     struct v4l2_input input;
156     GstV4l2TunerChannel *v4l2channel;
157     GstTunerChannel *channel;
158
159     memset (&input, 0, sizeof (input));
160
161     input.index = n;
162     if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUMINPUT, &input) < 0) {
163       if (errno == EINVAL || errno == ENOTTY)
164         break;                  /* end of enumeration */
165       else {
166         GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
167             (_("Failed to query attributes of input %d in device %s"),
168                 n, v4l2object->videodev),
169             ("Failed to get %d in input enumeration for %s. (%d - %s)",
170                 n, v4l2object->videodev, errno, strerror (errno)));
171         return FALSE;
172       }
173     }
174
175     GST_LOG_OBJECT (e, "   index:     %d", input.index);
176     GST_LOG_OBJECT (e, "   name:      '%s'", input.name);
177     GST_LOG_OBJECT (e, "   type:      %08x", input.type);
178     GST_LOG_OBJECT (e, "   audioset:  %08x", input.audioset);
179     GST_LOG_OBJECT (e, "   std:       %016" G_GINT64_MODIFIER "x",
180         (guint64) input.std);
181     GST_LOG_OBJECT (e, "   status:    %08x", input.status);
182
183     v4l2channel = g_object_new (GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
184     channel = GST_TUNER_CHANNEL (v4l2channel);
185     channel->label = g_strdup ((const gchar *) input.name);
186     channel->flags = GST_TUNER_CHANNEL_INPUT;
187     v4l2channel->index = n;
188
189     if (input.type == V4L2_INPUT_TYPE_TUNER) {
190       struct v4l2_tuner vtun;
191
192       v4l2channel->tuner = input.tuner;
193       channel->flags |= GST_TUNER_CHANNEL_FREQUENCY;
194
195       vtun.index = input.tuner;
196       if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &vtun) < 0) {
197         GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
198             (_("Failed to get setting of tuner %d on device '%s'."),
199                 input.tuner, v4l2object->videodev), GST_ERROR_SYSTEM);
200         g_object_unref (G_OBJECT (channel));
201         return FALSE;
202       }
203
204       channel->freq_multiplicator =
205           62.5 * ((vtun.capability & V4L2_TUNER_CAP_LOW) ? 1 : 1000);
206       channel->min_frequency = vtun.rangelow * channel->freq_multiplicator;
207       channel->max_frequency = vtun.rangehigh * channel->freq_multiplicator;
208       channel->min_signal = 0;
209       channel->max_signal = 0xffff;
210     }
211     if (input.audioset) {
212       /* we take the first. We don't care for
213        * the others for now */
214       while (!(input.audioset & (1 << v4l2channel->audio)))
215         v4l2channel->audio++;
216       channel->flags |= GST_TUNER_CHANNEL_AUDIO;
217     }
218
219     v4l2object->channels =
220         g_list_prepend (v4l2object->channels, (gpointer) channel);
221   }
222   v4l2object->channels = g_list_reverse (v4l2object->channels);
223
224   GST_DEBUG_OBJECT (e, "  norms");
225   /* norms... */
226   for (n = 0;; n++) {
227     struct v4l2_standard standard = { 0, };
228     GstV4l2TunerNorm *v4l2norm;
229
230     GstTunerNorm *norm;
231
232     /* fill in defaults */
233     standard.frameperiod.numerator = 1;
234     standard.frameperiod.denominator = 0;
235     standard.index = n;
236
237     if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUMSTD, &standard) < 0) {
238       if (errno == EINVAL || errno == ENOTTY)
239         break;                  /* end of enumeration */
240 #ifdef ENODATA
241       else if (errno == ENODATA)
242         break;                  /* end of enumeration, as of Linux 3.7-rc1 */
243 #endif
244       else {
245         GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
246             (_("Failed to query norm on device '%s'."),
247                 v4l2object->videodev),
248             ("Failed to get attributes for norm %d on devide '%s'. (%d - %s)",
249                 n, v4l2object->videodev, errno, strerror (errno)));
250         return FALSE;
251       }
252     }
253
254     GST_DEBUG_OBJECT (e, "    '%s', fps: %d / %d",
255         standard.name, standard.frameperiod.denominator,
256         standard.frameperiod.numerator);
257
258     v4l2norm = g_object_new (GST_TYPE_V4L2_TUNER_NORM, NULL);
259     norm = GST_TUNER_NORM (v4l2norm);
260     norm->label = g_strdup ((const gchar *) standard.name);
261     gst_value_set_fraction (&norm->framerate,
262         standard.frameperiod.denominator, standard.frameperiod.numerator);
263     v4l2norm->index = standard.id;
264
265     GST_DEBUG_OBJECT (v4l2object->element, "index=%08x, label=%s",
266         (unsigned int) v4l2norm->index, norm->label);
267
268     v4l2object->norms = g_list_prepend (v4l2object->norms, (gpointer) norm);
269   }
270   v4l2object->norms = g_list_reverse (v4l2object->norms);
271
272   GST_DEBUG_OBJECT (e, "  controls+menus");
273
274   /* and lastly, controls+menus (if appropriate) */
275   next = V4L2_CTRL_FLAG_NEXT_CTRL;
276   n = 0;
277   control.id = next;
278
279   while (TRUE) {
280     GstV4l2ColorBalanceChannel *v4l2channel;
281     GstColorBalanceChannel *channel;
282
283     if (!next)
284       n++;
285
286   retry:
287     /* when we reached the last official CID, continue with private CIDs */
288     if (n == V4L2_CID_LASTP1) {
289       GST_DEBUG_OBJECT (e, "checking private CIDs");
290       n = V4L2_CID_PRIVATE_BASE;
291     }
292     GST_DEBUG_OBJECT (e, "checking control %08x", n);
293
294     control.id = n | next;
295     if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
296       if (next) {
297         if (n > 0) {
298           GST_DEBUG_OBJECT (e, "controls finished");
299           break;
300         } else {
301           GST_DEBUG_OBJECT (e, "V4L2_CTRL_FLAG_NEXT_CTRL not supported.");
302           next = 0;
303           n = V4L2_CID_BASE;
304           goto retry;
305         }
306       }
307       if (errno == EINVAL || errno == ENOTTY || errno == EIO || errno == ENOENT) {
308         if (n < V4L2_CID_PRIVATE_BASE) {
309           GST_DEBUG_OBJECT (e, "skipping control %08x", n);
310           /* continue so that we also check private controls */
311           n = V4L2_CID_PRIVATE_BASE - 1;
312           continue;
313         } else {
314           GST_DEBUG_OBJECT (e, "controls finished");
315           break;
316         }
317       } else {
318         GST_WARNING_OBJECT (e, "Failed querying control %d on device '%s'. "
319             "(%d - %s)", n, v4l2object->videodev, errno, strerror (errno));
320         continue;
321       }
322     }
323     /* bogus driver might mess with id in unexpected ways (e.g. set to 0), so
324      * make sure to simply try all if V4L2_CTRL_FLAG_NEXT_CTRL not supported */
325     if (next)
326       n = control.id;
327     if (control.flags & V4L2_CTRL_FLAG_DISABLED) {
328       GST_DEBUG_OBJECT (e, "skipping disabled control");
329       continue;
330     }
331
332     if (control.type == V4L2_CTRL_TYPE_CTRL_CLASS) {
333       GST_DEBUG_OBJECT (e, "starting control class '%s'", control.name);
334       continue;
335     }
336
337     switch (control.type) {
338       case V4L2_CTRL_TYPE_INTEGER:
339       case V4L2_CTRL_TYPE_BOOLEAN:
340       case V4L2_CTRL_TYPE_MENU:
341       case V4L2_CTRL_TYPE_INTEGER_MENU:
342       case V4L2_CTRL_TYPE_BITMASK:
343       case V4L2_CTRL_TYPE_BUTTON:{
344         control.name[31] = '\0';
345         gst_v4l2_normalise_control_name ((gchar *) control.name);
346         g_datalist_id_set_data (&v4l2object->controls,
347             g_quark_from_string ((const gchar *) control.name),
348             GINT_TO_POINTER (n));
349         break;
350       }
351       default:
352         GST_DEBUG_OBJECT (e,
353             "Control type for '%s' not suppored for extra controls.",
354             control.name);
355         break;
356     }
357
358     switch (n) {
359       case V4L2_CID_BRIGHTNESS:
360       case V4L2_CID_CONTRAST:
361       case V4L2_CID_SATURATION:
362       case V4L2_CID_HUE:
363       case V4L2_CID_BLACK_LEVEL:
364       case V4L2_CID_AUTO_WHITE_BALANCE:
365       case V4L2_CID_DO_WHITE_BALANCE:
366       case V4L2_CID_RED_BALANCE:
367       case V4L2_CID_BLUE_BALANCE:
368       case V4L2_CID_GAMMA:
369       case V4L2_CID_EXPOSURE:
370       case V4L2_CID_AUTOGAIN:
371       case V4L2_CID_GAIN:
372       case V4L2_CID_SHARPNESS:
373         /* we only handle these for now (why?) */
374         break;
375       case V4L2_CID_HFLIP:
376       case V4L2_CID_VFLIP:
377       case V4L2_CID_PAN_RESET:
378       case V4L2_CID_TILT_RESET:
379         /* not handled here, handled by VideoOrientation interface */
380         control.id++;
381         break;
382       case V4L2_CID_AUDIO_VOLUME:
383       case V4L2_CID_AUDIO_BALANCE:
384       case V4L2_CID_AUDIO_BASS:
385       case V4L2_CID_AUDIO_TREBLE:
386       case V4L2_CID_AUDIO_MUTE:
387       case V4L2_CID_AUDIO_LOUDNESS:
388         /* FIXME: We should implement GstMixer interface instead */
389         /* but let's not be pedantic and make element more useful for now */
390         break;
391       case V4L2_CID_ALPHA_COMPONENT:
392         v4l2object->has_alpha_component = TRUE;
393         break;
394       default:
395         GST_DEBUG_OBJECT (e,
396             "ControlID %s (%x) unhandled, FIXME", control.name, n);
397         control.id++;
398         break;
399     }
400     if (n != control.id)
401       continue;
402
403     GST_DEBUG_OBJECT (e, "Adding ControlID %s (%x)", control.name, n);
404     v4l2channel = g_object_new (GST_TYPE_V4L2_COLOR_BALANCE_CHANNEL, NULL);
405     channel = GST_COLOR_BALANCE_CHANNEL (v4l2channel);
406     channel->label = g_strdup ((const gchar *) control.name);
407     v4l2channel->id = n;
408
409 #if 0
410     /* FIXME: it will be need just when handling private controls
411      *(currently none of base controls are of this type) */
412     if (control.type == V4L2_CTRL_TYPE_MENU) {
413       struct v4l2_querymenu menu, *mptr;
414
415       int i;
416
417       menu.id = n;
418       for (i = 0;; i++) {
419         menu.index = i;
420         if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYMENU, &menu) < 0) {
421           if (errno == EINVAL)
422             break;              /* end of enumeration */
423           else {
424             GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
425                 (_("Failed getting controls attributes on device '%s'."),
426                     v4l2object->videodev),
427                 ("Failed to get %d in menu enumeration for %s. (%d - %s)",
428                     n, v4l2object->videodev, errno, strerror (errno)));
429             return FALSE;
430           }
431         }
432         mptr = g_malloc (sizeof (menu));
433         memcpy (mptr, &menu, sizeof (menu));
434         menus = g_list_append (menus, mptr);
435       }
436     }
437     v4l2object->menus = g_list_append (v4l2object->menus, menus);
438 #endif
439
440     switch (control.type) {
441       case V4L2_CTRL_TYPE_INTEGER:
442         channel->min_value = control.minimum;
443         channel->max_value = control.maximum;
444         break;
445       case V4L2_CTRL_TYPE_BOOLEAN:
446         channel->min_value = FALSE;
447         channel->max_value = TRUE;
448         break;
449       default:
450         /* FIXME we should find out how to handle V4L2_CTRL_TYPE_BUTTON.
451            BUTTON controls like V4L2_CID_DO_WHITE_BALANCE can just be set (1) or
452            unset (0), but can't be queried */
453         GST_DEBUG_OBJECT (e,
454             "Control with non supported type %s (%x), type=%d",
455             control.name, n, control.type);
456         channel->min_value = channel->max_value = 0;
457         break;
458     }
459
460     v4l2object->colors =
461         g_list_prepend (v4l2object->colors, (gpointer) channel);
462   }
463   v4l2object->colors = g_list_reverse (v4l2object->colors);
464
465   GST_DEBUG_OBJECT (e, "done");
466   return TRUE;
467 }
468
469
470 static void
471 gst_v4l2_empty_lists (GstV4l2Object * v4l2object)
472 {
473   GST_DEBUG_OBJECT (v4l2object->element, "deleting enumerations");
474
475   g_list_foreach (v4l2object->channels, (GFunc) g_object_unref, NULL);
476   g_list_free (v4l2object->channels);
477   v4l2object->channels = NULL;
478
479   g_list_foreach (v4l2object->norms, (GFunc) g_object_unref, NULL);
480   g_list_free (v4l2object->norms);
481   v4l2object->norms = NULL;
482
483   g_list_foreach (v4l2object->colors, (GFunc) g_object_unref, NULL);
484   g_list_free (v4l2object->colors);
485   v4l2object->colors = NULL;
486
487   g_datalist_clear (&v4l2object->controls);
488 }
489
490 static void
491 gst_v4l2_adjust_buf_type (GstV4l2Object * v4l2object)
492 {
493   /* when calling gst_v4l2_object_new the user decides the initial type
494    * so adjust it if multi-planar is supported
495    * the driver should make it exclusive. So the driver should
496    * not support both MPLANE and non-PLANE.
497    * Because even when using MPLANE it still possibles to use it
498    * in a contiguous manner. In this case the first v4l2 plane
499    * contains all the gst planes.
500    */
501 #define CHECK_CAPS (V4L2_CAP_VIDEO_OUTPUT_MPLANE | V4L2_CAP_VIDEO_M2M_MPLANE)
502   switch (v4l2object->type) {
503     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
504       if (v4l2object->vcap.capabilities & CHECK_CAPS) {
505         GST_DEBUG ("adjust type to multi-planar output");
506         v4l2object->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
507       }
508       break;
509     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
510       if (v4l2object->vcap.capabilities & CHECK_CAPS) {
511         GST_DEBUG ("adjust type to multi-planar capture");
512         v4l2object->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
513       }
514       break;
515     default:
516       break;
517   }
518 #undef CHECK_CAPS
519 }
520
521 /******************************************************
522  * gst_v4l2_open():
523  *   open the video device (v4l2object->videodev)
524  * return value: TRUE on success, FALSE on error
525  ******************************************************/
526 gboolean
527 gst_v4l2_open (GstV4l2Object * v4l2object)
528 {
529   struct stat st;
530   int libv4l2_fd;
531 #ifdef GST_EXT_V4L2SRC_MODIFIED
532   int error_type = V4L2_OPEN_ERROR_STAT_FAILED;
533   int device_index = 0;
534   glob_t glob_buf;
535
536   memset(&glob_buf, 0x0, sizeof(glob_t));
537 #endif /* GST_EXT_V4L2SRC_MODIFIED */
538
539   GST_DEBUG_OBJECT (v4l2object->element, "Trying to open device %s",
540       v4l2object->videodev);
541
542   GST_V4L2_CHECK_NOT_OPEN (v4l2object);
543   GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
544
545   /* be sure we have a device */
546   if (!v4l2object->videodev)
547     v4l2object->videodev = g_strdup ("/dev/video");
548
549 #ifdef GST_EXT_V4L2SRC_MODIFIED
550 CHECK_AGAIN:
551 #endif /* GST_EXT_V4L2SRC_MODIFIED */
552   /* check if it is a device */
553 #ifdef GST_EXT_V4L2SRC_MODIFIED
554   if (stat (v4l2object->videodev, &st) == -1) {
555     error_type = V4L2_OPEN_ERROR_STAT_FAILED;
556     goto pre_error_check;
557   }
558 #else /* GST_EXT_V4L2SRC_MODIFIED */
559   if (stat (v4l2object->videodev, &st) == -1)
560     goto stat_failed;
561 #endif /* GST_EXT_V4L2SRC_MODIFIED */
562
563 #ifdef GST_EXT_V4L2SRC_MODIFIED
564   if (!S_ISCHR (st.st_mode)) {
565     error_type = V4L2_OPEN_ERROR_NO_DEVICE;
566     goto pre_error_check;
567   }
568 #else /* GST_EXT_V4L2SRC_MODIFIED */
569   if (!S_ISCHR (st.st_mode))
570     goto no_device;
571 #endif /* GST_EXT_V4L2SRC_MODIFIED */
572
573   /* open the device */
574   v4l2object->video_fd =
575       open (v4l2object->videodev, O_RDWR /* | O_NONBLOCK */ );
576
577 #ifdef GST_EXT_V4L2SRC_MODIFIED
578   if (!GST_V4L2_IS_OPEN (v4l2object)) {
579     error_type = V4L2_OPEN_ERROR_NOT_OPEN;
580     goto pre_error_check;
581   }
582 #else /* GST_EXT_V4L2SRC_MODIFIED */
583   if (!GST_V4L2_IS_OPEN (v4l2object))
584     goto not_open;
585 #endif /* GST_EXT_V4L2SRC_MODIFIED */
586
587   libv4l2_fd = v4l2_fd_open (v4l2object->video_fd,
588       V4L2_ENABLE_ENUM_FMT_EMULATION);
589   /* Note the v4l2_xxx functions are designed so that if they get passed an
590      unknown fd, the will behave exactly as their regular xxx counterparts, so
591      if v4l2_fd_open fails, we continue as normal (missing the libv4l2 custom
592      cam format to normal formats conversion). Chances are big we will still
593      fail then though, as normally v4l2_fd_open only fails if the device is not
594      a v4l2 device. */
595   if (libv4l2_fd != -1)
596     v4l2object->video_fd = libv4l2_fd;
597
598   /* get capabilities, error will be posted */
599 #ifdef GST_EXT_V4L2SRC_MODIFIED
600   if (!gst_v4l2_get_capabilities (v4l2object)) {
601     error_type = V4L2_OPEN_ERROR;
602     goto pre_error_check;
603   }
604 #else /* GST_EXT_V4L2SRC_MODIFIED */
605   if (!gst_v4l2_get_capabilities (v4l2object))
606     goto error;
607 #endif /* GST_EXT_V4L2SRC_MODIFIED */
608
609   /* do we need to be a capture device? */
610 #ifdef GST_EXT_V4L2SRC_MODIFIED
611   GST_INFO_OBJECT(v4l2object->element, "capabilities 0x%x", v4l2object->vcap.capabilities);
612   if (GST_IS_V4L2SRC (v4l2object->element) &&
613       (!(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) ||
614        (v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE)))) {
615     error_type = V4L2_OPEN_ERROR_NOT_CAPTURE;
616     goto pre_error_check;
617   }
618 #else /* GST_EXT_V4L2SRC_MODIFIED */
619   if (GST_IS_V4L2SRC (v4l2object->element) &&
620       !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_CAPTURE |
621               V4L2_CAP_VIDEO_CAPTURE_MPLANE)))
622     goto not_capture;
623 #endif /* GST_EXT_V4L2SRC_MODIFIED */
624
625 #ifdef GST_EXT_V4L2SRC_MODIFIED
626   if (GST_IS_V4L2SINK (v4l2object->element) &&
627       !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_OUTPUT |
628               V4L2_CAP_VIDEO_OUTPUT_MPLANE))) {
629     error_type = V4L2_OPEN_ERROR_NOT_OUTPUT;
630     goto pre_error_check;
631   }
632 #else /* GST_EXT_V4L2SRC_MODIFIED */
633   if (GST_IS_V4L2SINK (v4l2object->element) &&
634       !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_OUTPUT |
635               V4L2_CAP_VIDEO_OUTPUT_MPLANE)))
636     goto not_output;
637 #endif /* GST_EXT_V4L2SRC_MODIFIED */
638
639   if (GST_IS_V4L2_VIDEO_DEC (v4l2object->element) &&
640       /* Today's M2M device only expose M2M */
641       !((v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_M2M |
642                   V4L2_CAP_VIDEO_M2M_MPLANE)) ||
643           /* But legacy driver may expose both CAPTURE and OUTPUT */
644           ((v4l2object->vcap.capabilities &
645                   (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) &&
646               (v4l2object->vcap.capabilities &
647                   (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE)))))
648     goto not_m2m;
649
650   gst_v4l2_adjust_buf_type (v4l2object);
651
652   /* create enumerations, posts errors. */
653 #ifdef GST_EXT_V4L2SRC_MODIFIED
654   if (!gst_v4l2_fill_lists (v4l2object)) {
655     error_type = V4L2_OPEN_ERROR;
656     goto pre_error_check;
657   }
658 #else /* GST_EXT_V4L2SRC_MODIFIED */
659   if (!gst_v4l2_fill_lists (v4l2object))
660     goto error;
661 #endif /* GST_EXT_V4L2SRC_MODIFIED */
662
663   GST_INFO_OBJECT (v4l2object->element,
664       "Opened device '%s' (%s) successfully",
665       v4l2object->vcap.card, v4l2object->videodev);
666
667   if (v4l2object->extra_controls)
668     gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
669
670 #ifdef GST_EXT_V4L2SRC_MODIFIED
671   globfree(&glob_buf);
672 #endif /* GST_EXT_V4L2SRC_MODIFIED */
673
674   /* UVC devices are never interlaced, and doing VIDIOC_TRY_FMT on them
675    * causes expensive and slow USB IO, so don't probe them for interlaced
676    */
677   if (!strcmp ((char *) v4l2object->vcap.driver, "uvcusb")) {
678     v4l2object->never_interlaced = TRUE;
679   }
680
681   return TRUE;
682
683 #ifdef GST_EXT_V4L2SRC_MODIFIED
684 pre_error_check:
685   {
686     if (GST_IS_V4L2SRC(v4l2object->element) && glob_buf.gl_pathc == 0) {
687       if (glob("/dev/video*", 0, 0, &glob_buf) != 0) {
688         GST_WARNING_OBJECT(v4l2object->element, "glob failed");
689       }
690     }
691
692     if (glob_buf.gl_pathc > 0 && device_index < glob_buf.gl_pathc) {
693       if (v4l2object->videodev) {
694         g_free(v4l2object->videodev);
695       }
696       v4l2object->videodev = g_strdup(glob_buf.gl_pathv[device_index]);
697       if (v4l2object->videodev) {
698         device_index++;
699         GST_INFO_OBJECT(v4l2object->element, "check device [%s]",
700                         v4l2object->videodev);
701
702         if (GST_V4L2_IS_OPEN (v4l2object)) {
703           /* close device */
704           v4l2_close (v4l2object->video_fd);
705           v4l2object->video_fd = -1;
706         }
707         /* empty lists */
708         gst_v4l2_empty_lists (v4l2object);
709
710         goto CHECK_AGAIN;
711       } else {
712         GST_WARNING_OBJECT(v4l2object->element, "g_strdup failed [%s]",
713                            glob_buf.gl_pathv[device_index]);
714       }
715     }
716
717     GST_WARNING_OBJECT(v4l2object->element, "error type : %d", error_type);
718
719     switch (error_type) {
720     case V4L2_OPEN_ERROR_STAT_FAILED:
721       goto stat_failed;
722     case V4L2_OPEN_ERROR_NO_DEVICE:
723       goto no_device;
724     case V4L2_OPEN_ERROR_NOT_OPEN:
725       goto not_open;
726     case V4L2_OPEN_ERROR_NOT_CAPTURE:
727       goto not_capture;
728     case V4L2_OPEN_ERROR_NOT_OUTPUT:
729       goto not_output;
730     default:
731       goto error;
732     }
733   }
734 #endif /* GST_EXT_V4L2SRC_MODIFIED */
735
736   /* ERRORS */
737 stat_failed:
738   {
739     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
740         (_("Cannot identify device '%s'."), v4l2object->videodev),
741         GST_ERROR_SYSTEM);
742     goto error;
743   }
744 no_device:
745   {
746     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
747         (_("This isn't a device '%s'."), v4l2object->videodev),
748         GST_ERROR_SYSTEM);
749     goto error;
750   }
751 not_open:
752   {
753     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
754         (_("Could not open device '%s' for reading and writing."),
755             v4l2object->videodev), GST_ERROR_SYSTEM);
756     goto error;
757   }
758 not_capture:
759   {
760     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
761         (_("Device '%s' is not a capture device."),
762             v4l2object->videodev),
763         ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
764     goto error;
765   }
766 not_output:
767   {
768     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
769         (_("Device '%s' is not a output device."),
770             v4l2object->videodev),
771         ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
772     goto error;
773   }
774 not_m2m:
775   {
776     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
777         (_("Device '%s' is not a M2M device."),
778             v4l2object->videodev),
779         ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
780     goto error;
781   }
782 error:
783   {
784     if (GST_V4L2_IS_OPEN (v4l2object)) {
785       /* close device */
786       v4l2_close (v4l2object->video_fd);
787       v4l2object->video_fd = -1;
788     }
789     /* empty lists */
790     gst_v4l2_empty_lists (v4l2object);
791
792 #ifdef GST_EXT_V4L2SRC_MODIFIED
793     globfree(&glob_buf);
794 #endif /* GST_EXT_V4L2SRC_MODIFIED */
795
796     return FALSE;
797   }
798 }
799
800 gboolean
801 gst_v4l2_dup (GstV4l2Object * v4l2object, GstV4l2Object * other)
802 {
803   GST_DEBUG_OBJECT (v4l2object->element, "Trying to dup device %s",
804       other->videodev);
805
806   GST_V4L2_CHECK_OPEN (other);
807   GST_V4L2_CHECK_NOT_OPEN (v4l2object);
808   GST_V4L2_CHECK_NOT_ACTIVE (other);
809   GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
810
811   v4l2object->vcap = other->vcap;
812   gst_v4l2_adjust_buf_type (v4l2object);
813
814   v4l2object->video_fd = v4l2_dup (other->video_fd);
815   if (!GST_V4L2_IS_OPEN (v4l2object))
816     goto not_open;
817
818   g_free (v4l2object->videodev);
819   v4l2object->videodev = g_strdup (other->videodev);
820
821   GST_INFO_OBJECT (v4l2object->element,
822       "Cloned device '%s' (%s) successfully",
823       v4l2object->vcap.card, v4l2object->videodev);
824
825   v4l2object->never_interlaced = other->never_interlaced;
826
827   return TRUE;
828
829 not_open:
830   {
831     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
832         (_("Could not dup device '%s' for reading and writing."),
833             v4l2object->videodev), GST_ERROR_SYSTEM);
834
835     return FALSE;
836   }
837 }
838
839
840 /******************************************************
841  * gst_v4l2_close():
842  *   close the video device (v4l2object->video_fd)
843  * return value: TRUE on success, FALSE on error
844  ******************************************************/
845 gboolean
846 gst_v4l2_close (GstV4l2Object * v4l2object)
847 {
848   GST_DEBUG_OBJECT (v4l2object->element, "Trying to close %s",
849       v4l2object->videodev);
850
851   GST_V4L2_CHECK_OPEN (v4l2object);
852   GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
853
854   /* close device */
855   v4l2_close (v4l2object->video_fd);
856   v4l2object->video_fd = -1;
857
858   /* empty lists */
859   gst_v4l2_empty_lists (v4l2object);
860
861   return TRUE;
862 }
863
864
865 /******************************************************
866  * gst_v4l2_get_norm()
867  *   Get the norm of the current device
868  * return value: TRUE on success, FALSE on error
869  ******************************************************/
870 gboolean
871 gst_v4l2_get_norm (GstV4l2Object * v4l2object, v4l2_std_id * norm)
872 {
873   GST_DEBUG_OBJECT (v4l2object->element, "getting norm");
874
875   if (!GST_V4L2_IS_OPEN (v4l2object))
876     return FALSE;
877
878   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_STD, norm) < 0)
879     goto std_failed;
880
881   return TRUE;
882
883   /* ERRORS */
884 std_failed:
885   {
886     GST_DEBUG ("Failed to get the current norm for device %s",
887         v4l2object->videodev);
888     return FALSE;
889   }
890 }
891
892
893 /******************************************************
894  * gst_v4l2_set_norm()
895  *   Set the norm of the current device
896  * return value: TRUE on success, FALSE on error
897  ******************************************************/
898 gboolean
899 gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm)
900 {
901   GST_DEBUG_OBJECT (v4l2object->element, "trying to set norm to "
902       "%" G_GINT64_MODIFIER "x", (guint64) norm);
903
904   if (!GST_V4L2_IS_OPEN (v4l2object))
905     return FALSE;
906
907   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_STD, &norm) < 0)
908     goto std_failed;
909
910   return TRUE;
911
912   /* ERRORS */
913 std_failed:
914   {
915     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
916         (_("Failed to set norm for device '%s'."),
917             v4l2object->videodev), GST_ERROR_SYSTEM);
918     return FALSE;
919   }
920 }
921
922 /******************************************************
923  * gst_v4l2_get_frequency():
924  *   get the current frequency
925  * return value: TRUE on success, FALSE on error
926  ******************************************************/
927 gboolean
928 gst_v4l2_get_frequency (GstV4l2Object * v4l2object,
929     gint tunernum, gulong * frequency)
930 {
931   struct v4l2_frequency freq = { 0, };
932
933   GstTunerChannel *channel;
934
935   GST_DEBUG_OBJECT (v4l2object->element, "getting current tuner frequency");
936
937   if (!GST_V4L2_IS_OPEN (v4l2object))
938     return FALSE;
939
940   channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
941
942   freq.tuner = tunernum;
943   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0)
944     goto freq_failed;
945
946   *frequency = freq.frequency * channel->freq_multiplicator;
947
948   return TRUE;
949
950   /* ERRORS */
951 freq_failed:
952   {
953     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
954         (_("Failed to get current tuner frequency for device '%s'."),
955             v4l2object->videodev), GST_ERROR_SYSTEM);
956     return FALSE;
957   }
958 }
959
960
961 /******************************************************
962  * gst_v4l2_set_frequency():
963  *   set frequency
964  * return value: TRUE on success, FALSE on error
965  ******************************************************/
966 gboolean
967 gst_v4l2_set_frequency (GstV4l2Object * v4l2object,
968     gint tunernum, gulong frequency)
969 {
970   struct v4l2_frequency freq = { 0, };
971
972   GstTunerChannel *channel;
973
974   GST_DEBUG_OBJECT (v4l2object->element,
975       "setting current tuner frequency to %lu", frequency);
976
977   if (!GST_V4L2_IS_OPEN (v4l2object))
978     return FALSE;
979
980   channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
981
982   freq.tuner = tunernum;
983   /* fill in type - ignore error */
984   v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq);
985   freq.frequency = frequency / channel->freq_multiplicator;
986
987   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0)
988     goto freq_failed;
989
990   return TRUE;
991
992   /* ERRORS */
993 freq_failed:
994   {
995     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
996         (_("Failed to set current tuner frequency for device '%s' to %lu Hz."),
997             v4l2object->videodev, frequency), GST_ERROR_SYSTEM);
998     return FALSE;
999   }
1000 }
1001
1002 /******************************************************
1003  * gst_v4l2_signal_strength():
1004  *   get the strength of the signal on the current input
1005  * return value: TRUE on success, FALSE on error
1006  ******************************************************/
1007 gboolean
1008 gst_v4l2_signal_strength (GstV4l2Object * v4l2object,
1009     gint tunernum, gulong * signal_strength)
1010 {
1011   struct v4l2_tuner tuner = { 0, };
1012
1013   GST_DEBUG_OBJECT (v4l2object->element, "trying to get signal strength");
1014
1015   if (!GST_V4L2_IS_OPEN (v4l2object))
1016     return FALSE;
1017
1018   tuner.index = tunernum;
1019   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &tuner) < 0)
1020     goto tuner_failed;
1021
1022   *signal_strength = tuner.signal;
1023
1024   return TRUE;
1025
1026   /* ERRORS */
1027 tuner_failed:
1028   {
1029     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1030         (_("Failed to get signal strength for device '%s'."),
1031             v4l2object->videodev), GST_ERROR_SYSTEM);
1032     return FALSE;
1033   }
1034 }
1035
1036 /******************************************************
1037  * gst_v4l2_get_attribute():
1038  *   try to get the value of one specific attribute
1039  * return value: TRUE on success, FALSE on error
1040  ******************************************************/
1041 gboolean
1042 gst_v4l2_get_attribute (GstV4l2Object * v4l2object,
1043     int attribute_num, int *value)
1044 {
1045   struct v4l2_control control = { 0, };
1046
1047   GST_DEBUG_OBJECT (v4l2object->element, "getting value of attribute %d",
1048       attribute_num);
1049
1050   if (!GST_V4L2_IS_OPEN (v4l2object))
1051     return FALSE;
1052
1053   control.id = attribute_num;
1054
1055   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0)
1056     goto ctrl_failed;
1057
1058   *value = control.value;
1059
1060   return TRUE;
1061
1062   /* ERRORS */
1063 ctrl_failed:
1064   {
1065     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1066         (_("Failed to get value for control %d on device '%s'."),
1067             attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
1068     return FALSE;
1069   }
1070 }
1071
1072
1073 /******************************************************
1074  * gst_v4l2_set_attribute():
1075  *   try to set the value of one specific attribute
1076  * return value: TRUE on success, FALSE on error
1077  ******************************************************/
1078 gboolean
1079 gst_v4l2_set_attribute (GstV4l2Object * v4l2object,
1080     int attribute_num, const int value)
1081 {
1082   struct v4l2_control control = { 0, };
1083
1084   GST_DEBUG_OBJECT (v4l2object->element, "setting value of attribute %d to %d",
1085       attribute_num, value);
1086
1087   if (!GST_V4L2_IS_OPEN (v4l2object))
1088     return FALSE;
1089
1090   control.id = attribute_num;
1091   control.value = value;
1092   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0)
1093     goto ctrl_failed;
1094
1095   return TRUE;
1096
1097   /* ERRORS */
1098 ctrl_failed:
1099   {
1100     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1101         (_("Failed to set value %d for control %d on device '%s'."),
1102             value, attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
1103     return FALSE;
1104   }
1105 }
1106
1107 static gboolean
1108 set_control (GQuark field_id, const GValue * value, gpointer user_data)
1109 {
1110   GstV4l2Object *v4l2object = user_data;
1111   GQuark normalised_field_id;
1112   gpointer *d;
1113
1114   /* 32 bytes is the maximum size for a control name according to v4l2 */
1115   gchar name[32];
1116
1117   /* Backwards compatibility: in the past GStreamer would normalise strings in
1118      a subtly different way to v4l2-ctl.  e.g. the kernel's "Focus (absolute)"
1119      would become "focus__absolute_" whereas now it becomes "focus_absolute".
1120      Please remove the following in GStreamer 1.5 for 1.6 */
1121   strncpy (name, g_quark_to_string (field_id), sizeof (name));
1122   name[31] = '\0';
1123   gst_v4l2_normalise_control_name (name);
1124   normalised_field_id = g_quark_from_string (name);
1125   if (normalised_field_id != field_id)
1126     g_warning ("In GStreamer 1.4 the way V4L2 control names were normalised "
1127         "changed.  Instead of setting \"%s\" please use \"%s\".  The former is "
1128         "deprecated and will be removed in a future version of GStreamer",
1129         g_quark_to_string (field_id), name);
1130   field_id = normalised_field_id;
1131
1132   d = g_datalist_id_get_data (&v4l2object->controls, field_id);
1133   if (!d) {
1134     GST_WARNING_OBJECT (v4l2object,
1135         "Control '%s' does not exist or has an unsupported type.",
1136         g_quark_to_string (field_id));
1137     return TRUE;
1138   }
1139   if (!G_VALUE_HOLDS (value, G_TYPE_INT)) {
1140     GST_WARNING_OBJECT (v4l2object,
1141         "'int' value expected for control '%s'.", g_quark_to_string (field_id));
1142     return TRUE;
1143   }
1144   gst_v4l2_set_attribute (v4l2object, GPOINTER_TO_INT (d),
1145       g_value_get_int (value));
1146   return TRUE;
1147 }
1148
1149 gboolean
1150 gst_v4l2_set_controls (GstV4l2Object * v4l2object, GstStructure * controls)
1151 {
1152   return gst_structure_foreach (controls, set_control, v4l2object);
1153 }
1154
1155 gboolean
1156 gst_v4l2_get_input (GstV4l2Object * v4l2object, gint * input)
1157 {
1158   gint n;
1159
1160   GST_DEBUG_OBJECT (v4l2object->element, "trying to get input");
1161
1162   if (!GST_V4L2_IS_OPEN (v4l2object))
1163     return FALSE;
1164
1165   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_INPUT, &n) < 0)
1166     goto input_failed;
1167
1168   *input = n;
1169
1170   GST_DEBUG_OBJECT (v4l2object->element, "input: %d", n);
1171
1172   return TRUE;
1173
1174   /* ERRORS */
1175 input_failed:
1176   if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1177     /* only give a warning message if driver actually claims to have tuner
1178      * support
1179      */
1180     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1181         (_("Failed to get current input on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1182   }
1183   return FALSE;
1184 }
1185
1186 gboolean
1187 gst_v4l2_set_input (GstV4l2Object * v4l2object, gint input)
1188 {
1189   GST_DEBUG_OBJECT (v4l2object->element, "trying to set input to %d", input);
1190
1191   if (!GST_V4L2_IS_OPEN (v4l2object))
1192     return FALSE;
1193
1194   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_INPUT, &input) < 0)
1195     goto input_failed;
1196
1197   return TRUE;
1198
1199   /* ERRORS */
1200 input_failed:
1201   if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1202     /* only give a warning message if driver actually claims to have tuner
1203      * support
1204      */
1205     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1206         (_("Failed to set input %d on device %s."),
1207             input, v4l2object->videodev), GST_ERROR_SYSTEM);
1208   }
1209   return FALSE;
1210 }
1211
1212 gboolean
1213 gst_v4l2_get_output (GstV4l2Object * v4l2object, gint * output)
1214 {
1215   gint n;
1216
1217   GST_DEBUG_OBJECT (v4l2object->element, "trying to get output");
1218
1219   if (!GST_V4L2_IS_OPEN (v4l2object))
1220     return FALSE;
1221
1222   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_OUTPUT, &n) < 0)
1223     goto output_failed;
1224
1225   *output = n;
1226
1227   GST_DEBUG_OBJECT (v4l2object->element, "output: %d", n);
1228
1229   return TRUE;
1230
1231   /* ERRORS */
1232 output_failed:
1233   if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1234     /* only give a warning message if driver actually claims to have tuner
1235      * support
1236      */
1237     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1238         (_("Failed to get current output on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1239   }
1240   return FALSE;
1241 }
1242
1243 gboolean
1244 gst_v4l2_set_output (GstV4l2Object * v4l2object, gint output)
1245 {
1246   GST_DEBUG_OBJECT (v4l2object->element, "trying to set output to %d", output);
1247
1248   if (!GST_V4L2_IS_OPEN (v4l2object))
1249     return FALSE;
1250
1251   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_OUTPUT, &output) < 0)
1252     goto output_failed;
1253
1254   return TRUE;
1255
1256   /* ERRORS */
1257 output_failed:
1258   if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1259     /* only give a warning message if driver actually claims to have tuner
1260      * support
1261      */
1262     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1263         (_("Failed to set output %d on device %s."),
1264             output, v4l2object->videodev), GST_ERROR_SYSTEM);
1265   }
1266   return FALSE;
1267 }