forked from FFmpeg/FFmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashdec.c
2379 lines (2118 loc) · 79.8 KB
/
dashdec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Dynamic Adaptive Streaming over HTTP demux
* Copyright (c) 2017 [email protected] based on HLS demux
* Copyright (c) 2017 Steven Liu
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <libxml/parser.h>
#include "libavutil/bprint.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/opt.h"
#include "libavutil/time.h"
#include "libavutil/parseutils.h"
#include "internal.h"
#include "avio_internal.h"
#include "dash.h"
#define INITIAL_BUFFER_SIZE 32768
#define MAX_BPRINT_READ_SIZE (UINT_MAX - 1)
#define DEFAULT_MANIFEST_SIZE 8 * 1024
struct fragment {
int64_t url_offset;
int64_t size;
char *url;
};
/*
* reference to : ISO_IEC_23009-1-DASH-2012
* Section: 5.3.9.6.2
* Table: Table 17 — Semantics of SegmentTimeline element
* */
struct timeline {
/* starttime: Element or Attribute Name
* specifies the MPD start time, in @timescale units,
* the first Segment in the series starts relative to the beginning of the Period.
* The value of this attribute must be equal to or greater than the sum of the previous S
* element earliest presentation time and the sum of the contiguous Segment durations.
* If the value of the attribute is greater than what is expressed by the previous S element,
* it expresses discontinuities in the timeline.
* If not present then the value shall be assumed to be zero for the first S element
* and for the subsequent S elements, the value shall be assumed to be the sum of
* the previous S element's earliest presentation time and contiguous duration
* (i.e. previous S@starttime + @duration * (@repeat + 1)).
* */
int64_t starttime;
/* repeat: Element or Attribute Name
* specifies the repeat count of the number of following contiguous Segments with
* the same duration expressed by the value of @duration. This value is zero-based
* (e.g. a value of three means four Segments in the contiguous series).
* */
int64_t repeat;
/* duration: Element or Attribute Name
* specifies the Segment duration, in units of the value of the @timescale.
* */
int64_t duration;
};
/*
* Each playlist has its own demuxer. If it is currently active,
* it has an opened AVIOContext too, and potentially an AVPacket
* containing the next packet from this stream.
*/
struct representation {
char *url_template;
FFIOContext pb;
AVIOContext *input;
AVFormatContext *parent;
AVFormatContext *ctx;
int stream_index;
char *id;
char *lang;
int bandwidth;
AVRational framerate;
AVStream *assoc_stream; /* demuxer stream associated with this representation */
int n_fragments;
struct fragment **fragments; /* VOD list of fragment for profile */
int n_timelines;
struct timeline **timelines;
int64_t first_seq_no;
int64_t last_seq_no;
int64_t start_number; /* used in case when we have dynamic list of segment to know which segments are new one*/
int64_t fragment_duration;
int64_t fragment_timescale;
int64_t presentation_timeoffset;
int64_t cur_seq_no;
int64_t cur_seg_offset;
int64_t cur_seg_size;
struct fragment *cur_seg;
/* Currently active Media Initialization Section */
struct fragment *init_section;
uint8_t *init_sec_buf;
uint32_t init_sec_buf_size;
uint32_t init_sec_data_len;
uint32_t init_sec_buf_read_offset;
int64_t cur_timestamp;
int is_restart_needed;
};
typedef struct DASHContext {
const AVClass *class;
char *base_url;
int n_videos;
struct representation **videos;
int n_audios;
struct representation **audios;
int n_subtitles;
struct representation **subtitles;
/* MediaPresentationDescription Attribute */
uint64_t media_presentation_duration;
uint64_t suggested_presentation_delay;
uint64_t availability_start_time;
uint64_t availability_end_time;
uint64_t publish_time;
uint64_t minimum_update_period;
uint64_t time_shift_buffer_depth;
uint64_t min_buffer_time;
/* Period Attribute */
uint64_t period_duration;
uint64_t period_start;
/* AdaptationSet Attribute */
char *adaptionset_lang;
int is_live;
AVIOInterruptCB *interrupt_callback;
char *allowed_extensions;
AVDictionary *avio_opts;
int max_url_size;
/* Flags for init section*/
int is_init_section_common_video;
int is_init_section_common_audio;
int is_init_section_common_subtitle;
} DASHContext;
static int ishttp(char *url)
{
const char *proto_name = avio_find_protocol_name(url);
return proto_name && av_strstart(proto_name, "http", NULL);
}
static int aligned(int val)
{
return ((val + 0x3F) >> 6) << 6;
}
static uint64_t get_current_time_in_sec(void)
{
return av_gettime() / 1000000;
}
static uint64_t get_utc_date_time_insec(AVFormatContext *s, const char *datetime)
{
struct tm timeinfo;
int year = 0;
int month = 0;
int day = 0;
int hour = 0;
int minute = 0;
int ret = 0;
float second = 0.0;
/* ISO-8601 date parser */
if (!datetime)
return 0;
ret = sscanf(datetime, "%d-%d-%dT%d:%d:%fZ", &year, &month, &day, &hour, &minute, &second);
/* year, month, day, hour, minute, second 6 arguments */
if (ret != 6) {
av_log(s, AV_LOG_WARNING, "get_utc_date_time_insec get a wrong time format\n");
}
timeinfo.tm_year = year - 1900;
timeinfo.tm_mon = month - 1;
timeinfo.tm_mday = day;
timeinfo.tm_hour = hour;
timeinfo.tm_min = minute;
timeinfo.tm_sec = (int)second;
return av_timegm(&timeinfo);
}
static uint32_t get_duration_insec(AVFormatContext *s, const char *duration)
{
/* ISO-8601 duration parser */
uint32_t days = 0;
uint32_t hours = 0;
uint32_t mins = 0;
uint32_t secs = 0;
int size = 0;
float value = 0;
char type = '\0';
const char *ptr = duration;
while (*ptr) {
if (*ptr == 'P' || *ptr == 'T') {
ptr++;
continue;
}
if (sscanf(ptr, "%f%c%n", &value, &type, &size) != 2) {
av_log(s, AV_LOG_WARNING, "get_duration_insec get a wrong time format\n");
return 0; /* parser error */
}
switch (type) {
case 'D':
days = (uint32_t)value;
break;
case 'H':
hours = (uint32_t)value;
break;
case 'M':
mins = (uint32_t)value;
break;
case 'S':
secs = (uint32_t)value;
break;
default:
// handle invalid type
break;
}
ptr += size;
}
return ((days * 24 + hours) * 60 + mins) * 60 + secs;
}
static int64_t get_segment_start_time_based_on_timeline(struct representation *pls, int64_t cur_seq_no)
{
int64_t start_time = 0;
int64_t i = 0;
int64_t j = 0;
int64_t num = 0;
if (pls->n_timelines) {
for (i = 0; i < pls->n_timelines; i++) {
if (pls->timelines[i]->starttime > 0) {
start_time = pls->timelines[i]->starttime;
}
if (num == cur_seq_no)
goto finish;
start_time += pls->timelines[i]->duration;
if (pls->timelines[i]->repeat == -1) {
start_time = pls->timelines[i]->duration * cur_seq_no;
goto finish;
}
for (j = 0; j < pls->timelines[i]->repeat; j++) {
num++;
if (num == cur_seq_no)
goto finish;
start_time += pls->timelines[i]->duration;
}
num++;
}
}
finish:
return start_time;
}
static int64_t calc_next_seg_no_from_timelines(struct representation *pls, int64_t cur_time)
{
int64_t i = 0;
int64_t j = 0;
int64_t num = 0;
int64_t start_time = 0;
for (i = 0; i < pls->n_timelines; i++) {
if (pls->timelines[i]->starttime > 0) {
start_time = pls->timelines[i]->starttime;
}
if (start_time > cur_time)
goto finish;
start_time += pls->timelines[i]->duration;
for (j = 0; j < pls->timelines[i]->repeat; j++) {
num++;
if (start_time > cur_time)
goto finish;
start_time += pls->timelines[i]->duration;
}
num++;
}
return -1;
finish:
return num;
}
static void free_fragment(struct fragment **seg)
{
if (!(*seg)) {
return;
}
av_freep(&(*seg)->url);
av_freep(seg);
}
static void free_fragment_list(struct representation *pls)
{
int i;
for (i = 0; i < pls->n_fragments; i++) {
free_fragment(&pls->fragments[i]);
}
av_freep(&pls->fragments);
pls->n_fragments = 0;
}
static void free_timelines_list(struct representation *pls)
{
int i;
for (i = 0; i < pls->n_timelines; i++) {
av_freep(&pls->timelines[i]);
}
av_freep(&pls->timelines);
pls->n_timelines = 0;
}
static void free_representation(struct representation *pls)
{
free_fragment_list(pls);
free_timelines_list(pls);
free_fragment(&pls->cur_seg);
free_fragment(&pls->init_section);
av_freep(&pls->init_sec_buf);
av_freep(&pls->pb.pub.buffer);
ff_format_io_close(pls->parent, &pls->input);
if (pls->ctx) {
pls->ctx->pb = NULL;
avformat_close_input(&pls->ctx);
}
av_freep(&pls->url_template);
av_freep(&pls->lang);
av_freep(&pls->id);
av_freep(&pls);
}
static void free_video_list(DASHContext *c)
{
int i;
for (i = 0; i < c->n_videos; i++) {
struct representation *pls = c->videos[i];
free_representation(pls);
}
av_freep(&c->videos);
c->n_videos = 0;
}
static void free_audio_list(DASHContext *c)
{
int i;
for (i = 0; i < c->n_audios; i++) {
struct representation *pls = c->audios[i];
free_representation(pls);
}
av_freep(&c->audios);
c->n_audios = 0;
}
static void free_subtitle_list(DASHContext *c)
{
int i;
for (i = 0; i < c->n_subtitles; i++) {
struct representation *pls = c->subtitles[i];
free_representation(pls);
}
av_freep(&c->subtitles);
c->n_subtitles = 0;
}
static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
AVDictionary **opts, AVDictionary *opts2, int *is_http)
{
DASHContext *c = s->priv_data;
AVDictionary *tmp = NULL;
const char *proto_name = NULL;
int ret;
if (av_strstart(url, "crypto", NULL)) {
if (url[6] == '+' || url[6] == ':')
proto_name = avio_find_protocol_name(url + 7);
}
if (!proto_name)
proto_name = avio_find_protocol_name(url);
if (!proto_name)
return AVERROR_INVALIDDATA;
// only http(s) & file are allowed
if (av_strstart(proto_name, "file", NULL)) {
if (strcmp(c->allowed_extensions, "ALL") && !av_match_ext(url, c->allowed_extensions)) {
av_log(s, AV_LOG_ERROR,
"Filename extension of \'%s\' is not a common multimedia extension, blocked for security reasons.\n"
"If you wish to override this adjust allowed_extensions, you can set it to \'ALL\' to allow all\n",
url);
return AVERROR_INVALIDDATA;
}
} else if (av_strstart(proto_name, "http", NULL)) {
;
} else
return AVERROR_INVALIDDATA;
if (!strncmp(proto_name, url, strlen(proto_name)) && url[strlen(proto_name)] == ':')
;
else if (av_strstart(url, "crypto", NULL) && !strncmp(proto_name, url + 7, strlen(proto_name)) && url[7 + strlen(proto_name)] == ':')
;
else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5))
return AVERROR_INVALIDDATA;
av_freep(pb);
av_dict_copy(&tmp, *opts, 0);
av_dict_copy(&tmp, opts2, 0);
ret = avio_open2(pb, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp);
if (ret >= 0) {
// update cookies on http response with setcookies.
char *new_cookies = NULL;
if (!(s->flags & AVFMT_FLAG_CUSTOM_IO))
av_opt_get(*pb, "cookies", AV_OPT_SEARCH_CHILDREN, (uint8_t**)&new_cookies);
if (new_cookies) {
av_dict_set(opts, "cookies", new_cookies, AV_DICT_DONT_STRDUP_VAL);
}
}
av_dict_free(&tmp);
if (is_http)
*is_http = av_strstart(proto_name, "http", NULL);
return ret;
}
static char *get_content_url(xmlNodePtr *baseurl_nodes,
int n_baseurl_nodes,
int max_url_size,
char *rep_id_val,
char *rep_bandwidth_val,
char *val)
{
int i;
char *text;
char *url = NULL;
char *tmp_str = av_mallocz(max_url_size);
if (!tmp_str)
return NULL;
for (i = 0; i < n_baseurl_nodes; ++i) {
if (baseurl_nodes[i] &&
baseurl_nodes[i]->children &&
baseurl_nodes[i]->children->type == XML_TEXT_NODE) {
text = xmlNodeGetContent(baseurl_nodes[i]->children);
if (text) {
memset(tmp_str, 0, max_url_size);
ff_make_absolute_url(tmp_str, max_url_size, "", text);
xmlFree(text);
}
}
}
if (val)
ff_make_absolute_url(tmp_str, max_url_size, tmp_str, val);
if (rep_id_val) {
url = av_strireplace(tmp_str, "$RepresentationID$", rep_id_val);
if (!url) {
goto end;
}
av_strlcpy(tmp_str, url, max_url_size);
}
if (rep_bandwidth_val && tmp_str[0] != '\0') {
// free any previously assigned url before reassigning
av_free(url);
url = av_strireplace(tmp_str, "$Bandwidth$", rep_bandwidth_val);
if (!url) {
goto end;
}
}
end:
av_free(tmp_str);
return url;
}
static char *get_val_from_nodes_tab(xmlNodePtr *nodes, const int n_nodes, const char *attrname)
{
int i;
char *val;
for (i = 0; i < n_nodes; ++i) {
if (nodes[i]) {
val = xmlGetProp(nodes[i], attrname);
if (val)
return val;
}
}
return NULL;
}
static xmlNodePtr find_child_node_by_name(xmlNodePtr rootnode, const char *nodename)
{
xmlNodePtr node = rootnode;
if (!node) {
return NULL;
}
node = xmlFirstElementChild(node);
while (node) {
if (!av_strcasecmp(node->name, nodename)) {
return node;
}
node = xmlNextElementSibling(node);
}
return NULL;
}
static enum AVMediaType get_content_type(xmlNodePtr node)
{
enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
int i = 0;
const char *attr;
char *val = NULL;
if (node) {
for (i = 0; i < 2; i++) {
attr = i ? "mimeType" : "contentType";
val = xmlGetProp(node, attr);
if (val) {
if (av_stristr(val, "video")) {
type = AVMEDIA_TYPE_VIDEO;
} else if (av_stristr(val, "audio")) {
type = AVMEDIA_TYPE_AUDIO;
} else if (av_stristr(val, "text")) {
type = AVMEDIA_TYPE_SUBTITLE;
}
xmlFree(val);
}
}
}
return type;
}
static struct fragment * get_Fragment(char *range)
{
struct fragment * seg = av_mallocz(sizeof(struct fragment));
if (!seg)
return NULL;
seg->size = -1;
if (range) {
char *str_end_offset;
char *str_offset = av_strtok(range, "-", &str_end_offset);
seg->url_offset = strtoll(str_offset, NULL, 10);
seg->size = strtoll(str_end_offset, NULL, 10) - seg->url_offset + 1;
}
return seg;
}
static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representation *rep,
xmlNodePtr fragmenturl_node,
xmlNodePtr *baseurl_nodes,
char *rep_id_val,
char *rep_bandwidth_val)
{
DASHContext *c = s->priv_data;
char *initialization_val = NULL;
char *media_val = NULL;
char *range_val = NULL;
int max_url_size = c ? c->max_url_size: MAX_URL_SIZE;
int err;
if (!av_strcasecmp(fragmenturl_node->name, "Initialization")) {
initialization_val = xmlGetProp(fragmenturl_node, "sourceURL");
range_val = xmlGetProp(fragmenturl_node, "range");
if (initialization_val || range_val) {
free_fragment(&rep->init_section);
rep->init_section = get_Fragment(range_val);
xmlFree(range_val);
if (!rep->init_section) {
xmlFree(initialization_val);
return AVERROR(ENOMEM);
}
rep->init_section->url = get_content_url(baseurl_nodes, 4,
max_url_size,
rep_id_val,
rep_bandwidth_val,
initialization_val);
xmlFree(initialization_val);
if (!rep->init_section->url) {
av_freep(&rep->init_section);
return AVERROR(ENOMEM);
}
}
} else if (!av_strcasecmp(fragmenturl_node->name, "SegmentURL")) {
media_val = xmlGetProp(fragmenturl_node, "media");
range_val = xmlGetProp(fragmenturl_node, "mediaRange");
if (media_val || range_val) {
struct fragment *seg = get_Fragment(range_val);
xmlFree(range_val);
if (!seg) {
xmlFree(media_val);
return AVERROR(ENOMEM);
}
seg->url = get_content_url(baseurl_nodes, 4,
max_url_size,
rep_id_val,
rep_bandwidth_val,
media_val);
xmlFree(media_val);
if (!seg->url) {
av_free(seg);
return AVERROR(ENOMEM);
}
err = av_dynarray_add_nofree(&rep->fragments, &rep->n_fragments, seg);
if (err < 0) {
free_fragment(&seg);
return err;
}
}
}
return 0;
}
static int parse_manifest_segmenttimeline(AVFormatContext *s, struct representation *rep,
xmlNodePtr fragment_timeline_node)
{
xmlAttrPtr attr = NULL;
char *val = NULL;
int err;
if (!av_strcasecmp(fragment_timeline_node->name, "S")) {
struct timeline *tml = av_mallocz(sizeof(struct timeline));
if (!tml) {
return AVERROR(ENOMEM);
}
attr = fragment_timeline_node->properties;
while (attr) {
val = xmlGetProp(fragment_timeline_node, attr->name);
if (!val) {
av_log(s, AV_LOG_WARNING, "parse_manifest_segmenttimeline attr->name = %s val is NULL\n", attr->name);
continue;
}
if (!av_strcasecmp(attr->name, "t")) {
tml->starttime = (int64_t)strtoll(val, NULL, 10);
} else if (!av_strcasecmp(attr->name, "r")) {
tml->repeat =(int64_t) strtoll(val, NULL, 10);
} else if (!av_strcasecmp(attr->name, "d")) {
tml->duration = (int64_t)strtoll(val, NULL, 10);
}
attr = attr->next;
xmlFree(val);
}
err = av_dynarray_add_nofree(&rep->timelines, &rep->n_timelines, tml);
if (err < 0) {
av_free(tml);
return err;
}
}
return 0;
}
static int resolve_content_path(AVFormatContext *s, const char *url, int *max_url_size, xmlNodePtr *baseurl_nodes, int n_baseurl_nodes)
{
char *tmp_str = NULL;
char *path = NULL;
char *mpdName = NULL;
xmlNodePtr node = NULL;
char *baseurl = NULL;
char *root_url = NULL;
char *text = NULL;
char *tmp = NULL;
int isRootHttp = 0;
char token ='/';
int start = 0;
int rootId = 0;
int updated = 0;
int size = 0;
int i;
int tmp_max_url_size = strlen(url);
for (i = n_baseurl_nodes-1; i >= 0 ; i--) {
text = xmlNodeGetContent(baseurl_nodes[i]);
if (!text)
continue;
tmp_max_url_size += strlen(text);
if (ishttp(text)) {
xmlFree(text);
break;
}
xmlFree(text);
}
tmp_max_url_size = aligned(tmp_max_url_size);
text = av_mallocz(tmp_max_url_size);
if (!text) {
updated = AVERROR(ENOMEM);
goto end;
}
av_strlcpy(text, url, strlen(url)+1);
tmp = text;
while (mpdName = av_strtok(tmp, "/", &tmp)) {
size = strlen(mpdName);
}
av_free(text);
path = av_mallocz(tmp_max_url_size);
tmp_str = av_mallocz(tmp_max_url_size);
if (!tmp_str || !path) {
updated = AVERROR(ENOMEM);
goto end;
}
av_strlcpy (path, url, strlen(url) - size + 1);
for (rootId = n_baseurl_nodes - 1; rootId > 0; rootId --) {
if (!(node = baseurl_nodes[rootId])) {
continue;
}
text = xmlNodeGetContent(node);
if (ishttp(text)) {
xmlFree(text);
break;
}
xmlFree(text);
}
node = baseurl_nodes[rootId];
baseurl = xmlNodeGetContent(node);
root_url = (av_strcasecmp(baseurl, "")) ? baseurl : path;
if (node) {
xmlNodeSetContent(node, root_url);
updated = 1;
}
size = strlen(root_url);
isRootHttp = ishttp(root_url);
if (size > 0 && root_url[size - 1] != token) {
av_strlcat(root_url, "/", size + 2);
size += 2;
}
for (i = 0; i < n_baseurl_nodes; ++i) {
if (i == rootId) {
continue;
}
text = xmlNodeGetContent(baseurl_nodes[i]);
if (text && !av_strstart(text, "/", NULL)) {
memset(tmp_str, 0, strlen(tmp_str));
if (!ishttp(text) && isRootHttp) {
av_strlcpy(tmp_str, root_url, size + 1);
}
start = (text[0] == token);
if (start && av_stristr(tmp_str, text)) {
char *p = tmp_str;
if (!av_strncasecmp(tmp_str, "http://", 7)) {
p += 7;
} else if (!av_strncasecmp(tmp_str, "https://", 8)) {
p += 8;
}
p = strchr(p, '/');
memset(p + 1, 0, strlen(p));
}
av_strlcat(tmp_str, text + start, tmp_max_url_size);
xmlNodeSetContent(baseurl_nodes[i], tmp_str);
updated = 1;
xmlFree(text);
}
}
end:
if (tmp_max_url_size > *max_url_size) {
*max_url_size = tmp_max_url_size;
}
av_free(path);
av_free(tmp_str);
xmlFree(baseurl);
return updated;
}
static int parse_manifest_representation(AVFormatContext *s, const char *url,
xmlNodePtr node,
xmlNodePtr adaptionset_node,
xmlNodePtr mpd_baseurl_node,
xmlNodePtr period_baseurl_node,
xmlNodePtr period_segmenttemplate_node,
xmlNodePtr period_segmentlist_node,
xmlNodePtr fragment_template_node,
xmlNodePtr content_component_node,
xmlNodePtr adaptionset_baseurl_node,
xmlNodePtr adaptionset_segmentlist_node,
xmlNodePtr adaptionset_supplementalproperty_node)
{
int32_t ret = 0;
DASHContext *c = s->priv_data;
struct representation *rep = NULL;
struct fragment *seg = NULL;
xmlNodePtr representation_segmenttemplate_node = NULL;
xmlNodePtr representation_baseurl_node = NULL;
xmlNodePtr representation_segmentlist_node = NULL;
xmlNodePtr segmentlists_tab[3];
xmlNodePtr fragment_timeline_node = NULL;
xmlNodePtr fragment_templates_tab[5];
char *val = NULL;
xmlNodePtr baseurl_nodes[4];
xmlNodePtr representation_node = node;
char *rep_bandwidth_val;
enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
// try get information from representation
if (type == AVMEDIA_TYPE_UNKNOWN)
type = get_content_type(representation_node);
// try get information from contentComponen
if (type == AVMEDIA_TYPE_UNKNOWN)
type = get_content_type(content_component_node);
// try get information from adaption set
if (type == AVMEDIA_TYPE_UNKNOWN)
type = get_content_type(adaptionset_node);
if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO &&
type != AVMEDIA_TYPE_SUBTITLE) {
av_log(s, AV_LOG_VERBOSE, "Parsing '%s' - skipp not supported representation type\n", url);
return 0;
}
// convert selected representation to our internal struct
rep = av_mallocz(sizeof(struct representation));
if (!rep)
return AVERROR(ENOMEM);
if (c->adaptionset_lang) {
rep->lang = av_strdup(c->adaptionset_lang);
if (!rep->lang) {
av_log(s, AV_LOG_ERROR, "alloc language memory failure\n");
av_freep(&rep);
return AVERROR(ENOMEM);
}
}
rep->parent = s;
representation_segmenttemplate_node = find_child_node_by_name(representation_node, "SegmentTemplate");
representation_baseurl_node = find_child_node_by_name(representation_node, "BaseURL");
representation_segmentlist_node = find_child_node_by_name(representation_node, "SegmentList");
rep_bandwidth_val = xmlGetProp(representation_node, "bandwidth");
val = xmlGetProp(representation_node, "id");
if (val) {
rep->id = av_strdup(val);
xmlFree(val);
if (!rep->id)
goto enomem;
}
baseurl_nodes[0] = mpd_baseurl_node;
baseurl_nodes[1] = period_baseurl_node;
baseurl_nodes[2] = adaptionset_baseurl_node;
baseurl_nodes[3] = representation_baseurl_node;
ret = resolve_content_path(s, url, &c->max_url_size, baseurl_nodes, 4);
c->max_url_size = aligned(c->max_url_size
+ (rep->id ? strlen(rep->id) : 0)
+ (rep_bandwidth_val ? strlen(rep_bandwidth_val) : 0));
if (ret == AVERROR(ENOMEM) || ret == 0)
goto free;
if (representation_segmenttemplate_node || fragment_template_node || period_segmenttemplate_node) {
fragment_timeline_node = NULL;
fragment_templates_tab[0] = representation_segmenttemplate_node;
fragment_templates_tab[1] = adaptionset_segmentlist_node;
fragment_templates_tab[2] = fragment_template_node;
fragment_templates_tab[3] = period_segmenttemplate_node;
fragment_templates_tab[4] = period_segmentlist_node;
val = get_val_from_nodes_tab(fragment_templates_tab, 4, "initialization");
if (val) {
rep->init_section = av_mallocz(sizeof(struct fragment));
if (!rep->init_section) {
xmlFree(val);
goto enomem;
}
c->max_url_size = aligned(c->max_url_size + strlen(val));
rep->init_section->url = get_content_url(baseurl_nodes, 4,
c->max_url_size, rep->id,
rep_bandwidth_val, val);
xmlFree(val);
if (!rep->init_section->url)
goto enomem;
rep->init_section->size = -1;
}
val = get_val_from_nodes_tab(fragment_templates_tab, 4, "media");
if (val) {
c->max_url_size = aligned(c->max_url_size + strlen(val));
rep->url_template = get_content_url(baseurl_nodes, 4,
c->max_url_size, rep->id,
rep_bandwidth_val, val);
xmlFree(val);
}
val = get_val_from_nodes_tab(fragment_templates_tab, 4, "presentationTimeOffset");
if (val) {
rep->presentation_timeoffset = (int64_t) strtoll(val, NULL, 10);
av_log(s, AV_LOG_TRACE, "rep->presentation_timeoffset = [%"PRId64"]\n", rep->presentation_timeoffset);
xmlFree(val);
}
val = get_val_from_nodes_tab(fragment_templates_tab, 4, "duration");
if (val) {
rep->fragment_duration = (int64_t) strtoll(val, NULL, 10);
av_log(s, AV_LOG_TRACE, "rep->fragment_duration = [%"PRId64"]\n", rep->fragment_duration);
xmlFree(val);
}
val = get_val_from_nodes_tab(fragment_templates_tab, 4, "timescale");
if (val) {
rep->fragment_timescale = (int64_t) strtoll(val, NULL, 10);
av_log(s, AV_LOG_TRACE, "rep->fragment_timescale = [%"PRId64"]\n", rep->fragment_timescale);
xmlFree(val);
}
val = get_val_from_nodes_tab(fragment_templates_tab, 4, "startNumber");
if (val) {
rep->start_number = rep->first_seq_no = (int64_t) strtoll(val, NULL, 10);
av_log(s, AV_LOG_TRACE, "rep->first_seq_no = [%"PRId64"]\n", rep->first_seq_no);
xmlFree(val);
}
if (adaptionset_supplementalproperty_node) {
if (!av_strcasecmp(xmlGetProp(adaptionset_supplementalproperty_node,"schemeIdUri"), "http://dashif.org/guidelines/last-segment-number")) {
val = xmlGetProp(adaptionset_supplementalproperty_node,"value");
if (!val) {
av_log(s, AV_LOG_ERROR, "Missing value attribute in adaptionset_supplementalproperty_node\n");
} else {
rep->last_seq_no =(int64_t) strtoll(val, NULL, 10) - 1;
xmlFree(val);
}
}
}
fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
if (!fragment_timeline_node)
fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
if (!fragment_timeline_node)
fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
if (!fragment_timeline_node)
fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
if (fragment_timeline_node) {
fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
while (fragment_timeline_node) {
ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
if (ret < 0)
goto free;
fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
}
}
} else if (representation_baseurl_node && !representation_segmentlist_node) {
seg = av_mallocz(sizeof(struct fragment));
if (!seg)
goto enomem;
ret = av_dynarray_add_nofree(&rep->fragments, &rep->n_fragments, seg);
if (ret < 0) {
av_free(seg);
goto free;
}
seg->url = get_content_url(baseurl_nodes, 4, c->max_url_size,
rep->id, rep_bandwidth_val, NULL);
if (!seg->url)
goto enomem;
seg->size = -1;
} else if (representation_segmentlist_node) {