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 static GstStaticPadTemplate gst_mss_demux_sink_template =
46 GST_STATIC_PAD_TEMPLATE ("sink",
49 GST_STATIC_CAPS ("application/vnd.ms-sstr+xml")
52 static GstStaticPadTemplate gst_mss_demux_videosrc_template =
53 GST_STATIC_PAD_TEMPLATE ("video_%02u",
58 static GstStaticPadTemplate gst_mss_demux_audiosrc_template =
59 GST_STATIC_PAD_TEMPLATE ("audio_%02u",
64 GST_BOILERPLATE (GstMssDemux, gst_mss_demux, GstMssDemux, GST_TYPE_ELEMENT);
66 static void gst_mss_demux_dispose (GObject * object);
67 static GstStateChangeReturn
68 gst_mss_demux_change_state (GstElement * element, GstStateChange transition);
69 static GstFlowReturn gst_mss_demux_chain (GstPad * pad, GstBuffer * buffer);
70 static GstFlowReturn gst_mss_demux_event (GstPad * pad, GstEvent * event);
72 static void gst_mss_demux_stream_loop (GstMssDemuxStream * stream);
74 static void gst_mss_demux_process_manifest (GstMssDemux * mssdemux);
77 gst_mss_demux_base_init (gpointer klass)
79 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
81 gst_element_class_add_static_pad_template (element_class,
82 &gst_mss_demux_sink_template);
83 gst_element_class_add_static_pad_template (element_class,
84 &gst_mss_demux_videosrc_template);
85 gst_element_class_add_static_pad_template (element_class,
86 &gst_mss_demux_audiosrc_template);
87 gst_element_class_set_details_simple (element_class, "Smooth Streaming "
89 "Parse and demultiplex a Smooth Streaming manifest into audio and video "
90 "streams", "Thiago Santos <thiago.sousa.santos@collabora.com>");
92 GST_DEBUG_CATEGORY_INIT (mssdemux_debug, "mssdemux", 0, "mssdemux plugin");
96 gst_mss_demux_class_init (GstMssDemuxClass * klass)
98 GObjectClass *gobject_class;
99 GstElementClass *gstelement_class;
101 gobject_class = (GObjectClass *) klass;
102 gstelement_class = (GstElementClass *) klass;
104 parent_class = g_type_class_peek_parent (klass);
106 gobject_class->dispose = gst_mss_demux_dispose;
108 gstelement_class->change_state =
109 GST_DEBUG_FUNCPTR (gst_mss_demux_change_state);
113 gst_mss_demux_init (GstMssDemux * mssdemux, GstMssDemuxClass * klass)
116 gst_pad_new_from_static_template (&gst_mss_demux_sink_template, "sink");
117 gst_pad_set_chain_function (mssdemux->sinkpad,
118 GST_DEBUG_FUNCPTR (gst_mss_demux_chain));
119 gst_pad_set_event_function (mssdemux->sinkpad,
120 GST_DEBUG_FUNCPTR (gst_mss_demux_event));
121 gst_element_add_pad (GST_ELEMENT_CAST (mssdemux), mssdemux->sinkpad);
124 static GstMssDemuxStream *
125 gst_mss_demux_stream_new (GstMssDemux * mssdemux,
126 GstMssManifestStream * manifeststream, GstPad * srcpad)
128 GstMssDemuxStream *stream;
130 stream = g_new0 (GstMssDemuxStream, 1);
131 stream->downloader = gst_uri_downloader_new ();
134 g_static_rec_mutex_init (&stream->stream_lock);
135 stream->stream_task =
136 gst_task_create ((GstTaskFunction) gst_mss_demux_stream_loop, stream);
137 gst_task_set_lock (stream->stream_task, &stream->stream_lock);
139 stream->pad = srcpad;
140 stream->manifest_stream = manifeststream;
141 stream->parent = mssdemux;
147 gst_mss_demux_stream_free (GstMssDemuxStream * stream)
149 if (stream->stream_task) {
150 if (GST_TASK_STATE (stream->stream_task) != GST_TASK_STOPPED) {
151 GST_DEBUG_OBJECT (stream->parent, "Leaving streaming task %s:%s",
152 GST_DEBUG_PAD_NAME (stream->pad));
153 gst_task_stop (stream->stream_task);
154 g_static_rec_mutex_lock (&stream->stream_lock);
155 g_static_rec_mutex_unlock (&stream->stream_lock);
156 gst_task_join (stream->stream_task);
158 gst_object_unref (stream->stream_task);
159 g_static_rec_mutex_free (&stream->stream_lock);
160 stream->stream_task = NULL;
163 if (stream->downloader != NULL) {
164 g_object_unref (stream->downloader);
165 stream->downloader = NULL;
171 gst_mss_demux_reset (GstMssDemux * mssdemux)
174 if (mssdemux->manifest_buffer) {
175 gst_buffer_unref (mssdemux->manifest_buffer);
176 mssdemux->manifest_buffer = NULL;
179 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
180 GstMssDemuxStream *stream = iter->data;
181 gst_element_remove_pad (GST_ELEMENT_CAST (mssdemux), stream->pad);
182 gst_mss_demux_stream_free (stream);
184 g_slist_free (mssdemux->streams);
185 mssdemux->streams = NULL;
187 if (mssdemux->manifest) {
188 gst_mss_manifest_free (mssdemux->manifest);
189 mssdemux->manifest = NULL;
192 mssdemux->n_videos = mssdemux->n_audios = 0;
196 gst_mss_demux_dispose (GObject * object)
198 /* GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (object); */
200 G_OBJECT_CLASS (parent_class)->dispose (object);
203 static GstStateChangeReturn
204 gst_mss_demux_change_state (GstElement * element, GstStateChange transition)
206 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (element);
207 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
209 switch (transition) {
210 case GST_STATE_CHANGE_PAUSED_TO_READY:
212 case GST_STATE_CHANGE_READY_TO_NULL:
213 gst_mss_demux_reset (mssdemux);
219 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
221 switch (transition) {
222 case GST_STATE_CHANGE_PAUSED_TO_READY:{
233 gst_mss_demux_chain (GstPad * pad, GstBuffer * buffer)
235 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (GST_PAD_PARENT (pad));
236 if (mssdemux->manifest_buffer == NULL)
237 mssdemux->manifest_buffer = buffer;
239 mssdemux->manifest_buffer =
240 gst_buffer_join (mssdemux->manifest_buffer, buffer);
246 gst_mss_demux_event (GstPad * pad, GstEvent * event)
248 GstMssDemux *mssdemux = GST_MSS_DEMUX_CAST (GST_PAD_PARENT (pad));
249 gboolean forward = TRUE;
252 switch (GST_EVENT_TYPE (event)) {
254 if (mssdemux->manifest_buffer == NULL) {
255 GST_WARNING_OBJECT (mssdemux, "Received EOS without a manifest.");
259 gst_mss_demux_process_manifest (mssdemux);
267 ret = gst_pad_event_default (pad, event);
269 gst_event_unref (event);
276 gst_mss_demux_create_streams (GstMssDemux * mssdemux)
278 GSList *streams = gst_mss_manifest_get_streams (mssdemux->manifest);
281 if (streams == NULL) {
282 GST_INFO_OBJECT (mssdemux, "No streams found in the manifest");
286 for (iter = streams; iter; iter = g_slist_next (iter)) {
288 GstPad *srcpad = NULL;
289 GstMssDemuxStream *stream = NULL;
290 GstMssManifestStream *manifeststream = iter->data;
291 GstMssManifestStreamType streamtype;
293 streamtype = gst_mss_manifest_stream_get_type (manifeststream);
294 GST_DEBUG_OBJECT (mssdemux, "Found stream of type: %s",
295 gst_mss_manifest_stream_type_name (streamtype));
297 /* TODO use stream's name as the pad name? */
298 if (streamtype == MSS_STREAM_TYPE_VIDEO) {
299 name = g_strdup_printf ("video_%02u", mssdemux->n_videos++);
301 gst_pad_new_from_static_template (&gst_mss_demux_videosrc_template,
304 } else if (streamtype == MSS_STREAM_TYPE_AUDIO) {
305 name = g_strdup_printf ("audio_%02u", mssdemux->n_audios++);
307 gst_pad_new_from_static_template (&gst_mss_demux_audiosrc_template,
313 GST_WARNING_OBJECT (mssdemux, "Ignoring unknown type stream");
317 stream = gst_mss_demux_stream_new (mssdemux, manifeststream, srcpad);
318 mssdemux->streams = g_slist_append (mssdemux->streams, stream);
323 gst_mss_demux_expose_stream (GstMssDemux * mssdemux, GstMssDemuxStream * stream)
326 GstPad *pad = stream->pad;
328 caps = gst_mss_manifest_stream_get_caps (stream->manifest_stream);
331 gst_pad_set_caps (pad, caps);
332 gst_caps_unref (caps);
334 gst_pad_set_active (pad, TRUE);
335 GST_INFO_OBJECT (mssdemux, "Adding srcpad %s:%s with caps %" GST_PTR_FORMAT,
336 GST_DEBUG_PAD_NAME (pad), caps);
337 gst_element_add_pad (GST_ELEMENT_CAST (mssdemux), pad);
339 GST_WARNING_OBJECT (mssdemux, "Not exposing stream of unrecognized format");
344 gst_mss_demux_process_manifest (GstMssDemux * mssdemux)
351 g_return_if_fail (mssdemux->manifest_buffer != NULL);
352 g_return_if_fail (mssdemux->manifest == NULL);
354 query = gst_query_new_uri ();
355 ret = gst_pad_peer_query (mssdemux->sinkpad, query);
357 gst_query_parse_uri (query, &uri);
358 /* TODO use this to get the base url for the fragments */
361 gst_query_unref (query);
363 mssdemux->manifest = gst_mss_manifest_new (mssdemux->manifest_buffer);
364 if (!mssdemux->manifest) {
365 GST_ELEMENT_ERROR (mssdemux, STREAM, FORMAT, ("Bad manifest file"),
366 ("Xml manifest file couldn't be parsed"));
370 gst_mss_demux_create_streams (mssdemux);
371 for (iter = mssdemux->streams; iter; iter = g_slist_next (iter)) {
372 gst_mss_demux_expose_stream (mssdemux, iter->data);
377 gst_mss_demux_stream_loop (GstMssDemuxStream * stream)
380 GstFragment *fragment;
381 GstBufferList *buflist;
384 ret = gst_mss_manifest_stream_get_fragment_url (stream->manifest_stream,
394 fragment = gst_uri_downloader_fetch_uri (stream->downloader, url);
397 buflist = gst_fragment_get_buffer_list (fragment);
399 ret = gst_pad_push_list (stream->pad, buflist); /* TODO check return */
401 ret = gst_mss_manifest_stream_advance_fragment (stream->manifest_stream);