3 * Copyright (C) 2009 Texas Instruments, Inc - http://www.ti.com/
5 * Description: V4L2 sink element
6 * Created on: Jul 2, 2009
7 * Author: Rob Clark <rob@ti.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 * SECTION:element-v4l2sink
28 * v4l2sink can be used to display video to v4l2 devices (screen overlays
29 * provided by the graphics hardware, tv-out, etc)
32 * <title>Example launch lines</title>
34 * gst-launch videotestsrc ! v4l2sink device=/dev/video1
35 * ]| This pipeline displays a test pattern on /dev/video1
37 * gst-launch -v videotestsrc ! navigationtest ! v4l2sink
38 * ]| A pipeline to test navigation events.
39 * While moving the mouse pointer over the test signal you will see a black box
40 * following the mouse pointer. If you press the mouse button somewhere on the
41 * video and release it somewhere else a green box will appear where you pressed
42 * the button and a red one where you released it. (The navigationtest element
43 * is part of gst-plugins-good.) You can observe here that even if the images
44 * are scaled through hardware the pointer coordinates are converted back to the
45 * original video frame geometry so that the box can be drawn to the correct
46 * position. This also handles borders correctly, limiting coordinates to the
57 #include "gstv4l2colorbalance.h"
58 #include "gstv4l2tuner.h"
60 #include "gstv4l2xoverlay.h"
62 #include "gstv4l2vidorient.h"
64 #include "gstv4l2sink.h"
65 #include "gst/gst-i18n-plugin.h"
69 GST_DEBUG_CATEGORY (v4l2sink_debug);
70 #define GST_CAT_DEFAULT v4l2sink_debug
72 #define PROP_DEF_QUEUE_SIZE 12
73 #define PROP_DEF_MIN_QUEUED_BUFS 1
74 #define DEFAULT_PROP_DEVICE "/dev/video1"
79 V4L2_STD_OBJECT_PROPS,
93 GST_IMPLEMENT_V4L2_PROBE_METHODS (GstV4l2SinkClass, gst_v4l2sink);
94 GST_IMPLEMENT_V4L2_COLOR_BALANCE_METHODS (GstV4l2Sink, gst_v4l2sink);
95 GST_IMPLEMENT_V4L2_TUNER_METHODS (GstV4l2Sink, gst_v4l2sink);
97 GST_IMPLEMENT_V4L2_XOVERLAY_METHODS (GstV4l2Sink, gst_v4l2sink);
99 GST_IMPLEMENT_V4L2_VIDORIENT_METHODS (GstV4l2Sink, gst_v4l2sink);
102 gst_v4l2sink_iface_supported (GstImplementsInterface * iface, GType iface_type)
104 GstV4l2Object *v4l2object = GST_V4L2SINK (iface)->v4l2object;
106 g_assert (iface_type == GST_TYPE_TUNER ||
108 iface_type == GST_TYPE_X_OVERLAY ||
109 iface_type == GST_TYPE_NAVIGATION ||
111 iface_type == GST_TYPE_COLOR_BALANCE ||
112 iface_type == GST_TYPE_VIDEO_ORIENTATION);
114 if (v4l2object->video_fd == -1)
118 if (!GST_V4L2_IS_OVERLAY (v4l2object)) {
119 if (iface_type == GST_TYPE_X_OVERLAY || iface_type == GST_TYPE_NAVIGATION)
128 gst_v4l2sink_interface_init (GstImplementsInterfaceClass * klass)
131 * default virtual functions
133 klass->supported = gst_v4l2sink_iface_supported;
137 static void gst_v4l2sink_navigation_send_event (GstNavigation * navigation,
138 GstStructure * structure);
140 gst_v4l2sink_navigation_init (GstNavigationInterface * iface)
142 iface->send_event = gst_v4l2sink_navigation_send_event;
147 gst_v4l2sink_init_interfaces (GType type)
149 static const GInterfaceInfo v4l2iface_info = {
150 (GInterfaceInitFunc) gst_v4l2sink_interface_init,
154 static const GInterfaceInfo v4l2_tuner_info = {
155 (GInterfaceInitFunc) gst_v4l2sink_tuner_interface_init,
160 static const GInterfaceInfo v4l2_xoverlay_info = {
161 (GInterfaceInitFunc) gst_v4l2sink_xoverlay_interface_init,
165 static const GInterfaceInfo v4l2_navigation_info = {
166 (GInterfaceInitFunc) gst_v4l2sink_navigation_init,
171 static const GInterfaceInfo v4l2_colorbalance_info = {
172 (GInterfaceInitFunc) gst_v4l2sink_color_balance_interface_init,
176 static const GInterfaceInfo v4l2_videoorientation_info = {
177 (GInterfaceInitFunc) gst_v4l2sink_video_orientation_interface_init,
181 static const GInterfaceInfo v4l2_propertyprobe_info = {
182 (GInterfaceInitFunc) gst_v4l2sink_property_probe_interface_init,
187 g_type_add_interface_static (type,
188 GST_TYPE_IMPLEMENTS_INTERFACE, &v4l2iface_info);
189 g_type_add_interface_static (type, GST_TYPE_TUNER, &v4l2_tuner_info);
191 g_type_add_interface_static (type, GST_TYPE_X_OVERLAY, &v4l2_xoverlay_info);
192 g_type_add_interface_static (type,
193 GST_TYPE_NAVIGATION, &v4l2_navigation_info);
195 g_type_add_interface_static (type,
196 GST_TYPE_COLOR_BALANCE, &v4l2_colorbalance_info);
197 g_type_add_interface_static (type,
198 GST_TYPE_VIDEO_ORIENTATION, &v4l2_videoorientation_info);
199 g_type_add_interface_static (type, GST_TYPE_PROPERTY_PROBE,
200 &v4l2_propertyprobe_info);
204 GST_BOILERPLATE_FULL (GstV4l2Sink, gst_v4l2sink, GstVideoSink,
205 GST_TYPE_VIDEO_SINK, gst_v4l2sink_init_interfaces);
208 static void gst_v4l2sink_dispose (GObject * object);
209 static void gst_v4l2sink_finalize (GstV4l2Sink * v4l2sink);
211 /* GObject methods: */
212 static void gst_v4l2sink_set_property (GObject * object, guint prop_id,
213 const GValue * value, GParamSpec * pspec);
214 static void gst_v4l2sink_get_property (GObject * object, guint prop_id,
215 GValue * value, GParamSpec * pspec);
218 /* GstElement methods: */
219 static GstStateChangeReturn gst_v4l2sink_change_state (GstElement * element,
220 GstStateChange transition);
222 /* GstBaseSink methods: */
223 static GstCaps *gst_v4l2sink_get_caps (GstBaseSink * bsink);
224 static gboolean gst_v4l2sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
225 static GstFlowReturn gst_v4l2sink_buffer_alloc (GstBaseSink * bsink,
226 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
227 static GstFlowReturn gst_v4l2sink_show_frame (GstBaseSink * bsink,
231 gst_v4l2sink_base_init (gpointer g_class)
233 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
234 GstV4l2SinkClass *gstv4l2sink_class = GST_V4L2SINK_CLASS (g_class);
236 gstv4l2sink_class->v4l2_class_devices = NULL;
238 GST_DEBUG_CATEGORY_INIT (v4l2sink_debug, "v4l2sink", 0, "V4L2 sink element");
240 gst_element_class_set_details_simple (gstelement_class,
241 "Video (video4linux2) Sink", "Sink/Video",
242 "Displays frames on a video4linux2 device", "Rob Clark <rob@ti.com>,");
244 gst_element_class_add_pad_template
246 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
247 gst_v4l2_object_get_all_caps ()));
251 gst_v4l2sink_class_init (GstV4l2SinkClass * klass)
253 GObjectClass *gobject_class;
254 GstElementClass *element_class;
255 GstBaseSinkClass *basesink_class;
257 gobject_class = G_OBJECT_CLASS (klass);
258 element_class = GST_ELEMENT_CLASS (klass);
259 basesink_class = GST_BASE_SINK_CLASS (klass);
261 gobject_class->dispose = gst_v4l2sink_dispose;
262 gobject_class->finalize = (GObjectFinalizeFunc) gst_v4l2sink_finalize;
263 gobject_class->set_property = gst_v4l2sink_set_property;
264 gobject_class->get_property = gst_v4l2sink_get_property;
266 element_class->change_state = gst_v4l2sink_change_state;
268 gst_v4l2_object_install_properties_helper (gobject_class,
269 DEFAULT_PROP_DEVICE);
270 g_object_class_install_property (gobject_class, PROP_QUEUE_SIZE,
271 g_param_spec_uint ("queue-size", "Queue size",
272 "Number of buffers to be enqueud in the driver in streaming mode",
273 GST_V4L2_MIN_BUFFERS, GST_V4L2_MAX_BUFFERS, PROP_DEF_QUEUE_SIZE,
274 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
275 g_object_class_install_property (gobject_class, PROP_MIN_QUEUED_BUFS,
276 g_param_spec_uint ("min-queued-bufs", "Minimum queued bufs",
277 "Minimum number of queued bufs; v4l2sink won't dqbuf if the driver "
278 "doesn't have more than this number (which normally you shouldn't change)",
279 0, GST_V4L2_MAX_BUFFERS, PROP_DEF_MIN_QUEUED_BUFS,
280 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
281 g_object_class_install_property (gobject_class, PROP_OVERLAY_TOP,
282 g_param_spec_int ("overlay-top", "Overlay top",
283 "The topmost (y) coordinate of the video overlay; top left corner of screen is 0,0",
284 G_MININT, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
285 g_object_class_install_property (gobject_class, PROP_OVERLAY_LEFT,
286 g_param_spec_int ("overlay-left", "Overlay left",
287 "The leftmost (x) coordinate of the video overlay; top left corner of screen is 0,0",
288 G_MININT, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
289 g_object_class_install_property (gobject_class, PROP_OVERLAY_WIDTH,
290 g_param_spec_uint ("overlay-width", "Overlay width",
291 "The width of the video overlay; default is equal to negotiated image width",
292 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
293 g_object_class_install_property (gobject_class, PROP_OVERLAY_HEIGHT,
294 g_param_spec_uint ("overlay-height", "Overlay height",
295 "The height of the video overlay; default is equal to negotiated image height",
296 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
298 g_object_class_install_property (gobject_class, PROP_CROP_TOP,
299 g_param_spec_int ("crop-top", "Crop top",
300 "The topmost (y) coordinate of the video crop; top left corner of image is 0,0",
301 0x80000000, 0x7fffffff, 0, G_PARAM_READWRITE));
302 g_object_class_install_property (gobject_class, PROP_CROP_LEFT,
303 g_param_spec_int ("crop-left", "Crop left",
304 "The leftmost (x) coordinate of the video crop; top left corner of image is 0,0",
305 0x80000000, 0x7fffffff, 0, G_PARAM_READWRITE));
306 g_object_class_install_property (gobject_class, PROP_CROP_WIDTH,
307 g_param_spec_uint ("crop-width", "Crop width",
308 "The width of the video crop; default is equal to negotiated image width",
309 0, 0xffffffff, 0, G_PARAM_READWRITE));
310 g_object_class_install_property (gobject_class, PROP_CROP_HEIGHT,
311 g_param_spec_uint ("crop-height", "Crop height",
312 "The height of the video crop; default is equal to negotiated image height",
313 0, 0xffffffff, 0, G_PARAM_READWRITE));
315 basesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_v4l2sink_get_caps);
316 basesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_v4l2sink_set_caps);
317 basesink_class->buffer_alloc = GST_DEBUG_FUNCPTR (gst_v4l2sink_buffer_alloc);
318 basesink_class->render = GST_DEBUG_FUNCPTR (gst_v4l2sink_show_frame);
322 gst_v4l2sink_init (GstV4l2Sink * v4l2sink, GstV4l2SinkClass * klass)
324 v4l2sink->v4l2object = gst_v4l2_object_new (GST_ELEMENT (v4l2sink),
325 V4L2_BUF_TYPE_VIDEO_OUTPUT, DEFAULT_PROP_DEVICE,
326 gst_v4l2_get_output, gst_v4l2_set_output, NULL);
328 /* same default value for video output device as is used for
329 * v4l2src/capture is no good.. so lets set a saner default
330 * (which can be overridden by the one creating the v4l2sink
331 * after the constructor returns)
333 g_object_set (v4l2sink, "device", "/dev/video1", NULL);
335 /* number of buffers requested */
336 v4l2sink->num_buffers = PROP_DEF_QUEUE_SIZE;
337 v4l2sink->min_queued_bufs = PROP_DEF_MIN_QUEUED_BUFS;
339 v4l2sink->probed_caps = NULL;
340 v4l2sink->current_caps = NULL;
342 v4l2sink->overlay_fields_set = 0;
343 v4l2sink->crop_fields_set = 0;
349 gst_v4l2sink_dispose (GObject * object)
351 GstV4l2Sink *v4l2sink = GST_V4L2SINK (object);
353 if (v4l2sink->probed_caps) {
354 gst_caps_unref (v4l2sink->probed_caps);
357 if (v4l2sink->current_caps) {
358 gst_caps_unref (v4l2sink->current_caps);
361 G_OBJECT_CLASS (parent_class)->dispose (object);
366 gst_v4l2sink_finalize (GstV4l2Sink * v4l2sink)
368 gst_v4l2_object_destroy (v4l2sink->v4l2object);
370 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (v4l2sink));
380 STATE_PENDING_STREAMON,
385 * flags to indicate which overlay/crop properties the user has set (and
386 * therefore which ones should override the defaults from the driver)
391 RECT_LEFT_SET = 0x02,
392 RECT_WIDTH_SET = 0x04,
393 RECT_HEIGHT_SET = 0x08
397 gst_v4l2sink_sync_overlay_fields (GstV4l2Sink * v4l2sink)
399 if (!v4l2sink->overlay_fields_set)
402 if (GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
404 gint fd = v4l2sink->v4l2object->video_fd;
405 struct v4l2_format format;
407 memset (&format, 0x00, sizeof (struct v4l2_format));
408 format.type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
410 g_return_if_fail (v4l2_ioctl (fd, VIDIOC_G_FMT, &format) >= 0);
412 GST_DEBUG_OBJECT (v4l2sink,
413 "setting overlay: overlay_fields_set=0x%02x, top=%d, left=%d, width=%d, height=%d",
414 v4l2sink->overlay_fields_set,
415 v4l2sink->overlay.top, v4l2sink->overlay.left,
416 v4l2sink->overlay.width, v4l2sink->overlay.height);
418 if (v4l2sink->overlay_fields_set & RECT_TOP_SET)
419 format.fmt.win.w.top = v4l2sink->overlay.top;
420 if (v4l2sink->overlay_fields_set & RECT_LEFT_SET)
421 format.fmt.win.w.left = v4l2sink->overlay.left;
422 if (v4l2sink->overlay_fields_set & RECT_WIDTH_SET)
423 format.fmt.win.w.width = v4l2sink->overlay.width;
424 if (v4l2sink->overlay_fields_set & RECT_HEIGHT_SET)
425 format.fmt.win.w.height = v4l2sink->overlay.height;
427 g_return_if_fail (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) >= 0);
429 v4l2sink->overlay_fields_set = 0;
430 v4l2sink->overlay = format.fmt.win.w;
435 gst_v4l2sink_sync_crop_fields (GstV4l2Sink * v4l2sink)
437 if (!v4l2sink->crop_fields_set)
440 if (GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
442 gint fd = v4l2sink->v4l2object->video_fd;
443 struct v4l2_crop crop;
445 memset (&crop, 0x00, sizeof (struct v4l2_crop));
446 crop.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
448 g_return_if_fail (v4l2_ioctl (fd, VIDIOC_G_CROP, &crop) >= 0);
450 GST_DEBUG_OBJECT (v4l2sink,
451 "setting crop: crop_fields_set=0x%02x, top=%d, left=%d, width=%d, height=%d",
452 v4l2sink->crop_fields_set,
453 v4l2sink->crop.top, v4l2sink->crop.left,
454 v4l2sink->crop.width, v4l2sink->crop.height);
456 if (v4l2sink->crop_fields_set & RECT_TOP_SET)
457 crop.c.top = v4l2sink->crop.top;
458 if (v4l2sink->crop_fields_set & RECT_LEFT_SET)
459 crop.c.left = v4l2sink->crop.left;
460 if (v4l2sink->crop_fields_set & RECT_WIDTH_SET)
461 crop.c.width = v4l2sink->crop.width;
462 if (v4l2sink->crop_fields_set & RECT_HEIGHT_SET)
463 crop.c.height = v4l2sink->crop.height;
465 g_return_if_fail (v4l2_ioctl (fd, VIDIOC_S_CROP, &crop) >= 0);
467 v4l2sink->crop_fields_set = 0;
468 v4l2sink->crop = crop.c;
474 gst_v4l2sink_set_property (GObject * object,
475 guint prop_id, const GValue * value, GParamSpec * pspec)
477 GstV4l2Sink *v4l2sink = GST_V4L2SINK (object);
479 if (!gst_v4l2_object_set_property_helper (v4l2sink->v4l2object,
480 prop_id, value, pspec)) {
482 case PROP_QUEUE_SIZE:
483 v4l2sink->num_buffers = g_value_get_uint (value);
485 case PROP_MIN_QUEUED_BUFS:
486 v4l2sink->min_queued_bufs = g_value_get_uint (value);
488 case PROP_OVERLAY_TOP:
489 v4l2sink->overlay.top = g_value_get_int (value);
490 v4l2sink->overlay_fields_set |= RECT_TOP_SET;
491 gst_v4l2sink_sync_overlay_fields (v4l2sink);
493 case PROP_OVERLAY_LEFT:
494 v4l2sink->overlay.left = g_value_get_int (value);
495 v4l2sink->overlay_fields_set |= RECT_LEFT_SET;
496 gst_v4l2sink_sync_overlay_fields (v4l2sink);
498 case PROP_OVERLAY_WIDTH:
499 v4l2sink->overlay.width = g_value_get_uint (value);
500 v4l2sink->overlay_fields_set |= RECT_WIDTH_SET;
501 gst_v4l2sink_sync_overlay_fields (v4l2sink);
503 case PROP_OVERLAY_HEIGHT:
504 v4l2sink->overlay.height = g_value_get_uint (value);
505 v4l2sink->overlay_fields_set |= RECT_HEIGHT_SET;
506 gst_v4l2sink_sync_overlay_fields (v4l2sink);
509 v4l2sink->crop.top = g_value_get_int (value);
510 v4l2sink->crop_fields_set |= RECT_TOP_SET;
511 gst_v4l2sink_sync_crop_fields (v4l2sink);
514 v4l2sink->crop.left = g_value_get_int (value);
515 v4l2sink->crop_fields_set |= RECT_LEFT_SET;
516 gst_v4l2sink_sync_crop_fields (v4l2sink);
518 case PROP_CROP_WIDTH:
519 v4l2sink->crop.width = g_value_get_uint (value);
520 v4l2sink->crop_fields_set |= RECT_WIDTH_SET;
521 gst_v4l2sink_sync_crop_fields (v4l2sink);
523 case PROP_CROP_HEIGHT:
524 v4l2sink->crop.height = g_value_get_uint (value);
525 v4l2sink->crop_fields_set |= RECT_HEIGHT_SET;
526 gst_v4l2sink_sync_crop_fields (v4l2sink);
529 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
537 gst_v4l2sink_get_property (GObject * object,
538 guint prop_id, GValue * value, GParamSpec * pspec)
540 GstV4l2Sink *v4l2sink = GST_V4L2SINK (object);
542 if (!gst_v4l2_object_get_property_helper (v4l2sink->v4l2object,
543 prop_id, value, pspec)) {
545 case PROP_QUEUE_SIZE:
546 g_value_set_uint (value, v4l2sink->num_buffers);
548 case PROP_MIN_QUEUED_BUFS:
549 g_value_set_uint (value, v4l2sink->min_queued_bufs);
551 case PROP_OVERLAY_TOP:
552 g_value_set_int (value, v4l2sink->overlay.top);
554 case PROP_OVERLAY_LEFT:
555 g_value_set_int (value, v4l2sink->overlay.left);
557 case PROP_OVERLAY_WIDTH:
558 g_value_set_uint (value, v4l2sink->overlay.width);
560 case PROP_OVERLAY_HEIGHT:
561 g_value_set_uint (value, v4l2sink->overlay.height);
564 g_value_set_int (value, v4l2sink->crop.top);
567 g_value_set_int (value, v4l2sink->crop.left);
569 case PROP_CROP_WIDTH:
570 g_value_set_uint (value, v4l2sink->crop.width);
572 case PROP_CROP_HEIGHT:
573 g_value_set_uint (value, v4l2sink->crop.height);
576 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
582 static GstStateChangeReturn
583 gst_v4l2sink_change_state (GstElement * element, GstStateChange transition)
585 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
586 GstV4l2Sink *v4l2sink = GST_V4L2SINK (element);
588 GST_DEBUG_OBJECT (v4l2sink, "%d -> %d",
589 GST_STATE_TRANSITION_CURRENT (transition),
590 GST_STATE_TRANSITION_NEXT (transition));
592 switch (transition) {
593 case GST_STATE_CHANGE_NULL_TO_READY:
594 /* open the device */
595 if (!gst_v4l2_object_start (v4l2sink->v4l2object))
596 return GST_STATE_CHANGE_FAILURE;
602 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
604 switch (transition) {
605 case GST_STATE_CHANGE_PAUSED_TO_READY:
606 if (v4l2sink->state == STATE_STREAMING) {
607 if (!gst_v4l2_object_stop_streaming (v4l2sink->v4l2object)) {
608 return GST_STATE_CHANGE_FAILURE;
610 v4l2sink->state = STATE_PENDING_STREAMON;
613 case GST_STATE_CHANGE_READY_TO_NULL:
614 if (NULL != v4l2sink->pool)
615 gst_v4l2_buffer_pool_destroy (v4l2sink->pool);
616 v4l2sink->pool = NULL;
617 /* close the device */
618 if (!gst_v4l2_object_stop (v4l2sink->v4l2object))
619 return GST_STATE_CHANGE_FAILURE;
620 v4l2sink->state = STATE_OFF;
631 gst_v4l2sink_get_caps (GstBaseSink * bsink)
633 GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
638 if (!GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
640 GST_DEBUG_OBJECT (v4l2sink, "device is not open");
642 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD
646 if (v4l2sink->probed_caps) {
647 LOG_CAPS (v4l2sink, v4l2sink->probed_caps);
648 return gst_caps_ref (v4l2sink->probed_caps);
651 formats = gst_v4l2_object_get_format_list (v4l2sink->v4l2object);
653 ret = gst_caps_new_empty ();
655 for (walk = v4l2sink->v4l2object->formats; walk; walk = walk->next) {
656 struct v4l2_fmtdesc *format;
658 GstStructure *template;
660 format = (struct v4l2_fmtdesc *) walk->data;
662 template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
668 gst_v4l2_object_probe_caps_for_format (v4l2sink->v4l2object,
669 format->pixelformat, template);
671 gst_caps_append (ret, tmp);
673 gst_structure_free (template);
675 GST_DEBUG_OBJECT (v4l2sink, "unknown format %u", format->pixelformat);
679 v4l2sink->probed_caps = gst_caps_ref (ret);
681 GST_INFO_OBJECT (v4l2sink, "probed caps: %p", ret);
682 LOG_CAPS (v4l2sink, ret);
688 gst_v4l2sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
690 GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
693 struct v4l2_fmtdesc *format;
697 LOG_CAPS (v4l2sink, caps);
699 if (!GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
700 GST_DEBUG_OBJECT (v4l2sink, "device is not open");
704 if (v4l2sink->current_caps) {
705 GST_DEBUG_OBJECT (v4l2sink, "already have caps set.. are they equal?");
706 LOG_CAPS (v4l2sink, v4l2sink->current_caps);
707 if (gst_caps_is_equal (v4l2sink->current_caps, caps)) {
708 GST_DEBUG_OBJECT (v4l2sink, "yes they are!");
711 GST_DEBUG_OBJECT (v4l2sink, "no they aren't!");
714 if (v4l2sink->pool) {
715 /* TODO: if we've already allocated buffers, we probably need to
716 * do something here to free and reallocate....
718 * gst_v4l2_object_stop_streaming()
719 * gst_v4l2_buffer_pool_destroy()
722 GST_DEBUG_OBJECT (v4l2sink, "warning, changing caps not supported yet");
726 /* we want our own v4l2 type of fourcc codes */
727 if (!gst_v4l2_object_get_caps_info (v4l2sink->v4l2object, caps,
728 &format, &w, &h, &interlaced, &fps_n, &fps_d, &size)) {
729 GST_DEBUG_OBJECT (v4l2sink, "can't get capture format from caps %p", caps);
734 GST_DEBUG_OBJECT (v4l2sink, "unrecognized caps!!");
738 if (!gst_v4l2_object_set_format (v4l2sink->v4l2object, format->pixelformat,
740 /* error already posted */
744 v4l2sink->video_width = w;
745 v4l2sink->video_height = h;
747 /* TODO: videosink width/height should be scaled according to
750 GST_VIDEO_SINK_WIDTH (v4l2sink) = w;
751 GST_VIDEO_SINK_HEIGHT (v4l2sink) = h;
753 v4l2sink->current_caps = gst_caps_ref (caps);
758 /* buffer alloc function to implement pad_alloc for upstream element */
760 gst_v4l2sink_buffer_alloc (GstBaseSink * bsink, guint64 offset, guint size,
761 GstCaps * caps, GstBuffer ** buf)
763 GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
764 GstV4l2Buffer *v4l2buf;
766 if (v4l2sink->v4l2object->vcap.capabilities & V4L2_CAP_STREAMING) {
768 /* initialize the buffer pool if not initialized yet (first buffer): */
769 if (G_UNLIKELY (!v4l2sink->pool)) {
771 /* set_caps() might not be called yet.. so just to make sure: */
772 if (!gst_v4l2sink_set_caps (bsink, caps)) {
773 return GST_FLOW_ERROR;
776 GST_V4L2_CHECK_OPEN (v4l2sink->v4l2object);
778 if (!(v4l2sink->pool = gst_v4l2_buffer_pool_new (GST_ELEMENT (v4l2sink),
779 v4l2sink->v4l2object->video_fd,
780 v4l2sink->num_buffers, caps, FALSE,
781 V4L2_BUF_TYPE_VIDEO_OUTPUT))) {
782 return GST_FLOW_ERROR;
785 gst_v4l2sink_sync_overlay_fields (v4l2sink);
786 gst_v4l2sink_sync_crop_fields (v4l2sink);
789 gst_v4l2_xoverlay_prepare_xwindow_id (v4l2sink->v4l2object, TRUE);
792 v4l2sink->state = STATE_PENDING_STREAMON;
794 GST_INFO_OBJECT (v4l2sink, "outputting buffers via mmap()");
796 if (v4l2sink->num_buffers != v4l2sink->pool->buffer_count) {
797 v4l2sink->num_buffers = v4l2sink->pool->buffer_count;
798 g_object_notify (G_OBJECT (v4l2sink), "queue-size");
802 v4l2buf = gst_v4l2_buffer_pool_get (v4l2sink->pool, TRUE);
804 if (G_LIKELY (v4l2buf)) {
805 GST_DEBUG_OBJECT (v4l2sink, "allocated buffer: %p", v4l2buf);
806 *buf = GST_BUFFER (v4l2buf);
809 GST_DEBUG_OBJECT (v4l2sink, "failed to allocate buffer");
810 return GST_FLOW_ERROR;
814 GST_ERROR_OBJECT (v4l2sink, "only supporting streaming mode for now...");
815 return GST_FLOW_ERROR;
819 /* called after A/V sync to render frame */
821 gst_v4l2sink_show_frame (GstBaseSink * bsink, GstBuffer * buf)
823 GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
824 GstBuffer *newbuf = NULL;
826 GST_DEBUG_OBJECT (v4l2sink, "render buffer: %p", buf);
828 if (!GST_IS_V4L2_BUFFER (buf)) {
831 /* special case check for sub-buffers: In certain cases, places like
832 * GstBaseTransform, which might check that the buffer is writable
833 * before copying metadata, timestamp, and such, will find that the
834 * buffer has more than one reference to it. In these cases, they
835 * will create a sub-buffer with an offset=0 and length equal to the
836 * original buffer size.
838 * This could happen in two scenarios: (1) a tee in the pipeline, and
839 * (2) because the refcnt is incremented in gst_mini_object_free()
840 * before the finalize function is called, and decremented after it
841 * returns.. but returning this buffer to the buffer pool in the
842 * finalize function, could wake up a thread blocked in _buffer_alloc()
843 * which could run and get a buffer w/ refcnt==2 before the thread
844 * originally unref'ing the buffer returns from finalize function and
845 * decrements the refcnt back to 1!
848 (GST_BUFFER_DATA (buf) == GST_BUFFER_DATA (buf->parent)) &&
849 (GST_BUFFER_SIZE (buf) == GST_BUFFER_SIZE (buf->parent))) {
850 GST_DEBUG_OBJECT (v4l2sink, "I have a sub-buffer!");
851 return gst_v4l2sink_show_frame (bsink, buf->parent);
854 GST_DEBUG_OBJECT (v4l2sink, "slow-path.. I got a %s so I need to memcpy",
855 g_type_name (G_OBJECT_TYPE (buf)));
857 ret = gst_v4l2sink_buffer_alloc (bsink,
858 GST_BUFFER_OFFSET (buf), GST_BUFFER_SIZE (buf), GST_BUFFER_CAPS (buf),
861 if (GST_FLOW_OK != ret) {
862 GST_DEBUG_OBJECT (v4l2sink,
863 "dropping frame! Consider increasing 'queue-size' property!");
867 memcpy (GST_BUFFER_DATA (newbuf),
868 GST_BUFFER_DATA (buf),
869 MIN (GST_BUFFER_SIZE (newbuf), GST_BUFFER_SIZE (buf)));
871 GST_DEBUG_OBJECT (v4l2sink, "render copied buffer: %p", newbuf);
876 if (!gst_v4l2_buffer_pool_qbuf (v4l2sink->pool, GST_V4L2_BUFFER (buf))) {
877 return GST_FLOW_ERROR;
879 if (v4l2sink->state == STATE_PENDING_STREAMON) {
880 if (!gst_v4l2_object_start_streaming (v4l2sink->v4l2object)) {
881 return GST_FLOW_ERROR;
883 v4l2sink->state = STATE_STREAMING;
887 gst_buffer_ref (buf);
890 /* if the driver has more than one buffer, ie. more than just the one we
891 * just queued, then dequeue one immediately to make it available via
894 if (gst_v4l2_buffer_pool_available_buffers (v4l2sink->pool) >
895 v4l2sink->min_queued_bufs) {
896 GstV4l2Buffer *v4l2buf = gst_v4l2_buffer_pool_dqbuf (v4l2sink->pool);
898 /* note: if we get a buf, we don't want to use it directly (because
899 * someone else could still hold a ref).. but instead we release our
900 * reference to it, and if no one else holds a ref it will be returned
901 * to the pool of available buffers.. and if not, we keep looping.
904 gst_buffer_unref (GST_BUFFER (v4l2buf));
913 gst_v4l2sink_navigation_send_event (GstNavigation * navigation,
914 GstStructure * structure)
916 GstV4l2Sink *v4l2sink = GST_V4L2SINK (navigation);
917 GstV4l2Xv *xv = v4l2sink->v4l2object->xv;
923 if ((peer = gst_pad_get_peer (GST_VIDEO_SINK_PAD (v4l2sink)))) {
924 GstVideoRectangle rect;
925 gdouble x, y, xscale = 1.0, yscale = 1.0;
927 gst_v4l2_xoverlay_get_render_rect (v4l2sink->v4l2object, &rect);
929 /* We calculate scaling using the original video frames geometry to
930 * include pixel aspect ratio scaling.
932 xscale = (gdouble) v4l2sink->video_width / rect.w;
933 yscale = (gdouble) v4l2sink->video_height / rect.h;
935 /* Converting pointer coordinates to the non scaled geometry */
936 if (gst_structure_get_double (structure, "pointer_x", &x)) {
937 x = MIN (x, rect.x + rect.w);
938 x = MAX (x - rect.x, 0);
939 gst_structure_set (structure, "pointer_x", G_TYPE_DOUBLE,
940 (gdouble) x * xscale, NULL);
942 if (gst_structure_get_double (structure, "pointer_y", &y)) {
943 y = MIN (y, rect.y + rect.h);
944 y = MAX (y - rect.y, 0);
945 gst_structure_set (structure, "pointer_y", G_TYPE_DOUBLE,
946 (gdouble) y * yscale, NULL);
949 gst_pad_send_event (peer, gst_event_new_navigation (structure));
950 gst_object_unref (peer);