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