Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / ext / pango / gstclockoverlay.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2005> 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:element-clockoverlay
23  * @see_also: #GstTextOverlay, #GstTimeOverlay
24  *
25  * This element overlays the current clock time on top of a video
26  * stream. You can position the text and configure the font details
27  * using the properties of the #GstTextOverlay class. By default, the
28  * time is displayed in the top left corner of the picture, with some
29  * padding to the left and to the top.
30  *
31  * <refsect2>
32  * <title>Example launch lines</title>
33  * |[
34  * gst-launch -v videotestsrc ! clockoverlay ! xvimagesink
35  * ]| Display the current time in the top left corner of the video picture
36  * |[
37  * gst-launch -v videotestsrc ! clockoverlay halign=right valign=bottom text="Edge City" shaded-background=true ! ffmpegcolorspace ! ximagesink
38  * ]| Another pipeline that displays the current time with some leading
39  * text in the bottom right corner of the video picture, with the background
40  * of the text being shaded in order to make it more legible on top of a
41  * bright video background.
42  * </refsect2>
43  */
44
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #include <gstclockoverlay.h>
50 #include <gst/video/video.h>
51 #include <time.h>
52
53
54 #define DEFAULT_PROP_TIMEFORMAT         "%H:%M:%S"
55
56 enum
57 {
58   PROP_0,
59   PROP_TIMEFORMAT,
60   PROP_LAST
61 };
62
63 GST_BOILERPLATE (GstClockOverlay, gst_clock_overlay, GstTextOverlay,
64     GST_TYPE_TEXT_OVERLAY);
65
66 static void
67 gst_clock_overlay_base_init (gpointer g_class)
68 {
69   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
70
71   gst_element_class_set_details_simple (element_class, "Clock overlay",
72       "Filter/Editor/Video",
73       "Overlays the current clock time on a video stream",
74       "Tim-Philipp Müller <tim@centricular.net>");
75 }
76
77
78 static void gst_clock_overlay_finalize (GObject * object);
79 static void gst_clock_overlay_set_property (GObject * object, guint prop_id,
80     const GValue * value, GParamSpec * pspec);
81 static void gst_clock_overlay_get_property (GObject * object, guint prop_id,
82     GValue * value, GParamSpec * pspec);
83
84 static gchar *
85 gst_clock_overlay_render_time (GstClockOverlay * overlay)
86 {
87   struct tm *t;
88   time_t now;
89   gchar buf[256];
90
91 #ifdef HAVE_LOCALTIME_R
92   struct tm dummy;
93 #endif
94
95   now = time (NULL);
96
97 #ifdef HAVE_LOCALTIME_R
98   /* Need to call tzset explicitly when calling localtime_r for changes
99      to the timezone between calls to be visible.  */
100   tzset ();
101   t = localtime_r (&now, &dummy);
102 #else
103   /* on win32 this apparently returns a per-thread struct which would be fine */
104   t = localtime (&now);
105 #endif
106
107   if (t == NULL)
108     return g_strdup ("--:--:--");
109
110   if (strftime (buf, sizeof (buf), overlay->format, t) == 0)
111     return g_strdup ("");
112   return g_strdup (buf);
113 }
114
115 /* Called with lock held */
116 static gchar *
117 gst_clock_overlay_get_text (GstTextOverlay * overlay, GstBuffer * video_frame)
118 {
119   gchar *time_str, *txt, *ret;
120   GstClockOverlay *clock_overlay = GST_CLOCK_OVERLAY (overlay);
121
122   txt = g_strdup (overlay->default_text);
123
124   time_str = gst_clock_overlay_render_time (clock_overlay);
125   if (txt != NULL && *txt != '\0') {
126     ret = g_strdup_printf ("%s %s", txt, time_str);
127   } else {
128     ret = time_str;
129     time_str = NULL;
130   }
131
132   if (g_strcmp0 (ret, clock_overlay->text)) {
133     overlay->need_render = TRUE;
134     g_free (clock_overlay->text);
135     clock_overlay->text = g_strdup (ret);
136   }
137
138   g_free (txt);
139   g_free (time_str);
140
141   return ret;
142 }
143
144 static void
145 gst_clock_overlay_class_init (GstClockOverlayClass * klass)
146 {
147   GObjectClass *gobject_class;
148   GstTextOverlayClass *gsttextoverlay_class;
149   PangoContext *context;
150   PangoFontDescription *font_description;
151
152   gobject_class = (GObjectClass *) klass;
153   gsttextoverlay_class = (GstTextOverlayClass *) klass;
154
155   gobject_class->finalize = gst_clock_overlay_finalize;
156   gobject_class->set_property = gst_clock_overlay_set_property;
157   gobject_class->get_property = gst_clock_overlay_get_property;
158
159   gsttextoverlay_class->get_text = gst_clock_overlay_get_text;
160
161   g_object_class_install_property (gobject_class, PROP_TIMEFORMAT,
162       g_param_spec_string ("time-format", "Date/Time Format",
163           "Format to use for time and date value, as in strftime.",
164           DEFAULT_PROP_TIMEFORMAT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
165
166   g_mutex_lock (GST_TEXT_OVERLAY_CLASS (klass)->pango_lock);
167   context = GST_TEXT_OVERLAY_CLASS (klass)->pango_context;
168
169   pango_context_set_language (context, pango_language_from_string ("en_US"));
170   pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);
171
172   font_description = pango_font_description_new ();
173   pango_font_description_set_family_static (font_description, "Monospace");
174   pango_font_description_set_style (font_description, PANGO_STYLE_NORMAL);
175   pango_font_description_set_variant (font_description, PANGO_VARIANT_NORMAL);
176   pango_font_description_set_weight (font_description, PANGO_WEIGHT_NORMAL);
177   pango_font_description_set_stretch (font_description, PANGO_STRETCH_NORMAL);
178   pango_font_description_set_size (font_description, 18 * PANGO_SCALE);
179   pango_context_set_font_description (context, font_description);
180   pango_font_description_free (font_description);
181   g_mutex_unlock (GST_TEXT_OVERLAY_CLASS (klass)->pango_lock);
182 }
183
184
185 static void
186 gst_clock_overlay_finalize (GObject * object)
187 {
188   GstClockOverlay *overlay = GST_CLOCK_OVERLAY (object);
189
190   g_free (overlay->format);
191   g_free (overlay->text);
192   overlay->format = NULL;
193
194   G_OBJECT_CLASS (parent_class)->finalize (object);
195 }
196
197
198 static void
199 gst_clock_overlay_init (GstClockOverlay * overlay, GstClockOverlayClass * klass)
200 {
201   GstTextOverlay *textoverlay;
202
203   textoverlay = GST_TEXT_OVERLAY (overlay);
204
205   textoverlay->valign = GST_TEXT_OVERLAY_VALIGN_TOP;
206   textoverlay->halign = GST_TEXT_OVERLAY_HALIGN_LEFT;
207
208   overlay->format = g_strdup (DEFAULT_PROP_TIMEFORMAT);
209 }
210
211
212 static void
213 gst_clock_overlay_set_property (GObject * object, guint prop_id,
214     const GValue * value, GParamSpec * pspec)
215 {
216   GstClockOverlay *overlay = GST_CLOCK_OVERLAY (object);
217
218   GST_OBJECT_LOCK (overlay);
219   switch (prop_id) {
220     case PROP_TIMEFORMAT:
221       g_free (overlay->format);
222       overlay->format = g_value_dup_string (value);
223       break;
224     default:
225       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
226       break;
227   }
228   GST_OBJECT_UNLOCK (overlay);
229 }
230
231
232 static void
233 gst_clock_overlay_get_property (GObject * object, guint prop_id,
234     GValue * value, GParamSpec * pspec)
235 {
236   GstClockOverlay *overlay = GST_CLOCK_OVERLAY (object);
237
238   GST_OBJECT_LOCK (overlay);
239   switch (prop_id) {
240     case PROP_TIMEFORMAT:
241       g_value_set_string (value, overlay->format);
242       break;
243     default:
244       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245       break;
246   }
247   GST_OBJECT_UNLOCK (overlay);
248 }