1 /* GStreamer video sink base class
2 * Copyright (C) <2003> Julien Moutte <julien@moutte.net>
3 * Copyright (C) <2009> Tim-Philipp Müller <tim centricular net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:gstvideosink
23 * @short_description: Base class for video sinks
27 * Provides useful functions and a base class for video sinks.
30 * GstVideoSink will configure the default base sink to drop frames that
31 * arrive later than 20ms as this is considered the default threshold for
32 * observing out-of-sync frames.
41 #include "gstvideosink.h"
45 PROP_SHOW_PREROLL_FRAME = 1
48 #define DEFAULT_SHOW_PREROLL_FRAME TRUE
50 struct _GstVideoSinkPrivate
52 gboolean show_preroll_frame; /* ATOMIC */
55 GST_DEBUG_CATEGORY_STATIC (video_sink_debug);
56 #define GST_CAT_DEFAULT video_sink_debug
58 static GstBaseSinkClass *parent_class = NULL;
60 static void gst_video_sink_set_property (GObject * object, guint prop_id,
61 const GValue * value, GParamSpec * pspec);
62 static void gst_video_sink_get_property (GObject * object, guint prop_id,
63 GValue * value, GParamSpec * pspec);
65 static GstFlowReturn gst_video_sink_show_preroll_frame (GstBaseSink * bsink,
67 static GstFlowReturn gst_video_sink_show_frame (GstBaseSink * bsink,
71 * gst_video_sink_center_rect:
72 * @src: the #GstVideoRectangle describing the source area
73 * @dst: the #GstVideoRectangle describing the destination area
74 * @result: a pointer to a #GstVideoRectangle which will receive the result area
75 * @scaling: a #gboolean indicating if scaling should be applied or not
77 * Takes @src rectangle and position it at the center of @dst rectangle with or
78 * without @scaling. It handles clipping if the @src rectangle is bigger than
79 * the @dst one and @scaling is set to FALSE.
82 gst_video_sink_center_rect (GstVideoRectangle src, GstVideoRectangle dst,
83 GstVideoRectangle * result, gboolean scaling)
85 g_return_if_fail (result != NULL);
88 result->w = MIN (src.w, dst.w);
89 result->h = MIN (src.h, dst.h);
90 result->x = (dst.w - result->w) / 2;
91 result->y = (dst.h - result->h) / 2;
93 gdouble src_ratio, dst_ratio;
95 src_ratio = (gdouble) src.w / src.h;
96 dst_ratio = (gdouble) dst.w / dst.h;
98 if (src_ratio > dst_ratio) {
100 result->h = dst.w / src_ratio;
102 result->y = (dst.h - result->h) / 2;
103 } else if (src_ratio < dst_ratio) {
104 result->w = dst.h * src_ratio;
106 result->x = (dst.w - result->w) / 2;
116 GST_DEBUG ("source is %dx%d dest is %dx%d, result is %dx%d with x,y %dx%d",
117 src.w, src.h, dst.w, dst.h, result->w, result->h, result->x, result->y);
123 gst_video_sink_init (GstVideoSink * videosink)
125 videosink->width = 0;
126 videosink->height = 0;
128 /* 20ms is more than enough, 80-130ms is noticable */
129 gst_base_sink_set_max_lateness (GST_BASE_SINK (videosink), 20 * GST_MSECOND);
130 gst_base_sink_set_qos_enabled (GST_BASE_SINK (videosink), TRUE);
132 videosink->priv = G_TYPE_INSTANCE_GET_PRIVATE (videosink,
133 GST_TYPE_VIDEO_SINK, GstVideoSinkPrivate);
137 gst_video_sink_class_init (GstVideoSinkClass * klass)
139 GstBaseSinkClass *basesink_class = (GstBaseSinkClass *) klass;
140 GObjectClass *gobject_class = (GObjectClass *) klass;
142 parent_class = g_type_class_peek_parent (klass);
144 gobject_class->set_property = gst_video_sink_set_property;
145 gobject_class->get_property = gst_video_sink_get_property;
148 * GstVideoSink:show-preroll-frame
150 * Whether to show video frames during preroll. If set to #FALSE, video
151 * frames will only be rendered in PLAYING state.
155 g_object_class_install_property (gobject_class, PROP_SHOW_PREROLL_FRAME,
156 g_param_spec_boolean ("show-preroll-frame", "Show preroll frame",
157 "Whether to render video frames during preroll",
158 DEFAULT_SHOW_PREROLL_FRAME,
159 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
161 basesink_class->render = GST_DEBUG_FUNCPTR (gst_video_sink_show_frame);
162 basesink_class->preroll =
163 GST_DEBUG_FUNCPTR (gst_video_sink_show_preroll_frame);
165 g_type_class_add_private (klass, sizeof (GstVideoSinkPrivate));
169 gst_video_sink_base_init (gpointer g_class)
171 GST_DEBUG_CATEGORY_INIT (video_sink_debug, "videosink", 0, "GstVideoSink");
175 gst_video_sink_show_preroll_frame (GstBaseSink * bsink, GstBuffer * buf)
177 GstVideoSinkClass *klass;
181 vsink = GST_VIDEO_SINK_CAST (bsink);
182 klass = GST_VIDEO_SINK_GET_CLASS (vsink);
184 do_show = g_atomic_int_get (&vsink->priv->show_preroll_frame);
186 if (G_UNLIKELY (!do_show)) {
187 GST_DEBUG_OBJECT (bsink, "not rendering frame with ts=%" GST_TIME_FORMAT
188 ", preroll rendering disabled",
189 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
192 if (klass->show_frame == NULL || !do_show) {
193 if (parent_class->preroll != NULL)
194 return parent_class->preroll (bsink, buf);
199 GST_LOG_OBJECT (bsink, "rendering frame, ts=%" GST_TIME_FORMAT,
200 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
202 return klass->show_frame (GST_VIDEO_SINK_CAST (bsink), buf);
206 gst_video_sink_show_frame (GstBaseSink * bsink, GstBuffer * buf)
208 GstVideoSinkClass *klass;
210 klass = GST_VIDEO_SINK_GET_CLASS (bsink);
212 if (klass->show_frame == NULL) {
213 if (parent_class->render != NULL)
214 return parent_class->render (bsink, buf);
219 GST_LOG_OBJECT (bsink, "rendering frame, ts=%" GST_TIME_FORMAT,
220 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
222 return klass->show_frame (GST_VIDEO_SINK_CAST (bsink), buf);
226 gst_video_sink_set_property (GObject * object, guint prop_id,
227 const GValue * value, GParamSpec * pspec)
231 vsink = GST_VIDEO_SINK (object);
234 case PROP_SHOW_PREROLL_FRAME:
235 g_atomic_int_set (&vsink->priv->show_preroll_frame,
236 g_value_get_boolean (value));
239 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245 gst_video_sink_get_property (GObject * object, guint prop_id,
246 GValue * value, GParamSpec * pspec)
250 vsink = GST_VIDEO_SINK (object);
253 case PROP_SHOW_PREROLL_FRAME:
254 g_value_set_boolean (value,
255 g_atomic_int_get (&vsink->priv->show_preroll_frame));
258 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
266 gst_video_sink_get_type (void)
268 static GType videosink_type = 0;
270 if (!videosink_type) {
271 static const GTypeInfo videosink_info = {
272 sizeof (GstVideoSinkClass),
273 gst_video_sink_base_init,
275 (GClassInitFunc) gst_video_sink_class_init,
278 sizeof (GstVideoSink),
280 (GInstanceInitFunc) gst_video_sink_init,
283 videosink_type = g_type_register_static (GST_TYPE_BASE_SINK,
284 "GstVideoSink", &videosink_info, 0);
287 return videosink_type;