2 * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
4 * gstoggdemux.c: ogg stream demuxer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:element-oggdemux
24 * @see_also: <link linkend="gst-plugins-base-plugins-oggmux">oggmux</link>
26 * This element demuxes ogg files into their encoded audio and video components.
29 * <title>Example pipelines</title>
31 * gst-launch -v filesrc location=test.ogg ! oggdemux ! vorbisdec ! audioconvert ! alsasink
32 * ]| Decodes the vorbis audio stored inside an ogg container.
35 * Last reviewed on 2006-12-30 (0.10.5)
43 #include <gst/gst-i18n-plugin.h>
44 #include <gst/tag/tag.h>
46 #include "gstoggdemux.h"
48 #define CHUNKSIZE (8500) /* this is out of vorbisfile */
50 #define GST_FLOW_LIMIT GST_FLOW_CUSTOM_ERROR
52 #define GST_CHAIN_LOCK(ogg) g_mutex_lock((ogg)->chain_lock)
53 #define GST_CHAIN_UNLOCK(ogg) g_mutex_unlock((ogg)->chain_lock)
55 GST_DEBUG_CATEGORY (gst_ogg_demux_debug);
56 GST_DEBUG_CATEGORY (gst_ogg_demux_setup_debug);
57 #define GST_CAT_DEFAULT gst_ogg_demux_debug
61 _ogg_packet_copy (const ogg_packet * packet)
63 ogg_packet *ret = g_new0 (ogg_packet, 1);
66 ret->packet = g_memdup (packet->packet, packet->bytes);
72 _ogg_packet_free (ogg_packet * packet)
74 g_free (packet->packet);
79 gst_ogg_page_copy (ogg_page * page)
81 ogg_page *p = g_new0 (ogg_page, 1);
83 /* make a copy of the page */
84 p->header = g_memdup (page->header, page->header_len);
85 p->header_len = page->header_len;
86 p->body = g_memdup (page->body, page->body_len);
87 p->body_len = page->body_len;
93 gst_ogg_page_free (ogg_page * page)
95 g_free (page->header);
100 static gboolean gst_ogg_demux_collect_chain_info (GstOggDemux * ogg,
101 GstOggChain * chain);
102 static gboolean gst_ogg_demux_activate_chain (GstOggDemux * ogg,
103 GstOggChain * chain, GstEvent * event);
104 static void gst_ogg_chain_mark_discont (GstOggChain * chain);
106 static gboolean gst_ogg_demux_perform_seek (GstOggDemux * ogg,
108 static gboolean gst_ogg_demux_receive_event (GstElement * element,
111 static void gst_ogg_pad_dispose (GObject * object);
112 static void gst_ogg_pad_finalize (GObject * object);
114 static const GstQueryType *gst_ogg_pad_query_types (GstPad * pad);
115 static gboolean gst_ogg_pad_src_query (GstPad * pad, GstQuery * query);
116 static gboolean gst_ogg_pad_event (GstPad * pad, GstEvent * event);
117 static GstCaps *gst_ogg_pad_getcaps (GstPad * pad);
118 static GstOggPad *gst_ogg_chain_get_stream (GstOggChain * chain,
121 static GstFlowReturn gst_ogg_demux_combine_flows (GstOggDemux * ogg,
122 GstOggPad * pad, GstFlowReturn ret);
123 static void gst_ogg_demux_sync_streams (GstOggDemux * ogg);
125 GType gst_ogg_pad_get_type (void);
126 G_DEFINE_TYPE (GstOggPad, gst_ogg_pad, GST_TYPE_PAD);
129 gst_ogg_pad_class_init (GstOggPadClass * klass)
131 GObjectClass *gobject_class;
133 gobject_class = (GObjectClass *) klass;
135 gobject_class->dispose = gst_ogg_pad_dispose;
136 gobject_class->finalize = gst_ogg_pad_finalize;
140 gst_ogg_pad_init (GstOggPad * pad)
142 gst_pad_set_event_function (GST_PAD (pad),
143 GST_DEBUG_FUNCPTR (gst_ogg_pad_event));
144 gst_pad_set_getcaps_function (GST_PAD (pad),
145 GST_DEBUG_FUNCPTR (gst_ogg_pad_getcaps));
146 gst_pad_set_query_type_function (GST_PAD (pad),
147 GST_DEBUG_FUNCPTR (gst_ogg_pad_query_types));
148 gst_pad_set_query_function (GST_PAD (pad),
149 GST_DEBUG_FUNCPTR (gst_ogg_pad_src_query));
151 pad->mode = GST_OGG_PAD_MODE_INIT;
153 pad->current_granule = -1;
154 pad->keyframe_granule = -1;
156 pad->start_time = GST_CLOCK_TIME_NONE;
158 pad->last_stop = GST_CLOCK_TIME_NONE;
160 pad->have_type = FALSE;
161 pad->continued = NULL;
162 pad->map.headers = NULL;
163 pad->map.queued = NULL;
167 gst_ogg_pad_dispose (GObject * object)
169 GstOggPad *pad = GST_OGG_PAD (object);
174 g_list_foreach (pad->map.headers, (GFunc) _ogg_packet_free, NULL);
175 g_list_free (pad->map.headers);
176 pad->map.headers = NULL;
177 g_list_foreach (pad->map.queued, (GFunc) _ogg_packet_free, NULL);
178 g_list_free (pad->map.queued);
179 pad->map.queued = NULL;
181 g_free (pad->map.index);
182 pad->map.index = NULL;
184 /* clear continued pages */
185 g_list_foreach (pad->continued, (GFunc) gst_ogg_page_free, NULL);
186 g_list_free (pad->continued);
187 pad->continued = NULL;
190 gst_caps_unref (pad->map.caps);
191 pad->map.caps = NULL;
194 if (pad->map.taglist) {
195 gst_tag_list_free (pad->map.taglist);
196 pad->map.taglist = NULL;
199 ogg_stream_reset (&pad->map.stream);
201 G_OBJECT_CLASS (gst_ogg_pad_parent_class)->dispose (object);
205 gst_ogg_pad_finalize (GObject * object)
207 GstOggPad *pad = GST_OGG_PAD (object);
209 ogg_stream_clear (&pad->map.stream);
211 G_OBJECT_CLASS (gst_ogg_pad_parent_class)->finalize (object);
214 static const GstQueryType *
215 gst_ogg_pad_query_types (GstPad * pad)
217 static const GstQueryType query_types[] = {
227 gst_ogg_pad_getcaps (GstPad * pad)
229 return gst_caps_ref (GST_PAD_CAPS (pad));
233 gst_ogg_pad_src_query (GstPad * pad, GstQuery * query)
238 ogg = GST_OGG_DEMUX (gst_pad_get_parent (pad));
240 switch (GST_QUERY_TYPE (query)) {
241 case GST_QUERY_DURATION:
244 gint64 total_time = -1;
246 gst_query_parse_duration (query, &format, NULL);
247 /* can only get position in time */
248 if (format != GST_FORMAT_TIME)
251 if (ogg->total_time != -1) {
252 /* we can return the total length */
253 total_time = ogg->total_time;
255 gint bitrate = ogg->bitrate;
257 /* try with length and bitrate */
261 /* ask upstream for total length in bytes */
262 uquery = gst_query_new_duration (GST_FORMAT_BYTES);
263 if (gst_pad_peer_query (ogg->sinkpad, uquery)) {
266 gst_query_parse_duration (uquery, NULL, &length);
268 /* estimate using the bitrate */
270 gst_util_uint64_scale (length, 8 * GST_SECOND, bitrate);
273 "length: %" G_GINT64_FORMAT ", bitrate %d, total_time %"
274 GST_TIME_FORMAT, length, bitrate, GST_TIME_ARGS (total_time));
276 gst_query_unref (uquery);
280 gst_query_set_duration (query, GST_FORMAT_TIME, total_time);
283 case GST_QUERY_SEEKING:
287 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
288 if (format == GST_FORMAT_TIME) {
289 gboolean seekable = FALSE;
294 stop = ogg->total_time;
295 } else if (ogg->current_chain->streams->len) {
299 for (i = 0; i < ogg->current_chain->streams->len; i++) {
301 g_array_index (ogg->current_chain->streams, GstOggPad *, i);
303 seekable |= (pad->map.index != NULL && pad->map.n_index != 0);
305 if (pad->map.index != NULL && pad->map.n_index != 0) {
307 GstClockTime idx_time;
309 idx = &pad->map.index[pad->map.n_index - 1];
311 gst_util_uint64_scale (idx->timestamp, GST_SECOND,
316 stop = MAX (idx_time, stop);
321 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0, stop);
329 res = gst_pad_query_default (pad, query);
333 gst_object_unref (ogg);
340 GST_DEBUG_OBJECT (ogg, "only query duration on TIME is supported");
347 gst_ogg_demux_receive_event (GstElement * element, GstEvent * event)
352 ogg = GST_OGG_DEMUX (element);
354 switch (GST_EVENT_TYPE (event)) {
356 /* now do the seek */
357 res = gst_ogg_demux_perform_seek (ogg, event);
358 gst_event_unref (event);
361 GST_DEBUG_OBJECT (ogg, "We only handle seek events here");
370 GST_DEBUG_OBJECT (ogg, "error handling event");
371 gst_event_unref (event);
377 gst_ogg_pad_event (GstPad * pad, GstEvent * event)
382 ogg = GST_OGG_DEMUX (gst_pad_get_parent (pad));
384 switch (GST_EVENT_TYPE (event)) {
386 /* now do the seek */
387 res = gst_ogg_demux_perform_seek (ogg, event);
388 gst_event_unref (event);
391 res = gst_pad_event_default (pad, event);
394 gst_object_unref (ogg);
400 gst_ogg_pad_reset (GstOggPad * pad)
402 ogg_stream_reset (&pad->map.stream);
404 GST_DEBUG_OBJECT (pad, "doing reset");
406 /* clear continued pages */
407 g_list_foreach (pad->continued, (GFunc) gst_ogg_page_free, NULL);
408 g_list_free (pad->continued);
409 pad->continued = NULL;
411 pad->last_ret = GST_FLOW_OK;
412 pad->last_stop = GST_CLOCK_TIME_NONE;
413 pad->current_granule = -1;
414 pad->keyframe_granule = -1;
418 /* queue data, basically takes the packet, puts it in a buffer and store the
419 * buffer in the queued list. */
421 gst_ogg_demux_queue_data (GstOggPad * pad, ogg_packet * packet)
423 #ifndef GST_DISABLE_GST_DEBUG
424 GstOggDemux *ogg = pad->ogg;
427 GST_DEBUG_OBJECT (ogg, "%p queueing data serial %08lx", pad,
430 pad->map.queued = g_list_append (pad->map.queued, _ogg_packet_copy (packet));
437 gst_ogg_demux_chain_peer (GstOggPad * pad, ogg_packet * packet,
438 gboolean push_headers)
440 GstBuffer *buf = NULL;
441 GstFlowReturn ret, cret;
442 GstOggDemux *ogg = pad->ogg;
448 GstClockTime out_timestamp, out_duration;
449 guint64 out_offset, out_offset_end;
450 gboolean delta_unit = FALSE;
452 GST_DEBUG_OBJECT (ogg,
453 "%p streaming to peer serial %08lx", pad, pad->map.serialno);
455 if (pad->map.is_ogm) {
459 data = packet->packet;
460 bytes = packet->bytes;
465 if ((data[0] & 1) || (data[0] & 3 && pad->map.is_ogm_text)) {
466 /* We don't push header packets for OGM */
467 cret = gst_ogg_demux_combine_flows (ogg, pad, GST_FLOW_OK);
471 offset = 1 + (((data[0] & 0xc0) >> 6) | ((data[0] & 0x02) << 1));
472 delta_unit = (((data[0] & 0x08) >> 3) == 0);
476 /* Strip trailing \0 for subtitles */
477 if (pad->map.is_ogm_text) {
478 while (bytes && data[bytes - 1] == 0) {
483 } else if (pad->map.is_vp8) {
484 if ((packet->bytes >= 7 && memcmp (packet->packet, "OVP80\2 ", 7) == 0) ||
486 (packet->bytes >= 5 && memcmp (packet->packet, "OVP80", 5) == 0)) {
487 /* We don't push header packets for VP8 */
488 cret = gst_ogg_demux_combine_flows (ogg, pad, GST_FLOW_OK);
498 /* get timing info for the packet */
499 duration = gst_ogg_stream_get_packet_duration (&pad->map, packet);
500 GST_DEBUG_OBJECT (ogg, "packet duration %" G_GUINT64_FORMAT, duration);
503 out_timestamp = GST_CLOCK_TIME_NONE;
504 out_duration = GST_CLOCK_TIME_NONE;
508 if (packet->granulepos != -1) {
509 pad->current_granule = gst_ogg_stream_granulepos_to_granule (&pad->map,
511 pad->keyframe_granule =
512 gst_ogg_stream_granulepos_to_key_granule (&pad->map,
514 GST_DEBUG_OBJECT (ogg, "new granule %" G_GUINT64_FORMAT,
515 pad->current_granule);
516 } else if (ogg->segment.rate > 0.0 && pad->current_granule != -1) {
517 pad->current_granule += duration;
518 GST_DEBUG_OBJECT (ogg, "interpollating granule %" G_GUINT64_FORMAT,
519 pad->current_granule);
521 if (ogg->segment.rate < 0.0 && packet->granulepos == -1) {
522 /* negative rates, only set timestamp on the packets with a granulepos */
528 /* we only push buffers after we have a valid granule. This is done so that
529 * we nicely skip packets without a timestamp after a seek. This is ok
530 * because we base or seek on the packet after the page with the smaller
532 if (pad->current_granule == -1)
535 if (pad->map.is_ogm) {
536 out_timestamp = gst_ogg_stream_granule_to_time (&pad->map,
537 pad->current_granule);
538 out_duration = gst_util_uint64_scale (duration,
539 GST_SECOND * pad->map.granulerate_d, pad->map.granulerate_n);
540 } else if (pad->map.is_sparse) {
541 out_timestamp = gst_ogg_stream_granule_to_time (&pad->map,
542 pad->current_granule);
543 out_duration = GST_CLOCK_TIME_NONE;
545 out_timestamp = gst_ogg_stream_granule_to_time (&pad->map,
546 pad->current_granule - duration);
548 gst_ogg_stream_granule_to_time (&pad->map,
549 pad->current_granule) - out_timestamp;
552 gst_ogg_stream_granule_to_granulepos (&pad->map,
553 pad->current_granule, pad->keyframe_granule);
555 gst_ogg_stream_granule_to_time (&pad->map, pad->current_granule);
559 if (pad->map.is_ogm_text) {
560 /* check for invalid buffer sizes */
561 if (G_UNLIKELY (offset + trim >= packet->bytes))
566 gst_pad_alloc_buffer_and_set_caps (GST_PAD_CAST (pad),
567 GST_BUFFER_OFFSET_NONE, packet->bytes - offset - trim,
568 GST_PAD_CAPS (pad), &buf);
571 cret = gst_ogg_demux_combine_flows (ogg, pad, ret);
572 if (ret != GST_FLOW_OK)
575 /* set delta flag for OGM content */
577 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
579 /* copy packet in buffer */
580 memcpy (buf->data, packet->packet + offset, packet->bytes - offset - trim);
582 GST_BUFFER_TIMESTAMP (buf) = out_timestamp;
583 GST_BUFFER_DURATION (buf) = out_duration;
584 GST_BUFFER_OFFSET (buf) = out_offset;
585 GST_BUFFER_OFFSET_END (buf) = out_offset_end;
587 /* Mark discont on the buffer */
589 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
590 pad->discont = FALSE;
593 pad->last_stop = ogg->segment.last_stop;
595 /* don't push the header packets when we are asked to skip them */
596 if (!packet->b_o_s || push_headers) {
597 ret = gst_pad_push (GST_PAD_CAST (pad), buf);
601 cret = gst_ogg_demux_combine_flows (ogg, pad, ret);
604 /* we're done with skeleton stuff */
605 if (pad->map.is_skeleton)
608 /* check if valid granulepos, then we can calculate the current
609 * position. We know the granule for each packet but we only want to update
610 * the last_stop when we have a valid granulepos on the packet because else
611 * our time jumps around for the different streams. */
612 if (packet->granulepos < 0)
615 /* convert to time */
616 current_time = gst_ogg_stream_get_end_time_for_granulepos (&pad->map,
619 /* convert to stream time */
620 if ((chain = pad->chain)) {
621 gint64 chain_start = 0;
623 if (chain->segment_start != GST_CLOCK_TIME_NONE)
624 chain_start = chain->segment_start;
626 current_time = current_time - chain_start + chain->begin_time;
629 /* and store as the current position */
630 gst_segment_set_last_stop (&ogg->segment, GST_FORMAT_TIME, current_time);
632 GST_DEBUG_OBJECT (ogg, "ogg current time %" GST_TIME_FORMAT,
633 GST_TIME_ARGS (current_time));
635 /* check stream eos */
636 if ((ogg->segment.rate > 0.0 && ogg->segment.stop != GST_CLOCK_TIME_NONE &&
637 current_time > ogg->segment.stop) ||
638 (ogg->segment.rate < 0.0 && ogg->segment.start != GST_CLOCK_TIME_NONE &&
639 current_time < ogg->segment.start)) {
640 GST_DEBUG_OBJECT (ogg, "marking pad %p EOS", pad);
646 gst_buffer_unref (buf);
647 /* return combined flow result */
653 GST_DEBUG_OBJECT (ogg, "Skipping empty packet");
654 cret = gst_ogg_demux_combine_flows (ogg, pad, GST_FLOW_OK);
660 GST_DEBUG_OBJECT (ogg, "skipping packet: no valid granule found yet");
661 cret = gst_ogg_demux_combine_flows (ogg, pad, GST_FLOW_OK);
667 GST_DEBUG_OBJECT (ogg,
668 "%p could not get buffer from peer %08lx, %d (%s), combined %d (%s)",
669 pad, pad->map.serialno, ret, gst_flow_get_name (ret),
670 cret, gst_flow_get_name (cret));
676 gst_ogg_demux_collect_start_time (GstOggDemux * ogg, GstOggChain * chain)
679 guint64 start_time = G_MAXUINT64;
681 for (i = 0; i < chain->streams->len; i++) {
682 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
684 if (pad->map.is_sparse)
687 /* can do this if the pad start time is not defined */
688 if (pad->start_time == GST_CLOCK_TIME_NONE) {
689 start_time = G_MAXUINT64;
692 start_time = MIN (start_time, pad->start_time);
698 /* submit a packet to the oggpad, this function will run the
699 * typefind code for the pad if this is the first packet for this
703 gst_ogg_pad_submit_packet (GstOggPad * pad, ogg_packet * packet)
706 GstFlowReturn ret = GST_FLOW_OK;
708 GstOggDemux *ogg = pad->ogg;
710 GST_DEBUG_OBJECT (ogg, "%p submit packet serial %08lx", pad,
713 if (!pad->have_type) {
714 pad->have_type = gst_ogg_stream_setup_map (&pad->map, packet);
715 if (!pad->have_type) {
716 pad->map.caps = gst_caps_new_simple ("application/x-unknown", NULL);
718 if (pad->map.is_skeleton) {
719 GST_DEBUG_OBJECT (ogg, "we have a fishead");
720 /* copy values over to global ogg level */
721 ogg->basetime = pad->map.basetime;
722 ogg->prestime = pad->map.prestime;
724 /* use total time to update the total ogg time */
725 if (ogg->total_time == -1) {
726 ogg->total_time = pad->map.total_time;
727 } else if (pad->map.total_time > 0) {
728 ogg->total_time = MAX (ogg->total_time, pad->map.total_time);
732 gst_pad_set_caps (GST_PAD (pad), pad->map.caps);
734 GST_WARNING_OBJECT (ogg, "stream parser didn't create src pad caps");
738 if (pad->map.is_skeleton) {
743 /* try to parse the serialno first */
744 if (gst_ogg_map_parse_fisbone (&pad->map, packet->packet, packet->bytes,
747 GST_WARNING_OBJECT (pad->ogg,
748 "got skeleton packet for stream 0x%08x", serialno);
750 skel_pad = gst_ogg_chain_get_stream (pad->chain, serialno);
753 case GST_OGG_SKELETON_FISBONE:
754 /* parse the remainder of the fisbone in the pad with the serialno,
755 * note that we ignore the start_time as this is usually wrong for
757 gst_ogg_map_add_fisbone (&skel_pad->map, &pad->map, packet->packet,
758 packet->bytes, NULL);
760 case GST_OGG_SKELETON_INDEX:
761 gst_ogg_map_add_index (&skel_pad->map, &pad->map, packet->packet,
764 /* use total time to update the total ogg time */
765 if (ogg->total_time == -1) {
766 ogg->total_time = skel_pad->map.total_time;
767 } else if (skel_pad->map.total_time > 0) {
768 ogg->total_time = MAX (ogg->total_time, skel_pad->map.total_time);
776 GST_WARNING_OBJECT (pad->ogg,
777 "found skeleton fisbone for an unknown stream 0x%08x", serialno);
782 granule = gst_ogg_stream_granulepos_to_granule (&pad->map,
785 GST_DEBUG_OBJECT (ogg, "%p has granulepos %" G_GINT64_FORMAT, pad, granule);
786 pad->current_granule = granule;
789 /* restart header packet count when seeing a b_o_s page;
790 * particularly useful following a seek or even following chain finding */
792 GST_DEBUG_OBJECT (ogg, "b_o_s packet, resetting header packet count");
793 pad->map.n_header_packets_seen = 0;
794 if (!pad->map.have_headers) {
795 GST_DEBUG_OBJECT (ogg, "clearing header packets");
796 g_list_foreach (pad->map.headers, (GFunc) _ogg_packet_free, NULL);
797 g_list_free (pad->map.headers);
798 pad->map.headers = NULL;
802 /* Overload the value of b_o_s in ogg_packet with a flag whether or
803 * not this is a header packet. Maybe some day this could be cleaned
805 packet->b_o_s = gst_ogg_stream_packet_is_header (&pad->map, packet);
806 if (!packet->b_o_s) {
807 GST_DEBUG ("found non-header packet");
808 pad->map.have_headers = TRUE;
809 if (pad->start_time == GST_CLOCK_TIME_NONE) {
810 gint64 duration = gst_ogg_stream_get_packet_duration (&pad->map, packet);
811 GST_DEBUG ("duration %" G_GINT64_FORMAT, duration);
812 if (duration != -1) {
813 pad->map.accumulated_granule += duration;
814 GST_DEBUG ("accumulated granule %" G_GINT64_FORMAT,
815 pad->map.accumulated_granule);
818 if (packet->granulepos != -1) {
819 ogg_int64_t start_granule;
822 granule = gst_ogg_stream_granulepos_to_granule (&pad->map,
825 if (granule > pad->map.accumulated_granule)
826 start_granule = granule - pad->map.accumulated_granule;
830 pad->start_time = gst_ogg_stream_granule_to_time (&pad->map,
832 GST_DEBUG ("start time %" G_GINT64_FORMAT, pad->start_time);
834 packet->granulepos = gst_ogg_stream_granule_to_granulepos (&pad->map,
835 pad->map.accumulated_granule, pad->keyframe_granule);
839 /* look for tags in header packet (before inc header count) */
840 gst_ogg_stream_extract_tags (&pad->map, packet);
841 pad->map.n_header_packets_seen++;
842 if (!pad->map.have_headers) {
844 g_list_append (pad->map.headers, _ogg_packet_copy (packet));
845 GST_DEBUG ("keeping header packet %d", pad->map.n_header_packets_seen);
849 /* we know the start_time of the pad data, see if we
850 * can activate the complete chain if this is a dynamic
851 * chain. We need all the headers too for this. */
852 if (pad->start_time != GST_CLOCK_TIME_NONE && pad->map.have_headers) {
853 GstOggChain *chain = pad->chain;
855 /* check if complete chain has start time */
856 if (chain == ogg->building_chain) {
857 GstEvent *event = NULL;
862 GST_DEBUG_OBJECT (ogg, "need to resync");
864 /* when we need to resync after a seek, we wait until we have received
865 * timestamps on all streams */
866 start_time = gst_ogg_demux_collect_start_time (ogg, chain);
868 if (start_time != G_MAXUINT64) {
871 GST_DEBUG_OBJECT (ogg, "start_time: %" GST_TIME_FORMAT,
872 GST_TIME_ARGS (start_time));
874 if (chain->segment_start < start_time)
876 (start_time - chain->segment_start) + chain->begin_time;
878 segment_time = chain->begin_time;
880 /* create the newsegment event we are going to send out */
881 event = gst_event_new_new_segment (FALSE, ogg->segment.rate,
882 GST_FORMAT_TIME, start_time, chain->segment_stop, segment_time);
887 /* see if we have enough info to activate the chain, we have enough info
888 * when all streams have a valid start time. */
889 if (gst_ogg_demux_collect_chain_info (ogg, chain)) {
891 GST_DEBUG_OBJECT (ogg, "segment_start: %" GST_TIME_FORMAT,
892 GST_TIME_ARGS (chain->segment_start));
893 GST_DEBUG_OBJECT (ogg, "segment_stop: %" GST_TIME_FORMAT,
894 GST_TIME_ARGS (chain->segment_stop));
895 GST_DEBUG_OBJECT (ogg, "segment_time: %" GST_TIME_FORMAT,
896 GST_TIME_ARGS (chain->begin_time));
898 /* create the newsegment event we are going to send out */
899 event = gst_event_new_new_segment (FALSE, ogg->segment.rate,
900 GST_FORMAT_TIME, chain->segment_start, chain->segment_stop,
906 gst_event_set_seqnum (event, ogg->seqnum);
908 gst_ogg_demux_activate_chain (ogg, chain, event);
910 ogg->building_chain = NULL;
915 /* if we are building a chain, store buffer for when we activate
916 * it. This path is taken if we operate in streaming mode. */
917 if (ogg->building_chain) {
918 /* bos packets where stored in the header list so we can discard
921 ret = gst_ogg_demux_queue_data (pad, packet);
923 /* else we are completely streaming to the peer */
925 ret = gst_ogg_demux_chain_peer (pad, packet, !ogg->pullmode);
930 /* flush at most @npackets from the stream layer. All packets if
934 gst_ogg_pad_stream_out (GstOggPad * pad, gint npackets)
936 GstFlowReturn result = GST_FLOW_OK;
937 gboolean done = FALSE;
946 ret = ogg_stream_packetout (&pad->map.stream, &packet);
949 GST_LOG_OBJECT (ogg, "packetout done");
953 GST_LOG_OBJECT (ogg, "packetout discont");
954 gst_ogg_chain_mark_discont (pad->chain);
957 GST_LOG_OBJECT (ogg, "packetout gave packet of size %ld", packet.bytes);
958 result = gst_ogg_pad_submit_packet (pad, &packet);
959 /* not linked is not a problem, it's possible that we are still
960 * collecting headers and that we don't have exposed the pads yet */
961 if (result == GST_FLOW_NOT_LINKED)
963 else if (result <= GST_FLOW_UNEXPECTED)
964 goto could_not_submit;
967 GST_WARNING_OBJECT (ogg,
968 "invalid return value %d for ogg_stream_packetout, resetting stream",
970 gst_ogg_pad_reset (pad);
975 done = (npackets == 0);
983 GST_WARNING_OBJECT (ogg,
984 "could not submit packet for stream %08lx, error: %d",
985 pad->map.serialno, result);
986 gst_ogg_pad_reset (pad);
991 /* submit a page to an oggpad, this function will then submit all
992 * the packets in the page.
995 gst_ogg_pad_submit_page (GstOggPad * pad, ogg_page * page)
997 GstFlowReturn result = GST_FLOW_OK;
999 gboolean continued = FALSE;
1003 /* for negative rates we read pages backwards and must therefore be carefull
1004 * with continued pages */
1005 if (ogg->segment.rate < 0.0) {
1008 continued = ogg_page_continued (page);
1010 /* number of completed packets in the page */
1011 npackets = ogg_page_packets (page);
1013 /* page is not continued so it contains at least one packet start. It's
1014 * possible that no packet ends on this page (npackets == 0). In that
1015 * case, the next (continued) page(s) we kept contain the remainder of the
1016 * packets. We mark npackets=1 to make us start decoding the pages in the
1017 * remainder of the algorithm. */
1021 GST_LOG_OBJECT (ogg, "continued: %d, %d packets", continued, npackets);
1023 if (npackets == 0) {
1024 GST_LOG_OBJECT (ogg, "no decodable packets, we need a previous page");
1029 if (ogg_stream_pagein (&pad->map.stream, page) != 0)
1032 /* flush all packets in the stream layer, this might not give a packet if
1033 * the page had no packets finishing on the page (npackets == 0). */
1034 result = gst_ogg_pad_stream_out (pad, 0);
1036 if (pad->continued) {
1039 /* now send the continued pages to the stream layer */
1040 while (pad->continued) {
1041 ogg_page *p = (ogg_page *) pad->continued->data;
1043 GST_LOG_OBJECT (ogg, "submitting continued page %p", p);
1044 if (ogg_stream_pagein (&pad->map.stream, p) != 0)
1047 pad->continued = g_list_delete_link (pad->continued, pad->continued);
1050 gst_ogg_page_free (p);
1053 GST_LOG_OBJECT (ogg, "flushing last continued packet");
1054 /* flush 1 continued packet in the stream layer */
1055 result = gst_ogg_pad_stream_out (pad, 1);
1057 /* flush all remaining packets, we pushed them in the previous round.
1058 * We don't use _reset() because we still want to get the discont when
1059 * we submit a next page. */
1060 while (ogg_stream_packetout (&pad->map.stream, &packet) != 0);
1064 /* keep continued pages (only in reverse mode) */
1066 ogg_page *p = gst_ogg_page_copy (page);
1068 GST_LOG_OBJECT (ogg, "keeping continued page %p", p);
1069 pad->continued = g_list_prepend (pad->continued, p);
1076 GST_WARNING_OBJECT (ogg,
1077 "ogg stream choked on page (serial %08lx), resetting stream",
1079 gst_ogg_pad_reset (pad);
1080 /* we continue to recover */
1086 static GstOggChain *
1087 gst_ogg_chain_new (GstOggDemux * ogg)
1089 GstOggChain *chain = g_new0 (GstOggChain, 1);
1091 GST_DEBUG_OBJECT (ogg, "creating new chain %p", chain);
1095 chain->have_bos = FALSE;
1096 chain->streams = g_array_new (FALSE, TRUE, sizeof (GstOggPad *));
1097 chain->begin_time = GST_CLOCK_TIME_NONE;
1098 chain->segment_start = GST_CLOCK_TIME_NONE;
1099 chain->segment_stop = GST_CLOCK_TIME_NONE;
1100 chain->total_time = GST_CLOCK_TIME_NONE;
1106 gst_ogg_chain_free (GstOggChain * chain)
1110 for (i = 0; i < chain->streams->len; i++) {
1111 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
1113 gst_object_unref (pad);
1115 g_array_free (chain->streams, TRUE);
1120 gst_ogg_pad_mark_discont (GstOggPad * pad)
1122 pad->discont = TRUE;
1123 pad->map.last_size = 0;
1127 gst_ogg_chain_mark_discont (GstOggChain * chain)
1131 for (i = 0; i < chain->streams->len; i++) {
1132 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
1134 gst_ogg_pad_mark_discont (pad);
1139 gst_ogg_chain_reset (GstOggChain * chain)
1143 for (i = 0; i < chain->streams->len; i++) {
1144 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
1146 gst_ogg_pad_reset (pad);
1151 gst_ogg_chain_new_stream (GstOggChain * chain, glong serialno)
1157 GST_DEBUG_OBJECT (chain->ogg, "creating new stream %08lx in chain %p",
1160 ret = g_object_new (GST_TYPE_OGG_PAD, NULL);
1161 /* we own this one */
1162 gst_object_ref (ret);
1163 gst_object_sink (ret);
1165 GST_PAD_DIRECTION (ret) = GST_PAD_SRC;
1166 gst_ogg_pad_mark_discont (ret);
1169 ret->ogg = chain->ogg;
1171 ret->map.serialno = serialno;
1172 if (ogg_stream_init (&ret->map.stream, serialno) != 0)
1175 name = g_strdup_printf ("serial_%08lx", serialno);
1176 gst_object_set_name (GST_OBJECT (ret), name);
1179 /* FIXME: either do something with it or remove it */
1180 list = gst_tag_list_new ();
1181 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, GST_TAG_SERIAL, serialno,
1183 gst_tag_list_free (list);
1185 GST_DEBUG_OBJECT (chain->ogg,
1186 "created new ogg src %p for stream with serial %08lx", ret, serialno);
1188 g_array_append_val (chain->streams, ret);
1195 GST_ERROR ("Could not initialize ogg_stream struct for serial %08lx.",
1197 gst_object_unref (ret);
1203 gst_ogg_chain_get_stream (GstOggChain * chain, glong serialno)
1207 for (i = 0; i < chain->streams->len; i++) {
1208 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
1210 if (pad->map.serialno == serialno)
1217 gst_ogg_chain_has_stream (GstOggChain * chain, glong serialno)
1219 return gst_ogg_chain_get_stream (chain, serialno) != NULL;
1222 /* signals and args */
1235 static GstStaticPadTemplate ogg_demux_src_template_factory =
1236 GST_STATIC_PAD_TEMPLATE ("src_%d",
1239 GST_STATIC_CAPS_ANY);
1241 static GstStaticPadTemplate ogg_demux_sink_template_factory =
1242 GST_STATIC_PAD_TEMPLATE ("sink",
1245 GST_STATIC_CAPS ("application/ogg; application/x-annodex")
1248 static void gst_ogg_demux_finalize (GObject * object);
1250 static GstFlowReturn gst_ogg_demux_read_chain (GstOggDemux * ogg,
1251 GstOggChain ** chain);
1252 static GstFlowReturn gst_ogg_demux_read_end_chain (GstOggDemux * ogg,
1253 GstOggChain * chain);
1255 static gboolean gst_ogg_demux_sink_event (GstPad * pad, GstEvent * event);
1256 static void gst_ogg_demux_loop (GstOggPad * pad);
1257 static GstFlowReturn gst_ogg_demux_chain (GstPad * pad, GstBuffer * buffer);
1258 static gboolean gst_ogg_demux_sink_activate (GstPad * sinkpad);
1259 static gboolean gst_ogg_demux_sink_activate_pull (GstPad * sinkpad,
1261 static gboolean gst_ogg_demux_sink_activate_push (GstPad * sinkpad,
1263 static GstStateChangeReturn gst_ogg_demux_change_state (GstElement * element,
1264 GstStateChange transition);
1265 static gboolean gst_ogg_demux_send_event (GstOggDemux * ogg, GstEvent * event);
1267 static void gst_ogg_print (GstOggDemux * demux);
1269 GST_BOILERPLATE (GstOggDemux, gst_ogg_demux, GstElement, GST_TYPE_ELEMENT);
1272 gst_ogg_demux_base_init (gpointer g_class)
1274 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1276 gst_element_class_set_details_simple (element_class,
1277 "Ogg demuxer", "Codec/Demuxer",
1278 "demux ogg streams (info about ogg: http://xiph.org)",
1279 "Wim Taymans <wim@fluendo.com>");
1281 gst_element_class_add_pad_template (element_class,
1282 gst_static_pad_template_get (&ogg_demux_sink_template_factory));
1283 gst_element_class_add_pad_template (element_class,
1284 gst_static_pad_template_get (&ogg_demux_src_template_factory));
1288 gst_ogg_demux_class_init (GstOggDemuxClass * klass)
1290 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1291 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1293 gstelement_class->change_state = gst_ogg_demux_change_state;
1294 gstelement_class->send_event = gst_ogg_demux_receive_event;
1296 gobject_class->finalize = gst_ogg_demux_finalize;
1300 gst_ogg_demux_init (GstOggDemux * ogg, GstOggDemuxClass * g_class)
1302 /* create the sink pad */
1304 gst_pad_new_from_static_template (&ogg_demux_sink_template_factory,
1307 gst_pad_set_event_function (ogg->sinkpad, gst_ogg_demux_sink_event);
1308 gst_pad_set_chain_function (ogg->sinkpad, gst_ogg_demux_chain);
1309 gst_pad_set_activate_function (ogg->sinkpad, gst_ogg_demux_sink_activate);
1310 gst_pad_set_activatepull_function (ogg->sinkpad,
1311 gst_ogg_demux_sink_activate_pull);
1312 gst_pad_set_activatepush_function (ogg->sinkpad,
1313 gst_ogg_demux_sink_activate_push);
1314 gst_element_add_pad (GST_ELEMENT (ogg), ogg->sinkpad);
1316 ogg->chain_lock = g_mutex_new ();
1317 ogg->chains = g_array_new (FALSE, TRUE, sizeof (GstOggChain *));
1319 ogg->newsegment = NULL;
1323 gst_ogg_demux_finalize (GObject * object)
1327 ogg = GST_OGG_DEMUX (object);
1329 g_array_free (ogg->chains, TRUE);
1330 g_mutex_free (ogg->chain_lock);
1331 ogg_sync_clear (&ogg->sync);
1333 if (ogg->newsegment)
1334 gst_event_unref (ogg->newsegment);
1336 G_OBJECT_CLASS (parent_class)->finalize (object);
1340 gst_ogg_demux_reset_streams (GstOggDemux * ogg)
1345 chain = ogg->current_chain;
1349 for (i = 0; i < chain->streams->len; i++) {
1350 GstOggPad *stream = g_array_index (chain->streams, GstOggPad *, i);
1352 stream->start_time = -1;
1353 stream->map.accumulated_granule = 0;
1355 ogg->building_chain = chain;
1356 ogg->current_chain = NULL;
1361 gst_ogg_demux_sink_event (GstPad * pad, GstEvent * event)
1366 ogg = GST_OGG_DEMUX (gst_pad_get_parent (pad));
1368 switch (GST_EVENT_TYPE (event)) {
1369 case GST_EVENT_FLUSH_START:
1370 res = gst_ogg_demux_send_event (ogg, event);
1372 case GST_EVENT_FLUSH_STOP:
1373 GST_DEBUG_OBJECT (ogg, "got a flush stop event");
1374 ogg_sync_reset (&ogg->sync);
1375 res = gst_ogg_demux_send_event (ogg, event);
1376 gst_ogg_demux_reset_streams (ogg);
1378 case GST_EVENT_NEWSEGMENT:
1379 GST_DEBUG_OBJECT (ogg, "got a new segment event");
1380 gst_event_unref (event);
1385 GST_DEBUG_OBJECT (ogg, "got an EOS event");
1386 res = gst_ogg_demux_send_event (ogg, event);
1387 if (ogg->current_chain == NULL) {
1388 GST_ELEMENT_ERROR (ogg, STREAM, DEMUX, (NULL),
1389 ("can't get first chain"));
1394 res = gst_ogg_demux_send_event (ogg, event);
1397 gst_object_unref (ogg);
1402 /* submit the given buffer to the ogg sync.
1404 * Returns the number of bytes submited.
1406 static GstFlowReturn
1407 gst_ogg_demux_submit_buffer (GstOggDemux * ogg, GstBuffer * buffer)
1412 GstFlowReturn ret = GST_FLOW_OK;
1414 size = GST_BUFFER_SIZE (buffer);
1415 data = GST_BUFFER_DATA (buffer);
1417 GST_DEBUG_OBJECT (ogg, "submitting %u bytes", size);
1418 if (G_UNLIKELY (size == 0))
1421 oggbuffer = ogg_sync_buffer (&ogg->sync, size);
1422 if (G_UNLIKELY (oggbuffer == NULL))
1425 memcpy (oggbuffer, data, size);
1426 if (G_UNLIKELY (ogg_sync_wrote (&ogg->sync, size) < 0))
1430 gst_buffer_unref (buffer);
1437 GST_ELEMENT_ERROR (ogg, STREAM, DECODE,
1438 (NULL), ("failed to get ogg sync buffer"));
1439 ret = GST_FLOW_ERROR;
1444 GST_ELEMENT_ERROR (ogg, STREAM, DECODE,
1445 (NULL), ("failed to write %d bytes to the sync buffer", size));
1446 ret = GST_FLOW_ERROR;
1451 /* in random access mode this code updates the current read position
1452 * and resets the ogg sync buffer so that the next read will happen
1453 * from this new location.
1456 gst_ogg_demux_seek (GstOggDemux * ogg, gint64 offset)
1458 GST_LOG_OBJECT (ogg, "seeking to %" G_GINT64_FORMAT, offset);
1460 ogg->offset = offset;
1461 ogg->read_offset = offset;
1462 ogg_sync_reset (&ogg->sync);
1465 /* read more data from the current offset and submit to
1466 * the ogg sync layer.
1468 static GstFlowReturn
1469 gst_ogg_demux_get_data (GstOggDemux * ogg, gint64 end_offset)
1474 GST_LOG_OBJECT (ogg,
1475 "get data %" G_GINT64_FORMAT " %" G_GINT64_FORMAT " %" G_GINT64_FORMAT,
1476 ogg->read_offset, ogg->length, end_offset);
1478 if (end_offset > 0 && ogg->read_offset >= end_offset)
1479 goto boundary_reached;
1481 if (ogg->read_offset == ogg->length)
1484 ret = gst_pad_pull_range (ogg->sinkpad, ogg->read_offset, CHUNKSIZE, &buffer);
1485 if (ret != GST_FLOW_OK)
1488 ogg->read_offset += GST_BUFFER_SIZE (buffer);
1490 ret = gst_ogg_demux_submit_buffer (ogg, buffer);
1497 GST_LOG_OBJECT (ogg, "reached boundary");
1498 return GST_FLOW_LIMIT;
1502 GST_LOG_OBJECT (ogg, "reached EOS");
1503 return GST_FLOW_UNEXPECTED;
1507 GST_WARNING_OBJECT (ogg, "got %d (%s) from pull range", ret,
1508 gst_flow_get_name (ret));
1513 /* Read the next page from the current offset.
1514 * boundary: number of bytes ahead we allow looking for;
1517 * @offset will contain the offset the next page starts at when this function
1518 * returns GST_FLOW_OK.
1520 * GST_FLOW_UNEXPECTED is returned on EOS.
1522 * GST_FLOW_LIMIT is returned when we did not find a page before the
1523 * boundary. If @boundary is -1, this is never returned.
1525 * Any other error returned while retrieving data from the peer is returned as
1528 static GstFlowReturn
1529 gst_ogg_demux_get_next_page (GstOggDemux * ogg, ogg_page * og,
1530 gint64 boundary, gint64 * offset)
1532 gint64 end_offset = -1;
1535 GST_LOG_OBJECT (ogg,
1536 "get next page, current offset %" G_GINT64_FORMAT ", bytes boundary %"
1537 G_GINT64_FORMAT, ogg->offset, boundary);
1540 end_offset = ogg->offset + boundary;
1545 if (end_offset > 0 && ogg->offset >= end_offset)
1546 goto boundary_reached;
1548 more = ogg_sync_pageseek (&ogg->sync, og);
1550 GST_LOG_OBJECT (ogg, "pageseek gave %ld", more);
1553 /* skipped n bytes */
1554 ogg->offset -= more;
1555 GST_LOG_OBJECT (ogg, "skipped %ld bytes, offset %" G_GINT64_FORMAT,
1557 } else if (more == 0) {
1558 /* we need more data */
1560 goto boundary_reached;
1562 GST_LOG_OBJECT (ogg, "need more data");
1563 ret = gst_ogg_demux_get_data (ogg, end_offset);
1564 if (ret != GST_FLOW_OK)
1567 gint64 res_offset = ogg->offset;
1569 /* got a page. Return the offset at the page beginning,
1570 advance the internal offset past the page end */
1572 *offset = res_offset;
1575 ogg->offset += more;
1577 GST_LOG_OBJECT (ogg,
1578 "got page at %" G_GINT64_FORMAT ", serial %08x, end at %"
1579 G_GINT64_FORMAT ", granule %" G_GINT64_FORMAT, res_offset,
1580 ogg_page_serialno (og), ogg->offset,
1581 (gint64) ogg_page_granulepos (og));
1585 GST_LOG_OBJECT (ogg, "returning %d", ret);
1592 GST_LOG_OBJECT (ogg,
1593 "offset %" G_GINT64_FORMAT " >= end_offset %" G_GINT64_FORMAT,
1594 ogg->offset, end_offset);
1595 return GST_FLOW_LIMIT;
1599 /* from the current offset, find the previous page, seeking backwards
1600 * until we find the page.
1602 static GstFlowReturn
1603 gst_ogg_demux_get_prev_page (GstOggDemux * ogg, ogg_page * og, gint64 * offset)
1606 gint64 begin = ogg->offset;
1608 gint64 cur_offset = -1;
1610 GST_LOG_OBJECT (ogg, "getting page before %" G_GINT64_FORMAT, begin);
1612 while (cur_offset == -1) {
1617 /* seek CHUNKSIZE back */
1618 gst_ogg_demux_seek (ogg, begin);
1620 /* now continue reading until we run out of data, if we find a page
1621 * start, we save it. It might not be the final page as there could be
1622 * another page after this one. */
1623 while (ogg->offset < end) {
1627 gst_ogg_demux_get_next_page (ogg, og, end - ogg->offset, &new_offset);
1628 /* we hit the upper limit, offset contains the last page start */
1629 if (ret == GST_FLOW_LIMIT) {
1630 GST_LOG_OBJECT (ogg, "hit limit");
1633 /* something went wrong */
1634 if (ret == GST_FLOW_UNEXPECTED) {
1636 GST_LOG_OBJECT (ogg, "got unexpected");
1637 } else if (ret != GST_FLOW_OK) {
1638 GST_LOG_OBJECT (ogg, "got error %d", ret);
1642 GST_LOG_OBJECT (ogg, "found page at %" G_GINT64_FORMAT, new_offset);
1644 /* offset is next page start */
1645 cur_offset = new_offset;
1649 GST_LOG_OBJECT (ogg, "found previous page at %" G_GINT64_FORMAT, cur_offset);
1651 /* we have the offset. Actually snork and hold the page now */
1652 gst_ogg_demux_seek (ogg, cur_offset);
1653 ret = gst_ogg_demux_get_next_page (ogg, og, -1, NULL);
1654 if (ret != GST_FLOW_OK) {
1655 GST_WARNING_OBJECT (ogg, "can't get last page at %" G_GINT64_FORMAT,
1657 /* this shouldn't be possible */
1662 *offset = cur_offset;
1668 gst_ogg_demux_deactivate_current_chain (GstOggDemux * ogg)
1671 GstOggChain *chain = ogg->current_chain;
1676 GST_DEBUG_OBJECT (ogg, "deactivating chain %p", chain);
1678 /* send EOS on all the pads */
1679 for (i = 0; i < chain->streams->len; i++) {
1680 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
1686 event = gst_event_new_eos ();
1687 gst_event_set_seqnum (event, ogg->seqnum);
1688 gst_pad_push_event (GST_PAD_CAST (pad), event);
1690 GST_DEBUG_OBJECT (ogg, "removing pad %" GST_PTR_FORMAT, pad);
1692 /* deactivate first */
1693 gst_pad_set_active (GST_PAD_CAST (pad), FALSE);
1695 gst_element_remove_pad (GST_ELEMENT (ogg), GST_PAD_CAST (pad));
1699 /* if we cannot seek back to the chain, we can destroy the chain
1701 if (!ogg->pullmode) {
1702 gst_ogg_chain_free (chain);
1704 ogg->current_chain = NULL;
1710 gst_ogg_demux_activate_chain (GstOggDemux * ogg, GstOggChain * chain,
1714 gint bitrate, idx_bitrate;
1716 g_return_val_if_fail (chain != NULL, FALSE);
1718 if (chain == ogg->current_chain) {
1720 gst_event_unref (event);
1725 GST_DEBUG_OBJECT (ogg, "activating chain %p", chain);
1727 bitrate = idx_bitrate = 0;
1729 /* first add the pads */
1730 for (i = 0; i < chain->streams->len; i++) {
1732 GstStructure *structure;
1734 pad = g_array_index (chain->streams, GstOggPad *, i);
1736 if (pad->map.idx_bitrate)
1737 idx_bitrate = MAX (idx_bitrate, pad->map.idx_bitrate);
1739 bitrate += pad->map.bitrate;
1742 gst_ogg_pad_mark_discont (pad);
1743 pad->last_ret = GST_FLOW_OK;
1745 if (pad->map.is_skeleton || pad->added || GST_PAD_CAPS (pad) == NULL)
1748 GST_DEBUG_OBJECT (ogg, "adding pad %" GST_PTR_FORMAT, pad);
1750 structure = gst_caps_get_structure (GST_PAD_CAPS (pad), 0);
1752 /* activate first */
1753 gst_pad_set_active (GST_PAD_CAST (pad), TRUE);
1755 gst_element_add_pad (GST_ELEMENT (ogg), GST_PAD_CAST (pad));
1758 /* prefer the index bitrate over the ones encoded in the streams */
1759 ogg->bitrate = (idx_bitrate ? idx_bitrate : bitrate);
1761 /* after adding the new pads, remove the old pads */
1762 gst_ogg_demux_deactivate_current_chain (ogg);
1764 ogg->current_chain = chain;
1766 /* we are finished now */
1767 gst_element_no_more_pads (GST_ELEMENT (ogg));
1769 /* FIXME, must be sent from the streaming thread */
1771 gst_ogg_demux_send_event (ogg, event);
1773 gst_element_found_tags (GST_ELEMENT_CAST (ogg),
1774 gst_tag_list_new_full (GST_TAG_CONTAINER_FORMAT, "Ogg", NULL));
1777 GST_DEBUG_OBJECT (ogg, "starting chain");
1779 /* then send out any headers and queued packets */
1780 for (i = 0; i < chain->streams->len; i++) {
1784 pad = g_array_index (chain->streams, GstOggPad *, i);
1786 /* FIXME also streaming thread */
1787 if (pad->map.taglist) {
1788 GST_DEBUG_OBJECT (ogg, "pushing tags");
1789 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (ogg),
1790 GST_PAD_CAST (pad), pad->map.taglist);
1791 pad->map.taglist = NULL;
1794 GST_DEBUG_OBJECT (ogg, "pushing headers");
1796 for (walk = pad->map.headers; walk; walk = g_list_next (walk)) {
1797 ogg_packet *p = walk->data;
1799 gst_ogg_demux_chain_peer (pad, p, TRUE);
1802 GST_DEBUG_OBJECT (ogg, "pushing queued buffers");
1803 /* push queued packets */
1804 for (walk = pad->map.queued; walk; walk = g_list_next (walk)) {
1805 ogg_packet *p = walk->data;
1807 gst_ogg_demux_chain_peer (pad, p, TRUE);
1808 _ogg_packet_free (p);
1810 /* and free the queued buffers */
1811 g_list_free (pad->map.queued);
1812 pad->map.queued = NULL;
1818 do_binary_search (GstOggDemux * ogg, GstOggChain * chain, gint64 begin,
1819 gint64 end, gint64 begintime, gint64 endtime, gint64 target,
1828 GST_DEBUG_OBJECT (ogg,
1829 "chain offset %" G_GINT64_FORMAT ", end offset %" G_GINT64_FORMAT,
1831 GST_DEBUG_OBJECT (ogg,
1832 "chain begin time %" GST_TIME_FORMAT ", end time %" GST_TIME_FORMAT,
1833 GST_TIME_ARGS (begintime), GST_TIME_ARGS (endtime));
1834 GST_DEBUG_OBJECT (ogg, "target %" GST_TIME_FORMAT, GST_TIME_ARGS (target));
1836 /* perform the seek */
1837 while (begin < end) {
1840 if ((end - begin < CHUNKSIZE) || (endtime == begintime)) {
1843 /* take a (pretty decent) guess, avoiding overflow */
1844 gint64 rate = (end - begin) * GST_MSECOND / (endtime - begintime);
1846 bisect = (target - begintime) / GST_MSECOND * rate + begin - CHUNKSIZE;
1848 if (bisect <= begin)
1850 GST_DEBUG_OBJECT (ogg, "Initial guess: %" G_GINT64_FORMAT, bisect);
1852 gst_ogg_demux_seek (ogg, bisect);
1854 while (begin < end) {
1857 GST_DEBUG_OBJECT (ogg,
1858 "after seek, bisect %" G_GINT64_FORMAT ", begin %" G_GINT64_FORMAT
1859 ", end %" G_GINT64_FORMAT, bisect, begin, end);
1861 ret = gst_ogg_demux_get_next_page (ogg, &og, end - ogg->offset, &result);
1862 GST_LOG_OBJECT (ogg, "looking for next page returned %" G_GINT64_FORMAT,
1865 if (ret == GST_FLOW_LIMIT) {
1866 /* we hit the upper limit, go back a bit */
1867 if (bisect <= begin + 1) {
1868 end = begin; /* found it */
1873 bisect -= CHUNKSIZE;
1874 if (bisect <= begin)
1877 gst_ogg_demux_seek (ogg, bisect);
1879 } else if (ret == GST_FLOW_OK) {
1880 /* found offset of next ogg page */
1882 GstClockTime granuletime;
1885 /* get the granulepos */
1886 GST_LOG_OBJECT (ogg, "found next ogg page at %" G_GINT64_FORMAT,
1888 granulepos = ogg_page_granulepos (&og);
1889 if (granulepos == -1) {
1890 GST_LOG_OBJECT (ogg, "granulepos of next page is -1");
1894 /* get the stream */
1895 pad = gst_ogg_chain_get_stream (chain, ogg_page_serialno (&og));
1896 if (pad == NULL || pad->map.is_skeleton)
1899 /* convert granulepos to time */
1900 granuletime = gst_ogg_stream_get_end_time_for_granulepos (&pad->map,
1902 if (granuletime < pad->start_time)
1905 GST_LOG_OBJECT (ogg, "granulepos %" G_GINT64_FORMAT " maps to time %"
1906 GST_TIME_FORMAT, granulepos, GST_TIME_ARGS (granuletime));
1908 granuletime -= pad->start_time;
1909 granuletime += chain->begin_time;
1911 GST_DEBUG_OBJECT (ogg,
1912 "found page with granule %" G_GINT64_FORMAT " and time %"
1913 GST_TIME_FORMAT, granulepos, GST_TIME_ARGS (granuletime));
1915 if (granuletime < target) {
1916 best = result; /* raw offset of packet with granulepos */
1917 begin = ogg->offset; /* raw offset of next page */
1918 begintime = granuletime;
1920 bisect = begin; /* *not* begin + 1 */
1922 if (bisect <= begin + 1) {
1923 end = begin; /* found it */
1925 if (end == ogg->offset) { /* we're pretty close - we'd be stuck in */
1927 bisect -= CHUNKSIZE; /* an endless loop otherwise. */
1928 if (bisect <= begin)
1930 gst_ogg_demux_seek (ogg, bisect);
1933 endtime = granuletime;
1942 GST_DEBUG_OBJECT (ogg, "seeking to %" G_GINT64_FORMAT, best);
1943 gst_ogg_demux_seek (ogg, best);
1951 GST_DEBUG_OBJECT (ogg, "got a seek error");
1957 do_index_search (GstOggDemux * ogg, GstOggChain * chain, gint64 begin,
1958 gint64 end, gint64 begintime, gint64 endtime, gint64 target,
1959 gint64 * p_offset, gint64 * p_timestamp)
1962 guint64 timestamp, offset;
1963 guint64 r_timestamp, r_offset;
1964 gboolean result = FALSE;
1966 target -= begintime;
1971 for (i = 0; i < chain->streams->len; i++) {
1972 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
1975 if (gst_ogg_map_search_index (&pad->map, TRUE, ×tamp, &offset)) {
1976 GST_INFO ("found %" G_GUINT64_FORMAT " at offset %" G_GUINT64_FORMAT,
1979 if (r_offset == -1 || offset < r_offset) {
1981 r_timestamp = timestamp;
1988 *p_timestamp = r_timestamp;
1990 *p_offset = r_offset;
1996 * do seek to time @position, return FALSE or chain and TRUE
1999 gst_ogg_demux_do_seek (GstOggDemux * ogg, GstSegment * segment,
2000 gboolean accurate, gboolean keyframe, GstOggChain ** rchain)
2003 GstOggChain *chain = NULL;
2005 gint64 begintime, endtime;
2006 gint64 target, keytarget;
2011 gint i, pending, len;
2012 gboolean first_parsed_page = TRUE;
2014 position = segment->last_stop;
2016 /* first find the chain to search in */
2017 total = ogg->total_time;
2018 if (ogg->chains->len == 0)
2021 for (i = ogg->chains->len - 1; i >= 0; i--) {
2022 chain = g_array_index (ogg->chains, GstOggChain *, i);
2023 total -= chain->total_time;
2024 if (position >= total)
2028 /* first step, locate page containing the required data */
2029 begin = chain->offset;
2030 end = chain->end_offset;
2031 begintime = chain->begin_time;
2032 endtime = begintime + chain->total_time;
2033 target = position - total + begintime;
2035 if (!do_binary_search (ogg, chain, begin, end, begintime, endtime, target,
2039 /* second step: find pages for all streams, we use the keyframe_granule to keep
2040 * track of which ones we saw. If we have seen a page for each stream we can
2041 * calculate the positions of each keyframe. */
2042 GST_DEBUG_OBJECT (ogg, "find keyframes");
2043 len = pending = chain->streams->len;
2045 /* figure out where the keyframes are */
2052 GstClockTime keyframe_time, granule_time;
2054 ret = gst_ogg_demux_get_next_page (ogg, &og, end - ogg->offset, &result);
2055 GST_LOG_OBJECT (ogg, "looking for next page returned %" G_GINT64_FORMAT,
2057 if (ret == GST_FLOW_LIMIT) {
2058 GST_LOG_OBJECT (ogg, "reached limit");
2060 } else if (ret != GST_FLOW_OK)
2063 /* get the stream */
2064 pad = gst_ogg_chain_get_stream (chain, ogg_page_serialno (&og));
2068 if (pad->map.is_skeleton)
2071 granulepos = ogg_page_granulepos (&og);
2072 if (granulepos == -1) {
2073 GST_LOG_OBJECT (ogg, "granulepos of next page is -1");
2077 /* we only do this the first time we pass here */
2078 if (first_parsed_page) {
2079 /* Now that we have a time reference from the page, we can check
2080 * whether all streams still have pages from here on.
2082 * This would be more elegant before the loop, but getting the page from
2083 * there without breaking anything would be more costly */
2084 granule_time = gst_ogg_stream_get_end_time_for_granulepos (&pad->map,
2086 for (i = 0; i < len; i++) {
2087 GstOggPad *stream = g_array_index (chain->streams, GstOggPad *, i);
2090 /* we already know we have at least one page (the current one)
2091 * for this stream */
2094 if (granule_time > stream->map.total_time)
2095 /* we won't encounter any more pages of this stream, so we don't
2096 * try finding a key frame for it */
2099 first_parsed_page = FALSE;
2103 /* in reverse we want to go past the page with the lower timestamp */
2104 if (segment->rate < 0.0) {
2105 /* get time for this pad */
2106 granule_time = gst_ogg_stream_get_end_time_for_granulepos (&pad->map,
2109 GST_LOG_OBJECT (ogg,
2110 "looking at page with ts %" GST_TIME_FORMAT ", target %"
2111 GST_TIME_FORMAT, GST_TIME_ARGS (granule_time),
2112 GST_TIME_ARGS (target));
2113 if (granule_time < target)
2117 /* we've seen this pad before */
2118 if (pad->keyframe_granule != -1)
2121 /* convert granule of this pad to the granule of the keyframe */
2122 pad->keyframe_granule =
2123 gst_ogg_stream_granulepos_to_key_granule (&pad->map, granulepos);
2124 GST_LOG_OBJECT (ogg, "marking stream granule %" G_GINT64_FORMAT,
2125 pad->keyframe_granule);
2127 /* get time of the keyframe */
2129 gst_ogg_stream_granule_to_time (&pad->map, pad->keyframe_granule);
2130 GST_LOG_OBJECT (ogg, "stream %08lx granule time %" GST_TIME_FORMAT,
2131 pad->map.serialno, GST_TIME_ARGS (keyframe_time));
2133 /* collect smallest value */
2134 if (keyframe_time != -1) {
2135 keyframe_time += begintime;
2136 if (keyframe_time < keytarget)
2137 keytarget = keyframe_time;
2146 /* for negative rates we will get to the keyframe backwards */
2147 if (segment->rate < 0.0)
2150 if (keytarget != target) {
2151 GST_LOG_OBJECT (ogg, "final seek to target %" GST_TIME_FORMAT,
2152 GST_TIME_ARGS (keytarget));
2154 /* last step, seek to the location of the keyframe */
2155 if (!do_binary_search (ogg, chain, begin, end, begintime, endtime,
2159 /* seek back to previous position */
2160 GST_LOG_OBJECT (ogg, "keyframe on target");
2161 gst_ogg_demux_seek (ogg, best);
2166 if (segment->rate > 0.0)
2167 segment->time = keytarget;
2168 segment->last_stop = keytarget - begintime;
2177 GST_DEBUG_OBJECT (ogg, "no chains");
2182 GST_DEBUG_OBJECT (ogg, "got a seek error");
2187 /* does not take ownership of the event */
2189 gst_ogg_demux_perform_seek_pull (GstOggDemux * ogg, GstEvent * event)
2191 GstOggChain *chain = NULL;
2193 gboolean flush, accurate, keyframe;
2197 GstSeekType cur_type, stop_type;
2204 GST_DEBUG_OBJECT (ogg, "seek with event");
2206 gst_event_parse_seek (event, &rate, &format, &flags,
2207 &cur_type, &cur, &stop_type, &stop);
2209 /* we can only seek on time */
2210 if (format != GST_FORMAT_TIME) {
2211 GST_DEBUG_OBJECT (ogg, "can only seek on TIME");
2214 seqnum = gst_event_get_seqnum (event);
2216 GST_DEBUG_OBJECT (ogg, "seek without event");
2220 seqnum = gst_util_seqnum_next ();
2223 GST_DEBUG_OBJECT (ogg, "seek, rate %g", rate);
2225 flush = flags & GST_SEEK_FLAG_FLUSH;
2226 accurate = flags & GST_SEEK_FLAG_ACCURATE;
2227 keyframe = flags & GST_SEEK_FLAG_KEY_UNIT;
2229 /* first step is to unlock the streaming thread if it is
2230 * blocked in a chain call, we do this by starting the flush. because
2231 * we cannot yet hold any streaming lock, we have to protect the chains
2232 * with their own lock. */
2236 tevent = gst_event_new_flush_start ();
2237 gst_event_set_seqnum (tevent, seqnum);
2239 gst_event_ref (tevent);
2240 gst_pad_push_event (ogg->sinkpad, tevent);
2242 GST_CHAIN_LOCK (ogg);
2243 for (i = 0; i < ogg->chains->len; i++) {
2244 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
2247 for (j = 0; j < chain->streams->len; j++) {
2248 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, j);
2250 gst_event_ref (tevent);
2251 gst_pad_push_event (GST_PAD (pad), tevent);
2254 GST_CHAIN_UNLOCK (ogg);
2256 gst_event_unref (tevent);
2258 gst_pad_pause_task (ogg->sinkpad);
2261 /* now grab the stream lock so that streaming cannot continue, for
2262 * non flushing seeks when the element is in PAUSED this could block
2264 GST_PAD_STREAM_LOCK (ogg->sinkpad);
2266 if (ogg->segment_running && !flush) {
2267 /* create the segment event to close the current segment */
2268 if ((chain = ogg->current_chain)) {
2270 gint64 chain_start = 0;
2272 if (chain->segment_start != GST_CLOCK_TIME_NONE)
2273 chain_start = chain->segment_start;
2275 newseg = gst_event_new_new_segment (TRUE, ogg->segment.rate,
2276 GST_FORMAT_TIME, ogg->segment.start + chain_start,
2277 ogg->segment.last_stop + chain_start, ogg->segment.time);
2278 /* set the seqnum of the running segment */
2279 gst_event_set_seqnum (newseg, ogg->seqnum);
2281 /* send segment on old chain, FIXME, must be sent from streaming thread. */
2282 gst_ogg_demux_send_event (ogg, newseg);
2287 gst_segment_set_seek (&ogg->segment, rate, format, flags,
2288 cur_type, cur, stop_type, stop, &update);
2291 GST_DEBUG_OBJECT (ogg, "segment positions set to %" GST_TIME_FORMAT "-%"
2292 GST_TIME_FORMAT, GST_TIME_ARGS (ogg->segment.start),
2293 GST_TIME_ARGS (ogg->segment.stop));
2295 /* we need to stop flushing on the srcpad as we're going to use it
2296 * next. We can do this as we have the STREAM lock now. */
2298 tevent = gst_event_new_flush_stop ();
2299 gst_event_set_seqnum (tevent, seqnum);
2300 gst_pad_push_event (ogg->sinkpad, tevent);
2306 /* reset all ogg streams now, need to do this from within the lock to
2307 * make sure the streaming thread is not messing with the stream */
2308 for (i = 0; i < ogg->chains->len; i++) {
2309 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
2311 gst_ogg_chain_reset (chain);
2315 /* for reverse we will already seek accurately */
2316 res = gst_ogg_demux_do_seek (ogg, &ogg->segment, accurate, keyframe, &chain);
2318 /* seek failed, make sure we continue the current chain */
2320 GST_DEBUG_OBJECT (ogg, "seek failed");
2321 chain = ogg->current_chain;
2323 GST_DEBUG_OBJECT (ogg, "seek success");
2329 /* now we have a new position, prepare for streaming again */
2334 gint64 last_stop, begin_time;
2336 /* we have to send the flush to the old chain, not the new one */
2338 tevent = gst_event_new_flush_stop ();
2339 gst_event_set_seqnum (tevent, seqnum);
2340 gst_ogg_demux_send_event (ogg, tevent);
2343 /* we need this to see how far inside the chain we need to start */
2344 if (chain->begin_time != GST_CLOCK_TIME_NONE)
2345 begin_time = chain->begin_time;
2349 /* segment.start gives the start over all chains, we calculate the amount
2350 * of time into this chain we need to start */
2351 start = ogg->segment.start - begin_time;
2352 if (chain->segment_start != GST_CLOCK_TIME_NONE)
2353 start += chain->segment_start;
2355 if ((stop = ogg->segment.stop) == -1)
2356 stop = ogg->segment.duration;
2358 /* segment.stop gives the stop time over all chains, calculate the amount of
2359 * time we need to stop in this chain */
2361 if (stop > begin_time)
2365 stop += chain->segment_start;
2366 /* we must stop when this chain ends and switch to the next chain to play
2367 * the remainder of the segment. */
2368 stop = MIN (stop, chain->segment_stop);
2371 last_stop = ogg->segment.last_stop;
2372 if (chain->segment_start != GST_CLOCK_TIME_NONE)
2373 last_stop += chain->segment_start;
2375 /* create the segment event we are going to send out */
2376 if (ogg->segment.rate >= 0.0)
2377 event = gst_event_new_new_segment (FALSE, ogg->segment.rate,
2378 ogg->segment.format, last_stop, stop, ogg->segment.time);
2380 event = gst_event_new_new_segment (FALSE, ogg->segment.rate,
2381 ogg->segment.format, start, last_stop, ogg->segment.time);
2383 gst_event_set_seqnum (event, seqnum);
2385 if (chain != ogg->current_chain) {
2386 /* switch to different chain, send segment on new chain */
2387 gst_ogg_demux_activate_chain (ogg, chain, event);
2389 /* mark discont and send segment on current chain */
2390 gst_ogg_chain_mark_discont (chain);
2391 /* This event should be sent from the streaming thread (sink pad task) */
2392 if (ogg->newsegment)
2393 gst_event_unref (ogg->newsegment);
2394 ogg->newsegment = event;
2397 /* notify start of new segment */
2398 if (ogg->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2399 GstMessage *message;
2401 message = gst_message_new_segment_start (GST_OBJECT (ogg),
2402 GST_FORMAT_TIME, ogg->segment.last_stop);
2403 gst_message_set_seqnum (message, seqnum);
2405 gst_element_post_message (GST_ELEMENT (ogg), message);
2408 ogg->segment_running = TRUE;
2409 ogg->seqnum = seqnum;
2410 /* restart our task since it might have been stopped when we did the
2412 gst_pad_start_task (ogg->sinkpad, (GstTaskFunction) gst_ogg_demux_loop,
2416 /* streaming can continue now */
2417 GST_PAD_STREAM_UNLOCK (ogg->sinkpad);
2424 GST_DEBUG_OBJECT (ogg, "seek failed");
2429 GST_DEBUG_OBJECT (ogg, "no chain to seek in");
2430 GST_PAD_STREAM_UNLOCK (ogg->sinkpad);
2436 gst_ogg_demux_perform_seek_push (GstOggDemux * ogg, GstEvent * event)
2439 gboolean res = TRUE;
2443 GstSeekType start_type, stop_type;
2447 gint64 best, best_time;
2449 gst_event_parse_seek (event, &rate, &format, &flags,
2450 &start_type, &start, &stop_type, &stop);
2452 if (format != GST_FORMAT_TIME) {
2453 GST_DEBUG_OBJECT (ogg, "can only seek on TIME");
2457 chain = ogg->current_chain;
2461 if (do_index_search (ogg, chain, 0, -1, 0, -1, start, &best, &best_time)) {
2462 /* the index gave some result */
2463 GST_DEBUG_OBJECT (ogg,
2464 "found offset %" G_GINT64_FORMAT " with time %" G_GUINT64_FORMAT,
2467 } else if ((bitrate = ogg->bitrate) > 0) {
2468 /* try with bitrate convert the seek positions to bytes */
2469 if (start_type != GST_SEEK_TYPE_NONE) {
2470 start = gst_util_uint64_scale (start, bitrate, 8 * GST_SECOND);
2472 if (stop_type != GST_SEEK_TYPE_NONE) {
2473 stop = gst_util_uint64_scale (stop, bitrate, 8 * GST_SECOND);
2481 GST_DEBUG_OBJECT (ogg,
2482 "seeking to %" G_GINT64_FORMAT " - %" G_GINT64_FORMAT, start, stop);
2484 sevent = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
2485 start_type, start, stop_type, stop);
2487 res = gst_pad_push_event (ogg->sinkpad, sevent);
2495 GST_DEBUG_OBJECT (ogg, "seek failed");
2501 gst_ogg_demux_perform_seek (GstOggDemux * ogg, GstEvent * event)
2505 if (ogg->pullmode) {
2506 res = gst_ogg_demux_perform_seek_pull (ogg, event);
2508 res = gst_ogg_demux_perform_seek_push (ogg, event);
2514 /* finds each bitstream link one at a time using a bisection search
2515 * (has to begin by knowing the offset of the lb's initial page).
2516 * Recurses for each link so it can alloc the link storage after
2517 * finding them all, then unroll and fill the cache at the same time
2519 static GstFlowReturn
2520 gst_ogg_demux_bisect_forward_serialno (GstOggDemux * ogg,
2521 gint64 begin, gint64 searched, gint64 end, GstOggChain * chain, glong m)
2523 gint64 endsearched = end;
2528 GstOggChain *nextchain;
2530 GST_LOG_OBJECT (ogg,
2531 "bisect begin: %" G_GINT64_FORMAT ", searched: %" G_GINT64_FORMAT
2532 ", end %" G_GINT64_FORMAT ", chain: %p", begin, searched, end, chain);
2534 /* the below guards against garbage seperating the last and
2535 * first pages of two links. */
2536 while (searched < endsearched) {
2539 if (endsearched - searched < CHUNKSIZE) {
2542 bisect = (searched + endsearched) / 2;
2545 gst_ogg_demux_seek (ogg, bisect);
2546 ret = gst_ogg_demux_get_next_page (ogg, &og, -1, &offset);
2548 if (ret == GST_FLOW_UNEXPECTED) {
2549 endsearched = bisect;
2550 } else if (ret == GST_FLOW_OK) {
2551 glong serial = ogg_page_serialno (&og);
2553 if (!gst_ogg_chain_has_stream (chain, serial)) {
2554 endsearched = bisect;
2557 searched = offset + og.header_len + og.body_len;
2563 GST_LOG_OBJECT (ogg, "current chain ends at %" G_GINT64_FORMAT, searched);
2565 chain->end_offset = searched;
2566 ret = gst_ogg_demux_read_end_chain (ogg, chain);
2567 if (ret != GST_FLOW_OK)
2570 GST_LOG_OBJECT (ogg, "found begin at %" G_GINT64_FORMAT, next);
2572 gst_ogg_demux_seek (ogg, next);
2573 ret = gst_ogg_demux_read_chain (ogg, &nextchain);
2574 if (ret == GST_FLOW_UNEXPECTED) {
2577 GST_LOG_OBJECT (ogg, "no next chain");
2578 } else if (ret != GST_FLOW_OK)
2581 if (searched < end && nextchain != NULL) {
2582 ret = gst_ogg_demux_bisect_forward_serialno (ogg, next, ogg->offset,
2583 end, nextchain, m + 1);
2584 if (ret != GST_FLOW_OK)
2587 GST_LOG_OBJECT (ogg, "adding chain %p", chain);
2589 g_array_insert_val (ogg->chains, 0, chain);
2595 /* read a chain from the ogg file. This code will
2596 * read all BOS pages and will create and return a GstOggChain
2597 * structure with the results.
2599 * This function will also read N pages from each stream in the
2600 * chain and submit them to the decoders. When the decoder has
2601 * decoded the first buffer, we know the timestamp of the first
2602 * page in the chain.
2604 static GstFlowReturn
2605 gst_ogg_demux_read_chain (GstOggDemux * ogg, GstOggChain ** res_chain)
2608 GstOggChain *chain = NULL;
2609 gint64 offset = ogg->offset;
2614 GST_LOG_OBJECT (ogg, "reading chain at %" G_GINT64_FORMAT, offset);
2616 /* first read the BOS pages, do typefind on them, create
2617 * the decoders, send data to the decoders. */
2622 ret = gst_ogg_demux_get_next_page (ogg, &op, -1, NULL);
2623 if (ret != GST_FLOW_OK) {
2624 GST_WARNING_OBJECT (ogg, "problem reading BOS page: ret=%d", ret);
2627 if (!ogg_page_bos (&op)) {
2628 GST_WARNING_OBJECT (ogg, "page is not BOS page");
2629 /* if we did not find a chain yet, assume this is a bogus stream and
2632 ret = GST_FLOW_UNEXPECTED;
2636 if (chain == NULL) {
2637 chain = gst_ogg_chain_new (ogg);
2638 chain->offset = offset;
2641 serial = ogg_page_serialno (&op);
2642 if (gst_ogg_chain_get_stream (chain, serial) != NULL) {
2643 GST_WARNING_OBJECT (ogg, "found serial %08lx BOS page twice, ignoring",
2648 pad = gst_ogg_chain_new_stream (chain, serial);
2649 gst_ogg_pad_submit_page (pad, &op);
2652 if (ret != GST_FLOW_OK || chain == NULL) {
2653 if (ret == GST_FLOW_OK) {
2654 GST_WARNING_OBJECT (ogg, "no chain was found");
2655 ret = GST_FLOW_ERROR;
2656 } else if (ret != GST_FLOW_UNEXPECTED) {
2657 GST_WARNING_OBJECT (ogg, "failed to read chain");
2659 GST_DEBUG_OBJECT (ogg, "done reading chains");
2662 gst_ogg_chain_free (chain);
2669 chain->have_bos = TRUE;
2670 GST_LOG_OBJECT (ogg, "read bos pages, init decoder now");
2672 /* now read pages until we receive a buffer from each of the
2673 * stream decoders, this will tell us the timestamp of the
2674 * first packet in the chain then */
2676 /* save the offset to the first non bos page in the chain: if searching for
2677 * pad->first_time we read past the end of the chain, we'll seek back to this
2680 offset = ogg->offset;
2685 gboolean known_serial = FALSE;
2688 serial = ogg_page_serialno (&op);
2690 for (i = 0; i < chain->streams->len; i++) {
2691 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
2693 GST_LOG_OBJECT (ogg, "serial %08lx time %" GST_TIME_FORMAT,
2694 pad->map.serialno, GST_TIME_ARGS (pad->start_time));
2696 if (pad->map.serialno == serial) {
2697 known_serial = TRUE;
2699 /* submit the page now, this will fill in the start_time when the
2700 * internal decoder finds it */
2701 gst_ogg_pad_submit_page (pad, &op);
2703 if (!pad->map.is_skeleton && pad->start_time == -1
2704 && ogg_page_eos (&op)) {
2705 /* got EOS on a pad before we could find its start_time.
2706 * We have no chance of finding a start_time for every pad so
2707 * stop searching for the other start_time(s).
2713 /* the timestamp will be filled in when we submit the pages */
2714 if (!pad->map.is_sparse)
2715 done &= (pad->start_time != GST_CLOCK_TIME_NONE);
2717 GST_LOG_OBJECT (ogg, "done %08lx now %d", pad->map.serialno, done);
2720 /* we read a page not belonging to the current chain: seek back to the
2721 * beginning of the chain
2723 if (!known_serial) {
2724 GST_LOG_OBJECT (ogg, "unknown serial %08lx", serial);
2725 gst_ogg_demux_seek (ogg, offset);
2730 ret = gst_ogg_demux_get_next_page (ogg, &op, -1, NULL);
2731 if (ret != GST_FLOW_OK)
2735 GST_LOG_OBJECT (ogg, "done reading chain");
2736 /* now we can fill in the missing info using queries */
2737 for (i = 0; i < chain->streams->len; i++) {
2738 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
2740 if (pad->map.is_skeleton)
2743 pad->mode = GST_OGG_PAD_MODE_STREAMING;
2752 /* read the last pages from the ogg stream to get the final
2755 static GstFlowReturn
2756 gst_ogg_demux_read_end_chain (GstOggDemux * ogg, GstOggChain * chain)
2758 gint64 begin = chain->end_offset;
2760 gint64 last_granule = -1;
2761 GstOggPad *last_pad = NULL;
2763 gboolean done = FALSE;
2772 gst_ogg_demux_seek (ogg, begin);
2774 /* now continue reading until we run out of data, if we find a page
2775 * start, we save it. It might not be the final page as there could be
2776 * another page after this one. */
2777 while (ogg->offset < end) {
2778 ret = gst_ogg_demux_get_next_page (ogg, &og, end - ogg->offset, NULL);
2780 if (ret == GST_FLOW_LIMIT)
2782 if (ret != GST_FLOW_OK)
2785 for (i = 0; i < chain->streams->len; i++) {
2786 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
2788 if (pad->map.is_sparse)
2791 if (pad->map.serialno == ogg_page_serialno (&og)) {
2792 gint64 granulepos = ogg_page_granulepos (&og);
2794 if (granulepos != -1) {
2795 last_granule = granulepos;
2806 chain->segment_stop =
2807 gst_ogg_stream_get_end_time_for_granulepos (&last_pad->map,
2810 chain->segment_stop = GST_CLOCK_TIME_NONE;
2813 GST_INFO ("segment stop %" G_GUINT64_FORMAT, chain->segment_stop);
2818 /* find a pad with a given serial number
2821 gst_ogg_demux_find_pad (GstOggDemux * ogg, glong serialno)
2826 /* first look in building chain if any */
2827 if (ogg->building_chain) {
2828 pad = gst_ogg_chain_get_stream (ogg->building_chain, serialno);
2833 /* then look in current chain if any */
2834 if (ogg->current_chain) {
2835 pad = gst_ogg_chain_get_stream (ogg->current_chain, serialno);
2840 for (i = 0; i < ogg->chains->len; i++) {
2841 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
2843 pad = gst_ogg_chain_get_stream (chain, serialno);
2850 /* find a chain with a given serial number
2852 static GstOggChain *
2853 gst_ogg_demux_find_chain (GstOggDemux * ogg, glong serialno)
2857 pad = gst_ogg_demux_find_pad (ogg, serialno);
2864 /* returns TRUE if all streams have valid start time */
2866 gst_ogg_demux_collect_chain_info (GstOggDemux * ogg, GstOggChain * chain)
2868 gboolean res = TRUE;
2870 chain->total_time = GST_CLOCK_TIME_NONE;
2871 GST_DEBUG_OBJECT (ogg, "trying to collect chain info");
2873 /* see if we have a start time on all streams */
2874 chain->segment_start = gst_ogg_demux_collect_start_time (ogg, chain);
2876 if (chain->segment_start == G_MAXUINT64) {
2877 /* not yet, stream some more data */
2879 } else if (chain->segment_stop != GST_CLOCK_TIME_NONE) {
2880 /* we can calculate a total time */
2881 chain->total_time = chain->segment_stop - chain->segment_start;
2884 GST_DEBUG ("total time %" G_GUINT64_FORMAT, chain->total_time);
2886 GST_DEBUG_OBJECT (ogg, "return %d", res);
2892 gst_ogg_demux_collect_info (GstOggDemux * ogg)
2896 /* collect all info */
2897 ogg->total_time = 0;
2899 for (i = 0; i < ogg->chains->len; i++) {
2900 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
2902 chain->begin_time = ogg->total_time;
2904 gst_ogg_demux_collect_chain_info (ogg, chain);
2906 ogg->total_time += chain->total_time;
2908 gst_segment_set_duration (&ogg->segment, GST_FORMAT_TIME, ogg->total_time);
2911 /* find all the chains in the ogg file, this reads the first and
2912 * last page of the ogg stream, if they match then the ogg file has
2913 * just one chain, else we do a binary search for all chains.
2915 static GstFlowReturn
2916 gst_ogg_demux_find_chains (GstOggDemux * ogg)
2926 /* get peer to figure out length */
2927 if ((peer = gst_pad_get_peer (ogg->sinkpad)) == NULL)
2930 /* find length to read last page, we store this for later use. */
2931 format = GST_FORMAT_BYTES;
2932 res = gst_pad_query_duration (peer, &format, &ogg->length);
2933 gst_object_unref (peer);
2934 if (!res || ogg->length <= 0)
2937 GST_DEBUG_OBJECT (ogg, "file length %" G_GINT64_FORMAT, ogg->length);
2939 /* read chain from offset 0, this is the first chain of the
2941 gst_ogg_demux_seek (ogg, 0);
2942 ret = gst_ogg_demux_read_chain (ogg, &chain);
2943 if (ret != GST_FLOW_OK)
2944 goto no_first_chain;
2946 /* read page from end offset, we use this page to check if its serial
2947 * number is contained in the first chain. If this is the case then
2948 * this ogg is not a chained ogg and we can skip the scanning. */
2949 gst_ogg_demux_seek (ogg, ogg->length);
2950 ret = gst_ogg_demux_get_prev_page (ogg, &og, NULL);
2951 if (ret != GST_FLOW_OK)
2954 serialno = ogg_page_serialno (&og);
2956 if (!gst_ogg_chain_has_stream (chain, serialno)) {
2957 /* the last page is not in the first stream, this means we should
2958 * find all the chains in this chained ogg. */
2960 gst_ogg_demux_bisect_forward_serialno (ogg, 0, 0, ogg->length, chain,
2963 /* we still call this function here but with an empty range so that
2964 * we can reuse the setup code in this routine. */
2966 gst_ogg_demux_bisect_forward_serialno (ogg, 0, ogg->length,
2967 ogg->length, chain, 0);
2969 if (ret != GST_FLOW_OK)
2972 /* all fine, collect and print */
2973 gst_ogg_demux_collect_info (ogg);
2975 /* dump our chains and streams */
2976 gst_ogg_print (ogg);
2981 /*** error cases ***/
2984 GST_ELEMENT_ERROR (ogg, STREAM, DEMUX, (NULL), ("we don't have a peer"));
2985 return GST_FLOW_NOT_LINKED;
2989 GST_ELEMENT_ERROR (ogg, STREAM, DEMUX, (NULL), ("can't get file length"));
2990 return GST_FLOW_NOT_SUPPORTED;
2994 GST_ELEMENT_ERROR (ogg, STREAM, DEMUX, (NULL), ("can't get first chain"));
2995 return GST_FLOW_ERROR;
2999 GST_DEBUG_OBJECT (ogg, "can't get last page");
3001 gst_ogg_chain_free (chain);
3006 static GstFlowReturn
3007 gst_ogg_demux_handle_page (GstOggDemux * ogg, ogg_page * page)
3012 GstFlowReturn result = GST_FLOW_OK;
3014 serialno = ogg_page_serialno (page);
3015 granule = ogg_page_granulepos (page);
3017 GST_LOG_OBJECT (ogg,
3018 "processing ogg page (serial %08lx, pageno %ld, granulepos %"
3019 G_GINT64_FORMAT ", bos %d)",
3020 serialno, ogg_page_pageno (page), granule, ogg_page_bos (page));
3022 if (ogg_page_bos (page)) {
3026 /* see if we know about the chain already */
3027 chain = gst_ogg_demux_find_chain (ogg, serialno);
3032 if (chain->segment_start != GST_CLOCK_TIME_NONE)
3033 start = chain->segment_start;
3035 /* create the newsegment event we are going to send out */
3036 event = gst_event_new_new_segment (FALSE, ogg->segment.rate,
3037 GST_FORMAT_TIME, start, chain->segment_stop, chain->begin_time);
3038 gst_event_set_seqnum (event, ogg->seqnum);
3040 GST_DEBUG_OBJECT (ogg,
3041 "segment: start %" GST_TIME_FORMAT ", stop %" GST_TIME_FORMAT
3042 ", time %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
3043 GST_TIME_ARGS (chain->segment_stop),
3044 GST_TIME_ARGS (chain->begin_time));
3046 /* activate it as it means we have a non-header, this will also deactivate
3047 * the currently running chain. */
3048 gst_ogg_demux_activate_chain (ogg, chain, event);
3049 pad = gst_ogg_demux_find_pad (ogg, serialno);
3051 GstClockTime chain_time;
3052 GstOggChain *current_chain;
3053 gint64 current_time;
3055 /* this can only happen in push mode */
3059 current_chain = ogg->current_chain;
3060 current_time = ogg->segment.last_stop;
3062 /* time of new chain is current time */
3063 chain_time = current_time;
3065 if (ogg->building_chain == NULL) {
3066 GstOggChain *newchain;
3068 newchain = gst_ogg_chain_new (ogg);
3069 newchain->offset = 0;
3070 /* set new chain begin time aligned with end time of old chain */
3071 newchain->begin_time = chain_time;
3072 GST_DEBUG_OBJECT (ogg, "new chain, begin time %" GST_TIME_FORMAT,
3073 GST_TIME_ARGS (chain_time));
3075 /* and this is the one we are building now */
3076 ogg->building_chain = newchain;
3078 pad = gst_ogg_chain_new_stream (ogg->building_chain, serialno);
3081 pad = gst_ogg_demux_find_pad (ogg, serialno);
3084 result = gst_ogg_pad_submit_page (pad, page);
3086 /* no pad. This means an ogg page without bos has been seen for this
3087 * serialno. we just ignore it but post a warning... */
3088 GST_ELEMENT_WARNING (ogg, STREAM, DECODE,
3089 (NULL), ("unknown ogg pad for serial %08lx detected", serialno));
3097 GST_ELEMENT_ERROR (ogg, STREAM, DECODE,
3098 (NULL), ("unknown ogg chain for serial %08lx detected", serialno));
3099 return GST_FLOW_ERROR;
3103 /* streaming mode, receive a buffer, parse it, create pads for
3104 * the serialno, submit pages and packets to the oggpads
3106 static GstFlowReturn
3107 gst_ogg_demux_chain (GstPad * pad, GstBuffer * buffer)
3111 GstFlowReturn result = GST_FLOW_OK;
3113 ogg = GST_OGG_DEMUX (GST_OBJECT_PARENT (pad));
3115 GST_DEBUG_OBJECT (ogg, "chain");
3116 result = gst_ogg_demux_submit_buffer (ogg, buffer);
3118 while (result == GST_FLOW_OK) {
3121 ret = ogg_sync_pageout (&ogg->sync, &page);
3123 /* need more data */
3126 /* discontinuity in the pages */
3127 GST_DEBUG_OBJECT (ogg, "discont in page found, continuing");
3129 result = gst_ogg_demux_handle_page (ogg, &page);
3132 if (ret == 0 || result == GST_FLOW_OK) {
3133 gst_ogg_demux_sync_streams (ogg);
3139 gst_ogg_demux_send_event (GstOggDemux * ogg, GstEvent * event)
3141 GstOggChain *chain = ogg->current_chain;
3142 gboolean res = TRUE;
3147 for (i = 0; i < chain->streams->len; i++) {
3148 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
3150 gst_event_ref (event);
3151 GST_DEBUG_OBJECT (pad, "Pushing event %" GST_PTR_FORMAT, event);
3152 res &= gst_pad_push_event (GST_PAD (pad), event);
3155 gst_event_unref (event);
3160 static GstFlowReturn
3161 gst_ogg_demux_combine_flows (GstOggDemux * ogg, GstOggPad * pad,
3166 /* store the value */
3167 pad->last_ret = ret;
3169 /* any other error that is not-linked can be returned right
3171 if (ret != GST_FLOW_NOT_LINKED)
3174 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3175 chain = ogg->current_chain;
3179 for (i = 0; i < chain->streams->len; i++) {
3180 GstOggPad *opad = g_array_index (chain->streams, GstOggPad *, i);
3182 ret = opad->last_ret;
3183 /* some other return value (must be SUCCESS but we can return
3184 * other values as well) */
3185 if (ret != GST_FLOW_NOT_LINKED)
3188 /* if we get here, all other pads were unlinked and we return
3189 * NOT_LINKED then */
3195 /* returns TRUE if all streams in current chain reached EOS, FALSE otherwise */
3197 gst_ogg_demux_check_eos (GstOggDemux * ogg)
3200 gboolean eos = TRUE;
3202 chain = ogg->current_chain;
3203 if (G_LIKELY (chain)) {
3206 for (i = 0; i < chain->streams->len; i++) {
3207 GstOggPad *opad = g_array_index (chain->streams, GstOggPad *, i);
3209 eos = eos && opad->is_eos;
3218 static GstFlowReturn
3219 gst_ogg_demux_loop_forward (GstOggDemux * ogg)
3224 if (ogg->offset == ogg->length) {
3225 GST_LOG_OBJECT (ogg, "no more data to pull %" G_GINT64_FORMAT
3226 " == %" G_GINT64_FORMAT, ogg->offset, ogg->length);
3227 ret = GST_FLOW_UNEXPECTED;
3231 GST_LOG_OBJECT (ogg, "pull data %" G_GINT64_FORMAT, ogg->offset);
3232 ret = gst_pad_pull_range (ogg->sinkpad, ogg->offset, CHUNKSIZE, &buffer);
3233 if (ret != GST_FLOW_OK) {
3234 GST_LOG_OBJECT (ogg, "Failed pull_range");
3238 ogg->offset += GST_BUFFER_SIZE (buffer);
3240 if (G_UNLIKELY (ogg->newsegment)) {
3241 gst_ogg_demux_send_event (ogg, ogg->newsegment);
3242 ogg->newsegment = NULL;
3245 ret = gst_ogg_demux_chain (ogg->sinkpad, buffer);
3246 if (ret != GST_FLOW_OK) {
3247 GST_LOG_OBJECT (ogg, "Failed demux_chain");
3251 /* check for the end of the segment */
3252 if (gst_ogg_demux_check_eos (ogg)) {
3253 GST_LOG_OBJECT (ogg, "got EOS");
3254 ret = GST_FLOW_UNEXPECTED;
3263 * We read the pages backwards and send the packets forwards. The first packet
3264 * in the page will be pushed with the DISCONT flag set.
3266 * Special care has to be taken for continued pages, which we can only decode
3267 * when we have the previous page(s).
3269 static GstFlowReturn
3270 gst_ogg_demux_loop_reverse (GstOggDemux * ogg)
3276 if (ogg->offset == 0) {
3277 GST_LOG_OBJECT (ogg, "no more data to pull %" G_GINT64_FORMAT
3278 " == 0", ogg->offset);
3279 ret = GST_FLOW_UNEXPECTED;
3283 GST_LOG_OBJECT (ogg, "read page from %" G_GINT64_FORMAT, ogg->offset);
3284 ret = gst_ogg_demux_get_prev_page (ogg, &page, &offset);
3285 if (ret != GST_FLOW_OK)
3288 ogg->offset = offset;
3290 if (G_UNLIKELY (ogg->newsegment)) {
3291 gst_ogg_demux_send_event (ogg, ogg->newsegment);
3292 ogg->newsegment = NULL;
3295 ret = gst_ogg_demux_handle_page (ogg, &page);
3296 if (ret != GST_FLOW_OK)
3299 /* check for the end of the segment */
3300 if (gst_ogg_demux_check_eos (ogg)) {
3301 GST_LOG_OBJECT (ogg, "got EOS");
3302 ret = GST_FLOW_UNEXPECTED;
3310 gst_ogg_demux_sync_streams (GstOggDemux * ogg)
3316 chain = ogg->current_chain;
3317 cur = ogg->segment.last_stop;
3318 if (chain == NULL || cur == -1)
3321 for (i = 0; i < chain->streams->len; i++) {
3322 GstOggPad *stream = g_array_index (chain->streams, GstOggPad *, i);
3324 /* Theoretically, we should be doing this for all streams, but we're only
3325 * doing it for known-to-be-sparse streams at the moment in order not to
3326 * break things for wrongly-muxed streams (like we used to produce once) */
3327 if (stream->map.is_sparse && stream->last_stop != GST_CLOCK_TIME_NONE) {
3329 /* Does this stream lag? Random threshold of 2 seconds */
3330 if (GST_CLOCK_DIFF (stream->last_stop, cur) > (2 * GST_SECOND)) {
3331 GST_DEBUG_OBJECT (stream, "synchronizing stream with others by "
3332 "advancing time from %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
3333 GST_TIME_ARGS (stream->last_stop), GST_TIME_ARGS (cur));
3334 stream->last_stop = cur;
3335 /* advance stream time (FIXME: is this right, esp. time_pos?) */
3336 gst_pad_push_event (GST_PAD_CAST (stream),
3337 gst_event_new_new_segment (TRUE, ogg->segment.rate,
3338 GST_FORMAT_TIME, stream->last_stop, -1, stream->last_stop));
3344 /* random access code
3346 * - first find all the chains and streams by scanning the file.
3347 * - then get and chain buffers, just like the streaming case.
3348 * - when seeking, we can use the chain info to perform the seek.
3351 gst_ogg_demux_loop (GstOggPad * pad)
3357 ogg = GST_OGG_DEMUX (GST_OBJECT_PARENT (pad));
3359 if (ogg->need_chains) {
3362 /* this is the only place where we write chains and thus need to lock. */
3363 GST_CHAIN_LOCK (ogg);
3364 ret = gst_ogg_demux_find_chains (ogg);
3365 GST_CHAIN_UNLOCK (ogg);
3366 if (ret != GST_FLOW_OK)
3367 goto chain_read_failed;
3369 ogg->need_chains = FALSE;
3371 GST_OBJECT_LOCK (ogg);
3372 ogg->running = TRUE;
3375 GST_OBJECT_UNLOCK (ogg);
3377 /* and seek to configured positions without FLUSH */
3378 res = gst_ogg_demux_perform_seek_pull (ogg, event);
3380 gst_event_unref (event);
3386 if (ogg->segment.rate >= 0.0)
3387 ret = gst_ogg_demux_loop_forward (ogg);
3389 ret = gst_ogg_demux_loop_reverse (ogg);
3391 if (ret != GST_FLOW_OK)
3394 gst_ogg_demux_sync_streams (ogg);
3400 /* error was posted */
3405 GST_ELEMENT_ERROR (ogg, STREAM, DEMUX, (NULL),
3406 ("failed to start demuxing ogg"));
3407 ret = GST_FLOW_ERROR;
3412 const gchar *reason = gst_flow_get_name (ret);
3413 GstEvent *event = NULL;
3415 GST_LOG_OBJECT (ogg, "pausing task, reason %s", reason);
3416 ogg->segment_running = FALSE;
3417 gst_pad_pause_task (ogg->sinkpad);
3419 if (ret == GST_FLOW_UNEXPECTED) {
3420 /* perform EOS logic */
3421 if (ogg->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3423 GstMessage *message;
3425 /* for segment playback we need to post when (in stream time)
3426 * we stopped, this is either stop (when set) or the duration. */
3427 if ((stop = ogg->segment.stop) == -1)
3428 stop = ogg->segment.duration;
3430 GST_LOG_OBJECT (ogg, "Sending segment done, at end of segment");
3432 gst_message_new_segment_done (GST_OBJECT (ogg), GST_FORMAT_TIME,
3434 gst_message_set_seqnum (message, ogg->seqnum);
3436 gst_element_post_message (GST_ELEMENT (ogg), message);
3438 /* normal playback, send EOS to all linked pads */
3439 GST_LOG_OBJECT (ogg, "Sending EOS, at end of stream");
3440 event = gst_event_new_eos ();
3442 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
3443 GST_ELEMENT_ERROR (ogg, STREAM, FAILED,
3444 (_("Internal data stream error.")),
3445 ("stream stopped, reason %s", reason));
3446 event = gst_event_new_eos ();
3449 /* For wrong-state we still want to pause the task and stop
3450 * but no error message or other things are necessary.
3451 * wrong-state is no real error and will be caused by flushing,
3452 * e.g. because of a flushing seek.
3455 gst_event_set_seqnum (event, ogg->seqnum);
3456 gst_ogg_demux_send_event (ogg, event);
3463 gst_ogg_demux_clear_chains (GstOggDemux * ogg)
3467 gst_ogg_demux_deactivate_current_chain (ogg);
3469 GST_CHAIN_LOCK (ogg);
3470 for (i = 0; i < ogg->chains->len; i++) {
3471 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
3473 gst_ogg_chain_free (chain);
3475 ogg->chains = g_array_set_size (ogg->chains, 0);
3476 GST_CHAIN_UNLOCK (ogg);
3479 /* this function is called when the pad is activated and should start
3482 * We check if we can do random access to decide if we work push or
3486 gst_ogg_demux_sink_activate (GstPad * sinkpad)
3488 if (gst_pad_check_pull_range (sinkpad)) {
3489 GST_DEBUG_OBJECT (sinkpad, "activating pull");
3490 return gst_pad_activate_pull (sinkpad, TRUE);
3492 GST_DEBUG_OBJECT (sinkpad, "activating push");
3493 return gst_pad_activate_push (sinkpad, TRUE);
3497 /* this function gets called when we activate ourselves in push mode.
3498 * We cannot seek (ourselves) in the stream */
3500 gst_ogg_demux_sink_activate_push (GstPad * sinkpad, gboolean active)
3504 ogg = GST_OGG_DEMUX (GST_OBJECT_PARENT (sinkpad));
3506 ogg->pullmode = FALSE;
3507 ogg->resync = FALSE;
3512 /* this function gets called when we activate ourselves in pull mode.
3513 * We can perform random access to the resource and we start a task
3514 * to start reading */
3516 gst_ogg_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
3520 ogg = GST_OGG_DEMUX (GST_OBJECT_PARENT (sinkpad));
3523 ogg->need_chains = TRUE;
3524 ogg->pullmode = TRUE;
3526 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_ogg_demux_loop,
3529 return gst_pad_stop_task (sinkpad);
3533 static GstStateChangeReturn
3534 gst_ogg_demux_change_state (GstElement * element, GstStateChange transition)
3537 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
3539 ogg = GST_OGG_DEMUX (element);
3541 switch (transition) {
3542 case GST_STATE_CHANGE_NULL_TO_READY:
3544 ogg_sync_init (&ogg->sync);
3546 case GST_STATE_CHANGE_READY_TO_PAUSED:
3547 ogg_sync_reset (&ogg->sync);
3548 ogg->running = FALSE;
3550 ogg->segment_running = FALSE;
3551 ogg->total_time = -1;
3552 gst_segment_init (&ogg->segment, GST_FORMAT_TIME);
3554 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3560 result = parent_class->change_state (element, transition);
3562 switch (transition) {
3563 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3565 case GST_STATE_CHANGE_PAUSED_TO_READY:
3566 gst_ogg_demux_clear_chains (ogg);
3567 GST_OBJECT_LOCK (ogg);
3568 ogg->running = FALSE;
3569 ogg->segment_running = FALSE;
3570 GST_OBJECT_UNLOCK (ogg);
3572 case GST_STATE_CHANGE_READY_TO_NULL:
3573 ogg_sync_clear (&ogg->sync);
3582 gst_ogg_demux_plugin_init (GstPlugin * plugin)
3584 GST_DEBUG_CATEGORY_INIT (gst_ogg_demux_debug, "oggdemux", 0, "ogg demuxer");
3585 GST_DEBUG_CATEGORY_INIT (gst_ogg_demux_setup_debug, "oggdemux_setup", 0,
3586 "ogg demuxer setup stage when parsing pipeline");
3589 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
3591 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
3592 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
3595 return gst_element_register (plugin, "oggdemux", GST_RANK_PRIMARY,
3596 GST_TYPE_OGG_DEMUX);
3599 /* prints all info about the element */
3600 #undef GST_CAT_DEFAULT
3601 #define GST_CAT_DEFAULT gst_ogg_demux_setup_debug
3603 #ifdef GST_DISABLE_GST_DEBUG
3606 gst_ogg_print (GstOggDemux * ogg)
3611 #else /* !GST_DISABLE_GST_DEBUG */
3614 gst_ogg_print (GstOggDemux * ogg)
3618 GST_INFO_OBJECT (ogg, "%u chains", ogg->chains->len);
3619 GST_INFO_OBJECT (ogg, " total time: %" GST_TIME_FORMAT,
3620 GST_TIME_ARGS (ogg->total_time));
3622 for (i = 0; i < ogg->chains->len; i++) {
3623 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
3625 GST_INFO_OBJECT (ogg, " chain %d (%u streams):", i, chain->streams->len);
3626 GST_INFO_OBJECT (ogg,
3627 " offset: %" G_GINT64_FORMAT " - %" G_GINT64_FORMAT, chain->offset,
3629 GST_INFO_OBJECT (ogg, " begin time: %" GST_TIME_FORMAT,
3630 GST_TIME_ARGS (chain->begin_time));
3631 GST_INFO_OBJECT (ogg, " total time: %" GST_TIME_FORMAT,
3632 GST_TIME_ARGS (chain->total_time));
3633 GST_INFO_OBJECT (ogg, " segment start: %" GST_TIME_FORMAT,
3634 GST_TIME_ARGS (chain->segment_start));
3635 GST_INFO_OBJECT (ogg, " segment stop: %" GST_TIME_FORMAT,
3636 GST_TIME_ARGS (chain->segment_stop));
3638 for (j = 0; j < chain->streams->len; j++) {
3639 GstOggPad *stream = g_array_index (chain->streams, GstOggPad *, j);
3641 GST_INFO_OBJECT (ogg, " stream %08lx:", stream->map.serialno);
3642 GST_INFO_OBJECT (ogg, " start time: %" GST_TIME_FORMAT,
3643 GST_TIME_ARGS (stream->start_time));
3647 #endif /* GST_DISABLE_GST_DEBUG */