timeoverlay: fix pad leak
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-base / 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 #include "gstpangoelements.h"
55
56 #define DEFAULT_TIME_LINE GST_TIME_OVERLAY_TIME_LINE_BUFFER_TIME
57 #define DEFAULT_SHOW_TIMES_AS_DATES FALSE
58 #define DEFAULT_DATETIME_FORMAT "%F %T" /* YYYY-MM-DD hh:mm:ss */
59
60 static GstStaticCaps ntp_reference_timestamp_caps =
61 GST_STATIC_CAPS ("timestamp/x-ntp");
62
63 enum
64 {
65   PROP_0,
66   PROP_TIME_LINE,
67   PROP_SHOW_TIMES_AS_DATES,
68   PROP_DATETIME_EPOCH,
69   PROP_DATETIME_FORMAT,
70   PROP_REFERENCE_TIMESTAMP_CAPS,
71 };
72
73 #define gst_time_overlay_parent_class parent_class
74 G_DEFINE_TYPE (GstTimeOverlay, gst_time_overlay, GST_TYPE_BASE_TEXT_OVERLAY);
75 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (timeoverlay, "timeoverlay",
76     GST_RANK_NONE, GST_TYPE_TIME_OVERLAY, pango_element_init (plugin));
77
78 static void gst_time_overlay_set_property (GObject * object, guint prop_id,
79     const GValue * value, GParamSpec * pspec);
80 static void gst_time_overlay_get_property (GObject * object, guint prop_id,
81     GValue * value, GParamSpec * pspec);
82
83 /**
84  * GstTimeOverlayTimeLine::elapsed-running-time:
85  *
86  * Overlay elapsed running time since the first observed running time.
87  *
88  * Since: 1.20
89  */
90
91 /**
92  * GstTimeOverlayTimeLine::reference-timestamp:
93  *
94  * Use #GstReferenceTimestampMeta.
95  *
96  * Since: 1.22
97  */
98
99 #define GST_TYPE_TIME_OVERLAY_TIME_LINE (gst_time_overlay_time_line_type())
100 static GType
101 gst_time_overlay_time_line_type (void)
102 {
103   static GType time_line_type = 0;
104   static const GEnumValue modes[] = {
105     {GST_TIME_OVERLAY_TIME_LINE_BUFFER_TIME, "buffer-time", "buffer-time"},
106     {GST_TIME_OVERLAY_TIME_LINE_STREAM_TIME, "stream-time", "stream-time"},
107     {GST_TIME_OVERLAY_TIME_LINE_RUNNING_TIME, "running-time", "running-time"},
108     {GST_TIME_OVERLAY_TIME_LINE_TIME_CODE, "time-code", "time-code"},
109     {GST_TIME_OVERLAY_TIME_LINE_ELAPSED_RUNNING_TIME,
110         "elapsed-running-time", "elapsed-running-time"},
111     {GST_TIME_OVERLAY_TIME_LINE_REFERENCE_TIMESTAMP,
112         "reference-timestamp", "reference-timestamp"},
113     {0, NULL, NULL},
114   };
115
116   if (!time_line_type) {
117     time_line_type = g_enum_register_static ("GstTimeOverlayTimeLine", modes);
118   }
119   return time_line_type;
120 }
121
122 static gchar *
123 gst_time_overlay_render_time (GstTimeOverlay * overlay, GstClockTime time)
124 {
125   guint hours, mins, secs, msecs;
126
127   if (!GST_CLOCK_TIME_IS_VALID (time))
128     return g_strdup ("");
129
130   hours = (guint) (time / (GST_SECOND * 60 * 60));
131   mins = (guint) ((time / (GST_SECOND * 60)) % 60);
132   secs = (guint) ((time / GST_SECOND) % 60);
133   msecs = (guint) ((time % GST_SECOND) / (1000 * 1000));
134
135   return g_strdup_printf ("%u:%02u:%02u.%03u", hours, mins, secs, msecs);
136 }
137
138 /* Called with lock held */
139 static gchar *
140 gst_time_overlay_get_text (GstBaseTextOverlay * overlay,
141     GstBuffer * video_frame)
142 {
143   GstTimeOverlay *self = GST_TIME_OVERLAY (overlay);
144   GstTimeOverlayTimeLine time_line;
145   gchar *time_str, *txt, *ret;
146
147   overlay->need_render = TRUE;
148
149   time_line = g_atomic_int_get (&GST_TIME_OVERLAY_CAST (overlay)->time_line);
150   if (time_line == GST_TIME_OVERLAY_TIME_LINE_TIME_CODE) {
151     GstVideoTimeCodeMeta *tc_meta =
152         gst_buffer_get_video_time_code_meta (video_frame);
153     if (!tc_meta) {
154       GST_DEBUG ("buffer without valid timecode");
155       return g_strdup ("00:00:00:00");
156     }
157     time_str = gst_video_time_code_to_string (&tc_meta->tc);
158     GST_DEBUG ("buffer with timecode %s", time_str);
159   } else {
160     GstClockTime ts, ts_buffer;
161     GstSegment *segment = &overlay->segment;
162
163     ts_buffer = GST_BUFFER_TIMESTAMP (video_frame);
164
165     if (!GST_CLOCK_TIME_IS_VALID (ts_buffer)) {
166       GST_DEBUG ("buffer without valid timestamp");
167       return g_strdup ("");
168     }
169
170     GST_DEBUG ("buffer with timestamp %" GST_TIME_FORMAT,
171         GST_TIME_ARGS (ts_buffer));
172
173     switch (time_line) {
174       case GST_TIME_OVERLAY_TIME_LINE_STREAM_TIME:
175         ts = gst_segment_to_stream_time (segment, GST_FORMAT_TIME, ts_buffer);
176         break;
177       case GST_TIME_OVERLAY_TIME_LINE_RUNNING_TIME:
178         ts = gst_segment_to_running_time (segment, GST_FORMAT_TIME, ts_buffer);
179         break;
180       case GST_TIME_OVERLAY_TIME_LINE_ELAPSED_RUNNING_TIME:
181         ts = gst_segment_to_running_time (segment, GST_FORMAT_TIME, ts_buffer);
182         if (self->first_running_time == GST_CLOCK_TIME_NONE)
183           self->first_running_time = ts;
184         ts -= self->first_running_time;
185         break;
186       case GST_TIME_OVERLAY_TIME_LINE_REFERENCE_TIMESTAMP:
187       {
188         GstReferenceTimestampMeta *meta;
189
190         if (self->reference_timestamp_caps) {
191           meta =
192               gst_buffer_get_reference_timestamp_meta (video_frame,
193               self->reference_timestamp_caps);
194
195           if (meta) {
196             ts = meta->timestamp;
197           } else {
198             ts = 0;
199           }
200         } else {
201           ts = 0;
202         }
203
204         break;
205       }
206       case GST_TIME_OVERLAY_TIME_LINE_BUFFER_TIME:
207       default:
208         ts = ts_buffer;
209         break;
210     }
211
212     if (self->show_times_as_dates) {
213       GDateTime *datetime;
214
215       datetime =
216           g_date_time_add_seconds (self->datetime_epoch,
217           ((gdouble) ts) / GST_SECOND);
218
219       time_str = g_date_time_format (datetime, self->datetime_format);
220
221       g_date_time_unref (datetime);
222     } else {
223       time_str = gst_time_overlay_render_time (GST_TIME_OVERLAY (overlay), ts);
224     }
225   }
226
227   txt = g_strdup (overlay->default_text);
228
229   if (txt != NULL && *txt != '\0') {
230     ret = g_strdup_printf ("%s %s", txt, time_str);
231   } else {
232     ret = time_str;
233     time_str = NULL;
234   }
235
236   g_free (txt);
237   g_free (time_str);
238
239   return ret;
240 }
241
242 static GstStateChangeReturn
243 gst_time_overlay_change_state (GstElement * element, GstStateChange transition)
244 {
245   GstTimeOverlay *self = GST_TIME_OVERLAY (element);
246
247   switch (transition) {
248     case GST_STATE_CHANGE_READY_TO_PAUSED:
249       self->first_running_time = GST_CLOCK_TIME_NONE;
250       break;
251     default:
252       break;
253   }
254
255   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
256 }
257
258 static void
259 gst_time_overlay_finalize (GObject * gobject)
260 {
261   GstTimeOverlay *self = GST_TIME_OVERLAY (gobject);
262
263   gst_clear_caps (&self->reference_timestamp_caps);
264   g_date_time_unref (self->datetime_epoch);
265   g_free (self->datetime_format);
266
267   G_OBJECT_CLASS (parent_class)->finalize (gobject);
268 }
269
270 static void
271 gst_time_overlay_class_init (GstTimeOverlayClass * klass)
272 {
273   GstElementClass *gstelement_class;
274   GstBaseTextOverlayClass *gsttextoverlay_class;
275   GObjectClass *gobject_class;
276
277   gsttextoverlay_class = (GstBaseTextOverlayClass *) klass;
278   gstelement_class = (GstElementClass *) klass;
279   gobject_class = (GObjectClass *) klass;
280
281   gst_element_class_set_static_metadata (gstelement_class, "Time overlay",
282       "Filter/Editor/Video",
283       "Overlays buffer time stamps on a video stream",
284       "Tim-Philipp Müller <tim@centricular.net>");
285
286   gsttextoverlay_class->get_text = gst_time_overlay_get_text;
287
288   gstelement_class->change_state = gst_time_overlay_change_state;
289
290   gobject_class->finalize = gst_time_overlay_finalize;
291   gobject_class->set_property = gst_time_overlay_set_property;
292   gobject_class->get_property = gst_time_overlay_get_property;
293
294   g_object_class_install_property (gobject_class, PROP_TIME_LINE,
295       g_param_spec_enum ("time-mode", "Time Mode", "What time to show",
296           GST_TYPE_TIME_OVERLAY_TIME_LINE, DEFAULT_TIME_LINE,
297           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
298
299   g_object_class_install_property (gobject_class, PROP_DATETIME_EPOCH,
300       g_param_spec_boxed ("datetime-epoch", "Datetime Epoch",
301           "When showing times as dates, the initial date from which time "
302           "is counted, if not specified prime epoch is used (1900-01-01)",
303           G_TYPE_DATE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
304
305   g_object_class_install_property (gobject_class, PROP_DATETIME_FORMAT,
306       g_param_spec_string ("datetime-format", "Datetime Format",
307           "When showing times as dates, the format to render date and time in",
308           DEFAULT_DATETIME_FORMAT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
309
310   g_object_class_install_property (gobject_class, PROP_SHOW_TIMES_AS_DATES,
311       g_param_spec_boolean ("show-times-as-dates", "Show times as dates",
312           "Whether to display times, counted from datetime-epoch, as dates",
313           DEFAULT_SHOW_TIMES_AS_DATES,
314           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
315
316   /**
317    * timeoverlay:reference-timestamp-caps
318    *
319    * Selects the caps to use for the reference timestamp meta in
320    * time-mode=reference-timestamp.
321    *
322    * Since: 1.22
323    */
324   g_object_class_install_property (gobject_class, PROP_REFERENCE_TIMESTAMP_CAPS,
325       g_param_spec_boxed ("reference-timestamp-caps",
326           "Reference Timestamp Caps",
327           "Caps to use for the reference timestamp time mode",
328           GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
329
330   gst_type_mark_as_plugin_api (GST_TYPE_TIME_OVERLAY_TIME_LINE, 0);
331 }
332
333 static gboolean
334 gst_time_overlay_video_event (GstPad * pad, GstObject * parent,
335     GstEvent * event)
336 {
337   GstTimeOverlay *overlay = GST_TIME_OVERLAY (parent);
338
339   switch (GST_EVENT_TYPE (event)) {
340     case GST_EVENT_FLUSH_STOP:
341       overlay->first_running_time = GST_CLOCK_TIME_NONE;
342       break;
343     default:
344       break;
345   }
346
347   return overlay->orig_video_event (pad, parent, event);
348 }
349
350 static void
351 gst_time_overlay_init (GstTimeOverlay * overlay)
352 {
353   GstBaseTextOverlay *textoverlay;
354   PangoContext *context;
355   PangoFontDescription *font_description;
356   GstPad *video_sink;
357
358   textoverlay = GST_BASE_TEXT_OVERLAY (overlay);
359
360   textoverlay->valign = GST_BASE_TEXT_OVERLAY_VALIGN_TOP;
361   textoverlay->halign = GST_BASE_TEXT_OVERLAY_HALIGN_LEFT;
362
363   overlay->time_line = DEFAULT_TIME_LINE;
364   overlay->show_times_as_dates = DEFAULT_SHOW_TIMES_AS_DATES;
365   overlay->datetime_epoch = g_date_time_new_utc (1900, 1, 1, 0, 0, 0);
366   overlay->datetime_format = g_strdup (DEFAULT_DATETIME_FORMAT);
367
368   overlay->reference_timestamp_caps =
369       gst_static_caps_get (&ntp_reference_timestamp_caps);
370
371   context = textoverlay->pango_context;
372
373   pango_context_set_language (context, pango_language_from_string ("en_US"));
374   pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);
375
376   font_description = pango_font_description_new ();
377   pango_font_description_set_family_static (font_description, "Monospace");
378   pango_font_description_set_style (font_description, PANGO_STYLE_NORMAL);
379   pango_font_description_set_variant (font_description, PANGO_VARIANT_NORMAL);
380   pango_font_description_set_weight (font_description, PANGO_WEIGHT_NORMAL);
381   pango_font_description_set_stretch (font_description, PANGO_STRETCH_NORMAL);
382   pango_font_description_set_size (font_description, 18 * PANGO_SCALE);
383   pango_context_set_font_description (context, font_description);
384   pango_font_description_free (font_description);
385
386   video_sink = gst_element_get_static_pad (GST_ELEMENT (overlay), "video_sink");
387   overlay->orig_video_event = GST_PAD_EVENTFUNC (video_sink);
388   gst_pad_set_event_function (video_sink, gst_time_overlay_video_event);
389   gst_object_unref (video_sink);
390 }
391
392 static void
393 gst_time_overlay_set_property (GObject * object, guint prop_id,
394     const GValue * value, GParamSpec * pspec)
395 {
396   GstTimeOverlay *overlay = GST_TIME_OVERLAY (object);
397
398   switch (prop_id) {
399     case PROP_TIME_LINE:
400       g_atomic_int_set (&overlay->time_line, g_value_get_enum (value));
401       break;
402     case PROP_SHOW_TIMES_AS_DATES:
403       overlay->show_times_as_dates = g_value_get_boolean (value);
404       break;
405     case PROP_DATETIME_EPOCH:
406       g_date_time_unref (overlay->datetime_epoch);
407       overlay->datetime_epoch = (GDateTime *) g_value_dup_boxed (value);
408       break;
409     case PROP_DATETIME_FORMAT:
410       g_free (overlay->datetime_format);
411       overlay->datetime_format = g_value_dup_string (value);
412       break;
413     case PROP_REFERENCE_TIMESTAMP_CAPS:
414       gst_clear_caps (&overlay->reference_timestamp_caps);
415       overlay->reference_timestamp_caps = g_value_dup_boxed (value);
416       break;
417     default:
418       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
419       break;
420   }
421 }
422
423 static void
424 gst_time_overlay_get_property (GObject * object, guint prop_id,
425     GValue * value, GParamSpec * pspec)
426 {
427   GstTimeOverlay *overlay = GST_TIME_OVERLAY (object);
428
429   switch (prop_id) {
430     case PROP_TIME_LINE:
431       g_value_set_enum (value, g_atomic_int_get (&overlay->time_line));
432       break;
433     case PROP_SHOW_TIMES_AS_DATES:
434       g_value_set_boolean (value, overlay->show_times_as_dates);
435       break;
436     case PROP_DATETIME_EPOCH:
437       g_value_set_boxed (value, overlay->datetime_epoch);
438       break;
439     case PROP_DATETIME_FORMAT:
440       g_value_set_string (value, overlay->datetime_format);
441       break;
442     case PROP_REFERENCE_TIMESTAMP_CAPS:
443       g_value_set_boxed (value, overlay->reference_timestamp_caps);
444       break;
445     default:
446       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
447       break;
448   }
449 }