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
84 #define DEFAULT_BITRATE_LIMIT 0.8
86 #define DOWNLOAD_RATE_MAX_HISTORY_LENGTH 5
87 #define MAX_DOWNLOAD_ERROR_COUNT 3
93 PROP_CONNECTION_SPEED,
94 PROP_MAX_QUEUE_SIZE_BUFFERS,
99 static GstStaticPadTemplate gst_mss_demux_sink_template =
100 GST_STATIC_PAD_TEMPLATE ("sink",
103 GST_STATIC_CAPS ("application/vnd.ms-sstr+xml")
106 static GstStaticPadTemplate gst_mss_demux_videosrc_template =
107 GST_STATIC_PAD_TEMPLATE ("video_%02u",
110 GST_STATIC_CAPS_ANY);
112 static GstStaticPadTemplate gst_mss_demux_audiosrc_template =
113 GST_STATIC_PAD_TEMPLATE ("audio_%02u",
116 GST_STATIC_CAPS_ANY);
118 GST_BOILERPLATE (GstMssDemux, gst_mss_demux, GstMssDemux, GST_TYPE_ELEMENT);
120 static void gst_mss_demux_dispose (GObject * object);
121 static void gst_mss_demux_set_property (GObject * object, guint prop_id,
122 const GValue * value, GParamSpec * pspec);
123 static void gst_mss_demux_get_property (GObject * object, guint prop_id,
124 GValue * value, GParamSpec * pspec);
125 static GstStateChangeReturn gst_mss_demux_change_state (GstElement * element,
126 GstStateChange transition);
127 static GstFlowReturn gst_mss_demux_chain (GstPad * pad, GstBuffer * buffer);
128 static GstFlowReturn gst_mss_demux_event (GstPad * pad, GstEvent * event);
130 static gboolean gst_mss_demux_src_query (GstPad * pad, GstQuery * query);
132 static void gst_mss_demux_download_loop (GstMssDemuxStream * stream);
133 static void gst_mss_demux_stream_loop (GstMssDemux * mssdemux);
135 static gboolean gst_mss_demux_process_manifest (GstMssDemux * mssdemux);
138 gst_mss_demux_base_init (gpointer klass)
140 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
142 gst_element_class_add_static_pad_template (element_class,
143 &gst_mss_demux_sink_template);
144 gst_element_class_add_static_pad_template (element_class,
145 &gst_mss_demux_videosrc_template);
146 gst_element_class_add_static_pad_template (element_class,
147 &gst_mss_demux_audiosrc_template);
148 gst_element_class_set_details_simple (element_class, "Smooth Streaming "
149 "demuxer", "Demuxer",
150 "Parse and demultiplex a Smooth Streaming manifest into audio and video "
151 "streams", "Thiago Santos <thiago.sousa.santos@collabora.com>");
153 GST_DEBUG_CATEGORY_INIT (mssdemux_debug, "mssdemux", 0, "mssdemux plugin");
157 gst_mss_demux_class_init (GstMssDemuxClass * klass)
159 GObjectClass *gobject_class;
160 GstElementClass *gstelement_class;
162 gobject_class = (GObjectClass *) klass;
163 gstelement_class = (GstElementClass *) klass;
165 gobject_class->dispose = gst_mss_demux_dispose;
166 gobject_class->set_property = gst_mss_demux_set_property;
167 gobject_class->get_property = gst_mss_demux_get_property;
169 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
170 g_param_spec_uint ("connection-speed", "Connection Speed",
171 "Network connection speed in kbps (0 = unknown)",
172 0, G_MAXUINT / 1000, DEFAULT_CONNECTION_SPEED,
173 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175 g_object_class_install_property (gobject_class, PROP_MAX_QUEUE_SIZE_BUFFERS,
176 g_param_spec_uint ("max-queue-size-buffers", "Max queue size in buffers",
177 "Maximum buffers that can be stored in each internal stream queue "
178 "(0 = infinite)", 0, G_MAXUINT, DEFAULT_MAX_QUEUE_SIZE_BUFFERS,
179 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
181 g_object_class_install_property (gobject_class, PROP_BITRATE_LIMIT,
182 g_param_spec_float ("bitrate-limit",
183 "Bitrate limit in %",
184 "Limit of the available bitrate to use when switching to alternates.",
185 0, 1, DEFAULT_BITRATE_LIMIT,
186 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188 gstelement_class->change_state =
189 GST_DEBUG_FUNCPTR (gst_mss_demux_change_state);
193 gst_mss_demux_init (GstMssDemux * mssdemux, GstMssDemuxClass * klass)
196 gst_pad_new_from_static_template (&gst_mss_demux_sink_template, "sink");
197 gst_pad_set_chain_function (mssdemux->sinkpad,
198 GST_DEBUG_FUNCPTR (gst_mss_demux_chain));
199 gst_pad_set_event_function (mssdemux->sinkpad,
200 GST_DEBUG_FUNCPTR (gst_mss_demux_event));
201 gst_element_add_pad (GST_ELEMENT_CAST (mssdemux), mssdemux->sinkpad);
203 g_static_rec_mutex_init (&mssdemux->stream_lock);
204 mssdemux->stream_task =
205 gst_task_create ((GstTaskFunction) gst_mss_demux_stream_loop, mssdemux);
206 gst_task_set_lock (mssdemux->stream_task, &mssdemux->stream_lock);
208 mssdemux->data_queue_max_size = DEFAULT_MAX_QUEUE_SIZE_BUFFERS;
209 mssdemux->bitrate_limit = DEFAULT_BITRATE_LIMIT;
213 _data_queue_check_full (GstDataQueue * queue, guint visible, guint bytes,
214 guint64 time, gpointer checkdata)
216 GstMssDemuxStream *stream = checkdata;
217 GstMssDemux *mssdemux = stream->parent;
219 if (mssdemux->data_queue_max_size == 0)
220 return FALSE; /* never full */
221 return visible >= mssdemux->data_queue_max_size;
224 static GstMssDemuxStream *
225 gst_mss_demux_stream_new (GstMssDemux * mssdemux,
226 GstMssStream * manifeststream, GstPad * srcpad)
228 GstMssDemuxStream *stream;
230 stream = g_new0 (GstMssDemuxStream, 1);
231 stream->downloader = gst_uri_downloader_new ();
232 stream->dataqueue = gst_data_queue_new (_data_queue_check_full, stream);
234 /* Downloading task */
235 g_static_rec_mutex_init (&stream->download_lock);
236 stream->download_task =
237 gst_task_create ((GstTaskFunction) gst_mss_demux_download_loop, stream);
238 gst_task_set_lock (stream->download_task, &stream->download_lock);
240 stream->pad = srcpad;
241 stream->manifest_stream = manifeststream;
242 stream->parent = mssdemux;
243 gst_download_rate_init (&stream->download_rate);
244 gst_download_rate_set_max_length (&stream->download_rate,
245 DOWNLOAD_RATE_MAX_HISTORY_LENGTH);
251 gst_mss_demux_stream_free (GstMssDemuxStream * stream)
253 gst_download_rate_deinit (&stream->download_rate);
254 if (stream->download_task) {
255 if (GST_TASK_STATE (stream->download_task) != GST_TASK_STOPPED) {
256 GST_DEBUG_OBJECT (stream->parent, "Leaving streaming task %s:%s",
257 GST_DEBUG_PAD_NAME (stream->pad));
258 gst_uri_downloader_cancel (stream->downloader);
259 gst_task_stop (stream->download_task);
260 g_static_rec_mutex_lock (&stream->download_lock);
261 g_static_rec_mutex_unlock (&stream->download_lock);
262 GST_LOG_OBJECT (stream->parent, "Waiting for task to finish");
263 gst_task_join (stream->download_task);
264 GST_LOG_OBJECT (stream->parent, "Finished");
266 gst_object_unref (stream->download_task);
267 g_static_rec_mutex_free (&stream->download_lock);
268 stream->download_task = NULL;
271 if (stream->pending_newsegment) {
272 gst_event_unref (stream->pending_newsegment);
273 stream->pending_newsegment = NULL;
277 if (stream->downloader != NULL) {
278 g_object_unref (stream->downloader);
279 stream->downloader = NULL;
281 if (stream->dataqueue) {
282 g_object_unref (stream->dataqueue);
283 stream->dataqueue = NULL;
286 gst_object_unref (stream->pad);
293 gst_mss_demux_reset (GstMssDemux * mssdemux)
297 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
298 GstMssDemuxStream *stream = iter->data;
299 if (stream->downloader)
300 gst_uri_downloader_cancel (stream->downloader);
302 gst_data_queue_set_flushing (stream->dataqueue, TRUE);
305 if (GST_TASK_STATE (mssdemux->stream_task) != GST_TASK_STOPPED) {
306 gst_task_stop (mssdemux->stream_task);
307 g_static_rec_mutex_lock (&mssdemux->stream_lock);
308 g_static_rec_mutex_unlock (&mssdemux->stream_lock);
309 gst_task_join (mssdemux->stream_task);
312 if (mssdemux->manifest_buffer) {
313 gst_buffer_unref (mssdemux->manifest_buffer);
314 mssdemux->manifest_buffer = NULL;
317 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
318 GstMssDemuxStream *stream = iter->data;
320 gst_element_remove_pad (GST_ELEMENT_CAST (mssdemux), stream->pad);
321 gst_mss_demux_stream_free (stream);
323 g_slist_free (mssdemux->streams);
324 mssdemux->streams = NULL;
326 if (mssdemux->manifest) {
327 gst_mss_manifest_free (mssdemux->manifest);
328 mssdemux->manifest = NULL;
331 mssdemux->n_videos = mssdemux->n_audios = 0;
332 g_free (mssdemux->base_url);
333 mssdemux->base_url = NULL;
334 g_free (mssdemux->manifest_uri);
335 mssdemux->manifest_uri = NULL;
339 gst_mss_demux_dispose (GObject * object)
341 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (object);
343 gst_mss_demux_reset (mssdemux);
345 if (mssdemux->stream_task) {
346 gst_object_unref (mssdemux->stream_task);
347 g_static_rec_mutex_free (&mssdemux->stream_lock);
348 mssdemux->stream_task = NULL;
351 G_OBJECT_CLASS (parent_class)->dispose (object);
355 gst_mss_demux_set_property (GObject * object, guint prop_id,
356 const GValue * value, GParamSpec * pspec)
358 GstMssDemux *mssdemux = GST_MSS_DEMUX (object);
361 case PROP_CONNECTION_SPEED:
362 GST_OBJECT_LOCK (mssdemux);
363 mssdemux->connection_speed = g_value_get_uint (value) * 1000;
364 mssdemux->update_bitrates = TRUE;
365 GST_DEBUG_OBJECT (mssdemux, "Connection speed set to %llu",
366 mssdemux->connection_speed);
367 GST_OBJECT_UNLOCK (mssdemux);
369 case PROP_MAX_QUEUE_SIZE_BUFFERS:
370 mssdemux->data_queue_max_size = g_value_get_uint (value);
372 case PROP_BITRATE_LIMIT:
373 mssdemux->bitrate_limit = g_value_get_float (value);
376 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
382 gst_mss_demux_get_property (GObject * object, guint prop_id, GValue * value,
385 GstMssDemux *mssdemux = GST_MSS_DEMUX (object);
388 case PROP_CONNECTION_SPEED:
389 g_value_set_uint (value, mssdemux->connection_speed / 1000);
391 case PROP_MAX_QUEUE_SIZE_BUFFERS:
392 g_value_set_uint (value, mssdemux->data_queue_max_size);
394 case PROP_BITRATE_LIMIT:
395 g_value_set_float (value, mssdemux->bitrate_limit);
398 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
403 static GstStateChangeReturn
404 gst_mss_demux_change_state (GstElement * element, GstStateChange transition)
406 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (element);
407 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
409 switch (transition) {
410 case GST_STATE_CHANGE_PAUSED_TO_READY:
411 gst_mss_demux_reset (mssdemux);
413 case GST_STATE_CHANGE_READY_TO_NULL:
419 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
421 switch (transition) {
422 case GST_STATE_CHANGE_PAUSED_TO_READY:{
433 gst_mss_demux_chain (GstPad * pad, GstBuffer * buffer)
435 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (GST_PAD_PARENT (pad));
436 if (mssdemux->manifest_buffer == NULL)
437 mssdemux->manifest_buffer = buffer;
439 mssdemux->manifest_buffer =
440 gst_buffer_join (mssdemux->manifest_buffer, buffer);
442 GST_INFO_OBJECT (mssdemux, "Received manifest buffer, total size is %i bytes",
443 GST_BUFFER_SIZE (mssdemux->manifest_buffer));
449 gst_mss_demux_start (GstMssDemux * mssdemux)
453 GST_INFO_OBJECT (mssdemux, "Starting streams' tasks");
454 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
455 GstMssDemuxStream *stream = iter->data;
456 gst_task_start (stream->download_task);
459 gst_task_start (mssdemux->stream_task);
463 gst_mss_demux_push_src_event (GstMssDemux * mssdemux, GstEvent * event)
468 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
469 GstMssDemuxStream *stream = iter->data;
470 gst_event_ref (event);
471 ret = ret & gst_pad_push_event (stream->pad, event);
473 gst_event_unref (event);
478 gst_mss_demux_event (GstPad * pad, GstEvent * event)
480 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (GST_PAD_PARENT (pad));
481 gboolean forward = TRUE;
484 switch (GST_EVENT_TYPE (event)) {
485 case GST_EVENT_FLUSH_STOP:
486 gst_mss_demux_reset (mssdemux);
489 if (mssdemux->manifest_buffer == NULL) {
490 GST_WARNING_OBJECT (mssdemux, "Received EOS without a manifest.");
493 GST_INFO_OBJECT (mssdemux, "Received EOS");
495 if (gst_mss_demux_process_manifest (mssdemux))
496 gst_mss_demux_start (mssdemux);
504 ret = gst_pad_event_default (pad, event);
506 gst_event_unref (event);
513 gst_mss_demux_stop_tasks (GstMssDemux * mssdemux, gboolean immediate)
517 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
518 GstMssDemuxStream *stream = iter->data;
520 gst_data_queue_set_flushing (stream->dataqueue, TRUE);
523 gst_uri_downloader_cancel (stream->downloader);
524 gst_task_pause (stream->download_task);
526 gst_task_pause (mssdemux->stream_task);
528 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
529 GstMssDemuxStream *stream = iter->data;
530 g_static_rec_mutex_lock (&stream->download_lock);
532 g_static_rec_mutex_lock (&mssdemux->stream_lock);
536 gst_mss_demux_restart_tasks (GstMssDemux * mssdemux)
539 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
540 GstMssDemuxStream *stream = iter->data;
541 gst_uri_downloader_reset (stream->downloader);
542 g_static_rec_mutex_unlock (&stream->download_lock);
544 g_static_rec_mutex_unlock (&mssdemux->stream_lock);
545 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
546 GstMssDemuxStream *stream = iter->data;
548 gst_data_queue_set_flushing (stream->dataqueue, FALSE);
549 gst_task_start (stream->download_task);
551 gst_task_start (mssdemux->stream_task);
555 gst_mss_demux_src_event (GstPad * pad, GstEvent * event)
557 GstMssDemux *mssdemux;
559 mssdemux = GST_MSS_DEMUX (GST_PAD_PARENT (pad));
561 switch (event->type) {
567 GstSeekType start_type, stop_type;
569 GstEvent *newsegment;
572 GST_INFO_OBJECT (mssdemux, "Received GST_EVENT_SEEK");
574 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
577 if (format != GST_FORMAT_TIME)
580 GST_DEBUG_OBJECT (mssdemux,
581 "seek event, rate: %f start: %" GST_TIME_FORMAT " stop: %"
582 GST_TIME_FORMAT, rate, GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
584 if (flags & GST_SEEK_FLAG_FLUSH) {
585 GstEvent *flush = gst_event_new_flush_start ();
586 GST_DEBUG_OBJECT (mssdemux, "sending flush start");
588 gst_event_set_seqnum (flush, gst_event_get_seqnum (event));
589 gst_mss_demux_push_src_event (mssdemux, flush);
592 gst_mss_demux_stop_tasks (mssdemux, TRUE);
594 if (!gst_mss_manifest_seek (mssdemux->manifest, start)) {;
595 GST_WARNING_OBJECT (mssdemux, "Could not find seeked fragment");
600 gst_event_new_new_segment (FALSE, rate, format, start, stop, start);
601 gst_event_set_seqnum (newsegment, gst_event_get_seqnum (event));
602 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
603 GstMssDemuxStream *stream = iter->data;
606 gst_data_queue_flush (stream->dataqueue);
607 gst_event_ref (newsegment);
608 gst_event_replace (&stream->pending_newsegment, newsegment);
610 gst_event_unref (newsegment);
612 if (flags & GST_SEEK_FLAG_FLUSH) {
613 GstEvent *flush = gst_event_new_flush_stop ();
614 GST_DEBUG_OBJECT (mssdemux, "sending flush stop");
616 gst_event_set_seqnum (flush, gst_event_get_seqnum (event));
617 gst_mss_demux_push_src_event (mssdemux, flush);
620 gst_mss_demux_restart_tasks (mssdemux);
622 gst_event_unref (event);
629 return gst_pad_event_default (pad, event);
632 gst_event_unref (event);
637 gst_mss_demux_src_query (GstPad * pad, GstQuery * query)
639 GstMssDemux *mssdemux;
640 gboolean ret = FALSE;
645 mssdemux = GST_MSS_DEMUX (GST_PAD_PARENT (pad));
647 switch (query->type) {
648 case GST_QUERY_DURATION:{
649 GstClockTime duration = -1;
652 gst_query_parse_duration (query, &fmt, NULL);
653 if (fmt == GST_FORMAT_TIME && mssdemux->manifest) {
654 /* TODO should we use the streams accumulated duration or the main manifest duration? */
655 duration = gst_mss_manifest_get_gst_duration (mssdemux->manifest);
657 if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0) {
658 gst_query_set_duration (query, GST_FORMAT_TIME, duration);
662 GST_INFO_OBJECT (mssdemux, "GST_QUERY_DURATION returns %s with duration %"
663 GST_TIME_FORMAT, ret ? "TRUE" : "FALSE", GST_TIME_ARGS (duration));
666 case GST_QUERY_LATENCY:{
667 gboolean live = FALSE;
669 live = mssdemux->manifest
670 && gst_mss_manifest_is_live (mssdemux->manifest);
672 gst_query_set_latency (query, live, 0, -1);
676 case GST_QUERY_SEEKING:{
680 if (mssdemux->manifest && gst_mss_manifest_is_live (mssdemux->manifest)) {
681 return FALSE; /* no live seeking */
684 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
685 GST_INFO_OBJECT (mssdemux, "Received GST_QUERY_SEEKING with format %d",
687 if (fmt == GST_FORMAT_TIME) {
688 GstClockTime duration;
689 duration = gst_mss_manifest_get_gst_duration (mssdemux->manifest);
690 if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0)
692 gst_query_set_seeking (query, fmt, TRUE, 0, stop);
694 GST_INFO_OBJECT (mssdemux, "GST_QUERY_SEEKING returning with stop : %"
695 GST_TIME_FORMAT, GST_TIME_ARGS (stop));
700 /* Don't fordward queries upstream because of the special nature of this
701 * "demuxer", which relies on the upstream element only to be fed
711 _set_src_pad_functions (GstPad * pad)
713 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_mss_demux_src_query));
714 gst_pad_set_event_function (pad, GST_DEBUG_FUNCPTR (gst_mss_demux_src_event));
718 _create_pad (GstMssDemux * mssdemux, GstMssStream * manifeststream)
721 GstPad *srcpad = NULL;
722 GstMssStreamType streamtype;
724 streamtype = gst_mss_stream_get_type (manifeststream);
725 GST_DEBUG_OBJECT (mssdemux, "Found stream of type: %s",
726 gst_mss_stream_type_name (streamtype));
728 /* TODO use stream's name/bitrate/index as the pad name? */
729 if (streamtype == MSS_STREAM_TYPE_VIDEO) {
730 name = g_strdup_printf ("video_%02u", mssdemux->n_videos++);
732 gst_pad_new_from_static_template (&gst_mss_demux_videosrc_template,
735 } else if (streamtype == MSS_STREAM_TYPE_AUDIO) {
736 name = g_strdup_printf ("audio_%02u", mssdemux->n_audios++);
738 gst_pad_new_from_static_template (&gst_mss_demux_audiosrc_template,
744 GST_WARNING_OBJECT (mssdemux, "Ignoring unknown type stream");
748 _set_src_pad_functions (srcpad);
753 gst_mss_demux_create_streams (GstMssDemux * mssdemux)
755 GSList *streams = gst_mss_manifest_get_streams (mssdemux->manifest);
758 if (streams == NULL) {
759 GST_INFO_OBJECT (mssdemux, "No streams found in the manifest");
760 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
761 (_("This file contains no playable streams.")),
762 ("no streams found at the Manifest"));
766 for (iter = streams; iter; iter = g_slist_next (iter)) {
767 GstPad *srcpad = NULL;
768 GstMssDemuxStream *stream = NULL;
769 GstMssStream *manifeststream = iter->data;
771 srcpad = _create_pad (mssdemux, manifeststream);
777 stream = gst_mss_demux_stream_new (mssdemux, manifeststream, srcpad);
778 gst_mss_stream_set_active (manifeststream, TRUE);
779 mssdemux->streams = g_slist_append (mssdemux->streams, stream);
782 /* select initial bitrates */
783 GST_OBJECT_LOCK (mssdemux);
784 GST_INFO_OBJECT (mssdemux, "Changing max bitrate to %llu",
785 mssdemux->connection_speed);
786 gst_mss_manifest_change_bitrate (mssdemux->manifest,
787 mssdemux->connection_speed);
788 mssdemux->update_bitrates = FALSE;
789 GST_OBJECT_UNLOCK (mssdemux);
793 gst_mss_demux_expose_stream (GstMssDemux * mssdemux, GstMssDemuxStream * stream)
797 GstPad *pad = stream->pad;
799 media_caps = gst_mss_stream_get_caps (stream->manifest_stream);
802 caps = gst_caps_new_simple ("video/quicktime", "variant", G_TYPE_STRING,
803 "mss-fragmented", "timescale", G_TYPE_UINT64,
804 gst_mss_stream_get_timescale (stream->manifest_stream), "media-caps",
805 GST_TYPE_CAPS, media_caps, NULL);
806 gst_caps_unref (media_caps);
807 gst_pad_set_caps (pad, caps);
808 gst_caps_unref (caps);
810 gst_pad_set_active (pad, TRUE);
811 GST_INFO_OBJECT (mssdemux, "Adding srcpad %s:%s with caps %" GST_PTR_FORMAT,
812 GST_DEBUG_PAD_NAME (pad), caps);
813 gst_object_ref (pad);
814 gst_element_add_pad (GST_ELEMENT_CAST (mssdemux), pad);
816 GST_WARNING_OBJECT (mssdemux,
817 "Couldn't get caps from manifest stream %p %s, not exposing it", stream,
818 GST_PAD_NAME (stream->pad));
825 gst_mss_demux_process_manifest (GstMssDemux * mssdemux)
832 g_return_val_if_fail (mssdemux->manifest_buffer != NULL, FALSE);
833 g_return_val_if_fail (mssdemux->manifest == NULL, FALSE);
835 query = gst_query_new_uri ();
836 ret = gst_pad_peer_query (mssdemux->sinkpad, query);
839 gst_query_parse_uri (query, &uri);
840 GST_INFO_OBJECT (mssdemux, "Upstream is using URI: %s", uri);
842 mssdemux->manifest_uri = g_strdup (uri);
843 baseurl_end = g_strrstr (uri, "/Manifest");
845 /* set the new end of the string */
846 baseurl_end[0] = '\0';
848 GST_WARNING_OBJECT (mssdemux, "Stream's URI didn't end with /manifest");
851 mssdemux->base_url = uri;
853 gst_query_unref (query);
855 if (mssdemux->base_url == NULL) {
856 GST_ELEMENT_ERROR (mssdemux, RESOURCE, NOT_FOUND,
857 (_("Couldn't get the Manifest's URI")),
858 ("need to get the manifest's URI from upstream elements"));
862 GST_INFO_OBJECT (mssdemux, "Received manifest: %i bytes",
863 GST_BUFFER_SIZE (mssdemux->manifest_buffer));
865 mssdemux->manifest = gst_mss_manifest_new (mssdemux->manifest_buffer);
866 if (!mssdemux->manifest) {
867 GST_ELEMENT_ERROR (mssdemux, STREAM, FORMAT, ("Bad manifest file"),
868 ("Xml manifest file couldn't be parsed"));
872 GST_INFO_OBJECT (mssdemux, "Live stream: %d",
873 gst_mss_manifest_is_live (mssdemux->manifest));
875 gst_mss_demux_create_streams (mssdemux);
876 for (iter = mssdemux->streams; iter;) {
877 GSList *current = iter;
878 GstMssDemuxStream *stream = iter->data;
879 iter = g_slist_next (iter); /* do it ourselves as we want it done in the beginning of the loop */
880 if (!gst_mss_demux_expose_stream (mssdemux, stream)) {
882 gst_element_remove_pad (GST_ELEMENT_CAST (mssdemux), stream->pad);
883 gst_mss_demux_stream_free (stream);
884 mssdemux->streams = g_slist_delete_link (mssdemux->streams, current);
888 if (!mssdemux->streams) {
890 GST_WARNING_OBJECT (mssdemux, "Couldn't identify the caps for any of the "
891 "streams found in the manifest");
892 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
893 (_("This file contains no playable streams.")),
894 ("No known stream formats found at the Manifest"));
898 gst_element_no_more_pads (GST_ELEMENT_CAST (mssdemux));
903 gst_mss_demux_reload_manifest (GstMssDemux * mssdemux)
905 GstUriDownloader *downloader;
906 GstFragment *manifest_data;
907 GstBuffer *manifest_buffer;
909 downloader = gst_uri_downloader_new ();
912 gst_uri_downloader_fetch_uri (downloader, mssdemux->manifest_uri);
913 manifest_buffer = gst_fragment_get_buffer (manifest_data);
914 g_object_unref (manifest_data);
916 gst_mss_manifest_reload_fragments (mssdemux->manifest, manifest_buffer);
917 gst_buffer_replace (&mssdemux->manifest_buffer, manifest_buffer);
918 gst_buffer_unref (manifest_buffer);
920 g_object_unref (downloader);
924 gst_mss_demux_get_download_bitrate (GstMssDemux * mssdemux)
930 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
931 GstMssDemuxStream *stream = iter->data;
933 total += gst_download_rate_get_current_rate (&stream->download_rate);
937 return total / count;
941 gst_mss_demux_all_streams_have_data (GstMssDemux * mssdemux)
945 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
946 GstMssDemuxStream *stream = iter->data;
948 if (!stream->have_data)
956 gst_mss_demux_reconfigure (GstMssDemux * mssdemux)
958 GSList *oldpads = NULL;
961 gboolean changed = FALSE;
965 if (!gst_mss_demux_all_streams_have_data (mssdemux))
968 gst_mss_demux_stop_tasks (mssdemux, TRUE);
969 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
970 GstMssDemuxStream *stream;
975 mssdemux->bitrate_limit *
976 gst_download_rate_get_current_rate (&stream->download_rate);
977 if (mssdemux->connection_speed) {
978 new_bitrate = MIN (mssdemux->connection_speed, new_bitrate);
981 GST_DEBUG_OBJECT (mssdemux, "Current stream %s download bitrate %llu",
982 GST_PAD_NAME (stream->pad), new_bitrate);
984 if (gst_mss_stream_select_bitrate (stream->manifest_stream, new_bitrate)) {
986 GST_INFO_OBJECT (mssdemux, "Stream %s changed bitrate to %llu",
987 GST_PAD_NAME (stream->pad),
988 gst_mss_stream_get_current_bitrate (stream->manifest_stream));
993 GstClockTime newseg_ts = GST_CLOCK_TIME_NONE;
995 GST_DEBUG_OBJECT (mssdemux,
996 "Bitrates have changed, creating new pad group");
997 /* if we changed the bitrate, we need to add new pads */
998 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
999 GstMssDemuxStream *stream = iter->data;
1000 GstPad *oldpad = stream->pad;
1001 GstClockTime ts = GST_CLOCK_TIME_NONE;
1004 oldpads = g_slist_prepend (oldpads, oldpad);
1006 /* since we are flushing the queue, get the next un-pushed timestamp to seek
1008 gst_data_queue_set_flushing (stream->dataqueue, FALSE);
1009 if (!gst_data_queue_is_empty (stream->dataqueue)) {
1010 GstDataQueueItem *item = NULL;
1012 while (!gst_data_queue_is_empty (stream->dataqueue)
1013 && !GST_CLOCK_TIME_IS_VALID (ts)) {
1014 gst_data_queue_pop (stream->dataqueue, &item);
1017 g_assert_not_reached ();
1021 if (GST_IS_BUFFER (item->object)) {
1022 GstBuffer *buffer = GST_BUFFER_CAST (item->object);
1024 ts = GST_BUFFER_TIMESTAMP (buffer);
1026 item->destroy (item);
1030 if (!GST_CLOCK_TIME_IS_VALID (ts)) {
1031 ts = gst_mss_stream_get_fragment_gst_timestamp
1032 (stream->manifest_stream);
1038 GST_DEBUG_OBJECT (mssdemux,
1039 "Seeking stream %p %s to ts %" GST_TIME_FORMAT, stream,
1040 GST_PAD_NAME (stream->pad), GST_TIME_ARGS (ts));
1041 gst_mss_stream_seek (stream->manifest_stream, ts);
1042 gst_data_queue_flush (stream->dataqueue);
1044 stream->pad = _create_pad (mssdemux, stream->manifest_stream);
1045 gst_mss_demux_expose_stream (mssdemux, stream);
1047 stream->have_data = FALSE;
1050 gst_element_no_more_pads (GST_ELEMENT (mssdemux));
1052 for (iter = oldpads; iter; iter = g_slist_next (iter)) {
1053 GstPad *oldpad = iter->data;
1055 gst_pad_push_event (oldpad, gst_event_new_eos ());
1056 gst_pad_set_active (oldpad, FALSE);
1057 gst_element_remove_pad (GST_ELEMENT (mssdemux), oldpad);
1058 gst_object_unref (oldpad);
1060 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
1061 GstMssDemuxStream *stream = iter->data;
1063 stream->pending_newsegment =
1064 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, newseg_ts, -1,
1068 gst_mss_demux_restart_tasks (mssdemux);
1072 _free_data_queue_item (gpointer obj)
1074 GstDataQueueItem *item = obj;
1076 gst_mini_object_unref (item->object);
1077 g_slice_free (GstDataQueueItem, item);
1081 gst_mss_demux_stream_store_object (GstMssDemuxStream * stream,
1082 GstMiniObject * obj)
1084 GstDataQueueItem *item;
1086 item = g_slice_new (GstDataQueueItem);
1087 item->object = (GstMiniObject *) obj;
1089 item->duration = 0; /* we don't care */
1091 item->visible = TRUE;
1093 item->destroy = (GDestroyNotify) _free_data_queue_item;
1095 if (!gst_data_queue_push (stream->dataqueue, item)) {
1096 GST_DEBUG_OBJECT (stream->parent, "Failed to store object %p", obj);
1097 item->destroy (item);
1101 static GstFlowReturn
1102 gst_mss_demux_stream_download_fragment (GstMssDemuxStream * stream,
1103 GstBuffer ** buffer)
1105 GstMssDemux *mssdemux = stream->parent;
1108 GstFragment *fragment;
1110 GstFlowReturn ret = GST_FLOW_OK;
1111 guint64 before_download, after_download;
1113 before_download = g_get_real_time ();
1115 GST_DEBUG_OBJECT (mssdemux, "Getting url for stream %p", stream);
1116 ret = gst_mss_stream_get_fragment_url (stream->manifest_stream, &path);
1119 break; /* all is good, let's go */
1120 case GST_FLOW_UNEXPECTED: /* EOS */
1121 if (gst_mss_manifest_is_live (mssdemux->manifest)) {
1122 gst_mss_demux_reload_manifest (mssdemux);
1125 return GST_FLOW_UNEXPECTED;
1126 case GST_FLOW_ERROR:
1134 GST_DEBUG_OBJECT (mssdemux, "Got url path '%s' for stream %p", path, stream);
1136 url = g_strdup_printf ("%s/%s", mssdemux->base_url, path);
1138 GST_DEBUG_OBJECT (mssdemux, "Got url '%s' for stream %p", url, stream);
1140 fragment = gst_uri_downloader_fetch_uri (stream->downloader, url);
1145 GST_INFO_OBJECT (mssdemux, "No fragment downloaded");
1146 /* TODO check if we are truly stoping */
1147 if (gst_mss_manifest_is_live (mssdemux->manifest)) {
1148 /* looks like there is no way of knowing when a live stream has ended
1149 * Have to assume we are falling behind and cause a manifest reload */
1152 return GST_FLOW_ERROR;
1155 _buffer = gst_fragment_get_buffer (fragment);
1156 _buffer = gst_buffer_make_metadata_writable (_buffer);
1157 gst_buffer_set_caps (_buffer, GST_PAD_CAPS (stream->pad));
1158 GST_BUFFER_TIMESTAMP (_buffer) =
1159 gst_mss_stream_get_fragment_gst_timestamp (stream->manifest_stream);
1160 GST_BUFFER_DURATION (_buffer) =
1161 gst_mss_stream_get_fragment_gst_duration (stream->manifest_stream);
1163 g_object_unref (fragment);
1168 after_download = g_get_real_time ();
1170 guint64 bitrate = (8 * GST_BUFFER_SIZE (_buffer) * 1000000LLU) /
1171 (after_download - before_download);
1173 GST_DEBUG_OBJECT (mssdemux, "Measured download bitrate: %s %llu bps",
1174 GST_PAD_NAME (stream->pad), bitrate);
1175 gst_download_rate_add_rate (&stream->download_rate,
1176 GST_BUFFER_SIZE (_buffer), 1000 * (after_download - before_download));
1178 GST_DEBUG_OBJECT (mssdemux,
1179 "Storing buffer for stream %p - %s. Timestamp: %" GST_TIME_FORMAT
1180 " Duration: %" GST_TIME_FORMAT,
1181 stream, GST_PAD_NAME (stream->pad),
1182 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (_buffer)),
1183 GST_TIME_ARGS (GST_BUFFER_DURATION (_buffer)));
1184 gst_mss_demux_stream_store_object (stream, GST_MINI_OBJECT_CAST (_buffer));
1188 GST_OBJECT_LOCK (mssdemux);
1189 mssdemux->update_bitrates = TRUE;
1190 GST_OBJECT_UNLOCK (mssdemux);
1196 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
1197 (_("Failed to get fragment URL.")),
1198 ("An error happened when getting fragment URL"));
1199 gst_task_pause (stream->download_task);
1200 return GST_FLOW_ERROR;
1204 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1205 gst_task_pause (stream->download_task);
1206 return GST_FLOW_ERROR;
1211 gst_mss_demux_download_loop (GstMssDemuxStream * stream)
1213 GstMssDemux *mssdemux = stream->parent;
1214 GstBuffer *buffer = NULL;
1217 GST_LOG_OBJECT (mssdemux, "download loop start %p", stream);
1219 ret = gst_mss_demux_stream_download_fragment (stream, &buffer);
1222 break; /* all is good, let's go */
1223 case GST_FLOW_UNEXPECTED: /* EOS */
1225 case GST_FLOW_ERROR:
1231 stream->download_error_count = 0;
1234 gst_mss_stream_advance_fragment (stream->manifest_stream);
1236 GST_LOG_OBJECT (mssdemux, "download loop end %p", stream);
1241 GST_DEBUG_OBJECT (mssdemux, "Storing EOS for pad %s:%s",
1242 GST_DEBUG_PAD_NAME (stream->pad));
1243 gst_mss_demux_stream_store_object (stream,
1244 GST_MINI_OBJECT_CAST (gst_event_new_eos ()));
1245 gst_task_pause (stream->download_task);
1250 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1251 if (++stream->download_error_count >= DOWNLOAD_RATE_MAX_HISTORY_LENGTH) {
1252 GST_ELEMENT_ERROR (mssdemux, RESOURCE, NOT_FOUND,
1253 (_("Couldn't download fragments")),
1254 ("fragment downloading has failed too much consecutive times"));
1260 static GstFlowReturn
1261 gst_mss_demux_select_latest_stream (GstMssDemux * mssdemux,
1262 GstMssDemuxStream ** stream)
1264 GstFlowReturn ret = GST_FLOW_OK;
1265 GstMssDemuxStream *current = NULL;
1266 GstClockTime cur_time = GST_CLOCK_TIME_NONE;
1269 if (!mssdemux->streams)
1270 return GST_FLOW_ERROR;
1272 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
1274 GstMssDemuxStream *other;
1275 GstDataQueueItem *item;
1282 if (!gst_data_queue_peek (other->dataqueue, &item)) {
1284 return GST_FLOW_WRONG_STATE;
1287 if (GST_IS_EVENT (item->object)) {
1288 /* events have higher priority */
1292 time = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (item->object));
1293 if (time < cur_time) {
1300 if (current == NULL)
1301 ret = GST_FLOW_UNEXPECTED;
1306 gst_mss_demux_stream_loop (GstMssDemux * mssdemux)
1308 GstMssDemuxStream *stream = NULL;
1310 GstMiniObject *object = NULL;
1311 GstDataQueueItem *item = NULL;
1313 GST_LOG_OBJECT (mssdemux, "Starting stream loop");
1315 GST_OBJECT_LOCK (mssdemux);
1316 if (mssdemux->update_bitrates) {
1317 mssdemux->update_bitrates = FALSE;
1318 GST_OBJECT_UNLOCK (mssdemux);
1320 GST_DEBUG_OBJECT (mssdemux,
1321 "Starting streams reconfiguration due to bitrate changes");
1322 gst_mss_demux_reconfigure (mssdemux);
1323 GST_DEBUG_OBJECT (mssdemux, "Finished streams reconfiguration");
1325 GST_OBJECT_UNLOCK (mssdemux);
1328 ret = gst_mss_demux_select_latest_stream (mssdemux, &stream);
1331 GST_DEBUG_OBJECT (mssdemux,
1332 "Stream loop selected %p stream of pad %s. %d - %s", stream,
1333 GST_PAD_NAME (stream->pad), ret, gst_flow_get_name (ret));
1335 GST_DEBUG_OBJECT (mssdemux, "No streams selected -> %d - %s", ret,
1336 gst_flow_get_name (ret));
1341 case GST_FLOW_ERROR:
1343 case GST_FLOW_UNEXPECTED:
1345 case GST_FLOW_WRONG_STATE:
1346 GST_DEBUG_OBJECT (mssdemux, "Wrong state, stopping task");
1349 g_assert_not_reached ();
1352 GST_LOG_OBJECT (mssdemux, "popping next item from queue for stream %p %s",
1353 stream, GST_PAD_NAME (stream->pad));
1354 if (gst_data_queue_pop (stream->dataqueue, &item)) {
1356 object = gst_mini_object_ref (item->object);
1357 item->destroy (item);
1359 GST_DEBUG_OBJECT (mssdemux,
1360 "Failed to get object from dataqueue on stream %p %s", stream,
1361 GST_PAD_NAME (stream->pad));
1365 if (G_UNLIKELY (stream->pending_newsegment)) {
1366 gst_pad_push_event (stream->pad, stream->pending_newsegment);
1367 stream->pending_newsegment = NULL;
1370 if (G_LIKELY (GST_IS_BUFFER (object))) {
1371 if (GST_BUFFER_TIMESTAMP (object) != stream->next_timestamp) {
1372 GST_ERROR_OBJECT (mssdemux, "Marking buffer %p as discont buffer:%"
1373 GST_TIME_FORMAT " != expected:%" GST_TIME_FORMAT, object,
1374 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (object)),
1375 GST_TIME_ARGS (stream->next_timestamp));
1376 GST_BUFFER_FLAG_SET (object, GST_BUFFER_FLAG_DISCONT);
1379 GST_DEBUG_OBJECT (mssdemux,
1380 "Pushing buffer %p %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
1381 " discont:%d on pad %s", object,
1382 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (object)),
1383 GST_TIME_ARGS (GST_BUFFER_DURATION (object)),
1384 GST_BUFFER_FLAG_IS_SET (object, GST_BUFFER_FLAG_DISCONT),
1385 GST_PAD_NAME (stream->pad));
1387 stream->next_timestamp =
1388 GST_BUFFER_TIMESTAMP (object) + GST_BUFFER_DURATION (object);
1390 stream->have_data = TRUE;
1391 ret = gst_pad_push (stream->pad, GST_BUFFER_CAST (object));
1392 } else if (GST_IS_EVENT (object)) {
1393 if (GST_EVENT_TYPE (object) == GST_EVENT_EOS)
1395 GST_DEBUG_OBJECT (mssdemux, "Pushing event %p on pad %s", object,
1396 GST_PAD_NAME (stream->pad));
1397 gst_pad_push_event (stream->pad, GST_EVENT_CAST (object));
1399 g_return_if_reached ();
1403 case GST_FLOW_UNEXPECTED:
1404 goto eos; /* EOS ? */
1405 case GST_FLOW_ERROR:
1407 case GST_FLOW_NOT_LINKED:
1408 break; /* TODO what to do here? pause the task or just keep pushing? */
1414 GST_LOG_OBJECT (mssdemux, "Stream loop end");
1419 GST_DEBUG_OBJECT (mssdemux, "EOS on all pads");
1420 gst_task_pause (mssdemux->stream_task);
1425 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1426 gst_task_pause (mssdemux->stream_task);
1431 GST_DEBUG_OBJECT (mssdemux, "Pausing streaming task");
1432 gst_task_pause (mssdemux->stream_task);