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: #GstTextOverlay, #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 #GstTextOverlay 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 GST_BOILERPLATE (GstTimeOverlay, gst_time_overlay, GstTextOverlay,
54 GST_TYPE_TEXT_OVERLAY);
57 gst_time_overlay_base_init (gpointer g_class)
59 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
61 gst_element_class_set_details_simple (element_class, "Time overlay",
62 "Filter/Editor/Video",
63 "Overlays buffer time stamps on a video stream",
64 "Tim-Philipp Müller <tim@centricular.net>");
68 gst_time_overlay_render_time (GstTimeOverlay * overlay, GstClockTime time)
70 guint hours, mins, secs, msecs;
72 if (!GST_CLOCK_TIME_IS_VALID (time))
75 hours = (guint) (time / (GST_SECOND * 60 * 60));
76 mins = (guint) ((time / (GST_SECOND * 60)) % 60);
77 secs = (guint) ((time / GST_SECOND) % 60);
78 msecs = (guint) ((time % GST_SECOND) / (1000 * 1000));
80 return g_strdup_printf ("%u:%02u:%02u.%03u", hours, mins, secs, msecs);
83 /* Called with lock held */
85 gst_time_overlay_get_text (GstTextOverlay * overlay, GstBuffer * video_frame)
87 GstClockTime time = GST_BUFFER_TIMESTAMP (video_frame);
88 gchar *time_str, *txt, *ret;
90 overlay->need_render = TRUE;
92 if (!GST_CLOCK_TIME_IS_VALID (time)) {
93 GST_DEBUG ("buffer without valid timestamp");
97 GST_DEBUG ("buffer with timestamp %" GST_TIME_FORMAT, GST_TIME_ARGS (time));
99 txt = g_strdup (overlay->default_text);
101 time_str = gst_time_overlay_render_time (GST_TIME_OVERLAY (overlay), time);
102 if (txt != NULL && *txt != '\0') {
103 ret = g_strdup_printf ("%s %s", txt, time_str);
116 gst_time_overlay_class_init (GstTimeOverlayClass * klass)
118 GstTextOverlayClass *gsttextoverlay_class;
119 PangoContext *context;
120 PangoFontDescription *font_description;
122 gsttextoverlay_class = (GstTextOverlayClass *) klass;
124 gsttextoverlay_class->get_text = gst_time_overlay_get_text;
126 g_mutex_lock (GST_TEXT_OVERLAY_CLASS (klass)->pango_lock);
127 context = GST_TEXT_OVERLAY_CLASS (klass)->pango_context;
129 pango_context_set_language (context, pango_language_from_string ("en_US"));
130 pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);
132 font_description = pango_font_description_new ();
133 pango_font_description_set_family_static (font_description, "Monospace");
134 pango_font_description_set_style (font_description, PANGO_STYLE_NORMAL);
135 pango_font_description_set_variant (font_description, PANGO_VARIANT_NORMAL);
136 pango_font_description_set_weight (font_description, PANGO_WEIGHT_NORMAL);
137 pango_font_description_set_stretch (font_description, PANGO_STRETCH_NORMAL);
138 pango_font_description_set_size (font_description, 18 * PANGO_SCALE);
139 pango_context_set_font_description (context, font_description);
140 pango_font_description_free (font_description);
141 g_mutex_unlock (GST_TEXT_OVERLAY_CLASS (klass)->pango_lock);
145 gst_time_overlay_init (GstTimeOverlay * overlay, GstTimeOverlayClass * klass)
147 GstTextOverlay *textoverlay;
149 textoverlay = GST_TEXT_OVERLAY (overlay);
151 textoverlay->valign = GST_TEXT_OVERLAY_VALIGN_TOP;
152 textoverlay->halign = GST_TEXT_OVERLAY_HALIGN_LEFT;