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>
4  *
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.
9  *
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.
14  *
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.
19  */
20 
21 /**
22  * SECTION:gstvideosink
23  * @title: GstVideoSink
24  * @short_description: Base class for video sinks
25  *
26  * Provides useful functions and a base class for video sinks.
27  *
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.
31  *
32  */
33 
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37 
38 #include "gstvideosink.h"
39 
40 enum
41 {
42   PROP_SHOW_PREROLL_FRAME = 1
43 };
44 
45 #define DEFAULT_SHOW_PREROLL_FRAME TRUE
46 
47 struct _GstVideoSinkPrivate
48 {
49   gboolean show_preroll_frame;  /* ATOMIC */
50 };
51 
52 G_DEFINE_TYPE_WITH_PRIVATE (GstVideoSink, gst_video_sink, GST_TYPE_BASE_SINK);
53 
54 #ifndef GST_DISABLE_GST_DEBUG
55 #define GST_CAT_DEFAULT gst_video_sink_ensure_debug_category()
56 
57 static GstDebugCategory *
gst_video_sink_ensure_debug_category(void)58 gst_video_sink_ensure_debug_category (void)
59 {
60   static gsize cat_gonce = 0;
61 
62   if (g_once_init_enter (&cat_gonce)) {
63     GstDebugCategory *cat = NULL;
64 
65     GST_DEBUG_CATEGORY_INIT (cat, "videosink", 0, "GstVideoSink");
66 
67     g_once_init_leave (&cat_gonce, (gsize) cat);
68   }
69 
70   return (GstDebugCategory *) cat_gonce;
71 }
72 #endif /* GST_DISABLE_GST_DEBUG */
73 
74 static GstBaseSinkClass *parent_class = NULL;
75 
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);
80 
81 static GstFlowReturn gst_video_sink_show_preroll_frame (GstBaseSink * bsink,
82     GstBuffer * buf);
83 static GstFlowReturn gst_video_sink_show_frame (GstBaseSink * bsink,
84     GstBuffer * buf);
85 
86 /**
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
92  *
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.
96  */
97 void
gst_video_sink_center_rect(GstVideoRectangle src,GstVideoRectangle dst,GstVideoRectangle * result,gboolean scaling)98 gst_video_sink_center_rect (GstVideoRectangle src, GstVideoRectangle dst,
99     GstVideoRectangle * result, gboolean scaling)
100 {
101   g_return_if_fail (result != NULL);
102 
103   if (!scaling) {
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;
108   } else {
109     gdouble src_ratio, dst_ratio;
110 
111     src_ratio = (gdouble) src.w / src.h;
112     dst_ratio = (gdouble) dst.w / dst.h;
113 
114     if (src_ratio > dst_ratio) {
115       result->w = dst.w;
116       result->h = dst.w / src_ratio;
117       result->x = dst.x;
118       result->y = dst.y + (dst.h - result->h) / 2;
119     } else if (src_ratio < dst_ratio) {
120       result->w = dst.h * src_ratio;
121       result->h = dst.h;
122       result->x = dst.x + (dst.w - result->w) / 2;
123       result->y = dst.y;
124     } else {
125       result->x = dst.x;
126       result->y = dst.y;
127       result->w = dst.w;
128       result->h = dst.h;
129     }
130   }
131 
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);
134 }
135 
136 /* Initing stuff */
137 
138 static void
gst_video_sink_init(GstVideoSink * videosink)139 gst_video_sink_init (GstVideoSink * videosink)
140 {
141   videosink->width = 0;
142   videosink->height = 0;
143 
144   /* 20ms is more than enough, 80-130ms is noticable */
145   gst_base_sink_set_processing_deadline (GST_BASE_SINK (videosink),
146       15 * GST_MSECOND);
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);
149 
150   videosink->priv = gst_video_sink_get_instance_private (videosink);
151 }
152 
153 static void
gst_video_sink_class_init(GstVideoSinkClass * klass)154 gst_video_sink_class_init (GstVideoSinkClass * klass)
155 {
156   GstBaseSinkClass *basesink_class = (GstBaseSinkClass *) klass;
157   GObjectClass *gobject_class = (GObjectClass *) klass;
158 
159   parent_class = g_type_class_peek_parent (klass);
160 
161   gobject_class->set_property = gst_video_sink_set_property;
162   gobject_class->get_property = gst_video_sink_get_property;
163 
164   /**
165    * GstVideoSink:show-preroll-frame:
166    *
167    * Whether to show video frames during preroll. If set to %FALSE, video
168    * frames will only be rendered in PLAYING state.
169    */
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));
175 
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);
179 }
180 
181 static GstFlowReturn
gst_video_sink_show_preroll_frame(GstBaseSink * bsink,GstBuffer * buf)182 gst_video_sink_show_preroll_frame (GstBaseSink * bsink, GstBuffer * buf)
183 {
184   GstVideoSinkClass *klass;
185   GstVideoSink *vsink;
186   gboolean do_show;
187 
188   vsink = GST_VIDEO_SINK_CAST (bsink);
189   klass = GST_VIDEO_SINK_GET_CLASS (vsink);
190 
191   do_show = g_atomic_int_get (&vsink->priv->show_preroll_frame);
192 
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)));
197   }
198 
199   if (klass->show_frame == NULL || !do_show) {
200     if (parent_class->preroll != NULL)
201       return parent_class->preroll (bsink, buf);
202     else
203       return GST_FLOW_OK;
204   }
205 
206   GST_LOG_OBJECT (bsink, "rendering frame, ts=%" GST_TIME_FORMAT,
207       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
208 
209   return klass->show_frame (GST_VIDEO_SINK_CAST (bsink), buf);
210 }
211 
212 static GstFlowReturn
gst_video_sink_show_frame(GstBaseSink * bsink,GstBuffer * buf)213 gst_video_sink_show_frame (GstBaseSink * bsink, GstBuffer * buf)
214 {
215   GstVideoSinkClass *klass;
216 
217   klass = GST_VIDEO_SINK_GET_CLASS (bsink);
218 
219   if (klass->show_frame == NULL) {
220     if (parent_class->render != NULL)
221       return parent_class->render (bsink, buf);
222     else
223       return GST_FLOW_OK;
224   }
225 
226   GST_LOG_OBJECT (bsink, "rendering frame, ts=%" GST_TIME_FORMAT,
227       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
228 
229   return klass->show_frame (GST_VIDEO_SINK_CAST (bsink), buf);
230 }
231 
232 static void
gst_video_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)233 gst_video_sink_set_property (GObject * object, guint prop_id,
234     const GValue * value, GParamSpec * pspec)
235 {
236   GstVideoSink *vsink;
237 
238   vsink = GST_VIDEO_SINK (object);
239 
240   switch (prop_id) {
241     case PROP_SHOW_PREROLL_FRAME:
242       g_atomic_int_set (&vsink->priv->show_preroll_frame,
243           g_value_get_boolean (value));
244       break;
245     default:
246       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247       break;
248   }
249 }
250 
251 static void
gst_video_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)252 gst_video_sink_get_property (GObject * object, guint prop_id,
253     GValue * value, GParamSpec * pspec)
254 {
255   GstVideoSink *vsink;
256 
257   vsink = GST_VIDEO_SINK (object);
258 
259   switch (prop_id) {
260     case PROP_SHOW_PREROLL_FRAME:
261       g_value_set_boolean (value,
262           g_atomic_int_get (&vsink->priv->show_preroll_frame));
263       break;
264     default:
265       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
266       break;
267   }
268 }
269