2 * Copyright (C) 2012 Smart TV Alliance
3 * Author: Thiago Sousa Santos <thiago.sousa.santos@collabora.com>, Collabora Ltd.
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.
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.
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.
24 * SECTION:element-mssdemux
26 * Demuxes a Microsoft's Smooth Streaming manifest into its audio and/or video streams.
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).
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.
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.
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.
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.
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.
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.
72 #include "gst/gst-i18n-plugin.h"
78 #include "gstmssdemux.h"
80 GST_DEBUG_CATEGORY (mssdemux_debug);
82 #define DEFAULT_CONNECTION_SPEED 0
83 #define DEFAULT_MAX_QUEUE_SIZE_BUFFERS 0
89 PROP_CONNECTION_SPEED,
90 PROP_MAX_QUEUE_SIZE_BUFFERS,
94 static GstStaticPadTemplate gst_mss_demux_sink_template =
95 GST_STATIC_PAD_TEMPLATE ("sink",
98 GST_STATIC_CAPS ("application/vnd.ms-sstr+xml")
101 static GstStaticPadTemplate gst_mss_demux_videosrc_template =
102 GST_STATIC_PAD_TEMPLATE ("video_%02u",
105 GST_STATIC_CAPS_ANY);
107 static GstStaticPadTemplate gst_mss_demux_audiosrc_template =
108 GST_STATIC_PAD_TEMPLATE ("audio_%02u",
111 GST_STATIC_CAPS_ANY);
113 GST_BOILERPLATE (GstMssDemux, gst_mss_demux, GstMssDemux, GST_TYPE_ELEMENT);
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);
125 static gboolean gst_mss_demux_src_query (GstPad * pad, GstQuery * query);
127 static void gst_mss_demux_download_loop (GstMssDemuxStream * stream);
128 static void gst_mss_demux_stream_loop (GstMssDemux * mssdemux);
130 static gboolean gst_mss_demux_process_manifest (GstMssDemux * mssdemux);
133 gst_mss_demux_base_init (gpointer klass)
135 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
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>");
148 GST_DEBUG_CATEGORY_INIT (mssdemux_debug, "mssdemux", 0, "mssdemux plugin");
152 gst_mss_demux_class_init (GstMssDemuxClass * klass)
154 GObjectClass *gobject_class;
155 GstElementClass *gstelement_class;
157 gobject_class = (GObjectClass *) klass;
158 gstelement_class = (GstElementClass *) klass;
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;
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));
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));
176 gstelement_class->change_state =
177 GST_DEBUG_FUNCPTR (gst_mss_demux_change_state);
181 gst_mss_demux_init (GstMssDemux * mssdemux, GstMssDemuxClass * klass)
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);
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);
196 mssdemux->data_queue_max_size = DEFAULT_MAX_QUEUE_SIZE_BUFFERS;
200 _data_queue_check_full (GstDataQueue * queue, guint visible, guint bytes,
201 guint64 time, gpointer checkdata)
203 GstMssDemuxStream *stream = checkdata;
204 GstMssDemux *mssdemux = stream->parent;
206 if (mssdemux->data_queue_max_size == 0)
207 return FALSE; /* never full */
208 return visible >= mssdemux->data_queue_max_size;
211 static GstMssDemuxStream *
212 gst_mss_demux_stream_new (GstMssDemux * mssdemux,
213 GstMssStream * manifeststream, GstPad * srcpad)
215 GstMssDemuxStream *stream;
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);
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);
227 stream->pad = srcpad;
228 stream->manifest_stream = manifeststream;
229 stream->parent = mssdemux;
235 gst_mss_demux_stream_free (GstMssDemuxStream * stream)
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");
249 gst_object_unref (stream->download_task);
250 g_static_rec_mutex_free (&stream->download_lock);
251 stream->download_task = NULL;
254 if (stream->pending_newsegment) {
255 gst_event_unref (stream->pending_newsegment);
256 stream->pending_newsegment = NULL;
260 if (stream->downloader != NULL) {
261 g_object_unref (stream->downloader);
262 stream->downloader = NULL;
264 if (stream->dataqueue) {
265 g_object_unref (stream->dataqueue);
266 stream->dataqueue = NULL;
269 gst_object_unref (stream->pad);
276 gst_mss_demux_reset (GstMssDemux * mssdemux)
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);
285 gst_data_queue_set_flushing (stream->dataqueue, TRUE);
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);
295 if (mssdemux->manifest_buffer) {
296 gst_buffer_unref (mssdemux->manifest_buffer);
297 mssdemux->manifest_buffer = NULL;
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);
305 g_slist_free (mssdemux->streams);
306 mssdemux->streams = NULL;
308 if (mssdemux->manifest) {
309 gst_mss_manifest_free (mssdemux->manifest);
310 mssdemux->manifest = NULL;
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;
321 gst_mss_demux_dispose (GObject * object)
323 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (object);
325 gst_mss_demux_reset (mssdemux);
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;
333 G_OBJECT_CLASS (parent_class)->dispose (object);
337 gst_mss_demux_set_property (GObject * object, guint prop_id,
338 const GValue * value, GParamSpec * pspec)
340 GstMssDemux *mssdemux = GST_MSS_DEMUX (object);
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);
351 case PROP_MAX_QUEUE_SIZE_BUFFERS:
352 mssdemux->data_queue_max_size = g_value_get_uint (value);
355 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
361 gst_mss_demux_get_property (GObject * object, guint prop_id, GValue * value,
364 GstMssDemux *mssdemux = GST_MSS_DEMUX (object);
367 case PROP_CONNECTION_SPEED:
368 g_value_set_uint (value, mssdemux->connection_speed / 1000);
370 case PROP_MAX_QUEUE_SIZE_BUFFERS:
371 g_value_set_uint (value, mssdemux->data_queue_max_size);
374 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
379 static GstStateChangeReturn
380 gst_mss_demux_change_state (GstElement * element, GstStateChange transition)
382 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (element);
383 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
385 switch (transition) {
386 case GST_STATE_CHANGE_PAUSED_TO_READY:
387 gst_mss_demux_reset (mssdemux);
389 case GST_STATE_CHANGE_READY_TO_NULL:
395 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
397 switch (transition) {
398 case GST_STATE_CHANGE_PAUSED_TO_READY:{
409 gst_mss_demux_chain (GstPad * pad, GstBuffer * buffer)
411 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (GST_PAD_PARENT (pad));
412 if (mssdemux->manifest_buffer == NULL)
413 mssdemux->manifest_buffer = buffer;
415 mssdemux->manifest_buffer =
416 gst_buffer_join (mssdemux->manifest_buffer, buffer);
418 GST_INFO_OBJECT (mssdemux, "Received manifest buffer, total size is %i bytes",
419 GST_BUFFER_SIZE (mssdemux->manifest_buffer));
425 gst_mss_demux_start (GstMssDemux * mssdemux)
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);
435 gst_task_start (mssdemux->stream_task);
439 gst_mss_demux_push_src_event (GstMssDemux * mssdemux, GstEvent * event)
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);
449 gst_event_unref (event);
454 gst_mss_demux_event (GstPad * pad, GstEvent * event)
456 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (GST_PAD_PARENT (pad));
457 gboolean forward = TRUE;
460 switch (GST_EVENT_TYPE (event)) {
461 case GST_EVENT_FLUSH_STOP:
462 gst_mss_demux_reset (mssdemux);
465 if (mssdemux->manifest_buffer == NULL) {
466 GST_WARNING_OBJECT (mssdemux, "Received EOS without a manifest.");
469 GST_INFO_OBJECT (mssdemux, "Received EOS");
471 if (gst_mss_demux_process_manifest (mssdemux))
472 gst_mss_demux_start (mssdemux);
480 ret = gst_pad_event_default (pad, event);
482 gst_event_unref (event);
489 gst_mss_demux_stop_tasks (GstMssDemux * mssdemux, gboolean immediate)
493 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
494 GstMssDemuxStream *stream = iter->data;
496 gst_data_queue_set_flushing (stream->dataqueue, TRUE);
499 gst_uri_downloader_cancel (stream->downloader);
500 gst_task_pause (stream->download_task);
502 gst_task_pause (mssdemux->stream_task);
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);
508 g_static_rec_mutex_lock (&mssdemux->stream_lock);
512 gst_mss_demux_restart_tasks (GstMssDemux * mssdemux)
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);
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;
524 gst_data_queue_set_flushing (stream->dataqueue, FALSE);
525 gst_task_start (stream->download_task);
527 gst_task_start (mssdemux->stream_task);
531 gst_mss_demux_src_event (GstPad * pad, GstEvent * event)
533 GstMssDemux *mssdemux;
535 mssdemux = GST_MSS_DEMUX (GST_PAD_PARENT (pad));
537 switch (event->type) {
543 GstSeekType start_type, stop_type;
545 GstEvent *newsegment;
548 GST_INFO_OBJECT (mssdemux, "Received GST_EVENT_SEEK");
550 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
553 if (format != GST_FORMAT_TIME)
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));
560 if (flags & GST_SEEK_FLAG_FLUSH) {
561 GstEvent *flush = gst_event_new_flush_start ();
562 GST_DEBUG_OBJECT (mssdemux, "sending flush start");
564 gst_event_set_seqnum (flush, gst_event_get_seqnum (event));
565 gst_mss_demux_push_src_event (mssdemux, flush);
568 gst_mss_demux_stop_tasks (mssdemux, TRUE);
570 if (!gst_mss_manifest_seek (mssdemux->manifest, start)) {;
571 GST_WARNING_OBJECT (mssdemux, "Could not find seeked fragment");
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;
582 gst_data_queue_flush (stream->dataqueue);
583 gst_event_ref (newsegment);
584 gst_event_replace (&stream->pending_newsegment, newsegment);
586 gst_event_unref (newsegment);
588 if (flags & GST_SEEK_FLAG_FLUSH) {
589 GstEvent *flush = gst_event_new_flush_stop ();
590 GST_DEBUG_OBJECT (mssdemux, "sending flush stop");
592 gst_event_set_seqnum (flush, gst_event_get_seqnum (event));
593 gst_mss_demux_push_src_event (mssdemux, flush);
596 gst_mss_demux_restart_tasks (mssdemux);
598 gst_event_unref (event);
605 return gst_pad_event_default (pad, event);
608 gst_event_unref (event);
613 gst_mss_demux_src_query (GstPad * pad, GstQuery * query)
615 GstMssDemux *mssdemux;
616 gboolean ret = FALSE;
621 mssdemux = GST_MSS_DEMUX (GST_PAD_PARENT (pad));
623 switch (query->type) {
624 case GST_QUERY_DURATION:{
625 GstClockTime duration = -1;
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);
633 if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0) {
634 gst_query_set_duration (query, GST_FORMAT_TIME, duration);
638 GST_INFO_OBJECT (mssdemux, "GST_QUERY_DURATION returns %s with duration %"
639 GST_TIME_FORMAT, ret ? "TRUE" : "FALSE", GST_TIME_ARGS (duration));
642 case GST_QUERY_LATENCY:{
643 gboolean live = FALSE;
645 live = mssdemux->manifest
646 && gst_mss_manifest_is_live (mssdemux->manifest);
648 gst_query_set_latency (query, live, 0, -1);
652 case GST_QUERY_SEEKING:{
656 if (mssdemux->manifest && gst_mss_manifest_is_live (mssdemux->manifest)) {
657 return FALSE; /* no live seeking */
660 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
661 GST_INFO_OBJECT (mssdemux, "Received GST_QUERY_SEEKING with format %d",
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)
668 gst_query_set_seeking (query, fmt, TRUE, 0, stop);
670 GST_INFO_OBJECT (mssdemux, "GST_QUERY_SEEKING returning with stop : %"
671 GST_TIME_FORMAT, GST_TIME_ARGS (stop));
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
687 _set_src_pad_functions (GstPad * pad)
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));
694 _create_pad (GstMssDemux * mssdemux, GstMssStream * manifeststream)
697 GstPad *srcpad = NULL;
698 GstMssStreamType streamtype;
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));
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++);
708 gst_pad_new_from_static_template (&gst_mss_demux_videosrc_template,
711 } else if (streamtype == MSS_STREAM_TYPE_AUDIO) {
712 name = g_strdup_printf ("audio_%02u", mssdemux->n_audios++);
714 gst_pad_new_from_static_template (&gst_mss_demux_audiosrc_template,
720 GST_WARNING_OBJECT (mssdemux, "Ignoring unknown type stream");
724 _set_src_pad_functions (srcpad);
729 gst_mss_demux_create_streams (GstMssDemux * mssdemux)
731 GSList *streams = gst_mss_manifest_get_streams (mssdemux->manifest);
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"));
742 for (iter = streams; iter; iter = g_slist_next (iter)) {
743 GstPad *srcpad = NULL;
744 GstMssDemuxStream *stream = NULL;
745 GstMssStream *manifeststream = iter->data;
747 srcpad = _create_pad (mssdemux, manifeststream);
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);
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);
769 gst_mss_demux_expose_stream (GstMssDemux * mssdemux, GstMssDemuxStream * stream)
773 GstPad *pad = stream->pad;
775 media_caps = gst_mss_stream_get_caps (stream->manifest_stream);
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);
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);
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));
801 gst_mss_demux_process_manifest (GstMssDemux * mssdemux)
808 g_return_val_if_fail (mssdemux->manifest_buffer != NULL, FALSE);
809 g_return_val_if_fail (mssdemux->manifest == NULL, FALSE);
811 query = gst_query_new_uri ();
812 ret = gst_pad_peer_query (mssdemux->sinkpad, query);
815 gst_query_parse_uri (query, &uri);
816 GST_INFO_OBJECT (mssdemux, "Upstream is using URI: %s", uri);
818 mssdemux->manifest_uri = g_strdup (uri);
819 baseurl_end = g_strrstr (uri, "/Manifest");
821 /* set the new end of the string */
822 baseurl_end[0] = '\0';
824 GST_WARNING_OBJECT (mssdemux, "Stream's URI didn't end with /manifest");
827 mssdemux->base_url = uri;
829 gst_query_unref (query);
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"));
838 GST_INFO_OBJECT (mssdemux, "Received manifest: %i bytes",
839 GST_BUFFER_SIZE (mssdemux->manifest_buffer));
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"));
848 GST_INFO_OBJECT (mssdemux, "Live stream: %d",
849 gst_mss_manifest_is_live (mssdemux->manifest));
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);
862 if (!mssdemux->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"));
872 gst_element_no_more_pads (GST_ELEMENT_CAST (mssdemux));
877 gst_mss_demux_reload_manifest (GstMssDemux * mssdemux)
879 GstUriDownloader *downloader;
880 GstFragment *manifest_data;
881 GstBuffer *manifest_buffer;
883 downloader = gst_uri_downloader_new ();
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);
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);
894 g_object_unref (downloader);
898 gst_mss_demux_reconfigure (GstMssDemux * mssdemux)
900 GSList *oldpads = NULL;
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;
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;
915 oldpads = g_slist_prepend (oldpads, oldpad);
917 /* since we are flushing the queue, get the next un-pushed timestamp to seek
919 gst_data_queue_set_flushing (stream->dataqueue, FALSE);
920 if (!gst_data_queue_is_empty (stream->dataqueue)) {
921 GstDataQueueItem *item = NULL;
923 while (!gst_data_queue_is_empty (stream->dataqueue)
924 && !GST_CLOCK_TIME_IS_VALID (ts)) {
925 gst_data_queue_pop (stream->dataqueue, &item);
928 g_assert_not_reached ();
932 if (GST_IS_BUFFER (item->object)) {
933 GstBuffer *buffer = GST_BUFFER_CAST (item->object);
935 ts = GST_BUFFER_TIMESTAMP (buffer);
937 item->destroy (item);
941 if (!GST_CLOCK_TIME_IS_VALID (ts)) {
942 ts = gst_mss_stream_get_fragment_gst_timestamp
943 (stream->manifest_stream);
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);
955 stream->pad = _create_pad (mssdemux, stream->manifest_stream);
956 gst_mss_demux_expose_stream (mssdemux, stream);
958 gst_pad_push_event (oldpad, gst_event_new_eos ());
961 gst_element_no_more_pads (GST_ELEMENT (mssdemux));
963 for (iter = oldpads; iter; iter = g_slist_next (iter)) {
964 GstPad *oldpad = iter->data;
966 gst_pad_set_active (oldpad, FALSE);
967 gst_element_remove_pad (GST_ELEMENT (mssdemux), oldpad);
968 gst_object_unref (oldpad);
970 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
971 GstMssDemuxStream *stream = iter->data;
973 stream->pending_newsegment =
974 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, newseg_ts, -1,
978 gst_mss_demux_restart_tasks (mssdemux);
982 _free_data_queue_item (gpointer obj)
984 GstDataQueueItem *item = obj;
986 gst_mini_object_unref (item->object);
987 g_slice_free (GstDataQueueItem, item);
991 gst_mss_demux_stream_store_object (GstMssDemuxStream * stream,
994 GstDataQueueItem *item;
996 item = g_slice_new (GstDataQueueItem);
997 item->object = (GstMiniObject *) obj;
999 item->duration = 0; /* we don't care */
1001 item->visible = TRUE;
1003 item->destroy = (GDestroyNotify) _free_data_queue_item;
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);
1011 static GstFlowReturn
1012 gst_mss_demux_stream_download_fragment (GstMssDemuxStream * stream,
1013 GstBuffer ** buffer)
1015 GstMssDemux *mssdemux = stream->parent;
1018 GstFragment *fragment;
1020 GstFlowReturn ret = GST_FLOW_OK;
1022 GST_DEBUG_OBJECT (mssdemux, "Getting url for stream %p", stream);
1023 ret = gst_mss_stream_get_fragment_url (stream->manifest_stream, &path);
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);
1032 return GST_FLOW_UNEXPECTED;
1033 case GST_FLOW_ERROR:
1041 GST_DEBUG_OBJECT (mssdemux, "Got url path '%s' for stream %p", path, stream);
1043 url = g_strdup_printf ("%s/%s", mssdemux->base_url, path);
1045 GST_DEBUG_OBJECT (mssdemux, "Got url '%s' for stream %p", url, stream);
1047 fragment = gst_uri_downloader_fetch_uri (stream->downloader, url);
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 */
1059 return GST_FLOW_ERROR;
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);
1070 g_object_unref (fragment);
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));
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;
1097 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1098 gst_task_pause (stream->download_task);
1099 return GST_FLOW_ERROR;
1104 gst_mss_demux_download_loop (GstMssDemuxStream * stream)
1106 GstMssDemux *mssdemux = stream->parent;
1107 GstBuffer *buffer = NULL;
1110 GST_LOG_OBJECT (mssdemux, "download loop start %p", stream);
1113 ret = gst_mss_demux_stream_download_fragment (stream, &buffer);
1116 break; /* all is good, let's go */
1117 case GST_FLOW_UNEXPECTED: /* EOS */
1119 case GST_FLOW_ERROR:
1126 gst_mss_stream_advance_fragment (stream->manifest_stream);
1128 GST_LOG_OBJECT (mssdemux, "download loop end %p", stream);
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);
1142 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1143 gst_task_pause (stream->download_task);
1148 static GstFlowReturn
1149 gst_mss_demux_select_latest_stream (GstMssDemux * mssdemux,
1150 GstMssDemuxStream ** stream)
1152 GstFlowReturn ret = GST_FLOW_OK;
1153 GstMssDemuxStream *current = NULL;
1154 GstClockTime cur_time = GST_CLOCK_TIME_NONE;
1157 if (!mssdemux->streams)
1158 return GST_FLOW_ERROR;
1160 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
1162 GstMssDemuxStream *other;
1163 GstDataQueueItem *item;
1170 if (gst_data_queue_peek (other->dataqueue, &item)) {
1173 return GST_FLOW_WRONG_STATE;
1176 if (GST_IS_EVENT (item->object)) {
1177 /* events have higher priority */
1181 time = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (item->object));
1182 if (time < cur_time) {
1189 if (current == NULL)
1190 ret = GST_FLOW_UNEXPECTED;
1195 gst_mss_demux_stream_loop (GstMssDemux * mssdemux)
1197 GstMssDemuxStream *stream = NULL;
1199 GstMiniObject *object = NULL;
1200 GstDataQueueItem *item = NULL;
1202 GST_LOG_OBJECT (mssdemux, "Starting stream loop");
1204 GST_OBJECT_LOCK (mssdemux);
1205 if (mssdemux->update_bitrates) {
1206 mssdemux->update_bitrates = FALSE;
1207 GST_OBJECT_UNLOCK (mssdemux);
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");
1214 GST_OBJECT_UNLOCK (mssdemux);
1217 ret = gst_mss_demux_select_latest_stream (mssdemux, &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));
1224 GST_DEBUG_OBJECT (mssdemux, "No streams selected -> %d - %s", ret,
1225 gst_flow_get_name (ret));
1230 case GST_FLOW_ERROR:
1232 case GST_FLOW_UNEXPECTED:
1234 case GST_FLOW_WRONG_STATE:
1235 GST_DEBUG_OBJECT (mssdemux, "Wrong state, stopping task");
1238 g_assert_not_reached ();
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)) {
1245 object = gst_mini_object_ref (item->object);
1246 item->destroy (item);
1248 GST_DEBUG_OBJECT (mssdemux,
1249 "Failed to get object from dataqueue on stream %p %s", stream,
1250 GST_PAD_NAME (stream->pad));
1254 if (G_UNLIKELY (stream->pending_newsegment)) {
1255 gst_pad_push_event (stream->pad, stream->pending_newsegment);
1256 stream->pending_newsegment = NULL;
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);
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));
1276 stream->next_timestamp =
1277 GST_BUFFER_TIMESTAMP (object) + GST_BUFFER_DURATION (object);
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)
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));
1287 g_return_if_reached ();
1291 case GST_FLOW_UNEXPECTED:
1292 goto eos; /* EOS ? */
1293 case GST_FLOW_ERROR:
1295 case GST_FLOW_NOT_LINKED:
1296 break; /* TODO what to do here? pause the task or just keep pushing? */
1302 GST_LOG_OBJECT (mssdemux, "Stream loop end");
1307 GST_DEBUG_OBJECT (mssdemux, "EOS on all pads");
1308 gst_task_pause (mssdemux->stream_task);
1313 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1314 gst_task_pause (mssdemux->stream_task);
1319 GST_DEBUG_OBJECT (mssdemux, "Pausing streaming task");
1320 gst_task_pause (mssdemux->stream_task);