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