1 /* GStreamer Base Class for Tag Demuxing
2 * Copyright (C) 2005 Jan Schmidt <thaytan@mad.scientist.com>
3 * Copyright (C) 2006-2007 Tim-Philipp Müller <tim centricular net>
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.
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.
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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 * @see_also: GstApeDemux, GstID3Demux
24 * @short_description: Base class for demuxing tags that are in chunks
25 * directly at the beginning or at the end of a file
29 * Provides a base class for demuxing tags at the beginning or end of a
30 * stream and handles things like typefinding, querying, seeking, and
31 * different modes of operation (chain-based, pull_range-based, and providing
32 * downstream elements with random access if upstream supports that). The tag
33 * is stripped from the output, and all offsets are adjusted for the tag
34 * sizes, so that to the downstream element the stream will appear as if
35 * there was no tag at all. Also, once the tag has been parsed, GstTagDemux
36 * will try to determine the media type of the resulting stream and add a
37 * source pad with the appropriate caps in order to facilitate auto-plugging.
39 * <title>Deriving from GstTagDemux</title>
41 * Subclasses have to do four things:
44 * In their base init function, they must add a pad template for the sink
45 * pad to the element class, describing the media type they can parse in
46 * the caps of the pad template.
49 * In their class init function, they must override
50 * GST_TAG_DEMUX_CLASS(demux_klass)->identify_tag with their own identify
54 * In their class init function, they must override
55 * GST_TAG_DEMUX_CLASS(demux_klass)->parse_tag with their own parse
59 * In their class init function, they must also set
60 * GST_TAG_DEMUX_CLASS(demux_klass)->min_start_size and/or
61 * GST_TAG_DEMUX_CLASS(demux_klass)->min_end_size to the minimum size required
62 * for the identify function to decide whether the stream has a supported tag
63 * or not. A class parsing ID3v1 tags, for example, would set min_end_size to
75 #include "gsttagdemux.h"
77 #include <gst/base/gsttypefindhelper.h>
78 #include <gst/gst-i18n-plugin.h>
83 GST_TAG_DEMUX_READ_START_TAG,
84 GST_TAG_DEMUX_TYPEFINDING,
85 GST_TAG_DEMUX_STREAMING
88 struct _GstTagDemuxPrivate
93 /* Number of bytes to remove from the
94 * start of file (tag at beginning) */
97 /* Number of bytes to remove from the
98 * end of file (tag at end) */
101 gint64 upstream_size;
103 GstTagDemuxState state;
108 GstTagList *event_tags;
109 GstTagList *parsed_tags;
110 gboolean send_tag_event;
113 gboolean need_newseg;
117 GList *pending_events;
120 /* Require at least 8kB of data before we attempt typefind.
121 * Seems a decent value based on test files
122 * 40kB is massive overkill for the maximum, I think, but it
123 * doesn't do any harm (tpm: increased to 64kB after watching
124 * typefinding fail on a wavpack file that needed 42kB to succeed) */
125 #define TYPE_FIND_MIN_SIZE 8192
126 #define TYPE_FIND_MAX_SIZE 65536
128 #define DEFAULT_PULL_BLOCKSIZE 4096
130 GST_DEBUG_CATEGORY_STATIC (tagdemux_debug);
131 #define GST_CAT_DEFAULT (tagdemux_debug)
133 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
136 GST_STATIC_CAPS ("ANY")
139 static void gst_tag_demux_dispose (GObject * object);
141 static GstFlowReturn gst_tag_demux_chain (GstPad * pad, GstObject * parent,
143 static gboolean gst_tag_demux_sink_event (GstPad * pad, GstObject * parent,
146 static gboolean gst_tag_demux_sink_activate_mode (GstPad * pad,
147 GstObject * parent, GstPadMode mode, gboolean active);
148 static gboolean gst_tag_demux_src_activate_mode (GstPad * pad,
149 GstObject * parent, GstPadMode mode, gboolean active);
150 static GstFlowReturn gst_tag_demux_read_range (GstTagDemux * tagdemux,
151 GstObject * parent, guint64 offset, guint length, GstBuffer ** buffer);
153 static GstFlowReturn gst_tag_demux_src_getrange (GstPad * srcpad,
154 GstObject * parent, guint64 offset, guint length, GstBuffer ** buffer);
156 static void gst_tag_demux_set_src_caps (GstTagDemux * tagdemux,
159 static gboolean gst_tag_demux_srcpad_event (GstPad * pad, GstObject * parent,
161 static gboolean gst_tag_demux_sink_activate (GstPad * sinkpad,
163 static GstStateChangeReturn gst_tag_demux_change_state (GstElement * element,
164 GstStateChange transition);
165 static gboolean gst_tag_demux_pad_query (GstPad * pad, GstObject * parent,
167 static gboolean gst_tag_demux_get_upstream_size (GstTagDemux * tagdemux);
168 static void gst_tag_demux_send_pending_events (GstTagDemux * tagdemux);
169 static void gst_tag_demux_send_tag_event (GstTagDemux * tagdemux);
170 static gboolean gst_tag_demux_send_new_segment (GstTagDemux * tagdemux);
172 static void gst_tag_demux_base_init (gpointer g_class);
173 static void gst_tag_demux_class_init (gpointer g_class, gpointer d);
174 static void gst_tag_demux_init (GstTagDemux * obj, GstTagDemuxClass * klass);
176 static gpointer parent_class; /* NULL */
179 gst_tag_demux_result_get_type (void)
181 static GType etype = 0;
183 static const GEnumValue values[] = {
184 {GST_TAG_DEMUX_RESULT_BROKEN_TAG, "GST_TAG_DEMUX_RESULT_BROKEN_TAG",
186 {GST_TAG_DEMUX_RESULT_AGAIN, "GST_TAG_DEMUX_RESULT_AGAIN", "again"},
187 {GST_TAG_DEMUX_RESULT_OK, "GST_TAG_DEMUX_RESULT_OK", "ok"},
190 etype = g_enum_register_static ("GstTagDemuxResult", values);
195 /* Cannot use boilerplate macros here because we want the abstract flag */
197 gst_tag_demux_get_type (void)
199 static GType object_type; /* 0 */
201 if (object_type == 0) {
202 static const GTypeInfo object_info = {
203 sizeof (GstTagDemuxClass),
204 gst_tag_demux_base_init,
205 NULL, /* base_finalize */
206 gst_tag_demux_class_init,
207 NULL, /* class_finalize */
208 NULL, /* class_data */
209 sizeof (GstTagDemux),
211 (GInstanceInitFunc) gst_tag_demux_init
214 object_type = g_type_register_static (GST_TYPE_ELEMENT,
215 "GstTagDemux", &object_info, G_TYPE_FLAG_ABSTRACT);
222 gst_tag_demux_base_init (gpointer klass)
224 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
226 gst_element_class_add_pad_template (element_class,
227 gst_static_pad_template_get (&src_factory));
229 GST_DEBUG_CATEGORY_INIT (tagdemux_debug, "tagdemux", 0,
230 "tag demux base class");
234 gst_tag_demux_class_init (gpointer klass, gpointer d)
236 GstTagDemuxClass *tagdemux_class = GST_TAG_DEMUX_CLASS (klass);
237 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
238 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
240 parent_class = g_type_class_peek_parent (klass);
242 gobject_class->dispose = gst_tag_demux_dispose;
244 element_class->change_state = GST_DEBUG_FUNCPTR (gst_tag_demux_change_state);
246 g_type_class_add_private (klass, sizeof (GstTagDemuxPrivate));
248 /* subclasses must set at least one of these */
249 tagdemux_class->min_start_size = 0;
250 tagdemux_class->min_end_size = 0;
254 gst_tag_demux_reset (GstTagDemux * tagdemux)
256 GstBuffer **buffer_p = &tagdemux->priv->collect;
257 GstCaps **caps_p = &tagdemux->priv->src_caps;
259 tagdemux->priv->strip_start = 0;
260 tagdemux->priv->strip_end = 0;
261 tagdemux->priv->upstream_size = -1;
262 tagdemux->priv->state = GST_TAG_DEMUX_READ_START_TAG;
263 tagdemux->priv->send_tag_event = FALSE;
265 gst_buffer_replace (buffer_p, NULL);
266 tagdemux->priv->collect_size = 0;
267 gst_caps_replace (caps_p, NULL);
269 if (tagdemux->priv->event_tags) {
270 gst_tag_list_unref (tagdemux->priv->event_tags);
271 tagdemux->priv->event_tags = NULL;
273 if (tagdemux->priv->parsed_tags) {
274 gst_tag_list_unref (tagdemux->priv->parsed_tags);
275 tagdemux->priv->parsed_tags = NULL;
278 gst_segment_init (&tagdemux->priv->segment, GST_FORMAT_UNDEFINED);
279 tagdemux->priv->need_newseg = TRUE;
281 g_list_foreach (tagdemux->priv->pending_events,
282 (GFunc) gst_mini_object_unref, NULL);
283 g_list_free (tagdemux->priv->pending_events);
284 tagdemux->priv->pending_events = NULL;
288 gst_tag_demux_init (GstTagDemux * demux, GstTagDemuxClass * gclass)
290 GstElementClass *element_klass = GST_ELEMENT_CLASS (gclass);
291 GstPadTemplate *tmpl;
293 demux->priv = g_type_instance_get_private ((GTypeInstance *) demux,
297 tmpl = gst_element_class_get_pad_template (element_klass, "sink");
299 demux->priv->sinkpad = gst_pad_new_from_template (tmpl, "sink");
301 gst_pad_set_activatemode_function (demux->priv->sinkpad,
302 GST_DEBUG_FUNCPTR (gst_tag_demux_sink_activate_mode));
303 gst_pad_set_activate_function (demux->priv->sinkpad,
304 GST_DEBUG_FUNCPTR (gst_tag_demux_sink_activate));
305 gst_pad_set_event_function (demux->priv->sinkpad,
306 GST_DEBUG_FUNCPTR (gst_tag_demux_sink_event));
307 gst_pad_set_chain_function (demux->priv->sinkpad,
308 GST_DEBUG_FUNCPTR (gst_tag_demux_chain));
309 gst_element_add_pad (GST_ELEMENT (demux), demux->priv->sinkpad);
311 g_warning ("GstTagDemux subclass %s must provide a sink pad template",
312 G_OBJECT_TYPE_NAME (demux));
316 tmpl = gst_element_class_get_pad_template (element_klass, "src");
317 demux->priv->srcpad = gst_pad_new_from_template (tmpl, "src");
318 gst_pad_set_query_function (demux->priv->srcpad,
319 GST_DEBUG_FUNCPTR (gst_tag_demux_pad_query));
320 gst_pad_set_event_function (demux->priv->srcpad,
321 GST_DEBUG_FUNCPTR (gst_tag_demux_srcpad_event));
322 gst_pad_set_activatemode_function (demux->priv->srcpad,
323 GST_DEBUG_FUNCPTR (gst_tag_demux_src_activate_mode));
324 gst_pad_set_getrange_function (demux->priv->srcpad,
325 GST_DEBUG_FUNCPTR (gst_tag_demux_src_getrange));
326 gst_pad_use_fixed_caps (demux->priv->srcpad);
327 gst_element_add_pad (GST_ELEMENT (demux), demux->priv->srcpad);
329 gst_tag_demux_reset (demux);
333 gst_tag_demux_dispose (GObject * object)
335 GstTagDemux *tagdemux = GST_TAG_DEMUX (object);
337 gst_tag_demux_reset (tagdemux);
339 G_OBJECT_CLASS (parent_class)->dispose (object);
342 // FIXME: convert to set_caps / sending a caps event
344 gst_tag_demux_set_src_caps (GstTagDemux * tagdemux, GstCaps * new_caps)
346 GstCaps *old_caps = tagdemux->priv->src_caps;
348 if (old_caps == NULL || !gst_caps_is_equal (new_caps, old_caps)) {
349 gchar *stream_id = gst_pad_create_stream_id (tagdemux->priv->srcpad,
350 GST_ELEMENT_CAST (tagdemux), NULL);
352 gst_caps_replace (&tagdemux->priv->src_caps, new_caps);
354 GST_DEBUG_OBJECT (tagdemux, "Changing src pad caps to %" GST_PTR_FORMAT,
355 tagdemux->priv->src_caps);
357 gst_pad_push_event (tagdemux->priv->srcpad,
358 gst_event_new_stream_start (stream_id));
360 gst_pad_set_caps (tagdemux->priv->srcpad, tagdemux->priv->src_caps);
362 /* Caps never changed */
366 /* will return FALSE if buffer is beyond end of data; will return TRUE
367 * if buffer was trimmed successfully or didn't need trimming, but may
368 * also return TRUE and set *buf_ref to NULL if the buffer was before
369 * the start of the data */
371 gst_tag_demux_trim_buffer (GstTagDemux * tagdemux, GstBuffer ** buf_ref,
374 GstBuffer *buf = *buf_ref;
376 guint trim_start = 0;
377 guint out_size, bsize;
378 guint64 out_offset, boffset;
379 gboolean need_sub = FALSE;
381 bsize = out_size = gst_buffer_get_size (buf);
382 boffset = out_offset = GST_BUFFER_OFFSET (buf);
384 /* Adjust offset and length */
385 if (!GST_BUFFER_OFFSET_IS_VALID (buf)) {
386 /* Can't change anything without an offset */
391 /* If the buffer crosses the tag at the end of file, trim it */
392 if (tagdemux->priv->strip_end > 0) {
393 if (gst_tag_demux_get_upstream_size (tagdemux)) {
394 guint64 v1tag_offset =
395 tagdemux->priv->upstream_size - tagdemux->priv->strip_end;
397 if (out_offset >= v1tag_offset) {
398 GST_DEBUG_OBJECT (tagdemux, "Buffer is past the end of the data");
399 goto no_out_buffer_end;
402 if (out_offset + out_size > v1tag_offset) {
403 out_size = v1tag_offset - out_offset;
409 if (tagdemux->priv->strip_start > 0) {
410 /* If the buffer crosses the tag at the start of file, trim it */
411 if (out_offset <= tagdemux->priv->strip_start) {
412 if (out_offset + out_size <= tagdemux->priv->strip_start) {
413 GST_DEBUG_OBJECT (tagdemux, "Buffer is before the start of the data");
414 goto no_out_buffer_start;
417 trim_start = tagdemux->priv->strip_start - out_offset;
418 out_size -= trim_start;
421 out_offset -= tagdemux->priv->strip_start;
426 if (need_sub == TRUE) {
427 if (out_size != bsize || !gst_buffer_is_writable (buf)) {
430 GST_DEBUG_OBJECT (tagdemux, "Sub-buffering to trim size %d offset %"
431 G_GINT64_FORMAT " to %d offset %" G_GINT64_FORMAT,
432 bsize, boffset, out_size, out_offset);
435 gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, trim_start,
437 g_return_val_if_fail (sub != NULL, FALSE);
438 gst_buffer_unref (buf);
439 *buf_ref = buf = sub;
440 *buf_size = out_size;
442 GST_DEBUG_OBJECT (tagdemux, "Adjusting buffer from size %d offset %"
443 G_GINT64_FORMAT " to %d offset %" G_GINT64_FORMAT,
444 bsize, boffset, out_size, out_offset);
447 GST_BUFFER_OFFSET (buf) = out_offset;
448 GST_BUFFER_OFFSET_END (buf) = out_offset + out_size;
455 gst_buffer_unref (buf);
461 gst_buffer_unref (buf);
468 gst_tag_demux_chain_parse_tag (GstTagDemux * demux, GstBuffer * collect)
470 GstTagDemuxResult parse_ret;
471 GstTagDemuxClass *klass;
475 g_assert (gst_buffer_is_writable (collect));
477 klass = GST_TAG_DEMUX_CLASS (G_OBJECT_GET_CLASS (demux));
479 /* If we receive a buffer that's from the middle of the file,
480 * we can't read tags so move to typefinding */
481 if (GST_BUFFER_OFFSET_IS_VALID (collect) && GST_BUFFER_OFFSET (collect) != 0) {
482 GST_DEBUG_OBJECT (demux, "Received buffer from non-zero offset %"
483 G_GINT64_FORMAT ". Can't read tags", GST_BUFFER_OFFSET (collect));
484 demux->priv->state = GST_TAG_DEMUX_TYPEFINDING;
488 g_assert (klass->identify_tag != NULL);
489 g_assert (klass->parse_tag != NULL);
491 available = gst_buffer_get_size (collect);
493 if (available < klass->min_start_size) {
494 GST_DEBUG_OBJECT (demux, "Only %u bytes available, but %u needed "
495 "to identify tag", available, klass->min_start_size);
496 return; /* wait for more data */
499 if (!klass->identify_tag (demux, collect, TRUE, &tagsize)) {
500 GST_DEBUG_OBJECT (demux, "Could not identify start tag");
501 demux->priv->state = GST_TAG_DEMUX_TYPEFINDING;
505 /* need to set offset of first buffer to 0 or trimming won't work */
506 if (!GST_BUFFER_OFFSET_IS_VALID (collect)) {
507 GST_WARNING_OBJECT (demux, "Fixing up first buffer without offset");
508 GST_BUFFER_OFFSET (collect) = 0;
511 GST_DEBUG_OBJECT (demux, "Identified tag, size = %u bytes", tagsize);
514 GstTagList *tags = NULL;
515 guint newsize, saved_size;
517 demux->priv->strip_start = tagsize;
519 if (available < tagsize) {
520 GST_DEBUG_OBJECT (demux, "Only %u bytes available, but %u needed "
521 "to parse tag", available, tagsize);
522 return; /* wait for more data */
525 saved_size = gst_buffer_get_size (collect);
526 gst_buffer_set_size (collect, tagsize);
529 parse_ret = klass->parse_tag (demux, collect, TRUE, &newsize, &tags);
531 gst_buffer_set_size (collect, saved_size);
534 case GST_TAG_DEMUX_RESULT_OK:
535 demux->priv->strip_start = newsize;
536 demux->priv->parsed_tags = tags;
537 GST_DEBUG_OBJECT (demux, "Read start tag of size %u", newsize);
539 case GST_TAG_DEMUX_RESULT_BROKEN_TAG:
540 demux->priv->strip_start = newsize;
541 demux->priv->parsed_tags = tags;
542 GST_WARNING_OBJECT (demux, "Ignoring broken start tag of size %d",
543 demux->priv->strip_start);
545 case GST_TAG_DEMUX_RESULT_AGAIN:
546 GST_DEBUG_OBJECT (demux, "Re-parse, this time with %u bytes", newsize);
547 g_assert (newsize != tagsize);
551 } while (parse_ret == GST_TAG_DEMUX_RESULT_AGAIN);
553 GST_LOG_OBJECT (demux, "Parsed tag. Proceeding to typefinding");
554 demux->priv->state = GST_TAG_DEMUX_TYPEFINDING;
555 demux->priv->send_tag_event = TRUE;
559 gst_tag_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
564 demux = GST_TAG_DEMUX (parent);
566 size = gst_buffer_get_size (buf);
568 /* Update our segment position info */
569 if (demux->priv->segment.format == GST_FORMAT_BYTES) {
570 if (GST_BUFFER_OFFSET_IS_VALID (buf))
571 demux->priv->segment.position = GST_BUFFER_OFFSET (buf);
572 demux->priv->segment.position += size;
573 } else if (demux->priv->segment.format == GST_FORMAT_TIME) {
574 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf))
575 demux->priv->segment.position = GST_BUFFER_TIMESTAMP (buf);
576 if (GST_BUFFER_DURATION_IS_VALID (buf))
577 demux->priv->segment.position += GST_BUFFER_DURATION (buf);
580 if (demux->priv->collect == NULL) {
581 demux->priv->collect = buf;
583 demux->priv->collect = gst_buffer_append (demux->priv->collect, buf);
585 demux->priv->collect_size += size;
588 switch (demux->priv->state) {
589 case GST_TAG_DEMUX_READ_START_TAG:
590 demux->priv->collect = gst_buffer_make_writable (demux->priv->collect);
591 gst_tag_demux_chain_parse_tag (demux, demux->priv->collect);
592 if (demux->priv->state != GST_TAG_DEMUX_TYPEFINDING)
595 case GST_TAG_DEMUX_TYPEFINDING:{
596 GstTypeFindProbability probability = 0;
597 GstBuffer *typefind_buf = NULL;
601 if (demux->priv->collect_size <
602 TYPE_FIND_MIN_SIZE + demux->priv->strip_start)
603 break; /* Go get more data first */
605 GST_DEBUG_OBJECT (demux, "Typefinding with size %" G_GSIZE_FORMAT,
606 demux->priv->collect_size);
608 /* Trim the buffer and adjust offset for typefinding */
609 typefind_buf = demux->priv->collect;
610 gst_buffer_ref (typefind_buf);
611 if (!gst_tag_demux_trim_buffer (demux, &typefind_buf, &typefind_size))
614 if (typefind_buf == NULL)
615 break; /* Still need more data */
617 caps = gst_type_find_helper_for_buffer (GST_OBJECT (demux),
618 typefind_buf, &probability);
621 if (typefind_size < TYPE_FIND_MAX_SIZE) {
622 /* Just break for more data */
623 gst_buffer_unref (typefind_buf);
627 /* We failed typefind */
628 GST_ELEMENT_ERROR (demux, STREAM, TYPE_NOT_FOUND, (NULL),
629 ("Could not detect type for contents within tag"));
630 gst_buffer_unref (typefind_buf);
631 gst_buffer_unref (demux->priv->collect);
632 demux->priv->collect = NULL;
633 demux->priv->collect_size = 0;
634 return GST_FLOW_ERROR;
636 gst_buffer_unref (typefind_buf);
638 GST_DEBUG_OBJECT (demux, "Found type %" GST_PTR_FORMAT " with a "
639 "probability of %u", caps, probability);
641 gst_tag_demux_set_src_caps (demux, caps);
642 gst_caps_unref (caps);
644 /* Move onto streaming and fall-through to push out existing
646 demux->priv->state = GST_TAG_DEMUX_STREAMING;
649 case GST_TAG_DEMUX_STREAMING:{
650 GstBuffer *outbuf = NULL;
653 /* Trim the buffer and adjust offset */
654 if (demux->priv->collect) {
655 outbuf = demux->priv->collect;
656 demux->priv->collect = NULL;
657 demux->priv->collect_size = 0;
658 if (!gst_tag_demux_trim_buffer (demux, &outbuf, &outbuf_size))
662 /* Might need a new segment before the buffer */
663 if (demux->priv->need_newseg) {
664 if (!gst_tag_demux_send_new_segment (demux)) {
665 GST_WARNING_OBJECT (demux, "Downstream did not handle newsegment "
666 "event as it should");
668 demux->priv->need_newseg = FALSE;
671 /* send any pending events we cached */
672 gst_tag_demux_send_pending_events (demux);
674 /* Send our own pending tag event */
675 if (demux->priv->send_tag_event) {
676 gst_tag_demux_send_tag_event (demux);
677 demux->priv->send_tag_event = FALSE;
680 GST_LOG_OBJECT (demux, "Pushing buffer %p", outbuf);
682 return gst_pad_push (demux->priv->srcpad, outbuf);
690 gst_tag_demux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
695 demux = GST_TAG_DEMUX (parent);
697 switch (GST_EVENT_TYPE (event)) {
699 /* FIXME, detect this differently */
700 if (demux->priv->srcpad == NULL) {
701 GST_WARNING_OBJECT (demux, "EOS before we found a type");
702 GST_ELEMENT_ERROR (demux, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
704 ret = gst_pad_event_default (pad, parent, event);
706 case GST_EVENT_SEGMENT:
708 gst_event_copy_segment (event, &demux->priv->segment);
710 demux->priv->need_newseg = TRUE;
711 gst_event_unref (event);
715 case GST_EVENT_FLUSH_STOP:
716 case GST_EVENT_FLUSH_START:
717 ret = gst_pad_event_default (pad, parent, event);
720 /* we drop the caps event. We do typefind and push a new caps event. */
721 ret = gst_pad_event_default (pad, parent, event);
724 if (demux->priv->need_newseg && GST_EVENT_IS_SERIALIZED (event)) {
725 /* Cache all events if we have a pending segment, so they don't get
726 * lost (esp. tag events) */
727 GST_INFO_OBJECT (demux, "caching event: %" GST_PTR_FORMAT, event);
728 GST_OBJECT_LOCK (demux);
729 demux->priv->pending_events =
730 g_list_append (demux->priv->pending_events, event);
731 GST_OBJECT_UNLOCK (demux);
734 ret = gst_pad_event_default (pad, parent, event);
743 gst_tag_demux_get_upstream_size (GstTagDemux * tagdemux)
747 /* Short-cut if we already queried upstream */
748 if (tagdemux->priv->upstream_size > 0)
751 if (!gst_pad_peer_query_duration (tagdemux->priv->sinkpad, GST_FORMAT_BYTES,
756 tagdemux->priv->upstream_size = len;
761 gst_tag_demux_srcpad_event (GstPad * pad, GstObject * parent, GstEvent * event)
763 GstTagDemux *tagdemux;
764 gboolean res = FALSE;
766 tagdemux = GST_TAG_DEMUX (parent);
768 /* Handle SEEK events, with adjusted byte offsets and sizes. */
770 switch (GST_EVENT_TYPE (event)) {
775 GstSeekType start_type, stop_type;
779 gst_event_parse_seek (event, &rate, &format, &flags,
780 &start_type, &start, &stop_type, &stop);
782 if (format == GST_FORMAT_BYTES &&
783 tagdemux->priv->state == GST_TAG_DEMUX_STREAMING &&
784 gst_pad_is_linked (tagdemux->priv->sinkpad)) {
787 switch (start_type) {
788 case GST_SEEK_TYPE_SET:
791 start += tagdemux->priv->strip_start;
793 case GST_SEEK_TYPE_END:
794 /* Adjust the seek to be relative to the start of any end tag
795 * (note: 10 bytes before end is represented by stop=-10) */
798 start -= tagdemux->priv->strip_end;
800 case GST_SEEK_TYPE_NONE:
805 case GST_SEEK_TYPE_SET:
807 /* -1 means the end of the file, pass it upstream intact */
808 stop += tagdemux->priv->strip_start;
811 case GST_SEEK_TYPE_END:
812 /* Adjust the seek to be relative to the start of any end tag
813 * (note: 10 bytes before end is represented by stop=-10) */
816 stop -= tagdemux->priv->strip_end;
818 case GST_SEEK_TYPE_NONE:
822 upstream = gst_event_new_seek (rate, format, flags,
823 start_type, start, stop_type, stop);
824 res = gst_pad_push_event (tagdemux->priv->sinkpad, upstream);
829 res = gst_pad_push_event (tagdemux->priv->sinkpad, event);
835 gst_event_unref (event);
840 /* Read and interpret any end tag when activating in pull_range.
841 * Returns FALSE if pad activation should fail. */
843 gst_tag_demux_pull_end_tag (GstTagDemux * demux, GstTagList ** tags)
845 GstTagDemuxResult parse_ret;
846 GstTagDemuxClass *klass;
847 GstFlowReturn flow_ret;
848 GstTagList *new_tags = NULL;
849 GstBuffer *buffer = NULL;
851 gboolean res = FALSE;
856 klass = GST_TAG_DEMUX_CLASS (G_OBJECT_GET_CLASS (demux));
858 g_assert (klass->identify_tag != NULL);
859 g_assert (klass->parse_tag != NULL);
861 if (klass->min_end_size == 0) {
862 GST_DEBUG_OBJECT (demux, "Not looking for tag at the end");
866 if (demux->priv->upstream_size < klass->min_end_size) {
867 GST_DEBUG_OBJECT (demux, "File too small");
871 /* Pull enough to identify the tag and retrieve its total size */
872 offset = demux->priv->upstream_size - klass->min_end_size;
874 flow_ret = gst_pad_pull_range (demux->priv->sinkpad, offset,
875 klass->min_end_size, &buffer);
877 if (flow_ret != GST_FLOW_OK) {
878 GST_DEBUG_OBJECT (demux, "Could not read tag header from end of file, "
879 "ret = %s", gst_flow_get_name (flow_ret));
883 bsize = gst_buffer_get_size (buffer);
885 if (bsize < klass->min_end_size) {
886 GST_DEBUG_OBJECT (demux, "Only managed to read %" G_GSIZE_FORMAT " bytes"
887 "from file (required: %u bytes)", bsize, klass->min_end_size);
891 have_tag = klass->identify_tag (demux, buffer, FALSE, &tagsize);
894 GST_DEBUG_OBJECT (demux, "Could not find tag at end");
898 /* Now pull the entire tag */
900 guint newsize, saved_size;
902 GST_DEBUG_OBJECT (demux, "Identified tag at end, size=%u bytes", tagsize);
904 demux->priv->strip_end = tagsize;
906 g_assert (tagsize >= klass->min_end_size);
908 /* Get buffer that's exactly the requested size */
909 if (bsize != tagsize) {
910 gst_buffer_unref (buffer);
913 offset = demux->priv->upstream_size - tagsize;
915 flow_ret = gst_pad_pull_range (demux->priv->sinkpad, offset,
918 if (flow_ret != GST_FLOW_OK) {
919 GST_DEBUG_OBJECT (demux, "Could not read data from end of file at "
920 "offset %" G_GUINT64_FORMAT ". ret = %s", offset,
921 gst_flow_get_name (flow_ret));
925 bsize = gst_buffer_get_size (buffer);
927 if (bsize < tagsize) {
928 GST_DEBUG_OBJECT (demux, "Only managed to read %" G_GSIZE_FORMAT
929 " bytes from file", bsize);
934 GST_BUFFER_OFFSET (buffer) = offset;
937 gst_buffer_set_size (buffer, tagsize);
940 parse_ret = klass->parse_tag (demux, buffer, FALSE, &newsize, &new_tags);
942 gst_buffer_set_size (buffer, saved_size);
945 case GST_TAG_DEMUX_RESULT_OK:
947 demux->priv->strip_end = newsize;
948 GST_DEBUG_OBJECT (demux, "Read tag at end, size %d",
949 demux->priv->strip_end);
951 case GST_TAG_DEMUX_RESULT_BROKEN_TAG:
953 demux->priv->strip_end = newsize;
954 GST_WARNING_OBJECT (demux, "Ignoring broken tag at end, size %d",
955 demux->priv->strip_end);
957 case GST_TAG_DEMUX_RESULT_AGAIN:
958 GST_DEBUG_OBJECT (demux, "Re-parse, this time with %d bytes", newsize);
959 g_assert (newsize != tagsize);
963 } while (parse_ret == GST_TAG_DEMUX_RESULT_AGAIN);
970 gst_tag_list_unref (new_tags);
972 gst_buffer_unref (buffer);
976 /* Read and interpret any tag at the start when activating in
977 * pull_range. Returns FALSE if pad activation should fail. */
979 gst_tag_demux_pull_start_tag (GstTagDemux * demux, GstTagList ** tags)
981 GstTagDemuxResult parse_ret;
982 GstTagDemuxClass *klass;
983 GstFlowReturn flow_ret;
984 GstTagList *new_tags = NULL;
985 GstBuffer *buffer = NULL;
987 gboolean res = FALSE;
991 klass = GST_TAG_DEMUX_CLASS (G_OBJECT_GET_CLASS (demux));
993 g_assert (klass->identify_tag != NULL);
994 g_assert (klass->parse_tag != NULL);
996 if (klass->min_start_size == 0) {
997 GST_DEBUG_OBJECT (demux, "Not looking for tag at the beginning");
1001 /* Handle tag at start. Try with 4kB to start with */
1002 req = MAX (klass->min_start_size, 4096);
1004 /* Pull enough to identify the tag and retrieve its total size */
1005 flow_ret = gst_pad_pull_range (demux->priv->sinkpad, 0, req, &buffer);
1006 if (flow_ret != GST_FLOW_OK) {
1007 GST_DEBUG_OBJECT (demux, "Could not read data from start of file ret=%s",
1008 gst_flow_get_name (flow_ret));
1012 bsize = gst_buffer_get_size (buffer);
1014 if (bsize < klass->min_start_size) {
1015 GST_DEBUG_OBJECT (demux, "Only managed to read %" G_GSIZE_FORMAT
1016 " bytes from file - no tag in this file", bsize);
1020 have_tag = klass->identify_tag (demux, buffer, TRUE, &tagsize);
1023 GST_DEBUG_OBJECT (demux, "Could not find start tag");
1028 GST_DEBUG_OBJECT (demux, "Identified start tag, size = %u bytes", tagsize);
1031 guint newsize, saved_size;
1033 demux->priv->strip_start = tagsize;
1035 /* Now pull the entire tag */
1036 g_assert (tagsize >= klass->min_start_size);
1038 if (bsize < tagsize) {
1039 gst_buffer_unref (buffer);
1042 flow_ret = gst_pad_pull_range (demux->priv->sinkpad, 0, tagsize, &buffer);
1043 if (flow_ret != GST_FLOW_OK) {
1044 GST_DEBUG_OBJECT (demux, "Could not read data from start of file, "
1045 "ret = %s", gst_flow_get_name (flow_ret));
1049 bsize = gst_buffer_get_size (buffer);
1051 if (bsize < tagsize) {
1052 GST_DEBUG_OBJECT (demux, "Only managed to read %" G_GSIZE_FORMAT
1053 " bytes from file", bsize);
1054 GST_ELEMENT_ERROR (demux, STREAM, DECODE,
1055 (_("Failed to read tag: not enough data")), (NULL));
1061 gst_buffer_set_size (buffer, tagsize);
1063 parse_ret = klass->parse_tag (demux, buffer, TRUE, &newsize, &new_tags);
1065 gst_buffer_set_size (buffer, saved_size);
1067 switch (parse_ret) {
1068 case GST_TAG_DEMUX_RESULT_OK:
1070 demux->priv->strip_start = newsize;
1071 GST_DEBUG_OBJECT (demux, "Read start tag of size %d", newsize);
1073 case GST_TAG_DEMUX_RESULT_BROKEN_TAG:
1075 demux->priv->strip_start = newsize;
1076 GST_WARNING_OBJECT (demux, "Ignoring broken start tag of size %d",
1077 demux->priv->strip_start);
1079 case GST_TAG_DEMUX_RESULT_AGAIN:
1080 GST_DEBUG_OBJECT (demux, "Re-parse, this time with %d bytes", newsize);
1081 g_assert (newsize != tagsize);
1085 } while (parse_ret == GST_TAG_DEMUX_RESULT_AGAIN);
1092 gst_tag_list_unref (new_tags);
1094 gst_buffer_unref (buffer);
1098 /* This function operates similarly to gst_type_find_element_loop
1099 * in the typefind element
1100 * 1. try to read tags in pull mode
1101 * 2. typefind the contents
1102 * 3. if we didn't find any caps, fail.
1103 * 4. set caps on srcpad
1105 static GstFlowReturn
1106 gst_tag_demux_element_find (GstTagDemux * demux)
1108 GstTagDemuxClass *klass;
1109 GstTypeFindProbability probability = 0;
1110 GstFlowReturn ret = GST_FLOW_OK;
1111 GstTagList *start_tags = NULL;
1112 GstTagList *end_tags = NULL;
1113 gboolean e_tag_ok, s_tag_ok;
1114 GstCaps *caps = NULL;
1116 /* Look for tags at start and end of file */
1117 GST_DEBUG_OBJECT (demux, "Activated pull mode. Looking for tags");
1118 if (!gst_tag_demux_get_upstream_size (demux))
1121 demux->priv->strip_start = 0;
1122 demux->priv->strip_end = 0;
1125 s_tag_ok = gst_tag_demux_pull_start_tag (demux, &start_tags);
1126 e_tag_ok = gst_tag_demux_pull_end_tag (demux, &end_tags);
1128 klass = GST_TAG_DEMUX_CLASS (G_OBJECT_GET_CLASS (demux));
1130 if (klass->merge_tags != NULL) {
1131 demux->priv->parsed_tags = klass->merge_tags (demux, start_tags, end_tags);
1133 /* we merge in REPLACE mode, so put the less important tags first, which
1134 * we'll just assume is the end tag (subclasses may change this behaviour
1135 * or make it configurable by overriding the merge_tags vfunc) */
1136 demux->priv->parsed_tags =
1137 gst_tag_list_merge (end_tags, start_tags, GST_TAG_MERGE_REPLACE);
1141 gst_tag_list_unref (start_tags);
1143 gst_tag_list_unref (end_tags);
1145 if (!e_tag_ok && !s_tag_ok)
1148 if (demux->priv->parsed_tags != NULL) {
1149 demux->priv->send_tag_event = TRUE;
1152 if (demux->priv->upstream_size <=
1153 demux->priv->strip_start + demux->priv->strip_end)
1156 /* 2 - Do typefinding on data, but not if downstream is in charge */
1157 if (GST_PAD_MODE (demux->priv->srcpad) == GST_PAD_MODE_PULL)
1158 goto skip_typefinding;
1160 caps = gst_type_find_helper_get_range (GST_OBJECT (demux), NULL,
1161 (GstTypeFindHelperGetRangeFunction) gst_tag_demux_read_range,
1162 demux->priv->upstream_size
1163 - (demux->priv->strip_start + demux->priv->strip_end), NULL,
1166 GST_INFO_OBJECT (demux, "Found type %" GST_PTR_FORMAT " with a "
1167 "probability of %u", caps, probability);
1169 /* 3 - If we didn't find the caps, fail */
1173 /* tag reading and typefinding were already done, don't do them again in
1174 * the chain function if we end up in push mode */
1175 demux->priv->state = GST_TAG_DEMUX_STREAMING;
1177 /* 6 Set the srcpad caps now that we know them */
1178 gst_tag_demux_set_src_caps (demux, caps);
1179 gst_caps_unref (caps);
1183 /* set it again, in case we skipped typefinding */
1184 demux->priv->state = GST_TAG_DEMUX_STREAMING;
1191 GST_ELEMENT_ERROR (demux, STREAM, TYPE_NOT_FOUND,
1192 ("Could not get stream size"), (NULL));
1193 return GST_FLOW_ERROR;
1197 GST_ELEMENT_ERROR (demux, STREAM, TYPE_NOT_FOUND,
1198 ("Could not get start and/or end tag"), (NULL));
1199 return GST_FLOW_ERROR;
1203 /* There was no data (probably due to a truncated file) */
1204 /* so we don't know about type either */
1205 GST_ELEMENT_ERROR (demux, STREAM, TYPE_NOT_FOUND, ("No data in file"),
1207 return GST_FLOW_ERROR;
1211 GST_ELEMENT_ERROR (demux, STREAM, TYPE_NOT_FOUND,
1212 ("Could not detect type of contents"), (NULL));
1213 return GST_FLOW_ERROR;
1217 /* This function operates similarly to gst_type_find_element_loop
1218 * in the typefind element
1219 * 1. try to read tags in pull mode
1220 * 2. typefind the contents
1221 * 3. if we didn't find any caps, fail.
1222 * 4. set caps on srcpad
1225 gst_tag_demux_element_loop (GstTagDemux * demux)
1229 switch (demux->priv->state) {
1230 case GST_TAG_DEMUX_READ_START_TAG:
1231 case GST_TAG_DEMUX_TYPEFINDING:
1232 ret = gst_tag_demux_element_find (demux);
1234 case GST_TAG_DEMUX_STREAMING:
1236 GstBuffer *outbuf = NULL;
1238 if (demux->priv->need_newseg) {
1239 demux->priv->need_newseg = FALSE;
1240 /* FIXME: check segment, should be 0-N for downstream */
1241 gst_tag_demux_send_new_segment (demux);
1244 /* Send our own pending tag event */
1245 if (demux->priv->send_tag_event) {
1246 gst_tag_demux_send_tag_event (demux);
1247 demux->priv->send_tag_event = FALSE;
1250 /* Pull data and push it downstream */
1251 ret = gst_pad_pull_range (demux->priv->sinkpad, demux->priv->offset,
1252 DEFAULT_PULL_BLOCKSIZE, &outbuf);
1254 if (ret != GST_FLOW_OK)
1257 demux->priv->offset += gst_buffer_get_size (outbuf);
1259 ret = gst_pad_push (demux->priv->srcpad, outbuf);
1263 ret = GST_FLOW_ERROR;
1266 if (ret != GST_FLOW_OK)
1274 const gchar *reason = gst_flow_get_name (ret);
1275 gboolean push_eos = FALSE;
1277 GST_LOG_OBJECT (demux, "pausing task, reason %s", reason);
1278 gst_pad_pause_task (demux->priv->sinkpad);
1280 if (ret == GST_FLOW_EOS) {
1281 /* perform EOS logic */
1283 if (demux->priv->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1286 /* for segment playback we need to post when (in stream time)
1287 * we stopped, this is either stop (when set) or the duration. */
1288 if ((stop = demux->priv->segment.stop) == -1)
1289 stop = demux->priv->offset;
1291 GST_LOG_OBJECT (demux, "Sending segment done, at end of segment");
1292 gst_element_post_message (GST_ELEMENT_CAST (demux),
1293 gst_message_new_segment_done (GST_OBJECT_CAST (demux),
1294 GST_FORMAT_BYTES, stop));
1295 gst_pad_push_event (demux->priv->srcpad,
1296 gst_event_new_segment_done (GST_FORMAT_BYTES, stop));
1300 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
1301 /* for fatal errors we post an error message */
1302 GST_ELEMENT_ERROR (demux, STREAM, FAILED, (NULL),
1303 ("Stream stopped, reason %s", reason));
1307 /* send EOS, and prevent hanging if no streams yet */
1308 GST_LOG_OBJECT (demux, "Sending EOS, at end of stream");
1309 gst_pad_push_event (demux->priv->srcpad, gst_event_new_eos ());
1316 gst_tag_demux_sink_activate_mode (GstPad * pad, GstObject * parent,
1317 GstPadMode mode, gboolean active)
1322 case GST_PAD_MODE_PULL:
1326 res = gst_pad_stop_task (pad);
1334 GST_TAG_DEMUX (parent)->priv->state = GST_TAG_DEMUX_READ_START_TAG;
1340 gst_tag_demux_sink_activate (GstPad * sinkpad, GstObject * parent)
1346 demux = GST_TAG_DEMUX (parent);
1349 /* If we can activate pull_range upstream, then read any end and start
1350 * tags, otherwise activate in push mode and the chain function will
1351 * collect buffers, read the start tag and output a buffer to end
1354 query = gst_query_new_scheduling ();
1356 if (!gst_pad_peer_query (sinkpad, query)) {
1357 gst_query_unref (query);
1361 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
1362 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
1363 gst_query_unref (query);
1368 if (!gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE))
1371 /* only start our task if we ourselves decide to start in pull mode */
1372 return gst_pad_start_task (sinkpad,
1373 (GstTaskFunction) gst_tag_demux_element_loop, demux, NULL);
1377 GST_DEBUG_OBJECT (demux, "No pull mode. Changing to push, but won't be "
1378 "able to read end tags");
1379 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
1384 gst_tag_demux_src_activate_mode (GstPad * pad, GstObject * parent,
1385 GstPadMode mode, gboolean active)
1388 GstTagDemux *demux = GST_TAG_DEMUX (parent);
1391 case GST_PAD_MODE_PULL:
1392 /* make sure our task stops pushing, we can't call _stop here
1393 * because this activation might happen from the streaming thread. */
1394 gst_pad_pause_task (demux->priv->sinkpad);
1395 res = gst_pad_activate_mode (demux->priv->sinkpad, mode, active);
1404 static GstFlowReturn
1405 gst_tag_demux_read_range (GstTagDemux * demux, GstObject * parent,
1406 guint64 offset, guint length, GstBuffer ** buffer)
1413 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
1415 /* Adjust offset and length of the request to trim off tag information.
1416 * For the returned buffer, adjust the output offset to match what downstream
1418 in_offset = offset + demux->priv->strip_start;
1420 if (!gst_tag_demux_get_upstream_size (demux))
1421 return GST_FLOW_ERROR;
1423 if (in_offset + length >= demux->priv->upstream_size - demux->priv->strip_end) {
1424 if (in_offset + demux->priv->strip_end >= demux->priv->upstream_size)
1425 return GST_FLOW_EOS;
1426 in_length = demux->priv->upstream_size - demux->priv->strip_end - in_offset;
1431 ret = gst_pad_pull_range (demux->priv->sinkpad, in_offset, in_length, buffer);
1433 if (ret == GST_FLOW_OK && *buffer) {
1434 if (!gst_tag_demux_trim_buffer (demux, buffer, &size))
1435 goto read_beyond_end;
1437 /* this should only happen in streaming mode */
1438 g_assert (*buffer != NULL);
1445 GST_DEBUG_OBJECT (demux, "attempted read beyond end of file");
1446 if (*buffer != NULL) {
1447 gst_buffer_unref (*buffer);
1450 return GST_FLOW_EOS;
1454 static GstFlowReturn
1455 gst_tag_demux_src_getrange (GstPad * srcpad, GstObject * parent,
1456 guint64 offset, guint length, GstBuffer ** buffer)
1458 GstTagDemux *demux = GST_TAG_DEMUX (parent);
1460 /* downstream in pull mode won't miss a newsegment event,
1461 * but it likely appreciates other (tag) events */
1462 if (demux->priv->need_newseg) {
1463 gst_tag_demux_send_pending_events (demux);
1464 demux->priv->need_newseg = FALSE;
1467 if (demux->priv->send_tag_event) {
1468 gst_tag_demux_send_tag_event (demux);
1469 demux->priv->send_tag_event = FALSE;
1472 return gst_tag_demux_read_range (demux, NULL, offset, length, buffer);
1475 static GstStateChangeReturn
1476 gst_tag_demux_change_state (GstElement * element, GstStateChange transition)
1478 GstStateChangeReturn ret;
1479 GstTagDemux *demux = GST_TAG_DEMUX (element);
1481 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1483 switch (transition) {
1484 case GST_STATE_CHANGE_PAUSED_TO_READY:
1485 gst_tag_demux_reset (demux);
1494 gst_tag_demux_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
1496 /* For a position or duration query, adjust the returned
1497 * bytes to strip off the end and start areas */
1498 GstTagDemux *demux = GST_TAG_DEMUX (parent);
1501 gboolean res = TRUE;
1503 /* FIXME: locking ? */
1504 switch (GST_QUERY_TYPE (query)) {
1505 case GST_QUERY_SCHEDULING:
1506 res = gst_pad_peer_query (demux->priv->sinkpad, query);
1508 case GST_QUERY_POSITION:
1510 if (!(res = gst_pad_peer_query (demux->priv->sinkpad, query)))
1513 gst_query_parse_position (query, &format, &result);
1514 if (format == GST_FORMAT_BYTES) {
1515 result -= demux->priv->strip_start;
1516 gst_query_set_position (query, format, result);
1520 case GST_QUERY_DURATION:
1522 if (!(res = gst_pad_peer_query (demux->priv->sinkpad, query)))
1525 gst_query_parse_duration (query, &format, &result);
1526 if (format == GST_FORMAT_BYTES) {
1527 /* if downstream activated us in pull mode right away, e.g. in case of
1528 * filesrc ! id3demux ! xyzparse ! .., read tags here, since we don't
1529 * have a streaming thread of our own to do that. We do it here and
1530 * not in get_range(), so we can return the right size in bytes.. */
1531 if (demux->priv->state == GST_TAG_DEMUX_READ_START_TAG &&
1532 GST_PAD_MODE (demux->priv->srcpad) == GST_PAD_MODE_PULL) {
1533 GstFlowReturn flow G_GNUC_UNUSED;
1535 flow = gst_tag_demux_element_find (demux);
1536 GST_INFO_OBJECT (demux, "pulled tags: %s", gst_flow_get_name (flow));
1538 result -= demux->priv->strip_start + demux->priv->strip_end;
1541 gst_query_set_duration (query, format, result);
1546 res = gst_pad_query_default (pad, parent, query);
1554 gst_tag_demux_send_pending_events (GstTagDemux * demux)
1558 /* send any pending events we cached */
1559 GST_OBJECT_LOCK (demux);
1560 events = demux->priv->pending_events;
1561 demux->priv->pending_events = NULL;
1562 GST_OBJECT_UNLOCK (demux);
1564 while (events != NULL) {
1565 GST_DEBUG_OBJECT (demux->priv->srcpad, "sending cached %s event: %"
1566 GST_PTR_FORMAT, GST_EVENT_TYPE_NAME (events->data), events->data);
1567 gst_pad_push_event (demux->priv->srcpad, GST_EVENT (events->data));
1568 events = g_list_delete_link (events, events);
1573 gst_tag_demux_send_tag_event (GstTagDemux * demux)
1575 /* FIXME: what's the correct merge mode? Docs need to tell... */
1576 GstTagList *merged = gst_tag_list_merge (demux->priv->event_tags,
1577 demux->priv->parsed_tags, GST_TAG_MERGE_KEEP);
1580 GstEvent *event = gst_event_new_tag (merged);
1582 GST_EVENT_TIMESTAMP (event) = 0;
1583 GST_DEBUG_OBJECT (demux, "Sending tag event on src pad");
1584 gst_pad_push_event (demux->priv->srcpad, event);
1589 gst_tag_demux_send_new_segment (GstTagDemux * tagdemux)
1592 gint64 start, stop, position;
1593 GstSegment *seg = &tagdemux->priv->segment;
1596 if (seg->format == GST_FORMAT_UNDEFINED) {
1597 GST_LOG_OBJECT (tagdemux,
1598 "No new segment received before first buffer. Using default");
1599 gst_segment_init (seg, GST_FORMAT_BYTES);
1600 seg->start = tagdemux->priv->strip_start;
1601 seg->time = tagdemux->priv->strip_start;
1604 /* Can't adjust segments in non-BYTES formats */
1605 if (tagdemux->priv->segment.format != GST_FORMAT_BYTES) {
1606 event = gst_event_new_segment (seg);
1607 return gst_pad_push_event (tagdemux->priv->srcpad, event);
1612 position = seg->time;
1614 g_return_val_if_fail (start != -1, FALSE);
1615 g_return_val_if_fail (position != -1, FALSE);
1617 if (tagdemux->priv->strip_end > 0) {
1618 if (gst_tag_demux_get_upstream_size (tagdemux)) {
1619 guint64 v1tag_offset =
1620 tagdemux->priv->upstream_size - tagdemux->priv->strip_end;
1622 if (start >= v1tag_offset) {
1623 /* Segment is completely within the end tag, output an open-ended
1624 * segment, even though all the buffers will get trimmed away */
1625 start = v1tag_offset;
1629 if (stop != -1 && stop >= v1tag_offset) {
1630 GST_DEBUG_OBJECT (tagdemux,
1631 "Segment crosses the end tag. Trimming end");
1632 stop = v1tag_offset;
1637 if (tagdemux->priv->strip_start > 0) {
1638 if (start > tagdemux->priv->strip_start)
1639 start -= tagdemux->priv->strip_start;
1643 if (position > tagdemux->priv->strip_start)
1644 position -= tagdemux->priv->strip_start;
1649 if (stop > tagdemux->priv->strip_start)
1650 stop -= tagdemux->priv->strip_start;
1656 GST_DEBUG_OBJECT (tagdemux, "Sending segment %" GST_SEGMENT_FORMAT, seg);
1658 gst_segment_copy_into (seg, &newseg);
1659 newseg.start = start;
1661 newseg.position = position;
1662 event = gst_event_new_segment (&newseg);
1664 return gst_pad_push_event (tagdemux->priv->srcpad, event);