1 /* GStreamer
2 * Copyright (C) 1999 Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) 2005-2014 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-timeoverlay
23 * @title: timeoverlay
24 * @see_also: #GstBaseTextOverlay, #GstClockOverlay
25 *
26 * This element overlays the buffer time stamps of a video stream on
27 * top of itself. You can position the text and configure the font details
28 * using its properties.
29 *
30 * By default, the time stamp is displayed in the top left corner of the picture,
31 * with some padding to the left and to the top.
32 *
33 * |[
34 * gst-launch-1.0 -v videotestsrc ! timeoverlay ! autovideosink
35 * ]|
36 * Display the time stamps in the top left corner of the video picture.
37 * |[
38 * gst-launch-1.0 -v videotestsrc ! timeoverlay halignment=right valignment=bottom text="Stream time:" shaded-background=true font-desc="Sans, 24" ! autovideosink
39 * ]|
40 * Another pipeline that displays the time stamps 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 <gst/video/video.h>
52
53 #include "gsttimeoverlay.h"
54 #include "gstpangoelements.h"
55
56 #define DEFAULT_TIME_LINE GST_TIME_OVERLAY_TIME_LINE_BUFFER_TIME
57 #define DEFAULT_SHOW_TIMES_AS_DATES FALSE
58 #define DEFAULT_DATETIME_FORMAT "%F %T" /* YYYY-MM-DD hh:mm:ss */
59
60 enum
61 {
62 PROP_0,
63 PROP_TIME_LINE,
64 PROP_SHOW_TIMES_AS_DATES,
65 PROP_DATETIME_EPOCH,
66 PROP_DATETIME_FORMAT,
67 };
68
69 #define gst_time_overlay_parent_class parent_class
70 G_DEFINE_TYPE (GstTimeOverlay, gst_time_overlay, GST_TYPE_BASE_TEXT_OVERLAY);
71 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (timeoverlay, "timeoverlay",
72 GST_RANK_NONE, GST_TYPE_TIME_OVERLAY, pango_element_init (plugin));
73
74 static void gst_time_overlay_set_property (GObject * object, guint prop_id,
75 const GValue * value, GParamSpec * pspec);
76 static void gst_time_overlay_get_property (GObject * object, guint prop_id,
77 GValue * value, GParamSpec * pspec);
78
79 /**
80 * GstTimeOverlayTimeLine::elapsed-running-time:
81 *
82 * Overlay elapsed running time since the first observed running time.
83 *
84 * Since: 1.20
85 */
86
87 #define GST_TYPE_TIME_OVERLAY_TIME_LINE (gst_time_overlay_time_line_type())
88 static GType
gst_time_overlay_time_line_type(void)89 gst_time_overlay_time_line_type (void)
90 {
91 static GType time_line_type = 0;
92 static const GEnumValue modes[] = {
93 {GST_TIME_OVERLAY_TIME_LINE_BUFFER_TIME, "buffer-time", "buffer-time"},
94 {GST_TIME_OVERLAY_TIME_LINE_STREAM_TIME, "stream-time", "stream-time"},
95 {GST_TIME_OVERLAY_TIME_LINE_RUNNING_TIME, "running-time", "running-time"},
96 {GST_TIME_OVERLAY_TIME_LINE_TIME_CODE, "time-code", "time-code"},
97 {GST_TIME_OVERLAY_TIME_LINE_ELAPSED_RUNNING_TIME,
98 "elapsed-running-time", "elapsed-running-time"},
99 {0, NULL, NULL},
100 };
101
102 if (!time_line_type) {
103 time_line_type = g_enum_register_static ("GstTimeOverlayTimeLine", modes);
104 }
105 return time_line_type;
106 }
107
108 static gchar *
gst_time_overlay_render_time(GstTimeOverlay * overlay,GstClockTime time)109 gst_time_overlay_render_time (GstTimeOverlay * overlay, GstClockTime time)
110 {
111 guint hours, mins, secs, msecs;
112
113 if (!GST_CLOCK_TIME_IS_VALID (time))
114 return g_strdup ("");
115
116 hours = (guint) (time / (GST_SECOND * 60 * 60));
117 mins = (guint) ((time / (GST_SECOND * 60)) % 60);
118 secs = (guint) ((time / GST_SECOND) % 60);
119 msecs = (guint) ((time % GST_SECOND) / (1000 * 1000));
120
121 return g_strdup_printf ("%u:%02u:%02u.%03u", hours, mins, secs, msecs);
122 }
123
124 /* Called with lock held */
125 static gchar *
gst_time_overlay_get_text(GstBaseTextOverlay * overlay,GstBuffer * video_frame)126 gst_time_overlay_get_text (GstBaseTextOverlay * overlay,
127 GstBuffer * video_frame)
128 {
129 GstTimeOverlay *self = GST_TIME_OVERLAY (overlay);
130 GstTimeOverlayTimeLine time_line;
131 gchar *time_str, *txt, *ret;
132
133 overlay->need_render = TRUE;
134
135 time_line = g_atomic_int_get (&GST_TIME_OVERLAY_CAST (overlay)->time_line);
136 if (time_line == GST_TIME_OVERLAY_TIME_LINE_TIME_CODE) {
137 GstVideoTimeCodeMeta *tc_meta =
138 gst_buffer_get_video_time_code_meta (video_frame);
139 if (!tc_meta) {
140 GST_DEBUG ("buffer without valid timecode");
141 return g_strdup ("00:00:00:00");
142 }
143 time_str = gst_video_time_code_to_string (&tc_meta->tc);
144 GST_DEBUG ("buffer with timecode %s", time_str);
145 } else {
146 GstClockTime ts, ts_buffer;
147 GstSegment *segment = &overlay->segment;
148
149 ts_buffer = GST_BUFFER_TIMESTAMP (video_frame);
150
151 if (!GST_CLOCK_TIME_IS_VALID (ts_buffer)) {
152 GST_DEBUG ("buffer without valid timestamp");
153 return g_strdup ("");
154 }
155
156 GST_DEBUG ("buffer with timestamp %" GST_TIME_FORMAT,
157 GST_TIME_ARGS (ts_buffer));
158
159 switch (time_line) {
160 case GST_TIME_OVERLAY_TIME_LINE_STREAM_TIME:
161 ts = gst_segment_to_stream_time (segment, GST_FORMAT_TIME, ts_buffer);
162 break;
163 case GST_TIME_OVERLAY_TIME_LINE_RUNNING_TIME:
164 ts = gst_segment_to_running_time (segment, GST_FORMAT_TIME, ts_buffer);
165 break;
166 case GST_TIME_OVERLAY_TIME_LINE_ELAPSED_RUNNING_TIME:
167 ts = gst_segment_to_running_time (segment, GST_FORMAT_TIME, ts_buffer);
168 if (self->first_running_time == GST_CLOCK_TIME_NONE)
169 self->first_running_time = ts;
170 ts -= self->first_running_time;
171 break;
172 case GST_TIME_OVERLAY_TIME_LINE_BUFFER_TIME:
173 default:
174 ts = ts_buffer;
175 break;
176 }
177
178 if (self->show_times_as_dates) {
179 GDateTime *datetime;
180
181 datetime =
182 g_date_time_add_seconds (self->datetime_epoch,
183 (gdouble) GST_BUFFER_TIMESTAMP (video_frame) / GST_SECOND);
184 time_str = g_date_time_format (datetime, self->datetime_format);
185 g_date_time_unref (datetime);
186 } else {
187 time_str = gst_time_overlay_render_time (GST_TIME_OVERLAY (overlay), ts);
188 }
189 }
190
191 txt = g_strdup (overlay->default_text);
192
193 if (txt != NULL && *txt != '\0') {
194 ret = g_strdup_printf ("%s %s", txt, time_str);
195 } else {
196 ret = time_str;
197 time_str = NULL;
198 }
199
200 g_free (txt);
201 g_free (time_str);
202
203 return ret;
204 }
205
206 static GstStateChangeReturn
gst_time_overlay_change_state(GstElement * element,GstStateChange transition)207 gst_time_overlay_change_state (GstElement * element, GstStateChange transition)
208 {
209 GstTimeOverlay *self = GST_TIME_OVERLAY (element);
210
211 switch (transition) {
212 case GST_STATE_CHANGE_READY_TO_PAUSED:
213 self->first_running_time = GST_CLOCK_TIME_NONE;
214 break;
215 default:
216 break;
217 }
218
219 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
220 }
221
222 static void
gst_time_overlay_finalize(GObject * gobject)223 gst_time_overlay_finalize (GObject * gobject)
224 {
225 GstTimeOverlay *self = GST_TIME_OVERLAY (gobject);
226
227 g_date_time_unref (self->datetime_epoch);
228 g_free (self->datetime_format);
229 G_OBJECT_CLASS (parent_class)->finalize (gobject);
230 }
231
232 static void
gst_time_overlay_class_init(GstTimeOverlayClass * klass)233 gst_time_overlay_class_init (GstTimeOverlayClass * klass)
234 {
235 GstElementClass *gstelement_class;
236 GstBaseTextOverlayClass *gsttextoverlay_class;
237 GObjectClass *gobject_class;
238
239 gsttextoverlay_class = (GstBaseTextOverlayClass *) klass;
240 gstelement_class = (GstElementClass *) klass;
241 gobject_class = (GObjectClass *) klass;
242
243 gst_element_class_set_static_metadata (gstelement_class, "Time overlay",
244 "Filter/Editor/Video",
245 "Overlays buffer time stamps on a video stream",
246 "Tim-Philipp Müller <tim@centricular.net>");
247
248 gsttextoverlay_class->get_text = gst_time_overlay_get_text;
249
250 gstelement_class->change_state = gst_time_overlay_change_state;
251
252 gobject_class->finalize = gst_time_overlay_finalize;
253 gobject_class->set_property = gst_time_overlay_set_property;
254 gobject_class->get_property = gst_time_overlay_get_property;
255
256 g_object_class_install_property (gobject_class, PROP_TIME_LINE,
257 g_param_spec_enum ("time-mode", "Time Mode", "What time to show",
258 GST_TYPE_TIME_OVERLAY_TIME_LINE, DEFAULT_TIME_LINE,
259 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
260
261 g_object_class_install_property (gobject_class, PROP_DATETIME_EPOCH,
262 g_param_spec_boxed ("datetime-epoch", "Datetime Epoch",
263 "When showing times as dates, the initial date from which time "
264 "is counted, if not specified prime epoch is used (1900-01-01)",
265 G_TYPE_DATE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
266
267 g_object_class_install_property (gobject_class, PROP_DATETIME_FORMAT,
268 g_param_spec_string ("datetime-format", "Datetime Format",
269 "When showing times as dates, the format to render date and time in",
270 DEFAULT_DATETIME_FORMAT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
271
272 g_object_class_install_property (gobject_class, PROP_SHOW_TIMES_AS_DATES,
273 g_param_spec_boolean ("show-times-as-dates", "Show times as dates",
274 "Whether to display times, counted from datetime-epoch, as dates",
275 DEFAULT_SHOW_TIMES_AS_DATES,
276 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
277
278 gst_type_mark_as_plugin_api (GST_TYPE_TIME_OVERLAY_TIME_LINE, 0);
279 }
280
281 static gboolean
gst_time_overlay_video_event(GstPad * pad,GstObject * parent,GstEvent * event)282 gst_time_overlay_video_event (GstPad * pad, GstObject * parent,
283 GstEvent * event)
284 {
285 GstTimeOverlay *overlay = GST_TIME_OVERLAY (parent);
286
287 switch (GST_EVENT_TYPE (event)) {
288 case GST_EVENT_FLUSH_STOP:
289 overlay->first_running_time = GST_CLOCK_TIME_NONE;
290 break;
291 default:
292 break;
293 }
294
295 return overlay->orig_video_event (pad, parent, event);
296 }
297
298 static void
gst_time_overlay_init(GstTimeOverlay * overlay)299 gst_time_overlay_init (GstTimeOverlay * overlay)
300 {
301 GstBaseTextOverlay *textoverlay;
302 PangoContext *context;
303 PangoFontDescription *font_description;
304 GstPad *video_sink;
305
306 textoverlay = GST_BASE_TEXT_OVERLAY (overlay);
307
308 textoverlay->valign = GST_BASE_TEXT_OVERLAY_VALIGN_TOP;
309 textoverlay->halign = GST_BASE_TEXT_OVERLAY_HALIGN_LEFT;
310
311 overlay->time_line = DEFAULT_TIME_LINE;
312 overlay->show_times_as_dates = DEFAULT_SHOW_TIMES_AS_DATES;
313 overlay->datetime_epoch = g_date_time_new_utc (1900, 1, 1, 0, 0, 0);
314 overlay->datetime_format = g_strdup (DEFAULT_DATETIME_FORMAT);
315
316 context = textoverlay->pango_context;
317
318 pango_context_set_language (context, pango_language_from_string ("en_US"));
319 pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);
320
321 font_description = pango_font_description_new ();
322 pango_font_description_set_family_static (font_description, "Monospace");
323 pango_font_description_set_style (font_description, PANGO_STYLE_NORMAL);
324 pango_font_description_set_variant (font_description, PANGO_VARIANT_NORMAL);
325 pango_font_description_set_weight (font_description, PANGO_WEIGHT_NORMAL);
326 pango_font_description_set_stretch (font_description, PANGO_STRETCH_NORMAL);
327 pango_font_description_set_size (font_description, 18 * PANGO_SCALE);
328 pango_context_set_font_description (context, font_description);
329 pango_font_description_free (font_description);
330
331 video_sink = gst_element_get_static_pad (GST_ELEMENT (overlay), "video_sink");
332 overlay->orig_video_event = GST_PAD_EVENTFUNC (video_sink);
333 gst_pad_set_event_function (video_sink, gst_time_overlay_video_event);
334 }
335
336 static void
gst_time_overlay_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)337 gst_time_overlay_set_property (GObject * object, guint prop_id,
338 const GValue * value, GParamSpec * pspec)
339 {
340 GstTimeOverlay *overlay = GST_TIME_OVERLAY (object);
341
342 switch (prop_id) {
343 case PROP_TIME_LINE:
344 g_atomic_int_set (&overlay->time_line, g_value_get_enum (value));
345 break;
346 case PROP_SHOW_TIMES_AS_DATES:
347 overlay->show_times_as_dates = g_value_get_boolean (value);
348 break;
349 case PROP_DATETIME_EPOCH:
350 g_date_time_unref (overlay->datetime_epoch);
351 overlay->datetime_epoch = (GDateTime *) g_value_dup_boxed (value);
352 break;
353 case PROP_DATETIME_FORMAT:
354 g_free (overlay->datetime_format);
355 overlay->datetime_format = g_value_dup_string (value);
356 break;
357 default:
358 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
359 break;
360 }
361 }
362
363 static void
gst_time_overlay_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)364 gst_time_overlay_get_property (GObject * object, guint prop_id,
365 GValue * value, GParamSpec * pspec)
366 {
367 GstTimeOverlay *overlay = GST_TIME_OVERLAY (object);
368
369 switch (prop_id) {
370 case PROP_TIME_LINE:
371 g_value_set_enum (value, g_atomic_int_get (&overlay->time_line));
372 break;
373 case PROP_SHOW_TIMES_AS_DATES:
374 g_value_set_boolean (value, overlay->show_times_as_dates);
375 break;
376 case PROP_DATETIME_EPOCH:
377 g_value_set_boxed (value, overlay->datetime_epoch);
378 break;
379 case PROP_DATETIME_FORMAT:
380 g_value_set_string (value, overlay->datetime_format);
381 break;
382 default:
383 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
384 break;
385 }
386 }
387