b99bcc6a51c73da37d8f8554a4f1c867b91ff814
[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_SCAN_DEVICE_NODE
37 #include <glob.h>
38 #endif /* TIZEN_FEATURE_V4L2SRC_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_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_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         /* we only handle these for now (why?) */
378         break;
379       case V4L2_CID_HFLIP:
380       case V4L2_CID_VFLIP:
381       case V4L2_CID_PAN_RESET:
382       case V4L2_CID_TILT_RESET:
383         /* not handled here, handled by VideoOrientation interface */
384         control.id++;
385         break;
386       case V4L2_CID_AUDIO_VOLUME:
387       case V4L2_CID_AUDIO_BALANCE:
388       case V4L2_CID_AUDIO_BASS:
389       case V4L2_CID_AUDIO_TREBLE:
390       case V4L2_CID_AUDIO_MUTE:
391       case V4L2_CID_AUDIO_LOUDNESS:
392         /* FIXME: We should implement GstMixer interface instead */
393         /* but let's not be pedantic and make element more useful for now */
394         break;
395       case V4L2_CID_ALPHA_COMPONENT:
396         v4l2object->has_alpha_component = TRUE;
397         break;
398       default:
399         GST_DEBUG_OBJECT (e,
400             "ControlID %s (%x) unhandled, FIXME", control.name, n);
401         control.id++;
402         break;
403     }
404     if (n != control.id)
405       continue;
406
407     GST_DEBUG_OBJECT (e, "Adding ControlID %s (%x)", control.name, n);
408     v4l2channel = g_object_new (GST_TYPE_V4L2_COLOR_BALANCE_CHANNEL, NULL);
409     channel = GST_COLOR_BALANCE_CHANNEL (v4l2channel);
410     channel->label = g_strdup ((const gchar *) control.name);
411     v4l2channel->id = n;
412
413 #if 0
414     /* FIXME: it will be need just when handling private controls
415      *(currently none of base controls are of this type) */
416     if (control.type == V4L2_CTRL_TYPE_MENU) {
417       struct v4l2_querymenu menu, *mptr;
418
419       int i;
420
421       menu.id = n;
422       for (i = 0;; i++) {
423         menu.index = i;
424         if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_QUERYMENU,
425                 &menu) < 0) {
426           if (errno == EINVAL)
427             break;              /* end of enumeration */
428           else {
429             GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
430                 (_("Failed getting controls attributes on device '%s'."),
431                     v4l2object->videodev),
432                 ("Failed to get %d in menu enumeration for %s. (%d - %s)",
433                     n, v4l2object->videodev, errno, strerror (errno)));
434             return FALSE;
435           }
436         }
437         mptr = g_malloc (sizeof (menu));
438         memcpy (mptr, &menu, sizeof (menu));
439         menus = g_list_append (menus, mptr);
440       }
441     }
442     v4l2object->menus = g_list_append (v4l2object->menus, menus);
443 #endif
444
445     switch (control.type) {
446       case V4L2_CTRL_TYPE_INTEGER:
447         channel->min_value = control.minimum;
448         channel->max_value = control.maximum;
449         break;
450       case V4L2_CTRL_TYPE_BOOLEAN:
451         channel->min_value = FALSE;
452         channel->max_value = TRUE;
453         break;
454       default:
455         /* FIXME we should find out how to handle V4L2_CTRL_TYPE_BUTTON.
456            BUTTON controls like V4L2_CID_DO_WHITE_BALANCE can just be set (1) or
457            unset (0), but can't be queried */
458         GST_DEBUG_OBJECT (e,
459             "Control with non supported type %s (%x), type=%d",
460             control.name, n, control.type);
461         channel->min_value = channel->max_value = 0;
462         break;
463     }
464
465     v4l2object->colors =
466         g_list_prepend (v4l2object->colors, (gpointer) channel);
467   }
468   v4l2object->colors = g_list_reverse (v4l2object->colors);
469
470   GST_DEBUG_OBJECT (e, "done");
471   return TRUE;
472 }
473
474
475 static void
476 gst_v4l2_empty_lists (GstV4l2Object * v4l2object)
477 {
478   GST_DEBUG_OBJECT (v4l2object->dbg_obj, "deleting enumerations");
479
480   g_list_foreach (v4l2object->channels, (GFunc) g_object_unref, NULL);
481   g_list_free (v4l2object->channels);
482   v4l2object->channels = NULL;
483
484   g_list_foreach (v4l2object->norms, (GFunc) g_object_unref, NULL);
485   g_list_free (v4l2object->norms);
486   v4l2object->norms = NULL;
487
488   g_list_foreach (v4l2object->colors, (GFunc) g_object_unref, NULL);
489   g_list_free (v4l2object->colors);
490   v4l2object->colors = NULL;
491
492   g_datalist_clear (&v4l2object->controls);
493 }
494
495 static void
496 gst_v4l2_adjust_buf_type (GstV4l2Object * v4l2object)
497 {
498   /* when calling gst_v4l2_object_new the user decides the initial type
499    * so adjust it if multi-planar is supported
500    * the driver should make it exclusive. So the driver should
501    * not support both MPLANE and non-PLANE.
502    * Because even when using MPLANE it still possibles to use it
503    * in a contiguous manner. In this case the first v4l2 plane
504    * contains all the gst planes.
505    */
506   switch (v4l2object->type) {
507     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
508       if (v4l2object->device_caps &
509           (V4L2_CAP_VIDEO_OUTPUT_MPLANE | V4L2_CAP_VIDEO_M2M_MPLANE)) {
510         GST_DEBUG ("adjust type to multi-planar output");
511         v4l2object->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
512       }
513       break;
514     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
515       if (v4l2object->device_caps &
516           (V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_M2M_MPLANE)) {
517         GST_DEBUG ("adjust type to multi-planar capture");
518         v4l2object->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
519       }
520       break;
521     default:
522       break;
523   }
524 }
525
526 /******************************************************
527  * gst_v4l2_open():
528  *   open the video device (v4l2object->videodev)
529  * return value: TRUE on success, FALSE on error
530  ******************************************************/
531 gboolean
532 gst_v4l2_open (GstV4l2Object * v4l2object)
533 {
534   struct stat st;
535   int libv4l2_fd = -1;
536 #ifdef TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE
537   int error_type = V4L2_OPEN_ERROR_STAT_FAILED;
538   int device_index = 0;
539   glob_t glob_buf;
540
541   memset(&glob_buf, 0x0, sizeof(glob_t));
542
543   if (!v4l2object) {
544     GST_ERROR("v4l2object is NULL");
545     return FALSE;
546   }
547 #endif /* TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE */
548   GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Trying to open device %s",
549       v4l2object->videodev);
550
551   GST_V4L2_CHECK_NOT_OPEN (v4l2object);
552   GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
553
554   /* be sure we have a device */
555   if (!v4l2object->videodev)
556     v4l2object->videodev = g_strdup ("/dev/video");
557
558 #ifdef TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE
559   if (!v4l2object->videodev) {
560     GST_ERROR_OBJECT(v4l2object->element, "videodev is NULL");
561     return FALSE;
562   }
563
564 CHECK_AGAIN:
565 #endif /* TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE */
566   /* check if it is a device */
567 #ifdef TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE
568   if (stat (v4l2object->videodev, &st) == -1) {
569     error_type = V4L2_OPEN_ERROR_STAT_FAILED;
570     goto pre_error_check;
571   }
572 #else /* TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE */
573   if (stat (v4l2object->videodev, &st) == -1)
574     goto stat_failed;
575 #endif /* TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE */
576
577 #ifdef TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE
578   if (!S_ISCHR (st.st_mode)) {
579     error_type = V4L2_OPEN_ERROR_NO_DEVICE;
580     goto pre_error_check;
581   }
582 #else /* TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE */
583   if (!S_ISCHR (st.st_mode))
584     goto no_device;
585 #endif /* TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE */
586
587   /* open the device */
588   v4l2object->video_fd =
589       open (v4l2object->videodev, O_RDWR /* | O_NONBLOCK */ );
590
591 #ifdef TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE
592   if (!GST_V4L2_IS_OPEN (v4l2object)) {
593     error_type = V4L2_OPEN_ERROR_NOT_OPEN;
594     goto pre_error_check;
595   }
596 #else /* TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE */
597   if (!GST_V4L2_IS_OPEN (v4l2object))
598     goto not_open;
599 #endif /* TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE */
600
601 #ifdef HAVE_LIBV4L2
602   if (v4l2object->fd_open)
603     libv4l2_fd = v4l2object->fd_open (v4l2object->video_fd,
604         V4L2_ENABLE_ENUM_FMT_EMULATION);
605 #endif
606
607   /* Note the v4l2_xxx functions are designed so that if they get passed an
608      unknown fd, the will behave exactly as their regular xxx counterparts, so
609      if v4l2_fd_open fails, we continue as normal (missing the libv4l2 custom
610      cam format to normal formats conversion). Chances are big we will still
611      fail then though, as normally v4l2_fd_open only fails if the device is not
612      a v4l2 device. */
613   if (libv4l2_fd != -1)
614     v4l2object->video_fd = libv4l2_fd;
615
616   /* get capabilities, error will be posted */
617 #ifdef TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE
618   if (!gst_v4l2_get_capabilities (v4l2object)) {
619     error_type = V4L2_OPEN_ERROR;
620     goto pre_error_check;
621   }
622 #else /* TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE */
623   if (!gst_v4l2_get_capabilities (v4l2object))
624     goto error;
625 #endif /* TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE */
626
627   /* do we need to be a capture device? */
628 #ifdef TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE
629   GST_INFO_OBJECT(v4l2object->element, "device_caps 0x%x", v4l2object->device_caps);
630   if (GST_IS_V4L2SRC (v4l2object->element) &&
631       (!(v4l2object->device_caps & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) ||
632        (v4l2object->device_caps & (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE)))) {
633     error_type = V4L2_OPEN_ERROR_NOT_CAPTURE;
634     goto pre_error_check;
635   }
636 #else /* TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE */
637   if (GST_IS_V4L2SRC (v4l2object->element) &&
638       !(v4l2object->device_caps & (V4L2_CAP_VIDEO_CAPTURE |
639               V4L2_CAP_VIDEO_CAPTURE_MPLANE)))
640     goto not_capture;
641 #endif /* TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE */
642
643 #ifdef TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE
644   if (GST_IS_V4L2SINK (v4l2object->element) &&
645       !(v4l2object->device_caps & (V4L2_CAP_VIDEO_OUTPUT |
646               V4L2_CAP_VIDEO_OUTPUT_MPLANE))) {
647     error_type = V4L2_OPEN_ERROR_NOT_OUTPUT;
648     goto pre_error_check;
649   }
650 #else /* TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE */
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_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_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_SCAN_DEVICE_NODE */
670   if (!gst_v4l2_fill_lists (v4l2object))
671     goto error;
672 #endif /* TIZEN_FEATURE_V4L2SRC_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_SCAN_DEVICE_NODE
682   globfree(&glob_buf);
683 #endif /* TIZEN_FEATURE_V4L2SRC_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_SCAN_DEVICE_NODE
696 pre_error_check:
697   {
698     if (GST_IS_V4L2SRC(v4l2object->element) && glob_buf.gl_pathc == 0) {
699       if (glob("/dev/video*", 0, 0, &glob_buf) != 0) {
700         GST_WARNING_OBJECT(v4l2object->element, "glob failed");
701       }
702     }
703
704     if (glob_buf.gl_pathc > 0 && device_index < glob_buf.gl_pathc) {
705       if (v4l2object->videodev) {
706         g_free(v4l2object->videodev);
707         v4l2object->videodev = NULL;
708       }
709       v4l2object->videodev = g_strdup(glob_buf.gl_pathv[device_index]);
710       if (v4l2object->videodev) {
711         device_index++;
712         GST_INFO_OBJECT(v4l2object->element, "check device [%s]",
713                         v4l2object->videodev);
714
715         if (GST_V4L2_IS_OPEN (v4l2object)) {
716           /* close device */
717           v4l2_close (v4l2object->video_fd);
718           v4l2object->video_fd = -1;
719         }
720         /* empty lists */
721         gst_v4l2_empty_lists (v4l2object);
722
723         goto CHECK_AGAIN;
724       } else {
725         GST_WARNING_OBJECT(v4l2object->element, "g_strdup failed [%s]",
726                            glob_buf.gl_pathv[device_index]);
727       }
728     }
729
730     GST_WARNING_OBJECT(v4l2object->element, "error type : %d", error_type);
731
732     switch (error_type) {
733     case V4L2_OPEN_ERROR_STAT_FAILED:
734       goto stat_failed;
735     case V4L2_OPEN_ERROR_NO_DEVICE:
736       goto no_device;
737     case V4L2_OPEN_ERROR_NOT_OPEN:
738       goto not_open;
739     case V4L2_OPEN_ERROR_NOT_CAPTURE:
740       goto not_capture;
741     case V4L2_OPEN_ERROR_NOT_OUTPUT:
742       goto not_output;
743     default:
744       goto error;
745     }
746   }
747 #endif /* TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE */
748
749   /* ERRORS */
750 stat_failed:
751   {
752     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
753         (_("Cannot identify device '%s'."), v4l2object->videodev),
754         GST_ERROR_SYSTEM);
755     goto error;
756   }
757 no_device:
758   {
759     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
760         (_("This isn't a device '%s'."), v4l2object->videodev),
761         GST_ERROR_SYSTEM);
762     goto error;
763   }
764 not_open:
765   {
766     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
767         (_("Could not open device '%s' for reading and writing."),
768             v4l2object->videodev), GST_ERROR_SYSTEM);
769     goto error;
770   }
771 not_capture:
772   {
773     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
774         (_("Device '%s' is not a capture device."),
775             v4l2object->videodev),
776         ("Capabilities: 0x%x", v4l2object->device_caps));
777     goto error;
778   }
779 not_output:
780   {
781     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
782         (_("Device '%s' is not a output device."),
783             v4l2object->videodev),
784         ("Capabilities: 0x%x", v4l2object->device_caps));
785     goto error;
786   }
787 not_m2m:
788   {
789     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
790         (_("Device '%s' is not a M2M device."),
791             v4l2object->videodev),
792         ("Capabilities: 0x%x", v4l2object->device_caps));
793     goto error;
794   }
795 error:
796   {
797     if (GST_V4L2_IS_OPEN (v4l2object)) {
798       /* close device */
799       v4l2object->close (v4l2object->video_fd);
800       v4l2object->video_fd = -1;
801     }
802     /* empty lists */
803     gst_v4l2_empty_lists (v4l2object);
804
805 #ifdef TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE
806     globfree(&glob_buf);
807 #endif /* TIZEN_FEATURE_V4L2SRC_SCAN_DEVICE_NODE */
808
809     return FALSE;
810   }
811 }
812
813 gboolean
814 gst_v4l2_dup (GstV4l2Object * v4l2object, GstV4l2Object * other)
815 {
816   GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Trying to dup device %s",
817       other->videodev);
818
819   GST_V4L2_CHECK_OPEN (other);
820   GST_V4L2_CHECK_NOT_OPEN (v4l2object);
821   GST_V4L2_CHECK_NOT_ACTIVE (other);
822   GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
823
824   v4l2object->vcap = other->vcap;
825   v4l2object->device_caps = other->device_caps;
826   gst_v4l2_adjust_buf_type (v4l2object);
827
828   v4l2object->video_fd = v4l2object->dup (other->video_fd);
829   if (!GST_V4L2_IS_OPEN (v4l2object))
830     goto not_open;
831
832   g_free (v4l2object->videodev);
833   v4l2object->videodev = g_strdup (other->videodev);
834
835   GST_INFO_OBJECT (v4l2object->dbg_obj,
836       "Cloned device '%s' (%s) successfully",
837       v4l2object->vcap.card, v4l2object->videodev);
838
839   v4l2object->never_interlaced = other->never_interlaced;
840   v4l2object->no_initial_format = other->no_initial_format;
841
842   return TRUE;
843
844 not_open:
845   {
846     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
847         (_("Could not dup device '%s' for reading and writing."),
848             v4l2object->videodev), GST_ERROR_SYSTEM);
849
850     return FALSE;
851   }
852 }
853
854
855 /******************************************************
856  * gst_v4l2_close():
857  *   close the video device (v4l2object->video_fd)
858  * return value: TRUE on success, FALSE on error
859  ******************************************************/
860 gboolean
861 gst_v4l2_close (GstV4l2Object * v4l2object)
862 {
863   GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Trying to close %s",
864       v4l2object->videodev);
865
866   GST_V4L2_CHECK_OPEN (v4l2object);
867   GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
868
869   /* close device */
870   v4l2object->close (v4l2object->video_fd);
871   v4l2object->video_fd = -1;
872
873   /* empty lists */
874   gst_v4l2_empty_lists (v4l2object);
875
876   return TRUE;
877 }
878
879
880 /******************************************************
881  * gst_v4l2_get_norm()
882  *   Get the norm of the current device
883  * return value: TRUE on success, FALSE on error
884  ******************************************************/
885 gboolean
886 gst_v4l2_get_norm (GstV4l2Object * v4l2object, v4l2_std_id * norm)
887 {
888   GST_DEBUG_OBJECT (v4l2object->dbg_obj, "getting norm");
889
890   if (!GST_V4L2_IS_OPEN (v4l2object))
891     return FALSE;
892
893   if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_STD, norm) < 0)
894     goto std_failed;
895
896   return TRUE;
897
898   /* ERRORS */
899 std_failed:
900   {
901     GST_DEBUG ("Failed to get the current norm for device %s",
902         v4l2object->videodev);
903     return FALSE;
904   }
905 }
906
907
908 /******************************************************
909  * gst_v4l2_set_norm()
910  *   Set the norm of the current device
911  * return value: TRUE on success, FALSE on error
912  ******************************************************/
913 gboolean
914 gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm)
915 {
916   GST_DEBUG_OBJECT (v4l2object->dbg_obj, "trying to set norm to "
917       "%" G_GINT64_MODIFIER "x", (guint64) norm);
918
919   if (!GST_V4L2_IS_OPEN (v4l2object))
920     return FALSE;
921
922   if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_S_STD, &norm) < 0)
923     goto std_failed;
924
925   return TRUE;
926
927   /* ERRORS */
928 std_failed:
929   {
930     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
931         (_("Failed to set norm for device '%s'."),
932             v4l2object->videodev), GST_ERROR_SYSTEM);
933     return FALSE;
934   }
935 }
936
937 /******************************************************
938  * gst_v4l2_get_frequency():
939  *   get the current frequency
940  * return value: TRUE on success, FALSE on error
941  ******************************************************/
942 gboolean
943 gst_v4l2_get_frequency (GstV4l2Object * v4l2object,
944     gint tunernum, gulong * frequency)
945 {
946   struct v4l2_frequency freq = { 0, };
947
948   GstTunerChannel *channel;
949
950   GST_DEBUG_OBJECT (v4l2object->dbg_obj, "getting current tuner frequency");
951
952   if (!GST_V4L2_IS_OPEN (v4l2object))
953     return FALSE;
954
955   channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
956
957   freq.tuner = tunernum;
958   if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0)
959     goto freq_failed;
960
961   *frequency = freq.frequency * channel->freq_multiplicator;
962
963   return TRUE;
964
965   /* ERRORS */
966 freq_failed:
967   {
968     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
969         (_("Failed to get current tuner frequency for device '%s'."),
970             v4l2object->videodev), GST_ERROR_SYSTEM);
971     return FALSE;
972   }
973 }
974
975
976 /******************************************************
977  * gst_v4l2_set_frequency():
978  *   set frequency
979  * return value: TRUE on success, FALSE on error
980  ******************************************************/
981 gboolean
982 gst_v4l2_set_frequency (GstV4l2Object * v4l2object,
983     gint tunernum, gulong frequency)
984 {
985   struct v4l2_frequency freq = { 0, };
986
987   GstTunerChannel *channel;
988
989   GST_DEBUG_OBJECT (v4l2object->dbg_obj,
990       "setting current tuner frequency to %lu", frequency);
991
992   if (!GST_V4L2_IS_OPEN (v4l2object))
993     return FALSE;
994
995   channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));
996
997   freq.tuner = tunernum;
998   /* fill in type - ignore error */
999   (void) v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq);
1000   freq.frequency = frequency / channel->freq_multiplicator;
1001
1002   if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0)
1003     goto freq_failed;
1004
1005   return TRUE;
1006
1007   /* ERRORS */
1008 freq_failed:
1009   {
1010     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1011         (_("Failed to set current tuner frequency for device '%s' to %lu Hz."),
1012             v4l2object->videodev, frequency), GST_ERROR_SYSTEM);
1013     return FALSE;
1014   }
1015 }
1016
1017 /******************************************************
1018  * gst_v4l2_signal_strength():
1019  *   get the strength of the signal on the current input
1020  * return value: TRUE on success, FALSE on error
1021  ******************************************************/
1022 gboolean
1023 gst_v4l2_signal_strength (GstV4l2Object * v4l2object,
1024     gint tunernum, gulong * signal_strength)
1025 {
1026   struct v4l2_tuner tuner = { 0, };
1027
1028   GST_DEBUG_OBJECT (v4l2object->dbg_obj, "trying to get signal strength");
1029
1030   if (!GST_V4L2_IS_OPEN (v4l2object))
1031     return FALSE;
1032
1033   tuner.index = tunernum;
1034   if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &tuner) < 0)
1035     goto tuner_failed;
1036
1037   *signal_strength = tuner.signal;
1038
1039   return TRUE;
1040
1041   /* ERRORS */
1042 tuner_failed:
1043   {
1044     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1045         (_("Failed to get signal strength for device '%s'."),
1046             v4l2object->videodev), GST_ERROR_SYSTEM);
1047     return FALSE;
1048   }
1049 }
1050
1051 /******************************************************
1052  * gst_v4l2_get_attribute():
1053  *   try to get the value of one specific attribute
1054  * return value: TRUE on success, FALSE on error
1055  ******************************************************/
1056 gboolean
1057 gst_v4l2_get_attribute (GstV4l2Object * v4l2object,
1058     int attribute_num, int *value)
1059 {
1060   struct v4l2_control control = { 0, };
1061
1062   GST_DEBUG_OBJECT (v4l2object->dbg_obj, "getting value of attribute %d",
1063       attribute_num);
1064
1065   if (!GST_V4L2_IS_OPEN (v4l2object))
1066     return FALSE;
1067
1068   control.id = attribute_num;
1069
1070   if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0)
1071     goto ctrl_failed;
1072
1073   *value = control.value;
1074
1075   return TRUE;
1076
1077   /* ERRORS */
1078 ctrl_failed:
1079   {
1080     GST_WARNING_OBJECT (v4l2object,
1081         _("Failed to get value for control %d on device '%s'."),
1082         attribute_num, v4l2object->videodev);
1083     return FALSE;
1084   }
1085 }
1086
1087
1088 /******************************************************
1089  * gst_v4l2_set_attribute():
1090  *   try to set the value of one specific attribute
1091  * return value: TRUE on success, FALSE on error
1092  ******************************************************/
1093 gboolean
1094 gst_v4l2_set_attribute (GstV4l2Object * v4l2object,
1095     int attribute_num, const int value)
1096 {
1097   struct v4l2_control control = { 0, };
1098
1099   GST_DEBUG_OBJECT (v4l2object->dbg_obj, "setting value of attribute %d to %d",
1100       attribute_num, value);
1101
1102   if (!GST_V4L2_IS_OPEN (v4l2object))
1103     return FALSE;
1104
1105   control.id = attribute_num;
1106   control.value = value;
1107   if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0)
1108     goto ctrl_failed;
1109
1110   return TRUE;
1111
1112   /* ERRORS */
1113 ctrl_failed:
1114   {
1115     GST_WARNING_OBJECT (v4l2object,
1116         _("Failed to set value %d for control %d on device '%s'."),
1117         value, attribute_num, v4l2object->videodev);
1118     return FALSE;
1119   }
1120 }
1121
1122 static gboolean
1123 set_control (GQuark field_id, const GValue * value, gpointer user_data)
1124 {
1125   GstV4l2Object *v4l2object = user_data;
1126   GQuark normalised_field_id;
1127   gpointer *d;
1128
1129   /* 32 bytes is the maximum size for a control name according to v4l2 */
1130   gchar name[32];
1131
1132   /* Backwards compatibility: in the past GStreamer would normalise strings in
1133      a subtly different way to v4l2-ctl.  e.g. the kernel's "Focus (absolute)"
1134      would become "focus__absolute_" whereas now it becomes "focus_absolute".
1135      Please remove the following in GStreamer 1.5 for 1.6 */
1136   strncpy (name, g_quark_to_string (field_id), sizeof (name));
1137   name[31] = '\0';
1138   gst_v4l2_normalise_control_name (name);
1139   normalised_field_id = g_quark_from_string (name);
1140   if (normalised_field_id != field_id)
1141     g_warning ("In GStreamer 1.4 the way V4L2 control names were normalised "
1142         "changed.  Instead of setting \"%s\" please use \"%s\".  The former is "
1143         "deprecated and will be removed in a future version of GStreamer",
1144         g_quark_to_string (field_id), name);
1145   field_id = normalised_field_id;
1146
1147   d = g_datalist_id_get_data (&v4l2object->controls, field_id);
1148   if (!d) {
1149     GST_WARNING_OBJECT (v4l2object,
1150         "Control '%s' does not exist or has an unsupported type.",
1151         g_quark_to_string (field_id));
1152     return TRUE;
1153   }
1154   if (!G_VALUE_HOLDS (value, G_TYPE_INT)) {
1155     GST_WARNING_OBJECT (v4l2object,
1156         "'int' value expected for control '%s'.", g_quark_to_string (field_id));
1157     return TRUE;
1158   }
1159   gst_v4l2_set_attribute (v4l2object, GPOINTER_TO_INT (d),
1160       g_value_get_int (value));
1161   return TRUE;
1162 }
1163
1164 gboolean
1165 gst_v4l2_set_controls (GstV4l2Object * v4l2object, GstStructure * controls)
1166 {
1167   return gst_structure_foreach (controls, set_control, v4l2object);
1168 }
1169
1170 gboolean
1171 gst_v4l2_get_input (GstV4l2Object * v4l2object, gint * input)
1172 {
1173   gint n;
1174
1175   GST_DEBUG_OBJECT (v4l2object->dbg_obj, "trying to get input");
1176
1177   if (!GST_V4L2_IS_OPEN (v4l2object))
1178     return FALSE;
1179
1180   if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_INPUT, &n) < 0)
1181     goto input_failed;
1182
1183   *input = n;
1184
1185   GST_DEBUG_OBJECT (v4l2object->dbg_obj, "input: %d", n);
1186
1187   return TRUE;
1188
1189   /* ERRORS */
1190 input_failed:
1191   if (v4l2object->device_caps & V4L2_CAP_TUNER) {
1192     /* only give a warning message if driver actually claims to have tuner
1193      * support
1194      */
1195     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1196         (_("Failed to get current input on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1197   }
1198   return FALSE;
1199 }
1200
1201 gboolean
1202 gst_v4l2_set_input (GstV4l2Object * v4l2object, gint input)
1203 {
1204   GST_DEBUG_OBJECT (v4l2object->dbg_obj, "trying to set input to %d", input);
1205
1206   if (!GST_V4L2_IS_OPEN (v4l2object))
1207     return FALSE;
1208
1209   if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_S_INPUT, &input) < 0)
1210     goto input_failed;
1211
1212   return TRUE;
1213
1214   /* ERRORS */
1215 input_failed:
1216   if (v4l2object->device_caps & V4L2_CAP_TUNER) {
1217     /* only give a warning message if driver actually claims to have tuner
1218      * support
1219      */
1220     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1221         (_("Failed to set input %d on device %s."),
1222             input, v4l2object->videodev), GST_ERROR_SYSTEM);
1223   }
1224   return FALSE;
1225 }
1226
1227 gboolean
1228 gst_v4l2_get_output (GstV4l2Object * v4l2object, gint * output)
1229 {
1230   gint n;
1231
1232   GST_DEBUG_OBJECT (v4l2object->dbg_obj, "trying to get output");
1233
1234   if (!GST_V4L2_IS_OPEN (v4l2object))
1235     return FALSE;
1236
1237   if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_OUTPUT, &n) < 0)
1238     goto output_failed;
1239
1240   *output = n;
1241
1242   GST_DEBUG_OBJECT (v4l2object->dbg_obj, "output: %d", n);
1243
1244   return TRUE;
1245
1246   /* ERRORS */
1247 output_failed:
1248   if (v4l2object->device_caps & V4L2_CAP_TUNER) {
1249     /* only give a warning message if driver actually claims to have tuner
1250      * support
1251      */
1252     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1253         (_("Failed to get current output on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
1254   }
1255   return FALSE;
1256 }
1257
1258 gboolean
1259 gst_v4l2_set_output (GstV4l2Object * v4l2object, gint output)
1260 {
1261   GST_DEBUG_OBJECT (v4l2object->dbg_obj, "trying to set output to %d", output);
1262
1263   if (!GST_V4L2_IS_OPEN (v4l2object))
1264     return FALSE;
1265
1266   if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_S_OUTPUT, &output) < 0)
1267     goto output_failed;
1268
1269   return TRUE;
1270
1271   /* ERRORS */
1272 output_failed:
1273   if (v4l2object->device_caps & V4L2_CAP_TUNER) {
1274     /* only give a warning message if driver actually claims to have tuner
1275      * support
1276      */
1277     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
1278         (_("Failed to set output %d on device %s."),
1279             output, v4l2object->videodev), GST_ERROR_SYSTEM);
1280   }
1281   return FALSE;
1282 }