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
90 PROP_CONNECTION_SPEED,
91 PROP_MAX_QUEUE_SIZE_BUFFERS,
96 static GstStaticPadTemplate gst_mss_demux_sink_template =
97 GST_STATIC_PAD_TEMPLATE ("sink",
100 GST_STATIC_CAPS ("application/vnd.ms-sstr+xml")
103 static GstStaticPadTemplate gst_mss_demux_videosrc_template =
104 GST_STATIC_PAD_TEMPLATE ("video_%02u",
107 GST_STATIC_CAPS_ANY);
109 static GstStaticPadTemplate gst_mss_demux_audiosrc_template =
110 GST_STATIC_PAD_TEMPLATE ("audio_%02u",
113 GST_STATIC_CAPS_ANY);
115 GST_BOILERPLATE (GstMssDemux, gst_mss_demux, GstMssDemux, GST_TYPE_ELEMENT);
117 static void gst_mss_demux_dispose (GObject * object);
118 static void gst_mss_demux_set_property (GObject * object, guint prop_id,
119 const GValue * value, GParamSpec * pspec);
120 static void gst_mss_demux_get_property (GObject * object, guint prop_id,
121 GValue * value, GParamSpec * pspec);
122 static GstStateChangeReturn gst_mss_demux_change_state (GstElement * element,
123 GstStateChange transition);
124 static GstFlowReturn gst_mss_demux_chain (GstPad * pad, GstBuffer * buffer);
125 static GstFlowReturn gst_mss_demux_event (GstPad * pad, GstEvent * event);
127 static gboolean gst_mss_demux_src_query (GstPad * pad, GstQuery * query);
129 static void gst_mss_demux_download_loop (GstMssDemuxStream * stream);
130 static void gst_mss_demux_stream_loop (GstMssDemux * mssdemux);
132 static gboolean gst_mss_demux_process_manifest (GstMssDemux * mssdemux);
135 gst_mss_demux_base_init (gpointer klass)
137 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
139 gst_element_class_add_static_pad_template (element_class,
140 &gst_mss_demux_sink_template);
141 gst_element_class_add_static_pad_template (element_class,
142 &gst_mss_demux_videosrc_template);
143 gst_element_class_add_static_pad_template (element_class,
144 &gst_mss_demux_audiosrc_template);
145 gst_element_class_set_details_simple (element_class, "Smooth Streaming "
146 "demuxer", "Demuxer",
147 "Parse and demultiplex a Smooth Streaming manifest into audio and video "
148 "streams", "Thiago Santos <thiago.sousa.santos@collabora.com>");
150 GST_DEBUG_CATEGORY_INIT (mssdemux_debug, "mssdemux", 0, "mssdemux plugin");
154 gst_mss_demux_class_init (GstMssDemuxClass * klass)
156 GObjectClass *gobject_class;
157 GstElementClass *gstelement_class;
159 gobject_class = (GObjectClass *) klass;
160 gstelement_class = (GstElementClass *) klass;
162 gobject_class->dispose = gst_mss_demux_dispose;
163 gobject_class->set_property = gst_mss_demux_set_property;
164 gobject_class->get_property = gst_mss_demux_get_property;
166 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
167 g_param_spec_uint ("connection-speed", "Connection Speed",
168 "Network connection speed in kbps (0 = unknown)",
169 0, G_MAXUINT / 1000, DEFAULT_CONNECTION_SPEED,
170 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
172 g_object_class_install_property (gobject_class, PROP_MAX_QUEUE_SIZE_BUFFERS,
173 g_param_spec_uint ("max-queue-size-buffers", "Max queue size in buffers",
174 "Maximum buffers that can be stored in each internal stream queue "
175 "(0 = infinite)", 0, G_MAXUINT, DEFAULT_MAX_QUEUE_SIZE_BUFFERS,
176 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
178 g_object_class_install_property (gobject_class, PROP_BITRATE_LIMIT,
179 g_param_spec_float ("bitrate-limit",
180 "Bitrate limit in %",
181 "Limit of the available bitrate to use when switching to alternates.",
182 0, 1, DEFAULT_BITRATE_LIMIT,
183 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
185 gstelement_class->change_state =
186 GST_DEBUG_FUNCPTR (gst_mss_demux_change_state);
190 gst_mss_demux_init (GstMssDemux * mssdemux, GstMssDemuxClass * klass)
193 gst_pad_new_from_static_template (&gst_mss_demux_sink_template, "sink");
194 gst_pad_set_chain_function (mssdemux->sinkpad,
195 GST_DEBUG_FUNCPTR (gst_mss_demux_chain));
196 gst_pad_set_event_function (mssdemux->sinkpad,
197 GST_DEBUG_FUNCPTR (gst_mss_demux_event));
198 gst_element_add_pad (GST_ELEMENT_CAST (mssdemux), mssdemux->sinkpad);
200 g_static_rec_mutex_init (&mssdemux->stream_lock);
201 mssdemux->stream_task =
202 gst_task_create ((GstTaskFunction) gst_mss_demux_stream_loop, mssdemux);
203 gst_task_set_lock (mssdemux->stream_task, &mssdemux->stream_lock);
205 mssdemux->data_queue_max_size = DEFAULT_MAX_QUEUE_SIZE_BUFFERS;
206 mssdemux->bitrate_limit = DEFAULT_BITRATE_LIMIT;
210 _data_queue_check_full (GstDataQueue * queue, guint visible, guint bytes,
211 guint64 time, gpointer checkdata)
213 GstMssDemuxStream *stream = checkdata;
214 GstMssDemux *mssdemux = stream->parent;
216 if (mssdemux->data_queue_max_size == 0)
217 return FALSE; /* never full */
218 return visible >= mssdemux->data_queue_max_size;
221 static GstMssDemuxStream *
222 gst_mss_demux_stream_new (GstMssDemux * mssdemux,
223 GstMssStream * manifeststream, GstPad * srcpad)
225 GstMssDemuxStream *stream;
227 stream = g_new0 (GstMssDemuxStream, 1);
228 stream->downloader = gst_uri_downloader_new ();
229 stream->dataqueue = gst_data_queue_new (_data_queue_check_full, stream);
231 /* Downloading task */
232 g_static_rec_mutex_init (&stream->download_lock);
233 stream->download_task =
234 gst_task_create ((GstTaskFunction) gst_mss_demux_download_loop, stream);
235 gst_task_set_lock (stream->download_task, &stream->download_lock);
237 stream->pad = srcpad;
238 stream->manifest_stream = manifeststream;
239 stream->parent = mssdemux;
245 gst_mss_demux_stream_free (GstMssDemuxStream * stream)
247 if (stream->download_task) {
248 if (GST_TASK_STATE (stream->download_task) != GST_TASK_STOPPED) {
249 GST_DEBUG_OBJECT (stream->parent, "Leaving streaming task %s:%s",
250 GST_DEBUG_PAD_NAME (stream->pad));
251 gst_uri_downloader_cancel (stream->downloader);
252 gst_task_stop (stream->download_task);
253 g_static_rec_mutex_lock (&stream->download_lock);
254 g_static_rec_mutex_unlock (&stream->download_lock);
255 GST_LOG_OBJECT (stream->parent, "Waiting for task to finish");
256 gst_task_join (stream->download_task);
257 GST_LOG_OBJECT (stream->parent, "Finished");
259 gst_object_unref (stream->download_task);
260 g_static_rec_mutex_free (&stream->download_lock);
261 stream->download_task = NULL;
264 if (stream->pending_newsegment) {
265 gst_event_unref (stream->pending_newsegment);
266 stream->pending_newsegment = NULL;
270 if (stream->downloader != NULL) {
271 g_object_unref (stream->downloader);
272 stream->downloader = NULL;
274 if (stream->dataqueue) {
275 g_object_unref (stream->dataqueue);
276 stream->dataqueue = NULL;
279 gst_object_unref (stream->pad);
286 gst_mss_demux_reset (GstMssDemux * mssdemux)
290 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
291 GstMssDemuxStream *stream = iter->data;
292 if (stream->downloader)
293 gst_uri_downloader_cancel (stream->downloader);
295 gst_data_queue_set_flushing (stream->dataqueue, TRUE);
298 if (GST_TASK_STATE (mssdemux->stream_task) != GST_TASK_STOPPED) {
299 gst_task_stop (mssdemux->stream_task);
300 g_static_rec_mutex_lock (&mssdemux->stream_lock);
301 g_static_rec_mutex_unlock (&mssdemux->stream_lock);
302 gst_task_join (mssdemux->stream_task);
305 if (mssdemux->manifest_buffer) {
306 gst_buffer_unref (mssdemux->manifest_buffer);
307 mssdemux->manifest_buffer = NULL;
310 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
311 GstMssDemuxStream *stream = iter->data;
312 gst_element_remove_pad (GST_ELEMENT_CAST (mssdemux), stream->pad);
313 gst_mss_demux_stream_free (stream);
315 g_slist_free (mssdemux->streams);
316 mssdemux->streams = NULL;
318 if (mssdemux->manifest) {
319 gst_mss_manifest_free (mssdemux->manifest);
320 mssdemux->manifest = NULL;
323 mssdemux->n_videos = mssdemux->n_audios = 0;
324 g_free (mssdemux->base_url);
325 mssdemux->base_url = NULL;
326 g_free (mssdemux->manifest_uri);
327 mssdemux->manifest_uri = NULL;
331 gst_mss_demux_dispose (GObject * object)
333 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (object);
335 gst_mss_demux_reset (mssdemux);
337 if (mssdemux->stream_task) {
338 gst_object_unref (mssdemux->stream_task);
339 g_static_rec_mutex_free (&mssdemux->stream_lock);
340 mssdemux->stream_task = NULL;
343 G_OBJECT_CLASS (parent_class)->dispose (object);
347 gst_mss_demux_set_property (GObject * object, guint prop_id,
348 const GValue * value, GParamSpec * pspec)
350 GstMssDemux *mssdemux = GST_MSS_DEMUX (object);
353 case PROP_CONNECTION_SPEED:
354 GST_OBJECT_LOCK (mssdemux);
355 mssdemux->connection_speed = g_value_get_uint (value) * 1000;
356 mssdemux->update_bitrates = TRUE;
357 GST_DEBUG_OBJECT (mssdemux, "Connection speed set to %llu",
358 mssdemux->connection_speed);
359 GST_OBJECT_UNLOCK (mssdemux);
361 case PROP_MAX_QUEUE_SIZE_BUFFERS:
362 mssdemux->data_queue_max_size = g_value_get_uint (value);
364 case PROP_BITRATE_LIMIT:
365 mssdemux->bitrate_limit = g_value_get_float (value);
368 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
374 gst_mss_demux_get_property (GObject * object, guint prop_id, GValue * value,
377 GstMssDemux *mssdemux = GST_MSS_DEMUX (object);
380 case PROP_CONNECTION_SPEED:
381 g_value_set_uint (value, mssdemux->connection_speed / 1000);
383 case PROP_MAX_QUEUE_SIZE_BUFFERS:
384 g_value_set_uint (value, mssdemux->data_queue_max_size);
386 case PROP_BITRATE_LIMIT:
387 g_value_set_float (value, mssdemux->bitrate_limit);
390 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
395 static GstStateChangeReturn
396 gst_mss_demux_change_state (GstElement * element, GstStateChange transition)
398 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (element);
399 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
401 switch (transition) {
402 case GST_STATE_CHANGE_PAUSED_TO_READY:
403 gst_mss_demux_reset (mssdemux);
405 case GST_STATE_CHANGE_READY_TO_NULL:
411 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
413 switch (transition) {
414 case GST_STATE_CHANGE_PAUSED_TO_READY:{
425 gst_mss_demux_chain (GstPad * pad, GstBuffer * buffer)
427 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (GST_PAD_PARENT (pad));
428 if (mssdemux->manifest_buffer == NULL)
429 mssdemux->manifest_buffer = buffer;
431 mssdemux->manifest_buffer =
432 gst_buffer_join (mssdemux->manifest_buffer, buffer);
434 GST_INFO_OBJECT (mssdemux, "Received manifest buffer, total size is %i bytes",
435 GST_BUFFER_SIZE (mssdemux->manifest_buffer));
441 gst_mss_demux_start (GstMssDemux * mssdemux)
445 GST_INFO_OBJECT (mssdemux, "Starting streams' tasks");
446 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
447 GstMssDemuxStream *stream = iter->data;
448 gst_task_start (stream->download_task);
451 gst_task_start (mssdemux->stream_task);
455 gst_mss_demux_push_src_event (GstMssDemux * mssdemux, GstEvent * event)
460 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
461 GstMssDemuxStream *stream = iter->data;
462 gst_event_ref (event);
463 ret = ret & gst_pad_push_event (stream->pad, event);
465 gst_event_unref (event);
470 gst_mss_demux_event (GstPad * pad, GstEvent * event)
472 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (GST_PAD_PARENT (pad));
473 gboolean forward = TRUE;
476 switch (GST_EVENT_TYPE (event)) {
477 case GST_EVENT_FLUSH_STOP:
478 gst_mss_demux_reset (mssdemux);
481 if (mssdemux->manifest_buffer == NULL) {
482 GST_WARNING_OBJECT (mssdemux, "Received EOS without a manifest.");
485 GST_INFO_OBJECT (mssdemux, "Received EOS");
487 if (gst_mss_demux_process_manifest (mssdemux))
488 gst_mss_demux_start (mssdemux);
496 ret = gst_pad_event_default (pad, event);
498 gst_event_unref (event);
505 gst_mss_demux_stop_tasks (GstMssDemux * mssdemux, gboolean immediate)
509 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
510 GstMssDemuxStream *stream = iter->data;
512 gst_data_queue_set_flushing (stream->dataqueue, TRUE);
515 gst_uri_downloader_cancel (stream->downloader);
516 gst_task_pause (stream->download_task);
518 gst_task_pause (mssdemux->stream_task);
520 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
521 GstMssDemuxStream *stream = iter->data;
522 g_static_rec_mutex_lock (&stream->download_lock);
524 g_static_rec_mutex_lock (&mssdemux->stream_lock);
528 gst_mss_demux_restart_tasks (GstMssDemux * mssdemux)
531 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
532 GstMssDemuxStream *stream = iter->data;
533 gst_uri_downloader_reset (stream->downloader);
534 g_static_rec_mutex_unlock (&stream->download_lock);
536 g_static_rec_mutex_unlock (&mssdemux->stream_lock);
537 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
538 GstMssDemuxStream *stream = iter->data;
540 gst_data_queue_set_flushing (stream->dataqueue, FALSE);
541 gst_task_start (stream->download_task);
543 gst_task_start (mssdemux->stream_task);
547 gst_mss_demux_src_event (GstPad * pad, GstEvent * event)
549 GstMssDemux *mssdemux;
551 mssdemux = GST_MSS_DEMUX (GST_PAD_PARENT (pad));
553 switch (event->type) {
559 GstSeekType start_type, stop_type;
561 GstEvent *newsegment;
564 GST_INFO_OBJECT (mssdemux, "Received GST_EVENT_SEEK");
566 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
569 if (format != GST_FORMAT_TIME)
572 GST_DEBUG_OBJECT (mssdemux,
573 "seek event, rate: %f start: %" GST_TIME_FORMAT " stop: %"
574 GST_TIME_FORMAT, rate, GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
576 if (flags & GST_SEEK_FLAG_FLUSH) {
577 GstEvent *flush = gst_event_new_flush_start ();
578 GST_DEBUG_OBJECT (mssdemux, "sending flush start");
580 gst_event_set_seqnum (flush, gst_event_get_seqnum (event));
581 gst_mss_demux_push_src_event (mssdemux, flush);
584 gst_mss_demux_stop_tasks (mssdemux, TRUE);
586 if (!gst_mss_manifest_seek (mssdemux->manifest, start)) {;
587 GST_WARNING_OBJECT (mssdemux, "Could not find seeked fragment");
592 gst_event_new_new_segment (FALSE, rate, format, start, stop, start);
593 gst_event_set_seqnum (newsegment, gst_event_get_seqnum (event));
594 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
595 GstMssDemuxStream *stream = iter->data;
598 gst_data_queue_flush (stream->dataqueue);
599 gst_event_ref (newsegment);
600 gst_event_replace (&stream->pending_newsegment, newsegment);
602 gst_event_unref (newsegment);
604 if (flags & GST_SEEK_FLAG_FLUSH) {
605 GstEvent *flush = gst_event_new_flush_stop ();
606 GST_DEBUG_OBJECT (mssdemux, "sending flush stop");
608 gst_event_set_seqnum (flush, gst_event_get_seqnum (event));
609 gst_mss_demux_push_src_event (mssdemux, flush);
612 gst_mss_demux_restart_tasks (mssdemux);
614 gst_event_unref (event);
621 return gst_pad_event_default (pad, event);
624 gst_event_unref (event);
629 gst_mss_demux_src_query (GstPad * pad, GstQuery * query)
631 GstMssDemux *mssdemux;
632 gboolean ret = FALSE;
637 mssdemux = GST_MSS_DEMUX (GST_PAD_PARENT (pad));
639 switch (query->type) {
640 case GST_QUERY_DURATION:{
641 GstClockTime duration = -1;
644 gst_query_parse_duration (query, &fmt, NULL);
645 if (fmt == GST_FORMAT_TIME && mssdemux->manifest) {
646 /* TODO should we use the streams accumulated duration or the main manifest duration? */
647 duration = gst_mss_manifest_get_gst_duration (mssdemux->manifest);
649 if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0) {
650 gst_query_set_duration (query, GST_FORMAT_TIME, duration);
654 GST_INFO_OBJECT (mssdemux, "GST_QUERY_DURATION returns %s with duration %"
655 GST_TIME_FORMAT, ret ? "TRUE" : "FALSE", GST_TIME_ARGS (duration));
658 case GST_QUERY_LATENCY:{
659 gboolean live = FALSE;
661 live = mssdemux->manifest
662 && gst_mss_manifest_is_live (mssdemux->manifest);
664 gst_query_set_latency (query, live, 0, -1);
668 case GST_QUERY_SEEKING:{
672 if (mssdemux->manifest && gst_mss_manifest_is_live (mssdemux->manifest)) {
673 return FALSE; /* no live seeking */
676 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
677 GST_INFO_OBJECT (mssdemux, "Received GST_QUERY_SEEKING with format %d",
679 if (fmt == GST_FORMAT_TIME) {
680 GstClockTime duration;
681 duration = gst_mss_manifest_get_gst_duration (mssdemux->manifest);
682 if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0)
684 gst_query_set_seeking (query, fmt, TRUE, 0, stop);
686 GST_INFO_OBJECT (mssdemux, "GST_QUERY_SEEKING returning with stop : %"
687 GST_TIME_FORMAT, GST_TIME_ARGS (stop));
692 /* Don't fordward queries upstream because of the special nature of this
693 * "demuxer", which relies on the upstream element only to be fed
703 _set_src_pad_functions (GstPad * pad)
705 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_mss_demux_src_query));
706 gst_pad_set_event_function (pad, GST_DEBUG_FUNCPTR (gst_mss_demux_src_event));
710 _create_pad (GstMssDemux * mssdemux, GstMssStream * manifeststream)
713 GstPad *srcpad = NULL;
714 GstMssStreamType streamtype;
716 streamtype = gst_mss_stream_get_type (manifeststream);
717 GST_DEBUG_OBJECT (mssdemux, "Found stream of type: %s",
718 gst_mss_stream_type_name (streamtype));
720 /* TODO use stream's name/bitrate/index as the pad name? */
721 if (streamtype == MSS_STREAM_TYPE_VIDEO) {
722 name = g_strdup_printf ("video_%02u", mssdemux->n_videos++);
724 gst_pad_new_from_static_template (&gst_mss_demux_videosrc_template,
727 } else if (streamtype == MSS_STREAM_TYPE_AUDIO) {
728 name = g_strdup_printf ("audio_%02u", mssdemux->n_audios++);
730 gst_pad_new_from_static_template (&gst_mss_demux_audiosrc_template,
736 GST_WARNING_OBJECT (mssdemux, "Ignoring unknown type stream");
740 _set_src_pad_functions (srcpad);
745 gst_mss_demux_create_streams (GstMssDemux * mssdemux)
747 GSList *streams = gst_mss_manifest_get_streams (mssdemux->manifest);
750 if (streams == NULL) {
751 GST_INFO_OBJECT (mssdemux, "No streams found in the manifest");
752 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
753 (_("This file contains no playable streams.")),
754 ("no streams found at the Manifest"));
758 for (iter = streams; iter; iter = g_slist_next (iter)) {
759 GstPad *srcpad = NULL;
760 GstMssDemuxStream *stream = NULL;
761 GstMssStream *manifeststream = iter->data;
763 srcpad = _create_pad (mssdemux, manifeststream);
769 stream = gst_mss_demux_stream_new (mssdemux, manifeststream, srcpad);
770 gst_mss_stream_set_active (manifeststream, TRUE);
771 mssdemux->streams = g_slist_append (mssdemux->streams, stream);
774 /* select initial bitrates */
775 GST_OBJECT_LOCK (mssdemux);
776 GST_INFO_OBJECT (mssdemux, "Changing max bitrate to %llu",
777 mssdemux->connection_speed);
778 gst_mss_manifest_change_bitrate (mssdemux->manifest,
779 mssdemux->connection_speed);
780 mssdemux->update_bitrates = FALSE;
781 GST_OBJECT_UNLOCK (mssdemux);
785 gst_mss_demux_expose_stream (GstMssDemux * mssdemux, GstMssDemuxStream * stream)
789 GstPad *pad = stream->pad;
791 media_caps = gst_mss_stream_get_caps (stream->manifest_stream);
794 caps = gst_caps_new_simple ("video/quicktime", "variant", G_TYPE_STRING,
795 "mss-fragmented", "timescale", G_TYPE_UINT64,
796 gst_mss_stream_get_timescale (stream->manifest_stream), "media-caps",
797 GST_TYPE_CAPS, media_caps, NULL);
798 gst_caps_unref (media_caps);
799 gst_pad_set_caps (pad, caps);
800 gst_caps_unref (caps);
802 gst_pad_set_active (pad, TRUE);
803 GST_INFO_OBJECT (mssdemux, "Adding srcpad %s:%s with caps %" GST_PTR_FORMAT,
804 GST_DEBUG_PAD_NAME (pad), caps);
805 gst_object_ref (pad);
806 gst_element_add_pad (GST_ELEMENT_CAST (mssdemux), pad);
808 GST_WARNING_OBJECT (mssdemux,
809 "Couldn't get caps from manifest stream %p %s, not exposing it", stream,
810 GST_PAD_NAME (stream->pad));
817 gst_mss_demux_process_manifest (GstMssDemux * mssdemux)
824 g_return_val_if_fail (mssdemux->manifest_buffer != NULL, FALSE);
825 g_return_val_if_fail (mssdemux->manifest == NULL, FALSE);
827 query = gst_query_new_uri ();
828 ret = gst_pad_peer_query (mssdemux->sinkpad, query);
831 gst_query_parse_uri (query, &uri);
832 GST_INFO_OBJECT (mssdemux, "Upstream is using URI: %s", uri);
834 mssdemux->manifest_uri = g_strdup (uri);
835 baseurl_end = g_strrstr (uri, "/Manifest");
837 /* set the new end of the string */
838 baseurl_end[0] = '\0';
840 GST_WARNING_OBJECT (mssdemux, "Stream's URI didn't end with /manifest");
843 mssdemux->base_url = uri;
845 gst_query_unref (query);
847 if (mssdemux->base_url == NULL) {
848 GST_ELEMENT_ERROR (mssdemux, RESOURCE, NOT_FOUND,
849 (_("Couldn't get the Manifest's URI")),
850 ("need to get the manifest's URI from upstream elements"));
854 GST_INFO_OBJECT (mssdemux, "Received manifest: %i bytes",
855 GST_BUFFER_SIZE (mssdemux->manifest_buffer));
857 mssdemux->manifest = gst_mss_manifest_new (mssdemux->manifest_buffer);
858 if (!mssdemux->manifest) {
859 GST_ELEMENT_ERROR (mssdemux, STREAM, FORMAT, ("Bad manifest file"),
860 ("Xml manifest file couldn't be parsed"));
864 GST_INFO_OBJECT (mssdemux, "Live stream: %d",
865 gst_mss_manifest_is_live (mssdemux->manifest));
867 gst_mss_demux_create_streams (mssdemux);
868 for (iter = mssdemux->streams; iter;) {
869 GSList *current = iter;
870 GstMssDemuxStream *stream = iter->data;
871 iter = g_slist_next (iter); /* do it ourselves as we want it done in the beginning of the loop */
872 if (!gst_mss_demux_expose_stream (mssdemux, stream)) {
873 gst_mss_demux_stream_free (stream);
874 mssdemux->streams = g_slist_delete_link (mssdemux->streams, current);
878 if (!mssdemux->streams) {
880 GST_WARNING_OBJECT (mssdemux, "Couldn't identify the caps for any of the "
881 "streams found in the manifest");
882 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
883 (_("This file contains no playable streams.")),
884 ("No known stream formats found at the Manifest"));
888 gst_element_no_more_pads (GST_ELEMENT_CAST (mssdemux));
893 gst_mss_demux_reload_manifest (GstMssDemux * mssdemux)
895 GstUriDownloader *downloader;
896 GstFragment *manifest_data;
897 GstBuffer *manifest_buffer;
899 downloader = gst_uri_downloader_new ();
902 gst_uri_downloader_fetch_uri (downloader, mssdemux->manifest_uri);
903 manifest_buffer = gst_fragment_get_buffer (manifest_data);
904 g_object_unref (manifest_data);
906 gst_mss_manifest_reload_fragments (mssdemux->manifest, manifest_buffer);
907 gst_buffer_replace (&mssdemux->manifest_buffer, manifest_buffer);
908 gst_buffer_unref (manifest_buffer);
910 g_object_unref (downloader);
914 gst_mss_demux_get_download_bitrate (GstMssDemux * mssdemux)
920 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
921 GstMssDemuxStream *stream = iter->data;
923 total += stream->download_bitrate;
927 return total / count;
931 gst_mss_demux_all_streams_have_data (GstMssDemux * mssdemux)
935 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
936 GstMssDemuxStream *stream = iter->data;
938 if (!stream->have_data)
946 gst_mss_demux_reconfigure (GstMssDemux * mssdemux)
948 GSList *oldpads = NULL;
954 if (!gst_mss_demux_all_streams_have_data (mssdemux))
957 new_bitrate = 0.8 * gst_mss_demux_get_download_bitrate (mssdemux) / 1000;
958 if (mssdemux->connection_speed) {
959 new_bitrate = MIN (mssdemux->connection_speed, new_bitrate);
962 GST_DEBUG_OBJECT ("Current suggested bitrate: %llu", new_bitrate);
964 gst_mss_demux_stop_tasks (mssdemux, TRUE);
965 if (gst_mss_manifest_change_bitrate (mssdemux->manifest, new_bitrate)) {
966 GstClockTime newseg_ts = GST_CLOCK_TIME_NONE;
968 GST_INFO_OBJECT ("Switching to bitrate %llu", new_bitrate);
970 GST_DEBUG_OBJECT (mssdemux, "Creating new pad group");
971 /* if we changed the bitrate, we need to add new pads */
972 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
973 GstMssDemuxStream *stream = iter->data;
974 GstPad *oldpad = stream->pad;
975 GstClockTime ts = GST_CLOCK_TIME_NONE;
977 oldpads = g_slist_prepend (oldpads, oldpad);
979 /* since we are flushing the queue, get the next un-pushed timestamp to seek
981 gst_data_queue_set_flushing (stream->dataqueue, FALSE);
982 if (!gst_data_queue_is_empty (stream->dataqueue)) {
983 GstDataQueueItem *item = NULL;
985 while (!gst_data_queue_is_empty (stream->dataqueue)
986 && !GST_CLOCK_TIME_IS_VALID (ts)) {
987 gst_data_queue_pop (stream->dataqueue, &item);
990 g_assert_not_reached ();
994 if (GST_IS_BUFFER (item->object)) {
995 GstBuffer *buffer = GST_BUFFER_CAST (item->object);
997 ts = GST_BUFFER_TIMESTAMP (buffer);
999 item->destroy (item);
1003 if (!GST_CLOCK_TIME_IS_VALID (ts)) {
1004 ts = gst_mss_stream_get_fragment_gst_timestamp
1005 (stream->manifest_stream);
1011 GST_DEBUG_OBJECT (mssdemux,
1012 "Seeking stream %p %s to ts %" GST_TIME_FORMAT, stream,
1013 GST_PAD_NAME (stream->pad), GST_TIME_ARGS (ts));
1014 gst_mss_stream_seek (stream->manifest_stream, ts);
1015 gst_data_queue_flush (stream->dataqueue);
1017 stream->pad = _create_pad (mssdemux, stream->manifest_stream);
1018 gst_mss_demux_expose_stream (mssdemux, stream);
1020 gst_pad_push_event (oldpad, gst_event_new_eos ());
1021 stream->have_data = FALSE;
1024 gst_element_no_more_pads (GST_ELEMENT (mssdemux));
1026 for (iter = oldpads; iter; iter = g_slist_next (iter)) {
1027 GstPad *oldpad = iter->data;
1029 gst_pad_set_active (oldpad, FALSE);
1030 gst_element_remove_pad (GST_ELEMENT (mssdemux), oldpad);
1031 gst_object_unref (oldpad);
1033 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
1034 GstMssDemuxStream *stream = iter->data;
1036 stream->pending_newsegment =
1037 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, newseg_ts, -1,
1041 gst_mss_demux_restart_tasks (mssdemux);
1045 _free_data_queue_item (gpointer obj)
1047 GstDataQueueItem *item = obj;
1049 gst_mini_object_unref (item->object);
1050 g_slice_free (GstDataQueueItem, item);
1054 gst_mss_demux_stream_store_object (GstMssDemuxStream * stream,
1055 GstMiniObject * obj)
1057 GstDataQueueItem *item;
1059 item = g_slice_new (GstDataQueueItem);
1060 item->object = (GstMiniObject *) obj;
1062 item->duration = 0; /* we don't care */
1064 item->visible = TRUE;
1066 item->destroy = (GDestroyNotify) _free_data_queue_item;
1068 if (!gst_data_queue_push (stream->dataqueue, item)) {
1069 GST_DEBUG_OBJECT (stream->parent, "Failed to store object %p", obj);
1070 item->destroy (item);
1074 static GstFlowReturn
1075 gst_mss_demux_stream_download_fragment (GstMssDemuxStream * stream,
1076 GstBuffer ** buffer)
1078 GstMssDemux *mssdemux = stream->parent;
1081 GstFragment *fragment;
1083 GstFlowReturn ret = GST_FLOW_OK;
1084 guint64 before_download, after_download;
1086 before_download = g_get_real_time ();
1088 GST_DEBUG_OBJECT (mssdemux, "Getting url for stream %p", stream);
1089 ret = gst_mss_stream_get_fragment_url (stream->manifest_stream, &path);
1092 break; /* all is good, let's go */
1093 case GST_FLOW_UNEXPECTED: /* EOS */
1094 if (gst_mss_manifest_is_live (mssdemux->manifest)) {
1095 gst_mss_demux_reload_manifest (mssdemux);
1098 return GST_FLOW_UNEXPECTED;
1099 case GST_FLOW_ERROR:
1107 GST_DEBUG_OBJECT (mssdemux, "Got url path '%s' for stream %p", path, stream);
1109 url = g_strdup_printf ("%s/%s", mssdemux->base_url, path);
1111 GST_DEBUG_OBJECT (mssdemux, "Got url '%s' for stream %p", url, stream);
1113 fragment = gst_uri_downloader_fetch_uri (stream->downloader, url);
1118 GST_INFO_OBJECT (mssdemux, "No fragment downloaded");
1119 /* TODO check if we are truly stoping */
1120 if (gst_mss_manifest_is_live (mssdemux->manifest)) {
1121 /* looks like there is no way of knowing when a live stream has ended
1122 * Have to assume we are falling behind and cause a manifest reload */
1125 return GST_FLOW_ERROR;
1128 _buffer = gst_fragment_get_buffer (fragment);
1129 _buffer = gst_buffer_make_metadata_writable (_buffer);
1130 gst_buffer_set_caps (_buffer, GST_PAD_CAPS (stream->pad));
1131 GST_BUFFER_TIMESTAMP (_buffer) =
1132 gst_mss_stream_get_fragment_gst_timestamp (stream->manifest_stream);
1133 GST_BUFFER_DURATION (_buffer) =
1134 gst_mss_stream_get_fragment_gst_duration (stream->manifest_stream);
1136 g_object_unref (fragment);
1141 after_download = g_get_real_time ();
1143 guint64 bitrate = 8 * GST_BUFFER_SIZE (_buffer) /
1144 ((after_download - before_download) / 1000000ULL);
1146 GST_DEBUG_OBJECT (mssdemux, "Measured download bitrate: %s %llu bps",
1147 GST_PAD_NAME (stream->pad), bitrate);
1148 stream->download_bitrate = bitrate;
1150 GST_DEBUG_OBJECT (mssdemux,
1151 "Storing buffer for stream %p - %s. Timestamp: %" GST_TIME_FORMAT
1152 " Duration: %" GST_TIME_FORMAT,
1153 stream, GST_PAD_NAME (stream->pad),
1154 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (_buffer)),
1155 GST_TIME_ARGS (GST_BUFFER_DURATION (_buffer)));
1156 gst_mss_demux_stream_store_object (stream, GST_MINI_OBJECT_CAST (_buffer));
1160 GST_OBJECT_LOCK (mssdemux);
1161 mssdemux->update_bitrates = TRUE;
1162 GST_OBJECT_UNLOCK (mssdemux);
1168 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
1169 (_("Failed to get fragment URL.")),
1170 ("An error happened when getting fragment URL"));
1171 gst_task_pause (stream->download_task);
1172 return GST_FLOW_ERROR;
1176 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1177 gst_task_pause (stream->download_task);
1178 return GST_FLOW_ERROR;
1183 gst_mss_demux_download_loop (GstMssDemuxStream * stream)
1185 GstMssDemux *mssdemux = stream->parent;
1186 GstBuffer *buffer = NULL;
1189 GST_LOG_OBJECT (mssdemux, "download loop start %p", stream);
1191 ret = gst_mss_demux_stream_download_fragment (stream, &buffer);
1194 break; /* all is good, let's go */
1195 case GST_FLOW_UNEXPECTED: /* EOS */
1197 case GST_FLOW_ERROR:
1204 gst_mss_stream_advance_fragment (stream->manifest_stream);
1206 GST_LOG_OBJECT (mssdemux, "download loop end %p", stream);
1211 GST_DEBUG_OBJECT (mssdemux, "Storing EOS for pad %s:%s",
1212 GST_DEBUG_PAD_NAME (stream->pad));
1213 gst_mss_demux_stream_store_object (stream,
1214 GST_MINI_OBJECT_CAST (gst_event_new_eos ()));
1215 gst_task_pause (stream->download_task);
1220 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1221 gst_task_pause (stream->download_task);
1226 static GstFlowReturn
1227 gst_mss_demux_select_latest_stream (GstMssDemux * mssdemux,
1228 GstMssDemuxStream ** stream)
1230 GstFlowReturn ret = GST_FLOW_OK;
1231 GstMssDemuxStream *current = NULL;
1232 GstClockTime cur_time = GST_CLOCK_TIME_NONE;
1235 if (!mssdemux->streams)
1236 return GST_FLOW_ERROR;
1238 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
1240 GstMssDemuxStream *other;
1241 GstDataQueueItem *item;
1248 if (!gst_data_queue_peek (other->dataqueue, &item)) {
1250 return GST_FLOW_WRONG_STATE;
1253 if (GST_IS_EVENT (item->object)) {
1254 /* events have higher priority */
1258 time = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (item->object));
1259 if (time < cur_time) {
1266 if (current == NULL)
1267 ret = GST_FLOW_UNEXPECTED;
1272 gst_mss_demux_stream_loop (GstMssDemux * mssdemux)
1274 GstMssDemuxStream *stream = NULL;
1276 GstMiniObject *object = NULL;
1277 GstDataQueueItem *item = NULL;
1279 GST_LOG_OBJECT (mssdemux, "Starting stream loop");
1281 GST_OBJECT_LOCK (mssdemux);
1282 if (mssdemux->update_bitrates) {
1283 mssdemux->update_bitrates = FALSE;
1284 GST_OBJECT_UNLOCK (mssdemux);
1286 GST_DEBUG_OBJECT (mssdemux,
1287 "Starting streams reconfiguration due to bitrate changes");
1288 gst_mss_demux_reconfigure (mssdemux);
1289 GST_DEBUG_OBJECT (mssdemux, "Finished streams reconfiguration");
1291 GST_OBJECT_UNLOCK (mssdemux);
1294 ret = gst_mss_demux_select_latest_stream (mssdemux, &stream);
1297 GST_DEBUG_OBJECT (mssdemux,
1298 "Stream loop selected %p stream of pad %s. %d - %s", stream,
1299 GST_PAD_NAME (stream->pad), ret, gst_flow_get_name (ret));
1301 GST_DEBUG_OBJECT (mssdemux, "No streams selected -> %d - %s", ret,
1302 gst_flow_get_name (ret));
1307 case GST_FLOW_ERROR:
1309 case GST_FLOW_UNEXPECTED:
1311 case GST_FLOW_WRONG_STATE:
1312 GST_DEBUG_OBJECT (mssdemux, "Wrong state, stopping task");
1315 g_assert_not_reached ();
1318 GST_LOG_OBJECT (mssdemux, "popping next item from queue for stream %p %s",
1319 stream, GST_PAD_NAME (stream->pad));
1320 if (gst_data_queue_pop (stream->dataqueue, &item)) {
1322 object = gst_mini_object_ref (item->object);
1323 item->destroy (item);
1325 GST_DEBUG_OBJECT (mssdemux,
1326 "Failed to get object from dataqueue on stream %p %s", stream,
1327 GST_PAD_NAME (stream->pad));
1331 if (G_UNLIKELY (stream->pending_newsegment)) {
1332 gst_pad_push_event (stream->pad, stream->pending_newsegment);
1333 stream->pending_newsegment = NULL;
1336 if (G_LIKELY (GST_IS_BUFFER (object))) {
1337 if (GST_BUFFER_TIMESTAMP (object) != stream->next_timestamp) {
1338 GST_ERROR_OBJECT (mssdemux, "Marking buffer %p as discont buffer:%"
1339 GST_TIME_FORMAT " != expected:%" GST_TIME_FORMAT, object,
1340 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (object)),
1341 GST_TIME_ARGS (stream->next_timestamp));
1342 GST_BUFFER_FLAG_SET (object, GST_BUFFER_FLAG_DISCONT);
1345 GST_DEBUG_OBJECT (mssdemux,
1346 "Pushing buffer %p %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
1347 " discont:%d on pad %s", object,
1348 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (object)),
1349 GST_TIME_ARGS (GST_BUFFER_DURATION (object)),
1350 GST_BUFFER_FLAG_IS_SET (object, GST_BUFFER_FLAG_DISCONT),
1351 GST_PAD_NAME (stream->pad));
1353 stream->next_timestamp =
1354 GST_BUFFER_TIMESTAMP (object) + GST_BUFFER_DURATION (object);
1356 stream->have_data = TRUE;
1357 ret = gst_pad_push (stream->pad, GST_BUFFER_CAST (object));
1358 } else if (GST_IS_EVENT (object)) {
1359 if (GST_EVENT_TYPE (object) == GST_EVENT_EOS)
1361 GST_DEBUG_OBJECT (mssdemux, "Pushing event %p on pad %s", object,
1362 GST_PAD_NAME (stream->pad));
1363 gst_pad_push_event (stream->pad, GST_EVENT_CAST (object));
1365 g_return_if_reached ();
1369 case GST_FLOW_UNEXPECTED:
1370 goto eos; /* EOS ? */
1371 case GST_FLOW_ERROR:
1373 case GST_FLOW_NOT_LINKED:
1374 break; /* TODO what to do here? pause the task or just keep pushing? */
1380 GST_LOG_OBJECT (mssdemux, "Stream loop end");
1385 GST_DEBUG_OBJECT (mssdemux, "EOS on all pads");
1386 gst_task_pause (mssdemux->stream_task);
1391 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1392 gst_task_pause (mssdemux->stream_task);
1397 GST_DEBUG_OBJECT (mssdemux, "Pausing streaming task");
1398 gst_task_pause (mssdemux->stream_task);