6b3da4e1a861e2176df995d143951763d852df1c
[platform/upstream/gst-plugins-good.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   g_assert (iface_type == GST_TYPE_TUNER ||
107 #ifdef HAVE_XVIDEO
108       iface_type == GST_TYPE_X_OVERLAY ||
109       iface_type == GST_TYPE_NAVIGATION ||
110 #endif
111       iface_type == GST_TYPE_COLOR_BALANCE ||
112       iface_type == GST_TYPE_VIDEO_ORIENTATION);
113
114   if (v4l2object->video_fd == -1)
115     return FALSE;
116
117 #ifdef HAVE_XVIDEO
118   if (!GST_V4L2_IS_OVERLAY (v4l2object)) {
119     if (iface_type == GST_TYPE_X_OVERLAY || iface_type == GST_TYPE_NAVIGATION)
120       return FALSE;
121   }
122 #endif
123
124   return TRUE;
125 }
126
127 static void
128 gst_v4l2sink_interface_init (GstImplementsInterfaceClass * klass)
129 {
130   /*
131    * default virtual functions
132    */
133   klass->supported = gst_v4l2sink_iface_supported;
134 }
135
136 #ifdef HAVE_XVIDEO
137 static void gst_v4l2sink_navigation_send_event (GstNavigation * navigation,
138     GstStructure * structure);
139 static void
140 gst_v4l2sink_navigation_init (GstNavigationInterface * iface)
141 {
142   iface->send_event = gst_v4l2sink_navigation_send_event;
143 }
144 #endif
145
146 static void
147 gst_v4l2sink_init_interfaces (GType type)
148 {
149   static const GInterfaceInfo v4l2iface_info = {
150     (GInterfaceInitFunc) gst_v4l2sink_interface_init,
151     NULL,
152     NULL,
153   };
154   static const GInterfaceInfo v4l2_tuner_info = {
155     (GInterfaceInitFunc) gst_v4l2sink_tuner_interface_init,
156     NULL,
157     NULL,
158   };
159 #ifdef HAVE_XVIDEO
160   static const GInterfaceInfo v4l2_xoverlay_info = {
161     (GInterfaceInitFunc) gst_v4l2sink_xoverlay_interface_init,
162     NULL,
163     NULL,
164   };
165   static const GInterfaceInfo v4l2_navigation_info = {
166     (GInterfaceInitFunc) gst_v4l2sink_navigation_init,
167     NULL,
168     NULL,
169   };
170 #endif
171   static const GInterfaceInfo v4l2_colorbalance_info = {
172     (GInterfaceInitFunc) gst_v4l2sink_color_balance_interface_init,
173     NULL,
174     NULL,
175   };
176   static const GInterfaceInfo v4l2_videoorientation_info = {
177     (GInterfaceInitFunc) gst_v4l2sink_video_orientation_interface_init,
178     NULL,
179     NULL,
180   };
181   static const GInterfaceInfo v4l2_propertyprobe_info = {
182     (GInterfaceInitFunc) gst_v4l2sink_property_probe_interface_init,
183     NULL,
184     NULL,
185   };
186
187   g_type_add_interface_static (type,
188       GST_TYPE_IMPLEMENTS_INTERFACE, &v4l2iface_info);
189   g_type_add_interface_static (type, GST_TYPE_TUNER, &v4l2_tuner_info);
190 #ifdef HAVE_XVIDEO
191   g_type_add_interface_static (type, GST_TYPE_X_OVERLAY, &v4l2_xoverlay_info);
192   g_type_add_interface_static (type,
193       GST_TYPE_NAVIGATION, &v4l2_navigation_info);
194 #endif
195   g_type_add_interface_static (type,
196       GST_TYPE_COLOR_BALANCE, &v4l2_colorbalance_info);
197   g_type_add_interface_static (type,
198       GST_TYPE_VIDEO_ORIENTATION, &v4l2_videoorientation_info);
199   g_type_add_interface_static (type, GST_TYPE_PROPERTY_PROBE,
200       &v4l2_propertyprobe_info);
201 }
202
203
204 GST_BOILERPLATE_FULL (GstV4l2Sink, gst_v4l2sink, GstVideoSink,
205     GST_TYPE_VIDEO_SINK, gst_v4l2sink_init_interfaces);
206
207
208 static void gst_v4l2sink_dispose (GObject * object);
209 static void gst_v4l2sink_finalize (GstV4l2Sink * v4l2sink);
210
211 /* GObject methods: */
212 static void gst_v4l2sink_set_property (GObject * object, guint prop_id,
213     const GValue * value, GParamSpec * pspec);
214 static void gst_v4l2sink_get_property (GObject * object, guint prop_id,
215     GValue * value, GParamSpec * pspec);
216
217
218 /* GstElement methods: */
219 static GstStateChangeReturn gst_v4l2sink_change_state (GstElement * element,
220     GstStateChange transition);
221
222 /* GstBaseSink methods: */
223 static GstCaps *gst_v4l2sink_get_caps (GstBaseSink * bsink);
224 static gboolean gst_v4l2sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
225 static GstFlowReturn gst_v4l2sink_buffer_alloc (GstBaseSink * bsink,
226     guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
227 static GstFlowReturn gst_v4l2sink_show_frame (GstBaseSink * bsink,
228     GstBuffer * buf);
229
230 static void
231 gst_v4l2sink_base_init (gpointer g_class)
232 {
233   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
234   GstV4l2SinkClass *gstv4l2sink_class = GST_V4L2SINK_CLASS (g_class);
235
236   gstv4l2sink_class->v4l2_class_devices = NULL;
237
238   GST_DEBUG_CATEGORY_INIT (v4l2sink_debug, "v4l2sink", 0, "V4L2 sink element");
239
240   gst_element_class_set_details_simple (gstelement_class,
241       "Video (video4linux2) Sink", "Sink/Video",
242       "Displays frames on a video4linux2 device", "Rob Clark <rob@ti.com>,");
243
244   gst_element_class_add_pad_template
245       (gstelement_class,
246       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
247           gst_v4l2_object_get_all_caps ()));
248 }
249
250 static void
251 gst_v4l2sink_class_init (GstV4l2SinkClass * klass)
252 {
253   GObjectClass *gobject_class;
254   GstElementClass *element_class;
255   GstBaseSinkClass *basesink_class;
256
257   gobject_class = G_OBJECT_CLASS (klass);
258   element_class = GST_ELEMENT_CLASS (klass);
259   basesink_class = GST_BASE_SINK_CLASS (klass);
260
261   gobject_class->dispose = gst_v4l2sink_dispose;
262   gobject_class->finalize = (GObjectFinalizeFunc) gst_v4l2sink_finalize;
263   gobject_class->set_property = gst_v4l2sink_set_property;
264   gobject_class->get_property = gst_v4l2sink_get_property;
265
266   element_class->change_state = gst_v4l2sink_change_state;
267
268   gst_v4l2_object_install_properties_helper (gobject_class,
269       DEFAULT_PROP_DEVICE);
270   g_object_class_install_property (gobject_class, PROP_QUEUE_SIZE,
271       g_param_spec_uint ("queue-size", "Queue size",
272           "Number of buffers to be enqueud in the driver in streaming mode",
273           GST_V4L2_MIN_BUFFERS, GST_V4L2_MAX_BUFFERS, PROP_DEF_QUEUE_SIZE,
274           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
275   g_object_class_install_property (gobject_class, PROP_MIN_QUEUED_BUFS,
276       g_param_spec_uint ("min-queued-bufs", "Minimum queued bufs",
277           "Minimum number of queued bufs; v4l2sink won't dqbuf if the driver "
278           "doesn't have more than this number (which normally you shouldn't change)",
279           0, GST_V4L2_MAX_BUFFERS, PROP_DEF_MIN_QUEUED_BUFS,
280           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
281   g_object_class_install_property (gobject_class, PROP_OVERLAY_TOP,
282       g_param_spec_int ("overlay-top", "Overlay top",
283           "The topmost (y) coordinate of the video overlay; top left corner of screen is 0,0",
284           G_MININT, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
285   g_object_class_install_property (gobject_class, PROP_OVERLAY_LEFT,
286       g_param_spec_int ("overlay-left", "Overlay left",
287           "The leftmost (x) coordinate of the video overlay; top left corner of screen is 0,0",
288           G_MININT, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
289   g_object_class_install_property (gobject_class, PROP_OVERLAY_WIDTH,
290       g_param_spec_uint ("overlay-width", "Overlay width",
291           "The width of the video overlay; default is equal to negotiated image width",
292           0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
293   g_object_class_install_property (gobject_class, PROP_OVERLAY_HEIGHT,
294       g_param_spec_uint ("overlay-height", "Overlay height",
295           "The height of the video overlay; default is equal to negotiated image height",
296           0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
297
298   g_object_class_install_property (gobject_class, PROP_CROP_TOP,
299       g_param_spec_int ("crop-top", "Crop top",
300           "The topmost (y) coordinate of the video crop; top left corner of image is 0,0",
301           0x80000000, 0x7fffffff, 0, G_PARAM_READWRITE));
302   g_object_class_install_property (gobject_class, PROP_CROP_LEFT,
303       g_param_spec_int ("crop-left", "Crop left",
304           "The leftmost (x) coordinate of the video crop; top left corner of image is 0,0",
305           0x80000000, 0x7fffffff, 0, G_PARAM_READWRITE));
306   g_object_class_install_property (gobject_class, PROP_CROP_WIDTH,
307       g_param_spec_uint ("crop-width", "Crop width",
308           "The width of the video crop; default is equal to negotiated image width",
309           0, 0xffffffff, 0, G_PARAM_READWRITE));
310   g_object_class_install_property (gobject_class, PROP_CROP_HEIGHT,
311       g_param_spec_uint ("crop-height", "Crop height",
312           "The height of the video crop; default is equal to negotiated image height",
313           0, 0xffffffff, 0, G_PARAM_READWRITE));
314
315   basesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_v4l2sink_get_caps);
316   basesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_v4l2sink_set_caps);
317   basesink_class->buffer_alloc = GST_DEBUG_FUNCPTR (gst_v4l2sink_buffer_alloc);
318   basesink_class->render = GST_DEBUG_FUNCPTR (gst_v4l2sink_show_frame);
319 }
320
321 static void
322 gst_v4l2sink_init (GstV4l2Sink * v4l2sink, GstV4l2SinkClass * klass)
323 {
324   v4l2sink->v4l2object = gst_v4l2_object_new (GST_ELEMENT (v4l2sink),
325       V4L2_BUF_TYPE_VIDEO_OUTPUT, DEFAULT_PROP_DEVICE,
326       gst_v4l2_get_output, gst_v4l2_set_output, NULL);
327
328   /* same default value for video output device as is used for
329    * v4l2src/capture is no good..  so lets set a saner default
330    * (which can be overridden by the one creating the v4l2sink
331    * after the constructor returns)
332    */
333   g_object_set (v4l2sink, "device", "/dev/video1", NULL);
334
335   /* number of buffers requested */
336   v4l2sink->num_buffers = PROP_DEF_QUEUE_SIZE;
337   v4l2sink->min_queued_bufs = PROP_DEF_MIN_QUEUED_BUFS;
338
339   v4l2sink->probed_caps = NULL;
340   v4l2sink->current_caps = NULL;
341
342   v4l2sink->overlay_fields_set = 0;
343   v4l2sink->crop_fields_set = 0;
344   v4l2sink->state = 0;
345 }
346
347
348 static void
349 gst_v4l2sink_dispose (GObject * object)
350 {
351   GstV4l2Sink *v4l2sink = GST_V4L2SINK (object);
352
353   if (v4l2sink->probed_caps) {
354     gst_caps_unref (v4l2sink->probed_caps);
355   }
356
357   if (v4l2sink->current_caps) {
358     gst_caps_unref (v4l2sink->current_caps);
359   }
360
361   G_OBJECT_CLASS (parent_class)->dispose (object);
362 }
363
364
365 static void
366 gst_v4l2sink_finalize (GstV4l2Sink * v4l2sink)
367 {
368   gst_v4l2_object_destroy (v4l2sink->v4l2object);
369
370   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (v4l2sink));
371 }
372
373
374 /*
375  * State values
376  */
377 enum
378 {
379   STATE_OFF = 0,
380   STATE_PENDING_STREAMON,
381   STATE_STREAMING
382 };
383
384 /*
385  * flags to indicate which overlay/crop properties the user has set (and
386  * therefore which ones should override the defaults from the driver)
387  */
388 enum
389 {
390   RECT_TOP_SET = 0x01,
391   RECT_LEFT_SET = 0x02,
392   RECT_WIDTH_SET = 0x04,
393   RECT_HEIGHT_SET = 0x08
394 };
395
396 static void
397 gst_v4l2sink_sync_overlay_fields (GstV4l2Sink * v4l2sink)
398 {
399   if (!v4l2sink->overlay_fields_set)
400     return;
401
402   if (GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
403
404     gint fd = v4l2sink->v4l2object->video_fd;
405     struct v4l2_format format;
406
407     memset (&format, 0x00, sizeof (struct v4l2_format));
408     format.type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
409
410     g_return_if_fail (v4l2_ioctl (fd, VIDIOC_G_FMT, &format) >= 0);
411
412     GST_DEBUG_OBJECT (v4l2sink,
413         "setting overlay: overlay_fields_set=0x%02x, top=%d, left=%d, width=%d, height=%d",
414         v4l2sink->overlay_fields_set,
415         v4l2sink->overlay.top, v4l2sink->overlay.left,
416         v4l2sink->overlay.width, v4l2sink->overlay.height);
417
418     if (v4l2sink->overlay_fields_set & RECT_TOP_SET)
419       format.fmt.win.w.top = v4l2sink->overlay.top;
420     if (v4l2sink->overlay_fields_set & RECT_LEFT_SET)
421       format.fmt.win.w.left = v4l2sink->overlay.left;
422     if (v4l2sink->overlay_fields_set & RECT_WIDTH_SET)
423       format.fmt.win.w.width = v4l2sink->overlay.width;
424     if (v4l2sink->overlay_fields_set & RECT_HEIGHT_SET)
425       format.fmt.win.w.height = v4l2sink->overlay.height;
426
427     g_return_if_fail (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) >= 0);
428
429     v4l2sink->overlay_fields_set = 0;
430     v4l2sink->overlay = format.fmt.win.w;
431   }
432 }
433
434 static void
435 gst_v4l2sink_sync_crop_fields (GstV4l2Sink * v4l2sink)
436 {
437   if (!v4l2sink->crop_fields_set)
438     return;
439
440   if (GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
441
442     gint fd = v4l2sink->v4l2object->video_fd;
443     struct v4l2_crop crop;
444
445     memset (&crop, 0x00, sizeof (struct v4l2_crop));
446     crop.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
447
448     g_return_if_fail (v4l2_ioctl (fd, VIDIOC_G_CROP, &crop) >= 0);
449
450     GST_DEBUG_OBJECT (v4l2sink,
451         "setting crop: crop_fields_set=0x%02x, top=%d, left=%d, width=%d, height=%d",
452         v4l2sink->crop_fields_set,
453         v4l2sink->crop.top, v4l2sink->crop.left,
454         v4l2sink->crop.width, v4l2sink->crop.height);
455
456     if (v4l2sink->crop_fields_set & RECT_TOP_SET)
457       crop.c.top = v4l2sink->crop.top;
458     if (v4l2sink->crop_fields_set & RECT_LEFT_SET)
459       crop.c.left = v4l2sink->crop.left;
460     if (v4l2sink->crop_fields_set & RECT_WIDTH_SET)
461       crop.c.width = v4l2sink->crop.width;
462     if (v4l2sink->crop_fields_set & RECT_HEIGHT_SET)
463       crop.c.height = v4l2sink->crop.height;
464
465     g_return_if_fail (v4l2_ioctl (fd, VIDIOC_S_CROP, &crop) >= 0);
466
467     v4l2sink->crop_fields_set = 0;
468     v4l2sink->crop = crop.c;
469   }
470 }
471
472
473 static void
474 gst_v4l2sink_set_property (GObject * object,
475     guint prop_id, const GValue * value, GParamSpec * pspec)
476 {
477   GstV4l2Sink *v4l2sink = GST_V4L2SINK (object);
478
479   if (!gst_v4l2_object_set_property_helper (v4l2sink->v4l2object,
480           prop_id, value, pspec)) {
481     switch (prop_id) {
482       case PROP_QUEUE_SIZE:
483         v4l2sink->num_buffers = g_value_get_uint (value);
484         break;
485       case PROP_MIN_QUEUED_BUFS:
486         v4l2sink->min_queued_bufs = g_value_get_uint (value);
487         break;
488       case PROP_OVERLAY_TOP:
489         v4l2sink->overlay.top = g_value_get_int (value);
490         v4l2sink->overlay_fields_set |= RECT_TOP_SET;
491         gst_v4l2sink_sync_overlay_fields (v4l2sink);
492         break;
493       case PROP_OVERLAY_LEFT:
494         v4l2sink->overlay.left = g_value_get_int (value);
495         v4l2sink->overlay_fields_set |= RECT_LEFT_SET;
496         gst_v4l2sink_sync_overlay_fields (v4l2sink);
497         break;
498       case PROP_OVERLAY_WIDTH:
499         v4l2sink->overlay.width = g_value_get_uint (value);
500         v4l2sink->overlay_fields_set |= RECT_WIDTH_SET;
501         gst_v4l2sink_sync_overlay_fields (v4l2sink);
502         break;
503       case PROP_OVERLAY_HEIGHT:
504         v4l2sink->overlay.height = g_value_get_uint (value);
505         v4l2sink->overlay_fields_set |= RECT_HEIGHT_SET;
506         gst_v4l2sink_sync_overlay_fields (v4l2sink);
507         break;
508       case PROP_CROP_TOP:
509         v4l2sink->crop.top = g_value_get_int (value);
510         v4l2sink->crop_fields_set |= RECT_TOP_SET;
511         gst_v4l2sink_sync_crop_fields (v4l2sink);
512         break;
513       case PROP_CROP_LEFT:
514         v4l2sink->crop.left = g_value_get_int (value);
515         v4l2sink->crop_fields_set |= RECT_LEFT_SET;
516         gst_v4l2sink_sync_crop_fields (v4l2sink);
517         break;
518       case PROP_CROP_WIDTH:
519         v4l2sink->crop.width = g_value_get_uint (value);
520         v4l2sink->crop_fields_set |= RECT_WIDTH_SET;
521         gst_v4l2sink_sync_crop_fields (v4l2sink);
522         break;
523       case PROP_CROP_HEIGHT:
524         v4l2sink->crop.height = g_value_get_uint (value);
525         v4l2sink->crop_fields_set |= RECT_HEIGHT_SET;
526         gst_v4l2sink_sync_crop_fields (v4l2sink);
527         break;
528       default:
529         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
530         break;
531     }
532   }
533 }
534
535
536 static void
537 gst_v4l2sink_get_property (GObject * object,
538     guint prop_id, GValue * value, GParamSpec * pspec)
539 {
540   GstV4l2Sink *v4l2sink = GST_V4L2SINK (object);
541
542   if (!gst_v4l2_object_get_property_helper (v4l2sink->v4l2object,
543           prop_id, value, pspec)) {
544     switch (prop_id) {
545       case PROP_QUEUE_SIZE:
546         g_value_set_uint (value, v4l2sink->num_buffers);
547         break;
548       case PROP_MIN_QUEUED_BUFS:
549         g_value_set_uint (value, v4l2sink->min_queued_bufs);
550         break;
551       case PROP_OVERLAY_TOP:
552         g_value_set_int (value, v4l2sink->overlay.top);
553         break;
554       case PROP_OVERLAY_LEFT:
555         g_value_set_int (value, v4l2sink->overlay.left);
556         break;
557       case PROP_OVERLAY_WIDTH:
558         g_value_set_uint (value, v4l2sink->overlay.width);
559         break;
560       case PROP_OVERLAY_HEIGHT:
561         g_value_set_uint (value, v4l2sink->overlay.height);
562         break;
563       case PROP_CROP_TOP:
564         g_value_set_int (value, v4l2sink->crop.top);
565         break;
566       case PROP_CROP_LEFT:
567         g_value_set_int (value, v4l2sink->crop.left);
568         break;
569       case PROP_CROP_WIDTH:
570         g_value_set_uint (value, v4l2sink->crop.width);
571         break;
572       case PROP_CROP_HEIGHT:
573         g_value_set_uint (value, v4l2sink->crop.height);
574         break;
575       default:
576         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
577         break;
578     }
579   }
580 }
581
582 static GstStateChangeReturn
583 gst_v4l2sink_change_state (GstElement * element, GstStateChange transition)
584 {
585   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
586   GstV4l2Sink *v4l2sink = GST_V4L2SINK (element);
587
588   GST_DEBUG_OBJECT (v4l2sink, "%d -> %d",
589       GST_STATE_TRANSITION_CURRENT (transition),
590       GST_STATE_TRANSITION_NEXT (transition));
591
592   switch (transition) {
593     case GST_STATE_CHANGE_NULL_TO_READY:
594       /* open the device */
595       if (!gst_v4l2_object_start (v4l2sink->v4l2object))
596         return GST_STATE_CHANGE_FAILURE;
597       break;
598     default:
599       break;
600   }
601
602   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
603
604   switch (transition) {
605     case GST_STATE_CHANGE_PAUSED_TO_READY:
606       if (v4l2sink->state == STATE_STREAMING) {
607         if (!gst_v4l2_object_stop_streaming (v4l2sink->v4l2object)) {
608           return GST_STATE_CHANGE_FAILURE;
609         }
610         v4l2sink->state = STATE_PENDING_STREAMON;
611       }
612       break;
613     case GST_STATE_CHANGE_READY_TO_NULL:
614       if (NULL != v4l2sink->pool)
615         gst_v4l2_buffer_pool_destroy (v4l2sink->pool);
616       v4l2sink->pool = NULL;
617       /* close the device */
618       if (!gst_v4l2_object_stop (v4l2sink->v4l2object))
619         return GST_STATE_CHANGE_FAILURE;
620       v4l2sink->state = STATE_OFF;
621       break;
622     default:
623       break;
624   }
625
626   return ret;
627 }
628
629
630 static GstCaps *
631 gst_v4l2sink_get_caps (GstBaseSink * bsink)
632 {
633   GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
634   GstCaps *ret;
635   GSList *walk;
636   GSList *formats;
637
638   if (!GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
639     /* FIXME: copy? */
640     GST_DEBUG_OBJECT (v4l2sink, "device is not open");
641     return
642         gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD
643             (v4l2sink)));
644   }
645
646   if (v4l2sink->probed_caps) {
647     LOG_CAPS (v4l2sink, v4l2sink->probed_caps);
648     return gst_caps_ref (v4l2sink->probed_caps);
649   }
650
651   formats = gst_v4l2_object_get_format_list (v4l2sink->v4l2object);
652
653   ret = gst_caps_new_empty ();
654
655   for (walk = v4l2sink->v4l2object->formats; walk; walk = walk->next) {
656     struct v4l2_fmtdesc *format;
657
658     GstStructure *template;
659
660     format = (struct v4l2_fmtdesc *) walk->data;
661
662     template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
663
664     if (template) {
665       GstCaps *tmp;
666
667       tmp =
668           gst_v4l2_object_probe_caps_for_format (v4l2sink->v4l2object,
669           format->pixelformat, template);
670       if (tmp)
671         gst_caps_append (ret, tmp);
672
673       gst_structure_free (template);
674     } else {
675       GST_DEBUG_OBJECT (v4l2sink, "unknown format %u", format->pixelformat);
676     }
677   }
678
679   v4l2sink->probed_caps = gst_caps_ref (ret);
680
681   GST_INFO_OBJECT (v4l2sink, "probed caps: %p", ret);
682   LOG_CAPS (v4l2sink, ret);
683
684   return ret;
685 }
686
687 static gboolean
688 gst_v4l2sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
689 {
690   GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
691   gint w = 0, h = 0;
692   gboolean interlaced;
693   struct v4l2_fmtdesc *format;
694   guint fps_n, fps_d;
695   guint size;
696
697   LOG_CAPS (v4l2sink, caps);
698
699   if (!GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
700     GST_DEBUG_OBJECT (v4l2sink, "device is not open");
701     return FALSE;
702   }
703
704   if (v4l2sink->current_caps) {
705     GST_DEBUG_OBJECT (v4l2sink, "already have caps set.. are they equal?");
706     LOG_CAPS (v4l2sink, v4l2sink->current_caps);
707     if (gst_caps_is_equal (v4l2sink->current_caps, caps)) {
708       GST_DEBUG_OBJECT (v4l2sink, "yes they are!");
709       return TRUE;
710     }
711     GST_DEBUG_OBJECT (v4l2sink, "no they aren't!");
712   }
713
714   if (v4l2sink->pool) {
715     /* TODO: if we've already allocated buffers, we probably need to
716      * do something here to free and reallocate....
717      *
718      *   gst_v4l2_object_stop_streaming()
719      *   gst_v4l2_buffer_pool_destroy()
720      *
721      */
722     GST_DEBUG_OBJECT (v4l2sink, "warning, changing caps not supported yet");
723     return FALSE;
724   }
725
726   /* we want our own v4l2 type of fourcc codes */
727   if (!gst_v4l2_object_get_caps_info (v4l2sink->v4l2object, caps,
728           &format, &w, &h, &interlaced, &fps_n, &fps_d, &size)) {
729     GST_DEBUG_OBJECT (v4l2sink, "can't get capture format from caps %p", caps);
730     return FALSE;
731   }
732
733   if (!format) {
734     GST_DEBUG_OBJECT (v4l2sink, "unrecognized caps!!");
735     return FALSE;
736   }
737
738   if (!gst_v4l2_object_set_format (v4l2sink->v4l2object, format->pixelformat,
739           w, h, interlaced)) {
740     /* error already posted */
741     return FALSE;
742   }
743
744   v4l2sink->video_width = w;
745   v4l2sink->video_height = h;
746
747   /* TODO: videosink width/height should be scaled according to
748    * pixel-aspect-ratio
749    */
750   GST_VIDEO_SINK_WIDTH (v4l2sink) = w;
751   GST_VIDEO_SINK_HEIGHT (v4l2sink) = h;
752
753   v4l2sink->current_caps = gst_caps_ref (caps);
754
755   return TRUE;
756 }
757
758 /* buffer alloc function to implement pad_alloc for upstream element */
759 static GstFlowReturn
760 gst_v4l2sink_buffer_alloc (GstBaseSink * bsink, guint64 offset, guint size,
761     GstCaps * caps, GstBuffer ** buf)
762 {
763   GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
764   GstV4l2Buffer *v4l2buf;
765
766   if (v4l2sink->v4l2object->vcap.capabilities & V4L2_CAP_STREAMING) {
767
768     /* initialize the buffer pool if not initialized yet (first buffer): */
769     if (G_UNLIKELY (!v4l2sink->pool)) {
770
771       /* set_caps() might not be called yet.. so just to make sure: */
772       if (!gst_v4l2sink_set_caps (bsink, caps)) {
773         return GST_FLOW_ERROR;
774       }
775
776       GST_V4L2_CHECK_OPEN (v4l2sink->v4l2object);
777
778       if (!(v4l2sink->pool = gst_v4l2_buffer_pool_new (GST_ELEMENT (v4l2sink),
779                   v4l2sink->v4l2object->video_fd,
780                   v4l2sink->num_buffers, caps, FALSE,
781                   V4L2_BUF_TYPE_VIDEO_OUTPUT))) {
782         return GST_FLOW_ERROR;
783       }
784
785       gst_v4l2sink_sync_overlay_fields (v4l2sink);
786       gst_v4l2sink_sync_crop_fields (v4l2sink);
787
788 #ifdef HAVE_XVIDEO
789       gst_v4l2_xoverlay_prepare_xwindow_id (v4l2sink->v4l2object, TRUE);
790 #endif
791
792       v4l2sink->state = STATE_PENDING_STREAMON;
793
794       GST_INFO_OBJECT (v4l2sink, "outputting buffers via mmap()");
795
796       if (v4l2sink->num_buffers != v4l2sink->pool->buffer_count) {
797         v4l2sink->num_buffers = v4l2sink->pool->buffer_count;
798         g_object_notify (G_OBJECT (v4l2sink), "queue-size");
799       }
800     }
801
802     v4l2buf = gst_v4l2_buffer_pool_get (v4l2sink->pool, TRUE);
803
804     if (G_LIKELY (v4l2buf)) {
805       GST_DEBUG_OBJECT (v4l2sink, "allocated buffer: %p", v4l2buf);
806       *buf = GST_BUFFER (v4l2buf);
807       return GST_FLOW_OK;
808     } else {
809       GST_DEBUG_OBJECT (v4l2sink, "failed to allocate buffer");
810       return GST_FLOW_ERROR;
811     }
812
813   } else {
814     GST_ERROR_OBJECT (v4l2sink, "only supporting streaming mode for now...");
815     return GST_FLOW_ERROR;
816   }
817 }
818
819 /* called after A/V sync to render frame */
820 static GstFlowReturn
821 gst_v4l2sink_show_frame (GstBaseSink * bsink, GstBuffer * buf)
822 {
823   GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
824   GstBuffer *newbuf = NULL;
825
826   GST_DEBUG_OBJECT (v4l2sink, "render buffer: %p", buf);
827
828   if (!GST_IS_V4L2_BUFFER (buf)) {
829     GstFlowReturn ret;
830
831     /* special case check for sub-buffers:  In certain cases, places like
832      * GstBaseTransform, which might check that the buffer is writable
833      * before copying metadata, timestamp, and such, will find that the
834      * buffer has more than one reference to it.  In these cases, they
835      * will create a sub-buffer with an offset=0 and length equal to the
836      * original buffer size.
837      *
838      * This could happen in two scenarios: (1) a tee in the pipeline, and
839      * (2) because the refcnt is incremented in gst_mini_object_free()
840      * before the finalize function is called, and decremented after it
841      * returns..  but returning this buffer to the buffer pool in the
842      * finalize function, could wake up a thread blocked in _buffer_alloc()
843      * which could run and get a buffer w/ refcnt==2 before the thread
844      * originally unref'ing the buffer returns from finalize function and
845      * decrements the refcnt back to 1!
846      */
847     if (buf->parent &&
848         (GST_BUFFER_DATA (buf) == GST_BUFFER_DATA (buf->parent)) &&
849         (GST_BUFFER_SIZE (buf) == GST_BUFFER_SIZE (buf->parent))) {
850       GST_DEBUG_OBJECT (v4l2sink, "I have a sub-buffer!");
851       return gst_v4l2sink_show_frame (bsink, buf->parent);
852     }
853
854     GST_DEBUG_OBJECT (v4l2sink, "slow-path.. I got a %s so I need to memcpy",
855         g_type_name (G_OBJECT_TYPE (buf)));
856
857     ret = gst_v4l2sink_buffer_alloc (bsink,
858         GST_BUFFER_OFFSET (buf), GST_BUFFER_SIZE (buf), GST_BUFFER_CAPS (buf),
859         &newbuf);
860
861     if (GST_FLOW_OK != ret) {
862       GST_DEBUG_OBJECT (v4l2sink,
863           "dropping frame!  Consider increasing 'queue-size' property!");
864       return GST_FLOW_OK;
865     }
866
867     memcpy (GST_BUFFER_DATA (newbuf),
868         GST_BUFFER_DATA (buf),
869         MIN (GST_BUFFER_SIZE (newbuf), GST_BUFFER_SIZE (buf)));
870
871     GST_DEBUG_OBJECT (v4l2sink, "render copied buffer: %p", newbuf);
872
873     buf = newbuf;
874   }
875
876   if (!gst_v4l2_buffer_pool_qbuf (v4l2sink->pool, GST_V4L2_BUFFER (buf))) {
877     return GST_FLOW_ERROR;
878   }
879   if (v4l2sink->state == STATE_PENDING_STREAMON) {
880     if (!gst_v4l2_object_start_streaming (v4l2sink->v4l2object)) {
881       return GST_FLOW_ERROR;
882     }
883     v4l2sink->state = STATE_STREAMING;
884   }
885
886   if (!newbuf) {
887     gst_buffer_ref (buf);
888   }
889
890   /* if the driver has more than one buffer, ie. more than just the one we
891    * just queued, then dequeue one immediately to make it available via
892    * _buffer_alloc():
893    */
894   if (gst_v4l2_buffer_pool_available_buffers (v4l2sink->pool) >
895       v4l2sink->min_queued_bufs) {
896     GstV4l2Buffer *v4l2buf = gst_v4l2_buffer_pool_dqbuf (v4l2sink->pool);
897
898     /* note: if we get a buf, we don't want to use it directly (because
899      * someone else could still hold a ref).. but instead we release our
900      * reference to it, and if no one else holds a ref it will be returned
901      * to the pool of available buffers..  and if not, we keep looping.
902      */
903     if (v4l2buf) {
904       gst_buffer_unref (GST_BUFFER (v4l2buf));
905     }
906   }
907
908   return GST_FLOW_OK;
909 }
910
911 #ifdef HAVE_XVIDEO
912 static void
913 gst_v4l2sink_navigation_send_event (GstNavigation * navigation,
914     GstStructure * structure)
915 {
916   GstV4l2Sink *v4l2sink = GST_V4L2SINK (navigation);
917   GstV4l2Xv *xv = v4l2sink->v4l2object->xv;
918   GstPad *peer;
919
920   if (!xv)
921     return;
922
923   if ((peer = gst_pad_get_peer (GST_VIDEO_SINK_PAD (v4l2sink)))) {
924     GstVideoRectangle rect;
925     gdouble x, y, xscale = 1.0, yscale = 1.0;
926
927     gst_v4l2_xoverlay_get_render_rect (v4l2sink->v4l2object, &rect);
928
929     /* We calculate scaling using the original video frames geometry to
930      * include pixel aspect ratio scaling.
931      */
932     xscale = (gdouble) v4l2sink->video_width / rect.w;
933     yscale = (gdouble) v4l2sink->video_height / rect.h;
934
935     /* Converting pointer coordinates to the non scaled geometry */
936     if (gst_structure_get_double (structure, "pointer_x", &x)) {
937       x = MIN (x, rect.x + rect.w);
938       x = MAX (x - rect.x, 0);
939       gst_structure_set (structure, "pointer_x", G_TYPE_DOUBLE,
940           (gdouble) x * xscale, NULL);
941     }
942     if (gst_structure_get_double (structure, "pointer_y", &y)) {
943       y = MIN (y, rect.y + rect.h);
944       y = MAX (y - rect.y, 0);
945       gst_structure_set (structure, "pointer_y", G_TYPE_DOUBLE,
946           (gdouble) y * yscale, NULL);
947     }
948
949     gst_pad_send_event (peer, gst_event_new_navigation (structure));
950     gst_object_unref (peer);
951   }
952 }
953 #endif