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 the properties of the #GstBaseTextOverlay class. By default, the
29 * time stamp is displayed in the top left corner of the picture, with some
30 * padding to the left and to the top.
31 *
32 * |[
33 * gst-launch-1.0 -v videotestsrc ! timeoverlay ! autovideosink
34 * ]|
35 * Display the time stamps in the top left corner of the video picture.
36 * |[
37 * gst-launch-1.0 -v videotestsrc ! timeoverlay halignment=right valignment=bottom text="Stream time:" shaded-background=true font-desc="Sans, 24" ! autovideosink
38 * ]|
39 * Another pipeline that displays the time stamps with some leading
40 * text in the bottom right corner of the video picture, with the background
41 * of the text being shaded in order to make it more legible on top of a
42 * bright video background.
43 *
44 */
45
46 #ifdef HAVE_CONFIG_H
47 #include "config.h"
48 #endif
49
50 #include <gst/video/video.h>
51
52 #include "gsttimeoverlay.h"
53
54 #define DEFAULT_TIME_LINE GST_TIME_OVERLAY_TIME_LINE_BUFFER_TIME
55
56 enum
57 {
58 PROP_0,
59 PROP_TIME_LINE
60 };
61
62 #define gst_time_overlay_parent_class parent_class
63 G_DEFINE_TYPE (GstTimeOverlay, gst_time_overlay, GST_TYPE_BASE_TEXT_OVERLAY);
64
65 static void gst_time_overlay_set_property (GObject * object, guint prop_id,
66 const GValue * value, GParamSpec * pspec);
67 static void gst_time_overlay_get_property (GObject * object, guint prop_id,
68 GValue * value, GParamSpec * pspec);
69
70 #define GST_TYPE_TIME_OVERLAY_TIME_LINE (gst_time_overlay_time_line_type())
71 static GType
gst_time_overlay_time_line_type(void)72 gst_time_overlay_time_line_type (void)
73 {
74 static GType time_line_type = 0;
75 static const GEnumValue modes[] = {
76 {GST_TIME_OVERLAY_TIME_LINE_BUFFER_TIME, "buffer-time", "buffer-time"},
77 {GST_TIME_OVERLAY_TIME_LINE_STREAM_TIME, "stream-time", "stream-time"},
78 {GST_TIME_OVERLAY_TIME_LINE_RUNNING_TIME, "running-time", "running-time"},
79 {GST_TIME_OVERLAY_TIME_LINE_TIME_CODE, "time-code", "time-code"},
80 {0, NULL, NULL},
81 };
82
83 if (!time_line_type) {
84 time_line_type = g_enum_register_static ("GstTimeOverlayTimeLine", modes);
85 }
86 return time_line_type;
87 }
88
89 static gchar *
gst_time_overlay_render_time(GstTimeOverlay * overlay,GstClockTime time)90 gst_time_overlay_render_time (GstTimeOverlay * overlay, GstClockTime time)
91 {
92 guint hours, mins, secs, msecs;
93
94 if (!GST_CLOCK_TIME_IS_VALID (time))
95 return g_strdup ("");
96
97 hours = (guint) (time / (GST_SECOND * 60 * 60));
98 mins = (guint) ((time / (GST_SECOND * 60)) % 60);
99 secs = (guint) ((time / GST_SECOND) % 60);
100 msecs = (guint) ((time % GST_SECOND) / (1000 * 1000));
101
102 return g_strdup_printf ("%u:%02u:%02u.%03u", hours, mins, secs, msecs);
103 }
104
105 /* Called with lock held */
106 static gchar *
gst_time_overlay_get_text(GstBaseTextOverlay * overlay,GstBuffer * video_frame)107 gst_time_overlay_get_text (GstBaseTextOverlay * overlay,
108 GstBuffer * video_frame)
109 {
110 GstTimeOverlayTimeLine time_line;
111 gchar *time_str, *txt, *ret;
112
113 overlay->need_render = TRUE;
114
115 time_line = g_atomic_int_get (&GST_TIME_OVERLAY_CAST (overlay)->time_line);
116 if (time_line == GST_TIME_OVERLAY_TIME_LINE_TIME_CODE) {
117 GstVideoTimeCodeMeta *tc_meta =
118 gst_buffer_get_video_time_code_meta (video_frame);
119 if (!tc_meta) {
120 GST_DEBUG ("buffer without valid timecode");
121 return g_strdup ("00:00:00:00");
122 }
123 time_str = gst_video_time_code_to_string (&tc_meta->tc);
124 GST_DEBUG ("buffer with timecode %s", time_str);
125 } else {
126 GstClockTime ts, ts_buffer;
127 GstSegment *segment = &overlay->segment;
128
129 ts_buffer = GST_BUFFER_TIMESTAMP (video_frame);
130
131 if (!GST_CLOCK_TIME_IS_VALID (ts_buffer)) {
132 GST_DEBUG ("buffer without valid timestamp");
133 return g_strdup ("");
134 }
135
136 GST_DEBUG ("buffer with timestamp %" GST_TIME_FORMAT,
137 GST_TIME_ARGS (ts_buffer));
138
139 switch (time_line) {
140 case GST_TIME_OVERLAY_TIME_LINE_STREAM_TIME:
141 ts = gst_segment_to_stream_time (segment, GST_FORMAT_TIME, ts_buffer);
142 break;
143 case GST_TIME_OVERLAY_TIME_LINE_RUNNING_TIME:
144 ts = gst_segment_to_running_time (segment, GST_FORMAT_TIME, ts_buffer);
145 break;
146 case GST_TIME_OVERLAY_TIME_LINE_BUFFER_TIME:
147 default:
148 ts = ts_buffer;
149 break;
150 }
151
152 time_str = gst_time_overlay_render_time (GST_TIME_OVERLAY (overlay), ts);
153 }
154 txt = g_strdup (overlay->default_text);
155
156 if (txt != NULL && *txt != '\0') {
157 ret = g_strdup_printf ("%s %s", txt, time_str);
158 } else {
159 ret = time_str;
160 time_str = NULL;
161 }
162
163 g_free (txt);
164 g_free (time_str);
165
166 return ret;
167 }
168
169 static void
gst_time_overlay_class_init(GstTimeOverlayClass * klass)170 gst_time_overlay_class_init (GstTimeOverlayClass * klass)
171 {
172 GstElementClass *gstelement_class;
173 GstBaseTextOverlayClass *gsttextoverlay_class;
174 GObjectClass *gobject_class;
175
176 gsttextoverlay_class = (GstBaseTextOverlayClass *) klass;
177 gstelement_class = (GstElementClass *) klass;
178 gobject_class = (GObjectClass *) klass;
179
180 gst_element_class_set_static_metadata (gstelement_class, "Time overlay",
181 "Filter/Editor/Video",
182 "Overlays buffer time stamps on a video stream",
183 "Tim-Philipp Müller <tim@centricular.net>");
184
185 gsttextoverlay_class->get_text = gst_time_overlay_get_text;
186
187 gobject_class->set_property = gst_time_overlay_set_property;
188 gobject_class->get_property = gst_time_overlay_get_property;
189
190 g_object_class_install_property (gobject_class, PROP_TIME_LINE,
191 g_param_spec_enum ("time-mode", "Time Mode", "What time to show",
192 GST_TYPE_TIME_OVERLAY_TIME_LINE, DEFAULT_TIME_LINE,
193 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
194 }
195
196 static void
gst_time_overlay_init(GstTimeOverlay * overlay)197 gst_time_overlay_init (GstTimeOverlay * overlay)
198 {
199 GstBaseTextOverlay *textoverlay;
200 PangoContext *context;
201 PangoFontDescription *font_description;
202
203 textoverlay = GST_BASE_TEXT_OVERLAY (overlay);
204
205 textoverlay->valign = GST_BASE_TEXT_OVERLAY_VALIGN_TOP;
206 textoverlay->halign = GST_BASE_TEXT_OVERLAY_HALIGN_LEFT;
207
208 overlay->time_line = DEFAULT_TIME_LINE;
209
210 context = textoverlay->pango_context;
211
212 pango_context_set_language (context, pango_language_from_string ("en_US"));
213 pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);
214
215 font_description = pango_font_description_new ();
216 pango_font_description_set_family_static (font_description, "Monospace");
217 pango_font_description_set_style (font_description, PANGO_STYLE_NORMAL);
218 pango_font_description_set_variant (font_description, PANGO_VARIANT_NORMAL);
219 pango_font_description_set_weight (font_description, PANGO_WEIGHT_NORMAL);
220 pango_font_description_set_stretch (font_description, PANGO_STRETCH_NORMAL);
221 pango_font_description_set_size (font_description, 18 * PANGO_SCALE);
222 pango_context_set_font_description (context, font_description);
223 pango_font_description_free (font_description);
224 }
225
226 static void
gst_time_overlay_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)227 gst_time_overlay_set_property (GObject * object, guint prop_id,
228 const GValue * value, GParamSpec * pspec)
229 {
230 GstTimeOverlay *overlay = GST_TIME_OVERLAY (object);
231
232 switch (prop_id) {
233 case PROP_TIME_LINE:
234 g_atomic_int_set (&overlay->time_line, g_value_get_enum (value));
235 break;
236 default:
237 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238 break;
239 }
240 }
241
242 static void
gst_time_overlay_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)243 gst_time_overlay_get_property (GObject * object, guint prop_id,
244 GValue * value, GParamSpec * pspec)
245 {
246 GstTimeOverlay *overlay = GST_TIME_OVERLAY (object);
247
248 switch (prop_id) {
249 case PROP_TIME_LINE:
250 g_value_set_enum (value, g_atomic_int_get (&overlay->time_line));
251 break;
252 default:
253 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
254 break;
255 }
256 }
257