1 /* GStreamer
2 * Copyright (C) <2008> Thijs Vermeir <thijsvermeir@gmail.com>
3 * Copyright (C) 2011 David Schleef <ds@schleef.org>
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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <string.h>
26
27 #include "gstwebvttenc.h"
28
29 GST_DEBUG_CATEGORY_STATIC (webvttenc_debug);
30 #define GST_CAT_DEFAULT webvttenc_debug
31
32 enum
33 {
34 PROP_0,
35 PROP_TIMESTAMP,
36 PROP_DURATION
37 };
38
39 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
40 GST_PAD_SRC,
41 GST_PAD_ALWAYS,
42 GST_STATIC_CAPS ("application/x-subtitle-vtt"));
43
44 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
45 GST_PAD_SINK,
46 GST_PAD_ALWAYS,
47 GST_STATIC_CAPS ("text/x-raw, format = { pango-markup, utf8 }"));
48
49 static GstFlowReturn gst_webvtt_enc_chain (GstPad * pad, GstObject * parent,
50 GstBuffer * buf);
51 static void gst_webvtt_enc_append_timestamp_to_string (GstClockTime timestamp,
52 GString * str);
53 static void gst_webvtt_enc_get_property (GObject * object, guint prop_id,
54 GValue * value, GParamSpec * pspec);
55 static void gst_webvtt_enc_reset (GstWebvttEnc * webvttenc);
56 static void gst_webvtt_enc_set_property (GObject * object, guint prop_id,
57 const GValue * value, GParamSpec * pspec);
58
59 #define parent_class gst_webvtt_enc_parent_class
60 G_DEFINE_TYPE (GstWebvttEnc, gst_webvtt_enc, GST_TYPE_ELEMENT);
61 GST_ELEMENT_REGISTER_DEFINE (webvttenc, "webvttenc", GST_RANK_NONE,
62 GST_TYPE_WEBVTT_ENC);
63
64 static void
gst_webvtt_enc_append_timestamp_to_string(GstClockTime timestamp,GString * str)65 gst_webvtt_enc_append_timestamp_to_string (GstClockTime timestamp,
66 GString * str)
67 {
68 guint h, m, s, ms;
69
70 h = timestamp / (3600 * GST_SECOND);
71
72 timestamp -= h * 3600 * GST_SECOND;
73 m = timestamp / (60 * GST_SECOND);
74
75 timestamp -= m * 60 * GST_SECOND;
76 s = timestamp / GST_SECOND;
77
78 timestamp -= s * GST_SECOND;
79 ms = timestamp / GST_MSECOND;
80
81 g_string_append_printf (str, "%02d:%02d:%02d.%03d", h, m, s, ms);
82 }
83
84 static GstFlowReturn
gst_webvtt_enc_chain(GstPad * pad,GstObject * parent,GstBuffer * buf)85 gst_webvtt_enc_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
86 {
87 GstWebvttEnc *webvttenc = GST_WEBVTT_ENC (parent);
88 GstClockTime ts, dur = GST_SECOND;
89 GstBuffer *new_buffer;
90 GstMapInfo map_info;
91 GstFlowReturn ret;
92 GString *s;
93 gsize buf_size;
94
95 if (!webvttenc->pushed_header) {
96 const char *header = "WEBVTT\n\n";
97
98 new_buffer = gst_buffer_new_wrapped (g_strdup (header), strlen (header));
99
100 GST_BUFFER_PTS (new_buffer) = GST_CLOCK_TIME_NONE;
101 GST_BUFFER_DURATION (new_buffer) = GST_CLOCK_TIME_NONE;
102
103 ret = gst_pad_push (webvttenc->srcpad, new_buffer);
104
105 if (ret != GST_FLOW_OK)
106 goto out;
107
108 webvttenc->pushed_header = TRUE;
109 }
110
111 gst_object_sync_values (GST_OBJECT (webvttenc), GST_BUFFER_PTS (buf));
112
113 ts = GST_BUFFER_PTS (buf) + webvttenc->timestamp;
114 if (GST_BUFFER_DURATION_IS_VALID (buf))
115 dur = GST_BUFFER_DURATION (buf) + webvttenc->duration;
116 else if (webvttenc->duration > 0)
117 dur = webvttenc->duration;
118 else
119 dur = GST_SECOND;
120
121 buf_size = gst_buffer_get_size (buf);
122 s = g_string_sized_new (50 + buf_size + 1 + 1);
123
124 /* start_time --> end_time */
125 gst_webvtt_enc_append_timestamp_to_string (ts, s);
126 g_string_append_printf (s, " --> ");
127 gst_webvtt_enc_append_timestamp_to_string (ts + dur, s);
128 g_string_append_c (s, '\n');
129
130 /* text */
131 if (gst_buffer_map (buf, &map_info, GST_MAP_READ)) {
132 g_string_append_len (s, (const gchar *) map_info.data, map_info.size);
133 gst_buffer_unmap (buf, &map_info);
134 }
135
136 g_string_append (s, "\n\n");
137
138 buf_size = s->len;
139 new_buffer = gst_buffer_new_wrapped (g_string_free (s, FALSE), buf_size);
140
141 GST_BUFFER_TIMESTAMP (new_buffer) = GST_BUFFER_TIMESTAMP (buf);
142 GST_BUFFER_DURATION (new_buffer) = GST_BUFFER_DURATION (buf);
143
144 ret = gst_pad_push (webvttenc->srcpad, new_buffer);
145
146 out:
147
148 gst_buffer_unref (buf);
149
150 return ret;
151 }
152
153 static gboolean
gst_webvtt_enc_event(GstPad * pad,GstObject * parent,GstEvent * event)154 gst_webvtt_enc_event (GstPad * pad, GstObject * parent, GstEvent * event)
155 {
156 GstWebvttEnc *webvttenc = GST_WEBVTT_ENC (parent);
157 gboolean ret;
158
159 switch (GST_EVENT_TYPE (event)) {
160 case GST_EVENT_CAPS:
161 {
162 GstCaps *caps;
163
164 caps = gst_static_pad_template_get_caps (&src_template);
165 gst_pad_set_caps (webvttenc->srcpad, caps);
166 gst_caps_unref (caps);
167 gst_event_unref (event);
168 ret = TRUE;
169 break;
170 }
171 default:
172 ret = gst_pad_event_default (pad, parent, event);
173 break;
174 }
175
176 return ret;
177 }
178
179 static void
gst_webvtt_enc_reset(GstWebvttEnc * webvttenc)180 gst_webvtt_enc_reset (GstWebvttEnc * webvttenc)
181 {
182 webvttenc->counter = 1;
183 }
184
185 static GstStateChangeReturn
gst_webvtt_enc_change_state(GstElement * element,GstStateChange transition)186 gst_webvtt_enc_change_state (GstElement * element, GstStateChange transition)
187 {
188 GstStateChangeReturn ret;
189 GstWebvttEnc *webvttenc = GST_WEBVTT_ENC (element);
190
191 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
192 if (ret == GST_STATE_CHANGE_FAILURE)
193 return ret;
194
195 switch (transition) {
196 case GST_STATE_CHANGE_PAUSED_TO_READY:
197 gst_webvtt_enc_reset (webvttenc);
198 break;
199 default:
200 break;
201 }
202
203 return ret;
204 }
205
206 static void
gst_webvtt_enc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)207 gst_webvtt_enc_get_property (GObject * object,
208 guint prop_id, GValue * value, GParamSpec * pspec)
209 {
210 GstWebvttEnc *webvttenc;
211
212 webvttenc = GST_WEBVTT_ENC (object);
213
214 switch (prop_id) {
215 case PROP_TIMESTAMP:
216 g_value_set_int64 (value, webvttenc->timestamp);
217 break;
218 case PROP_DURATION:
219 g_value_set_int64 (value, webvttenc->duration);
220 break;
221 default:
222 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223 break;
224 }
225 }
226
227 static void
gst_webvtt_enc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)228 gst_webvtt_enc_set_property (GObject * object,
229 guint prop_id, const GValue * value, GParamSpec * pspec)
230 {
231
232 GstWebvttEnc *webvttenc;
233
234 webvttenc = GST_WEBVTT_ENC (object);
235
236 switch (prop_id) {
237 case PROP_TIMESTAMP:
238 webvttenc->timestamp = g_value_get_int64 (value);
239 break;
240 case PROP_DURATION:
241 webvttenc->duration = g_value_get_int64 (value);
242 break;
243 default:
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245 break;
246 }
247 }
248
249 static void
gst_webvtt_enc_class_init(GstWebvttEncClass * klass)250 gst_webvtt_enc_class_init (GstWebvttEncClass * klass)
251 {
252 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
253 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
254
255 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_webvtt_enc_set_property);
256 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_webvtt_enc_get_property);
257
258 element_class->change_state = GST_DEBUG_FUNCPTR (gst_webvtt_enc_change_state);
259
260 g_object_class_install_property (gobject_class, PROP_TIMESTAMP,
261 g_param_spec_int64 ("timestamp", "Offset for the starttime",
262 "Offset for the starttime for the subtitles", G_MININT64, G_MAXINT64,
263 0,
264 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
265 g_object_class_install_property (gobject_class, PROP_DURATION,
266 g_param_spec_int64 ("duration", "Offset for the duration",
267 "Offset for the duration of the subtitles", G_MININT64, G_MAXINT64,
268 0,
269 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
270
271 gst_element_class_add_static_pad_template (element_class, &sink_template);
272 gst_element_class_add_static_pad_template (element_class, &src_template);
273
274 gst_element_class_set_static_metadata (element_class,
275 "WebVTT encoder", "Codec/Encoder/Subtitle",
276 "WebVTT subtitle encoder", "David Schleef <ds@schleef.org>");
277
278 GST_DEBUG_CATEGORY_INIT (webvttenc_debug, "webvttenc", 0,
279 "SubRip subtitle encoder");
280 }
281
282 static void
gst_webvtt_enc_init(GstWebvttEnc * webvttenc)283 gst_webvtt_enc_init (GstWebvttEnc * webvttenc)
284 {
285 gst_webvtt_enc_reset (webvttenc);
286
287 webvttenc->srcpad = gst_pad_new_from_static_template (&src_template, "src");
288 gst_element_add_pad (GST_ELEMENT (webvttenc), webvttenc->srcpad);
289 webvttenc->sinkpad =
290 gst_pad_new_from_static_template (&sink_template, "sink");
291 gst_pad_set_chain_function (webvttenc->sinkpad,
292 GST_DEBUG_FUNCPTR (gst_webvtt_enc_chain));
293 gst_pad_set_event_function (webvttenc->sinkpad,
294 GST_DEBUG_FUNCPTR (gst_webvtt_enc_event));
295 gst_element_add_pad (GST_ELEMENT (webvttenc), webvttenc->sinkpad);
296 }
297