1ec96bdddb5f4ef96ebb35412b70e4aef2fae7ce
[platform/upstream/gstreamer.git] / sys / v4l2 / gstv4l2sink.c
1 /* GStreamer
2  *
3  * Copyright (C) 2009 Texas Instruments, Inc - http://www.ti.com/
4  *
5  * Description: V4L2 sink element
6  *  Created on: Jul 2, 2009
7  *      Author: Rob Clark <rob@ti.com>
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 /**
26  * SECTION:element-v4l2sink
27  *
28  * v4l2sink can be used to display video to v4l2 devices (screen overlays
29  * provided by the graphics hardware, tv-out, etc)
30  *
31  * <refsect2>
32  * <title>Example launch lines</title>
33  * |[
34  * gst-launch videotestsrc ! v4l2sink device=/dev/video1
35  * ]| This pipeline displays a test pattern on /dev/video1
36  * |[
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
47  * image area
48  * </refsect2>
49  */
50
51
52 #ifdef HAVE_CONFIG_H
53 #include <config.h>
54 #endif
55
56
57 #include "gstv4l2colorbalance.h"
58 #include "gstv4l2tuner.h"
59 #ifdef HAVE_XVIDEO
60 #include "gstv4l2xoverlay.h"
61 #endif
62 #include "gstv4l2vidorient.h"
63
64 #include "gstv4l2sink.h"
65 #include "gst/gst-i18n-plugin.h"
66
67 #include <string.h>
68
69 GST_DEBUG_CATEGORY (v4l2sink_debug);
70 #define GST_CAT_DEFAULT v4l2sink_debug
71
72 #define PROP_DEF_QUEUE_SIZE         12
73 #define PROP_DEF_MIN_QUEUED_BUFS    1
74 #define DEFAULT_PROP_DEVICE   "/dev/video1"
75
76 enum
77 {
78   PROP_0,
79   V4L2_STD_OBJECT_PROPS,
80   PROP_QUEUE_SIZE,
81   PROP_MIN_QUEUED_BUFS,
82   PROP_OVERLAY_TOP,
83   PROP_OVERLAY_LEFT,
84   PROP_OVERLAY_WIDTH,
85   PROP_OVERLAY_HEIGHT,
86   PROP_CROP_TOP,
87   PROP_CROP_LEFT,
88   PROP_CROP_WIDTH,
89   PROP_CROP_HEIGHT,
90 };
91
92
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);
96 #ifdef HAVE_XVIDEO
97 GST_IMPLEMENT_V4L2_XOVERLAY_METHODS (GstV4l2Sink, gst_v4l2sink);
98 #endif
99 GST_IMPLEMENT_V4L2_VIDORIENT_METHODS (GstV4l2Sink, gst_v4l2sink);
100
101 #ifdef HAVE_XVIDEO
102 static void gst_v4l2sink_navigation_send_event (GstNavigation * navigation,
103     GstStructure * structure);
104 static void
105 gst_v4l2sink_navigation_init (GstNavigationInterface * iface)
106 {
107   iface->send_event = gst_v4l2sink_navigation_send_event;
108 }
109 #endif
110
111 #define gst_v4l2sink_parent_class parent_class
112 G_DEFINE_TYPE_WITH_CODE (GstV4l2Sink, gst_v4l2sink, GST_TYPE_VIDEO_SINK,
113     G_IMPLEMENT_INTERFACE (GST_TYPE_TUNER, gst_v4l2sink_tuner_interface_init);
114 #ifdef HAVE_XVIDEO
115     G_IMPLEMENT_INTERFACE (GST_TYPE_X_OVERLAY,
116         gst_v4l2sink_xoverlay_interface_init);
117     G_IMPLEMENT_INTERFACE (GST_TYPE_NAVIGATION, gst_v4l2sink_navigation_init);
118 #endif
119     G_IMPLEMENT_INTERFACE (GST_TYPE_COLOR_BALANCE,
120         gst_v4l2sink_color_balance_interface_init);
121     G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_ORIENTATION,
122         gst_v4l2sink_video_orientation_interface_init);
123     G_IMPLEMENT_INTERFACE (GST_TYPE_PROPERTY_PROBE,
124         gst_v4l2sink_property_probe_interface_init));
125
126
127 static void gst_v4l2sink_dispose (GObject * object);
128 static void gst_v4l2sink_finalize (GstV4l2Sink * v4l2sink);
129
130 /* GObject methods: */
131 static void gst_v4l2sink_set_property (GObject * object, guint prop_id,
132     const GValue * value, GParamSpec * pspec);
133 static void gst_v4l2sink_get_property (GObject * object, guint prop_id,
134     GValue * value, GParamSpec * pspec);
135
136
137 /* GstElement methods: */
138 static GstStateChangeReturn gst_v4l2sink_change_state (GstElement * element,
139     GstStateChange transition);
140
141 /* GstBaseSink methods: */
142 static GstCaps *gst_v4l2sink_get_caps (GstBaseSink * bsink, GstCaps * filter);
143 static gboolean gst_v4l2sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
144 #if 0
145 static GstFlowReturn gst_v4l2sink_buffer_alloc (GstBaseSink * bsink,
146     guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
147 #endif
148 static GstFlowReturn gst_v4l2sink_show_frame (GstBaseSink * bsink,
149     GstBuffer * buf);
150
151 static void
152 gst_v4l2sink_class_init (GstV4l2SinkClass * klass)
153 {
154   GObjectClass *gobject_class;
155   GstElementClass *element_class;
156   GstBaseSinkClass *basesink_class;
157
158   gobject_class = G_OBJECT_CLASS (klass);
159   element_class = GST_ELEMENT_CLASS (klass);
160   basesink_class = GST_BASE_SINK_CLASS (klass);
161
162   gobject_class->dispose = gst_v4l2sink_dispose;
163   gobject_class->finalize = (GObjectFinalizeFunc) gst_v4l2sink_finalize;
164   gobject_class->set_property = gst_v4l2sink_set_property;
165   gobject_class->get_property = gst_v4l2sink_get_property;
166
167   element_class->change_state = gst_v4l2sink_change_state;
168
169   gst_v4l2_object_install_properties_helper (gobject_class,
170       DEFAULT_PROP_DEVICE);
171   g_object_class_install_property (gobject_class, PROP_QUEUE_SIZE,
172       g_param_spec_uint ("queue-size", "Queue size",
173           "Number of buffers to be enqueud in the driver in streaming mode",
174           GST_V4L2_MIN_BUFFERS, GST_V4L2_MAX_BUFFERS, PROP_DEF_QUEUE_SIZE,
175           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
176   g_object_class_install_property (gobject_class, PROP_MIN_QUEUED_BUFS,
177       g_param_spec_uint ("min-queued-bufs", "Minimum queued bufs",
178           "Minimum number of queued bufs; v4l2sink won't dqbuf if the driver "
179           "doesn't have more than this number (which normally you shouldn't change)",
180           0, GST_V4L2_MAX_BUFFERS, PROP_DEF_MIN_QUEUED_BUFS,
181           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
182   g_object_class_install_property (gobject_class, PROP_OVERLAY_TOP,
183       g_param_spec_int ("overlay-top", "Overlay top",
184           "The topmost (y) coordinate of the video overlay; top left corner of screen is 0,0",
185           G_MININT, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
186   g_object_class_install_property (gobject_class, PROP_OVERLAY_LEFT,
187       g_param_spec_int ("overlay-left", "Overlay left",
188           "The leftmost (x) coordinate of the video overlay; top left corner of screen is 0,0",
189           G_MININT, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
190   g_object_class_install_property (gobject_class, PROP_OVERLAY_WIDTH,
191       g_param_spec_uint ("overlay-width", "Overlay width",
192           "The width of the video overlay; default is equal to negotiated image width",
193           0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
194   g_object_class_install_property (gobject_class, PROP_OVERLAY_HEIGHT,
195       g_param_spec_uint ("overlay-height", "Overlay height",
196           "The height of the video overlay; default is equal to negotiated image height",
197           0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198
199   g_object_class_install_property (gobject_class, PROP_CROP_TOP,
200       g_param_spec_int ("crop-top", "Crop top",
201           "The topmost (y) coordinate of the video crop; top left corner of image is 0,0",
202           0x80000000, 0x7fffffff, 0, G_PARAM_READWRITE));
203   g_object_class_install_property (gobject_class, PROP_CROP_LEFT,
204       g_param_spec_int ("crop-left", "Crop left",
205           "The leftmost (x) coordinate of the video crop; top left corner of image is 0,0",
206           0x80000000, 0x7fffffff, 0, G_PARAM_READWRITE));
207   g_object_class_install_property (gobject_class, PROP_CROP_WIDTH,
208       g_param_spec_uint ("crop-width", "Crop width",
209           "The width of the video crop; default is equal to negotiated image width",
210           0, 0xffffffff, 0, G_PARAM_READWRITE));
211   g_object_class_install_property (gobject_class, PROP_CROP_HEIGHT,
212       g_param_spec_uint ("crop-height", "Crop height",
213           "The height of the video crop; default is equal to negotiated image height",
214           0, 0xffffffff, 0, G_PARAM_READWRITE));
215
216   gst_element_class_set_details_simple (element_class,
217       "Video (video4linux2) Sink", "Sink/Video",
218       "Displays frames on a video4linux2 device", "Rob Clark <rob@ti.com>,");
219
220   gst_element_class_add_pad_template (element_class,
221       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
222           gst_v4l2_object_get_all_caps ()));
223
224   basesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_v4l2sink_get_caps);
225   basesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_v4l2sink_set_caps);
226   basesink_class->render = GST_DEBUG_FUNCPTR (gst_v4l2sink_show_frame);
227
228   klass->v4l2_class_devices = NULL;
229
230   GST_DEBUG_CATEGORY_INIT (v4l2sink_debug, "v4l2sink", 0, "V4L2 sink element");
231
232 }
233
234 static void
235 gst_v4l2sink_init (GstV4l2Sink * v4l2sink)
236 {
237   v4l2sink->v4l2object = gst_v4l2_object_new (GST_ELEMENT (v4l2sink),
238       V4L2_BUF_TYPE_VIDEO_OUTPUT, DEFAULT_PROP_DEVICE,
239       gst_v4l2_get_output, gst_v4l2_set_output, NULL);
240
241   /* same default value for video output device as is used for
242    * v4l2src/capture is no good..  so lets set a saner default
243    * (which can be overridden by the one creating the v4l2sink
244    * after the constructor returns)
245    */
246   g_object_set (v4l2sink, "device", "/dev/video1", NULL);
247
248   /* number of buffers requested */
249   v4l2sink->v4l2object->num_buffers = PROP_DEF_QUEUE_SIZE;
250   v4l2sink->v4l2object->min_queued_bufs = PROP_DEF_MIN_QUEUED_BUFS;
251
252   v4l2sink->probed_caps = NULL;
253   v4l2sink->current_caps = NULL;
254
255   v4l2sink->overlay_fields_set = 0;
256   v4l2sink->crop_fields_set = 0;
257 }
258
259
260 static void
261 gst_v4l2sink_dispose (GObject * object)
262 {
263   GstV4l2Sink *v4l2sink = GST_V4L2SINK (object);
264
265   if (v4l2sink->probed_caps) {
266     gst_caps_unref (v4l2sink->probed_caps);
267   }
268
269   if (v4l2sink->current_caps) {
270     gst_caps_unref (v4l2sink->current_caps);
271   }
272
273   G_OBJECT_CLASS (parent_class)->dispose (object);
274 }
275
276
277 static void
278 gst_v4l2sink_finalize (GstV4l2Sink * v4l2sink)
279 {
280   gst_v4l2_object_destroy (v4l2sink->v4l2object);
281
282   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (v4l2sink));
283 }
284
285
286 /*
287  * flags to indicate which overlay/crop properties the user has set (and
288  * therefore which ones should override the defaults from the driver)
289  */
290 enum
291 {
292   RECT_TOP_SET = 0x01,
293   RECT_LEFT_SET = 0x02,
294   RECT_WIDTH_SET = 0x04,
295   RECT_HEIGHT_SET = 0x08
296 };
297
298 static void
299 gst_v4l2sink_sync_overlay_fields (GstV4l2Sink * v4l2sink)
300 {
301   if (!v4l2sink->overlay_fields_set)
302     return;
303
304   if (GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
305
306     gint fd = v4l2sink->v4l2object->video_fd;
307     struct v4l2_format format;
308
309     memset (&format, 0x00, sizeof (struct v4l2_format));
310     format.type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
311
312     if (v4l2_ioctl (fd, VIDIOC_G_FMT, &format) < 0) {
313       GST_WARNING_OBJECT (v4l2sink, "VIDIOC_G_FMT failed");
314       return;
315     }
316
317     GST_DEBUG_OBJECT (v4l2sink,
318         "setting overlay: overlay_fields_set=0x%02x, top=%d, left=%d, width=%d, height=%d",
319         v4l2sink->overlay_fields_set,
320         v4l2sink->overlay.top, v4l2sink->overlay.left,
321         v4l2sink->overlay.width, v4l2sink->overlay.height);
322
323     if (v4l2sink->overlay_fields_set & RECT_TOP_SET)
324       format.fmt.win.w.top = v4l2sink->overlay.top;
325     if (v4l2sink->overlay_fields_set & RECT_LEFT_SET)
326       format.fmt.win.w.left = v4l2sink->overlay.left;
327     if (v4l2sink->overlay_fields_set & RECT_WIDTH_SET)
328       format.fmt.win.w.width = v4l2sink->overlay.width;
329     if (v4l2sink->overlay_fields_set & RECT_HEIGHT_SET)
330       format.fmt.win.w.height = v4l2sink->overlay.height;
331
332     if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0) {
333       GST_WARNING_OBJECT (v4l2sink, "VIDIOC_S_FMT failed");
334       return;
335     }
336
337     v4l2sink->overlay_fields_set = 0;
338     v4l2sink->overlay = format.fmt.win.w;
339   }
340 }
341
342 static void
343 gst_v4l2sink_sync_crop_fields (GstV4l2Sink * v4l2sink)
344 {
345   if (!v4l2sink->crop_fields_set)
346     return;
347
348   if (GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
349
350     gint fd = v4l2sink->v4l2object->video_fd;
351     struct v4l2_crop crop;
352
353     memset (&crop, 0x00, sizeof (struct v4l2_crop));
354     crop.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
355
356     if (v4l2_ioctl (fd, VIDIOC_G_CROP, &crop) < 0) {
357       GST_WARNING_OBJECT (v4l2sink, "VIDIOC_G_CROP failed");
358       return;
359     }
360
361     GST_DEBUG_OBJECT (v4l2sink,
362         "setting crop: crop_fields_set=0x%02x, top=%d, left=%d, width=%d, height=%d",
363         v4l2sink->crop_fields_set,
364         v4l2sink->crop.top, v4l2sink->crop.left,
365         v4l2sink->crop.width, v4l2sink->crop.height);
366
367     if (v4l2sink->crop_fields_set & RECT_TOP_SET)
368       crop.c.top = v4l2sink->crop.top;
369     if (v4l2sink->crop_fields_set & RECT_LEFT_SET)
370       crop.c.left = v4l2sink->crop.left;
371     if (v4l2sink->crop_fields_set & RECT_WIDTH_SET)
372       crop.c.width = v4l2sink->crop.width;
373     if (v4l2sink->crop_fields_set & RECT_HEIGHT_SET)
374       crop.c.height = v4l2sink->crop.height;
375
376     if (v4l2_ioctl (fd, VIDIOC_S_CROP, &crop) < 0) {
377       GST_WARNING_OBJECT (v4l2sink, "VIDIOC_S_CROP failed");
378       return;
379     }
380
381     v4l2sink->crop_fields_set = 0;
382     v4l2sink->crop = crop.c;
383   }
384 }
385
386
387 static void
388 gst_v4l2sink_set_property (GObject * object,
389     guint prop_id, const GValue * value, GParamSpec * pspec)
390 {
391   GstV4l2Sink *v4l2sink = GST_V4L2SINK (object);
392
393   if (!gst_v4l2_object_set_property_helper (v4l2sink->v4l2object,
394           prop_id, value, pspec)) {
395     switch (prop_id) {
396       case PROP_QUEUE_SIZE:
397         v4l2sink->v4l2object->num_buffers = g_value_get_uint (value);
398         break;
399       case PROP_MIN_QUEUED_BUFS:
400         v4l2sink->v4l2object->min_queued_bufs = g_value_get_uint (value);
401         break;
402       case PROP_OVERLAY_TOP:
403         v4l2sink->overlay.top = g_value_get_int (value);
404         v4l2sink->overlay_fields_set |= RECT_TOP_SET;
405         gst_v4l2sink_sync_overlay_fields (v4l2sink);
406         break;
407       case PROP_OVERLAY_LEFT:
408         v4l2sink->overlay.left = g_value_get_int (value);
409         v4l2sink->overlay_fields_set |= RECT_LEFT_SET;
410         gst_v4l2sink_sync_overlay_fields (v4l2sink);
411         break;
412       case PROP_OVERLAY_WIDTH:
413         v4l2sink->overlay.width = g_value_get_uint (value);
414         v4l2sink->overlay_fields_set |= RECT_WIDTH_SET;
415         gst_v4l2sink_sync_overlay_fields (v4l2sink);
416         break;
417       case PROP_OVERLAY_HEIGHT:
418         v4l2sink->overlay.height = g_value_get_uint (value);
419         v4l2sink->overlay_fields_set |= RECT_HEIGHT_SET;
420         gst_v4l2sink_sync_overlay_fields (v4l2sink);
421         break;
422       case PROP_CROP_TOP:
423         v4l2sink->crop.top = g_value_get_int (value);
424         v4l2sink->crop_fields_set |= RECT_TOP_SET;
425         gst_v4l2sink_sync_crop_fields (v4l2sink);
426         break;
427       case PROP_CROP_LEFT:
428         v4l2sink->crop.left = g_value_get_int (value);
429         v4l2sink->crop_fields_set |= RECT_LEFT_SET;
430         gst_v4l2sink_sync_crop_fields (v4l2sink);
431         break;
432       case PROP_CROP_WIDTH:
433         v4l2sink->crop.width = g_value_get_uint (value);
434         v4l2sink->crop_fields_set |= RECT_WIDTH_SET;
435         gst_v4l2sink_sync_crop_fields (v4l2sink);
436         break;
437       case PROP_CROP_HEIGHT:
438         v4l2sink->crop.height = g_value_get_uint (value);
439         v4l2sink->crop_fields_set |= RECT_HEIGHT_SET;
440         gst_v4l2sink_sync_crop_fields (v4l2sink);
441         break;
442       default:
443         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
444         break;
445     }
446   }
447 }
448
449
450 static void
451 gst_v4l2sink_get_property (GObject * object,
452     guint prop_id, GValue * value, GParamSpec * pspec)
453 {
454   GstV4l2Sink *v4l2sink = GST_V4L2SINK (object);
455
456   if (!gst_v4l2_object_get_property_helper (v4l2sink->v4l2object,
457           prop_id, value, pspec)) {
458     switch (prop_id) {
459       case PROP_QUEUE_SIZE:
460         g_value_set_uint (value, v4l2sink->v4l2object->num_buffers);
461         break;
462       case PROP_MIN_QUEUED_BUFS:
463         g_value_set_uint (value, v4l2sink->v4l2object->min_queued_bufs);
464         break;
465       case PROP_OVERLAY_TOP:
466         g_value_set_int (value, v4l2sink->overlay.top);
467         break;
468       case PROP_OVERLAY_LEFT:
469         g_value_set_int (value, v4l2sink->overlay.left);
470         break;
471       case PROP_OVERLAY_WIDTH:
472         g_value_set_uint (value, v4l2sink->overlay.width);
473         break;
474       case PROP_OVERLAY_HEIGHT:
475         g_value_set_uint (value, v4l2sink->overlay.height);
476         break;
477       case PROP_CROP_TOP:
478         g_value_set_int (value, v4l2sink->crop.top);
479         break;
480       case PROP_CROP_LEFT:
481         g_value_set_int (value, v4l2sink->crop.left);
482         break;
483       case PROP_CROP_WIDTH:
484         g_value_set_uint (value, v4l2sink->crop.width);
485         break;
486       case PROP_CROP_HEIGHT:
487         g_value_set_uint (value, v4l2sink->crop.height);
488         break;
489       default:
490         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
491         break;
492     }
493   }
494 }
495
496 static GstStateChangeReturn
497 gst_v4l2sink_change_state (GstElement * element, GstStateChange transition)
498 {
499   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
500   GstV4l2Sink *v4l2sink = GST_V4L2SINK (element);
501
502   GST_DEBUG_OBJECT (v4l2sink, "%d -> %d",
503       GST_STATE_TRANSITION_CURRENT (transition),
504       GST_STATE_TRANSITION_NEXT (transition));
505
506   switch (transition) {
507     case GST_STATE_CHANGE_NULL_TO_READY:
508       /* open the device */
509       if (!gst_v4l2_object_open (v4l2sink->v4l2object))
510         return GST_STATE_CHANGE_FAILURE;
511       break;
512     default:
513       break;
514   }
515
516   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
517
518   switch (transition) {
519     case GST_STATE_CHANGE_PAUSED_TO_READY:
520       if (!gst_v4l2_object_stop (v4l2sink->v4l2object))
521         return GST_STATE_CHANGE_FAILURE;
522       break;
523     case GST_STATE_CHANGE_READY_TO_NULL:
524       /* we need to call stop here too */
525       if (!gst_v4l2_object_stop (v4l2sink->v4l2object))
526         return GST_STATE_CHANGE_FAILURE;
527       /* close the device */
528       if (!gst_v4l2_object_close (v4l2sink->v4l2object))
529         return GST_STATE_CHANGE_FAILURE;
530       break;
531     default:
532       break;
533   }
534
535   return ret;
536 }
537
538
539 static GstCaps *
540 gst_v4l2sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
541 {
542   GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
543   GstCaps *ret;
544   GSList *walk;
545   GSList *formats;
546
547   if (!GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
548     /* FIXME: copy? */
549     GST_DEBUG_OBJECT (v4l2sink, "device is not open");
550     return
551         gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD
552             (v4l2sink)));
553   }
554
555   if (v4l2sink->probed_caps == NULL) {
556     formats = gst_v4l2_object_get_format_list (v4l2sink->v4l2object);
557
558     ret = gst_caps_new_empty ();
559
560     for (walk = formats; walk; walk = walk->next) {
561       struct v4l2_fmtdesc *format;
562
563       GstStructure *template;
564
565       format = (struct v4l2_fmtdesc *) walk->data;
566
567       template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
568
569       if (template) {
570         GstCaps *tmp;
571
572         tmp =
573             gst_v4l2_object_probe_caps_for_format (v4l2sink->v4l2object,
574             format->pixelformat, template);
575         if (tmp)
576           gst_caps_append (ret, tmp);
577
578         gst_structure_free (template);
579       } else {
580         GST_DEBUG_OBJECT (v4l2sink, "unknown format %u", format->pixelformat);
581       }
582     }
583     v4l2sink->probed_caps = ret;
584   }
585
586   if (filter) {
587     ret =
588         gst_caps_intersect_full (filter, v4l2sink->probed_caps,
589         GST_CAPS_INTERSECT_FIRST);
590   } else {
591     ret = gst_caps_ref (v4l2sink->probed_caps);
592   }
593
594   GST_INFO_OBJECT (v4l2sink, "probed caps: %p", ret);
595   LOG_CAPS (v4l2sink, ret);
596
597   return ret;
598 }
599
600 static gboolean
601 gst_v4l2sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
602 {
603   GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
604   GstV4l2Object *obj = v4l2sink->v4l2object;
605
606   LOG_CAPS (v4l2sink, caps);
607
608   if (!GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
609     GST_DEBUG_OBJECT (v4l2sink, "device is not open");
610     return FALSE;
611   }
612
613   if (v4l2sink->current_caps) {
614     GST_DEBUG_OBJECT (v4l2sink, "already have caps set.. are they equal?");
615     LOG_CAPS (v4l2sink, v4l2sink->current_caps);
616     if (gst_caps_is_equal (v4l2sink->current_caps, caps)) {
617       GST_DEBUG_OBJECT (v4l2sink, "yes they are!");
618       return TRUE;
619     }
620     GST_DEBUG_OBJECT (v4l2sink, "no they aren't!");
621   }
622
623   if (!gst_v4l2_object_stop (obj))
624     goto stop_failed;
625
626   if (!gst_v4l2_object_set_format (v4l2sink->v4l2object, caps))
627     goto invalid_format;
628
629   gst_v4l2sink_sync_overlay_fields (v4l2sink);
630   gst_v4l2sink_sync_crop_fields (v4l2sink);
631
632 #ifdef HAVE_XVIDEO
633   gst_v4l2_xoverlay_prepare_xwindow_id (v4l2sink->v4l2object, TRUE);
634 #endif
635
636   GST_INFO_OBJECT (v4l2sink, "outputting buffers via mmap()");
637
638   v4l2sink->video_width = GST_V4L2_WIDTH (v4l2sink->v4l2object);
639   v4l2sink->video_height = GST_V4L2_HEIGHT (v4l2sink->v4l2object);
640
641   /* TODO: videosink width/height should be scaled according to
642    * pixel-aspect-ratio
643    */
644   GST_VIDEO_SINK_WIDTH (v4l2sink) = v4l2sink->video_width;
645   GST_VIDEO_SINK_HEIGHT (v4l2sink) = v4l2sink->video_height;
646
647   v4l2sink->current_caps = gst_caps_ref (caps);
648
649   return TRUE;
650
651   /* ERRORS */
652 stop_failed:
653   {
654     GST_DEBUG_OBJECT (v4l2sink, "failed to stop streaming");
655     return FALSE;
656   }
657 invalid_format:
658   {
659     /* error already posted */
660     GST_DEBUG_OBJECT (v4l2sink, "can't set format");
661     return FALSE;
662   }
663 }
664
665 /* called after A/V sync to render frame */
666 static GstFlowReturn
667 gst_v4l2sink_show_frame (GstBaseSink * bsink, GstBuffer * buf)
668 {
669   GstFlowReturn ret;
670   GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
671   GstV4l2Object *obj = v4l2sink->v4l2object;
672
673   GST_DEBUG_OBJECT (v4l2sink, "render buffer: %p", buf);
674
675   ret = gst_v4l2_object_output_buffer (obj, buf);
676
677   return ret;
678 }
679
680 #ifdef HAVE_XVIDEO
681 static void
682 gst_v4l2sink_navigation_send_event (GstNavigation * navigation,
683     GstStructure * structure)
684 {
685   GstV4l2Sink *v4l2sink = GST_V4L2SINK (navigation);
686   GstV4l2Xv *xv = v4l2sink->v4l2object->xv;
687   GstPad *peer;
688
689   if (!xv)
690     return;
691
692   if ((peer = gst_pad_get_peer (GST_VIDEO_SINK_PAD (v4l2sink)))) {
693     GstVideoRectangle rect;
694     gdouble x, y, xscale = 1.0, yscale = 1.0;
695
696     gst_v4l2_xoverlay_get_render_rect (v4l2sink->v4l2object, &rect);
697
698     /* We calculate scaling using the original video frames geometry to
699      * include pixel aspect ratio scaling.
700      */
701     xscale = (gdouble) v4l2sink->video_width / rect.w;
702     yscale = (gdouble) v4l2sink->video_height / rect.h;
703
704     /* Converting pointer coordinates to the non scaled geometry */
705     if (gst_structure_get_double (structure, "pointer_x", &x)) {
706       x = MIN (x, rect.x + rect.w);
707       x = MAX (x - rect.x, 0);
708       gst_structure_set (structure, "pointer_x", G_TYPE_DOUBLE,
709           (gdouble) x * xscale, NULL);
710     }
711     if (gst_structure_get_double (structure, "pointer_y", &y)) {
712       y = MIN (y, rect.y + rect.h);
713       y = MAX (y - rect.y, 0);
714       gst_structure_set (structure, "pointer_y", G_TYPE_DOUBLE,
715           (gdouble) y * yscale, NULL);
716     }
717
718     gst_pad_send_event (peer, gst_event_new_navigation (structure));
719     gst_object_unref (peer);
720   }
721 }
722 #endif