tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.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 static gboolean
102 gst_v4l2sink_iface_supported (GstImplementsInterface * iface, GType iface_type)
103 {
104   GstV4l2Object *v4l2object = GST_V4L2SINK (iface)->v4l2object;
105
106 #ifdef HAVE_XVIDEO
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);
112 #else
113   g_assert (iface_type == GST_TYPE_COLOR_BALANCE ||
114       iface_type == GST_TYPE_VIDEO_ORIENTATION ||
115       iface_type == GST_TYPE_TUNER);
116 #endif
117
118   if (v4l2object->video_fd == -1)
119     return FALSE;
120
121 #ifdef HAVE_XVIDEO
122   if (!GST_V4L2_IS_OVERLAY (v4l2object)) {
123     if (iface_type == GST_TYPE_X_OVERLAY || iface_type == GST_TYPE_NAVIGATION)
124       return FALSE;
125   }
126 #endif
127
128   return TRUE;
129 }
130
131 static void
132 gst_v4l2sink_interface_init (GstImplementsInterfaceClass * klass)
133 {
134   /*
135    * default virtual functions
136    */
137   klass->supported = gst_v4l2sink_iface_supported;
138 }
139
140 #ifdef HAVE_XVIDEO
141 static void gst_v4l2sink_navigation_send_event (GstNavigation * navigation,
142     GstStructure * structure);
143 static void
144 gst_v4l2sink_navigation_init (GstNavigationInterface * iface)
145 {
146   iface->send_event = gst_v4l2sink_navigation_send_event;
147 }
148 #endif
149
150 static void
151 gst_v4l2sink_init_interfaces (GType type)
152 {
153   static const GInterfaceInfo v4l2iface_info = {
154     (GInterfaceInitFunc) gst_v4l2sink_interface_init,
155     NULL,
156     NULL,
157   };
158   static const GInterfaceInfo v4l2_tuner_info = {
159     (GInterfaceInitFunc) gst_v4l2sink_tuner_interface_init,
160     NULL,
161     NULL,
162   };
163 #ifdef HAVE_XVIDEO
164   static const GInterfaceInfo v4l2_xoverlay_info = {
165     (GInterfaceInitFunc) gst_v4l2sink_xoverlay_interface_init,
166     NULL,
167     NULL,
168   };
169   static const GInterfaceInfo v4l2_navigation_info = {
170     (GInterfaceInitFunc) gst_v4l2sink_navigation_init,
171     NULL,
172     NULL,
173   };
174 #endif
175   static const GInterfaceInfo v4l2_colorbalance_info = {
176     (GInterfaceInitFunc) gst_v4l2sink_color_balance_interface_init,
177     NULL,
178     NULL,
179   };
180   static const GInterfaceInfo v4l2_videoorientation_info = {
181     (GInterfaceInitFunc) gst_v4l2sink_video_orientation_interface_init,
182     NULL,
183     NULL,
184   };
185   static const GInterfaceInfo v4l2_propertyprobe_info = {
186     (GInterfaceInitFunc) gst_v4l2sink_property_probe_interface_init,
187     NULL,
188     NULL,
189   };
190
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);
194 #ifdef HAVE_XVIDEO
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);
198 #endif
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);
205 }
206
207
208 GST_BOILERPLATE_FULL (GstV4l2Sink, gst_v4l2sink, GstVideoSink,
209     GST_TYPE_VIDEO_SINK, gst_v4l2sink_init_interfaces);
210
211
212 static void gst_v4l2sink_dispose (GObject * object);
213 static void gst_v4l2sink_finalize (GstV4l2Sink * v4l2sink);
214
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);
220
221
222 /* GstElement methods: */
223 static GstStateChangeReturn gst_v4l2sink_change_state (GstElement * element,
224     GstStateChange transition);
225
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,
232     GstBuffer * buf);
233
234 static void
235 gst_v4l2sink_base_init (gpointer g_class)
236 {
237   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
238   GstV4l2SinkClass *gstv4l2sink_class = GST_V4L2SINK_CLASS (g_class);
239   GstPadTemplate *pad_template;
240
241   gstv4l2sink_class->v4l2_class_devices = NULL;
242
243   GST_DEBUG_CATEGORY_INIT (v4l2sink_debug, "v4l2sink", 0, "V4L2 sink element");
244
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>,");
248
249   pad_template =
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);
254 }
255
256 static void
257 gst_v4l2sink_class_init (GstV4l2SinkClass * klass)
258 {
259   GObjectClass *gobject_class;
260   GstElementClass *element_class;
261   GstBaseSinkClass *basesink_class;
262
263   gobject_class = G_OBJECT_CLASS (klass);
264   element_class = GST_ELEMENT_CLASS (klass);
265   basesink_class = GST_BASE_SINK_CLASS (klass);
266
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;
271
272   element_class->change_state = gst_v4l2sink_change_state;
273
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));
303
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));
320
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);
325 }
326
327 static void
328 gst_v4l2sink_init (GstV4l2Sink * v4l2sink, GstV4l2SinkClass * klass)
329 {
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);
333
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)
338    */
339   g_object_set (v4l2sink, "device", "/dev/video1", NULL);
340
341   /* number of buffers requested */
342   v4l2sink->num_buffers = PROP_DEF_QUEUE_SIZE;
343   v4l2sink->min_queued_bufs = PROP_DEF_MIN_QUEUED_BUFS;
344
345   v4l2sink->probed_caps = NULL;
346   v4l2sink->current_caps = NULL;
347
348   v4l2sink->overlay_fields_set = 0;
349   v4l2sink->crop_fields_set = 0;
350   v4l2sink->state = 0;
351 }
352
353
354 static void
355 gst_v4l2sink_dispose (GObject * object)
356 {
357   GstV4l2Sink *v4l2sink = GST_V4L2SINK (object);
358
359   if (v4l2sink->probed_caps) {
360     gst_caps_unref (v4l2sink->probed_caps);
361   }
362
363   if (v4l2sink->current_caps) {
364     gst_caps_unref (v4l2sink->current_caps);
365   }
366
367   G_OBJECT_CLASS (parent_class)->dispose (object);
368 }
369
370
371 static void
372 gst_v4l2sink_finalize (GstV4l2Sink * v4l2sink)
373 {
374   gst_v4l2_object_destroy (v4l2sink->v4l2object);
375
376   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (v4l2sink));
377 }
378
379
380 /*
381  * State values
382  */
383 enum
384 {
385   STATE_OFF = 0,
386   STATE_PENDING_STREAMON,
387   STATE_STREAMING
388 };
389
390 /*
391  * flags to indicate which overlay/crop properties the user has set (and
392  * therefore which ones should override the defaults from the driver)
393  */
394 enum
395 {
396   RECT_TOP_SET = 0x01,
397   RECT_LEFT_SET = 0x02,
398   RECT_WIDTH_SET = 0x04,
399   RECT_HEIGHT_SET = 0x08
400 };
401
402 static void
403 gst_v4l2sink_sync_overlay_fields (GstV4l2Sink * v4l2sink)
404 {
405   if (!v4l2sink->overlay_fields_set)
406     return;
407
408   if (GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
409
410     gint fd = v4l2sink->v4l2object->video_fd;
411     struct v4l2_format format;
412
413     memset (&format, 0x00, sizeof (struct v4l2_format));
414     format.type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
415
416     if (v4l2_ioctl (fd, VIDIOC_G_FMT, &format) < 0) {
417       GST_WARNING_OBJECT (v4l2sink, "VIDIOC_G_FMT failed");
418       return;
419     }
420
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);
426
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;
435
436     if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0) {
437       GST_WARNING_OBJECT (v4l2sink, "VIDIOC_S_FMT failed");
438       return;
439     }
440
441     v4l2sink->overlay_fields_set = 0;
442     v4l2sink->overlay = format.fmt.win.w;
443   }
444 }
445
446 static void
447 gst_v4l2sink_sync_crop_fields (GstV4l2Sink * v4l2sink)
448 {
449   if (!v4l2sink->crop_fields_set)
450     return;
451
452   if (GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
453
454     gint fd = v4l2sink->v4l2object->video_fd;
455     struct v4l2_crop crop;
456
457     memset (&crop, 0x00, sizeof (struct v4l2_crop));
458     crop.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
459
460     if (v4l2_ioctl (fd, VIDIOC_G_CROP, &crop) < 0) {
461       GST_WARNING_OBJECT (v4l2sink, "VIDIOC_G_CROP failed");
462       return;
463     }
464
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);
470
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;
479
480     if (v4l2_ioctl (fd, VIDIOC_S_CROP, &crop) < 0) {
481       GST_WARNING_OBJECT (v4l2sink, "VIDIOC_S_CROP failed");
482       return;
483     }
484
485     v4l2sink->crop_fields_set = 0;
486     v4l2sink->crop = crop.c;
487   }
488 }
489
490
491 static void
492 gst_v4l2sink_set_property (GObject * object,
493     guint prop_id, const GValue * value, GParamSpec * pspec)
494 {
495   GstV4l2Sink *v4l2sink = GST_V4L2SINK (object);
496
497   if (!gst_v4l2_object_set_property_helper (v4l2sink->v4l2object,
498           prop_id, value, pspec)) {
499     switch (prop_id) {
500       case PROP_QUEUE_SIZE:
501         v4l2sink->num_buffers = g_value_get_uint (value);
502         break;
503       case PROP_MIN_QUEUED_BUFS:
504         v4l2sink->min_queued_bufs = g_value_get_uint (value);
505         break;
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);
510         break;
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);
515         break;
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);
520         break;
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);
525         break;
526       case PROP_CROP_TOP:
527         v4l2sink->crop.top = g_value_get_int (value);
528         v4l2sink->crop_fields_set |= RECT_TOP_SET;
529         gst_v4l2sink_sync_crop_fields (v4l2sink);
530         break;
531       case PROP_CROP_LEFT:
532         v4l2sink->crop.left = g_value_get_int (value);
533         v4l2sink->crop_fields_set |= RECT_LEFT_SET;
534         gst_v4l2sink_sync_crop_fields (v4l2sink);
535         break;
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);
540         break;
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);
545         break;
546       default:
547         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
548         break;
549     }
550   }
551 }
552
553
554 static void
555 gst_v4l2sink_get_property (GObject * object,
556     guint prop_id, GValue * value, GParamSpec * pspec)
557 {
558   GstV4l2Sink *v4l2sink = GST_V4L2SINK (object);
559
560   if (!gst_v4l2_object_get_property_helper (v4l2sink->v4l2object,
561           prop_id, value, pspec)) {
562     switch (prop_id) {
563       case PROP_QUEUE_SIZE:
564         g_value_set_uint (value, v4l2sink->num_buffers);
565         break;
566       case PROP_MIN_QUEUED_BUFS:
567         g_value_set_uint (value, v4l2sink->min_queued_bufs);
568         break;
569       case PROP_OVERLAY_TOP:
570         g_value_set_int (value, v4l2sink->overlay.top);
571         break;
572       case PROP_OVERLAY_LEFT:
573         g_value_set_int (value, v4l2sink->overlay.left);
574         break;
575       case PROP_OVERLAY_WIDTH:
576         g_value_set_uint (value, v4l2sink->overlay.width);
577         break;
578       case PROP_OVERLAY_HEIGHT:
579         g_value_set_uint (value, v4l2sink->overlay.height);
580         break;
581       case PROP_CROP_TOP:
582         g_value_set_int (value, v4l2sink->crop.top);
583         break;
584       case PROP_CROP_LEFT:
585         g_value_set_int (value, v4l2sink->crop.left);
586         break;
587       case PROP_CROP_WIDTH:
588         g_value_set_uint (value, v4l2sink->crop.width);
589         break;
590       case PROP_CROP_HEIGHT:
591         g_value_set_uint (value, v4l2sink->crop.height);
592         break;
593       default:
594         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
595         break;
596     }
597   }
598 }
599
600 static GstStateChangeReturn
601 gst_v4l2sink_change_state (GstElement * element, GstStateChange transition)
602 {
603   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
604   GstV4l2Sink *v4l2sink = GST_V4L2SINK (element);
605
606   GST_DEBUG_OBJECT (v4l2sink, "%d -> %d",
607       GST_STATE_TRANSITION_CURRENT (transition),
608       GST_STATE_TRANSITION_NEXT (transition));
609
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;
615       break;
616     default:
617       break;
618   }
619
620   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
621
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;
627         }
628         v4l2sink->state = STATE_PENDING_STREAMON;
629       }
630       break;
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;
639       break;
640     default:
641       break;
642   }
643
644   return ret;
645 }
646
647
648 static GstCaps *
649 gst_v4l2sink_get_caps (GstBaseSink * bsink)
650 {
651   GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
652   GstCaps *ret;
653   GSList *walk;
654   GSList *formats;
655
656   if (!GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
657     /* FIXME: copy? */
658     GST_DEBUG_OBJECT (v4l2sink, "device is not open");
659     return
660         gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD
661             (v4l2sink)));
662   }
663
664   if (v4l2sink->probed_caps) {
665     LOG_CAPS (v4l2sink, v4l2sink->probed_caps);
666     return gst_caps_ref (v4l2sink->probed_caps);
667   }
668
669   formats = gst_v4l2_object_get_format_list (v4l2sink->v4l2object);
670
671   ret = gst_caps_new_empty ();
672
673   for (walk = formats; walk; walk = walk->next) {
674     struct v4l2_fmtdesc *format;
675
676     GstStructure *template;
677
678     format = (struct v4l2_fmtdesc *) walk->data;
679
680     template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
681
682     if (template) {
683       GstCaps *tmp;
684
685       tmp =
686           gst_v4l2_object_probe_caps_for_format (v4l2sink->v4l2object,
687           format->pixelformat, template);
688       if (tmp)
689         gst_caps_append (ret, tmp);
690
691       gst_structure_free (template);
692     } else {
693       GST_DEBUG_OBJECT (v4l2sink, "unknown format %u", format->pixelformat);
694     }
695   }
696
697   v4l2sink->probed_caps = gst_caps_ref (ret);
698
699   GST_INFO_OBJECT (v4l2sink, "probed caps: %p", ret);
700   LOG_CAPS (v4l2sink, ret);
701
702   return ret;
703 }
704
705 static gboolean
706 gst_v4l2sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
707 {
708   GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
709   gint w = 0, h = 0;
710   gboolean interlaced;
711   struct v4l2_fmtdesc *format;
712   guint fps_n, fps_d;
713   guint size;
714
715   LOG_CAPS (v4l2sink, caps);
716
717   if (!GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
718     GST_DEBUG_OBJECT (v4l2sink, "device is not open");
719     return FALSE;
720   }
721
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!");
727       return TRUE;
728     }
729     GST_DEBUG_OBJECT (v4l2sink, "no they aren't!");
730   }
731
732   if (v4l2sink->pool) {
733     /* TODO: if we've already allocated buffers, we probably need to
734      * do something here to free and reallocate....
735      *
736      *   gst_v4l2_object_stop_streaming()
737      *   gst_v4l2_buffer_pool_destroy()
738      *
739      */
740     GST_DEBUG_OBJECT (v4l2sink, "warning, changing caps not supported yet");
741     return FALSE;
742   }
743
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);
748     return FALSE;
749   }
750
751   if (!format) {
752     GST_DEBUG_OBJECT (v4l2sink, "unrecognized caps!!");
753     return FALSE;
754   }
755
756   if (!gst_v4l2_object_set_format (v4l2sink->v4l2object, format->pixelformat,
757           w, h, interlaced)) {
758     /* error already posted */
759     return FALSE;
760   }
761
762   v4l2sink->video_width = w;
763   v4l2sink->video_height = h;
764
765   /* TODO: videosink width/height should be scaled according to
766    * pixel-aspect-ratio
767    */
768   GST_VIDEO_SINK_WIDTH (v4l2sink) = w;
769   GST_VIDEO_SINK_HEIGHT (v4l2sink) = h;
770
771   v4l2sink->current_caps = gst_caps_ref (caps);
772
773   return TRUE;
774 }
775
776 /* buffer alloc function to implement pad_alloc for upstream element */
777 static GstFlowReturn
778 gst_v4l2sink_buffer_alloc (GstBaseSink * bsink, guint64 offset, guint size,
779     GstCaps * caps, GstBuffer ** buf)
780 {
781   GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
782   GstV4l2Buffer *v4l2buf;
783
784   if (v4l2sink->v4l2object->vcap.capabilities & V4L2_CAP_STREAMING) {
785
786     /* initialize the buffer pool if not initialized yet (first buffer): */
787     if (G_UNLIKELY (!v4l2sink->pool)) {
788
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;
792       }
793
794       GST_V4L2_CHECK_OPEN (v4l2sink->v4l2object);
795
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;
801       }
802
803       gst_v4l2sink_sync_overlay_fields (v4l2sink);
804       gst_v4l2sink_sync_crop_fields (v4l2sink);
805
806 #ifdef HAVE_XVIDEO
807       gst_v4l2_xoverlay_prepare_xwindow_id (v4l2sink->v4l2object, TRUE);
808 #endif
809
810       v4l2sink->state = STATE_PENDING_STREAMON;
811
812       GST_INFO_OBJECT (v4l2sink, "outputting buffers via mmap()");
813
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");
817       }
818     }
819
820     v4l2buf = gst_v4l2_buffer_pool_get (v4l2sink->pool, TRUE);
821
822     if (G_LIKELY (v4l2buf)) {
823       GST_DEBUG_OBJECT (v4l2sink, "allocated buffer: %p", v4l2buf);
824       *buf = GST_BUFFER (v4l2buf);
825       return GST_FLOW_OK;
826     } else {
827       GST_DEBUG_OBJECT (v4l2sink, "failed to allocate buffer");
828       return GST_FLOW_ERROR;
829     }
830
831   } else {
832     GST_ERROR_OBJECT (v4l2sink, "only supporting streaming mode for now...");
833     return GST_FLOW_ERROR;
834   }
835 }
836
837 /* called after A/V sync to render frame */
838 static GstFlowReturn
839 gst_v4l2sink_show_frame (GstBaseSink * bsink, GstBuffer * buf)
840 {
841   GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
842   GstBuffer *newbuf = NULL;
843
844   GST_DEBUG_OBJECT (v4l2sink, "render buffer: %p", buf);
845
846   if (!GST_IS_V4L2_BUFFER (buf)) {
847     GstFlowReturn ret;
848
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.
855      *
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!
864      */
865     if (buf->parent &&
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);
870     }
871
872     GST_DEBUG_OBJECT (v4l2sink, "slow-path.. I got a %s so I need to memcpy",
873         g_type_name (G_OBJECT_TYPE (buf)));
874
875     ret = gst_v4l2sink_buffer_alloc (bsink,
876         GST_BUFFER_OFFSET (buf), GST_BUFFER_SIZE (buf), GST_BUFFER_CAPS (buf),
877         &newbuf);
878
879     if (GST_FLOW_OK != ret) {
880       GST_DEBUG_OBJECT (v4l2sink,
881           "dropping frame!  Consider increasing 'queue-size' property!");
882       return GST_FLOW_OK;
883     }
884
885     memcpy (GST_BUFFER_DATA (newbuf),
886         GST_BUFFER_DATA (buf),
887         MIN (GST_BUFFER_SIZE (newbuf), GST_BUFFER_SIZE (buf)));
888
889     GST_DEBUG_OBJECT (v4l2sink, "render copied buffer: %p", newbuf);
890
891     buf = newbuf;
892   }
893
894   if (!gst_v4l2_buffer_pool_qbuf (v4l2sink->pool, GST_V4L2_BUFFER (buf))) {
895     return GST_FLOW_ERROR;
896   }
897   if (v4l2sink->state == STATE_PENDING_STREAMON) {
898     if (!gst_v4l2_object_start_streaming (v4l2sink->v4l2object)) {
899       return GST_FLOW_ERROR;
900     }
901     v4l2sink->state = STATE_STREAMING;
902   }
903
904   if (!newbuf) {
905     gst_buffer_ref (buf);
906   }
907
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
910    * _buffer_alloc():
911    */
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);
915
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.
920      */
921     if (v4l2buf) {
922       gst_buffer_unref (GST_BUFFER (v4l2buf));
923     }
924   }
925
926   return GST_FLOW_OK;
927 }
928
929 #ifdef HAVE_XVIDEO
930 static void
931 gst_v4l2sink_navigation_send_event (GstNavigation * navigation,
932     GstStructure * structure)
933 {
934   GstV4l2Sink *v4l2sink = GST_V4L2SINK (navigation);
935   GstV4l2Xv *xv = v4l2sink->v4l2object->xv;
936   GstPad *peer;
937
938   if (!xv)
939     return;
940
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;
944
945     gst_v4l2_xoverlay_get_render_rect (v4l2sink->v4l2object, &rect);
946
947     /* We calculate scaling using the original video frames geometry to
948      * include pixel aspect ratio scaling.
949      */
950     xscale = (gdouble) v4l2sink->video_width / rect.w;
951     yscale = (gdouble) v4l2sink->video_height / rect.h;
952
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);
959     }
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);
965     }
966
967     gst_pad_send_event (peer, gst_event_new_navigation (structure));
968     gst_object_unref (peer);
969   }
970 }
971 #endif