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