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 /* we hope we get a granpos within this many bytes off the end */
51 #define DURATION_CHUNK_OFFSET (64*1024)
53 /* stop duration checks within this much of EOS */
54 #define EOS_AVOIDANCE_THRESHOLD 8192
56 #define GST_FLOW_LIMIT GST_FLOW_CUSTOM_ERROR
57 #define GST_FLOW_SKIP_PUSH GST_FLOW_CUSTOM_SUCCESS_1
59 #define GST_CHAIN_LOCK(ogg) g_mutex_lock((ogg)->chain_lock)
60 #define GST_CHAIN_UNLOCK(ogg) g_mutex_unlock((ogg)->chain_lock)
62 #define GST_PUSH_LOCK(ogg) \
64 GST_TRACE_OBJECT(ogg, "Push lock"); \
65 g_mutex_lock((ogg)->push_lock); \
68 #define GST_PUSH_UNLOCK(ogg) \
70 GST_TRACE_OBJECT(ogg, "Push unlock"); \
71 g_mutex_unlock((ogg)->push_lock); \
74 GST_DEBUG_CATEGORY (gst_ogg_demux_debug);
75 GST_DEBUG_CATEGORY (gst_ogg_demux_setup_debug);
76 #define GST_CAT_DEFAULT gst_ogg_demux_debug
80 _ogg_packet_copy (const ogg_packet * packet)
82 ogg_packet *ret = g_slice_new (ogg_packet);
85 ret->packet = g_memdup (packet->packet, packet->bytes);
91 _ogg_packet_free (ogg_packet * packet)
93 g_free (packet->packet);
94 g_slice_free (ogg_packet, packet);
98 gst_ogg_page_copy (ogg_page * page)
100 ogg_page *p = g_slice_new (ogg_page);
102 /* make a copy of the page */
103 p->header = g_memdup (page->header, page->header_len);
104 p->header_len = page->header_len;
105 p->body = g_memdup (page->body, page->body_len);
106 p->body_len = page->body_len;
112 gst_ogg_page_free (ogg_page * page)
114 g_free (page->header);
116 g_slice_free (ogg_page, page);
119 static gboolean gst_ogg_demux_collect_chain_info (GstOggDemux * ogg,
120 GstOggChain * chain);
121 static gboolean gst_ogg_demux_activate_chain (GstOggDemux * ogg,
122 GstOggChain * chain, GstEvent * event);
123 static void gst_ogg_pad_mark_discont (GstOggPad * pad);
124 static void gst_ogg_chain_mark_discont (GstOggChain * chain);
126 static gboolean gst_ogg_demux_perform_seek (GstOggDemux * ogg,
128 static gboolean gst_ogg_demux_receive_event (GstElement * element,
131 static void gst_ogg_pad_dispose (GObject * object);
132 static void gst_ogg_pad_finalize (GObject * object);
134 static gboolean gst_ogg_pad_src_query (GstPad * pad, GstObject * parent,
136 static gboolean gst_ogg_pad_event (GstPad * pad, GstObject * parent,
138 static GstOggPad *gst_ogg_chain_get_stream (GstOggChain * chain,
141 static GstFlowReturn gst_ogg_demux_combine_flows (GstOggDemux * ogg,
142 GstOggPad * pad, GstFlowReturn ret);
143 static void gst_ogg_demux_sync_streams (GstOggDemux * ogg);
145 GstCaps *gst_ogg_demux_set_header_on_caps (GstOggDemux * ogg,
146 GstCaps * caps, GList * headers);
147 static gboolean gst_ogg_demux_send_event (GstOggDemux * ogg, GstEvent * event);
148 static gboolean gst_ogg_demux_perform_seek_push (GstOggDemux * ogg,
150 static gboolean gst_ogg_demux_check_duration_push (GstOggDemux * ogg,
151 GstSeekFlags flags, GstEvent * event);
153 GType gst_ogg_pad_get_type (void);
154 G_DEFINE_TYPE (GstOggPad, gst_ogg_pad, GST_TYPE_PAD);
157 gst_ogg_pad_class_init (GstOggPadClass * klass)
159 GObjectClass *gobject_class;
161 gobject_class = (GObjectClass *) klass;
163 gobject_class->dispose = gst_ogg_pad_dispose;
164 gobject_class->finalize = gst_ogg_pad_finalize;
168 gst_ogg_pad_init (GstOggPad * pad)
170 gst_pad_set_event_function (GST_PAD (pad),
171 GST_DEBUG_FUNCPTR (gst_ogg_pad_event));
172 gst_pad_set_query_function (GST_PAD (pad),
173 GST_DEBUG_FUNCPTR (gst_ogg_pad_src_query));
174 gst_pad_use_fixed_caps (GST_PAD (pad));
176 pad->mode = GST_OGG_PAD_MODE_INIT;
178 pad->current_granule = -1;
179 pad->keyframe_granule = -1;
181 pad->start_time = GST_CLOCK_TIME_NONE;
183 pad->position = GST_CLOCK_TIME_NONE;
185 pad->have_type = FALSE;
186 pad->continued = NULL;
187 pad->map.headers = NULL;
188 pad->map.queued = NULL;
190 pad->map.granulerate_n = 0;
191 pad->map.granulerate_d = 0;
192 pad->map.granuleshift = -1;
196 gst_ogg_pad_dispose (GObject * object)
198 GstOggPad *pad = GST_OGG_PAD (object);
203 g_list_foreach (pad->map.headers, (GFunc) _ogg_packet_free, NULL);
204 g_list_free (pad->map.headers);
205 pad->map.headers = NULL;
206 g_list_foreach (pad->map.queued, (GFunc) _ogg_packet_free, NULL);
207 g_list_free (pad->map.queued);
208 pad->map.queued = NULL;
210 g_free (pad->map.index);
211 pad->map.index = NULL;
213 /* clear continued pages */
214 g_list_foreach (pad->continued, (GFunc) gst_ogg_page_free, NULL);
215 g_list_free (pad->continued);
216 pad->continued = NULL;
219 gst_caps_unref (pad->map.caps);
220 pad->map.caps = NULL;
223 if (pad->map.taglist) {
224 gst_tag_list_free (pad->map.taglist);
225 pad->map.taglist = NULL;
228 ogg_stream_reset (&pad->map.stream);
230 G_OBJECT_CLASS (gst_ogg_pad_parent_class)->dispose (object);
234 gst_ogg_pad_finalize (GObject * object)
236 GstOggPad *pad = GST_OGG_PAD (object);
238 ogg_stream_clear (&pad->map.stream);
240 G_OBJECT_CLASS (gst_ogg_pad_parent_class)->finalize (object);
244 gst_ogg_pad_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
249 ogg = GST_OGG_DEMUX (parent);
251 switch (GST_QUERY_TYPE (query)) {
252 case GST_QUERY_DURATION:
255 gint64 total_time = -1;
257 gst_query_parse_duration (query, &format, NULL);
258 /* can only get position in time */
259 if (format != GST_FORMAT_TIME)
262 if (ogg->total_time != -1) {
263 /* we can return the total length */
264 total_time = ogg->total_time;
266 gint bitrate = ogg->bitrate;
268 /* try with length and bitrate */
272 /* ask upstream for total length in bytes */
273 uquery = gst_query_new_duration (GST_FORMAT_BYTES);
274 if (gst_pad_peer_query (ogg->sinkpad, uquery)) {
277 gst_query_parse_duration (uquery, NULL, &length);
279 /* estimate using the bitrate */
281 gst_util_uint64_scale (length, 8 * GST_SECOND, bitrate);
284 "length: %" G_GINT64_FORMAT ", bitrate %d, total_time %"
285 GST_TIME_FORMAT, length, bitrate, GST_TIME_ARGS (total_time));
287 gst_query_unref (uquery);
291 gst_query_set_duration (query, GST_FORMAT_TIME, total_time);
294 case GST_QUERY_SEEKING:
298 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
299 if (format == GST_FORMAT_TIME) {
300 gboolean seekable = FALSE;
305 stop = ogg->total_time;
306 } else if (ogg->current_chain->streams->len) {
310 for (i = 0; i < ogg->current_chain->streams->len; i++) {
312 g_array_index (ogg->current_chain->streams, GstOggPad *, i);
315 if (pad->map.index != NULL && pad->map.n_index != 0) {
317 GstClockTime idx_time;
319 idx = &pad->map.index[pad->map.n_index - 1];
321 gst_util_uint64_scale (idx->timestamp, GST_SECOND,
326 stop = MAX (idx_time, stop);
328 stop = -1; /* we've no clue, sadly, without seeking */
333 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0, stop);
341 res = gst_pad_query_default (pad, parent, query);
351 GST_DEBUG_OBJECT (ogg, "only query duration on TIME is supported");
358 gst_ogg_demux_receive_event (GstElement * element, GstEvent * event)
363 ogg = GST_OGG_DEMUX (element);
365 switch (GST_EVENT_TYPE (event)) {
367 /* now do the seek */
368 res = gst_ogg_demux_perform_seek (ogg, event);
369 gst_event_unref (event);
372 GST_DEBUG_OBJECT (ogg, "We only handle seek events here");
381 GST_DEBUG_OBJECT (ogg, "error handling event");
382 gst_event_unref (event);
388 gst_ogg_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
393 ogg = GST_OGG_DEMUX (parent);
395 switch (GST_EVENT_TYPE (event)) {
397 /* now do the seek */
398 res = gst_ogg_demux_perform_seek (ogg, event);
399 gst_event_unref (event);
402 res = gst_pad_event_default (pad, parent, event);
410 gst_ogg_pad_reset (GstOggPad * pad)
412 ogg_stream_reset (&pad->map.stream);
414 GST_DEBUG_OBJECT (pad, "doing reset");
416 /* clear continued pages */
417 g_list_foreach (pad->continued, (GFunc) gst_ogg_page_free, NULL);
418 g_list_free (pad->continued);
419 pad->continued = NULL;
421 pad->last_ret = GST_FLOW_OK;
422 pad->position = GST_CLOCK_TIME_NONE;
423 pad->current_granule = -1;
424 pad->keyframe_granule = -1;
428 /* queue data, basically takes the packet, puts it in a buffer and store the
429 * buffer in the queued list. */
431 gst_ogg_demux_queue_data (GstOggPad * pad, ogg_packet * packet)
433 #ifndef GST_DISABLE_GST_DEBUG
434 GstOggDemux *ogg = pad->ogg;
437 GST_DEBUG_OBJECT (ogg, "%p queueing data serial %08x",
438 pad, pad->map.serialno);
440 pad->map.queued = g_list_append (pad->map.queued, _ogg_packet_copy (packet));
447 gst_ogg_demux_chain_peer (GstOggPad * pad, ogg_packet * packet,
448 gboolean push_headers)
450 GstBuffer *buf = NULL;
451 GstFlowReturn ret, cret;
452 GstOggDemux *ogg = pad->ogg;
458 GstClockTime out_timestamp, out_duration;
459 guint64 out_offset, out_offset_end;
460 gboolean delta_unit = FALSE;
465 if (!ogg->pullmode && ogg->push_state == PUSH_PLAYING
466 && ogg->push_time_length == GST_CLOCK_TIME_NONE
467 && !ogg->push_disable_seeking) {
468 if (!ogg->building_chain) {
469 /* we got all headers, now try to get duration */
470 if (!gst_ogg_demux_check_duration_push (ogg, GST_SEEK_FLAG_FLUSH, NULL)) {
471 GST_PUSH_UNLOCK (ogg);
475 GST_PUSH_UNLOCK (ogg);
478 GST_PUSH_UNLOCK (ogg);
480 GST_DEBUG_OBJECT (ogg,
481 "%p streaming to peer serial %08x", pad, pad->map.serialno);
483 if (pad->map.is_ogm) {
487 data = packet->packet;
488 bytes = packet->bytes;
493 if ((data[0] & 1) || (data[0] & 3 && pad->map.is_ogm_text)) {
494 /* We don't push header packets for OGM */
498 offset = 1 + (((data[0] & 0xc0) >> 6) | ((data[0] & 0x02) << 1));
499 delta_unit = (((data[0] & 0x08) >> 3) == 0);
503 /* Strip trailing \0 for subtitles */
504 if (pad->map.is_ogm_text) {
505 while (bytes && data[bytes - 1] == 0) {
510 } else if (pad->map.is_vp8) {
511 if ((packet->bytes >= 7 && memcmp (packet->packet, "OVP80\2 ", 7) == 0) ||
513 (packet->bytes >= 5 && memcmp (packet->packet, "OVP80", 5) == 0)) {
514 /* We don't push header packets for VP8 */
524 /* get timing info for the packet */
525 if (gst_ogg_stream_packet_is_header (&pad->map, packet)) {
527 GST_DEBUG_OBJECT (ogg, "packet is header");
529 duration = gst_ogg_stream_get_packet_duration (&pad->map, packet);
530 GST_DEBUG_OBJECT (ogg, "packet duration %" G_GUINT64_FORMAT, duration);
534 out_timestamp = GST_CLOCK_TIME_NONE;
535 out_duration = GST_CLOCK_TIME_NONE;
539 if (packet->granulepos != -1) {
540 pad->current_granule = gst_ogg_stream_granulepos_to_granule (&pad->map,
542 pad->keyframe_granule =
543 gst_ogg_stream_granulepos_to_key_granule (&pad->map,
545 GST_DEBUG_OBJECT (ogg, "new granule %" G_GUINT64_FORMAT,
546 pad->current_granule);
547 } else if (ogg->segment.rate > 0.0 && pad->current_granule != -1) {
548 pad->current_granule += duration;
549 GST_DEBUG_OBJECT (ogg, "interpollating granule %" G_GUINT64_FORMAT,
550 pad->current_granule);
552 if (ogg->segment.rate < 0.0 && packet->granulepos == -1) {
553 /* negative rates, only set timestamp on the packets with a granulepos */
559 /* we only push buffers after we have a valid granule. This is done so that
560 * we nicely skip packets without a timestamp after a seek. This is ok
561 * because we base or seek on the packet after the page with the smaller
563 if (pad->current_granule == -1)
566 if (pad->map.is_ogm) {
567 out_timestamp = gst_ogg_stream_granule_to_time (&pad->map,
568 pad->current_granule);
569 out_duration = gst_util_uint64_scale (duration,
570 GST_SECOND * pad->map.granulerate_d, pad->map.granulerate_n);
571 } else if (pad->map.is_sparse) {
572 out_timestamp = gst_ogg_stream_granule_to_time (&pad->map,
573 pad->current_granule);
574 if (duration == GST_CLOCK_TIME_NONE) {
575 out_duration = GST_CLOCK_TIME_NONE;
577 out_duration = gst_util_uint64_scale (duration,
578 GST_SECOND * pad->map.granulerate_d, pad->map.granulerate_n);
581 out_timestamp = gst_ogg_stream_granule_to_time (&pad->map,
582 pad->current_granule - duration);
584 gst_ogg_stream_granule_to_time (&pad->map,
585 pad->current_granule) - out_timestamp;
588 gst_ogg_stream_granule_to_granulepos (&pad->map,
589 pad->current_granule, pad->keyframe_granule);
591 gst_ogg_stream_granule_to_time (&pad->map, pad->current_granule);
595 if (pad->map.is_ogm_text) {
596 /* check for invalid buffer sizes */
597 if (G_UNLIKELY (offset + trim >= packet->bytes))
604 buf = gst_buffer_new_and_alloc (packet->bytes - offset - trim);
606 /* set delta flag for OGM content */
608 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
610 /* copy packet in buffer */
611 gst_buffer_fill (buf, 0, packet->packet + offset,
612 packet->bytes - offset - trim);
614 GST_BUFFER_TIMESTAMP (buf) = out_timestamp;
615 GST_BUFFER_DURATION (buf) = out_duration;
616 GST_BUFFER_OFFSET (buf) = out_offset;
617 GST_BUFFER_OFFSET_END (buf) = out_offset_end;
619 /* Mark discont on the buffer */
621 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
622 pad->discont = FALSE;
625 pad->position = ogg->segment.position;
627 /* don't push the header packets when we are asked to skip them */
628 if (!packet->b_o_s || push_headers) {
629 ret = gst_pad_push (GST_PAD_CAST (pad), buf);
633 cret = gst_ogg_demux_combine_flows (ogg, pad, ret);
636 /* we're done with skeleton stuff */
637 if (pad->map.is_skeleton)
640 /* check if valid granulepos, then we can calculate the current
641 * position. We know the granule for each packet but we only want to update
642 * the position when we have a valid granulepos on the packet because else
643 * our time jumps around for the different streams. */
644 if (packet->granulepos < 0)
647 /* convert to time */
648 current_time = gst_ogg_stream_get_end_time_for_granulepos (&pad->map,
651 /* convert to stream time */
652 if ((chain = pad->chain)) {
653 gint64 chain_start = 0;
655 if (chain->segment_start != GST_CLOCK_TIME_NONE)
656 chain_start = chain->segment_start;
658 current_time = current_time - chain_start + chain->begin_time;
661 /* and store as the current position */
662 ogg->segment.position = current_time;
664 GST_DEBUG_OBJECT (ogg, "ogg current time %" GST_TIME_FORMAT,
665 GST_TIME_ARGS (current_time));
667 /* check stream eos */
668 if ((ogg->segment.rate > 0.0 && ogg->segment.stop != GST_CLOCK_TIME_NONE &&
669 current_time > ogg->segment.stop) ||
670 (ogg->segment.rate < 0.0 && ogg->segment.start != GST_CLOCK_TIME_NONE &&
671 current_time < ogg->segment.start)) {
672 GST_DEBUG_OBJECT (ogg, "marking pad %p EOS", pad);
678 gst_buffer_unref (buf);
679 /* return combined flow result */
685 GST_DEBUG_OBJECT (ogg, "Skipping empty packet");
691 GST_DEBUG_OBJECT (ogg, "skipping packet: no valid granule found yet");
696 GST_DEBUG_OBJECT (ogg, "pad not added yet");
702 gst_ogg_demux_collect_start_time (GstOggDemux * ogg, GstOggChain * chain)
705 guint64 start_time = G_MAXUINT64;
707 for (i = 0; i < chain->streams->len; i++) {
708 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
710 if (pad->map.is_skeleton)
713 /* can do this if the pad start time is not defined */
714 GST_DEBUG_OBJECT (ogg, "Pad %08x (%s) start time is %" GST_TIME_FORMAT,
715 pad->map.serialno, gst_ogg_stream_get_media_type (&pad->map),
716 GST_TIME_ARGS (pad->start_time));
717 if (pad->start_time == GST_CLOCK_TIME_NONE) {
718 if (!pad->map.is_sparse) {
719 start_time = G_MAXUINT64;
723 start_time = MIN (start_time, pad->start_time);
730 gst_ogg_demux_collect_sync_time (GstOggDemux * ogg, GstOggChain * chain)
733 GstClockTime sync_time = GST_CLOCK_TIME_NONE;
736 GST_WARNING_OBJECT (ogg, "No chain!");
737 return GST_CLOCK_TIME_NONE;
740 for (i = 0; i < chain->streams->len; i++) {
741 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
743 if (pad->map.is_sparse)
746 if (pad->push_sync_time == GST_CLOCK_TIME_NONE) {
747 sync_time = GST_CLOCK_TIME_NONE;
750 if (sync_time == GST_CLOCK_TIME_NONE)
751 sync_time = pad->push_sync_time;
753 sync_time = MAX (sync_time, pad->push_sync_time);
759 /* submit a packet to the oggpad, this function will run the
760 * typefind code for the pad if this is the first packet for this
764 gst_ogg_pad_submit_packet (GstOggPad * pad, ogg_packet * packet)
767 GstFlowReturn ret = GST_FLOW_OK;
769 GstOggDemux *ogg = pad->ogg;
771 GST_DEBUG_OBJECT (ogg, "%p submit packet serial %08x",
772 pad, pad->map.serialno);
774 if (!pad->have_type) {
775 pad->have_type = gst_ogg_stream_setup_map (&pad->map, packet);
776 if (!pad->have_type) {
777 pad->map.caps = gst_caps_new_empty_simple ("application/x-unknown");
779 if (pad->map.is_skeleton) {
780 GST_DEBUG_OBJECT (ogg, "we have a fishead");
781 /* copy values over to global ogg level */
782 ogg->basetime = pad->map.basetime;
783 ogg->prestime = pad->map.prestime;
785 /* use total time to update the total ogg time */
786 if (ogg->total_time == -1) {
787 ogg->total_time = pad->map.total_time;
788 } else if (pad->map.total_time > 0) {
789 ogg->total_time = MAX (ogg->total_time, pad->map.total_time);
793 gst_pad_set_caps (GST_PAD (pad), pad->map.caps);
795 GST_WARNING_OBJECT (ogg, "stream parser didn't create src pad caps");
799 if (pad->map.is_skeleton) {
804 /* try to parse the serialno first */
805 if (gst_ogg_map_parse_fisbone (&pad->map, packet->packet, packet->bytes,
808 GST_DEBUG_OBJECT (pad->ogg,
809 "got skeleton packet for stream 0x%08x", serialno);
811 skel_pad = gst_ogg_chain_get_stream (pad->chain, serialno);
814 case GST_OGG_SKELETON_FISBONE:
815 /* parse the remainder of the fisbone in the pad with the serialno,
816 * note that we ignore the start_time as this is usually wrong for
818 gst_ogg_map_add_fisbone (&skel_pad->map, &pad->map, packet->packet,
819 packet->bytes, NULL);
821 case GST_OGG_SKELETON_INDEX:
822 gst_ogg_map_add_index (&skel_pad->map, &pad->map, packet->packet,
825 /* use total time to update the total ogg time */
826 if (ogg->total_time == -1) {
827 ogg->total_time = skel_pad->map.total_time;
828 } else if (skel_pad->map.total_time > 0) {
829 ogg->total_time = MAX (ogg->total_time, skel_pad->map.total_time);
837 GST_WARNING_OBJECT (pad->ogg,
838 "found skeleton fisbone for an unknown stream 0x%08x", serialno);
843 granule = gst_ogg_stream_granulepos_to_granule (&pad->map,
846 GST_DEBUG_OBJECT (ogg, "%p has granulepos %" G_GINT64_FORMAT, pad, granule);
847 pad->current_granule = granule;
850 /* restart header packet count when seeing a b_o_s page;
851 * particularly useful following a seek or even following chain finding */
853 GST_DEBUG_OBJECT (ogg, "b_o_s packet, resetting header packet count");
854 pad->map.n_header_packets_seen = 0;
855 if (!pad->map.have_headers) {
856 GST_DEBUG_OBJECT (ogg, "clearing header packets");
857 g_list_foreach (pad->map.headers, (GFunc) _ogg_packet_free, NULL);
858 g_list_free (pad->map.headers);
859 pad->map.headers = NULL;
863 /* Overload the value of b_o_s in ogg_packet with a flag whether or
864 * not this is a header packet. Maybe some day this could be cleaned
866 packet->b_o_s = gst_ogg_stream_packet_is_header (&pad->map, packet);
867 if (!packet->b_o_s) {
868 GST_DEBUG ("found non-header packet");
869 pad->map.have_headers = TRUE;
870 if (pad->start_time == GST_CLOCK_TIME_NONE) {
871 gint64 duration = gst_ogg_stream_get_packet_duration (&pad->map, packet);
872 GST_DEBUG ("duration %" G_GINT64_FORMAT, duration);
873 if (duration != -1) {
874 pad->map.accumulated_granule += duration;
875 GST_DEBUG ("accumulated granule %" G_GINT64_FORMAT,
876 pad->map.accumulated_granule);
879 if (packet->granulepos != -1) {
880 ogg_int64_t start_granule;
883 granule = gst_ogg_stream_granulepos_to_granule (&pad->map,
886 if (granule > pad->map.accumulated_granule)
887 start_granule = granule - pad->map.accumulated_granule;
891 pad->start_time = gst_ogg_stream_granule_to_time (&pad->map,
893 GST_DEBUG_OBJECT (ogg,
894 "start time %" GST_TIME_FORMAT " (%" GST_TIME_FORMAT ") for %s",
895 GST_TIME_ARGS (pad->start_time), GST_TIME_ARGS (pad->start_time),
896 gst_ogg_stream_get_media_type (&pad->map));
898 packet->granulepos = gst_ogg_stream_granule_to_granulepos (&pad->map,
899 pad->map.accumulated_granule, pad->keyframe_granule);
903 /* look for tags in header packet (before inc header count) */
904 gst_ogg_stream_extract_tags (&pad->map, packet);
905 pad->map.n_header_packets_seen++;
906 if (!pad->map.have_headers) {
908 g_list_append (pad->map.headers, _ogg_packet_copy (packet));
909 GST_DEBUG ("keeping header packet %d", pad->map.n_header_packets_seen);
913 /* we know the start_time of the pad data, see if we
914 * can activate the complete chain if this is a dynamic
915 * chain. We need all the headers too for this. */
916 if (pad->start_time != GST_CLOCK_TIME_NONE && pad->map.have_headers) {
917 GstOggChain *chain = pad->chain;
919 /* check if complete chain has start time */
920 if (chain == ogg->building_chain) {
921 GstEvent *event = NULL;
926 GST_DEBUG_OBJECT (ogg, "need to resync");
928 /* when we need to resync after a seek, we wait until we have received
929 * timestamps on all streams */
930 start_time = gst_ogg_demux_collect_start_time (ogg, chain);
932 if (start_time != G_MAXUINT64) {
936 GST_DEBUG_OBJECT (ogg, "start_time: %" GST_TIME_FORMAT,
937 GST_TIME_ARGS (start_time));
939 if (chain->segment_start < start_time)
941 (start_time - chain->segment_start) + chain->begin_time;
943 segment_time = chain->begin_time;
945 /* create the newsegment event we are going to send out */
946 gst_segment_init (&segment, GST_FORMAT_TIME);
949 if (!ogg->pullmode && ogg->push_state == PUSH_LINEAR2) {
950 /* if we are fast forwarding to the actual seek target,
951 ensure previous frames are clipped */
952 GST_DEBUG_OBJECT (ogg,
953 "Resynced, starting segment at %" GST_TIME_FORMAT
954 ", start_time %" GST_TIME_FORMAT,
955 GST_TIME_ARGS (ogg->push_seek_time_original_target),
956 GST_TIME_ARGS (start_time));
957 segment.rate = ogg->push_seek_rate;
958 segment.start = ogg->push_seek_time_original_target;
960 segment.time = ogg->push_seek_time_original_target;
961 event = gst_event_new_segment (&segment);
962 ogg->push_state = PUSH_PLAYING;
964 segment.rate = ogg->segment.rate;
965 segment.applied_rate = ogg->segment.applied_rate;
966 segment.start = start_time;
967 segment.stop = chain->segment_stop;
968 segment.time = segment_time;
969 event = gst_event_new_segment (&segment);
971 GST_PUSH_UNLOCK (ogg);
976 /* see if we have enough info to activate the chain, we have enough info
977 * when all streams have a valid start time. */
978 if (gst_ogg_demux_collect_chain_info (ogg, chain)) {
981 GST_DEBUG_OBJECT (ogg, "segment_start: %" GST_TIME_FORMAT,
982 GST_TIME_ARGS (chain->segment_start));
983 GST_DEBUG_OBJECT (ogg, "segment_stop: %" GST_TIME_FORMAT,
984 GST_TIME_ARGS (chain->segment_stop));
985 GST_DEBUG_OBJECT (ogg, "segment_time: %" GST_TIME_FORMAT,
986 GST_TIME_ARGS (chain->begin_time));
988 /* create the newsegment event we are going to send out */
989 gst_segment_init (&segment, GST_FORMAT_TIME);
990 segment.rate = ogg->segment.rate;
991 segment.applied_rate = ogg->segment.applied_rate;
992 segment.start = chain->segment_start;
993 segment.stop = chain->segment_stop;
994 segment.time = chain->begin_time;
995 event = gst_event_new_segment (&segment);
1000 gst_event_set_seqnum (event, ogg->seqnum);
1002 gst_ogg_demux_activate_chain (ogg, chain, event);
1004 ogg->building_chain = NULL;
1009 /* if we are building a chain, store buffer for when we activate
1010 * it. This path is taken if we operate in streaming mode. */
1011 if (ogg->building_chain) {
1012 /* bos packets where stored in the header list so we can discard
1015 ret = gst_ogg_demux_queue_data (pad, packet);
1017 /* else we are completely streaming to the peer */
1019 ret = gst_ogg_demux_chain_peer (pad, packet, !ogg->pullmode);
1024 /* flush at most @npackets from the stream layer. All packets if
1027 static GstFlowReturn
1028 gst_ogg_pad_stream_out (GstOggPad * pad, gint npackets)
1030 GstFlowReturn result = GST_FLOW_OK;
1031 gboolean done = FALSE;
1040 ret = ogg_stream_packetout (&pad->map.stream, &packet);
1043 GST_LOG_OBJECT (ogg, "packetout done");
1047 GST_LOG_OBJECT (ogg, "packetout discont");
1048 if (!pad->map.is_sparse) {
1049 gst_ogg_chain_mark_discont (pad->chain);
1051 gst_ogg_pad_mark_discont (pad);
1055 GST_LOG_OBJECT (ogg, "packetout gave packet of size %ld", packet.bytes);
1056 result = gst_ogg_pad_submit_packet (pad, &packet);
1057 /* not linked is not a problem, it's possible that we are still
1058 * collecting headers and that we don't have exposed the pads yet */
1059 if (result == GST_FLOW_NOT_LINKED)
1061 else if (result <= GST_FLOW_EOS)
1062 goto could_not_submit;
1065 GST_WARNING_OBJECT (ogg,
1066 "invalid return value %d for ogg_stream_packetout, resetting stream",
1068 gst_ogg_pad_reset (pad);
1073 done = (npackets == 0);
1081 GST_WARNING_OBJECT (ogg,
1082 "could not submit packet for stream %08x, "
1083 "error: %d", pad->map.serialno, result);
1084 gst_ogg_pad_reset (pad);
1090 gst_ogg_demux_setup_bisection_bounds (GstOggDemux * ogg)
1092 if (ogg->push_last_seek_time >= ogg->push_seek_time_target) {
1093 GST_DEBUG_OBJECT (ogg, "We overshot by %" GST_TIME_FORMAT,
1094 GST_TIME_ARGS (ogg->push_last_seek_time - ogg->push_seek_time_target));
1095 ogg->push_offset1 = ogg->push_last_seek_offset;
1096 ogg->push_time1 = ogg->push_last_seek_time;
1098 GST_DEBUG_OBJECT (ogg, "We undershot by %" GST_TIME_FORMAT,
1099 GST_TIME_ARGS (ogg->push_seek_time_target - ogg->push_last_seek_time));
1100 ogg->push_offset0 = ogg->push_last_seek_offset;
1101 ogg->push_time0 = ogg->push_last_seek_time;
1106 gst_ogg_demux_estimate_bisection_target (GstOggDemux * ogg)
1109 gint64 segment_bitrate;
1111 /* we might not know the length of the stream in time,
1112 so push_time1 might not be set */
1113 GST_DEBUG_OBJECT (ogg,
1114 "push time 1: %" GST_TIME_FORMAT ", dbytes %" G_GINT64_FORMAT,
1115 GST_TIME_ARGS (ogg->push_time1), ogg->push_offset1 - ogg->push_offset0);
1116 if (ogg->push_time1 == GST_CLOCK_TIME_NONE) {
1117 GST_DEBUG_OBJECT (ogg,
1118 "New segment to consider: bytes %" G_GINT64_FORMAT " %" G_GINT64_FORMAT
1119 ", time %" GST_TIME_FORMAT " (open ended)", ogg->push_offset0,
1120 ogg->push_offset1, GST_TIME_ARGS (ogg->push_time0));
1121 if (ogg->push_last_seek_time == ogg->push_start_time) {
1122 /* if we're at start and don't know the end time, we can't estimate
1123 bitrate, so get the nominal declared bitrate as a failsafe, or some
1124 random constant which will be discarded after we made a (probably
1125 dire) first guess */
1126 segment_bitrate = (ogg->bitrate > 0 ? ogg->bitrate : 1000);
1129 gst_util_uint64_scale (ogg->push_last_seek_offset - 0,
1130 8 * GST_SECOND, ogg->push_last_seek_time - ogg->push_start_time);
1134 gst_util_uint64_scale (ogg->push_seek_time_target - ogg->push_time0,
1135 segment_bitrate, 8 * GST_SECOND);
1137 GST_DEBUG_OBJECT (ogg,
1138 "New segment to consider: bytes %" G_GINT64_FORMAT " %" G_GINT64_FORMAT
1139 ", time %" GST_TIME_FORMAT " %" GST_TIME_FORMAT, ogg->push_offset0,
1140 ogg->push_offset1, GST_TIME_ARGS (ogg->push_time0),
1141 GST_TIME_ARGS (ogg->push_time1));
1142 if (ogg->push_time0 == ogg->push_time1) {
1143 best = ogg->push_offset0;
1146 gst_util_uint64_scale (ogg->push_offset1 - ogg->push_offset0,
1147 8 * GST_SECOND, ogg->push_time1 - ogg->push_time0);
1148 GST_DEBUG_OBJECT (ogg,
1149 "Local bitrate on the %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT
1150 " segment: %" G_GINT64_FORMAT, GST_TIME_ARGS (ogg->push_time0),
1151 GST_TIME_ARGS (ogg->push_time1), segment_bitrate);
1154 gst_util_uint64_scale (ogg->push_seek_time_target - ogg->push_time0,
1155 segment_bitrate, 8 * GST_SECOND);
1159 /* offset by typical page size */
1161 if (best < ogg->push_offset0)
1162 best = ogg->push_offset0;
1170 gst_ogg_demux_record_keyframe_time (GstOggDemux * ogg, GstOggPad * pad,
1171 ogg_int64_t granpos)
1174 GstClockTime kf_time;
1176 kf_granule = gst_ogg_stream_granulepos_to_key_granule (&pad->map, granpos);
1177 kf_time = gst_ogg_stream_granule_to_time (&pad->map, kf_granule);
1179 pad->push_kf_time = kf_time;
1182 /* returns the earliest keyframe time for all non sparse pads in the chain,
1183 * if known, and GST_CLOCK_TIME_NONE if not */
1185 gst_ogg_demux_get_earliest_keyframe_time (GstOggDemux * ogg)
1187 GstClockTime t = GST_CLOCK_TIME_NONE;
1188 GstOggChain *chain = ogg->building_chain;
1192 GST_WARNING_OBJECT (ogg, "No chain!");
1193 return GST_CLOCK_TIME_NONE;
1195 for (i = 0; i < chain->streams->len; i++) {
1196 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
1198 if (pad->map.is_sparse)
1200 if (pad->push_kf_time == GST_CLOCK_TIME_NONE)
1201 return GST_CLOCK_TIME_NONE;
1202 if (t == GST_CLOCK_TIME_NONE || pad->push_kf_time < t)
1203 t = pad->push_kf_time;
1209 /* MUST be called with the push lock locked, and will unlock it
1210 regardless of return value. */
1211 static GstFlowReturn
1212 gst_ogg_demux_seek_back_after_push_duration_check_unlock (GstOggDemux * ogg)
1216 /* Get the delayed event, if any */
1217 event = ogg->push_mode_seek_delayed_event;
1218 ogg->push_mode_seek_delayed_event = NULL;
1220 ogg->push_state = PUSH_PLAYING;
1222 GST_PUSH_UNLOCK (ogg);
1225 /* If there is one, perform it */
1226 gst_ogg_demux_perform_seek_push (ogg, event);
1228 /* If there wasn't, seek back at start to start normal playback */
1229 GST_INFO_OBJECT (ogg, "Seeking back to 0 after duration check");
1230 event = gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1231 GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_FLUSH,
1232 GST_SEEK_TYPE_SET, 1, GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE);
1233 if (!gst_pad_push_event (ogg->sinkpad, event)) {
1234 GST_WARNING_OBJECT (ogg, "Failed seeking back to start");
1235 return GST_FLOW_ERROR;
1243 gst_ogg_pad_handle_push_mode_state (GstOggPad * pad, ogg_page * page)
1245 GstOggDemux *ogg = pad->ogg;
1246 ogg_int64_t granpos = ogg_page_granulepos (page);
1248 GST_PUSH_LOCK (ogg);
1250 if (ogg->push_start_time == GST_CLOCK_TIME_NONE) {
1251 ogg->push_start_time =
1252 gst_ogg_stream_get_start_time_for_granulepos (&pad->map, granpos);
1253 GST_DEBUG_OBJECT (ogg, "Stream start time: %" GST_TIME_FORMAT,
1254 GST_TIME_ARGS (ogg->push_start_time));
1256 ogg->push_time_offset =
1257 gst_ogg_stream_get_end_time_for_granulepos (&pad->map, granpos);
1258 if (ogg->push_time_offset > 0) {
1259 GST_DEBUG_OBJECT (ogg, "Bitrate since start: %" G_GUINT64_FORMAT,
1260 gst_util_uint64_scale (ogg->push_byte_offset, 8 * GST_SECOND,
1261 ogg->push_time_offset));
1264 if (ogg->push_state == PUSH_DURATION) {
1266 gst_ogg_stream_get_end_time_for_granulepos (&pad->map, granpos);
1268 if (ogg->total_time == GST_CLOCK_TIME_NONE || t > ogg->total_time) {
1269 GST_DEBUG_OBJECT (ogg, "New total time: %" GST_TIME_FORMAT,
1271 ogg->total_time = t;
1272 ogg->push_time_length = t;
1275 /* If we were determining the duration of the stream, we're now done,
1276 and can get back to sending the original event we delayed.
1277 We stop a bit before the end of the stream, as if we get a EOS
1278 event and there is a queue2 upstream (such as when using playbin2),
1279 it will pause the task *after* we come back from the EOS handler,
1280 so we cannot prevent the pausing by issuing a seek. */
1281 if (ogg->push_byte_offset + EOS_AVOIDANCE_THRESHOLD >=
1282 ogg->push_byte_length) {
1283 GstMessage *message;
1286 /* tell the pipeline we've just found out the duration */
1287 ogg->push_time_length = ogg->total_time;
1288 GST_INFO_OBJECT (ogg, "New duration found: %" GST_TIME_FORMAT,
1289 GST_TIME_ARGS (ogg->total_time));
1291 gst_message_new_duration (GST_OBJECT (ogg), GST_FORMAT_TIME,
1293 gst_element_post_message (GST_ELEMENT (ogg), message);
1295 GST_DEBUG_OBJECT (ogg,
1296 "We're close enough to the end, and we're scared "
1297 "to get too close, seeking back to start");
1299 res = gst_ogg_demux_seek_back_after_push_duration_check_unlock (ogg);
1300 if (res != GST_FLOW_OK)
1302 return GST_FLOW_SKIP_PUSH;
1304 GST_PUSH_UNLOCK (ogg);
1306 return GST_FLOW_SKIP_PUSH;
1310 /* if we're seeking, look at time, and decide what to do */
1311 if (ogg->push_state != PUSH_PLAYING && ogg->push_state != PUSH_LINEAR2) {
1316 gboolean close_enough;
1318 /* ignore -1 granpos when seeking, we want to sync on a real granpos */
1320 GST_PUSH_UNLOCK (ogg);
1321 if (ogg_stream_pagein (&pad->map.stream, page) != 0)
1323 return GST_FLOW_SKIP_PUSH;
1326 t = gst_ogg_stream_get_end_time_for_granulepos (&pad->map, granpos);
1328 if (ogg->push_state == PUSH_BISECT1 || ogg->push_state == PUSH_BISECT2) {
1329 GstClockTime sync_time;
1331 if (pad->push_sync_time == GST_CLOCK_TIME_NONE)
1332 pad->push_sync_time = t;
1333 GST_DEBUG_OBJECT (ogg, "Got timestamp %" GST_TIME_FORMAT " for %s",
1334 GST_TIME_ARGS (t), gst_ogg_stream_get_media_type (&pad->map));
1335 sync_time = gst_ogg_demux_collect_sync_time (ogg, ogg->building_chain);
1336 if (sync_time == GST_CLOCK_TIME_NONE) {
1337 GST_PUSH_UNLOCK (ogg);
1338 GST_DEBUG_OBJECT (ogg,
1339 "Not enough timing info collected for sync, waiting for more");
1340 if (ogg_stream_pagein (&pad->map.stream, page) != 0)
1342 return GST_FLOW_SKIP_PUSH;
1344 ogg->push_last_seek_time = sync_time;
1346 GST_DEBUG_OBJECT (ogg,
1347 "Bisection just seeked at %" G_GINT64_FORMAT ", time %"
1348 GST_TIME_FORMAT ", target was %" GST_TIME_FORMAT,
1349 ogg->push_last_seek_offset,
1350 GST_TIME_ARGS (ogg->push_last_seek_time),
1351 GST_TIME_ARGS (ogg->push_seek_time_target));
1353 if (ogg->push_time1 != GST_CLOCK_TIME_NONE) {
1354 GST_DEBUG_OBJECT (ogg,
1355 "Interval was %" G_GINT64_FORMAT " - %" G_GINT64_FORMAT " (%"
1356 G_GINT64_FORMAT "), time %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT
1357 " (%" GST_TIME_FORMAT ")", ogg->push_offset0, ogg->push_offset1,
1358 ogg->push_offset1 - ogg->push_offset0,
1359 GST_TIME_ARGS (ogg->push_time0), GST_TIME_ARGS (ogg->push_time1),
1360 GST_TIME_ARGS (ogg->push_time1 - ogg->push_time0));
1362 GST_DEBUG_OBJECT (ogg,
1363 "Interval was %" G_GINT64_FORMAT " - %" G_GINT64_FORMAT " (%"
1364 G_GINT64_FORMAT "), time %" GST_TIME_FORMAT " - unknown",
1365 ogg->push_offset0, ogg->push_offset1,
1366 ogg->push_offset1 - ogg->push_offset0,
1367 GST_TIME_ARGS (ogg->push_time0));
1370 gst_ogg_demux_setup_bisection_bounds (ogg);
1372 best = gst_ogg_demux_estimate_bisection_target (ogg);
1374 if (ogg->push_seek_time_target == 0) {
1375 GST_DEBUG_OBJECT (ogg, "Seeking to 0, deemed close enough");
1376 close_enough = (ogg->push_last_seek_time == 0);
1378 /* TODO: make this dependent on framerate ? */
1379 GstClockTime threshold = GST_SECOND / 2;
1381 /* We want to be within half a second before the target */
1382 if (threshold > ogg->push_seek_time_target)
1383 threshold = ogg->push_seek_time_target;
1384 close_enough = ogg->push_last_seek_time < ogg->push_seek_time_target
1385 && ogg->push_last_seek_time >=
1386 ogg->push_seek_time_target - threshold;
1387 GST_DEBUG_OBJECT (ogg,
1388 "testing if we're close enough: %" GST_TIME_FORMAT " <= %"
1389 GST_TIME_FORMAT " < %" GST_TIME_FORMAT " ? %s",
1390 GST_TIME_ARGS (ogg->push_seek_time_target - threshold),
1391 GST_TIME_ARGS (ogg->push_last_seek_time),
1392 GST_TIME_ARGS (ogg->push_seek_time_target),
1393 close_enough ? "Yes" : "No");
1396 if (close_enough || best == ogg->push_last_seek_offset) {
1397 if (ogg->push_state == PUSH_BISECT1) {
1398 /* we now know the time segment we'll have to search for
1399 the second bisection */
1400 ogg->push_time0 = ogg->push_start_time;
1401 ogg->push_offset0 = 0;
1403 GST_DEBUG_OBJECT (ogg,
1404 "Seek to %" GST_TIME_FORMAT
1405 " (%lx) done, now gathering pages for all non-sparse streams",
1406 GST_TIME_ARGS (ogg->push_seek_time_target), (long) granpos);
1407 ogg->push_state = PUSH_LINEAR1;
1409 /* If we're asked for an accurate seek, we'll go forward till
1410 we get to the original seek target time, else we'll just drop
1411 here at the keyframe */
1412 if (ogg->push_seek_flags & GST_SEEK_FLAG_ACCURATE) {
1413 GST_INFO_OBJECT (ogg,
1414 "Seek to keyframe at %" GST_TIME_FORMAT " done (we're at %"
1415 GST_TIME_FORMAT "), skipping to original target (%"
1416 GST_TIME_FORMAT ")",
1417 GST_TIME_ARGS (ogg->push_seek_time_target),
1418 GST_TIME_ARGS (sync_time),
1419 GST_TIME_ARGS (ogg->push_seek_time_original_target));
1420 ogg->push_state = PUSH_LINEAR2;
1422 GST_DEBUG_OBJECT (ogg, "Seek to keyframe done, playing");
1424 /* we're synced to the seek target, so flush stream and stuff
1425 any queued pages into the stream so we start decoding there */
1426 ogg->push_state = PUSH_PLAYING;
1428 GST_INFO_OBJECT (ogg, "Bisection needed %d + %d steps",
1429 ogg->push_bisection_steps[0], ogg->push_bisection_steps[1]);
1432 } else if (ogg->push_state == PUSH_LINEAR1) {
1433 if (pad->push_kf_time == GST_CLOCK_TIME_NONE) {
1434 GstClockTime earliest_keyframe_time;
1436 gst_ogg_demux_record_keyframe_time (ogg, pad, granpos);
1437 GST_DEBUG_OBJECT (ogg,
1438 "Previous keyframe for %s stream at %" GST_TIME_FORMAT,
1439 gst_ogg_stream_get_media_type (&pad->map),
1440 GST_TIME_ARGS (pad->push_kf_time));
1441 earliest_keyframe_time = gst_ogg_demux_get_earliest_keyframe_time (ogg);
1442 if (earliest_keyframe_time != GST_CLOCK_TIME_NONE) {
1443 GST_DEBUG_OBJECT (ogg,
1444 "All non sparse streams now have a previous keyframe time,"
1445 "bisecting again to %" GST_TIME_FORMAT,
1446 GST_TIME_ARGS (earliest_keyframe_time));
1447 ogg->push_seek_time_target = earliest_keyframe_time;
1449 ogg->push_state = PUSH_BISECT2;
1450 best = gst_ogg_demux_estimate_bisection_target (ogg);
1455 if (ogg->push_state == PUSH_BISECT1 || ogg->push_state == PUSH_BISECT2) {
1458 ogg_sync_reset (&ogg->sync);
1459 for (i = 0; i < ogg->building_chain->streams->len; i++) {
1461 g_array_index (ogg->building_chain->streams, GstOggPad *, i);
1463 pad->push_sync_time = GST_CLOCK_TIME_NONE;
1464 ogg_stream_reset (&pad->map.stream);
1467 GST_DEBUG_OBJECT (ogg,
1468 "seeking to %" G_GINT64_FORMAT " - %" G_GINT64_FORMAT, best,
1471 g_assert (best != -1);
1472 ogg->push_bisection_steps[ogg->push_state == PUSH_BISECT2 ? 1 : 0]++;
1474 gst_event_new_seek (ogg->push_seek_rate, GST_FORMAT_BYTES,
1475 ogg->push_seek_flags, GST_SEEK_TYPE_SET, best,
1476 GST_SEEK_TYPE_NONE, -1);
1478 GST_PUSH_UNLOCK (ogg);
1479 res = gst_pad_push_event (ogg->sinkpad, sevent);
1481 /* We failed to send the seek event, notify the pipeline */
1482 GST_ELEMENT_ERROR (ogg, RESOURCE, SEEK, (NULL), ("Failed to seek"));
1483 return GST_FLOW_ERROR;
1485 return GST_FLOW_SKIP_PUSH;
1488 if (ogg->push_state != PUSH_PLAYING) {
1489 GST_PUSH_UNLOCK (ogg);
1490 return GST_FLOW_SKIP_PUSH;
1493 GST_PUSH_UNLOCK (ogg);
1499 GST_WARNING_OBJECT (ogg,
1500 "ogg stream choked on page (serial %08x), "
1501 "resetting stream", pad->map.serialno);
1502 gst_ogg_pad_reset (pad);
1503 /* we continue to recover */
1504 return GST_FLOW_SKIP_PUSH;
1508 /* submit a page to an oggpad, this function will then submit all
1509 * the packets in the page.
1511 static GstFlowReturn
1512 gst_ogg_pad_submit_page (GstOggPad * pad, ogg_page * page)
1514 GstFlowReturn result = GST_FLOW_OK;
1516 gboolean continued = FALSE;
1520 /* for negative rates we read pages backwards and must therefore be careful
1521 * with continued pages */
1522 if (ogg->segment.rate < 0.0) {
1525 continued = ogg_page_continued (page);
1527 /* number of completed packets in the page */
1528 npackets = ogg_page_packets (page);
1530 /* page is not continued so it contains at least one packet start. It's
1531 * possible that no packet ends on this page (npackets == 0). In that
1532 * case, the next (continued) page(s) we kept contain the remainder of the
1533 * packets. We mark npackets=1 to make us start decoding the pages in the
1534 * remainder of the algorithm. */
1538 GST_LOG_OBJECT (ogg, "continued: %d, %d packets", continued, npackets);
1540 if (npackets == 0) {
1541 GST_LOG_OBJECT (ogg, "no decodable packets, we need a previous page");
1546 /* keep track of time in push mode */
1547 if (!ogg->pullmode) {
1548 result = gst_ogg_pad_handle_push_mode_state (pad, page);
1549 if (result == GST_FLOW_SKIP_PUSH)
1551 if (result != GST_FLOW_OK)
1555 if (ogg_stream_pagein (&pad->map.stream, page) != 0)
1558 /* flush all packets in the stream layer, this might not give a packet if
1559 * the page had no packets finishing on the page (npackets == 0). */
1560 result = gst_ogg_pad_stream_out (pad, 0);
1562 if (pad->continued) {
1565 /* now send the continued pages to the stream layer */
1566 while (pad->continued) {
1567 ogg_page *p = (ogg_page *) pad->continued->data;
1569 GST_LOG_OBJECT (ogg, "submitting continued page %p", p);
1570 if (ogg_stream_pagein (&pad->map.stream, p) != 0)
1573 pad->continued = g_list_delete_link (pad->continued, pad->continued);
1576 gst_ogg_page_free (p);
1579 GST_LOG_OBJECT (ogg, "flushing last continued packet");
1580 /* flush 1 continued packet in the stream layer */
1581 result = gst_ogg_pad_stream_out (pad, 1);
1583 /* flush all remaining packets, we pushed them in the previous round.
1584 * We don't use _reset() because we still want to get the discont when
1585 * we submit a next page. */
1586 while (ogg_stream_packetout (&pad->map.stream, &packet) != 0);
1590 /* keep continued pages (only in reverse mode) */
1592 ogg_page *p = gst_ogg_page_copy (page);
1594 GST_LOG_OBJECT (ogg, "keeping continued page %p", p);
1595 pad->continued = g_list_prepend (pad->continued, p);
1602 GST_WARNING_OBJECT (ogg,
1603 "ogg stream choked on page (serial %08x), "
1604 "resetting stream", pad->map.serialno);
1605 gst_ogg_pad_reset (pad);
1606 /* we continue to recover */
1612 static GstOggChain *
1613 gst_ogg_chain_new (GstOggDemux * ogg)
1615 GstOggChain *chain = g_slice_new0 (GstOggChain);
1617 GST_DEBUG_OBJECT (ogg, "creating new chain %p", chain);
1621 chain->have_bos = FALSE;
1622 chain->streams = g_array_new (FALSE, TRUE, sizeof (GstOggPad *));
1623 chain->begin_time = GST_CLOCK_TIME_NONE;
1624 chain->segment_start = GST_CLOCK_TIME_NONE;
1625 chain->segment_stop = GST_CLOCK_TIME_NONE;
1626 chain->total_time = GST_CLOCK_TIME_NONE;
1632 gst_ogg_chain_free (GstOggChain * chain)
1636 for (i = 0; i < chain->streams->len; i++) {
1637 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
1639 gst_object_unref (pad);
1641 g_array_free (chain->streams, TRUE);
1642 g_slice_free (GstOggChain, chain);
1646 gst_ogg_pad_mark_discont (GstOggPad * pad)
1648 pad->discont = TRUE;
1649 pad->map.last_size = 0;
1653 gst_ogg_chain_mark_discont (GstOggChain * chain)
1657 for (i = 0; i < chain->streams->len; i++) {
1658 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
1660 gst_ogg_pad_mark_discont (pad);
1665 gst_ogg_chain_reset (GstOggChain * chain)
1669 for (i = 0; i < chain->streams->len; i++) {
1670 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
1672 gst_ogg_pad_reset (pad);
1677 gst_ogg_chain_new_stream (GstOggChain * chain, guint32 serialno)
1683 GST_DEBUG_OBJECT (chain->ogg,
1684 "creating new stream %08x in chain %p", serialno, chain);
1686 name = g_strdup_printf ("src_%08x", serialno);
1687 ret = g_object_new (GST_TYPE_OGG_PAD, "name", name, NULL);
1689 /* we own this one */
1690 gst_object_ref_sink (ret);
1692 GST_PAD_DIRECTION (ret) = GST_PAD_SRC;
1693 gst_ogg_pad_mark_discont (ret);
1696 ret->ogg = chain->ogg;
1698 ret->map.serialno = serialno;
1699 if (ogg_stream_init (&ret->map.stream, serialno) != 0)
1702 /* FIXME: either do something with it or remove it */
1703 list = gst_tag_list_new_empty ();
1704 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, GST_TAG_SERIAL, serialno,
1706 gst_tag_list_free (list);
1708 GST_DEBUG_OBJECT (chain->ogg,
1709 "created new ogg src %p for stream with serial %08x", ret, serialno);
1711 g_array_append_val (chain->streams, ret);
1712 gst_pad_set_active (GST_PAD_CAST (ret), TRUE);
1719 GST_ERROR ("Could not initialize ogg_stream struct for serial %08x",
1721 gst_object_unref (ret);
1727 gst_ogg_chain_get_stream (GstOggChain * chain, guint32 serialno)
1731 for (i = 0; i < chain->streams->len; i++) {
1732 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
1734 if (pad->map.serialno == serialno)
1741 gst_ogg_chain_has_stream (GstOggChain * chain, guint32 serialno)
1743 return gst_ogg_chain_get_stream (chain, serialno) != NULL;
1746 /* signals and args */
1759 static GstStaticPadTemplate ogg_demux_src_template_factory =
1760 GST_STATIC_PAD_TEMPLATE ("src_%08x",
1763 GST_STATIC_CAPS_ANY);
1765 static GstStaticPadTemplate ogg_demux_sink_template_factory =
1766 GST_STATIC_PAD_TEMPLATE ("sink",
1769 GST_STATIC_CAPS ("application/ogg; application/x-annodex")
1772 static void gst_ogg_demux_finalize (GObject * object);
1774 static GstFlowReturn gst_ogg_demux_read_chain (GstOggDemux * ogg,
1775 GstOggChain ** chain);
1776 static GstFlowReturn gst_ogg_demux_read_end_chain (GstOggDemux * ogg,
1777 GstOggChain * chain);
1779 static gboolean gst_ogg_demux_sink_event (GstPad * pad, GstObject * parent,
1781 static void gst_ogg_demux_loop (GstOggPad * pad);
1782 static GstFlowReturn gst_ogg_demux_chain (GstPad * pad, GstObject * parent,
1783 GstBuffer * buffer);
1784 static gboolean gst_ogg_demux_sink_activate (GstPad * sinkpad,
1785 GstObject * parent);
1786 static gboolean gst_ogg_demux_sink_activate_mode (GstPad * sinkpad,
1787 GstObject * parent, GstPadMode mode, gboolean active);
1788 static GstStateChangeReturn gst_ogg_demux_change_state (GstElement * element,
1789 GstStateChange transition);
1791 static void gst_ogg_print (GstOggDemux * demux);
1793 #define gst_ogg_demux_parent_class parent_class
1794 G_DEFINE_TYPE (GstOggDemux, gst_ogg_demux, GST_TYPE_ELEMENT);
1797 gst_ogg_demux_class_init (GstOggDemuxClass * klass)
1799 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1800 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1802 gst_element_class_set_details_simple (gstelement_class,
1803 "Ogg demuxer", "Codec/Demuxer",
1804 "demux ogg streams (info about ogg: http://xiph.org)",
1805 "Wim Taymans <wim@fluendo.com>");
1807 gst_element_class_add_pad_template (gstelement_class,
1808 gst_static_pad_template_get (&ogg_demux_sink_template_factory));
1809 gst_element_class_add_pad_template (gstelement_class,
1810 gst_static_pad_template_get (&ogg_demux_src_template_factory));
1812 gstelement_class->change_state = gst_ogg_demux_change_state;
1813 gstelement_class->send_event = gst_ogg_demux_receive_event;
1815 gobject_class->finalize = gst_ogg_demux_finalize;
1819 gst_ogg_demux_init (GstOggDemux * ogg)
1821 /* create the sink pad */
1823 gst_pad_new_from_static_template (&ogg_demux_sink_template_factory,
1826 gst_pad_set_event_function (ogg->sinkpad, gst_ogg_demux_sink_event);
1827 gst_pad_set_chain_function (ogg->sinkpad, gst_ogg_demux_chain);
1828 gst_pad_set_activate_function (ogg->sinkpad, gst_ogg_demux_sink_activate);
1829 gst_pad_set_activatemode_function (ogg->sinkpad,
1830 gst_ogg_demux_sink_activate_mode);
1831 gst_element_add_pad (GST_ELEMENT (ogg), ogg->sinkpad);
1833 ogg->chain_lock = g_mutex_new ();
1834 ogg->push_lock = g_mutex_new ();
1835 ogg->chains = g_array_new (FALSE, TRUE, sizeof (GstOggChain *));
1837 ogg->newsegment = NULL;
1841 gst_ogg_demux_finalize (GObject * object)
1845 ogg = GST_OGG_DEMUX (object);
1847 g_array_free (ogg->chains, TRUE);
1848 g_mutex_free (ogg->chain_lock);
1849 g_mutex_free (ogg->push_lock);
1850 ogg_sync_clear (&ogg->sync);
1852 if (ogg->newsegment)
1853 gst_event_unref (ogg->newsegment);
1855 G_OBJECT_CLASS (parent_class)->finalize (object);
1859 gst_ogg_demux_reset_streams (GstOggDemux * ogg)
1864 chain = ogg->current_chain;
1868 for (i = 0; i < chain->streams->len; i++) {
1869 GstOggPad *stream = g_array_index (chain->streams, GstOggPad *, i);
1871 stream->start_time = -1;
1872 stream->map.accumulated_granule = 0;
1874 ogg->building_chain = chain;
1875 GST_DEBUG_OBJECT (ogg, "Resetting current chain");
1876 ogg->current_chain = NULL;
1881 gst_ogg_demux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
1886 ogg = GST_OGG_DEMUX (parent);
1888 switch (GST_EVENT_TYPE (event)) {
1889 case GST_EVENT_FLUSH_START:
1890 res = gst_ogg_demux_send_event (ogg, event);
1892 case GST_EVENT_FLUSH_STOP:
1893 GST_DEBUG_OBJECT (ogg, "got a flush stop event");
1894 ogg_sync_reset (&ogg->sync);
1895 res = gst_ogg_demux_send_event (ogg, event);
1896 if (ogg->pullmode || ogg->push_state != PUSH_DURATION) {
1897 /* it's starting to feel reaaaally dirty :(
1898 if we're on a spliced seek to get duration, don't reset streams,
1899 we'll need them for the delayed seek */
1900 gst_ogg_demux_reset_streams (ogg);
1903 case GST_EVENT_SEGMENT:
1904 GST_DEBUG_OBJECT (ogg, "got a new segment event");
1908 gst_event_copy_segment (event, &segment);
1910 if (segment.format == GST_FORMAT_BYTES) {
1911 GST_PUSH_LOCK (ogg);
1912 ogg->push_byte_offset = segment.start;
1913 ogg->push_last_seek_offset = segment.start;
1914 GST_PUSH_UNLOCK (ogg);
1916 GST_WARNING_OBJECT (ogg, "unexpected segment format: %s",
1917 gst_format_get_name (segment.format));
1920 gst_event_unref (event);
1925 GST_DEBUG_OBJECT (ogg, "got an EOS event");
1927 /* This would be what is needed (recover from EOS by going on to
1928 the next step (issue the delayed seek)), but it does not work
1929 if there is a queue2 upstream - see more details comment in
1930 gst_ogg_pad_submit_page.
1931 If I could find a way to bypass queue2 behavior, this should
1933 GST_PUSH_LOCK (ogg);
1934 if (ogg->push_state == PUSH_DURATION) {
1935 GST_DEBUG_OBJECT (ogg, "Got EOS while determining length");
1936 res = gst_ogg_demux_seek_back_after_push_duration_check_unlock (ogg);
1937 if (res != GST_FLOW_OK) {
1938 GST_DEBUG_OBJECT (ogg, "Error seeking back after duration check: %d",
1943 GST_PUSH_UNLOCK (ogg);
1945 res = gst_ogg_demux_send_event (ogg, event);
1946 if (ogg->current_chain == NULL) {
1947 GST_ELEMENT_ERROR (ogg, STREAM, DEMUX, (NULL),
1948 ("can't get first chain"));
1953 res = gst_ogg_demux_send_event (ogg, event);
1960 /* submit the given buffer to the ogg sync */
1961 static GstFlowReturn
1962 gst_ogg_demux_submit_buffer (GstOggDemux * ogg, GstBuffer * buffer)
1966 GstFlowReturn ret = GST_FLOW_OK;
1968 size = gst_buffer_get_size (buffer);
1969 GST_DEBUG_OBJECT (ogg, "submitting %" G_GSIZE_FORMAT " bytes", size);
1970 if (G_UNLIKELY (size == 0))
1973 oggbuffer = ogg_sync_buffer (&ogg->sync, size);
1974 if (G_UNLIKELY (oggbuffer == NULL))
1977 gst_buffer_extract (buffer, 0, oggbuffer, size);
1979 if (G_UNLIKELY (ogg_sync_wrote (&ogg->sync, size) < 0))
1982 if (!ogg->pullmode) {
1983 GST_PUSH_LOCK (ogg);
1984 ogg->push_byte_offset += size;
1985 GST_PUSH_UNLOCK (ogg);
1989 gst_buffer_unref (buffer);
1996 GST_ELEMENT_ERROR (ogg, STREAM, DECODE,
1997 (NULL), ("failed to get ogg sync buffer"));
1998 ret = GST_FLOW_ERROR;
2003 GST_ELEMENT_ERROR (ogg, STREAM, DECODE, (NULL),
2004 ("failed to write %" G_GSIZE_FORMAT " bytes to the sync buffer", size));
2005 ret = GST_FLOW_ERROR;
2010 /* in random access mode this code updates the current read position
2011 * and resets the ogg sync buffer so that the next read will happen
2012 * from this new location.
2015 gst_ogg_demux_seek (GstOggDemux * ogg, gint64 offset)
2017 GST_LOG_OBJECT (ogg, "seeking to %" G_GINT64_FORMAT, offset);
2019 ogg->offset = offset;
2020 ogg->read_offset = offset;
2021 ogg_sync_reset (&ogg->sync);
2024 /* read more data from the current offset and submit to
2025 * the ogg sync layer.
2027 static GstFlowReturn
2028 gst_ogg_demux_get_data (GstOggDemux * ogg, gint64 end_offset)
2033 GST_LOG_OBJECT (ogg,
2034 "get data %" G_GINT64_FORMAT " %" G_GINT64_FORMAT " %" G_GINT64_FORMAT,
2035 ogg->read_offset, ogg->length, end_offset);
2037 if (end_offset > 0 && ogg->read_offset >= end_offset)
2038 goto boundary_reached;
2040 if (ogg->read_offset == ogg->length)
2043 ret = gst_pad_pull_range (ogg->sinkpad, ogg->read_offset, CHUNKSIZE, &buffer);
2044 if (ret != GST_FLOW_OK)
2047 ogg->read_offset += gst_buffer_get_size (buffer);
2049 ret = gst_ogg_demux_submit_buffer (ogg, buffer);
2056 GST_LOG_OBJECT (ogg, "reached boundary");
2057 return GST_FLOW_LIMIT;
2061 GST_LOG_OBJECT (ogg, "reached EOS");
2062 return GST_FLOW_EOS;
2066 GST_WARNING_OBJECT (ogg, "got %d (%s) from pull range", ret,
2067 gst_flow_get_name (ret));
2072 /* Read the next page from the current offset.
2073 * boundary: number of bytes ahead we allow looking for;
2076 * @offset will contain the offset the next page starts at when this function
2077 * returns GST_FLOW_OK.
2079 * GST_FLOW_EOS is returned on EOS.
2081 * GST_FLOW_LIMIT is returned when we did not find a page before the
2082 * boundary. If @boundary is -1, this is never returned.
2084 * Any other error returned while retrieving data from the peer is returned as
2087 static GstFlowReturn
2088 gst_ogg_demux_get_next_page (GstOggDemux * ogg, ogg_page * og,
2089 gint64 boundary, gint64 * offset)
2091 gint64 end_offset = -1;
2094 GST_LOG_OBJECT (ogg,
2095 "get next page, current offset %" G_GINT64_FORMAT ", bytes boundary %"
2096 G_GINT64_FORMAT, ogg->offset, boundary);
2099 end_offset = ogg->offset + boundary;
2104 if (end_offset > 0 && ogg->offset >= end_offset)
2105 goto boundary_reached;
2107 more = ogg_sync_pageseek (&ogg->sync, og);
2109 GST_LOG_OBJECT (ogg, "pageseek gave %ld", more);
2112 /* skipped n bytes */
2113 ogg->offset -= more;
2114 GST_LOG_OBJECT (ogg, "skipped %ld bytes, offset %" G_GINT64_FORMAT,
2116 } else if (more == 0) {
2117 /* we need more data */
2119 goto boundary_reached;
2121 GST_LOG_OBJECT (ogg, "need more data");
2122 ret = gst_ogg_demux_get_data (ogg, end_offset);
2123 if (ret != GST_FLOW_OK)
2126 gint64 res_offset = ogg->offset;
2128 /* got a page. Return the offset at the page beginning,
2129 advance the internal offset past the page end */
2131 *offset = res_offset;
2134 ogg->offset += more;
2136 GST_LOG_OBJECT (ogg,
2137 "got page at %" G_GINT64_FORMAT ", serial %08x, end at %"
2138 G_GINT64_FORMAT ", granule %" G_GINT64_FORMAT, res_offset,
2139 ogg_page_serialno (og), ogg->offset,
2140 (gint64) ogg_page_granulepos (og));
2144 GST_LOG_OBJECT (ogg, "returning %d", ret);
2151 GST_LOG_OBJECT (ogg,
2152 "offset %" G_GINT64_FORMAT " >= end_offset %" G_GINT64_FORMAT,
2153 ogg->offset, end_offset);
2154 return GST_FLOW_LIMIT;
2158 /* from the current offset, find the previous page, seeking backwards
2159 * until we find the page.
2161 static GstFlowReturn
2162 gst_ogg_demux_get_prev_page (GstOggDemux * ogg, ogg_page * og, gint64 * offset)
2165 gint64 begin = ogg->offset;
2167 gint64 cur_offset = -1;
2169 GST_LOG_OBJECT (ogg, "getting page before %" G_GINT64_FORMAT, begin);
2171 while (cur_offset == -1) {
2176 /* seek CHUNKSIZE back */
2177 gst_ogg_demux_seek (ogg, begin);
2179 /* now continue reading until we run out of data, if we find a page
2180 * start, we save it. It might not be the final page as there could be
2181 * another page after this one. */
2182 while (ogg->offset < end) {
2186 gst_ogg_demux_get_next_page (ogg, og, end - ogg->offset, &new_offset);
2187 /* we hit the upper limit, offset contains the last page start */
2188 if (ret == GST_FLOW_LIMIT) {
2189 GST_LOG_OBJECT (ogg, "hit limit");
2192 /* something went wrong */
2193 if (ret == GST_FLOW_EOS) {
2195 GST_LOG_OBJECT (ogg, "got unexpected");
2196 } else if (ret != GST_FLOW_OK) {
2197 GST_LOG_OBJECT (ogg, "got error %d", ret);
2201 GST_LOG_OBJECT (ogg, "found page at %" G_GINT64_FORMAT, new_offset);
2203 /* offset is next page start */
2204 cur_offset = new_offset;
2208 GST_LOG_OBJECT (ogg, "found previous page at %" G_GINT64_FORMAT, cur_offset);
2210 /* we have the offset. Actually snork and hold the page now */
2211 gst_ogg_demux_seek (ogg, cur_offset);
2212 ret = gst_ogg_demux_get_next_page (ogg, og, -1, NULL);
2213 if (ret != GST_FLOW_OK) {
2214 GST_WARNING_OBJECT (ogg, "can't get last page at %" G_GINT64_FORMAT,
2216 /* this shouldn't be possible */
2221 *offset = cur_offset;
2227 gst_ogg_demux_deactivate_current_chain (GstOggDemux * ogg)
2230 GstOggChain *chain = ogg->current_chain;
2235 GST_DEBUG_OBJECT (ogg, "deactivating chain %p", chain);
2237 /* send EOS on all the pads */
2238 for (i = 0; i < chain->streams->len; i++) {
2239 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
2245 event = gst_event_new_eos ();
2246 gst_event_set_seqnum (event, ogg->seqnum);
2247 gst_pad_push_event (GST_PAD_CAST (pad), event);
2249 GST_DEBUG_OBJECT (ogg, "removing pad %" GST_PTR_FORMAT, pad);
2251 /* deactivate first */
2252 gst_pad_set_active (GST_PAD_CAST (pad), FALSE);
2254 gst_element_remove_pad (GST_ELEMENT (ogg), GST_PAD_CAST (pad));
2258 /* With push mode seeking implemented, we can now seek back to the chain,
2259 so we do not destroy it */
2260 GST_DEBUG_OBJECT (ogg, "Resetting current chain");
2261 ogg->current_chain = NULL;
2267 gst_ogg_demux_set_header_on_caps (GstOggDemux * ogg, GstCaps * caps,
2270 GstStructure *structure;
2271 GValue array = { 0 };
2273 GST_LOG_OBJECT (ogg, "caps: %" GST_PTR_FORMAT, caps);
2275 if (G_UNLIKELY (!caps))
2277 if (G_UNLIKELY (!headers))
2280 caps = gst_caps_make_writable (caps);
2281 structure = gst_caps_get_structure (caps, 0);
2283 g_value_init (&array, GST_TYPE_ARRAY);
2286 GValue value = { 0 };
2288 ogg_packet *op = headers->data;
2290 buffer = gst_buffer_new_and_alloc (op->bytes);
2292 gst_buffer_fill (buffer, 0, op->packet, op->bytes);
2293 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_IN_CAPS);
2294 g_value_init (&value, GST_TYPE_BUFFER);
2295 gst_value_take_buffer (&value, buffer);
2296 gst_value_array_append_value (&array, &value);
2297 g_value_unset (&value);
2298 headers = headers->next;
2301 gst_structure_set_value (structure, "streamheader", &array);
2302 g_value_unset (&array);
2303 GST_LOG_OBJECT (ogg, "here are the newly set caps: %" GST_PTR_FORMAT, caps);
2309 gst_ogg_demux_push_queued_buffers (GstOggDemux * ogg, GstOggPad * pad)
2313 /* push queued packets */
2314 for (walk = pad->map.queued; walk; walk = g_list_next (walk)) {
2315 ogg_packet *p = walk->data;
2317 gst_ogg_demux_chain_peer (pad, p, TRUE);
2318 _ogg_packet_free (p);
2320 /* and free the queued buffers */
2321 g_list_free (pad->map.queued);
2322 pad->map.queued = NULL;
2326 gst_ogg_demux_activate_chain (GstOggDemux * ogg, GstOggChain * chain,
2330 gint bitrate, idx_bitrate;
2332 g_return_val_if_fail (chain != NULL, FALSE);
2334 if (chain == ogg->current_chain) {
2336 gst_event_unref (event);
2338 for (i = 0; i < chain->streams->len; i++) {
2339 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
2340 gst_ogg_demux_push_queued_buffers (ogg, pad);
2346 GST_DEBUG_OBJECT (ogg, "activating chain %p", chain);
2348 bitrate = idx_bitrate = 0;
2350 /* first add the pads */
2351 for (i = 0; i < chain->streams->len; i++) {
2354 pad = g_array_index (chain->streams, GstOggPad *, i);
2356 if (pad->map.idx_bitrate)
2357 idx_bitrate = MAX (idx_bitrate, pad->map.idx_bitrate);
2359 bitrate += pad->map.bitrate;
2362 gst_ogg_pad_mark_discont (pad);
2363 pad->last_ret = GST_FLOW_OK;
2365 if (pad->map.is_skeleton || pad->added
2366 || !gst_pad_has_current_caps (GST_PAD_CAST (pad)))
2369 GST_DEBUG_OBJECT (ogg, "adding pad %" GST_PTR_FORMAT, pad);
2371 /* activate first */
2372 gst_pad_set_active (GST_PAD_CAST (pad), TRUE);
2374 gst_element_add_pad (GST_ELEMENT (ogg), GST_PAD_CAST (pad));
2377 /* prefer the index bitrate over the ones encoded in the streams */
2378 ogg->bitrate = (idx_bitrate ? idx_bitrate : bitrate);
2380 /* after adding the new pads, remove the old pads */
2381 gst_ogg_demux_deactivate_current_chain (ogg);
2383 GST_DEBUG_OBJECT (ogg, "Setting current chain to %p", chain);
2384 ogg->current_chain = chain;
2386 /* we are finished now */
2387 gst_element_no_more_pads (GST_ELEMENT (ogg));
2389 /* FIXME, must be sent from the streaming thread */
2393 gst_ogg_demux_send_event (ogg, event);
2395 tags = gst_tag_list_new (GST_TAG_CONTAINER_FORMAT, "Ogg", NULL);
2396 gst_ogg_demux_send_event (ogg, gst_event_new_tag (tags));
2399 GST_DEBUG_OBJECT (ogg, "starting chain");
2401 /* then send out any headers and queued packets */
2402 for (i = 0; i < chain->streams->len; i++) {
2406 pad = g_array_index (chain->streams, GstOggPad *, i);
2408 /* FIXME also streaming thread */
2409 if (pad->map.taglist) {
2410 GST_DEBUG_OBJECT (ogg, "pushing tags");
2411 gst_pad_push_event (GST_PAD_CAST (pad),
2412 gst_event_new_tag (pad->map.taglist));
2413 pad->map.taglist = NULL;
2416 /* Set headers on caps */
2418 gst_ogg_demux_set_header_on_caps (ogg, pad->map.caps, pad->map.headers);
2419 gst_pad_set_caps (GST_PAD_CAST (pad), pad->map.caps);
2421 GST_DEBUG_OBJECT (ogg, "pushing headers");
2423 for (walk = pad->map.headers; walk; walk = g_list_next (walk)) {
2424 ogg_packet *p = walk->data;
2426 gst_ogg_demux_chain_peer (pad, p, TRUE);
2429 GST_DEBUG_OBJECT (ogg, "pushing queued buffers");
2430 gst_ogg_demux_push_queued_buffers (ogg, pad);
2436 do_binary_search (GstOggDemux * ogg, GstOggChain * chain, gint64 begin,
2437 gint64 end, gint64 begintime, gint64 endtime, gint64 target,
2446 GST_DEBUG_OBJECT (ogg,
2447 "chain offset %" G_GINT64_FORMAT ", end offset %" G_GINT64_FORMAT,
2449 GST_DEBUG_OBJECT (ogg,
2450 "chain begin time %" GST_TIME_FORMAT ", end time %" GST_TIME_FORMAT,
2451 GST_TIME_ARGS (begintime), GST_TIME_ARGS (endtime));
2452 GST_DEBUG_OBJECT (ogg, "target %" GST_TIME_FORMAT, GST_TIME_ARGS (target));
2454 /* perform the seek */
2455 while (begin < end) {
2458 if ((end - begin < CHUNKSIZE) || (endtime == begintime)) {
2461 /* take a (pretty decent) guess, avoiding overflow */
2462 gint64 rate = (end - begin) * GST_MSECOND / (endtime - begintime);
2464 bisect = (target - begintime) / GST_MSECOND * rate + begin - CHUNKSIZE;
2466 if (bisect <= begin)
2468 GST_DEBUG_OBJECT (ogg, "Initial guess: %" G_GINT64_FORMAT, bisect);
2470 gst_ogg_demux_seek (ogg, bisect);
2472 while (begin < end) {
2475 GST_DEBUG_OBJECT (ogg,
2476 "after seek, bisect %" G_GINT64_FORMAT ", begin %" G_GINT64_FORMAT
2477 ", end %" G_GINT64_FORMAT, bisect, begin, end);
2479 ret = gst_ogg_demux_get_next_page (ogg, &og, end - ogg->offset, &result);
2480 GST_LOG_OBJECT (ogg, "looking for next page returned %" G_GINT64_FORMAT,
2483 if (ret == GST_FLOW_LIMIT) {
2484 /* we hit the upper limit, go back a bit */
2485 if (bisect <= begin + 1) {
2486 end = begin; /* found it */
2491 bisect -= CHUNKSIZE;
2492 if (bisect <= begin)
2495 gst_ogg_demux_seek (ogg, bisect);
2497 } else if (ret == GST_FLOW_OK) {
2498 /* found offset of next ogg page */
2500 GstClockTime granuletime;
2503 /* get the granulepos */
2504 GST_LOG_OBJECT (ogg, "found next ogg page at %" G_GINT64_FORMAT,
2506 granulepos = ogg_page_granulepos (&og);
2507 if (granulepos == -1) {
2508 GST_LOG_OBJECT (ogg, "granulepos of next page is -1");
2512 /* get the stream */
2513 pad = gst_ogg_chain_get_stream (chain, ogg_page_serialno (&og));
2514 if (pad == NULL || pad->map.is_skeleton)
2517 /* convert granulepos to time */
2518 granuletime = gst_ogg_stream_get_end_time_for_granulepos (&pad->map,
2520 if (granuletime < pad->start_time)
2523 GST_LOG_OBJECT (ogg, "granulepos %" G_GINT64_FORMAT " maps to time %"
2524 GST_TIME_FORMAT, granulepos, GST_TIME_ARGS (granuletime));
2526 granuletime -= pad->start_time;
2527 granuletime += chain->begin_time;
2529 GST_DEBUG_OBJECT (ogg,
2530 "found page with granule %" G_GINT64_FORMAT " and time %"
2531 GST_TIME_FORMAT, granulepos, GST_TIME_ARGS (granuletime));
2533 if (granuletime < target) {
2534 best = result; /* raw offset of packet with granulepos */
2535 begin = ogg->offset; /* raw offset of next page */
2536 begintime = granuletime;
2538 bisect = begin; /* *not* begin + 1 */
2540 if (bisect <= begin + 1) {
2541 end = begin; /* found it */
2543 if (end == ogg->offset) { /* we're pretty close - we'd be stuck in */
2545 bisect -= CHUNKSIZE; /* an endless loop otherwise. */
2546 if (bisect <= begin)
2548 gst_ogg_demux_seek (ogg, bisect);
2551 endtime = granuletime;
2560 GST_DEBUG_OBJECT (ogg, "seeking to %" G_GINT64_FORMAT, best);
2561 gst_ogg_demux_seek (ogg, best);
2569 GST_DEBUG_OBJECT (ogg, "got a seek error");
2575 do_index_search (GstOggDemux * ogg, GstOggChain * chain, gint64 begin,
2576 gint64 end, gint64 begintime, gint64 endtime, gint64 target,
2577 gint64 * p_offset, gint64 * p_timestamp)
2580 guint64 timestamp, offset;
2581 guint64 r_timestamp, r_offset;
2582 gboolean result = FALSE;
2584 target -= begintime;
2589 for (i = 0; i < chain->streams->len; i++) {
2590 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
2593 if (gst_ogg_map_search_index (&pad->map, TRUE, ×tamp, &offset)) {
2594 GST_INFO ("found %" G_GUINT64_FORMAT " at offset %" G_GUINT64_FORMAT,
2597 if (r_offset == -1 || offset < r_offset) {
2599 r_timestamp = timestamp;
2606 *p_timestamp = r_timestamp;
2608 *p_offset = r_offset;
2614 * do seek to time @position, return FALSE or chain and TRUE
2617 gst_ogg_demux_do_seek (GstOggDemux * ogg, GstSegment * segment,
2618 gboolean accurate, gboolean keyframe, GstOggChain ** rchain)
2621 GstOggChain *chain = NULL;
2623 gint64 begintime, endtime;
2624 gint64 target, keytarget;
2629 gint i, pending, len;
2630 gboolean first_parsed_page = TRUE;
2632 position = segment->position;
2634 /* first find the chain to search in */
2635 total = ogg->total_time;
2636 if (ogg->chains->len == 0)
2639 for (i = ogg->chains->len - 1; i >= 0; i--) {
2640 chain = g_array_index (ogg->chains, GstOggChain *, i);
2641 total -= chain->total_time;
2642 if (position >= total)
2646 /* first step, locate page containing the required data */
2647 begin = chain->offset;
2648 end = chain->end_offset;
2649 begintime = chain->begin_time;
2650 endtime = begintime + chain->total_time;
2651 target = position - total + begintime;
2653 if (!do_binary_search (ogg, chain, begin, end, begintime, endtime, target,
2657 /* second step: find pages for all streams, we use the keyframe_granule to keep
2658 * track of which ones we saw. If we have seen a page for each stream we can
2659 * calculate the positions of each keyframe. */
2660 GST_DEBUG_OBJECT (ogg, "find keyframes");
2661 len = pending = chain->streams->len;
2663 /* figure out where the keyframes are */
2670 GstClockTime keyframe_time, granule_time;
2672 ret = gst_ogg_demux_get_next_page (ogg, &og, end - ogg->offset, &result);
2673 GST_LOG_OBJECT (ogg, "looking for next page returned %" G_GINT64_FORMAT,
2675 if (ret == GST_FLOW_LIMIT) {
2676 GST_LOG_OBJECT (ogg, "reached limit");
2678 } else if (ret != GST_FLOW_OK)
2681 /* get the stream */
2682 pad = gst_ogg_chain_get_stream (chain, ogg_page_serialno (&og));
2686 if (pad->map.is_skeleton)
2689 granulepos = ogg_page_granulepos (&og);
2690 if (granulepos == -1 || granulepos == 0) {
2691 GST_LOG_OBJECT (ogg, "granulepos of next page is -1");
2695 /* we only do this the first time we pass here */
2696 if (first_parsed_page) {
2697 /* Now that we have a time reference from the page, we can check
2698 * whether all streams still have pages from here on.
2700 * This would be more elegant before the loop, but getting the page from
2701 * there without breaking anything would be more costly */
2702 granule_time = gst_ogg_stream_get_end_time_for_granulepos (&pad->map,
2704 for (i = 0; i < len; i++) {
2705 GstOggPad *stream = g_array_index (chain->streams, GstOggPad *, i);
2708 /* we already know we have at least one page (the current one)
2709 * for this stream */
2712 if (granule_time > stream->map.total_time)
2713 /* we won't encounter any more pages of this stream, so we don't
2714 * try finding a key frame for it */
2717 first_parsed_page = FALSE;
2721 /* in reverse we want to go past the page with the lower timestamp */
2722 if (segment->rate < 0.0) {
2723 /* get time for this pad */
2724 granule_time = gst_ogg_stream_get_end_time_for_granulepos (&pad->map,
2727 GST_LOG_OBJECT (ogg,
2728 "looking at page with ts %" GST_TIME_FORMAT ", target %"
2729 GST_TIME_FORMAT, GST_TIME_ARGS (granule_time),
2730 GST_TIME_ARGS (target));
2731 if (granule_time < target)
2735 /* we've seen this pad before */
2736 if (pad->keyframe_granule != -1)
2739 /* convert granule of this pad to the granule of the keyframe */
2740 pad->keyframe_granule =
2741 gst_ogg_stream_granulepos_to_key_granule (&pad->map, granulepos);
2742 GST_LOG_OBJECT (ogg, "marking stream granule %" G_GINT64_FORMAT,
2743 pad->keyframe_granule);
2745 /* get time of the keyframe */
2747 gst_ogg_stream_granule_to_time (&pad->map, pad->keyframe_granule);
2748 GST_LOG_OBJECT (ogg,
2749 "stream %08x granule time %" GST_TIME_FORMAT,
2750 pad->map.serialno, GST_TIME_ARGS (keyframe_time));
2752 /* collect smallest value */
2753 if (keyframe_time != -1) {
2754 keyframe_time += begintime;
2755 if (keyframe_time < keytarget)
2756 keytarget = keyframe_time;
2765 /* for negative rates we will get to the keyframe backwards */
2766 if (segment->rate < 0.0)
2769 if (keytarget != target) {
2770 GST_LOG_OBJECT (ogg, "final seek to target %" GST_TIME_FORMAT,
2771 GST_TIME_ARGS (keytarget));
2773 /* last step, seek to the location of the keyframe */
2774 if (!do_binary_search (ogg, chain, begin, end, begintime, endtime,
2778 /* seek back to previous position */
2779 GST_LOG_OBJECT (ogg, "keyframe on target");
2780 gst_ogg_demux_seek (ogg, best);
2785 if (segment->rate > 0.0)
2786 segment->time = keytarget;
2787 segment->position = keytarget - begintime;
2796 GST_DEBUG_OBJECT (ogg, "no chains");
2801 GST_DEBUG_OBJECT (ogg, "got a seek error");
2806 /* does not take ownership of the event */
2808 gst_ogg_demux_perform_seek_pull (GstOggDemux * ogg, GstEvent * event)
2810 GstOggChain *chain = NULL;
2812 gboolean flush, accurate, keyframe;
2816 GstSeekType cur_type, stop_type;
2823 GST_DEBUG_OBJECT (ogg, "seek with event");
2825 gst_event_parse_seek (event, &rate, &format, &flags,
2826 &cur_type, &cur, &stop_type, &stop);
2828 /* we can only seek on time */
2829 if (format != GST_FORMAT_TIME) {
2830 GST_DEBUG_OBJECT (ogg, "can only seek on TIME");
2833 seqnum = gst_event_get_seqnum (event);
2835 GST_DEBUG_OBJECT (ogg, "seek without event");
2839 seqnum = gst_util_seqnum_next ();
2842 GST_DEBUG_OBJECT (ogg, "seek, rate %g", rate);
2844 flush = flags & GST_SEEK_FLAG_FLUSH;
2845 accurate = flags & GST_SEEK_FLAG_ACCURATE;
2846 keyframe = flags & GST_SEEK_FLAG_KEY_UNIT;
2848 /* first step is to unlock the streaming thread if it is
2849 * blocked in a chain call, we do this by starting the flush. because
2850 * we cannot yet hold any streaming lock, we have to protect the chains
2851 * with their own lock. */
2855 tevent = gst_event_new_flush_start ();
2856 gst_event_set_seqnum (tevent, seqnum);
2858 gst_event_ref (tevent);
2859 gst_pad_push_event (ogg->sinkpad, tevent);
2861 GST_CHAIN_LOCK (ogg);
2862 for (i = 0; i < ogg->chains->len; i++) {
2863 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
2866 for (j = 0; j < chain->streams->len; j++) {
2867 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, j);
2869 gst_event_ref (tevent);
2870 gst_pad_push_event (GST_PAD (pad), tevent);
2873 GST_CHAIN_UNLOCK (ogg);
2875 gst_event_unref (tevent);
2877 gst_pad_pause_task (ogg->sinkpad);
2880 /* now grab the stream lock so that streaming cannot continue, for
2881 * non flushing seeks when the element is in PAUSED this could block
2883 GST_PAD_STREAM_LOCK (ogg->sinkpad);
2886 gst_segment_do_seek (&ogg->segment, rate, format, flags,
2887 cur_type, cur, stop_type, stop, &update);
2890 GST_DEBUG_OBJECT (ogg, "segment positions set to %" GST_TIME_FORMAT "-%"
2891 GST_TIME_FORMAT, GST_TIME_ARGS (ogg->segment.start),
2892 GST_TIME_ARGS (ogg->segment.stop));
2894 /* we need to stop flushing on the srcpad as we're going to use it
2895 * next. We can do this as we have the STREAM lock now. */
2897 tevent = gst_event_new_flush_stop (TRUE);
2898 gst_event_set_seqnum (tevent, seqnum);
2899 gst_pad_push_event (ogg->sinkpad, tevent);
2905 /* reset all ogg streams now, need to do this from within the lock to
2906 * make sure the streaming thread is not messing with the stream */
2907 for (i = 0; i < ogg->chains->len; i++) {
2908 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
2910 gst_ogg_chain_reset (chain);
2914 /* for reverse we will already seek accurately */
2915 res = gst_ogg_demux_do_seek (ogg, &ogg->segment, accurate, keyframe, &chain);
2917 /* seek failed, make sure we continue the current chain */
2919 GST_DEBUG_OBJECT (ogg, "seek failed");
2920 chain = ogg->current_chain;
2922 GST_DEBUG_OBJECT (ogg, "seek success");
2928 /* now we have a new position, prepare for streaming again */
2933 gint64 position, begin_time;
2936 /* we have to send the flush to the old chain, not the new one */
2938 tevent = gst_event_new_flush_stop (TRUE);
2939 gst_event_set_seqnum (tevent, seqnum);
2940 gst_ogg_demux_send_event (ogg, tevent);
2943 /* we need this to see how far inside the chain we need to start */
2944 if (chain->begin_time != GST_CLOCK_TIME_NONE)
2945 begin_time = chain->begin_time;
2949 /* segment.start gives the start over all chains, we calculate the amount
2950 * of time into this chain we need to start */
2951 start = ogg->segment.start - begin_time;
2952 if (chain->segment_start != GST_CLOCK_TIME_NONE)
2953 start += chain->segment_start;
2955 if ((stop = ogg->segment.stop) == -1)
2956 stop = ogg->segment.duration;
2958 /* segment.stop gives the stop time over all chains, calculate the amount of
2959 * time we need to stop in this chain */
2961 if (stop > begin_time)
2965 stop += chain->segment_start;
2966 /* we must stop when this chain ends and switch to the next chain to play
2967 * the remainder of the segment. */
2968 stop = MIN (stop, chain->segment_stop);
2971 position = ogg->segment.position;
2972 if (chain->segment_start != GST_CLOCK_TIME_NONE)
2973 position += chain->segment_start;
2975 gst_segment_copy_into (&ogg->segment, &segment);
2977 /* create the segment event we are going to send out */
2978 if (ogg->segment.rate >= 0.0) {
2979 segment.start = position;
2980 segment.stop = stop;
2982 segment.start = start;
2983 segment.stop = position;
2985 event = gst_event_new_segment (&segment);
2986 gst_event_set_seqnum (event, seqnum);
2988 if (chain != ogg->current_chain) {
2989 /* switch to different chain, send segment on new chain */
2990 gst_ogg_demux_activate_chain (ogg, chain, event);
2992 /* mark discont and send segment on current chain */
2993 gst_ogg_chain_mark_discont (chain);
2994 /* This event should be sent from the streaming thread (sink pad task) */
2995 if (ogg->newsegment)
2996 gst_event_unref (ogg->newsegment);
2997 ogg->newsegment = event;
3000 /* notify start of new segment */
3001 if (ogg->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3002 GstMessage *message;
3004 message = gst_message_new_segment_start (GST_OBJECT (ogg),
3005 GST_FORMAT_TIME, ogg->segment.position);
3006 gst_message_set_seqnum (message, seqnum);
3008 gst_element_post_message (GST_ELEMENT (ogg), message);
3011 ogg->seqnum = seqnum;
3012 /* restart our task since it might have been stopped when we did the
3014 gst_pad_start_task (ogg->sinkpad, (GstTaskFunction) gst_ogg_demux_loop,
3018 /* streaming can continue now */
3019 GST_PAD_STREAM_UNLOCK (ogg->sinkpad);
3026 GST_DEBUG_OBJECT (ogg, "seek failed");
3031 GST_DEBUG_OBJECT (ogg, "no chain to seek in");
3032 GST_PAD_STREAM_UNLOCK (ogg->sinkpad);
3038 gst_ogg_demux_get_duration_push (GstOggDemux * ogg, int flags)
3040 /* In push mode, we get to the end of the stream to get the duration */
3045 /* A full Ogg page can be almost 64 KB. There's no guarantee that there'll be a
3046 granpos there, but it's fairly likely */
3048 ogg->push_byte_length - DURATION_CHUNK_OFFSET - EOS_AVOIDANCE_THRESHOLD;
3052 GST_DEBUG_OBJECT (ogg,
3053 "Getting duration, seeking near the end, to %" G_GINT64_FORMAT, position);
3054 ogg->push_state = PUSH_DURATION;
3055 /* do not read the last byte */
3056 sevent = gst_event_new_seek (1.0, GST_FORMAT_BYTES, flags, GST_SEEK_TYPE_SET,
3057 position, GST_SEEK_TYPE_SET, ogg->push_byte_length - 1);
3058 res = gst_pad_push_event (ogg->sinkpad, sevent);
3060 GST_DEBUG_OBJECT (ogg, "Seek succesful");
3063 GST_INFO_OBJECT (ogg, "Seek failed, duration will stay unknown");
3064 ogg->push_state = PUSH_PLAYING;
3065 ogg->push_disable_seeking = TRUE;
3071 gst_ogg_demux_check_duration_push (GstOggDemux * ogg, GstSeekFlags flags,
3074 if (ogg->push_byte_length < 0) {
3077 GST_DEBUG_OBJECT (ogg, "Trying to find byte/time length");
3078 if ((peer = gst_pad_get_peer (ogg->sinkpad)) != NULL) {
3082 res = gst_pad_query_duration (peer, GST_FORMAT_BYTES, &length);
3083 if (res && length > 0) {
3084 ogg->push_byte_length = length;
3085 GST_DEBUG_OBJECT (ogg,
3086 "File byte length %" G_GINT64_FORMAT, ogg->push_byte_length);
3088 res = gst_pad_query_duration (peer, GST_FORMAT_TIME, &length);
3089 gst_object_unref (peer);
3090 if (res && length >= 0) {
3091 ogg->push_time_length = length;
3092 GST_DEBUG_OBJECT (ogg, "File time length %" GST_TIME_FORMAT,
3093 GST_TIME_ARGS (ogg->push_time_length));
3094 } else if (!ogg->push_disable_seeking) {
3097 res = gst_ogg_demux_get_duration_push (ogg, flags);
3099 GST_DEBUG_OBJECT (ogg,
3100 "File time length unknown, trying to determine");
3101 ogg->push_mode_seek_delayed_event = NULL;
3103 GST_DEBUG_OBJECT (ogg,
3104 "Let me intercept this innocent looking seek request");
3105 ogg->push_mode_seek_delayed_event = gst_event_copy (event);
3116 gst_ogg_demux_perform_seek_push (GstOggDemux * ogg, GstEvent * event)
3119 gboolean res = TRUE;
3123 GstSeekType start_type, stop_type;
3127 gint64 best, best_time;
3130 GST_DEBUG_OBJECT (ogg, "Push mode seek request received");
3132 gst_event_parse_seek (event, &rate, &format, &flags,
3133 &start_type, &start, &stop_type, &stop);
3135 if (format != GST_FORMAT_TIME) {
3136 GST_DEBUG_OBJECT (ogg, "can only seek on TIME");
3140 if (start_type != GST_SEEK_TYPE_SET) {
3141 GST_DEBUG_OBJECT (ogg, "can only seek to a SET target");
3145 if (!(flags & GST_SEEK_FLAG_FLUSH)) {
3146 GST_DEBUG_OBJECT (ogg, "can only do flushing seeks");
3150 GST_DEBUG_OBJECT (ogg, "Push mode seek request: %" GST_TIME_FORMAT,
3151 GST_TIME_ARGS (start));
3153 chain = ogg->current_chain;
3155 GST_WARNING_OBJECT (ogg, "No chain to seek on");
3159 /* start accessing push_* members */
3160 GST_PUSH_LOCK (ogg);
3162 /* not if we disabled seeking (chained streams) */
3163 if (ogg->push_disable_seeking) {
3164 GST_DEBUG_OBJECT (ogg, "Seeking disabled");
3168 /* not when we're trying to work out duration */
3169 if (ogg->push_state == PUSH_DURATION) {
3170 GST_DEBUG_OBJECT (ogg, "Busy working out duration, try again later");
3174 /* actually, not if we're doing any seeking already */
3175 if (ogg->push_state != PUSH_PLAYING) {
3176 GST_DEBUG_OBJECT (ogg, "Already doing some seeking, try again later");
3180 /* on the first seek, get length if we can */
3181 if (!gst_ogg_demux_check_duration_push (ogg, flags, event)) {
3182 GST_PUSH_UNLOCK (ogg);
3186 if (do_index_search (ogg, chain, 0, -1, 0, -1, start, &best, &best_time)) {
3187 /* the index gave some result */
3188 GST_DEBUG_OBJECT (ogg,
3189 "found offset %" G_GINT64_FORMAT " with time %" G_GUINT64_FORMAT,
3192 if (ogg->push_time_length > 0) {
3193 /* if we know the time length, we know the full segment bitrate */
3194 GST_DEBUG_OBJECT (ogg, "Using real file bitrate");
3196 gst_util_uint64_scale (ogg->push_byte_length, 8 * GST_SECOND,
3197 ogg->push_time_length);
3198 } else if (ogg->push_time_offset > 0) {
3199 /* get a first approximation using known bitrate to the current position */
3200 GST_DEBUG_OBJECT (ogg, "Using file bitrate so far");
3202 gst_util_uint64_scale (ogg->push_byte_offset, 8 * GST_SECOND,
3203 ogg->push_time_offset);
3204 } else if (ogg->bitrate > 0) {
3205 /* nominal bitrate is better than nothing, even if it lies often */
3206 GST_DEBUG_OBJECT (ogg, "Using nominal bitrate");
3207 bitrate = ogg->bitrate;
3210 GST_DEBUG_OBJECT (ogg,
3211 "At stream start, and no nominal bitrate, using some random magic "
3213 /* the bisection, once started, should give us a better approximation */
3216 best = gst_util_uint64_scale (start, bitrate, 8 * GST_SECOND);
3219 /* offset by typical page length, and ensure our best guess is within
3220 reasonable bounds */
3224 if (ogg->push_byte_length > 0 && best >= ogg->push_byte_length)
3225 best = ogg->push_byte_length - 1;
3227 /* set up bisection search */
3228 ogg->push_offset0 = 0;
3229 ogg->push_offset1 = ogg->push_byte_length - 1;
3230 ogg->push_time0 = ogg->push_start_time;
3231 ogg->push_time1 = ogg->push_time_length;
3232 ogg->push_seek_time_target = start;
3233 ogg->push_seek_time_original_target = start;
3234 ogg->push_state = PUSH_BISECT1;
3236 /* reset pad push mode seeking state */
3237 for (i = 0; i < chain->streams->len; i++) {
3238 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
3239 pad->push_kf_time = GST_CLOCK_TIME_NONE;
3240 pad->push_sync_time = GST_CLOCK_TIME_NONE;
3243 GST_DEBUG_OBJECT (ogg,
3244 "Setting up bisection search for %" G_GINT64_FORMAT " - %" G_GINT64_FORMAT
3245 " (time %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT ")", ogg->push_offset0,
3246 ogg->push_offset1, GST_TIME_ARGS (ogg->push_time0),
3247 GST_TIME_ARGS (ogg->push_time1));
3248 GST_DEBUG_OBJECT (ogg,
3249 "Target time is %" GST_TIME_FORMAT ", best first guess is %"
3250 G_GINT64_FORMAT, GST_TIME_ARGS (ogg->push_seek_time_target), best);
3252 ogg->push_seek_rate = rate;
3253 ogg->push_seek_flags = flags;
3254 ogg->push_mode_seek_delayed_event = NULL;
3255 ogg->push_bisection_steps[0] = 1;
3256 ogg->push_bisection_steps[1] = 0;
3257 sevent = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
3258 start_type, best, GST_SEEK_TYPE_NONE, -1);
3260 GST_PUSH_UNLOCK (ogg);
3261 res = gst_pad_push_event (ogg->sinkpad, sevent);
3268 GST_DEBUG_OBJECT (ogg, "seek failed");
3273 GST_PUSH_UNLOCK (ogg);
3278 gst_ogg_demux_perform_seek (GstOggDemux * ogg, GstEvent * event)
3282 if (ogg->pullmode) {
3283 res = gst_ogg_demux_perform_seek_pull (ogg, event);
3285 res = gst_ogg_demux_perform_seek_push (ogg, event);
3291 /* finds each bitstream link one at a time using a bisection search
3292 * (has to begin by knowing the offset of the lb's initial page).
3293 * Recurses for each link so it can alloc the link storage after
3294 * finding them all, then unroll and fill the cache at the same time
3296 static GstFlowReturn
3297 gst_ogg_demux_bisect_forward_serialno (GstOggDemux * ogg,
3298 gint64 begin, gint64 searched, gint64 end, GstOggChain * chain, glong m)
3300 gint64 endsearched = end;
3305 GstOggChain *nextchain;
3307 GST_LOG_OBJECT (ogg,
3308 "bisect begin: %" G_GINT64_FORMAT ", searched: %" G_GINT64_FORMAT
3309 ", end %" G_GINT64_FORMAT ", chain: %p", begin, searched, end, chain);
3311 /* the below guards against garbage seperating the last and
3312 * first pages of two links. */
3313 while (searched < endsearched) {
3316 if (endsearched - searched < CHUNKSIZE) {
3319 bisect = (searched + endsearched) / 2;
3322 gst_ogg_demux_seek (ogg, bisect);
3323 ret = gst_ogg_demux_get_next_page (ogg, &og, -1, &offset);
3325 if (ret == GST_FLOW_EOS) {
3326 endsearched = bisect;
3327 } else if (ret == GST_FLOW_OK) {
3328 guint32 serial = ogg_page_serialno (&og);
3330 if (!gst_ogg_chain_has_stream (chain, serial)) {
3331 endsearched = bisect;
3334 searched = offset + og.header_len + og.body_len;
3340 GST_LOG_OBJECT (ogg, "current chain ends at %" G_GINT64_FORMAT, searched);
3342 chain->end_offset = searched;
3343 ret = gst_ogg_demux_read_end_chain (ogg, chain);
3344 if (ret != GST_FLOW_OK)
3347 GST_LOG_OBJECT (ogg, "found begin at %" G_GINT64_FORMAT, next);
3349 gst_ogg_demux_seek (ogg, next);
3350 ret = gst_ogg_demux_read_chain (ogg, &nextchain);
3351 if (ret == GST_FLOW_EOS) {
3354 GST_LOG_OBJECT (ogg, "no next chain");
3355 } else if (ret != GST_FLOW_OK)
3358 if (searched < end && nextchain != NULL) {
3359 ret = gst_ogg_demux_bisect_forward_serialno (ogg, next, ogg->offset,
3360 end, nextchain, m + 1);
3361 if (ret != GST_FLOW_OK)
3364 GST_LOG_OBJECT (ogg, "adding chain %p", chain);
3366 g_array_insert_val (ogg->chains, 0, chain);
3372 /* read a chain from the ogg file. This code will
3373 * read all BOS pages and will create and return a GstOggChain
3374 * structure with the results.
3376 * This function will also read N pages from each stream in the
3377 * chain and submit them to the decoders. When the decoder has
3378 * decoded the first buffer, we know the timestamp of the first
3379 * page in the chain.
3381 static GstFlowReturn
3382 gst_ogg_demux_read_chain (GstOggDemux * ogg, GstOggChain ** res_chain)
3385 GstOggChain *chain = NULL;
3386 gint64 offset = ogg->offset;
3391 GST_LOG_OBJECT (ogg, "reading chain at %" G_GINT64_FORMAT, offset);
3393 /* first read the BOS pages, do typefind on them, create
3394 * the decoders, send data to the decoders. */
3399 ret = gst_ogg_demux_get_next_page (ogg, &og, -1, NULL);
3400 if (ret != GST_FLOW_OK) {
3401 if (ret == GST_FLOW_EOS) {
3402 GST_DEBUG_OBJECT (ogg, "Reached EOS, done reading end chain");
3404 GST_WARNING_OBJECT (ogg, "problem reading BOS page: ret=%d", ret);
3408 if (!ogg_page_bos (&og)) {
3409 GST_INFO_OBJECT (ogg, "page is not BOS page, all streams identified");
3410 /* if we did not find a chain yet, assume this is a bogus stream and
3413 GST_WARNING_OBJECT (ogg, "No chain found, no Ogg data in stream ?");
3419 if (chain == NULL) {
3420 chain = gst_ogg_chain_new (ogg);
3421 chain->offset = offset;
3424 serial = ogg_page_serialno (&og);
3425 if (gst_ogg_chain_get_stream (chain, serial) != NULL) {
3426 GST_WARNING_OBJECT (ogg,
3427 "found serial %08x BOS page twice, ignoring", serial);
3431 pad = gst_ogg_chain_new_stream (chain, serial);
3432 gst_ogg_pad_submit_page (pad, &og);
3435 if (ret != GST_FLOW_OK || chain == NULL) {
3436 if (ret == GST_FLOW_OK) {
3437 GST_WARNING_OBJECT (ogg, "no chain was found");
3438 ret = GST_FLOW_ERROR;
3439 } else if (ret != GST_FLOW_EOS) {
3440 GST_WARNING_OBJECT (ogg, "failed to read chain");
3442 GST_DEBUG_OBJECT (ogg, "done reading chains");
3445 gst_ogg_chain_free (chain);
3452 chain->have_bos = TRUE;
3453 GST_LOG_OBJECT (ogg, "read bos pages, init decoder now");
3455 /* now read pages until we receive a buffer from each of the
3456 * stream decoders, this will tell us the timestamp of the
3457 * first packet in the chain then */
3459 /* save the offset to the first non bos page in the chain: if searching for
3460 * pad->first_time we read past the end of the chain, we'll seek back to this
3463 offset = ogg->offset;
3468 gboolean known_serial = FALSE;
3471 serial = ogg_page_serialno (&og);
3473 for (i = 0; i < chain->streams->len; i++) {
3474 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
3476 GST_LOG_OBJECT (ogg,
3477 "serial %08x time %" GST_TIME_FORMAT,
3478 pad->map.serialno, GST_TIME_ARGS (pad->start_time));
3480 if (pad->map.serialno == serial) {
3481 known_serial = TRUE;
3483 /* submit the page now, this will fill in the start_time when the
3484 * internal decoder finds it */
3485 gst_ogg_pad_submit_page (pad, &og);
3487 if (!pad->map.is_skeleton && pad->start_time == -1
3488 && ogg_page_eos (&og)) {
3489 /* got EOS on a pad before we could find its start_time.
3490 * We have no chance of finding a start_time for every pad so
3491 * stop searching for the other start_time(s).
3497 /* the timestamp will be filled in when we submit the pages */
3498 if (!pad->map.is_sparse)
3499 done &= (pad->start_time != GST_CLOCK_TIME_NONE);
3501 GST_LOG_OBJECT (ogg, "done %08x now %d", pad->map.serialno, done);
3504 /* we read a page not belonging to the current chain: seek back to the
3505 * beginning of the chain
3507 if (!known_serial) {
3508 GST_LOG_OBJECT (ogg, "unknown serial %08x", serial);
3509 gst_ogg_demux_seek (ogg, offset);
3514 ret = gst_ogg_demux_get_next_page (ogg, &og, -1, NULL);
3515 if (ret != GST_FLOW_OK)
3519 GST_LOG_OBJECT (ogg, "done reading chain");
3520 /* now we can fill in the missing info using queries */
3521 for (i = 0; i < chain->streams->len; i++) {
3522 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
3524 if (pad->map.is_skeleton)
3527 pad->mode = GST_OGG_PAD_MODE_STREAMING;
3536 /* read the last pages from the ogg stream to get the final
3539 static GstFlowReturn
3540 gst_ogg_demux_read_end_chain (GstOggDemux * ogg, GstOggChain * chain)
3542 gint64 begin = chain->end_offset;
3544 gint64 last_granule = -1;
3545 GstOggPad *last_pad = NULL;
3547 gboolean done = FALSE;
3556 gst_ogg_demux_seek (ogg, begin);
3558 /* now continue reading until we run out of data, if we find a page
3559 * start, we save it. It might not be the final page as there could be
3560 * another page after this one. */
3561 while (ogg->offset < end) {
3562 ret = gst_ogg_demux_get_next_page (ogg, &og, end - ogg->offset, NULL);
3564 if (ret == GST_FLOW_LIMIT)
3566 if (ret != GST_FLOW_OK)
3569 for (i = 0; i < chain->streams->len; i++) {
3570 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
3572 if (pad->map.is_skeleton)
3575 if (pad->map.serialno == ogg_page_serialno (&og)) {
3576 gint64 granulepos = ogg_page_granulepos (&og);
3578 if (granulepos != -1) {
3579 last_granule = granulepos;
3590 chain->segment_stop =
3591 gst_ogg_stream_get_end_time_for_granulepos (&last_pad->map,
3594 chain->segment_stop = GST_CLOCK_TIME_NONE;
3597 GST_INFO ("segment stop %" G_GUINT64_FORMAT, chain->segment_stop);
3602 /* find a pad with a given serial number
3605 gst_ogg_demux_find_pad (GstOggDemux * ogg, guint32 serialno)
3610 /* first look in building chain if any */
3611 if (ogg->building_chain) {
3612 pad = gst_ogg_chain_get_stream (ogg->building_chain, serialno);
3617 /* then look in current chain if any */
3618 if (ogg->current_chain) {
3619 pad = gst_ogg_chain_get_stream (ogg->current_chain, serialno);
3624 for (i = 0; i < ogg->chains->len; i++) {
3625 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
3627 pad = gst_ogg_chain_get_stream (chain, serialno);
3634 /* find a chain with a given serial number
3636 static GstOggChain *
3637 gst_ogg_demux_find_chain (GstOggDemux * ogg, guint32 serialno)
3641 pad = gst_ogg_demux_find_pad (ogg, serialno);
3648 /* returns TRUE if all streams have valid start time */
3650 gst_ogg_demux_collect_chain_info (GstOggDemux * ogg, GstOggChain * chain)
3652 gboolean res = TRUE;
3654 chain->total_time = GST_CLOCK_TIME_NONE;
3655 GST_DEBUG_OBJECT (ogg, "trying to collect chain info");
3657 /* see if we have a start time on all streams */
3658 chain->segment_start = gst_ogg_demux_collect_start_time (ogg, chain);
3660 if (chain->segment_start == G_MAXUINT64) {
3661 /* not yet, stream some more data */
3663 } else if (chain->segment_stop != GST_CLOCK_TIME_NONE) {
3664 /* we can calculate a total time */
3665 chain->total_time = chain->segment_stop - chain->segment_start;
3668 GST_DEBUG ("total time %" G_GUINT64_FORMAT, chain->total_time);
3670 GST_DEBUG_OBJECT (ogg, "return %d", res);
3676 gst_ogg_demux_collect_info (GstOggDemux * ogg)
3680 /* collect all info */
3681 ogg->total_time = 0;
3683 for (i = 0; i < ogg->chains->len; i++) {
3684 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
3686 chain->begin_time = ogg->total_time;
3688 gst_ogg_demux_collect_chain_info (ogg, chain);
3690 ogg->total_time += chain->total_time;
3692 ogg->segment.duration = ogg->total_time;
3695 /* find all the chains in the ogg file, this reads the first and
3696 * last page of the ogg stream, if they match then the ogg file has
3697 * just one chain, else we do a binary search for all chains.
3699 static GstFlowReturn
3700 gst_ogg_demux_find_chains (GstOggDemux * ogg)
3709 /* get peer to figure out length */
3710 if ((peer = gst_pad_get_peer (ogg->sinkpad)) == NULL)
3713 /* find length to read last page, we store this for later use. */
3714 res = gst_pad_query_duration (peer, GST_FORMAT_BYTES, &ogg->length);
3715 gst_object_unref (peer);
3716 if (!res || ogg->length <= 0)
3719 GST_DEBUG_OBJECT (ogg, "file length %" G_GINT64_FORMAT, ogg->length);
3721 /* read chain from offset 0, this is the first chain of the
3723 gst_ogg_demux_seek (ogg, 0);
3724 ret = gst_ogg_demux_read_chain (ogg, &chain);
3725 if (ret != GST_FLOW_OK)
3726 goto no_first_chain;
3728 /* read page from end offset, we use this page to check if its serial
3729 * number is contained in the first chain. If this is the case then
3730 * this ogg is not a chained ogg and we can skip the scanning. */
3731 gst_ogg_demux_seek (ogg, ogg->length);
3732 ret = gst_ogg_demux_get_prev_page (ogg, &og, NULL);
3733 if (ret != GST_FLOW_OK)
3736 serialno = ogg_page_serialno (&og);
3738 if (!gst_ogg_chain_has_stream (chain, serialno)) {
3739 /* the last page is not in the first stream, this means we should
3740 * find all the chains in this chained ogg. */
3742 gst_ogg_demux_bisect_forward_serialno (ogg, 0, 0, ogg->length, chain,
3745 /* we still call this function here but with an empty range so that
3746 * we can reuse the setup code in this routine. */
3748 gst_ogg_demux_bisect_forward_serialno (ogg, 0, ogg->length,
3749 ogg->length, chain, 0);
3751 if (ret != GST_FLOW_OK)
3754 /* all fine, collect and print */
3755 gst_ogg_demux_collect_info (ogg);
3757 /* dump our chains and streams */
3758 gst_ogg_print (ogg);
3763 /*** error cases ***/
3766 GST_ELEMENT_ERROR (ogg, STREAM, DEMUX, (NULL), ("we don't have a peer"));
3767 return GST_FLOW_NOT_LINKED;
3771 GST_ELEMENT_ERROR (ogg, STREAM, DEMUX, (NULL), ("can't get file length"));
3772 return GST_FLOW_NOT_SUPPORTED;
3776 GST_ELEMENT_ERROR (ogg, STREAM, DEMUX, (NULL), ("can't get first chain"));
3777 return GST_FLOW_ERROR;
3781 GST_DEBUG_OBJECT (ogg, "can't get last page");
3783 gst_ogg_chain_free (chain);
3788 static GstFlowReturn
3789 gst_ogg_demux_handle_page (GstOggDemux * ogg, ogg_page * page)
3794 GstFlowReturn result = GST_FLOW_OK;
3796 serialno = ogg_page_serialno (page);
3797 granule = ogg_page_granulepos (page);
3799 GST_LOG_OBJECT (ogg,
3800 "processing ogg page (serial %08x, "
3801 "pageno %ld, granulepos %" G_GINT64_FORMAT ", bos %d)", serialno,
3802 ogg_page_pageno (page), granule, ogg_page_bos (page));
3804 if (ogg_page_bos (page)) {
3808 /* see if we know about the chain already */
3809 chain = gst_ogg_demux_find_chain (ogg, serialno);
3815 if (chain->segment_start != GST_CLOCK_TIME_NONE)
3816 start = chain->segment_start;
3818 /* create the newsegment event we are going to send out */
3819 gst_segment_copy_into (&ogg->segment, &segment);
3820 segment.start = start;
3821 segment.stop = chain->segment_stop;
3822 segment.time = chain->begin_time;
3823 event = gst_event_new_segment (&segment);
3824 gst_event_set_seqnum (event, ogg->seqnum);
3826 GST_DEBUG_OBJECT (ogg,
3827 "segment: start %" GST_TIME_FORMAT ", stop %" GST_TIME_FORMAT
3828 ", time %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
3829 GST_TIME_ARGS (chain->segment_stop),
3830 GST_TIME_ARGS (chain->begin_time));
3832 /* activate it as it means we have a non-header, this will also deactivate
3833 * the currently running chain. */
3834 gst_ogg_demux_activate_chain (ogg, chain, event);
3835 pad = gst_ogg_demux_find_pad (ogg, serialno);
3837 GstClockTime chain_time;
3838 gint64 current_time;
3840 /* this can only happen in push mode */
3844 current_time = ogg->segment.position;
3846 /* time of new chain is current time */
3847 chain_time = current_time;
3849 if (ogg->building_chain == NULL) {
3850 GstOggChain *newchain;
3852 newchain = gst_ogg_chain_new (ogg);
3853 newchain->offset = 0;
3854 /* set new chain begin time aligned with end time of old chain */
3855 newchain->begin_time = chain_time;
3856 GST_DEBUG_OBJECT (ogg, "new chain, begin time %" GST_TIME_FORMAT,
3857 GST_TIME_ARGS (chain_time));
3859 /* and this is the one we are building now */
3860 ogg->building_chain = newchain;
3862 pad = gst_ogg_chain_new_stream (ogg->building_chain, serialno);
3865 pad = gst_ogg_demux_find_pad (ogg, serialno);
3868 result = gst_ogg_pad_submit_page (pad, page);
3870 GST_PUSH_LOCK (ogg);
3871 if (!ogg->pullmode && !ogg->push_disable_seeking) {
3872 /* no pad while probing for duration, we must have a chained stream,
3873 and we don't support them, so back off */
3874 GST_INFO_OBJECT (ogg, "We seem to have a chained stream, we won't seek");
3875 if (ogg->push_state == PUSH_DURATION) {
3878 res = gst_ogg_demux_seek_back_after_push_duration_check_unlock (ogg);
3879 if (res != GST_FLOW_OK)
3883 /* only once we seeked back */
3884 GST_PUSH_LOCK (ogg);
3885 ogg->push_disable_seeking = TRUE;
3887 GST_PUSH_UNLOCK (ogg);
3888 /* no pad. This means an ogg page without bos has been seen for this
3889 * serialno. we just ignore it but post a warning... */
3890 GST_ELEMENT_WARNING (ogg, STREAM, DECODE,
3891 (NULL), ("unknown ogg pad for serial %08x detected", serialno));
3894 GST_PUSH_UNLOCK (ogg);
3901 GST_ELEMENT_ERROR (ogg, STREAM, DECODE,
3902 (NULL), ("unknown ogg chain for serial %08x detected", serialno));
3903 return GST_FLOW_ERROR;
3907 /* streaming mode, receive a buffer, parse it, create pads for
3908 * the serialno, submit pages and packets to the oggpads
3910 static GstFlowReturn
3911 gst_ogg_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
3915 GstFlowReturn result = GST_FLOW_OK;
3917 ogg = GST_OGG_DEMUX (parent);
3919 GST_DEBUG_OBJECT (ogg, "enter");
3920 result = gst_ogg_demux_submit_buffer (ogg, buffer);
3922 GST_DEBUG_OBJECT (ogg, "gst_ogg_demux_submit_buffer returned %d", result);
3925 while (result == GST_FLOW_OK) {
3928 ret = ogg_sync_pageout (&ogg->sync, &page);
3930 /* need more data */
3933 /* discontinuity in the pages */
3934 GST_DEBUG_OBJECT (ogg, "discont in page found, continuing");
3936 result = gst_ogg_demux_handle_page (ogg, &page);
3938 GST_DEBUG_OBJECT (ogg, "gst_ogg_demux_handle_page returned %d", result);
3942 if (ret == 0 || result == GST_FLOW_OK) {
3943 gst_ogg_demux_sync_streams (ogg);
3945 GST_DEBUG_OBJECT (ogg, "leave with %d", result);
3950 gst_ogg_demux_send_event (GstOggDemux * ogg, GstEvent * event)
3952 GstOggChain *chain = ogg->current_chain;
3953 gboolean res = TRUE;
3956 chain = ogg->building_chain;
3961 for (i = 0; i < chain->streams->len; i++) {
3962 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
3964 gst_event_ref (event);
3965 GST_DEBUG_OBJECT (pad, "Pushing event %" GST_PTR_FORMAT, event);
3966 res &= gst_pad_push_event (GST_PAD (pad), event);
3969 GST_WARNING_OBJECT (ogg, "No chain to forward event to");
3971 gst_event_unref (event);
3976 static GstFlowReturn
3977 gst_ogg_demux_combine_flows (GstOggDemux * ogg, GstOggPad * pad,
3982 /* store the value */
3983 pad->last_ret = ret;
3985 /* any other error that is not-linked can be returned right
3987 if (ret != GST_FLOW_NOT_LINKED)
3990 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3991 chain = ogg->current_chain;
3995 for (i = 0; i < chain->streams->len; i++) {
3996 GstOggPad *opad = g_array_index (chain->streams, GstOggPad *, i);
3998 ret = opad->last_ret;
3999 /* some other return value (must be SUCCESS but we can return
4000 * other values as well) */
4001 if (ret != GST_FLOW_NOT_LINKED)
4004 /* if we get here, all other pads were unlinked and we return
4005 * NOT_LINKED then */
4011 /* returns TRUE if all streams in current chain reached EOS, FALSE otherwise */
4013 gst_ogg_demux_check_eos (GstOggDemux * ogg)
4016 gboolean eos = TRUE;
4018 chain = ogg->current_chain;
4019 if (G_LIKELY (chain)) {
4022 for (i = 0; i < chain->streams->len; i++) {
4023 GstOggPad *opad = g_array_index (chain->streams, GstOggPad *, i);
4025 eos = eos && opad->is_eos;
4034 static GstFlowReturn
4035 gst_ogg_demux_loop_forward (GstOggDemux * ogg)
4040 if (ogg->offset == ogg->length) {
4041 GST_LOG_OBJECT (ogg, "no more data to pull %" G_GINT64_FORMAT
4042 " == %" G_GINT64_FORMAT, ogg->offset, ogg->length);
4047 GST_LOG_OBJECT (ogg, "pull data %" G_GINT64_FORMAT, ogg->offset);
4048 ret = gst_pad_pull_range (ogg->sinkpad, ogg->offset, CHUNKSIZE, &buffer);
4049 if (ret != GST_FLOW_OK) {
4050 GST_LOG_OBJECT (ogg, "Failed pull_range");
4054 ogg->offset += gst_buffer_get_size (buffer);
4056 if (G_UNLIKELY (ogg->newsegment)) {
4057 gst_ogg_demux_send_event (ogg, ogg->newsegment);
4058 ogg->newsegment = NULL;
4061 ret = gst_ogg_demux_chain (ogg->sinkpad, GST_OBJECT_CAST (ogg), buffer);
4062 if (ret != GST_FLOW_OK) {
4063 GST_LOG_OBJECT (ogg, "Failed demux_chain");
4067 /* check for the end of the segment */
4068 if (gst_ogg_demux_check_eos (ogg)) {
4069 GST_LOG_OBJECT (ogg, "got EOS");
4079 * We read the pages backwards and send the packets forwards. The first packet
4080 * in the page will be pushed with the DISCONT flag set.
4082 * Special care has to be taken for continued pages, which we can only decode
4083 * when we have the previous page(s).
4085 static GstFlowReturn
4086 gst_ogg_demux_loop_reverse (GstOggDemux * ogg)
4092 if (ogg->offset == 0) {
4093 GST_LOG_OBJECT (ogg, "no more data to pull %" G_GINT64_FORMAT
4094 " == 0", ogg->offset);
4099 GST_LOG_OBJECT (ogg, "read page from %" G_GINT64_FORMAT, ogg->offset);
4100 ret = gst_ogg_demux_get_prev_page (ogg, &page, &offset);
4101 if (ret != GST_FLOW_OK)
4104 ogg->offset = offset;
4106 if (G_UNLIKELY (ogg->newsegment)) {
4107 gst_ogg_demux_send_event (ogg, ogg->newsegment);
4108 ogg->newsegment = NULL;
4111 ret = gst_ogg_demux_handle_page (ogg, &page);
4112 if (ret != GST_FLOW_OK)
4115 /* check for the end of the segment */
4116 if (gst_ogg_demux_check_eos (ogg)) {
4117 GST_LOG_OBJECT (ogg, "got EOS");
4126 gst_ogg_demux_sync_streams (GstOggDemux * ogg)
4132 chain = ogg->current_chain;
4133 cur = ogg->segment.position;
4134 if (chain == NULL || cur == -1)
4137 for (i = 0; i < chain->streams->len; i++) {
4138 GstOggPad *stream = g_array_index (chain->streams, GstOggPad *, i);
4140 /* Theoretically, we should be doing this for all streams, but we're only
4141 * doing it for known-to-be-sparse streams at the moment in order not to
4142 * break things for wrongly-muxed streams (like we used to produce once) */
4143 if (stream->map.is_sparse && stream->position != GST_CLOCK_TIME_NONE) {
4145 /* Does this stream lag? Random threshold of 2 seconds */
4146 if (GST_CLOCK_DIFF (stream->position, cur) > (2 * GST_SECOND)) {
4147 GST_DEBUG_OBJECT (stream, "synchronizing stream with others by "
4148 "advancing time from %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
4149 GST_TIME_ARGS (stream->position), GST_TIME_ARGS (cur));
4151 stream->position = cur;
4154 ogg->segment.base += cur - stream->position;
4155 /* advance stream time (FIXME: is this right, esp. time_pos?) */
4156 gst_pad_push_event (GST_PAD_CAST (stream),
4157 gst_event_new_new_segment (TRUE, ogg->segment.rate,
4158 ogg->segment.applied_rate,
4159 GST_FORMAT_TIME, stream->position, -1, stream->position));
4166 /* random access code
4168 * - first find all the chains and streams by scanning the file.
4169 * - then get and chain buffers, just like the streaming case.
4170 * - when seeking, we can use the chain info to perform the seek.
4173 gst_ogg_demux_loop (GstOggPad * pad)
4179 ogg = GST_OGG_DEMUX (GST_OBJECT_PARENT (pad));
4181 if (ogg->need_chains) {
4184 /* this is the only place where we write chains and thus need to lock. */
4185 GST_CHAIN_LOCK (ogg);
4186 ret = gst_ogg_demux_find_chains (ogg);
4187 GST_CHAIN_UNLOCK (ogg);
4188 if (ret != GST_FLOW_OK)
4189 goto chain_read_failed;
4191 ogg->need_chains = FALSE;
4193 GST_OBJECT_LOCK (ogg);
4194 ogg->running = TRUE;
4197 GST_OBJECT_UNLOCK (ogg);
4199 /* and seek to configured positions without FLUSH */
4200 res = gst_ogg_demux_perform_seek_pull (ogg, event);
4202 gst_event_unref (event);
4208 if (ogg->segment.rate >= 0.0)
4209 ret = gst_ogg_demux_loop_forward (ogg);
4211 ret = gst_ogg_demux_loop_reverse (ogg);
4213 if (ret != GST_FLOW_OK)
4216 gst_ogg_demux_sync_streams (ogg);
4222 /* error was posted */
4227 GST_ELEMENT_ERROR (ogg, STREAM, DEMUX, (NULL),
4228 ("failed to start demuxing ogg"));
4229 ret = GST_FLOW_ERROR;
4234 const gchar *reason = gst_flow_get_name (ret);
4235 GstEvent *event = NULL;
4237 GST_LOG_OBJECT (ogg, "pausing task, reason %s", reason);
4238 gst_pad_pause_task (ogg->sinkpad);
4240 if (ret == GST_FLOW_EOS) {
4241 /* perform EOS logic */
4242 if (ogg->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4244 GstMessage *message;
4246 /* for segment playback we need to post when (in stream time)
4247 * we stopped, this is either stop (when set) or the duration. */
4248 if ((stop = ogg->segment.stop) == -1)
4249 stop = ogg->segment.duration;
4251 GST_LOG_OBJECT (ogg, "Sending segment done, at end of segment");
4253 gst_message_new_segment_done (GST_OBJECT (ogg), GST_FORMAT_TIME,
4255 gst_message_set_seqnum (message, ogg->seqnum);
4257 gst_element_post_message (GST_ELEMENT (ogg), message);
4259 /* normal playback, send EOS to all linked pads */
4260 GST_LOG_OBJECT (ogg, "Sending EOS, at end of stream");
4261 event = gst_event_new_eos ();
4263 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
4264 GST_ELEMENT_ERROR (ogg, STREAM, FAILED,
4265 (_("Internal data stream error.")),
4266 ("stream stopped, reason %s", reason));
4267 event = gst_event_new_eos ();
4270 /* For wrong-state we still want to pause the task and stop
4271 * but no error message or other things are necessary.
4272 * wrong-state is no real error and will be caused by flushing,
4273 * e.g. because of a flushing seek.
4276 gst_event_set_seqnum (event, ogg->seqnum);
4277 gst_ogg_demux_send_event (ogg, event);
4284 gst_ogg_demux_clear_chains (GstOggDemux * ogg)
4288 gst_ogg_demux_deactivate_current_chain (ogg);
4290 GST_CHAIN_LOCK (ogg);
4291 for (i = 0; i < ogg->chains->len; i++) {
4292 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
4294 gst_ogg_chain_free (chain);
4296 ogg->chains = g_array_set_size (ogg->chains, 0);
4297 GST_CHAIN_UNLOCK (ogg);
4300 /* this function is called when the pad is activated and should start
4303 * We check if we can do random access to decide if we work push or
4307 gst_ogg_demux_sink_activate (GstPad * sinkpad, GstObject * parent)
4312 query = gst_query_new_scheduling ();
4314 if (!gst_pad_peer_query (sinkpad, query)) {
4315 gst_query_unref (query);
4319 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
4320 gst_query_unref (query);
4325 GST_DEBUG_OBJECT (sinkpad, "activating pull");
4326 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
4330 GST_DEBUG_OBJECT (sinkpad, "activating push");
4331 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
4336 gst_ogg_demux_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
4337 GstPadMode mode, gboolean active)
4342 ogg = GST_OGG_DEMUX (parent);
4345 case GST_PAD_MODE_PUSH:
4346 ogg->pullmode = FALSE;
4347 ogg->resync = FALSE;
4350 case GST_PAD_MODE_PULL:
4352 ogg->need_chains = TRUE;
4353 ogg->pullmode = TRUE;
4355 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_ogg_demux_loop,
4358 res = gst_pad_stop_task (sinkpad);
4368 static GstStateChangeReturn
4369 gst_ogg_demux_change_state (GstElement * element, GstStateChange transition)
4372 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
4374 ogg = GST_OGG_DEMUX (element);
4376 switch (transition) {
4377 case GST_STATE_CHANGE_NULL_TO_READY:
4379 ogg_sync_init (&ogg->sync);
4381 case GST_STATE_CHANGE_READY_TO_PAUSED:
4382 ogg_sync_reset (&ogg->sync);
4383 ogg->running = FALSE;
4385 ogg->total_time = -1;
4386 GST_PUSH_LOCK (ogg);
4387 ogg->push_byte_offset = 0;
4388 ogg->push_byte_length = -1;
4389 ogg->push_time_length = GST_CLOCK_TIME_NONE;
4390 ogg->push_time_offset = GST_CLOCK_TIME_NONE;
4391 ogg->push_disable_seeking = FALSE;
4392 ogg->push_state = PUSH_PLAYING;
4393 GST_PUSH_UNLOCK (ogg);
4394 gst_segment_init (&ogg->segment, GST_FORMAT_TIME);
4396 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4402 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4404 switch (transition) {
4405 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4407 case GST_STATE_CHANGE_PAUSED_TO_READY:
4408 gst_ogg_demux_clear_chains (ogg);
4409 GST_OBJECT_LOCK (ogg);
4410 ogg->running = FALSE;
4411 GST_OBJECT_UNLOCK (ogg);
4413 case GST_STATE_CHANGE_READY_TO_NULL:
4414 ogg_sync_clear (&ogg->sync);
4423 gst_ogg_demux_plugin_init (GstPlugin * plugin)
4425 GST_DEBUG_CATEGORY_INIT (gst_ogg_demux_debug, "oggdemux", 0, "ogg demuxer");
4426 GST_DEBUG_CATEGORY_INIT (gst_ogg_demux_setup_debug, "oggdemux_setup", 0,
4427 "ogg demuxer setup stage when parsing pipeline");
4430 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
4432 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
4433 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
4436 return gst_element_register (plugin, "oggdemux", GST_RANK_PRIMARY,
4437 GST_TYPE_OGG_DEMUX);
4440 /* prints all info about the element */
4441 #undef GST_CAT_DEFAULT
4442 #define GST_CAT_DEFAULT gst_ogg_demux_setup_debug
4444 #ifdef GST_DISABLE_GST_DEBUG
4447 gst_ogg_print (GstOggDemux * ogg)
4452 #else /* !GST_DISABLE_GST_DEBUG */
4455 gst_ogg_print (GstOggDemux * ogg)
4459 GST_INFO_OBJECT (ogg, "%u chains", ogg->chains->len);
4460 GST_INFO_OBJECT (ogg, " total time: %" GST_TIME_FORMAT,
4461 GST_TIME_ARGS (ogg->total_time));
4463 for (i = 0; i < ogg->chains->len; i++) {
4464 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
4466 GST_INFO_OBJECT (ogg, " chain %d (%u streams):", i, chain->streams->len);
4467 GST_INFO_OBJECT (ogg,
4468 " offset: %" G_GINT64_FORMAT " - %" G_GINT64_FORMAT, chain->offset,
4470 GST_INFO_OBJECT (ogg, " begin time: %" GST_TIME_FORMAT,
4471 GST_TIME_ARGS (chain->begin_time));
4472 GST_INFO_OBJECT (ogg, " total time: %" GST_TIME_FORMAT,
4473 GST_TIME_ARGS (chain->total_time));
4474 GST_INFO_OBJECT (ogg, " segment start: %" GST_TIME_FORMAT,
4475 GST_TIME_ARGS (chain->segment_start));
4476 GST_INFO_OBJECT (ogg, " segment stop: %" GST_TIME_FORMAT,
4477 GST_TIME_ARGS (chain->segment_stop));
4479 for (j = 0; j < chain->streams->len; j++) {
4480 GstOggPad *stream = g_array_index (chain->streams, GstOggPad *, j);
4482 GST_INFO_OBJECT (ogg, " stream %08x: %s", stream->map.serialno,
4483 gst_ogg_stream_get_media_type (&stream->map));
4484 GST_INFO_OBJECT (ogg, " start time: %" GST_TIME_FORMAT,
4485 GST_TIME_ARGS (stream->start_time));
4489 #endif /* GST_DISABLE_GST_DEBUG */