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