tizen 2.3 release
[framework/multimedia/gst-plugins-base0.10.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: #GstTextOverlay, #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 #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.
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 GST_BOILERPLATE (GstTimeOverlay, gst_time_overlay, GstTextOverlay,
54     GST_TYPE_TEXT_OVERLAY);
55
56 static void
57 gst_time_overlay_base_init (gpointer g_class)
58 {
59   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
60
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>");
65 }
66
67 static gchar *
68 gst_time_overlay_render_time (GstTimeOverlay * overlay, GstClockTime time)
69 {
70   guint hours, mins, secs, msecs;
71
72   if (!GST_CLOCK_TIME_IS_VALID (time))
73     return g_strdup ("");
74
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));
79
80   return g_strdup_printf ("%u:%02u:%02u.%03u", hours, mins, secs, msecs);
81 }
82
83 /* Called with lock held */
84 static gchar *
85 gst_time_overlay_get_text (GstTextOverlay * overlay, GstBuffer * video_frame)
86 {
87   GstClockTime time = GST_BUFFER_TIMESTAMP (video_frame);
88   gchar *time_str, *txt, *ret;
89
90   overlay->need_render = TRUE;
91
92   if (!GST_CLOCK_TIME_IS_VALID (time)) {
93     GST_DEBUG ("buffer without valid timestamp");
94     return g_strdup ("");
95   }
96
97   GST_DEBUG ("buffer with timestamp %" GST_TIME_FORMAT, GST_TIME_ARGS (time));
98
99   txt = g_strdup (overlay->default_text);
100
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);
104   } else {
105     ret = time_str;
106     time_str = NULL;
107   }
108
109   g_free (txt);
110   g_free (time_str);
111
112   return ret;
113 }
114
115 static void
116 gst_time_overlay_class_init (GstTimeOverlayClass * klass)
117 {
118   GstTextOverlayClass *gsttextoverlay_class;
119   PangoContext *context;
120   PangoFontDescription *font_description;
121
122   gsttextoverlay_class = (GstTextOverlayClass *) klass;
123
124   gsttextoverlay_class->get_text = gst_time_overlay_get_text;
125
126   g_mutex_lock (GST_TEXT_OVERLAY_CLASS (klass)->pango_lock);
127   context = GST_TEXT_OVERLAY_CLASS (klass)->pango_context;
128
129   pango_context_set_language (context, pango_language_from_string ("en_US"));
130   pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);
131
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);
142 }
143
144 static void
145 gst_time_overlay_init (GstTimeOverlay * overlay, GstTimeOverlayClass * klass)
146 {
147   GstTextOverlay *textoverlay;
148
149   textoverlay = GST_TEXT_OVERLAY (overlay);
150
151   textoverlay->valign = GST_TEXT_OVERLAY_VALIGN_TOP;
152   textoverlay->halign = GST_TEXT_OVERLAY_HALIGN_LEFT;
153 }