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