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