plugins: uddate gst_type_mark_as_plugin_api() calls
[platform/upstream/gstreamer.git] / ext / pango / gsttimeoverlay.c
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 its properties.
29  *
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.
32  *
33  * |[
34  * gst-launch-1.0 -v videotestsrc ! timeoverlay ! autovideosink
35  * ]|
36  * Display the time stamps in the top left corner of the video picture.
37  * |[
38  * gst-launch-1.0 -v videotestsrc ! timeoverlay halignment=right valignment=bottom text="Stream time:" shaded-background=true font-desc="Sans, 24" ! autovideosink
39  * ]|
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.
44  *
45  */
46
47 #ifdef HAVE_CONFIG_H
48 #include "config.h"
49 #endif
50
51 #include <gst/video/video.h>
52
53 #include "gsttimeoverlay.h"
54
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 */
58
59 enum
60 {
61   PROP_0,
62   PROP_TIME_LINE,
63   PROP_SHOW_TIMES_AS_DATES,
64   PROP_DATETIME_EPOCH,
65   PROP_DATETIME_FORMAT,
66 };
67
68 #define gst_time_overlay_parent_class parent_class
69 G_DEFINE_TYPE (GstTimeOverlay, gst_time_overlay, GST_TYPE_BASE_TEXT_OVERLAY);
70
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);
75
76 #define GST_TYPE_TIME_OVERLAY_TIME_LINE (gst_time_overlay_time_line_type())
77 static GType
78 gst_time_overlay_time_line_type (void)
79 {
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"},
86     {0, NULL, NULL},
87   };
88
89   if (!time_line_type) {
90     time_line_type = g_enum_register_static ("GstTimeOverlayTimeLine", modes);
91   }
92   return time_line_type;
93 }
94
95 static gchar *
96 gst_time_overlay_render_time (GstTimeOverlay * overlay, GstClockTime time)
97 {
98   guint hours, mins, secs, msecs;
99
100   if (!GST_CLOCK_TIME_IS_VALID (time))
101     return g_strdup ("");
102
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));
107
108   return g_strdup_printf ("%u:%02u:%02u.%03u", hours, mins, secs, msecs);
109 }
110
111 /* Called with lock held */
112 static gchar *
113 gst_time_overlay_get_text (GstBaseTextOverlay * overlay,
114     GstBuffer * video_frame)
115 {
116   GstTimeOverlay *self = GST_TIME_OVERLAY (overlay);
117   GstTimeOverlayTimeLine time_line;
118   gchar *time_str, *txt, *ret;
119
120   overlay->need_render = TRUE;
121
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);
126     if (!tc_meta) {
127       GST_DEBUG ("buffer without valid timecode");
128       return g_strdup ("00:00:00:00");
129     }
130     time_str = gst_video_time_code_to_string (&tc_meta->tc);
131     GST_DEBUG ("buffer with timecode %s", time_str);
132   } else {
133     GstClockTime ts, ts_buffer;
134     GstSegment *segment = &overlay->segment;
135
136     ts_buffer = GST_BUFFER_TIMESTAMP (video_frame);
137
138     if (!GST_CLOCK_TIME_IS_VALID (ts_buffer)) {
139       GST_DEBUG ("buffer without valid timestamp");
140       return g_strdup ("");
141     }
142
143     GST_DEBUG ("buffer with timestamp %" GST_TIME_FORMAT,
144         GST_TIME_ARGS (ts_buffer));
145
146     switch (time_line) {
147       case GST_TIME_OVERLAY_TIME_LINE_STREAM_TIME:
148         ts = gst_segment_to_stream_time (segment, GST_FORMAT_TIME, ts_buffer);
149         break;
150       case GST_TIME_OVERLAY_TIME_LINE_RUNNING_TIME:
151         ts = gst_segment_to_running_time (segment, GST_FORMAT_TIME, ts_buffer);
152         break;
153       case GST_TIME_OVERLAY_TIME_LINE_BUFFER_TIME:
154       default:
155         ts = ts_buffer;
156         break;
157     }
158
159     if (self->show_times_as_dates) {
160       GDateTime *datetime;
161
162       datetime =
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);
167     } else {
168       time_str = gst_time_overlay_render_time (GST_TIME_OVERLAY (overlay), ts);
169     }
170   }
171
172   txt = g_strdup (overlay->default_text);
173
174   if (txt != NULL && *txt != '\0') {
175     ret = g_strdup_printf ("%s %s", txt, time_str);
176   } else {
177     ret = time_str;
178     time_str = NULL;
179   }
180
181   g_free (txt);
182   g_free (time_str);
183
184   return ret;
185 }
186
187 static void
188 gst_time_overlay_finalize (GObject * gobject)
189 {
190   GstTimeOverlay *self = GST_TIME_OVERLAY (gobject);
191
192   g_date_time_unref (self->datetime_epoch);
193   g_free (self->datetime_format);
194   G_OBJECT_CLASS (parent_class)->finalize (gobject);
195 }
196
197 static void
198 gst_time_overlay_class_init (GstTimeOverlayClass * klass)
199 {
200   GstElementClass *gstelement_class;
201   GstBaseTextOverlayClass *gsttextoverlay_class;
202   GObjectClass *gobject_class;
203
204   gsttextoverlay_class = (GstBaseTextOverlayClass *) klass;
205   gstelement_class = (GstElementClass *) klass;
206   gobject_class = (GObjectClass *) klass;
207
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>");
212
213   gsttextoverlay_class->get_text = gst_time_overlay_get_text;
214
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;
218
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));
223
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));
229
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));
234
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));
240
241   gst_type_mark_as_plugin_api (GST_TYPE_TIME_OVERLAY_TIME_LINE, 0);
242 }
243
244 static void
245 gst_time_overlay_init (GstTimeOverlay * overlay)
246 {
247   GstBaseTextOverlay *textoverlay;
248   PangoContext *context;
249   PangoFontDescription *font_description;
250
251   textoverlay = GST_BASE_TEXT_OVERLAY (overlay);
252
253   textoverlay->valign = GST_BASE_TEXT_OVERLAY_VALIGN_TOP;
254   textoverlay->halign = GST_BASE_TEXT_OVERLAY_HALIGN_LEFT;
255
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);
260
261   context = textoverlay->pango_context;
262
263   pango_context_set_language (context, pango_language_from_string ("en_US"));
264   pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);
265
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);
275 }
276
277 static void
278 gst_time_overlay_set_property (GObject * object, guint prop_id,
279     const GValue * value, GParamSpec * pspec)
280 {
281   GstTimeOverlay *overlay = GST_TIME_OVERLAY (object);
282
283   switch (prop_id) {
284     case PROP_TIME_LINE:
285       g_atomic_int_set (&overlay->time_line, g_value_get_enum (value));
286       break;
287     case PROP_SHOW_TIMES_AS_DATES:
288       overlay->show_times_as_dates = g_value_get_boolean (value);
289       break;
290     case PROP_DATETIME_EPOCH:
291       g_date_time_unref (overlay->datetime_epoch);
292       overlay->datetime_epoch = (GDateTime *) g_value_dup_boxed (value);
293       break;
294     case PROP_DATETIME_FORMAT:
295       g_free (overlay->datetime_format);
296       overlay->datetime_format = g_value_dup_string (value);
297       break;
298     default:
299       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
300       break;
301   }
302 }
303
304 static void
305 gst_time_overlay_get_property (GObject * object, guint prop_id,
306     GValue * value, GParamSpec * pspec)
307 {
308   GstTimeOverlay *overlay = GST_TIME_OVERLAY (object);
309
310   switch (prop_id) {
311     case PROP_TIME_LINE:
312       g_value_set_enum (value, g_atomic_int_get (&overlay->time_line));
313       break;
314     case PROP_SHOW_TIMES_AS_DATES:
315       g_value_set_boolean (value, overlay->show_times_as_dates);
316       break;
317     case PROP_DATETIME_EPOCH:
318       g_value_set_boxed (value, overlay->datetime_epoch);
319       break;
320     case PROP_DATETIME_FORMAT:
321       g_value_set_string (value, overlay->datetime_format);
322       break;
323     default:
324       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
325       break;
326   }
327 }