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