1 /* GStreamer tag muxer base class
2 * Copyright (C) 2006 Christophe Fergeau <teuf@gnome.org>
3 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
4 * Copyright (C) 2006 Sebastian Dröge <slomo@circular-chaos.org>
5 * Copyright (C) 2009 Pioneers of the Inevitable <songbird@songbirdnest.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 /**
24 * SECTION:gsttagmux
25 * @title: GstTagMux
26 * @see_also: GstApeMux, GstId3Mux
27 * @short_description: Base class for adding tags that are in one single chunk
28 * directly at the beginning or at the end of a file
29 *
30 * Provides a base class for adding tags at the beginning or end of a
31 * stream.
32 *
33 * ## Deriving from GstTagMux
34 *
35 * Subclasses have to do the following things:
36 *
37 * * In their base init function, they must add pad templates for the sink
38 * pad and the source pad to the element class, describing the media type
39 * they accept and output in the caps of the pad template.
40 * * In their class init function, they must override the
41 * GST_TAG_MUX_CLASS(mux_klass)->render_start_tag and/or
42 * GST_TAG_MUX_CLASS(mux_klass)->render_end_tag vfuncs and set up a render
43 * function.
44 *
45 */
46 #ifdef HAVE_CONFIG_H
47 #include <config.h>
48 #endif
49
50 #include <string.h>
51 #include <gst/gsttagsetter.h>
52 #include <gst/tag/tag.h>
53
54 #include "gsttagmux.h"
55
56 struct _GstTagMuxPrivate
57 {
58 GstPad *srcpad;
59 GstPad *sinkpad;
60 GstTagList *event_tags; /* tags received from upstream elements */
61 GstTagList *final_tags; /* Final set of tags used for muxing */
62 gsize start_tag_size;
63 gsize end_tag_size;
64 gboolean render_start_tag;
65 gboolean render_end_tag;
66
67 gint64 current_offset;
68 gint64 max_offset;
69
70 GstEvent *newsegment_ev; /* cached newsegment event from upstream */
71 };
72
73 GST_DEBUG_CATEGORY_STATIC (gst_tag_mux_debug);
74 #define GST_CAT_DEFAULT gst_tag_mux_debug
75
76 static GstElementClass *parent_class;
77 static gint private_offset = 0;
78
79 static void gst_tag_mux_class_init (GstTagMuxClass * klass);
80 static void gst_tag_mux_init (GstTagMux * mux, GstTagMuxClass * mux_class);
81 static GstStateChangeReturn
82 gst_tag_mux_change_state (GstElement * element, GstStateChange transition);
83 static GstFlowReturn gst_tag_mux_chain (GstPad * pad, GstObject * parent,
84 GstBuffer * buffer);
85 static gboolean gst_tag_mux_sink_event (GstPad * pad, GstObject * parent,
86 GstEvent * event);
87
88 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
89 * method to get to the padtemplates */
90 GType
gst_tag_mux_get_type(void)91 gst_tag_mux_get_type (void)
92 {
93 static volatile gsize tag_mux_type = 0;
94
95 if (g_once_init_enter (&tag_mux_type)) {
96 const GInterfaceInfo interface_info = { NULL, NULL, NULL };
97 GType _type;
98
99 _type = g_type_register_static_simple (GST_TYPE_ELEMENT,
100 "GstTagMux", sizeof (GstTagMuxClass),
101 (GClassInitFunc) gst_tag_mux_class_init, sizeof (GstTagMux),
102 (GInstanceInitFunc) gst_tag_mux_init, G_TYPE_FLAG_ABSTRACT);
103
104 private_offset =
105 g_type_add_instance_private (_type, sizeof (GstTagMuxPrivate));
106
107 g_type_add_interface_static (_type, GST_TYPE_TAG_SETTER, &interface_info);
108
109 g_once_init_leave (&tag_mux_type, _type);
110 }
111 return tag_mux_type;
112 }
113
114 static inline GstTagMuxPrivate *
gst_tag_mux_get_instance_private(GstTagMux * self)115 gst_tag_mux_get_instance_private (GstTagMux * self)
116 {
117 return (G_STRUCT_MEMBER_P (self, private_offset));
118 }
119
120 static void
gst_tag_mux_finalize(GObject * obj)121 gst_tag_mux_finalize (GObject * obj)
122 {
123 GstTagMux *mux = GST_TAG_MUX (obj);
124
125 if (mux->priv->newsegment_ev) {
126 gst_event_unref (mux->priv->newsegment_ev);
127 mux->priv->newsegment_ev = NULL;
128 }
129
130 if (mux->priv->event_tags) {
131 gst_tag_list_unref (mux->priv->event_tags);
132 mux->priv->event_tags = NULL;
133 }
134
135 if (mux->priv->final_tags) {
136 gst_tag_list_unref (mux->priv->final_tags);
137 mux->priv->final_tags = NULL;
138 }
139
140 G_OBJECT_CLASS (parent_class)->finalize (obj);
141 }
142
143 static void
gst_tag_mux_class_init(GstTagMuxClass * klass)144 gst_tag_mux_class_init (GstTagMuxClass * klass)
145 {
146 GObjectClass *gobject_class;
147 GstElementClass *gstelement_class;
148
149 gobject_class = (GObjectClass *) klass;
150 gstelement_class = (GstElementClass *) klass;
151
152 parent_class = g_type_class_peek_parent (klass);
153
154 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_tag_mux_finalize);
155 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_tag_mux_change_state);
156
157 if (private_offset != 0)
158 g_type_class_adjust_private_offset (klass, &private_offset);
159
160 GST_DEBUG_CATEGORY_INIT (gst_tag_mux_debug, "tagmux", 0,
161 "tag muxer base class");
162 }
163
164 static void
gst_tag_mux_init(GstTagMux * mux,GstTagMuxClass * mux_class)165 gst_tag_mux_init (GstTagMux * mux, GstTagMuxClass * mux_class)
166 {
167 GstElementClass *element_klass = GST_ELEMENT_CLASS (mux_class);
168 GstPadTemplate *tmpl;
169
170 mux->priv = gst_tag_mux_get_instance_private (mux);
171
172 /* pad through which data comes in to the element */
173 tmpl = gst_element_class_get_pad_template (element_klass, "sink");
174 if (tmpl) {
175 mux->priv->sinkpad = gst_pad_new_from_template (tmpl, "sink");
176 } else {
177 g_warning ("GstTagMux subclass '%s' did not install a %s pad template!\n",
178 G_OBJECT_CLASS_NAME (element_klass), "sink");
179 mux->priv->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
180 }
181 gst_pad_set_chain_function (mux->priv->sinkpad,
182 GST_DEBUG_FUNCPTR (gst_tag_mux_chain));
183 gst_pad_set_event_function (mux->priv->sinkpad,
184 GST_DEBUG_FUNCPTR (gst_tag_mux_sink_event));
185 gst_element_add_pad (GST_ELEMENT (mux), mux->priv->sinkpad);
186
187 /* pad through which data goes out of the element */
188 tmpl = gst_element_class_get_pad_template (element_klass, "src");
189 if (tmpl) {
190 GstCaps *tmpl_caps = gst_pad_template_get_caps (tmpl);
191
192 mux->priv->srcpad = gst_pad_new_from_template (tmpl, "src");
193 gst_pad_use_fixed_caps (mux->priv->srcpad);
194 if (tmpl_caps != NULL && gst_caps_is_fixed (tmpl_caps)) {
195 gst_pad_set_caps (mux->priv->srcpad, tmpl_caps);
196 }
197 } else {
198 g_warning ("GstTagMux subclass '%s' did not install a %s pad template!\n",
199 G_OBJECT_CLASS_NAME (element_klass), "source");
200 mux->priv->srcpad = gst_pad_new ("src", GST_PAD_SRC);
201 }
202 gst_element_add_pad (GST_ELEMENT (mux), mux->priv->srcpad);
203
204 mux->priv->render_start_tag = TRUE;
205 mux->priv->render_end_tag = TRUE;
206 }
207
208 static GstTagList *
gst_tag_mux_get_tags(GstTagMux * mux)209 gst_tag_mux_get_tags (GstTagMux * mux)
210 {
211 GstTagSetter *tagsetter = GST_TAG_SETTER (mux);
212 const GstTagList *tagsetter_tags;
213 GstTagMergeMode merge_mode;
214
215 if (mux->priv->final_tags)
216 return mux->priv->final_tags;
217
218 tagsetter_tags = gst_tag_setter_get_tag_list (tagsetter);
219 merge_mode = gst_tag_setter_get_tag_merge_mode (tagsetter);
220
221 GST_LOG_OBJECT (mux, "merging tags, merge mode = %d", merge_mode);
222 GST_LOG_OBJECT (mux, "event tags: %" GST_PTR_FORMAT, mux->priv->event_tags);
223 GST_LOG_OBJECT (mux, "set tags: %" GST_PTR_FORMAT, tagsetter_tags);
224
225 mux->priv->final_tags =
226 gst_tag_list_merge (tagsetter_tags, mux->priv->event_tags, merge_mode);
227
228 if (mux->priv->final_tags == NULL)
229 mux->priv->final_tags = gst_tag_list_new_empty ();
230
231 GST_LOG_OBJECT (mux, "final tags: %" GST_PTR_FORMAT, mux->priv->final_tags);
232
233 return mux->priv->final_tags;
234 }
235
236 static GstFlowReturn
gst_tag_mux_render_start_tag(GstTagMux * mux)237 gst_tag_mux_render_start_tag (GstTagMux * mux)
238 {
239 GstTagMuxClass *klass;
240 GstBuffer *buffer;
241 GstTagList *taglist;
242 GstEvent *event;
243 GstFlowReturn ret;
244 GstSegment segment;
245
246 taglist = gst_tag_mux_get_tags (mux);
247
248 klass = GST_TAG_MUX_CLASS (G_OBJECT_GET_CLASS (mux));
249
250 if (klass->render_start_tag == NULL)
251 goto no_vfunc;
252
253 buffer = klass->render_start_tag (mux, taglist);
254
255 /* Null buffer is ok, just means we're not outputting anything */
256 if (buffer == NULL) {
257 GST_INFO_OBJECT (mux, "No start tag generated");
258 mux->priv->start_tag_size = 0;
259 return GST_FLOW_OK;
260 }
261
262 mux->priv->start_tag_size = gst_buffer_get_size (buffer);
263 GST_LOG_OBJECT (mux, "tag size = %" G_GSIZE_FORMAT " bytes",
264 mux->priv->start_tag_size);
265
266 /* Send newsegment event from byte position 0, so the tag really gets
267 * written to the start of the file, independent of the upstream segment */
268 gst_segment_init (&segment, GST_FORMAT_BYTES);
269 gst_pad_push_event (mux->priv->srcpad, gst_event_new_segment (&segment));
270
271 /* Send an event about the new tags to downstream elements */
272 /* gst_event_new_tag takes ownership of the list, so use a copy */
273 event = gst_event_new_tag (gst_tag_list_ref (taglist));
274 gst_pad_push_event (mux->priv->srcpad, event);
275
276 GST_BUFFER_OFFSET (buffer) = 0;
277 ret = gst_pad_push (mux->priv->srcpad, buffer);
278
279 mux->priv->current_offset = mux->priv->start_tag_size;
280 mux->priv->max_offset =
281 MAX (mux->priv->max_offset, mux->priv->current_offset);
282
283 return ret;
284
285 no_vfunc:
286 {
287 GST_ERROR_OBJECT (mux, "Subclass does not implement "
288 "render_start_tag vfunc!");
289 return GST_FLOW_ERROR;
290 }
291 }
292
293 static GstFlowReturn
gst_tag_mux_render_end_tag(GstTagMux * mux)294 gst_tag_mux_render_end_tag (GstTagMux * mux)
295 {
296 GstTagMuxClass *klass;
297 GstBuffer *buffer;
298 GstTagList *taglist;
299 GstFlowReturn ret;
300 GstSegment segment;
301
302 taglist = gst_tag_mux_get_tags (mux);
303
304 klass = GST_TAG_MUX_CLASS (G_OBJECT_GET_CLASS (mux));
305
306 if (klass->render_end_tag == NULL)
307 goto no_vfunc;
308
309 buffer = klass->render_end_tag (mux, taglist);
310
311 if (buffer == NULL) {
312 GST_INFO_OBJECT (mux, "No end tag generated");
313 mux->priv->end_tag_size = 0;
314 return GST_FLOW_OK;
315 }
316
317 mux->priv->end_tag_size = gst_buffer_get_size (buffer);
318 GST_LOG_OBJECT (mux, "tag size = %" G_GSIZE_FORMAT " bytes",
319 mux->priv->end_tag_size);
320
321 /* Send newsegment event from the end of the file, so it gets written there,
322 independent of whatever new segment events upstream has sent us */
323 gst_segment_init (&segment, GST_FORMAT_BYTES);
324 segment.start = mux->priv->max_offset;
325 gst_pad_push_event (mux->priv->srcpad, gst_event_new_segment (&segment));
326
327 GST_BUFFER_OFFSET (buffer) = mux->priv->max_offset;
328 ret = gst_pad_push (mux->priv->srcpad, buffer);
329
330 return ret;
331
332 no_vfunc:
333 {
334 GST_ERROR_OBJECT (mux, "Subclass does not implement "
335 "render_end_tag vfunc!");
336 return GST_FLOW_ERROR;
337 }
338 }
339
340 static GstEvent *
gst_tag_mux_adjust_event_offsets(GstTagMux * mux,const GstEvent * newsegment_event)341 gst_tag_mux_adjust_event_offsets (GstTagMux * mux,
342 const GstEvent * newsegment_event)
343 {
344 GstSegment segment;
345
346 gst_event_copy_segment ((GstEvent *) newsegment_event, &segment);
347
348 g_assert (segment.format == GST_FORMAT_BYTES);
349
350 if (segment.start != -1)
351 segment.start += mux->priv->start_tag_size;
352 if (segment.stop != -1)
353 segment.stop += mux->priv->start_tag_size;
354 if (segment.time != -1)
355 segment.time += mux->priv->start_tag_size;
356
357 GST_DEBUG_OBJECT (mux, "adjusting newsegment event offsets to start=%"
358 G_GINT64_FORMAT ", stop=%" G_GINT64_FORMAT ", cur=%" G_GINT64_FORMAT
359 " (delta = +%" G_GSIZE_FORMAT ")", segment.start, segment.stop,
360 segment.time, mux->priv->start_tag_size);
361
362 return gst_event_new_segment (&segment);
363 }
364
365 static GstFlowReturn
gst_tag_mux_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)366 gst_tag_mux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
367 {
368 GstTagMux *mux = GST_TAG_MUX (parent);
369 GstFlowReturn ret;
370 int length;
371
372 if (mux->priv->render_start_tag) {
373
374 GST_INFO_OBJECT (mux, "Adding tags to stream");
375 ret = gst_tag_mux_render_start_tag (mux);
376 if (ret != GST_FLOW_OK) {
377 GST_DEBUG_OBJECT (mux, "flow: %s", gst_flow_get_name (ret));
378 gst_buffer_unref (buffer);
379 return ret;
380 }
381
382 /* Now send the cached newsegment event that we got from upstream */
383 if (mux->priv->newsegment_ev) {
384 GstEvent *newseg;
385 GstSegment segment;
386
387 GST_DEBUG_OBJECT (mux, "sending cached newsegment event");
388 newseg = gst_tag_mux_adjust_event_offsets (mux, mux->priv->newsegment_ev);
389 gst_event_unref (mux->priv->newsegment_ev);
390 mux->priv->newsegment_ev = NULL;
391
392 gst_event_copy_segment (newseg, &segment);
393
394 gst_pad_push_event (mux->priv->srcpad, newseg);
395 mux->priv->current_offset = segment.start;
396 mux->priv->max_offset =
397 MAX (mux->priv->max_offset, mux->priv->current_offset);
398 } else {
399 /* upstream sent no newsegment event or only one in a non-BYTE format */
400 }
401
402 mux->priv->render_start_tag = FALSE;
403 }
404
405 buffer = gst_buffer_make_writable (buffer);
406
407 if (GST_BUFFER_OFFSET (buffer) != GST_BUFFER_OFFSET_NONE) {
408 GST_LOG_OBJECT (mux, "Adjusting buffer offset from %" G_GINT64_FORMAT
409 " to %" G_GINT64_FORMAT, GST_BUFFER_OFFSET (buffer),
410 GST_BUFFER_OFFSET (buffer) + mux->priv->start_tag_size);
411 GST_BUFFER_OFFSET (buffer) += mux->priv->start_tag_size;
412 }
413
414 length = gst_buffer_get_size (buffer);
415
416 ret = gst_pad_push (mux->priv->srcpad, buffer);
417
418 mux->priv->current_offset += length;
419 mux->priv->max_offset =
420 MAX (mux->priv->max_offset, mux->priv->current_offset);
421
422 return ret;
423 }
424
425 static gboolean
gst_tag_mux_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)426 gst_tag_mux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
427 {
428 GstTagMux *mux;
429 gboolean result;
430
431 mux = GST_TAG_MUX (parent);
432 result = FALSE;
433
434 switch (GST_EVENT_TYPE (event)) {
435 case GST_EVENT_TAG:{
436 GstTagList *tags;
437
438 gst_event_parse_tag (event, &tags);
439
440 GST_INFO_OBJECT (mux, "Got tag event: %" GST_PTR_FORMAT, tags);
441
442 if (mux->priv->event_tags != NULL) {
443 gst_tag_list_insert (mux->priv->event_tags, tags,
444 GST_TAG_MERGE_REPLACE);
445 } else {
446 mux->priv->event_tags = gst_tag_list_copy (tags);
447 }
448
449 GST_INFO_OBJECT (mux, "Event tags are now: %" GST_PTR_FORMAT,
450 mux->priv->event_tags);
451
452 /* just drop the event, we'll push a new tag event in render_start_tag */
453 gst_event_unref (event);
454 result = TRUE;
455 break;
456 }
457 case GST_EVENT_SEGMENT:
458 {
459 GstSegment segment;
460
461 gst_event_copy_segment (event, &segment);
462
463 if (segment.format != GST_FORMAT_BYTES) {
464 GST_WARNING_OBJECT (mux, "dropping newsegment event in %s format",
465 gst_format_get_name (segment.format));
466 gst_event_unref (event);
467 /* drop it quietly, so it is not seen as a failure to push event,
468 * which will turn into failure to push data as it is sticky */
469 result = TRUE;
470 break;
471 }
472
473 if (mux->priv->render_start_tag) {
474 /* we have not rendered the tag yet, which means that we don't know
475 * how large it is going to be yet, so we can't adjust the offsets
476 * here at this point and need to cache the newsegment event for now
477 * (also, there could be tag events coming after this newsegment event
478 * and before the first buffer). */
479 if (mux->priv->newsegment_ev) {
480 GST_WARNING_OBJECT (mux, "discarding old cached newsegment event");
481 gst_event_unref (mux->priv->newsegment_ev);
482 }
483
484 GST_LOG_OBJECT (mux, "caching newsegment event for later");
485 mux->priv->newsegment_ev = event;
486 } else {
487 GST_DEBUG_OBJECT (mux, "got newsegment event, adjusting offsets");
488 gst_pad_push_event (mux->priv->srcpad,
489 gst_tag_mux_adjust_event_offsets (mux, event));
490 gst_event_unref (event);
491
492 mux->priv->current_offset = segment.start;
493 mux->priv->max_offset =
494 MAX (mux->priv->max_offset, mux->priv->current_offset);
495 }
496 event = NULL;
497 result = TRUE;
498 break;
499 }
500 case GST_EVENT_EOS:{
501 if (mux->priv->render_end_tag) {
502 GstFlowReturn ret;
503
504 GST_INFO_OBJECT (mux, "Adding tags to stream");
505 ret = gst_tag_mux_render_end_tag (mux);
506 if (ret != GST_FLOW_OK) {
507 GST_DEBUG_OBJECT (mux, "flow: %s", gst_flow_get_name (ret));
508 return ret;
509 }
510
511 mux->priv->render_end_tag = FALSE;
512 }
513
514 /* Now forward EOS */
515 result = gst_pad_event_default (pad, parent, event);
516 break;
517 }
518 default:
519 result = gst_pad_event_default (pad, parent, event);
520 break;
521 }
522
523 return result;
524 }
525
526
527 static GstStateChangeReturn
gst_tag_mux_change_state(GstElement * element,GstStateChange transition)528 gst_tag_mux_change_state (GstElement * element, GstStateChange transition)
529 {
530 GstTagMux *mux;
531 GstStateChangeReturn result;
532
533 mux = GST_TAG_MUX (element);
534
535 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
536 if (result != GST_STATE_CHANGE_SUCCESS) {
537 return result;
538 }
539
540 switch (transition) {
541 case GST_STATE_CHANGE_PAUSED_TO_READY:{
542 if (mux->priv->newsegment_ev) {
543 gst_event_unref (mux->priv->newsegment_ev);
544 mux->priv->newsegment_ev = NULL;
545 }
546 if (mux->priv->event_tags) {
547 gst_tag_list_unref (mux->priv->event_tags);
548 mux->priv->event_tags = NULL;
549 }
550 if (mux->priv->final_tags) {
551 gst_tag_list_unref (mux->priv->final_tags);
552 mux->priv->final_tags = NULL;
553 }
554 mux->priv->start_tag_size = 0;
555 mux->priv->end_tag_size = 0;
556 mux->priv->render_start_tag = TRUE;
557 mux->priv->render_end_tag = TRUE;
558 mux->priv->current_offset = 0;
559 mux->priv->max_offset = 0;
560 break;
561 }
562 default:
563 break;
564 }
565
566 return result;
567 }
568