[v4l2src] Add new property for TBM output buffer
[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., 51 Franklin St, Fifth Floor,
22  * Boston, MA 02110-1301, 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-1.0 videotestsrc ! v4l2sink device=/dev/video1
35  * ]| This pipeline displays a test pattern on /dev/video1
36  * |[
37  * gst-launch-1.0 -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 #include "gstv4l2vidorient.h"
61
62 #include "gstv4l2sink.h"
63 #include "gst/gst-i18n-plugin.h"
64
65 #include <string.h>
66
67 GST_DEBUG_CATEGORY (v4l2sink_debug);
68 #define GST_CAT_DEFAULT v4l2sink_debug
69
70 #define DEFAULT_PROP_DEVICE   "/dev/video1"
71
72 enum
73 {
74   PROP_0,
75   V4L2_STD_OBJECT_PROPS,
76   PROP_OVERLAY_TOP,
77   PROP_OVERLAY_LEFT,
78   PROP_OVERLAY_WIDTH,
79   PROP_OVERLAY_HEIGHT,
80   PROP_CROP_TOP,
81   PROP_CROP_LEFT,
82   PROP_CROP_WIDTH,
83   PROP_CROP_HEIGHT,
84 };
85
86
87 GST_IMPLEMENT_V4L2_COLOR_BALANCE_METHODS (GstV4l2Sink, gst_v4l2sink);
88 GST_IMPLEMENT_V4L2_TUNER_METHODS (GstV4l2Sink, gst_v4l2sink);
89 GST_IMPLEMENT_V4L2_VIDORIENT_METHODS (GstV4l2Sink, gst_v4l2sink);
90
91 #define gst_v4l2sink_parent_class parent_class
92 G_DEFINE_TYPE_WITH_CODE (GstV4l2Sink, gst_v4l2sink, GST_TYPE_VIDEO_SINK,
93     G_IMPLEMENT_INTERFACE (GST_TYPE_TUNER, gst_v4l2sink_tuner_interface_init);
94     G_IMPLEMENT_INTERFACE (GST_TYPE_COLOR_BALANCE,
95         gst_v4l2sink_color_balance_interface_init);
96     G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_ORIENTATION,
97         gst_v4l2sink_video_orientation_interface_init));
98
99
100 static void gst_v4l2sink_finalize (GstV4l2Sink * v4l2sink);
101
102 /* GObject methods: */
103 static void gst_v4l2sink_set_property (GObject * object, guint prop_id,
104     const GValue * value, GParamSpec * pspec);
105 static void gst_v4l2sink_get_property (GObject * object, guint prop_id,
106     GValue * value, GParamSpec * pspec);
107
108 /* GstElement methods: */
109 static GstStateChangeReturn gst_v4l2sink_change_state (GstElement * element,
110     GstStateChange transition);
111
112 /* GstBaseSink methods: */
113 static gboolean gst_v4l2sink_propose_allocation (GstBaseSink * bsink,
114     GstQuery * query);
115 static GstCaps *gst_v4l2sink_get_caps (GstBaseSink * bsink, GstCaps * filter);
116 static gboolean gst_v4l2sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
117 static GstFlowReturn gst_v4l2sink_show_frame (GstVideoSink * bsink,
118     GstBuffer * buf);
119 static gboolean gst_v4l2sink_unlock (GstBaseSink * sink);
120 static gboolean gst_v4l2sink_unlock_stop (GstBaseSink * sink);
121
122 static void
123 gst_v4l2sink_class_init (GstV4l2SinkClass * klass)
124 {
125   GObjectClass *gobject_class;
126   GstElementClass *element_class;
127   GstBaseSinkClass *basesink_class;
128   GstVideoSinkClass *videosink_class;
129
130   gobject_class = G_OBJECT_CLASS (klass);
131   element_class = GST_ELEMENT_CLASS (klass);
132   basesink_class = GST_BASE_SINK_CLASS (klass);
133   videosink_class = GST_VIDEO_SINK_CLASS (klass);
134
135   gobject_class->finalize = (GObjectFinalizeFunc) gst_v4l2sink_finalize;
136   gobject_class->set_property = gst_v4l2sink_set_property;
137   gobject_class->get_property = gst_v4l2sink_get_property;
138
139   element_class->change_state = gst_v4l2sink_change_state;
140
141   gst_v4l2_object_install_properties_helper (gobject_class,
142       DEFAULT_PROP_DEVICE);
143
144   g_object_class_install_property (gobject_class, PROP_OVERLAY_TOP,
145       g_param_spec_int ("overlay-top", "Overlay top",
146           "The topmost (y) coordinate of the video overlay; top left corner of screen is 0,0",
147           G_MININT, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
148   g_object_class_install_property (gobject_class, PROP_OVERLAY_LEFT,
149       g_param_spec_int ("overlay-left", "Overlay left",
150           "The leftmost (x) coordinate of the video overlay; top left corner of screen is 0,0",
151           G_MININT, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
152   g_object_class_install_property (gobject_class, PROP_OVERLAY_WIDTH,
153       g_param_spec_uint ("overlay-width", "Overlay width",
154           "The width of the video overlay; default is equal to negotiated image width",
155           0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
156   g_object_class_install_property (gobject_class, PROP_OVERLAY_HEIGHT,
157       g_param_spec_uint ("overlay-height", "Overlay height",
158           "The height of the video overlay; default is equal to negotiated image height",
159           0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
160
161   g_object_class_install_property (gobject_class, PROP_CROP_TOP,
162       g_param_spec_int ("crop-top", "Crop top",
163           "The topmost (y) coordinate of the video crop; top left corner of image is 0,0",
164           0x80000000, 0x7fffffff, 0, G_PARAM_READWRITE));
165   g_object_class_install_property (gobject_class, PROP_CROP_LEFT,
166       g_param_spec_int ("crop-left", "Crop left",
167           "The leftmost (x) coordinate of the video crop; top left corner of image is 0,0",
168           0x80000000, 0x7fffffff, 0, G_PARAM_READWRITE));
169   g_object_class_install_property (gobject_class, PROP_CROP_WIDTH,
170       g_param_spec_uint ("crop-width", "Crop width",
171           "The width of the video crop; default is equal to negotiated image width",
172           0, 0xffffffff, 0, G_PARAM_READWRITE));
173   g_object_class_install_property (gobject_class, PROP_CROP_HEIGHT,
174       g_param_spec_uint ("crop-height", "Crop height",
175           "The height of the video crop; default is equal to negotiated image height",
176           0, 0xffffffff, 0, G_PARAM_READWRITE));
177
178   gst_element_class_set_static_metadata (element_class,
179       "Video (video4linux2) Sink", "Sink/Video",
180       "Displays frames on a video4linux2 device", "Rob Clark <rob@ti.com>,");
181
182   gst_element_class_add_pad_template (element_class,
183       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
184           gst_v4l2_object_get_all_caps ()));
185
186   basesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_v4l2sink_get_caps);
187   basesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_v4l2sink_set_caps);
188   basesink_class->propose_allocation =
189       GST_DEBUG_FUNCPTR (gst_v4l2sink_propose_allocation);
190   basesink_class->unlock = GST_DEBUG_FUNCPTR (gst_v4l2sink_unlock);
191   basesink_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_v4l2sink_unlock_stop);
192
193   videosink_class->show_frame = GST_DEBUG_FUNCPTR (gst_v4l2sink_show_frame);
194
195   klass->v4l2_class_devices = NULL;
196
197   GST_DEBUG_CATEGORY_INIT (v4l2sink_debug, "v4l2sink", 0, "V4L2 sink element");
198
199 }
200
201 static void
202 gst_v4l2sink_init (GstV4l2Sink * v4l2sink)
203 {
204   v4l2sink->v4l2object = gst_v4l2_object_new (GST_ELEMENT (v4l2sink),
205       GST_OBJECT (GST_BASE_SINK_PAD (v4l2sink)), V4L2_BUF_TYPE_VIDEO_OUTPUT,
206       DEFAULT_PROP_DEVICE, gst_v4l2_get_output, gst_v4l2_set_output, NULL);
207
208   /* same default value for video output device as is used for
209    * v4l2src/capture is no good..  so lets set a saner default
210    * (which can be overridden by the one creating the v4l2sink
211    * after the constructor returns)
212    */
213   g_object_set (v4l2sink, "device", "/dev/video1", NULL);
214
215   v4l2sink->overlay_fields_set = 0;
216   v4l2sink->crop_fields_set = 0;
217 }
218
219
220 static void
221 gst_v4l2sink_finalize (GstV4l2Sink * v4l2sink)
222 {
223   gst_v4l2_object_destroy (v4l2sink->v4l2object);
224
225   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (v4l2sink));
226 }
227
228
229 /*
230  * flags to indicate which overlay/crop properties the user has set (and
231  * therefore which ones should override the defaults from the driver)
232  */
233 enum
234 {
235   RECT_TOP_SET = 0x01,
236   RECT_LEFT_SET = 0x02,
237   RECT_WIDTH_SET = 0x04,
238   RECT_HEIGHT_SET = 0x08
239 };
240
241 static void
242 gst_v4l2sink_sync_overlay_fields (GstV4l2Sink * v4l2sink)
243 {
244   if (!v4l2sink->overlay_fields_set)
245     return;
246
247   if (GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
248
249     GstV4l2Object *obj = v4l2sink->v4l2object;
250     struct v4l2_format format;
251
252     memset (&format, 0x00, sizeof (struct v4l2_format));
253     if (obj->device_caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY)
254       format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY;
255     else
256       format.type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
257
258     if (obj->ioctl (obj->video_fd, VIDIOC_G_FMT, &format) < 0) {
259       GST_WARNING_OBJECT (v4l2sink, "VIDIOC_G_FMT failed");
260       return;
261     }
262
263     GST_DEBUG_OBJECT (v4l2sink,
264         "setting overlay: overlay_fields_set=0x%02x, top=%d, left=%d, width=%d, height=%d",
265         v4l2sink->overlay_fields_set,
266         v4l2sink->overlay.top, v4l2sink->overlay.left,
267         v4l2sink->overlay.width, v4l2sink->overlay.height);
268
269     if (v4l2sink->overlay_fields_set & RECT_TOP_SET)
270       format.fmt.win.w.top = v4l2sink->overlay.top;
271     if (v4l2sink->overlay_fields_set & RECT_LEFT_SET)
272       format.fmt.win.w.left = v4l2sink->overlay.left;
273     if (v4l2sink->overlay_fields_set & RECT_WIDTH_SET)
274       format.fmt.win.w.width = v4l2sink->overlay.width;
275     if (v4l2sink->overlay_fields_set & RECT_HEIGHT_SET)
276       format.fmt.win.w.height = v4l2sink->overlay.height;
277
278     if (obj->ioctl (obj->video_fd, VIDIOC_S_FMT, &format) < 0) {
279       GST_WARNING_OBJECT (v4l2sink, "VIDIOC_S_FMT failed");
280       return;
281     }
282
283     v4l2sink->overlay_fields_set = 0;
284     v4l2sink->overlay = format.fmt.win.w;
285   }
286 }
287
288 static void
289 gst_v4l2sink_sync_crop_fields (GstV4l2Sink * v4l2sink)
290 {
291   if (!v4l2sink->crop_fields_set)
292     return;
293
294   if (GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
295
296     GstV4l2Object *obj = v4l2sink->v4l2object;
297     struct v4l2_crop crop;
298
299     memset (&crop, 0x00, sizeof (struct v4l2_crop));
300     crop.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
301
302     if (obj->ioctl (obj->video_fd, VIDIOC_G_CROP, &crop) < 0) {
303       GST_WARNING_OBJECT (v4l2sink, "VIDIOC_G_CROP failed");
304       return;
305     }
306
307     GST_DEBUG_OBJECT (v4l2sink,
308         "setting crop: crop_fields_set=0x%02x, top=%d, left=%d, width=%d, height=%d",
309         v4l2sink->crop_fields_set,
310         v4l2sink->crop.top, v4l2sink->crop.left,
311         v4l2sink->crop.width, v4l2sink->crop.height);
312
313     if (v4l2sink->crop_fields_set & RECT_TOP_SET)
314       crop.c.top = v4l2sink->crop.top;
315     if (v4l2sink->crop_fields_set & RECT_LEFT_SET)
316       crop.c.left = v4l2sink->crop.left;
317     if (v4l2sink->crop_fields_set & RECT_WIDTH_SET)
318       crop.c.width = v4l2sink->crop.width;
319     if (v4l2sink->crop_fields_set & RECT_HEIGHT_SET)
320       crop.c.height = v4l2sink->crop.height;
321
322     if (obj->ioctl (obj->video_fd, VIDIOC_S_CROP, &crop) < 0) {
323       GST_WARNING_OBJECT (v4l2sink, "VIDIOC_S_CROP failed");
324       return;
325     }
326
327     if (obj->ioctl (obj->video_fd, VIDIOC_G_CROP, &crop) < 0) {
328       GST_WARNING_OBJECT (v4l2sink, "VIDIOC_G_CROP failed");
329       return;
330     }
331
332     v4l2sink->crop_fields_set = 0;
333     v4l2sink->crop = crop.c;
334   }
335 }
336
337
338 static void
339 gst_v4l2sink_set_property (GObject * object,
340     guint prop_id, const GValue * value, GParamSpec * pspec)
341 {
342   GstV4l2Sink *v4l2sink = GST_V4L2SINK (object);
343
344   if (!gst_v4l2_object_set_property_helper (v4l2sink->v4l2object,
345           prop_id, value, pspec)) {
346     switch (prop_id) {
347       case PROP_OVERLAY_TOP:
348         v4l2sink->overlay.top = g_value_get_int (value);
349         v4l2sink->overlay_fields_set |= RECT_TOP_SET;
350         gst_v4l2sink_sync_overlay_fields (v4l2sink);
351         break;
352       case PROP_OVERLAY_LEFT:
353         v4l2sink->overlay.left = g_value_get_int (value);
354         v4l2sink->overlay_fields_set |= RECT_LEFT_SET;
355         gst_v4l2sink_sync_overlay_fields (v4l2sink);
356         break;
357       case PROP_OVERLAY_WIDTH:
358         v4l2sink->overlay.width = g_value_get_uint (value);
359         v4l2sink->overlay_fields_set |= RECT_WIDTH_SET;
360         gst_v4l2sink_sync_overlay_fields (v4l2sink);
361         break;
362       case PROP_OVERLAY_HEIGHT:
363         v4l2sink->overlay.height = g_value_get_uint (value);
364         v4l2sink->overlay_fields_set |= RECT_HEIGHT_SET;
365         gst_v4l2sink_sync_overlay_fields (v4l2sink);
366         break;
367       case PROP_CROP_TOP:
368         v4l2sink->crop.top = g_value_get_int (value);
369         v4l2sink->crop_fields_set |= RECT_TOP_SET;
370         gst_v4l2sink_sync_crop_fields (v4l2sink);
371         break;
372       case PROP_CROP_LEFT:
373         v4l2sink->crop.left = g_value_get_int (value);
374         v4l2sink->crop_fields_set |= RECT_LEFT_SET;
375         gst_v4l2sink_sync_crop_fields (v4l2sink);
376         break;
377       case PROP_CROP_WIDTH:
378         v4l2sink->crop.width = g_value_get_uint (value);
379         v4l2sink->crop_fields_set |= RECT_WIDTH_SET;
380         gst_v4l2sink_sync_crop_fields (v4l2sink);
381         break;
382       case PROP_CROP_HEIGHT:
383         v4l2sink->crop.height = g_value_get_uint (value);
384         v4l2sink->crop_fields_set |= RECT_HEIGHT_SET;
385         gst_v4l2sink_sync_crop_fields (v4l2sink);
386         break;
387       default:
388         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
389         break;
390     }
391   }
392 }
393
394
395 static void
396 gst_v4l2sink_get_property (GObject * object,
397     guint prop_id, GValue * value, GParamSpec * pspec)
398 {
399   GstV4l2Sink *v4l2sink = GST_V4L2SINK (object);
400
401   if (!gst_v4l2_object_get_property_helper (v4l2sink->v4l2object,
402           prop_id, value, pspec)) {
403     switch (prop_id) {
404       case PROP_OVERLAY_TOP:
405         g_value_set_int (value, v4l2sink->overlay.top);
406         break;
407       case PROP_OVERLAY_LEFT:
408         g_value_set_int (value, v4l2sink->overlay.left);
409         break;
410       case PROP_OVERLAY_WIDTH:
411         g_value_set_uint (value, v4l2sink->overlay.width);
412         break;
413       case PROP_OVERLAY_HEIGHT:
414         g_value_set_uint (value, v4l2sink->overlay.height);
415         break;
416       case PROP_CROP_TOP:
417         g_value_set_int (value, v4l2sink->crop.top);
418         break;
419       case PROP_CROP_LEFT:
420         g_value_set_int (value, v4l2sink->crop.left);
421         break;
422       case PROP_CROP_WIDTH:
423         g_value_set_uint (value, v4l2sink->crop.width);
424         break;
425       case PROP_CROP_HEIGHT:
426         g_value_set_uint (value, v4l2sink->crop.height);
427         break;
428       default:
429         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
430         break;
431     }
432   }
433 }
434
435 static GstStateChangeReturn
436 gst_v4l2sink_change_state (GstElement * element, GstStateChange transition)
437 {
438   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
439   GstV4l2Sink *v4l2sink = GST_V4L2SINK (element);
440
441   GST_DEBUG_OBJECT (v4l2sink, "%d -> %d",
442       GST_STATE_TRANSITION_CURRENT (transition),
443       GST_STATE_TRANSITION_NEXT (transition));
444
445   switch (transition) {
446     case GST_STATE_CHANGE_NULL_TO_READY:
447       /* open the device */
448       if (!gst_v4l2_object_open (v4l2sink->v4l2object))
449         return GST_STATE_CHANGE_FAILURE;
450       break;
451     default:
452       break;
453   }
454
455   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
456
457   switch (transition) {
458     case GST_STATE_CHANGE_PAUSED_TO_READY:
459       if (!gst_v4l2_object_stop (v4l2sink->v4l2object))
460         return GST_STATE_CHANGE_FAILURE;
461       break;
462     case GST_STATE_CHANGE_READY_TO_NULL:
463       /* we need to call stop here too */
464       if (!gst_v4l2_object_stop (v4l2sink->v4l2object))
465         return GST_STATE_CHANGE_FAILURE;
466       /* close the device */
467       if (!gst_v4l2_object_close (v4l2sink->v4l2object))
468         return GST_STATE_CHANGE_FAILURE;
469       break;
470     default:
471       break;
472   }
473
474   return ret;
475 }
476
477
478 static GstCaps *
479 gst_v4l2sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
480 {
481   GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
482
483   if (!GST_V4L2_IS_OPEN (v4l2sink->v4l2object)) {
484     /* FIXME: copy? */
485     GST_DEBUG_OBJECT (v4l2sink, "device is not open");
486     return gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD (v4l2sink));
487   }
488
489
490   return gst_v4l2_object_get_caps (v4l2sink->v4l2object, filter);
491 }
492
493 static gboolean
494 gst_v4l2sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
495 {
496   GstV4l2Error error = GST_V4L2_ERROR_INIT;
497   GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
498   GstV4l2Object *obj = v4l2sink->v4l2object;
499
500   GST_DEBUG_OBJECT (v4l2sink, "caps: %" GST_PTR_FORMAT, caps);
501
502   if (!GST_V4L2_IS_OPEN (obj)) {
503     GST_DEBUG_OBJECT (v4l2sink, "device is not open");
504     return FALSE;
505   }
506
507   /* make sure the caps changed before doing anything */
508   if (gst_v4l2_object_caps_equal (obj, caps))
509     return TRUE;
510
511   if (!gst_v4l2_object_stop (obj))
512     goto stop_failed;
513
514   if (!gst_v4l2_object_set_format (obj, caps, &error))
515     goto invalid_format;
516
517   gst_v4l2sink_sync_overlay_fields (v4l2sink);
518   gst_v4l2sink_sync_crop_fields (v4l2sink);
519
520   GST_INFO_OBJECT (v4l2sink, "outputting buffers via mode %u", obj->mode);
521
522   v4l2sink->video_width = GST_V4L2_WIDTH (obj);
523   v4l2sink->video_height = GST_V4L2_HEIGHT (obj);
524
525   /* TODO: videosink width/height should be scaled according to
526    * pixel-aspect-ratio
527    */
528   GST_VIDEO_SINK_WIDTH (v4l2sink) = v4l2sink->video_width;
529   GST_VIDEO_SINK_HEIGHT (v4l2sink) = v4l2sink->video_height;
530
531   return TRUE;
532
533   /* ERRORS */
534 stop_failed:
535   {
536     GST_DEBUG_OBJECT (v4l2sink, "failed to stop streaming");
537     return FALSE;
538   }
539 invalid_format:
540   {
541     /* error already posted */
542     gst_v4l2_error (v4l2sink, &error);
543     GST_DEBUG_OBJECT (v4l2sink, "can't set format");
544     return FALSE;
545   }
546 }
547
548 static gboolean
549 gst_v4l2sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
550 {
551   GstV4l2Sink *v4l2sink = GST_V4L2SINK (bsink);
552   gboolean last_sample_enabled;
553
554   if (!gst_v4l2_object_propose_allocation (v4l2sink->v4l2object, query))
555     return FALSE;
556
557   g_object_get (bsink, "enable-last-sample", &last_sample_enabled, NULL);
558
559   if (last_sample_enabled && gst_query_get_n_allocation_pools (query) > 0) {
560     GstBufferPool *pool;
561     guint size, min, max;
562
563     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
564
565     /* we need 1 more, otherwise we'll run out of buffers at preroll */
566     min++;
567     if (max < min)
568       max = min;
569
570     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
571     if (pool)
572       gst_object_unref (pool);
573   }
574
575   return TRUE;
576 }
577
578 /* called after A/V sync to render frame */
579 static GstFlowReturn
580 gst_v4l2sink_show_frame (GstVideoSink * vsink, GstBuffer * buf)
581 {
582   GstFlowReturn ret;
583   GstV4l2Sink *v4l2sink = GST_V4L2SINK (vsink);
584   GstV4l2Object *obj = v4l2sink->v4l2object;
585   GstBufferPool *bpool = GST_BUFFER_POOL (obj->pool);
586
587   GST_DEBUG_OBJECT (v4l2sink, "render buffer: %p", buf);
588
589   if (G_UNLIKELY (obj->pool == NULL))
590     goto not_negotiated;
591
592   if (G_UNLIKELY (!gst_buffer_pool_is_active (bpool))) {
593     GstStructure *config;
594
595     /* this pool was not activated, configure and activate */
596     GST_DEBUG_OBJECT (v4l2sink, "activating pool");
597
598     config = gst_buffer_pool_get_config (bpool);
599     gst_buffer_pool_config_add_option (config,
600         GST_BUFFER_POOL_OPTION_VIDEO_META);
601     gst_buffer_pool_set_config (bpool, config);
602
603     if (!gst_buffer_pool_set_active (bpool, TRUE))
604       goto activate_failed;
605   }
606
607   gst_buffer_ref (buf);
608 again:
609   ret = gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL_CAST (obj->pool),
610       &buf);
611   if (ret == GST_FLOW_FLUSHING) {
612     ret = gst_base_sink_wait_preroll (GST_BASE_SINK (vsink));
613     if (ret == GST_FLOW_OK)
614       goto again;
615   }
616   gst_buffer_unref (buf);
617
618   return ret;
619
620   /* ERRORS */
621 not_negotiated:
622   {
623     GST_ERROR_OBJECT (v4l2sink, "not negotiated");
624     return GST_FLOW_NOT_NEGOTIATED;
625   }
626 activate_failed:
627   {
628     GST_ELEMENT_ERROR (v4l2sink, RESOURCE, SETTINGS,
629         (_("Failed to allocated required memory.")),
630         ("Buffer pool activation failed"));
631     return GST_FLOW_ERROR;
632   }
633 }
634
635 static gboolean
636 gst_v4l2sink_unlock (GstBaseSink * sink)
637 {
638   GstV4l2Sink *v4l2sink = GST_V4L2SINK (sink);
639   return gst_v4l2_object_unlock (v4l2sink->v4l2object);
640 }
641
642 static gboolean
643 gst_v4l2sink_unlock_stop (GstBaseSink * sink)
644 {
645   GstV4l2Sink *v4l2sink = GST_V4L2SINK (sink);
646   return gst_v4l2_object_unlock_stop (v4l2sink->v4l2object);
647 }