c2e048f8c9142797750bcb65dcc84de0ae6506e6
[platform/upstream/gstreamer.git] / ext / smoothstreaming / gstmssdemux.c
1 /* GStreamer
2  * Copyright (C) 2012 Smart TV Alliance
3  *  Author: Thiago Sousa Santos <thiago.sousa.santos@collabora.com>, Collabora Ltd.
4  *
5  * gstmssdemux.c:
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /**
24  * SECTION:element-mssdemux
25  *
26  * Demuxes a Microsoft's Smooth Streaming manifest into its audio and/or video streams.
27  *
28  *
29  */
30
31 /*
32  * == Internals
33  *
34  * = Smooth streaming in a few lines
35  * A SS stream is defined by a xml manifest file. This file has a list of
36  * tracks (StreamIndex), each one can have multiple QualityLevels, that define
37  * different encoding/bitrates. When playing a track, only one of those
38  * QualityLevels can be active at a time (per stream).
39  *
40  * The StreamIndex defines a URL with {time} and {bitrate} tags that are
41  * replaced by values indicated by the fragment start times and the selected
42  * QualityLevel, that generates the fragments URLs.
43  *
44  * Another relevant detail is that the Isomedia fragments for smoothstreaming
45  * won't contains a 'moov' atom, nor a 'stsd', so there is no information
46  * about the media type/configuration on the fragments, it must be extracted
47  * from the Manifest and passed downstream. mssdemux does this via GstCaps.
48  *
49  * = How mssdemux works
50  * There is a gstmssmanifest.c utility that holds the manifest and parses
51  * and has functions to extract information from it. mssdemux received the
52  * manifest from its sink pad and starts processing it when it gets EOS.
53  *
54  * The Manifest is parsed and the streams are exposed, 1 pad for each, with
55  * a initially selected QualityLevel. Each stream starts its own GstTaks that
56  * is responsible for downloading fragments and storing in its own GstDataQueue.
57  *
58  * The mssdemux starts another GstTask, this one iterates through the streams
59  * and selects the fragment with the smaller timestamp to push and repeats this.
60  *
61  * When a new connection-speed is set, mssdemux evaluates the available
62  * QualityLevels and might decide to switch to another one. In this case it
63  * exposes new pads for each stream, pushes EOS to the old ones and removes
64  * them. This should make decodebin2 pad switching mechanism act and the
65  * switch would be smooth for the final user.
66  */
67
68 #ifdef HAVE_CONFIG_H
69 #include "config.h"
70 #endif
71
72 #include "gst/gst-i18n-plugin.h"
73
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <string.h>
77
78 #include "gstmssdemux.h"
79
80 GST_DEBUG_CATEGORY (mssdemux_debug);
81
82 #define DEFAULT_CONNECTION_SPEED 0
83 #define DEFAULT_MAX_QUEUE_SIZE_BUFFERS 0
84
85 enum
86 {
87   PROP_0,
88
89   PROP_CONNECTION_SPEED,
90   PROP_MAX_QUEUE_SIZE_BUFFERS,
91   PROP_LAST
92 };
93
94 static GstStaticPadTemplate gst_mss_demux_sink_template =
95 GST_STATIC_PAD_TEMPLATE ("sink",
96     GST_PAD_SINK,
97     GST_PAD_ALWAYS,
98     GST_STATIC_CAPS ("application/vnd.ms-sstr+xml")
99     );
100
101 static GstStaticPadTemplate gst_mss_demux_videosrc_template =
102 GST_STATIC_PAD_TEMPLATE ("video_%02u",
103     GST_PAD_SRC,
104     GST_PAD_SOMETIMES,
105     GST_STATIC_CAPS_ANY);
106
107 static GstStaticPadTemplate gst_mss_demux_audiosrc_template =
108 GST_STATIC_PAD_TEMPLATE ("audio_%02u",
109     GST_PAD_SRC,
110     GST_PAD_SOMETIMES,
111     GST_STATIC_CAPS_ANY);
112
113 GST_BOILERPLATE (GstMssDemux, gst_mss_demux, GstMssDemux, GST_TYPE_ELEMENT);
114
115 static void gst_mss_demux_dispose (GObject * object);
116 static void gst_mss_demux_set_property (GObject * object, guint prop_id,
117     const GValue * value, GParamSpec * pspec);
118 static void gst_mss_demux_get_property (GObject * object, guint prop_id,
119     GValue * value, GParamSpec * pspec);
120 static GstStateChangeReturn gst_mss_demux_change_state (GstElement * element,
121     GstStateChange transition);
122 static GstFlowReturn gst_mss_demux_chain (GstPad * pad, GstBuffer * buffer);
123 static GstFlowReturn gst_mss_demux_event (GstPad * pad, GstEvent * event);
124
125 static gboolean gst_mss_demux_src_query (GstPad * pad, GstQuery * query);
126
127 static void gst_mss_demux_download_loop (GstMssDemuxStream * stream);
128 static void gst_mss_demux_stream_loop (GstMssDemux * mssdemux);
129
130 static gboolean gst_mss_demux_process_manifest (GstMssDemux * mssdemux);
131
132 static void
133 gst_mss_demux_base_init (gpointer klass)
134 {
135   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
136
137   gst_element_class_add_static_pad_template (element_class,
138       &gst_mss_demux_sink_template);
139   gst_element_class_add_static_pad_template (element_class,
140       &gst_mss_demux_videosrc_template);
141   gst_element_class_add_static_pad_template (element_class,
142       &gst_mss_demux_audiosrc_template);
143   gst_element_class_set_details_simple (element_class, "Smooth Streaming "
144       "demuxer", "Demuxer",
145       "Parse and demultiplex a Smooth Streaming manifest into audio and video "
146       "streams", "Thiago Santos <thiago.sousa.santos@collabora.com>");
147
148   GST_DEBUG_CATEGORY_INIT (mssdemux_debug, "mssdemux", 0, "mssdemux plugin");
149 }
150
151 static void
152 gst_mss_demux_class_init (GstMssDemuxClass * klass)
153 {
154   GObjectClass *gobject_class;
155   GstElementClass *gstelement_class;
156
157   gobject_class = (GObjectClass *) klass;
158   gstelement_class = (GstElementClass *) klass;
159
160   gobject_class->dispose = gst_mss_demux_dispose;
161   gobject_class->set_property = gst_mss_demux_set_property;
162   gobject_class->get_property = gst_mss_demux_get_property;
163
164   g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
165       g_param_spec_uint ("connection-speed", "Connection Speed",
166           "Network connection speed in kbps (0 = unknown)",
167           0, G_MAXUINT / 1000, DEFAULT_CONNECTION_SPEED,
168           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169
170   g_object_class_install_property (gobject_class, PROP_MAX_QUEUE_SIZE_BUFFERS,
171       g_param_spec_uint ("max-queue-size-buffers", "Max queue size in buffers",
172           "Maximum buffers that can be stored in each internal stream queue "
173           "(0 = infinite)", 0, G_MAXUINT, DEFAULT_MAX_QUEUE_SIZE_BUFFERS,
174           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175
176   gstelement_class->change_state =
177       GST_DEBUG_FUNCPTR (gst_mss_demux_change_state);
178 }
179
180 static void
181 gst_mss_demux_init (GstMssDemux * mssdemux, GstMssDemuxClass * klass)
182 {
183   mssdemux->sinkpad =
184       gst_pad_new_from_static_template (&gst_mss_demux_sink_template, "sink");
185   gst_pad_set_chain_function (mssdemux->sinkpad,
186       GST_DEBUG_FUNCPTR (gst_mss_demux_chain));
187   gst_pad_set_event_function (mssdemux->sinkpad,
188       GST_DEBUG_FUNCPTR (gst_mss_demux_event));
189   gst_element_add_pad (GST_ELEMENT_CAST (mssdemux), mssdemux->sinkpad);
190
191   g_static_rec_mutex_init (&mssdemux->stream_lock);
192   mssdemux->stream_task =
193       gst_task_create ((GstTaskFunction) gst_mss_demux_stream_loop, mssdemux);
194   gst_task_set_lock (mssdemux->stream_task, &mssdemux->stream_lock);
195
196   mssdemux->data_queue_max_size = DEFAULT_MAX_QUEUE_SIZE_BUFFERS;
197 }
198
199 static gboolean
200 _data_queue_check_full (GstDataQueue * queue, guint visible, guint bytes,
201     guint64 time, gpointer checkdata)
202 {
203   GstMssDemuxStream *stream = checkdata;
204   GstMssDemux *mssdemux = stream->parent;
205
206   if (mssdemux->data_queue_max_size == 0)
207     return FALSE;               /* never full */
208   return visible >= mssdemux->data_queue_max_size;
209 }
210
211 static GstMssDemuxStream *
212 gst_mss_demux_stream_new (GstMssDemux * mssdemux,
213     GstMssStream * manifeststream, GstPad * srcpad)
214 {
215   GstMssDemuxStream *stream;
216
217   stream = g_new0 (GstMssDemuxStream, 1);
218   stream->downloader = gst_uri_downloader_new ();
219   stream->dataqueue = gst_data_queue_new (_data_queue_check_full, stream);
220
221   /* Downloading task */
222   g_static_rec_mutex_init (&stream->download_lock);
223   stream->download_task =
224       gst_task_create ((GstTaskFunction) gst_mss_demux_download_loop, stream);
225   gst_task_set_lock (stream->download_task, &stream->download_lock);
226
227   stream->pad = srcpad;
228   stream->manifest_stream = manifeststream;
229   stream->parent = mssdemux;
230
231   return stream;
232 }
233
234 static void
235 gst_mss_demux_stream_free (GstMssDemuxStream * stream)
236 {
237   if (stream->download_task) {
238     if (GST_TASK_STATE (stream->download_task) != GST_TASK_STOPPED) {
239       GST_DEBUG_OBJECT (stream->parent, "Leaving streaming task %s:%s",
240           GST_DEBUG_PAD_NAME (stream->pad));
241       gst_uri_downloader_cancel (stream->downloader);
242       gst_task_stop (stream->download_task);
243       g_static_rec_mutex_lock (&stream->download_lock);
244       g_static_rec_mutex_unlock (&stream->download_lock);
245       GST_LOG_OBJECT (stream->parent, "Waiting for task to finish");
246       gst_task_join (stream->download_task);
247       GST_LOG_OBJECT (stream->parent, "Finished");
248     }
249     gst_object_unref (stream->download_task);
250     g_static_rec_mutex_free (&stream->download_lock);
251     stream->download_task = NULL;
252   }
253
254   if (stream->pending_newsegment) {
255     gst_event_unref (stream->pending_newsegment);
256     stream->pending_newsegment = NULL;
257   }
258
259
260   if (stream->downloader != NULL) {
261     g_object_unref (stream->downloader);
262     stream->downloader = NULL;
263   }
264   if (stream->dataqueue) {
265     g_object_unref (stream->dataqueue);
266     stream->dataqueue = NULL;
267   }
268   if (stream->pad) {
269     gst_object_unref (stream->pad);
270     stream->pad = NULL;
271   }
272   g_free (stream);
273 }
274
275 static void
276 gst_mss_demux_reset (GstMssDemux * mssdemux)
277 {
278   GSList *iter;
279
280   for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
281     GstMssDemuxStream *stream = iter->data;
282     if (stream->downloader)
283       gst_uri_downloader_cancel (stream->downloader);
284
285     gst_data_queue_set_flushing (stream->dataqueue, TRUE);
286   }
287
288   if (GST_TASK_STATE (mssdemux->stream_task) != GST_TASK_STOPPED) {
289     gst_task_stop (mssdemux->stream_task);
290     g_static_rec_mutex_lock (&mssdemux->stream_lock);
291     g_static_rec_mutex_unlock (&mssdemux->stream_lock);
292     gst_task_join (mssdemux->stream_task);
293   }
294
295   if (mssdemux->manifest_buffer) {
296     gst_buffer_unref (mssdemux->manifest_buffer);
297     mssdemux->manifest_buffer = NULL;
298   }
299
300   for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
301     GstMssDemuxStream *stream = iter->data;
302     gst_element_remove_pad (GST_ELEMENT_CAST (mssdemux), stream->pad);
303     gst_mss_demux_stream_free (stream);
304   }
305   g_slist_free (mssdemux->streams);
306   mssdemux->streams = NULL;
307
308   if (mssdemux->manifest) {
309     gst_mss_manifest_free (mssdemux->manifest);
310     mssdemux->manifest = NULL;
311   }
312
313   mssdemux->n_videos = mssdemux->n_audios = 0;
314   g_free (mssdemux->base_url);
315   mssdemux->base_url = NULL;
316   g_free (mssdemux->manifest_uri);
317   mssdemux->manifest_uri = NULL;
318 }
319
320 static void
321 gst_mss_demux_dispose (GObject * object)
322 {
323   GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (object);
324
325   gst_mss_demux_reset (mssdemux);
326
327   if (mssdemux->stream_task) {
328     gst_object_unref (mssdemux->stream_task);
329     g_static_rec_mutex_free (&mssdemux->stream_lock);
330     mssdemux->stream_task = NULL;
331   }
332
333   G_OBJECT_CLASS (parent_class)->dispose (object);
334 }
335
336 static void
337 gst_mss_demux_set_property (GObject * object, guint prop_id,
338     const GValue * value, GParamSpec * pspec)
339 {
340   GstMssDemux *mssdemux = GST_MSS_DEMUX (object);
341
342   switch (prop_id) {
343     case PROP_CONNECTION_SPEED:
344       GST_OBJECT_LOCK (mssdemux);
345       mssdemux->connection_speed = g_value_get_uint (value) * 1000;
346       mssdemux->update_bitrates = TRUE;
347       GST_DEBUG_OBJECT (mssdemux, "Connection speed set to %llu",
348           mssdemux->connection_speed);
349       GST_OBJECT_UNLOCK (mssdemux);
350       break;
351     case PROP_MAX_QUEUE_SIZE_BUFFERS:
352       mssdemux->data_queue_max_size = g_value_get_uint (value);
353       break;
354     default:
355       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
356       break;
357   }
358 }
359
360 static void
361 gst_mss_demux_get_property (GObject * object, guint prop_id, GValue * value,
362     GParamSpec * pspec)
363 {
364   GstMssDemux *mssdemux = GST_MSS_DEMUX (object);
365
366   switch (prop_id) {
367     case PROP_CONNECTION_SPEED:
368       g_value_set_uint (value, mssdemux->connection_speed / 1000);
369       break;
370     case PROP_MAX_QUEUE_SIZE_BUFFERS:
371       g_value_set_uint (value, mssdemux->data_queue_max_size);
372       break;
373     default:
374       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
375       break;
376   }
377 }
378
379 static GstStateChangeReturn
380 gst_mss_demux_change_state (GstElement * element, GstStateChange transition)
381 {
382   GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (element);
383   GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
384
385   switch (transition) {
386     case GST_STATE_CHANGE_PAUSED_TO_READY:
387       gst_mss_demux_reset (mssdemux);
388       break;
389     case GST_STATE_CHANGE_READY_TO_NULL:
390       break;
391     default:
392       break;
393   }
394
395   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
396
397   switch (transition) {
398     case GST_STATE_CHANGE_PAUSED_TO_READY:{
399       break;
400     }
401     default:
402       break;
403   }
404
405   return result;
406 }
407
408 static GstFlowReturn
409 gst_mss_demux_chain (GstPad * pad, GstBuffer * buffer)
410 {
411   GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (GST_PAD_PARENT (pad));
412   if (mssdemux->manifest_buffer == NULL)
413     mssdemux->manifest_buffer = buffer;
414   else
415     mssdemux->manifest_buffer =
416         gst_buffer_join (mssdemux->manifest_buffer, buffer);
417
418   GST_INFO_OBJECT (mssdemux, "Received manifest buffer, total size is %i bytes",
419       GST_BUFFER_SIZE (mssdemux->manifest_buffer));
420
421   return GST_FLOW_OK;
422 }
423
424 static void
425 gst_mss_demux_start (GstMssDemux * mssdemux)
426 {
427   GSList *iter;
428
429   GST_INFO_OBJECT (mssdemux, "Starting streams' tasks");
430   for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
431     GstMssDemuxStream *stream = iter->data;
432     gst_task_start (stream->download_task);
433   }
434
435   gst_task_start (mssdemux->stream_task);
436 }
437
438 static gboolean
439 gst_mss_demux_push_src_event (GstMssDemux * mssdemux, GstEvent * event)
440 {
441   GSList *iter;
442   gboolean ret = TRUE;
443
444   for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
445     GstMssDemuxStream *stream = iter->data;
446     gst_event_ref (event);
447     ret = ret & gst_pad_push_event (stream->pad, event);
448   }
449   gst_event_unref (event);
450   return ret;
451 }
452
453 static gboolean
454 gst_mss_demux_event (GstPad * pad, GstEvent * event)
455 {
456   GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (GST_PAD_PARENT (pad));
457   gboolean forward = TRUE;
458   gboolean ret = TRUE;
459
460   switch (GST_EVENT_TYPE (event)) {
461     case GST_EVENT_FLUSH_STOP:
462       gst_mss_demux_reset (mssdemux);
463       break;
464     case GST_EVENT_EOS:
465       if (mssdemux->manifest_buffer == NULL) {
466         GST_WARNING_OBJECT (mssdemux, "Received EOS without a manifest.");
467         break;
468       }
469       GST_INFO_OBJECT (mssdemux, "Received EOS");
470
471       if (gst_mss_demux_process_manifest (mssdemux))
472         gst_mss_demux_start (mssdemux);
473       forward = FALSE;
474       break;
475     default:
476       break;
477   }
478
479   if (forward) {
480     ret = gst_pad_event_default (pad, event);
481   } else {
482     gst_event_unref (event);
483   }
484
485   return ret;
486 }
487
488 static void
489 gst_mss_demux_stop_tasks (GstMssDemux * mssdemux, gboolean immediate)
490 {
491   GSList *iter;
492
493   for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
494     GstMssDemuxStream *stream = iter->data;
495
496     gst_data_queue_set_flushing (stream->dataqueue, TRUE);
497
498     if (immediate)
499       gst_uri_downloader_cancel (stream->downloader);
500     gst_task_pause (stream->download_task);
501   }
502   gst_task_pause (mssdemux->stream_task);
503
504   for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
505     GstMssDemuxStream *stream = iter->data;
506     g_static_rec_mutex_lock (&stream->download_lock);
507   }
508   g_static_rec_mutex_lock (&mssdemux->stream_lock);
509 }
510
511 static void
512 gst_mss_demux_restart_tasks (GstMssDemux * mssdemux)
513 {
514   GSList *iter;
515   for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
516     GstMssDemuxStream *stream = iter->data;
517     gst_uri_downloader_reset (stream->downloader);
518     g_static_rec_mutex_unlock (&stream->download_lock);
519   }
520   g_static_rec_mutex_unlock (&mssdemux->stream_lock);
521   for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
522     GstMssDemuxStream *stream = iter->data;
523
524     gst_data_queue_set_flushing (stream->dataqueue, FALSE);
525     gst_task_start (stream->download_task);
526   }
527   gst_task_start (mssdemux->stream_task);
528 }
529
530 static gboolean
531 gst_mss_demux_src_event (GstPad * pad, GstEvent * event)
532 {
533   GstMssDemux *mssdemux;
534
535   mssdemux = GST_MSS_DEMUX (GST_PAD_PARENT (pad));
536
537   switch (event->type) {
538     case GST_EVENT_SEEK:
539     {
540       gdouble rate;
541       GstFormat format;
542       GstSeekFlags flags;
543       GstSeekType start_type, stop_type;
544       gint64 start, stop;
545       GstEvent *newsegment;
546       GSList *iter;
547
548       GST_INFO_OBJECT (mssdemux, "Received GST_EVENT_SEEK");
549
550       gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
551           &stop_type, &stop);
552
553       if (format != GST_FORMAT_TIME)
554         goto not_supported;
555
556       GST_DEBUG_OBJECT (mssdemux,
557           "seek event, rate: %f start: %" GST_TIME_FORMAT " stop: %"
558           GST_TIME_FORMAT, rate, GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
559
560       if (flags & GST_SEEK_FLAG_FLUSH) {
561         GstEvent *flush = gst_event_new_flush_start ();
562         GST_DEBUG_OBJECT (mssdemux, "sending flush start");
563
564         gst_event_set_seqnum (flush, gst_event_get_seqnum (event));
565         gst_mss_demux_push_src_event (mssdemux, flush);
566       }
567
568       gst_mss_demux_stop_tasks (mssdemux, TRUE);
569
570       if (!gst_mss_manifest_seek (mssdemux->manifest, start)) {;
571         GST_WARNING_OBJECT (mssdemux, "Could not find seeked fragment");
572         goto not_supported;
573       }
574
575       newsegment =
576           gst_event_new_new_segment (FALSE, rate, format, start, stop, start);
577       gst_event_set_seqnum (newsegment, gst_event_get_seqnum (event));
578       for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
579         GstMssDemuxStream *stream = iter->data;
580
581         stream->eos = FALSE;
582         gst_data_queue_flush (stream->dataqueue);
583         gst_event_ref (newsegment);
584         gst_event_replace (&stream->pending_newsegment, newsegment);
585       }
586       gst_event_unref (newsegment);
587
588       if (flags & GST_SEEK_FLAG_FLUSH) {
589         GstEvent *flush = gst_event_new_flush_stop ();
590         GST_DEBUG_OBJECT (mssdemux, "sending flush stop");
591
592         gst_event_set_seqnum (flush, gst_event_get_seqnum (event));
593         gst_mss_demux_push_src_event (mssdemux, flush);
594       }
595
596       gst_mss_demux_restart_tasks (mssdemux);
597
598       gst_event_unref (event);
599       return TRUE;
600     }
601     default:
602       break;
603   }
604
605   return gst_pad_event_default (pad, event);
606
607 not_supported:
608   gst_event_unref (event);
609   return FALSE;
610 }
611
612 static gboolean
613 gst_mss_demux_src_query (GstPad * pad, GstQuery * query)
614 {
615   GstMssDemux *mssdemux;
616   gboolean ret = FALSE;
617
618   if (query == NULL)
619     return FALSE;
620
621   mssdemux = GST_MSS_DEMUX (GST_PAD_PARENT (pad));
622
623   switch (query->type) {
624     case GST_QUERY_DURATION:{
625       GstClockTime duration = -1;
626       GstFormat fmt;
627
628       gst_query_parse_duration (query, &fmt, NULL);
629       if (fmt == GST_FORMAT_TIME && mssdemux->manifest) {
630         /* TODO should we use the streams accumulated duration or the main manifest duration? */
631         duration = gst_mss_manifest_get_gst_duration (mssdemux->manifest);
632
633         if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0) {
634           gst_query_set_duration (query, GST_FORMAT_TIME, duration);
635           ret = TRUE;
636         }
637       }
638       GST_INFO_OBJECT (mssdemux, "GST_QUERY_DURATION returns %s with duration %"
639           GST_TIME_FORMAT, ret ? "TRUE" : "FALSE", GST_TIME_ARGS (duration));
640       break;
641     }
642     case GST_QUERY_LATENCY:{
643       gboolean live = FALSE;
644
645       live = mssdemux->manifest
646           && gst_mss_manifest_is_live (mssdemux->manifest);
647
648       gst_query_set_latency (query, live, 0, -1);
649       ret = TRUE;
650       break;
651     }
652     case GST_QUERY_SEEKING:{
653       GstFormat fmt;
654       gint64 stop = -1;
655
656       if (mssdemux->manifest && gst_mss_manifest_is_live (mssdemux->manifest)) {
657         return FALSE;           /* no live seeking */
658       }
659
660       gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
661       GST_INFO_OBJECT (mssdemux, "Received GST_QUERY_SEEKING with format %d",
662           fmt);
663       if (fmt == GST_FORMAT_TIME) {
664         GstClockTime duration;
665         duration = gst_mss_manifest_get_gst_duration (mssdemux->manifest);
666         if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0)
667           stop = duration;
668         gst_query_set_seeking (query, fmt, TRUE, 0, stop);
669         ret = TRUE;
670         GST_INFO_OBJECT (mssdemux, "GST_QUERY_SEEKING returning with stop : %"
671             GST_TIME_FORMAT, GST_TIME_ARGS (stop));
672       }
673       break;
674     }
675     default:
676       /* Don't fordward queries upstream because of the special nature of this
677        *  "demuxer", which relies on the upstream element only to be fed
678        *  the Manifest
679        */
680       break;
681   }
682
683   return ret;
684 }
685
686 static void
687 _set_src_pad_functions (GstPad * pad)
688 {
689   gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_mss_demux_src_query));
690   gst_pad_set_event_function (pad, GST_DEBUG_FUNCPTR (gst_mss_demux_src_event));
691 }
692
693 static GstPad *
694 _create_pad (GstMssDemux * mssdemux, GstMssStream * manifeststream)
695 {
696   gchar *name;
697   GstPad *srcpad = NULL;
698   GstMssStreamType streamtype;
699
700   streamtype = gst_mss_stream_get_type (manifeststream);
701   GST_DEBUG_OBJECT (mssdemux, "Found stream of type: %s",
702       gst_mss_stream_type_name (streamtype));
703
704   /* TODO use stream's name/bitrate/index as the pad name? */
705   if (streamtype == MSS_STREAM_TYPE_VIDEO) {
706     name = g_strdup_printf ("video_%02u", mssdemux->n_videos++);
707     srcpad =
708         gst_pad_new_from_static_template (&gst_mss_demux_videosrc_template,
709         name);
710     g_free (name);
711   } else if (streamtype == MSS_STREAM_TYPE_AUDIO) {
712     name = g_strdup_printf ("audio_%02u", mssdemux->n_audios++);
713     srcpad =
714         gst_pad_new_from_static_template (&gst_mss_demux_audiosrc_template,
715         name);
716     g_free (name);
717   }
718
719   if (!srcpad) {
720     GST_WARNING_OBJECT (mssdemux, "Ignoring unknown type stream");
721     return NULL;
722   }
723
724   _set_src_pad_functions (srcpad);
725   return srcpad;
726 }
727
728 static void
729 gst_mss_demux_create_streams (GstMssDemux * mssdemux)
730 {
731   GSList *streams = gst_mss_manifest_get_streams (mssdemux->manifest);
732   GSList *iter;
733
734   if (streams == NULL) {
735     GST_INFO_OBJECT (mssdemux, "No streams found in the manifest");
736     GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
737         (_("This file contains no playable streams.")),
738         ("no streams found at the Manifest"));
739     return;
740   }
741
742   for (iter = streams; iter; iter = g_slist_next (iter)) {
743     GstPad *srcpad = NULL;
744     GstMssDemuxStream *stream = NULL;
745     GstMssStream *manifeststream = iter->data;
746
747     srcpad = _create_pad (mssdemux, manifeststream);
748
749     if (!srcpad) {
750       continue;
751     }
752
753     stream = gst_mss_demux_stream_new (mssdemux, manifeststream, srcpad);
754     gst_mss_stream_set_active (manifeststream, TRUE);
755     mssdemux->streams = g_slist_append (mssdemux->streams, stream);
756   }
757
758   /* select initial bitrates */
759   GST_OBJECT_LOCK (mssdemux);
760   GST_INFO_OBJECT (mssdemux, "Changing max bitrate to %llu",
761       mssdemux->connection_speed);
762   gst_mss_manifest_change_bitrate (mssdemux->manifest,
763       mssdemux->connection_speed);
764   mssdemux->update_bitrates = FALSE;
765   GST_OBJECT_UNLOCK (mssdemux);
766 }
767
768 static gboolean
769 gst_mss_demux_expose_stream (GstMssDemux * mssdemux, GstMssDemuxStream * stream)
770 {
771   GstCaps *caps;
772   GstCaps *media_caps;
773   GstPad *pad = stream->pad;
774
775   media_caps = gst_mss_stream_get_caps (stream->manifest_stream);
776
777   if (media_caps) {
778     caps = gst_caps_new_simple ("video/quicktime", "variant", G_TYPE_STRING,
779         "mss-fragmented", "timescale", G_TYPE_UINT64,
780         gst_mss_stream_get_timescale (stream->manifest_stream), "media-caps",
781         GST_TYPE_CAPS, media_caps, NULL);
782     gst_caps_unref (media_caps);
783     gst_pad_set_caps (pad, caps);
784     gst_caps_unref (caps);
785
786     gst_pad_set_active (pad, TRUE);
787     GST_INFO_OBJECT (mssdemux, "Adding srcpad %s:%s with caps %" GST_PTR_FORMAT,
788         GST_DEBUG_PAD_NAME (pad), caps);
789     gst_object_ref (pad);
790     gst_element_add_pad (GST_ELEMENT_CAST (mssdemux), pad);
791   } else {
792     GST_WARNING_OBJECT (mssdemux,
793         "Couldn't get caps from manifest stream %p %s, not exposing it", stream,
794         GST_PAD_NAME (stream->pad));
795     return FALSE;
796   }
797   return TRUE;
798 }
799
800 static gboolean
801 gst_mss_demux_process_manifest (GstMssDemux * mssdemux)
802 {
803   GstQuery *query;
804   gchar *uri = NULL;
805   gboolean ret;
806   GSList *iter;
807
808   g_return_val_if_fail (mssdemux->manifest_buffer != NULL, FALSE);
809   g_return_val_if_fail (mssdemux->manifest == NULL, FALSE);
810
811   query = gst_query_new_uri ();
812   ret = gst_pad_peer_query (mssdemux->sinkpad, query);
813   if (ret) {
814     gchar *baseurl_end;
815     gst_query_parse_uri (query, &uri);
816     GST_INFO_OBJECT (mssdemux, "Upstream is using URI: %s", uri);
817
818     mssdemux->manifest_uri = g_strdup (uri);
819     baseurl_end = g_strrstr (uri, "/Manifest");
820     if (baseurl_end) {
821       /* set the new end of the string */
822       baseurl_end[0] = '\0';
823     } else {
824       GST_WARNING_OBJECT (mssdemux, "Stream's URI didn't end with /manifest");
825     }
826
827     mssdemux->base_url = uri;
828   }
829   gst_query_unref (query);
830
831   if (mssdemux->base_url == NULL) {
832     GST_ELEMENT_ERROR (mssdemux, RESOURCE, NOT_FOUND,
833         (_("Couldn't get the Manifest's URI")),
834         ("need to get the manifest's URI from upstream elements"));
835     return FALSE;
836   }
837
838   GST_INFO_OBJECT (mssdemux, "Received manifest: %i bytes",
839       GST_BUFFER_SIZE (mssdemux->manifest_buffer));
840
841   mssdemux->manifest = gst_mss_manifest_new (mssdemux->manifest_buffer);
842   if (!mssdemux->manifest) {
843     GST_ELEMENT_ERROR (mssdemux, STREAM, FORMAT, ("Bad manifest file"),
844         ("Xml manifest file couldn't be parsed"));
845     return FALSE;
846   }
847
848   GST_INFO_OBJECT (mssdemux, "Live stream: %d",
849       gst_mss_manifest_is_live (mssdemux->manifest));
850
851   gst_mss_demux_create_streams (mssdemux);
852   for (iter = mssdemux->streams; iter;) {
853     GSList *current = iter;
854     GstMssDemuxStream *stream = iter->data;
855     iter = g_slist_next (iter); /* do it ourselves as we want it done in the beginning of the loop */
856     if (!gst_mss_demux_expose_stream (mssdemux, stream)) {
857       gst_mss_demux_stream_free (stream);
858       mssdemux->streams = g_slist_delete_link (mssdemux->streams, current);
859     }
860   }
861
862   if (!mssdemux->streams) {
863     /* no streams */
864     GST_WARNING_OBJECT (mssdemux, "Couldn't identify the caps for any of the "
865         "streams found in the manifest");
866     GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
867         (_("This file contains no playable streams.")),
868         ("No known stream formats found at the Manifest"));
869     return FALSE;
870   }
871
872   gst_element_no_more_pads (GST_ELEMENT_CAST (mssdemux));
873   return TRUE;
874 }
875
876 static void
877 gst_mss_demux_reload_manifest (GstMssDemux * mssdemux)
878 {
879   GstUriDownloader *downloader;
880   GstFragment *manifest_data;
881   GstBuffer *manifest_buffer;
882
883   downloader = gst_uri_downloader_new ();
884
885   manifest_data =
886       gst_uri_downloader_fetch_uri (downloader, mssdemux->manifest_uri);
887   manifest_buffer = gst_fragment_get_buffer (manifest_data);
888   g_object_unref (manifest_data);
889
890   gst_mss_manifest_reload_fragments (mssdemux->manifest, manifest_buffer);
891   gst_buffer_replace (&mssdemux->manifest_buffer, manifest_buffer);
892   gst_buffer_unref (manifest_buffer);
893
894   g_object_unref (downloader);
895 }
896
897 static void
898 gst_mss_demux_reconfigure (GstMssDemux * mssdemux)
899 {
900   GSList *oldpads = NULL;
901   GSList *iter;
902
903   gst_mss_demux_stop_tasks (mssdemux, TRUE);
904   if (gst_mss_manifest_change_bitrate (mssdemux->manifest,
905           mssdemux->connection_speed)) {
906     GstClockTime newseg_ts = GST_CLOCK_TIME_NONE;
907
908     GST_DEBUG_OBJECT (mssdemux, "Creating new pad group");
909     /* if we changed the bitrate, we need to add new pads */
910     for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
911       GstMssDemuxStream *stream = iter->data;
912       GstPad *oldpad = stream->pad;
913       GstClockTime ts = GST_CLOCK_TIME_NONE;
914
915       oldpads = g_slist_prepend (oldpads, oldpad);
916
917       /* since we are flushing the queue, get the next un-pushed timestamp to seek
918        * and avoid gaps */
919       gst_data_queue_set_flushing (stream->dataqueue, FALSE);
920       if (!gst_data_queue_is_empty (stream->dataqueue)) {
921         GstDataQueueItem *item = NULL;
922
923         while (!gst_data_queue_is_empty (stream->dataqueue)
924             && !GST_CLOCK_TIME_IS_VALID (ts)) {
925           gst_data_queue_pop (stream->dataqueue, &item);
926
927           if (!item) {
928             g_assert_not_reached ();
929             break;
930           }
931
932           if (GST_IS_BUFFER (item->object)) {
933             GstBuffer *buffer = GST_BUFFER_CAST (item->object);
934
935             ts = GST_BUFFER_TIMESTAMP (buffer);
936           }
937           item->destroy (item);
938         }
939
940       }
941       if (!GST_CLOCK_TIME_IS_VALID (ts)) {
942         ts = gst_mss_stream_get_fragment_gst_timestamp
943             (stream->manifest_stream);
944       }
945
946       if (ts < newseg_ts)
947         newseg_ts = ts;
948
949       GST_DEBUG_OBJECT (mssdemux,
950           "Seeking stream %p %s to ts %" GST_TIME_FORMAT, stream,
951           GST_PAD_NAME (stream->pad), GST_TIME_ARGS (ts));
952       gst_mss_stream_seek (stream->manifest_stream, ts);
953       gst_data_queue_flush (stream->dataqueue);
954
955       stream->pad = _create_pad (mssdemux, stream->manifest_stream);
956       gst_mss_demux_expose_stream (mssdemux, stream);
957
958       gst_pad_push_event (oldpad, gst_event_new_eos ());
959     }
960
961     gst_element_no_more_pads (GST_ELEMENT (mssdemux));
962
963     for (iter = oldpads; iter; iter = g_slist_next (iter)) {
964       GstPad *oldpad = iter->data;
965
966       gst_pad_set_active (oldpad, FALSE);
967       gst_element_remove_pad (GST_ELEMENT (mssdemux), oldpad);
968       gst_object_unref (oldpad);
969     }
970     for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
971       GstMssDemuxStream *stream = iter->data;
972
973       stream->pending_newsegment =
974           gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, newseg_ts, -1,
975           newseg_ts);
976     }
977   }
978   gst_mss_demux_restart_tasks (mssdemux);
979 }
980
981 static void
982 _free_data_queue_item (gpointer obj)
983 {
984   GstDataQueueItem *item = obj;
985
986   gst_mini_object_unref (item->object);
987   g_slice_free (GstDataQueueItem, item);
988 }
989
990 static void
991 gst_mss_demux_stream_store_object (GstMssDemuxStream * stream,
992     GstMiniObject * obj)
993 {
994   GstDataQueueItem *item;
995
996   item = g_slice_new (GstDataQueueItem);
997   item->object = (GstMiniObject *) obj;
998
999   item->duration = 0;           /* we don't care */
1000   item->size = 0;
1001   item->visible = TRUE;
1002
1003   item->destroy = (GDestroyNotify) _free_data_queue_item;
1004
1005   if (!gst_data_queue_push (stream->dataqueue, item)) {
1006     GST_DEBUG_OBJECT (stream->parent, "Failed to store object %p", obj);
1007     item->destroy (item);
1008   }
1009 }
1010
1011 static GstFlowReturn
1012 gst_mss_demux_stream_download_fragment (GstMssDemuxStream * stream,
1013     GstBuffer ** buffer)
1014 {
1015   GstMssDemux *mssdemux = stream->parent;
1016   gchar *path;
1017   gchar *url;
1018   GstFragment *fragment;
1019   GstBuffer *_buffer;
1020   GstFlowReturn ret = GST_FLOW_OK;
1021
1022   GST_DEBUG_OBJECT (mssdemux, "Getting url for stream %p", stream);
1023   ret = gst_mss_stream_get_fragment_url (stream->manifest_stream, &path);
1024   switch (ret) {
1025     case GST_FLOW_OK:
1026       break;                    /* all is good, let's go */
1027     case GST_FLOW_UNEXPECTED:  /* EOS */
1028       if (gst_mss_manifest_is_live (mssdemux->manifest)) {
1029         gst_mss_demux_reload_manifest (mssdemux);
1030         return GST_FLOW_OK;
1031       }
1032       return GST_FLOW_UNEXPECTED;
1033     case GST_FLOW_ERROR:
1034       goto error;
1035     default:
1036       break;
1037   }
1038   if (!path) {
1039     goto no_url_error;
1040   }
1041   GST_DEBUG_OBJECT (mssdemux, "Got url path '%s' for stream %p", path, stream);
1042
1043   url = g_strdup_printf ("%s/%s", mssdemux->base_url, path);
1044
1045   GST_DEBUG_OBJECT (mssdemux, "Got url '%s' for stream %p", url, stream);
1046
1047   fragment = gst_uri_downloader_fetch_uri (stream->downloader, url);
1048   g_free (path);
1049   g_free (url);
1050
1051   if (!fragment) {
1052     GST_INFO_OBJECT (mssdemux, "No fragment downloaded");
1053     /* TODO check if we are truly stoping */
1054     if (gst_mss_manifest_is_live (mssdemux->manifest)) {
1055       /* looks like there is no way of knowing when a live stream has ended
1056        * Have to assume we are falling behind and cause a manifest reload */
1057       return GST_FLOW_OK;
1058     }
1059     return GST_FLOW_ERROR;
1060   }
1061
1062   _buffer = gst_fragment_get_buffer (fragment);
1063   _buffer = gst_buffer_make_metadata_writable (_buffer);
1064   gst_buffer_set_caps (_buffer, GST_PAD_CAPS (stream->pad));
1065   GST_BUFFER_TIMESTAMP (_buffer) =
1066       gst_mss_stream_get_fragment_gst_timestamp (stream->manifest_stream);
1067   GST_BUFFER_DURATION (_buffer) =
1068       gst_mss_stream_get_fragment_gst_duration (stream->manifest_stream);
1069
1070   g_object_unref (fragment);
1071
1072   if (buffer)
1073     *buffer = _buffer;
1074
1075   if (_buffer) {
1076     GST_DEBUG_OBJECT (mssdemux,
1077         "Storing buffer for stream %p - %s. Timestamp: %" GST_TIME_FORMAT
1078         " Duration: %" GST_TIME_FORMAT,
1079         stream, GST_PAD_NAME (stream->pad),
1080         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (_buffer)),
1081         GST_TIME_ARGS (GST_BUFFER_DURATION (_buffer)));
1082     gst_mss_demux_stream_store_object (stream, GST_MINI_OBJECT_CAST (_buffer));
1083   }
1084
1085   return ret;
1086
1087 no_url_error:
1088   {
1089     GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
1090         (_("Failed to get fragment URL.")),
1091         ("An error happened when getting fragment URL"));
1092     gst_task_pause (stream->download_task);
1093     return GST_FLOW_ERROR;
1094   }
1095 error:
1096   {
1097     GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1098     gst_task_pause (stream->download_task);
1099     return GST_FLOW_ERROR;
1100   }
1101 }
1102
1103 static void
1104 gst_mss_demux_download_loop (GstMssDemuxStream * stream)
1105 {
1106   GstMssDemux *mssdemux = stream->parent;
1107   GstBuffer *buffer = NULL;
1108   GstFlowReturn ret;
1109
1110   GST_LOG_OBJECT (mssdemux, "download loop start %p", stream);
1111
1112
1113   ret = gst_mss_demux_stream_download_fragment (stream, &buffer);
1114   switch (ret) {
1115     case GST_FLOW_OK:
1116       break;                    /* all is good, let's go */
1117     case GST_FLOW_UNEXPECTED:  /* EOS */
1118       goto eos;
1119     case GST_FLOW_ERROR:
1120       goto error;
1121     default:
1122       break;
1123   }
1124
1125   if (buffer) {
1126     gst_mss_stream_advance_fragment (stream->manifest_stream);
1127   }
1128   GST_LOG_OBJECT (mssdemux, "download loop end %p", stream);
1129   return;
1130
1131 eos:
1132   {
1133     GST_DEBUG_OBJECT (mssdemux, "Storing EOS for pad %s:%s",
1134         GST_DEBUG_PAD_NAME (stream->pad));
1135     gst_mss_demux_stream_store_object (stream,
1136         GST_MINI_OBJECT_CAST (gst_event_new_eos ()));
1137     gst_task_pause (stream->download_task);
1138     return;
1139   }
1140 error:
1141   {
1142     GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1143     gst_task_pause (stream->download_task);
1144     return;
1145   }
1146 }
1147
1148 static GstFlowReturn
1149 gst_mss_demux_select_latest_stream (GstMssDemux * mssdemux,
1150     GstMssDemuxStream ** stream)
1151 {
1152   GstFlowReturn ret = GST_FLOW_OK;
1153   GstMssDemuxStream *current = NULL;
1154   GstClockTime cur_time = GST_CLOCK_TIME_NONE;
1155   GSList *iter;
1156
1157   if (!mssdemux->streams)
1158     return GST_FLOW_ERROR;
1159
1160   for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
1161     GstClockTime time;
1162     GstMssDemuxStream *other;
1163     GstDataQueueItem *item;
1164
1165     other = iter->data;
1166     if (other->eos) {
1167       continue;
1168     }
1169
1170     if (gst_data_queue_peek (other->dataqueue, &item)) {
1171     } else {
1172       /* flushing */
1173       return GST_FLOW_WRONG_STATE;
1174     }
1175
1176     if (GST_IS_EVENT (item->object)) {
1177       /* events have higher priority */
1178       current = other;
1179       break;
1180     }
1181     time = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (item->object));
1182     if (time < cur_time) {
1183       cur_time = time;
1184       current = other;
1185     }
1186   }
1187
1188   *stream = current;
1189   if (current == NULL)
1190     ret = GST_FLOW_UNEXPECTED;
1191   return ret;
1192 }
1193
1194 static void
1195 gst_mss_demux_stream_loop (GstMssDemux * mssdemux)
1196 {
1197   GstMssDemuxStream *stream = NULL;
1198   GstFlowReturn ret;
1199   GstMiniObject *object = NULL;
1200   GstDataQueueItem *item = NULL;
1201
1202   GST_LOG_OBJECT (mssdemux, "Starting stream loop");
1203
1204   GST_OBJECT_LOCK (mssdemux);
1205   if (mssdemux->update_bitrates) {
1206     mssdemux->update_bitrates = FALSE;
1207     GST_OBJECT_UNLOCK (mssdemux);
1208
1209     GST_DEBUG_OBJECT (mssdemux,
1210         "Starting streams reconfiguration due to bitrate changes");
1211     gst_mss_demux_reconfigure (mssdemux);
1212     GST_DEBUG_OBJECT (mssdemux, "Finished streams reconfiguration");
1213   } else {
1214     GST_OBJECT_UNLOCK (mssdemux);
1215   }
1216
1217   ret = gst_mss_demux_select_latest_stream (mssdemux, &stream);
1218
1219   if (stream)
1220     GST_DEBUG_OBJECT (mssdemux,
1221         "Stream loop selected %p stream of pad %s. %d - %s", stream,
1222         GST_PAD_NAME (stream->pad), ret, gst_flow_get_name (ret));
1223   else
1224     GST_DEBUG_OBJECT (mssdemux, "No streams selected -> %d - %s", ret,
1225         gst_flow_get_name (ret));
1226
1227   switch (ret) {
1228     case GST_FLOW_OK:
1229       break;
1230     case GST_FLOW_ERROR:
1231       goto error;
1232     case GST_FLOW_UNEXPECTED:
1233       goto eos;
1234     case GST_FLOW_WRONG_STATE:
1235       GST_DEBUG_OBJECT (mssdemux, "Wrong state, stopping task");
1236       goto stop;
1237     default:
1238       g_assert_not_reached ();
1239   }
1240
1241   GST_LOG_OBJECT (mssdemux, "popping next item from queue for stream %p %s",
1242       stream, GST_PAD_NAME (stream->pad));
1243   if (gst_data_queue_pop (stream->dataqueue, &item)) {
1244     if (item->object)
1245       object = gst_mini_object_ref (item->object);
1246     item->destroy (item);
1247   } else {
1248     GST_DEBUG_OBJECT (mssdemux,
1249         "Failed to get object from dataqueue on stream %p %s", stream,
1250         GST_PAD_NAME (stream->pad));
1251     goto stop;
1252   }
1253
1254   if (G_UNLIKELY (stream->pending_newsegment)) {
1255     gst_pad_push_event (stream->pad, stream->pending_newsegment);
1256     stream->pending_newsegment = NULL;
1257   }
1258
1259   if (G_LIKELY (GST_IS_BUFFER (object))) {
1260     if (GST_BUFFER_TIMESTAMP (object) != stream->next_timestamp) {
1261       GST_ERROR_OBJECT (mssdemux, "Marking buffer %p as discont buffer:%"
1262           GST_TIME_FORMAT " != expected:%" GST_TIME_FORMAT, object,
1263           GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (object)),
1264           GST_TIME_ARGS (stream->next_timestamp));
1265       GST_BUFFER_FLAG_SET (object, GST_BUFFER_FLAG_DISCONT);
1266     }
1267
1268     GST_DEBUG_OBJECT (mssdemux,
1269         "Pushing buffer %p %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
1270         " discont:%d on pad %s", object,
1271         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (object)),
1272         GST_TIME_ARGS (GST_BUFFER_DURATION (object)),
1273         GST_BUFFER_FLAG_IS_SET (object, GST_BUFFER_FLAG_DISCONT),
1274         GST_PAD_NAME (stream->pad));
1275
1276     stream->next_timestamp =
1277         GST_BUFFER_TIMESTAMP (object) + GST_BUFFER_DURATION (object);
1278
1279     ret = gst_pad_push (stream->pad, GST_BUFFER_CAST (object));
1280   } else if (GST_IS_EVENT (object)) {
1281     if (GST_EVENT_TYPE (object) == GST_EVENT_EOS)
1282       stream->eos = TRUE;
1283     GST_DEBUG_OBJECT (mssdemux, "Pushing event %p on pad %s", object,
1284         GST_PAD_NAME (stream->pad));
1285     gst_pad_push_event (stream->pad, GST_EVENT_CAST (object));
1286   } else {
1287     g_return_if_reached ();
1288   }
1289
1290   switch (ret) {
1291     case GST_FLOW_UNEXPECTED:
1292       goto eos;                 /* EOS ? */
1293     case GST_FLOW_ERROR:
1294       goto error;
1295     case GST_FLOW_NOT_LINKED:
1296       break;                    /* TODO what to do here? pause the task or just keep pushing? */
1297     case GST_FLOW_OK:
1298     default:
1299       break;
1300   }
1301
1302   GST_LOG_OBJECT (mssdemux, "Stream loop end");
1303   return;
1304
1305 eos:
1306   {
1307     GST_DEBUG_OBJECT (mssdemux, "EOS on all pads");
1308     gst_task_pause (mssdemux->stream_task);
1309     return;
1310   }
1311 error:
1312   {
1313     GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1314     gst_task_pause (mssdemux->stream_task);
1315     return;
1316   }
1317 stop:
1318   {
1319     GST_DEBUG_OBJECT (mssdemux, "Pausing streaming task");
1320     gst_task_pause (mssdemux->stream_task);
1321     return;
1322   }
1323 }