Debian Codegraph @

ext/adaptivedemux2/dash/gstmpdparser.c

raw
1 /*
2 * DASH MPD parsing library
3 *
4 * gstmpdparser.c
5 *
6 * Copyright (C) 2012 STMicroelectronics
7 *
8 * Authors:
9 * Gianluca Gennari <[email protected]>
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
20 *
21 * You should have received a copy of the GNU Library General Public
22 * License along with this library (COPYING); if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
25 */
26
27 #include <string.h>
28
29 #include <gst/pbutils/pbutils.h>
30 #include "gstmpdparser.h"
31 #include "gstdash_debug.h"
32
33 #define GST_CAT_DEFAULT gst_dash_demux2_debug
34
35
36 /* XML node parsing */
37 static void gst_mpdparser_parse_baseURL_node (GList ** list, xmlNode * a_node);
38 static void gst_mpdparser_parse_descriptor_type (GList ** list,
39 xmlNode * a_node);
40 static void gst_mpdparser_parse_content_component_node (GList ** list,
41 xmlNode * a_node);
42 static void gst_mpdparser_parse_location_node (GList ** list, xmlNode * a_node);
43 static void gst_mpdparser_parse_subrepresentation_node (GList ** list,
44 xmlNode * a_node);
45 static void gst_mpdparser_parse_segment_url_node (GList ** list,
46 xmlNode * a_node);
47 static void gst_mpdparser_parse_url_type_node (GstMPDURLTypeNode ** pointer,
48 xmlNode * a_node);
49 static void gst_mpdparser_parse_seg_base_type_ext (GstMPDSegmentBaseNode **
50 pointer, xmlNode * a_node, GstMPDSegmentBaseNode * parent);
51 static void gst_mpdparser_parse_s_node (GQueue * queue, xmlNode * a_node);
52 static void gst_mpdparser_parse_segment_timeline_node (GstMPDSegmentTimelineNode
53 ** pointer, xmlNode * a_node);
54 static gboolean
55 gst_mpdparser_parse_mult_seg_base_node (GstMPDMultSegmentBaseNode *
56 pointer, xmlNode * a_node, GstMPDMultSegmentBaseNode * parent);
57 static gboolean gst_mpdparser_parse_segment_list_node (GstMPDSegmentListNode **
58 pointer, xmlNode * a_node, GstMPDSegmentListNode * parent);
59 static void
60 gst_mpdparser_parse_representation_base (GstMPDRepresentationBaseNode *
61 pointer, xmlNode * a_node);
62 static gboolean gst_mpdparser_parse_representation_node (GList ** list,
63 xmlNode * a_node, GstMPDAdaptationSetNode * parent,
64 GstMPDPeriodNode * period_node);
65 static gboolean gst_mpdparser_parse_adaptation_set_node (GList ** list,
66 xmlNode * a_node, GstMPDPeriodNode * parent);
67 static void gst_mpdparser_parse_subset_node (GList ** list, xmlNode * a_node);
68 static gboolean
69 gst_mpdparser_parse_segment_template_node (GstMPDSegmentTemplateNode ** pointer,
70 xmlNode * a_node, GstMPDSegmentTemplateNode * parent);
71 static gboolean gst_mpdparser_parse_period_node (GList ** list,
72 xmlNode * a_node);
73 static void gst_mpdparser_parse_program_info_node (GList ** list,
74 xmlNode * a_node);
75 static void gst_mpdparser_parse_metrics_range_node (GList ** list,
76 xmlNode * a_node);
77 static void gst_mpdparser_parse_metrics_node (GList ** list, xmlNode * a_node);
78 static gboolean gst_mpdparser_parse_root_node (GstMPDRootNode ** pointer,
79 xmlNode * a_node);
80 static void gst_mpdparser_parse_utctiming_node (GList ** list,
81 xmlNode * a_node);
82
83 /*
84 Duration Data Type
85
86 The duration data type is used to specify a time interval.
87
88 The time interval is specified in the following form "-PnYnMnDTnHnMnS" where:
89
90 * - indicates the negative sign (optional)
91 * P indicates the period (required)
92 * nY indicates the number of years
93 * nM indicates the number of months
94 * nD indicates the number of days
95 * T indicates the start of a time section (required if you are going to specify hours, minutes, or seconds)
96 * nH indicates the number of hours
97 * nM indicates the number of minutes
98 * nS indicates the number of seconds
99 */
100
101
102
103
104 static void
105 gst_mpdparser_parse_baseURL_node (GList ** list, xmlNode * a_node)
106 {
107 GstMPDBaseURLNode *new_base_url;
108
109 new_base_url = gst_mpd_baseurl_node_new ();
110 *list = g_list_append (*list, new_base_url);
111
112 GST_LOG ("content of BaseURL node:");
113 gst_xml_helper_get_node_content (a_node, &new_base_url->baseURL);
114
115 GST_LOG ("attributes of BaseURL node:");
116 gst_xml_helper_get_prop_string (a_node, "serviceLocation",
117 &new_base_url->serviceLocation);
118 gst_xml_helper_get_prop_string (a_node, "byteRange",
119 &new_base_url->byteRange);
120 }
121
122 static void
123 gst_mpdparser_parse_descriptor_type (GList ** list, xmlNode * a_node)
124 {
125 GstMPDDescriptorTypeNode *new_descriptor;
126
127 new_descriptor =
128 gst_mpd_descriptor_type_node_new ((const gchar *) a_node->name);
129 *list = g_list_append (*list, new_descriptor);
130
131 GST_LOG ("attributes of %s node:", a_node->name);
132 gst_xml_helper_get_prop_string_stripped (a_node, "schemeIdUri",
133 &new_descriptor->schemeIdUri);
134 if (!gst_xml_helper_get_prop_string (a_node, "value", &new_descriptor->value)) {
135 /* if no value attribute, use XML string representation of the node */
136 gst_xml_helper_get_node_as_string (a_node, &new_descriptor->value);
137 }
138 }
139
140 static void
141 gst_mpdparser_parse_content_component_node (GList ** list, xmlNode * a_node)
142 {
143 xmlNode *cur_node;
144 GstMPDContentComponentNode *new_content_component;
145
146 new_content_component = gst_mpd_content_component_node_new ();
147 *list = g_list_append (*list, new_content_component);
148
149 GST_LOG ("attributes of ContentComponent node:");
150 gst_xml_helper_get_prop_unsigned_integer (a_node, "id", 0,
151 &new_content_component->id);
152 gst_xml_helper_get_prop_string (a_node, "lang", &new_content_component->lang);
153 gst_xml_helper_get_prop_string (a_node, "contentType",
154 &new_content_component->contentType);
155 gst_xml_helper_get_prop_ratio (a_node, "par", &new_content_component->par);
156
157 /* explore children nodes */
158 for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {
159 if (cur_node->type == XML_ELEMENT_NODE) {
160 if (xmlStrcmp (cur_node->name, (xmlChar *) "Accessibility") == 0) {
161 gst_mpdparser_parse_descriptor_type
162 (&new_content_component->Accessibility, cur_node);
163 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "Role") == 0) {
164 gst_mpdparser_parse_descriptor_type (&new_content_component->Role,
165 cur_node);
166 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "Rating") == 0) {
167 gst_mpdparser_parse_descriptor_type
168 (&new_content_component->Rating, cur_node);
169 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "Viewpoint") == 0) {
170 gst_mpdparser_parse_descriptor_type
171 (&new_content_component->Viewpoint, cur_node);
172 }
173 }
174 }
175 }
176
177 static void
178 gst_mpdparser_parse_location_node (GList ** list, xmlNode * a_node)
179 {
180 gchar *location = NULL;
181 GstMPDLocationNode *locationNode;
182
183 GST_LOG ("content of Location node:");
184 if (gst_xml_helper_get_node_content (a_node, &location)) {
185 locationNode = gst_mpd_location_node_new ();
186 locationNode->location = location;
187 *list = g_list_append (*list, locationNode);
188 }
189 }
190
191 static void
192 gst_mpdparser_parse_subrepresentation_node (GList ** list, xmlNode * a_node)
193 {
194 GstMPDSubRepresentationNode *new_subrep;
195
196 new_subrep = gst_mpd_sub_representation_node_new ();
197 *list = g_list_append (*list, new_subrep);
198
199 GST_LOG ("attributes of SubRepresentation node:");
200 gst_xml_helper_get_prop_unsigned_integer (a_node, "level", 0,
201 &new_subrep->level);
202 gst_xml_helper_get_prop_uint_vector_type (a_node, "dependencyLevel",
203 &new_subrep->dependencyLevel, &new_subrep->dependencyLevel_size);
204 gst_xml_helper_get_prop_unsigned_integer (a_node, "bandwidth", 0,
205 &new_subrep->bandwidth);
206 gst_xml_helper_get_prop_string_vector_type (a_node,
207 "contentComponent", &new_subrep->contentComponent);
208
209 /* RepresentationBase extension */
210 gst_mpdparser_parse_representation_base (GST_MPD_REPRESENTATION_BASE_NODE
211 (new_subrep), a_node);
212 }
213
214
215
216 static void
217 gst_mpdparser_parse_segment_url_node (GList ** list, xmlNode * a_node)
218 {
219 GstMPDSegmentURLNode *new_segment_url;
220
221 new_segment_url = gst_mpd_segment_url_node_new ();
222 *list = g_list_append (*list, new_segment_url);
223
224 GST_LOG ("attributes of SegmentURL node:");
225 gst_xml_helper_get_prop_string (a_node, "media", &new_segment_url->media);
226 gst_xml_helper_get_prop_range (a_node, "mediaRange",
227 &new_segment_url->mediaRange);
228 gst_xml_helper_get_prop_string (a_node, "index", &new_segment_url->index);
229 gst_xml_helper_get_prop_range (a_node, "indexRange",
230 &new_segment_url->indexRange);
231 }
232
233 static void
234 gst_mpdparser_parse_url_type_node (GstMPDURLTypeNode ** pointer,
235 xmlNode * a_node)
236 {
237 GstMPDURLTypeNode *new_url_type;
238
239 gst_mpd_url_type_node_free (*pointer);
240 *pointer = new_url_type =
241 gst_mpd_url_type_node_new ((const gchar *) a_node->name);
242
243 GST_LOG ("attributes of URLType node:");
244 gst_xml_helper_get_prop_string (a_node, "sourceURL",
245 &new_url_type->sourceURL);
246 gst_xml_helper_get_prop_range (a_node, "range", &new_url_type->range);
247 }
248
249 static void
250 gst_mpdparser_parse_seg_base_type_ext (GstMPDSegmentBaseNode ** pointer,
251 xmlNode * a_node, GstMPDSegmentBaseNode * parent)
252 {
253 xmlNode *cur_node;
254 GstMPDSegmentBaseNode *seg_base_type;
255 guint intval;
256 guint64 int64val;
257 gboolean boolval;
258 GstXMLRange *rangeval;
259
260 gst_mpd_segment_base_node_free (*pointer);
261 *pointer = seg_base_type = gst_mpd_segment_base_node_new ();
262
263 /* Initialize values that have defaults */
264 seg_base_type->indexRangeExact = FALSE;
265 seg_base_type->timescale = 1;
266
267 /* Inherit attribute values from parent */
268 if (parent) {
269 seg_base_type->timescale = parent->timescale;
270 seg_base_type->presentationTimeOffset = parent->presentationTimeOffset;
271 seg_base_type->indexRange = gst_xml_helper_clone_range (parent->indexRange);
272 seg_base_type->indexRangeExact = parent->indexRangeExact;
273 seg_base_type->Initialization =
274 gst_mpd_url_type_node_clone (parent->Initialization);
275 seg_base_type->RepresentationIndex =
276 gst_mpd_url_type_node_clone (parent->RepresentationIndex);
277 }
278
279 /* We must retrieve each value first to see if it exists. If it does not
280 * exist, we do not want to overwrite an inherited value */
281 GST_LOG ("attributes of SegmentBaseType extension:");
282 if (gst_xml_helper_get_prop_unsigned_integer (a_node, "timescale", 1,
283 &intval)) {
284 seg_base_type->timescale = intval;
285 }
286 if (gst_xml_helper_get_prop_unsigned_integer_64 (a_node,
287 "presentationTimeOffset", 0, &int64val)) {
288 seg_base_type->presentationTimeOffset = int64val;
289 }
290 if (gst_xml_helper_get_prop_range (a_node, "indexRange", &rangeval)) {
291 if (seg_base_type->indexRange) {
292 g_free (seg_base_type->indexRange);
293 }
294 seg_base_type->indexRange = rangeval;
295 }
296 if (gst_xml_helper_get_prop_boolean (a_node, "indexRangeExact",
297 FALSE, &boolval)) {
298 seg_base_type->indexRangeExact = boolval;
299 }
300
301 /* explore children nodes */
302 for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {
303 if (cur_node->type == XML_ELEMENT_NODE) {
304 if (xmlStrcmp (cur_node->name, (xmlChar *) "Initialization") == 0 ||
305 xmlStrcmp (cur_node->name, (xmlChar *) "Initialisation") == 0) {
306 /* parse will free the previous pointer to create a new one */
307 gst_mpdparser_parse_url_type_node (&seg_base_type->Initialization,
308 cur_node);
309 } else if (xmlStrcmp (cur_node->name,
310 (xmlChar *) "RepresentationIndex") == 0) {
311 /* parse will free the previous pointer to create a new one */
312 gst_mpdparser_parse_url_type_node (&seg_base_type->RepresentationIndex,
313 cur_node);
314 }
315 }
316 }
317 }
318
319
320
321 static void
322 gst_mpdparser_parse_s_node (GQueue * queue, xmlNode * a_node)
323 {
324 GstMPDSNode *new_s_node;
325
326 new_s_node = gst_mpd_s_node_new ();
327 g_queue_push_tail (queue, new_s_node);
328
329 GST_LOG ("attributes of S node:");
330 gst_xml_helper_get_prop_unsigned_integer_64 (a_node, "t", 0, &new_s_node->t);
331 gst_xml_helper_get_prop_unsigned_integer_64 (a_node, "d", 0, &new_s_node->d);
332 gst_xml_helper_get_prop_signed_integer (a_node, "r", 0, &new_s_node->r);
333 }
334
335
336
337 static void
338 gst_mpdparser_parse_segment_timeline_node (GstMPDSegmentTimelineNode ** pointer,
339 xmlNode * a_node)
340 {
341 xmlNode *cur_node;
342 GstMPDSegmentTimelineNode *new_seg_timeline;
343
344 gst_mpd_segment_timeline_node_free (*pointer);
345 *pointer = new_seg_timeline = gst_mpd_segment_timeline_node_new ();
346 if (new_seg_timeline == NULL) {
347 GST_WARNING ("Allocation of SegmentTimeline node failed!");
348 return;
349 }
350
351 /* explore children nodes */
352 for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {
353 if (cur_node->type == XML_ELEMENT_NODE) {
354 if (xmlStrcmp (cur_node->name, (xmlChar *) "S") == 0) {
355 gst_mpdparser_parse_s_node (&new_seg_timeline->S, cur_node);
356 }
357 }
358 }
359 }
360
361 static gboolean
362 gst_mpdparser_parse_mult_seg_base_node (GstMPDMultSegmentBaseNode *
363 mult_seg_base_node, xmlNode * a_node, GstMPDMultSegmentBaseNode * parent)
364 {
365 xmlNode *cur_node;
366
367 guint intval;
368 gboolean has_timeline = FALSE, has_duration = FALSE;
369
370 mult_seg_base_node->duration = 0;
371 mult_seg_base_node->startNumber = 1;
372
373 /* Inherit attribute values from parent */
374 if (parent) {
375 mult_seg_base_node->duration = parent->duration;
376 mult_seg_base_node->startNumber = parent->startNumber;
377 mult_seg_base_node->SegmentTimeline =
378 gst_mpd_segment_timeline_node_clone (parent->SegmentTimeline);
379 mult_seg_base_node->BitstreamSwitching =
380 gst_mpd_url_type_node_clone (parent->BitstreamSwitching);
381 }
382 GST_LOG ("attributes of MultipleSegmentBaseType extension:");
383 if (gst_xml_helper_get_prop_unsigned_integer (a_node, "duration", 0, &intval)) {
384 mult_seg_base_node->duration = intval;
385 }
386
387 /* duration might be specified from parent */
388 if (mult_seg_base_node->duration)
389 has_duration = TRUE;
390
391 if (gst_xml_helper_get_prop_unsigned_integer (a_node, "startNumber", 1,
392 &intval)) {
393 mult_seg_base_node->startNumber = intval;
394 }
395
396 GST_LOG ("extension of MultipleSegmentBaseType extension:");
397 gst_mpdparser_parse_seg_base_type_ext (&mult_seg_base_node->SegmentBase,
398 a_node, (parent ? parent->SegmentBase : NULL));
399
400 /* explore children nodes */
401 for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {
402 if (cur_node->type == XML_ELEMENT_NODE) {
403 if (xmlStrcmp (cur_node->name, (xmlChar *) "SegmentTimeline") == 0) {
404 /* parse frees the segmenttimeline if any */
405 gst_mpdparser_parse_segment_timeline_node
406 (&mult_seg_base_node->SegmentTimeline, cur_node);
407 } else if (xmlStrcmp (cur_node->name,
408 (xmlChar *) "BitstreamSwitching") == 0) {
409 /* parse frees the old url before setting the new one */
410 gst_mpdparser_parse_url_type_node
411 (&mult_seg_base_node->BitstreamSwitching, cur_node);
412 }
413 }
414 }
415
416 has_timeline = mult_seg_base_node->SegmentTimeline != NULL;
417
418 /* Checking duration and timeline only at Representation's child level */
419 if (xmlStrcmp (a_node->parent->name, (xmlChar *) "Representation") == 0
420 && !has_duration && !has_timeline) {
421 GST_ERROR ("segment has neither duration nor timeline");
422 }
423
424 return TRUE;
425 }
426
427 static gboolean
428 gst_mpdparser_parse_segment_list_node (GstMPDSegmentListNode ** pointer,
429 xmlNode * a_node, GstMPDSegmentListNode * parent)
430 {
431 xmlNode *cur_node;
432 GstMPDSegmentListNode *new_segment_list;
433 gchar *actuate;
434 gboolean segment_urls_inherited_from_parent = FALSE;
435
436 gst_mpd_segment_list_node_free (*pointer);
437 new_segment_list = gst_mpd_segment_list_node_new ();
438
439 /* Inherit attribute values from parent */
440 if (parent) {
441 GList *list;
442 GstMPDSegmentURLNode *seg_url;
443 for (list = g_list_first (parent->SegmentURL); list;
444 list = g_list_next (list)) {
445 seg_url = (GstMPDSegmentURLNode *) list->data;
446 new_segment_list->SegmentURL =
447 g_list_append (new_segment_list->SegmentURL,
448 gst_mpd_segment_url_node_clone (seg_url));
449 segment_urls_inherited_from_parent = TRUE;
450 }
451 }
452
453 new_segment_list->actuate = GST_MPD_XLINK_ACTUATE_ON_REQUEST;
454 if (gst_xml_helper_get_ns_prop_string (a_node,
455 "http://www.w3.org/1999/xlink", "href", &new_segment_list->xlink_href)
456 && gst_xml_helper_get_ns_prop_string (a_node,
457 "http://www.w3.org/1999/xlink", "actuate", &actuate)) {
458 if (strcmp (actuate, GST_MPD_XLINK_ACTUATE_ON_LOAD_STR) == 0)
459 new_segment_list->actuate = GST_MPD_XLINK_ACTUATE_ON_LOAD;
460 xmlFree (actuate);
461 }
462
463 GST_LOG ("extension of SegmentList node:");
464 if (!gst_mpdparser_parse_mult_seg_base_node
465 (GST_MPD_MULT_SEGMENT_BASE_NODE (new_segment_list), a_node,
466 (parent ? GST_MPD_MULT_SEGMENT_BASE_NODE (parent) : NULL)))
467 goto error;
468
469 /* explore children nodes */
470 for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {
471 if (cur_node->type == XML_ELEMENT_NODE) {
472 if (xmlStrcmp (cur_node->name, (xmlChar *) "SegmentURL") == 0) {
473 if (segment_urls_inherited_from_parent) {
474 /*
475 * SegmentBase, SegmentTemplate and SegmentList shall inherit
476 * attributes and elements from the same element on a higher level.
477 * If the same attribute or element is present on both levels,
478 * the one on the lower level shall take precedence over the one
479 * on the higher level.
480 */
481
482 /* Clear the list of inherited segment URLs */
483 g_list_free_full (new_segment_list->SegmentURL,
484 (GDestroyNotify) gst_mpd_segment_url_node_free);
485 new_segment_list->SegmentURL = NULL;
486
487 /* mark the fact that we cleared the list, so that it is not tried again */
488 segment_urls_inherited_from_parent = FALSE;
489 }
490 gst_mpdparser_parse_segment_url_node (&new_segment_list->SegmentURL,
491 cur_node);
492 }
493 }
494 }
495
496 *pointer = new_segment_list;
497 return TRUE;
498
499 error:
500 gst_mpd_segment_list_node_free (new_segment_list);
501 return FALSE;
502 }
503
504 static void
505 gst_mpdparser_parse_content_protection_node (GList ** list, xmlNode * a_node)
506 {
507 gchar *value = NULL;
508 if (gst_xml_helper_get_prop_string (a_node, "value", &value)) {
509 if (!g_strcmp0 (value, "MSPR 2.0")) {
510 xmlNode *cur_node;
511 for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {
512 if (cur_node->type == XML_ELEMENT_NODE) {
513 if (xmlStrcmp (cur_node->name, (xmlChar *) "pro") == 0) {
514 GstMPDDescriptorTypeNode *new_descriptor;
515 new_descriptor = gst_mpd_descriptor_type_node_new ((const gchar *)
516 cur_node->name);
517 *list = g_list_append (*list, new_descriptor);
518
519 gst_xml_helper_get_prop_string_stripped (a_node, "schemeIdUri",
520 &new_descriptor->schemeIdUri);
521
522 gst_xml_helper_get_node_content (cur_node, &new_descriptor->value);
523 goto beach;
524 }
525 }
526 }
527 } else {
528 gst_mpdparser_parse_descriptor_type (list, a_node);
529 }
530 } else {
531 gst_mpdparser_parse_descriptor_type (list, a_node);
532 }
533 beach:
534 if (value)
535 g_free (value);
536 }
537
538 static void
539 gst_mpdparser_parse_representation_base (GstMPDRepresentationBaseNode *
540 representation_base, xmlNode * a_node)
541 {
542 xmlNode *cur_node;
543
544 GST_LOG ("attributes of RepresentationBaseType extension:");
545 gst_xml_helper_get_prop_string (a_node, "profiles",
546 &representation_base->profiles);
547 gst_xml_helper_get_prop_unsigned_integer (a_node, "width", 0,
548 &representation_base->width);
549 gst_xml_helper_get_prop_unsigned_integer (a_node, "height", 0,
550 &representation_base->height);
551 gst_xml_helper_get_prop_ratio (a_node, "sar", &representation_base->sar);
552 gst_xml_helper_get_prop_framerate (a_node, "frameRate",
553 &representation_base->frameRate);
554 gst_xml_helper_get_prop_framerate (a_node, "minFrameRate",
555 &representation_base->minFrameRate);
556 gst_xml_helper_get_prop_framerate (a_node, "maxFrameRate",
557 &representation_base->maxFrameRate);
558 gst_xml_helper_get_prop_string (a_node, "audioSamplingRate",
559 &representation_base->audioSamplingRate);
560 gst_xml_helper_get_prop_string (a_node, "mimeType",
561 &representation_base->mimeType);
562 gst_xml_helper_get_prop_string (a_node, "segmentProfiles",
563 &representation_base->segmentProfiles);
564 gst_xml_helper_get_prop_string (a_node, "codecs",
565 &representation_base->codecs);
566 if (representation_base->codecs) {
567 GST_DEBUG ("Getting caps ");
568 representation_base->caps =
569 gst_codec_utils_caps_from_mime_codec (representation_base->codecs);
570 } else {
571 representation_base->caps =
572 gst_mpd_helper_mimetype_to_codec_caps (representation_base->mimeType);
573 GST_DEBUG ("Getting caps from mime type gave %" GST_PTR_FORMAT,
574 representation_base->caps);
575 }
576 gst_xml_helper_get_prop_double (a_node, "maximumSAPPeriod",
577 &representation_base->maximumSAPPeriod);
578 gst_mpd_helper_get_SAP_type (a_node, "startWithSAP",
579 &representation_base->startWithSAP);
580 gst_xml_helper_get_prop_double (a_node, "maxPlayoutRate",
581 &representation_base->maxPlayoutRate);
582 gst_xml_helper_get_prop_boolean (a_node, "codingDependency",
583 FALSE, &representation_base->codingDependency);
584 gst_xml_helper_get_prop_string (a_node, "scanType",
585 &representation_base->scanType);
586
587 /* explore children nodes */
588 for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {
589 if (cur_node->type == XML_ELEMENT_NODE) {
590 if (xmlStrcmp (cur_node->name, (xmlChar *) "FramePacking") == 0) {
591 gst_mpdparser_parse_descriptor_type
592 (&representation_base->FramePacking, cur_node);
593 } else if (xmlStrcmp (cur_node->name,
594 (xmlChar *) "AudioChannelConfiguration") == 0) {
595 gst_mpdparser_parse_descriptor_type
596 (&representation_base->AudioChannelConfiguration, cur_node);
597 } else if (xmlStrcmp (cur_node->name,
598 (xmlChar *) "ContentProtection") == 0) {
599 gst_mpdparser_parse_content_protection_node
600 (&representation_base->ContentProtection, cur_node);
601 }
602 }
603 }
604 }
605
606 static gboolean
607 gst_mpdparser_parse_representation_node (GList ** list, xmlNode * a_node,
608 GstMPDAdaptationSetNode * parent, GstMPDPeriodNode * period_node)
609 {
610 xmlNode *cur_node;
611 GstMPDRepresentationNode *new_representation;
612
613 new_representation = gst_mpd_representation_node_new ();
614
615 GST_LOG ("attributes of Representation node:");
616 if (!gst_xml_helper_get_prop_string (a_node, "id", &new_representation->id)) {
617 GST_ERROR ("Cannot parse Representation id, invalid manifest");
618 goto error;
619 }
620 if (!gst_xml_helper_get_prop_unsigned_integer (a_node, "bandwidth", 0,
621 &new_representation->bandwidth)) {
622 GST_ERROR ("Cannot parse Representation bandwidth, invalid manifest");
623 goto error;
624 }
625 gst_xml_helper_get_prop_unsigned_integer (a_node, "qualityRanking", 0,
626 &new_representation->qualityRanking);
627 gst_xml_helper_get_prop_string_vector_type (a_node, "dependencyId",
628 &new_representation->dependencyId);
629 gst_xml_helper_get_prop_string_vector_type (a_node,
630 "mediaStreamStructureId", &new_representation->mediaStreamStructureId);
631 /* RepresentationBase extension */
632 gst_mpdparser_parse_representation_base
633 (GST_MPD_REPRESENTATION_BASE_NODE (new_representation), a_node);
634
635 /* explore children nodes */
636 for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {
637 if (cur_node->type == XML_ELEMENT_NODE) {
638 if (xmlStrcmp (cur_node->name, (xmlChar *) "SegmentBase") == 0) {
639 gst_mpdparser_parse_seg_base_type_ext (&new_representation->SegmentBase,
640 cur_node, parent->SegmentBase ?
641 parent->SegmentBase : period_node->SegmentBase);
642 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "SegmentTemplate") == 0) {
643 if (!gst_mpdparser_parse_segment_template_node
644 (&new_representation->SegmentTemplate, cur_node,
645 parent->SegmentTemplate ?
646 parent->SegmentTemplate : period_node->SegmentTemplate))
647 goto error;
648 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "SegmentList") == 0) {
649 if (!gst_mpdparser_parse_segment_list_node
650 (&new_representation->SegmentList, cur_node,
651 parent->SegmentList ? parent->SegmentList : period_node->
652 SegmentList))
653 goto error;
654 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "BaseURL") == 0) {
655 gst_mpdparser_parse_baseURL_node (&new_representation->BaseURLs,
656 cur_node);
657 } else if (xmlStrcmp (cur_node->name,
658 (xmlChar *) "SubRepresentation") == 0) {
659 gst_mpdparser_parse_subrepresentation_node
660 (&new_representation->SubRepresentations, cur_node);
661 }
662 }
663 }
664
665 /* some sanity checking */
666
667 *list = g_list_append (*list, new_representation);
668 return TRUE;
669
670 error:
671 gst_mpd_representation_node_free (new_representation);
672 return FALSE;
673 }
674
675 static gboolean
676 gst_mpdparser_parse_adaptation_set_node (GList ** list, xmlNode * a_node,
677 GstMPDPeriodNode * parent)
678 {
679 xmlNode *cur_node;
680 GstMPDAdaptationSetNode *new_adap_set;
681 gchar *actuate;
682
683 new_adap_set = gst_mpd_adaptation_set_node_new ();
684
685 GST_LOG ("attributes of AdaptationSet node:");
686
687 new_adap_set->actuate = GST_MPD_XLINK_ACTUATE_ON_REQUEST;
688 if (gst_xml_helper_get_ns_prop_string (a_node,
689 "http://www.w3.org/1999/xlink", "href", &new_adap_set->xlink_href)
690 && gst_xml_helper_get_ns_prop_string (a_node,
691 "http://www.w3.org/1999/xlink", "actuate", &actuate)) {
692 if (strcmp (actuate, "onLoad") == 0)
693 new_adap_set->actuate = GST_MPD_XLINK_ACTUATE_ON_LOAD;
694 xmlFree (actuate);
695 }
696
697 gst_xml_helper_get_prop_unsigned_integer (a_node, "id", 0, &new_adap_set->id);
698 gst_xml_helper_get_prop_unsigned_integer (a_node, "group", 0,
699 &new_adap_set->group);
700 gst_xml_helper_get_prop_string (a_node, "lang", &new_adap_set->lang);
701 gst_xml_helper_get_prop_string (a_node, "contentType",
702 &new_adap_set->contentType);
703 gst_xml_helper_get_prop_ratio (a_node, "par", &new_adap_set->par);
704 gst_xml_helper_get_prop_unsigned_integer (a_node, "minBandwidth", 0,
705 &new_adap_set->minBandwidth);
706 gst_xml_helper_get_prop_unsigned_integer (a_node, "maxBandwidth", 0,
707 &new_adap_set->maxBandwidth);
708 gst_xml_helper_get_prop_unsigned_integer (a_node, "minWidth", 0,
709 &new_adap_set->minWidth);
710 gst_xml_helper_get_prop_unsigned_integer (a_node, "maxWidth", 0,
711 &new_adap_set->maxWidth);
712 gst_xml_helper_get_prop_unsigned_integer (a_node, "minHeight", 0,
713 &new_adap_set->minHeight);
714 gst_xml_helper_get_prop_unsigned_integer (a_node, "maxHeight", 0,
715 &new_adap_set->maxHeight);
716 gst_xml_helper_get_prop_cond_uint (a_node, "segmentAlignment",
717 &new_adap_set->segmentAlignment);
718 gst_xml_helper_get_prop_boolean (a_node, "bitstreamSwitching",
719 parent->bitstreamSwitching, &new_adap_set->bitstreamSwitching);
720 if (parent->bitstreamSwitching && !new_adap_set->bitstreamSwitching) {
721 /* according to the standard, if the Period's bitstreamSwitching attribute
722 * is true, the AdaptationSet should not have the bitstreamSwitching
723 * attribute set to false.
724 * We should return a parsing error, but we are generous and ignore the
725 * standard violation.
726 */
727 new_adap_set->bitstreamSwitching = parent->bitstreamSwitching;
728 }
729 gst_xml_helper_get_prop_cond_uint (a_node, "subsegmentAlignment",
730 &new_adap_set->subsegmentAlignment);
731 gst_mpd_helper_get_SAP_type (a_node, "subsegmentStartsWithSAP",
732 &new_adap_set->subsegmentStartsWithSAP);
733
734 /* RepresentationBase extension */
735 gst_mpdparser_parse_representation_base
736 (GST_MPD_REPRESENTATION_BASE_NODE (new_adap_set), a_node);
737
738 /* explore children nodes */
739 for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {
740 if (cur_node->type == XML_ELEMENT_NODE) {
741 if (xmlStrcmp (cur_node->name, (xmlChar *) "Accessibility") == 0) {
742 gst_mpdparser_parse_descriptor_type (&new_adap_set->Accessibility,
743 cur_node);
744 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "Role") == 0) {
745 gst_mpdparser_parse_descriptor_type (&new_adap_set->Role, cur_node);
746 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "Rating") == 0) {
747 gst_mpdparser_parse_descriptor_type (&new_adap_set->Rating, cur_node);
748 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "Viewpoint") == 0) {
749 gst_mpdparser_parse_descriptor_type (&new_adap_set->Viewpoint,
750 cur_node);
751 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "BaseURL") == 0) {
752 gst_mpdparser_parse_baseURL_node (&new_adap_set->BaseURLs, cur_node);
753 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "SegmentBase") == 0) {
754 gst_mpdparser_parse_seg_base_type_ext (&new_adap_set->SegmentBase,
755 cur_node, parent->SegmentBase);
756 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "SegmentList") == 0) {
757 if (!gst_mpdparser_parse_segment_list_node (&new_adap_set->SegmentList,
758 cur_node, parent->SegmentList))
759 goto error;
760 } else if (xmlStrcmp (cur_node->name,
761 (xmlChar *) "ContentComponent") == 0) {
762 gst_mpdparser_parse_content_component_node
763 (&new_adap_set->ContentComponents, cur_node);
764 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "SegmentTemplate") == 0) {
765 if (!gst_mpdparser_parse_segment_template_node
766 (&new_adap_set->SegmentTemplate, cur_node, parent->SegmentTemplate))
767 goto error;
768 }
769 }
770 }
771
772 /* We must parse Representation after everything else in the AdaptationSet
773 * has been parsed because certain Representation child elements can inherit
774 * attributes specified by the same element in the AdaptationSet
775 */
776 for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {
777 if (cur_node->type == XML_ELEMENT_NODE) {
778 if (xmlStrcmp (cur_node->name, (xmlChar *) "Representation") == 0) {
779 if (!gst_mpdparser_parse_representation_node
780 (&new_adap_set->Representations, cur_node, new_adap_set, parent))
781 goto error;
782 }
783 }
784 }
785
786 *list = g_list_append (*list, new_adap_set);
787 return TRUE;
788
789 error:
790 gst_mpd_adaptation_set_node_free (new_adap_set);
791 return FALSE;
792 }
793
794 static void
795 gst_mpdparser_parse_subset_node (GList ** list, xmlNode * a_node)
796 {
797 GstMPDSubsetNode *new_subset;
798
799 new_subset = gst_mpd_subset_node_new ();
800 *list = g_list_append (*list, new_subset);
801
802 GST_LOG ("attributes of Subset node:");
803 gst_xml_helper_get_prop_uint_vector_type (a_node, "contains",
804 &new_subset->contains, &new_subset->contains_size);
805 }
806
807 static gboolean
808 gst_mpdparser_parse_segment_template_node (GstMPDSegmentTemplateNode ** pointer,
809 xmlNode * a_node, GstMPDSegmentTemplateNode * parent)
810 {
811 GstMPDSegmentTemplateNode *new_segment_template;
812 gchar *strval;
813
814 gst_mpd_segment_template_node_free (*pointer);
815 new_segment_template = gst_mpd_segment_template_node_new ();
816
817 GST_LOG ("extension of SegmentTemplate node:");
818 if (!gst_mpdparser_parse_mult_seg_base_node
819 (GST_MPD_MULT_SEGMENT_BASE_NODE (new_segment_template), a_node,
820 (parent ? GST_MPD_MULT_SEGMENT_BASE_NODE (parent) : NULL)))
821 goto error;
822
823 /* Inherit attribute values from parent when the value isn't found */
824 GST_LOG ("attributes of SegmentTemplate node:");
825 if (gst_xml_helper_get_prop_string (a_node, "media", &strval)) {
826 new_segment_template->media = strval;
827 } else if (parent) {
828 new_segment_template->media = xmlMemStrdup (parent->media);
829 }
830
831 if (gst_xml_helper_get_prop_string (a_node, "index", &strval)) {
832 new_segment_template->index = strval;
833 } else if (parent) {
834 new_segment_template->index = xmlMemStrdup (parent->index);
835 }
836
837 if (gst_xml_helper_get_prop_string (a_node, "initialization", &strval)) {
838 new_segment_template->initialization = strval;
839 } else if (parent) {
840 new_segment_template->initialization =
841 xmlMemStrdup (parent->initialization);
842 }
843
844 if (gst_xml_helper_get_prop_string (a_node, "bitstreamSwitching", &strval)) {
845 new_segment_template->bitstreamSwitching = strval;
846 } else if (parent) {
847 new_segment_template->bitstreamSwitching =
848 xmlMemStrdup (parent->bitstreamSwitching);
849 }
850
851 *pointer = new_segment_template;
852 return TRUE;
853
854 error:
855 gst_mpd_segment_template_node_free (new_segment_template);
856 return FALSE;
857 }
858
859 static gboolean
860 gst_mpdparser_parse_period_node (GList ** list, xmlNode * a_node)
861 {
862 xmlNode *cur_node;
863 GstMPDPeriodNode *new_period;
864 gchar *actuate;
865
866 new_period = gst_mpd_period_node_new ();
867
868 GST_LOG ("attributes of Period node:");
869
870 new_period->actuate = GST_MPD_XLINK_ACTUATE_ON_REQUEST;
871 if (gst_xml_helper_get_ns_prop_string (a_node,
872 "http://www.w3.org/1999/xlink", "href", &new_period->xlink_href)
873 && gst_xml_helper_get_ns_prop_string (a_node,
874 "http://www.w3.org/1999/xlink", "actuate", &actuate)) {
875 if (strcmp (actuate, "onLoad") == 0)
876 new_period->actuate = GST_MPD_XLINK_ACTUATE_ON_LOAD;
877 xmlFree (actuate);
878 }
879
880 gst_xml_helper_get_prop_string (a_node, "id", &new_period->id);
881 gst_xml_helper_get_prop_duration (a_node, "start", GST_MPD_DURATION_NONE,
882 &new_period->start);
883 gst_xml_helper_get_prop_duration (a_node, "duration",
884 GST_MPD_DURATION_NONE, &new_period->duration);
885 gst_xml_helper_get_prop_boolean (a_node, "bitstreamSwitching", FALSE,
886 &new_period->bitstreamSwitching);
887
888 /* explore children nodes */
889 for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {
890 if (cur_node->type == XML_ELEMENT_NODE) {
891 if (xmlStrcmp (cur_node->name, (xmlChar *) "SegmentBase") == 0) {
892 gst_mpdparser_parse_seg_base_type_ext (&new_period->SegmentBase,
893 cur_node, NULL);
894 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "SegmentList") == 0) {
895 if (!gst_mpdparser_parse_segment_list_node (&new_period->SegmentList,
896 cur_node, NULL))
897 goto error;
898 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "SegmentTemplate") == 0) {
899 if (!gst_mpdparser_parse_segment_template_node
900 (&new_period->SegmentTemplate, cur_node, NULL))
901 goto error;
902 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "Subset") == 0) {
903 gst_mpdparser_parse_subset_node (&new_period->Subsets, cur_node);
904 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "BaseURL") == 0) {
905 gst_mpdparser_parse_baseURL_node (&new_period->BaseURLs, cur_node);
906 }
907 }
908 }
909
910 /* We must parse AdaptationSet after everything else in the Period has been
911 * parsed because certain AdaptationSet child elements can inherit attributes
912 * specified by the same element in the Period
913 */
914 for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {
915 if (cur_node->type == XML_ELEMENT_NODE) {
916 if (xmlStrcmp (cur_node->name, (xmlChar *) "AdaptationSet") == 0) {
917 if (!gst_mpdparser_parse_adaptation_set_node
918 (&new_period->AdaptationSets, cur_node, new_period))
919 goto error;
920 }
921 }
922 }
923
924 *list = g_list_append (*list, new_period);
925 return TRUE;
926
927 error:
928 gst_mpd_period_node_free (new_period);
929 return FALSE;
930 }
931
932 static void
933 gst_mpdparser_parse_program_info_node (GList ** list, xmlNode * a_node)
934 {
935 xmlNode *cur_node;
936 GstMPDProgramInformationNode *new_prog_info;
937
938 new_prog_info = gst_mpd_program_information_node_new ();
939 *list = g_list_append (*list, new_prog_info);
940
941 GST_LOG ("attributes of ProgramInformation node:");
942 gst_xml_helper_get_prop_string (a_node, "lang", &new_prog_info->lang);
943 gst_xml_helper_get_prop_string (a_node, "moreInformationURL",
944 &new_prog_info->moreInformationURL);
945
946 /* explore children nodes */
947 GST_LOG ("children of ProgramInformation node:");
948 for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {
949 if (cur_node->type == XML_ELEMENT_NODE) {
950 if (xmlStrcmp (cur_node->name, (xmlChar *) "Title") == 0) {
951 gst_xml_helper_get_node_content (cur_node, &new_prog_info->Title);
952 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "Source") == 0) {
953 gst_xml_helper_get_node_content (cur_node, &new_prog_info->Source);
954 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "Copyright") == 0) {
955 gst_xml_helper_get_node_content (cur_node, &new_prog_info->Copyright);
956 }
957 }
958 }
959 }
960
961 static void
962 gst_mpdparser_parse_metrics_range_node (GList ** list, xmlNode * a_node)
963 {
964 GstMPDMetricsRangeNode *new_metrics_range;
965
966 new_metrics_range = gst_mpd_metrics_range_node_new ();
967 *list = g_list_append (*list, new_metrics_range);
968
969 GST_LOG ("attributes of Metrics Range node:");
970 gst_xml_helper_get_prop_duration (a_node, "starttime",
971 GST_MPD_DURATION_NONE, &new_metrics_range->starttime);
972 gst_xml_helper_get_prop_duration (a_node, "duration",
973 GST_MPD_DURATION_NONE, &new_metrics_range->duration);
974 }
975
976 static void
977 gst_mpdparser_parse_metrics_node (GList ** list, xmlNode * a_node)
978 {
979 xmlNode *cur_node;
980 GstMPDMetricsNode *new_metrics;
981
982 new_metrics = gst_mpd_metrics_node_new ();
983 *list = g_list_append (*list, new_metrics);
984
985 GST_LOG ("attributes of Metrics node:");
986 gst_xml_helper_get_prop_string (a_node, "metrics", &new_metrics->metrics);
987
988 /* explore children nodes */
989 GST_LOG ("children of Metrics node:");
990 for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {
991 if (cur_node->type == XML_ELEMENT_NODE) {
992 if (xmlStrcmp (cur_node->name, (xmlChar *) "Range") == 0) {
993 gst_mpdparser_parse_metrics_range_node (&new_metrics->MetricsRanges,
994 cur_node);
995 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "Reporting") == 0) {
996 /* No reporting scheme is specified in this part of ISO/IEC 23009.
997 * It is expected that external specifications may define formats
998 * and delivery for the reporting data. */
999 GST_LOG (" - Reporting node found (unknown structure)");
1000 }
1001 }
1002 }
1003 }
1004
1005 /* The UTCTiming element is defined in
1006 * ISO/IEC 23009-1:2014/PDAM 1 "Information technology — Dynamic adaptive streaming over HTTP (DASH) — Part 1: Media presentation description and segment formats / Amendment 1: High Profile and Availability Time Synchronization"
1007 */
1008 static void
1009 gst_mpdparser_parse_utctiming_node (GList ** list, xmlNode * a_node)
1010 {
1011 GstMPDUTCTimingNode *new_timing;
1012 gchar *method = NULL;
1013 gchar *value = NULL;
1014
1015 new_timing = gst_mpd_utctiming_node_new ();
1016
1017 GST_LOG ("attributes of UTCTiming node:");
1018 if (gst_xml_helper_get_prop_string (a_node, "schemeIdUri", &method)) {
1019 new_timing->method = gst_mpd_utctiming_get_method (method);
1020 xmlFree (method);
1021 }
1022
1023 if (gst_xml_helper_get_prop_string (a_node, "value", &value)) {
1024 int max_tokens = 0;
1025 if (GST_MPD_UTCTIMING_TYPE_DIRECT == new_timing->method) {
1026 /* The GST_MPD_UTCTIMING_TYPE_DIRECT method is a special case
1027 * that is not a space separated list.
1028 */
1029 max_tokens = 1;
1030 }
1031 new_timing->urls = g_strsplit (value, " ", max_tokens);
1032 xmlFree (value);
1033 }
1034
1035 /* append to list only if both method and urls were set */
1036 if (new_timing->method != 0 && new_timing->urls != NULL &&
1037 g_strv_length (new_timing->urls) != 0) {
1038 *list = g_list_append (*list, new_timing);
1039 } else {
1040 gst_mpd_utctiming_node_free (new_timing);
1041 }
1042 }
1043
1044 static gboolean
1045 gst_mpdparser_parse_root_node (GstMPDRootNode ** pointer, xmlNode * a_node)
1046 {
1047 xmlNode *cur_node;
1048 GstMPDRootNode *new_mpd_root;
1049
1050 gst_mpd_root_node_free (*pointer);
1051 *pointer = NULL;
1052 new_mpd_root = gst_mpd_root_node_new ();
1053
1054 GST_LOG ("namespaces of root MPD node:");
1055 new_mpd_root->default_namespace =
1056 gst_xml_helper_get_node_namespace (a_node, NULL);
1057 new_mpd_root->namespace_xsi =
1058 gst_xml_helper_get_node_namespace (a_node, "xsi");
1059 new_mpd_root->namespace_ext =
1060 gst_xml_helper_get_node_namespace (a_node, "ext");
1061
1062 GST_LOG ("attributes of root MPD node:");
1063 gst_xml_helper_get_prop_string (a_node, "schemaLocation",
1064 &new_mpd_root->schemaLocation);
1065 gst_xml_helper_get_prop_string (a_node, "id", &new_mpd_root->id);
1066 gst_xml_helper_get_prop_string (a_node, "profiles", &new_mpd_root->profiles);
1067 gst_mpd_helper_get_mpd_type (a_node, "type", &new_mpd_root->type);
1068 gst_xml_helper_get_prop_dateTime (a_node, "availabilityStartTime",
1069 &new_mpd_root->availabilityStartTime);
1070 gst_xml_helper_get_prop_dateTime (a_node, "availabilityEndTime",
1071 &new_mpd_root->availabilityEndTime);
1072 gst_xml_helper_get_prop_duration (a_node, "mediaPresentationDuration",
1073 GST_MPD_DURATION_NONE, &new_mpd_root->mediaPresentationDuration);
1074 gst_xml_helper_get_prop_duration (a_node, "minimumUpdatePeriod",
1075 GST_MPD_DURATION_NONE, &new_mpd_root->minimumUpdatePeriod);
1076 gst_xml_helper_get_prop_duration (a_node, "minBufferTime",
1077 GST_MPD_DURATION_NONE, &new_mpd_root->minBufferTime);
1078 gst_xml_helper_get_prop_duration (a_node, "timeShiftBufferDepth",
1079 GST_MPD_DURATION_NONE, &new_mpd_root->timeShiftBufferDepth);
1080 gst_xml_helper_get_prop_duration (a_node, "suggestedPresentationDelay",
1081 GST_MPD_DURATION_NONE, &new_mpd_root->suggestedPresentationDelay);
1082 gst_xml_helper_get_prop_duration (a_node, "maxSegmentDuration",
1083 GST_MPD_DURATION_NONE, &new_mpd_root->maxSegmentDuration);
1084 gst_xml_helper_get_prop_duration (a_node, "maxSubsegmentDuration",
1085 GST_MPD_DURATION_NONE, &new_mpd_root->maxSubsegmentDuration);
1086
1087 /* explore children Period nodes */
1088 for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {
1089 if (cur_node->type == XML_ELEMENT_NODE) {
1090 if (xmlStrcmp (cur_node->name, (xmlChar *) "Period") == 0) {
1091 if (!gst_mpdparser_parse_period_node (&new_mpd_root->Periods, cur_node))
1092 goto error;
1093 } else if (xmlStrcmp (cur_node->name,
1094 (xmlChar *) "ProgramInformation") == 0) {
1095 gst_mpdparser_parse_program_info_node (&new_mpd_root->ProgramInfos,
1096 cur_node);
1097 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "BaseURL") == 0) {
1098 gst_mpdparser_parse_baseURL_node (&new_mpd_root->BaseURLs, cur_node);
1099 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "Location") == 0) {
1100 gst_mpdparser_parse_location_node (&new_mpd_root->Locations, cur_node);
1101 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "Metrics") == 0) {
1102 gst_mpdparser_parse_metrics_node (&new_mpd_root->Metrics, cur_node);
1103 } else if (xmlStrcmp (cur_node->name, (xmlChar *) "UTCTiming") == 0) {
1104 gst_mpdparser_parse_utctiming_node (&new_mpd_root->UTCTimings,
1105 cur_node);
1106 }
1107 }
1108 }
1109
1110 *pointer = new_mpd_root;
1111 return TRUE;
1112
1113 error:
1114 gst_mpd_root_node_free (new_mpd_root);
1115 return FALSE;
1116 }
1117
1118 /* internal memory management functions */
1119
1120 /* ISO/IEC 23009-1:2004 5.3.9.4.4 */
1121 static gboolean
1122 validate_format (const gchar * format)
1123 {
1124 const gchar *p = format;
1125
1126 /* Check if it starts with % */
1127 if (!p || p[0] != '%')
1128 return FALSE;
1129 p++;
1130
1131 /* the spec mandates a format like %0[width]d */
1132 /* Following the %, we must have a 0 */
1133 if (p[0] != '0')
1134 return FALSE;
1135
1136 /* Following the % must be a number starting with 0
1137 */
1138 while (g_ascii_isdigit (*p))
1139 p++;
1140
1141 /* After any 0 and alphanumeric values, there must be a d.
1142 */
1143 if (p[0] != 'd')
1144 return FALSE;
1145 p++;
1146
1147 /* And then potentially more characters without any
1148 * further %, even if the spec does not mention this
1149 */
1150 p = strchr (p, '%');
1151 if (p)
1152 return FALSE;
1153
1154 return TRUE;
1155 }
1156
1157 static gchar *
1158 promote_format_to_uint64 (const gchar * format)
1159 {
1160 const gchar *p = format;
1161 gchar *promoted_format;
1162
1163 /* Must be called with a validated format! */
1164 g_return_val_if_fail (validate_format (format), NULL);
1165
1166 /* it starts with % */
1167 p++;
1168
1169 /* Following the % must be a 0, or any of d, x or u.
1170 * x and u are not part of the spec, but don't hurt us
1171 */
1172 if (p[0] == '0') {
1173 p++;
1174
1175 while (g_ascii_isdigit (*p))
1176 p++;
1177 }
1178
1179 /* After any 0 and alphanumeric values, there must be a d.
1180 * Otherwise validation would have failed
1181 */
1182 g_assert (p[0] == 'd');
1183
1184 promoted_format =
1185 g_strdup_printf ("%.*s" G_GINT64_MODIFIER "%s", (gint) (p - format),
1186 format, p);
1187
1188 return promoted_format;
1189 }
1190
1191 static gboolean
1192 gst_mpdparser_validate_rfc1738_url (const char *s)
1193 {
1194 while (*s) {
1195 if (!strchr
1196 (";:@&=aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789$-_.+!*'(),%/",
1197 *s))
1198 return FALSE;
1199 if (*s == '%') {
1200 /* g_ascii_isdigit returns FALSE for NUL, and || is a short circuiting
1201 operator, so this is safe for strings ending before two hex digits */
1202 if (!g_ascii_isxdigit (s[1]) || !g_ascii_isxdigit (s[2]))
1203 return FALSE;
1204 s += 2;
1205 }
1206 s++;
1207 }
1208 return TRUE;
1209 }
1210
1211 void
1212 gst_mpdparser_media_fragment_info_clear (GstMediaFragmentInfo * fragment)
1213 {
1214 g_free (fragment->uri);
1215 g_free (fragment->index_uri);
1216 }
1217
1218 /* API */
1219 gboolean
1220 gst_mpdparser_get_mpd_root_node (GstMPDRootNode ** mpd_root_node,
1221 const gchar * data, gint size)
1222 {
1223 gboolean ret = FALSE;
1224
1225 if (data) {
1226 xmlDocPtr doc;
1227 xmlNode *root_element = NULL;
1228
1229 GST_DEBUG ("MPD file fully buffered, start parsing...");
1230
1231 /* parse the complete MPD file into a tree (using the libxml2 default parser API) */
1232
1233 /* this initialize the library and check potential ABI mismatches
1234 * between the version it was compiled for and the actual shared
1235 * library used
1236 */
1237 LIBXML_TEST_VERSION;
1238
1239 /* parse "data" into a document (which is a libxml2 tree structure xmlDoc) */
1240 doc = xmlReadMemory (data, size, "noname.xml", NULL, XML_PARSE_NONET);
1241 if (doc == NULL) {
1242 GST_ERROR ("failed to parse the MPD file");
1243 ret = FALSE;
1244 } else {
1245 /* get the root element node */
1246 root_element = xmlDocGetRootElement (doc);
1247
1248 if (root_element->type != XML_ELEMENT_NODE
1249 || xmlStrcmp (root_element->name, (xmlChar *) "MPD") != 0) {
1250 GST_ERROR
1251 ("can not find the root element MPD, failed to parse the MPD file");
1252 ret = FALSE; /* used to return TRUE before, but this seems wrong */
1253 } else {
1254 /* now we can parse the MPD root node and all children nodes, recursively */
1255 ret = gst_mpdparser_parse_root_node (mpd_root_node, root_element);
1256 }
1257 /* free the document */
1258 xmlFreeDoc (doc);
1259 }
1260 }
1261
1262 return ret;
1263 }
1264
1265 GstMPDSegmentListNode *
1266 gst_mpdparser_get_external_segment_list (const gchar * data, gint size,
1267 GstMPDSegmentListNode * parent)
1268 {
1269 xmlDocPtr doc = NULL;
1270 GstMPDSegmentListNode *new_segment_list = NULL;
1271
1272 doc = xmlReadMemory (data, size, "noname.xml", NULL, XML_PARSE_NONET);
1273
1274
1275 /* NOTE: ISO/IEC 23009-1:2014 5.3.9.3.2 is saying that one or multiple SegmentList
1276 * in external xml is allowed, however, multiple SegmentList does not make sense
1277 * because Period/AdaptationSet/Representation allow only one SegmentList */
1278 if (doc) {
1279 xmlNode *root_element = xmlDocGetRootElement (doc);
1280
1281
1282 if (root_element->type == XML_ELEMENT_NODE &&
1283 xmlStrcmp (root_element->name, (xmlChar *) "SegmentList") == 0) {
1284 gst_mpdparser_parse_segment_list_node (&new_segment_list, root_element,
1285 parent);
1286 }
1287 }
1288
1289 if (doc)
1290 xmlFreeDoc (doc);
1291
1292 return new_segment_list;
1293 }
1294
1295 GList *
1296 gst_mpdparser_get_external_periods (const gchar * data, gint size)
1297 {
1298 xmlDocPtr doc = NULL;
1299 GList *new_periods = NULL;
1300
1301 doc = xmlReadMemory (data, size, "noname.xml", NULL, XML_PARSE_NONET);
1302
1303
1304 if (doc) {
1305 xmlNode *root_element = xmlDocGetRootElement (doc);
1306 xmlNode *iter;
1307
1308 for (iter = root_element->children; iter; iter = iter->next) {
1309 if (iter->type == XML_ELEMENT_NODE) {
1310 if (xmlStrcmp (iter->name, (xmlChar *) "Period") == 0) {
1311 gst_mpdparser_parse_period_node (&new_periods, iter);
1312 } else {
1313 goto error;
1314 }
1315 }
1316 }
1317 }
1318
1319 done:
1320 if (doc)
1321 xmlFreeDoc (doc);
1322
1323 return new_periods;
1324
1325 error:
1326 GST_ERROR ("Failed to parse period node XML");
1327
1328 if (new_periods) {
1329 g_list_free_full (new_periods, (GDestroyNotify) gst_mpd_period_node_free);
1330 new_periods = NULL;
1331 }
1332 goto done;
1333 }
1334
1335 GList *
1336 gst_mpdparser_get_external_adaptation_sets (const gchar * data, gint size,
1337 GstMPDPeriodNode * period)
1338 {
1339 xmlDocPtr doc = NULL;
1340 GList *new_adaptation_sets = NULL;
1341
1342 doc = xmlReadMemory (data, size, "noname.xml", NULL, XML_PARSE_NONET);
1343
1344 /* NOTE: ISO/IEC 23009-1:2014 5.3.3.2 is saying that exactly one AdaptationSet
1345 * in external xml is allowed */
1346 if (doc) {
1347 xmlNode *root_element = xmlDocGetRootElement (doc);
1348 if (root_element->type == XML_ELEMENT_NODE &&
1349 xmlStrcmp (root_element->name, (xmlChar *) "AdaptationSet") == 0) {
1350 gst_mpdparser_parse_adaptation_set_node (&new_adaptation_sets,
1351 root_element, period);
1352 }
1353 }
1354
1355 if (doc)
1356 xmlFreeDoc (doc);
1357
1358 return new_adaptation_sets;
1359 }
1360
1361 void
1362 gst_mpdparser_free_stream_period (GstStreamPeriod * stream_period)
1363 {
1364 if (stream_period) {
1365 g_free (stream_period);
1366 }
1367 }
1368
1369 void
1370 gst_mpdparser_free_media_segment (GstMediaSegment * media_segment)
1371 {
1372 if (media_segment) {
1373 g_free (media_segment);
1374 }
1375 }
1376
1377 void
1378 gst_mpdparser_init_active_stream_segments (GstActiveStream * stream)
1379 {
1380 g_assert (stream->segments == NULL);
1381 stream->segments = g_ptr_array_new ();
1382 g_ptr_array_set_free_func (stream->segments,
1383 (GDestroyNotify) gst_mpdparser_free_media_segment);
1384 }
1385
1386 void
1387 gst_mpdparser_free_active_stream (GstActiveStream * active_stream)
1388 {
1389 if (active_stream) {
1390 g_free (active_stream->baseURL);
1391 active_stream->baseURL = NULL;
1392 g_free (active_stream->queryURL);
1393 active_stream->queryURL = NULL;
1394 if (active_stream->segments)
1395 g_ptr_array_unref (active_stream->segments);
1396 g_free (active_stream);
1397 }
1398 }
1399
1400 static gchar *
1401 get_base_url_with_query (GstActiveStream * stream)
1402 {
1403 GstUri *uri;
1404 gchar *uri_str;
1405
1406 if (!stream->queryURL)
1407 return g_strdup (stream->baseURL);
1408
1409 uri = gst_uri_from_string (stream->baseURL);
1410 gst_uri_set_query_string (uri, stream->queryURL);
1411 uri_str = gst_uri_to_string (uri);
1412
1413 gst_uri_unref (uri);
1414 return uri_str;
1415 }
1416
1417 /*
1418 * gst_mpdparser_get_initializationURL:
1419 *
1420 * Returns: (transfer full): stream initializationURL if available,
1421 * baseURL combined with queryURL otherwise.
1422 */
1423 gchar *
1424 gst_mpdparser_get_initializationURL (GstActiveStream * stream,
1425 GstMPDURLTypeNode * InitializationURL)
1426 {
1427 g_return_val_if_fail (stream != NULL, NULL);
1428
1429 return (InitializationURL && InitializationURL->sourceURL)
1430 ? g_strdup (InitializationURL->sourceURL)
1431 : get_base_url_with_query (stream);
1432 }
1433
1434 /*
1435 * gst_mpdparser_get_mediaURL:
1436 *
1437 * Returns: (transfer full): stream mediaURL if available,
1438 * baseURL combined with queryURL otherwise.
1439 */
1440 gchar *
1441 gst_mpdparser_get_mediaURL (GstActiveStream * stream,
1442 GstMPDSegmentURLNode * segmentURL)
1443 {
1444 g_return_val_if_fail (stream != NULL, NULL);
1445 g_return_val_if_fail (segmentURL != NULL, NULL);
1446
1447 return (segmentURL->media)
1448 ? g_strdup (segmentURL->media)
1449 : get_base_url_with_query (stream);
1450 }
1451
1452 /* navigation functions */
1453 GstStreamMimeType
1454 gst_mpdparser_representation_get_mimetype (GstMPDAdaptationSetNode * adapt_set,
1455 GstMPDRepresentationNode * rep)
1456 {
1457 gchar *mime = NULL;
1458 if (rep)
1459 mime = GST_MPD_REPRESENTATION_BASE_NODE (rep)->mimeType;
1460 if (mime == NULL) {
1461 mime = GST_MPD_REPRESENTATION_BASE_NODE (adapt_set)->mimeType;
1462 }
1463
1464 if (gst_mpd_helper_strncmp_ext (mime, "audio") == 0)
1465 return GST_STREAM_AUDIO;
1466 if (gst_mpd_helper_strncmp_ext (mime, "video") == 0)
1467 return GST_STREAM_VIDEO;
1468 if (gst_mpd_helper_strncmp_ext (mime, "application") == 0
1469 || gst_mpd_helper_strncmp_ext (mime, "text") == 0)
1470 return GST_STREAM_APPLICATION;
1471
1472 return GST_STREAM_UNKNOWN;
1473 }
1474
1475 /* Helper methods */
1476 gchar *
1477 gst_mpdparser_build_URL_from_template (const gchar * url_template,
1478 const gchar * id, guint number, guint bandwidth, guint64 time)
1479 {
1480 static const gchar default_format[] = "%01d";
1481 gchar **tokens, *token, *ret;
1482 const gchar *format;
1483 gint i, num_tokens;
1484
1485 g_return_val_if_fail (url_template != NULL, NULL);
1486 tokens = g_strsplit_set (url_template, "$", -1);
1487 if (!tokens) {
1488 GST_WARNING ("Scan of URL template failed!");
1489 return NULL;
1490 }
1491 num_tokens = g_strv_length (tokens);
1492
1493 /*
1494 * each identifier is guarded by 2 $, which means that we must have an odd number of tokens
1495 * An even number of tokens means the string is not valid.
1496 */
1497 if ((num_tokens & 1) == 0) {
1498 GST_ERROR ("Invalid number of tokens (%d). url_template is '%s'",
1499 num_tokens, url_template);
1500 g_strfreev (tokens);
1501 return NULL;
1502 }
1503
1504 for (i = 0; i < num_tokens; i++) {
1505 token = tokens[i];
1506 format = default_format;
1507
1508 /* the tokens to replace must be provided between $ characters, eg $token$
1509 * For a string like token0$token1$token2$token3$token4, only the odd number
1510 * tokens (1,3,...) must be parsed.
1511 *
1512 * Skip even tokens
1513 */
1514 if ((i & 1) == 0)
1515 continue;
1516
1517 if (!g_strcmp0 (token, "RepresentationID")) {
1518 if (!gst_mpdparser_validate_rfc1738_url (id))
1519 goto invalid_representation_id;
1520
1521 tokens[i] = g_strdup_printf ("%s", id);
1522 g_free (token);
1523 } else if (!strncmp (token, "Number", 6)) {
1524 if (strlen (token) > 6) {
1525 format = token + 6; /* format tag */
1526 }
1527 if (!validate_format (format))
1528 goto invalid_format;
1529
1530 tokens[i] = g_strdup_printf (format, number);
1531 g_free (token);
1532 } else if (!strncmp (token, "Bandwidth", 9)) {
1533 if (strlen (token) > 9) {
1534 format = token + 9; /* format tag */
1535 }
1536 if (!validate_format (format))
1537 goto invalid_format;
1538
1539 tokens[i] = g_strdup_printf (format, bandwidth);
1540 g_free (token);
1541 } else if (!strncmp (token, "Time", 4)) {
1542 gchar *promoted_format;
1543
1544 if (strlen (token) > 4) {
1545 format = token + 4; /* format tag */
1546 }
1547 if (!validate_format (format))
1548 goto invalid_format;
1549
1550 promoted_format = promote_format_to_uint64 (format);
1551 tokens[i] = g_strdup_printf (promoted_format, time);
1552 g_free (promoted_format);
1553 g_free (token);
1554 } else if (!g_strcmp0 (token, "")) {
1555 tokens[i] = g_strdup_printf ("%s", "$");
1556 g_free (token);
1557 } else {
1558 /* unexpected identifier found between $ signs
1559 *
1560 * "If the URL contains unescaped $ symbols which do not enclose a valid
1561 * identifier then the result of URL formation is undefined"
1562 */
1563 goto invalid_format;
1564 }
1565 }
1566
1567 ret = g_strjoinv (NULL, tokens);
1568
1569 g_strfreev (tokens);
1570
1571 return ret;
1572
1573 invalid_format:
1574 {
1575 GST_ERROR ("Invalid format '%s' in '%s'", format, token);
1576
1577 g_strfreev (tokens);
1578
1579 return NULL;
1580 }
1581 invalid_representation_id:
1582 {
1583 GST_ERROR
1584 ("Representation ID string '%s' has characters invalid in an RFC 1738 URL",
1585 id);
1586
1587 g_strfreev (tokens);
1588
1589 return NULL;
1590 }
1591 }