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