apply tizen build option rule
[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 TIZEN_FEATURE_V4L2SRC_MODIFICATION
37 #include <glob.h>
38 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
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 TIZEN_FEATURE_V4L2SRC_MODIFICATION
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 /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
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 TIZEN_FEATURE_V4L2SRC_MODIFICATION
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
538   if (!v4l2object) {
539     GST_ERROR("v4l2object is NULL");
540     return FALSE;
541   }
542 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
543
544   GST_DEBUG_OBJECT (v4l2object->element, "Trying to open device %s",
545       v4l2object->videodev);
546
547   GST_V4L2_CHECK_NOT_OPEN (v4l2object);
548   GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
549
550   /* be sure we have a device */
551   if (!v4l2object->videodev)
552     v4l2object->videodev = g_strdup ("/dev/video");
553
554 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
555   if (!v4l2object->videodev) {
556     GST_ERROR_OBJECT(v4l2object->element, "videodev is NULL");
557     return FALSE;
558   }
559
560 CHECK_AGAIN:
561 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
562   /* check if it is a device */
563 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
564   if (stat (v4l2object->videodev, &st) == -1) {
565     error_type = V4L2_OPEN_ERROR_STAT_FAILED;
566     goto pre_error_check;
567   }
568 #else /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
569   if (stat (v4l2object->videodev, &st) == -1)
570     goto stat_failed;
571 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
572
573 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
574   if (!S_ISCHR (st.st_mode)) {
575     error_type = V4L2_OPEN_ERROR_NO_DEVICE;
576     goto pre_error_check;
577   }
578 #else /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
579   if (!S_ISCHR (st.st_mode))
580     goto no_device;
581 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
582
583   /* open the device */
584   v4l2object->video_fd =
585       open (v4l2object->videodev, O_RDWR /* | O_NONBLOCK */ );
586
587 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
588   if (!GST_V4L2_IS_OPEN (v4l2object)) {
589     error_type = V4L2_OPEN_ERROR_NOT_OPEN;
590     goto pre_error_check;
591   }
592 #else /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
593   if (!GST_V4L2_IS_OPEN (v4l2object))
594     goto not_open;
595 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
596
597   libv4l2_fd = v4l2_fd_open (v4l2object->video_fd,
598       V4L2_ENABLE_ENUM_FMT_EMULATION);
599   /* Note the v4l2_xxx functions are designed so that if they get passed an
600      unknown fd, the will behave exactly as their regular xxx counterparts, so
601      if v4l2_fd_open fails, we continue as normal (missing the libv4l2 custom
602      cam format to normal formats conversion). Chances are big we will still
603      fail then though, as normally v4l2_fd_open only fails if the device is not
604      a v4l2 device. */
605   if (libv4l2_fd != -1)
606     v4l2object->video_fd = libv4l2_fd;
607
608   /* get capabilities, error will be posted */
609 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
610   if (!gst_v4l2_get_capabilities (v4l2object)) {
611     error_type = V4L2_OPEN_ERROR;
612     goto pre_error_check;
613   }
614 #else /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
615   if (!gst_v4l2_get_capabilities (v4l2object))
616     goto error;
617 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
618
619   /* do we need to be a capture device? */
620 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
621   GST_INFO_OBJECT(v4l2object->element, "capabilities 0x%x", v4l2object->vcap.capabilities);
622   if (GST_IS_V4L2SRC (v4l2object->element) &&
623       (!(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) ||
624        (v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE)))) {
625     error_type = V4L2_OPEN_ERROR_NOT_CAPTURE;
626     goto pre_error_check;
627   }
628 #else /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
629   if (GST_IS_V4L2SRC (v4l2object->element) &&
630       !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_CAPTURE |
631               V4L2_CAP_VIDEO_CAPTURE_MPLANE)))
632     goto not_capture;
633 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
634
635 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
636   if (GST_IS_V4L2SINK (v4l2object->element) &&
637       !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_OUTPUT |
638               V4L2_CAP_VIDEO_OUTPUT_MPLANE))) {
639     error_type = V4L2_OPEN_ERROR_NOT_OUTPUT;
640     goto pre_error_check;
641   }
642 #else /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
643   if (GST_IS_V4L2SINK (v4l2object->element) &&
644       !(v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_OUTPUT |
645               V4L2_CAP_VIDEO_OUTPUT_MPLANE)))
646     goto not_output;
647 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
648
649   if (GST_IS_V4L2_VIDEO_DEC (v4l2object->element) &&
650       /* Today's M2M device only expose M2M */
651       !((v4l2object->vcap.capabilities & (V4L2_CAP_VIDEO_M2M |
652                   V4L2_CAP_VIDEO_M2M_MPLANE)) ||
653           /* But legacy driver may expose both CAPTURE and OUTPUT */
654           ((v4l2object->vcap.capabilities &
655                   (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) &&
656               (v4l2object->vcap.capabilities &
657                   (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE)))))
658     goto not_m2m;
659
660   gst_v4l2_adjust_buf_type (v4l2object);
661
662   /* create enumerations, posts errors. */
663 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
664   if (!gst_v4l2_fill_lists (v4l2object)) {
665     error_type = V4L2_OPEN_ERROR;
666     goto pre_error_check;
667   }
668 #else /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
669   if (!gst_v4l2_fill_lists (v4l2object))
670     goto error;
671 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
672
673   GST_INFO_OBJECT (v4l2object->element,
674       "Opened device '%s' (%s) successfully",
675       v4l2object->vcap.card, v4l2object->videodev);
676
677   if (v4l2object->extra_controls)
678     gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
679
680 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
681   globfree(&glob_buf);
682 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
683
684   /* UVC devices are never interlaced, and doing VIDIOC_TRY_FMT on them
685    * causes expensive and slow USB IO, so don't probe them for interlaced
686    */
687   if (!strcmp ((char *) v4l2object->vcap.driver, "uvcusb")) {
688     v4l2object->never_interlaced = TRUE;
689   }
690
691   return TRUE;
692
693 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
694 pre_error_check:
695   {
696     if (GST_IS_V4L2SRC(v4l2object->element) && glob_buf.gl_pathc == 0) {
697       if (glob("/dev/video*", 0, 0, &glob_buf) != 0) {
698         GST_WARNING_OBJECT(v4l2object->element, "glob failed");
699       }
700     }
701
702     if (glob_buf.gl_pathc > 0 && device_index < glob_buf.gl_pathc) {
703       if (v4l2object->videodev) {
704         g_free(v4l2object->videodev);
705         v4l2object->videodev = NULL;
706       }
707       v4l2object->videodev = g_strdup(glob_buf.gl_pathv[device_index]);
708       if (v4l2object->videodev) {
709         device_index++;
710         GST_INFO_OBJECT(v4l2object->element, "check device [%s]",
711                         v4l2object->videodev);
712
713         if (GST_V4L2_IS_OPEN (v4l2object)) {
714           /* close device */
715           v4l2_close (v4l2object->video_fd);
716           v4l2object->video_fd = -1;
717         }
718         /* empty lists */
719         gst_v4l2_empty_lists (v4l2object);
720
721         goto CHECK_AGAIN;
722       } else {
723         GST_WARNING_OBJECT(v4l2object->element, "g_strdup failed [%s]",
724                            glob_buf.gl_pathv[device_index]);
725       }
726     }
727
728     GST_WARNING_OBJECT(v4l2object->element, "error type : %d", error_type);
729
730     switch (error_type) {
731     case V4L2_OPEN_ERROR_STAT_FAILED:
732       goto stat_failed;
733     case V4L2_OPEN_ERROR_NO_DEVICE:
734       goto no_device;
735     case V4L2_OPEN_ERROR_NOT_OPEN:
736       goto not_open;
737     case V4L2_OPEN_ERROR_NOT_CAPTURE:
738       goto not_capture;
739     case V4L2_OPEN_ERROR_NOT_OUTPUT:
740       goto not_output;
741     default:
742       goto error;
743     }
744   }
745 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
746
747   /* ERRORS */
748 stat_failed:
749   {
750     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
751         (_("Cannot identify device '%s'."), v4l2object->videodev),
752         GST_ERROR_SYSTEM);
753     goto error;
754   }
755 no_device:
756   {
757     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
758         (_("This isn't a device '%s'."), v4l2object->videodev),
759         GST_ERROR_SYSTEM);
760     goto error;
761   }
762 not_open:
763   {
764     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
765         (_("Could not open device '%s' for reading and writing."),
766             v4l2object->videodev), GST_ERROR_SYSTEM);
767     goto error;
768   }
769 not_capture:
770   {
771     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
772         (_("Device '%s' is not a capture device."),
773             v4l2object->videodev),
774         ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
775     goto error;
776   }
777 not_output:
778   {
779     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
780         (_("Device '%s' is not a output device."),
781             v4l2object->videodev),
782         ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
783     goto error;
784   }
785 not_m2m:
786   {
787     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
788         (_("Device '%s' is not a M2M device."),
789             v4l2object->videodev),
790         ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
791     goto error;
792   }
793 error:
794   {
795     if (GST_V4L2_IS_OPEN (v4l2object)) {
796       /* close device */
797       v4l2_close (v4l2object->video_fd);
798       v4l2object->video_fd = -1;
799     }
800     /* empty lists */
801     gst_v4l2_empty_lists (v4l2object);
802
803 #ifdef TIZEN_FEATURE_V4L2SRC_MODIFICATION
804     globfree(&glob_buf);
805 #endif /* TIZEN_FEATURE_V4L2SRC_MODIFICATION */
806
807     return FALSE;
808   }
809 }
810
811 gboolean
812 gst_v4l2_dup (GstV4l2Object * v4l2object, GstV4l2Object * other)
813 {
814   GST_DEBUG_OBJECT (v4l2object->element, "Trying to dup device %s",
815       other->videodev);
816
817   GST_V4L2_CHECK_OPEN (other);
818   GST_V4L2_CHECK_NOT_OPEN (v4l2object);
819   GST_V4L2_CHECK_NOT_ACTIVE (other);
820   GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
821
822   v4l2object->vcap = other->vcap;
823   gst_v4l2_adjust_buf_type (v4l2object);
824
825   v4l2object->video_fd = v4l2_dup (other->video_fd);
826   if (!GST_V4L2_IS_OPEN (v4l2object))
827     goto not_open;
828
829   g_free (v4l2object->videodev);
830   v4l2object->videodev = g_strdup (other->videodev);
831
832   GST_INFO_OBJECT (v4l2object->element,
833       "Cloned device '%s' (%s) successfully",
834       v4l2object->vcap.card, v4l2object->videodev);
835
836   v4l2object->never_interlaced = other->never_interlaced;
837
838   return TRUE;
839
840 not_open:
841   {
842     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
843         (_("Could not dup device '%s' for reading and writing."),
844             v4l2object->videodev), GST_ERROR_SYSTEM);
845
846     return FALSE;
847   }
848 }
849
850
851 /******************************************************
852  * gst_v4l2_close():
853  *   close the video device (v4l2object->video_fd)
854  * return value: TRUE on success, FALSE on error
855  ******************************************************/
856 gboolean
857 gst_v4l2_close (GstV4l2Object * v4l2object)
858 {
859   GST_DEBUG_OBJECT (v4l2object->element, "Trying to close %s",
860       v4l2object->videodev);
861
862   GST_V4L2_CHECK_OPEN (v4l2object);
863   GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
864
865   /* close device */
866   v4l2_close (v4l2object->video_fd);
867   v4l2object->video_fd = -1;
868
869   /* empty lists */
870   gst_v4l2_empty_lists (v4l2object);
871
872   return TRUE;
873 }
874
875
876 /******************************************************
877  * gst_v4l2_get_norm()
878  *   Get the norm of the current device
879  * return value: TRUE on success, FALSE on error
880  ******************************************************/
881 gboolean
882 gst_v4l2_get_norm (GstV4l2Object * v4l2object, v4l2_std_id * norm)
883 {
884   GST_DEBUG_OBJECT (v4l2object->element, "getting norm");
885
886   if (!GST_V4L2_IS_OPEN (v4l2object))
887     return FALSE;
888
889   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_STD, norm) < 0)
890     goto std_failed;
891
892   return TRUE;
893
894   /* ERRORS */
895 std_failed:
896   {
897     GST_DEBUG ("Failed to get the current norm for device %s",
898         v4l2object->videodev);
899     return FALSE;
900   }
901 }
902
903
904 /******************************************************
905  * gst_v4l2_set_norm()
906  *   Set the norm of the current device
907  * return value: TRUE on success, FALSE on error
908  ******************************************************/
909 gboolean
910 gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm)
911 {
912   GST_DEBUG_OBJECT (v4l2object->element, "trying to set norm to "
913       "%" G_GINT64_MODIFIER "x", (guint64) norm);
914
915   if (!GST_V4L2_IS_OPEN (v4l2object))
916     return FALSE;
917
918   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_STD, &norm) < 0)
919     goto std_failed;
920
921   return TRUE;
922
923   /* ERRORS */
924 std_failed:
925   {
926     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
927         (_("Failed to set norm for device '%s'."),
928             v4l2object->videodev), GST_ERROR_SYSTEM);
929     return FALSE;
930   }
931 }
932
933 /******************************************************
934  * gst_v4l2_get_frequency():
935  *   get the current frequency
936  * return value: TRUE on success, FALSE on error
937  ******************************************************/
938 gboolean
939 gst_v4l2_get_frequency (GstV4l2Object * v4l2object,
940     gint tunernum, gulong * frequency)
941 {
942   struct v4l2_frequency freq = { 0, };
943
944   GstTunerChannel *channel;
945
946   GST_DEBUG_OBJECT (v4l2object->element, "getting current tuner frequency");
947
948   if (!GST_V4L2_IS_OPEN (v4l2object))
949     return FALSE;
950
951   channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
952
953   freq.tuner = tunernum;
954   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0)
955     goto freq_failed;
956
957   *frequency = freq.frequency * channel->freq_multiplicator;
958
959   return TRUE;
960
961   /* ERRORS */
962 freq_failed:
963   {
964     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
965         (_("Failed to get current tuner frequency for device '%s'."),
966             v4l2object->videodev), GST_ERROR_SYSTEM);
967     return FALSE;
968   }
969 }
970
971
972 /******************************************************
973  * gst_v4l2_set_frequency():
974  *   set frequency
975  * return value: TRUE on success, FALSE on error
976  ******************************************************/
977 gboolean
978 gst_v4l2_set_frequency (GstV4l2Object * v4l2object,
979     gint tunernum, gulong frequency)
980 {
981   struct v4l2_frequency freq = { 0, };
982
983   GstTunerChannel *channel;
984
985   GST_DEBUG_OBJECT (v4l2object->element,
986       "setting current tuner frequency to %lu", frequency);
987
988   if (!GST_V4L2_IS_OPEN (v4l2object))
989     return FALSE;
990
991   channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
992
993   freq.tuner = tunernum;
994   /* fill in type - ignore error */
995   (void) v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq);
996   freq.frequency = frequency / channel->freq_multiplicator;
997
998   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0)
999     goto freq_failed;
1000
1001   return TRUE;
1002
1003   /* ERRORS */
1004 freq_failed:
1005   {
1006     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1007         (_("Failed to set current tuner frequency for device '%s' to %lu Hz."),
1008             v4l2object->videodev, frequency), GST_ERROR_SYSTEM);
1009     return FALSE;
1010   }
1011 }
1012
1013 /******************************************************
1014  * gst_v4l2_signal_strength():
1015  *   get the strength of the signal on the current input
1016  * return value: TRUE on success, FALSE on error
1017  ******************************************************/
1018 gboolean
1019 gst_v4l2_signal_strength (GstV4l2Object * v4l2object,
1020     gint tunernum, gulong * signal_strength)
1021 {
1022   struct v4l2_tuner tuner = { 0, };
1023
1024   GST_DEBUG_OBJECT (v4l2object->element, "trying to get signal strength");
1025
1026   if (!GST_V4L2_IS_OPEN (v4l2object))
1027     return FALSE;
1028
1029   tuner.index = tunernum;
1030   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &tuner) < 0)
1031     goto tuner_failed;
1032
1033   *signal_strength = tuner.signal;
1034
1035   return TRUE;
1036
1037   /* ERRORS */
1038 tuner_failed:
1039   {
1040     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1041         (_("Failed to get signal strength for device '%s'."),
1042             v4l2object->videodev), GST_ERROR_SYSTEM);
1043     return FALSE;
1044   }
1045 }
1046
1047 /******************************************************
1048  * gst_v4l2_get_attribute():
1049  *   try to get the value of one specific attribute
1050  * return value: TRUE on success, FALSE on error
1051  ******************************************************/
1052 gboolean
1053 gst_v4l2_get_attribute (GstV4l2Object * v4l2object,
1054     int attribute_num, int *value)
1055 {
1056   struct v4l2_control control = { 0, };
1057
1058   GST_DEBUG_OBJECT (v4l2object->element, "getting value of attribute %d",
1059       attribute_num);
1060
1061   if (!GST_V4L2_IS_OPEN (v4l2object))
1062     return FALSE;
1063
1064   control.id = attribute_num;
1065
1066   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0)
1067     goto ctrl_failed;
1068
1069   *value = control.value;
1070
1071   return TRUE;
1072
1073   /* ERRORS */
1074 ctrl_failed:
1075   {
1076     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1077         (_("Failed to get value for control %d on device '%s'."),
1078             attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
1079     return FALSE;
1080   }
1081 }
1082
1083
1084 /******************************************************
1085  * gst_v4l2_set_attribute():
1086  *   try to set the value of one specific attribute
1087  * return value: TRUE on success, FALSE on error
1088  ******************************************************/
1089 gboolean
1090 gst_v4l2_set_attribute (GstV4l2Object * v4l2object,
1091     int attribute_num, const int value)
1092 {
1093   struct v4l2_control control = { 0, };
1094
1095   GST_DEBUG_OBJECT (v4l2object->element, "setting value of attribute %d to %d",
1096       attribute_num, value);
1097
1098   if (!GST_V4L2_IS_OPEN (v4l2object))
1099     return FALSE;
1100
1101   control.id = attribute_num;
1102   control.value = value;
1103   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0)
1104     goto ctrl_failed;
1105
1106   return TRUE;
1107
1108   /* ERRORS */
1109 ctrl_failed:
1110   {
1111     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1112         (_("Failed to set value %d for control %d on device '%s'."),
1113             value, attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
1114     return FALSE;
1115   }
1116 }
1117
1118 static gboolean
1119 set_control (GQuark field_id, const GValue * value, gpointer user_data)
1120 {
1121   GstV4l2Object *v4l2object = user_data;
1122   GQuark normalised_field_id;
1123   gpointer *d;
1124
1125   /* 32 bytes is the maximum size for a control name according to v4l2 */
1126   gchar name[32];
1127
1128   /* Backwards compatibility: in the past GStreamer would normalise strings in
1129      a subtly different way to v4l2-ctl.  e.g. the kernel's "Focus (absolute)"
1130      would become "focus__absolute_" whereas now it becomes "focus_absolute".
1131      Please remove the following in GStreamer 1.5 for 1.6 */
1132   strncpy (name, g_quark_to_string (field_id), sizeof (name));
1133   name[31] = '\0';
1134   gst_v4l2_normalise_control_name (name);
1135   normalised_field_id = g_quark_from_string (name);
1136   if (normalised_field_id != field_id)
1137     g_warning ("In GStreamer 1.4 the way V4L2 control names were normalised "
1138         "changed.  Instead of setting \"%s\" please use \"%s\".  The former is "
1139         "deprecated and will be removed in a future version of GStreamer",
1140         g_quark_to_string (field_id), name);
1141   field_id = normalised_field_id;
1142
1143   d = g_datalist_id_get_data (&v4l2object->controls, field_id);
1144   if (!d) {
1145     GST_WARNING_OBJECT (v4l2object,
1146         "Control '%s' does not exist or has an unsupported type.",
1147         g_quark_to_string (field_id));
1148     return TRUE;
1149   }
1150   if (!G_VALUE_HOLDS (value, G_TYPE_INT)) {
1151     GST_WARNING_OBJECT (v4l2object,
1152         "'int' value expected for control '%s'.", g_quark_to_string (field_id));
1153     return TRUE;
1154   }
1155   gst_v4l2_set_attribute (v4l2object, GPOINTER_TO_INT (d),
1156       g_value_get_int (value));
1157   return TRUE;
1158 }
1159
1160 gboolean
1161 gst_v4l2_set_controls (GstV4l2Object * v4l2object, GstStructure * controls)
1162 {
1163   return gst_structure_foreach (controls, set_control, v4l2object);
1164 }
1165
1166 gboolean
1167 gst_v4l2_get_input (GstV4l2Object * v4l2object, gint * input)
1168 {
1169   gint n;
1170
1171   GST_DEBUG_OBJECT (v4l2object->element, "trying to get input");
1172
1173   if (!GST_V4L2_IS_OPEN (v4l2object))
1174     return FALSE;
1175
1176   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_INPUT, &n) < 0)
1177     goto input_failed;
1178
1179   *input = n;
1180
1181   GST_DEBUG_OBJECT (v4l2object->element, "input: %d", n);
1182
1183   return TRUE;
1184
1185   /* ERRORS */
1186 input_failed:
1187   if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1188     /* only give a warning message if driver actually claims to have tuner
1189      * support
1190      */
1191     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1192         (_("Failed to get current input on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1193   }
1194   return FALSE;
1195 }
1196
1197 gboolean
1198 gst_v4l2_set_input (GstV4l2Object * v4l2object, gint input)
1199 {
1200   GST_DEBUG_OBJECT (v4l2object->element, "trying to set input to %d", input);
1201
1202   if (!GST_V4L2_IS_OPEN (v4l2object))
1203     return FALSE;
1204
1205   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_INPUT, &input) < 0)
1206     goto input_failed;
1207
1208   return TRUE;
1209
1210   /* ERRORS */
1211 input_failed:
1212   if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1213     /* only give a warning message if driver actually claims to have tuner
1214      * support
1215      */
1216     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1217         (_("Failed to set input %d on device %s."),
1218             input, v4l2object->videodev), GST_ERROR_SYSTEM);
1219   }
1220   return FALSE;
1221 }
1222
1223 gboolean
1224 gst_v4l2_get_output (GstV4l2Object * v4l2object, gint * output)
1225 {
1226   gint n;
1227
1228   GST_DEBUG_OBJECT (v4l2object->element, "trying to get output");
1229
1230   if (!GST_V4L2_IS_OPEN (v4l2object))
1231     return FALSE;
1232
1233   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_OUTPUT, &n) < 0)
1234     goto output_failed;
1235
1236   *output = n;
1237
1238   GST_DEBUG_OBJECT (v4l2object->element, "output: %d", n);
1239
1240   return TRUE;
1241
1242   /* ERRORS */
1243 output_failed:
1244   if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1245     /* only give a warning message if driver actually claims to have tuner
1246      * support
1247      */
1248     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1249         (_("Failed to get current output on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1250   }
1251   return FALSE;
1252 }
1253
1254 gboolean
1255 gst_v4l2_set_output (GstV4l2Object * v4l2object, gint output)
1256 {
1257   GST_DEBUG_OBJECT (v4l2object->element, "trying to set output to %d", output);
1258
1259   if (!GST_V4L2_IS_OPEN (v4l2object))
1260     return FALSE;
1261
1262   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_OUTPUT, &output) < 0)
1263     goto output_failed;
1264
1265   return TRUE;
1266
1267   /* ERRORS */
1268 output_failed:
1269   if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
1270     /* only give a warning message if driver actually claims to have tuner
1271      * support
1272      */
1273     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1274         (_("Failed to set output %d on device %s."),
1275             output, v4l2object->videodev), GST_ERROR_SYSTEM);
1276   }
1277   return FALSE;
1278 }