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