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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 /**
22 * SECTION:element-clockoverlay
23 * @title: clockoverlay
24 * @see_also: #GstBaseTextOverlay, #GstTimeOverlay
25 *
26 * This element overlays the current clock time on top of a video
27 * stream. You can position the text and configure the font details
28 * using its properties.
29 *
30 * By default, the time is displayed in the top left corner of the picture, with some
31 * padding to the left and to the top.
32 *
33 * ## Example launch lines
34 * |[
35 * gst-launch-1.0 -v videotestsrc ! clockoverlay ! autovideosink
36 * ]|
37 * Display the current wall clock time in the top left corner of the video picture
38 * |[
39 * gst-launch-1.0 -v videotestsrc ! clockoverlay halignment=right valignment=bottom text="Edge City" shaded-background=true font-desc="Sans, 36" ! videoconvert ! autovideosink
40 * ]|
41 * Another pipeline that displays the current time with some leading
42 * text in the bottom right corner of the video picture, with the background
43 * of the text being shaded in order to make it more legible on top of a
44 * bright video background.
45 *
46 */
47
48 #ifdef HAVE_CONFIG_H
49 #include "config.h"
50 #endif
51
52 #include <gst/video/video.h>
53 #include <time.h>
54
55 #include "gstclockoverlay.h"
56 #include "gstpangoelements.h"
57
58 #define DEFAULT_PROP_TIMEFORMAT "%H:%M:%S"
59
60 enum
61 {
62 PROP_0,
63 PROP_TIMEFORMAT,
64 PROP_LAST
65 };
66
67 #define gst_clock_overlay_parent_class parent_class
68 G_DEFINE_TYPE (GstClockOverlay, gst_clock_overlay, GST_TYPE_BASE_TEXT_OVERLAY);
69 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (clockoverlay, "clockoverlay",
70 GST_RANK_NONE, GST_TYPE_CLOCK_OVERLAY, pango_element_init (plugin));
71
72 static void gst_clock_overlay_finalize (GObject * object);
73 static void gst_clock_overlay_set_property (GObject * object, guint prop_id,
74 const GValue * value, GParamSpec * pspec);
75 static void gst_clock_overlay_get_property (GObject * object, guint prop_id,
76 GValue * value, GParamSpec * pspec);
77
78 static gchar *
gst_clock_overlay_render_time(GstClockOverlay * overlay)79 gst_clock_overlay_render_time (GstClockOverlay * overlay)
80 {
81 #ifdef HAVE_LOCALTIME_R
82 struct tm dummy;
83 #endif
84 struct tm *t;
85 time_t now;
86 #ifdef G_OS_WIN32
87 gunichar2 buf[256];
88 #else
89 gchar buf[256];
90 #endif
91
92 now = time (NULL);
93
94 #ifdef HAVE_LOCALTIME_R
95 /* Need to call tzset explicitly when calling localtime_r for changes
96 * to the timezone between calls to be visible. */
97 tzset ();
98 t = localtime_r (&now, &dummy);
99 #else
100 /* on win32 this apparently returns a per-thread struct which would be fine */
101 t = localtime (&now);
102 #endif
103
104 if (t == NULL)
105 return g_strdup ("--:--:--");
106
107 #ifdef G_OS_WIN32
108 if (wcsftime (buf, sizeof (buf), (wchar_t *) overlay->wformat, t) == 0)
109 return g_strdup ("");
110
111 return g_utf16_to_utf8 (buf, -1, NULL, NULL, NULL);
112 #else
113 if (strftime (buf, sizeof (buf), overlay->format, t) == 0)
114 return g_strdup ("");
115
116 return g_strdup (buf);
117 #endif
118 }
119
120 /* Called with lock held */
121 static gchar *
gst_clock_overlay_get_text(GstBaseTextOverlay * overlay,GstBuffer * video_frame)122 gst_clock_overlay_get_text (GstBaseTextOverlay * overlay,
123 GstBuffer * video_frame)
124 {
125 gchar *time_str, *txt, *ret;
126 GstClockOverlay *clock_overlay = GST_CLOCK_OVERLAY (overlay);
127
128 txt = g_strdup (overlay->default_text);
129
130 GST_OBJECT_LOCK (overlay);
131 time_str = gst_clock_overlay_render_time (clock_overlay);
132 GST_OBJECT_UNLOCK (overlay);
133
134 if (txt != NULL && *txt != '\0') {
135 ret = g_strdup_printf ("%s %s", txt, time_str);
136 } else {
137 ret = time_str;
138 time_str = NULL;
139 }
140
141 if (g_strcmp0 (ret, clock_overlay->text)) {
142 overlay->need_render = TRUE;
143 g_free (clock_overlay->text);
144 clock_overlay->text = g_strdup (ret);
145 }
146
147 g_free (txt);
148 g_free (time_str);
149
150 return ret;
151 }
152
153 static void
gst_clock_overlay_class_init(GstClockOverlayClass * klass)154 gst_clock_overlay_class_init (GstClockOverlayClass * klass)
155 {
156 GObjectClass *gobject_class;
157 GstElementClass *gstelement_class;
158 GstBaseTextOverlayClass *gsttextoverlay_class;
159
160 gobject_class = (GObjectClass *) klass;
161 gstelement_class = (GstElementClass *) klass;
162 gsttextoverlay_class = (GstBaseTextOverlayClass *) klass;
163
164 gobject_class->finalize = gst_clock_overlay_finalize;
165 gobject_class->set_property = gst_clock_overlay_set_property;
166 gobject_class->get_property = gst_clock_overlay_get_property;
167
168 gst_element_class_set_static_metadata (gstelement_class, "Clock overlay",
169 "Filter/Editor/Video",
170 "Overlays the current clock time on a video stream",
171 "Tim-Philipp Müller <tim@centricular.net>");
172
173 gsttextoverlay_class->get_text = gst_clock_overlay_get_text;
174
175 g_object_class_install_property (gobject_class, PROP_TIMEFORMAT,
176 g_param_spec_string ("time-format", "Date/Time Format",
177 "Format to use for time and date value, as in strftime.",
178 DEFAULT_PROP_TIMEFORMAT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
179 }
180
181
182 static void
gst_clock_overlay_finalize(GObject * object)183 gst_clock_overlay_finalize (GObject * object)
184 {
185 GstClockOverlay *overlay = GST_CLOCK_OVERLAY (object);
186
187 g_free (overlay->format);
188 g_free (overlay->text);
189 overlay->format = NULL;
190
191 g_free (overlay->wformat);
192
193 G_OBJECT_CLASS (parent_class)->finalize (object);
194 }
195
196
197 static void
gst_clock_overlay_init(GstClockOverlay * overlay)198 gst_clock_overlay_init (GstClockOverlay * overlay)
199 {
200 GstBaseTextOverlay *textoverlay;
201 PangoContext *context;
202 PangoFontDescription *font_description;
203
204 textoverlay = GST_BASE_TEXT_OVERLAY (overlay);
205
206 textoverlay->valign = GST_BASE_TEXT_OVERLAY_VALIGN_TOP;
207 textoverlay->halign = GST_BASE_TEXT_OVERLAY_HALIGN_LEFT;
208
209 overlay->format = g_strdup (DEFAULT_PROP_TIMEFORMAT);
210
211 #ifdef G_OS_WIN32
212 overlay->wformat =
213 g_utf8_to_utf16 (DEFAULT_PROP_TIMEFORMAT, -1, NULL, NULL, NULL);
214 #endif
215
216 context = textoverlay->pango_context;
217
218 pango_context_set_language (context, pango_language_from_string ("en_US"));
219 pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);
220
221 font_description = pango_font_description_new ();
222 pango_font_description_set_family_static (font_description, "Monospace");
223 pango_font_description_set_style (font_description, PANGO_STYLE_NORMAL);
224 pango_font_description_set_variant (font_description, PANGO_VARIANT_NORMAL);
225 pango_font_description_set_weight (font_description, PANGO_WEIGHT_NORMAL);
226 pango_font_description_set_stretch (font_description, PANGO_STRETCH_NORMAL);
227 pango_font_description_set_size (font_description, 18 * PANGO_SCALE);
228 pango_context_set_font_description (context, font_description);
229 pango_font_description_free (font_description);
230 }
231
232 static void
gst_clock_overlay_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)233 gst_clock_overlay_set_property (GObject * object, guint prop_id,
234 const 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_free (overlay->format);
242 overlay->format = g_value_dup_string (value);
243 if (!overlay->format)
244 overlay->format = g_strdup (DEFAULT_PROP_TIMEFORMAT);
245 #ifdef G_OS_WIN32
246 g_free (overlay->wformat);
247 overlay->wformat =
248 g_utf8_to_utf16 (overlay->format, -1, NULL, NULL, NULL);
249 #endif
250 break;
251 default:
252 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
253 break;
254 }
255 GST_OBJECT_UNLOCK (overlay);
256 }
257
258
259 static void
gst_clock_overlay_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)260 gst_clock_overlay_get_property (GObject * object, guint prop_id,
261 GValue * value, GParamSpec * pspec)
262 {
263 GstClockOverlay *overlay = GST_CLOCK_OVERLAY (object);
264
265 GST_OBJECT_LOCK (overlay);
266 switch (prop_id) {
267 case PROP_TIMEFORMAT:
268 g_value_set_string (value, overlay->format);
269 break;
270 default:
271 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
272 break;
273 }
274 GST_OBJECT_UNLOCK (overlay);
275 }
276