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