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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * SECTION:gstvideosink
23 * @title: GstVideoSink
24 * @short_description: Base class for video sinks
26 * Provides useful functions and a base class for video sinks.
28 * GstVideoSink will configure the default base sink to drop frames that
29 * arrive later than 20ms as this is considered the default threshold for
30 * observing out-of-sync frames.
38 #include "gstvideosink.h"
42 PROP_SHOW_PREROLL_FRAME = 1
45 #define DEFAULT_SHOW_PREROLL_FRAME TRUE
47 struct _GstVideoSinkPrivate
49 gboolean show_preroll_frame; /* ATOMIC */
52 G_DEFINE_TYPE_WITH_PRIVATE (GstVideoSink, gst_video_sink, GST_TYPE_BASE_SINK);
54 #ifndef GST_DISABLE_GST_DEBUG
55 #define GST_CAT_DEFAULT gst_video_sink_ensure_debug_category()
57 static GstDebugCategory *
58 gst_video_sink_ensure_debug_category (void)
60 static gsize cat_gonce = 0;
62 if (g_once_init_enter (&cat_gonce)) {
63 GstDebugCategory *cat = NULL;
65 GST_DEBUG_CATEGORY_INIT (cat, "videosink", 0, "GstVideoSink");
67 g_once_init_leave (&cat_gonce, (gsize) cat);
70 return (GstDebugCategory *) cat_gonce;
72 #endif /* GST_DISABLE_GST_DEBUG */
74 static GstBaseSinkClass *parent_class = NULL;
76 static void gst_video_sink_set_property (GObject * object, guint prop_id,
77 const GValue * value, GParamSpec * pspec);
78 static void gst_video_sink_get_property (GObject * object, guint prop_id,
79 GValue * value, GParamSpec * pspec);
81 static GstFlowReturn gst_video_sink_show_preroll_frame (GstBaseSink * bsink,
83 static GstFlowReturn gst_video_sink_show_frame (GstBaseSink * bsink,
87 * gst_video_sink_center_rect:
88 * @src: the #GstVideoRectangle describing the source area
89 * @dst: the #GstVideoRectangle describing the destination area
90 * @result: a pointer to a #GstVideoRectangle which will receive the result area
91 * @scaling: a #gboolean indicating if scaling should be applied or not
93 * Takes @src rectangle and position it at the center of @dst rectangle with or
94 * without @scaling. It handles clipping if the @src rectangle is bigger than
95 * the @dst one and @scaling is set to FALSE.
98 gst_video_sink_center_rect (GstVideoRectangle src, GstVideoRectangle dst,
99 GstVideoRectangle * result, gboolean scaling)
101 g_return_if_fail (result != NULL);
104 result->w = MIN (src.w, dst.w);
105 result->h = MIN (src.h, dst.h);
106 result->x = dst.x + (dst.w - result->w) / 2;
107 result->y = dst.y + (dst.h - result->h) / 2;
109 gdouble src_ratio, dst_ratio;
111 src_ratio = (gdouble) src.w / src.h;
112 dst_ratio = (gdouble) dst.w / dst.h;
114 if (src_ratio > dst_ratio) {
116 result->h = dst.w / src_ratio;
118 result->y = dst.y + (dst.h - result->h) / 2;
119 } else if (src_ratio < dst_ratio) {
120 result->w = dst.h * src_ratio;
122 result->x = dst.x + (dst.w - result->w) / 2;
132 GST_DEBUG ("source is %dx%d dest is %dx%d, result is %dx%d with x,y %dx%d",
133 src.w, src.h, dst.w, dst.h, result->w, result->h, result->x, result->y);
139 gst_video_sink_init (GstVideoSink * videosink)
141 videosink->width = 0;
142 videosink->height = 0;
144 /* 20ms is more than enough, 80-130ms is noticeable */
145 gst_base_sink_set_processing_deadline (GST_BASE_SINK (videosink),
147 gst_base_sink_set_max_lateness (GST_BASE_SINK (videosink), 5 * GST_MSECOND);
148 gst_base_sink_set_qos_enabled (GST_BASE_SINK (videosink), TRUE);
150 videosink->priv = gst_video_sink_get_instance_private (videosink);
154 gst_video_sink_class_init (GstVideoSinkClass * klass)
156 GstBaseSinkClass *basesink_class = (GstBaseSinkClass *) klass;
157 GObjectClass *gobject_class = (GObjectClass *) klass;
159 parent_class = g_type_class_peek_parent (klass);
161 gobject_class->set_property = gst_video_sink_set_property;
162 gobject_class->get_property = gst_video_sink_get_property;
165 * GstVideoSink:show-preroll-frame:
167 * Whether to show video frames during preroll. If set to %FALSE, video
168 * frames will only be rendered in PLAYING state.
170 g_object_class_install_property (gobject_class, PROP_SHOW_PREROLL_FRAME,
171 g_param_spec_boolean ("show-preroll-frame", "Show preroll frame",
172 "Whether to render video frames during preroll",
173 DEFAULT_SHOW_PREROLL_FRAME,
174 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
176 basesink_class->render = GST_DEBUG_FUNCPTR (gst_video_sink_show_frame);
177 basesink_class->preroll =
178 GST_DEBUG_FUNCPTR (gst_video_sink_show_preroll_frame);
182 gst_video_sink_show_preroll_frame (GstBaseSink * bsink, GstBuffer * buf)
184 GstVideoSinkClass *klass;
188 vsink = GST_VIDEO_SINK_CAST (bsink);
189 klass = GST_VIDEO_SINK_GET_CLASS (vsink);
191 do_show = g_atomic_int_get (&vsink->priv->show_preroll_frame);
193 if (G_UNLIKELY (!do_show)) {
194 GST_DEBUG_OBJECT (bsink, "not rendering frame with ts=%" GST_TIME_FORMAT
195 ", preroll rendering disabled",
196 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
199 if (klass->show_frame == NULL || !do_show) {
200 if (parent_class->preroll != NULL)
201 return parent_class->preroll (bsink, buf);
206 GST_LOG_OBJECT (bsink, "rendering frame, ts=%" GST_TIME_FORMAT,
207 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
209 return klass->show_frame (GST_VIDEO_SINK_CAST (bsink), buf);
213 gst_video_sink_show_frame (GstBaseSink * bsink, GstBuffer * buf)
215 GstVideoSinkClass *klass;
217 klass = GST_VIDEO_SINK_GET_CLASS (bsink);
219 if (klass->show_frame == NULL) {
220 if (parent_class->render != NULL)
221 return parent_class->render (bsink, buf);
226 GST_LOG_OBJECT (bsink, "rendering frame, ts=%" GST_TIME_FORMAT,
227 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
229 return klass->show_frame (GST_VIDEO_SINK_CAST (bsink), buf);
233 gst_video_sink_set_property (GObject * object, guint prop_id,
234 const GValue * value, GParamSpec * pspec)
238 vsink = GST_VIDEO_SINK (object);
241 case PROP_SHOW_PREROLL_FRAME:
242 g_atomic_int_set (&vsink->priv->show_preroll_frame,
243 g_value_get_boolean (value));
246 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252 gst_video_sink_get_property (GObject * object, guint prop_id,
253 GValue * value, GParamSpec * pspec)
257 vsink = GST_VIDEO_SINK (object);
260 case PROP_SHOW_PREROLL_FRAME:
261 g_value_set_boolean (value,
262 g_atomic_int_get (&vsink->priv->show_preroll_frame));
265 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);