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 void 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 mssdemux->base_url = NULL;
265 gst_mss_demux_dispose (GObject * object)
267 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (object);
269 if (mssdemux->stream_task) {
270 gst_object_unref (mssdemux->stream_task);
271 g_static_rec_mutex_free (&mssdemux->stream_lock);
272 mssdemux->stream_task = NULL;
275 G_OBJECT_CLASS (parent_class)->dispose (object);
279 gst_mss_demux_set_property (GObject * object, guint prop_id,
280 const GValue * value, GParamSpec * pspec)
282 GstMssDemux *mssdemux = GST_MSS_DEMUX (object);
285 case PROP_CONNECTION_SPEED:
286 GST_OBJECT_LOCK (mssdemux);
287 mssdemux->connection_speed = g_value_get_uint (value) * 1000;
288 mssdemux->update_bitrates = TRUE;
289 GST_DEBUG_OBJECT (mssdemux, "Connection speed set to %llu",
290 mssdemux->connection_speed);
291 GST_OBJECT_UNLOCK (mssdemux);
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
300 gst_mss_demux_get_property (GObject * object, guint prop_id, GValue * value,
303 GstMssDemux *mssdemux = GST_MSS_DEMUX (object);
306 case PROP_CONNECTION_SPEED:
307 g_value_set_uint (value, mssdemux->connection_speed / 1000);
310 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
315 static GstStateChangeReturn
316 gst_mss_demux_change_state (GstElement * element, GstStateChange transition)
318 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (element);
319 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
321 switch (transition) {
322 case GST_STATE_CHANGE_PAUSED_TO_READY:
323 gst_mss_demux_reset (mssdemux);
325 case GST_STATE_CHANGE_READY_TO_NULL:
331 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
333 switch (transition) {
334 case GST_STATE_CHANGE_PAUSED_TO_READY:{
345 gst_mss_demux_chain (GstPad * pad, GstBuffer * buffer)
347 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (GST_PAD_PARENT (pad));
348 if (mssdemux->manifest_buffer == NULL)
349 mssdemux->manifest_buffer = buffer;
351 mssdemux->manifest_buffer =
352 gst_buffer_join (mssdemux->manifest_buffer, buffer);
358 gst_mss_demux_start (GstMssDemux * mssdemux)
362 GST_INFO_OBJECT (mssdemux, "Starting streams' tasks");
363 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
364 GstMssDemuxStream *stream = iter->data;
365 gst_task_start (stream->download_task);
368 gst_task_start (mssdemux->stream_task);
372 gst_mss_demux_push_src_event (GstMssDemux * mssdemux, GstEvent * event)
377 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
378 GstMssDemuxStream *stream = iter->data;
379 gst_event_ref (event);
380 ret = ret & gst_pad_push_event (stream->pad, event);
386 gst_mss_demux_event (GstPad * pad, GstEvent * event)
388 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (GST_PAD_PARENT (pad));
389 gboolean forward = TRUE;
392 switch (GST_EVENT_TYPE (event)) {
394 if (mssdemux->manifest_buffer == NULL) {
395 GST_WARNING_OBJECT (mssdemux, "Received EOS without a manifest.");
399 gst_mss_demux_process_manifest (mssdemux);
400 gst_mss_demux_start (mssdemux);
408 ret = gst_pad_event_default (pad, event);
410 gst_event_unref (event);
417 gst_mss_demux_stop_tasks (GstMssDemux * mssdemux, gboolean immediate)
421 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
422 GstMssDemuxStream *stream = iter->data;
424 gst_data_queue_set_flushing (stream->dataqueue, TRUE);
427 gst_uri_downloader_cancel (stream->downloader);
428 gst_task_pause (stream->download_task);
430 gst_task_pause (mssdemux->stream_task);
432 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
433 GstMssDemuxStream *stream = iter->data;
434 g_static_rec_mutex_lock (&stream->download_lock);
436 g_static_rec_mutex_lock (&mssdemux->stream_lock);
440 gst_mss_demux_restart_tasks (GstMssDemux * mssdemux)
443 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
444 GstMssDemuxStream *stream = iter->data;
445 g_static_rec_mutex_unlock (&stream->download_lock);
447 g_static_rec_mutex_unlock (&mssdemux->stream_lock);
448 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
449 GstMssDemuxStream *stream = iter->data;
451 gst_data_queue_set_flushing (stream->dataqueue, FALSE);
452 gst_task_start (stream->download_task);
454 gst_task_start (mssdemux->stream_task);
458 gst_mss_demux_src_event (GstPad * pad, GstEvent * event)
460 GstMssDemux *mssdemux;
462 mssdemux = GST_MSS_DEMUX (GST_PAD_PARENT (pad));
464 switch (event->type) {
470 GstSeekType start_type, stop_type;
472 GstEvent *newsegment;
475 GST_INFO_OBJECT (mssdemux, "Received GST_EVENT_SEEK");
477 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
480 if (format != GST_FORMAT_TIME)
483 GST_DEBUG_OBJECT (mssdemux,
484 "seek event, rate: %f start: %" GST_TIME_FORMAT " stop: %"
485 GST_TIME_FORMAT, rate, GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
487 if (flags & GST_SEEK_FLAG_FLUSH) {
488 GstEvent *flush = gst_event_new_flush_start ();
489 GST_DEBUG_OBJECT (mssdemux, "sending flush start");
491 gst_event_set_seqnum (flush, gst_event_get_seqnum (event));
492 gst_mss_demux_push_src_event (mssdemux, flush);
493 gst_event_unref (flush);
496 gst_mss_demux_stop_tasks (mssdemux, TRUE);
498 if (!gst_mss_manifest_seek (mssdemux->manifest, start)) {;
499 GST_WARNING_OBJECT (mssdemux, "Could not find seeked fragment");
504 gst_event_new_new_segment (FALSE, rate, format, start, stop, start);
505 gst_event_set_seqnum (newsegment, gst_event_get_seqnum (event));
506 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
507 GstMssDemuxStream *stream = iter->data;
510 gst_data_queue_flush (stream->dataqueue);
511 stream->pending_newsegment = gst_event_ref (newsegment);
513 gst_event_unref (newsegment);
515 if (flags & GST_SEEK_FLAG_FLUSH) {
516 GstEvent *flush = gst_event_new_flush_stop ();
517 GST_DEBUG_OBJECT (mssdemux, "sending flush stop");
519 gst_event_set_seqnum (flush, gst_event_get_seqnum (event));
520 gst_mss_demux_push_src_event (mssdemux, flush);
521 gst_event_unref (flush);
524 gst_mss_demux_restart_tasks (mssdemux);
532 return gst_pad_event_default (pad, event);
536 gst_mss_demux_src_query (GstPad * pad, GstQuery * query)
538 GstMssDemux *mssdemux;
539 gboolean ret = FALSE;
544 mssdemux = GST_MSS_DEMUX (GST_PAD_PARENT (pad));
546 switch (query->type) {
547 case GST_QUERY_DURATION:{
548 GstClockTime duration = -1;
551 gst_query_parse_duration (query, &fmt, NULL);
552 if (fmt == GST_FORMAT_TIME && mssdemux->manifest) {
553 /* TODO should we use the streams accumulated duration or the main manifest duration? */
554 duration = gst_mss_manifest_get_gst_duration (mssdemux->manifest);
556 if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0) {
557 gst_query_set_duration (query, GST_FORMAT_TIME, duration);
561 GST_INFO_OBJECT (mssdemux, "GST_QUERY_DURATION returns %s with duration %"
562 GST_TIME_FORMAT, ret ? "TRUE" : "FALSE", GST_TIME_ARGS (duration));
565 case GST_QUERY_LATENCY:
566 gst_query_set_latency (query, FALSE, 0, -1);
569 case GST_QUERY_SEEKING:{
573 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
574 GST_INFO_OBJECT (mssdemux, "Received GST_QUERY_SEEKING with format %d",
576 if (fmt == GST_FORMAT_TIME) {
577 GstClockTime duration;
578 duration = gst_mss_manifest_get_gst_duration (mssdemux->manifest);
579 if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0)
581 gst_query_set_seeking (query, fmt, TRUE, 0, stop);
583 GST_INFO_OBJECT (mssdemux, "GST_QUERY_SEEKING returning with stop : %"
584 GST_TIME_FORMAT, GST_TIME_ARGS (stop));
589 /* Don't fordward queries upstream because of the special nature of this
590 * "demuxer", which relies on the upstream element only to be fed
600 _set_src_pad_functions (GstPad * pad)
602 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_mss_demux_src_query));
603 gst_pad_set_event_function (pad, GST_DEBUG_FUNCPTR (gst_mss_demux_src_event));
607 _create_pad (GstMssDemux * mssdemux, GstMssStream * manifeststream)
610 GstPad *srcpad = NULL;
611 GstMssStreamType streamtype;
613 streamtype = gst_mss_stream_get_type (manifeststream);
614 GST_DEBUG_OBJECT (mssdemux, "Found stream of type: %s",
615 gst_mss_stream_type_name (streamtype));
617 /* TODO use stream's name/bitrate/index as the pad name? */
618 if (streamtype == MSS_STREAM_TYPE_VIDEO) {
619 name = g_strdup_printf ("video_%02u", mssdemux->n_videos++);
621 gst_pad_new_from_static_template (&gst_mss_demux_videosrc_template,
624 } else if (streamtype == MSS_STREAM_TYPE_AUDIO) {
625 name = g_strdup_printf ("audio_%02u", mssdemux->n_audios++);
627 gst_pad_new_from_static_template (&gst_mss_demux_audiosrc_template,
633 GST_WARNING_OBJECT (mssdemux, "Ignoring unknown type stream");
637 _set_src_pad_functions (srcpad);
642 gst_mss_demux_create_streams (GstMssDemux * mssdemux)
644 GSList *streams = gst_mss_manifest_get_streams (mssdemux->manifest);
647 if (streams == NULL) {
648 GST_INFO_OBJECT (mssdemux, "No streams found in the manifest");
649 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
650 (_("This file contains no playable streams.")),
651 ("no streams found at the Manifest"));
655 for (iter = streams; iter; iter = g_slist_next (iter)) {
656 GstPad *srcpad = NULL;
657 GstMssDemuxStream *stream = NULL;
658 GstMssStream *manifeststream = iter->data;
660 srcpad = _create_pad (mssdemux, manifeststream);
666 stream = gst_mss_demux_stream_new (mssdemux, manifeststream, srcpad);
667 gst_mss_stream_set_active (manifeststream, TRUE);
668 mssdemux->streams = g_slist_append (mssdemux->streams, stream);
671 /* select initial bitrates */
672 GST_OBJECT_LOCK (mssdemux);
673 GST_INFO_OBJECT (mssdemux, "Changing max bitrate to %llu",
674 mssdemux->connection_speed);
675 gst_mss_manifest_change_bitrate (mssdemux->manifest,
676 mssdemux->connection_speed);
677 mssdemux->update_bitrates = FALSE;
678 GST_OBJECT_UNLOCK (mssdemux);
682 gst_mss_demux_expose_stream (GstMssDemux * mssdemux, GstMssDemuxStream * stream)
686 GstPad *pad = stream->pad;
688 media_caps = gst_mss_stream_get_caps (stream->manifest_stream);
691 caps = gst_caps_new_simple ("video/quicktime", "variant", G_TYPE_STRING,
692 "mss-fragmented", "timescale", G_TYPE_UINT64,
693 gst_mss_stream_get_timescale (stream->manifest_stream), "media-caps",
694 GST_TYPE_CAPS, media_caps, NULL);
695 gst_caps_unref (media_caps);
696 gst_pad_set_caps (pad, caps);
697 gst_caps_unref (caps);
699 gst_pad_set_active (pad, TRUE);
700 GST_INFO_OBJECT (mssdemux, "Adding srcpad %s:%s with caps %" GST_PTR_FORMAT,
701 GST_DEBUG_PAD_NAME (pad), caps);
702 gst_object_ref (pad);
703 gst_element_add_pad (GST_ELEMENT_CAST (mssdemux), pad);
705 GST_WARNING_OBJECT (mssdemux,
706 "Couldn't get caps from manifest stream %p %s, not exposing it", stream,
707 GST_PAD_NAME (stream->pad));
714 gst_mss_demux_process_manifest (GstMssDemux * mssdemux)
721 g_return_if_fail (mssdemux->manifest_buffer != NULL);
722 g_return_if_fail (mssdemux->manifest == NULL);
724 query = gst_query_new_uri ();
725 ret = gst_pad_peer_query (mssdemux->sinkpad, query);
728 gst_query_parse_uri (query, &uri);
729 GST_INFO_OBJECT (mssdemux, "Upstream is using URI: %s", uri);
731 baseurl_end = g_strrstr (uri, "/Manifest");
733 /* set the new end of the string */
734 baseurl_end[0] = '\0';
736 GST_WARNING_OBJECT (mssdemux, "Stream's URI didn't end with /manifest");
739 mssdemux->base_url = uri;
741 gst_query_unref (query);
743 mssdemux->manifest = gst_mss_manifest_new (mssdemux->manifest_buffer);
744 if (!mssdemux->manifest) {
745 GST_ELEMENT_ERROR (mssdemux, STREAM, FORMAT, ("Bad manifest file"),
746 ("Xml manifest file couldn't be parsed"));
750 gst_mss_demux_create_streams (mssdemux);
751 for (iter = mssdemux->streams; iter;) {
752 GSList *current = iter;
753 GstMssDemuxStream *stream = iter->data;
754 iter = g_slist_next (iter); /* do it ourselves as we want it done in the beginning of the loop */
755 if (!gst_mss_demux_expose_stream (mssdemux, stream)) {
756 gst_mss_demux_stream_free (stream);
757 mssdemux->streams = g_slist_delete_link (mssdemux->streams, current);
761 if (!mssdemux->streams) {
763 GST_WARNING_OBJECT (mssdemux, "Couldn't identify the caps for any of the "
764 "streams found in the manifest");
765 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
766 (_("This file contains no playable streams.")),
767 ("No known stream formats found at the Manifest"));
771 gst_element_no_more_pads (GST_ELEMENT_CAST (mssdemux));
775 gst_mss_demux_reconfigure (GstMssDemux * mssdemux)
777 GSList *oldpads = NULL;
780 gst_mss_demux_stop_tasks (mssdemux, TRUE);
781 if (gst_mss_manifest_change_bitrate (mssdemux->manifest,
782 mssdemux->connection_speed)) {
784 GST_DEBUG_OBJECT (mssdemux, "Creating new pad group");
785 /* if we changed the bitrate, we need to add new pads */
786 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
787 GstMssDemuxStream *stream = iter->data;
788 GstPad *oldpad = stream->pad;
789 GstClockTime ts = GST_CLOCK_TIME_NONE;
791 oldpads = g_slist_prepend (oldpads, oldpad);
793 /* since we are flushing the queue, get the next un-pushed timestamp to seek
795 gst_data_queue_set_flushing (stream->dataqueue, FALSE);
796 if (!gst_data_queue_is_empty (stream->dataqueue)) {
797 GstDataQueueItem *item = NULL;
799 while (!gst_data_queue_is_empty (stream->dataqueue)
800 && !GST_CLOCK_TIME_IS_VALID (ts)) {
801 gst_data_queue_pop (stream->dataqueue, &item);
804 g_assert_not_reached ();
808 if (GST_IS_BUFFER (item->object)) {
809 GstBuffer *buffer = GST_BUFFER_CAST (item->object);
811 ts = GST_BUFFER_TIMESTAMP (buffer);
813 item->destroy (item);
817 if (!GST_CLOCK_TIME_IS_VALID (ts)) {
818 ts = gst_mss_stream_get_fragment_gst_timestamp
819 (stream->manifest_stream);
822 GST_DEBUG_OBJECT (mssdemux,
823 "Seeking stream %p %s to ts %" GST_TIME_FORMAT, stream,
824 GST_PAD_NAME (stream->pad), GST_TIME_ARGS (ts));
825 gst_mss_stream_seek (stream->manifest_stream, ts);
826 gst_data_queue_flush (stream->dataqueue);
828 stream->pad = _create_pad (mssdemux, stream->manifest_stream);
829 gst_mss_demux_expose_stream (mssdemux, stream);
831 gst_pad_push_event (oldpad, gst_event_new_eos ());
834 gst_element_no_more_pads (GST_ELEMENT (mssdemux));
836 for (iter = oldpads; iter; iter = g_slist_next (iter)) {
837 GstPad *oldpad = iter->data;
839 gst_pad_set_active (oldpad, FALSE);
840 gst_element_remove_pad (GST_ELEMENT (mssdemux), oldpad);
841 gst_object_unref (oldpad);
844 gst_mss_demux_restart_tasks (mssdemux);
848 _free_data_queue_item (gpointer obj)
850 GstDataQueueItem *item = obj;
852 gst_mini_object_unref (item->object);
853 g_slice_free (GstDataQueueItem, item);
857 gst_mss_demux_stream_store_object (GstMssDemuxStream * stream,
860 GstDataQueueItem *item;
862 item = g_slice_new (GstDataQueueItem);
863 item->object = (GstMiniObject *) obj;
865 item->duration = 0; /* we don't care */
867 item->visible = TRUE;
869 item->destroy = (GDestroyNotify) _free_data_queue_item;
871 if (!gst_data_queue_push (stream->dataqueue, item)) {
872 GST_DEBUG_OBJECT (stream->parent, "Failed to store object %p", obj);
873 gst_mini_object_unref (obj);
874 g_slice_free (GstDataQueueItem, item);
879 gst_mss_demux_stream_download_fragment (GstMssDemuxStream * stream,
882 GstMssDemux *mssdemux = stream->parent;
885 GstFragment *fragment;
887 GstFlowReturn ret = GST_FLOW_OK;
889 GST_DEBUG_OBJECT (mssdemux, "Getting url for stream %p", stream);
890 ret = gst_mss_stream_get_fragment_url (stream->manifest_stream, &path);
893 break; /* all is good, let's go */
894 case GST_FLOW_UNEXPECTED: /* EOS */
895 return GST_FLOW_UNEXPECTED;
904 GST_DEBUG_OBJECT (mssdemux, "Got url path '%s' for stream %p", path, stream);
906 url = g_strdup_printf ("%s/%s", mssdemux->base_url, path);
908 fragment = gst_uri_downloader_fetch_uri (stream->downloader, url);
913 GST_INFO_OBJECT (mssdemux, "No fragment downloaded");
914 /* TODO check if we are truly stoping */
915 return GST_FLOW_ERROR;
918 _buffer = gst_fragment_get_buffer (fragment);
919 _buffer = gst_buffer_make_metadata_writable (_buffer);
920 gst_buffer_set_caps (_buffer, GST_PAD_CAPS (stream->pad));
921 GST_BUFFER_TIMESTAMP (_buffer) =
922 gst_mss_stream_get_fragment_gst_timestamp (stream->manifest_stream);
923 GST_BUFFER_DURATION (_buffer) =
924 gst_mss_stream_get_fragment_gst_duration (stream->manifest_stream);
930 GST_DEBUG_OBJECT (mssdemux,
931 "Storing buffer for stream %p - %s. Timestamp: %" GST_TIME_FORMAT,
932 stream, GST_PAD_NAME (stream->pad),
933 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (_buffer)));
934 gst_mss_demux_stream_store_object (stream, GST_MINI_OBJECT_CAST (_buffer));
941 GST_ELEMENT_ERROR (mssdemux, STREAM, DEMUX,
942 (_("Failed to get fragment URL.")),
943 ("An error happened when getting fragment URL"));
944 gst_task_stop (stream->download_task);
945 return GST_FLOW_ERROR;
949 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
950 gst_task_stop (stream->download_task);
951 return GST_FLOW_ERROR;
956 gst_mss_demux_download_loop (GstMssDemuxStream * stream)
958 GstMssDemux *mssdemux = stream->parent;
959 GstBuffer *buffer = NULL;
962 GST_LOG_OBJECT (mssdemux, "download loop start %p", stream);
965 ret = gst_mss_demux_stream_download_fragment (stream, &buffer);
968 break; /* all is good, let's go */
969 case GST_FLOW_UNEXPECTED: /* EOS */
977 g_assert (buffer != NULL);
979 gst_mss_stream_advance_fragment (stream->manifest_stream);
980 GST_LOG_OBJECT (mssdemux, "download loop end %p", stream);
985 GST_DEBUG_OBJECT (mssdemux, "Storing EOS for pad %s:%s",
986 GST_DEBUG_PAD_NAME (stream->pad));
987 gst_mss_demux_stream_store_object (stream,
988 GST_MINI_OBJECT_CAST (gst_event_new_eos ()));
989 gst_task_stop (stream->download_task);
994 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
995 gst_task_stop (stream->download_task);
1000 static GstFlowReturn
1001 gst_mss_demux_select_latest_stream (GstMssDemux * mssdemux,
1002 GstMssDemuxStream ** stream)
1004 GstFlowReturn ret = GST_FLOW_OK;
1005 GstMssDemuxStream *current = NULL;
1006 GstClockTime cur_time = GST_CLOCK_TIME_NONE;
1009 if (!mssdemux->streams)
1010 return GST_FLOW_ERROR;
1012 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
1014 GstMssDemuxStream *other;
1015 GstDataQueueItem *item;
1022 if (gst_data_queue_peek (other->dataqueue, &item)) {
1025 return GST_FLOW_WRONG_STATE;
1028 if (GST_IS_EVENT (item->object)) {
1029 /* events have higher priority */
1033 time = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (item->object));
1034 if (time < cur_time) {
1041 if (current == NULL)
1042 ret = GST_FLOW_UNEXPECTED;
1047 gst_mss_demux_stream_loop (GstMssDemux * mssdemux)
1049 GstMssDemuxStream *stream = NULL;
1051 GstMiniObject *object = NULL;
1052 GstDataQueueItem *item = NULL;
1054 GST_LOG_OBJECT (mssdemux, "Starting stream loop");
1056 GST_OBJECT_LOCK (mssdemux);
1057 if (mssdemux->update_bitrates) {
1058 mssdemux->update_bitrates = FALSE;
1059 GST_OBJECT_UNLOCK (mssdemux);
1061 GST_DEBUG_OBJECT (mssdemux,
1062 "Starting streams reconfiguration due to bitrate changes");
1063 gst_mss_demux_reconfigure (mssdemux);
1064 GST_DEBUG_OBJECT (mssdemux, "Finished streams reconfiguration");
1066 GST_OBJECT_UNLOCK (mssdemux);
1069 ret = gst_mss_demux_select_latest_stream (mssdemux, &stream);
1072 GST_DEBUG_OBJECT (mssdemux,
1073 "Stream loop selected %p stream of pad %s. %d - %s", stream,
1074 GST_PAD_NAME (stream->pad), ret, gst_flow_get_name (ret));
1076 GST_DEBUG_OBJECT (mssdemux, "No streams selected -> %d - %s", ret,
1077 gst_flow_get_name (ret));
1082 case GST_FLOW_ERROR:
1084 case GST_FLOW_UNEXPECTED:
1086 case GST_FLOW_WRONG_STATE:
1087 GST_DEBUG_OBJECT (mssdemux, "Wrong state, stopping task");
1090 g_assert_not_reached ();
1093 GST_LOG_OBJECT (mssdemux, "popping next item from queue for stream %p %s",
1094 stream, GST_PAD_NAME (stream->pad));
1095 if (gst_data_queue_pop (stream->dataqueue, &item)) {
1097 object = gst_mini_object_ref (item->object);
1098 item->destroy (item);
1100 GST_DEBUG_OBJECT (mssdemux,
1101 "Failed to get object from dataqueue on stream %p %s", stream,
1102 GST_PAD_NAME (stream->pad));
1106 if (G_UNLIKELY (stream->pending_newsegment)) {
1107 gst_pad_push_event (stream->pad, stream->pending_newsegment);
1108 stream->pending_newsegment = NULL;
1111 if (G_LIKELY (GST_IS_BUFFER (object))) {
1112 GST_DEBUG_OBJECT (mssdemux,
1113 "Pushing buffer %p %" GST_TIME_FORMAT " on pad %s", object,
1114 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (object)),
1115 GST_PAD_NAME (stream->pad));
1116 ret = gst_pad_push (stream->pad, GST_BUFFER_CAST (object));
1117 } else if (GST_IS_EVENT (object)) {
1118 if (GST_EVENT_TYPE (object) == GST_EVENT_EOS)
1120 GST_DEBUG_OBJECT (mssdemux, "Pushing event %p on pad %s", object,
1121 GST_PAD_NAME (stream->pad));
1122 gst_pad_push_event (stream->pad, GST_EVENT_CAST (object));
1124 g_return_if_reached ();
1128 case GST_FLOW_UNEXPECTED:
1129 goto eos; /* EOS ? */
1130 case GST_FLOW_ERROR:
1132 case GST_FLOW_NOT_LINKED:
1133 break; /* TODO what to do here? pause the task or just keep pushing? */
1139 GST_LOG_OBJECT (mssdemux, "Stream loop end");
1144 GST_DEBUG_OBJECT (mssdemux, "EOS on all pads");
1145 gst_task_stop (mssdemux->stream_task);
1150 GST_WARNING_OBJECT (mssdemux, "Error while pushing fragment");
1151 gst_task_stop (mssdemux->stream_task);
1156 GST_DEBUG_OBJECT (mssdemux, "Stopping streaming task");
1157 gst_task_stop (mssdemux->stream_task);