Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-base.git] / ext / pango / gsttimeoverlay.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2005> 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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * SECTION:element-timeoverlay
23  * @see_also: #GstBaseTextOverlay, #GstClockOverlay
24  *
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.
30  *
31  * <refsect2>
32  * |[
33  * gst-launch -v videotestsrc ! timeoverlay ! xvimagesink
34  * ]| Display the time stamps in the top left
35  * corner of the video picture.
36  * |[
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.
42  * </refsect2>
43  */
44
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #include <gst/video/video.h>
50
51 #include <gsttimeoverlay.h>
52
53 #define gst_time_overlay_parent_class parent_class
54 G_DEFINE_TYPE (GstTimeOverlay, gst_time_overlay, GST_TYPE_BASE_TEXT_OVERLAY);
55
56 static gchar *
57 gst_time_overlay_render_time (GstTimeOverlay * overlay, GstClockTime time)
58 {
59   guint hours, mins, secs, msecs;
60
61   if (!GST_CLOCK_TIME_IS_VALID (time))
62     return g_strdup ("");
63
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));
68
69   return g_strdup_printf ("%u:%02u:%02u.%03u", hours, mins, secs, msecs);
70 }
71
72 /* Called with lock held */
73 static gchar *
74 gst_time_overlay_get_text (GstBaseTextOverlay * overlay,
75     GstBuffer * video_frame)
76 {
77   GstClockTime time = GST_BUFFER_TIMESTAMP (video_frame);
78   gchar *time_str, *txt, *ret;
79
80   overlay->need_render = TRUE;
81
82   if (!GST_CLOCK_TIME_IS_VALID (time)) {
83     GST_DEBUG ("buffer without valid timestamp");
84     return g_strdup ("");
85   }
86
87   GST_DEBUG ("buffer with timestamp %" GST_TIME_FORMAT, GST_TIME_ARGS (time));
88
89   txt = g_strdup (overlay->default_text);
90
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);
94   } else {
95     ret = time_str;
96     time_str = NULL;
97   }
98
99   g_free (txt);
100   g_free (time_str);
101
102   return ret;
103 }
104
105 static void
106 gst_time_overlay_class_init (GstTimeOverlayClass * klass)
107 {
108   GstElementClass *gstelement_class;
109   GstBaseTextOverlayClass *gsttextoverlay_class;
110   PangoContext *context;
111   PangoFontDescription *font_description;
112
113   gsttextoverlay_class = (GstBaseTextOverlayClass *) klass;
114   gstelement_class = (GstElementClass *) klass;
115
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>");
120
121   gsttextoverlay_class->get_text = gst_time_overlay_get_text;
122
123   g_mutex_lock (gsttextoverlay_class->pango_lock);
124   context = gsttextoverlay_class->pango_context;
125
126   pango_context_set_language (context, pango_language_from_string ("en_US"));
127   pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);
128
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);
139 }
140
141 static void
142 gst_time_overlay_init (GstTimeOverlay * overlay)
143 {
144   GstBaseTextOverlay *textoverlay;
145
146   textoverlay = GST_BASE_TEXT_OVERLAY (overlay);
147
148   textoverlay->valign = GST_BASE_TEXT_OVERLAY_VALIGN_TOP;
149   textoverlay->halign = GST_BASE_TEXT_OVERLAY_HALIGN_LEFT;
150 }