50e5ba658081ccf4bd7357e85541cd043955ff1a
[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->num_buffers = PROP_DEF_QUEUE_SIZE;
250   v4l2sink->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   v4l2sink->state = 0;
258 }
259
260
261 static void
262 gst_v4l2sink_dispose (GObject * object)
263 {
264   GstV4l2Sink *v4l2sink = GST_V4L2SINK (object);
265
266   if (v4l2sink->probed_caps) {
267     gst_caps_unref (v4l2sink->probed_caps);
268   }
269
270   if (v4l2sink->current_caps) {
271     gst_caps_unref (v4l2sink->current_caps);
272   }
273
274   G_OBJECT_CLASS (parent_class)->dispose (object);
275 }
276
277
278 static void
279 gst_v4l2sink_finalize (GstV4l2Sink * v4l2sink)
280 {
281   gst_v4l2_object_destroy (v4l2sink->v4l2object);
282
283   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (v4l2sink));
284 }
285
286
287 /*
288  * State values
289  */
290 enum
291 {
292   STATE_OFF = 0,
293   STATE_PENDING_STREAMON,
294   STATE_STREAMING
295 };
296
297 /*
298  * flags to indicate which overlay/crop properties the user has set (and
299  * therefore which ones should override the defaults from the driver)
300  */
301 enum
302 {
303   RECT_TOP_SET = 0x01,
304   RECT_LEFT_SET = 0x02,
305   RECT_WIDTH_SET = 0x04,
306   RECT_HEIGHT_SET = 0x08
307 };
308
309 static void
310 gst_v4l2sink_sync_overlay_fields (GstV4l2Sink * v4l2sink)
311 {
312   if (!v4l2sink->overlay_fields_set)
313     return;
314
315   if (GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
316
317     gint fd = v4l2sink->v4l2object->video_fd;
318     struct v4l2_format format;
319
320     memset (&format, 0x00, sizeof (struct v4l2_format));
321     format.type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
322
323     if (v4l2_ioctl (fd, VIDIOC_G_FMT, &format) < 0) {
324       GST_WARNING_OBJECT (v4l2sink, "VIDIOC_G_FMT failed");
325       return;
326     }
327
328     GST_DEBUG_OBJECT (v4l2sink,
329         "setting overlay: overlay_fields_set=0x%02x, top=%d, left=%d, width=%d, height=%d",
330         v4l2sink->overlay_fields_set,
331         v4l2sink->overlay.top, v4l2sink->overlay.left,
332         v4l2sink->overlay.width, v4l2sink->overlay.height);
333
334     if (v4l2sink->overlay_fields_set & RECT_TOP_SET)
335       format.fmt.win.w.top = v4l2sink->overlay.top;
336     if (v4l2sink->overlay_fields_set & RECT_LEFT_SET)
337       format.fmt.win.w.left = v4l2sink->overlay.left;
338     if (v4l2sink->overlay_fields_set & RECT_WIDTH_SET)
339       format.fmt.win.w.width = v4l2sink->overlay.width;
340     if (v4l2sink->overlay_fields_set & RECT_HEIGHT_SET)
341       format.fmt.win.w.height = v4l2sink->overlay.height;
342
343     if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0) {
344       GST_WARNING_OBJECT (v4l2sink, "VIDIOC_S_FMT failed");
345       return;
346     }
347
348     v4l2sink->overlay_fields_set = 0;
349     v4l2sink->overlay = format.fmt.win.w;
350   }
351 }
352
353 static void
354 gst_v4l2sink_sync_crop_fields (GstV4l2Sink * v4l2sink)
355 {
356   if (!v4l2sink->crop_fields_set)
357     return;
358
359   if (GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
360
361     gint fd = v4l2sink->v4l2object->video_fd;
362     struct v4l2_crop crop;
363
364     memset (&crop, 0x00, sizeof (struct v4l2_crop));
365     crop.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
366
367     if (v4l2_ioctl (fd, VIDIOC_G_CROP, &crop) < 0) {
368       GST_WARNING_OBJECT (v4l2sink, "VIDIOC_G_CROP failed");
369       return;
370     }
371
372     GST_DEBUG_OBJECT (v4l2sink,
373         "setting crop: crop_fields_set=0x%02x, top=%d, left=%d, width=%d, height=%d",
374         v4l2sink->crop_fields_set,
375         v4l2sink->crop.top, v4l2sink->crop.left,
376         v4l2sink->crop.width, v4l2sink->crop.height);
377
378     if (v4l2sink->crop_fields_set & RECT_TOP_SET)
379       crop.c.top = v4l2sink->crop.top;
380     if (v4l2sink->crop_fields_set & RECT_LEFT_SET)
381       crop.c.left = v4l2sink->crop.left;
382     if (v4l2sink->crop_fields_set & RECT_WIDTH_SET)
383       crop.c.width = v4l2sink->crop.width;
384     if (v4l2sink->crop_fields_set & RECT_HEIGHT_SET)
385       crop.c.height = v4l2sink->crop.height;
386
387     if (v4l2_ioctl (fd, VIDIOC_S_CROP, &crop) < 0) {
388       GST_WARNING_OBJECT (v4l2sink, "VIDIOC_S_CROP failed");
389       return;
390     }
391
392     v4l2sink->crop_fields_set = 0;
393     v4l2sink->crop = crop.c;
394   }
395 }
396
397
398 static void
399 gst_v4l2sink_set_property (GObject * object,
400     guint prop_id, const GValue * value, GParamSpec * pspec)
401 {
402   GstV4l2Sink *v4l2sink = GST_V4L2SINK (object);
403
404   if (!gst_v4l2_object_set_property_helper (v4l2sink->v4l2object,
405           prop_id, value, pspec)) {
406     switch (prop_id) {
407       case PROP_QUEUE_SIZE:
408         v4l2sink->num_buffers = g_value_get_uint (value);
409         break;
410       case PROP_MIN_QUEUED_BUFS:
411         v4l2sink->min_queued_bufs = g_value_get_uint (value);
412         break;
413       case PROP_OVERLAY_TOP:
414         v4l2sink->overlay.top = g_value_get_int (value);
415         v4l2sink->overlay_fields_set |= RECT_TOP_SET;
416         gst_v4l2sink_sync_overlay_fields (v4l2sink);
417         break;
418       case PROP_OVERLAY_LEFT:
419         v4l2sink->overlay.left = g_value_get_int (value);
420         v4l2sink->overlay_fields_set |= RECT_LEFT_SET;
421         gst_v4l2sink_sync_overlay_fields (v4l2sink);
422         break;
423       case PROP_OVERLAY_WIDTH:
424         v4l2sink->overlay.width = g_value_get_uint (value);
425         v4l2sink->overlay_fields_set |= RECT_WIDTH_SET;
426         gst_v4l2sink_sync_overlay_fields (v4l2sink);
427         break;
428       case PROP_OVERLAY_HEIGHT:
429         v4l2sink->overlay.height = g_value_get_uint (value);
430         v4l2sink->overlay_fields_set |= RECT_HEIGHT_SET;
431         gst_v4l2sink_sync_overlay_fields (v4l2sink);
432         break;
433       case PROP_CROP_TOP:
434         v4l2sink->crop.top = g_value_get_int (value);
435         v4l2sink->crop_fields_set |= RECT_TOP_SET;
436         gst_v4l2sink_sync_crop_fields (v4l2sink);
437         break;
438       case PROP_CROP_LEFT:
439         v4l2sink->crop.left = g_value_get_int (value);
440         v4l2sink->crop_fields_set |= RECT_LEFT_SET;
441         gst_v4l2sink_sync_crop_fields (v4l2sink);
442         break;
443       case PROP_CROP_WIDTH:
444         v4l2sink->crop.width = g_value_get_uint (value);
445         v4l2sink->crop_fields_set |= RECT_WIDTH_SET;
446         gst_v4l2sink_sync_crop_fields (v4l2sink);
447         break;
448       case PROP_CROP_HEIGHT:
449         v4l2sink->crop.height = g_value_get_uint (value);
450         v4l2sink->crop_fields_set |= RECT_HEIGHT_SET;
451         gst_v4l2sink_sync_crop_fields (v4l2sink);
452         break;
453       default:
454         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
455         break;
456     }
457   }
458 }
459
460
461 static void
462 gst_v4l2sink_get_property (GObject * object,
463     guint prop_id, GValue * value, GParamSpec * pspec)
464 {
465   GstV4l2Sink *v4l2sink = GST_V4L2SINK (object);
466
467   if (!gst_v4l2_object_get_property_helper (v4l2sink->v4l2object,
468           prop_id, value, pspec)) {
469     switch (prop_id) {
470       case PROP_QUEUE_SIZE:
471         g_value_set_uint (value, v4l2sink->num_buffers);
472         break;
473       case PROP_MIN_QUEUED_BUFS:
474         g_value_set_uint (value, v4l2sink->min_queued_bufs);
475         break;
476       case PROP_OVERLAY_TOP:
477         g_value_set_int (value, v4l2sink->overlay.top);
478         break;
479       case PROP_OVERLAY_LEFT:
480         g_value_set_int (value, v4l2sink->overlay.left);
481         break;
482       case PROP_OVERLAY_WIDTH:
483         g_value_set_uint (value, v4l2sink->overlay.width);
484         break;
485       case PROP_OVERLAY_HEIGHT:
486         g_value_set_uint (value, v4l2sink->overlay.height);
487         break;
488       case PROP_CROP_TOP:
489         g_value_set_int (value, v4l2sink->crop.top);
490         break;
491       case PROP_CROP_LEFT:
492         g_value_set_int (value, v4l2sink->crop.left);
493         break;
494       case PROP_CROP_WIDTH:
495         g_value_set_uint (value, v4l2sink->crop.width);
496         break;
497       case PROP_CROP_HEIGHT:
498         g_value_set_uint (value, v4l2sink->crop.height);
499         break;
500       default:
501         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
502         break;
503     }
504   }
505 }
506
507 static GstStateChangeReturn
508 gst_v4l2sink_change_state (GstElement * element, GstStateChange transition)
509 {
510   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
511   GstV4l2Sink *v4l2sink = GST_V4L2SINK (element);
512
513   GST_DEBUG_OBJECT (v4l2sink, "%d -> %d",
514       GST_STATE_TRANSITION_CURRENT (transition),
515       GST_STATE_TRANSITION_NEXT (transition));
516
517   switch (transition) {
518     case GST_STATE_CHANGE_NULL_TO_READY:
519       /* open the device */
520       if (!gst_v4l2_object_start (v4l2sink->v4l2object))
521         return GST_STATE_CHANGE_FAILURE;
522       break;
523     default:
524       break;
525   }
526
527   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
528
529   switch (transition) {
530     case GST_STATE_CHANGE_PAUSED_TO_READY:
531       if (v4l2sink->state == STATE_STREAMING) {
532         if (!gst_v4l2_object_stop_streaming (v4l2sink->v4l2object)) {
533           return GST_STATE_CHANGE_FAILURE;
534         }
535         v4l2sink->state = STATE_PENDING_STREAMON;
536       }
537       break;
538     case GST_STATE_CHANGE_READY_TO_NULL:
539       if (NULL != v4l2sink->pool)
540         gst_v4l2_buffer_pool_destroy (v4l2sink->pool);
541       v4l2sink->pool = NULL;
542       /* close the device */
543       if (!gst_v4l2_object_stop (v4l2sink->v4l2object))
544         return GST_STATE_CHANGE_FAILURE;
545       v4l2sink->state = STATE_OFF;
546       break;
547     default:
548       break;
549   }
550
551   return ret;
552 }
553
554
555 static GstCaps *
556 gst_v4l2sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
557 {
558   GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
559   GstCaps *ret;
560   GSList *walk;
561   GSList *formats;
562
563   if (!GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
564     /* FIXME: copy? */
565     GST_DEBUG_OBJECT (v4l2sink, "device is not open");
566     return
567         gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD
568             (v4l2sink)));
569   }
570
571   if (v4l2sink->probed_caps == NULL) {
572     formats = gst_v4l2_object_get_format_list (v4l2sink->v4l2object);
573
574     ret = gst_caps_new_empty ();
575
576     for (walk = formats; walk; walk = walk->next) {
577       struct v4l2_fmtdesc *format;
578
579       GstStructure *template;
580
581       format = (struct v4l2_fmtdesc *) walk->data;
582
583       template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
584
585       if (template) {
586         GstCaps *tmp;
587
588         tmp =
589             gst_v4l2_object_probe_caps_for_format (v4l2sink->v4l2object,
590             format->pixelformat, template);
591         if (tmp)
592           gst_caps_append (ret, tmp);
593
594         gst_structure_free (template);
595       } else {
596         GST_DEBUG_OBJECT (v4l2sink, "unknown format %u", format->pixelformat);
597       }
598     }
599     v4l2sink->probed_caps = ret;
600   }
601
602   if (filter) {
603     ret =
604         gst_caps_intersect_full (filter, v4l2sink->probed_caps,
605         GST_CAPS_INTERSECT_FIRST);
606   } else {
607     ret = gst_caps_ref (v4l2sink->probed_caps);
608   }
609
610   GST_INFO_OBJECT (v4l2sink, "probed caps: %p", ret);
611   LOG_CAPS (v4l2sink, ret);
612
613   return ret;
614 }
615
616 static gboolean
617 gst_v4l2sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
618 {
619   GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
620   GstV4l2BufferPool *newpool;
621
622   LOG_CAPS (v4l2sink, caps);
623
624   if (!GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
625     GST_DEBUG_OBJECT (v4l2sink, "device is not open");
626     return FALSE;
627   }
628
629   if (v4l2sink->current_caps) {
630     GST_DEBUG_OBJECT (v4l2sink, "already have caps set.. are they equal?");
631     LOG_CAPS (v4l2sink, v4l2sink->current_caps);
632     if (gst_caps_is_equal (v4l2sink->current_caps, caps)) {
633       GST_DEBUG_OBJECT (v4l2sink, "yes they are!");
634       return TRUE;
635     }
636     GST_DEBUG_OBJECT (v4l2sink, "no they aren't!");
637   }
638
639   if (v4l2sink->pool) {
640     /* we have a pool already, stop and destroy the old pool */
641     if (v4l2sink->state == STATE_STREAMING) {
642       if (!gst_v4l2_object_stop_streaming (v4l2sink->v4l2object))
643         goto stop_failed;
644
645       v4l2sink->state = STATE_PENDING_STREAMON;
646     }
647     gst_v4l2_buffer_pool_destroy (v4l2sink->pool);
648     v4l2sink->pool = NULL;
649   }
650
651   if (!gst_v4l2_object_set_format (v4l2sink->v4l2object, caps))
652     goto invalid_format;
653
654   if (!(v4l2sink->v4l2object->vcap.capabilities & V4L2_CAP_STREAMING))
655     goto no_streaming;
656
657   newpool = gst_v4l2_buffer_pool_new (v4l2sink->v4l2object,
658       v4l2sink->num_buffers, FALSE);
659   if (newpool == NULL)
660     goto no_pool;
661
662   v4l2sink->pool = newpool;
663
664   gst_v4l2sink_sync_overlay_fields (v4l2sink);
665   gst_v4l2sink_sync_crop_fields (v4l2sink);
666
667 #ifdef HAVE_XVIDEO
668   gst_v4l2_xoverlay_prepare_xwindow_id (v4l2sink->v4l2object, TRUE);
669 #endif
670
671   v4l2sink->state = STATE_PENDING_STREAMON;
672
673   GST_INFO_OBJECT (v4l2sink, "outputting buffers via mmap()");
674
675   if (v4l2sink->num_buffers != v4l2sink->pool->buffer_count) {
676     v4l2sink->num_buffers = v4l2sink->pool->buffer_count;
677     g_object_notify (G_OBJECT (v4l2sink), "queue-size");
678   }
679
680   v4l2sink->video_width = GST_V4L2_WIDTH (v4l2sink->v4l2object);
681   v4l2sink->video_height = GST_V4L2_HEIGHT (v4l2sink->v4l2object);
682
683   /* TODO: videosink width/height should be scaled according to
684    * pixel-aspect-ratio
685    */
686   GST_VIDEO_SINK_WIDTH (v4l2sink) = v4l2sink->video_width;
687   GST_VIDEO_SINK_HEIGHT (v4l2sink) = v4l2sink->video_height;
688
689   v4l2sink->current_caps = gst_caps_ref (caps);
690
691   return TRUE;
692
693   /* ERRORS */
694 stop_failed:
695   {
696     GST_DEBUG_OBJECT (v4l2sink, "failed to stop streaming");
697     return FALSE;
698   }
699 invalid_format:
700   {
701     /* error already posted */
702     GST_DEBUG_OBJECT (v4l2sink, "can't set format");
703     return FALSE;
704   }
705 no_streaming:
706   {
707     GST_DEBUG_OBJECT (v4l2sink, "we don't support streaming");
708     return FALSE;
709   }
710 no_pool:
711   {
712     GST_DEBUG_OBJECT (v4l2sink, "can't create new pool");
713     return FALSE;
714   }
715 }
716
717 /* called after A/V sync to render frame */
718 static GstFlowReturn
719 gst_v4l2sink_show_frame (GstBaseSink * bsink, GstBuffer * buf)
720 {
721   GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
722   GstBuffer *newbuf = NULL;
723   GstMetaV4l2 *meta;
724
725   GST_DEBUG_OBJECT (v4l2sink, "render buffer: %p", buf);
726
727   meta = GST_META_V4L2_GET (buf);
728
729   if (meta == NULL) {
730     guint8 *data;
731     gsize size;
732
733     /* not our buffer */
734     GST_DEBUG_OBJECT (v4l2sink, "slow-path.. need to memcpy");
735     newbuf = gst_v4l2_buffer_pool_get (v4l2sink->pool, TRUE);
736
737     data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
738     gst_buffer_fill (newbuf, 0, data, size);
739     gst_buffer_unmap (buf, data, size);
740
741     GST_DEBUG_OBJECT (v4l2sink, "render copied buffer: %p", newbuf);
742
743     buf = newbuf;
744   }
745
746   if (!gst_v4l2_buffer_pool_qbuf (v4l2sink->pool, buf))
747     goto queue_failed;
748
749   if (v4l2sink->state == STATE_PENDING_STREAMON) {
750     if (!gst_v4l2_object_start_streaming (v4l2sink->v4l2object)) {
751       return GST_FLOW_ERROR;
752     }
753     v4l2sink->state = STATE_STREAMING;
754   }
755
756   if (!newbuf) {
757     gst_buffer_ref (buf);
758   }
759
760   /* if the driver has more than one buffer, ie. more than just the one we
761    * just queued, then dequeue one immediately to make it available via
762    * _buffer_alloc():
763    */
764   if (gst_v4l2_buffer_pool_available_buffers (v4l2sink->pool) >
765       v4l2sink->min_queued_bufs) {
766     GstBuffer *v4l2buf = gst_v4l2_buffer_pool_dqbuf (v4l2sink->pool);
767
768     /* note: if we get a buf, we don't want to use it directly (because
769      * someone else could still hold a ref).. but instead we release our
770      * reference to it, and if no one else holds a ref it will be returned
771      * to the pool of available buffers..  and if not, we keep looping.
772      */
773     if (v4l2buf) {
774       gst_buffer_unref (v4l2buf);
775     }
776   }
777
778   return GST_FLOW_OK;
779
780   /* ERRORS */
781 queue_failed:
782   {
783     GST_DEBUG_OBJECT (v4l2sink, "failed to queue buffer");
784     return GST_FLOW_ERROR;
785   }
786 }
787
788 #ifdef HAVE_XVIDEO
789 static void
790 gst_v4l2sink_navigation_send_event (GstNavigation * navigation,
791     GstStructure * structure)
792 {
793   GstV4l2Sink *v4l2sink = GST_V4L2SINK (navigation);
794   GstV4l2Xv *xv = v4l2sink->v4l2object->xv;
795   GstPad *peer;
796
797   if (!xv)
798     return;
799
800   if ((peer = gst_pad_get_peer (GST_VIDEO_SINK_PAD (v4l2sink)))) {
801     GstVideoRectangle rect;
802     gdouble x, y, xscale = 1.0, yscale = 1.0;
803
804     gst_v4l2_xoverlay_get_render_rect (v4l2sink->v4l2object, &rect);
805
806     /* We calculate scaling using the original video frames geometry to
807      * include pixel aspect ratio scaling.
808      */
809     xscale = (gdouble) v4l2sink->video_width / rect.w;
810     yscale = (gdouble) v4l2sink->video_height / rect.h;
811
812     /* Converting pointer coordinates to the non scaled geometry */
813     if (gst_structure_get_double (structure, "pointer_x", &x)) {
814       x = MIN (x, rect.x + rect.w);
815       x = MAX (x - rect.x, 0);
816       gst_structure_set (structure, "pointer_x", G_TYPE_DOUBLE,
817           (gdouble) x * xscale, NULL);
818     }
819     if (gst_structure_get_double (structure, "pointer_y", &y)) {
820       y = MIN (y, rect.y + rect.h);
821       y = MAX (y - rect.y, 0);
822       gst_structure_set (structure, "pointer_y", G_TYPE_DOUBLE,
823           (gdouble) y * yscale, NULL);
824     }
825
826     gst_pad_send_event (peer, gst_event_new_navigation (structure));
827     gst_object_unref (peer);
828   }
829 }
830 #endif