Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / gst-libs / gst / video / gstvideosink.c
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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * SECTION:gstvideosink
23  * @short_description: Base class for video sinks
24  * 
25  * <refsect2>
26  * <para>
27  * Provides useful functions and a base class for video sinks. 
28  * </para>
29  * <para>
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.
33  * </para>
34  * </refsect2>
35  */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include "gstvideosink.h"
42
43 enum
44 {
45   PROP_SHOW_PREROLL_FRAME = 1
46 };
47
48 #define DEFAULT_SHOW_PREROLL_FRAME TRUE
49
50 struct _GstVideoSinkPrivate
51 {
52   gboolean show_preroll_frame;  /* ATOMIC */
53 };
54
55 GST_DEBUG_CATEGORY_STATIC (video_sink_debug);
56 #define GST_CAT_DEFAULT video_sink_debug
57
58 static GstBaseSinkClass *parent_class = NULL;
59
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);
64
65 static GstFlowReturn gst_video_sink_show_preroll_frame (GstBaseSink * bsink,
66     GstBuffer * buf);
67 static GstFlowReturn gst_video_sink_show_frame (GstBaseSink * bsink,
68     GstBuffer * buf);
69
70 /**
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
76  * 
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.
80  */
81 void
82 gst_video_sink_center_rect (GstVideoRectangle src, GstVideoRectangle dst,
83     GstVideoRectangle * result, gboolean scaling)
84 {
85   g_return_if_fail (result != NULL);
86
87   if (!scaling) {
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;
92   } else {
93     gdouble src_ratio, dst_ratio;
94
95     src_ratio = (gdouble) src.w / src.h;
96     dst_ratio = (gdouble) dst.w / dst.h;
97
98     if (src_ratio > dst_ratio) {
99       result->w = dst.w;
100       result->h = dst.w / src_ratio;
101       result->x = 0;
102       result->y = (dst.h - result->h) / 2;
103     } else if (src_ratio < dst_ratio) {
104       result->w = dst.h * src_ratio;
105       result->h = dst.h;
106       result->x = (dst.w - result->w) / 2;
107       result->y = 0;
108     } else {
109       result->x = 0;
110       result->y = 0;
111       result->w = dst.w;
112       result->h = dst.h;
113     }
114   }
115
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);
118 }
119
120 /* Initing stuff */
121
122 static void
123 gst_video_sink_init (GstVideoSink * videosink)
124 {
125   videosink->width = 0;
126   videosink->height = 0;
127
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);
131
132   videosink->priv = G_TYPE_INSTANCE_GET_PRIVATE (videosink,
133       GST_TYPE_VIDEO_SINK, GstVideoSinkPrivate);
134 }
135
136 static void
137 gst_video_sink_class_init (GstVideoSinkClass * klass)
138 {
139   GstBaseSinkClass *basesink_class = (GstBaseSinkClass *) klass;
140   GObjectClass *gobject_class = (GObjectClass *) klass;
141
142   parent_class = g_type_class_peek_parent (klass);
143
144   gobject_class->set_property = gst_video_sink_set_property;
145   gobject_class->get_property = gst_video_sink_get_property;
146
147   /**
148    * GstVideoSink:show-preroll-frame
149    *
150    * Whether to show video frames during preroll. If set to #FALSE, video
151    * frames will only be rendered in PLAYING state.
152    *
153    * Since: 0.10.25
154    */
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));
160
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);
164
165   g_type_class_add_private (klass, sizeof (GstVideoSinkPrivate));
166 }
167
168 static void
169 gst_video_sink_base_init (gpointer g_class)
170 {
171   GST_DEBUG_CATEGORY_INIT (video_sink_debug, "videosink", 0, "GstVideoSink");
172 }
173
174 static GstFlowReturn
175 gst_video_sink_show_preroll_frame (GstBaseSink * bsink, GstBuffer * buf)
176 {
177   GstVideoSinkClass *klass;
178   GstVideoSink *vsink;
179   gboolean do_show;
180
181   vsink = GST_VIDEO_SINK_CAST (bsink);
182   klass = GST_VIDEO_SINK_GET_CLASS (vsink);
183
184   do_show = g_atomic_int_get (&vsink->priv->show_preroll_frame);
185
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)));
190   }
191
192   if (klass->show_frame == NULL || !do_show) {
193     if (parent_class->preroll != NULL)
194       return parent_class->preroll (bsink, buf);
195     else
196       return GST_FLOW_OK;
197   }
198
199   GST_LOG_OBJECT (bsink, "rendering frame, ts=%" GST_TIME_FORMAT,
200       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
201
202   return klass->show_frame (GST_VIDEO_SINK_CAST (bsink), buf);
203 }
204
205 static GstFlowReturn
206 gst_video_sink_show_frame (GstBaseSink * bsink, GstBuffer * buf)
207 {
208   GstVideoSinkClass *klass;
209
210   klass = GST_VIDEO_SINK_GET_CLASS (bsink);
211
212   if (klass->show_frame == NULL) {
213     if (parent_class->render != NULL)
214       return parent_class->render (bsink, buf);
215     else
216       return GST_FLOW_OK;
217   }
218
219   GST_LOG_OBJECT (bsink, "rendering frame, ts=%" GST_TIME_FORMAT,
220       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
221
222   return klass->show_frame (GST_VIDEO_SINK_CAST (bsink), buf);
223 }
224
225 static void
226 gst_video_sink_set_property (GObject * object, guint prop_id,
227     const GValue * value, GParamSpec * pspec)
228 {
229   GstVideoSink *vsink;
230
231   vsink = GST_VIDEO_SINK (object);
232
233   switch (prop_id) {
234     case PROP_SHOW_PREROLL_FRAME:
235       g_atomic_int_set (&vsink->priv->show_preroll_frame,
236           g_value_get_boolean (value));
237       break;
238     default:
239       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
240       break;
241   }
242 }
243
244 static void
245 gst_video_sink_get_property (GObject * object, guint prop_id,
246     GValue * value, GParamSpec * pspec)
247 {
248   GstVideoSink *vsink;
249
250   vsink = GST_VIDEO_SINK (object);
251
252   switch (prop_id) {
253     case PROP_SHOW_PREROLL_FRAME:
254       g_value_set_boolean (value,
255           g_atomic_int_get (&vsink->priv->show_preroll_frame));
256       break;
257     default:
258       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259       break;
260   }
261 }
262
263 /* Public methods */
264
265 GType
266 gst_video_sink_get_type (void)
267 {
268   static GType videosink_type = 0;
269
270   if (!videosink_type) {
271     static const GTypeInfo videosink_info = {
272       sizeof (GstVideoSinkClass),
273       gst_video_sink_base_init,
274       NULL,
275       (GClassInitFunc) gst_video_sink_class_init,
276       NULL,
277       NULL,
278       sizeof (GstVideoSink),
279       0,
280       (GInstanceInitFunc) gst_video_sink_init,
281     };
282
283     videosink_type = g_type_register_static (GST_TYPE_BASE_SINK,
284         "GstVideoSink", &videosink_info, 0);
285   }
286
287   return videosink_type;
288 }