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 _GstMssManifestStreamFragment
46 } GstMssManifestStreamFragment;
48 struct _GstMssManifestStream
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_manifest_stream_init (GstMssManifestStream * stream, xmlNodePtr node)
85 GstMssManifestStreamFragment *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 GstMssManifestStreamFragment *fragment =
101 g_new (GstMssManifestStreamFragment, 1);
103 duration_str = (gchar *) xmlGetProp (iter, (xmlChar *) MSS_PROP_DURATION);
104 time_str = (gchar *) xmlGetProp (iter, (xmlChar *) MSS_PROP_TIME);
105 seqnum_str = (gchar *) xmlGetProp (iter, (xmlChar *) MSS_PROP_NUMBER);
107 /* use the node's seq number or use the previous + 1 */
109 fragment->number = atoi (seqnum_str);
112 fragment->number = fragment_number;
114 fragment_number = fragment->number + 1;
117 fragment->time = atoi (time_str);
120 fragment->time = fragment_time_accum;
123 /* if we have a previous fragment, means we need to set its duration */
124 if (previous_fragment)
125 previous_fragment->duration = fragment->time - previous_fragment->time;
128 fragment->duration = atoi (duration_str);
130 previous_fragment = NULL;
131 fragment_time_accum += fragment->duration;
132 g_free (duration_str);
134 /* store to set the duration at the next iteration */
135 previous_fragment = fragment;
138 /* we reverse it later */
139 stream->fragments = g_list_prepend (stream->fragments, fragment);
141 } else if (node_has_type (iter, MSS_NODE_STREAM_QUALITY)) {
142 stream->qualities = g_list_prepend (stream->qualities, iter);
144 /* TODO gst log this */
148 stream->fragments = g_list_reverse (stream->fragments);
149 stream->qualities = g_list_reverse (stream->qualities);
151 stream->current_fragment = stream->fragments;
152 stream->current_quality = stream->qualities;
154 stream->regex_bitrate = g_regex_new ("\\{[Bb]itrate\\}", 0, 0, &gerror);
155 stream->regex_position = g_regex_new ("\\{start[ _]time\\}", 0, 0, &gerror);
159 gst_mss_manifest_new (const GstBuffer * data)
161 GstMssManifest *manifest;
165 manifest = g_malloc0 (sizeof (GstMssManifest));
167 manifest->xml = xmlReadMemory ((const gchar *) GST_BUFFER_DATA (data),
168 GST_BUFFER_SIZE (data), "manifest", NULL, 0);
169 root = manifest->xmlrootnode = xmlDocGetRootElement (manifest->xml);
171 for (nodeiter = root->children; nodeiter; nodeiter = nodeiter->next) {
172 if (nodeiter->type == XML_ELEMENT_NODE
173 && (strcmp ((const char *) nodeiter->name, "StreamIndex") == 0)) {
174 GstMssManifestStream *stream = g_new0 (GstMssManifestStream, 1);
176 manifest->streams = g_slist_append (manifest->streams, stream);
177 _gst_mss_manifest_stream_init (stream, nodeiter);
185 gst_mss_manifest_stream_free (GstMssManifestStream * stream)
187 g_list_free_full (stream->fragments, g_free);
188 g_list_free (stream->qualities);
189 g_free (stream->url);
190 g_regex_unref (stream->regex_position);
191 g_regex_unref (stream->regex_bitrate);
196 gst_mss_manifest_free (GstMssManifest * manifest)
198 g_return_if_fail (manifest != NULL);
200 g_slist_free_full (manifest->streams,
201 (GDestroyNotify) gst_mss_manifest_stream_free);
203 xmlFreeDoc (manifest->xml);
208 gst_mss_manifest_get_streams (GstMssManifest * manifest)
210 return manifest->streams;
213 GstMssManifestStreamType
214 gst_mss_manifest_stream_get_type (GstMssManifestStream * stream)
216 gchar *prop = (gchar *) xmlGetProp (stream->xmlnode, (xmlChar *) "Type");
217 GstMssManifestStreamType ret = MSS_STREAM_TYPE_UNKNOWN;
219 if (strcmp (prop, "video") == 0) {
220 ret = MSS_STREAM_TYPE_VIDEO;
221 } else if (strcmp (prop, "audio") == 0) {
222 ret = MSS_STREAM_TYPE_AUDIO;
229 _gst_mss_manifest_stream_video_caps_from_fourcc (gchar * fourcc)
234 if (strcmp (fourcc, "H264") == 0) {
235 return gst_caps_new_simple ("video/x-h264", NULL);
241 _gst_mss_manifest_stream_audio_caps_from_fourcc (gchar * fourcc)
246 if (strcmp (fourcc, "AACL") == 0) {
247 return gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 4,
254 _gst_mss_manifest_stream_video_caps_from_qualitylevel_xml (xmlNodePtr node)
257 GstStructure *structure;
258 gchar *fourcc = (gchar *) xmlGetProp (node, (xmlChar *) "FourCC");
259 gchar *max_width = (gchar *) xmlGetProp (node, (xmlChar *) "MaxWidth");
260 gchar *max_height = (gchar *) xmlGetProp (node, (xmlChar *) "MaxHeight");
262 (gchar *) xmlGetProp (node, (xmlChar *) "CodecPrivateData");
264 caps = _gst_mss_manifest_stream_video_caps_from_fourcc (fourcc);
268 structure = gst_caps_get_structure (caps, 0);
271 gst_structure_set (structure, "width", G_TYPE_INT, atoi (max_width), NULL);
273 gst_structure_set (structure, "height", G_TYPE_INT, atoi (max_height),
277 GValue *value = g_new0 (GValue, 1);
278 g_value_init (value, GST_TYPE_BUFFER);
279 gst_value_deserialize (value, (gchar *) codec_data);
280 gst_structure_take_value (structure, "codec_data", value);
293 _gst_mss_manifest_stream_audio_caps_from_qualitylevel_xml (xmlNodePtr node)
296 GstStructure *structure;
297 gchar *fourcc = (gchar *) xmlGetProp (node, (xmlChar *) "FourCC");
298 gchar *channels = (gchar *) xmlGetProp (node, (xmlChar *) "Channels");
299 gchar *rate = (gchar *) xmlGetProp (node, (xmlChar *) "SamplingRate");
301 (gchar *) xmlGetProp (node, (xmlChar *) "CodecPrivateData");
303 caps = _gst_mss_manifest_stream_audio_caps_from_fourcc (fourcc);
307 structure = gst_caps_get_structure (caps, 0);
310 gst_structure_set (structure, "channels", G_TYPE_INT, atoi (channels),
313 gst_structure_set (structure, "rate", G_TYPE_INT, atoi (rate), NULL);
316 GValue *value = g_new0 (GValue, 1);
317 g_value_init (value, GST_TYPE_BUFFER);
318 gst_value_deserialize (value, (gchar *) codec_data);
319 gst_structure_take_value (structure, "codec_data", value);
332 gst_mss_manifest_stream_get_caps (GstMssManifestStream * stream)
334 GstMssManifestStreamType streamtype =
335 gst_mss_manifest_stream_get_type (stream);
336 xmlNodePtr qualitylevel = stream->current_quality->data;
338 if (streamtype == MSS_STREAM_TYPE_VIDEO)
340 _gst_mss_manifest_stream_video_caps_from_qualitylevel_xml
342 else if (streamtype == MSS_STREAM_TYPE_AUDIO)
344 _gst_mss_manifest_stream_audio_caps_from_qualitylevel_xml
351 gst_mss_manifest_stream_get_fragment_url (GstMssManifestStream * stream,
356 gchar *start_time_str;
357 GstMssManifestStreamFragment *fragment = stream->current_fragment->data;
359 if (stream->current_fragment == NULL) /* stream is over */
360 return GST_FLOW_UNEXPECTED;
363 (gchar *) xmlGetProp (stream->current_quality->data,
364 (xmlChar *) MSS_PROP_BITRATE);
365 start_time_str = g_strdup_printf ("%" G_GUINT64_FORMAT, fragment->time);
367 tmp = g_regex_replace_literal (stream->regex_bitrate, stream->url,
368 strlen (stream->url), 0, bitrate_str, 0, NULL);
369 *url = g_regex_replace_literal (stream->regex_position, tmp,
370 strlen (tmp), 0, start_time_str, 0, NULL);
373 g_free (start_time_str);
374 g_free (bitrate_str);
379 gst_mss_manifest_stream_advance_fragment (GstMssManifestStream * stream)
381 if (stream->current_fragment == NULL)
382 return GST_FLOW_UNEXPECTED;
384 stream->current_fragment = g_list_next (stream->current_fragment);
385 if (stream->current_fragment == NULL);
386 return GST_FLOW_UNEXPECTED;
391 gst_mss_manifest_stream_type_name (GstMssManifestStreamType streamtype)
393 switch (streamtype) {
394 case MSS_STREAM_TYPE_VIDEO:
396 case MSS_STREAM_TYPE_AUDIO:
398 case MSS_STREAM_TYPE_UNKNOWN: