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;
319 gst_element_remove_pad (GST_ELEMENT_CAST (mssdemux), stream->pad);
320 gst_mss_demux_stream_free (stream);
322 g_slist_free (mssdemux->streams);
323 mssdemux->streams = NULL;
325 if (mssdemux->manifest) {
326 gst_mss_manifest_free (mssdemux->manifest);
327 mssdemux->manifest = NULL;
330 mssdemux->n_videos = mssdemux->n_audios = 0;
331 g_free (mssdemux->base_url);
332 mssdemux->base_url = NULL;
333 g_free (mssdemux->manifest_uri);
334 mssdemux->manifest_uri = NULL;
338 gst_mss_demux_dispose (GObject * object)
340 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (object);
342 gst_mss_demux_reset (mssdemux);
344 if (mssdemux->stream_task) {
345 gst_object_unref (mssdemux->stream_task);
346 g_static_rec_mutex_free (&mssdemux->stream_lock);
347 mssdemux->stream_task = NULL;
350 G_OBJECT_CLASS (parent_class)->dispose (object);
354 gst_mss_demux_set_property (GObject * object, guint prop_id,
355 const GValue * value, GParamSpec * pspec)
357 GstMssDemux *mssdemux = GST_MSS_DEMUX (object);
360 case PROP_CONNECTION_SPEED:
361 GST_OBJECT_LOCK (mssdemux);
362 mssdemux->connection_speed = g_value_get_uint (value) * 1000;
363 mssdemux->update_bitrates = TRUE;
364 GST_DEBUG_OBJECT (mssdemux, "Connection speed set to %llu",
365 mssdemux->connection_speed);
366 GST_OBJECT_UNLOCK (mssdemux);
368 case PROP_MAX_QUEUE_SIZE_BUFFERS:
369 mssdemux->data_queue_max_size = g_value_get_uint (value);
371 case PROP_BITRATE_LIMIT:
372 mssdemux->bitrate_limit = g_value_get_float (value);
375 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
381 gst_mss_demux_get_property (GObject * object, guint prop_id, GValue * value,
384 GstMssDemux *mssdemux = GST_MSS_DEMUX (object);
387 case PROP_CONNECTION_SPEED:
388 g_value_set_uint (value, mssdemux->connection_speed / 1000);
390 case PROP_MAX_QUEUE_SIZE_BUFFERS:
391 g_value_set_uint (value, mssdemux->data_queue_max_size);
393 case PROP_BITRATE_LIMIT:
394 g_value_set_float (value, mssdemux->bitrate_limit);
397 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
402 static GstStateChangeReturn
403 gst_mss_demux_change_state (GstElement * element, GstStateChange transition)
405 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (element);
406 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
408 switch (transition) {
409 case GST_STATE_CHANGE_PAUSED_TO_READY:
410 gst_mss_demux_reset (mssdemux);
412 case GST_STATE_CHANGE_READY_TO_NULL:
418 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
420 switch (transition) {
421 case GST_STATE_CHANGE_PAUSED_TO_READY:{
432 gst_mss_demux_chain (GstPad * pad, GstBuffer * buffer)
434 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (GST_PAD_PARENT (pad));
435 if (mssdemux->manifest_buffer == NULL)
436 mssdemux->manifest_buffer = buffer;
438 mssdemux->manifest_buffer =
439 gst_buffer_join (mssdemux->manifest_buffer, buffer);
441 GST_INFO_OBJECT (mssdemux, "Received manifest buffer, total size is %i bytes",
442 GST_BUFFER_SIZE (mssdemux->manifest_buffer));
448 gst_mss_demux_start (GstMssDemux * mssdemux)
452 GST_INFO_OBJECT (mssdemux, "Starting streams' tasks");
453 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
454 GstMssDemuxStream *stream = iter->data;
455 gst_task_start (stream->download_task);
458 gst_task_start (mssdemux->stream_task);
462 gst_mss_demux_push_src_event (GstMssDemux * mssdemux, GstEvent * event)
467 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
468 GstMssDemuxStream *stream = iter->data;
469 gst_event_ref (event);
470 ret = ret & gst_pad_push_event (stream->pad, event);
472 gst_event_unref (event);
477 gst_mss_demux_event (GstPad * pad, GstEvent * event)
479 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (GST_PAD_PARENT (pad));
480 gboolean forward = TRUE;
483 switch (GST_EVENT_TYPE (event)) {
484 case GST_EVENT_FLUSH_STOP:
485 gst_mss_demux_reset (mssdemux);
488 if (mssdemux->manifest_buffer == NULL) {
489 GST_WARNING_OBJECT (mssdemux, "Received EOS without a manifest.");
492 GST_INFO_OBJECT (mssdemux, "Received EOS");
494 if (gst_mss_demux_process_manifest (mssdemux))
495 gst_mss_demux_start (mssdemux);
503 ret = gst_pad_event_default (pad, event);
505 gst_event_unref (event);
512 gst_mss_demux_stop_tasks (GstMssDemux * mssdemux, gboolean immediate)
516 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
517 GstMssDemuxStream *stream = iter->data;
519 gst_data_queue_set_flushing (stream->dataqueue, TRUE);
522 gst_uri_downloader_cancel (stream->downloader);
523 gst_task_pause (stream->download_task);
525 gst_task_pause (mssdemux->stream_task);
527 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
528 GstMssDemuxStream *stream = iter->data;
529 g_static_rec_mutex_lock (&stream->download_lock);
531 g_static_rec_mutex_lock (&mssdemux->stream_lock);
535 gst_mss_demux_restart_tasks (GstMssDemux * mssdemux)
538 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
539 GstMssDemuxStream *stream = iter->data;
540 gst_uri_downloader_reset (stream->downloader);
541 g_static_rec_mutex_unlock (&stream->download_lock);
543 g_static_rec_mutex_unlock (&mssdemux->stream_lock);
544 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
545 GstMssDemuxStream *stream = iter->data;
547 gst_data_queue_set_flushing (stream->dataqueue, FALSE);
548 gst_task_start (stream->download_task);
550 gst_task_start (mssdemux->stream_task);
554 gst_mss_demux_src_event (GstPad * pad, GstEvent * event)
556 GstMssDemux *mssdemux;
558 mssdemux = GST_MSS_DEMUX (GST_PAD_PARENT (pad));
560 switch (event->type) {
566 GstSeekType start_type, stop_type;
568 GstEvent *newsegment;
571 GST_INFO_OBJECT (mssdemux, "Received GST_EVENT_SEEK");
573 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
576 if (format != GST_FORMAT_TIME)
579 GST_DEBUG_OBJECT (mssdemux,
580 "seek event, rate: %f start: %" GST_TIME_FORMAT " stop: %"
581 GST_TIME_FORMAT, rate, GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
583 if (flags & GST_SEEK_FLAG_FLUSH) {
584 GstEvent *flush = gst_event_new_flush_start ();
585 GST_DEBUG_OBJECT (mssdemux, "sending flush start");
587 gst_event_set_seqnum (flush, gst_event_get_seqnum (event));
588 gst_mss_demux_push_src_event (mssdemux, flush);
591 gst_mss_demux_stop_tasks (mssdemux, TRUE);
593 if (!gst_mss_manifest_seek (mssdemux->manifest, start)) {;
594 GST_WARNING_OBJECT (mssdemux, "Could not find seeked fragment");
599 gst_event_new_new_segment (FALSE, rate, format, start, stop, start);
600 gst_event_set_seqnum (newsegment, gst_event_get_seqnum (event));
601 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
602 GstMssDemuxStream *stream = iter->data;
605 gst_data_queue_flush (stream->dataqueue);
606 gst_event_ref (newsegment);
607 gst_event_replace (&stream->pending_newsegment, newsegment);
609 gst_event_unref (newsegment);
611 if (flags & GST_SEEK_FLAG_FLUSH) {
612 GstEvent *flush = gst_event_new_flush_stop ();
613 GST_DEBUG_OBJECT (mssdemux, "sending flush stop");
615 gst_event_set_seqnum (flush, gst_event_get_seqnum (event));
616 gst_mss_demux_push_src_event (mssdemux, flush);
619 gst_mss_demux_restart_tasks (mssdemux);
621 gst_event_unref (event);
628 return gst_pad_event_default (pad, event);
631 gst_event_unref (event);
636 gst_mss_demux_src_query (GstPad * pad, GstQuery * query)
638 GstMssDemux *mssdemux;
639 gboolean ret = FALSE;
644 mssdemux = GST_MSS_DEMUX (GST_PAD_PARENT (pad));
646 switch (query->type) {
647 case GST_QUERY_DURATION:{
648 GstClockTime duration = -1;
651 gst_query_parse_duration (query, &fmt, NULL);
652 if (fmt == GST_FORMAT_TIME && mssdemux->manifest) {
653 /* TODO should we use the streams accumulated duration or the main manifest duration? */
654 duration = gst_mss_manifest_get_gst_duration (mssdemux->manifest);
656 if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0) {
657 gst_query_set_duration (query, GST_FORMAT_TIME, duration);
661 GST_INFO_OBJECT (mssdemux, "GST_QUERY_DURATION returns %s with duration %"
662 GST_TIME_FORMAT, ret ? "TRUE" : "FALSE", GST_TIME_ARGS (duration));
665 case GST_QUERY_LATENCY:{
666 gboolean live = FALSE;
668 live = mssdemux->manifest
669 && gst_mss_manifest_is_live (mssdemux->manifest);
671 gst_query_set_latency (query, live, 0, -1);
675 case GST_QUERY_SEEKING:{
679 if (mssdemux->manifest && gst_mss_manifest_is_live (mssdemux->manifest)) {
680 return FALSE; /* no live seeking */
683 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
684 GST_INFO_OBJECT (mssdemux, "Received GST_QUERY_SEEKING with format %d",
686 if (fmt == GST_FORMAT_TIME) {
687 GstClockTime duration;
688 duration = gst_mss_manifest_get_gst_duration (mssdemux->manifest);
689 if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0)
691 gst_query_set_seeking (query, fmt, TRUE, 0, stop);
693 GST_INFO_OBJECT (mssdemux, "GST_QUERY_SEEKING returning with stop : %"
694 GST_TIME_FORMAT, GST_TIME_ARGS (stop));
699 /* Don't fordward queries upstream because of the special nature of this
700 * "demuxer", which relies on the upstream element only to be fed
710 _set_src_pad_functions (GstPad * pad)
712 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_mss_demux_src_query));
713 gst_pad_set_event_function (pad, GST_DEBUG_FUNCPTR (gst_mss_demux_src_event));
717 _create_pad (GstMssDemux * mssdemux, GstMssStream * manifeststream)
720 GstPad *srcpad = NULL;
721 GstMssStreamType streamtype;
723 streamtype = gst_mss_stream_get_type (manifeststream);
724 GST_DEBUG_OBJECT (mssdemux, "Found stream of type: %s",
725 gst_mss_stream_type_name (streamtype));
727 /* TODO use stream's name/bitrate/index as the pad name? */
728 if (streamtype == MSS_STREAM_TYPE_VIDEO) {
729 name = g_strdup_printf ("video_%02u", mssdemux->n_videos++);
731 gst_pad_new_from_static_template (&gst_mss_demux_videosrc_template,
734 } else if (streamtype == MSS_STREAM_TYPE_AUDIO) {
735 name = g_strdup_printf ("audio_%02u", mssdemux->n_audios++);
737 gst_pad_new_from_static_template (&gst_mss_demux_audiosrc_template,
743 GST_WARNING_OBJECT (mssdemux, "Ignoring unknown type stream");
747 _set_src_pad_functions (srcpad);
752 gst_mss_demux_create_streams (GstMssDemux * mssdemux)
754 GSList *streams = gst_mss_manifest_get_streams (mssdemux->manifest);
757 if (streams == NULL) {
758 GST_INFO_OBJECT (mssdemux, "No streams found in the manifest");
759 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
760 (_("This file contains no playable streams.")),
761 ("no streams found at the Manifest"));
765 for (iter = streams; iter; iter = g_slist_next (iter)) {
766 GstPad *srcpad = NULL;
767 GstMssDemuxStream *stream = NULL;
768 GstMssStream *manifeststream = iter->data;
770 srcpad = _create_pad (mssdemux, manifeststream);
776 stream = gst_mss_demux_stream_new (mssdemux, manifeststream, srcpad);
777 gst_mss_stream_set_active (manifeststream, TRUE);
778 mssdemux->streams = g_slist_append (mssdemux->streams, stream);
781 /* select initial bitrates */
782 GST_OBJECT_LOCK (mssdemux);
783 GST_INFO_OBJECT (mssdemux, "Changing max bitrate to %llu",
784 mssdemux->connection_speed);
785 gst_mss_manifest_change_bitrate (mssdemux->manifest,
786 mssdemux->connection_speed);
787 mssdemux->update_bitrates = FALSE;
788 GST_OBJECT_UNLOCK (mssdemux);
792 gst_mss_demux_expose_stream (GstMssDemux * mssdemux, GstMssDemuxStream * stream)
796 GstPad *pad = stream->pad;
798 media_caps = gst_mss_stream_get_caps (stream->manifest_stream);
801 caps = gst_caps_new_simple ("video/quicktime", "variant", G_TYPE_STRING,
802 "mss-fragmented", "timescale", G_TYPE_UINT64,
803 gst_mss_stream_get_timescale (stream->manifest_stream), "media-caps",
804 GST_TYPE_CAPS, media_caps, NULL);
805 gst_caps_unref (media_caps);
806 gst_pad_set_caps (pad, caps);
807 gst_caps_unref (caps);
809 gst_pad_set_active (pad, TRUE);
810 GST_INFO_OBJECT (mssdemux, "Adding srcpad %s:%s with caps %" GST_PTR_FORMAT,
811 GST_DEBUG_PAD_NAME (pad), caps);
812 gst_object_ref (pad);
813 gst_element_add_pad (GST_ELEMENT_CAST (mssdemux), pad);
815 GST_WARNING_OBJECT (mssdemux,
816 "Couldn't get caps from manifest stream %p %s, not exposing it", stream,
817 GST_PAD_NAME (stream->pad));
824 gst_mss_demux_process_manifest (GstMssDemux * mssdemux)
831 g_return_val_if_fail (mssdemux->manifest_buffer != NULL, FALSE);
832 g_return_val_if_fail (mssdemux->manifest == NULL, FALSE);
834 query = gst_query_new_uri ();
835 ret = gst_pad_peer_query (mssdemux->sinkpad, query);
838 gst_query_parse_uri (query, &uri);
839 GST_INFO_OBJECT (mssdemux, "Upstream is using URI: %s", uri);
841 mssdemux->manifest_uri = g_strdup (uri);
842 baseurl_end = g_strrstr (uri, "/Manifest");
844 /* set the new end of the string */
845 baseurl_end[0] = '\0';
847 GST_WARNING_OBJECT (mssdemux, "Stream's URI didn't end with /manifest");
850 mssdemux->base_url = uri;
852 gst_query_unref (query);
854 if (mssdemux->base_url == NULL) {
855 GST_ELEMENT_ERROR (mssdemux, RESOURCE, NOT_FOUND,
856 (_("Couldn't get the Manifest's URI")),
857 ("need to get the manifest's URI from upstream elements"));
861 GST_INFO_OBJECT (mssdemux, "Received manifest: %i bytes",
862 GST_BUFFER_SIZE (mssdemux->manifest_buffer));
864 mssdemux->manifest = gst_mss_manifest_new (mssdemux->manifest_buffer);
865 if (!mssdemux->manifest) {
866 GST_ELEMENT_ERROR (mssdemux, STREAM, FORMAT, ("Bad manifest file"),
867 ("Xml manifest file couldn't be parsed"));
871 GST_INFO_OBJECT (mssdemux, "Live stream: %d",
872 gst_mss_manifest_is_live (mssdemux->manifest));
874 gst_mss_demux_create_streams (mssdemux);
875 for (iter = mssdemux->streams; iter;) {
876 GSList *current = iter;
877 GstMssDemuxStream *stream = iter->data;
878 iter = g_slist_next (iter); /* do it ourselves as we want it done in the beginning of the loop */
879 if (!gst_mss_demux_expose_stream (mssdemux, stream)) {
880 gst_mss_demux_stream_free (stream);
881 mssdemux->streams = g_slist_delete_link (mssdemux->streams, current);
885 if (!mssdemux->streams) {
887 GST_WARNING_OBJECT (mssdemux, "Couldn't identify the caps for any of the "
888 "streams found in the manifest");
889 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
890 (_("This file contains no playable streams.")),
891 ("No known stream formats found at the Manifest"));
895 gst_element_no_more_pads (GST_ELEMENT_CAST (mssdemux));
900 gst_mss_demux_reload_manifest (GstMssDemux * mssdemux)
902 GstUriDownloader *downloader;
903 GstFragment *manifest_data;
904 GstBuffer *manifest_buffer;
906 downloader = gst_uri_downloader_new ();
909 gst_uri_downloader_fetch_uri (downloader, mssdemux->manifest_uri);
910 manifest_buffer = gst_fragment_get_buffer (manifest_data);
911 g_object_unref (manifest_data);
913 gst_mss_manifest_reload_fragments (mssdemux->manifest, manifest_buffer);
914 gst_buffer_replace (&mssdemux->manifest_buffer, manifest_buffer);
915 gst_buffer_unref (manifest_buffer);
917 g_object_unref (downloader);
921 gst_mss_demux_get_download_bitrate (GstMssDemux * mssdemux)
927 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
928 GstMssDemuxStream *stream = iter->data;
930 total += gst_download_rate_get_current_rate (&stream->download_rate);
934 return total / count;
938 gst_mss_demux_all_streams_have_data (GstMssDemux * mssdemux)
942 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
943 GstMssDemuxStream *stream = iter->data;
945 if (!stream->have_data)
953 gst_mss_demux_reconfigure (GstMssDemux * mssdemux)
955 GSList *oldpads = NULL;
958 gboolean changed = FALSE;
962 if (!gst_mss_demux_all_streams_have_data (mssdemux))
965 gst_mss_demux_stop_tasks (mssdemux, TRUE);
966 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
967 GstMssDemuxStream *stream;
972 mssdemux->bitrate_limit *
973 gst_download_rate_get_current_rate (&stream->download_rate);
974 if (mssdemux->connection_speed) {
975 new_bitrate = MIN (mssdemux->connection_speed, new_bitrate);
978 GST_DEBUG_OBJECT (mssdemux, "Current stream %s download bitrate %llu",
979 GST_PAD_NAME (stream->pad), new_bitrate);
981 if (gst_mss_stream_select_bitrate (stream->manifest_stream, new_bitrate)) {
983 GST_INFO_OBJECT (mssdemux, "Stream %s changed bitrate to %llu",
984 GST_PAD_NAME (stream->pad),
985 gst_mss_stream_get_current_bitrate (stream->manifest_stream));
990 GstClockTime newseg_ts = GST_CLOCK_TIME_NONE;
992 GST_DEBUG_OBJECT (mssdemux,
993 "Bitrates have changed, creating new pad group");
994 /* if we changed the bitrate, we need to add new pads */
995 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
996 GstMssDemuxStream *stream = iter->data;
997 GstPad *oldpad = stream->pad;
998 GstClockTime ts = GST_CLOCK_TIME_NONE;
1000 oldpads = g_slist_prepend (oldpads, oldpad);
1002 /* since we are flushing the queue, get the next un-pushed timestamp to seek
1004 gst_data_queue_set_flushing (stream->dataqueue, FALSE);
1005 if (!gst_data_queue_is_empty (stream->dataqueue)) {
1006 GstDataQueueItem *item = NULL;
1008 while (!gst_data_queue_is_empty (stream->dataqueue)
1009 && !GST_CLOCK_TIME_IS_VALID (ts)) {
1010 gst_data_queue_pop (stream->dataqueue, &item);
1013 g_assert_not_reached ();
1017 if (GST_IS_BUFFER (item->object)) {
1018 GstBuffer *buffer = GST_BUFFER_CAST (item->object);
1020 ts = GST_BUFFER_TIMESTAMP (buffer);
1022 item->destroy (item);
1026 if (!GST_CLOCK_TIME_IS_VALID (ts)) {
1027 ts = gst_mss_stream_get_fragment_gst_timestamp
1028 (stream->manifest_stream);
1034 GST_DEBUG_OBJECT (mssdemux,
1035 "Seeking stream %p %s to ts %" GST_TIME_FORMAT, stream,
1036 GST_PAD_NAME (stream->pad), GST_TIME_ARGS (ts));
1037 gst_mss_stream_seek (stream->manifest_stream, ts);
1038 gst_data_queue_flush (stream->dataqueue);
1040 stream->pad = _create_pad (mssdemux, stream->manifest_stream);
1041 gst_mss_demux_expose_stream (mssdemux, stream);
1043 gst_pad_push_event (oldpad, gst_event_new_eos ());
1044 stream->have_data = FALSE;
1047 gst_element_no_more_pads (GST_ELEMENT (mssdemux));
1049 for (iter = oldpads; iter; iter = g_slist_next (iter)) {
1050 GstPad *oldpad = iter->data;
1052 gst_pad_set_active (oldpad, FALSE);
1053 gst_element_remove_pad (GST_ELEMENT (mssdemux), oldpad);
1054 gst_object_unref (oldpad);
1056 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
1057 GstMssDemuxStream *stream = iter->data;
1059 stream->pending_newsegment =
1060 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, newseg_ts, -1,
1064 gst_mss_demux_restart_tasks (mssdemux);
1068 _free_data_queue_item (gpointer obj)
1070 GstDataQueueItem *item = obj;
1072 gst_mini_object_unref (item->object);
1073 g_slice_free (GstDataQueueItem, item);
1077 gst_mss_demux_stream_store_object (GstMssDemuxStream * stream,
1078 GstMiniObject * obj)
1080 GstDataQueueItem *item;
1082 item = g_slice_new (GstDataQueueItem);
1083 item->object = (GstMiniObject *) obj;
1085 item->duration = 0; /* we don't care */
1087 item->visible = TRUE;
1089 item->destroy = (GDestroyNotify) _free_data_queue_item;
1091 if (!gst_data_queue_push (stream->dataqueue, item)) {
1092 GST_DEBUG_OBJECT (stream->parent, "Failed to store object %p", obj);
1093 item->destroy (item);
1097 static GstFlowReturn
1098 gst_mss_demux_stream_download_fragment (GstMssDemuxStream * stream,
1099 GstBuffer ** buffer)
1101 GstMssDemux *mssdemux = stream->parent;
1104 GstFragment *fragment;
1106 GstFlowReturn ret = GST_FLOW_OK;
1107 guint64 before_download, after_download;
1109 before_download = g_get_real_time ();
1111 GST_DEBUG_OBJECT (mssdemux, "Getting url for stream %p", stream);
1112 ret = gst_mss_stream_get_fragment_url (stream->manifest_stream, &path);
1115 break; /* all is good, let's go */
1116 case GST_FLOW_UNEXPECTED: /* EOS */
1117 if (gst_mss_manifest_is_live (mssdemux->manifest)) {
1118 gst_mss_demux_reload_manifest (mssdemux);
1121 return GST_FLOW_UNEXPECTED;
1122 case GST_FLOW_ERROR:
1130 GST_DEBUG_OBJECT (mssdemux, "Got url path '%s' for stream %p", path, stream);
1132 url = g_strdup_printf ("%s/%s", mssdemux->base_url, path);
1134 GST_DEBUG_OBJECT (mssdemux, "Got url '%s' for stream %p", url, stream);
1136 fragment = gst_uri_downloader_fetch_uri (stream->downloader, url);
1141 GST_INFO_OBJECT (mssdemux, "No fragment downloaded");
1142 /* TODO check if we are truly stoping */
1143 if (gst_mss_manifest_is_live (mssdemux->manifest)) {
1144 /* looks like there is no way of knowing when a live stream has ended
1145 * Have to assume we are falling behind and cause a manifest reload */
1148 return GST_FLOW_ERROR;
1151 _buffer = gst_fragment_get_buffer (fragment);
1152 _buffer = gst_buffer_make_metadata_writable (_buffer);
1153 gst_buffer_set_caps (_buffer, GST_PAD_CAPS (stream->pad));
1154 GST_BUFFER_TIMESTAMP (_buffer) =
1155 gst_mss_stream_get_fragment_gst_timestamp (stream->manifest_stream);
1156 GST_BUFFER_DURATION (_buffer) =
1157 gst_mss_stream_get_fragment_gst_duration (stream->manifest_stream);
1159 g_object_unref (fragment);
1164 after_download = g_get_real_time ();
1166 guint64 bitrate = (8 * GST_BUFFER_SIZE (_buffer) * 1000000LLU) /
1167 (after_download - before_download);
1169 GST_DEBUG_OBJECT (mssdemux, "Measured download bitrate: %s %llu bps",
1170 GST_PAD_NAME (stream->pad), bitrate);
1171 gst_download_rate_add_rate (&stream->download_rate,
1172 GST_BUFFER_SIZE (_buffer), 1000 * (after_download - before_download));
1174 GST_DEBUG_OBJECT (mssdemux,
1175 "Storing buffer for stream %p - %s. Timestamp: %" GST_TIME_FORMAT
1176 " Duration: %" GST_TIME_FORMAT,
1177 stream, GST_PAD_NAME (stream->pad),
1178 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (_buffer)),
1179 GST_TIME_ARGS (GST_BUFFER_DURATION (_buffer)));
1180 gst_mss_demux_stream_store_object (stream, GST_MINI_OBJECT_CAST (_buffer));
1184 GST_OBJECT_LOCK (mssdemux);
1185 mssdemux->update_bitrates = TRUE;
1186 GST_OBJECT_UNLOCK (mssdemux);
1192 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
1193 (_("Failed to get fragment URL.")),
1194 ("An error happened when getting fragment URL"));
1195 gst_task_pause (stream->download_task);
1196 return GST_FLOW_ERROR;
1200 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1201 gst_task_pause (stream->download_task);
1202 return GST_FLOW_ERROR;
1207 gst_mss_demux_download_loop (GstMssDemuxStream * stream)
1209 GstMssDemux *mssdemux = stream->parent;
1210 GstBuffer *buffer = NULL;
1213 GST_LOG_OBJECT (mssdemux, "download loop start %p", stream);
1215 ret = gst_mss_demux_stream_download_fragment (stream, &buffer);
1218 break; /* all is good, let's go */
1219 case GST_FLOW_UNEXPECTED: /* EOS */
1221 case GST_FLOW_ERROR:
1227 stream->download_error_count = 0;
1230 gst_mss_stream_advance_fragment (stream->manifest_stream);
1232 GST_LOG_OBJECT (mssdemux, "download loop end %p", stream);
1237 GST_DEBUG_OBJECT (mssdemux, "Storing EOS for pad %s:%s",
1238 GST_DEBUG_PAD_NAME (stream->pad));
1239 gst_mss_demux_stream_store_object (stream,
1240 GST_MINI_OBJECT_CAST (gst_event_new_eos ()));
1241 gst_task_pause (stream->download_task);
1246 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1247 if (++stream->download_error_count >= DOWNLOAD_RATE_MAX_HISTORY_LENGTH) {
1248 GST_ELEMENT_ERROR (mssdemux, RESOURCE, NOT_FOUND,
1249 (_("Couldn't download fragments")),
1250 ("fragment downloading has failed too much consecutive times"));
1256 static GstFlowReturn
1257 gst_mss_demux_select_latest_stream (GstMssDemux * mssdemux,
1258 GstMssDemuxStream ** stream)
1260 GstFlowReturn ret = GST_FLOW_OK;
1261 GstMssDemuxStream *current = NULL;
1262 GstClockTime cur_time = GST_CLOCK_TIME_NONE;
1265 if (!mssdemux->streams)
1266 return GST_FLOW_ERROR;
1268 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
1270 GstMssDemuxStream *other;
1271 GstDataQueueItem *item;
1278 if (!gst_data_queue_peek (other->dataqueue, &item)) {
1280 return GST_FLOW_WRONG_STATE;
1283 if (GST_IS_EVENT (item->object)) {
1284 /* events have higher priority */
1288 time = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (item->object));
1289 if (time < cur_time) {
1296 if (current == NULL)
1297 ret = GST_FLOW_UNEXPECTED;
1302 gst_mss_demux_stream_loop (GstMssDemux * mssdemux)
1304 GstMssDemuxStream *stream = NULL;
1306 GstMiniObject *object = NULL;
1307 GstDataQueueItem *item = NULL;
1309 GST_LOG_OBJECT (mssdemux, "Starting stream loop");
1311 GST_OBJECT_LOCK (mssdemux);
1312 if (mssdemux->update_bitrates) {
1313 mssdemux->update_bitrates = FALSE;
1314 GST_OBJECT_UNLOCK (mssdemux);
1316 GST_DEBUG_OBJECT (mssdemux,
1317 "Starting streams reconfiguration due to bitrate changes");
1318 gst_mss_demux_reconfigure (mssdemux);
1319 GST_DEBUG_OBJECT (mssdemux, "Finished streams reconfiguration");
1321 GST_OBJECT_UNLOCK (mssdemux);
1324 ret = gst_mss_demux_select_latest_stream (mssdemux, &stream);
1327 GST_DEBUG_OBJECT (mssdemux,
1328 "Stream loop selected %p stream of pad %s. %d - %s", stream,
1329 GST_PAD_NAME (stream->pad), ret, gst_flow_get_name (ret));
1331 GST_DEBUG_OBJECT (mssdemux, "No streams selected -> %d - %s", ret,
1332 gst_flow_get_name (ret));
1337 case GST_FLOW_ERROR:
1339 case GST_FLOW_UNEXPECTED:
1341 case GST_FLOW_WRONG_STATE:
1342 GST_DEBUG_OBJECT (mssdemux, "Wrong state, stopping task");
1345 g_assert_not_reached ();
1348 GST_LOG_OBJECT (mssdemux, "popping next item from queue for stream %p %s",
1349 stream, GST_PAD_NAME (stream->pad));
1350 if (gst_data_queue_pop (stream->dataqueue, &item)) {
1352 object = gst_mini_object_ref (item->object);
1353 item->destroy (item);
1355 GST_DEBUG_OBJECT (mssdemux,
1356 "Failed to get object from dataqueue on stream %p %s", stream,
1357 GST_PAD_NAME (stream->pad));
1361 if (G_UNLIKELY (stream->pending_newsegment)) {
1362 gst_pad_push_event (stream->pad, stream->pending_newsegment);
1363 stream->pending_newsegment = NULL;
1366 if (G_LIKELY (GST_IS_BUFFER (object))) {
1367 if (GST_BUFFER_TIMESTAMP (object) != stream->next_timestamp) {
1368 GST_ERROR_OBJECT (mssdemux, "Marking buffer %p as discont buffer:%"
1369 GST_TIME_FORMAT " != expected:%" GST_TIME_FORMAT, object,
1370 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (object)),
1371 GST_TIME_ARGS (stream->next_timestamp));
1372 GST_BUFFER_FLAG_SET (object, GST_BUFFER_FLAG_DISCONT);
1375 GST_DEBUG_OBJECT (mssdemux,
1376 "Pushing buffer %p %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
1377 " discont:%d on pad %s", object,
1378 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (object)),
1379 GST_TIME_ARGS (GST_BUFFER_DURATION (object)),
1380 GST_BUFFER_FLAG_IS_SET (object, GST_BUFFER_FLAG_DISCONT),
1381 GST_PAD_NAME (stream->pad));
1383 stream->next_timestamp =
1384 GST_BUFFER_TIMESTAMP (object) + GST_BUFFER_DURATION (object);
1386 stream->have_data = TRUE;
1387 ret = gst_pad_push (stream->pad, GST_BUFFER_CAST (object));
1388 } else if (GST_IS_EVENT (object)) {
1389 if (GST_EVENT_TYPE (object) == GST_EVENT_EOS)
1391 GST_DEBUG_OBJECT (mssdemux, "Pushing event %p on pad %s", object,
1392 GST_PAD_NAME (stream->pad));
1393 gst_pad_push_event (stream->pad, GST_EVENT_CAST (object));
1395 g_return_if_reached ();
1399 case GST_FLOW_UNEXPECTED:
1400 goto eos; /* EOS ? */
1401 case GST_FLOW_ERROR:
1403 case GST_FLOW_NOT_LINKED:
1404 break; /* TODO what to do here? pause the task or just keep pushing? */
1410 GST_LOG_OBJECT (mssdemux, "Stream loop end");
1415 GST_DEBUG_OBJECT (mssdemux, "EOS on all pads");
1416 gst_task_pause (mssdemux->stream_task);
1421 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1422 gst_task_pause (mssdemux->stream_task);
1427 GST_DEBUG_OBJECT (mssdemux, "Pausing streaming task");
1428 gst_task_pause (mssdemux->stream_task);