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;
107 g_assert (iface_type == GST_TYPE_X_OVERLAY ||
108 iface_type == GST_TYPE_NAVIGATION ||
109 iface_type == GST_TYPE_COLOR_BALANCE ||
110 iface_type == GST_TYPE_VIDEO_ORIENTATION ||
111 iface_type == GST_TYPE_TUNER);
113 g_assert (iface_type == GST_TYPE_COLOR_BALANCE ||
114 iface_type == GST_TYPE_VIDEO_ORIENTATION ||
115 iface_type == GST_TYPE_TUNER);
118 if (v4l2object->video_fd == -1)
122 if (!GST_V4L2_IS_OVERLAY (v4l2object)) {
123 if (iface_type == GST_TYPE_X_OVERLAY || iface_type == GST_TYPE_NAVIGATION)
132 gst_v4l2sink_interface_init (GstImplementsInterfaceClass * klass)
135 * default virtual functions
137 klass->supported = gst_v4l2sink_iface_supported;
141 static void gst_v4l2sink_navigation_send_event (GstNavigation * navigation,
142 GstStructure * structure);
144 gst_v4l2sink_navigation_init (GstNavigationInterface * iface)
146 iface->send_event = gst_v4l2sink_navigation_send_event;
151 gst_v4l2sink_init_interfaces (GType type)
153 static const GInterfaceInfo v4l2iface_info = {
154 (GInterfaceInitFunc) gst_v4l2sink_interface_init,
158 static const GInterfaceInfo v4l2_tuner_info = {
159 (GInterfaceInitFunc) gst_v4l2sink_tuner_interface_init,
164 static const GInterfaceInfo v4l2_xoverlay_info = {
165 (GInterfaceInitFunc) gst_v4l2sink_xoverlay_interface_init,
169 static const GInterfaceInfo v4l2_navigation_info = {
170 (GInterfaceInitFunc) gst_v4l2sink_navigation_init,
175 static const GInterfaceInfo v4l2_colorbalance_info = {
176 (GInterfaceInitFunc) gst_v4l2sink_color_balance_interface_init,
180 static const GInterfaceInfo v4l2_videoorientation_info = {
181 (GInterfaceInitFunc) gst_v4l2sink_video_orientation_interface_init,
185 static const GInterfaceInfo v4l2_propertyprobe_info = {
186 (GInterfaceInitFunc) gst_v4l2sink_property_probe_interface_init,
191 g_type_add_interface_static (type,
192 GST_TYPE_IMPLEMENTS_INTERFACE, &v4l2iface_info);
193 g_type_add_interface_static (type, GST_TYPE_TUNER, &v4l2_tuner_info);
195 g_type_add_interface_static (type, GST_TYPE_X_OVERLAY, &v4l2_xoverlay_info);
196 g_type_add_interface_static (type,
197 GST_TYPE_NAVIGATION, &v4l2_navigation_info);
199 g_type_add_interface_static (type,
200 GST_TYPE_COLOR_BALANCE, &v4l2_colorbalance_info);
201 g_type_add_interface_static (type,
202 GST_TYPE_VIDEO_ORIENTATION, &v4l2_videoorientation_info);
203 g_type_add_interface_static (type, GST_TYPE_PROPERTY_PROBE,
204 &v4l2_propertyprobe_info);
208 GST_BOILERPLATE_FULL (GstV4l2Sink, gst_v4l2sink, GstVideoSink,
209 GST_TYPE_VIDEO_SINK, gst_v4l2sink_init_interfaces);
212 static void gst_v4l2sink_dispose (GObject * object);
213 static void gst_v4l2sink_finalize (GstV4l2Sink * v4l2sink);
215 /* GObject methods: */
216 static void gst_v4l2sink_set_property (GObject * object, guint prop_id,
217 const GValue * value, GParamSpec * pspec);
218 static void gst_v4l2sink_get_property (GObject * object, guint prop_id,
219 GValue * value, GParamSpec * pspec);
222 /* GstElement methods: */
223 static GstStateChangeReturn gst_v4l2sink_change_state (GstElement * element,
224 GstStateChange transition);
226 /* GstBaseSink methods: */
227 static GstCaps *gst_v4l2sink_get_caps (GstBaseSink * bsink);
228 static gboolean gst_v4l2sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
229 static GstFlowReturn gst_v4l2sink_buffer_alloc (GstBaseSink * bsink,
230 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
231 static GstFlowReturn gst_v4l2sink_show_frame (GstBaseSink * bsink,
235 gst_v4l2sink_base_init (gpointer g_class)
237 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
238 GstV4l2SinkClass *gstv4l2sink_class = GST_V4L2SINK_CLASS (g_class);
239 GstPadTemplate *pad_template;
241 gstv4l2sink_class->v4l2_class_devices = NULL;
243 GST_DEBUG_CATEGORY_INIT (v4l2sink_debug, "v4l2sink", 0, "V4L2 sink element");
245 gst_element_class_set_details_simple (gstelement_class,
246 "Video (video4linux2) Sink", "Sink/Video",
247 "Displays frames on a video4linux2 device", "Rob Clark <rob@ti.com>,");
250 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
251 gst_v4l2_object_get_all_caps ());
252 gst_element_class_add_pad_template (gstelement_class, pad_template);
253 gst_object_unref (pad_template);
257 gst_v4l2sink_class_init (GstV4l2SinkClass * klass)
259 GObjectClass *gobject_class;
260 GstElementClass *element_class;
261 GstBaseSinkClass *basesink_class;
263 gobject_class = G_OBJECT_CLASS (klass);
264 element_class = GST_ELEMENT_CLASS (klass);
265 basesink_class = GST_BASE_SINK_CLASS (klass);
267 gobject_class->dispose = gst_v4l2sink_dispose;
268 gobject_class->finalize = (GObjectFinalizeFunc) gst_v4l2sink_finalize;
269 gobject_class->set_property = gst_v4l2sink_set_property;
270 gobject_class->get_property = gst_v4l2sink_get_property;
272 element_class->change_state = gst_v4l2sink_change_state;
274 gst_v4l2_object_install_properties_helper (gobject_class,
275 DEFAULT_PROP_DEVICE);
276 g_object_class_install_property (gobject_class, PROP_QUEUE_SIZE,
277 g_param_spec_uint ("queue-size", "Queue size",
278 "Number of buffers to be enqueud in the driver in streaming mode",
279 GST_V4L2_MIN_BUFFERS, GST_V4L2_MAX_BUFFERS, PROP_DEF_QUEUE_SIZE,
280 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
281 g_object_class_install_property (gobject_class, PROP_MIN_QUEUED_BUFS,
282 g_param_spec_uint ("min-queued-bufs", "Minimum queued bufs",
283 "Minimum number of queued bufs; v4l2sink won't dqbuf if the driver "
284 "doesn't have more than this number (which normally you shouldn't change)",
285 0, GST_V4L2_MAX_BUFFERS, PROP_DEF_MIN_QUEUED_BUFS,
286 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
287 g_object_class_install_property (gobject_class, PROP_OVERLAY_TOP,
288 g_param_spec_int ("overlay-top", "Overlay top",
289 "The topmost (y) coordinate of the video overlay; top left corner of screen is 0,0",
290 G_MININT, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
291 g_object_class_install_property (gobject_class, PROP_OVERLAY_LEFT,
292 g_param_spec_int ("overlay-left", "Overlay left",
293 "The leftmost (x) coordinate of the video overlay; top left corner of screen is 0,0",
294 G_MININT, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
295 g_object_class_install_property (gobject_class, PROP_OVERLAY_WIDTH,
296 g_param_spec_uint ("overlay-width", "Overlay width",
297 "The width of the video overlay; default is equal to negotiated image width",
298 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
299 g_object_class_install_property (gobject_class, PROP_OVERLAY_HEIGHT,
300 g_param_spec_uint ("overlay-height", "Overlay height",
301 "The height of the video overlay; default is equal to negotiated image height",
302 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
304 g_object_class_install_property (gobject_class, PROP_CROP_TOP,
305 g_param_spec_int ("crop-top", "Crop top",
306 "The topmost (y) coordinate of the video crop; top left corner of image is 0,0",
307 0x80000000, 0x7fffffff, 0, G_PARAM_READWRITE));
308 g_object_class_install_property (gobject_class, PROP_CROP_LEFT,
309 g_param_spec_int ("crop-left", "Crop left",
310 "The leftmost (x) coordinate of the video crop; top left corner of image is 0,0",
311 0x80000000, 0x7fffffff, 0, G_PARAM_READWRITE));
312 g_object_class_install_property (gobject_class, PROP_CROP_WIDTH,
313 g_param_spec_uint ("crop-width", "Crop width",
314 "The width of the video crop; default is equal to negotiated image width",
315 0, 0xffffffff, 0, G_PARAM_READWRITE));
316 g_object_class_install_property (gobject_class, PROP_CROP_HEIGHT,
317 g_param_spec_uint ("crop-height", "Crop height",
318 "The height of the video crop; default is equal to negotiated image height",
319 0, 0xffffffff, 0, G_PARAM_READWRITE));
321 basesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_v4l2sink_get_caps);
322 basesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_v4l2sink_set_caps);
323 basesink_class->buffer_alloc = GST_DEBUG_FUNCPTR (gst_v4l2sink_buffer_alloc);
324 basesink_class->render = GST_DEBUG_FUNCPTR (gst_v4l2sink_show_frame);
328 gst_v4l2sink_init (GstV4l2Sink * v4l2sink, GstV4l2SinkClass * klass)
330 v4l2sink->v4l2object = gst_v4l2_object_new (GST_ELEMENT (v4l2sink),
331 V4L2_BUF_TYPE_VIDEO_OUTPUT, DEFAULT_PROP_DEVICE,
332 gst_v4l2_get_output, gst_v4l2_set_output, NULL);
334 /* same default value for video output device as is used for
335 * v4l2src/capture is no good.. so lets set a saner default
336 * (which can be overridden by the one creating the v4l2sink
337 * after the constructor returns)
339 g_object_set (v4l2sink, "device", "/dev/video1", NULL);
341 /* number of buffers requested */
342 v4l2sink->num_buffers = PROP_DEF_QUEUE_SIZE;
343 v4l2sink->min_queued_bufs = PROP_DEF_MIN_QUEUED_BUFS;
345 v4l2sink->probed_caps = NULL;
346 v4l2sink->current_caps = NULL;
348 v4l2sink->overlay_fields_set = 0;
349 v4l2sink->crop_fields_set = 0;
355 gst_v4l2sink_dispose (GObject * object)
357 GstV4l2Sink *v4l2sink = GST_V4L2SINK (object);
359 if (v4l2sink->probed_caps) {
360 gst_caps_unref (v4l2sink->probed_caps);
363 if (v4l2sink->current_caps) {
364 gst_caps_unref (v4l2sink->current_caps);
367 G_OBJECT_CLASS (parent_class)->dispose (object);
372 gst_v4l2sink_finalize (GstV4l2Sink * v4l2sink)
374 gst_v4l2_object_destroy (v4l2sink->v4l2object);
376 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (v4l2sink));
386 STATE_PENDING_STREAMON,
391 * flags to indicate which overlay/crop properties the user has set (and
392 * therefore which ones should override the defaults from the driver)
397 RECT_LEFT_SET = 0x02,
398 RECT_WIDTH_SET = 0x04,
399 RECT_HEIGHT_SET = 0x08
403 gst_v4l2sink_sync_overlay_fields (GstV4l2Sink * v4l2sink)
405 if (!v4l2sink->overlay_fields_set)
408 if (GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
410 gint fd = v4l2sink->v4l2object->video_fd;
411 struct v4l2_format format;
413 memset (&format, 0x00, sizeof (struct v4l2_format));
414 format.type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
416 if (v4l2_ioctl (fd, VIDIOC_G_FMT, &format) < 0) {
417 GST_WARNING_OBJECT (v4l2sink, "VIDIOC_G_FMT failed");
421 GST_DEBUG_OBJECT (v4l2sink,
422 "setting overlay: overlay_fields_set=0x%02x, top=%d, left=%d, width=%d, height=%d",
423 v4l2sink->overlay_fields_set,
424 v4l2sink->overlay.top, v4l2sink->overlay.left,
425 v4l2sink->overlay.width, v4l2sink->overlay.height);
427 if (v4l2sink->overlay_fields_set & RECT_TOP_SET)
428 format.fmt.win.w.top = v4l2sink->overlay.top;
429 if (v4l2sink->overlay_fields_set & RECT_LEFT_SET)
430 format.fmt.win.w.left = v4l2sink->overlay.left;
431 if (v4l2sink->overlay_fields_set & RECT_WIDTH_SET)
432 format.fmt.win.w.width = v4l2sink->overlay.width;
433 if (v4l2sink->overlay_fields_set & RECT_HEIGHT_SET)
434 format.fmt.win.w.height = v4l2sink->overlay.height;
436 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0) {
437 GST_WARNING_OBJECT (v4l2sink, "VIDIOC_S_FMT failed");
441 v4l2sink->overlay_fields_set = 0;
442 v4l2sink->overlay = format.fmt.win.w;
447 gst_v4l2sink_sync_crop_fields (GstV4l2Sink * v4l2sink)
449 if (!v4l2sink->crop_fields_set)
452 if (GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
454 gint fd = v4l2sink->v4l2object->video_fd;
455 struct v4l2_crop crop;
457 memset (&crop, 0x00, sizeof (struct v4l2_crop));
458 crop.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
460 if (v4l2_ioctl (fd, VIDIOC_G_CROP, &crop) < 0) {
461 GST_WARNING_OBJECT (v4l2sink, "VIDIOC_G_CROP failed");
465 GST_DEBUG_OBJECT (v4l2sink,
466 "setting crop: crop_fields_set=0x%02x, top=%d, left=%d, width=%d, height=%d",
467 v4l2sink->crop_fields_set,
468 v4l2sink->crop.top, v4l2sink->crop.left,
469 v4l2sink->crop.width, v4l2sink->crop.height);
471 if (v4l2sink->crop_fields_set & RECT_TOP_SET)
472 crop.c.top = v4l2sink->crop.top;
473 if (v4l2sink->crop_fields_set & RECT_LEFT_SET)
474 crop.c.left = v4l2sink->crop.left;
475 if (v4l2sink->crop_fields_set & RECT_WIDTH_SET)
476 crop.c.width = v4l2sink->crop.width;
477 if (v4l2sink->crop_fields_set & RECT_HEIGHT_SET)
478 crop.c.height = v4l2sink->crop.height;
480 if (v4l2_ioctl (fd, VIDIOC_S_CROP, &crop) < 0) {
481 GST_WARNING_OBJECT (v4l2sink, "VIDIOC_S_CROP failed");
485 v4l2sink->crop_fields_set = 0;
486 v4l2sink->crop = crop.c;
492 gst_v4l2sink_set_property (GObject * object,
493 guint prop_id, const GValue * value, GParamSpec * pspec)
495 GstV4l2Sink *v4l2sink = GST_V4L2SINK (object);
497 if (!gst_v4l2_object_set_property_helper (v4l2sink->v4l2object,
498 prop_id, value, pspec)) {
500 case PROP_QUEUE_SIZE:
501 v4l2sink->num_buffers = g_value_get_uint (value);
503 case PROP_MIN_QUEUED_BUFS:
504 v4l2sink->min_queued_bufs = g_value_get_uint (value);
506 case PROP_OVERLAY_TOP:
507 v4l2sink->overlay.top = g_value_get_int (value);
508 v4l2sink->overlay_fields_set |= RECT_TOP_SET;
509 gst_v4l2sink_sync_overlay_fields (v4l2sink);
511 case PROP_OVERLAY_LEFT:
512 v4l2sink->overlay.left = g_value_get_int (value);
513 v4l2sink->overlay_fields_set |= RECT_LEFT_SET;
514 gst_v4l2sink_sync_overlay_fields (v4l2sink);
516 case PROP_OVERLAY_WIDTH:
517 v4l2sink->overlay.width = g_value_get_uint (value);
518 v4l2sink->overlay_fields_set |= RECT_WIDTH_SET;
519 gst_v4l2sink_sync_overlay_fields (v4l2sink);
521 case PROP_OVERLAY_HEIGHT:
522 v4l2sink->overlay.height = g_value_get_uint (value);
523 v4l2sink->overlay_fields_set |= RECT_HEIGHT_SET;
524 gst_v4l2sink_sync_overlay_fields (v4l2sink);
527 v4l2sink->crop.top = g_value_get_int (value);
528 v4l2sink->crop_fields_set |= RECT_TOP_SET;
529 gst_v4l2sink_sync_crop_fields (v4l2sink);
532 v4l2sink->crop.left = g_value_get_int (value);
533 v4l2sink->crop_fields_set |= RECT_LEFT_SET;
534 gst_v4l2sink_sync_crop_fields (v4l2sink);
536 case PROP_CROP_WIDTH:
537 v4l2sink->crop.width = g_value_get_uint (value);
538 v4l2sink->crop_fields_set |= RECT_WIDTH_SET;
539 gst_v4l2sink_sync_crop_fields (v4l2sink);
541 case PROP_CROP_HEIGHT:
542 v4l2sink->crop.height = g_value_get_uint (value);
543 v4l2sink->crop_fields_set |= RECT_HEIGHT_SET;
544 gst_v4l2sink_sync_crop_fields (v4l2sink);
547 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
555 gst_v4l2sink_get_property (GObject * object,
556 guint prop_id, GValue * value, GParamSpec * pspec)
558 GstV4l2Sink *v4l2sink = GST_V4L2SINK (object);
560 if (!gst_v4l2_object_get_property_helper (v4l2sink->v4l2object,
561 prop_id, value, pspec)) {
563 case PROP_QUEUE_SIZE:
564 g_value_set_uint (value, v4l2sink->num_buffers);
566 case PROP_MIN_QUEUED_BUFS:
567 g_value_set_uint (value, v4l2sink->min_queued_bufs);
569 case PROP_OVERLAY_TOP:
570 g_value_set_int (value, v4l2sink->overlay.top);
572 case PROP_OVERLAY_LEFT:
573 g_value_set_int (value, v4l2sink->overlay.left);
575 case PROP_OVERLAY_WIDTH:
576 g_value_set_uint (value, v4l2sink->overlay.width);
578 case PROP_OVERLAY_HEIGHT:
579 g_value_set_uint (value, v4l2sink->overlay.height);
582 g_value_set_int (value, v4l2sink->crop.top);
585 g_value_set_int (value, v4l2sink->crop.left);
587 case PROP_CROP_WIDTH:
588 g_value_set_uint (value, v4l2sink->crop.width);
590 case PROP_CROP_HEIGHT:
591 g_value_set_uint (value, v4l2sink->crop.height);
594 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
600 static GstStateChangeReturn
601 gst_v4l2sink_change_state (GstElement * element, GstStateChange transition)
603 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
604 GstV4l2Sink *v4l2sink = GST_V4L2SINK (element);
606 GST_DEBUG_OBJECT (v4l2sink, "%d -> %d",
607 GST_STATE_TRANSITION_CURRENT (transition),
608 GST_STATE_TRANSITION_NEXT (transition));
610 switch (transition) {
611 case GST_STATE_CHANGE_NULL_TO_READY:
612 /* open the device */
613 if (!gst_v4l2_object_start (v4l2sink->v4l2object))
614 return GST_STATE_CHANGE_FAILURE;
620 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
622 switch (transition) {
623 case GST_STATE_CHANGE_PAUSED_TO_READY:
624 if (v4l2sink->state == STATE_STREAMING) {
625 if (!gst_v4l2_object_stop_streaming (v4l2sink->v4l2object)) {
626 return GST_STATE_CHANGE_FAILURE;
628 v4l2sink->state = STATE_PENDING_STREAMON;
631 case GST_STATE_CHANGE_READY_TO_NULL:
632 if (NULL != v4l2sink->pool)
633 gst_v4l2_buffer_pool_destroy (v4l2sink->pool);
634 v4l2sink->pool = NULL;
635 /* close the device */
636 if (!gst_v4l2_object_stop (v4l2sink->v4l2object))
637 return GST_STATE_CHANGE_FAILURE;
638 v4l2sink->state = STATE_OFF;
649 gst_v4l2sink_get_caps (GstBaseSink * bsink)
651 GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
656 if (!GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
658 GST_DEBUG_OBJECT (v4l2sink, "device is not open");
660 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD
664 if (v4l2sink->probed_caps) {
665 LOG_CAPS (v4l2sink, v4l2sink->probed_caps);
666 return gst_caps_ref (v4l2sink->probed_caps);
669 formats = gst_v4l2_object_get_format_list (v4l2sink->v4l2object);
671 ret = gst_caps_new_empty ();
673 for (walk = formats; walk; walk = walk->next) {
674 struct v4l2_fmtdesc *format;
676 GstStructure *template;
678 format = (struct v4l2_fmtdesc *) walk->data;
680 template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
686 gst_v4l2_object_probe_caps_for_format (v4l2sink->v4l2object,
687 format->pixelformat, template);
689 gst_caps_append (ret, tmp);
691 gst_structure_free (template);
693 GST_DEBUG_OBJECT (v4l2sink, "unknown format %u", format->pixelformat);
697 v4l2sink->probed_caps = gst_caps_ref (ret);
699 GST_INFO_OBJECT (v4l2sink, "probed caps: %p", ret);
700 LOG_CAPS (v4l2sink, ret);
706 gst_v4l2sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
708 GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
711 struct v4l2_fmtdesc *format;
715 LOG_CAPS (v4l2sink, caps);
717 if (!GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
718 GST_DEBUG_OBJECT (v4l2sink, "device is not open");
722 if (v4l2sink->current_caps) {
723 GST_DEBUG_OBJECT (v4l2sink, "already have caps set.. are they equal?");
724 LOG_CAPS (v4l2sink, v4l2sink->current_caps);
725 if (gst_caps_is_equal (v4l2sink->current_caps, caps)) {
726 GST_DEBUG_OBJECT (v4l2sink, "yes they are!");
729 GST_DEBUG_OBJECT (v4l2sink, "no they aren't!");
732 if (v4l2sink->pool) {
733 /* TODO: if we've already allocated buffers, we probably need to
734 * do something here to free and reallocate....
736 * gst_v4l2_object_stop_streaming()
737 * gst_v4l2_buffer_pool_destroy()
740 GST_DEBUG_OBJECT (v4l2sink, "warning, changing caps not supported yet");
744 /* we want our own v4l2 type of fourcc codes */
745 if (!gst_v4l2_object_get_caps_info (v4l2sink->v4l2object, caps,
746 &format, &w, &h, &interlaced, &fps_n, &fps_d, &size)) {
747 GST_DEBUG_OBJECT (v4l2sink, "can't get capture format from caps %p", caps);
752 GST_DEBUG_OBJECT (v4l2sink, "unrecognized caps!!");
756 if (!gst_v4l2_object_set_format (v4l2sink->v4l2object, format->pixelformat,
758 /* error already posted */
762 v4l2sink->video_width = w;
763 v4l2sink->video_height = h;
765 /* TODO: videosink width/height should be scaled according to
768 GST_VIDEO_SINK_WIDTH (v4l2sink) = w;
769 GST_VIDEO_SINK_HEIGHT (v4l2sink) = h;
771 v4l2sink->current_caps = gst_caps_ref (caps);
776 /* buffer alloc function to implement pad_alloc for upstream element */
778 gst_v4l2sink_buffer_alloc (GstBaseSink * bsink, guint64 offset, guint size,
779 GstCaps * caps, GstBuffer ** buf)
781 GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
782 GstV4l2Buffer *v4l2buf;
784 if (v4l2sink->v4l2object->vcap.capabilities & V4L2_CAP_STREAMING) {
786 /* initialize the buffer pool if not initialized yet (first buffer): */
787 if (G_UNLIKELY (!v4l2sink->pool)) {
789 /* set_caps() might not be called yet.. so just to make sure: */
790 if (!gst_v4l2sink_set_caps (bsink, caps)) {
791 return GST_FLOW_ERROR;
794 GST_V4L2_CHECK_OPEN (v4l2sink->v4l2object);
796 if (!(v4l2sink->pool = gst_v4l2_buffer_pool_new (GST_ELEMENT (v4l2sink),
797 v4l2sink->v4l2object->video_fd,
798 v4l2sink->num_buffers, caps, FALSE,
799 V4L2_BUF_TYPE_VIDEO_OUTPUT))) {
800 return GST_FLOW_ERROR;
803 gst_v4l2sink_sync_overlay_fields (v4l2sink);
804 gst_v4l2sink_sync_crop_fields (v4l2sink);
807 gst_v4l2_xoverlay_prepare_xwindow_id (v4l2sink->v4l2object, TRUE);
810 v4l2sink->state = STATE_PENDING_STREAMON;
812 GST_INFO_OBJECT (v4l2sink, "outputting buffers via mmap()");
814 if (v4l2sink->num_buffers != v4l2sink->pool->buffer_count) {
815 v4l2sink->num_buffers = v4l2sink->pool->buffer_count;
816 g_object_notify (G_OBJECT (v4l2sink), "queue-size");
820 v4l2buf = gst_v4l2_buffer_pool_get (v4l2sink->pool, TRUE);
822 if (G_LIKELY (v4l2buf)) {
823 GST_DEBUG_OBJECT (v4l2sink, "allocated buffer: %p", v4l2buf);
824 *buf = GST_BUFFER (v4l2buf);
827 GST_DEBUG_OBJECT (v4l2sink, "failed to allocate buffer");
828 return GST_FLOW_ERROR;
832 GST_ERROR_OBJECT (v4l2sink, "only supporting streaming mode for now...");
833 return GST_FLOW_ERROR;
837 /* called after A/V sync to render frame */
839 gst_v4l2sink_show_frame (GstBaseSink * bsink, GstBuffer * buf)
841 GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
842 GstBuffer *newbuf = NULL;
844 GST_DEBUG_OBJECT (v4l2sink, "render buffer: %p", buf);
846 if (!GST_IS_V4L2_BUFFER (buf)) {
849 /* special case check for sub-buffers: In certain cases, places like
850 * GstBaseTransform, which might check that the buffer is writable
851 * before copying metadata, timestamp, and such, will find that the
852 * buffer has more than one reference to it. In these cases, they
853 * will create a sub-buffer with an offset=0 and length equal to the
854 * original buffer size.
856 * This could happen in two scenarios: (1) a tee in the pipeline, and
857 * (2) because the refcnt is incremented in gst_mini_object_free()
858 * before the finalize function is called, and decremented after it
859 * returns.. but returning this buffer to the buffer pool in the
860 * finalize function, could wake up a thread blocked in _buffer_alloc()
861 * which could run and get a buffer w/ refcnt==2 before the thread
862 * originally unref'ing the buffer returns from finalize function and
863 * decrements the refcnt back to 1!
866 (GST_BUFFER_DATA (buf) == GST_BUFFER_DATA (buf->parent)) &&
867 (GST_BUFFER_SIZE (buf) == GST_BUFFER_SIZE (buf->parent))) {
868 GST_DEBUG_OBJECT (v4l2sink, "I have a sub-buffer!");
869 return gst_v4l2sink_show_frame (bsink, buf->parent);
872 GST_DEBUG_OBJECT (v4l2sink, "slow-path.. I got a %s so I need to memcpy",
873 g_type_name (G_OBJECT_TYPE (buf)));
875 ret = gst_v4l2sink_buffer_alloc (bsink,
876 GST_BUFFER_OFFSET (buf), GST_BUFFER_SIZE (buf), GST_BUFFER_CAPS (buf),
879 if (GST_FLOW_OK != ret) {
880 GST_DEBUG_OBJECT (v4l2sink,
881 "dropping frame! Consider increasing 'queue-size' property!");
885 memcpy (GST_BUFFER_DATA (newbuf),
886 GST_BUFFER_DATA (buf),
887 MIN (GST_BUFFER_SIZE (newbuf), GST_BUFFER_SIZE (buf)));
889 GST_DEBUG_OBJECT (v4l2sink, "render copied buffer: %p", newbuf);
894 if (!gst_v4l2_buffer_pool_qbuf (v4l2sink->pool, GST_V4L2_BUFFER (buf))) {
895 return GST_FLOW_ERROR;
897 if (v4l2sink->state == STATE_PENDING_STREAMON) {
898 if (!gst_v4l2_object_start_streaming (v4l2sink->v4l2object)) {
899 return GST_FLOW_ERROR;
901 v4l2sink->state = STATE_STREAMING;
905 gst_buffer_ref (buf);
908 /* if the driver has more than one buffer, ie. more than just the one we
909 * just queued, then dequeue one immediately to make it available via
912 if (gst_v4l2_buffer_pool_available_buffers (v4l2sink->pool) >
913 v4l2sink->min_queued_bufs) {
914 GstV4l2Buffer *v4l2buf = gst_v4l2_buffer_pool_dqbuf (v4l2sink->pool);
916 /* note: if we get a buf, we don't want to use it directly (because
917 * someone else could still hold a ref).. but instead we release our
918 * reference to it, and if no one else holds a ref it will be returned
919 * to the pool of available buffers.. and if not, we keep looping.
922 gst_buffer_unref (GST_BUFFER (v4l2buf));
931 gst_v4l2sink_navigation_send_event (GstNavigation * navigation,
932 GstStructure * structure)
934 GstV4l2Sink *v4l2sink = GST_V4L2SINK (navigation);
935 GstV4l2Xv *xv = v4l2sink->v4l2object->xv;
941 if ((peer = gst_pad_get_peer (GST_VIDEO_SINK_PAD (v4l2sink)))) {
942 GstVideoRectangle rect;
943 gdouble x, y, xscale = 1.0, yscale = 1.0;
945 gst_v4l2_xoverlay_get_render_rect (v4l2sink->v4l2object, &rect);
947 /* We calculate scaling using the original video frames geometry to
948 * include pixel aspect ratio scaling.
950 xscale = (gdouble) v4l2sink->video_width / rect.w;
951 yscale = (gdouble) v4l2sink->video_height / rect.h;
953 /* Converting pointer coordinates to the non scaled geometry */
954 if (gst_structure_get_double (structure, "pointer_x", &x)) {
955 x = MIN (x, rect.x + rect.w);
956 x = MAX (x - rect.x, 0);
957 gst_structure_set (structure, "pointer_x", G_TYPE_DOUBLE,
958 (gdouble) x * xscale, NULL);
960 if (gst_structure_get_double (structure, "pointer_y", &y)) {
961 y = MIN (y, rect.y + rect.h);
962 y = MAX (y - rect.y, 0);
963 gst_structure_set (structure, "pointer_y", G_TYPE_DOUBLE,
964 (gdouble) y * yscale, NULL);
967 gst_pad_send_event (peer, gst_event_new_navigation (structure));
968 gst_object_unref (peer);