gstclockoverlay.c 7.36 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 * Copyright (C) <2005> Tim-Philipp Müller <tim@centricular.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

21
22
/**
 * SECTION:element-clockoverlay
23
 * @see_also: #GstBaseTextOverlay, #GstTimeOverlay
24
25
26
 *
 * This element overlays the current clock time on top of a video
 * stream. You can position the text and configure the font details
27
 * using the properties of the #GstBaseTextOverlay class. By default, the
28
29
 * time is displayed in the top left corner of the picture, with some
 * padding to the left and to the top.
30
31
32
33
 *
 * <refsect2>
 * <title>Example launch lines</title>
 * |[
34
 * gst-launch -v videotestsrc ! clockoverlay ! xvimagesink
35
36
 * ]| Display the current time in the top left corner of the video picture
 * |[
37
 * gst-launch -v videotestsrc ! clockoverlay halign=right valign=bottom text="Edge City" shaded-background=true ! videoconvert ! ximagesink
38
 * ]| Another pipeline that displays the current time with some leading
39
40
 * text in the bottom right corner of the video picture, with the background
 * of the text being shaded in order to make it more legible on top of a
41
 * bright video background.
42
43
44
 * </refsect2>
 */

45
46
47
48
49
50
51
52
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gstclockoverlay.h>
#include <gst/video/video.h>
#include <time.h>

53
54
55
56
57
58
59
60
61
62

#define DEFAULT_PROP_TIMEFORMAT 	"%H:%M:%S"

enum
{
  PROP_0,
  PROP_TIMEFORMAT,
  PROP_LAST
};

63
64
#define gst_clock_overlay_parent_class parent_class
G_DEFINE_TYPE (GstClockOverlay, gst_clock_overlay, GST_TYPE_BASE_TEXT_OVERLAY);
65
66
67
68
69
70
71

static void gst_clock_overlay_finalize (GObject * object);
static void gst_clock_overlay_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_clock_overlay_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);

72
73
74
static gchar *
gst_clock_overlay_render_time (GstClockOverlay * overlay)
{
75
  struct tm *t;
76
  time_t now;
77
  gchar buf[256];
78

79
80
81
82
#ifdef HAVE_LOCALTIME_R
  struct tm dummy;
#endif

83
  now = time (NULL);
84
85

#ifdef HAVE_LOCALTIME_R
86
87
88
  /* Need to call tzset explicitly when calling localtime_r for changes
     to the timezone between calls to be visible.  */
  tzset ();
89
90
91
92
93
94
95
  t = localtime_r (&now, &dummy);
#else
  /* on win32 this apparently returns a per-thread struct which would be fine */
  t = localtime (&now);
#endif

  if (t == NULL)
96
97
    return g_strdup ("--:--:--");

98
99
100
  if (strftime (buf, sizeof (buf), overlay->format, t) == 0)
    return g_strdup ("");
  return g_strdup (buf);
101
102
}

103
/* Called with lock held */
104
static gchar *
105
106
gst_clock_overlay_get_text (GstBaseTextOverlay * overlay,
    GstBuffer * video_frame)
107
108
{
  gchar *time_str, *txt, *ret;
109
  GstClockOverlay *clock_overlay = GST_CLOCK_OVERLAY (overlay);
110
111
112

  txt = g_strdup (overlay->default_text);

113
  time_str = gst_clock_overlay_render_time (clock_overlay);
114
115
116
117
118
119
120
  if (txt != NULL && *txt != '\0') {
    ret = g_strdup_printf ("%s %s", txt, time_str);
  } else {
    ret = time_str;
    time_str = NULL;
  }

121
122
123
124
125
126
  if (g_strcmp0 (ret, clock_overlay->text)) {
    overlay->need_render = TRUE;
    g_free (clock_overlay->text);
    clock_overlay->text = g_strdup (ret);
  }

127
128
129
130
131
132
133
134
135
  g_free (txt);
  g_free (time_str);

  return ret;
}

static void
gst_clock_overlay_class_init (GstClockOverlayClass * klass)
{
136
  GObjectClass *gobject_class;
137
  GstElementClass *gstelement_class;
138
  GstBaseTextOverlayClass *gsttextoverlay_class;
139
140
  PangoContext *context;
  PangoFontDescription *font_description;
141

142
  gobject_class = (GObjectClass *) klass;
143
  gstelement_class = (GstElementClass *) klass;
144
  gsttextoverlay_class = (GstBaseTextOverlayClass *) klass;
145

146
147
148
149
  gobject_class->finalize = gst_clock_overlay_finalize;
  gobject_class->set_property = gst_clock_overlay_set_property;
  gobject_class->get_property = gst_clock_overlay_get_property;

150
151
152
153
154
  gst_element_class_set_details_simple (gstelement_class, "Clock overlay",
      "Filter/Editor/Video",
      "Overlays the current clock time on a video stream",
      "Tim-Philipp Müller <tim@centricular.net>");

155
  gsttextoverlay_class->get_text = gst_clock_overlay_get_text;
156
157
158
159
160

  g_object_class_install_property (gobject_class, PROP_TIMEFORMAT,
      g_param_spec_string ("time-format", "Date/Time Format",
          "Format to use for time and date value, as in strftime.",
          DEFAULT_PROP_TIMEFORMAT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
161

162
163
  g_mutex_lock (gsttextoverlay_class->pango_lock);
  context = gsttextoverlay_class->pango_context;
164
165
166
167
168
169
170
171
172
173
174
175
176

  pango_context_set_language (context, pango_language_from_string ("en_US"));
  pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);

  font_description = pango_font_description_new ();
  pango_font_description_set_family_static (font_description, "Monospace");
  pango_font_description_set_style (font_description, PANGO_STYLE_NORMAL);
  pango_font_description_set_variant (font_description, PANGO_VARIANT_NORMAL);
  pango_font_description_set_weight (font_description, PANGO_WEIGHT_NORMAL);
  pango_font_description_set_stretch (font_description, PANGO_STRETCH_NORMAL);
  pango_font_description_set_size (font_description, 18 * PANGO_SCALE);
  pango_context_set_font_description (context, font_description);
  pango_font_description_free (font_description);
177
  g_mutex_unlock (gsttextoverlay_class->pango_lock);
178
179
}

180
181
182
183
184
185
186

static void
gst_clock_overlay_finalize (GObject * object)
{
  GstClockOverlay *overlay = GST_CLOCK_OVERLAY (object);

  g_free (overlay->format);
187
  g_free (overlay->text);
188
189
190
191
192
193
  overlay->format = NULL;

  G_OBJECT_CLASS (parent_class)->finalize (object);
}


194
static void
195
gst_clock_overlay_init (GstClockOverlay * overlay)
196
{
197
  GstBaseTextOverlay *textoverlay;
198

199
  textoverlay = GST_BASE_TEXT_OVERLAY (overlay);
200

201
202
  textoverlay->valign = GST_BASE_TEXT_OVERLAY_VALIGN_TOP;
  textoverlay->halign = GST_BASE_TEXT_OVERLAY_HALIGN_LEFT;
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243

  overlay->format = g_strdup (DEFAULT_PROP_TIMEFORMAT);
}


static void
gst_clock_overlay_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstClockOverlay *overlay = GST_CLOCK_OVERLAY (object);

  GST_OBJECT_LOCK (overlay);
  switch (prop_id) {
    case PROP_TIMEFORMAT:
      g_free (overlay->format);
      overlay->format = g_value_dup_string (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
  GST_OBJECT_UNLOCK (overlay);
}


static void
gst_clock_overlay_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstClockOverlay *overlay = GST_CLOCK_OVERLAY (object);

  GST_OBJECT_LOCK (overlay);
  switch (prop_id) {
    case PROP_TIMEFORMAT:
      g_value_set_string (value, overlay->format);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
  GST_OBJECT_UNLOCK (overlay);
244
}