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"
55 #define DEFAULT_TIME_LINE GST_TIME_OVERLAY_TIME_LINE_BUFFER_TIME
56 #define DEFAULT_SHOW_TIMES_AS_DATES FALSE
57 #define DEFAULT_DATETIME_FORMAT "%F %T" /* YYYY-MM-DD hh:mm:ss */
63 PROP_SHOW_TIMES_AS_DATES,
68 #define gst_time_overlay_parent_class parent_class
69 G_DEFINE_TYPE (GstTimeOverlay, gst_time_overlay, GST_TYPE_BASE_TEXT_OVERLAY);
71 static void gst_time_overlay_set_property (GObject * object, guint prop_id,
72 const GValue * value, GParamSpec * pspec);
73 static void gst_time_overlay_get_property (GObject * object, guint prop_id,
74 GValue * value, GParamSpec * pspec);
76 #define GST_TYPE_TIME_OVERLAY_TIME_LINE (gst_time_overlay_time_line_type())
78 gst_time_overlay_time_line_type (void)
80 static GType time_line_type = 0;
81 static const GEnumValue modes[] = {
82 {GST_TIME_OVERLAY_TIME_LINE_BUFFER_TIME, "buffer-time", "buffer-time"},
83 {GST_TIME_OVERLAY_TIME_LINE_STREAM_TIME, "stream-time", "stream-time"},
84 {GST_TIME_OVERLAY_TIME_LINE_RUNNING_TIME, "running-time", "running-time"},
85 {GST_TIME_OVERLAY_TIME_LINE_TIME_CODE, "time-code", "time-code"},
89 if (!time_line_type) {
90 time_line_type = g_enum_register_static ("GstTimeOverlayTimeLine", modes);
92 return time_line_type;
96 gst_time_overlay_render_time (GstTimeOverlay * overlay, GstClockTime time)
98 guint hours, mins, secs, msecs;
100 if (!GST_CLOCK_TIME_IS_VALID (time))
101 return g_strdup ("");
103 hours = (guint) (time / (GST_SECOND * 60 * 60));
104 mins = (guint) ((time / (GST_SECOND * 60)) % 60);
105 secs = (guint) ((time / GST_SECOND) % 60);
106 msecs = (guint) ((time % GST_SECOND) / (1000 * 1000));
108 return g_strdup_printf ("%u:%02u:%02u.%03u", hours, mins, secs, msecs);
111 /* Called with lock held */
113 gst_time_overlay_get_text (GstBaseTextOverlay * overlay,
114 GstBuffer * video_frame)
116 GstTimeOverlay *self = GST_TIME_OVERLAY (overlay);
117 GstTimeOverlayTimeLine time_line;
118 gchar *time_str, *txt, *ret;
120 overlay->need_render = TRUE;
122 time_line = g_atomic_int_get (&GST_TIME_OVERLAY_CAST (overlay)->time_line);
123 if (time_line == GST_TIME_OVERLAY_TIME_LINE_TIME_CODE) {
124 GstVideoTimeCodeMeta *tc_meta =
125 gst_buffer_get_video_time_code_meta (video_frame);
127 GST_DEBUG ("buffer without valid timecode");
128 return g_strdup ("00:00:00:00");
130 time_str = gst_video_time_code_to_string (&tc_meta->tc);
131 GST_DEBUG ("buffer with timecode %s", time_str);
133 GstClockTime ts, ts_buffer;
134 GstSegment *segment = &overlay->segment;
136 ts_buffer = GST_BUFFER_TIMESTAMP (video_frame);
138 if (!GST_CLOCK_TIME_IS_VALID (ts_buffer)) {
139 GST_DEBUG ("buffer without valid timestamp");
140 return g_strdup ("");
143 GST_DEBUG ("buffer with timestamp %" GST_TIME_FORMAT,
144 GST_TIME_ARGS (ts_buffer));
147 case GST_TIME_OVERLAY_TIME_LINE_STREAM_TIME:
148 ts = gst_segment_to_stream_time (segment, GST_FORMAT_TIME, ts_buffer);
150 case GST_TIME_OVERLAY_TIME_LINE_RUNNING_TIME:
151 ts = gst_segment_to_running_time (segment, GST_FORMAT_TIME, ts_buffer);
153 case GST_TIME_OVERLAY_TIME_LINE_BUFFER_TIME:
159 if (self->show_times_as_dates) {
163 g_date_time_add_seconds (self->datetime_epoch,
164 (gdouble) GST_BUFFER_TIMESTAMP (video_frame) / GST_SECOND);
165 time_str = g_date_time_format (datetime, self->datetime_format);
166 g_date_time_unref (datetime);
168 time_str = gst_time_overlay_render_time (GST_TIME_OVERLAY (overlay), ts);
172 txt = g_strdup (overlay->default_text);
174 if (txt != NULL && *txt != '\0') {
175 ret = g_strdup_printf ("%s %s", txt, time_str);
188 gst_time_overlay_finalize (GObject * gobject)
190 GstTimeOverlay *self = GST_TIME_OVERLAY (gobject);
192 g_date_time_unref (self->datetime_epoch);
193 g_free (self->datetime_format);
194 G_OBJECT_CLASS (parent_class)->finalize (gobject);
198 gst_time_overlay_class_init (GstTimeOverlayClass * klass)
200 GstElementClass *gstelement_class;
201 GstBaseTextOverlayClass *gsttextoverlay_class;
202 GObjectClass *gobject_class;
204 gsttextoverlay_class = (GstBaseTextOverlayClass *) klass;
205 gstelement_class = (GstElementClass *) klass;
206 gobject_class = (GObjectClass *) klass;
208 gst_element_class_set_static_metadata (gstelement_class, "Time overlay",
209 "Filter/Editor/Video",
210 "Overlays buffer time stamps on a video stream",
211 "Tim-Philipp Müller <tim@centricular.net>");
213 gsttextoverlay_class->get_text = gst_time_overlay_get_text;
215 gobject_class->finalize = gst_time_overlay_finalize;
216 gobject_class->set_property = gst_time_overlay_set_property;
217 gobject_class->get_property = gst_time_overlay_get_property;
219 g_object_class_install_property (gobject_class, PROP_TIME_LINE,
220 g_param_spec_enum ("time-mode", "Time Mode", "What time to show",
221 GST_TYPE_TIME_OVERLAY_TIME_LINE, DEFAULT_TIME_LINE,
222 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224 g_object_class_install_property (gobject_class, PROP_DATETIME_EPOCH,
225 g_param_spec_boxed ("datetime-epoch", "Datetime Epoch",
226 "When showing times as dates, the initial date from which time "
227 "is counted, if not specified prime epoch is used (1900-01-01)",
228 G_TYPE_DATE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230 g_object_class_install_property (gobject_class, PROP_DATETIME_FORMAT,
231 g_param_spec_string ("datetime-format", "Datetime Format",
232 "When showing times as dates, the format to render date and time in",
233 DEFAULT_DATETIME_FORMAT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
235 g_object_class_install_property (gobject_class, PROP_SHOW_TIMES_AS_DATES,
236 g_param_spec_boolean ("show-times-as-dates", "Show times as dates",
237 "Whether to display times, counted from datetime-epoch, as dates",
238 DEFAULT_SHOW_TIMES_AS_DATES,
239 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
241 gst_type_mark_as_plugin_api (GST_TYPE_TIME_OVERLAY_TIME_LINE, 0);
245 gst_time_overlay_init (GstTimeOverlay * overlay)
247 GstBaseTextOverlay *textoverlay;
248 PangoContext *context;
249 PangoFontDescription *font_description;
251 textoverlay = GST_BASE_TEXT_OVERLAY (overlay);
253 textoverlay->valign = GST_BASE_TEXT_OVERLAY_VALIGN_TOP;
254 textoverlay->halign = GST_BASE_TEXT_OVERLAY_HALIGN_LEFT;
256 overlay->time_line = DEFAULT_TIME_LINE;
257 overlay->show_times_as_dates = DEFAULT_SHOW_TIMES_AS_DATES;
258 overlay->datetime_epoch = g_date_time_new_utc (1900, 1, 1, 0, 0, 0);
259 overlay->datetime_format = g_strdup (DEFAULT_DATETIME_FORMAT);
261 context = textoverlay->pango_context;
263 pango_context_set_language (context, pango_language_from_string ("en_US"));
264 pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);
266 font_description = pango_font_description_new ();
267 pango_font_description_set_family_static (font_description, "Monospace");
268 pango_font_description_set_style (font_description, PANGO_STYLE_NORMAL);
269 pango_font_description_set_variant (font_description, PANGO_VARIANT_NORMAL);
270 pango_font_description_set_weight (font_description, PANGO_WEIGHT_NORMAL);
271 pango_font_description_set_stretch (font_description, PANGO_STRETCH_NORMAL);
272 pango_font_description_set_size (font_description, 18 * PANGO_SCALE);
273 pango_context_set_font_description (context, font_description);
274 pango_font_description_free (font_description);
278 gst_time_overlay_set_property (GObject * object, guint prop_id,
279 const GValue * value, GParamSpec * pspec)
281 GstTimeOverlay *overlay = GST_TIME_OVERLAY (object);
285 g_atomic_int_set (&overlay->time_line, g_value_get_enum (value));
287 case PROP_SHOW_TIMES_AS_DATES:
288 overlay->show_times_as_dates = g_value_get_boolean (value);
290 case PROP_DATETIME_EPOCH:
291 g_date_time_unref (overlay->datetime_epoch);
292 overlay->datetime_epoch = (GDateTime *) g_value_dup_boxed (value);
294 case PROP_DATETIME_FORMAT:
295 g_free (overlay->datetime_format);
296 overlay->datetime_format = g_value_dup_string (value);
299 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
305 gst_time_overlay_get_property (GObject * object, guint prop_id,
306 GValue * value, GParamSpec * pspec)
308 GstTimeOverlay *overlay = GST_TIME_OVERLAY (object);
312 g_value_set_enum (value, g_atomic_int_get (&overlay->time_line));
314 case PROP_SHOW_TIMES_AS_DATES:
315 g_value_set_boolean (value, overlay->show_times_as_dates);
317 case PROP_DATETIME_EPOCH:
318 g_value_set_boxed (value, overlay->datetime_epoch);
320 case PROP_DATETIME_FORMAT:
321 g_value_set_string (value, overlay->datetime_format);
324 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);