Debian Codegraph @

ext/adaptivedemux2/dash/gstxmlhelper.c

raw
1 /* GStreamer
2 *
3 * Copyright (C) 2019 Collabora Ltd.
4 * Author: Stéphane Cerveau <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #include "gstxmlhelper.h"
23
24 #define GST_CAT_DEFAULT gst_dash_demux2_debug
25
26 #define XML_HELPER_MINUTE_TO_SEC 60
27 #define XML_HELPER_HOUR_TO_SEC (60 * XML_HELPER_MINUTE_TO_SEC)
28 #define XML_HELPER_DAY_TO_SEC (24 * XML_HELPER_HOUR_TO_SEC)
29 #define XML_HELPER_MONTH_TO_SEC (30 * XML_HELPER_DAY_TO_SEC)
30 #define XML_HELPER_YEAR_TO_SEC (365 * XML_HELPER_DAY_TO_SEC)
31 #define XML_HELPER_MS_TO_SEC(time) ((time) / 1000)
32 /* static methods */
33 /* this function computes decimals * 10 ^ (3 - pos) */
34 static guint
35 _mpd_helper_convert_to_millisecs (guint decimals, gint pos)
36 {
37 guint num = 1, den = 1;
38 gint i = 3 - pos;
39
40 while (i < 0) {
41 den *= 10;
42 i++;
43 }
44 while (i > 0) {
45 num *= 10;
46 i--;
47 }
48 /* if i == 0 we have exactly 3 decimals and nothing to do */
49 return decimals * num / den;
50 }
51
52 static gboolean
53 _mpd_helper_accumulate (guint64 * v, guint64 mul, guint64 add)
54 {
55 guint64 tmp;
56
57 if (*v > G_MAXUINT64 / mul)
58 return FALSE;
59 tmp = *v * mul;
60 if (tmp > G_MAXUINT64 - add)
61 return FALSE;
62 *v = tmp + add;
63 return TRUE;
64 }
65
66 /*
67 Duration Data Type
68
69 The duration data type is used to specify a time interval.
70
71 The time interval is specified in the following form "-PnYnMnDTnHnMnS" where:
72
73 * - indicates the negative sign (optional)
74 * P indicates the period (required)
75 * nY indicates the number of years
76 * nM indicates the number of months
77 * nD indicates the number of days
78 * T indicates the start of a time section (required if you are going to specify hours, minutes, or seconds)
79 * nH indicates the number of hours
80 * nM indicates the number of minutes
81 * nS indicates the number of seconds
82 */
83 static gboolean
84 _mpd_helper_parse_duration (const char *str, guint64 * value)
85 {
86 gint ret, len, pos, posT;
87 gint years = -1, months = -1, days = -1, hours = -1, minutes = -1, seconds =
88 -1, decimals = -1, read;
89 gboolean have_ms = FALSE;
90 guint64 tmp_value;
91
92 len = strlen (str);
93 GST_TRACE ("duration: %s, len %d", str, len);
94 if (strspn (str, "PT0123456789., \tHMDSY") < len) {
95 GST_WARNING ("Invalid character found: '%s'", str);
96 goto error;
97 }
98 /* skip leading/trailing whitespace */
99 while (g_ascii_isspace (str[0])) {
100 str++;
101 len--;
102 }
103 while (len > 0 && g_ascii_isspace (str[len - 1]))
104 --len;
105
106 /* read "P" for period */
107 if (str[0] != 'P') {
108 GST_WARNING ("P not found at the beginning of the string!");
109 goto error;
110 }
111 str++;
112 len--;
113
114 /* read "T" for time (if present) */
115 posT = strcspn (str, "T");
116 len -= posT;
117 if (posT > 0) {
118 /* there is some room between P and T, so there must be a period section */
119 /* read years, months, days */
120 do {
121 GST_TRACE ("parsing substring %s", str);
122 pos = strcspn (str, "YMD");
123 ret = sscanf (str, "%u", &read);
124 if (ret != 1) {
125 GST_WARNING ("can not read integer value from string %s!", str);
126 goto error;
127 }
128 switch (str[pos]) {
129 case 'Y':
130 if (years != -1 || months != -1 || days != -1) {
131 GST_WARNING ("year, month or day was already set");
132 goto error;
133 }
134 years = read;
135 break;
136 case 'M':
137 if (months != -1 || days != -1) {
138 GST_WARNING ("month or day was already set");
139 goto error;
140 }
141 months = read;
142 if (months >= 12) {
143 GST_WARNING ("Month out of range");
144 goto error;
145 }
146 break;
147 case 'D':
148 if (days != -1) {
149 GST_WARNING ("day was already set");
150 goto error;
151 }
152 days = read;
153 if (days >= 31) {
154 GST_WARNING ("Day out of range");
155 goto error;
156 }
157 break;
158 default:
159 GST_WARNING ("unexpected char %c!", str[pos]);
160 goto error;
161 break;
162 }
163 GST_TRACE ("read number %u type %c", read, str[pos]);
164 str += (pos + 1);
165 posT -= (pos + 1);
166 } while (posT > 0);
167 }
168
169 if (years == -1)
170 years = 0;
171 if (months == -1)
172 months = 0;
173 if (days == -1)
174 days = 0;
175
176 GST_TRACE ("Y:M:D=%d:%d:%d", years, months, days);
177
178 /* read "T" for time (if present) */
179 /* here T is at pos == 0 */
180 str++;
181 len--;
182 pos = 0;
183 if (pos < len) {
184 /* T found, there is a time section */
185 /* read hours, minutes, seconds, hundredths of second */
186 do {
187 GST_TRACE ("parsing substring %s", str);
188 pos = strcspn (str, "HMS,.");
189 ret = sscanf (str, "%u", &read);
190 if (ret != 1) {
191 GST_WARNING ("can not read integer value from string %s!", str);
192 goto error;
193 }
194 switch (str[pos]) {
195 case 'H':
196 if (hours != -1 || minutes != -1 || seconds != -1) {
197 GST_WARNING ("hour, minute or second was already set");
198 goto error;
199 }
200 hours = read;
201 break;
202 case 'M':
203 if (minutes != -1 || seconds != -1) {
204 GST_WARNING ("minute or second was already set");
205 goto error;
206 }
207 minutes = read;
208 break;
209 case 'S':
210 if (have_ms) {
211 /* we have read the decimal part of the seconds */
212 decimals = _mpd_helper_convert_to_millisecs (read, pos);
213 GST_TRACE ("decimal number %u (%d digits) -> %d ms", read, pos,
214 decimals);
215 } else {
216 if (seconds != -1) {
217 GST_WARNING ("second was already set");
218 goto error;
219 }
220 /* no decimals */
221 seconds = read;
222 }
223 break;
224 case '.':
225 case ',':
226 /* we have read the integer part of a decimal number in seconds */
227 if (seconds != -1) {
228 GST_WARNING ("second was already set");
229 goto error;
230 }
231 seconds = read;
232 have_ms = TRUE;
233 break;
234 default:
235 GST_WARNING ("unexpected char %c!", str[pos]);
236 goto error;
237 break;
238 }
239 GST_TRACE ("read number %u type %c", read, str[pos]);
240 str += pos + 1;
241 len -= (pos + 1);
242 } while (len > 0);
243 }
244
245 if (hours == -1)
246 hours = 0;
247 if (minutes == -1)
248 minutes = 0;
249 if (seconds == -1)
250 seconds = 0;
251 if (decimals == -1)
252 decimals = 0;
253 GST_TRACE ("H:M:S.MS=%d:%d:%d.%03d", hours, minutes, seconds, decimals);
254
255 tmp_value = 0;
256 if (!_mpd_helper_accumulate (&tmp_value, 1, years)
257 || !_mpd_helper_accumulate (&tmp_value, 365, months * 30)
258 || !_mpd_helper_accumulate (&tmp_value, 1, days)
259 || !_mpd_helper_accumulate (&tmp_value, 24, hours)
260 || !_mpd_helper_accumulate (&tmp_value, 60, minutes)
261 || !_mpd_helper_accumulate (&tmp_value, 60, seconds)
262 || !_mpd_helper_accumulate (&tmp_value, 1000, decimals))
263 goto error;
264
265 /* ensure it can be converted from milliseconds to nanoseconds */
266 if (tmp_value > G_MAXUINT64 / 1000000)
267 goto error;
268
269 *value = tmp_value;
270 return TRUE;
271
272 error:
273 return FALSE;
274 }
275
276 static gboolean
277 _mpd_helper_validate_no_whitespace (const char *s)
278 {
279 return !strpbrk (s, "\r\n\t ");
280 }
281
282 /* API */
283
284 GstXMLRange *
285 gst_xml_helper_clone_range (GstXMLRange * range)
286 {
287 GstXMLRange *clone = NULL;
288
289 if (range) {
290 clone = g_new0 (GstXMLRange, 1);
291 clone->first_byte_pos = range->first_byte_pos;
292 clone->last_byte_pos = range->last_byte_pos;
293 }
294
295 return clone;
296 }
297
298 GstXMLRatio *
299 gst_xml_helper_clone_ratio (GstXMLRatio * ratio)
300 {
301 GstXMLRatio *clone = NULL;
302
303 if (ratio) {
304 clone = g_new0 (GstXMLRatio, 1);
305 clone->num = ratio->num;
306 clone->den = ratio->den;
307 }
308
309 return clone;
310 }
311
312 GstXMLFrameRate *
313 gst_xml_helper_clone_frame_rate (GstXMLFrameRate * frameRate)
314 {
315 GstXMLFrameRate *clone = NULL;
316
317 if (frameRate) {
318 clone = g_new0 (GstXMLFrameRate, 1);
319 clone->num = frameRate->num;
320 clone->den = frameRate->den;
321 }
322
323 return clone;
324 }
325
326 /* XML property get method */
327 gboolean
328 gst_xml_helper_get_prop_validated_string (xmlNode * a_node,
329 const gchar * property_name, gchar ** property_value,
330 gboolean (*validate) (const char *))
331 {
332 xmlChar *prop_string;
333 gboolean exists = FALSE;
334
335 prop_string = xmlGetProp (a_node, (const xmlChar *) property_name);
336 if (prop_string) {
337 if (validate && !(*validate) ((const char *) prop_string)) {
338 GST_WARNING ("Validation failure: %s", prop_string);
339 xmlFree (prop_string);
340 return FALSE;
341 }
342 *property_value = (gchar *) prop_string;
343 exists = TRUE;
344 GST_LOG (" - %s: %s", property_name, prop_string);
345 }
346
347 return exists;
348 }
349
350 gboolean
351 gst_xml_helper_get_ns_prop_string (xmlNode * a_node,
352 const gchar * ns_name, const gchar * property_name, gchar ** property_value)
353 {
354 xmlChar *prop_string;
355 gboolean exists = FALSE;
356
357 prop_string =
358 xmlGetNsProp (a_node, (const xmlChar *) property_name,
359 (const xmlChar *) ns_name);
360 if (prop_string) {
361 *property_value = (gchar *) prop_string;
362 exists = TRUE;
363 GST_LOG (" - %s:%s: %s", ns_name, property_name, prop_string);
364 }
365
366 return exists;
367 }
368
369 gboolean
370 gst_xml_helper_get_prop_string (xmlNode * a_node,
371 const gchar * property_name, gchar ** property_value)
372 {
373 return gst_xml_helper_get_prop_validated_string (a_node, property_name,
374 property_value, NULL);
375 }
376
377 gboolean
378 gst_xml_helper_get_prop_string_vector_type (xmlNode * a_node,
379 const gchar * property_name, gchar *** property_value)
380 {
381 xmlChar *prop_string;
382 gchar **prop_string_vector = NULL;
383 guint i = 0;
384 gboolean exists = FALSE;
385
386 prop_string = xmlGetProp (a_node, (const xmlChar *) property_name);
387 if (prop_string) {
388 prop_string_vector = g_strsplit ((gchar *) prop_string, " ", -1);
389 if (prop_string_vector) {
390 exists = TRUE;
391 *property_value = prop_string_vector;
392 GST_LOG (" - %s:", property_name);
393 while (prop_string_vector[i]) {
394 GST_LOG (" %s", prop_string_vector[i]);
395 i++;
396 }
397 } else {
398 GST_WARNING ("Scan of string vector property failed!");
399 }
400 xmlFree (prop_string);
401 }
402
403 return exists;
404 }
405
406 gboolean
407 gst_xml_helper_get_prop_signed_integer (xmlNode * a_node,
408 const gchar * property_name, gint default_val, gint * property_value)
409 {
410 xmlChar *prop_string;
411 gboolean exists = FALSE;
412
413 *property_value = default_val;
414 prop_string = xmlGetProp (a_node, (const xmlChar *) property_name);
415 if (prop_string) {
416 if (sscanf ((const gchar *) prop_string, "%d", property_value) == 1) {
417 exists = TRUE;
418 GST_LOG (" - %s: %d", property_name, *property_value);
419 } else {
420 GST_WARNING
421 ("failed to parse signed integer property %s from xml string %s",
422 property_name, prop_string);
423 }
424 xmlFree (prop_string);
425 }
426
427 return exists;
428 }
429
430 gboolean
431 gst_xml_helper_get_prop_unsigned_integer (xmlNode * a_node,
432 const gchar * property_name, guint default_val, guint * property_value)
433 {
434 xmlChar *prop_string;
435 gboolean exists = FALSE;
436
437 *property_value = default_val;
438 prop_string = xmlGetProp (a_node, (const xmlChar *) property_name);
439 if (prop_string) {
440 if (sscanf ((gchar *) prop_string, "%u", property_value) == 1 &&
441 strstr ((gchar *) prop_string, "-") == NULL) {
442 exists = TRUE;
443 GST_LOG (" - %s: %u", property_name, *property_value);
444 } else {
445 GST_WARNING
446 ("failed to parse unsigned integer property %s from xml string %s",
447 property_name, prop_string);
448 /* sscanf might have written to *property_value. Restore to default */
449 *property_value = default_val;
450 }
451 xmlFree (prop_string);
452 }
453
454 return exists;
455 }
456
457 gboolean
458 gst_xml_helper_get_prop_unsigned_integer_64 (xmlNode * a_node,
459 const gchar * property_name, guint64 default_val, guint64 * property_value)
460 {
461 xmlChar *prop_string;
462 gboolean exists = FALSE;
463
464 *property_value = default_val;
465 prop_string = xmlGetProp (a_node, (const xmlChar *) property_name);
466 if (prop_string) {
467 if (g_ascii_string_to_unsigned ((gchar *) prop_string, 10, 0, G_MAXUINT64,
468 property_value, NULL)) {
469 exists = TRUE;
470 GST_LOG (" - %s: %" G_GUINT64_FORMAT, property_name, *property_value);
471 } else {
472 GST_WARNING
473 ("failed to parse unsigned integer property %s from xml string %s",
474 property_name, prop_string);
475 }
476 xmlFree (prop_string);
477 }
478
479 return exists;
480 }
481
482 gboolean
483 gst_xml_helper_get_prop_uint_vector_type (xmlNode * a_node,
484 const gchar * property_name, guint ** property_value, guint * value_size)
485 {
486 xmlChar *prop_string;
487 gchar **str_vector;
488 guint *prop_uint_vector = NULL, i;
489 gboolean exists = FALSE;
490
491 prop_string = xmlGetProp (a_node, (const xmlChar *) property_name);
492 if (prop_string) {
493 str_vector = g_strsplit ((gchar *) prop_string, " ", -1);
494 if (str_vector) {
495 *value_size = g_strv_length (str_vector);
496 prop_uint_vector = g_malloc (*value_size * sizeof (guint));
497 if (prop_uint_vector) {
498 exists = TRUE;
499 GST_LOG (" - %s:", property_name);
500 for (i = 0; i < *value_size; i++) {
501 if (sscanf ((gchar *) str_vector[i], "%u", &prop_uint_vector[i]) == 1
502 && strstr (str_vector[i], "-") == NULL) {
503 GST_LOG (" %u", prop_uint_vector[i]);
504 } else {
505 GST_WARNING
506 ("failed to parse uint vector type property %s from xml string %s",
507 property_name, str_vector[i]);
508 /* there is no special value to put in prop_uint_vector[i] to
509 * signal it is invalid, so we just clean everything and return
510 * FALSE
511 */
512 g_free (prop_uint_vector);
513 prop_uint_vector = NULL;
514 exists = FALSE;
515 break;
516 }
517 }
518 *property_value = prop_uint_vector;
519 } else {
520 GST_WARNING ("Array allocation failed!");
521 }
522 } else {
523 GST_WARNING ("Scan of uint vector property failed!");
524 }
525 xmlFree (prop_string);
526 g_strfreev (str_vector);
527 }
528
529 return exists;
530 }
531
532 gboolean
533 gst_xml_helper_get_prop_double (xmlNode * a_node,
534 const gchar * property_name, gdouble * property_value)
535 {
536 xmlChar *prop_string;
537 gboolean exists = FALSE;
538
539 prop_string = xmlGetProp (a_node, (const xmlChar *) property_name);
540 if (prop_string) {
541 if (sscanf ((gchar *) prop_string, "%lf", property_value) == 1) {
542 exists = TRUE;
543 GST_LOG (" - %s: %lf", property_name, *property_value);
544 } else {
545 GST_WARNING ("failed to parse double property %s from xml string %s",
546 property_name, prop_string);
547 }
548 xmlFree (prop_string);
549 }
550
551 return exists;
552 }
553
554 gboolean
555 gst_xml_helper_get_prop_boolean (xmlNode * a_node,
556 const gchar * property_name, gboolean default_val,
557 gboolean * property_value)
558 {
559 xmlChar *prop_string;
560 gboolean exists = FALSE;
561
562 *property_value = default_val;
563 prop_string = xmlGetProp (a_node, (const xmlChar *) property_name);
564 if (prop_string) {
565 if (xmlStrcmp (prop_string, (xmlChar *) "false") == 0) {
566 exists = TRUE;
567 *property_value = FALSE;
568 GST_LOG (" - %s: false", property_name);
569 } else if (xmlStrcmp (prop_string, (xmlChar *) "true") == 0) {
570 exists = TRUE;
571 *property_value = TRUE;
572 GST_LOG (" - %s: true", property_name);
573 } else {
574 GST_WARNING ("failed to parse boolean property %s from xml string %s",
575 property_name, prop_string);
576 }
577 xmlFree (prop_string);
578 }
579
580 return exists;
581 }
582
583 gboolean
584 gst_xml_helper_get_prop_range (xmlNode * a_node,
585 const gchar * property_name, GstXMLRange ** property_value)
586 {
587 xmlChar *prop_string;
588 guint64 first_byte_pos = 0, last_byte_pos = -1;
589 guint len, pos;
590 gchar *str;
591 gboolean exists = FALSE;
592
593 prop_string = xmlGetProp (a_node, (const xmlChar *) property_name);
594 if (prop_string) {
595 len = xmlStrlen (prop_string);
596 str = (gchar *) prop_string;
597 GST_TRACE ("range: %s, len %d", str, len);
598
599 /* find "-" */
600 pos = strcspn (str, "-");
601 if (pos >= len) {
602 GST_TRACE ("pos %d >= len %d", pos, len);
603 goto error;
604 }
605 if (pos == 0) {
606 GST_TRACE ("pos == 0, but first_byte_pos is not optional");
607 goto error;
608 }
609
610 /* read first_byte_pos */
611
612 /* replace str[pos] with '\0' since we only want to read the
613 * first_byte_pos, and g_ascii_string_to_unsigned requires the entire
614 * string to be a single number, which is exactly what we want */
615 str[pos] = '\0';
616 if (!g_ascii_string_to_unsigned (str, 10, 0, G_MAXUINT64, &first_byte_pos,
617 NULL)) {
618 /* restore the '-' sign */
619 str[pos] = '-';
620 goto error;
621 }
622 /* restore the '-' sign */
623 str[pos] = '-';
624
625 /* read last_byte_pos, which is optional */
626 if (pos < (len - 1) && !g_ascii_string_to_unsigned (str + pos + 1, 10, 0,
627 G_MAXUINT64, &last_byte_pos, NULL)) {
628 goto error;
629 }
630 /* malloc return data structure */
631 *property_value = g_new0 (GstXMLRange, 1);
632 exists = TRUE;
633 (*property_value)->first_byte_pos = first_byte_pos;
634 (*property_value)->last_byte_pos = last_byte_pos;
635 xmlFree (prop_string);
636 GST_LOG (" - %s: %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT,
637 property_name, first_byte_pos, last_byte_pos);
638 }
639
640 return exists;
641
642 error:
643 GST_WARNING ("failed to parse property %s from xml string %s", property_name,
644 prop_string);
645 xmlFree (prop_string);
646 return FALSE;
647 }
648
649 gboolean
650 gst_xml_helper_get_prop_ratio (xmlNode * a_node,
651 const gchar * property_name, GstXMLRatio ** property_value)
652 {
653 xmlChar *prop_string;
654 guint num = 0, den = 1;
655 guint len, pos;
656 gchar *str;
657 gboolean exists = FALSE;
658
659 prop_string = xmlGetProp (a_node, (const xmlChar *) property_name);
660 if (prop_string) {
661 len = xmlStrlen (prop_string);
662 str = (gchar *) prop_string;
663 GST_TRACE ("ratio: %s, len %d", str, len);
664
665 /* read ":" */
666 pos = strcspn (str, ":");
667 if (pos >= len) {
668 GST_TRACE ("pos %d >= len %d", pos, len);
669 goto error;
670 }
671 /* search for negative sign */
672 if (strstr (str, "-") != NULL) {
673 goto error;
674 }
675 /* read num */
676 if (pos != 0) {
677 if (sscanf (str, "%u", &num) != 1) {
678 goto error;
679 }
680 }
681 /* read den */
682 if (pos < (len - 1)) {
683 if (sscanf (str + pos + 1, "%u", &den) != 1) {
684 goto error;
685 }
686 }
687 /* malloc return data structure */
688 *property_value = g_new0 (GstXMLRatio, 1);
689 exists = TRUE;
690 (*property_value)->num = num;
691 (*property_value)->den = den;
692 xmlFree (prop_string);
693 GST_LOG (" - %s: %u:%u", property_name, num, den);
694 }
695
696 return exists;
697
698 error:
699 GST_WARNING ("failed to parse property %s from xml string %s", property_name,
700 prop_string);
701 xmlFree (prop_string);
702 return FALSE;
703 }
704
705 gboolean
706 gst_xml_helper_get_prop_framerate (xmlNode * a_node,
707 const gchar * property_name, GstXMLFrameRate ** property_value)
708 {
709 xmlChar *prop_string;
710 guint num = 0, den = 1;
711 guint len, pos;
712 gchar *str;
713 gboolean exists = FALSE;
714
715 prop_string = xmlGetProp (a_node, (const xmlChar *) property_name);
716 if (prop_string) {
717 len = xmlStrlen (prop_string);
718 str = (gchar *) prop_string;
719 GST_TRACE ("framerate: %s, len %d", str, len);
720
721 /* search for negative sign */
722 if (strstr (str, "-") != NULL) {
723 goto error;
724 }
725
726 /* read "/" if available */
727 pos = strcspn (str, "/");
728 /* read num */
729 if (pos != 0) {
730 if (sscanf (str, "%u", &num) != 1) {
731 goto error;
732 }
733 }
734 /* read den (if available) */
735 if (pos < (len - 1)) {
736 if (sscanf (str + pos + 1, "%u", &den) != 1) {
737 goto error;
738 }
739 }
740 /* alloc return data structure */
741 *property_value = g_new0 (GstXMLFrameRate, 1);
742 exists = TRUE;
743 (*property_value)->num = num;
744 (*property_value)->den = den;
745 xmlFree (prop_string);
746 if (den == 1)
747 GST_LOG (" - %s: %u", property_name, num);
748 else
749 GST_LOG (" - %s: %u/%u", property_name, num, den);
750 }
751
752 return exists;
753
754 error:
755 GST_WARNING ("failed to parse property %s from xml string %s", property_name,
756 prop_string);
757 xmlFree (prop_string);
758 return FALSE;
759 }
760
761 gboolean
762 gst_xml_helper_get_prop_cond_uint (xmlNode * a_node,
763 const gchar * property_name, GstXMLConditionalUintType ** property_value)
764 {
765 xmlChar *prop_string;
766 gchar *str;
767 gboolean flag;
768 guint val;
769 gboolean exists = FALSE;
770
771 prop_string = xmlGetProp (a_node, (const xmlChar *) property_name);
772 if (prop_string) {
773 str = (gchar *) prop_string;
774 GST_TRACE ("conditional uint: %s", str);
775
776 if (strcmp (str, "false") == 0) {
777 flag = FALSE;
778 val = 0;
779 } else if (strcmp (str, "true") == 0) {
780 flag = TRUE;
781 val = 0;
782 } else {
783 flag = TRUE;
784 if (sscanf (str, "%u", &val) != 1 || strstr (str, "-") != NULL)
785 goto error;
786 }
787
788 /* alloc return data structure */
789 *property_value = g_new0 (GstXMLConditionalUintType, 1);
790 exists = TRUE;
791 (*property_value)->flag = flag;
792 (*property_value)->value = val;
793 xmlFree (prop_string);
794 GST_LOG (" - %s: flag=%s val=%u", property_name, flag ? "true" : "false",
795 val);
796 }
797
798 return exists;
799
800 error:
801 GST_WARNING ("failed to parse property %s from xml string %s", property_name,
802 prop_string);
803 xmlFree (prop_string);
804 return FALSE;
805 }
806
807 gboolean
808 gst_xml_helper_get_prop_dateTime (xmlNode * a_node,
809 const gchar * property_name, GstDateTime ** property_value)
810 {
811 xmlChar *prop_string;
812 gchar *str;
813 gint ret, pos;
814 gint year, month, day, hour, minute;
815 gdouble second;
816 gboolean exists = FALSE;
817 gfloat tzoffset = 0.0;
818 gint gmt_offset_hour = -99, gmt_offset_min = -99;
819
820 prop_string = xmlGetProp (a_node, (const xmlChar *) property_name);
821 if (prop_string) {
822 str = (gchar *) prop_string;
823 GST_TRACE ("dateTime: %s, len %d", str, xmlStrlen (prop_string));
824 /* parse year */
825 ret = sscanf (str, "%d", &year);
826 if (ret != 1 || year <= 0)
827 goto error;
828 pos = strcspn (str, "-");
829 str += (pos + 1);
830 GST_TRACE (" - year %d", year);
831 /* parse month */
832 ret = sscanf (str, "%d", &month);
833 if (ret != 1 || month <= 0)
834 goto error;
835 pos = strcspn (str, "-");
836 str += (pos + 1);
837 GST_TRACE (" - month %d", month);
838 /* parse day */
839 ret = sscanf (str, "%d", &day);
840 if (ret != 1 || day <= 0)
841 goto error;
842 pos = strcspn (str, "T");
843 str += (pos + 1);
844 GST_TRACE (" - day %d", day);
845 /* parse hour */
846 ret = sscanf (str, "%d", &hour);
847 if (ret != 1 || hour < 0)
848 goto error;
849 pos = strcspn (str, ":");
850 str += (pos + 1);
851 GST_TRACE (" - hour %d", hour);
852 /* parse minute */
853 ret = sscanf (str, "%d", &minute);
854 if (ret != 1 || minute < 0)
855 goto error;
856 pos = strcspn (str, ":");
857 str += (pos + 1);
858 GST_TRACE (" - minute %d", minute);
859 /* parse second */
860 ret = sscanf (str, "%lf", &second);
861 if (ret != 1 || second < 0)
862 goto error;
863 GST_TRACE (" - second %lf", second);
864
865 GST_LOG (" - %s: %4d/%02d/%02d %02d:%02d:%09.6lf", property_name,
866 year, month, day, hour, minute, second);
867
868 if (strrchr (str, '+') || strrchr (str, '-')) {
869 /* reuse some code from gst-plugins-base/gst-libs/gst/tag/gstxmptag.c */
870 gint gmt_offset = -1;
871 gchar *plus_pos = NULL;
872 gchar *neg_pos = NULL;
873 gchar *pos = NULL;
874
875 GST_LOG ("Checking for timezone information");
876
877 /* check if there is timezone info */
878 plus_pos = strrchr (str, '+');
879 neg_pos = strrchr (str, '-');
880 if (plus_pos)
881 pos = plus_pos + 1;
882 else if (neg_pos)
883 pos = neg_pos + 1;
884
885 if (pos && strlen (pos) >= 3) {
886 gint ret_tz;
887 if (pos[2] == ':')
888 ret_tz = sscanf (pos, "%d:%d", &gmt_offset_hour, &gmt_offset_min);
889 else
890 ret_tz = sscanf (pos, "%02d%02d", &gmt_offset_hour, &gmt_offset_min);
891
892 GST_DEBUG ("Parsing timezone: %s", pos);
893
894 if (ret_tz == 2) {
895 if (neg_pos != NULL && neg_pos + 1 == pos) {
896 gmt_offset_hour *= -1;
897 gmt_offset_min *= -1;
898 }
899 gmt_offset = gmt_offset_hour * 60 + gmt_offset_min;
900
901 tzoffset = gmt_offset / 60.0;
902
903 GST_LOG ("Timezone offset: %f (%d minutes)", tzoffset, gmt_offset);
904 } else
905 GST_WARNING ("Failed to parse timezone information");
906 }
907 }
908
909 exists = TRUE;
910 *property_value =
911 gst_date_time_new (tzoffset, year, month, day, hour, minute, second);
912 xmlFree (prop_string);
913 }
914
915 return exists;
916
917 error:
918 GST_WARNING ("failed to parse property %s from xml string %s", property_name,
919 prop_string);
920 xmlFree (prop_string);
921 return FALSE;
922 }
923
924 gboolean
925 gst_xml_helper_get_prop_duration (xmlNode * a_node,
926 const gchar * property_name, guint64 default_value,
927 guint64 * property_value)
928 {
929 xmlChar *prop_string;
930 gchar *str;
931 gboolean exists = FALSE;
932
933 *property_value = default_value;
934 prop_string = xmlGetProp (a_node, (const xmlChar *) property_name);
935 if (prop_string) {
936 str = (gchar *) prop_string;
937 if (!_mpd_helper_parse_duration (str, property_value))
938 goto error;
939 GST_LOG (" - %s: %" G_GUINT64_FORMAT, property_name, *property_value);
940 xmlFree (prop_string);
941 exists = TRUE;
942 }
943 return exists;
944
945 error:
946 xmlFree (prop_string);
947 return FALSE;
948 }
949
950 gboolean
951 gst_xml_helper_get_node_content (xmlNode * a_node, gchar ** content)
952 {
953 xmlChar *node_content = NULL;
954 gboolean exists = FALSE;
955
956 node_content = xmlNodeGetContent (a_node);
957 if (node_content) {
958 exists = TRUE;
959 *content = (gchar *) node_content;
960 GST_LOG (" - %s: %s", a_node->name, *content);
961 }
962
963 return exists;
964 }
965
966 gboolean
967 gst_xml_helper_get_node_as_string (xmlNode * a_node, gchar ** content)
968 {
969 gboolean exists = FALSE;
970 const char *txt_encoding;
971 xmlOutputBufferPtr out_buf;
972
973 txt_encoding = (const char *) a_node->doc->encoding;
974 out_buf = xmlAllocOutputBuffer (NULL);
975 g_assert (out_buf != NULL);
976 xmlNodeDumpOutput (out_buf, a_node->doc, a_node, 0, 0, txt_encoding);
977 (void) xmlOutputBufferFlush (out_buf);
978 #ifdef LIBXML2_NEW_BUFFER
979 if (xmlOutputBufferGetSize (out_buf) > 0) {
980 *content =
981 (gchar *) xmlStrndup (xmlOutputBufferGetContent (out_buf),
982 xmlOutputBufferGetSize (out_buf));
983 exists = TRUE;
984 }
985 #else
986 if (out_buf->conv && out_buf->conv->use > 0) {
987 *content =
988 (gchar *) xmlStrndup (out_buf->conv->content, out_buf->conv->use);
989 exists = TRUE;
990 } else if (out_buf->buffer && out_buf->buffer->use > 0) {
991 *content =
992 (gchar *) xmlStrndup (out_buf->buffer->content, out_buf->buffer->use);
993 exists = TRUE;
994 }
995 #endif // LIBXML2_NEW_BUFFER
996 (void) xmlOutputBufferClose (out_buf);
997
998 if (exists) {
999 GST_LOG (" - %s: %s", a_node->name, *content);
1000 }
1001 return exists;
1002 }
1003
1004 gchar *
1005 gst_xml_helper_get_node_namespace (xmlNode * a_node, const gchar * prefix)
1006 {
1007 xmlNs *curr_ns;
1008 gchar *namespace = NULL;
1009
1010 if (prefix == NULL) {
1011 /* return the default namespace */
1012 if (a_node->ns) {
1013 namespace = xmlMemStrdup ((const gchar *) a_node->ns->href);
1014 if (namespace) {
1015 GST_LOG (" - default namespace: %s", namespace);
1016 }
1017 }
1018 } else {
1019 /* look for the specified prefix in the namespace list */
1020 for (curr_ns = a_node->ns; curr_ns; curr_ns = curr_ns->next) {
1021 if (xmlStrcmp (curr_ns->prefix, (xmlChar *) prefix) == 0) {
1022 namespace = xmlMemStrdup ((const gchar *) curr_ns->href);
1023 if (namespace) {
1024 GST_LOG (" - %s namespace: %s", curr_ns->prefix, curr_ns->href);
1025 }
1026 }
1027 }
1028 }
1029
1030 return namespace;
1031 }
1032
1033 gboolean
1034 gst_xml_helper_get_prop_string_stripped (xmlNode * a_node,
1035 const gchar * property_name, gchar ** property_value)
1036 {
1037 gboolean ret;
1038 ret = gst_xml_helper_get_prop_string (a_node, property_name, property_value);
1039 if (ret)
1040 *property_value = g_strstrip (*property_value);
1041 return ret;
1042 }
1043
1044 gboolean
1045 gst_xml_helper_get_prop_string_no_whitespace (xmlNode * a_node,
1046 const gchar * property_name, gchar ** property_value)
1047 {
1048 return gst_xml_helper_get_prop_validated_string (a_node, property_name,
1049 property_value, _mpd_helper_validate_no_whitespace);
1050 }
1051
1052
1053 /* XML property set method */
1054
1055 void
1056 gst_xml_helper_set_prop_string (xmlNodePtr node, const gchar * name,
1057 gchar * value)
1058 {
1059 if (value)
1060 xmlSetProp (node, (xmlChar *) name, (xmlChar *) value);
1061 }
1062
1063 void
1064 gst_xml_helper_set_prop_boolean (xmlNodePtr node, const gchar * name,
1065 gboolean value)
1066 {
1067 if (value)
1068 xmlSetProp (node, (xmlChar *) name, (xmlChar *) "true");
1069 else
1070 xmlSetProp (node, (xmlChar *) name, (xmlChar *) "false");
1071 }
1072
1073 void
1074 gst_xml_helper_set_prop_int (xmlNodePtr node, const gchar * name, gint value)
1075 {
1076 gchar *text;
1077 text = g_strdup_printf ("%d", value);
1078 xmlSetProp (node, (xmlChar *) name, (xmlChar *) text);
1079 g_free (text);
1080 }
1081
1082 void
1083 gst_xml_helper_set_prop_uint (xmlNodePtr node, const gchar * name, guint value)
1084 {
1085 gchar *text;
1086 text = g_strdup_printf ("%d", value);
1087 xmlSetProp (node, (xmlChar *) name, (xmlChar *) text);
1088 g_free (text);
1089 }
1090
1091 void
1092 gst_xml_helper_set_prop_int64 (xmlNodePtr node, const gchar * name,
1093 gint64 value)
1094 {
1095 gchar *text;
1096 text = g_strdup_printf ("%" G_GINT64_FORMAT, value);
1097 xmlSetProp (node, (xmlChar *) name, (xmlChar *) text);
1098 g_free (text);
1099 }
1100
1101 void
1102 gst_xml_helper_set_prop_uint64 (xmlNodePtr node, const gchar * name,
1103 guint64 value)
1104 {
1105 gchar *text;
1106 text = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
1107 xmlSetProp (node, (xmlChar *) name, (xmlChar *) text);
1108 g_free (text);
1109 }
1110
1111 void
1112 gst_xml_helper_set_prop_double (xmlNodePtr node, const gchar * name,
1113 gdouble value)
1114 {
1115 gchar *text;
1116 text = g_strdup_printf ("%lf", value);
1117 xmlSetProp (node, (xmlChar *) name, (xmlChar *) text);
1118 g_free (text);
1119 }
1120
1121 void
1122 gst_xml_helper_set_prop_uint_vector_type (xmlNode * node, const gchar * name,
1123 guint * value, guint value_size)
1124 {
1125 int i;
1126 gchar *text = NULL;
1127 gchar *prev;
1128 gchar *temp;
1129
1130 for (i = 0; i < value_size; i++) {
1131 temp = g_strdup_printf ("%d", value[i]);
1132 prev = text;
1133 text = g_strjoin (" ", text, prev, NULL);
1134 g_free (prev);
1135 g_free (temp);
1136 }
1137
1138 if (text) {
1139 xmlSetProp (node, (xmlChar *) name, (xmlChar *) text);
1140 g_free (text);
1141 }
1142 }
1143
1144 void
1145 gst_xml_helper_set_prop_date_time (xmlNodePtr node, const gchar * name,
1146 GstDateTime * value)
1147 {
1148 gchar *text;
1149 if (value) {
1150 text = gst_date_time_to_iso8601_string (value);
1151 xmlSetProp (node, (xmlChar *) name, (xmlChar *) text);
1152 g_free (text);
1153 }
1154 }
1155
1156 void
1157 gst_xml_helper_set_prop_duration (xmlNode * node, const gchar * name,
1158 guint64 value)
1159 {
1160 gchar *text;
1161 gint years, months, days, hours, minutes, seconds, milliseconds;
1162 if (value) {
1163 years = (gint) (XML_HELPER_MS_TO_SEC (value) / (XML_HELPER_YEAR_TO_SEC));
1164 months =
1165 (gint) ((XML_HELPER_MS_TO_SEC (value) % XML_HELPER_YEAR_TO_SEC) /
1166 XML_HELPER_MONTH_TO_SEC);
1167 days =
1168 (gint) ((XML_HELPER_MS_TO_SEC (value) % XML_HELPER_MONTH_TO_SEC) /
1169 XML_HELPER_DAY_TO_SEC);
1170 hours =
1171 (gint) ((XML_HELPER_MS_TO_SEC (value) % XML_HELPER_DAY_TO_SEC) /
1172 XML_HELPER_HOUR_TO_SEC);
1173 minutes =
1174 (gint) ((XML_HELPER_MS_TO_SEC (value) % XML_HELPER_HOUR_TO_SEC) /
1175 XML_HELPER_MINUTE_TO_SEC);
1176 seconds = (gint) (XML_HELPER_MS_TO_SEC (value) % XML_HELPER_MINUTE_TO_SEC);
1177 milliseconds = value % 1000;
1178
1179 text =
1180 g_strdup_printf ("P%dY%dM%dDT%dH%dM%d.%dS", years, months, days, hours,
1181 minutes, seconds, milliseconds);
1182 GST_LOG ("duration %" G_GUINT64_FORMAT " -> %s", value, text);
1183 xmlSetProp (node, (xmlChar *) name, (xmlChar *) text);
1184 g_free (text);
1185 }
1186 }
1187
1188 void
1189 gst_xml_helper_set_prop_ratio (xmlNodePtr node, const gchar * name,
1190 GstXMLRatio * value)
1191 {
1192 gchar *text;
1193 if (value) {
1194 text = g_strdup_printf ("%d:%d", value->num, value->den);
1195 xmlSetProp (node, (xmlChar *) name, (xmlChar *) text);
1196 g_free (text);
1197 }
1198 }
1199
1200
1201 void
1202 gst_xml_helper_set_prop_framerate (xmlNodePtr node, const gchar * name,
1203 GstXMLFrameRate * value)
1204 {
1205 gchar *text;
1206 if (value) {
1207 text = g_strdup_printf ("%d/%d", value->num, value->den);
1208 xmlSetProp (node, (xmlChar *) name, (xmlChar *) text);
1209 g_free (text);
1210 }
1211 }
1212
1213 void
1214 gst_xml_helper_set_prop_range (xmlNodePtr node, const gchar * name,
1215 GstXMLRange * value)
1216 {
1217 gchar *text;
1218 if (value) {
1219 text =
1220 g_strdup_printf ("%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT,
1221 value->first_byte_pos, value->last_byte_pos);
1222 xmlSetProp (node, (xmlChar *) name, (xmlChar *) text);
1223 g_free (text);
1224 }
1225 }
1226
1227 void
1228 gst_xml_helper_set_prop_cond_uint (xmlNodePtr node, const gchar * name,
1229 GstXMLConditionalUintType * cond)
1230 {
1231 gchar *text;
1232 if (cond) {
1233 if (cond->flag)
1234 if (cond->value)
1235 text = g_strdup_printf ("%d", cond->value);
1236 else
1237 text = g_strdup_printf ("%s", "true");
1238 else
1239 text = g_strdup_printf ("%s", "false");
1240
1241 xmlSetProp (node, (xmlChar *) name, (xmlChar *) text);
1242 g_free (text);
1243 }
1244 }
1245
1246 void
1247 gst_xml_helper_set_content (xmlNodePtr node, gchar * content)
1248 {
1249 if (content)
1250 xmlNodeSetContent (node, (xmlChar *) content);
1251 }