2 * Copyright (C) 2012 Smart TV Alliance
3 * Author: Thiago Sousa Santos <thiago.sousa.santos@collabora.com>, Collabora Ltd.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * SECTION:element-mssdemux
26 * Demuxes a Microsoft's Smooth Streaming manifest into its audio and/or video streams.
34 * = Smooth streaming in a few lines
35 * A SS stream is defined by a xml manifest file. This file has a list of
36 * tracks (StreamIndex), each one can have multiple QualityLevels, that define
37 * different encoding/bitrates. When playing a track, only one of those
38 * QualityLevels can be active at a time (per stream).
40 * The StreamIndex defines a URL with {time} and {bitrate} tags that are
41 * replaced by values indicated by the fragment start times and the selected
42 * QualityLevel, that generates the fragments URLs.
44 * Another relevant detail is that the Isomedia fragments for smoothstreaming
45 * won't contains a 'moov' atom, nor a 'stsd', so there is no information
46 * about the media type/configuration on the fragments, it must be extracted
47 * from the Manifest and passed downstream. mssdemux does this via GstCaps.
49 * = How mssdemux works
50 * There is a gstmssmanifest.c utility that holds the manifest and parses
51 * and has functions to extract information from it. mssdemux received the
52 * manifest from its sink pad and starts processing it when it gets EOS.
54 * The Manifest is parsed and the streams are exposed, 1 pad for each, with
55 * a initially selected QualityLevel. Each stream starts its own GstTaks that
56 * is responsible for downloading fragments and storing in its own GstDataQueue.
58 * The mssdemux starts another GstTask, this one iterates through the streams
59 * and selects the fragment with the smaller timestamp to push and repeats this.
61 * When a new connection-speed is set, mssdemux evaluates the available
62 * QualityLevels and might decide to switch to another one. In this case it
63 * exposes new pads for each stream, pushes EOS to the old ones and removes
64 * them. This should make decodebin2 pad switching mechanism act and the
65 * switch would be smooth for the final user.
72 #include "gst/gst-i18n-plugin.h"
78 #include "gstmssdemux.h"
80 GST_DEBUG_CATEGORY (mssdemux_debug);
82 #define DEFAULT_CONNECTION_SPEED 0
83 #define DEFAULT_MAX_QUEUE_SIZE_BUFFERS 0
89 PROP_CONNECTION_SPEED,
90 PROP_MAX_QUEUE_SIZE_BUFFERS,
94 static GstStaticPadTemplate gst_mss_demux_sink_template =
95 GST_STATIC_PAD_TEMPLATE ("sink",
98 GST_STATIC_CAPS ("application/vnd.ms-sstr+xml")
101 static GstStaticPadTemplate gst_mss_demux_videosrc_template =
102 GST_STATIC_PAD_TEMPLATE ("video_%02u",
105 GST_STATIC_CAPS_ANY);
107 static GstStaticPadTemplate gst_mss_demux_audiosrc_template =
108 GST_STATIC_PAD_TEMPLATE ("audio_%02u",
111 GST_STATIC_CAPS_ANY);
113 GST_BOILERPLATE (GstMssDemux, gst_mss_demux, GstMssDemux, GST_TYPE_ELEMENT);
115 static void gst_mss_demux_dispose (GObject * object);
116 static void gst_mss_demux_set_property (GObject * object, guint prop_id,
117 const GValue * value, GParamSpec * pspec);
118 static void gst_mss_demux_get_property (GObject * object, guint prop_id,
119 GValue * value, GParamSpec * pspec);
120 static GstStateChangeReturn gst_mss_demux_change_state (GstElement * element,
121 GstStateChange transition);
122 static GstFlowReturn gst_mss_demux_chain (GstPad * pad, GstBuffer * buffer);
123 static GstFlowReturn gst_mss_demux_event (GstPad * pad, GstEvent * event);
125 static gboolean gst_mss_demux_src_query (GstPad * pad, GstQuery * query);
127 static void gst_mss_demux_download_loop (GstMssDemuxStream * stream);
128 static void gst_mss_demux_stream_loop (GstMssDemux * mssdemux);
130 static gboolean gst_mss_demux_process_manifest (GstMssDemux * mssdemux);
133 gst_mss_demux_base_init (gpointer klass)
135 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
137 gst_element_class_add_static_pad_template (element_class,
138 &gst_mss_demux_sink_template);
139 gst_element_class_add_static_pad_template (element_class,
140 &gst_mss_demux_videosrc_template);
141 gst_element_class_add_static_pad_template (element_class,
142 &gst_mss_demux_audiosrc_template);
143 gst_element_class_set_details_simple (element_class, "Smooth Streaming "
144 "demuxer", "Demuxer",
145 "Parse and demultiplex a Smooth Streaming manifest into audio and video "
146 "streams", "Thiago Santos <thiago.sousa.santos@collabora.com>");
148 GST_DEBUG_CATEGORY_INIT (mssdemux_debug, "mssdemux", 0, "mssdemux plugin");
152 gst_mss_demux_class_init (GstMssDemuxClass * klass)
154 GObjectClass *gobject_class;
155 GstElementClass *gstelement_class;
157 gobject_class = (GObjectClass *) klass;
158 gstelement_class = (GstElementClass *) klass;
160 parent_class = g_type_class_peek_parent (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 gstelement_class->change_state =
179 GST_DEBUG_FUNCPTR (gst_mss_demux_change_state);
183 gst_mss_demux_init (GstMssDemux * mssdemux, GstMssDemuxClass * klass)
186 gst_pad_new_from_static_template (&gst_mss_demux_sink_template, "sink");
187 gst_pad_set_chain_function (mssdemux->sinkpad,
188 GST_DEBUG_FUNCPTR (gst_mss_demux_chain));
189 gst_pad_set_event_function (mssdemux->sinkpad,
190 GST_DEBUG_FUNCPTR (gst_mss_demux_event));
191 gst_element_add_pad (GST_ELEMENT_CAST (mssdemux), mssdemux->sinkpad);
193 g_static_rec_mutex_init (&mssdemux->stream_lock);
194 mssdemux->stream_task =
195 gst_task_create ((GstTaskFunction) gst_mss_demux_stream_loop, mssdemux);
196 gst_task_set_lock (mssdemux->stream_task, &mssdemux->stream_lock);
198 mssdemux->data_queue_max_size = DEFAULT_MAX_QUEUE_SIZE_BUFFERS;
202 _data_queue_check_full (GstDataQueue * queue, guint visible, guint bytes,
203 guint64 time, gpointer checkdata)
205 GstMssDemuxStream *stream = checkdata;
206 GstMssDemux *mssdemux = stream->parent;
208 if (mssdemux->data_queue_max_size == 0)
209 return FALSE; /* never full */
210 return visible >= mssdemux->data_queue_max_size;
213 static GstMssDemuxStream *
214 gst_mss_demux_stream_new (GstMssDemux * mssdemux,
215 GstMssStream * manifeststream, GstPad * srcpad)
217 GstMssDemuxStream *stream;
219 stream = g_new0 (GstMssDemuxStream, 1);
220 stream->downloader = gst_uri_downloader_new ();
221 stream->dataqueue = gst_data_queue_new (_data_queue_check_full, stream);
223 /* Downloading task */
224 g_static_rec_mutex_init (&stream->download_lock);
225 stream->download_task =
226 gst_task_create ((GstTaskFunction) gst_mss_demux_download_loop, stream);
227 gst_task_set_lock (stream->download_task, &stream->download_lock);
229 stream->pad = srcpad;
230 stream->manifest_stream = manifeststream;
231 stream->parent = mssdemux;
237 gst_mss_demux_stream_free (GstMssDemuxStream * stream)
239 if (stream->download_task) {
240 if (GST_TASK_STATE (stream->download_task) != GST_TASK_STOPPED) {
241 GST_DEBUG_OBJECT (stream->parent, "Leaving streaming task %s:%s",
242 GST_DEBUG_PAD_NAME (stream->pad));
243 gst_task_stop (stream->download_task);
244 gst_uri_downloader_cancel (stream->downloader);
245 g_static_rec_mutex_lock (&stream->download_lock);
246 g_static_rec_mutex_unlock (&stream->download_lock);
247 GST_LOG_OBJECT (stream->parent, "Waiting for task to finish");
248 gst_task_join (stream->download_task);
249 GST_LOG_OBJECT (stream->parent, "Finished");
251 gst_object_unref (stream->download_task);
252 g_static_rec_mutex_free (&stream->download_lock);
253 stream->download_task = NULL;
256 if (stream->pending_newsegment) {
257 gst_event_unref (stream->pending_newsegment);
258 stream->pending_newsegment = NULL;
262 if (stream->downloader != NULL) {
263 g_object_unref (stream->downloader);
264 stream->downloader = NULL;
266 if (stream->dataqueue) {
267 g_object_unref (stream->dataqueue);
268 stream->dataqueue = NULL;
271 gst_object_unref (stream->pad);
278 gst_mss_demux_reset (GstMssDemux * mssdemux)
282 if (GST_TASK_STATE (mssdemux->stream_task) != GST_TASK_STOPPED) {
283 gst_task_stop (mssdemux->stream_task);
284 g_static_rec_mutex_lock (&mssdemux->stream_lock);
285 g_static_rec_mutex_unlock (&mssdemux->stream_lock);
286 gst_task_join (mssdemux->stream_task);
289 if (mssdemux->manifest_buffer) {
290 gst_buffer_unref (mssdemux->manifest_buffer);
291 mssdemux->manifest_buffer = NULL;
294 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
295 GstMssDemuxStream *stream = iter->data;
296 gst_element_remove_pad (GST_ELEMENT_CAST (mssdemux), stream->pad);
297 gst_mss_demux_stream_free (stream);
299 g_slist_free (mssdemux->streams);
300 mssdemux->streams = NULL;
302 if (mssdemux->manifest) {
303 gst_mss_manifest_free (mssdemux->manifest);
304 mssdemux->manifest = NULL;
307 mssdemux->n_videos = mssdemux->n_audios = 0;
308 g_free (mssdemux->base_url);
309 g_free (mssdemux->manifest_uri);
310 mssdemux->base_url = NULL;
314 gst_mss_demux_dispose (GObject * object)
316 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (object);
318 if (mssdemux->stream_task) {
319 gst_object_unref (mssdemux->stream_task);
320 g_static_rec_mutex_free (&mssdemux->stream_lock);
321 mssdemux->stream_task = NULL;
324 G_OBJECT_CLASS (parent_class)->dispose (object);
328 gst_mss_demux_set_property (GObject * object, guint prop_id,
329 const GValue * value, GParamSpec * pspec)
331 GstMssDemux *mssdemux = GST_MSS_DEMUX (object);
334 case PROP_CONNECTION_SPEED:
335 GST_OBJECT_LOCK (mssdemux);
336 mssdemux->connection_speed = g_value_get_uint (value) * 1000;
337 mssdemux->update_bitrates = TRUE;
338 GST_DEBUG_OBJECT (mssdemux, "Connection speed set to %llu",
339 mssdemux->connection_speed);
340 GST_OBJECT_UNLOCK (mssdemux);
342 case PROP_MAX_QUEUE_SIZE_BUFFERS:
343 mssdemux->data_queue_max_size = g_value_get_uint (value);
346 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
352 gst_mss_demux_get_property (GObject * object, guint prop_id, GValue * value,
355 GstMssDemux *mssdemux = GST_MSS_DEMUX (object);
358 case PROP_CONNECTION_SPEED:
359 g_value_set_uint (value, mssdemux->connection_speed / 1000);
361 case PROP_MAX_QUEUE_SIZE_BUFFERS:
362 g_value_set_uint (value, mssdemux->data_queue_max_size);
365 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
370 static GstStateChangeReturn
371 gst_mss_demux_change_state (GstElement * element, GstStateChange transition)
373 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (element);
374 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
376 switch (transition) {
377 case GST_STATE_CHANGE_PAUSED_TO_READY:
378 gst_mss_demux_reset (mssdemux);
380 case GST_STATE_CHANGE_READY_TO_NULL:
386 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
388 switch (transition) {
389 case GST_STATE_CHANGE_PAUSED_TO_READY:{
400 gst_mss_demux_chain (GstPad * pad, GstBuffer * buffer)
402 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (GST_PAD_PARENT (pad));
403 if (mssdemux->manifest_buffer == NULL)
404 mssdemux->manifest_buffer = buffer;
406 mssdemux->manifest_buffer =
407 gst_buffer_join (mssdemux->manifest_buffer, buffer);
413 gst_mss_demux_start (GstMssDemux * mssdemux)
417 GST_INFO_OBJECT (mssdemux, "Starting streams' tasks");
418 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
419 GstMssDemuxStream *stream = iter->data;
420 gst_task_start (stream->download_task);
423 gst_task_start (mssdemux->stream_task);
427 gst_mss_demux_push_src_event (GstMssDemux * mssdemux, GstEvent * event)
432 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
433 GstMssDemuxStream *stream = iter->data;
434 gst_event_ref (event);
435 ret = ret & gst_pad_push_event (stream->pad, event);
441 gst_mss_demux_event (GstPad * pad, GstEvent * event)
443 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (GST_PAD_PARENT (pad));
444 gboolean forward = TRUE;
447 switch (GST_EVENT_TYPE (event)) {
449 if (mssdemux->manifest_buffer == NULL) {
450 GST_WARNING_OBJECT (mssdemux, "Received EOS without a manifest.");
454 if (gst_mss_demux_process_manifest (mssdemux))
455 gst_mss_demux_start (mssdemux);
463 ret = gst_pad_event_default (pad, event);
465 gst_event_unref (event);
472 gst_mss_demux_stop_tasks (GstMssDemux * mssdemux, gboolean immediate)
476 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
477 GstMssDemuxStream *stream = iter->data;
479 gst_data_queue_set_flushing (stream->dataqueue, TRUE);
482 gst_uri_downloader_cancel (stream->downloader);
483 gst_task_pause (stream->download_task);
485 gst_task_pause (mssdemux->stream_task);
487 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
488 GstMssDemuxStream *stream = iter->data;
489 g_static_rec_mutex_lock (&stream->download_lock);
491 g_static_rec_mutex_lock (&mssdemux->stream_lock);
495 gst_mss_demux_restart_tasks (GstMssDemux * mssdemux)
498 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
499 GstMssDemuxStream *stream = iter->data;
500 g_static_rec_mutex_unlock (&stream->download_lock);
502 g_static_rec_mutex_unlock (&mssdemux->stream_lock);
503 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
504 GstMssDemuxStream *stream = iter->data;
506 gst_data_queue_set_flushing (stream->dataqueue, FALSE);
507 gst_task_start (stream->download_task);
509 gst_task_start (mssdemux->stream_task);
513 gst_mss_demux_src_event (GstPad * pad, GstEvent * event)
515 GstMssDemux *mssdemux;
517 mssdemux = GST_MSS_DEMUX (GST_PAD_PARENT (pad));
519 switch (event->type) {
525 GstSeekType start_type, stop_type;
527 GstEvent *newsegment;
530 GST_INFO_OBJECT (mssdemux, "Received GST_EVENT_SEEK");
532 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
535 if (format != GST_FORMAT_TIME)
538 GST_DEBUG_OBJECT (mssdemux,
539 "seek event, rate: %f start: %" GST_TIME_FORMAT " stop: %"
540 GST_TIME_FORMAT, rate, GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
542 if (flags & GST_SEEK_FLAG_FLUSH) {
543 GstEvent *flush = gst_event_new_flush_start ();
544 GST_DEBUG_OBJECT (mssdemux, "sending flush start");
546 gst_event_set_seqnum (flush, gst_event_get_seqnum (event));
547 gst_mss_demux_push_src_event (mssdemux, flush);
548 gst_event_unref (flush);
551 gst_mss_demux_stop_tasks (mssdemux, TRUE);
553 if (!gst_mss_manifest_seek (mssdemux->manifest, start)) {;
554 GST_WARNING_OBJECT (mssdemux, "Could not find seeked fragment");
559 gst_event_new_new_segment (FALSE, rate, format, start, stop, start);
560 gst_event_set_seqnum (newsegment, gst_event_get_seqnum (event));
561 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
562 GstMssDemuxStream *stream = iter->data;
565 gst_data_queue_flush (stream->dataqueue);
566 stream->pending_newsegment = gst_event_ref (newsegment);
568 gst_event_unref (newsegment);
570 if (flags & GST_SEEK_FLAG_FLUSH) {
571 GstEvent *flush = gst_event_new_flush_stop ();
572 GST_DEBUG_OBJECT (mssdemux, "sending flush stop");
574 gst_event_set_seqnum (flush, gst_event_get_seqnum (event));
575 gst_mss_demux_push_src_event (mssdemux, flush);
576 gst_event_unref (flush);
579 gst_mss_demux_restart_tasks (mssdemux);
587 return gst_pad_event_default (pad, event);
591 gst_mss_demux_src_query (GstPad * pad, GstQuery * query)
593 GstMssDemux *mssdemux;
594 gboolean ret = FALSE;
599 mssdemux = GST_MSS_DEMUX (GST_PAD_PARENT (pad));
601 switch (query->type) {
602 case GST_QUERY_DURATION:{
603 GstClockTime duration = -1;
606 gst_query_parse_duration (query, &fmt, NULL);
607 if (fmt == GST_FORMAT_TIME && mssdemux->manifest) {
608 /* TODO should we use the streams accumulated duration or the main manifest duration? */
609 duration = gst_mss_manifest_get_gst_duration (mssdemux->manifest);
611 if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0) {
612 gst_query_set_duration (query, GST_FORMAT_TIME, duration);
616 GST_INFO_OBJECT (mssdemux, "GST_QUERY_DURATION returns %s with duration %"
617 GST_TIME_FORMAT, ret ? "TRUE" : "FALSE", GST_TIME_ARGS (duration));
620 case GST_QUERY_LATENCY:{
621 gboolean live = FALSE;
623 live = mssdemux->manifest
624 && gst_mss_manifest_is_live (mssdemux->manifest);
626 gst_query_set_latency (query, live, 0, -1);
630 case GST_QUERY_SEEKING:{
634 if (mssdemux->manifest && gst_mss_manifest_is_live (mssdemux->manifest)) {
635 return FALSE; /* no live seeking */
638 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
639 GST_INFO_OBJECT (mssdemux, "Received GST_QUERY_SEEKING with format %d",
641 if (fmt == GST_FORMAT_TIME) {
642 GstClockTime duration;
643 duration = gst_mss_manifest_get_gst_duration (mssdemux->manifest);
644 if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0)
646 gst_query_set_seeking (query, fmt, TRUE, 0, stop);
648 GST_INFO_OBJECT (mssdemux, "GST_QUERY_SEEKING returning with stop : %"
649 GST_TIME_FORMAT, GST_TIME_ARGS (stop));
654 /* Don't fordward queries upstream because of the special nature of this
655 * "demuxer", which relies on the upstream element only to be fed
665 _set_src_pad_functions (GstPad * pad)
667 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_mss_demux_src_query));
668 gst_pad_set_event_function (pad, GST_DEBUG_FUNCPTR (gst_mss_demux_src_event));
672 _create_pad (GstMssDemux * mssdemux, GstMssStream * manifeststream)
675 GstPad *srcpad = NULL;
676 GstMssStreamType streamtype;
678 streamtype = gst_mss_stream_get_type (manifeststream);
679 GST_DEBUG_OBJECT (mssdemux, "Found stream of type: %s",
680 gst_mss_stream_type_name (streamtype));
682 /* TODO use stream's name/bitrate/index as the pad name? */
683 if (streamtype == MSS_STREAM_TYPE_VIDEO) {
684 name = g_strdup_printf ("video_%02u", mssdemux->n_videos++);
686 gst_pad_new_from_static_template (&gst_mss_demux_videosrc_template,
689 } else if (streamtype == MSS_STREAM_TYPE_AUDIO) {
690 name = g_strdup_printf ("audio_%02u", mssdemux->n_audios++);
692 gst_pad_new_from_static_template (&gst_mss_demux_audiosrc_template,
698 GST_WARNING_OBJECT (mssdemux, "Ignoring unknown type stream");
702 _set_src_pad_functions (srcpad);
707 gst_mss_demux_create_streams (GstMssDemux * mssdemux)
709 GSList *streams = gst_mss_manifest_get_streams (mssdemux->manifest);
712 if (streams == NULL) {
713 GST_INFO_OBJECT (mssdemux, "No streams found in the manifest");
714 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
715 (_("This file contains no playable streams.")),
716 ("no streams found at the Manifest"));
720 for (iter = streams; iter; iter = g_slist_next (iter)) {
721 GstPad *srcpad = NULL;
722 GstMssDemuxStream *stream = NULL;
723 GstMssStream *manifeststream = iter->data;
725 srcpad = _create_pad (mssdemux, manifeststream);
731 stream = gst_mss_demux_stream_new (mssdemux, manifeststream, srcpad);
732 gst_mss_stream_set_active (manifeststream, TRUE);
733 mssdemux->streams = g_slist_append (mssdemux->streams, stream);
736 /* select initial bitrates */
737 GST_OBJECT_LOCK (mssdemux);
738 GST_INFO_OBJECT (mssdemux, "Changing max bitrate to %llu",
739 mssdemux->connection_speed);
740 gst_mss_manifest_change_bitrate (mssdemux->manifest,
741 mssdemux->connection_speed);
742 mssdemux->update_bitrates = FALSE;
743 GST_OBJECT_UNLOCK (mssdemux);
747 gst_mss_demux_expose_stream (GstMssDemux * mssdemux, GstMssDemuxStream * stream)
751 GstPad *pad = stream->pad;
753 media_caps = gst_mss_stream_get_caps (stream->manifest_stream);
756 caps = gst_caps_new_simple ("video/quicktime", "variant", G_TYPE_STRING,
757 "mss-fragmented", "timescale", G_TYPE_UINT64,
758 gst_mss_stream_get_timescale (stream->manifest_stream), "media-caps",
759 GST_TYPE_CAPS, media_caps, NULL);
760 gst_caps_unref (media_caps);
761 gst_pad_set_caps (pad, caps);
762 gst_caps_unref (caps);
764 gst_pad_set_active (pad, TRUE);
765 GST_INFO_OBJECT (mssdemux, "Adding srcpad %s:%s with caps %" GST_PTR_FORMAT,
766 GST_DEBUG_PAD_NAME (pad), caps);
767 gst_object_ref (pad);
768 gst_element_add_pad (GST_ELEMENT_CAST (mssdemux), pad);
770 GST_WARNING_OBJECT (mssdemux,
771 "Couldn't get caps from manifest stream %p %s, not exposing it", stream,
772 GST_PAD_NAME (stream->pad));
779 gst_mss_demux_process_manifest (GstMssDemux * mssdemux)
786 g_return_val_if_fail (mssdemux->manifest_buffer != NULL, FALSE);
787 g_return_val_if_fail (mssdemux->manifest == NULL, FALSE);
789 query = gst_query_new_uri ();
790 ret = gst_pad_peer_query (mssdemux->sinkpad, query);
793 gst_query_parse_uri (query, &uri);
794 GST_INFO_OBJECT (mssdemux, "Upstream is using URI: %s", uri);
796 mssdemux->manifest_uri = g_strdup (uri);
797 baseurl_end = g_strrstr (uri, "/Manifest");
799 /* set the new end of the string */
800 baseurl_end[0] = '\0';
802 GST_WARNING_OBJECT (mssdemux, "Stream's URI didn't end with /manifest");
805 mssdemux->base_url = uri;
807 gst_query_unref (query);
809 if (mssdemux->base_url == NULL) {
810 GST_ELEMENT_ERROR (mssdemux, RESOURCE, NOT_FOUND,
811 (_("Couldn't get the Manifest's URI")),
812 ("need to get the manifest's URI from upstream elements"));
816 mssdemux->manifest = gst_mss_manifest_new (mssdemux->manifest_buffer);
817 if (!mssdemux->manifest) {
818 GST_ELEMENT_ERROR (mssdemux, STREAM, FORMAT, ("Bad manifest file"),
819 ("Xml manifest file couldn't be parsed"));
823 GST_INFO_OBJECT (mssdemux, "Live stream: %d",
824 gst_mss_manifest_is_live (mssdemux->manifest));
826 gst_mss_demux_create_streams (mssdemux);
827 for (iter = mssdemux->streams; iter;) {
828 GSList *current = iter;
829 GstMssDemuxStream *stream = iter->data;
830 iter = g_slist_next (iter); /* do it ourselves as we want it done in the beginning of the loop */
831 if (!gst_mss_demux_expose_stream (mssdemux, stream)) {
832 gst_mss_demux_stream_free (stream);
833 mssdemux->streams = g_slist_delete_link (mssdemux->streams, current);
837 if (!mssdemux->streams) {
839 GST_WARNING_OBJECT (mssdemux, "Couldn't identify the caps for any of the "
840 "streams found in the manifest");
841 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
842 (_("This file contains no playable streams.")),
843 ("No known stream formats found at the Manifest"));
847 gst_element_no_more_pads (GST_ELEMENT_CAST (mssdemux));
852 gst_mss_demux_reload_manifest (GstMssDemux * mssdemux)
854 GstUriDownloader *downloader;
855 GstFragment *manifest_data;
856 GstBuffer *manifest_buffer;
858 downloader = gst_uri_downloader_new ();
861 gst_uri_downloader_fetch_uri (downloader, mssdemux->manifest_uri);
862 manifest_buffer = gst_fragment_get_buffer (manifest_data);
863 g_object_unref (manifest_data);
865 gst_mss_manifest_reload_fragments (mssdemux->manifest, manifest_buffer);
866 gst_buffer_replace (&mssdemux->manifest_buffer, manifest_buffer);
867 gst_buffer_unref (manifest_buffer);
869 g_object_unref (downloader);
873 gst_mss_demux_reconfigure (GstMssDemux * mssdemux)
875 GSList *oldpads = NULL;
878 gst_mss_demux_stop_tasks (mssdemux, TRUE);
879 if (gst_mss_manifest_change_bitrate (mssdemux->manifest,
880 mssdemux->connection_speed)) {
881 GstClockTime newseg_ts = GST_CLOCK_TIME_NONE;
883 GST_DEBUG_OBJECT (mssdemux, "Creating new pad group");
884 /* if we changed the bitrate, we need to add new pads */
885 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
886 GstMssDemuxStream *stream = iter->data;
887 GstPad *oldpad = stream->pad;
888 GstClockTime ts = GST_CLOCK_TIME_NONE;
890 oldpads = g_slist_prepend (oldpads, oldpad);
892 /* since we are flushing the queue, get the next un-pushed timestamp to seek
894 gst_data_queue_set_flushing (stream->dataqueue, FALSE);
895 if (!gst_data_queue_is_empty (stream->dataqueue)) {
896 GstDataQueueItem *item = NULL;
898 while (!gst_data_queue_is_empty (stream->dataqueue)
899 && !GST_CLOCK_TIME_IS_VALID (ts)) {
900 gst_data_queue_pop (stream->dataqueue, &item);
903 g_assert_not_reached ();
907 if (GST_IS_BUFFER (item->object)) {
908 GstBuffer *buffer = GST_BUFFER_CAST (item->object);
910 ts = GST_BUFFER_TIMESTAMP (buffer);
912 item->destroy (item);
916 if (!GST_CLOCK_TIME_IS_VALID (ts)) {
917 ts = gst_mss_stream_get_fragment_gst_timestamp
918 (stream->manifest_stream);
924 GST_DEBUG_OBJECT (mssdemux,
925 "Seeking stream %p %s to ts %" GST_TIME_FORMAT, stream,
926 GST_PAD_NAME (stream->pad), GST_TIME_ARGS (ts));
927 gst_mss_stream_seek (stream->manifest_stream, ts);
928 gst_data_queue_flush (stream->dataqueue);
930 stream->pad = _create_pad (mssdemux, stream->manifest_stream);
931 gst_mss_demux_expose_stream (mssdemux, stream);
933 gst_pad_push_event (oldpad, gst_event_new_eos ());
936 gst_element_no_more_pads (GST_ELEMENT (mssdemux));
938 for (iter = oldpads; iter; iter = g_slist_next (iter)) {
939 GstPad *oldpad = iter->data;
941 gst_pad_set_active (oldpad, FALSE);
942 gst_element_remove_pad (GST_ELEMENT (mssdemux), oldpad);
943 gst_object_unref (oldpad);
945 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
946 GstMssDemuxStream *stream = iter->data;
948 stream->pending_newsegment =
949 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, newseg_ts, -1,
953 gst_mss_demux_restart_tasks (mssdemux);
957 _free_data_queue_item (gpointer obj)
959 GstDataQueueItem *item = obj;
961 gst_mini_object_unref (item->object);
962 g_slice_free (GstDataQueueItem, item);
966 gst_mss_demux_stream_store_object (GstMssDemuxStream * stream,
969 GstDataQueueItem *item;
971 item = g_slice_new (GstDataQueueItem);
972 item->object = (GstMiniObject *) obj;
974 item->duration = 0; /* we don't care */
976 item->visible = TRUE;
978 item->destroy = (GDestroyNotify) _free_data_queue_item;
980 if (!gst_data_queue_push (stream->dataqueue, item)) {
981 GST_DEBUG_OBJECT (stream->parent, "Failed to store object %p", obj);
982 gst_mini_object_unref (obj);
983 g_slice_free (GstDataQueueItem, item);
988 gst_mss_demux_stream_download_fragment (GstMssDemuxStream * stream,
991 GstMssDemux *mssdemux = stream->parent;
994 GstFragment *fragment;
996 GstFlowReturn ret = GST_FLOW_OK;
998 GST_DEBUG_OBJECT (mssdemux, "Getting url for stream %p", stream);
999 ret = gst_mss_stream_get_fragment_url (stream->manifest_stream, &path);
1002 break; /* all is good, let's go */
1003 case GST_FLOW_UNEXPECTED: /* EOS */
1004 if (gst_mss_manifest_is_live (mssdemux->manifest)) {
1005 gst_mss_demux_reload_manifest (mssdemux);
1008 return GST_FLOW_UNEXPECTED;
1009 case GST_FLOW_ERROR:
1017 GST_DEBUG_OBJECT (mssdemux, "Got url path '%s' for stream %p", path, stream);
1019 url = g_strdup_printf ("%s/%s", mssdemux->base_url, path);
1021 GST_DEBUG_OBJECT (mssdemux, "Got url '%s' for stream %p", url, stream);
1023 fragment = gst_uri_downloader_fetch_uri (stream->downloader, url);
1028 GST_INFO_OBJECT (mssdemux, "No fragment downloaded");
1029 /* TODO check if we are truly stoping */
1030 if (gst_mss_manifest_is_live (mssdemux->manifest)) {
1031 /* looks like there is no way of knowing when a live stream has ended
1032 * Have to assume we are falling behind and cause a manifest reload */
1035 return GST_FLOW_ERROR;
1038 _buffer = gst_fragment_get_buffer (fragment);
1039 _buffer = gst_buffer_make_metadata_writable (_buffer);
1040 gst_buffer_set_caps (_buffer, GST_PAD_CAPS (stream->pad));
1041 GST_BUFFER_TIMESTAMP (_buffer) =
1042 gst_mss_stream_get_fragment_gst_timestamp (stream->manifest_stream);
1043 GST_BUFFER_DURATION (_buffer) =
1044 gst_mss_stream_get_fragment_gst_duration (stream->manifest_stream);
1046 g_object_unref (fragment);
1052 GST_DEBUG_OBJECT (mssdemux,
1053 "Storing buffer for stream %p - %s. Timestamp: %" GST_TIME_FORMAT
1054 " Duration: %" GST_TIME_FORMAT,
1055 stream, GST_PAD_NAME (stream->pad),
1056 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (_buffer)),
1057 GST_TIME_ARGS (GST_BUFFER_DURATION (_buffer)));
1058 gst_mss_demux_stream_store_object (stream, GST_MINI_OBJECT_CAST (_buffer));
1065 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
1066 (_("Failed to get fragment URL.")),
1067 ("An error happened when getting fragment URL"));
1068 gst_task_stop (stream->download_task);
1069 return GST_FLOW_ERROR;
1073 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1074 gst_task_stop (stream->download_task);
1075 return GST_FLOW_ERROR;
1080 gst_mss_demux_download_loop (GstMssDemuxStream * stream)
1082 GstMssDemux *mssdemux = stream->parent;
1083 GstBuffer *buffer = NULL;
1086 GST_LOG_OBJECT (mssdemux, "download loop start %p", stream);
1089 ret = gst_mss_demux_stream_download_fragment (stream, &buffer);
1092 break; /* all is good, let's go */
1093 case GST_FLOW_UNEXPECTED: /* EOS */
1095 case GST_FLOW_ERROR:
1102 gst_mss_stream_advance_fragment (stream->manifest_stream);
1104 GST_LOG_OBJECT (mssdemux, "download loop end %p", stream);
1109 GST_DEBUG_OBJECT (mssdemux, "Storing EOS for pad %s:%s",
1110 GST_DEBUG_PAD_NAME (stream->pad));
1111 gst_mss_demux_stream_store_object (stream,
1112 GST_MINI_OBJECT_CAST (gst_event_new_eos ()));
1113 gst_task_stop (stream->download_task);
1118 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1119 gst_task_stop (stream->download_task);
1124 static GstFlowReturn
1125 gst_mss_demux_select_latest_stream (GstMssDemux * mssdemux,
1126 GstMssDemuxStream ** stream)
1128 GstFlowReturn ret = GST_FLOW_OK;
1129 GstMssDemuxStream *current = NULL;
1130 GstClockTime cur_time = GST_CLOCK_TIME_NONE;
1133 if (!mssdemux->streams)
1134 return GST_FLOW_ERROR;
1136 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
1138 GstMssDemuxStream *other;
1139 GstDataQueueItem *item;
1146 if (gst_data_queue_peek (other->dataqueue, &item)) {
1149 return GST_FLOW_WRONG_STATE;
1152 if (GST_IS_EVENT (item->object)) {
1153 /* events have higher priority */
1157 time = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (item->object));
1158 if (time < cur_time) {
1165 if (current == NULL)
1166 ret = GST_FLOW_UNEXPECTED;
1171 gst_mss_demux_stream_loop (GstMssDemux * mssdemux)
1173 GstMssDemuxStream *stream = NULL;
1175 GstMiniObject *object = NULL;
1176 GstDataQueueItem *item = NULL;
1178 GST_LOG_OBJECT (mssdemux, "Starting stream loop");
1180 GST_OBJECT_LOCK (mssdemux);
1181 if (mssdemux->update_bitrates) {
1182 mssdemux->update_bitrates = FALSE;
1183 GST_OBJECT_UNLOCK (mssdemux);
1185 GST_DEBUG_OBJECT (mssdemux,
1186 "Starting streams reconfiguration due to bitrate changes");
1187 gst_mss_demux_reconfigure (mssdemux);
1188 GST_DEBUG_OBJECT (mssdemux, "Finished streams reconfiguration");
1190 GST_OBJECT_UNLOCK (mssdemux);
1193 ret = gst_mss_demux_select_latest_stream (mssdemux, &stream);
1196 GST_DEBUG_OBJECT (mssdemux,
1197 "Stream loop selected %p stream of pad %s. %d - %s", stream,
1198 GST_PAD_NAME (stream->pad), ret, gst_flow_get_name (ret));
1200 GST_DEBUG_OBJECT (mssdemux, "No streams selected -> %d - %s", ret,
1201 gst_flow_get_name (ret));
1206 case GST_FLOW_ERROR:
1208 case GST_FLOW_UNEXPECTED:
1210 case GST_FLOW_WRONG_STATE:
1211 GST_DEBUG_OBJECT (mssdemux, "Wrong state, stopping task");
1214 g_assert_not_reached ();
1217 GST_LOG_OBJECT (mssdemux, "popping next item from queue for stream %p %s",
1218 stream, GST_PAD_NAME (stream->pad));
1219 if (gst_data_queue_pop (stream->dataqueue, &item)) {
1221 object = gst_mini_object_ref (item->object);
1222 item->destroy (item);
1224 GST_DEBUG_OBJECT (mssdemux,
1225 "Failed to get object from dataqueue on stream %p %s", stream,
1226 GST_PAD_NAME (stream->pad));
1230 if (G_UNLIKELY (stream->pending_newsegment)) {
1231 gst_pad_push_event (stream->pad, stream->pending_newsegment);
1232 stream->pending_newsegment = NULL;
1235 if (G_LIKELY (GST_IS_BUFFER (object))) {
1236 if (GST_BUFFER_TIMESTAMP (object) != stream->next_timestamp) {
1237 GST_ERROR_OBJECT (mssdemux, "Marking buffer %p as discont buffer:%"
1238 GST_TIME_FORMAT " != expected:%" GST_TIME_FORMAT, object,
1239 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (object)),
1240 GST_TIME_ARGS (stream->next_timestamp));
1241 GST_BUFFER_FLAG_SET (object, GST_BUFFER_FLAG_DISCONT);
1244 GST_DEBUG_OBJECT (mssdemux,
1245 "Pushing buffer %p %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
1246 " discont:%d on pad %s", object,
1247 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (object)),
1248 GST_TIME_ARGS (GST_BUFFER_DURATION (object)),
1249 GST_BUFFER_FLAG_IS_SET (object, GST_BUFFER_FLAG_DISCONT),
1250 GST_PAD_NAME (stream->pad));
1252 stream->next_timestamp =
1253 GST_BUFFER_TIMESTAMP (object) + GST_BUFFER_DURATION (object);
1255 ret = gst_pad_push (stream->pad, GST_BUFFER_CAST (object));
1256 } else if (GST_IS_EVENT (object)) {
1257 if (GST_EVENT_TYPE (object) == GST_EVENT_EOS)
1259 GST_DEBUG_OBJECT (mssdemux, "Pushing event %p on pad %s", object,
1260 GST_PAD_NAME (stream->pad));
1261 gst_pad_push_event (stream->pad, GST_EVENT_CAST (object));
1263 g_return_if_reached ();
1267 case GST_FLOW_UNEXPECTED:
1268 goto eos; /* EOS ? */
1269 case GST_FLOW_ERROR:
1271 case GST_FLOW_NOT_LINKED:
1272 break; /* TODO what to do here? pause the task or just keep pushing? */
1278 GST_LOG_OBJECT (mssdemux, "Stream loop end");
1283 GST_DEBUG_OBJECT (mssdemux, "EOS on all pads");
1284 gst_task_stop (mssdemux->stream_task);
1289 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1290 gst_task_stop (mssdemux->stream_task);
1295 GST_DEBUG_OBJECT (mssdemux, "Stopping streaming task");
1296 gst_task_stop (mssdemux->stream_task);