46fda8c4f991e4a77613a2ab4d86d707c96dff6a
[platform/upstream/gstreamer.git] / 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., 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 "gstclockoverlay.h"
53 #include <gst/video/video.h>
54 #include <time.h>
55
56
57 #define DEFAULT_PROP_TIMEFORMAT         "%H:%M:%S"
58
59 enum
60 {
61   PROP_0,
62   PROP_TIMEFORMAT,
63   PROP_LAST
64 };
65
66 #define gst_clock_overlay_parent_class parent_class
67 G_DEFINE_TYPE (GstClockOverlay, gst_clock_overlay, GST_TYPE_BASE_TEXT_OVERLAY);
68
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);
74
75 static gchar *
76 gst_clock_overlay_render_time (GstClockOverlay * overlay)
77 {
78   struct tm *t;
79   time_t now;
80   gchar buf[256];
81
82 #ifdef HAVE_LOCALTIME_R
83   struct tm dummy;
84 #endif
85
86   now = time (NULL);
87
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.  */
91   tzset ();
92   t = localtime_r (&now, &dummy);
93 #else
94   /* on win32 this apparently returns a per-thread struct which would be fine */
95   t = localtime (&now);
96 #endif
97
98   if (t == NULL)
99     return g_strdup ("--:--:--");
100
101   if (strftime (buf, sizeof (buf), overlay->format, t) == 0)
102     return g_strdup ("");
103   return g_strdup (buf);
104 }
105
106 /* Called with lock held */
107 static gchar *
108 gst_clock_overlay_get_text (GstBaseTextOverlay * overlay,
109     GstBuffer * video_frame)
110 {
111   gchar *time_str, *txt, *ret;
112   GstClockOverlay *clock_overlay = GST_CLOCK_OVERLAY (overlay);
113
114   txt = g_strdup (overlay->default_text);
115
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);
119   } else {
120     ret = time_str;
121     time_str = NULL;
122   }
123
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);
128   }
129
130   g_free (txt);
131   g_free (time_str);
132
133   return ret;
134 }
135
136 static void
137 gst_clock_overlay_class_init (GstClockOverlayClass * klass)
138 {
139   GObjectClass *gobject_class;
140   GstElementClass *gstelement_class;
141   GstBaseTextOverlayClass *gsttextoverlay_class;
142
143   gobject_class = (GObjectClass *) klass;
144   gstelement_class = (GstElementClass *) klass;
145   gsttextoverlay_class = (GstBaseTextOverlayClass *) klass;
146
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;
150
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>");
155
156   gsttextoverlay_class->get_text = gst_clock_overlay_get_text;
157
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));
162 }
163
164
165 static void
166 gst_clock_overlay_finalize (GObject * object)
167 {
168   GstClockOverlay *overlay = GST_CLOCK_OVERLAY (object);
169
170   g_free (overlay->format);
171   g_free (overlay->text);
172   overlay->format = NULL;
173
174   G_OBJECT_CLASS (parent_class)->finalize (object);
175 }
176
177
178 static void
179 gst_clock_overlay_init (GstClockOverlay * overlay)
180 {
181   GstBaseTextOverlay *textoverlay;
182   PangoContext *context;
183   PangoFontDescription *font_description;
184
185   textoverlay = GST_BASE_TEXT_OVERLAY (overlay);
186
187   textoverlay->valign = GST_BASE_TEXT_OVERLAY_VALIGN_TOP;
188   textoverlay->halign = GST_BASE_TEXT_OVERLAY_HALIGN_LEFT;
189
190   overlay->format = g_strdup (DEFAULT_PROP_TIMEFORMAT);
191
192   context = textoverlay->pango_context;
193
194   pango_context_set_language (context, pango_language_from_string ("en_US"));
195   pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);
196
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);
206 }
207
208 static void
209 gst_clock_overlay_set_property (GObject * object, guint prop_id,
210     const GValue * value, GParamSpec * pspec)
211 {
212   GstClockOverlay *overlay = GST_CLOCK_OVERLAY (object);
213
214   GST_OBJECT_LOCK (overlay);
215   switch (prop_id) {
216     case PROP_TIMEFORMAT:
217       g_free (overlay->format);
218       overlay->format = g_value_dup_string (value);
219       break;
220     default:
221       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222       break;
223   }
224   GST_OBJECT_UNLOCK (overlay);
225 }
226
227
228 static void
229 gst_clock_overlay_get_property (GObject * object, guint prop_id,
230     GValue * value, GParamSpec * pspec)
231 {
232   GstClockOverlay *overlay = GST_CLOCK_OVERLAY (object);
233
234   GST_OBJECT_LOCK (overlay);
235   switch (prop_id) {
236     case PROP_TIMEFORMAT:
237       g_value_set_string (value, overlay->format);
238       break;
239     default:
240       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241       break;
242   }
243   GST_OBJECT_UNLOCK (overlay);
244 }