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.
25 #include <libxml/parser.h>
26 #include <libxml/tree.h>
28 #include "gstmssmanifest.h"
30 #define MSS_NODE_STREAM_FRAGMENT "c"
31 #define MSS_NODE_STREAM_QUALITY "QualityLevel"
33 #define MSS_PROP_BITRATE "Bitrate"
34 #define MSS_PROP_DURATION "d"
35 #define MSS_PROP_NUMBER "n"
36 #define MSS_PROP_TIME "t"
37 #define MSS_PROP_URL "Url"
39 /* TODO check if atoi is successful? */
41 typedef struct _GstMssStreamFragment
46 } GstMssStreamFragment;
52 gint selectedQualityIndex;
59 GList *current_fragment;
60 GList *current_quality;
62 /* TODO move this to somewhere static */
63 GRegex *regex_bitrate;
64 GRegex *regex_position;
67 struct _GstMssManifest
70 xmlNodePtr xmlrootnode;
76 node_has_type (xmlNodePtr node, const gchar * name)
78 return strcmp ((gchar *) node->name, name) == 0;
82 _gst_mss_stream_init (GstMssStream * stream, xmlNodePtr node)
85 GstMssStreamFragment *previous_fragment = NULL;
86 guint fragment_number = 0;
87 guint fragment_time_accum = 0;
88 GError *gerror = NULL;
90 stream->xmlnode = node;
92 /* get the base url path generator */
93 stream->url = (gchar *) xmlGetProp (node, (xmlChar *) MSS_PROP_URL);
95 for (iter = node->children; iter; iter = iter->next) {
96 if (node_has_type (iter, MSS_NODE_STREAM_FRAGMENT)) {
100 GstMssStreamFragment *fragment = g_new (GstMssStreamFragment, 1);
102 duration_str = (gchar *) xmlGetProp (iter, (xmlChar *) MSS_PROP_DURATION);
103 time_str = (gchar *) xmlGetProp (iter, (xmlChar *) MSS_PROP_TIME);
104 seqnum_str = (gchar *) xmlGetProp (iter, (xmlChar *) MSS_PROP_NUMBER);
106 /* use the node's seq number or use the previous + 1 */
108 fragment->number = atoi (seqnum_str);
111 fragment->number = fragment_number;
113 fragment_number = fragment->number + 1;
116 fragment->time = atoi (time_str);
119 fragment->time = fragment_time_accum;
122 /* if we have a previous fragment, means we need to set its duration */
123 if (previous_fragment)
124 previous_fragment->duration = fragment->time - previous_fragment->time;
127 fragment->duration = atoi (duration_str);
129 previous_fragment = NULL;
130 fragment_time_accum += fragment->duration;
131 g_free (duration_str);
133 /* store to set the duration at the next iteration */
134 previous_fragment = fragment;
137 /* we reverse it later */
138 stream->fragments = g_list_prepend (stream->fragments, fragment);
140 } else if (node_has_type (iter, MSS_NODE_STREAM_QUALITY)) {
141 stream->qualities = g_list_prepend (stream->qualities, iter);
143 /* TODO gst log this */
147 stream->fragments = g_list_reverse (stream->fragments);
148 stream->qualities = g_list_reverse (stream->qualities);
150 stream->current_fragment = stream->fragments;
151 stream->current_quality = stream->qualities;
153 stream->regex_bitrate = g_regex_new ("\\{[Bb]itrate\\}", 0, 0, &gerror);
154 stream->regex_position = g_regex_new ("\\{start[ _]time\\}", 0, 0, &gerror);
158 gst_mss_manifest_new (const GstBuffer * data)
160 GstMssManifest *manifest;
164 manifest = g_malloc0 (sizeof (GstMssManifest));
166 manifest->xml = xmlReadMemory ((const gchar *) GST_BUFFER_DATA (data),
167 GST_BUFFER_SIZE (data), "manifest", NULL, 0);
168 root = manifest->xmlrootnode = xmlDocGetRootElement (manifest->xml);
170 for (nodeiter = root->children; nodeiter; nodeiter = nodeiter->next) {
171 if (nodeiter->type == XML_ELEMENT_NODE
172 && (strcmp ((const char *) nodeiter->name, "StreamIndex") == 0)) {
173 GstMssStream *stream = g_new0 (GstMssStream, 1);
175 manifest->streams = g_slist_append (manifest->streams, stream);
176 _gst_mss_stream_init (stream, nodeiter);
184 gst_mss_stream_free (GstMssStream * stream)
186 g_list_free_full (stream->fragments, g_free);
187 g_list_free (stream->qualities);
188 g_free (stream->url);
189 g_regex_unref (stream->regex_position);
190 g_regex_unref (stream->regex_bitrate);
195 gst_mss_manifest_free (GstMssManifest * manifest)
197 g_return_if_fail (manifest != NULL);
199 g_slist_free_full (manifest->streams, (GDestroyNotify) gst_mss_stream_free);
201 xmlFreeDoc (manifest->xml);
206 gst_mss_manifest_get_streams (GstMssManifest * manifest)
208 return manifest->streams;
212 gst_mss_stream_get_type (GstMssStream * stream)
214 gchar *prop = (gchar *) xmlGetProp (stream->xmlnode, (xmlChar *) "Type");
215 GstMssStreamType ret = MSS_STREAM_TYPE_UNKNOWN;
217 if (strcmp (prop, "video") == 0) {
218 ret = MSS_STREAM_TYPE_VIDEO;
219 } else if (strcmp (prop, "audio") == 0) {
220 ret = MSS_STREAM_TYPE_AUDIO;
227 _gst_mss_stream_video_caps_from_fourcc (gchar * fourcc)
232 if (strcmp (fourcc, "H264") == 0) {
233 return gst_caps_new_simple ("video/x-h264", NULL);
239 _gst_mss_stream_audio_caps_from_fourcc (gchar * fourcc)
244 if (strcmp (fourcc, "AACL") == 0) {
245 return gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 4,
252 _gst_mss_stream_video_caps_from_qualitylevel_xml (xmlNodePtr node)
255 GstStructure *structure;
256 gchar *fourcc = (gchar *) xmlGetProp (node, (xmlChar *) "FourCC");
257 gchar *max_width = (gchar *) xmlGetProp (node, (xmlChar *) "MaxWidth");
258 gchar *max_height = (gchar *) xmlGetProp (node, (xmlChar *) "MaxHeight");
260 (gchar *) xmlGetProp (node, (xmlChar *) "CodecPrivateData");
262 caps = _gst_mss_stream_video_caps_from_fourcc (fourcc);
266 structure = gst_caps_get_structure (caps, 0);
269 gst_structure_set (structure, "width", G_TYPE_INT, atoi (max_width), NULL);
271 gst_structure_set (structure, "height", G_TYPE_INT, atoi (max_height),
275 GValue *value = g_new0 (GValue, 1);
276 g_value_init (value, GST_TYPE_BUFFER);
277 gst_value_deserialize (value, (gchar *) codec_data);
278 gst_structure_take_value (structure, "codec_data", value);
291 _gst_mss_stream_audio_caps_from_qualitylevel_xml (xmlNodePtr node)
294 GstStructure *structure;
295 gchar *fourcc = (gchar *) xmlGetProp (node, (xmlChar *) "FourCC");
296 gchar *channels = (gchar *) xmlGetProp (node, (xmlChar *) "Channels");
297 gchar *rate = (gchar *) xmlGetProp (node, (xmlChar *) "SamplingRate");
299 (gchar *) xmlGetProp (node, (xmlChar *) "CodecPrivateData");
301 caps = _gst_mss_stream_audio_caps_from_fourcc (fourcc);
305 structure = gst_caps_get_structure (caps, 0);
308 gst_structure_set (structure, "channels", G_TYPE_INT, atoi (channels),
311 gst_structure_set (structure, "rate", G_TYPE_INT, atoi (rate), NULL);
314 GValue *value = g_new0 (GValue, 1);
315 g_value_init (value, GST_TYPE_BUFFER);
316 gst_value_deserialize (value, (gchar *) codec_data);
317 gst_structure_take_value (structure, "codec_data", value);
330 gst_mss_stream_get_caps (GstMssStream * stream)
332 GstMssStreamType streamtype = gst_mss_stream_get_type (stream);
333 xmlNodePtr qualitylevel = stream->current_quality->data;
335 if (streamtype == MSS_STREAM_TYPE_VIDEO)
336 return _gst_mss_stream_video_caps_from_qualitylevel_xml (qualitylevel);
337 else if (streamtype == MSS_STREAM_TYPE_AUDIO)
338 return _gst_mss_stream_audio_caps_from_qualitylevel_xml (qualitylevel);
344 gst_mss_stream_get_fragment_url (GstMssStream * stream, gchar ** url)
348 gchar *start_time_str;
349 GstMssStreamFragment *fragment;
351 if (stream->current_fragment == NULL) /* stream is over */
352 return GST_FLOW_UNEXPECTED;
354 fragment = stream->current_fragment->data;
357 (gchar *) xmlGetProp (stream->current_quality->data,
358 (xmlChar *) MSS_PROP_BITRATE);
359 start_time_str = g_strdup_printf ("%" G_GUINT64_FORMAT, fragment->time);
361 tmp = g_regex_replace_literal (stream->regex_bitrate, stream->url,
362 strlen (stream->url), 0, bitrate_str, 0, NULL);
363 *url = g_regex_replace_literal (stream->regex_position, tmp,
364 strlen (tmp), 0, start_time_str, 0, NULL);
367 g_free (start_time_str);
368 g_free (bitrate_str);
373 gst_mss_stream_advance_fragment (GstMssStream * stream)
375 if (stream->current_fragment == NULL)
376 return GST_FLOW_UNEXPECTED;
378 stream->current_fragment = g_list_next (stream->current_fragment);
379 if (stream->current_fragment == NULL);
380 return GST_FLOW_UNEXPECTED;
385 gst_mss_stream_type_name (GstMssStreamType streamtype)
387 switch (streamtype) {
388 case MSS_STREAM_TYPE_VIDEO:
390 case MSS_STREAM_TYPE_AUDIO:
392 case MSS_STREAM_TYPE_UNKNOWN: