2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2005> 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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:element-clockoverlay
23 * @see_also: #GstTextOverlay, #GstTimeOverlay
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.
32 * <title>Example launch lines</title>
34 * gst-launch -v videotestsrc ! clockoverlay ! xvimagesink
35 * ]| Display the current time in the top left corner of the video picture
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.
49 #include "gstclockoverlay.h"
50 #include <gst/video/video.h>
54 #define DEFAULT_PROP_TIMEFORMAT "%H:%M:%S"
63 GST_BOILERPLATE (GstClockOverlay, gst_clock_overlay, GstTextOverlay,
64 GST_TYPE_TEXT_OVERLAY);
67 gst_clock_overlay_base_init (gpointer g_class)
69 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
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>");
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);
85 gst_clock_overlay_render_time (GstClockOverlay * overlay)
91 #ifdef HAVE_LOCALTIME_R
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. */
101 t = localtime_r (&now, &dummy);
103 /* on win32 this apparently returns a per-thread struct which would be fine */
104 t = localtime (&now);
108 return g_strdup ("--:--:--");
110 if (strftime (buf, sizeof (buf), overlay->format, t) == 0)
111 return g_strdup ("");
112 return g_strdup (buf);
115 /* Called with lock held */
117 gst_clock_overlay_get_text (GstTextOverlay * overlay, GstBuffer * video_frame)
119 gchar *time_str, *txt, *ret;
120 GstClockOverlay *clock_overlay = GST_CLOCK_OVERLAY (overlay);
122 txt = g_strdup (overlay->default_text);
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);
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);
145 gst_clock_overlay_class_init (GstClockOverlayClass * klass)
147 GObjectClass *gobject_class;
148 GstTextOverlayClass *gsttextoverlay_class;
149 PangoContext *context;
150 PangoFontDescription *font_description;
152 gobject_class = (GObjectClass *) klass;
153 gsttextoverlay_class = (GstTextOverlayClass *) klass;
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;
159 gsttextoverlay_class->get_text = gst_clock_overlay_get_text;
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));
166 g_mutex_lock (GST_TEXT_OVERLAY_CLASS (klass)->pango_lock);
167 context = GST_TEXT_OVERLAY_CLASS (klass)->pango_context;
169 pango_context_set_language (context, pango_language_from_string ("en_US"));
170 pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);
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);
186 gst_clock_overlay_finalize (GObject * object)
188 GstClockOverlay *overlay = GST_CLOCK_OVERLAY (object);
190 g_free (overlay->format);
191 g_free (overlay->text);
192 overlay->format = NULL;
194 G_OBJECT_CLASS (parent_class)->finalize (object);
199 gst_clock_overlay_init (GstClockOverlay * overlay, GstClockOverlayClass * klass)
201 GstTextOverlay *textoverlay;
203 textoverlay = GST_TEXT_OVERLAY (overlay);
205 textoverlay->valign = GST_TEXT_OVERLAY_VALIGN_TOP;
206 textoverlay->halign = GST_TEXT_OVERLAY_HALIGN_LEFT;
208 overlay->format = g_strdup (DEFAULT_PROP_TIMEFORMAT);
213 gst_clock_overlay_set_property (GObject * object, guint prop_id,
214 const GValue * value, GParamSpec * pspec)
216 GstClockOverlay *overlay = GST_CLOCK_OVERLAY (object);
218 GST_OBJECT_LOCK (overlay);
220 case PROP_TIMEFORMAT:
221 g_free (overlay->format);
222 overlay->format = g_value_dup_string (value);
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
228 GST_OBJECT_UNLOCK (overlay);
233 gst_clock_overlay_get_property (GObject * object, guint prop_id,
234 GValue * value, GParamSpec * pspec)
236 GstClockOverlay *overlay = GST_CLOCK_OVERLAY (object);
238 GST_OBJECT_LOCK (overlay);
240 case PROP_TIMEFORMAT:
241 g_value_set_string (value, overlay->format);
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247 GST_OBJECT_UNLOCK (overlay);