FFmpeg
Loading...
Searching...
No Matches
dashdec.c
Go to the documentation of this file.
1/*
2 * Dynamic Adaptive Streaming over HTTP demux
3 * Copyright (c) 2017 samsamsam@o2.pl based on HLS demux
4 * Copyright (c) 2017 Steven Liu
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22#include <libxml/parser.h>
23#include <time.h>
24#include "libavutil/avassert.h"
25#include "libavutil/bprint.h"
26#include "libavutil/mem.h"
27#include "libavutil/opt.h"
28#include "libavutil/time.h"
30#include "internal.h"
31#include "avio_internal.h"
32#include "dash.h"
33#include "demux.h"
34#include "url.h"
35
36#define INITIAL_BUFFER_SIZE 32768
37
43
44/*
45 * reference to : ISO_IEC_23009-1-DASH-2012
46 * Section: 5.3.9.6.2
47 * Table: Table 17 — Semantics of SegmentTimeline element
48 * */
49struct timeline {
50 /* starttime: Element or Attribute Name
51 * specifies the MPD start time, in @timescale units,
52 * the first Segment in the series starts relative to the beginning of the Period.
53 * The value of this attribute must be equal to or greater than the sum of the previous S
54 * element earliest presentation time and the sum of the contiguous Segment durations.
55 * If the value of the attribute is greater than what is expressed by the previous S element,
56 * it expresses discontinuities in the timeline.
57 * If not present then the value shall be assumed to be zero for the first S element
58 * and for the subsequent S elements, the value shall be assumed to be the sum of
59 * the previous S element's earliest presentation time and contiguous duration
60 * (i.e. previous S@starttime + @duration * (@repeat + 1)).
61 * */
63 /* repeat: Element or Attribute Name
64 * specifies the repeat count of the number of following contiguous Segments with
65 * the same duration expressed by the value of @duration. This value is zero-based
66 * (e.g. a value of three means four Segments in the contiguous series).
67 * */
69 /* duration: Element or Attribute Name
70 * specifies the Segment duration, in units of the value of the @timescale.
71 * */
73};
74
75/*
76 * Each playlist has its own demuxer. If it is currently active,
77 * it has an opened AVIOContext too, and potentially an AVPacket
78 * containing the next packet from this stream.
79 */
87
88 char *id;
89 char *lang;
92 char *codecs;
94 AVStream **assoc_stream; /* demuxer streams associated with this representation */
96
98 struct fragment **fragments; /* VOD list of fragment for profile */
99
102
105 int64_t start_number; /* used in case when we have dynamic list of segment to know which segments are new one*/
106
109
111
116 int n_open_failures; /* consecutive open_input failures since last good read */
117
118 /* Currently active Media Initialization Section */
120 uint8_t *init_sec_buf;
126};
127
171
172static int ishttp(const char *url)
173{
174 const char *proto_name = avio_find_protocol_name(url);
175 return proto_name && av_strstart(proto_name, "http", NULL);
176}
177
178static int add_url_size(int *url_size, uint64_t len)
179{
180 if (len > INT_MAX - 0x3F - *url_size)
181 return AVERROR(ERANGE);
182 *url_size = FFALIGN(*url_size + len, 64);
183 return 0;
184}
185
187{
188 return av_gettime() / 1000000;
189}
190
191static int64_t get_utc_date_time_insec(AVFormatContext *s, const char *datetime)
192{
193 struct tm timeinfo;
194 int year = 0;
195 int month = 0;
196 int day = 0;
197 int hour = 0;
198 int minute = 0;
199 int ret = 0;
200 float second = 0.0;
201
202 /* ISO-8601 date parser */
203 if (!datetime)
204 return 0;
205
206 ret = sscanf(datetime, "%d-%d-%dT%d:%d:%fZ", &year, &month, &day, &hour, &minute, &second);
207 /* year, month, day, hour, minute, second 6 arguments */
208 if (ret != 6) {
209 av_log(s, AV_LOG_WARNING, "get_utc_date_time_insec get a wrong time format\n");
210 }
211 timeinfo.tm_year = year - 1900;
212 timeinfo.tm_mon = month - 1;
213 timeinfo.tm_mday = day;
214 timeinfo.tm_hour = hour;
215 timeinfo.tm_min = minute;
216 timeinfo.tm_sec = (int)second;
217
218 return av_timegm(&timeinfo);
219}
220
221static uint32_t get_duration_insec(AVFormatContext *s, const char *duration)
222{
223 /* ISO-8601 duration parser */
224 uint32_t days = 0;
225 uint32_t hours = 0;
226 uint32_t mins = 0;
227 uint32_t secs = 0;
228 int size = 0;
229 float value = 0;
230 char type = '\0';
231 const char *ptr = duration;
232
233 while (*ptr) {
234 if (*ptr == 'P' || *ptr == 'T') {
235 ptr++;
236 continue;
237 }
238
239 if (sscanf(ptr, "%f%c%n", &value, &type, &size) != 2) {
240 av_log(s, AV_LOG_WARNING, "get_duration_insec get a wrong time format\n");
241 return 0; /* parser error */
242 }
243 switch (type) {
244 case 'D':
245 days = (uint32_t)value;
246 break;
247 case 'H':
248 hours = (uint32_t)value;
249 break;
250 case 'M':
251 mins = (uint32_t)value;
252 break;
253 case 'S':
254 secs = (uint32_t)value;
255 break;
256 default:
257 // handle invalid type
258 break;
259 }
260 ptr += size;
261 }
262 return ((days * 24 + hours) * 60 + mins) * 60 + secs;
263}
264
266{
268 int64_t i = 0;
269 int64_t j = 0;
270 int64_t num = 0;
271
272 cur_seq_no -= pls->first_seq_no;
273 if (pls->n_timelines) {
274 for (i = 0; i < pls->n_timelines; i++) {
275 if (pls->timelines[i]->starttime > 0) {
277 }
278 if (num == cur_seq_no)
279 goto finish;
280
281 start_time += pls->timelines[i]->duration;
282
283 if (pls->timelines[i]->repeat == -1) {
284 start_time = pls->timelines[i]->duration * cur_seq_no;
285 goto finish;
286 }
287
288 for (j = 0; j < pls->timelines[i]->repeat; j++) {
289 num++;
290 if (num == cur_seq_no)
291 goto finish;
292 start_time += pls->timelines[i]->duration;
293 }
294 num++;
295 }
296 }
297finish:
298 return start_time;
299}
300
302{
303 int64_t i = 0;
304 int64_t j = 0;
305 int64_t num = 0;
307
308 for (i = 0; i < pls->n_timelines; i++) {
309 if (pls->timelines[i]->starttime > 0) {
311 }
312 if (start_time > cur_time)
313 goto finish;
314
315 start_time += pls->timelines[i]->duration;
316 for (j = 0; j < pls->timelines[i]->repeat; j++) {
317 num++;
318 if (start_time > cur_time)
319 goto finish;
320 start_time += pls->timelines[i]->duration;
321 }
322 num++;
323 }
324
325 return -1;
326
327finish:
328 return num + pls->first_seq_no;
329}
330
331static void free_fragment(struct fragment **seg)
332{
333 if (!(*seg)) {
334 return;
335 }
336 av_freep(&(*seg)->url);
337 av_freep(seg);
338}
339
340static void free_fragment_list(struct representation *pls)
341{
342 int i;
343
344 for (i = 0; i < pls->n_fragments; i++) {
345 free_fragment(&pls->fragments[i]);
346 }
347 av_freep(&pls->fragments);
348 pls->n_fragments = 0;
349}
350
351static void free_timelines_list(struct representation *pls)
352{
353 int i;
354
355 for (i = 0; i < pls->n_timelines; i++) {
356 av_freep(&pls->timelines[i]);
357 }
358 av_freep(&pls->timelines);
359 pls->n_timelines = 0;
360}
361
362static void free_representation(struct representation *pls)
363{
366 free_fragment(&pls->cur_seg);
368 av_freep(&pls->init_sec_buf);
369 av_freep(&pls->pb.pub.buffer);
370 ff_format_io_close(pls->parent, &pls->input);
371 if (pls->ctx) {
372 pls->ctx->pb = NULL;
374 }
375
376 av_freep(&pls->assoc_stream);
377 av_freep(&pls->url_template);
378 av_freep(&pls->lang);
379 av_freep(&pls->dependencyid);
380 av_freep(&pls->codecs);
381 av_freep(&pls->id);
382 av_freep(&pls);
383}
384
386{
387 int i;
388 for (i = 0; i < c->n_videos; i++) {
389 struct representation *pls = c->videos[i];
391 }
392 av_freep(&c->videos);
393 c->n_videos = 0;
394}
395
397{
398 int i;
399 for (i = 0; i < c->n_audios; i++) {
400 struct representation *pls = c->audios[i];
402 }
403 av_freep(&c->audios);
404 c->n_audios = 0;
405}
406
408{
409 int i;
410 for (i = 0; i < c->n_subtitles; i++) {
411 struct representation *pls = c->subtitles[i];
413 }
414 av_freep(&c->subtitles);
415 c->n_subtitles = 0;
416}
417
418static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
419 AVDictionary **opts, AVDictionary *opts2, int *is_http)
420{
421 DASHContext *c = s->priv_data;
423 const char *proto_name = NULL;
424 int proto_name_len;
425 int ret;
426
427 if (av_strstart(url, "crypto", NULL)) {
428 if (url[6] == '+' || url[6] == ':')
429 proto_name = avio_find_protocol_name(url + 7);
430 }
431
432 if (!proto_name)
433 proto_name = avio_find_protocol_name(url);
434
435 if (!proto_name)
436 return AVERROR_INVALIDDATA;
437
438 proto_name_len = strlen(proto_name);
439 // only http(s) & file are allowed
440 if (av_strstart(proto_name, "file", NULL)) {
441 if (strcmp(c->allowed_extensions, "ALL") && !av_match_ext(url, c->allowed_extensions)) {
443 "Filename extension of \'%s\' is not a common multimedia extension, blocked for security reasons.\n"
444 "If you wish to override this adjust allowed_extensions, you can set it to \'ALL\' to allow all\n",
445 url);
446 return AVERROR_INVALIDDATA;
447 }
448 } else if (av_strstart(proto_name, "http", NULL)) {
449 ;
450 } else
451 return AVERROR_INVALIDDATA;
452
453 if (!strncmp(proto_name, url, proto_name_len) && url[proto_name_len] == ':')
454 ;
455 else if (av_strstart(url, "crypto", NULL) && !strncmp(proto_name, url + 7, proto_name_len) && url[7 + proto_name_len] == ':')
456 ;
457 else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5))
458 return AVERROR_INVALIDDATA;
459
460 av_freep(pb);
461 av_dict_copy(&tmp, *opts, 0);
462 av_dict_copy(&tmp, opts2, 0);
463 ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp);
464 if (ret >= 0) {
465 // update cookies on http response with setcookies.
466 char *new_cookies = NULL;
467
468 if (!(s->flags & AVFMT_FLAG_CUSTOM_IO))
469 av_opt_get(*pb, "cookies", AV_OPT_SEARCH_CHILDREN, (uint8_t**)&new_cookies);
470
471 if (new_cookies) {
472 av_dict_set(opts, "cookies", new_cookies, AV_DICT_DONT_STRDUP_VAL);
473 }
474
475 }
476
478
479 if (is_http)
480 *is_http = av_strstart(proto_name, "http", NULL);
481
482 return ret;
483}
484
485static char *get_content_url(xmlNodePtr *baseurl_nodes,
486 int n_baseurl_nodes,
487 int max_url_size,
488 char *rep_id_val,
489 char *rep_bandwidth_val,
490 char *val)
491{
492 int i;
493 char *text;
494 char *url = NULL;
495 char *tmp_str = av_mallocz(max_url_size);
496
497 if (!tmp_str)
498 return NULL;
499
500 for (i = 0; i < n_baseurl_nodes; ++i) {
501 if (baseurl_nodes[i] &&
502 baseurl_nodes[i]->children &&
503 baseurl_nodes[i]->children->type == XML_TEXT_NODE) {
504 text = xmlNodeGetContent(baseurl_nodes[i]->children);
505 if (text) {
506 memset(tmp_str, 0, max_url_size);
507 ff_make_absolute_url(tmp_str, max_url_size, "", text);
508 xmlFree(text);
509 }
510 }
511 }
512
513 if (val)
514 ff_make_absolute_url(tmp_str, max_url_size, tmp_str, val);
515
516 if (rep_id_val) {
517 url = av_strireplace(tmp_str, "$RepresentationID$", rep_id_val);
518 if (!url) {
519 goto end;
520 }
521 av_strlcpy(tmp_str, url, max_url_size);
522 }
523 if (rep_bandwidth_val && tmp_str[0] != '\0') {
524 // free any previously assigned url before reassigning
525 av_free(url);
526 url = av_strireplace(tmp_str, "$Bandwidth$", rep_bandwidth_val);
527 if (!url) {
528 goto end;
529 }
530 }
531end:
532 av_free(tmp_str);
533 return url;
534}
535
536static char *get_val_from_nodes_tab(xmlNodePtr *nodes, const int n_nodes, const char *attrname)
537{
538 int i;
539 char *val;
540
541 for (i = 0; i < n_nodes; ++i) {
542 if (nodes[i]) {
543 val = xmlGetProp(nodes[i], attrname);
544 if (val)
545 return val;
546 }
547 }
548
549 return NULL;
550}
551
552static xmlNodePtr find_child_node_by_name(xmlNodePtr rootnode, const char *nodename)
553{
554 xmlNodePtr node = rootnode;
555 if (!node) {
556 return NULL;
557 }
558
559 node = xmlFirstElementChild(node);
560 while (node) {
561 if (!av_strcasecmp(node->name, nodename)) {
562 return node;
563 }
564 node = xmlNextElementSibling(node);
565 }
566 return NULL;
567}
568
569static enum AVMediaType get_content_type(xmlNodePtr node)
570{
572 int i = 0;
573 const char *attr;
574 char *val = NULL;
575
576 if (node) {
577 for (i = 0; i < 2; i++) {
578 attr = i ? "mimeType" : "contentType";
579 val = xmlGetProp(node, attr);
580 if (val) {
581 if (av_stristr(val, "video")) {
583 } else if (av_stristr(val, "audio")) {
585 } else if (av_stristr(val, "text")) {
587 }
588 xmlFree(val);
589 }
590 }
591 }
592 return type;
593}
594
596{
597 struct fragment *seg = av_mallocz(sizeof(struct fragment));
598
599 if (!seg)
600 return NULL;
601
602 seg->size = -1;
603 if (range) {
604 char *str_end_offset;
605 char *str_offset = av_strtok(range, "-", &str_end_offset);
606 if (!str_offset || !str_end_offset) {
607 av_log(s, AV_LOG_WARNING, "%s will get invalid range\n", range);
608 av_freep(&seg);
609 return NULL;
610 }
611 seg->url_offset = strtoll(str_offset, NULL, 10);
612 seg->size = strtoll(str_end_offset, NULL, 10) - seg->url_offset + 1;
613 }
614
615 return seg;
616}
617
619 xmlNodePtr fragmenturl_node,
620 xmlNodePtr *baseurl_nodes,
621 char *rep_id_val,
622 char *rep_bandwidth_val)
623{
624 DASHContext *c = s->priv_data;
625 char *initialization_val = NULL;
626 char *media_val = NULL;
627 char *range_val = NULL;
628 int max_url_size = c ? c->max_url_size: MAX_URL_SIZE;
629 int err;
630
631 if (!av_strcasecmp(fragmenturl_node->name, "Initialization")) {
632 initialization_val = xmlGetProp(fragmenturl_node, "sourceURL");
633 range_val = xmlGetProp(fragmenturl_node, "range");
634 if (initialization_val || range_val) {
636 rep->init_section = get_fragment(s, range_val);
637 xmlFree(range_val);
638 if (!rep->init_section) {
639 xmlFree(initialization_val);
640 return AVERROR(ENOMEM);
641 }
642 rep->init_section->url = get_content_url(baseurl_nodes, 4,
643 max_url_size,
644 rep_id_val,
645 rep_bandwidth_val,
646 initialization_val);
647 xmlFree(initialization_val);
648 if (!rep->init_section->url) {
649 av_freep(&rep->init_section);
650 return AVERROR(ENOMEM);
651 }
652 }
653 } else if (!av_strcasecmp(fragmenturl_node->name, "SegmentURL")) {
654 media_val = xmlGetProp(fragmenturl_node, "media");
655 range_val = xmlGetProp(fragmenturl_node, "mediaRange");
656 if (media_val || range_val) {
657 struct fragment *seg = get_fragment(s, range_val);
658 xmlFree(range_val);
659 if (!seg) {
660 xmlFree(media_val);
661 return AVERROR(ENOMEM);
662 }
663 seg->url = get_content_url(baseurl_nodes, 4,
664 max_url_size,
665 rep_id_val,
666 rep_bandwidth_val,
667 media_val);
668 xmlFree(media_val);
669 if (!seg->url) {
670 av_free(seg);
671 return AVERROR(ENOMEM);
672 }
673 err = av_dynarray_add_nofree(&rep->fragments, &rep->n_fragments, seg);
674 if (err < 0) {
675 free_fragment(&seg);
676 return err;
677 }
678 }
679 }
680
681 return 0;
682}
683
685 xmlNodePtr fragment_timeline_node)
686{
687 xmlAttrPtr attr = NULL;
688 char *val = NULL;
689 int err;
690
691 if (!av_strcasecmp(fragment_timeline_node->name, "S")) {
692 struct timeline *tml = av_mallocz(sizeof(struct timeline));
693 if (!tml) {
694 return AVERROR(ENOMEM);
695 }
696 attr = fragment_timeline_node->properties;
697 while (attr) {
698 val = xmlGetProp(fragment_timeline_node, attr->name);
699
700 if (!val) {
701 av_free(tml);
702 return AVERROR(ENOMEM);
703 }
704
705 if (!av_strcasecmp(attr->name, "t")) {
706 tml->starttime = (int64_t)strtoll(val, NULL, 10);
707 } else if (!av_strcasecmp(attr->name, "r")) {
708 tml->repeat =(int64_t) strtoll(val, NULL, 10);
709 } else if (!av_strcasecmp(attr->name, "d")) {
710 tml->duration = (int64_t)strtoll(val, NULL, 10);
711 }
712 attr = attr->next;
713 xmlFree(val);
714 }
715 err = av_dynarray_add_nofree(&rep->timelines, &rep->n_timelines, tml);
716 if (err < 0) {
717 av_free(tml);
718 return err;
719 }
720 }
721
722 return 0;
723}
724
725static int resolve_content_path(AVFormatContext *s, const char *url, int *max_url_size, xmlNodePtr *baseurl_nodes, int n_baseurl_nodes)
726{
727 char *tmp_str = NULL;
728 char *path = NULL;
729 xmlNodePtr node = NULL;
730 char *baseurl = NULL;
731 char *root_url = NULL;
732 char *text = NULL;
733 char *tmp = NULL;
734 int isRootHttp = 0;
735 char token ='/';
736 int start = 0;
737 int rootId = 0;
738 int updated = 0;
739 int size = 0;
740 int i;
741 int tmp_max_url_size = 0;
742 uint64_t url_len = strlen(url);
743
744 for (i = n_baseurl_nodes-1; i >= 0 ; i--) {
745 text = xmlNodeGetContent(baseurl_nodes[i]);
746 if (!text)
747 continue;
748 url_len += strlen(text);
749 if (ishttp(text)) {
750 xmlFree(text);
751 break;
752 }
753 xmlFree(text);
754 }
755
756 updated = add_url_size(&tmp_max_url_size, url_len);
757 if (updated < 0)
758 goto end;
759 path = av_mallocz(tmp_max_url_size + 2);
760 tmp_str = av_mallocz(tmp_max_url_size + 2);
761 if (!tmp_str || !path) {
762 updated = AVERROR(ENOMEM);
763 goto end;
764 }
765
766 if (ishttp(url)) {
767 const char *colon = strchr(url, ':');
768 if (colon && colon[1] == '/' && colon[2] == '/') {
769 updated = ff_make_absolute_url(path, tmp_max_url_size + 2, url, ".");
770 if (updated < 0)
771 goto end;
772 }
773 }
774 for (rootId = n_baseurl_nodes - 1; rootId > 0; rootId --) {
775 if (!(node = baseurl_nodes[rootId])) {
776 continue;
777 }
778 text = xmlNodeGetContent(node);
779 if (!text) {
780 updated = AVERROR(ENOMEM);
781 goto end;
782 }
783 if (ishttp(text)) {
784 xmlFree(text);
785 break;
786 }
787 xmlFree(text);
788 }
789
790 node = baseurl_nodes[rootId];
791 baseurl = xmlNodeGetContent(node);
792 if (baseurl)
793 tmp = xmlRealloc(baseurl, xmlStrlen(baseurl) + 2);
794 if (!tmp) {
795 updated = AVERROR(ENOMEM);
796 goto end;
797 }
798 baseurl = tmp;
799 root_url = (av_strcasecmp(baseurl, "")) ? baseurl : path;
800 if (node) {
801 xmlChar *escaped = xmlEncodeSpecialChars(NULL, root_url);
802 if (!escaped) {
803 updated = AVERROR(ENOMEM);
804 goto end;
805 }
806 xmlNodeSetContent(node, escaped);
807 xmlFree(escaped);
808 updated = 1;
809 }
810
811 size = strlen(root_url);
812 isRootHttp = ishttp(root_url);
813
814 if (size > 0 && root_url[size - 1] != token) {
815 av_strlcat(root_url, "/", size + 2);
816 }
817
818 for (i = 0; i < n_baseurl_nodes; ++i) {
819 if (i == rootId) {
820 continue;
821 }
822 text = xmlNodeGetContent(baseurl_nodes[i]);
823 if (text && !av_strstart(text, "/", NULL)) {
824 memset(tmp_str, 0, strlen(tmp_str));
825 if (!ishttp(text) && isRootHttp) {
826 av_strlcpy(tmp_str, root_url, tmp_max_url_size + 2);
827 }
828 start = (text[0] == token);
829 if (start && av_stristr(tmp_str, text)) {
830 char *p = tmp_str;
831 if (!av_strncasecmp(tmp_str, "http://", 7)) {
832 p += 7;
833 } else if (!av_strncasecmp(tmp_str, "https://", 8)) {
834 p += 8;
835 }
836 p = strchr(p, '/');
837 memset(p + 1, 0, strlen(p));
838 }
839 av_strlcat(tmp_str, text + start, tmp_max_url_size + 2);
840 xmlFree(text);
841 xmlChar* escaped = xmlEncodeSpecialChars(NULL, tmp_str);
842 if (!escaped) {
843 updated = AVERROR(ENOMEM);
844 goto end;
845 }
846 xmlNodeSetContent(baseurl_nodes[i], escaped);
847 updated = 1;
848 xmlFree(escaped);
849 } else {
850 xmlFree(text);
851 }
852 }
853
854end:
855 if (tmp_max_url_size > *max_url_size) {
856 *max_url_size = tmp_max_url_size;
857 }
858 av_free(path);
859 av_free(tmp_str);
860 xmlFree(baseurl);
861 return updated;
862
863}
864
865#define SET_REPRESENTATION_SEQUENCE_BASE_INFO(arg, cnt) { \
866 val = get_val_from_nodes_tab((arg), (cnt), "duration"); \
867 if (val) { \
868 int64_t fragment_duration = (int64_t) strtoll(val, NULL, 10); \
869 if (fragment_duration < 0) { \
870 av_log(s, AV_LOG_WARNING, "duration invalid, autochanged to 0.\n"); \
871 fragment_duration = 0; \
872 } \
873 rep->fragment_duration = fragment_duration; \
874 av_log(s, AV_LOG_TRACE, "rep->fragment_duration = [%"PRId64"]\n", rep->fragment_duration); \
875 xmlFree(val); \
876 } \
877 val = get_val_from_nodes_tab((arg), (cnt), "timescale"); \
878 if (val) { \
879 int64_t fragment_timescale = (int64_t) strtoll(val, NULL, 10); \
880 if (fragment_timescale < 0) { \
881 av_log(s, AV_LOG_WARNING, "timescale invalid, autochanged to 0.\n"); \
882 fragment_timescale = 0; \
883 } \
884 rep->fragment_timescale = fragment_timescale; \
885 av_log(s, AV_LOG_TRACE, "rep->fragment_timescale = [%"PRId64"]\n", rep->fragment_timescale); \
886 xmlFree(val); \
887 } \
888 val = get_val_from_nodes_tab((arg), (cnt), "startNumber"); \
889 if (val) { \
890 int64_t start_number = (int64_t) strtoll(val, NULL, 10); \
891 if (start_number < 0) { \
892 av_log(s, AV_LOG_WARNING, "startNumber invalid, autochanged to 0.\n"); \
893 start_number = 0; \
894 } \
895 rep->start_number = rep->first_seq_no = start_number; \
896 av_log(s, AV_LOG_TRACE, "rep->first_seq_no = [%"PRId64"]\n", rep->first_seq_no); \
897 xmlFree(val); \
898 } \
899 }
900
901
903 xmlNodePtr node,
904 xmlNodePtr adaptionset_node,
905 xmlNodePtr mpd_baseurl_node,
906 xmlNodePtr period_baseurl_node,
907 xmlNodePtr period_segmenttemplate_node,
908 xmlNodePtr period_segmentlist_node,
909 xmlNodePtr fragment_template_node,
910 xmlNodePtr content_component_node,
911 xmlNodePtr adaptionset_baseurl_node,
912 xmlNodePtr adaptionset_segmentlist_node,
913 xmlNodePtr adaptionset_supplementalproperty_node)
914{
915 int32_t ret = 0;
916 DASHContext *c = s->priv_data;
917 struct representation *rep = NULL;
918 struct fragment *seg = NULL;
919 xmlNodePtr representation_segmenttemplate_node = NULL;
920 xmlNodePtr representation_baseurl_node = NULL;
921 xmlNodePtr representation_segmentlist_node = NULL;
922 xmlNodePtr segmentlists_tab[3];
923 xmlNodePtr fragment_timeline_node = NULL;
924 xmlNodePtr fragment_templates_tab[5];
925 char *val = NULL;
926 xmlNodePtr baseurl_nodes[4];
927 xmlNodePtr representation_node = node;
928 char *rep_bandwidth_val;
929 char *rep_codecs_val;
930 char *rep_dependencyid_val;
932
933 // try get information from representation
935 type = get_content_type(representation_node);
936 // try get information from contentComponen
938 type = get_content_type(content_component_node);
939 // try get information from adaption set
941 type = get_content_type(adaptionset_node);
944 av_log(s, AV_LOG_VERBOSE, "Parsing '%s' - skip not supported representation type\n", url);
945 return 0;
946 }
947
948 // convert selected representation to our internal struct
949 rep = av_mallocz(sizeof(struct representation));
950 if (!rep)
951 return AVERROR(ENOMEM);
952 if (c->adaptionset_lang) {
953 rep->lang = av_strdup(c->adaptionset_lang);
954 if (!rep->lang) {
955 av_log(s, AV_LOG_ERROR, "alloc language memory failure\n");
956 av_freep(&rep);
957 return AVERROR(ENOMEM);
958 }
959 }
960 rep->parent = s;
961 representation_segmenttemplate_node = find_child_node_by_name(representation_node, "SegmentTemplate");
962 representation_baseurl_node = find_child_node_by_name(representation_node, "BaseURL");
963 representation_segmentlist_node = find_child_node_by_name(representation_node, "SegmentList");
964 rep_bandwidth_val = xmlGetProp(representation_node, "bandwidth");
965 rep_dependencyid_val = xmlGetProp(representation_node, "dependencyId");
966 rep_codecs_val = xmlGetProp(representation_node, "codecs");
967 if (!rep_codecs_val)
968 rep_codecs_val = xmlGetProp(adaptionset_node, "codecs");
969 val = xmlGetProp(representation_node, "id");
970 if (val) {
971 rep->id = av_strdup(val);
972 xmlFree(val);
973 if (!rep->id)
974 goto enomem;
975 }
976
977 baseurl_nodes[0] = mpd_baseurl_node;
978 baseurl_nodes[1] = period_baseurl_node;
979 baseurl_nodes[2] = adaptionset_baseurl_node;
980 baseurl_nodes[3] = representation_baseurl_node;
981
982 ret = resolve_content_path(s, url, &c->max_url_size, baseurl_nodes, 4);
983 if (ret <= 0)
984 goto free;
985 ret = add_url_size(&c->max_url_size,
986 (rep->id ? strlen(rep->id) : 0) +
987 (rep_bandwidth_val ? strlen(rep_bandwidth_val) : 0));
988 if (ret < 0)
989 goto free;
990 if (representation_segmenttemplate_node || fragment_template_node || period_segmenttemplate_node) {
991 fragment_timeline_node = NULL;
992 fragment_templates_tab[0] = representation_segmenttemplate_node;
993 fragment_templates_tab[1] = adaptionset_segmentlist_node;
994 fragment_templates_tab[2] = fragment_template_node;
995 fragment_templates_tab[3] = period_segmenttemplate_node;
996 fragment_templates_tab[4] = period_segmentlist_node;
997
998 val = get_val_from_nodes_tab(fragment_templates_tab, 4, "initialization");
999 if (val) {
1000 rep->init_section = av_mallocz(sizeof(struct fragment));
1001 if (!rep->init_section) {
1002 xmlFree(val);
1003 goto enomem;
1004 }
1005 ret = add_url_size(&c->max_url_size, strlen(val));
1006 if (ret < 0) {
1007 xmlFree(val);
1008 goto free;
1009 }
1010 rep->init_section->url = get_content_url(baseurl_nodes, 4,
1011 c->max_url_size, rep->id,
1012 rep_bandwidth_val, val);
1013 xmlFree(val);
1014 if (!rep->init_section->url)
1015 goto enomem;
1016 rep->init_section->size = -1;
1017 }
1018 val = get_val_from_nodes_tab(fragment_templates_tab, 4, "media");
1019 if (val) {
1020 ret = add_url_size(&c->max_url_size, strlen(val));
1021 if (ret < 0) {
1022 xmlFree(val);
1023 goto free;
1024 }
1025 rep->url_template = get_content_url(baseurl_nodes, 4,
1026 c->max_url_size, rep->id,
1027 rep_bandwidth_val, val);
1028 xmlFree(val);
1029 }
1030 val = get_val_from_nodes_tab(fragment_templates_tab, 4, "presentationTimeOffset");
1031 if (val) {
1032 int64_t presentation_timeoffset = (int64_t) strtoll(val, NULL, 10);
1033 if (presentation_timeoffset < 0) {
1034 av_log(s, AV_LOG_WARNING, "presentationTimeOffset invalid, autochanged to 0.\n");
1035 presentation_timeoffset = 0;
1036 }
1037 rep->presentation_timeoffset = presentation_timeoffset;
1038 av_log(s, AV_LOG_TRACE, "rep->presentation_timeoffset = [%"PRId64"]\n", rep->presentation_timeoffset);
1039 xmlFree(val);
1040 }
1041
1042 SET_REPRESENTATION_SEQUENCE_BASE_INFO(fragment_templates_tab, 4);
1043 if (adaptionset_supplementalproperty_node) {
1044 char *scheme_id_uri = xmlGetProp(adaptionset_supplementalproperty_node, "schemeIdUri");
1045 if (scheme_id_uri) {
1046 int is_last_segment_number = !av_strcasecmp(scheme_id_uri, "http://dashif.org/guidelines/last-segment-number");
1047 xmlFree(scheme_id_uri);
1048 if (is_last_segment_number) {
1049 val = xmlGetProp(adaptionset_supplementalproperty_node, "value");
1050 if (!val) {
1051 av_log(s, AV_LOG_ERROR, "Missing value attribute in adaptionset_supplementalproperty_node\n");
1052 } else {
1053 rep->last_seq_no = (int64_t)strtoll(val, NULL, 10) - 1;
1054 xmlFree(val);
1055 }
1056 }
1057 }
1058 }
1059
1060 fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
1061
1062 if (!fragment_timeline_node)
1063 fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
1064 if (!fragment_timeline_node)
1065 fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
1066 if (!fragment_timeline_node)
1067 fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
1068 if (fragment_timeline_node) {
1069 fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
1070 while (fragment_timeline_node) {
1071 ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
1072 if (ret < 0)
1073 goto free;
1074 fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
1075 }
1076 }
1077 } else if (representation_baseurl_node && !representation_segmentlist_node) {
1078 seg = av_mallocz(sizeof(struct fragment));
1079 if (!seg)
1080 goto enomem;
1081 ret = av_dynarray_add_nofree(&rep->fragments, &rep->n_fragments, seg);
1082 if (ret < 0) {
1083 av_free(seg);
1084 goto free;
1085 }
1086 seg->url = get_content_url(baseurl_nodes, 4, c->max_url_size,
1087 rep->id, rep_bandwidth_val, NULL);
1088 if (!seg->url)
1089 goto enomem;
1090 seg->size = -1;
1091 } else if (representation_segmentlist_node) {
1092 // TODO: https://www.brendanlong.com/the-structure-of-an-mpeg-dash-mpd.html
1093 // http://www-itec.uni-klu.ac.at/dash/ddash/mpdGenerator.php?fragmentlength=15&type=full
1094 xmlNodePtr fragmenturl_node = NULL;
1095 segmentlists_tab[0] = representation_segmentlist_node;
1096 segmentlists_tab[1] = adaptionset_segmentlist_node;
1097 segmentlists_tab[2] = period_segmentlist_node;
1098
1099 SET_REPRESENTATION_SEQUENCE_BASE_INFO(segmentlists_tab, 3)
1100 fragmenturl_node = xmlFirstElementChild(representation_segmentlist_node);
1101 while (fragmenturl_node) {
1102 ret = parse_manifest_segmenturlnode(s, rep, fragmenturl_node,
1103 baseurl_nodes, rep->id,
1104 rep_bandwidth_val);
1105 if (ret < 0)
1106 goto free;
1107 fragmenturl_node = xmlNextElementSibling(fragmenturl_node);
1108 }
1109
1110 fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
1111 if (!fragment_timeline_node)
1112 fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
1113 if (fragment_timeline_node) {
1114 fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
1115 while (fragment_timeline_node) {
1116 ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
1117 if (ret < 0)
1118 goto free;
1119 fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
1120 }
1121 }
1122 } else {
1123 av_log(s, AV_LOG_ERROR, "Unknown format of Representation node id '%s' \n",
1124 rep->id ? rep->id : "");
1125 goto free;
1126 }
1127
1128 if (rep->fragment_duration > 0 && !rep->fragment_timescale)
1129 rep->fragment_timescale = 1;
1130 rep->bandwidth = rep_bandwidth_val ? atoi(rep_bandwidth_val) : 0;
1131 if (rep_dependencyid_val) {
1132 rep->dependencyid = av_strdup(rep_dependencyid_val);
1133 if (!rep->dependencyid) {
1134 xmlFree(rep_dependencyid_val);
1135 goto enomem;
1136 }
1137 }
1138 if (rep_codecs_val) {
1139 rep->codecs = av_strdup(rep_codecs_val);
1140 if (!rep->codecs) {
1141 xmlFree(rep_codecs_val);
1142 goto enomem;
1143 }
1144 }
1145 rep->framerate = av_make_q(0, 0);
1146 if (type == AVMEDIA_TYPE_VIDEO) {
1147 char *rep_framerate_val = xmlGetProp(representation_node, "frameRate");
1148 if (rep_framerate_val) {
1149 ret = av_parse_video_rate(&rep->framerate, rep_framerate_val);
1150 if (ret < 0)
1151 av_log(s, AV_LOG_VERBOSE, "Ignoring invalid frame rate '%s'\n", rep_framerate_val);
1152 xmlFree(rep_framerate_val);
1153 }
1154 }
1155
1156 switch (type) {
1157 case AVMEDIA_TYPE_VIDEO:
1158 ret = av_dynarray_add_nofree(&c->videos, &c->n_videos, rep);
1159 break;
1160 case AVMEDIA_TYPE_AUDIO:
1161 ret = av_dynarray_add_nofree(&c->audios, &c->n_audios, rep);
1162 break;
1164 ret = av_dynarray_add_nofree(&c->subtitles, &c->n_subtitles, rep);
1165 break;
1166 }
1167 if (ret < 0)
1168 goto free;
1169
1170end:
1171 if (rep_bandwidth_val)
1172 xmlFree(rep_bandwidth_val);
1173 if (rep_dependencyid_val)
1174 xmlFree(rep_dependencyid_val);
1175 if (rep_codecs_val)
1176 xmlFree(rep_codecs_val);
1177
1178 return ret;
1179enomem:
1180 ret = AVERROR(ENOMEM);
1181free:
1183 goto end;
1184}
1185
1186static int parse_manifest_adaptationset_attr(AVFormatContext *s, xmlNodePtr adaptionset_node)
1187{
1188 DASHContext *c = s->priv_data;
1189
1190 if (!adaptionset_node) {
1191 av_log(s, AV_LOG_WARNING, "Cannot get AdaptionSet\n");
1192 return AVERROR(EINVAL);
1193 }
1194 c->adaptionset_lang = xmlGetProp(adaptionset_node, "lang");
1195
1196 return 0;
1197}
1198
1200 xmlNodePtr adaptionset_node,
1201 xmlNodePtr mpd_baseurl_node,
1202 xmlNodePtr period_baseurl_node,
1203 xmlNodePtr period_segmenttemplate_node,
1204 xmlNodePtr period_segmentlist_node)
1205{
1206 int ret = 0;
1207 DASHContext *c = s->priv_data;
1208 xmlNodePtr fragment_template_node = NULL;
1209 xmlNodePtr content_component_node = NULL;
1210 xmlNodePtr adaptionset_baseurl_node = NULL;
1211 xmlNodePtr adaptionset_segmentlist_node = NULL;
1212 xmlNodePtr adaptionset_supplementalproperty_node = NULL;
1213 xmlNodePtr node = NULL;
1214
1215 ret = parse_manifest_adaptationset_attr(s, adaptionset_node);
1216 if (ret < 0)
1217 return ret;
1218
1219 node = xmlFirstElementChild(adaptionset_node);
1220 while (node) {
1221 if (!av_strcasecmp(node->name, "SegmentTemplate")) {
1222 fragment_template_node = node;
1223 } else if (!av_strcasecmp(node->name, "ContentComponent")) {
1224 content_component_node = node;
1225 } else if (!av_strcasecmp(node->name, "BaseURL")) {
1226 adaptionset_baseurl_node = node;
1227 } else if (!av_strcasecmp(node->name, "SegmentList")) {
1228 adaptionset_segmentlist_node = node;
1229 } else if (!av_strcasecmp(node->name, "SupplementalProperty")) {
1230 adaptionset_supplementalproperty_node = node;
1231 } else if (!av_strcasecmp(node->name, "Representation")) {
1233 adaptionset_node,
1234 mpd_baseurl_node,
1235 period_baseurl_node,
1236 period_segmenttemplate_node,
1237 period_segmentlist_node,
1238 fragment_template_node,
1239 content_component_node,
1240 adaptionset_baseurl_node,
1241 adaptionset_segmentlist_node,
1242 adaptionset_supplementalproperty_node);
1243 if (ret < 0)
1244 goto err;
1245 }
1246 node = xmlNextElementSibling(node);
1247 }
1248
1249err:
1250 xmlFree(c->adaptionset_lang);
1251 c->adaptionset_lang = NULL;
1252 return ret;
1253}
1254
1255static int parse_programinformation(AVFormatContext *s, xmlNodePtr node)
1256{
1257 xmlChar *val = NULL;
1258
1259 node = xmlFirstElementChild(node);
1260 while (node) {
1261 if (!av_strcasecmp(node->name, "Title")) {
1262 val = xmlNodeGetContent(node);
1263 if (val) {
1264 av_dict_set(&s->metadata, "Title", val, 0);
1265 }
1266 } else if (!av_strcasecmp(node->name, "Source")) {
1267 val = xmlNodeGetContent(node);
1268 if (val) {
1269 av_dict_set(&s->metadata, "Source", val, 0);
1270 }
1271 } else if (!av_strcasecmp(node->name, "Copyright")) {
1272 val = xmlNodeGetContent(node);
1273 if (val) {
1274 av_dict_set(&s->metadata, "Copyright", val, 0);
1275 }
1276 }
1277 node = xmlNextElementSibling(node);
1278 xmlFree(val);
1279 val = NULL;
1280 }
1281 return 0;
1282}
1283
1284static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
1285{
1286 DASHContext *c = s->priv_data;
1287 int ret = 0;
1288 int close_in = 0;
1289 AVBPrint buf;
1291 xmlDoc *doc = NULL;
1292 xmlNodePtr root_element = NULL;
1293 xmlNodePtr node = NULL;
1294 xmlNodePtr period_node = NULL;
1295 xmlNodePtr tmp_node = NULL;
1296 xmlNodePtr mpd_baseurl_node = NULL;
1297 xmlNodePtr period_baseurl_node = NULL;
1298 xmlNodePtr period_segmenttemplate_node = NULL;
1299 xmlNodePtr period_segmentlist_node = NULL;
1300 xmlNodePtr adaptionset_node = NULL;
1301 xmlAttrPtr attr = NULL;
1302 char *val = NULL;
1303 uint32_t period_duration_sec = 0;
1304 uint32_t period_start_sec = 0;
1305
1306 if (!in) {
1307 close_in = 1;
1308
1309 av_dict_copy(&opts, c->avio_opts, 0);
1310 ret = s->io_open(s, &in, url, AVIO_FLAG_READ, &opts);
1312 if (ret < 0)
1313 return ret;
1314 }
1315
1316 if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, (uint8_t**)&c->base_url) < 0)
1317 c->base_url = av_strdup(url);
1318 if (!c->base_url) {
1319 if (close_in)
1320 ff_format_io_close(s, &in);
1321 return AVERROR(ENOMEM);
1322 }
1323 url = c->base_url;
1324
1325 av_bprint_init(&buf, 0, INT_MAX); // xmlReadMemory uses integer bufsize
1326
1327 if ((ret = avio_read_to_bprint(in, &buf, SIZE_MAX)) < 0 ||
1328 !avio_feof(in)) {
1329 av_log(s, AV_LOG_ERROR, "Unable to read to manifest '%s'\n", url);
1330 if (ret == 0)
1332 } else {
1333 LIBXML_TEST_VERSION
1334
1335 doc = xmlReadMemory(buf.str, buf.len, c->base_url, NULL, 0);
1336 root_element = xmlDocGetRootElement(doc);
1337 node = root_element;
1338
1339 if (!node) {
1341 av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing root node\n", url);
1342 goto cleanup;
1343 }
1344
1345 if (node->type != XML_ELEMENT_NODE ||
1346 av_strcasecmp(node->name, "MPD")) {
1348 av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - wrong root node name[%s] type[%d]\n", url, node->name, (int)node->type);
1349 goto cleanup;
1350 }
1351
1352 val = xmlGetProp(node, "type");
1353 if (!val) {
1354 av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing type attrib\n", url);
1356 goto cleanup;
1357 }
1358 if (!av_strcasecmp(val, "dynamic"))
1359 c->is_live = 1;
1360 xmlFree(val);
1361
1362 attr = node->properties;
1363 while (attr) {
1364 val = xmlGetProp(node, attr->name);
1365 if (!val) {
1366 ret = AVERROR(ENOMEM);
1367 goto cleanup;
1368 }
1369
1370 if (!av_strcasecmp(attr->name, "availabilityStartTime")) {
1371 c->availability_start_time = get_utc_date_time_insec(s, val);
1372 av_log(s, AV_LOG_TRACE, "c->availability_start_time = [%"PRId64"]\n", c->availability_start_time);
1373 } else if (!av_strcasecmp(attr->name, "availabilityEndTime")) {
1374 c->availability_end_time = get_utc_date_time_insec(s, val);
1375 av_log(s, AV_LOG_TRACE, "c->availability_end_time = [%"PRId64"]\n", c->availability_end_time);
1376 } else if (!av_strcasecmp(attr->name, "publishTime")) {
1377 c->publish_time = get_utc_date_time_insec(s, val);
1378 av_log(s, AV_LOG_TRACE, "c->publish_time = [%"PRId64"]\n", c->publish_time);
1379 } else if (!av_strcasecmp(attr->name, "minimumUpdatePeriod")) {
1380 c->minimum_update_period = get_duration_insec(s, val);
1381 av_log(s, AV_LOG_TRACE, "c->minimum_update_period = [%"PRId64"]\n", c->minimum_update_period);
1382 } else if (!av_strcasecmp(attr->name, "timeShiftBufferDepth")) {
1383 c->time_shift_buffer_depth = get_duration_insec(s, val);
1384 av_log(s, AV_LOG_TRACE, "c->time_shift_buffer_depth = [%"PRId64"]\n", c->time_shift_buffer_depth);
1385 } else if (!av_strcasecmp(attr->name, "minBufferTime")) {
1386 c->min_buffer_time = get_duration_insec(s, val);
1387 av_log(s, AV_LOG_TRACE, "c->min_buffer_time = [%"PRId64"]\n", c->min_buffer_time);
1388 } else if (!av_strcasecmp(attr->name, "suggestedPresentationDelay")) {
1389 c->suggested_presentation_delay = get_duration_insec(s, val);
1390 av_log(s, AV_LOG_TRACE, "c->suggested_presentation_delay = [%"PRId64"]\n", c->suggested_presentation_delay);
1391 } else if (!av_strcasecmp(attr->name, "mediaPresentationDuration")) {
1392 c->media_presentation_duration = get_duration_insec(s, val);
1393 av_log(s, AV_LOG_TRACE, "c->media_presentation_duration = [%"PRId64"]\n", c->media_presentation_duration);
1394 }
1395 attr = attr->next;
1396 xmlFree(val);
1397 }
1398
1399 tmp_node = find_child_node_by_name(node, "BaseURL");
1400 if (tmp_node) {
1401 mpd_baseurl_node = xmlCopyNode(tmp_node,1);
1402 } else {
1403 mpd_baseurl_node = xmlNewNode(NULL, "BaseURL");
1404 }
1405
1406 // at now we can handle only one period, with the longest duration
1407 node = xmlFirstElementChild(node);
1408 while (node) {
1409 if (!av_strcasecmp(node->name, "Period")) {
1410 period_duration_sec = 0;
1411 period_start_sec = 0;
1412 attr = node->properties;
1413 while (attr) {
1414 val = xmlGetProp(node, attr->name);
1415 if (!val) {
1416 ret = AVERROR(ENOMEM);
1417 goto cleanup;
1418 }
1419 if (!av_strcasecmp(attr->name, "duration")) {
1420 period_duration_sec = get_duration_insec(s, val);
1421 } else if (!av_strcasecmp(attr->name, "start")) {
1422 period_start_sec = get_duration_insec(s, val);
1423 }
1424 attr = attr->next;
1425 xmlFree(val);
1426 }
1427 if ((period_duration_sec) >= (c->period_duration)) {
1428 period_node = node;
1429 c->period_duration = period_duration_sec;
1430 c->period_start = period_start_sec;
1431 if (c->period_start > 0)
1432 c->media_presentation_duration = c->period_duration;
1433 }
1434 } else if (!av_strcasecmp(node->name, "ProgramInformation")) {
1436 }
1437 node = xmlNextElementSibling(node);
1438 }
1439 if (!period_node) {
1440 av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing Period node\n", url);
1442 goto cleanup;
1443 }
1444
1445 adaptionset_node = xmlFirstElementChild(period_node);
1446 while (adaptionset_node) {
1447 if (!av_strcasecmp(adaptionset_node->name, "BaseURL")) {
1448 period_baseurl_node = adaptionset_node;
1449 } else if (!av_strcasecmp(adaptionset_node->name, "SegmentTemplate")) {
1450 period_segmenttemplate_node = adaptionset_node;
1451 } else if (!av_strcasecmp(adaptionset_node->name, "SegmentList")) {
1452 period_segmentlist_node = adaptionset_node;
1453 } else if (!av_strcasecmp(adaptionset_node->name, "AdaptationSet")) {
1454 parse_manifest_adaptationset(s, url, adaptionset_node, mpd_baseurl_node, period_baseurl_node, period_segmenttemplate_node, period_segmentlist_node);
1455 }
1456 adaptionset_node = xmlNextElementSibling(adaptionset_node);
1457 }
1458cleanup:
1459 /*free the document */
1460 xmlFreeDoc(doc);
1461 xmlCleanupParser();
1462 xmlFreeNode(mpd_baseurl_node);
1463 }
1464
1465 av_bprint_finalize(&buf, NULL);
1466 if (close_in) {
1467 ff_format_io_close(s, &in);
1468 }
1469 return ret;
1470}
1471
1473{
1474 DASHContext *c = s->priv_data;
1475 int64_t num = 0;
1476 int64_t start_time_offset = 0;
1477
1478 if (c->is_live) {
1479 if (pls->n_fragments) {
1480 av_log(s, AV_LOG_TRACE, "in n_fragments mode\n");
1481 num = pls->first_seq_no;
1482 } else if (pls->n_timelines) {
1483 av_log(s, AV_LOG_TRACE, "in n_timelines mode\n");
1484 start_time_offset = get_segment_start_time_based_on_timeline(pls, 0xFFFFFFFF) - 60 * pls->fragment_timescale; // 60 seconds before end
1485 num = calc_next_seg_no_from_timelines(pls, start_time_offset);
1486 if (num == -1)
1487 num = pls->first_seq_no;
1488 } else if (pls->fragment_duration){
1489 av_log(s, AV_LOG_TRACE, "in fragment_duration mode fragment_timescale = %"PRId64", presentation_timeoffset = %"PRId64"\n", pls->fragment_timescale, pls->presentation_timeoffset);
1490 if (pls->presentation_timeoffset) {
1491 num = pls->first_seq_no + ((get_current_time_in_sec() - c->availability_start_time) * pls->fragment_timescale) / pls->fragment_duration - c->min_buffer_time;
1492 } else if (c->publish_time > 0 && !c->availability_start_time) {
1493 if (c->min_buffer_time) {
1494 num = pls->first_seq_no + (((c->publish_time + pls->fragment_duration) - c->suggested_presentation_delay) * pls->fragment_timescale) / pls->fragment_duration - c->min_buffer_time;
1495 } else {
1496 num = pls->first_seq_no + (((c->publish_time - c->time_shift_buffer_depth + pls->fragment_duration) - c->suggested_presentation_delay) * pls->fragment_timescale) / pls->fragment_duration;
1497 }
1498 } else {
1499 num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time) - c->suggested_presentation_delay) * pls->fragment_timescale) / pls->fragment_duration;
1500 }
1501 }
1502 } else {
1503 num = pls->first_seq_no;
1504 }
1505 return num;
1506}
1507
1509{
1510 DASHContext *c = s->priv_data;
1511 int64_t num = 0;
1512
1513 if (c->is_live && pls->fragment_duration) {
1514 av_log(s, AV_LOG_TRACE, "in live mode\n");
1515 num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time) - c->time_shift_buffer_depth) * pls->fragment_timescale) / pls->fragment_duration;
1516 } else {
1517 num = pls->first_seq_no;
1518 }
1519 return num;
1520}
1521
1523{
1524 int64_t num = 0;
1525
1526 if (pls->n_fragments) {
1527 num = pls->first_seq_no + pls->n_fragments - 1;
1528 } else if (pls->n_timelines) {
1529 int i = 0;
1530 num = pls->first_seq_no + pls->n_timelines - 1;
1531 for (i = 0; i < pls->n_timelines; i++) {
1532 if (pls->timelines[i]->repeat == -1) {
1533 num = pls->timelines[i]->duration ? av_rescale(c->period_duration, pls->fragment_timescale, pls->timelines[i]->duration) : pls->first_seq_no;
1534 } else {
1535 num += pls->timelines[i]->repeat;
1536 }
1537 }
1538 } else if (c->is_live && pls->fragment_duration) {
1539 num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time)) * pls->fragment_timescale) / pls->fragment_duration;
1540 } else if (pls->fragment_duration) {
1541 num = pls->first_seq_no + av_rescale_rnd(1, c->media_presentation_duration * pls->fragment_timescale, pls->fragment_duration, AV_ROUND_UP);
1542 }
1543
1544 return num;
1545}
1546
1547static void move_timelines(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
1548{
1549 if (rep_dest && rep_src ) {
1550 free_timelines_list(rep_dest);
1551 rep_dest->timelines = rep_src->timelines;
1552 rep_dest->n_timelines = rep_src->n_timelines;
1553 rep_dest->first_seq_no = rep_src->first_seq_no;
1554 rep_dest->last_seq_no = calc_max_seg_no(rep_dest, c);
1555 rep_src->timelines = NULL;
1556 rep_src->n_timelines = 0;
1557 rep_dest->cur_seq_no = rep_src->cur_seq_no;
1558 }
1559}
1560
1561static void move_segments(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
1562{
1563 if (rep_dest && rep_src ) {
1564 free_fragment_list(rep_dest);
1565 if (rep_src->start_number > (rep_dest->start_number + rep_dest->n_fragments))
1566 rep_dest->cur_seq_no = 0;
1567 else {
1568 rep_dest->cur_seq_no += rep_src->start_number - rep_dest->start_number;
1569 if (rep_dest->cur_seq_no < 0)
1570 rep_dest->cur_seq_no = 0;
1571 }
1572 rep_dest->fragments = rep_src->fragments;
1573 rep_dest->n_fragments = rep_src->n_fragments;
1574 rep_dest->parent = rep_src->parent;
1575 rep_dest->last_seq_no = calc_max_seg_no(rep_dest, c);
1576 rep_src->fragments = NULL;
1577 rep_src->n_fragments = 0;
1578 }
1579}
1580
1581
1583{
1584 int ret = 0, i;
1585 DASHContext *c = s->priv_data;
1586 // save current context
1587 int n_videos = c->n_videos;
1588 struct representation **videos = c->videos;
1589 int n_audios = c->n_audios;
1590 struct representation **audios = c->audios;
1591 int n_subtitles = c->n_subtitles;
1592 struct representation **subtitles = c->subtitles;
1593 char *base_url = c->base_url;
1594
1595 c->base_url = NULL;
1596 c->n_videos = 0;
1597 c->videos = NULL;
1598 c->n_audios = 0;
1599 c->audios = NULL;
1600 c->n_subtitles = 0;
1601 c->subtitles = NULL;
1602 ret = parse_manifest(s, s->url, NULL);
1603 if (ret)
1604 goto finish;
1605
1606 if (c->n_videos != n_videos) {
1608 "new manifest has mismatched no. of video representations, %d -> %d\n",
1609 n_videos, c->n_videos);
1610 return AVERROR_INVALIDDATA;
1611 }
1612 if (c->n_audios != n_audios) {
1614 "new manifest has mismatched no. of audio representations, %d -> %d\n",
1615 n_audios, c->n_audios);
1616 return AVERROR_INVALIDDATA;
1617 }
1618 if (c->n_subtitles != n_subtitles) {
1620 "new manifest has mismatched no. of subtitles representations, %d -> %d\n",
1621 n_subtitles, c->n_subtitles);
1622 return AVERROR_INVALIDDATA;
1623 }
1624
1625 for (i = 0; i < n_videos; i++) {
1626 struct representation *cur_video = videos[i];
1627 struct representation *ccur_video = c->videos[i];
1628 if (cur_video->timelines && cur_video->fragment_timescale > 0) {
1629 // calc current time
1630 int64_t currentTime = get_segment_start_time_based_on_timeline(cur_video, cur_video->cur_seq_no) / cur_video->fragment_timescale;
1631 // update segments
1632 ccur_video->cur_seq_no = calc_next_seg_no_from_timelines(ccur_video, currentTime * cur_video->fragment_timescale - 1);
1633 if (ccur_video->cur_seq_no >= 0) {
1634 move_timelines(ccur_video, cur_video, c);
1635 }
1636 }
1637 if (cur_video->fragments) {
1638 move_segments(ccur_video, cur_video, c);
1639 }
1640 }
1641 for (i = 0; i < n_audios; i++) {
1642 struct representation *cur_audio = audios[i];
1643 struct representation *ccur_audio = c->audios[i];
1644 if (cur_audio->timelines && cur_audio->fragment_timescale > 0) {
1645 // calc current time
1646 int64_t currentTime = get_segment_start_time_based_on_timeline(cur_audio, cur_audio->cur_seq_no) / cur_audio->fragment_timescale;
1647 // update segments
1648 ccur_audio->cur_seq_no = calc_next_seg_no_from_timelines(ccur_audio, currentTime * cur_audio->fragment_timescale - 1);
1649 if (ccur_audio->cur_seq_no >= 0) {
1650 move_timelines(ccur_audio, cur_audio, c);
1651 }
1652 }
1653 if (cur_audio->fragments) {
1654 move_segments(ccur_audio, cur_audio, c);
1655 }
1656 }
1657
1658finish:
1659 // restore context
1660 if (c->base_url)
1661 av_free(base_url);
1662 else
1663 c->base_url = base_url;
1664
1665 if (c->subtitles)
1667 if (c->audios)
1669 if (c->videos)
1671
1672 c->n_subtitles = n_subtitles;
1673 c->subtitles = subtitles;
1674 c->n_audios = n_audios;
1675 c->audios = audios;
1676 c->n_videos = n_videos;
1677 c->videos = videos;
1678 return ret;
1679}
1680
1682{
1683 int64_t min_seq_no = 0;
1684 int64_t max_seq_no = 0;
1685 struct fragment *seg = NULL;
1686 struct fragment *seg_ptr = NULL;
1687 DASHContext *c = pls->parent->priv_data;
1688 int reload_count = 0;
1689
1690 while (( !ff_check_interrupt(c->interrupt_callback)&& pls->n_fragments > 0)) {
1691 if (pls->cur_seq_no >= 0 && pls->cur_seq_no < pls->n_fragments) {
1692 seg_ptr = pls->fragments[pls->cur_seq_no];
1693 seg = av_mallocz(sizeof(struct fragment));
1694 if (!seg) {
1695 return NULL;
1696 }
1697 seg->url = av_strdup(seg_ptr->url);
1698 if (!seg->url) {
1699 av_free(seg);
1700 return NULL;
1701 }
1702 seg->size = seg_ptr->size;
1703 seg->url_offset = seg_ptr->url_offset;
1704 return seg;
1705 } else if (c->is_live) {
1706 if (reload_count++ >= c->max_reload) {
1708 "Reached max manifest reloads (%d) at seq %"PRId64"\n",
1709 c->max_reload, pls->cur_seq_no);
1710 return NULL;
1711 }
1713 } else {
1714 break;
1715 }
1716 }
1717 if (c->is_live) {
1718 min_seq_no = calc_min_seg_no(pls->parent, pls);
1719 max_seq_no = calc_max_seg_no(pls, c);
1720
1721 if (pls->timelines || pls->fragments) {
1723 }
1724 if (pls->cur_seq_no <= min_seq_no) {
1725 av_log(pls->parent, AV_LOG_VERBOSE, "old fragment: cur[%"PRId64"] min[%"PRId64"] max[%"PRId64"]\n", (int64_t)pls->cur_seq_no, min_seq_no, max_seq_no);
1726 pls->cur_seq_no = calc_cur_seg_no(pls->parent, pls);
1727 } else if (pls->cur_seq_no > max_seq_no) {
1728 av_log(pls->parent, AV_LOG_VERBOSE, "new fragment: min[%"PRId64"] max[%"PRId64"]\n", min_seq_no, max_seq_no);
1729 }
1730 seg = av_mallocz(sizeof(struct fragment));
1731 if (!seg) {
1732 return NULL;
1733 }
1734 } else if (pls->cur_seq_no <= pls->last_seq_no) {
1735 seg = av_mallocz(sizeof(struct fragment));
1736 if (!seg) {
1737 return NULL;
1738 }
1739 }
1740 if (seg) {
1741 char *tmpfilename;
1742 if (!pls->url_template) {
1743 av_log(pls->parent, AV_LOG_ERROR, "Cannot get fragment, missing template URL\n");
1744 av_free(seg);
1745 return NULL;
1746 }
1747 tmpfilename = av_mallocz(c->max_url_size);
1748 if (!tmpfilename) {
1749 av_free(seg);
1750 return NULL;
1751 }
1752 ff_dash_fill_tmpl_params(tmpfilename, c->max_url_size, pls->url_template, 0, pls->cur_seq_no, 0, get_segment_start_time_based_on_timeline(pls, pls->cur_seq_no));
1753 seg->url = av_strireplace(pls->url_template, pls->url_template, tmpfilename);
1754 if (!seg->url) {
1755 av_log(pls->parent, AV_LOG_WARNING, "Unable to resolve template url '%s', try to use origin template\n", pls->url_template);
1756 seg->url = av_strdup(pls->url_template);
1757 if (!seg->url) {
1758 av_log(pls->parent, AV_LOG_ERROR, "Cannot resolve template url '%s'\n", pls->url_template);
1759 av_free(tmpfilename);
1760 av_free(seg);
1761 return NULL;
1762 }
1763 }
1764 av_free(tmpfilename);
1765 seg->size = -1;
1766 }
1767
1768 return seg;
1769}
1770
1771static int read_from_url(struct representation *pls, struct fragment *seg,
1772 uint8_t *buf, int buf_size)
1773{
1774 int ret;
1775
1776 /* limit read if the fragment was only a part of a file */
1777 if (seg->size >= 0) {
1778 buf_size = FFMIN(buf_size, pls->cur_seg_size - pls->cur_seg_offset);
1779 if (buf_size <= 0)
1780 return AVERROR_EOF;
1781 }
1782
1783 ret = avio_read(pls->input, buf, buf_size);
1784 if (ret > 0)
1785 pls->cur_seg_offset += ret;
1786
1787 return ret;
1788}
1789
1790static int open_input(DASHContext *c, struct representation *pls, struct fragment *seg)
1791{
1793 char *url = NULL;
1794 int ret = 0;
1795
1796 url = av_mallocz(c->max_url_size);
1797 if (!url) {
1798 ret = AVERROR(ENOMEM);
1799 goto cleanup;
1800 }
1801
1802 if (seg->size >= 0) {
1803 /* try to restrict the HTTP request to the part we want
1804 * (if this is in fact a HTTP request) */
1805 av_dict_set_int(&opts, "offset", seg->url_offset, 0);
1806 av_dict_set_int(&opts, "end_offset", seg->url_offset + seg->size, 0);
1807 }
1808
1809 ff_make_absolute_url(url, c->max_url_size, c->base_url, seg->url);
1810 av_log(pls->parent, AV_LOG_VERBOSE, "DASH request for url '%s', offset %"PRId64"\n",
1811 url, seg->url_offset);
1812 ret = open_url(pls->parent, &pls->input, url, &c->avio_opts, opts, NULL);
1813
1814cleanup:
1815 av_free(url);
1817 pls->cur_seg_offset = 0;
1818 pls->cur_seg_size = seg->size;
1819 return ret;
1820}
1821
1823{
1824 static const int max_init_section_size = 1024 * 1024;
1825 DASHContext *c = pls->parent->priv_data;
1826 int64_t sec_size;
1827 int64_t urlsize;
1828 int ret;
1829
1830 if (!pls->init_section || pls->init_sec_buf)
1831 return 0;
1832
1833 ret = open_input(c, pls, pls->init_section);
1834 if (ret < 0) {
1836 "Failed to open an initialization section\n");
1837 return ret;
1838 }
1839
1840 if (pls->init_section->size >= 0)
1841 sec_size = pls->init_section->size;
1842 else if ((urlsize = avio_size(pls->input)) >= 0)
1843 sec_size = urlsize;
1844 else
1845 sec_size = max_init_section_size;
1846
1848 "Downloading an initialization section of size %"PRId64"\n",
1849 sec_size);
1850
1851 sec_size = FFMIN(sec_size, max_init_section_size);
1852
1853 av_fast_malloc(&pls->init_sec_buf, &pls->init_sec_buf_size, sec_size);
1854
1855 ret = read_from_url(pls, pls->init_section, pls->init_sec_buf,
1856 pls->init_sec_buf_size);
1857 ff_format_io_close(pls->parent, &pls->input);
1858
1859 if (ret < 0)
1860 return ret;
1861
1862 pls->init_sec_data_len = ret;
1863 pls->init_sec_buf_read_offset = 0;
1864
1865 return 0;
1866}
1867
1868static int64_t seek_data(void *opaque, int64_t offset, int whence)
1869{
1870 struct representation *v = opaque;
1871 if (v->n_fragments && !v->init_sec_data_len) {
1872 return avio_seek(v->input, offset, whence);
1873 }
1874
1875 return AVERROR(ENOSYS);
1876}
1877
1878static int read_data(void *opaque, uint8_t *buf, int buf_size)
1879{
1880 int ret = 0;
1881 struct representation *v = opaque;
1883
1884restart:
1885 if (!v->input) {
1888 if (!v->cur_seg) {
1889 ret = AVERROR_EOF;
1890 goto end;
1891 }
1892
1893 /* load/update Media Initialization Section, if any */
1895 if (ret)
1896 goto end;
1897
1898 ret = open_input(c, v, v->cur_seg);
1899 if (ret < 0) {
1900 if (ff_check_interrupt(c->interrupt_callback)) {
1901 ret = AVERROR_EXIT;
1902 goto end;
1903 }
1904 av_log(v->parent, AV_LOG_WARNING, "Failed to open fragment of playlist\n");
1905 if (++v->n_open_failures > c->max_reload) {
1907 "Reached max consecutive fragment open failures (%d), giving up\n",
1908 c->max_reload);
1909 ret = AVERROR_EOF;
1910 goto end;
1911 }
1912 v->cur_seq_no++;
1913 goto restart;
1914 }
1915 v->n_open_failures = 0;
1916 }
1917
1919 /* Push init section out first before first actual fragment */
1920 int copy_size = FFMIN(v->init_sec_data_len - v->init_sec_buf_read_offset, buf_size);
1921 memcpy(buf, v->init_sec_buf + v->init_sec_buf_read_offset, copy_size);
1922 v->init_sec_buf_read_offset += copy_size;
1923 ret = copy_size;
1924 goto end;
1925 }
1926
1927 /* check the v->cur_seg, if it is null, get current and double check if the new v->cur_seg*/
1928 if (!v->cur_seg) {
1930 }
1931 if (!v->cur_seg) {
1932 ret = AVERROR_EOF;
1933 goto end;
1934 }
1935 ret = read_from_url(v, v->cur_seg, buf, buf_size);
1936 if (ret > 0)
1937 goto end;
1938
1939 if (c->is_live || v->cur_seq_no < v->last_seq_no) {
1940 if (!v->is_restart_needed)
1941 v->cur_seq_no++;
1942 v->is_restart_needed = 1;
1943 }
1944
1945end:
1946 return ret;
1947}
1948
1949static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
1950 int flags, AVDictionary **opts)
1951{
1953 "A DASH playlist item '%s' referred to an external file '%s'. "
1954 "Opening this file was forbidden for security reasons\n",
1955 s->url, url);
1956 return AVERROR(EPERM);
1957}
1958
1960{
1961 /* note: the internal buffer could have changed */
1962 av_freep(&pls->pb.pub.buffer);
1963 memset(&pls->pb, 0x00, sizeof(pls->pb));
1964 pls->ctx->pb = NULL;
1966}
1967
1969{
1970 DASHContext *c = s->priv_data;
1971 const AVInputFormat *in_fmt = NULL;
1972 AVDictionary *in_fmt_opts = NULL;
1973 uint8_t *avio_ctx_buffer = NULL;
1974 int ret = 0, i;
1975
1976 if (pls->ctx) {
1978 }
1979
1980 if (ff_check_interrupt(&s->interrupt_callback)) {
1981 ret = AVERROR_EXIT;
1982 goto fail;
1983 }
1984
1985 if (!(pls->ctx = avformat_alloc_context())) {
1986 ret = AVERROR(ENOMEM);
1987 goto fail;
1988 }
1989
1990 avio_ctx_buffer = av_malloc(INITIAL_BUFFER_SIZE);
1991 if (!avio_ctx_buffer ) {
1992 ret = AVERROR(ENOMEM);
1994 pls->ctx = NULL;
1995 goto fail;
1996 }
1997 av_freep(&pls->pb.pub.buffer);
1998 ffio_init_context(&pls->pb, avio_ctx_buffer, INITIAL_BUFFER_SIZE, 0,
1999 pls, read_data, NULL, c->is_live ? NULL : seek_data);
2000 pls->pb.pub.seekable = 0;
2001
2002 if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0) {
2004 pls->ctx = NULL;
2005 goto fail;
2006 }
2007
2009 pls->ctx->probesize = s->probesize > 0 ? s->probesize : 1024 * 4;
2010 pls->ctx->max_analyze_duration = s->max_analyze_duration > 0 ? s->max_analyze_duration : 4 * AV_TIME_BASE;
2011 pls->ctx->interrupt_callback = s->interrupt_callback;
2012 ret = av_probe_input_buffer(&pls->pb.pub, &in_fmt, "", NULL, 0, 0);
2013 if (ret < 0) {
2014 av_log(s, AV_LOG_ERROR, "Error when loading first fragment of playlist\n");
2016 pls->ctx = NULL;
2017 goto fail;
2018 }
2019
2020 pls->ctx->pb = &pls->pb.pub;
2021 pls->ctx->io_open = nested_io_open;
2022
2023 if (c->cenc_decryption_key)
2024 av_dict_set(&in_fmt_opts, "decryption_key", c->cenc_decryption_key, 0);
2025 if (c->cenc_decryption_keys)
2026 av_dict_set(&in_fmt_opts, "decryption_keys", c->cenc_decryption_keys, 0);
2027
2028 // provide additional information from mpd if available
2029 ret = avformat_open_input(&pls->ctx, "", in_fmt, &in_fmt_opts); //pls->init_section->url
2030 av_dict_free(&in_fmt_opts);
2031 if (ret < 0)
2032 goto fail;
2033 if (pls->n_fragments) {
2034#if FF_API_R_FRAME_RATE
2035 if (pls->framerate.den) {
2036 for (i = 0; i < pls->ctx->nb_streams; i++)
2037 pls->ctx->streams[i]->r_frame_rate = pls->framerate;
2038 }
2039#endif
2041 if (ret < 0)
2042 goto fail;
2043 }
2044
2045fail:
2046 return ret;
2047}
2048
2050{
2051 int ret = 0;
2052 int i;
2053
2054 pls->parent = s;
2055 pls->cur_seq_no = calc_cur_seg_no(s, pls);
2056
2057 if (!pls->last_seq_no)
2058 pls->last_seq_no = calc_max_seg_no(pls, s->priv_data);
2059
2061 if (ret < 0)
2062 return ret;
2063
2064 for (i = 0; i < pls->ctx->nb_streams; i++) {
2066 FFStream *sti = ffstream(st);
2067 const AVStream *ist = pls->ctx->streams[i];
2068 const FFStream *isti = cffstream(ist);
2069 if (!st)
2070 return AVERROR(ENOMEM);
2071
2072 st->id = i + pls->stream_index;
2073
2075 if (ret < 0)
2076 return ret;
2077
2079
2080 // copy disposition
2081 st->disposition = ist->disposition;
2082 sti->need_parsing = isti->need_parsing;
2083 }
2084
2085 for (i = 0; i < pls->ctx->nb_stream_groups; i++) {
2086 AVStreamGroup *istg = pls->ctx->stream_groups[i];
2087 AVStreamGroup *stg;
2088
2090 continue;
2091
2092 stg = avformat_stream_group_create(s, istg->type, NULL);
2093 if (!stg)
2094 return AVERROR(ENOMEM);
2095
2096 stg->id = s->nb_stream_groups;
2097
2098 for (int j = 0; j < istg->nb_entries; j++) {
2099 AVStream *ist = istg->entries[j]->stream;
2100 AVStream *st = s->streams[ist->index + pls->stream_index];
2102 if (ret < 0)
2103 return ret;
2104 }
2105
2106 switch (stg->type) {
2110 ret = av_opt_copy(lcevc, ilcevc);
2111 if (ret < 0)
2112 return ret;
2113 break;
2114 }
2115 default:
2116 av_unreachable("Unsupported Stream Group type should have been checked above");
2117 }
2118
2119 // copy disposition
2120 stg->disposition = istg->disposition;
2121 }
2122
2123 return 0;
2124}
2125
2126static int is_common_init_section_exist(struct representation **pls, int n_pls)
2127{
2128 struct fragment *first_init_section = pls[0]->init_section;
2129 char *url =NULL;
2130 int64_t url_offset = -1;
2131 int64_t size = -1;
2132 int i = 0;
2133
2134 if (first_init_section == NULL || n_pls == 0)
2135 return 0;
2136
2137 url = first_init_section->url;
2138 url_offset = first_init_section->url_offset;
2139 size = pls[0]->init_section->size;
2140 for (i=0;i<n_pls;i++) {
2141 if (!pls[i]->init_section)
2142 continue;
2143
2144 if (av_strcasecmp(pls[i]->init_section->url, url) ||
2145 pls[i]->init_section->url_offset != url_offset ||
2146 pls[i]->init_section->size != size) {
2147 return 0;
2148 }
2149 }
2150 return 1;
2151}
2152
2153static int copy_init_section(struct representation *rep_dest, struct representation *rep_src)
2154{
2155 rep_dest->init_sec_buf = av_mallocz(rep_src->init_sec_buf_size);
2156 if (!rep_dest->init_sec_buf) {
2157 av_log(rep_dest->ctx, AV_LOG_WARNING, "Cannot alloc memory for init_sec_buf\n");
2158 return AVERROR(ENOMEM);
2159 }
2160 memcpy(rep_dest->init_sec_buf, rep_src->init_sec_buf, rep_src->init_sec_data_len);
2161 rep_dest->init_sec_buf_size = rep_src->init_sec_buf_size;
2162 rep_dest->init_sec_data_len = rep_src->init_sec_data_len;
2163 rep_dest->cur_timestamp = rep_src->cur_timestamp;
2164
2165 return 0;
2166}
2167
2168static void move_metadata(AVStream *st, const char *key, char **value)
2169{
2170 if (*value) {
2172 *value = NULL;
2173 }
2174}
2175
2177{
2178 DASHContext *c = s->priv_data;
2179 struct representation *rep;
2180 AVProgram *program;
2181 int ret = 0;
2182 int stream_index = 0;
2183 int i, j;
2184
2185 c->interrupt_callback = &s->interrupt_callback;
2186
2187 if ((ret = ffio_copy_url_options(s->pb, &c->avio_opts)) < 0)
2188 return ret;
2189
2190 if ((ret = parse_manifest(s, s->url, s->pb)) < 0)
2191 return ret;
2192
2193 /* If this isn't a live stream, fill the total duration of the
2194 * stream. */
2195 if (!c->is_live) {
2196 s->duration = (int64_t) c->media_presentation_duration * AV_TIME_BASE;
2197 } else {
2198 av_dict_set(&c->avio_opts, "seekable", "0", 0);
2199 }
2200
2201 if(c->n_videos)
2202 c->is_init_section_common_video = is_common_init_section_exist(c->videos, c->n_videos);
2203
2204 /* Open the demuxer for video and audio components if available */
2205 for (i = 0; i < c->n_videos; i++) {
2206 rep = c->videos[i];
2207 if (i > 0 && c->is_init_section_common_video) {
2208 ret = copy_init_section(rep, c->videos[0]);
2209 if (ret < 0)
2210 return ret;
2211 }
2214
2215 if (ret)
2216 return ret;
2217 if (rep->ctx->nb_streams == 0)
2218 return AVERROR_PATCHWELCOME;
2219 stream_index += rep->ctx->nb_streams;
2220 }
2221
2222 if(c->n_audios)
2223 c->is_init_section_common_audio = is_common_init_section_exist(c->audios, c->n_audios);
2224
2225 for (i = 0; i < c->n_audios; i++) {
2226 rep = c->audios[i];
2227 if (i > 0 && c->is_init_section_common_audio) {
2228 ret = copy_init_section(rep, c->audios[0]);
2229 if (ret < 0)
2230 return ret;
2231 }
2234
2235 if (ret)
2236 return ret;
2237 if (rep->ctx->nb_streams == 0)
2238 return AVERROR_PATCHWELCOME;
2239 stream_index += rep->ctx->nb_streams;
2240 }
2241
2242 if (c->n_subtitles)
2243 c->is_init_section_common_subtitle = is_common_init_section_exist(c->subtitles, c->n_subtitles);
2244
2245 for (i = 0; i < c->n_subtitles; i++) {
2246 rep = c->subtitles[i];
2247 if (i > 0 && c->is_init_section_common_subtitle) {
2248 ret = copy_init_section(rep, c->subtitles[0]);
2249 if (ret < 0)
2250 return ret;
2251 }
2254
2255 if (ret)
2256 return ret;
2257 if (rep->ctx->nb_streams == 0)
2258 return AVERROR_PATCHWELCOME;
2259 stream_index += rep->ctx->nb_streams;
2260 }
2261
2262 if (!stream_index)
2263 return AVERROR_INVALIDDATA;
2264
2265 /* Create a program */
2266 program = av_new_program(s, 0);
2267 if (!program)
2268 return AVERROR(ENOMEM);
2269
2270 for (i = 0; i < c->n_videos; i++) {
2271 rep = c->videos[i];
2272 rep->assoc_stream = av_malloc_array(rep->ctx->nb_streams, sizeof(*rep->assoc_stream));
2273 if (!rep->assoc_stream)
2274 return AVERROR(ENOMEM);
2275 rep->nb_assoc_stream = rep->ctx->nb_streams;
2276 for (int j = 0; j < rep->ctx->nb_streams; j++) {
2278 rep->assoc_stream[j] = s->streams[rep->stream_index + j];
2279 }
2280 if (rep->bandwidth > 0)
2281 av_dict_set_int(&rep->assoc_stream[0]->metadata, "variant_bitrate", rep->bandwidth, 0);
2282 move_metadata(rep->assoc_stream[0], "id", &rep->id);
2283 }
2284 for (i = 0; i < c->n_audios; i++) {
2285 rep = c->audios[i];
2286 rep->assoc_stream = av_malloc_array(rep->ctx->nb_streams, sizeof(*rep->assoc_stream));
2287 if (!rep->assoc_stream)
2288 return AVERROR(ENOMEM);
2289 rep->nb_assoc_stream = rep->ctx->nb_streams;
2290 for (int j = 0; j < rep->ctx->nb_streams; j++) {
2292 rep->assoc_stream[j] = s->streams[rep->stream_index + j];
2293 }
2294 if (rep->bandwidth > 0)
2295 av_dict_set_int(&rep->assoc_stream[0]->metadata, "variant_bitrate", rep->bandwidth, 0);
2296 move_metadata(rep->assoc_stream[0], "id", &rep->id);
2297 move_metadata(rep->assoc_stream[0], "language", &rep->lang);
2298 }
2299 for (i = 0; i < c->n_subtitles; i++) {
2300 rep = c->subtitles[i];
2301 rep->assoc_stream = av_malloc_array(rep->ctx->nb_streams, sizeof(*rep->assoc_stream));
2302 if (!rep->assoc_stream)
2303 return AVERROR(ENOMEM);
2304 rep->nb_assoc_stream = rep->ctx->nb_streams;
2305 for (int j = 0; j < rep->ctx->nb_streams; j++) {
2307 rep->assoc_stream[j] = s->streams[rep->stream_index + j];
2308 }
2309 move_metadata(rep->assoc_stream[0], "id", &rep->id);
2310 move_metadata(rep->assoc_stream[0], "language", &rep->lang);
2311 }
2312
2313 /* Create stream groups if needed */
2314 for (i = 0; i < c->n_videos; i++) {
2315 struct representation *ref;
2316 rep = c->videos[i];
2317 if (!rep->dependencyid || !rep->codecs || !rep->nb_assoc_stream)
2318 continue;
2319 for (j = 0; j < c->n_videos; j++) {
2320 if (j == i)
2321 continue;
2322 ref = c->videos[j];
2323 if (!ref->nb_assoc_stream)
2324 continue;
2325 const AVDictionaryEntry *id = av_dict_get(ref->assoc_stream[0]->metadata, "id", NULL, AV_DICT_MATCH_CASE);
2326 if (id && !strcmp(rep->dependencyid, id->value))
2327 break;
2328 }
2329 if (j >= c->n_videos || !av_strstart(rep->codecs, "lvc1", NULL) ||
2331 continue;
2333 if (!stg)
2334 return AVERROR(ENOMEM);
2337 ret = avformat_stream_group_add_stream(stg, ref->assoc_stream[0]);
2338 if (ret < 0)
2339 return ret;
2341 if (ret < 0)
2342 return ret;
2343 stg->id = stg->index;
2344 stg->params.layered_video->el_index = stg->nb_entries - 1;
2345 }
2346
2347 return 0;
2348}
2349
2351{
2352 int i, j;
2353
2354 for (i = 0; i < n; i++) {
2355 struct representation *pls = p[i];
2356 int needed = !pls->nb_assoc_stream;
2357
2358 for (int j = 0; j < pls->nb_assoc_stream; j++)
2359 needed |= pls->assoc_stream[j]->discard < AVDISCARD_ALL;
2360
2361 if (needed && !pls->ctx) {
2362 pls->cur_seg_offset = 0;
2363 pls->init_sec_buf_read_offset = 0;
2364 /* Catch up */
2365 for (j = 0; j < n; j++) {
2366 pls->cur_seq_no = FFMAX(pls->cur_seq_no, p[j]->cur_seq_no);
2367 }
2369 av_log(s, AV_LOG_INFO, "Now receiving stream_index %d\n", pls->stream_index);
2370 } else if (!needed && pls->ctx) {
2372 ff_format_io_close(pls->parent, &pls->input);
2373 av_log(s, AV_LOG_INFO, "No longer receiving stream_index %d\n", pls->stream_index);
2374 }
2375 }
2376}
2377
2379{
2380 DASHContext *c = s->priv_data;
2381 int ret = 0, i;
2382 int64_t mints = 0;
2383 struct representation *cur = NULL;
2384 struct representation *rep = NULL;
2385
2386 recheck_discard_flags(s, c->videos, c->n_videos);
2387 recheck_discard_flags(s, c->audios, c->n_audios);
2388 recheck_discard_flags(s, c->subtitles, c->n_subtitles);
2389
2390 for (i = 0; i < c->n_videos; i++) {
2391 rep = c->videos[i];
2392 if (!rep->ctx)
2393 continue;
2394 if (!cur || rep->cur_timestamp < mints) {
2395 cur = rep;
2396 mints = rep->cur_timestamp;
2397 }
2398 }
2399 for (i = 0; i < c->n_audios; i++) {
2400 rep = c->audios[i];
2401 if (!rep->ctx)
2402 continue;
2403 if (!cur || rep->cur_timestamp < mints) {
2404 cur = rep;
2405 mints = rep->cur_timestamp;
2406 }
2407 }
2408
2409 for (i = 0; i < c->n_subtitles; i++) {
2410 rep = c->subtitles[i];
2411 if (!rep->ctx)
2412 continue;
2413 if (!cur || rep->cur_timestamp < mints) {
2414 cur = rep;
2415 mints = rep->cur_timestamp;
2416 }
2417 }
2418
2419 if (!cur) {
2420 return AVERROR_EOF;
2421 }
2422 while (!ff_check_interrupt(c->interrupt_callback) && !ret) {
2423 ret = av_read_frame(cur->ctx, pkt);
2424 if (ret >= 0) {
2425 /* If we got a packet, return it */
2426 cur->cur_timestamp = av_rescale(pkt->pts, (int64_t)cur->ctx->streams[0]->time_base.num * 90000, cur->ctx->streams[0]->time_base.den);
2427 pkt->stream_index += cur->stream_index;
2428 return 0;
2429 }
2430 if (cur->is_restart_needed) {
2431 cur->cur_seg_offset = 0;
2432 cur->init_sec_buf_read_offset = 0;
2433 cur->is_restart_needed = 0;
2434 ff_format_io_close(cur->parent, &cur->input);
2436 } else if (ret == AVERROR_EOF) {
2438 ff_format_io_close(cur->parent, &cur->input);
2439 av_log(s, AV_LOG_DEBUG, "EOF on stream_index %d\n", cur->stream_index);
2440 // prevent recheck_discard_flags() from re-enabling the component
2441 for (int i = 0; i < cur->nb_assoc_stream; i++)
2443 return FFERROR_REDO;
2444 }
2445 }
2446 return ret;
2447}
2448
2450{
2451 DASHContext *c = s->priv_data;
2455 av_dict_free(&c->avio_opts);
2456 av_freep(&c->base_url);
2457 return 0;
2458}
2459
2460static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos_msec, int flags, int dry_run)
2461{
2462 int ret = 0;
2463 int i = 0;
2464 int j = 0;
2465 int64_t duration = 0;
2466
2467 av_log(pls->parent, AV_LOG_VERBOSE, "DASH seek pos[%"PRId64"ms] %s\n",
2468 seek_pos_msec, dry_run ? " (dry)" : "");
2469
2470 // single fragment mode
2471 if (pls->n_fragments == 1) {
2472 pls->cur_timestamp = 0;
2473 pls->cur_seg_offset = 0;
2474 if (dry_run)
2475 return 0;
2477 return av_seek_frame(pls->ctx, -1, seek_pos_msec * 1000, flags);
2478 }
2479
2480 ff_format_io_close(pls->parent, &pls->input);
2481
2482 // find the nearest fragment
2483 if (pls->n_timelines > 0 && pls->fragment_timescale > 0) {
2484 int64_t num = pls->first_seq_no;
2485 av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline start n_timelines[%d] "
2486 "last_seq_no[%"PRId64"].\n",
2487 (int)pls->n_timelines, (int64_t)pls->last_seq_no);
2488 for (i = 0; i < pls->n_timelines; i++) {
2489 if (pls->timelines[i]->starttime > 0) {
2490 duration = pls->timelines[i]->starttime;
2491 }
2492 duration += pls->timelines[i]->duration;
2493 if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
2494 goto set_seq_num;
2495 }
2496 for (j = 0; j < pls->timelines[i]->repeat; j++) {
2497 duration += pls->timelines[i]->duration;
2498 num++;
2499 if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
2500 goto set_seq_num;
2501 }
2502 }
2503 num++;
2504 }
2505
2506set_seq_num:
2507 pls->cur_seq_no = num > pls->last_seq_no ? pls->last_seq_no : num;
2508 av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline end cur_seq_no[%"PRId64"].\n",
2509 (int64_t)pls->cur_seq_no);
2510 } else if (pls->fragment_duration > 0) {
2511 pls->cur_seq_no = pls->first_seq_no + ((seek_pos_msec * pls->fragment_timescale) / pls->fragment_duration) / 1000;
2512 } else {
2513 av_log(pls->parent, AV_LOG_ERROR, "dash_seek missing timeline or fragment_duration\n");
2514 pls->cur_seq_no = pls->first_seq_no;
2515 }
2516 pls->cur_timestamp = 0;
2517 pls->cur_seg_offset = 0;
2518 pls->init_sec_buf_read_offset = 0;
2519 ret = dry_run ? 0 : reopen_demux_for_component(s, pls);
2520
2521 return ret;
2522}
2523
2525{
2526 int ret = 0, i;
2527 DASHContext *c = s->priv_data;
2528 int64_t seek_pos_msec = av_rescale_rnd(timestamp, 1000,
2529 s->streams[stream_index]->time_base.den,
2532 if ((flags & AVSEEK_FLAG_BYTE) || c->is_live)
2533 return AVERROR(ENOSYS);
2534
2535 /* Seek in discarded streams with dry_run=1 to avoid reopening them */
2536 for (i = 0; i < c->n_videos; i++) {
2537 if (!ret)
2538 ret = dash_seek(s, c->videos[i], seek_pos_msec, flags, !c->videos[i]->ctx);
2539 }
2540 for (i = 0; i < c->n_audios; i++) {
2541 if (!ret)
2542 ret = dash_seek(s, c->audios[i], seek_pos_msec, flags, !c->audios[i]->ctx);
2543 }
2544 for (i = 0; i < c->n_subtitles; i++) {
2545 if (!ret)
2546 ret = dash_seek(s, c->subtitles[i], seek_pos_msec, flags, !c->subtitles[i]->ctx);
2547 }
2548
2549 return ret;
2550}
2551
2552static int dash_probe(const AVProbeData *p)
2553{
2554 if (!av_stristr(p->buf, "<MPD"))
2555 return 0;
2556
2557 if (av_stristr(p->buf, "dash:profile:isoff-on-demand:2011") ||
2558 av_stristr(p->buf, "dash:profile:isoff-live:2011") ||
2559 av_stristr(p->buf, "dash:profile:isoff-live:2012") ||
2560 av_stristr(p->buf, "dash:profile:isoff-main:2011") ||
2561 av_stristr(p->buf, "3GPP:PSS:profile:DASH1")) {
2562 return AVPROBE_SCORE_MAX;
2563 }
2564 if (av_stristr(p->buf, "dash:profile")) {
2565 return AVPROBE_SCORE_MAX;
2566 }
2567
2568 return 0;
2569}
2570
2571#define OFFSET(x) offsetof(DASHContext, x)
2572#define FLAGS AV_OPT_FLAG_DECODING_PARAM
2573static const AVOption dash_options[] = {
2574 {"allowed_extensions", "List of file extensions that dash is allowed to access",
2575 OFFSET(allowed_extensions), AV_OPT_TYPE_STRING,
2576 {.str = "aac,m4a,m4s,m4v,mov,mp4,webm,ts"},
2577 INT_MIN, INT_MAX, FLAGS},
2578 { "cenc_decryption_key", "Media default decryption key (hex)", OFFSET(cenc_decryption_key), AV_OPT_TYPE_STRING, {.str = NULL}, INT_MIN, INT_MAX, .flags = FLAGS },
2579 { "cenc_decryption_keys", "Media decryption keys by KID (hex)", OFFSET(cenc_decryption_keys), AV_OPT_TYPE_STRING, {.str = NULL}, INT_MIN, INT_MAX, .flags = FLAGS },
2580 { "max_reload", "Maximum number of manifest reloads in get_current_fragment() before giving up",
2581 OFFSET(max_reload), AV_OPT_TYPE_INT, { .i64 = 100 }, 0, INT_MAX, FLAGS },
2582 {NULL}
2583};
2584
2585static const AVClass dash_class = {
2586 .class_name = "dash",
2587 .item_name = av_default_item_name,
2588 .option = dash_options,
2589 .version = LIBAVUTIL_VERSION_INT,
2590};
2591
2593 .p.name = "dash",
2594 .p.long_name = NULL_IF_CONFIG_SMALL("Dynamic Adaptive Streaming over HTTP"),
2595 .p.priv_class = &dash_class,
2596 .p.flags = AVFMT_NO_BYTE_SEEK,
2597 .priv_data_size = sizeof(DASHContext),
2598 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
2604};
static double val(void *priv, double ch)
Definition aeval.c:77
const FFInputFormat ff_dash_demuxer
Definition dashdec.c:2592
static void finish(void)
static AVDictionary * opts
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition avassert.h:109
int ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition avformat.c:990
int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
Copies the whilelists from one context to the other.
Definition avformat.c:908
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:863
void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
Definition avformat.c:365
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:485
@ AV_STREAM_GROUP_PARAMS_LCEVC
Definition avformat.h:1162
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition avformat.h:508
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition avformat.h:2712
#define AVFMT_FLAG_CUSTOM_IO
The caller has supplied a custom AVIOContext, don't avio_close() it.
Definition avformat.h:1560
#define AVSEEK_FLAG_BACKWARD
seek backward
Definition avformat.h:2711
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrupt a blocking function associated with cb.
Definition avio.c:934
const char * avio_find_protocol_name(const char *url)
Return the name of the protocol that will handle the passed URL.
Definition avio.c:732
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_FLAG_READ
read-only
Definition avio.h:630
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
int avio_read_to_bprint(AVIOContext *h, struct AVBPrint *pb, size_t max_size)
Read contents of h into print buffer, up to max_size bytes, or up to EOF.
Definition aviobuf.c:1213
void ffio_init_context(FFIOContext *s, unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, const uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition aviobuf.c:50
int ffio_copy_url_options(AVIOContext *pb, AVDictionary **avio_opts)
Read url related dictionary options from the AVIOContext and write to the given dictionary.
Definition aviobuf.c:953
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition bprint.c:68
AVBPrint public header.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define FLAGS
Definition cmdutils.c:596
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Definition codec_par.c:107
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
void ff_dash_fill_tmpl_params(char *dst, size_t buffer_size, const char *template, int rep_id, int number, int bit_rate, int64_t time)
Definition dash.c:95
static int dash_read_header(AVFormatContext *s)
Definition dashdec.c:2176
#define SET_REPRESENTATION_SEQUENCE_BASE_INFO(arg, cnt)
Definition dashdec.c:865
static enum AVMediaType get_content_type(xmlNodePtr node)
Definition dashdec.c:569
static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url, AVDictionary **opts, AVDictionary *opts2, int *is_http)
Definition dashdec.c:418
#define INITIAL_BUFFER_SIZE
Definition dashdec.c:36
static int is_common_init_section_exist(struct representation **pls, int n_pls)
Definition dashdec.c:2126
static const AVOption dash_options[]
Definition dashdec.c:2573
static struct fragment * get_current_fragment(struct representation *pls)
Definition dashdec.c:1681
static int update_init_section(struct representation *pls)
Definition dashdec.c:1822
static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos_msec, int flags, int dry_run)
Definition dashdec.c:2460
static int read_data(void *opaque, uint8_t *buf, int buf_size)
Definition dashdec.c:1878
static int open_demux_for_component(AVFormatContext *s, struct representation *pls)
Definition dashdec.c:2049
static int parse_manifest_adaptationset_attr(AVFormatContext *s, xmlNodePtr adaptionset_node)
Definition dashdec.c:1186
static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representation *rep, xmlNodePtr fragmenturl_node, xmlNodePtr *baseurl_nodes, char *rep_id_val, char *rep_bandwidth_val)
Definition dashdec.c:618
static int64_t get_utc_date_time_insec(AVFormatContext *s, const char *datetime)
Definition dashdec.c:191
static void recheck_discard_flags(AVFormatContext *s, struct representation **p, int n)
Definition dashdec.c:2350
static void move_metadata(AVStream *st, const char *key, char **value)
Definition dashdec.c:2168
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)
Definition dashdec.c:902
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)
Definition dashdec.c:485
static int copy_init_section(struct representation *rep_dest, struct representation *rep_src)
Definition dashdec.c:2153
static const AVClass dash_class
Definition dashdec.c:2585
static int64_t get_segment_start_time_based_on_timeline(struct representation *pls, int64_t cur_seq_no)
Definition dashdec.c:265
static int reopen_demux_for_component(AVFormatContext *s, struct representation *pls)
Definition dashdec.c:1968
static int dash_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition dashdec.c:2378
static int64_t calc_min_seg_no(AVFormatContext *s, struct representation *pls)
Definition dashdec.c:1508
static int64_t get_current_time_in_sec(void)
Definition dashdec.c:186
static int ishttp(const char *url)
Definition dashdec.c:172
static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **opts)
Definition dashdec.c:1949
static void free_representation(struct representation *pls)
Definition dashdec.c:362
static void free_audio_list(DASHContext *c)
Definition dashdec.c:396
static uint32_t get_duration_insec(AVFormatContext *s, const char *duration)
Definition dashdec.c:221
static void move_timelines(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
Definition dashdec.c:1547
static int64_t calc_cur_seg_no(AVFormatContext *s, struct representation *pls)
Definition dashdec.c:1472
static void free_subtitle_list(DASHContext *c)
Definition dashdec.c:407
static int dash_close(AVFormatContext *s)
Definition dashdec.c:2449
static void close_demux_for_component(struct representation *pls)
Definition dashdec.c:1959
static int add_url_size(int *url_size, uint64_t len)
Definition dashdec.c:178
static int64_t seek_data(void *opaque, int64_t offset, int whence)
Definition dashdec.c:1868
static int dash_probe(const AVProbeData *p)
Definition dashdec.c:2552
static int open_input(DASHContext *c, struct representation *pls, struct fragment *seg)
Definition dashdec.c:1790
static xmlNodePtr find_child_node_by_name(xmlNodePtr rootnode, const char *nodename)
Definition dashdec.c:552
static int refresh_manifest(AVFormatContext *s)
Definition dashdec.c:1582
static struct fragment * get_fragment(AVFormatContext *s, char *range)
Definition dashdec.c:595
static int64_t calc_next_seg_no_from_timelines(struct representation *pls, int64_t cur_time)
Definition dashdec.c:301
static void free_fragment(struct fragment **seg)
Definition dashdec.c:331
static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
Definition dashdec.c:1284
static void move_segments(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
Definition dashdec.c:1561
#define OFFSET(x)
Definition dashdec.c:2571
static int64_t calc_max_seg_no(struct representation *pls, DASHContext *c)
Definition dashdec.c:1522
static int read_from_url(struct representation *pls, struct fragment *seg, uint8_t *buf, int buf_size)
Definition dashdec.c:1771
static int parse_programinformation(AVFormatContext *s, xmlNodePtr node)
Definition dashdec.c:1255
static char * get_val_from_nodes_tab(xmlNodePtr *nodes, const int n_nodes, const char *attrname)
Definition dashdec.c:536
static int dash_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition dashdec.c:2524
static int parse_manifest_adaptationset(AVFormatContext *s, const char *url, xmlNodePtr adaptionset_node, xmlNodePtr mpd_baseurl_node, xmlNodePtr period_baseurl_node, xmlNodePtr period_segmenttemplate_node, xmlNodePtr period_segmentlist_node)
Definition dashdec.c:1199
static void free_fragment_list(struct representation *pls)
Definition dashdec.c:340
static int parse_manifest_segmenttimeline(AVFormatContext *s, struct representation *rep, xmlNodePtr fragment_timeline_node)
Definition dashdec.c:684
static void free_timelines_list(struct representation *pls)
Definition dashdec.c:351
static void free_video_list(DASHContext *c)
Definition dashdec.c:385
static int resolve_content_path(AVFormatContext *s, const char *url, int *max_url_size, xmlNodePtr *baseurl_nodes, int n_baseurl_nodes)
Definition dashdec.c:725
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
void ff_read_frame_flush(AVFormatContext *s)
Flush the frame reader.
Definition seek.c:719
#define FFERROR_REDO
Returned by demuxers to indicate that data was consumed but discarded (ignored streams or junk data).
Definition demux.h:224
static AVPacket * pkt
enum AVCodecID id
Definition dts2pts.c:607
double value
Definition eval.c:102
const char * key
static int64_t duration
Definition ffplay.c:359
char * path
Definition ffplay.c:385
int ret
Definition ffplay.c:391
static int64_t start_time
Definition ffplay.c:358
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
static av_cold void cleanup(FlashSV2Context *s)
#define fail
Definition test.h:479
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
@ AV_CODEC_ID_LCEVC
Definition codec_id.h:614
@ AVDISCARD_ALL
discard all
Definition defs.h:246
AVProgram * av_new_program(AVFormatContext *ac, int id)
Definition avformat.c:297
AVStreamGroup * avformat_stream_group_create(AVFormatContext *s, enum AVStreamGroupParamsType type, AVDictionary **options)
Add a new empty stream group to a media file.
Definition options.c:472
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int avformat_stream_group_add_stream(AVStreamGroup *stg, AVStream *st)
Add an already allocated stream to a stream group.
Definition options.c:586
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition options.c:165
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition avformat.c:164
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition demux.c:1612
int av_probe_input_buffer(AVIOContext *pb, const AVInputFormat **fmt, const char *url, void *logctx, unsigned int offset, unsigned int max_probe_size)
Like av_probe_input_buffer2() but returns 0 on success.
Definition format.c:348
int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Seek to the keyframe at timestamp.
Definition seek.c:641
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition demux.c:231
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition demux.c:2640
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition demux.c:400
int av_match_ext(const char *filename, const char *extensions)
Return a positive value if the given filename has one of the given extensions, 0 otherwise.
Definition format.c:41
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition bprint.c:234
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:236
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:61
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition dict.c:250
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition dict.h:79
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:87
#define AV_DICT_MATCH_CASE
Only get an entry with exact-case key match.
Definition dict.h:74
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set() that converts the value to a string and stores it.
Definition dict.c:180
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition error.h:58
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:247
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition rational.h:71
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition mathematics.c:58
@ AV_ROUND_DOWN
Round toward -infinity.
@ AV_ROUND_UP
Round toward +infinity.
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
Definition mem.c:419
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition mem.c:661
AVMediaType
Definition avutil.h:198
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AVMEDIA_TYPE_UNKNOWN
Usually treated as AVMEDIA_TYPE_DATA.
Definition avutil.h:199
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes,...
Definition avstring.c:95
char * av_stristr(const char *s1, const char *s2)
Locate the first case-independent occurrence in the string haystack of the string needle.
Definition avstring.c:58
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition avstring.c:179
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition avstring.c:208
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition avstring.c:36
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition avstring.c:85
char * av_strireplace(const char *str, const char *from, const char *to)
Locale-independent strings replace.
Definition avstring.c:230
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition avstring.c:218
#define AV_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
Definition opt.c:1295
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition opt.h:604
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
Definition opt.c:2230
uint32_t type
Definition jpegmpfenc.c:80
unsigned offset
Definition libaomenc.c:763
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:370
static av_always_inline const FFStream * cffstream(const AVStream *st)
Definition internal.h:375
#define MAX_URL_SIZE
Definition internal.h:30
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define FFALIGN(x, a)
Definition macros.h:78
enum AVColorRange range
Memory handling functions.
#define av_strdup(s)
Definition ops_static.c:55
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition parseutils.c:181
time_t av_timegm(struct tm *tm)
Convert the decomposed UTC time in tm to a time_t value.
Definition parseutils.c:573
misc parsing utilities
Describe the class of an AVClass context structure.
Definition log.h:76
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1400
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition avformat.h:1456
AVStreamGroup ** stream_groups
A list of all stream groups in the file.
Definition avformat.h:1488
AVIOContext * pb
I/O context.
Definition avformat.h:1442
int flags
Flags modifying the (de)muxer behaviour.
Definition avformat.h:1552
int64_t max_analyze_duration
Maximum duration (in AV_TIME_BASE units) of the data read from input in avformat_find_stream_info().
Definition avformat.h:1608
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition avformat.h:1686
unsigned int nb_stream_groups
Number of elements in AVFormatContext.stream_groups.
Definition avformat.h:1475
void * priv_data
Format private data.
Definition avformat.h:1428
int64_t probesize
Maximum number of bytes read from input in order to determine stream properties.
Definition avformat.h:1600
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1468
int(* io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
A callback for opening new IO streams.
Definition avformat.h:2021
Bytestream IO Context.
Definition avio.h:160
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition avio.h:261
unsigned char * buffer
Start of the buffer.
Definition avio.h:225
Callback for checking whether to abort blocking functions.
Definition avio.h:59
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:587
This structure contains the data a format has to probe a file.
Definition avformat.h:473
New fields can be added to the end with minor version bumps.
Definition avformat.h:1324
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
AVStream * stream
Definition avformat.h:1179
AVStreamGroupLayeredVideo is meant to define the relation between a base layer video stream and a sep...
Definition avformat.h:1104
int height
Height of the final image for presentation.
Definition avformat.h:1129
unsigned int el_index
Index of the enhancement layer stream in AVStreamGroup.
Definition avformat.h:1113
int width
Width of the final stream for presentation.
Definition avformat.h:1125
unsigned int nb_entries
Number of elements in AVStreamGroup.entries.
Definition avformat.h:1285
enum AVStreamGroupParamsType type
Group type.
Definition avformat.h:1211
AVStreamGroupEntry ** entries
A list of streams or child groups in the group.
Definition avformat.h:1298
unsigned int index
Group index in AVFormatContext.
Definition avformat.h:1195
union AVStreamGroup::@313244315204024013007202273061153315146016101155 params
Group type-specific parameters.
int disposition
Stream group disposition - a combination of AV_DISPOSITION_* flags.
Definition avformat.h:1277
int64_t id
Group type-specific group ID.
Definition avformat.h:1203
struct AVStreamGroupLayeredVideo * layered_video
Definition avformat.h:1220
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition avformat.h:839
AVDictionary * metadata
Definition avformat.h:848
int id
Format-specific stream ID.
Definition avformat.h:780
int index
stream index in AVFormatContext
Definition avformat.h:774
int pts_wrap_bits
Number of bits in timestamps.
Definition avformat.h:911
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:807
AVRational r_frame_rate
Real base framerate of the stream.
Definition avformat.h:902
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition avformat.h:837
int is_init_section_common_video
Definition dashdec.c:166
int64_t availability_end_time
Definition dashdec.c:143
int max_url_size
Definition dashdec.c:160
char * adaptionset_lang
Definition dashdec.c:154
int64_t minimum_update_period
Definition dashdec.c:145
int is_init_section_common_audio
Definition dashdec.c:167
int64_t media_presentation_duration
Definition dashdec.c:140
int64_t suggested_presentation_delay
Definition dashdec.c:141
AVDictionary * avio_opts
Definition dashdec.c:159
char * cenc_decryption_key
Definition dashdec.c:162
int n_subtitles
Definition dashdec.c:136
uint64_t period_start
Definition dashdec.c:151
int64_t publish_time
Definition dashdec.c:144
struct representation ** videos
Definition dashdec.c:133
struct representation ** subtitles
Definition dashdec.c:137
char * base_url
Definition dashdec.c:130
int n_audios
Definition dashdec.c:134
int is_init_section_common_subtitle
Definition dashdec.c:168
char * allowed_extensions
Definition dashdec.c:158
int64_t min_buffer_time
Definition dashdec.c:147
int64_t availability_start_time
Definition dashdec.c:142
int is_live
Definition dashdec.c:156
int64_t time_shift_buffer_depth
Definition dashdec.c:146
int n_videos
Definition dashdec.c:132
AVIOInterruptCB * interrupt_callback
Definition dashdec.c:157
char * cenc_decryption_keys
Definition dashdec.c:163
uint64_t period_duration
Definition dashdec.c:150
int max_reload
Definition dashdec.c:161
struct representation ** audios
Definition dashdec.c:135
AVIOContext pub
enum AVStreamParseType need_parsing
Definition internal.h:326
char * url
Definition dashdec.c:41
int64_t url_offset
Definition dashdec.c:39
int64_t size
Definition dashdec.c:40
char * url_template
Definition dashdec.c:81
int n_open_failures
Definition dashdec.c:116
char * lang
Definition dashdec.c:89
int64_t last_seq_no
Definition dashdec.c:104
char * codecs
Definition dashdec.c:92
AVRational framerate
Definition dashdec.c:93
uint32_t init_sec_data_len
Definition dashdec.c:122
int64_t cur_seg_offset
Definition dashdec.c:113
AVIOContext * input
Definition dashdec.c:83
struct timeline ** timelines
Definition dashdec.c:101
struct fragment * cur_seg
Definition dashdec.c:115
char * dependencyid
Definition dashdec.c:91
int64_t first_seq_no
Definition dashdec.c:103
FFIOContext pb
Definition dashdec.c:82
char * id
Definition dashdec.c:88
uint32_t init_sec_buf_size
Definition dashdec.c:121
int64_t cur_timestamp
Definition dashdec.c:124
uint32_t init_sec_buf_read_offset
Definition dashdec.c:123
int64_t presentation_timeoffset
Definition dashdec.c:110
int n_fragments
Definition dashdec.c:97
int64_t cur_seg_size
Definition dashdec.c:114
AVStream ** assoc_stream
Definition dashdec.c:94
struct fragment ** fragments
Definition dashdec.c:98
int64_t fragment_timescale
Definition dashdec.c:108
struct fragment * init_section
Definition dashdec.c:119
int stream_index
Definition dashdec.c:86
uint8_t * init_sec_buf
Definition dashdec.c:120
int64_t start_number
Definition dashdec.c:105
AVFormatContext * ctx
Definition dashdec.c:85
int is_restart_needed
Definition dashdec.c:125
int nb_assoc_stream
Definition dashdec.c:95
AVFormatContext * parent
Definition dashdec.c:84
int64_t fragment_duration
Definition dashdec.c:107
int64_t cur_seq_no
Definition dashdec.c:112
int64_t repeat
Definition dashdec.c:68
int64_t starttime
Definition dashdec.c:62
int64_t duration
Definition dashdec.c:72
#define av_free(p)
#define av_malloc_array(a, b)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
int num
Definition error.c:23
static int ref[MAX_W *MAX_W]
int64_t av_gettime(void)
Get the current time in microseconds.
Definition time.c:40
int size
int ff_make_absolute_url(char *buf, int size, const char *base, const char *rel)
Convert a relative url into an absolute url, given a base url.
Definition url.c:321
unbuffered private I/O API
int len
static double c[64]