2 * Copyright (C) 1999 Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) 2005-2014 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-timeoverlay
24 * @see_also: #GstBaseTextOverlay, #GstClockOverlay
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.
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.
34 * gst-launch-1.0 -v videotestsrc ! timeoverlay ! autovideosink
36 * Display the time stamps in the top left corner of the video picture.
38 * gst-launch-1.0 -v videotestsrc ! timeoverlay halignment=right valignment=bottom text="Stream time:" shaded-background=true font-desc="Sans, 24" ! autovideosink
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.
51 #include <gst/video/video.h>
53 #include "gsttimeoverlay.h"
54 #include "gstpangoelements.h"
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 */
60 static GstStaticCaps ntp_reference_timestamp_caps =
61 GST_STATIC_CAPS ("timestamp/x-ntp");
67 PROP_SHOW_TIMES_AS_DATES,
70 PROP_REFERENCE_TIMESTAMP_CAPS,
73 #define gst_time_overlay_parent_class parent_class
74 G_DEFINE_TYPE (GstTimeOverlay, gst_time_overlay, GST_TYPE_BASE_TEXT_OVERLAY);
75 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (timeoverlay, "timeoverlay",
76 GST_RANK_NONE, GST_TYPE_TIME_OVERLAY, pango_element_init (plugin));
78 static void gst_time_overlay_set_property (GObject * object, guint prop_id,
79 const GValue * value, GParamSpec * pspec);
80 static void gst_time_overlay_get_property (GObject * object, guint prop_id,
81 GValue * value, GParamSpec * pspec);
84 * GstTimeOverlayTimeLine::elapsed-running-time:
86 * Overlay elapsed running time since the first observed running time.
92 * GstTimeOverlayTimeLine::reference-timestamp:
94 * Use #GstReferenceTimestampMeta.
99 #define GST_TYPE_TIME_OVERLAY_TIME_LINE (gst_time_overlay_time_line_type())
101 gst_time_overlay_time_line_type (void)
103 static GType time_line_type = 0;
104 static const GEnumValue modes[] = {
105 {GST_TIME_OVERLAY_TIME_LINE_BUFFER_TIME, "buffer-time", "buffer-time"},
106 {GST_TIME_OVERLAY_TIME_LINE_STREAM_TIME, "stream-time", "stream-time"},
107 {GST_TIME_OVERLAY_TIME_LINE_RUNNING_TIME, "running-time", "running-time"},
108 {GST_TIME_OVERLAY_TIME_LINE_TIME_CODE, "time-code", "time-code"},
109 {GST_TIME_OVERLAY_TIME_LINE_ELAPSED_RUNNING_TIME,
110 "elapsed-running-time", "elapsed-running-time"},
111 {GST_TIME_OVERLAY_TIME_LINE_REFERENCE_TIMESTAMP,
112 "reference-timestamp", "reference-timestamp"},
116 if (!time_line_type) {
117 time_line_type = g_enum_register_static ("GstTimeOverlayTimeLine", modes);
119 return time_line_type;
123 gst_time_overlay_render_time (GstTimeOverlay * overlay, GstClockTime time)
125 guint hours, mins, secs, msecs;
127 if (!GST_CLOCK_TIME_IS_VALID (time))
128 return g_strdup ("");
130 hours = (guint) (time / (GST_SECOND * 60 * 60));
131 mins = (guint) ((time / (GST_SECOND * 60)) % 60);
132 secs = (guint) ((time / GST_SECOND) % 60);
133 msecs = (guint) ((time % GST_SECOND) / (1000 * 1000));
135 return g_strdup_printf ("%u:%02u:%02u.%03u", hours, mins, secs, msecs);
138 /* Called with lock held */
140 gst_time_overlay_get_text (GstBaseTextOverlay * overlay,
141 GstBuffer * video_frame)
143 GstTimeOverlay *self = GST_TIME_OVERLAY (overlay);
144 GstTimeOverlayTimeLine time_line;
145 gchar *time_str, *txt, *ret;
147 overlay->need_render = TRUE;
149 time_line = g_atomic_int_get (&GST_TIME_OVERLAY_CAST (overlay)->time_line);
150 if (time_line == GST_TIME_OVERLAY_TIME_LINE_TIME_CODE) {
151 GstVideoTimeCodeMeta *tc_meta =
152 gst_buffer_get_video_time_code_meta (video_frame);
154 GST_DEBUG ("buffer without valid timecode");
155 return g_strdup ("00:00:00:00");
157 time_str = gst_video_time_code_to_string (&tc_meta->tc);
158 GST_DEBUG ("buffer with timecode %s", time_str);
160 GstClockTime ts, ts_buffer;
161 GstSegment *segment = &overlay->segment;
163 ts_buffer = GST_BUFFER_TIMESTAMP (video_frame);
165 if (!GST_CLOCK_TIME_IS_VALID (ts_buffer)) {
166 GST_DEBUG ("buffer without valid timestamp");
167 return g_strdup ("");
170 GST_DEBUG ("buffer with timestamp %" GST_TIME_FORMAT,
171 GST_TIME_ARGS (ts_buffer));
174 case GST_TIME_OVERLAY_TIME_LINE_STREAM_TIME:
175 ts = gst_segment_to_stream_time (segment, GST_FORMAT_TIME, ts_buffer);
177 case GST_TIME_OVERLAY_TIME_LINE_RUNNING_TIME:
178 ts = gst_segment_to_running_time (segment, GST_FORMAT_TIME, ts_buffer);
180 case GST_TIME_OVERLAY_TIME_LINE_ELAPSED_RUNNING_TIME:
181 ts = gst_segment_to_running_time (segment, GST_FORMAT_TIME, ts_buffer);
182 if (self->first_running_time == GST_CLOCK_TIME_NONE)
183 self->first_running_time = ts;
184 ts -= self->first_running_time;
186 case GST_TIME_OVERLAY_TIME_LINE_REFERENCE_TIMESTAMP:
188 GstReferenceTimestampMeta *meta;
190 if (self->reference_timestamp_caps) {
192 gst_buffer_get_reference_timestamp_meta (video_frame,
193 self->reference_timestamp_caps);
196 ts = meta->timestamp;
206 case GST_TIME_OVERLAY_TIME_LINE_BUFFER_TIME:
212 if (self->show_times_as_dates) {
216 g_date_time_add_seconds (self->datetime_epoch,
217 ((gdouble) ts) / GST_SECOND);
219 time_str = g_date_time_format (datetime, self->datetime_format);
221 g_date_time_unref (datetime);
223 time_str = gst_time_overlay_render_time (GST_TIME_OVERLAY (overlay), ts);
227 txt = g_strdup (overlay->default_text);
229 if (txt != NULL && *txt != '\0') {
230 ret = g_strdup_printf ("%s %s", txt, time_str);
242 static GstStateChangeReturn
243 gst_time_overlay_change_state (GstElement * element, GstStateChange transition)
245 GstTimeOverlay *self = GST_TIME_OVERLAY (element);
247 switch (transition) {
248 case GST_STATE_CHANGE_READY_TO_PAUSED:
249 self->first_running_time = GST_CLOCK_TIME_NONE;
255 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
259 gst_time_overlay_finalize (GObject * gobject)
261 GstTimeOverlay *self = GST_TIME_OVERLAY (gobject);
263 gst_clear_caps (&self->reference_timestamp_caps);
264 g_date_time_unref (self->datetime_epoch);
265 g_free (self->datetime_format);
267 G_OBJECT_CLASS (parent_class)->finalize (gobject);
271 gst_time_overlay_class_init (GstTimeOverlayClass * klass)
273 GstElementClass *gstelement_class;
274 GstBaseTextOverlayClass *gsttextoverlay_class;
275 GObjectClass *gobject_class;
277 gsttextoverlay_class = (GstBaseTextOverlayClass *) klass;
278 gstelement_class = (GstElementClass *) klass;
279 gobject_class = (GObjectClass *) klass;
281 gst_element_class_set_static_metadata (gstelement_class, "Time overlay",
282 "Filter/Editor/Video",
283 "Overlays buffer time stamps on a video stream",
284 "Tim-Philipp Müller <tim@centricular.net>");
286 gsttextoverlay_class->get_text = gst_time_overlay_get_text;
288 gstelement_class->change_state = gst_time_overlay_change_state;
290 gobject_class->finalize = gst_time_overlay_finalize;
291 gobject_class->set_property = gst_time_overlay_set_property;
292 gobject_class->get_property = gst_time_overlay_get_property;
294 g_object_class_install_property (gobject_class, PROP_TIME_LINE,
295 g_param_spec_enum ("time-mode", "Time Mode", "What time to show",
296 GST_TYPE_TIME_OVERLAY_TIME_LINE, DEFAULT_TIME_LINE,
297 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
299 g_object_class_install_property (gobject_class, PROP_DATETIME_EPOCH,
300 g_param_spec_boxed ("datetime-epoch", "Datetime Epoch",
301 "When showing times as dates, the initial date from which time "
302 "is counted, if not specified prime epoch is used (1900-01-01)",
303 G_TYPE_DATE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
305 g_object_class_install_property (gobject_class, PROP_DATETIME_FORMAT,
306 g_param_spec_string ("datetime-format", "Datetime Format",
307 "When showing times as dates, the format to render date and time in",
308 DEFAULT_DATETIME_FORMAT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
310 g_object_class_install_property (gobject_class, PROP_SHOW_TIMES_AS_DATES,
311 g_param_spec_boolean ("show-times-as-dates", "Show times as dates",
312 "Whether to display times, counted from datetime-epoch, as dates",
313 DEFAULT_SHOW_TIMES_AS_DATES,
314 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
317 * timeoverlay:reference-timestamp-caps
319 * Selects the caps to use for the reference timestamp meta in
320 * time-mode=reference-timestamp.
324 g_object_class_install_property (gobject_class, PROP_REFERENCE_TIMESTAMP_CAPS,
325 g_param_spec_boxed ("reference-timestamp-caps",
326 "Reference Timestamp Caps",
327 "Caps to use for the reference timestamp time mode",
328 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
330 gst_type_mark_as_plugin_api (GST_TYPE_TIME_OVERLAY_TIME_LINE, 0);
334 gst_time_overlay_video_event (GstPad * pad, GstObject * parent,
337 GstTimeOverlay *overlay = GST_TIME_OVERLAY (parent);
339 switch (GST_EVENT_TYPE (event)) {
340 case GST_EVENT_FLUSH_STOP:
341 overlay->first_running_time = GST_CLOCK_TIME_NONE;
347 return overlay->orig_video_event (pad, parent, event);
351 gst_time_overlay_init (GstTimeOverlay * overlay)
353 GstBaseTextOverlay *textoverlay;
354 PangoContext *context;
355 PangoFontDescription *font_description;
358 textoverlay = GST_BASE_TEXT_OVERLAY (overlay);
360 textoverlay->valign = GST_BASE_TEXT_OVERLAY_VALIGN_TOP;
361 textoverlay->halign = GST_BASE_TEXT_OVERLAY_HALIGN_LEFT;
363 overlay->time_line = DEFAULT_TIME_LINE;
364 overlay->show_times_as_dates = DEFAULT_SHOW_TIMES_AS_DATES;
365 overlay->datetime_epoch = g_date_time_new_utc (1900, 1, 1, 0, 0, 0);
366 overlay->datetime_format = g_strdup (DEFAULT_DATETIME_FORMAT);
368 overlay->reference_timestamp_caps =
369 gst_static_caps_get (&ntp_reference_timestamp_caps);
371 context = textoverlay->pango_context;
373 pango_context_set_language (context, pango_language_from_string ("en_US"));
374 pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);
376 font_description = pango_font_description_new ();
377 pango_font_description_set_family_static (font_description, "Monospace");
378 pango_font_description_set_style (font_description, PANGO_STYLE_NORMAL);
379 pango_font_description_set_variant (font_description, PANGO_VARIANT_NORMAL);
380 pango_font_description_set_weight (font_description, PANGO_WEIGHT_NORMAL);
381 pango_font_description_set_stretch (font_description, PANGO_STRETCH_NORMAL);
382 pango_font_description_set_size (font_description, 18 * PANGO_SCALE);
383 pango_context_set_font_description (context, font_description);
384 pango_font_description_free (font_description);
386 video_sink = gst_element_get_static_pad (GST_ELEMENT (overlay), "video_sink");
387 overlay->orig_video_event = GST_PAD_EVENTFUNC (video_sink);
388 gst_pad_set_event_function (video_sink, gst_time_overlay_video_event);
389 gst_object_unref (video_sink);
393 gst_time_overlay_set_property (GObject * object, guint prop_id,
394 const GValue * value, GParamSpec * pspec)
396 GstTimeOverlay *overlay = GST_TIME_OVERLAY (object);
400 g_atomic_int_set (&overlay->time_line, g_value_get_enum (value));
402 case PROP_SHOW_TIMES_AS_DATES:
403 overlay->show_times_as_dates = g_value_get_boolean (value);
405 case PROP_DATETIME_EPOCH:
406 g_date_time_unref (overlay->datetime_epoch);
407 overlay->datetime_epoch = (GDateTime *) g_value_dup_boxed (value);
409 case PROP_DATETIME_FORMAT:
410 g_free (overlay->datetime_format);
411 overlay->datetime_format = g_value_dup_string (value);
413 case PROP_REFERENCE_TIMESTAMP_CAPS:
414 gst_clear_caps (&overlay->reference_timestamp_caps);
415 overlay->reference_timestamp_caps = g_value_dup_boxed (value);
418 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
424 gst_time_overlay_get_property (GObject * object, guint prop_id,
425 GValue * value, GParamSpec * pspec)
427 GstTimeOverlay *overlay = GST_TIME_OVERLAY (object);
431 g_value_set_enum (value, g_atomic_int_get (&overlay->time_line));
433 case PROP_SHOW_TIMES_AS_DATES:
434 g_value_set_boolean (value, overlay->show_times_as_dates);
436 case PROP_DATETIME_EPOCH:
437 g_value_set_boxed (value, overlay->datetime_epoch);
439 case PROP_DATETIME_FORMAT:
440 g_value_set_string (value, overlay->datetime_format);
442 case PROP_REFERENCE_TIMESTAMP_CAPS:
443 g_value_set_boxed (value, overlay->reference_timestamp_caps);
446 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);