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