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_sink (ret);
1164 GST_PAD_DIRECTION (ret) = GST_PAD_SRC;
1165 gst_ogg_pad_mark_discont (ret);
1168 ret->ogg = chain->ogg;
1170 ret->map.serialno = serialno;
1171 if (ogg_stream_init (&ret->map.stream, serialno) != 0)
1174 name = g_strdup_printf ("serial_%08lx", serialno);
1175 gst_object_set_name (GST_OBJECT (ret), name);
1178 /* FIXME: either do something with it or remove it */
1179 list = gst_tag_list_new ();
1180 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, GST_TAG_SERIAL, serialno,
1182 gst_tag_list_free (list);
1184 GST_DEBUG_OBJECT (chain->ogg,
1185 "created new ogg src %p for stream with serial %08lx", ret, serialno);
1187 g_array_append_val (chain->streams, ret);
1194 GST_ERROR ("Could not initialize ogg_stream struct for serial %08lx.",
1196 gst_object_unref (ret);
1202 gst_ogg_chain_get_stream (GstOggChain * chain, glong serialno)
1206 for (i = 0; i < chain->streams->len; i++) {
1207 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
1209 if (pad->map.serialno == serialno)
1216 gst_ogg_chain_has_stream (GstOggChain * chain, glong serialno)
1218 return gst_ogg_chain_get_stream (chain, serialno) != NULL;
1221 /* signals and args */
1234 static GstStaticPadTemplate ogg_demux_src_template_factory =
1235 GST_STATIC_PAD_TEMPLATE ("src_%d",
1238 GST_STATIC_CAPS_ANY);
1240 static GstStaticPadTemplate ogg_demux_sink_template_factory =
1241 GST_STATIC_PAD_TEMPLATE ("sink",
1244 GST_STATIC_CAPS ("application/ogg; application/x-annodex")
1247 static void gst_ogg_demux_finalize (GObject * object);
1249 static GstFlowReturn gst_ogg_demux_read_chain (GstOggDemux * ogg,
1250 GstOggChain ** chain);
1251 static GstFlowReturn gst_ogg_demux_read_end_chain (GstOggDemux * ogg,
1252 GstOggChain * chain);
1254 static gboolean gst_ogg_demux_sink_event (GstPad * pad, GstEvent * event);
1255 static void gst_ogg_demux_loop (GstOggPad * pad);
1256 static GstFlowReturn gst_ogg_demux_chain (GstPad * pad, GstBuffer * buffer);
1257 static gboolean gst_ogg_demux_sink_activate (GstPad * sinkpad);
1258 static gboolean gst_ogg_demux_sink_activate_pull (GstPad * sinkpad,
1260 static gboolean gst_ogg_demux_sink_activate_push (GstPad * sinkpad,
1262 static GstStateChangeReturn gst_ogg_demux_change_state (GstElement * element,
1263 GstStateChange transition);
1264 static gboolean gst_ogg_demux_send_event (GstOggDemux * ogg, GstEvent * event);
1266 static void gst_ogg_print (GstOggDemux * demux);
1268 GST_BOILERPLATE (GstOggDemux, gst_ogg_demux, GstElement, GST_TYPE_ELEMENT);
1271 gst_ogg_demux_base_init (gpointer g_class)
1273 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1275 gst_element_class_set_details_simple (element_class,
1276 "Ogg demuxer", "Codec/Demuxer",
1277 "demux ogg streams (info about ogg: http://xiph.org)",
1278 "Wim Taymans <wim@fluendo.com>");
1280 gst_element_class_add_pad_template (element_class,
1281 gst_static_pad_template_get (&ogg_demux_sink_template_factory));
1282 gst_element_class_add_pad_template (element_class,
1283 gst_static_pad_template_get (&ogg_demux_src_template_factory));
1287 gst_ogg_demux_class_init (GstOggDemuxClass * klass)
1289 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1290 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1292 gstelement_class->change_state = gst_ogg_demux_change_state;
1293 gstelement_class->send_event = gst_ogg_demux_receive_event;
1295 gobject_class->finalize = gst_ogg_demux_finalize;
1299 gst_ogg_demux_init (GstOggDemux * ogg, GstOggDemuxClass * g_class)
1301 /* create the sink pad */
1303 gst_pad_new_from_static_template (&ogg_demux_sink_template_factory,
1306 gst_pad_set_event_function (ogg->sinkpad, gst_ogg_demux_sink_event);
1307 gst_pad_set_chain_function (ogg->sinkpad, gst_ogg_demux_chain);
1308 gst_pad_set_activate_function (ogg->sinkpad, gst_ogg_demux_sink_activate);
1309 gst_pad_set_activatepull_function (ogg->sinkpad,
1310 gst_ogg_demux_sink_activate_pull);
1311 gst_pad_set_activatepush_function (ogg->sinkpad,
1312 gst_ogg_demux_sink_activate_push);
1313 gst_element_add_pad (GST_ELEMENT (ogg), ogg->sinkpad);
1315 ogg->chain_lock = g_mutex_new ();
1316 ogg->chains = g_array_new (FALSE, TRUE, sizeof (GstOggChain *));
1318 ogg->newsegment = NULL;
1322 gst_ogg_demux_finalize (GObject * object)
1326 ogg = GST_OGG_DEMUX (object);
1328 g_array_free (ogg->chains, TRUE);
1329 g_mutex_free (ogg->chain_lock);
1330 ogg_sync_clear (&ogg->sync);
1332 if (ogg->newsegment)
1333 gst_event_unref (ogg->newsegment);
1335 G_OBJECT_CLASS (parent_class)->finalize (object);
1339 gst_ogg_demux_reset_streams (GstOggDemux * ogg)
1344 chain = ogg->current_chain;
1348 for (i = 0; i < chain->streams->len; i++) {
1349 GstOggPad *stream = g_array_index (chain->streams, GstOggPad *, i);
1351 stream->start_time = -1;
1352 stream->map.accumulated_granule = 0;
1354 ogg->building_chain = chain;
1355 ogg->current_chain = NULL;
1360 gst_ogg_demux_sink_event (GstPad * pad, GstEvent * event)
1365 ogg = GST_OGG_DEMUX (gst_pad_get_parent (pad));
1367 switch (GST_EVENT_TYPE (event)) {
1368 case GST_EVENT_FLUSH_START:
1369 res = gst_ogg_demux_send_event (ogg, event);
1371 case GST_EVENT_FLUSH_STOP:
1372 GST_DEBUG_OBJECT (ogg, "got a flush stop event");
1373 ogg_sync_reset (&ogg->sync);
1374 res = gst_ogg_demux_send_event (ogg, event);
1375 gst_ogg_demux_reset_streams (ogg);
1377 case GST_EVENT_NEWSEGMENT:
1378 GST_DEBUG_OBJECT (ogg, "got a new segment event");
1379 gst_event_unref (event);
1384 GST_DEBUG_OBJECT (ogg, "got an EOS event");
1385 res = gst_ogg_demux_send_event (ogg, event);
1386 if (ogg->current_chain == NULL) {
1387 GST_ELEMENT_ERROR (ogg, STREAM, DEMUX, (NULL),
1388 ("can't get first chain"));
1393 res = gst_ogg_demux_send_event (ogg, event);
1396 gst_object_unref (ogg);
1401 /* submit the given buffer to the ogg sync.
1403 * Returns the number of bytes submited.
1405 static GstFlowReturn
1406 gst_ogg_demux_submit_buffer (GstOggDemux * ogg, GstBuffer * buffer)
1411 GstFlowReturn ret = GST_FLOW_OK;
1413 size = GST_BUFFER_SIZE (buffer);
1414 data = GST_BUFFER_DATA (buffer);
1416 GST_DEBUG_OBJECT (ogg, "submitting %u bytes", size);
1417 if (G_UNLIKELY (size == 0))
1420 oggbuffer = ogg_sync_buffer (&ogg->sync, size);
1421 if (G_UNLIKELY (oggbuffer == NULL))
1424 memcpy (oggbuffer, data, size);
1425 if (G_UNLIKELY (ogg_sync_wrote (&ogg->sync, size) < 0))
1429 gst_buffer_unref (buffer);
1436 GST_ELEMENT_ERROR (ogg, STREAM, DECODE,
1437 (NULL), ("failed to get ogg sync buffer"));
1438 ret = GST_FLOW_ERROR;
1443 GST_ELEMENT_ERROR (ogg, STREAM, DECODE,
1444 (NULL), ("failed to write %d bytes to the sync buffer", size));
1445 ret = GST_FLOW_ERROR;
1450 /* in random access mode this code updates the current read position
1451 * and resets the ogg sync buffer so that the next read will happen
1452 * from this new location.
1455 gst_ogg_demux_seek (GstOggDemux * ogg, gint64 offset)
1457 GST_LOG_OBJECT (ogg, "seeking to %" G_GINT64_FORMAT, offset);
1459 ogg->offset = offset;
1460 ogg->read_offset = offset;
1461 ogg_sync_reset (&ogg->sync);
1464 /* read more data from the current offset and submit to
1465 * the ogg sync layer.
1467 static GstFlowReturn
1468 gst_ogg_demux_get_data (GstOggDemux * ogg, gint64 end_offset)
1473 GST_LOG_OBJECT (ogg,
1474 "get data %" G_GINT64_FORMAT " %" G_GINT64_FORMAT " %" G_GINT64_FORMAT,
1475 ogg->read_offset, ogg->length, end_offset);
1477 if (end_offset > 0 && ogg->read_offset >= end_offset)
1478 goto boundary_reached;
1480 if (ogg->read_offset == ogg->length)
1483 ret = gst_pad_pull_range (ogg->sinkpad, ogg->read_offset, CHUNKSIZE, &buffer);
1484 if (ret != GST_FLOW_OK)
1487 ogg->read_offset += GST_BUFFER_SIZE (buffer);
1489 ret = gst_ogg_demux_submit_buffer (ogg, buffer);
1496 GST_LOG_OBJECT (ogg, "reached boundary");
1497 return GST_FLOW_LIMIT;
1501 GST_LOG_OBJECT (ogg, "reached EOS");
1502 return GST_FLOW_UNEXPECTED;
1506 GST_WARNING_OBJECT (ogg, "got %d (%s) from pull range", ret,
1507 gst_flow_get_name (ret));
1512 /* Read the next page from the current offset.
1513 * boundary: number of bytes ahead we allow looking for;
1516 * @offset will contain the offset the next page starts at when this function
1517 * returns GST_FLOW_OK.
1519 * GST_FLOW_UNEXPECTED is returned on EOS.
1521 * GST_FLOW_LIMIT is returned when we did not find a page before the
1522 * boundary. If @boundary is -1, this is never returned.
1524 * Any other error returned while retrieving data from the peer is returned as
1527 static GstFlowReturn
1528 gst_ogg_demux_get_next_page (GstOggDemux * ogg, ogg_page * og,
1529 gint64 boundary, gint64 * offset)
1531 gint64 end_offset = -1;
1534 GST_LOG_OBJECT (ogg,
1535 "get next page, current offset %" G_GINT64_FORMAT ", bytes boundary %"
1536 G_GINT64_FORMAT, ogg->offset, boundary);
1539 end_offset = ogg->offset + boundary;
1544 if (end_offset > 0 && ogg->offset >= end_offset)
1545 goto boundary_reached;
1547 more = ogg_sync_pageseek (&ogg->sync, og);
1549 GST_LOG_OBJECT (ogg, "pageseek gave %ld", more);
1552 /* skipped n bytes */
1553 ogg->offset -= more;
1554 GST_LOG_OBJECT (ogg, "skipped %ld bytes, offset %" G_GINT64_FORMAT,
1556 } else if (more == 0) {
1557 /* we need more data */
1559 goto boundary_reached;
1561 GST_LOG_OBJECT (ogg, "need more data");
1562 ret = gst_ogg_demux_get_data (ogg, end_offset);
1563 if (ret != GST_FLOW_OK)
1566 gint64 res_offset = ogg->offset;
1568 /* got a page. Return the offset at the page beginning,
1569 advance the internal offset past the page end */
1571 *offset = res_offset;
1574 ogg->offset += more;
1576 GST_LOG_OBJECT (ogg,
1577 "got page at %" G_GINT64_FORMAT ", serial %08x, end at %"
1578 G_GINT64_FORMAT ", granule %" G_GINT64_FORMAT, res_offset,
1579 ogg_page_serialno (og), ogg->offset,
1580 (gint64) ogg_page_granulepos (og));
1584 GST_LOG_OBJECT (ogg, "returning %d", ret);
1591 GST_LOG_OBJECT (ogg,
1592 "offset %" G_GINT64_FORMAT " >= end_offset %" G_GINT64_FORMAT,
1593 ogg->offset, end_offset);
1594 return GST_FLOW_LIMIT;
1598 /* from the current offset, find the previous page, seeking backwards
1599 * until we find the page.
1601 static GstFlowReturn
1602 gst_ogg_demux_get_prev_page (GstOggDemux * ogg, ogg_page * og, gint64 * offset)
1605 gint64 begin = ogg->offset;
1607 gint64 cur_offset = -1;
1609 GST_LOG_OBJECT (ogg, "getting page before %" G_GINT64_FORMAT, begin);
1611 while (cur_offset == -1) {
1616 /* seek CHUNKSIZE back */
1617 gst_ogg_demux_seek (ogg, begin);
1619 /* now continue reading until we run out of data, if we find a page
1620 * start, we save it. It might not be the final page as there could be
1621 * another page after this one. */
1622 while (ogg->offset < end) {
1626 gst_ogg_demux_get_next_page (ogg, og, end - ogg->offset, &new_offset);
1627 /* we hit the upper limit, offset contains the last page start */
1628 if (ret == GST_FLOW_LIMIT) {
1629 GST_LOG_OBJECT (ogg, "hit limit");
1632 /* something went wrong */
1633 if (ret == GST_FLOW_UNEXPECTED) {
1635 GST_LOG_OBJECT (ogg, "got unexpected");
1636 } else if (ret != GST_FLOW_OK) {
1637 GST_LOG_OBJECT (ogg, "got error %d", ret);
1641 GST_LOG_OBJECT (ogg, "found page at %" G_GINT64_FORMAT, new_offset);
1643 /* offset is next page start */
1644 cur_offset = new_offset;
1648 GST_LOG_OBJECT (ogg, "found previous page at %" G_GINT64_FORMAT, cur_offset);
1650 /* we have the offset. Actually snork and hold the page now */
1651 gst_ogg_demux_seek (ogg, cur_offset);
1652 ret = gst_ogg_demux_get_next_page (ogg, og, -1, NULL);
1653 if (ret != GST_FLOW_OK) {
1654 GST_WARNING_OBJECT (ogg, "can't get last page at %" G_GINT64_FORMAT,
1656 /* this shouldn't be possible */
1661 *offset = cur_offset;
1667 gst_ogg_demux_deactivate_current_chain (GstOggDemux * ogg)
1670 GstOggChain *chain = ogg->current_chain;
1675 GST_DEBUG_OBJECT (ogg, "deactivating chain %p", chain);
1677 /* send EOS on all the pads */
1678 for (i = 0; i < chain->streams->len; i++) {
1679 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
1685 event = gst_event_new_eos ();
1686 gst_event_set_seqnum (event, ogg->seqnum);
1687 gst_pad_push_event (GST_PAD_CAST (pad), event);
1689 GST_DEBUG_OBJECT (ogg, "removing pad %" GST_PTR_FORMAT, pad);
1691 /* deactivate first */
1692 gst_pad_set_active (GST_PAD_CAST (pad), FALSE);
1694 gst_element_remove_pad (GST_ELEMENT (ogg), GST_PAD_CAST (pad));
1698 /* if we cannot seek back to the chain, we can destroy the chain
1700 if (!ogg->pullmode) {
1701 gst_ogg_chain_free (chain);
1703 ogg->current_chain = NULL;
1709 gst_ogg_demux_activate_chain (GstOggDemux * ogg, GstOggChain * chain,
1713 gint bitrate, idx_bitrate;
1715 g_return_val_if_fail (chain != NULL, FALSE);
1717 if (chain == ogg->current_chain) {
1719 gst_event_unref (event);
1724 GST_DEBUG_OBJECT (ogg, "activating chain %p", chain);
1726 bitrate = idx_bitrate = 0;
1728 /* first add the pads */
1729 for (i = 0; i < chain->streams->len; i++) {
1731 GstStructure *structure;
1733 pad = g_array_index (chain->streams, GstOggPad *, i);
1735 if (pad->map.idx_bitrate)
1736 idx_bitrate = MAX (idx_bitrate, pad->map.idx_bitrate);
1738 bitrate += pad->map.bitrate;
1741 gst_ogg_pad_mark_discont (pad);
1742 pad->last_ret = GST_FLOW_OK;
1744 if (pad->map.is_skeleton || pad->added || GST_PAD_CAPS (pad) == NULL)
1747 GST_DEBUG_OBJECT (ogg, "adding pad %" GST_PTR_FORMAT, pad);
1749 structure = gst_caps_get_structure (GST_PAD_CAPS (pad), 0);
1751 /* activate first */
1752 gst_pad_set_active (GST_PAD_CAST (pad), TRUE);
1754 gst_element_add_pad (GST_ELEMENT (ogg), GST_PAD_CAST (pad));
1757 /* prefer the index bitrate over the ones encoded in the streams */
1758 ogg->bitrate = (idx_bitrate ? idx_bitrate : bitrate);
1760 /* after adding the new pads, remove the old pads */
1761 gst_ogg_demux_deactivate_current_chain (ogg);
1763 ogg->current_chain = chain;
1765 /* we are finished now */
1766 gst_element_no_more_pads (GST_ELEMENT (ogg));
1768 /* FIXME, must be sent from the streaming thread */
1770 gst_ogg_demux_send_event (ogg, event);
1772 gst_element_found_tags (GST_ELEMENT_CAST (ogg),
1773 gst_tag_list_new_full (GST_TAG_CONTAINER_FORMAT, "Ogg", NULL));
1776 GST_DEBUG_OBJECT (ogg, "starting chain");
1778 /* then send out any headers and queued packets */
1779 for (i = 0; i < chain->streams->len; i++) {
1783 pad = g_array_index (chain->streams, GstOggPad *, i);
1785 /* FIXME also streaming thread */
1786 if (pad->map.taglist) {
1787 GST_DEBUG_OBJECT (ogg, "pushing tags");
1788 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (ogg),
1789 GST_PAD_CAST (pad), pad->map.taglist);
1790 pad->map.taglist = NULL;
1793 GST_DEBUG_OBJECT (ogg, "pushing headers");
1795 for (walk = pad->map.headers; walk; walk = g_list_next (walk)) {
1796 ogg_packet *p = walk->data;
1798 gst_ogg_demux_chain_peer (pad, p, TRUE);
1801 GST_DEBUG_OBJECT (ogg, "pushing queued buffers");
1802 /* push queued packets */
1803 for (walk = pad->map.queued; walk; walk = g_list_next (walk)) {
1804 ogg_packet *p = walk->data;
1806 gst_ogg_demux_chain_peer (pad, p, TRUE);
1807 _ogg_packet_free (p);
1809 /* and free the queued buffers */
1810 g_list_free (pad->map.queued);
1811 pad->map.queued = NULL;
1817 do_binary_search (GstOggDemux * ogg, GstOggChain * chain, gint64 begin,
1818 gint64 end, gint64 begintime, gint64 endtime, gint64 target,
1827 GST_DEBUG_OBJECT (ogg,
1828 "chain offset %" G_GINT64_FORMAT ", end offset %" G_GINT64_FORMAT,
1830 GST_DEBUG_OBJECT (ogg,
1831 "chain begin time %" GST_TIME_FORMAT ", end time %" GST_TIME_FORMAT,
1832 GST_TIME_ARGS (begintime), GST_TIME_ARGS (endtime));
1833 GST_DEBUG_OBJECT (ogg, "target %" GST_TIME_FORMAT, GST_TIME_ARGS (target));
1835 /* perform the seek */
1836 while (begin < end) {
1839 if ((end - begin < CHUNKSIZE) || (endtime == begintime)) {
1842 /* take a (pretty decent) guess, avoiding overflow */
1843 gint64 rate = (end - begin) * GST_MSECOND / (endtime - begintime);
1845 bisect = (target - begintime) / GST_MSECOND * rate + begin - CHUNKSIZE;
1847 if (bisect <= begin)
1849 GST_DEBUG_OBJECT (ogg, "Initial guess: %" G_GINT64_FORMAT, bisect);
1851 gst_ogg_demux_seek (ogg, bisect);
1853 while (begin < end) {
1856 GST_DEBUG_OBJECT (ogg,
1857 "after seek, bisect %" G_GINT64_FORMAT ", begin %" G_GINT64_FORMAT
1858 ", end %" G_GINT64_FORMAT, bisect, begin, end);
1860 ret = gst_ogg_demux_get_next_page (ogg, &og, end - ogg->offset, &result);
1861 GST_LOG_OBJECT (ogg, "looking for next page returned %" G_GINT64_FORMAT,
1864 if (ret == GST_FLOW_LIMIT) {
1865 /* we hit the upper limit, go back a bit */
1866 if (bisect <= begin + 1) {
1867 end = begin; /* found it */
1872 bisect -= CHUNKSIZE;
1873 if (bisect <= begin)
1876 gst_ogg_demux_seek (ogg, bisect);
1878 } else if (ret == GST_FLOW_OK) {
1879 /* found offset of next ogg page */
1881 GstClockTime granuletime;
1884 /* get the granulepos */
1885 GST_LOG_OBJECT (ogg, "found next ogg page at %" G_GINT64_FORMAT,
1887 granulepos = ogg_page_granulepos (&og);
1888 if (granulepos == -1) {
1889 GST_LOG_OBJECT (ogg, "granulepos of next page is -1");
1893 /* get the stream */
1894 pad = gst_ogg_chain_get_stream (chain, ogg_page_serialno (&og));
1895 if (pad == NULL || pad->map.is_skeleton)
1898 /* convert granulepos to time */
1899 granuletime = gst_ogg_stream_get_end_time_for_granulepos (&pad->map,
1901 if (granuletime < pad->start_time)
1904 GST_LOG_OBJECT (ogg, "granulepos %" G_GINT64_FORMAT " maps to time %"
1905 GST_TIME_FORMAT, granulepos, GST_TIME_ARGS (granuletime));
1907 granuletime -= pad->start_time;
1908 granuletime += chain->begin_time;
1910 GST_DEBUG_OBJECT (ogg,
1911 "found page with granule %" G_GINT64_FORMAT " and time %"
1912 GST_TIME_FORMAT, granulepos, GST_TIME_ARGS (granuletime));
1914 if (granuletime < target) {
1915 best = result; /* raw offset of packet with granulepos */
1916 begin = ogg->offset; /* raw offset of next page */
1917 begintime = granuletime;
1919 bisect = begin; /* *not* begin + 1 */
1921 if (bisect <= begin + 1) {
1922 end = begin; /* found it */
1924 if (end == ogg->offset) { /* we're pretty close - we'd be stuck in */
1926 bisect -= CHUNKSIZE; /* an endless loop otherwise. */
1927 if (bisect <= begin)
1929 gst_ogg_demux_seek (ogg, bisect);
1932 endtime = granuletime;
1941 GST_DEBUG_OBJECT (ogg, "seeking to %" G_GINT64_FORMAT, best);
1942 gst_ogg_demux_seek (ogg, best);
1950 GST_DEBUG_OBJECT (ogg, "got a seek error");
1956 do_index_search (GstOggDemux * ogg, GstOggChain * chain, gint64 begin,
1957 gint64 end, gint64 begintime, gint64 endtime, gint64 target,
1958 gint64 * p_offset, gint64 * p_timestamp)
1961 guint64 timestamp, offset;
1962 guint64 r_timestamp, r_offset;
1963 gboolean result = FALSE;
1965 target -= begintime;
1970 for (i = 0; i < chain->streams->len; i++) {
1971 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
1974 if (gst_ogg_map_search_index (&pad->map, TRUE, ×tamp, &offset)) {
1975 GST_INFO ("found %" G_GUINT64_FORMAT " at offset %" G_GUINT64_FORMAT,
1978 if (r_offset == -1 || offset < r_offset) {
1980 r_timestamp = timestamp;
1987 *p_timestamp = r_timestamp;
1989 *p_offset = r_offset;
1995 * do seek to time @position, return FALSE or chain and TRUE
1998 gst_ogg_demux_do_seek (GstOggDemux * ogg, GstSegment * segment,
1999 gboolean accurate, gboolean keyframe, GstOggChain ** rchain)
2002 GstOggChain *chain = NULL;
2004 gint64 begintime, endtime;
2005 gint64 target, keytarget;
2010 gint i, pending, len;
2011 gboolean first_parsed_page = TRUE;
2013 position = segment->last_stop;
2015 /* first find the chain to search in */
2016 total = ogg->total_time;
2017 if (ogg->chains->len == 0)
2020 for (i = ogg->chains->len - 1; i >= 0; i--) {
2021 chain = g_array_index (ogg->chains, GstOggChain *, i);
2022 total -= chain->total_time;
2023 if (position >= total)
2027 /* first step, locate page containing the required data */
2028 begin = chain->offset;
2029 end = chain->end_offset;
2030 begintime = chain->begin_time;
2031 endtime = begintime + chain->total_time;
2032 target = position - total + begintime;
2034 if (!do_binary_search (ogg, chain, begin, end, begintime, endtime, target,
2038 /* second step: find pages for all streams, we use the keyframe_granule to keep
2039 * track of which ones we saw. If we have seen a page for each stream we can
2040 * calculate the positions of each keyframe. */
2041 GST_DEBUG_OBJECT (ogg, "find keyframes");
2042 len = pending = chain->streams->len;
2044 /* figure out where the keyframes are */
2051 GstClockTime keyframe_time, granule_time;
2053 ret = gst_ogg_demux_get_next_page (ogg, &og, end - ogg->offset, &result);
2054 GST_LOG_OBJECT (ogg, "looking for next page returned %" G_GINT64_FORMAT,
2056 if (ret == GST_FLOW_LIMIT) {
2057 GST_LOG_OBJECT (ogg, "reached limit");
2059 } else if (ret != GST_FLOW_OK)
2062 /* get the stream */
2063 pad = gst_ogg_chain_get_stream (chain, ogg_page_serialno (&og));
2067 if (pad->map.is_skeleton)
2070 granulepos = ogg_page_granulepos (&og);
2071 if (granulepos == -1) {
2072 GST_LOG_OBJECT (ogg, "granulepos of next page is -1");
2076 /* we only do this the first time we pass here */
2077 if (first_parsed_page) {
2078 /* Now that we have a time reference from the page, we can check
2079 * whether all streams still have pages from here on.
2081 * This would be more elegant before the loop, but getting the page from
2082 * there without breaking anything would be more costly */
2083 granule_time = gst_ogg_stream_get_end_time_for_granulepos (&pad->map,
2085 for (i = 0; i < len; i++) {
2086 GstOggPad *stream = g_array_index (chain->streams, GstOggPad *, i);
2089 /* we already know we have at least one page (the current one)
2090 * for this stream */
2093 if (granule_time > stream->map.total_time)
2094 /* we won't encounter any more pages of this stream, so we don't
2095 * try finding a key frame for it */
2098 first_parsed_page = FALSE;
2102 /* in reverse we want to go past the page with the lower timestamp */
2103 if (segment->rate < 0.0) {
2104 /* get time for this pad */
2105 granule_time = gst_ogg_stream_get_end_time_for_granulepos (&pad->map,
2108 GST_LOG_OBJECT (ogg,
2109 "looking at page with ts %" GST_TIME_FORMAT ", target %"
2110 GST_TIME_FORMAT, GST_TIME_ARGS (granule_time),
2111 GST_TIME_ARGS (target));
2112 if (granule_time < target)
2116 /* we've seen this pad before */
2117 if (pad->keyframe_granule != -1)
2120 /* convert granule of this pad to the granule of the keyframe */
2121 pad->keyframe_granule =
2122 gst_ogg_stream_granulepos_to_key_granule (&pad->map, granulepos);
2123 GST_LOG_OBJECT (ogg, "marking stream granule %" G_GINT64_FORMAT,
2124 pad->keyframe_granule);
2126 /* get time of the keyframe */
2128 gst_ogg_stream_granule_to_time (&pad->map, pad->keyframe_granule);
2129 GST_LOG_OBJECT (ogg, "stream %08lx granule time %" GST_TIME_FORMAT,
2130 pad->map.serialno, GST_TIME_ARGS (keyframe_time));
2132 /* collect smallest value */
2133 if (keyframe_time != -1) {
2134 keyframe_time += begintime;
2135 if (keyframe_time < keytarget)
2136 keytarget = keyframe_time;
2145 /* for negative rates we will get to the keyframe backwards */
2146 if (segment->rate < 0.0)
2149 if (keytarget != target) {
2150 GST_LOG_OBJECT (ogg, "final seek to target %" GST_TIME_FORMAT,
2151 GST_TIME_ARGS (keytarget));
2153 /* last step, seek to the location of the keyframe */
2154 if (!do_binary_search (ogg, chain, begin, end, begintime, endtime,
2158 /* seek back to previous position */
2159 GST_LOG_OBJECT (ogg, "keyframe on target");
2160 gst_ogg_demux_seek (ogg, best);
2165 if (segment->rate > 0.0)
2166 segment->time = keytarget;
2167 segment->last_stop = keytarget - begintime;
2176 GST_DEBUG_OBJECT (ogg, "no chains");
2181 GST_DEBUG_OBJECT (ogg, "got a seek error");
2186 /* does not take ownership of the event */
2188 gst_ogg_demux_perform_seek_pull (GstOggDemux * ogg, GstEvent * event)
2190 GstOggChain *chain = NULL;
2192 gboolean flush, accurate, keyframe;
2196 GstSeekType cur_type, stop_type;
2203 GST_DEBUG_OBJECT (ogg, "seek with event");
2205 gst_event_parse_seek (event, &rate, &format, &flags,
2206 &cur_type, &cur, &stop_type, &stop);
2208 /* we can only seek on time */
2209 if (format != GST_FORMAT_TIME) {
2210 GST_DEBUG_OBJECT (ogg, "can only seek on TIME");
2213 seqnum = gst_event_get_seqnum (event);
2215 GST_DEBUG_OBJECT (ogg, "seek without event");
2219 seqnum = gst_util_seqnum_next ();
2222 GST_DEBUG_OBJECT (ogg, "seek, rate %g", rate);
2224 flush = flags & GST_SEEK_FLAG_FLUSH;
2225 accurate = flags & GST_SEEK_FLAG_ACCURATE;
2226 keyframe = flags & GST_SEEK_FLAG_KEY_UNIT;
2228 /* first step is to unlock the streaming thread if it is
2229 * blocked in a chain call, we do this by starting the flush. because
2230 * we cannot yet hold any streaming lock, we have to protect the chains
2231 * with their own lock. */
2235 tevent = gst_event_new_flush_start ();
2236 gst_event_set_seqnum (tevent, seqnum);
2238 gst_event_ref (tevent);
2239 gst_pad_push_event (ogg->sinkpad, tevent);
2241 GST_CHAIN_LOCK (ogg);
2242 for (i = 0; i < ogg->chains->len; i++) {
2243 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
2246 for (j = 0; j < chain->streams->len; j++) {
2247 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, j);
2249 gst_event_ref (tevent);
2250 gst_pad_push_event (GST_PAD (pad), tevent);
2253 GST_CHAIN_UNLOCK (ogg);
2255 gst_event_unref (tevent);
2257 gst_pad_pause_task (ogg->sinkpad);
2260 /* now grab the stream lock so that streaming cannot continue, for
2261 * non flushing seeks when the element is in PAUSED this could block
2263 GST_PAD_STREAM_LOCK (ogg->sinkpad);
2265 if (ogg->segment_running && !flush) {
2266 /* create the segment event to close the current segment */
2267 if ((chain = ogg->current_chain)) {
2269 gint64 chain_start = 0;
2271 if (chain->segment_start != GST_CLOCK_TIME_NONE)
2272 chain_start = chain->segment_start;
2274 newseg = gst_event_new_new_segment (TRUE, ogg->segment.rate,
2275 GST_FORMAT_TIME, ogg->segment.start + chain_start,
2276 ogg->segment.last_stop + chain_start, ogg->segment.time);
2277 /* set the seqnum of the running segment */
2278 gst_event_set_seqnum (newseg, ogg->seqnum);
2280 /* send segment on old chain, FIXME, must be sent from streaming thread. */
2281 gst_ogg_demux_send_event (ogg, newseg);
2286 gst_segment_set_seek (&ogg->segment, rate, format, flags,
2287 cur_type, cur, stop_type, stop, &update);
2290 GST_DEBUG_OBJECT (ogg, "segment positions set to %" GST_TIME_FORMAT "-%"
2291 GST_TIME_FORMAT, GST_TIME_ARGS (ogg->segment.start),
2292 GST_TIME_ARGS (ogg->segment.stop));
2294 /* we need to stop flushing on the srcpad as we're going to use it
2295 * next. We can do this as we have the STREAM lock now. */
2297 tevent = gst_event_new_flush_stop ();
2298 gst_event_set_seqnum (tevent, seqnum);
2299 gst_pad_push_event (ogg->sinkpad, tevent);
2305 /* reset all ogg streams now, need to do this from within the lock to
2306 * make sure the streaming thread is not messing with the stream */
2307 for (i = 0; i < ogg->chains->len; i++) {
2308 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
2310 gst_ogg_chain_reset (chain);
2314 /* for reverse we will already seek accurately */
2315 res = gst_ogg_demux_do_seek (ogg, &ogg->segment, accurate, keyframe, &chain);
2317 /* seek failed, make sure we continue the current chain */
2319 GST_DEBUG_OBJECT (ogg, "seek failed");
2320 chain = ogg->current_chain;
2322 GST_DEBUG_OBJECT (ogg, "seek success");
2328 /* now we have a new position, prepare for streaming again */
2333 gint64 last_stop, begin_time;
2335 /* we have to send the flush to the old chain, not the new one */
2337 tevent = gst_event_new_flush_stop ();
2338 gst_event_set_seqnum (tevent, seqnum);
2339 gst_ogg_demux_send_event (ogg, tevent);
2342 /* we need this to see how far inside the chain we need to start */
2343 if (chain->begin_time != GST_CLOCK_TIME_NONE)
2344 begin_time = chain->begin_time;
2348 /* segment.start gives the start over all chains, we calculate the amount
2349 * of time into this chain we need to start */
2350 start = ogg->segment.start - begin_time;
2351 if (chain->segment_start != GST_CLOCK_TIME_NONE)
2352 start += chain->segment_start;
2354 if ((stop = ogg->segment.stop) == -1)
2355 stop = ogg->segment.duration;
2357 /* segment.stop gives the stop time over all chains, calculate the amount of
2358 * time we need to stop in this chain */
2360 if (stop > begin_time)
2364 stop += chain->segment_start;
2365 /* we must stop when this chain ends and switch to the next chain to play
2366 * the remainder of the segment. */
2367 stop = MIN (stop, chain->segment_stop);
2370 last_stop = ogg->segment.last_stop;
2371 if (chain->segment_start != GST_CLOCK_TIME_NONE)
2372 last_stop += chain->segment_start;
2374 /* create the segment event we are going to send out */
2375 if (ogg->segment.rate >= 0.0)
2376 event = gst_event_new_new_segment (FALSE, ogg->segment.rate,
2377 ogg->segment.format, last_stop, stop, ogg->segment.time);
2379 event = gst_event_new_new_segment (FALSE, ogg->segment.rate,
2380 ogg->segment.format, start, last_stop, ogg->segment.time);
2382 gst_event_set_seqnum (event, seqnum);
2384 if (chain != ogg->current_chain) {
2385 /* switch to different chain, send segment on new chain */
2386 gst_ogg_demux_activate_chain (ogg, chain, event);
2388 /* mark discont and send segment on current chain */
2389 gst_ogg_chain_mark_discont (chain);
2390 /* This event should be sent from the streaming thread (sink pad task) */
2391 if (ogg->newsegment)
2392 gst_event_unref (ogg->newsegment);
2393 ogg->newsegment = event;
2396 /* notify start of new segment */
2397 if (ogg->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2398 GstMessage *message;
2400 message = gst_message_new_segment_start (GST_OBJECT (ogg),
2401 GST_FORMAT_TIME, ogg->segment.last_stop);
2402 gst_message_set_seqnum (message, seqnum);
2404 gst_element_post_message (GST_ELEMENT (ogg), message);
2407 ogg->segment_running = TRUE;
2408 ogg->seqnum = seqnum;
2409 /* restart our task since it might have been stopped when we did the
2411 gst_pad_start_task (ogg->sinkpad, (GstTaskFunction) gst_ogg_demux_loop,
2415 /* streaming can continue now */
2416 GST_PAD_STREAM_UNLOCK (ogg->sinkpad);
2423 GST_DEBUG_OBJECT (ogg, "seek failed");
2428 GST_DEBUG_OBJECT (ogg, "no chain to seek in");
2429 GST_PAD_STREAM_UNLOCK (ogg->sinkpad);
2435 gst_ogg_demux_perform_seek_push (GstOggDemux * ogg, GstEvent * event)
2438 gboolean res = TRUE;
2442 GstSeekType start_type, stop_type;
2446 gint64 best, best_time;
2448 gst_event_parse_seek (event, &rate, &format, &flags,
2449 &start_type, &start, &stop_type, &stop);
2451 if (format != GST_FORMAT_TIME) {
2452 GST_DEBUG_OBJECT (ogg, "can only seek on TIME");
2456 chain = ogg->current_chain;
2460 if (do_index_search (ogg, chain, 0, -1, 0, -1, start, &best, &best_time)) {
2461 /* the index gave some result */
2462 GST_DEBUG_OBJECT (ogg,
2463 "found offset %" G_GINT64_FORMAT " with time %" G_GUINT64_FORMAT,
2466 } else if ((bitrate = ogg->bitrate) > 0) {
2467 /* try with bitrate convert the seek positions to bytes */
2468 if (start_type != GST_SEEK_TYPE_NONE) {
2469 start = gst_util_uint64_scale (start, bitrate, 8 * GST_SECOND);
2471 if (stop_type != GST_SEEK_TYPE_NONE) {
2472 stop = gst_util_uint64_scale (stop, bitrate, 8 * GST_SECOND);
2480 GST_DEBUG_OBJECT (ogg,
2481 "seeking to %" G_GINT64_FORMAT " - %" G_GINT64_FORMAT, start, stop);
2483 sevent = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
2484 start_type, start, stop_type, stop);
2486 res = gst_pad_push_event (ogg->sinkpad, sevent);
2494 GST_DEBUG_OBJECT (ogg, "seek failed");
2500 gst_ogg_demux_perform_seek (GstOggDemux * ogg, GstEvent * event)
2504 if (ogg->pullmode) {
2505 res = gst_ogg_demux_perform_seek_pull (ogg, event);
2507 res = gst_ogg_demux_perform_seek_push (ogg, event);
2513 /* finds each bitstream link one at a time using a bisection search
2514 * (has to begin by knowing the offset of the lb's initial page).
2515 * Recurses for each link so it can alloc the link storage after
2516 * finding them all, then unroll and fill the cache at the same time
2518 static GstFlowReturn
2519 gst_ogg_demux_bisect_forward_serialno (GstOggDemux * ogg,
2520 gint64 begin, gint64 searched, gint64 end, GstOggChain * chain, glong m)
2522 gint64 endsearched = end;
2527 GstOggChain *nextchain;
2529 GST_LOG_OBJECT (ogg,
2530 "bisect begin: %" G_GINT64_FORMAT ", searched: %" G_GINT64_FORMAT
2531 ", end %" G_GINT64_FORMAT ", chain: %p", begin, searched, end, chain);
2533 /* the below guards against garbage seperating the last and
2534 * first pages of two links. */
2535 while (searched < endsearched) {
2538 if (endsearched - searched < CHUNKSIZE) {
2541 bisect = (searched + endsearched) / 2;
2544 gst_ogg_demux_seek (ogg, bisect);
2545 ret = gst_ogg_demux_get_next_page (ogg, &og, -1, &offset);
2547 if (ret == GST_FLOW_UNEXPECTED) {
2548 endsearched = bisect;
2549 } else if (ret == GST_FLOW_OK) {
2550 glong serial = ogg_page_serialno (&og);
2552 if (!gst_ogg_chain_has_stream (chain, serial)) {
2553 endsearched = bisect;
2556 searched = offset + og.header_len + og.body_len;
2562 GST_LOG_OBJECT (ogg, "current chain ends at %" G_GINT64_FORMAT, searched);
2564 chain->end_offset = searched;
2565 ret = gst_ogg_demux_read_end_chain (ogg, chain);
2566 if (ret != GST_FLOW_OK)
2569 GST_LOG_OBJECT (ogg, "found begin at %" G_GINT64_FORMAT, next);
2571 gst_ogg_demux_seek (ogg, next);
2572 ret = gst_ogg_demux_read_chain (ogg, &nextchain);
2573 if (ret == GST_FLOW_UNEXPECTED) {
2576 GST_LOG_OBJECT (ogg, "no next chain");
2577 } else if (ret != GST_FLOW_OK)
2580 if (searched < end && nextchain != NULL) {
2581 ret = gst_ogg_demux_bisect_forward_serialno (ogg, next, ogg->offset,
2582 end, nextchain, m + 1);
2583 if (ret != GST_FLOW_OK)
2586 GST_LOG_OBJECT (ogg, "adding chain %p", chain);
2588 g_array_insert_val (ogg->chains, 0, chain);
2594 /* read a chain from the ogg file. This code will
2595 * read all BOS pages and will create and return a GstOggChain
2596 * structure with the results.
2598 * This function will also read N pages from each stream in the
2599 * chain and submit them to the decoders. When the decoder has
2600 * decoded the first buffer, we know the timestamp of the first
2601 * page in the chain.
2603 static GstFlowReturn
2604 gst_ogg_demux_read_chain (GstOggDemux * ogg, GstOggChain ** res_chain)
2607 GstOggChain *chain = NULL;
2608 gint64 offset = ogg->offset;
2613 GST_LOG_OBJECT (ogg, "reading chain at %" G_GINT64_FORMAT, offset);
2615 /* first read the BOS pages, do typefind on them, create
2616 * the decoders, send data to the decoders. */
2621 ret = gst_ogg_demux_get_next_page (ogg, &op, -1, NULL);
2622 if (ret != GST_FLOW_OK) {
2623 GST_WARNING_OBJECT (ogg, "problem reading BOS page: ret=%d", ret);
2626 if (!ogg_page_bos (&op)) {
2627 GST_WARNING_OBJECT (ogg, "page is not BOS page");
2628 /* if we did not find a chain yet, assume this is a bogus stream and
2631 ret = GST_FLOW_UNEXPECTED;
2635 if (chain == NULL) {
2636 chain = gst_ogg_chain_new (ogg);
2637 chain->offset = offset;
2640 serial = ogg_page_serialno (&op);
2641 if (gst_ogg_chain_get_stream (chain, serial) != NULL) {
2642 GST_WARNING_OBJECT (ogg, "found serial %08lx BOS page twice, ignoring",
2647 pad = gst_ogg_chain_new_stream (chain, serial);
2648 gst_ogg_pad_submit_page (pad, &op);
2651 if (ret != GST_FLOW_OK || chain == NULL) {
2652 if (ret == GST_FLOW_OK) {
2653 GST_WARNING_OBJECT (ogg, "no chain was found");
2654 ret = GST_FLOW_ERROR;
2655 } else if (ret != GST_FLOW_UNEXPECTED) {
2656 GST_WARNING_OBJECT (ogg, "failed to read chain");
2658 GST_DEBUG_OBJECT (ogg, "done reading chains");
2661 gst_ogg_chain_free (chain);
2668 chain->have_bos = TRUE;
2669 GST_LOG_OBJECT (ogg, "read bos pages, init decoder now");
2671 /* now read pages until we receive a buffer from each of the
2672 * stream decoders, this will tell us the timestamp of the
2673 * first packet in the chain then */
2675 /* save the offset to the first non bos page in the chain: if searching for
2676 * pad->first_time we read past the end of the chain, we'll seek back to this
2679 offset = ogg->offset;
2684 gboolean known_serial = FALSE;
2687 serial = ogg_page_serialno (&op);
2689 for (i = 0; i < chain->streams->len; i++) {
2690 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
2692 GST_LOG_OBJECT (ogg, "serial %08lx time %" GST_TIME_FORMAT,
2693 pad->map.serialno, GST_TIME_ARGS (pad->start_time));
2695 if (pad->map.serialno == serial) {
2696 known_serial = TRUE;
2698 /* submit the page now, this will fill in the start_time when the
2699 * internal decoder finds it */
2700 gst_ogg_pad_submit_page (pad, &op);
2702 if (!pad->map.is_skeleton && pad->start_time == -1
2703 && ogg_page_eos (&op)) {
2704 /* got EOS on a pad before we could find its start_time.
2705 * We have no chance of finding a start_time for every pad so
2706 * stop searching for the other start_time(s).
2712 /* the timestamp will be filled in when we submit the pages */
2713 if (!pad->map.is_sparse)
2714 done &= (pad->start_time != GST_CLOCK_TIME_NONE);
2716 GST_LOG_OBJECT (ogg, "done %08lx now %d", pad->map.serialno, done);
2719 /* we read a page not belonging to the current chain: seek back to the
2720 * beginning of the chain
2722 if (!known_serial) {
2723 GST_LOG_OBJECT (ogg, "unknown serial %08lx", serial);
2724 gst_ogg_demux_seek (ogg, offset);
2729 ret = gst_ogg_demux_get_next_page (ogg, &op, -1, NULL);
2730 if (ret != GST_FLOW_OK)
2734 GST_LOG_OBJECT (ogg, "done reading chain");
2735 /* now we can fill in the missing info using queries */
2736 for (i = 0; i < chain->streams->len; i++) {
2737 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
2739 if (pad->map.is_skeleton)
2742 pad->mode = GST_OGG_PAD_MODE_STREAMING;
2751 /* read the last pages from the ogg stream to get the final
2754 static GstFlowReturn
2755 gst_ogg_demux_read_end_chain (GstOggDemux * ogg, GstOggChain * chain)
2757 gint64 begin = chain->end_offset;
2759 gint64 last_granule = -1;
2760 GstOggPad *last_pad = NULL;
2762 gboolean done = FALSE;
2771 gst_ogg_demux_seek (ogg, begin);
2773 /* now continue reading until we run out of data, if we find a page
2774 * start, we save it. It might not be the final page as there could be
2775 * another page after this one. */
2776 while (ogg->offset < end) {
2777 ret = gst_ogg_demux_get_next_page (ogg, &og, end - ogg->offset, NULL);
2779 if (ret == GST_FLOW_LIMIT)
2781 if (ret != GST_FLOW_OK)
2784 for (i = 0; i < chain->streams->len; i++) {
2785 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
2787 if (pad->map.is_sparse)
2790 if (pad->map.serialno == ogg_page_serialno (&og)) {
2791 gint64 granulepos = ogg_page_granulepos (&og);
2793 if (granulepos != -1) {
2794 last_granule = granulepos;
2805 chain->segment_stop =
2806 gst_ogg_stream_get_end_time_for_granulepos (&last_pad->map,
2809 chain->segment_stop = GST_CLOCK_TIME_NONE;
2812 GST_INFO ("segment stop %" G_GUINT64_FORMAT, chain->segment_stop);
2817 /* find a pad with a given serial number
2820 gst_ogg_demux_find_pad (GstOggDemux * ogg, glong serialno)
2825 /* first look in building chain if any */
2826 if (ogg->building_chain) {
2827 pad = gst_ogg_chain_get_stream (ogg->building_chain, serialno);
2832 /* then look in current chain if any */
2833 if (ogg->current_chain) {
2834 pad = gst_ogg_chain_get_stream (ogg->current_chain, serialno);
2839 for (i = 0; i < ogg->chains->len; i++) {
2840 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
2842 pad = gst_ogg_chain_get_stream (chain, serialno);
2849 /* find a chain with a given serial number
2851 static GstOggChain *
2852 gst_ogg_demux_find_chain (GstOggDemux * ogg, glong serialno)
2856 pad = gst_ogg_demux_find_pad (ogg, serialno);
2863 /* returns TRUE if all streams have valid start time */
2865 gst_ogg_demux_collect_chain_info (GstOggDemux * ogg, GstOggChain * chain)
2867 gboolean res = TRUE;
2869 chain->total_time = GST_CLOCK_TIME_NONE;
2870 GST_DEBUG_OBJECT (ogg, "trying to collect chain info");
2872 /* see if we have a start time on all streams */
2873 chain->segment_start = gst_ogg_demux_collect_start_time (ogg, chain);
2875 if (chain->segment_start == G_MAXUINT64) {
2876 /* not yet, stream some more data */
2878 } else if (chain->segment_stop != GST_CLOCK_TIME_NONE) {
2879 /* we can calculate a total time */
2880 chain->total_time = chain->segment_stop - chain->segment_start;
2883 GST_DEBUG ("total time %" G_GUINT64_FORMAT, chain->total_time);
2885 GST_DEBUG_OBJECT (ogg, "return %d", res);
2891 gst_ogg_demux_collect_info (GstOggDemux * ogg)
2895 /* collect all info */
2896 ogg->total_time = 0;
2898 for (i = 0; i < ogg->chains->len; i++) {
2899 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
2901 chain->begin_time = ogg->total_time;
2903 gst_ogg_demux_collect_chain_info (ogg, chain);
2905 ogg->total_time += chain->total_time;
2907 gst_segment_set_duration (&ogg->segment, GST_FORMAT_TIME, ogg->total_time);
2910 /* find all the chains in the ogg file, this reads the first and
2911 * last page of the ogg stream, if they match then the ogg file has
2912 * just one chain, else we do a binary search for all chains.
2914 static GstFlowReturn
2915 gst_ogg_demux_find_chains (GstOggDemux * ogg)
2925 /* get peer to figure out length */
2926 if ((peer = gst_pad_get_peer (ogg->sinkpad)) == NULL)
2929 /* find length to read last page, we store this for later use. */
2930 format = GST_FORMAT_BYTES;
2931 res = gst_pad_query_duration (peer, &format, &ogg->length);
2932 gst_object_unref (peer);
2933 if (!res || ogg->length <= 0)
2936 GST_DEBUG_OBJECT (ogg, "file length %" G_GINT64_FORMAT, ogg->length);
2938 /* read chain from offset 0, this is the first chain of the
2940 gst_ogg_demux_seek (ogg, 0);
2941 ret = gst_ogg_demux_read_chain (ogg, &chain);
2942 if (ret != GST_FLOW_OK)
2943 goto no_first_chain;
2945 /* read page from end offset, we use this page to check if its serial
2946 * number is contained in the first chain. If this is the case then
2947 * this ogg is not a chained ogg and we can skip the scanning. */
2948 gst_ogg_demux_seek (ogg, ogg->length);
2949 ret = gst_ogg_demux_get_prev_page (ogg, &og, NULL);
2950 if (ret != GST_FLOW_OK)
2953 serialno = ogg_page_serialno (&og);
2955 if (!gst_ogg_chain_has_stream (chain, serialno)) {
2956 /* the last page is not in the first stream, this means we should
2957 * find all the chains in this chained ogg. */
2959 gst_ogg_demux_bisect_forward_serialno (ogg, 0, 0, ogg->length, chain,
2962 /* we still call this function here but with an empty range so that
2963 * we can reuse the setup code in this routine. */
2965 gst_ogg_demux_bisect_forward_serialno (ogg, 0, ogg->length,
2966 ogg->length, chain, 0);
2968 if (ret != GST_FLOW_OK)
2971 /* all fine, collect and print */
2972 gst_ogg_demux_collect_info (ogg);
2974 /* dump our chains and streams */
2975 gst_ogg_print (ogg);
2980 /*** error cases ***/
2983 GST_ELEMENT_ERROR (ogg, STREAM, DEMUX, (NULL), ("we don't have a peer"));
2984 return GST_FLOW_NOT_LINKED;
2988 GST_ELEMENT_ERROR (ogg, STREAM, DEMUX, (NULL), ("can't get file length"));
2989 return GST_FLOW_NOT_SUPPORTED;
2993 GST_ELEMENT_ERROR (ogg, STREAM, DEMUX, (NULL), ("can't get first chain"));
2994 return GST_FLOW_ERROR;
2998 GST_DEBUG_OBJECT (ogg, "can't get last page");
3000 gst_ogg_chain_free (chain);
3005 static GstFlowReturn
3006 gst_ogg_demux_handle_page (GstOggDemux * ogg, ogg_page * page)
3011 GstFlowReturn result = GST_FLOW_OK;
3013 serialno = ogg_page_serialno (page);
3014 granule = ogg_page_granulepos (page);
3016 GST_LOG_OBJECT (ogg,
3017 "processing ogg page (serial %08lx, pageno %ld, granulepos %"
3018 G_GINT64_FORMAT ", bos %d)",
3019 serialno, ogg_page_pageno (page), granule, ogg_page_bos (page));
3021 if (ogg_page_bos (page)) {
3025 /* see if we know about the chain already */
3026 chain = gst_ogg_demux_find_chain (ogg, serialno);
3031 if (chain->segment_start != GST_CLOCK_TIME_NONE)
3032 start = chain->segment_start;
3034 /* create the newsegment event we are going to send out */
3035 event = gst_event_new_new_segment (FALSE, ogg->segment.rate,
3036 GST_FORMAT_TIME, start, chain->segment_stop, chain->begin_time);
3037 gst_event_set_seqnum (event, ogg->seqnum);
3039 GST_DEBUG_OBJECT (ogg,
3040 "segment: start %" GST_TIME_FORMAT ", stop %" GST_TIME_FORMAT
3041 ", time %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
3042 GST_TIME_ARGS (chain->segment_stop),
3043 GST_TIME_ARGS (chain->begin_time));
3045 /* activate it as it means we have a non-header, this will also deactivate
3046 * the currently running chain. */
3047 gst_ogg_demux_activate_chain (ogg, chain, event);
3048 pad = gst_ogg_demux_find_pad (ogg, serialno);
3050 GstClockTime chain_time;
3051 GstOggChain *current_chain;
3052 gint64 current_time;
3054 /* this can only happen in push mode */
3058 current_chain = ogg->current_chain;
3059 current_time = ogg->segment.last_stop;
3061 /* time of new chain is current time */
3062 chain_time = current_time;
3064 if (ogg->building_chain == NULL) {
3065 GstOggChain *newchain;
3067 newchain = gst_ogg_chain_new (ogg);
3068 newchain->offset = 0;
3069 /* set new chain begin time aligned with end time of old chain */
3070 newchain->begin_time = chain_time;
3071 GST_DEBUG_OBJECT (ogg, "new chain, begin time %" GST_TIME_FORMAT,
3072 GST_TIME_ARGS (chain_time));
3074 /* and this is the one we are building now */
3075 ogg->building_chain = newchain;
3077 pad = gst_ogg_chain_new_stream (ogg->building_chain, serialno);
3080 pad = gst_ogg_demux_find_pad (ogg, serialno);
3083 result = gst_ogg_pad_submit_page (pad, page);
3085 /* no pad. This means an ogg page without bos has been seen for this
3086 * serialno. we just ignore it but post a warning... */
3087 GST_ELEMENT_WARNING (ogg, STREAM, DECODE,
3088 (NULL), ("unknown ogg pad for serial %08lx detected", serialno));
3096 GST_ELEMENT_ERROR (ogg, STREAM, DECODE,
3097 (NULL), ("unknown ogg chain for serial %08lx detected", serialno));
3098 return GST_FLOW_ERROR;
3102 /* streaming mode, receive a buffer, parse it, create pads for
3103 * the serialno, submit pages and packets to the oggpads
3105 static GstFlowReturn
3106 gst_ogg_demux_chain (GstPad * pad, GstBuffer * buffer)
3110 GstFlowReturn result = GST_FLOW_OK;
3112 ogg = GST_OGG_DEMUX (GST_OBJECT_PARENT (pad));
3114 GST_DEBUG_OBJECT (ogg, "chain");
3115 result = gst_ogg_demux_submit_buffer (ogg, buffer);
3117 while (result == GST_FLOW_OK) {
3120 ret = ogg_sync_pageout (&ogg->sync, &page);
3122 /* need more data */
3125 /* discontinuity in the pages */
3126 GST_DEBUG_OBJECT (ogg, "discont in page found, continuing");
3128 result = gst_ogg_demux_handle_page (ogg, &page);
3131 if (ret == 0 || result == GST_FLOW_OK) {
3132 gst_ogg_demux_sync_streams (ogg);
3138 gst_ogg_demux_send_event (GstOggDemux * ogg, GstEvent * event)
3140 GstOggChain *chain = ogg->current_chain;
3141 gboolean res = TRUE;
3146 for (i = 0; i < chain->streams->len; i++) {
3147 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
3149 gst_event_ref (event);
3150 GST_DEBUG_OBJECT (pad, "Pushing event %" GST_PTR_FORMAT, event);
3151 res &= gst_pad_push_event (GST_PAD (pad), event);
3154 gst_event_unref (event);
3159 static GstFlowReturn
3160 gst_ogg_demux_combine_flows (GstOggDemux * ogg, GstOggPad * pad,
3165 /* store the value */
3166 pad->last_ret = ret;
3168 /* any other error that is not-linked can be returned right
3170 if (ret != GST_FLOW_NOT_LINKED)
3173 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3174 chain = ogg->current_chain;
3178 for (i = 0; i < chain->streams->len; i++) {
3179 GstOggPad *opad = g_array_index (chain->streams, GstOggPad *, i);
3181 ret = opad->last_ret;
3182 /* some other return value (must be SUCCESS but we can return
3183 * other values as well) */
3184 if (ret != GST_FLOW_NOT_LINKED)
3187 /* if we get here, all other pads were unlinked and we return
3188 * NOT_LINKED then */
3194 /* returns TRUE if all streams in current chain reached EOS, FALSE otherwise */
3196 gst_ogg_demux_check_eos (GstOggDemux * ogg)
3199 gboolean eos = TRUE;
3201 chain = ogg->current_chain;
3202 if (G_LIKELY (chain)) {
3205 for (i = 0; i < chain->streams->len; i++) {
3206 GstOggPad *opad = g_array_index (chain->streams, GstOggPad *, i);
3208 eos = eos && opad->is_eos;
3217 static GstFlowReturn
3218 gst_ogg_demux_loop_forward (GstOggDemux * ogg)
3223 if (ogg->offset == ogg->length) {
3224 GST_LOG_OBJECT (ogg, "no more data to pull %" G_GINT64_FORMAT
3225 " == %" G_GINT64_FORMAT, ogg->offset, ogg->length);
3226 ret = GST_FLOW_UNEXPECTED;
3230 GST_LOG_OBJECT (ogg, "pull data %" G_GINT64_FORMAT, ogg->offset);
3231 ret = gst_pad_pull_range (ogg->sinkpad, ogg->offset, CHUNKSIZE, &buffer);
3232 if (ret != GST_FLOW_OK) {
3233 GST_LOG_OBJECT (ogg, "Failed pull_range");
3237 ogg->offset += GST_BUFFER_SIZE (buffer);
3239 if (G_UNLIKELY (ogg->newsegment)) {
3240 gst_ogg_demux_send_event (ogg, ogg->newsegment);
3241 ogg->newsegment = NULL;
3244 ret = gst_ogg_demux_chain (ogg->sinkpad, buffer);
3245 if (ret != GST_FLOW_OK) {
3246 GST_LOG_OBJECT (ogg, "Failed demux_chain");
3250 /* check for the end of the segment */
3251 if (gst_ogg_demux_check_eos (ogg)) {
3252 GST_LOG_OBJECT (ogg, "got EOS");
3253 ret = GST_FLOW_UNEXPECTED;
3262 * We read the pages backwards and send the packets forwards. The first packet
3263 * in the page will be pushed with the DISCONT flag set.
3265 * Special care has to be taken for continued pages, which we can only decode
3266 * when we have the previous page(s).
3268 static GstFlowReturn
3269 gst_ogg_demux_loop_reverse (GstOggDemux * ogg)
3275 if (ogg->offset == 0) {
3276 GST_LOG_OBJECT (ogg, "no more data to pull %" G_GINT64_FORMAT
3277 " == 0", ogg->offset);
3278 ret = GST_FLOW_UNEXPECTED;
3282 GST_LOG_OBJECT (ogg, "read page from %" G_GINT64_FORMAT, ogg->offset);
3283 ret = gst_ogg_demux_get_prev_page (ogg, &page, &offset);
3284 if (ret != GST_FLOW_OK)
3287 ogg->offset = offset;
3289 if (G_UNLIKELY (ogg->newsegment)) {
3290 gst_ogg_demux_send_event (ogg, ogg->newsegment);
3291 ogg->newsegment = NULL;
3294 ret = gst_ogg_demux_handle_page (ogg, &page);
3295 if (ret != GST_FLOW_OK)
3298 /* check for the end of the segment */
3299 if (gst_ogg_demux_check_eos (ogg)) {
3300 GST_LOG_OBJECT (ogg, "got EOS");
3301 ret = GST_FLOW_UNEXPECTED;
3309 gst_ogg_demux_sync_streams (GstOggDemux * ogg)
3315 chain = ogg->current_chain;
3316 cur = ogg->segment.last_stop;
3317 if (chain == NULL || cur == -1)
3320 for (i = 0; i < chain->streams->len; i++) {
3321 GstOggPad *stream = g_array_index (chain->streams, GstOggPad *, i);
3323 /* Theoretically, we should be doing this for all streams, but we're only
3324 * doing it for known-to-be-sparse streams at the moment in order not to
3325 * break things for wrongly-muxed streams (like we used to produce once) */
3326 if (stream->map.is_sparse && stream->last_stop != GST_CLOCK_TIME_NONE) {
3328 /* Does this stream lag? Random threshold of 2 seconds */
3329 if (GST_CLOCK_DIFF (stream->last_stop, cur) > (2 * GST_SECOND)) {
3330 GST_DEBUG_OBJECT (stream, "synchronizing stream with others by "
3331 "advancing time from %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
3332 GST_TIME_ARGS (stream->last_stop), GST_TIME_ARGS (cur));
3333 stream->last_stop = cur;
3334 /* advance stream time (FIXME: is this right, esp. time_pos?) */
3335 gst_pad_push_event (GST_PAD_CAST (stream),
3336 gst_event_new_new_segment (TRUE, ogg->segment.rate,
3337 GST_FORMAT_TIME, stream->last_stop, -1, stream->last_stop));
3343 /* random access code
3345 * - first find all the chains and streams by scanning the file.
3346 * - then get and chain buffers, just like the streaming case.
3347 * - when seeking, we can use the chain info to perform the seek.
3350 gst_ogg_demux_loop (GstOggPad * pad)
3356 ogg = GST_OGG_DEMUX (GST_OBJECT_PARENT (pad));
3358 if (ogg->need_chains) {
3361 /* this is the only place where we write chains and thus need to lock. */
3362 GST_CHAIN_LOCK (ogg);
3363 ret = gst_ogg_demux_find_chains (ogg);
3364 GST_CHAIN_UNLOCK (ogg);
3365 if (ret != GST_FLOW_OK)
3366 goto chain_read_failed;
3368 ogg->need_chains = FALSE;
3370 GST_OBJECT_LOCK (ogg);
3371 ogg->running = TRUE;
3374 GST_OBJECT_UNLOCK (ogg);
3376 /* and seek to configured positions without FLUSH */
3377 res = gst_ogg_demux_perform_seek_pull (ogg, event);
3379 gst_event_unref (event);
3385 if (ogg->segment.rate >= 0.0)
3386 ret = gst_ogg_demux_loop_forward (ogg);
3388 ret = gst_ogg_demux_loop_reverse (ogg);
3390 if (ret != GST_FLOW_OK)
3393 gst_ogg_demux_sync_streams (ogg);
3399 /* error was posted */
3404 GST_ELEMENT_ERROR (ogg, STREAM, DEMUX, (NULL),
3405 ("failed to start demuxing ogg"));
3406 ret = GST_FLOW_ERROR;
3411 const gchar *reason = gst_flow_get_name (ret);
3412 GstEvent *event = NULL;
3414 GST_LOG_OBJECT (ogg, "pausing task, reason %s", reason);
3415 ogg->segment_running = FALSE;
3416 gst_pad_pause_task (ogg->sinkpad);
3418 if (ret == GST_FLOW_UNEXPECTED) {
3419 /* perform EOS logic */
3420 if (ogg->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3422 GstMessage *message;
3424 /* for segment playback we need to post when (in stream time)
3425 * we stopped, this is either stop (when set) or the duration. */
3426 if ((stop = ogg->segment.stop) == -1)
3427 stop = ogg->segment.duration;
3429 GST_LOG_OBJECT (ogg, "Sending segment done, at end of segment");
3431 gst_message_new_segment_done (GST_OBJECT (ogg), GST_FORMAT_TIME,
3433 gst_message_set_seqnum (message, ogg->seqnum);
3435 gst_element_post_message (GST_ELEMENT (ogg), message);
3437 /* normal playback, send EOS to all linked pads */
3438 GST_LOG_OBJECT (ogg, "Sending EOS, at end of stream");
3439 event = gst_event_new_eos ();
3441 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
3442 GST_ELEMENT_ERROR (ogg, STREAM, FAILED,
3443 (_("Internal data stream error.")),
3444 ("stream stopped, reason %s", reason));
3445 event = gst_event_new_eos ();
3448 /* For wrong-state we still want to pause the task and stop
3449 * but no error message or other things are necessary.
3450 * wrong-state is no real error and will be caused by flushing,
3451 * e.g. because of a flushing seek.
3454 gst_event_set_seqnum (event, ogg->seqnum);
3455 gst_ogg_demux_send_event (ogg, event);
3462 gst_ogg_demux_clear_chains (GstOggDemux * ogg)
3466 gst_ogg_demux_deactivate_current_chain (ogg);
3468 GST_CHAIN_LOCK (ogg);
3469 for (i = 0; i < ogg->chains->len; i++) {
3470 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
3472 gst_ogg_chain_free (chain);
3474 ogg->chains = g_array_set_size (ogg->chains, 0);
3475 GST_CHAIN_UNLOCK (ogg);
3478 /* this function is called when the pad is activated and should start
3481 * We check if we can do random access to decide if we work push or
3485 gst_ogg_demux_sink_activate (GstPad * sinkpad)
3487 if (gst_pad_check_pull_range (sinkpad)) {
3488 GST_DEBUG_OBJECT (sinkpad, "activating pull");
3489 return gst_pad_activate_pull (sinkpad, TRUE);
3491 GST_DEBUG_OBJECT (sinkpad, "activating push");
3492 return gst_pad_activate_push (sinkpad, TRUE);
3496 /* this function gets called when we activate ourselves in push mode.
3497 * We cannot seek (ourselves) in the stream */
3499 gst_ogg_demux_sink_activate_push (GstPad * sinkpad, gboolean active)
3503 ogg = GST_OGG_DEMUX (GST_OBJECT_PARENT (sinkpad));
3505 ogg->pullmode = FALSE;
3506 ogg->resync = FALSE;
3511 /* this function gets called when we activate ourselves in pull mode.
3512 * We can perform random access to the resource and we start a task
3513 * to start reading */
3515 gst_ogg_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
3519 ogg = GST_OGG_DEMUX (GST_OBJECT_PARENT (sinkpad));
3522 ogg->need_chains = TRUE;
3523 ogg->pullmode = TRUE;
3525 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_ogg_demux_loop,
3528 return gst_pad_stop_task (sinkpad);
3532 static GstStateChangeReturn
3533 gst_ogg_demux_change_state (GstElement * element, GstStateChange transition)
3536 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
3538 ogg = GST_OGG_DEMUX (element);
3540 switch (transition) {
3541 case GST_STATE_CHANGE_NULL_TO_READY:
3543 ogg_sync_init (&ogg->sync);
3545 case GST_STATE_CHANGE_READY_TO_PAUSED:
3546 ogg_sync_reset (&ogg->sync);
3547 ogg->running = FALSE;
3549 ogg->segment_running = FALSE;
3550 ogg->total_time = -1;
3551 gst_segment_init (&ogg->segment, GST_FORMAT_TIME);
3553 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3559 result = parent_class->change_state (element, transition);
3561 switch (transition) {
3562 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3564 case GST_STATE_CHANGE_PAUSED_TO_READY:
3565 gst_ogg_demux_clear_chains (ogg);
3566 GST_OBJECT_LOCK (ogg);
3567 ogg->running = FALSE;
3568 ogg->segment_running = FALSE;
3569 GST_OBJECT_UNLOCK (ogg);
3571 case GST_STATE_CHANGE_READY_TO_NULL:
3572 ogg_sync_clear (&ogg->sync);
3581 gst_ogg_demux_plugin_init (GstPlugin * plugin)
3583 GST_DEBUG_CATEGORY_INIT (gst_ogg_demux_debug, "oggdemux", 0, "ogg demuxer");
3584 GST_DEBUG_CATEGORY_INIT (gst_ogg_demux_setup_debug, "oggdemux_setup", 0,
3585 "ogg demuxer setup stage when parsing pipeline");
3588 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
3590 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
3591 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
3594 return gst_element_register (plugin, "oggdemux", GST_RANK_PRIMARY,
3595 GST_TYPE_OGG_DEMUX);
3598 /* prints all info about the element */
3599 #undef GST_CAT_DEFAULT
3600 #define GST_CAT_DEFAULT gst_ogg_demux_setup_debug
3602 #ifdef GST_DISABLE_GST_DEBUG
3605 gst_ogg_print (GstOggDemux * ogg)
3610 #else /* !GST_DISABLE_GST_DEBUG */
3613 gst_ogg_print (GstOggDemux * ogg)
3617 GST_INFO_OBJECT (ogg, "%u chains", ogg->chains->len);
3618 GST_INFO_OBJECT (ogg, " total time: %" GST_TIME_FORMAT,
3619 GST_TIME_ARGS (ogg->total_time));
3621 for (i = 0; i < ogg->chains->len; i++) {
3622 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
3624 GST_INFO_OBJECT (ogg, " chain %d (%u streams):", i, chain->streams->len);
3625 GST_INFO_OBJECT (ogg,
3626 " offset: %" G_GINT64_FORMAT " - %" G_GINT64_FORMAT, chain->offset,
3628 GST_INFO_OBJECT (ogg, " begin time: %" GST_TIME_FORMAT,
3629 GST_TIME_ARGS (chain->begin_time));
3630 GST_INFO_OBJECT (ogg, " total time: %" GST_TIME_FORMAT,
3631 GST_TIME_ARGS (chain->total_time));
3632 GST_INFO_OBJECT (ogg, " segment start: %" GST_TIME_FORMAT,
3633 GST_TIME_ARGS (chain->segment_start));
3634 GST_INFO_OBJECT (ogg, " segment stop: %" GST_TIME_FORMAT,
3635 GST_TIME_ARGS (chain->segment_stop));
3637 for (j = 0; j < chain->streams->len; j++) {
3638 GstOggPad *stream = g_array_index (chain->streams, GstOggPad *, j);
3640 GST_INFO_OBJECT (ogg, " stream %08lx:", stream->map.serialno);
3641 GST_INFO_OBJECT (ogg, " start time: %" GST_TIME_FORMAT,
3642 GST_TIME_ARGS (stream->start_time));
3646 #endif /* GST_DISABLE_GST_DEBUG */