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.
35 #include "gst/gst-i18n-plugin.h"
41 #include "gstmssdemux.h"
43 GST_DEBUG_CATEGORY (mssdemux_debug);
45 #define DEFAULT_CONNECTION_SPEED 0
51 PROP_CONNECTION_SPEED,
55 static GstStaticPadTemplate gst_mss_demux_sink_template =
56 GST_STATIC_PAD_TEMPLATE ("sink",
59 GST_STATIC_CAPS ("application/vnd.ms-sstr+xml")
62 static GstStaticPadTemplate gst_mss_demux_videosrc_template =
63 GST_STATIC_PAD_TEMPLATE ("video_%02u",
68 static GstStaticPadTemplate gst_mss_demux_audiosrc_template =
69 GST_STATIC_PAD_TEMPLATE ("audio_%02u",
74 GST_BOILERPLATE (GstMssDemux, gst_mss_demux, GstMssDemux, GST_TYPE_ELEMENT);
76 static void gst_mss_demux_dispose (GObject * object);
77 static void gst_mss_demux_set_property (GObject * object, guint prop_id,
78 const GValue * value, GParamSpec * pspec);
79 static void gst_mss_demux_get_property (GObject * object, guint prop_id,
80 GValue * value, GParamSpec * pspec);
81 static GstStateChangeReturn gst_mss_demux_change_state (GstElement * element,
82 GstStateChange transition);
83 static GstFlowReturn gst_mss_demux_chain (GstPad * pad, GstBuffer * buffer);
84 static GstFlowReturn gst_mss_demux_event (GstPad * pad, GstEvent * event);
86 static gboolean gst_mss_demux_src_query (GstPad * pad, GstQuery * query);
88 static void gst_mss_demux_download_loop (GstMssDemuxStream * stream);
89 static void gst_mss_demux_stream_loop (GstMssDemux * mssdemux);
91 static gboolean gst_mss_demux_process_manifest (GstMssDemux * mssdemux);
94 gst_mss_demux_base_init (gpointer klass)
96 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
98 gst_element_class_add_static_pad_template (element_class,
99 &gst_mss_demux_sink_template);
100 gst_element_class_add_static_pad_template (element_class,
101 &gst_mss_demux_videosrc_template);
102 gst_element_class_add_static_pad_template (element_class,
103 &gst_mss_demux_audiosrc_template);
104 gst_element_class_set_details_simple (element_class, "Smooth Streaming "
105 "demuxer", "Demuxer",
106 "Parse and demultiplex a Smooth Streaming manifest into audio and video "
107 "streams", "Thiago Santos <thiago.sousa.santos@collabora.com>");
109 GST_DEBUG_CATEGORY_INIT (mssdemux_debug, "mssdemux", 0, "mssdemux plugin");
113 gst_mss_demux_class_init (GstMssDemuxClass * klass)
115 GObjectClass *gobject_class;
116 GstElementClass *gstelement_class;
118 gobject_class = (GObjectClass *) klass;
119 gstelement_class = (GstElementClass *) klass;
121 parent_class = g_type_class_peek_parent (klass);
123 gobject_class->dispose = gst_mss_demux_dispose;
124 gobject_class->set_property = gst_mss_demux_set_property;
125 gobject_class->get_property = gst_mss_demux_get_property;
127 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
128 g_param_spec_uint ("connection-speed", "Connection Speed",
129 "Network connection speed in kbps (0 = unknown)",
130 0, G_MAXUINT / 1000, DEFAULT_CONNECTION_SPEED,
131 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
133 gstelement_class->change_state =
134 GST_DEBUG_FUNCPTR (gst_mss_demux_change_state);
138 gst_mss_demux_init (GstMssDemux * mssdemux, GstMssDemuxClass * klass)
141 gst_pad_new_from_static_template (&gst_mss_demux_sink_template, "sink");
142 gst_pad_set_chain_function (mssdemux->sinkpad,
143 GST_DEBUG_FUNCPTR (gst_mss_demux_chain));
144 gst_pad_set_event_function (mssdemux->sinkpad,
145 GST_DEBUG_FUNCPTR (gst_mss_demux_event));
146 gst_element_add_pad (GST_ELEMENT_CAST (mssdemux), mssdemux->sinkpad);
148 g_static_rec_mutex_init (&mssdemux->stream_lock);
149 mssdemux->stream_task =
150 gst_task_create ((GstTaskFunction) gst_mss_demux_stream_loop, mssdemux);
151 gst_task_set_lock (mssdemux->stream_task, &mssdemux->stream_lock);
155 _data_queue_check_full (GstDataQueue * queue, guint visible, guint bytes,
156 guint64 time, gpointer checkdata)
158 GstMssDemuxStream *stream = checkdata;
159 GstMssDemux *mssdemux = stream->parent;
161 if (mssdemux->data_queue_max_size == 0)
162 return FALSE; /* never full */
163 return visible >= mssdemux->data_queue_max_size;
166 static GstMssDemuxStream *
167 gst_mss_demux_stream_new (GstMssDemux * mssdemux,
168 GstMssStream * manifeststream, GstPad * srcpad)
170 GstMssDemuxStream *stream;
172 stream = g_new0 (GstMssDemuxStream, 1);
173 stream->downloader = gst_uri_downloader_new ();
174 stream->dataqueue = gst_data_queue_new (_data_queue_check_full, stream);
176 /* Downloading task */
177 g_static_rec_mutex_init (&stream->download_lock);
178 stream->download_task =
179 gst_task_create ((GstTaskFunction) gst_mss_demux_download_loop, stream);
180 gst_task_set_lock (stream->download_task, &stream->download_lock);
182 stream->pad = srcpad;
183 stream->manifest_stream = manifeststream;
184 stream->parent = mssdemux;
190 gst_mss_demux_stream_free (GstMssDemuxStream * stream)
192 if (stream->download_task) {
193 if (GST_TASK_STATE (stream->download_task) != GST_TASK_STOPPED) {
194 GST_DEBUG_OBJECT (stream->parent, "Leaving streaming task %s:%s",
195 GST_DEBUG_PAD_NAME (stream->pad));
196 gst_task_stop (stream->download_task);
197 gst_uri_downloader_cancel (stream->downloader);
198 g_static_rec_mutex_lock (&stream->download_lock);
199 g_static_rec_mutex_unlock (&stream->download_lock);
200 GST_LOG_OBJECT (stream->parent, "Waiting for task to finish");
201 gst_task_join (stream->download_task);
202 GST_LOG_OBJECT (stream->parent, "Finished");
204 gst_object_unref (stream->download_task);
205 g_static_rec_mutex_free (&stream->download_lock);
206 stream->download_task = NULL;
209 if (stream->pending_newsegment) {
210 gst_event_unref (stream->pending_newsegment);
211 stream->pending_newsegment = NULL;
215 if (stream->downloader != NULL) {
216 g_object_unref (stream->downloader);
217 stream->downloader = NULL;
219 if (stream->dataqueue) {
220 g_object_unref (stream->dataqueue);
221 stream->dataqueue = NULL;
224 gst_object_unref (stream->pad);
231 gst_mss_demux_reset (GstMssDemux * mssdemux)
235 if (GST_TASK_STATE (mssdemux->stream_task) != GST_TASK_STOPPED) {
236 gst_task_stop (mssdemux->stream_task);
237 g_static_rec_mutex_lock (&mssdemux->stream_lock);
238 g_static_rec_mutex_unlock (&mssdemux->stream_lock);
239 gst_task_join (mssdemux->stream_task);
242 if (mssdemux->manifest_buffer) {
243 gst_buffer_unref (mssdemux->manifest_buffer);
244 mssdemux->manifest_buffer = NULL;
247 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
248 GstMssDemuxStream *stream = iter->data;
249 gst_element_remove_pad (GST_ELEMENT_CAST (mssdemux), stream->pad);
250 gst_mss_demux_stream_free (stream);
252 g_slist_free (mssdemux->streams);
253 mssdemux->streams = NULL;
255 if (mssdemux->manifest) {
256 gst_mss_manifest_free (mssdemux->manifest);
257 mssdemux->manifest = NULL;
260 mssdemux->n_videos = mssdemux->n_audios = 0;
261 g_free (mssdemux->base_url);
262 g_free (mssdemux->manifest_uri);
263 mssdemux->base_url = NULL;
267 gst_mss_demux_dispose (GObject * object)
269 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (object);
271 if (mssdemux->stream_task) {
272 gst_object_unref (mssdemux->stream_task);
273 g_static_rec_mutex_free (&mssdemux->stream_lock);
274 mssdemux->stream_task = NULL;
277 G_OBJECT_CLASS (parent_class)->dispose (object);
281 gst_mss_demux_set_property (GObject * object, guint prop_id,
282 const GValue * value, GParamSpec * pspec)
284 GstMssDemux *mssdemux = GST_MSS_DEMUX (object);
287 case PROP_CONNECTION_SPEED:
288 GST_OBJECT_LOCK (mssdemux);
289 mssdemux->connection_speed = g_value_get_uint (value) * 1000;
290 mssdemux->update_bitrates = TRUE;
291 GST_DEBUG_OBJECT (mssdemux, "Connection speed set to %llu",
292 mssdemux->connection_speed);
293 GST_OBJECT_UNLOCK (mssdemux);
296 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
302 gst_mss_demux_get_property (GObject * object, guint prop_id, GValue * value,
305 GstMssDemux *mssdemux = GST_MSS_DEMUX (object);
308 case PROP_CONNECTION_SPEED:
309 g_value_set_uint (value, mssdemux->connection_speed / 1000);
312 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
317 static GstStateChangeReturn
318 gst_mss_demux_change_state (GstElement * element, GstStateChange transition)
320 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (element);
321 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
323 switch (transition) {
324 case GST_STATE_CHANGE_PAUSED_TO_READY:
325 gst_mss_demux_reset (mssdemux);
327 case GST_STATE_CHANGE_READY_TO_NULL:
333 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
335 switch (transition) {
336 case GST_STATE_CHANGE_PAUSED_TO_READY:{
347 gst_mss_demux_chain (GstPad * pad, GstBuffer * buffer)
349 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (GST_PAD_PARENT (pad));
350 if (mssdemux->manifest_buffer == NULL)
351 mssdemux->manifest_buffer = buffer;
353 mssdemux->manifest_buffer =
354 gst_buffer_join (mssdemux->manifest_buffer, buffer);
360 gst_mss_demux_start (GstMssDemux * mssdemux)
364 GST_INFO_OBJECT (mssdemux, "Starting streams' tasks");
365 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
366 GstMssDemuxStream *stream = iter->data;
367 gst_task_start (stream->download_task);
370 gst_task_start (mssdemux->stream_task);
374 gst_mss_demux_push_src_event (GstMssDemux * mssdemux, GstEvent * event)
379 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
380 GstMssDemuxStream *stream = iter->data;
381 gst_event_ref (event);
382 ret = ret & gst_pad_push_event (stream->pad, event);
388 gst_mss_demux_event (GstPad * pad, GstEvent * event)
390 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (GST_PAD_PARENT (pad));
391 gboolean forward = TRUE;
394 switch (GST_EVENT_TYPE (event)) {
396 if (mssdemux->manifest_buffer == NULL) {
397 GST_WARNING_OBJECT (mssdemux, "Received EOS without a manifest.");
401 if (gst_mss_demux_process_manifest (mssdemux))
402 gst_mss_demux_start (mssdemux);
410 ret = gst_pad_event_default (pad, event);
412 gst_event_unref (event);
419 gst_mss_demux_stop_tasks (GstMssDemux * mssdemux, gboolean immediate)
423 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
424 GstMssDemuxStream *stream = iter->data;
426 gst_data_queue_set_flushing (stream->dataqueue, TRUE);
429 gst_uri_downloader_cancel (stream->downloader);
430 gst_task_pause (stream->download_task);
432 gst_task_pause (mssdemux->stream_task);
434 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
435 GstMssDemuxStream *stream = iter->data;
436 g_static_rec_mutex_lock (&stream->download_lock);
438 g_static_rec_mutex_lock (&mssdemux->stream_lock);
442 gst_mss_demux_restart_tasks (GstMssDemux * mssdemux)
445 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
446 GstMssDemuxStream *stream = iter->data;
447 g_static_rec_mutex_unlock (&stream->download_lock);
449 g_static_rec_mutex_unlock (&mssdemux->stream_lock);
450 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
451 GstMssDemuxStream *stream = iter->data;
453 gst_data_queue_set_flushing (stream->dataqueue, FALSE);
454 gst_task_start (stream->download_task);
456 gst_task_start (mssdemux->stream_task);
460 gst_mss_demux_src_event (GstPad * pad, GstEvent * event)
462 GstMssDemux *mssdemux;
464 mssdemux = GST_MSS_DEMUX (GST_PAD_PARENT (pad));
466 switch (event->type) {
472 GstSeekType start_type, stop_type;
474 GstEvent *newsegment;
477 GST_INFO_OBJECT (mssdemux, "Received GST_EVENT_SEEK");
479 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
482 if (format != GST_FORMAT_TIME)
485 GST_DEBUG_OBJECT (mssdemux,
486 "seek event, rate: %f start: %" GST_TIME_FORMAT " stop: %"
487 GST_TIME_FORMAT, rate, GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
489 if (flags & GST_SEEK_FLAG_FLUSH) {
490 GstEvent *flush = gst_event_new_flush_start ();
491 GST_DEBUG_OBJECT (mssdemux, "sending flush start");
493 gst_event_set_seqnum (flush, gst_event_get_seqnum (event));
494 gst_mss_demux_push_src_event (mssdemux, flush);
495 gst_event_unref (flush);
498 gst_mss_demux_stop_tasks (mssdemux, TRUE);
500 if (!gst_mss_manifest_seek (mssdemux->manifest, start)) {;
501 GST_WARNING_OBJECT (mssdemux, "Could not find seeked fragment");
506 gst_event_new_new_segment (FALSE, rate, format, start, stop, start);
507 gst_event_set_seqnum (newsegment, gst_event_get_seqnum (event));
508 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
509 GstMssDemuxStream *stream = iter->data;
512 gst_data_queue_flush (stream->dataqueue);
513 stream->pending_newsegment = gst_event_ref (newsegment);
515 gst_event_unref (newsegment);
517 if (flags & GST_SEEK_FLAG_FLUSH) {
518 GstEvent *flush = gst_event_new_flush_stop ();
519 GST_DEBUG_OBJECT (mssdemux, "sending flush stop");
521 gst_event_set_seqnum (flush, gst_event_get_seqnum (event));
522 gst_mss_demux_push_src_event (mssdemux, flush);
523 gst_event_unref (flush);
526 gst_mss_demux_restart_tasks (mssdemux);
534 return gst_pad_event_default (pad, event);
538 gst_mss_demux_src_query (GstPad * pad, GstQuery * query)
540 GstMssDemux *mssdemux;
541 gboolean ret = FALSE;
546 mssdemux = GST_MSS_DEMUX (GST_PAD_PARENT (pad));
548 switch (query->type) {
549 case GST_QUERY_DURATION:{
550 GstClockTime duration = -1;
553 gst_query_parse_duration (query, &fmt, NULL);
554 if (fmt == GST_FORMAT_TIME && mssdemux->manifest) {
555 /* TODO should we use the streams accumulated duration or the main manifest duration? */
556 duration = gst_mss_manifest_get_gst_duration (mssdemux->manifest);
558 if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0) {
559 gst_query_set_duration (query, GST_FORMAT_TIME, duration);
563 GST_INFO_OBJECT (mssdemux, "GST_QUERY_DURATION returns %s with duration %"
564 GST_TIME_FORMAT, ret ? "TRUE" : "FALSE", GST_TIME_ARGS (duration));
567 case GST_QUERY_LATENCY:{
568 gboolean live = FALSE;
570 live = mssdemux->manifest
571 && gst_mss_manifest_is_live (mssdemux->manifest);
573 gst_query_set_latency (query, live, 0, -1);
577 case GST_QUERY_SEEKING:{
581 if (mssdemux->manifest && gst_mss_manifest_is_live (mssdemux->manifest)) {
582 return FALSE; /* no live seeking */
585 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
586 GST_INFO_OBJECT (mssdemux, "Received GST_QUERY_SEEKING with format %d",
588 if (fmt == GST_FORMAT_TIME) {
589 GstClockTime duration;
590 duration = gst_mss_manifest_get_gst_duration (mssdemux->manifest);
591 if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0)
593 gst_query_set_seeking (query, fmt, TRUE, 0, stop);
595 GST_INFO_OBJECT (mssdemux, "GST_QUERY_SEEKING returning with stop : %"
596 GST_TIME_FORMAT, GST_TIME_ARGS (stop));
601 /* Don't fordward queries upstream because of the special nature of this
602 * "demuxer", which relies on the upstream element only to be fed
612 _set_src_pad_functions (GstPad * pad)
614 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_mss_demux_src_query));
615 gst_pad_set_event_function (pad, GST_DEBUG_FUNCPTR (gst_mss_demux_src_event));
619 _create_pad (GstMssDemux * mssdemux, GstMssStream * manifeststream)
622 GstPad *srcpad = NULL;
623 GstMssStreamType streamtype;
625 streamtype = gst_mss_stream_get_type (manifeststream);
626 GST_DEBUG_OBJECT (mssdemux, "Found stream of type: %s",
627 gst_mss_stream_type_name (streamtype));
629 /* TODO use stream's name/bitrate/index as the pad name? */
630 if (streamtype == MSS_STREAM_TYPE_VIDEO) {
631 name = g_strdup_printf ("video_%02u", mssdemux->n_videos++);
633 gst_pad_new_from_static_template (&gst_mss_demux_videosrc_template,
636 } else if (streamtype == MSS_STREAM_TYPE_AUDIO) {
637 name = g_strdup_printf ("audio_%02u", mssdemux->n_audios++);
639 gst_pad_new_from_static_template (&gst_mss_demux_audiosrc_template,
645 GST_WARNING_OBJECT (mssdemux, "Ignoring unknown type stream");
649 _set_src_pad_functions (srcpad);
654 gst_mss_demux_create_streams (GstMssDemux * mssdemux)
656 GSList *streams = gst_mss_manifest_get_streams (mssdemux->manifest);
659 if (streams == NULL) {
660 GST_INFO_OBJECT (mssdemux, "No streams found in the manifest");
661 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
662 (_("This file contains no playable streams.")),
663 ("no streams found at the Manifest"));
667 for (iter = streams; iter; iter = g_slist_next (iter)) {
668 GstPad *srcpad = NULL;
669 GstMssDemuxStream *stream = NULL;
670 GstMssStream *manifeststream = iter->data;
672 srcpad = _create_pad (mssdemux, manifeststream);
678 stream = gst_mss_demux_stream_new (mssdemux, manifeststream, srcpad);
679 gst_mss_stream_set_active (manifeststream, TRUE);
680 mssdemux->streams = g_slist_append (mssdemux->streams, stream);
683 /* select initial bitrates */
684 GST_OBJECT_LOCK (mssdemux);
685 GST_INFO_OBJECT (mssdemux, "Changing max bitrate to %llu",
686 mssdemux->connection_speed);
687 gst_mss_manifest_change_bitrate (mssdemux->manifest,
688 mssdemux->connection_speed);
689 mssdemux->update_bitrates = FALSE;
690 GST_OBJECT_UNLOCK (mssdemux);
694 gst_mss_demux_expose_stream (GstMssDemux * mssdemux, GstMssDemuxStream * stream)
698 GstPad *pad = stream->pad;
700 media_caps = gst_mss_stream_get_caps (stream->manifest_stream);
703 caps = gst_caps_new_simple ("video/quicktime", "variant", G_TYPE_STRING,
704 "mss-fragmented", "timescale", G_TYPE_UINT64,
705 gst_mss_stream_get_timescale (stream->manifest_stream), "media-caps",
706 GST_TYPE_CAPS, media_caps, NULL);
707 gst_caps_unref (media_caps);
708 gst_pad_set_caps (pad, caps);
709 gst_caps_unref (caps);
711 gst_pad_set_active (pad, TRUE);
712 GST_INFO_OBJECT (mssdemux, "Adding srcpad %s:%s with caps %" GST_PTR_FORMAT,
713 GST_DEBUG_PAD_NAME (pad), caps);
714 gst_object_ref (pad);
715 gst_element_add_pad (GST_ELEMENT_CAST (mssdemux), pad);
717 GST_WARNING_OBJECT (mssdemux,
718 "Couldn't get caps from manifest stream %p %s, not exposing it", stream,
719 GST_PAD_NAME (stream->pad));
726 gst_mss_demux_process_manifest (GstMssDemux * mssdemux)
733 g_return_val_if_fail (mssdemux->manifest_buffer != NULL, FALSE);
734 g_return_val_if_fail (mssdemux->manifest == NULL, FALSE);
736 query = gst_query_new_uri ();
737 ret = gst_pad_peer_query (mssdemux->sinkpad, query);
740 gst_query_parse_uri (query, &uri);
741 GST_INFO_OBJECT (mssdemux, "Upstream is using URI: %s", uri);
743 mssdemux->manifest_uri = g_strdup (uri);
744 baseurl_end = g_strrstr (uri, "/Manifest");
746 /* set the new end of the string */
747 baseurl_end[0] = '\0';
749 GST_WARNING_OBJECT (mssdemux, "Stream's URI didn't end with /manifest");
752 mssdemux->base_url = uri;
754 gst_query_unref (query);
756 if (mssdemux->base_url == NULL) {
757 GST_ELEMENT_ERROR (mssdemux, RESOURCE, NOT_FOUND,
758 (_("Couldn't get the Manifest's URI")),
759 ("need to get the manifest's URI from upstream elements"));
763 mssdemux->manifest = gst_mss_manifest_new (mssdemux->manifest_buffer);
764 if (!mssdemux->manifest) {
765 GST_ELEMENT_ERROR (mssdemux, STREAM, FORMAT, ("Bad manifest file"),
766 ("Xml manifest file couldn't be parsed"));
770 GST_INFO_OBJECT (mssdemux, "Live stream: %d",
771 gst_mss_manifest_is_live (mssdemux->manifest));
773 gst_mss_demux_create_streams (mssdemux);
774 for (iter = mssdemux->streams; iter;) {
775 GSList *current = iter;
776 GstMssDemuxStream *stream = iter->data;
777 iter = g_slist_next (iter); /* do it ourselves as we want it done in the beginning of the loop */
778 if (!gst_mss_demux_expose_stream (mssdemux, stream)) {
779 gst_mss_demux_stream_free (stream);
780 mssdemux->streams = g_slist_delete_link (mssdemux->streams, current);
784 if (!mssdemux->streams) {
786 GST_WARNING_OBJECT (mssdemux, "Couldn't identify the caps for any of the "
787 "streams found in the manifest");
788 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
789 (_("This file contains no playable streams.")),
790 ("No known stream formats found at the Manifest"));
794 gst_element_no_more_pads (GST_ELEMENT_CAST (mssdemux));
799 gst_mss_demux_reload_manifest (GstMssDemux * mssdemux)
801 GstUriDownloader *downloader;
802 GstFragment *manifest_data;
803 GstBuffer *manifest_buffer;
805 downloader = gst_uri_downloader_new ();
808 gst_uri_downloader_fetch_uri (downloader, mssdemux->manifest_uri);
809 manifest_buffer = gst_fragment_get_buffer (manifest_data);
810 g_object_unref (manifest_data);
812 gst_mss_manifest_reload_fragments (mssdemux->manifest, manifest_buffer);
813 gst_buffer_replace (&mssdemux->manifest_buffer, manifest_buffer);
814 gst_buffer_unref (manifest_buffer);
816 g_object_unref (downloader);
820 gst_mss_demux_reconfigure (GstMssDemux * mssdemux)
822 GSList *oldpads = NULL;
825 gst_mss_demux_stop_tasks (mssdemux, TRUE);
826 if (gst_mss_manifest_change_bitrate (mssdemux->manifest,
827 mssdemux->connection_speed)) {
828 GstClockTime newseg_ts = GST_CLOCK_TIME_NONE;
830 GST_DEBUG_OBJECT (mssdemux, "Creating new pad group");
831 /* if we changed the bitrate, we need to add new pads */
832 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
833 GstMssDemuxStream *stream = iter->data;
834 GstPad *oldpad = stream->pad;
835 GstClockTime ts = GST_CLOCK_TIME_NONE;
837 oldpads = g_slist_prepend (oldpads, oldpad);
839 /* since we are flushing the queue, get the next un-pushed timestamp to seek
841 gst_data_queue_set_flushing (stream->dataqueue, FALSE);
842 if (!gst_data_queue_is_empty (stream->dataqueue)) {
843 GstDataQueueItem *item = NULL;
845 while (!gst_data_queue_is_empty (stream->dataqueue)
846 && !GST_CLOCK_TIME_IS_VALID (ts)) {
847 gst_data_queue_pop (stream->dataqueue, &item);
850 g_assert_not_reached ();
854 if (GST_IS_BUFFER (item->object)) {
855 GstBuffer *buffer = GST_BUFFER_CAST (item->object);
857 ts = GST_BUFFER_TIMESTAMP (buffer);
859 item->destroy (item);
863 if (!GST_CLOCK_TIME_IS_VALID (ts)) {
864 ts = gst_mss_stream_get_fragment_gst_timestamp
865 (stream->manifest_stream);
871 GST_DEBUG_OBJECT (mssdemux,
872 "Seeking stream %p %s to ts %" GST_TIME_FORMAT, stream,
873 GST_PAD_NAME (stream->pad), GST_TIME_ARGS (ts));
874 gst_mss_stream_seek (stream->manifest_stream, ts);
875 gst_data_queue_flush (stream->dataqueue);
877 stream->pad = _create_pad (mssdemux, stream->manifest_stream);
878 gst_mss_demux_expose_stream (mssdemux, stream);
880 gst_pad_push_event (oldpad, gst_event_new_eos ());
883 gst_element_no_more_pads (GST_ELEMENT (mssdemux));
885 for (iter = oldpads; iter; iter = g_slist_next (iter)) {
886 GstPad *oldpad = iter->data;
888 gst_pad_set_active (oldpad, FALSE);
889 gst_element_remove_pad (GST_ELEMENT (mssdemux), oldpad);
890 gst_object_unref (oldpad);
892 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
893 GstMssDemuxStream *stream = iter->data;
895 stream->pending_newsegment =
896 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, newseg_ts, -1,
900 gst_mss_demux_restart_tasks (mssdemux);
904 _free_data_queue_item (gpointer obj)
906 GstDataQueueItem *item = obj;
908 gst_mini_object_unref (item->object);
909 g_slice_free (GstDataQueueItem, item);
913 gst_mss_demux_stream_store_object (GstMssDemuxStream * stream,
916 GstDataQueueItem *item;
918 item = g_slice_new (GstDataQueueItem);
919 item->object = (GstMiniObject *) obj;
921 item->duration = 0; /* we don't care */
923 item->visible = TRUE;
925 item->destroy = (GDestroyNotify) _free_data_queue_item;
927 if (!gst_data_queue_push (stream->dataqueue, item)) {
928 GST_DEBUG_OBJECT (stream->parent, "Failed to store object %p", obj);
929 gst_mini_object_unref (obj);
930 g_slice_free (GstDataQueueItem, item);
935 gst_mss_demux_stream_download_fragment (GstMssDemuxStream * stream,
938 GstMssDemux *mssdemux = stream->parent;
941 GstFragment *fragment;
943 GstFlowReturn ret = GST_FLOW_OK;
945 GST_DEBUG_OBJECT (mssdemux, "Getting url for stream %p", stream);
946 ret = gst_mss_stream_get_fragment_url (stream->manifest_stream, &path);
949 break; /* all is good, let's go */
950 case GST_FLOW_UNEXPECTED: /* EOS */
951 gst_mss_demux_reload_manifest (mssdemux);
953 return GST_FLOW_UNEXPECTED;
962 GST_DEBUG_OBJECT (mssdemux, "Got url path '%s' for stream %p", path, stream);
964 url = g_strdup_printf ("%s/%s", mssdemux->base_url, path);
966 GST_DEBUG_OBJECT (mssdemux, "Got url '%s' for stream %p", url, stream);
968 fragment = gst_uri_downloader_fetch_uri (stream->downloader, url);
973 GST_INFO_OBJECT (mssdemux, "No fragment downloaded");
974 /* TODO check if we are truly stoping */
975 if (gst_mss_manifest_is_live (mssdemux->manifest)) {
976 /* looks like there is no way of knowing when a live stream has ended
977 * Have to assume we are falling behind and cause a manifest reload */
980 return GST_FLOW_ERROR;
983 _buffer = gst_fragment_get_buffer (fragment);
984 _buffer = gst_buffer_make_metadata_writable (_buffer);
985 gst_buffer_set_caps (_buffer, GST_PAD_CAPS (stream->pad));
986 GST_BUFFER_TIMESTAMP (_buffer) =
987 gst_mss_stream_get_fragment_gst_timestamp (stream->manifest_stream);
988 GST_BUFFER_DURATION (_buffer) =
989 gst_mss_stream_get_fragment_gst_duration (stream->manifest_stream);
991 g_object_unref (fragment);
997 GST_DEBUG_OBJECT (mssdemux,
998 "Storing buffer for stream %p - %s. Timestamp: %" GST_TIME_FORMAT
999 " Duration: %" GST_TIME_FORMAT,
1000 stream, GST_PAD_NAME (stream->pad),
1001 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (_buffer)),
1002 GST_TIME_ARGS (GST_BUFFER_DURATION (_buffer)));
1003 gst_mss_demux_stream_store_object (stream, GST_MINI_OBJECT_CAST (_buffer));
1010 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
1011 (_("Failed to get fragment URL.")),
1012 ("An error happened when getting fragment URL"));
1013 gst_task_stop (stream->download_task);
1014 return GST_FLOW_ERROR;
1018 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1019 gst_task_stop (stream->download_task);
1020 return GST_FLOW_ERROR;
1025 gst_mss_demux_download_loop (GstMssDemuxStream * stream)
1027 GstMssDemux *mssdemux = stream->parent;
1028 GstBuffer *buffer = NULL;
1031 GST_LOG_OBJECT (mssdemux, "download loop start %p", stream);
1034 ret = gst_mss_demux_stream_download_fragment (stream, &buffer);
1037 break; /* all is good, let's go */
1038 case GST_FLOW_UNEXPECTED: /* EOS */
1040 case GST_FLOW_ERROR:
1047 gst_mss_stream_advance_fragment (stream->manifest_stream);
1049 GST_LOG_OBJECT (mssdemux, "download loop end %p", stream);
1054 GST_DEBUG_OBJECT (mssdemux, "Storing EOS for pad %s:%s",
1055 GST_DEBUG_PAD_NAME (stream->pad));
1056 gst_mss_demux_stream_store_object (stream,
1057 GST_MINI_OBJECT_CAST (gst_event_new_eos ()));
1058 gst_task_stop (stream->download_task);
1063 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1064 gst_task_stop (stream->download_task);
1069 static GstFlowReturn
1070 gst_mss_demux_select_latest_stream (GstMssDemux * mssdemux,
1071 GstMssDemuxStream ** stream)
1073 GstFlowReturn ret = GST_FLOW_OK;
1074 GstMssDemuxStream *current = NULL;
1075 GstClockTime cur_time = GST_CLOCK_TIME_NONE;
1078 if (!mssdemux->streams)
1079 return GST_FLOW_ERROR;
1081 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
1083 GstMssDemuxStream *other;
1084 GstDataQueueItem *item;
1091 if (gst_data_queue_peek (other->dataqueue, &item)) {
1094 return GST_FLOW_WRONG_STATE;
1097 if (GST_IS_EVENT (item->object)) {
1098 /* events have higher priority */
1102 time = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (item->object));
1103 if (time < cur_time) {
1110 if (current == NULL)
1111 ret = GST_FLOW_UNEXPECTED;
1116 gst_mss_demux_stream_loop (GstMssDemux * mssdemux)
1118 GstMssDemuxStream *stream = NULL;
1120 GstMiniObject *object = NULL;
1121 GstDataQueueItem *item = NULL;
1123 GST_LOG_OBJECT (mssdemux, "Starting stream loop");
1125 GST_OBJECT_LOCK (mssdemux);
1126 if (mssdemux->update_bitrates) {
1127 mssdemux->update_bitrates = FALSE;
1128 GST_OBJECT_UNLOCK (mssdemux);
1130 GST_DEBUG_OBJECT (mssdemux,
1131 "Starting streams reconfiguration due to bitrate changes");
1132 gst_mss_demux_reconfigure (mssdemux);
1133 GST_DEBUG_OBJECT (mssdemux, "Finished streams reconfiguration");
1135 GST_OBJECT_UNLOCK (mssdemux);
1138 ret = gst_mss_demux_select_latest_stream (mssdemux, &stream);
1141 GST_DEBUG_OBJECT (mssdemux,
1142 "Stream loop selected %p stream of pad %s. %d - %s", stream,
1143 GST_PAD_NAME (stream->pad), ret, gst_flow_get_name (ret));
1145 GST_DEBUG_OBJECT (mssdemux, "No streams selected -> %d - %s", ret,
1146 gst_flow_get_name (ret));
1151 case GST_FLOW_ERROR:
1153 case GST_FLOW_UNEXPECTED:
1155 case GST_FLOW_WRONG_STATE:
1156 GST_DEBUG_OBJECT (mssdemux, "Wrong state, stopping task");
1159 g_assert_not_reached ();
1162 GST_LOG_OBJECT (mssdemux, "popping next item from queue for stream %p %s",
1163 stream, GST_PAD_NAME (stream->pad));
1164 if (gst_data_queue_pop (stream->dataqueue, &item)) {
1166 object = gst_mini_object_ref (item->object);
1167 item->destroy (item);
1169 GST_DEBUG_OBJECT (mssdemux,
1170 "Failed to get object from dataqueue on stream %p %s", stream,
1171 GST_PAD_NAME (stream->pad));
1175 if (G_UNLIKELY (stream->pending_newsegment)) {
1176 gst_pad_push_event (stream->pad, stream->pending_newsegment);
1177 stream->pending_newsegment = NULL;
1180 if (G_LIKELY (GST_IS_BUFFER (object))) {
1181 if (GST_BUFFER_TIMESTAMP (object) != stream->next_timestamp) {
1182 GST_ERROR_OBJECT (mssdemux, "Marking buffer %p as discont buffer:%"
1183 GST_TIME_FORMAT " != expected:%" GST_TIME_FORMAT, object,
1184 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (object)),
1185 GST_TIME_ARGS (stream->next_timestamp));
1186 GST_BUFFER_FLAG_SET (object, GST_BUFFER_FLAG_DISCONT);
1189 GST_DEBUG_OBJECT (mssdemux,
1190 "Pushing buffer %p %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
1191 " discont:%d on pad %s", object,
1192 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (object)),
1193 GST_TIME_ARGS (GST_BUFFER_DURATION (object)),
1194 GST_BUFFER_FLAG_IS_SET (object, GST_BUFFER_FLAG_DISCONT),
1195 GST_PAD_NAME (stream->pad));
1197 stream->next_timestamp =
1198 GST_BUFFER_TIMESTAMP (object) + GST_BUFFER_DURATION (object);
1200 ret = gst_pad_push (stream->pad, GST_BUFFER_CAST (object));
1201 } else if (GST_IS_EVENT (object)) {
1202 if (GST_EVENT_TYPE (object) == GST_EVENT_EOS)
1204 GST_DEBUG_OBJECT (mssdemux, "Pushing event %p on pad %s", object,
1205 GST_PAD_NAME (stream->pad));
1206 gst_pad_push_event (stream->pad, GST_EVENT_CAST (object));
1208 g_return_if_reached ();
1212 case GST_FLOW_UNEXPECTED:
1213 goto eos; /* EOS ? */
1214 case GST_FLOW_ERROR:
1216 case GST_FLOW_NOT_LINKED:
1217 break; /* TODO what to do here? pause the task or just keep pushing? */
1223 GST_LOG_OBJECT (mssdemux, "Stream loop end");
1228 GST_DEBUG_OBJECT (mssdemux, "EOS on all pads");
1229 gst_task_stop (mssdemux->stream_task);
1234 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1235 gst_task_stop (mssdemux->stream_task);
1240 GST_DEBUG_OBJECT (mssdemux, "Stopping streaming task");
1241 gst_task_stop (mssdemux->stream_task);