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 g_static_rec_mutex_lock (&stream->download_lock);
198 g_static_rec_mutex_unlock (&stream->download_lock);
199 GST_LOG_OBJECT (stream->parent, "Waiting for task to finish");
200 gst_task_join (stream->download_task);
201 GST_LOG_OBJECT (stream->parent, "Finished");
203 gst_object_unref (stream->download_task);
204 g_static_rec_mutex_free (&stream->download_lock);
205 stream->download_task = NULL;
208 if (stream->pending_newsegment) {
209 gst_event_unref (stream->pending_newsegment);
210 stream->pending_newsegment = NULL;
214 if (stream->downloader != NULL) {
215 g_object_unref (stream->downloader);
216 stream->downloader = NULL;
218 if (stream->dataqueue) {
219 g_object_unref (stream->dataqueue);
220 stream->dataqueue = NULL;
223 gst_object_unref (stream->pad);
230 gst_mss_demux_reset (GstMssDemux * mssdemux)
234 if (GST_TASK_STATE (mssdemux->stream_task) != GST_TASK_STOPPED) {
235 gst_task_stop (mssdemux->stream_task);
236 g_static_rec_mutex_lock (&mssdemux->stream_lock);
237 g_static_rec_mutex_unlock (&mssdemux->stream_lock);
238 gst_task_join (mssdemux->stream_task);
241 if (mssdemux->manifest_buffer) {
242 gst_buffer_unref (mssdemux->manifest_buffer);
243 mssdemux->manifest_buffer = NULL;
246 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
247 GstMssDemuxStream *stream = iter->data;
248 gst_element_remove_pad (GST_ELEMENT_CAST (mssdemux), stream->pad);
249 gst_mss_demux_stream_free (stream);
251 g_slist_free (mssdemux->streams);
252 mssdemux->streams = NULL;
254 if (mssdemux->manifest) {
255 gst_mss_manifest_free (mssdemux->manifest);
256 mssdemux->manifest = NULL;
259 mssdemux->n_videos = mssdemux->n_audios = 0;
260 g_free (mssdemux->base_url);
261 g_free (mssdemux->manifest_uri);
262 mssdemux->base_url = NULL;
266 gst_mss_demux_dispose (GObject * object)
268 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (object);
270 if (mssdemux->stream_task) {
271 gst_object_unref (mssdemux->stream_task);
272 g_static_rec_mutex_free (&mssdemux->stream_lock);
273 mssdemux->stream_task = NULL;
276 G_OBJECT_CLASS (parent_class)->dispose (object);
280 gst_mss_demux_set_property (GObject * object, guint prop_id,
281 const GValue * value, GParamSpec * pspec)
283 GstMssDemux *mssdemux = GST_MSS_DEMUX (object);
286 case PROP_CONNECTION_SPEED:
287 GST_OBJECT_LOCK (mssdemux);
288 mssdemux->connection_speed = g_value_get_uint (value) * 1000;
289 mssdemux->update_bitrates = TRUE;
290 GST_DEBUG_OBJECT (mssdemux, "Connection speed set to %llu",
291 mssdemux->connection_speed);
292 GST_OBJECT_UNLOCK (mssdemux);
295 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
301 gst_mss_demux_get_property (GObject * object, guint prop_id, GValue * value,
304 GstMssDemux *mssdemux = GST_MSS_DEMUX (object);
307 case PROP_CONNECTION_SPEED:
308 g_value_set_uint (value, mssdemux->connection_speed / 1000);
311 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
316 static GstStateChangeReturn
317 gst_mss_demux_change_state (GstElement * element, GstStateChange transition)
319 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (element);
320 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
322 switch (transition) {
323 case GST_STATE_CHANGE_PAUSED_TO_READY:
324 gst_mss_demux_reset (mssdemux);
326 case GST_STATE_CHANGE_READY_TO_NULL:
332 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
334 switch (transition) {
335 case GST_STATE_CHANGE_PAUSED_TO_READY:{
346 gst_mss_demux_chain (GstPad * pad, GstBuffer * buffer)
348 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (GST_PAD_PARENT (pad));
349 if (mssdemux->manifest_buffer == NULL)
350 mssdemux->manifest_buffer = buffer;
352 mssdemux->manifest_buffer =
353 gst_buffer_join (mssdemux->manifest_buffer, buffer);
359 gst_mss_demux_start (GstMssDemux * mssdemux)
363 GST_INFO_OBJECT (mssdemux, "Starting streams' tasks");
364 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
365 GstMssDemuxStream *stream = iter->data;
366 gst_task_start (stream->download_task);
369 gst_task_start (mssdemux->stream_task);
373 gst_mss_demux_push_src_event (GstMssDemux * mssdemux, GstEvent * event)
378 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
379 GstMssDemuxStream *stream = iter->data;
380 gst_event_ref (event);
381 ret = ret & gst_pad_push_event (stream->pad, event);
387 gst_mss_demux_event (GstPad * pad, GstEvent * event)
389 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (GST_PAD_PARENT (pad));
390 gboolean forward = TRUE;
393 switch (GST_EVENT_TYPE (event)) {
395 if (mssdemux->manifest_buffer == NULL) {
396 GST_WARNING_OBJECT (mssdemux, "Received EOS without a manifest.");
400 if (gst_mss_demux_process_manifest (mssdemux))
401 gst_mss_demux_start (mssdemux);
409 ret = gst_pad_event_default (pad, event);
411 gst_event_unref (event);
418 gst_mss_demux_stop_tasks (GstMssDemux * mssdemux, gboolean immediate)
422 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
423 GstMssDemuxStream *stream = iter->data;
425 gst_data_queue_set_flushing (stream->dataqueue, TRUE);
428 gst_uri_downloader_cancel (stream->downloader);
429 gst_task_pause (stream->download_task);
431 gst_task_pause (mssdemux->stream_task);
433 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
434 GstMssDemuxStream *stream = iter->data;
435 g_static_rec_mutex_lock (&stream->download_lock);
437 g_static_rec_mutex_lock (&mssdemux->stream_lock);
441 gst_mss_demux_restart_tasks (GstMssDemux * mssdemux)
444 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
445 GstMssDemuxStream *stream = iter->data;
446 g_static_rec_mutex_unlock (&stream->download_lock);
448 g_static_rec_mutex_unlock (&mssdemux->stream_lock);
449 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
450 GstMssDemuxStream *stream = iter->data;
452 gst_data_queue_set_flushing (stream->dataqueue, FALSE);
453 gst_task_start (stream->download_task);
455 gst_task_start (mssdemux->stream_task);
459 gst_mss_demux_src_event (GstPad * pad, GstEvent * event)
461 GstMssDemux *mssdemux;
463 mssdemux = GST_MSS_DEMUX (GST_PAD_PARENT (pad));
465 switch (event->type) {
471 GstSeekType start_type, stop_type;
473 GstEvent *newsegment;
476 GST_INFO_OBJECT (mssdemux, "Received GST_EVENT_SEEK");
478 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
481 if (format != GST_FORMAT_TIME)
484 GST_DEBUG_OBJECT (mssdemux,
485 "seek event, rate: %f start: %" GST_TIME_FORMAT " stop: %"
486 GST_TIME_FORMAT, rate, GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
488 if (flags & GST_SEEK_FLAG_FLUSH) {
489 GstEvent *flush = gst_event_new_flush_start ();
490 GST_DEBUG_OBJECT (mssdemux, "sending flush start");
492 gst_event_set_seqnum (flush, gst_event_get_seqnum (event));
493 gst_mss_demux_push_src_event (mssdemux, flush);
494 gst_event_unref (flush);
497 gst_mss_demux_stop_tasks (mssdemux, TRUE);
499 if (!gst_mss_manifest_seek (mssdemux->manifest, start)) {;
500 GST_WARNING_OBJECT (mssdemux, "Could not find seeked fragment");
505 gst_event_new_new_segment (FALSE, rate, format, start, stop, start);
506 gst_event_set_seqnum (newsegment, gst_event_get_seqnum (event));
507 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
508 GstMssDemuxStream *stream = iter->data;
511 gst_data_queue_flush (stream->dataqueue);
512 stream->pending_newsegment = gst_event_ref (newsegment);
514 gst_event_unref (newsegment);
516 if (flags & GST_SEEK_FLAG_FLUSH) {
517 GstEvent *flush = gst_event_new_flush_stop ();
518 GST_DEBUG_OBJECT (mssdemux, "sending flush stop");
520 gst_event_set_seqnum (flush, gst_event_get_seqnum (event));
521 gst_mss_demux_push_src_event (mssdemux, flush);
522 gst_event_unref (flush);
525 gst_mss_demux_restart_tasks (mssdemux);
533 return gst_pad_event_default (pad, event);
537 gst_mss_demux_src_query (GstPad * pad, GstQuery * query)
539 GstMssDemux *mssdemux;
540 gboolean ret = FALSE;
545 mssdemux = GST_MSS_DEMUX (GST_PAD_PARENT (pad));
547 switch (query->type) {
548 case GST_QUERY_DURATION:{
549 GstClockTime duration = -1;
552 gst_query_parse_duration (query, &fmt, NULL);
553 if (fmt == GST_FORMAT_TIME && mssdemux->manifest) {
554 /* TODO should we use the streams accumulated duration or the main manifest duration? */
555 duration = gst_mss_manifest_get_gst_duration (mssdemux->manifest);
557 if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0) {
558 gst_query_set_duration (query, GST_FORMAT_TIME, duration);
562 GST_INFO_OBJECT (mssdemux, "GST_QUERY_DURATION returns %s with duration %"
563 GST_TIME_FORMAT, ret ? "TRUE" : "FALSE", GST_TIME_ARGS (duration));
566 case GST_QUERY_LATENCY:{
567 gboolean live = FALSE;
569 live = mssdemux->manifest
570 && gst_mss_manifest_is_live (mssdemux->manifest);
572 gst_query_set_latency (query, live, 0, -1);
576 case GST_QUERY_SEEKING:{
580 if (mssdemux->manifest && gst_mss_manifest_is_live (mssdemux->manifest)) {
581 return FALSE; /* no live seeking */
584 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
585 GST_INFO_OBJECT (mssdemux, "Received GST_QUERY_SEEKING with format %d",
587 if (fmt == GST_FORMAT_TIME) {
588 GstClockTime duration;
589 duration = gst_mss_manifest_get_gst_duration (mssdemux->manifest);
590 if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0)
592 gst_query_set_seeking (query, fmt, TRUE, 0, stop);
594 GST_INFO_OBJECT (mssdemux, "GST_QUERY_SEEKING returning with stop : %"
595 GST_TIME_FORMAT, GST_TIME_ARGS (stop));
600 /* Don't fordward queries upstream because of the special nature of this
601 * "demuxer", which relies on the upstream element only to be fed
611 _set_src_pad_functions (GstPad * pad)
613 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_mss_demux_src_query));
614 gst_pad_set_event_function (pad, GST_DEBUG_FUNCPTR (gst_mss_demux_src_event));
618 _create_pad (GstMssDemux * mssdemux, GstMssStream * manifeststream)
621 GstPad *srcpad = NULL;
622 GstMssStreamType streamtype;
624 streamtype = gst_mss_stream_get_type (manifeststream);
625 GST_DEBUG_OBJECT (mssdemux, "Found stream of type: %s",
626 gst_mss_stream_type_name (streamtype));
628 /* TODO use stream's name/bitrate/index as the pad name? */
629 if (streamtype == MSS_STREAM_TYPE_VIDEO) {
630 name = g_strdup_printf ("video_%02u", mssdemux->n_videos++);
632 gst_pad_new_from_static_template (&gst_mss_demux_videosrc_template,
635 } else if (streamtype == MSS_STREAM_TYPE_AUDIO) {
636 name = g_strdup_printf ("audio_%02u", mssdemux->n_audios++);
638 gst_pad_new_from_static_template (&gst_mss_demux_audiosrc_template,
644 GST_WARNING_OBJECT (mssdemux, "Ignoring unknown type stream");
648 _set_src_pad_functions (srcpad);
653 gst_mss_demux_create_streams (GstMssDemux * mssdemux)
655 GSList *streams = gst_mss_manifest_get_streams (mssdemux->manifest);
658 if (streams == NULL) {
659 GST_INFO_OBJECT (mssdemux, "No streams found in the manifest");
660 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
661 (_("This file contains no playable streams.")),
662 ("no streams found at the Manifest"));
666 for (iter = streams; iter; iter = g_slist_next (iter)) {
667 GstPad *srcpad = NULL;
668 GstMssDemuxStream *stream = NULL;
669 GstMssStream *manifeststream = iter->data;
671 srcpad = _create_pad (mssdemux, manifeststream);
677 stream = gst_mss_demux_stream_new (mssdemux, manifeststream, srcpad);
678 gst_mss_stream_set_active (manifeststream, TRUE);
679 mssdemux->streams = g_slist_append (mssdemux->streams, stream);
682 /* select initial bitrates */
683 GST_OBJECT_LOCK (mssdemux);
684 GST_INFO_OBJECT (mssdemux, "Changing max bitrate to %llu",
685 mssdemux->connection_speed);
686 gst_mss_manifest_change_bitrate (mssdemux->manifest,
687 mssdemux->connection_speed);
688 mssdemux->update_bitrates = FALSE;
689 GST_OBJECT_UNLOCK (mssdemux);
693 gst_mss_demux_expose_stream (GstMssDemux * mssdemux, GstMssDemuxStream * stream)
697 GstPad *pad = stream->pad;
699 media_caps = gst_mss_stream_get_caps (stream->manifest_stream);
702 caps = gst_caps_new_simple ("video/quicktime", "variant", G_TYPE_STRING,
703 "mss-fragmented", "timescale", G_TYPE_UINT64,
704 gst_mss_stream_get_timescale (stream->manifest_stream), "media-caps",
705 GST_TYPE_CAPS, media_caps, NULL);
706 gst_caps_unref (media_caps);
707 gst_pad_set_caps (pad, caps);
708 gst_caps_unref (caps);
710 gst_pad_set_active (pad, TRUE);
711 GST_INFO_OBJECT (mssdemux, "Adding srcpad %s:%s with caps %" GST_PTR_FORMAT,
712 GST_DEBUG_PAD_NAME (pad), caps);
713 gst_object_ref (pad);
714 gst_element_add_pad (GST_ELEMENT_CAST (mssdemux), pad);
716 GST_WARNING_OBJECT (mssdemux,
717 "Couldn't get caps from manifest stream %p %s, not exposing it", stream,
718 GST_PAD_NAME (stream->pad));
725 gst_mss_demux_process_manifest (GstMssDemux * mssdemux)
732 g_return_val_if_fail (mssdemux->manifest_buffer != NULL, FALSE);
733 g_return_val_if_fail (mssdemux->manifest == NULL, FALSE);
735 query = gst_query_new_uri ();
736 ret = gst_pad_peer_query (mssdemux->sinkpad, query);
739 gst_query_parse_uri (query, &uri);
740 GST_INFO_OBJECT (mssdemux, "Upstream is using URI: %s", uri);
742 mssdemux->manifest_uri = g_strdup (uri);
743 baseurl_end = g_strrstr (uri, "/Manifest");
745 /* set the new end of the string */
746 baseurl_end[0] = '\0';
748 GST_WARNING_OBJECT (mssdemux, "Stream's URI didn't end with /manifest");
751 mssdemux->base_url = uri;
753 gst_query_unref (query);
755 if (mssdemux->base_url == NULL) {
756 GST_ELEMENT_ERROR (mssdemux, RESOURCE, NOT_FOUND,
757 (_("Couldn't get the Manifest's URI")),
758 ("need to get the manifest's URI from upstream elements"));
762 mssdemux->manifest = gst_mss_manifest_new (mssdemux->manifest_buffer);
763 if (!mssdemux->manifest) {
764 GST_ELEMENT_ERROR (mssdemux, STREAM, FORMAT, ("Bad manifest file"),
765 ("Xml manifest file couldn't be parsed"));
769 GST_INFO_OBJECT (mssdemux, "Live stream: %d",
770 gst_mss_manifest_is_live (mssdemux->manifest));
772 gst_mss_demux_create_streams (mssdemux);
773 for (iter = mssdemux->streams; iter;) {
774 GSList *current = iter;
775 GstMssDemuxStream *stream = iter->data;
776 iter = g_slist_next (iter); /* do it ourselves as we want it done in the beginning of the loop */
777 if (!gst_mss_demux_expose_stream (mssdemux, stream)) {
778 gst_mss_demux_stream_free (stream);
779 mssdemux->streams = g_slist_delete_link (mssdemux->streams, current);
783 if (!mssdemux->streams) {
785 GST_WARNING_OBJECT (mssdemux, "Couldn't identify the caps for any of the "
786 "streams found in the manifest");
787 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
788 (_("This file contains no playable streams.")),
789 ("No known stream formats found at the Manifest"));
793 gst_element_no_more_pads (GST_ELEMENT_CAST (mssdemux));
798 gst_mss_demux_reload_manifest (GstMssDemux * mssdemux)
800 GstUriDownloader *downloader;
801 GstFragment *manifest_data;
802 GstBuffer *manifest_buffer;
804 downloader = gst_uri_downloader_new ();
807 gst_uri_downloader_fetch_uri (downloader, mssdemux->manifest_uri);
808 manifest_buffer = gst_fragment_get_buffer (manifest_data);
809 g_object_unref (manifest_data);
811 gst_mss_manifest_reload_fragments (mssdemux->manifest, manifest_buffer);
812 gst_buffer_replace (&mssdemux->manifest_buffer, manifest_buffer);
813 gst_buffer_unref (manifest_buffer);
815 g_object_unref (downloader);
819 gst_mss_demux_reconfigure (GstMssDemux * mssdemux)
821 GSList *oldpads = NULL;
824 gst_mss_demux_stop_tasks (mssdemux, TRUE);
825 if (gst_mss_manifest_change_bitrate (mssdemux->manifest,
826 mssdemux->connection_speed)) {
827 GstClockTime newseg_ts = GST_CLOCK_TIME_NONE;
829 GST_DEBUG_OBJECT (mssdemux, "Creating new pad group");
830 /* if we changed the bitrate, we need to add new pads */
831 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
832 GstMssDemuxStream *stream = iter->data;
833 GstPad *oldpad = stream->pad;
834 GstClockTime ts = GST_CLOCK_TIME_NONE;
836 oldpads = g_slist_prepend (oldpads, oldpad);
838 /* since we are flushing the queue, get the next un-pushed timestamp to seek
840 gst_data_queue_set_flushing (stream->dataqueue, FALSE);
841 if (!gst_data_queue_is_empty (stream->dataqueue)) {
842 GstDataQueueItem *item = NULL;
844 while (!gst_data_queue_is_empty (stream->dataqueue)
845 && !GST_CLOCK_TIME_IS_VALID (ts)) {
846 gst_data_queue_pop (stream->dataqueue, &item);
849 g_assert_not_reached ();
853 if (GST_IS_BUFFER (item->object)) {
854 GstBuffer *buffer = GST_BUFFER_CAST (item->object);
856 ts = GST_BUFFER_TIMESTAMP (buffer);
858 item->destroy (item);
862 if (!GST_CLOCK_TIME_IS_VALID (ts)) {
863 ts = gst_mss_stream_get_fragment_gst_timestamp
864 (stream->manifest_stream);
870 GST_DEBUG_OBJECT (mssdemux,
871 "Seeking stream %p %s to ts %" GST_TIME_FORMAT, stream,
872 GST_PAD_NAME (stream->pad), GST_TIME_ARGS (ts));
873 gst_mss_stream_seek (stream->manifest_stream, ts);
874 gst_data_queue_flush (stream->dataqueue);
876 stream->pad = _create_pad (mssdemux, stream->manifest_stream);
877 gst_mss_demux_expose_stream (mssdemux, stream);
879 gst_pad_push_event (oldpad, gst_event_new_eos ());
882 gst_element_no_more_pads (GST_ELEMENT (mssdemux));
884 for (iter = oldpads; iter; iter = g_slist_next (iter)) {
885 GstPad *oldpad = iter->data;
887 gst_pad_set_active (oldpad, FALSE);
888 gst_element_remove_pad (GST_ELEMENT (mssdemux), oldpad);
889 gst_object_unref (oldpad);
891 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
892 GstMssDemuxStream *stream = iter->data;
894 stream->pending_newsegment =
895 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, newseg_ts, -1,
899 gst_mss_demux_restart_tasks (mssdemux);
903 _free_data_queue_item (gpointer obj)
905 GstDataQueueItem *item = obj;
907 gst_mini_object_unref (item->object);
908 g_slice_free (GstDataQueueItem, item);
912 gst_mss_demux_stream_store_object (GstMssDemuxStream * stream,
915 GstDataQueueItem *item;
917 item = g_slice_new (GstDataQueueItem);
918 item->object = (GstMiniObject *) obj;
920 item->duration = 0; /* we don't care */
922 item->visible = TRUE;
924 item->destroy = (GDestroyNotify) _free_data_queue_item;
926 if (!gst_data_queue_push (stream->dataqueue, item)) {
927 GST_DEBUG_OBJECT (stream->parent, "Failed to store object %p", obj);
928 gst_mini_object_unref (obj);
929 g_slice_free (GstDataQueueItem, item);
934 gst_mss_demux_stream_download_fragment (GstMssDemuxStream * stream,
937 GstMssDemux *mssdemux = stream->parent;
940 GstFragment *fragment;
942 GstFlowReturn ret = GST_FLOW_OK;
944 GST_DEBUG_OBJECT (mssdemux, "Getting url for stream %p", stream);
945 ret = gst_mss_stream_get_fragment_url (stream->manifest_stream, &path);
948 break; /* all is good, let's go */
949 case GST_FLOW_UNEXPECTED: /* EOS */
950 gst_mss_demux_reload_manifest (mssdemux);
952 return GST_FLOW_UNEXPECTED;
961 GST_DEBUG_OBJECT (mssdemux, "Got url path '%s' for stream %p", path, stream);
963 url = g_strdup_printf ("%s/%s", mssdemux->base_url, path);
965 GST_DEBUG_OBJECT (mssdemux, "Got url '%s' for stream %p", url, stream);
967 fragment = gst_uri_downloader_fetch_uri (stream->downloader, url);
972 GST_INFO_OBJECT (mssdemux, "No fragment downloaded");
973 /* TODO check if we are truly stoping */
974 if (gst_mss_manifest_is_live (mssdemux->manifest)) {
975 /* looks like there is no way of knowing when a live stream has ended
976 * Have to assume we are falling behind and cause a manifest reload */
979 return GST_FLOW_ERROR;
982 _buffer = gst_fragment_get_buffer (fragment);
983 _buffer = gst_buffer_make_metadata_writable (_buffer);
984 gst_buffer_set_caps (_buffer, GST_PAD_CAPS (stream->pad));
985 GST_BUFFER_TIMESTAMP (_buffer) =
986 gst_mss_stream_get_fragment_gst_timestamp (stream->manifest_stream);
987 GST_BUFFER_DURATION (_buffer) =
988 gst_mss_stream_get_fragment_gst_duration (stream->manifest_stream);
990 g_object_unref (fragment);
996 GST_DEBUG_OBJECT (mssdemux,
997 "Storing buffer for stream %p - %s. Timestamp: %" GST_TIME_FORMAT
998 " Duration: %" GST_TIME_FORMAT,
999 stream, GST_PAD_NAME (stream->pad),
1000 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (_buffer)),
1001 GST_TIME_ARGS (GST_BUFFER_DURATION (_buffer)));
1002 gst_mss_demux_stream_store_object (stream, GST_MINI_OBJECT_CAST (_buffer));
1009 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
1010 (_("Failed to get fragment URL.")),
1011 ("An error happened when getting fragment URL"));
1012 gst_task_stop (stream->download_task);
1013 return GST_FLOW_ERROR;
1017 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1018 gst_task_stop (stream->download_task);
1019 return GST_FLOW_ERROR;
1024 gst_mss_demux_download_loop (GstMssDemuxStream * stream)
1026 GstMssDemux *mssdemux = stream->parent;
1027 GstBuffer *buffer = NULL;
1030 GST_LOG_OBJECT (mssdemux, "download loop start %p", stream);
1033 ret = gst_mss_demux_stream_download_fragment (stream, &buffer);
1036 break; /* all is good, let's go */
1037 case GST_FLOW_UNEXPECTED: /* EOS */
1039 case GST_FLOW_ERROR:
1046 gst_mss_stream_advance_fragment (stream->manifest_stream);
1048 GST_LOG_OBJECT (mssdemux, "download loop end %p", stream);
1053 GST_DEBUG_OBJECT (mssdemux, "Storing EOS for pad %s:%s",
1054 GST_DEBUG_PAD_NAME (stream->pad));
1055 gst_mss_demux_stream_store_object (stream,
1056 GST_MINI_OBJECT_CAST (gst_event_new_eos ()));
1057 gst_task_stop (stream->download_task);
1062 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1063 gst_task_stop (stream->download_task);
1068 static GstFlowReturn
1069 gst_mss_demux_select_latest_stream (GstMssDemux * mssdemux,
1070 GstMssDemuxStream ** stream)
1072 GstFlowReturn ret = GST_FLOW_OK;
1073 GstMssDemuxStream *current = NULL;
1074 GstClockTime cur_time = GST_CLOCK_TIME_NONE;
1077 if (!mssdemux->streams)
1078 return GST_FLOW_ERROR;
1080 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
1082 GstMssDemuxStream *other;
1083 GstDataQueueItem *item;
1090 if (gst_data_queue_peek (other->dataqueue, &item)) {
1093 return GST_FLOW_WRONG_STATE;
1096 if (GST_IS_EVENT (item->object)) {
1097 /* events have higher priority */
1101 time = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (item->object));
1102 if (time < cur_time) {
1109 if (current == NULL)
1110 ret = GST_FLOW_UNEXPECTED;
1115 gst_mss_demux_stream_loop (GstMssDemux * mssdemux)
1117 GstMssDemuxStream *stream = NULL;
1119 GstMiniObject *object = NULL;
1120 GstDataQueueItem *item = NULL;
1122 GST_LOG_OBJECT (mssdemux, "Starting stream loop");
1124 GST_OBJECT_LOCK (mssdemux);
1125 if (mssdemux->update_bitrates) {
1126 mssdemux->update_bitrates = FALSE;
1127 GST_OBJECT_UNLOCK (mssdemux);
1129 GST_DEBUG_OBJECT (mssdemux,
1130 "Starting streams reconfiguration due to bitrate changes");
1131 gst_mss_demux_reconfigure (mssdemux);
1132 GST_DEBUG_OBJECT (mssdemux, "Finished streams reconfiguration");
1134 GST_OBJECT_UNLOCK (mssdemux);
1137 ret = gst_mss_demux_select_latest_stream (mssdemux, &stream);
1140 GST_DEBUG_OBJECT (mssdemux,
1141 "Stream loop selected %p stream of pad %s. %d - %s", stream,
1142 GST_PAD_NAME (stream->pad), ret, gst_flow_get_name (ret));
1144 GST_DEBUG_OBJECT (mssdemux, "No streams selected -> %d - %s", ret,
1145 gst_flow_get_name (ret));
1150 case GST_FLOW_ERROR:
1152 case GST_FLOW_UNEXPECTED:
1154 case GST_FLOW_WRONG_STATE:
1155 GST_DEBUG_OBJECT (mssdemux, "Wrong state, stopping task");
1158 g_assert_not_reached ();
1161 GST_LOG_OBJECT (mssdemux, "popping next item from queue for stream %p %s",
1162 stream, GST_PAD_NAME (stream->pad));
1163 if (gst_data_queue_pop (stream->dataqueue, &item)) {
1165 object = gst_mini_object_ref (item->object);
1166 item->destroy (item);
1168 GST_DEBUG_OBJECT (mssdemux,
1169 "Failed to get object from dataqueue on stream %p %s", stream,
1170 GST_PAD_NAME (stream->pad));
1174 if (G_UNLIKELY (stream->pending_newsegment)) {
1175 gst_pad_push_event (stream->pad, stream->pending_newsegment);
1176 stream->pending_newsegment = NULL;
1179 if (G_LIKELY (GST_IS_BUFFER (object))) {
1180 if (GST_BUFFER_TIMESTAMP (object) != stream->next_timestamp) {
1181 GST_ERROR_OBJECT (mssdemux, "Marking buffer %p as discont buffer:%"
1182 GST_TIME_FORMAT " != expected:%" GST_TIME_FORMAT, object,
1183 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (object)),
1184 GST_TIME_ARGS (stream->next_timestamp));
1185 GST_BUFFER_FLAG_SET (object, GST_BUFFER_FLAG_DISCONT);
1188 GST_DEBUG_OBJECT (mssdemux,
1189 "Pushing buffer %p %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
1190 " discont:%d on pad %s", object,
1191 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (object)),
1192 GST_TIME_ARGS (GST_BUFFER_DURATION (object)),
1193 GST_BUFFER_FLAG_IS_SET (object, GST_BUFFER_FLAG_DISCONT),
1194 GST_PAD_NAME (stream->pad));
1196 stream->next_timestamp =
1197 GST_BUFFER_TIMESTAMP (object) + GST_BUFFER_DURATION (object);
1199 ret = gst_pad_push (stream->pad, GST_BUFFER_CAST (object));
1200 } else if (GST_IS_EVENT (object)) {
1201 if (GST_EVENT_TYPE (object) == GST_EVENT_EOS)
1203 GST_DEBUG_OBJECT (mssdemux, "Pushing event %p on pad %s", object,
1204 GST_PAD_NAME (stream->pad));
1205 gst_pad_push_event (stream->pad, GST_EVENT_CAST (object));
1207 g_return_if_reached ();
1211 case GST_FLOW_UNEXPECTED:
1212 goto eos; /* EOS ? */
1213 case GST_FLOW_ERROR:
1215 case GST_FLOW_NOT_LINKED:
1216 break; /* TODO what to do here? pause the task or just keep pushing? */
1222 GST_LOG_OBJECT (mssdemux, "Stream loop end");
1227 GST_DEBUG_OBJECT (mssdemux, "EOS on all pads");
1228 gst_task_stop (mssdemux->stream_task);
1233 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1234 gst_task_stop (mssdemux->stream_task);
1239 GST_DEBUG_OBJECT (mssdemux, "Stopping streaming task");
1240 gst_task_stop (mssdemux->stream_task);