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