1 /* GStreamer
2 *
3 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@gmail.com>
5 *
6 * gstv4l2src.c: Video4Linux2 source element
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 /**
25 * SECTION:element-v4l2src
26 * @title: v4l2src
27 *
28 * v4l2src can be used to capture video from v4l2 devices, like webcams and tv
29 * cards.
30 *
31 * ## Example launch lines
32 * |[
33 * gst-launch-1.0 v4l2src ! xvimagesink
34 * ]| This pipeline shows the video captured from /dev/video0 tv card and for
35 * webcams.
36 * |[
37 * gst-launch-1.0 v4l2src ! jpegdec ! xvimagesink
38 * ]| This pipeline shows the video captured from a webcam that delivers jpeg
39 * images.
40 *
41 * Since 1.14, the use of libv4l2 has been disabled due to major bugs in the
42 * emulation layer. To enable usage of this library, set the environment
43 * variable GST_V4L2_USE_LIBV4L2=1.
44 */
45
46 #ifdef HAVE_CONFIG_H
47 #include <config.h>
48 #endif
49
50 #include <string.h>
51 #include <sys/time.h>
52 #include <unistd.h>
53
54 #include <gst/video/gstvideometa.h>
55 #include <gst/video/gstvideopool.h>
56
57 #include "gstv4l2elements.h"
58 #include "gstv4l2src.h"
59
60 #include "gstv4l2colorbalance.h"
61 #include "gstv4l2tuner.h"
62 #include "gstv4l2vidorient.h"
63
64 #include "gst/gst-i18n-plugin.h"
65
66 GST_DEBUG_CATEGORY (v4l2src_debug);
67 #define GST_CAT_DEFAULT v4l2src_debug
68
69 #define DEFAULT_PROP_DEVICE "/dev/video0"
70
71 enum
72 {
73 PROP_0,
74 V4L2_STD_OBJECT_PROPS,
75 PROP_LAST
76 };
77
78 /* signals and args */
79 enum
80 {
81 SIGNAL_PRE_SET_FORMAT,
82 LAST_SIGNAL
83 };
84
85 static guint gst_v4l2_signals[LAST_SIGNAL] = { 0 };
86
87 GST_IMPLEMENT_V4L2_COLOR_BALANCE_METHODS (GstV4l2Src, gst_v4l2src);
88 GST_IMPLEMENT_V4L2_TUNER_METHODS (GstV4l2Src, gst_v4l2src);
89 GST_IMPLEMENT_V4L2_VIDORIENT_METHODS (GstV4l2Src, gst_v4l2src);
90
91 static void gst_v4l2src_uri_handler_init (gpointer g_iface,
92 gpointer iface_data);
93
94 #define gst_v4l2src_parent_class parent_class
95 G_DEFINE_TYPE_WITH_CODE (GstV4l2Src, gst_v4l2src, GST_TYPE_PUSH_SRC,
96 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_v4l2src_uri_handler_init);
97 G_IMPLEMENT_INTERFACE (GST_TYPE_TUNER, gst_v4l2src_tuner_interface_init);
98 G_IMPLEMENT_INTERFACE (GST_TYPE_COLOR_BALANCE,
99 gst_v4l2src_color_balance_interface_init);
100 G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_ORIENTATION,
101 gst_v4l2src_video_orientation_interface_init));
102 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (v4l2src,
103 "v4l2src", GST_RANK_PRIMARY, GST_TYPE_V4L2SRC, v4l2_element_init (plugin));
104
105 struct PreferredCapsInfo
106 {
107 gint width;
108 gint height;
109 gint fps_n;
110 gint fps_d;
111 };
112
113 static void gst_v4l2src_finalize (GstV4l2Src * v4l2src);
114
115 /* element methods */
116 static GstStateChangeReturn gst_v4l2src_change_state (GstElement * element,
117 GstStateChange transition);
118
119 /* basesrc methods */
120 static gboolean gst_v4l2src_start (GstBaseSrc * src);
121 static gboolean gst_v4l2src_unlock (GstBaseSrc * src);
122 static gboolean gst_v4l2src_unlock_stop (GstBaseSrc * src);
123 static gboolean gst_v4l2src_stop (GstBaseSrc * src);
124 static GstCaps *gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter);
125 static gboolean gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query);
126 static gboolean gst_v4l2src_decide_allocation (GstBaseSrc * src,
127 GstQuery * query);
128 static GstFlowReturn gst_v4l2src_create (GstPushSrc * src, GstBuffer ** out);
129 static GstCaps *gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps,
130 struct PreferredCapsInfo *pref);
131 static gboolean gst_v4l2src_negotiate (GstBaseSrc * basesrc);
132
133 static void gst_v4l2src_set_property (GObject * object, guint prop_id,
134 const GValue * value, GParamSpec * pspec);
135 static void gst_v4l2src_get_property (GObject * object, guint prop_id,
136 GValue * value, GParamSpec * pspec);
137
138 static void
gst_v4l2src_class_init(GstV4l2SrcClass * klass)139 gst_v4l2src_class_init (GstV4l2SrcClass * klass)
140 {
141 GObjectClass *gobject_class;
142 GstElementClass *element_class;
143 GstBaseSrcClass *basesrc_class;
144 GstPushSrcClass *pushsrc_class;
145
146 gobject_class = G_OBJECT_CLASS (klass);
147 element_class = GST_ELEMENT_CLASS (klass);
148 basesrc_class = GST_BASE_SRC_CLASS (klass);
149 pushsrc_class = GST_PUSH_SRC_CLASS (klass);
150
151 gobject_class->finalize = (GObjectFinalizeFunc) gst_v4l2src_finalize;
152 gobject_class->set_property = gst_v4l2src_set_property;
153 gobject_class->get_property = gst_v4l2src_get_property;
154
155 element_class->change_state = gst_v4l2src_change_state;
156
157 gst_v4l2_object_install_properties_helper (gobject_class,
158 DEFAULT_PROP_DEVICE);
159
160 /**
161 * GstV4l2Src::prepare-format:
162 * @v4l2src: the v4l2src instance
163 * @fd: the file descriptor of the current device
164 * @caps: the caps of the format being set
165 *
166 * This signal gets emitted before calling the v4l2 VIDIOC_S_FMT ioctl
167 * (set format). This allows for any custom configuration of the device to
168 * happen prior to the format being set.
169 * This is mostly useful for UVC H264 encoding cameras which need the H264
170 * Probe & Commit to happen prior to the normal Probe & Commit.
171 */
172 gst_v4l2_signals[SIGNAL_PRE_SET_FORMAT] = g_signal_new ("prepare-format",
173 G_TYPE_FROM_CLASS (klass),
174 G_SIGNAL_RUN_LAST,
175 0, NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_INT, GST_TYPE_CAPS);
176
177 gst_element_class_set_static_metadata (element_class,
178 "Video (video4linux2) Source", "Source/Video",
179 "Reads frames from a Video4Linux2 device",
180 "Edgard Lima <edgard.lima@gmail.com>, "
181 "Stefan Kost <ensonic@users.sf.net>");
182
183 gst_element_class_add_pad_template
184 (element_class,
185 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
186 gst_v4l2_object_get_all_caps ()));
187
188 basesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_get_caps);
189 basesrc_class->start = GST_DEBUG_FUNCPTR (gst_v4l2src_start);
190 basesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock);
191 basesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock_stop);
192 basesrc_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2src_stop);
193 basesrc_class->query = GST_DEBUG_FUNCPTR (gst_v4l2src_query);
194 basesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_v4l2src_negotiate);
195 basesrc_class->decide_allocation =
196 GST_DEBUG_FUNCPTR (gst_v4l2src_decide_allocation);
197
198 pushsrc_class->create = GST_DEBUG_FUNCPTR (gst_v4l2src_create);
199
200 klass->v4l2_class_devices = NULL;
201
202 GST_DEBUG_CATEGORY_INIT (v4l2src_debug, "v4l2src", 0, "V4L2 source element");
203 }
204
205 static void
gst_v4l2src_init(GstV4l2Src * v4l2src)206 gst_v4l2src_init (GstV4l2Src * v4l2src)
207 {
208 /* fixme: give an update_fps_function */
209 v4l2src->v4l2object = gst_v4l2_object_new (GST_ELEMENT (v4l2src),
210 GST_OBJECT (GST_BASE_SRC_PAD (v4l2src)), V4L2_BUF_TYPE_VIDEO_CAPTURE,
211 DEFAULT_PROP_DEVICE, gst_v4l2_get_input, gst_v4l2_set_input, NULL);
212
213 /* Avoid the slow probes */
214 v4l2src->v4l2object->skip_try_fmt_probes = TRUE;
215
216 gst_base_src_set_format (GST_BASE_SRC (v4l2src), GST_FORMAT_TIME);
217 gst_base_src_set_live (GST_BASE_SRC (v4l2src), TRUE);
218 }
219
220
221 static void
gst_v4l2src_finalize(GstV4l2Src * v4l2src)222 gst_v4l2src_finalize (GstV4l2Src * v4l2src)
223 {
224 gst_v4l2_object_destroy (v4l2src->v4l2object);
225
226 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (v4l2src));
227 }
228
229
230 static void
gst_v4l2src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)231 gst_v4l2src_set_property (GObject * object,
232 guint prop_id, const GValue * value, GParamSpec * pspec)
233 {
234 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
235
236 if (!gst_v4l2_object_set_property_helper (v4l2src->v4l2object,
237 prop_id, value, pspec)) {
238 switch (prop_id) {
239 default:
240 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241 break;
242 }
243 }
244 }
245
246 static void
gst_v4l2src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)247 gst_v4l2src_get_property (GObject * object,
248 guint prop_id, GValue * value, GParamSpec * pspec)
249 {
250 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
251
252 if (!gst_v4l2_object_get_property_helper (v4l2src->v4l2object,
253 prop_id, value, pspec)) {
254 switch (prop_id) {
255 default:
256 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
257 break;
258 }
259 }
260 }
261
262 static gboolean
gst_vl42_src_fixate_fields(GQuark field_id,GValue * value,gpointer user_data)263 gst_vl42_src_fixate_fields (GQuark field_id, GValue * value, gpointer user_data)
264 {
265 GstStructure *s = user_data;
266
267 if (field_id == g_quark_from_string ("interlace-mode"))
268 return TRUE;
269
270 if (field_id == g_quark_from_string ("colorimetry"))
271 return TRUE;
272
273 gst_structure_fixate_field (s, g_quark_to_string (field_id));
274
275 return TRUE;
276 }
277
278 static void
gst_v4l2_src_fixate_struct_with_preference(GstStructure * s,struct PreferredCapsInfo * pref)279 gst_v4l2_src_fixate_struct_with_preference (GstStructure * s,
280 struct PreferredCapsInfo *pref)
281 {
282 if (gst_structure_has_field (s, "width"))
283 gst_structure_fixate_field_nearest_int (s, "width", pref->width);
284
285 if (gst_structure_has_field (s, "height"))
286 gst_structure_fixate_field_nearest_int (s, "height", pref->height);
287
288 if (gst_structure_has_field (s, "framerate"))
289 gst_structure_fixate_field_nearest_fraction (s, "framerate", pref->fps_n,
290 pref->fps_d);
291
292 /* Finally, fixate everything else except the interlace-mode and colorimetry
293 * which still need further negotiation as it wasn't probed */
294 gst_structure_map_in_place (s, gst_vl42_src_fixate_fields, s);
295 }
296
297 static void
gst_v4l2_src_parse_fixed_struct(GstStructure * s,gint * width,gint * height,gint * fps_n,gint * fps_d)298 gst_v4l2_src_parse_fixed_struct (GstStructure * s,
299 gint * width, gint * height, gint * fps_n, gint * fps_d)
300 {
301 if (gst_structure_has_field (s, "width") && width)
302 gst_structure_get_int (s, "width", width);
303
304 if (gst_structure_has_field (s, "height") && height)
305 gst_structure_get_int (s, "height", height);
306
307 if (gst_structure_has_field (s, "framerate") && fps_n && fps_d)
308 gst_structure_get_fraction (s, "framerate", fps_n, fps_d);
309 }
310
311 /* TODO Consider framerate */
312 static gint
gst_v4l2src_fixed_caps_compare(GstCaps * caps_a,GstCaps * caps_b,struct PreferredCapsInfo * pref)313 gst_v4l2src_fixed_caps_compare (GstCaps * caps_a, GstCaps * caps_b,
314 struct PreferredCapsInfo *pref)
315 {
316 GstStructure *a, *b;
317 gint aw = G_MAXINT, ah = G_MAXINT, ad = G_MAXINT;
318 gint bw = G_MAXINT, bh = G_MAXINT, bd = G_MAXINT;
319 gint ret;
320
321 a = gst_caps_get_structure (caps_a, 0);
322 b = gst_caps_get_structure (caps_b, 0);
323
324 gst_v4l2_src_parse_fixed_struct (a, &aw, &ah, NULL, NULL);
325 gst_v4l2_src_parse_fixed_struct (b, &bw, &bh, NULL, NULL);
326
327 /* When both are smaller then pref, just append to the end */
328 if ((bw < pref->width || bh < pref->height)
329 && (aw < pref->width || ah < pref->height)) {
330 ret = 1;
331 goto done;
332 }
333
334 /* If a is smaller then pref and not b, then a goes after b */
335 if (aw < pref->width || ah < pref->height) {
336 ret = 1;
337 goto done;
338 }
339
340 /* If b is smaller then pref and not a, then a goes before b */
341 if (bw < pref->width || bh < pref->height) {
342 ret = -1;
343 goto done;
344 }
345
346 /* Both are larger or equal to the preference, prefer the smallest */
347 ad = MAX (1, aw - pref->width) * MAX (1, ah - pref->height);
348 bd = MAX (1, bw - pref->width) * MAX (1, bh - pref->height);
349
350 /* Adjust slightly in case width/height matched the preference */
351 if (aw == pref->width)
352 ad -= 1;
353
354 if (ah == pref->height)
355 ad -= 1;
356
357 if (bw == pref->width)
358 bd -= 1;
359
360 if (bh == pref->height)
361 bd -= 1;
362
363 /* If the choices are equivalent, maintain the order */
364 if (ad == bd)
365 ret = 1;
366 else
367 ret = ad - bd;
368
369 done:
370 GST_TRACE ("Placing %ix%i (%s) %s %ix%i (%s)", aw, ah,
371 gst_structure_get_string (a, "format"), ret > 0 ? "after" : "before", bw,
372 bh, gst_structure_get_string (b, "format"));
373 return ret;
374 }
375
376 static gboolean
gst_v4l2src_set_format(GstV4l2Src * v4l2src,GstCaps * caps,GstV4l2Error * error)377 gst_v4l2src_set_format (GstV4l2Src * v4l2src, GstCaps * caps,
378 GstV4l2Error * error)
379 {
380 GstV4l2Object *obj;
381
382 obj = v4l2src->v4l2object;
383
384 /* make sure we stop capturing and dealloc buffers */
385 if (!gst_v4l2_object_stop (obj))
386 return FALSE;
387
388 g_signal_emit (v4l2src, gst_v4l2_signals[SIGNAL_PRE_SET_FORMAT], 0,
389 v4l2src->v4l2object->video_fd, caps);
390
391 return gst_v4l2_object_set_format (obj, caps, error);
392 }
393
394 static GstCaps *
gst_v4l2src_fixate(GstBaseSrc * basesrc,GstCaps * caps,struct PreferredCapsInfo * pref)395 gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps,
396 struct PreferredCapsInfo *pref)
397 {
398 GstV4l2Src *v4l2src = GST_V4L2SRC (basesrc);
399 GstV4l2Object *obj = v4l2src->v4l2object;
400 GList *caps_list = NULL;
401 GstStructure *s;
402 gint i = G_MAXINT;
403 GstV4l2Error error = GST_V4L2_ERROR_INIT;
404 GstCaps *fcaps = NULL;
405
406 GST_DEBUG_OBJECT (basesrc, "Fixating caps %" GST_PTR_FORMAT, caps);
407 GST_DEBUG_OBJECT (basesrc, "Preferred size %ix%i", pref->width, pref->height);
408
409 /* Sort the structures to get the caps that is nearest to our preferences,
410 * first. Use single struct caps for sorting so we preserve the features. */
411 for (i = 0; i < gst_caps_get_size (caps); i++) {
412 GstCaps *tmp = gst_caps_copy_nth (caps, i);
413
414 s = gst_caps_get_structure (tmp, 0);
415 gst_v4l2_src_fixate_struct_with_preference (s, pref);
416
417 caps_list = g_list_insert_sorted_with_data (caps_list, tmp,
418 (GCompareDataFunc) gst_v4l2src_fixed_caps_compare, pref);
419 }
420
421 gst_caps_unref (caps);
422 caps = gst_caps_new_empty ();
423
424 while (caps_list) {
425 GstCaps *tmp = caps_list->data;
426 caps_list = g_list_delete_link (caps_list, caps_list);
427 gst_caps_append (caps, tmp);
428 }
429
430 GST_DEBUG_OBJECT (basesrc, "sorted and normalized caps %" GST_PTR_FORMAT,
431 caps);
432
433 /* Each structure in the caps has been fixated, except for the
434 * interlace-mode and colorimetry. Now normalize the caps so we can
435 * enumerate the possibilities */
436 caps = gst_caps_normalize (caps);
437
438 for (i = 0; i < gst_caps_get_size (caps); ++i) {
439 gst_v4l2_clear_error (&error);
440 if (fcaps)
441 gst_caps_unref (fcaps);
442
443 fcaps = gst_caps_copy_nth (caps, i);
444
445 /* try hard to avoid TRY_FMT since some UVC camera just crash when this
446 * is called at run-time. */
447 if (gst_v4l2_object_caps_is_subset (obj, fcaps)) {
448 gst_caps_unref (fcaps);
449 fcaps = gst_v4l2_object_get_current_caps (obj);
450 break;
451 }
452
453 /* Just check if the format is acceptable, once we know
454 * no buffers should be outstanding we try S_FMT.
455 *
456 * Basesrc will do an allocation query that
457 * should indirectly reclaim buffers, after that we can
458 * set the format and then configure our pool */
459 if (gst_v4l2_object_try_format (obj, fcaps, &error)) {
460 /* make sure the caps changed before doing anything */
461 if (gst_v4l2_object_caps_equal (obj, fcaps))
462 break;
463
464 v4l2src->renegotiation_adjust = v4l2src->offset + 1;
465 v4l2src->pending_set_fmt = TRUE;
466 break;
467 }
468
469 /* Only EIVAL make sense, report any other errors, this way we don't keep
470 * probing if the device got disconnected, or if it's firmware stopped
471 * responding */
472 if (error.error->code != GST_RESOURCE_ERROR_SETTINGS) {
473 i = G_MAXINT;
474 break;
475 }
476 }
477
478 if (i >= gst_caps_get_size (caps)) {
479 gst_v4l2_error (v4l2src, &error);
480 if (fcaps)
481 gst_caps_unref (fcaps);
482 gst_caps_unref (caps);
483 return NULL;
484 }
485
486 gst_caps_unref (caps);
487
488 GST_DEBUG_OBJECT (basesrc, "fixated caps %" GST_PTR_FORMAT, fcaps);
489
490 return fcaps;
491 }
492
493 static gboolean
gst_v4l2src_query_preferred_dv_timings(GstV4l2Src * v4l2src,struct PreferredCapsInfo * pref)494 gst_v4l2src_query_preferred_dv_timings (GstV4l2Src * v4l2src,
495 struct PreferredCapsInfo *pref)
496 {
497 GstV4l2Object *obj = v4l2src->v4l2object;
498 struct v4l2_dv_timings dv_timings = { 0, };
499 const struct v4l2_bt_timings *bt = &dv_timings.bt;
500 gint tot_width, tot_height;
501 gint gcd;
502
503 if (!gst_v4l2_query_dv_timings (obj, &dv_timings))
504 return FALSE;
505
506 pref->width = bt->width;
507 pref->height = bt->height;
508
509 tot_height = bt->height +
510 bt->vfrontporch + bt->vsync + bt->vbackporch +
511 bt->il_vfrontporch + bt->il_vsync + bt->il_vbackporch;
512 tot_width = bt->width + bt->hfrontporch + bt->hsync + bt->hbackporch;
513
514 pref->fps_n = bt->pixelclock;
515 pref->fps_d = tot_width * tot_height;
516
517 if (bt->interlaced)
518 pref->fps_d /= 2;
519
520 gcd = gst_util_greatest_common_divisor (pref->fps_n, pref->fps_d);
521 pref->fps_n /= gcd;
522 pref->fps_d /= gcd;
523
524 /* If are are not streaming (e.g. we received source-change event), lock the
525 * new timing immediatly so that TRY_FMT can properly work */
526 if (!obj->pool || !GST_V4L2_BUFFER_POOL_IS_STREAMING (obj->pool)) {
527 gst_v4l2_set_dv_timings (obj, &dv_timings);
528 /* Setting a new DV timings invalidates the probed caps. */
529 gst_caps_replace (&obj->probed_caps, NULL);
530 }
531
532 GST_INFO_OBJECT (v4l2src, "Using DV Timings: %i x %i (%i/%i fps)",
533 pref->width, pref->height, pref->fps_n, pref->fps_d);
534
535 return TRUE;
536 }
537
538 static gboolean
gst_v4l2src_query_preferred_size(GstV4l2Src * v4l2src,struct PreferredCapsInfo * pref)539 gst_v4l2src_query_preferred_size (GstV4l2Src * v4l2src,
540 struct PreferredCapsInfo *pref)
541 {
542 struct v4l2_input in = { 0, };
543
544 if (!gst_v4l2_get_input (v4l2src->v4l2object, &in.index))
545 return FALSE;
546
547 if (!gst_v4l2_query_input (v4l2src->v4l2object, &in))
548 return FALSE;
549
550 GST_INFO_OBJECT (v4l2src, "Detect input %u as `%s`", in.index, in.name);
551
552 /* Notify signal status using WARNING/INFO messages */
553 if (in.status & (V4L2_IN_ST_NO_POWER | V4L2_IN_ST_NO_SIGNAL)) {
554 if (!v4l2src->no_signal)
555 /* note: taken from decklinksrc element */
556 GST_ELEMENT_WARNING (v4l2src, RESOURCE, READ, ("Signal lost"),
557 ("No input source was detected - video frames invalid"));
558 v4l2src->no_signal = TRUE;
559 } else if (v4l2src->no_signal) {
560 if (v4l2src->no_signal)
561 GST_ELEMENT_INFO (v4l2src, RESOURCE, READ,
562 ("Signal recovered"), ("Input source detected"));
563 v4l2src->no_signal = FALSE;
564 }
565
566 if (in.capabilities & V4L2_IN_CAP_NATIVE_SIZE) {
567 GST_FIXME_OBJECT (v4l2src, "missing support for native video size");
568 return FALSE;
569 } else if (in.capabilities & V4L2_IN_CAP_DV_TIMINGS) {
570 return gst_v4l2src_query_preferred_dv_timings (v4l2src, pref);
571 } else if (in.capabilities & V4L2_IN_CAP_STD) {
572 GST_FIXME_OBJECT (v4l2src, "missing support for video standards");
573 return FALSE;
574 }
575
576 return FALSE;
577 }
578
579 static gboolean
gst_v4l2src_negotiate(GstBaseSrc * basesrc)580 gst_v4l2src_negotiate (GstBaseSrc * basesrc)
581 {
582 GstV4l2Src *v4l2src = GST_V4L2SRC (basesrc);
583 GstCaps *thiscaps;
584 GstCaps *caps = NULL;
585 GstCaps *peercaps = NULL;
586 gboolean result = FALSE;
587 /* Let's prefer a good resolution as of today's standard. */
588 struct PreferredCapsInfo pref = {
589 3840, 2160, 120, 1
590 };
591 gboolean have_pref;
592
593 /* For drivers that has DV timings or other default size query
594 * capabilities, we will prefer that resolution. This must happen before we
595 * probe the caps, as locking DV Timings or standards will change result of
596 * the caps enumeration. */
597 have_pref = gst_v4l2src_query_preferred_size (v4l2src, &pref);
598
599 /* first see what is possible on our source pad */
600 thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
601 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
602
603 /* nothing or anything is allowed, we're done */
604 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
605 goto no_nego_needed;
606
607 /* get the peer caps without a filter as we'll filter ourselves later on */
608 peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
609 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
610 if (peercaps && !gst_caps_is_any (peercaps)) {
611 /* Prefer the first caps we are compatible with that the peer proposed */
612 caps = gst_caps_intersect_full (peercaps, thiscaps,
613 GST_CAPS_INTERSECT_FIRST);
614
615 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, caps);
616
617 gst_caps_unref (thiscaps);
618 } else {
619 /* no peer or peer have ANY caps, work with our own caps then */
620 caps = thiscaps;
621 }
622
623 if (caps) {
624 /* now fixate */
625 if (!gst_caps_is_empty (caps)) {
626
627 /* otherwise consider the first structure from peercaps to be a
628 * preference. This is useful for matching a reported native display,
629 * or simply to avoid transformation to happen downstream. */
630 if (!have_pref && peercaps && !gst_caps_is_any (peercaps)) {
631 GstStructure *pref_s = gst_caps_get_structure (peercaps, 0);
632 pref_s = gst_structure_copy (pref_s);
633 gst_v4l2_src_fixate_struct_with_preference (pref_s, &pref);
634 gst_v4l2_src_parse_fixed_struct (pref_s, &pref.width, &pref.height,
635 &pref.fps_n, &pref.fps_d);
636 gst_structure_free (pref_s);
637 }
638
639 caps = gst_v4l2src_fixate (basesrc, caps, &pref);
640
641 /* Fixating may fail as we now set the selected format */
642 if (!caps) {
643 result = FALSE;
644 goto done;
645 }
646
647 GST_INFO_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
648
649 if (gst_caps_is_any (caps)) {
650 /* hmm, still anything, so element can do anything and
651 * nego is not needed */
652 result = TRUE;
653 } else if (gst_caps_is_fixed (caps)) {
654 /* yay, fixed caps, use those then */
655 result = gst_base_src_set_caps (basesrc, caps);
656 }
657 }
658 gst_caps_unref (caps);
659 }
660
661 done:
662 if (peercaps)
663 gst_caps_unref (peercaps);
664
665 return result;
666
667 no_nego_needed:
668 {
669 GST_INFO_OBJECT (basesrc, "no negotiation needed");
670 if (thiscaps)
671 gst_caps_unref (thiscaps);
672 return TRUE;
673 }
674 }
675
676 static GstCaps *
gst_v4l2src_get_caps(GstBaseSrc * src,GstCaps * filter)677 gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter)
678 {
679 GstV4l2Src *v4l2src;
680 GstV4l2Object *obj;
681
682 v4l2src = GST_V4L2SRC (src);
683 obj = v4l2src->v4l2object;
684
685 if (!GST_V4L2_IS_OPEN (obj)) {
686 return gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD (v4l2src));
687 }
688
689 return gst_v4l2_object_get_caps (obj, filter);
690 }
691
692 static gboolean
gst_v4l2src_decide_allocation(GstBaseSrc * bsrc,GstQuery * query)693 gst_v4l2src_decide_allocation (GstBaseSrc * bsrc, GstQuery * query)
694 {
695 GstV4l2Src *src = GST_V4L2SRC (bsrc);
696 gboolean ret = TRUE;
697
698 if (src->pending_set_fmt) {
699 GstCaps *caps = gst_pad_get_current_caps (GST_BASE_SRC_PAD (bsrc));
700 GstV4l2Error error = GST_V4L2_ERROR_INIT;
701
702 caps = gst_caps_make_writable (caps);
703
704 ret = gst_v4l2src_set_format (src, caps, &error);
705 if (ret) {
706 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (src->v4l2object->pool);
707 gst_v4l2_buffer_pool_enable_resolution_change (pool);
708 } else {
709 gst_v4l2_error (src, &error);
710 }
711
712 gst_caps_unref (caps);
713 src->pending_set_fmt = FALSE;
714 } else if (gst_buffer_pool_is_active (src->v4l2object->pool)) {
715 /* Trick basesrc into not deactivating the active pool. Renegotiating here
716 * would otherwise turn off and on the camera. */
717 GstAllocator *allocator;
718 GstAllocationParams params;
719 GstBufferPool *pool;
720
721 gst_base_src_get_allocator (bsrc, &allocator, ¶ms);
722 pool = gst_base_src_get_buffer_pool (bsrc);
723
724 if (gst_query_get_n_allocation_params (query))
725 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
726 else
727 gst_query_add_allocation_param (query, allocator, ¶ms);
728
729 if (gst_query_get_n_allocation_pools (query))
730 gst_query_set_nth_allocation_pool (query, 0, pool,
731 src->v4l2object->info.size, 1, 0);
732 else
733 gst_query_add_allocation_pool (query, pool, src->v4l2object->info.size, 1,
734 0);
735
736 if (pool)
737 gst_object_unref (pool);
738 if (allocator)
739 gst_object_unref (allocator);
740
741 return GST_BASE_SRC_CLASS (parent_class)->decide_allocation (bsrc, query);
742 }
743
744 if (ret) {
745 ret = gst_v4l2_object_decide_allocation (src->v4l2object, query);
746 if (ret)
747 ret = GST_BASE_SRC_CLASS (parent_class)->decide_allocation (bsrc, query);
748 }
749
750 if (ret) {
751 if (!gst_buffer_pool_set_active (src->v4l2object->pool, TRUE))
752 goto activate_failed;
753 }
754
755 return ret;
756
757 activate_failed:
758 {
759 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS,
760 (_("Failed to allocate required memory.")),
761 ("Buffer pool activation failed"));
762 return FALSE;
763 }
764 }
765
766 static gboolean
gst_v4l2src_query(GstBaseSrc * bsrc,GstQuery * query)767 gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query)
768 {
769 GstV4l2Src *src;
770 GstV4l2Object *obj;
771 gboolean res = FALSE;
772
773 src = GST_V4L2SRC (bsrc);
774 obj = src->v4l2object;
775
776 switch (GST_QUERY_TYPE (query)) {
777 case GST_QUERY_LATENCY:{
778 GstClockTime min_latency, max_latency;
779 guint32 fps_n, fps_d;
780 guint num_buffers = 0;
781
782 /* device must be open */
783 if (!GST_V4L2_IS_OPEN (obj)) {
784 GST_WARNING_OBJECT (src,
785 "Can't give latency since device isn't open !");
786 goto done;
787 }
788
789 fps_n = GST_V4L2_FPS_N (obj);
790 fps_d = GST_V4L2_FPS_D (obj);
791
792 /* we must have a framerate */
793 if (fps_n <= 0 || fps_d <= 0) {
794 GST_WARNING_OBJECT (src,
795 "Can't give latency since framerate isn't fixated !");
796 goto done;
797 }
798
799 /* min latency is the time to capture one frame/field */
800 min_latency = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n);
801 if (GST_VIDEO_INFO_INTERLACE_MODE (&obj->info) ==
802 GST_VIDEO_INTERLACE_MODE_ALTERNATE)
803 min_latency /= 2;
804
805 /* max latency is total duration of the frame buffer */
806 if (obj->pool != NULL)
807 num_buffers = GST_V4L2_BUFFER_POOL_CAST (obj->pool)->max_latency;
808
809 if (num_buffers == 0)
810 max_latency = -1;
811 else
812 max_latency = num_buffers * min_latency;
813
814 GST_DEBUG_OBJECT (bsrc,
815 "report latency min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
816 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
817
818 /* we are always live, the min latency is 1 frame and the max latency is
819 * the complete buffer of frames. */
820 gst_query_set_latency (query, TRUE, min_latency, max_latency);
821
822 res = TRUE;
823 break;
824 }
825 default:
826 res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
827 break;
828 }
829
830 done:
831
832 return res;
833 }
834
835 /* start and stop are not symmetric -- start will open the device, but not start
836 * capture. it's setcaps that will start capture, which is called via basesrc's
837 * negotiate method. stop will both stop capture and close the device.
838 */
839 static gboolean
gst_v4l2src_start(GstBaseSrc * src)840 gst_v4l2src_start (GstBaseSrc * src)
841 {
842 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
843
844 v4l2src->offset = 0;
845 v4l2src->next_offset_same = FALSE;
846 v4l2src->renegotiation_adjust = 0;
847
848 /* activate settings for first frame */
849 v4l2src->ctrl_time = 0;
850 gst_object_sync_values (GST_OBJECT (src), v4l2src->ctrl_time);
851
852 v4l2src->has_bad_timestamp = FALSE;
853 v4l2src->last_timestamp = 0;
854
855 return TRUE;
856 }
857
858 static gboolean
gst_v4l2src_unlock(GstBaseSrc * src)859 gst_v4l2src_unlock (GstBaseSrc * src)
860 {
861 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
862 return gst_v4l2_object_unlock (v4l2src->v4l2object);
863 }
864
865 static gboolean
gst_v4l2src_unlock_stop(GstBaseSrc * src)866 gst_v4l2src_unlock_stop (GstBaseSrc * src)
867 {
868 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
869
870 v4l2src->last_timestamp = 0;
871
872 return gst_v4l2_object_unlock_stop (v4l2src->v4l2object);
873 }
874
875 static gboolean
gst_v4l2src_stop(GstBaseSrc * src)876 gst_v4l2src_stop (GstBaseSrc * src)
877 {
878 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
879 GstV4l2Object *obj = v4l2src->v4l2object;
880
881 if (GST_V4L2_IS_ACTIVE (obj)) {
882 if (!gst_v4l2_object_stop (obj))
883 return FALSE;
884 }
885
886 v4l2src->pending_set_fmt = FALSE;
887
888 return TRUE;
889 }
890
891 static GstStateChangeReturn
gst_v4l2src_change_state(GstElement * element,GstStateChange transition)892 gst_v4l2src_change_state (GstElement * element, GstStateChange transition)
893 {
894 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
895 GstV4l2Src *v4l2src = GST_V4L2SRC (element);
896 GstV4l2Object *obj = v4l2src->v4l2object;
897 GstV4l2Error error = GST_V4L2_ERROR_INIT;
898
899 switch (transition) {
900 case GST_STATE_CHANGE_NULL_TO_READY:
901 /* open the device */
902 if (!gst_v4l2_object_open (obj, &error)) {
903 gst_v4l2_error (v4l2src, &error);
904 return GST_STATE_CHANGE_FAILURE;
905 }
906 break;
907 default:
908 break;
909 }
910
911 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
912
913 switch (transition) {
914 case GST_STATE_CHANGE_READY_TO_NULL:
915 /* close the device */
916 if (!gst_v4l2_object_close (obj))
917 return GST_STATE_CHANGE_FAILURE;
918
919 break;
920 default:
921 break;
922 }
923
924 return ret;
925 }
926
927 static GstFlowReturn
gst_v4l2src_create(GstPushSrc * src,GstBuffer ** buf)928 gst_v4l2src_create (GstPushSrc * src, GstBuffer ** buf)
929 {
930 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
931 GstV4l2Object *obj = v4l2src->v4l2object;
932 GstFlowReturn ret;
933 GstClock *clock;
934 GstClockTime abs_time, base_time, timestamp, duration;
935 GstClockTime delay;
936 GstMessage *qos_msg;
937 gboolean half_frame;
938
939 do {
940 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL_CAST (obj->pool);
941
942 ret = GST_BASE_SRC_CLASS (parent_class)->alloc (GST_BASE_SRC (src), 0,
943 obj->info.size, buf);
944
945 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
946 if (ret == GST_V4L2_FLOW_RESOLUTION_CHANGE) {
947 GST_INFO_OBJECT (v4l2src, "Resolution change detected.");
948
949 /* It is required to always cycle through streamoff, we also need to
950 * streamoff in order to allow locking a new DV_TIMING which will
951 * influence the output of TRY_FMT */
952 gst_v4l2src_stop (GST_BASE_SRC (src));
953
954 /* Force renegotiation */
955 v4l2src->renegotiation_adjust = v4l2src->offset + 1;
956 v4l2src->pending_set_fmt = TRUE;
957
958 if (!gst_base_src_negotiate (GST_BASE_SRC (src))) {
959 ret = GST_FLOW_NOT_NEGOTIATED;
960 goto error;
961 }
962
963 continue;
964 }
965 goto alloc_failed;
966 }
967
968 ret = gst_v4l2_buffer_pool_process (pool, buf, NULL);
969
970 } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER ||
971 ret == GST_V4L2_FLOW_RESOLUTION_CHANGE);
972
973 if (G_UNLIKELY (ret != GST_FLOW_OK))
974 goto error;
975
976 timestamp = GST_BUFFER_TIMESTAMP (*buf);
977 duration = obj->duration;
978
979 /* timestamps, LOCK to get clock and base time. */
980 /* FIXME: element clock and base_time is rarely changing */
981 GST_OBJECT_LOCK (v4l2src);
982 if ((clock = GST_ELEMENT_CLOCK (v4l2src))) {
983 /* we have a clock, get base time and ref clock */
984 base_time = GST_ELEMENT (v4l2src)->base_time;
985 gst_object_ref (clock);
986 } else {
987 /* no clock, can't set timestamps */
988 base_time = GST_CLOCK_TIME_NONE;
989 }
990 GST_OBJECT_UNLOCK (v4l2src);
991
992 /* sample pipeline clock */
993 if (clock) {
994 abs_time = gst_clock_get_time (clock);
995 gst_object_unref (clock);
996 } else {
997 abs_time = GST_CLOCK_TIME_NONE;
998 }
999
1000 retry:
1001 if (!v4l2src->has_bad_timestamp && timestamp != GST_CLOCK_TIME_NONE) {
1002 struct timespec now;
1003 GstClockTime gstnow;
1004
1005 /* v4l2 specs say to use the system time although many drivers switched to
1006 * the more desirable monotonic time. We first try to use the monotonic time
1007 * and see how that goes */
1008 clock_gettime (CLOCK_MONOTONIC, &now);
1009 gstnow = GST_TIMESPEC_TO_TIME (now);
1010
1011 if (timestamp > gstnow || (gstnow - timestamp) > (10 * GST_SECOND)) {
1012 /* very large diff, fall back to system time */
1013 gstnow = g_get_real_time () * GST_USECOND;
1014 }
1015
1016 /* Detect buggy drivers here, and stop using their timestamp. Failing any
1017 * of these condition would imply a very buggy driver:
1018 * - Timestamp in the future
1019 * - Timestamp is going backward compare to last seen timestamp
1020 * - Timestamp is jumping forward for less then a frame duration
1021 * - Delay is bigger then the actual timestamp
1022 * */
1023 if (timestamp > gstnow) {
1024 GST_WARNING_OBJECT (v4l2src,
1025 "Timestamp in the future detected, ignoring driver timestamps");
1026 v4l2src->has_bad_timestamp = TRUE;
1027 goto retry;
1028 }
1029
1030 if (v4l2src->last_timestamp > timestamp) {
1031 GST_WARNING_OBJECT (v4l2src,
1032 "Timestamp going backward, ignoring driver timestamps");
1033 v4l2src->has_bad_timestamp = TRUE;
1034 goto retry;
1035 }
1036
1037 delay = gstnow - timestamp;
1038
1039 if (delay > timestamp) {
1040 GST_WARNING_OBJECT (v4l2src,
1041 "Timestamp does not correlate with any clock, ignoring driver timestamps");
1042 v4l2src->has_bad_timestamp = TRUE;
1043 goto retry;
1044 }
1045
1046 /* Save last timestamp for sanity checks */
1047 v4l2src->last_timestamp = timestamp;
1048
1049 GST_DEBUG_OBJECT (v4l2src, "ts: %" GST_TIME_FORMAT " now %" GST_TIME_FORMAT
1050 " delay %" GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
1051 GST_TIME_ARGS (gstnow), GST_TIME_ARGS (delay));
1052 } else {
1053 /* we assume 1 frame/field latency otherwise */
1054 if (GST_CLOCK_TIME_IS_VALID (duration))
1055 delay = duration;
1056 else
1057 delay = 0;
1058 }
1059
1060 /* set buffer metadata */
1061
1062 if (G_LIKELY (abs_time != GST_CLOCK_TIME_NONE)) {
1063 /* the time now is the time of the clock minus the base time */
1064 timestamp = abs_time - base_time;
1065
1066 /* adjust for delay in the device */
1067 if (timestamp > delay)
1068 timestamp -= delay;
1069 else
1070 timestamp = 0;
1071 } else {
1072 timestamp = GST_CLOCK_TIME_NONE;
1073 }
1074
1075 /* activate settings for next frame */
1076 if (GST_CLOCK_TIME_IS_VALID (duration)) {
1077 v4l2src->ctrl_time += duration;
1078 } else {
1079 /* this is not very good (as it should be the next timestamp),
1080 * still good enough for linear fades (as long as it is not -1)
1081 */
1082 v4l2src->ctrl_time = timestamp;
1083 }
1084 gst_object_sync_values (GST_OBJECT (src), v4l2src->ctrl_time);
1085
1086 GST_LOG_OBJECT (src, "sync to %" GST_TIME_FORMAT " out ts %" GST_TIME_FORMAT,
1087 GST_TIME_ARGS (v4l2src->ctrl_time), GST_TIME_ARGS (timestamp));
1088
1089 if (v4l2src->next_offset_same &&
1090 GST_BUFFER_OFFSET_IS_VALID (*buf) &&
1091 GST_BUFFER_OFFSET (*buf) != v4l2src->offset) {
1092 /* Probably had a lost field then, best to forget about last field. */
1093 GST_WARNING_OBJECT (v4l2src,
1094 "lost field detected - ts: %" GST_TIME_FORMAT,
1095 GST_TIME_ARGS (timestamp));
1096 v4l2src->next_offset_same = FALSE;
1097 }
1098
1099 half_frame = (GST_BUFFER_FLAG_IS_SET (*buf, GST_VIDEO_BUFFER_FLAG_ONEFIELD));
1100 if (half_frame)
1101 v4l2src->next_offset_same = !v4l2src->next_offset_same;
1102
1103 /* use generated offset values only if there are not already valid ones
1104 * set by the v4l2 device */
1105 if (!GST_BUFFER_OFFSET_IS_VALID (*buf)
1106 || !GST_BUFFER_OFFSET_END_IS_VALID (*buf)
1107 || GST_BUFFER_OFFSET (*buf) <=
1108 (v4l2src->offset - v4l2src->renegotiation_adjust)) {
1109 GST_BUFFER_OFFSET (*buf) = v4l2src->offset;
1110 GST_BUFFER_OFFSET_END (*buf) = v4l2src->offset + 1;
1111 if (!half_frame || !v4l2src->next_offset_same)
1112 v4l2src->offset++;
1113 } else {
1114 /* adjust raw v4l2 device sequence, will restart at null in case of renegotiation
1115 * (streamoff/streamon) */
1116 GST_BUFFER_OFFSET (*buf) += v4l2src->renegotiation_adjust;
1117 GST_BUFFER_OFFSET_END (*buf) += v4l2src->renegotiation_adjust;
1118 /* check for frame loss with given (from v4l2 device) buffer offset */
1119 if ((v4l2src->offset != 0)
1120 && (!half_frame || v4l2src->next_offset_same)
1121 && (GST_BUFFER_OFFSET (*buf) != (v4l2src->offset + 1))) {
1122 guint64 lost_frame_count = GST_BUFFER_OFFSET (*buf) - v4l2src->offset - 1;
1123 GST_WARNING_OBJECT (v4l2src,
1124 "lost frames detected: count = %" G_GUINT64_FORMAT " - ts: %"
1125 GST_TIME_FORMAT, lost_frame_count, GST_TIME_ARGS (timestamp));
1126
1127 qos_msg = gst_message_new_qos (GST_OBJECT_CAST (v4l2src), TRUE,
1128 GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE, timestamp,
1129 GST_CLOCK_TIME_IS_VALID (duration) ? lost_frame_count *
1130 duration : GST_CLOCK_TIME_NONE);
1131 gst_element_post_message (GST_ELEMENT_CAST (v4l2src), qos_msg);
1132
1133 }
1134 v4l2src->offset = GST_BUFFER_OFFSET (*buf);
1135 }
1136
1137 GST_BUFFER_TIMESTAMP (*buf) = timestamp;
1138 GST_BUFFER_DURATION (*buf) = duration;
1139
1140 return ret;
1141
1142 /* ERROR */
1143 alloc_failed:
1144 {
1145 if (ret != GST_FLOW_FLUSHING)
1146 GST_ELEMENT_ERROR (src, RESOURCE, NO_SPACE_LEFT,
1147 ("Failed to allocate a buffer"), (NULL));
1148 return ret;
1149 }
1150 error:
1151 {
1152 gst_buffer_replace (buf, NULL);
1153 if (ret == GST_V4L2_FLOW_LAST_BUFFER) {
1154 GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
1155 ("Driver returned a buffer with no payload, this most likely "
1156 "indicate a bug in the driver."), (NULL));
1157 ret = GST_FLOW_ERROR;
1158 } else {
1159 GST_DEBUG_OBJECT (src, "error processing buffer %d (%s)", ret,
1160 gst_flow_get_name (ret));
1161 }
1162 return ret;
1163 }
1164 }
1165
1166
1167 /* GstURIHandler interface */
1168 static GstURIType
gst_v4l2src_uri_get_type(GType type)1169 gst_v4l2src_uri_get_type (GType type)
1170 {
1171 return GST_URI_SRC;
1172 }
1173
1174 static const gchar *const *
gst_v4l2src_uri_get_protocols(GType type)1175 gst_v4l2src_uri_get_protocols (GType type)
1176 {
1177 static const gchar *protocols[] = { "v4l2", NULL };
1178
1179 return protocols;
1180 }
1181
1182 static gchar *
gst_v4l2src_uri_get_uri(GstURIHandler * handler)1183 gst_v4l2src_uri_get_uri (GstURIHandler * handler)
1184 {
1185 GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
1186
1187 if (v4l2src->v4l2object->videodev != NULL) {
1188 return g_strdup_printf ("v4l2://%s", v4l2src->v4l2object->videodev);
1189 }
1190
1191 return g_strdup ("v4l2://");
1192 }
1193
1194 static gboolean
gst_v4l2src_uri_set_uri(GstURIHandler * handler,const gchar * uri,GError ** error)1195 gst_v4l2src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
1196 GError ** error)
1197 {
1198 GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
1199 const gchar *device = DEFAULT_PROP_DEVICE;
1200
1201 if (strcmp (uri, "v4l2://") != 0) {
1202 device = uri + 7;
1203 }
1204 g_object_set (v4l2src, "device", device, NULL);
1205
1206 return TRUE;
1207 }
1208
1209
1210 static void
gst_v4l2src_uri_handler_init(gpointer g_iface,gpointer iface_data)1211 gst_v4l2src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1212 {
1213 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1214
1215 iface->get_type = gst_v4l2src_uri_get_type;
1216 iface->get_protocols = gst_v4l2src_uri_get_protocols;
1217 iface->get_uri = gst_v4l2src_uri_get_uri;
1218 iface->set_uri = gst_v4l2src_uri_set_uri;
1219 }
1220