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-clockoverlay |
|
23 * @see_also: #GstTextOverlay, #GstTimeOverlay |
|
24 * |
|
25 * <refsect2> |
|
26 * <para> |
|
27 * This element overlays the current clock time on top of a video |
|
28 * stream. You can position the text and configure the font details |
|
29 * using the properties of the #GstTextOverlay class. By default, the |
|
30 * time is displayed in the top left corner of the picture, with some |
|
31 * padding to the left and to the top. |
|
32 * </para> |
|
33 * <para> |
|
34 * Here is a simple pipeline that displays the current time in the top left |
|
35 * corner of the video picture: |
|
36 * <programlisting> |
|
37 * gst-launch -v videotestsrc ! clockoverlay ! xvimagesink |
|
38 * </programlisting> |
|
39 * </para> |
|
40 * <para> |
|
41 * Here is another pipeline that displays the current time with some leading |
|
42 * text in the bottom right corner of the video picture, with the background |
|
43 * of the text being shaded in order to make it more legible on top of a |
|
44 * bright video background: |
|
45 * <programlisting> |
|
46 * gst-launch -v videotestsrc ! clockoverlay halign=right valign=bottom text="Edge City" shaded-background=true ! ffmpegcolorspace ! ximagesink |
|
47 * </programlisting> |
|
48 * </para> |
|
49 * </refsect2> |
|
50 */ |
|
51 |
|
52 #ifdef HAVE_CONFIG_H |
|
53 #include "config.h" |
|
54 #endif |
|
55 |
|
56 #include <gstclockoverlay.h> |
|
57 #include <gst/video/video.h> |
|
58 #include <time.h> |
|
59 |
|
60 static const GstElementDetails clock_overlay_details = |
|
61 GST_ELEMENT_DETAILS ("Clock overlay", |
|
62 "Filter/Editor/Video", |
|
63 "Overlays the current clock time on a video stream", |
|
64 "Tim-Philipp Müller <tim@centricular.net>"); |
|
65 |
|
66 GST_BOILERPLATE (GstClockOverlay, gst_clock_overlay, GstTextOverlay, |
|
67 GST_TYPE_TEXT_OVERLAY) |
|
68 |
|
69 static void gst_clock_overlay_base_init (gpointer g_class) |
|
70 { |
|
71 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class); |
|
72 |
|
73 gst_element_class_set_details (element_class, &clock_overlay_details); |
|
74 } |
|
75 |
|
76 static gchar * |
|
77 gst_clock_overlay_render_time (GstClockOverlay * overlay) |
|
78 { |
|
79 struct tm *t; |
|
80 time_t now; |
|
81 |
|
82 #ifdef HAVE_LOCALTIME_R |
|
83 struct tm dummy; |
|
84 #endif |
|
85 |
|
86 now = time (NULL); |
|
87 |
|
88 #ifdef HAVE_LOCALTIME_R |
|
89 t = localtime_r (&now, &dummy); |
|
90 #else |
|
91 /* on win32 this apparently returns a per-thread struct which would be fine */ |
|
92 t = localtime (&now); |
|
93 #endif |
|
94 |
|
95 if (t == NULL) |
|
96 return g_strdup ("--:--:--"); |
|
97 |
|
98 return g_strdup_printf ("%02u:%02u:%02u", t->tm_hour, t->tm_min, t->tm_sec); |
|
99 } |
|
100 |
|
101 /* Called with lock held */ |
|
102 static gchar * |
|
103 gst_clock_overlay_get_text (GstTextOverlay * overlay, GstBuffer * video_frame) |
|
104 { |
|
105 gchar *time_str, *txt, *ret; |
|
106 |
|
107 overlay->need_render = TRUE; |
|
108 |
|
109 txt = g_strdup (overlay->default_text); |
|
110 |
|
111 time_str = gst_clock_overlay_render_time (GST_CLOCK_OVERLAY (overlay)); |
|
112 if (txt != NULL && *txt != '\0') { |
|
113 ret = g_strdup_printf ("%s %s", txt, time_str); |
|
114 } else { |
|
115 ret = time_str; |
|
116 time_str = NULL; |
|
117 } |
|
118 |
|
119 g_free (txt); |
|
120 g_free (time_str); |
|
121 |
|
122 return ret; |
|
123 } |
|
124 |
|
125 static void |
|
126 gst_clock_overlay_class_init (GstClockOverlayClass * klass) |
|
127 { |
|
128 GstTextOverlayClass *gsttextoverlay_class; |
|
129 |
|
130 gsttextoverlay_class = (GstTextOverlayClass *) klass; |
|
131 |
|
132 gsttextoverlay_class->get_text = gst_clock_overlay_get_text; |
|
133 } |
|
134 |
|
135 static void |
|
136 gst_clock_overlay_init (GstClockOverlay * overlay, GstClockOverlayClass * klass) |
|
137 { |
|
138 PangoFontDescription *font_description; |
|
139 GstTextOverlay *textoverlay; |
|
140 PangoContext *context; |
|
141 |
|
142 textoverlay = GST_TEXT_OVERLAY (overlay); |
|
143 |
|
144 context = GST_TEXT_OVERLAY_CLASS (klass)->pango_context; |
|
145 |
|
146 pango_context_set_language (context, pango_language_from_string ("en_US")); |
|
147 pango_context_set_base_dir (context, PANGO_DIRECTION_LTR); |
|
148 |
|
149 font_description = pango_font_description_new (); |
|
150 pango_font_description_set_family_static (font_description, "Monospace"); |
|
151 pango_font_description_set_style (font_description, PANGO_STYLE_NORMAL); |
|
152 pango_font_description_set_variant (font_description, PANGO_VARIANT_NORMAL); |
|
153 pango_font_description_set_weight (font_description, PANGO_WEIGHT_NORMAL); |
|
154 pango_font_description_set_stretch (font_description, PANGO_STRETCH_NORMAL); |
|
155 pango_font_description_set_size (font_description, 18 * PANGO_SCALE); |
|
156 pango_context_set_font_description (context, font_description); |
|
157 pango_font_description_free (font_description); |
|
158 |
|
159 textoverlay->valign = GST_TEXT_OVERLAY_VALIGN_TOP; |
|
160 textoverlay->halign = GST_TEXT_OVERLAY_HALIGN_LEFT; |
|
161 } |
|