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