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>
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.
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.
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.
25 * @see_also: GstApeMux, GstId3Mux
26 * @short_description: Base class for adding tags that are in one single chunk
27 * directly at the beginning or at the end of a file
31 * Provides a base class for adding tags at the beginning or end of a
34 * <title>Deriving from GstTagMux</title>
36 * Subclasses have to do the following things:
39 * In their base init function, they must add pad templates for the sink
40 * pad and the source pad to the element class, describing the media type
41 * they accept and output in the caps of the pad template.
44 * In their class init function, they must override the
45 * GST_TAG_MUX_CLASS(mux_klass)->render_start_tag and/or
46 * GST_TAG_MUX_CLASS(mux_klass)->render_end_tag vfuncs and set up a render
58 #include <gst/gsttagsetter.h>
59 #include <gst/tag/tag.h>
61 #include "gsttagmux.h"
63 struct _GstTagMuxPrivate
67 GstTagList *event_tags; /* tags received from upstream elements */
68 GstTagList *final_tags; /* Final set of tags used for muxing */
71 gboolean render_start_tag;
72 gboolean render_end_tag;
74 gint64 current_offset;
77 GstEvent *newsegment_ev; /* cached newsegment event from upstream */
80 GST_DEBUG_CATEGORY_STATIC (gst_tag_mux_debug);
81 #define GST_CAT_DEFAULT gst_tag_mux_debug
83 static GstElementClass *parent_class;
85 static void gst_tag_mux_class_init (GstTagMuxClass * klass);
86 static void gst_tag_mux_init (GstTagMux * mux, GstTagMuxClass * mux_class);
87 static GstStateChangeReturn
88 gst_tag_mux_change_state (GstElement * element, GstStateChange transition);
89 static GstFlowReturn gst_tag_mux_chain (GstPad * pad, GstObject * parent,
91 static gboolean gst_tag_mux_sink_event (GstPad * pad, GstObject * parent,
94 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
95 * method to get to the padtemplates */
97 gst_tag_mux_get_type (void)
99 static volatile gsize tag_mux_type = 0;
101 if (g_once_init_enter (&tag_mux_type)) {
102 const GInterfaceInfo interface_info = { NULL, NULL, NULL };
105 _type = g_type_register_static_simple (GST_TYPE_ELEMENT,
106 "GstTagMux", sizeof (GstTagMuxClass),
107 (GClassInitFunc) gst_tag_mux_class_init, sizeof (GstTagMux),
108 (GInstanceInitFunc) gst_tag_mux_init, G_TYPE_FLAG_ABSTRACT);
110 g_type_add_interface_static (_type, GST_TYPE_TAG_SETTER, &interface_info);
112 g_once_init_leave (&tag_mux_type, _type);
118 gst_tag_mux_finalize (GObject * obj)
120 GstTagMux *mux = GST_TAG_MUX (obj);
122 if (mux->priv->newsegment_ev) {
123 gst_event_unref (mux->priv->newsegment_ev);
124 mux->priv->newsegment_ev = NULL;
127 if (mux->priv->event_tags) {
128 gst_tag_list_unref (mux->priv->event_tags);
129 mux->priv->event_tags = NULL;
132 if (mux->priv->final_tags) {
133 gst_tag_list_unref (mux->priv->final_tags);
134 mux->priv->final_tags = NULL;
137 G_OBJECT_CLASS (parent_class)->finalize (obj);
141 gst_tag_mux_class_init (GstTagMuxClass * klass)
143 GObjectClass *gobject_class;
144 GstElementClass *gstelement_class;
146 gobject_class = (GObjectClass *) klass;
147 gstelement_class = (GstElementClass *) klass;
149 parent_class = g_type_class_peek_parent (klass);
151 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_tag_mux_finalize);
152 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_tag_mux_change_state);
154 g_type_class_add_private (klass, sizeof (GstTagMuxPrivate));
156 GST_DEBUG_CATEGORY_INIT (gst_tag_mux_debug, "tagmux", 0,
157 "tag muxer base class");
161 gst_tag_mux_init (GstTagMux * mux, GstTagMuxClass * mux_class)
163 GstElementClass *element_klass = GST_ELEMENT_CLASS (mux_class);
164 GstPadTemplate *tmpl;
167 G_TYPE_INSTANCE_GET_PRIVATE (mux, GST_TYPE_TAG_MUX, GstTagMuxPrivate);
169 /* pad through which data comes in to the element */
170 tmpl = gst_element_class_get_pad_template (element_klass, "sink");
172 mux->priv->sinkpad = gst_pad_new_from_template (tmpl, "sink");
174 g_warning ("GstTagMux subclass '%s' did not install a %s pad template!\n",
175 G_OBJECT_CLASS_NAME (element_klass), "sink");
176 mux->priv->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
178 gst_pad_set_chain_function (mux->priv->sinkpad,
179 GST_DEBUG_FUNCPTR (gst_tag_mux_chain));
180 gst_pad_set_event_function (mux->priv->sinkpad,
181 GST_DEBUG_FUNCPTR (gst_tag_mux_sink_event));
182 gst_element_add_pad (GST_ELEMENT (mux), mux->priv->sinkpad);
184 /* pad through which data goes out of the element */
185 tmpl = gst_element_class_get_pad_template (element_klass, "src");
187 GstCaps *tmpl_caps = gst_pad_template_get_caps (tmpl);
189 mux->priv->srcpad = gst_pad_new_from_template (tmpl, "src");
190 gst_pad_use_fixed_caps (mux->priv->srcpad);
191 if (tmpl_caps != NULL && gst_caps_is_fixed (tmpl_caps)) {
192 gst_pad_set_caps (mux->priv->srcpad, tmpl_caps);
195 g_warning ("GstTagMux subclass '%s' did not install a %s pad template!\n",
196 G_OBJECT_CLASS_NAME (element_klass), "source");
197 mux->priv->srcpad = gst_pad_new ("src", GST_PAD_SRC);
199 gst_element_add_pad (GST_ELEMENT (mux), mux->priv->srcpad);
201 mux->priv->render_start_tag = TRUE;
202 mux->priv->render_end_tag = TRUE;
206 gst_tag_mux_get_tags (GstTagMux * mux)
208 GstTagSetter *tagsetter = GST_TAG_SETTER (mux);
209 const GstTagList *tagsetter_tags;
210 GstTagMergeMode merge_mode;
212 if (mux->priv->final_tags)
213 return mux->priv->final_tags;
215 tagsetter_tags = gst_tag_setter_get_tag_list (tagsetter);
216 merge_mode = gst_tag_setter_get_tag_merge_mode (tagsetter);
218 GST_LOG_OBJECT (mux, "merging tags, merge mode = %d", merge_mode);
219 GST_LOG_OBJECT (mux, "event tags: %" GST_PTR_FORMAT, mux->priv->event_tags);
220 GST_LOG_OBJECT (mux, "set tags: %" GST_PTR_FORMAT, tagsetter_tags);
222 mux->priv->final_tags =
223 gst_tag_list_merge (tagsetter_tags, mux->priv->event_tags, merge_mode);
225 GST_LOG_OBJECT (mux, "final tags: %" GST_PTR_FORMAT, mux->priv->final_tags);
227 return mux->priv->final_tags;
231 gst_tag_mux_render_start_tag (GstTagMux * mux)
233 GstTagMuxClass *klass;
240 taglist = gst_tag_mux_get_tags (mux);
242 klass = GST_TAG_MUX_CLASS (G_OBJECT_GET_CLASS (mux));
244 if (klass->render_start_tag == NULL)
247 buffer = klass->render_start_tag (mux, taglist);
249 /* Null buffer is ok, just means we're not outputting anything */
250 if (buffer == NULL) {
251 GST_INFO_OBJECT (mux, "No start tag generated");
252 mux->priv->start_tag_size = 0;
256 mux->priv->start_tag_size = gst_buffer_get_size (buffer);
257 GST_LOG_OBJECT (mux, "tag size = %" G_GSIZE_FORMAT " bytes",
258 mux->priv->start_tag_size);
260 /* Send newsegment event from byte position 0, so the tag really gets
261 * written to the start of the file, independent of the upstream segment */
262 gst_segment_init (&segment, GST_FORMAT_BYTES);
263 gst_pad_push_event (mux->priv->srcpad, gst_event_new_segment (&segment));
265 /* Send an event about the new tags to downstream elements */
266 /* gst_event_new_tag takes ownership of the list, so use a copy */
267 event = gst_event_new_tag (gst_tag_list_ref (taglist));
268 gst_pad_push_event (mux->priv->srcpad, event);
270 GST_BUFFER_OFFSET (buffer) = 0;
271 ret = gst_pad_push (mux->priv->srcpad, buffer);
273 mux->priv->current_offset = mux->priv->start_tag_size;
274 mux->priv->max_offset =
275 MAX (mux->priv->max_offset, mux->priv->current_offset);
281 GST_ERROR_OBJECT (mux, "Subclass does not implement "
282 "render_start_tag vfunc!");
283 return GST_FLOW_ERROR;
288 gst_tag_mux_render_end_tag (GstTagMux * mux)
290 GstTagMuxClass *klass;
296 taglist = gst_tag_mux_get_tags (mux);
298 klass = GST_TAG_MUX_CLASS (G_OBJECT_GET_CLASS (mux));
300 if (klass->render_end_tag == NULL)
303 buffer = klass->render_end_tag (mux, taglist);
305 if (buffer == NULL) {
306 GST_INFO_OBJECT (mux, "No end tag generated");
307 mux->priv->end_tag_size = 0;
311 mux->priv->end_tag_size = gst_buffer_get_size (buffer);
312 GST_LOG_OBJECT (mux, "tag size = %" G_GSIZE_FORMAT " bytes",
313 mux->priv->end_tag_size);
315 /* Send newsegment event from the end of the file, so it gets written there,
316 independent of whatever new segment events upstream has sent us */
317 gst_segment_init (&segment, GST_FORMAT_BYTES);
318 segment.start = mux->priv->max_offset;
319 gst_pad_push_event (mux->priv->srcpad, gst_event_new_segment (&segment));
321 GST_BUFFER_OFFSET (buffer) = mux->priv->max_offset;
322 ret = gst_pad_push (mux->priv->srcpad, buffer);
328 GST_ERROR_OBJECT (mux, "Subclass does not implement "
329 "render_end_tag vfunc!");
330 return GST_FLOW_ERROR;
335 gst_tag_mux_adjust_event_offsets (GstTagMux * mux,
336 const GstEvent * newsegment_event)
340 gst_event_copy_segment ((GstEvent *) newsegment_event, &segment);
342 g_assert (segment.format == GST_FORMAT_BYTES);
344 if (segment.start != -1)
345 segment.start += mux->priv->start_tag_size;
346 if (segment.stop != -1)
347 segment.stop += mux->priv->start_tag_size;
348 if (segment.time != -1)
349 segment.time += mux->priv->start_tag_size;
351 GST_DEBUG_OBJECT (mux, "adjusting newsegment event offsets to start=%"
352 G_GINT64_FORMAT ", stop=%" G_GINT64_FORMAT ", cur=%" G_GINT64_FORMAT
353 " (delta = +%" G_GSIZE_FORMAT ")", segment.start, segment.stop,
354 segment.time, mux->priv->start_tag_size);
356 return gst_event_new_segment (&segment);
360 gst_tag_mux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
362 GstTagMux *mux = GST_TAG_MUX (parent);
366 if (mux->priv->render_start_tag) {
368 GST_INFO_OBJECT (mux, "Adding tags to stream");
369 ret = gst_tag_mux_render_start_tag (mux);
370 if (ret != GST_FLOW_OK) {
371 GST_DEBUG_OBJECT (mux, "flow: %s", gst_flow_get_name (ret));
372 gst_buffer_unref (buffer);
376 /* Now send the cached newsegment event that we got from upstream */
377 if (mux->priv->newsegment_ev) {
381 GST_DEBUG_OBJECT (mux, "sending cached newsegment event");
382 newseg = gst_tag_mux_adjust_event_offsets (mux, mux->priv->newsegment_ev);
383 gst_event_unref (mux->priv->newsegment_ev);
384 mux->priv->newsegment_ev = NULL;
386 gst_event_copy_segment (newseg, &segment);
388 gst_pad_push_event (mux->priv->srcpad, newseg);
389 mux->priv->current_offset = segment.start;
390 mux->priv->max_offset =
391 MAX (mux->priv->max_offset, mux->priv->current_offset);
393 /* upstream sent no newsegment event or only one in a non-BYTE format */
396 mux->priv->render_start_tag = FALSE;
399 buffer = gst_buffer_make_writable (buffer);
401 if (GST_BUFFER_OFFSET (buffer) != GST_BUFFER_OFFSET_NONE) {
402 GST_LOG_OBJECT (mux, "Adjusting buffer offset from %" G_GINT64_FORMAT
403 " to %" G_GINT64_FORMAT, GST_BUFFER_OFFSET (buffer),
404 GST_BUFFER_OFFSET (buffer) + mux->priv->start_tag_size);
405 GST_BUFFER_OFFSET (buffer) += mux->priv->start_tag_size;
408 length = gst_buffer_get_size (buffer);
410 ret = gst_pad_push (mux->priv->srcpad, buffer);
412 mux->priv->current_offset += length;
413 mux->priv->max_offset =
414 MAX (mux->priv->max_offset, mux->priv->current_offset);
420 gst_tag_mux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
425 mux = GST_TAG_MUX (parent);
428 switch (GST_EVENT_TYPE (event)) {
432 gst_event_parse_tag (event, &tags);
434 GST_INFO_OBJECT (mux, "Got tag event: %" GST_PTR_FORMAT, tags);
436 if (mux->priv->event_tags != NULL) {
437 gst_tag_list_insert (mux->priv->event_tags, tags,
438 GST_TAG_MERGE_REPLACE);
440 mux->priv->event_tags = gst_tag_list_copy (tags);
443 GST_INFO_OBJECT (mux, "Event tags are now: %" GST_PTR_FORMAT,
444 mux->priv->event_tags);
446 /* just drop the event, we'll push a new tag event in render_start_tag */
447 gst_event_unref (event);
451 case GST_EVENT_SEGMENT:
455 gst_event_copy_segment (event, &segment);
457 if (segment.format != GST_FORMAT_BYTES) {
458 GST_WARNING_OBJECT (mux, "dropping newsegment event in %s format",
459 gst_format_get_name (segment.format));
460 gst_event_unref (event);
461 /* drop it quietly, so it is not seen as a failure to push event,
462 * which will turn into failure to push data as it is sticky */
467 if (mux->priv->render_start_tag) {
468 /* we have not rendered the tag yet, which means that we don't know
469 * how large it is going to be yet, so we can't adjust the offsets
470 * here at this point and need to cache the newsegment event for now
471 * (also, there could be tag events coming after this newsegment event
472 * and before the first buffer). */
473 if (mux->priv->newsegment_ev) {
474 GST_WARNING_OBJECT (mux, "discarding old cached newsegment event");
475 gst_event_unref (mux->priv->newsegment_ev);
478 GST_LOG_OBJECT (mux, "caching newsegment event for later");
479 mux->priv->newsegment_ev = event;
481 GST_DEBUG_OBJECT (mux, "got newsegment event, adjusting offsets");
482 gst_pad_push_event (mux->priv->srcpad,
483 gst_tag_mux_adjust_event_offsets (mux, event));
484 gst_event_unref (event);
486 mux->priv->current_offset = segment.start;
487 mux->priv->max_offset =
488 MAX (mux->priv->max_offset, mux->priv->current_offset);
495 if (mux->priv->render_end_tag) {
498 GST_INFO_OBJECT (mux, "Adding tags to stream");
499 ret = gst_tag_mux_render_end_tag (mux);
500 if (ret != GST_FLOW_OK) {
501 GST_DEBUG_OBJECT (mux, "flow: %s", gst_flow_get_name (ret));
505 mux->priv->render_end_tag = FALSE;
508 /* Now forward EOS */
509 result = gst_pad_event_default (pad, parent, event);
513 result = gst_pad_event_default (pad, parent, event);
521 static GstStateChangeReturn
522 gst_tag_mux_change_state (GstElement * element, GstStateChange transition)
525 GstStateChangeReturn result;
527 mux = GST_TAG_MUX (element);
529 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
530 if (result != GST_STATE_CHANGE_SUCCESS) {
534 switch (transition) {
535 case GST_STATE_CHANGE_PAUSED_TO_READY:{
536 if (mux->priv->newsegment_ev) {
537 gst_event_unref (mux->priv->newsegment_ev);
538 mux->priv->newsegment_ev = NULL;
540 if (mux->priv->event_tags) {
541 gst_tag_list_unref (mux->priv->event_tags);
542 mux->priv->event_tags = NULL;
544 mux->priv->start_tag_size = 0;
545 mux->priv->end_tag_size = 0;
546 mux->priv->render_start_tag = TRUE;
547 mux->priv->render_end_tag = TRUE;
548 mux->priv->current_offset = 0;
549 mux->priv->max_offset = 0;