2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2005> 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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:element-timeoverlay
23 * @see_also: #GstBaseTextOverlay, #GstClockOverlay
25 * This element overlays the buffer time stamps of a video stream on
26 * top of itself. You can position the text and configure the font details
27 * using the properties of the #GstBaseTextOverlay class. By default, the
28 * time stamp is displayed in the top left corner of the picture, with some
29 * padding to the left and to the top.
33 * gst-launch -v videotestsrc ! timeoverlay ! xvimagesink
34 * ]| Display the time stamps in the top left
35 * corner of the video picture.
37 * gst-launch -v videotestsrc ! timeoverlay halign=right valign=bottom text="Stream time:" shaded-background=true ! xvimagesink
38 * ]| Another pipeline that displays the time stamps with some leading
39 * text in the bottom right corner of the video picture, with the background
40 * of the text being shaded in order to make it more legible on top of a
41 * bright video background.
49 #include <gst/video/video.h>
51 #include "gsttimeoverlay.h"
53 #define gst_time_overlay_parent_class parent_class
54 G_DEFINE_TYPE (GstTimeOverlay, gst_time_overlay, GST_TYPE_BASE_TEXT_OVERLAY);
57 gst_time_overlay_render_time (GstTimeOverlay * overlay, GstClockTime time)
59 guint hours, mins, secs, msecs;
61 if (!GST_CLOCK_TIME_IS_VALID (time))
64 hours = (guint) (time / (GST_SECOND * 60 * 60));
65 mins = (guint) ((time / (GST_SECOND * 60)) % 60);
66 secs = (guint) ((time / GST_SECOND) % 60);
67 msecs = (guint) ((time % GST_SECOND) / (1000 * 1000));
69 return g_strdup_printf ("%u:%02u:%02u.%03u", hours, mins, secs, msecs);
72 /* Called with lock held */
74 gst_time_overlay_get_text (GstBaseTextOverlay * overlay,
75 GstBuffer * video_frame)
77 GstClockTime time = GST_BUFFER_TIMESTAMP (video_frame);
78 gchar *time_str, *txt, *ret;
80 overlay->need_render = TRUE;
82 if (!GST_CLOCK_TIME_IS_VALID (time)) {
83 GST_DEBUG ("buffer without valid timestamp");
87 GST_DEBUG ("buffer with timestamp %" GST_TIME_FORMAT, GST_TIME_ARGS (time));
89 txt = g_strdup (overlay->default_text);
91 time_str = gst_time_overlay_render_time (GST_TIME_OVERLAY (overlay), time);
92 if (txt != NULL && *txt != '\0') {
93 ret = g_strdup_printf ("%s %s", txt, time_str);
106 gst_time_overlay_class_init (GstTimeOverlayClass * klass)
108 GstElementClass *gstelement_class;
109 GstBaseTextOverlayClass *gsttextoverlay_class;
110 PangoContext *context;
111 PangoFontDescription *font_description;
113 gsttextoverlay_class = (GstBaseTextOverlayClass *) klass;
114 gstelement_class = (GstElementClass *) klass;
116 gst_element_class_set_details_simple (gstelement_class, "Time overlay",
117 "Filter/Editor/Video",
118 "Overlays buffer time stamps on a video stream",
119 "Tim-Philipp Müller <tim@centricular.net>");
121 gsttextoverlay_class->get_text = gst_time_overlay_get_text;
123 g_mutex_lock (gsttextoverlay_class->pango_lock);
124 context = gsttextoverlay_class->pango_context;
126 pango_context_set_language (context, pango_language_from_string ("en_US"));
127 pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);
129 font_description = pango_font_description_new ();
130 pango_font_description_set_family_static (font_description, "Monospace");
131 pango_font_description_set_style (font_description, PANGO_STYLE_NORMAL);
132 pango_font_description_set_variant (font_description, PANGO_VARIANT_NORMAL);
133 pango_font_description_set_weight (font_description, PANGO_WEIGHT_NORMAL);
134 pango_font_description_set_stretch (font_description, PANGO_STRETCH_NORMAL);
135 pango_font_description_set_size (font_description, 18 * PANGO_SCALE);
136 pango_context_set_font_description (context, font_description);
137 pango_font_description_free (font_description);
138 g_mutex_unlock (gsttextoverlay_class->pango_lock);
142 gst_time_overlay_init (GstTimeOverlay * overlay)
144 GstBaseTextOverlay *textoverlay;
146 textoverlay = GST_BASE_TEXT_OVERLAY (overlay);
148 textoverlay->valign = GST_BASE_TEXT_OVERLAY_VALIGN_TOP;
149 textoverlay->halign = GST_BASE_TEXT_OVERLAY_HALIGN_LEFT;