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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * SECTION:element-clockoverlay
23 * @title: clockoverlay
24 * @see_also: #GstBaseTextOverlay, #GstTimeOverlay
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.
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.
33 * ## Example launch lines
35 * gst-launch-1.0 -v videotestsrc ! clockoverlay ! autovideosink
37 * Display the current wall clock time in the top left corner of the video picture
39 * gst-launch-1.0 -v videotestsrc ! clockoverlay halignment=right valignment=bottom text="Edge City" shaded-background=true font-desc="Sans, 36" ! videoconvert ! autovideosink
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.
52 #include "gstclockoverlay.h"
53 #include <gst/video/video.h>
57 #define DEFAULT_PROP_TIMEFORMAT "%H:%M:%S"
66 #define gst_clock_overlay_parent_class parent_class
67 G_DEFINE_TYPE (GstClockOverlay, gst_clock_overlay, GST_TYPE_BASE_TEXT_OVERLAY);
69 static void gst_clock_overlay_finalize (GObject * object);
70 static void gst_clock_overlay_set_property (GObject * object, guint prop_id,
71 const GValue * value, GParamSpec * pspec);
72 static void gst_clock_overlay_get_property (GObject * object, guint prop_id,
73 GValue * value, GParamSpec * pspec);
76 gst_clock_overlay_render_time (GstClockOverlay * overlay)
82 #ifdef HAVE_LOCALTIME_R
88 #ifdef HAVE_LOCALTIME_R
89 /* Need to call tzset explicitly when calling localtime_r for changes
90 to the timezone between calls to be visible. */
92 t = localtime_r (&now, &dummy);
94 /* on win32 this apparently returns a per-thread struct which would be fine */
99 return g_strdup ("--:--:--");
101 if (strftime (buf, sizeof (buf), overlay->format, t) == 0)
102 return g_strdup ("");
103 return g_strdup (buf);
106 /* Called with lock held */
108 gst_clock_overlay_get_text (GstBaseTextOverlay * overlay,
109 GstBuffer * video_frame)
111 gchar *time_str, *txt, *ret;
112 GstClockOverlay *clock_overlay = GST_CLOCK_OVERLAY (overlay);
114 txt = g_strdup (overlay->default_text);
116 time_str = gst_clock_overlay_render_time (clock_overlay);
117 if (txt != NULL && *txt != '\0') {
118 ret = g_strdup_printf ("%s %s", txt, time_str);
124 if (g_strcmp0 (ret, clock_overlay->text)) {
125 overlay->need_render = TRUE;
126 g_free (clock_overlay->text);
127 clock_overlay->text = g_strdup (ret);
137 gst_clock_overlay_class_init (GstClockOverlayClass * klass)
139 GObjectClass *gobject_class;
140 GstElementClass *gstelement_class;
141 GstBaseTextOverlayClass *gsttextoverlay_class;
143 gobject_class = (GObjectClass *) klass;
144 gstelement_class = (GstElementClass *) klass;
145 gsttextoverlay_class = (GstBaseTextOverlayClass *) klass;
147 gobject_class->finalize = gst_clock_overlay_finalize;
148 gobject_class->set_property = gst_clock_overlay_set_property;
149 gobject_class->get_property = gst_clock_overlay_get_property;
151 gst_element_class_set_static_metadata (gstelement_class, "Clock overlay",
152 "Filter/Editor/Video",
153 "Overlays the current clock time on a video stream",
154 "Tim-Philipp Müller <tim@centricular.net>");
156 gsttextoverlay_class->get_text = gst_clock_overlay_get_text;
158 g_object_class_install_property (gobject_class, PROP_TIMEFORMAT,
159 g_param_spec_string ("time-format", "Date/Time Format",
160 "Format to use for time and date value, as in strftime.",
161 DEFAULT_PROP_TIMEFORMAT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
166 gst_clock_overlay_finalize (GObject * object)
168 GstClockOverlay *overlay = GST_CLOCK_OVERLAY (object);
170 g_free (overlay->format);
171 g_free (overlay->text);
172 overlay->format = NULL;
174 G_OBJECT_CLASS (parent_class)->finalize (object);
179 gst_clock_overlay_init (GstClockOverlay * overlay)
181 GstBaseTextOverlay *textoverlay;
182 PangoContext *context;
183 PangoFontDescription *font_description;
185 textoverlay = GST_BASE_TEXT_OVERLAY (overlay);
187 textoverlay->valign = GST_BASE_TEXT_OVERLAY_VALIGN_TOP;
188 textoverlay->halign = GST_BASE_TEXT_OVERLAY_HALIGN_LEFT;
190 overlay->format = g_strdup (DEFAULT_PROP_TIMEFORMAT);
192 context = textoverlay->pango_context;
194 pango_context_set_language (context, pango_language_from_string ("en_US"));
195 pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);
197 font_description = pango_font_description_new ();
198 pango_font_description_set_family_static (font_description, "Monospace");
199 pango_font_description_set_style (font_description, PANGO_STYLE_NORMAL);
200 pango_font_description_set_variant (font_description, PANGO_VARIANT_NORMAL);
201 pango_font_description_set_weight (font_description, PANGO_WEIGHT_NORMAL);
202 pango_font_description_set_stretch (font_description, PANGO_STRETCH_NORMAL);
203 pango_font_description_set_size (font_description, 18 * PANGO_SCALE);
204 pango_context_set_font_description (context, font_description);
205 pango_font_description_free (font_description);
209 gst_clock_overlay_set_property (GObject * object, guint prop_id,
210 const GValue * value, GParamSpec * pspec)
212 GstClockOverlay *overlay = GST_CLOCK_OVERLAY (object);
214 GST_OBJECT_LOCK (overlay);
216 case PROP_TIMEFORMAT:
217 g_free (overlay->format);
218 overlay->format = g_value_dup_string (value);
221 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
224 GST_OBJECT_UNLOCK (overlay);
229 gst_clock_overlay_get_property (GObject * object, guint prop_id,
230 GValue * value, GParamSpec * pspec)
232 GstClockOverlay *overlay = GST_CLOCK_OVERLAY (object);
234 GST_OBJECT_LOCK (overlay);
236 case PROP_TIMEFORMAT:
237 g_value_set_string (value, overlay->format);
240 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
243 GST_OBJECT_UNLOCK (overlay);