2 * Dynamic Adaptive Streaming over HTTP demux
3 * Copyright (c) 2017 samsamsam@o2.pl based on HLS demux
4 * Copyright (c) 2017 Steven Liu
6 * This file is part of FFmpeg.
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.
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.
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
22 #include <libxml/parser.h>
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/time.h"
26 #include "libavutil/parseutils.h"
28 #include "avio_internal.h"
31 #define INITIAL_BUFFER_SIZE 32768
32 #define MAX_BPRINT_READ_SIZE (UINT_MAX - 1)
33 #define DEFAULT_MANIFEST_SIZE 8 * 1024
42 * reference to : ISO_IEC_23009-1-DASH-2012
44 * Table: Table 17 — Semantics of SegmentTimeline element
47 /* starttime: Element or Attribute Name
48 * specifies the MPD start time, in @timescale units,
49 * the first Segment in the series starts relative to the beginning of the Period.
50 * The value of this attribute must be equal to or greater than the sum of the previous S
51 * element earliest presentation time and the sum of the contiguous Segment durations.
52 * If the value of the attribute is greater than what is expressed by the previous S element,
53 * it expresses discontinuities in the timeline.
54 * If not present then the value shall be assumed to be zero for the first S element
55 * and for the subsequent S elements, the value shall be assumed to be the sum of
56 * the previous S element's earliest presentation time and contiguous duration
57 * (i.e. previous S@starttime + @duration * (@repeat + 1)).
60 /* repeat: Element or Attribute Name
61 * specifies the repeat count of the number of following contiguous Segments with
62 * the same duration expressed by the value of @duration. This value is zero-based
63 * (e.g. a value of three means four Segments in the contiguous series).
66 /* duration: Element or Attribute Name
67 * specifies the Segment duration, in units of the value of the @timescale.
73 * Each playlist has its own demuxer. If it is currently active,
74 * it has an opened AVIOContext too, and potentially an AVPacket
75 * containing the next packet from this stream.
77 struct representation {
81 AVFormatContext *parent;
89 AVStream *assoc_stream; /* demuxer stream associated with this representation */
92 struct fragment **fragments; /* VOD list of fragment for profile */
95 struct timeline **timelines;
99 int64_t start_number; /* used in case when we have dynamic list of segment to know which segments are new one*/
101 int64_t fragment_duration;
102 int64_t fragment_timescale;
104 int64_t presentation_timeoffset;
107 int64_t cur_seg_offset;
108 int64_t cur_seg_size;
109 struct fragment *cur_seg;
111 /* Currently active Media Initialization Section */
112 struct fragment *init_section;
113 uint8_t *init_sec_buf;
114 uint32_t init_sec_buf_size;
115 uint32_t init_sec_data_len;
116 uint32_t init_sec_buf_read_offset;
117 int64_t cur_timestamp;
118 int is_restart_needed;
121 typedef struct DASHContext {
122 const AVClass *class;
126 struct representation **videos;
128 struct representation **audios;
130 struct representation **subtitles;
132 /* MediaPresentationDescription Attribute */
133 uint64_t media_presentation_duration;
134 uint64_t suggested_presentation_delay;
135 uint64_t availability_start_time;
136 uint64_t availability_end_time;
137 uint64_t publish_time;
138 uint64_t minimum_update_period;
139 uint64_t time_shift_buffer_depth;
140 uint64_t min_buffer_time;
142 /* Period Attribute */
143 uint64_t period_duration;
144 uint64_t period_start;
146 /* AdaptationSet Attribute */
147 char *adaptionset_lang;
150 AVIOInterruptCB *interrupt_callback;
151 char *allowed_extensions;
152 AVDictionary *avio_opts;
155 /* Flags for init section*/
156 int is_init_section_common_video;
157 int is_init_section_common_audio;
161 static int ishttp(char *url)
163 const char *proto_name = avio_find_protocol_name(url);
164 return av_strstart(proto_name, "http", NULL);
167 static int aligned(int val)
169 return ((val + 0x3F) >> 6) << 6;
172 static uint64_t get_current_time_in_sec(void)
174 return av_gettime() / 1000000;
177 static uint64_t get_utc_date_time_insec(AVFormatContext *s, const char *datetime)
188 /* ISO-8601 date parser */
192 ret = sscanf(datetime, "%d-%d-%dT%d:%d:%fZ", &year, &month, &day, &hour, &minute, &second);
193 /* year, month, day, hour, minute, second 6 arguments */
195 av_log(s, AV_LOG_WARNING, "get_utc_date_time_insec get a wrong time format\n");
197 timeinfo.tm_year = year - 1900;
198 timeinfo.tm_mon = month - 1;
199 timeinfo.tm_mday = day;
200 timeinfo.tm_hour = hour;
201 timeinfo.tm_min = minute;
202 timeinfo.tm_sec = (int)second;
204 return av_timegm(&timeinfo);
207 static uint32_t get_duration_insec(AVFormatContext *s, const char *duration)
209 /* ISO-8601 duration parser */
217 const char *ptr = duration;
220 if (*ptr == 'P' || *ptr == 'T') {
225 if (sscanf(ptr, "%f%c%n", &value, &type, &size) != 2) {
226 av_log(s, AV_LOG_WARNING, "get_duration_insec get a wrong time format\n");
227 return 0; /* parser error */
231 days = (uint32_t)value;
234 hours = (uint32_t)value;
237 mins = (uint32_t)value;
240 secs = (uint32_t)value;
243 // handle invalid type
248 return ((days * 24 + hours) * 60 + mins) * 60 + secs;
251 static int64_t get_segment_start_time_based_on_timeline(struct representation *pls, int64_t cur_seq_no)
253 int64_t start_time = 0;
258 if (pls->n_timelines) {
259 for (i = 0; i < pls->n_timelines; i++) {
260 if (pls->timelines[i]->starttime > 0) {
261 start_time = pls->timelines[i]->starttime;
263 if (num == cur_seq_no)
266 start_time += pls->timelines[i]->duration;
268 if (pls->timelines[i]->repeat == -1) {
269 start_time = pls->timelines[i]->duration * cur_seq_no;
273 for (j = 0; j < pls->timelines[i]->repeat; j++) {
275 if (num == cur_seq_no)
277 start_time += pls->timelines[i]->duration;
286 static int64_t calc_next_seg_no_from_timelines(struct representation *pls, int64_t cur_time)
291 int64_t start_time = 0;
293 for (i = 0; i < pls->n_timelines; i++) {
294 if (pls->timelines[i]->starttime > 0) {
295 start_time = pls->timelines[i]->starttime;
297 if (start_time > cur_time)
300 start_time += pls->timelines[i]->duration;
301 for (j = 0; j < pls->timelines[i]->repeat; j++) {
303 if (start_time > cur_time)
305 start_time += pls->timelines[i]->duration;
316 static void free_fragment(struct fragment **seg)
321 av_freep(&(*seg)->url);
325 static void free_fragment_list(struct representation *pls)
329 for (i = 0; i < pls->n_fragments; i++) {
330 free_fragment(&pls->fragments[i]);
332 av_freep(&pls->fragments);
333 pls->n_fragments = 0;
336 static void free_timelines_list(struct representation *pls)
340 for (i = 0; i < pls->n_timelines; i++) {
341 av_freep(&pls->timelines[i]);
343 av_freep(&pls->timelines);
344 pls->n_timelines = 0;
347 static void free_representation(struct representation *pls)
349 free_fragment_list(pls);
350 free_timelines_list(pls);
351 free_fragment(&pls->cur_seg);
352 free_fragment(&pls->init_section);
353 av_freep(&pls->init_sec_buf);
354 av_freep(&pls->pb.buffer);
355 ff_format_io_close(pls->parent, &pls->input);
358 avformat_close_input(&pls->ctx);
361 av_freep(&pls->url_template);
365 static void free_video_list(DASHContext *c)
368 for (i = 0; i < c->n_videos; i++) {
369 struct representation *pls = c->videos[i];
370 free_representation(pls);
372 av_freep(&c->videos);
376 static void free_audio_list(DASHContext *c)
379 for (i = 0; i < c->n_audios; i++) {
380 struct representation *pls = c->audios[i];
381 free_representation(pls);
383 av_freep(&c->audios);
387 static void free_subtitle_list(DASHContext *c)
390 for (i = 0; i < c->n_subtitles; i++) {
391 struct representation *pls = c->subtitles[i];
392 free_representation(pls);
394 av_freep(&c->subtitles);
398 static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
399 AVDictionary **opts, AVDictionary *opts2, int *is_http)
401 DASHContext *c = s->priv_data;
402 AVDictionary *tmp = NULL;
403 const char *proto_name = NULL;
406 if (av_strstart(url, "crypto", NULL)) {
407 if (url[6] == '+' || url[6] == ':')
408 proto_name = avio_find_protocol_name(url + 7);
412 proto_name = avio_find_protocol_name(url);
415 return AVERROR_INVALIDDATA;
417 // only http(s) & file are allowed
418 if (av_strstart(proto_name, "file", NULL)) {
419 if (strcmp(c->allowed_extensions, "ALL") && !av_match_ext(url, c->allowed_extensions)) {
420 av_log(s, AV_LOG_ERROR,
421 "Filename extension of \'%s\' is not a common multimedia extension, blocked for security reasons.\n"
422 "If you wish to override this adjust allowed_extensions, you can set it to \'ALL\' to allow all\n",
424 return AVERROR_INVALIDDATA;
426 } else if (av_strstart(proto_name, "http", NULL)) {
429 return AVERROR_INVALIDDATA;
431 if (!strncmp(proto_name, url, strlen(proto_name)) && url[strlen(proto_name)] == ':')
433 else if (av_strstart(url, "crypto", NULL) && !strncmp(proto_name, url + 7, strlen(proto_name)) && url[7 + strlen(proto_name)] == ':')
435 else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5))
436 return AVERROR_INVALIDDATA;
439 av_dict_copy(&tmp, *opts, 0);
440 av_dict_copy(&tmp, opts2, 0);
441 ret = avio_open2(pb, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp);
443 // update cookies on http response with setcookies.
444 char *new_cookies = NULL;
446 if (!(s->flags & AVFMT_FLAG_CUSTOM_IO))
447 av_opt_get(*pb, "cookies", AV_OPT_SEARCH_CHILDREN, (uint8_t**)&new_cookies);
450 av_dict_set(opts, "cookies", new_cookies, AV_DICT_DONT_STRDUP_VAL);
458 *is_http = av_strstart(proto_name, "http", NULL);
463 static char *get_content_url(xmlNodePtr *baseurl_nodes,
467 char *rep_bandwidth_val,
473 char *tmp_str = av_mallocz(max_url_size);
474 char *tmp_str_2 = av_mallocz(max_url_size);
476 if (!tmp_str || !tmp_str_2) {
480 for (i = 0; i < n_baseurl_nodes; ++i) {
481 if (baseurl_nodes[i] &&
482 baseurl_nodes[i]->children &&
483 baseurl_nodes[i]->children->type == XML_TEXT_NODE) {
484 text = xmlNodeGetContent(baseurl_nodes[i]->children);
486 memset(tmp_str, 0, max_url_size);
487 memset(tmp_str_2, 0, max_url_size);
488 ff_make_absolute_url(tmp_str_2, max_url_size, tmp_str, text);
489 av_strlcpy(tmp_str, tmp_str_2, max_url_size);
496 ff_make_absolute_url(tmp_str, max_url_size, tmp_str, val);
499 url = av_strireplace(tmp_str, "$RepresentationID$", (const char*)rep_id_val);
503 av_strlcpy(tmp_str, url, max_url_size);
505 if (rep_bandwidth_val && tmp_str[0] != '\0') {
506 // free any previously assigned url before reassigning
508 url = av_strireplace(tmp_str, "$Bandwidth$", (const char*)rep_bandwidth_val);
519 static char *get_val_from_nodes_tab(xmlNodePtr *nodes, const int n_nodes, const char *attrname)
524 for (i = 0; i < n_nodes; ++i) {
526 val = xmlGetProp(nodes[i], attrname);
535 static xmlNodePtr find_child_node_by_name(xmlNodePtr rootnode, const char *nodename)
537 xmlNodePtr node = rootnode;
542 node = xmlFirstElementChild(node);
544 if (!av_strcasecmp(node->name, nodename)) {
547 node = xmlNextElementSibling(node);
552 static enum AVMediaType get_content_type(xmlNodePtr node)
554 enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
560 for (i = 0; i < 2; i++) {
561 attr = i ? "mimeType" : "contentType";
562 val = xmlGetProp(node, attr);
564 if (av_stristr((const char *)val, "video")) {
565 type = AVMEDIA_TYPE_VIDEO;
566 } else if (av_stristr((const char *)val, "audio")) {
567 type = AVMEDIA_TYPE_AUDIO;
568 } else if (av_stristr((const char *)val, "text")) {
569 type = AVMEDIA_TYPE_SUBTITLE;
578 static struct fragment * get_Fragment(char *range)
580 struct fragment * seg = av_mallocz(sizeof(struct fragment));
587 char *str_end_offset;
588 char *str_offset = av_strtok(range, "-", &str_end_offset);
589 seg->url_offset = strtoll(str_offset, NULL, 10);
590 seg->size = strtoll(str_end_offset, NULL, 10) - seg->url_offset + 1;
596 static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representation *rep,
597 xmlNodePtr fragmenturl_node,
598 xmlNodePtr *baseurl_nodes,
600 char *rep_bandwidth_val)
602 DASHContext *c = s->priv_data;
603 char *initialization_val = NULL;
604 char *media_val = NULL;
605 char *range_val = NULL;
606 int max_url_size = c ? c->max_url_size: MAX_URL_SIZE;
608 if (!av_strcasecmp(fragmenturl_node->name, (const char *)"Initialization")) {
609 initialization_val = xmlGetProp(fragmenturl_node, "sourceURL");
610 range_val = xmlGetProp(fragmenturl_node, "range");
611 if (initialization_val || range_val) {
612 free_fragment(&rep->init_section);
613 rep->init_section = get_Fragment(range_val);
615 if (!rep->init_section) {
616 xmlFree(initialization_val);
617 return AVERROR(ENOMEM);
619 rep->init_section->url = get_content_url(baseurl_nodes, 4,
624 xmlFree(initialization_val);
625 if (!rep->init_section->url) {
626 av_freep(&rep->init_section);
627 return AVERROR(ENOMEM);
630 } else if (!av_strcasecmp(fragmenturl_node->name, (const char *)"SegmentURL")) {
631 media_val = xmlGetProp(fragmenturl_node, "media");
632 range_val = xmlGetProp(fragmenturl_node, "mediaRange");
633 if (media_val || range_val) {
634 struct fragment *seg = get_Fragment(range_val);
638 return AVERROR(ENOMEM);
640 seg->url = get_content_url(baseurl_nodes, 4,
648 return AVERROR(ENOMEM);
650 dynarray_add(&rep->fragments, &rep->n_fragments, seg);
657 static int parse_manifest_segmenttimeline(AVFormatContext *s, struct representation *rep,
658 xmlNodePtr fragment_timeline_node)
660 xmlAttrPtr attr = NULL;
663 if (!av_strcasecmp(fragment_timeline_node->name, (const char *)"S")) {
664 struct timeline *tml = av_mallocz(sizeof(struct timeline));
666 return AVERROR(ENOMEM);
668 attr = fragment_timeline_node->properties;
670 val = xmlGetProp(fragment_timeline_node, attr->name);
673 av_log(s, AV_LOG_WARNING, "parse_manifest_segmenttimeline attr->name = %s val is NULL\n", attr->name);
677 if (!av_strcasecmp(attr->name, (const char *)"t")) {
678 tml->starttime = (int64_t)strtoll(val, NULL, 10);
679 } else if (!av_strcasecmp(attr->name, (const char *)"r")) {
680 tml->repeat =(int64_t) strtoll(val, NULL, 10);
681 } else if (!av_strcasecmp(attr->name, (const char *)"d")) {
682 tml->duration = (int64_t)strtoll(val, NULL, 10);
687 dynarray_add(&rep->timelines, &rep->n_timelines, tml);
693 static int resolve_content_path(AVFormatContext *s, const char *url, int *max_url_size, xmlNodePtr *baseurl_nodes, int n_baseurl_nodes)
695 char *tmp_str = NULL;
697 char *mpdName = NULL;
698 xmlNodePtr node = NULL;
699 char *baseurl = NULL;
700 char *root_url = NULL;
710 int tmp_max_url_size = strlen(url);
712 for (i = n_baseurl_nodes-1; i >= 0 ; i--) {
713 text = xmlNodeGetContent(baseurl_nodes[i]);
716 tmp_max_url_size += strlen(text);
724 tmp_max_url_size = aligned(tmp_max_url_size);
725 text = av_mallocz(tmp_max_url_size);
727 updated = AVERROR(ENOMEM);
730 av_strlcpy(text, url, strlen(url)+1);
732 while (mpdName = av_strtok(tmp, "/", &tmp)) {
733 size = strlen(mpdName);
737 path = av_mallocz(tmp_max_url_size);
738 tmp_str = av_mallocz(tmp_max_url_size);
739 if (!tmp_str || !path) {
740 updated = AVERROR(ENOMEM);
744 av_strlcpy (path, url, strlen(url) - size + 1);
745 for (rootId = n_baseurl_nodes - 1; rootId > 0; rootId --) {
746 if (!(node = baseurl_nodes[rootId])) {
749 text = xmlNodeGetContent(node);
757 node = baseurl_nodes[rootId];
758 baseurl = xmlNodeGetContent(node);
759 root_url = (av_strcasecmp(baseurl, "")) ? baseurl : path;
761 xmlNodeSetContent(node, root_url);
765 size = strlen(root_url);
766 isRootHttp = ishttp(root_url);
768 if (root_url[size - 1] != token) {
769 av_strlcat(root_url, "/", size + 2);
773 for (i = 0; i < n_baseurl_nodes; ++i) {
777 text = xmlNodeGetContent(baseurl_nodes[i]);
778 if (text && !av_strstart(text, "/", NULL)) {
779 memset(tmp_str, 0, strlen(tmp_str));
780 if (!ishttp(text) && isRootHttp) {
781 av_strlcpy(tmp_str, root_url, size + 1);
783 start = (text[0] == token);
784 if (start && av_stristr(tmp_str, text)) {
786 if (!av_strncasecmp(tmp_str, "http://", 7)) {
788 } else if (!av_strncasecmp(tmp_str, "https://", 8)) {
792 memset(p + 1, 0, strlen(p));
794 av_strlcat(tmp_str, text + start, tmp_max_url_size);
795 xmlNodeSetContent(baseurl_nodes[i], tmp_str);
802 if (tmp_max_url_size > *max_url_size) {
803 *max_url_size = tmp_max_url_size;
812 static int parse_manifest_representation(AVFormatContext *s, const char *url,
814 xmlNodePtr adaptionset_node,
815 xmlNodePtr mpd_baseurl_node,
816 xmlNodePtr period_baseurl_node,
817 xmlNodePtr period_segmenttemplate_node,
818 xmlNodePtr period_segmentlist_node,
819 xmlNodePtr fragment_template_node,
820 xmlNodePtr content_component_node,
821 xmlNodePtr adaptionset_baseurl_node,
822 xmlNodePtr adaptionset_segmentlist_node,
823 xmlNodePtr adaptionset_supplementalproperty_node)
826 DASHContext *c = s->priv_data;
827 struct representation *rep = NULL;
828 struct fragment *seg = NULL;
829 xmlNodePtr representation_segmenttemplate_node = NULL;
830 xmlNodePtr representation_baseurl_node = NULL;
831 xmlNodePtr representation_segmentlist_node = NULL;
832 xmlNodePtr segmentlists_tab[3];
833 xmlNodePtr fragment_timeline_node = NULL;
834 xmlNodePtr fragment_templates_tab[5];
835 char *duration_val = NULL;
836 char *presentation_timeoffset_val = NULL;
837 char *startnumber_val = NULL;
838 char *timescale_val = NULL;
839 char *initialization_val = NULL;
840 char *media_val = NULL;
842 xmlNodePtr baseurl_nodes[4];
843 xmlNodePtr representation_node = node;
844 char *rep_id_val = xmlGetProp(representation_node, "id");
845 char *rep_bandwidth_val = xmlGetProp(representation_node, "bandwidth");
846 char *rep_framerate_val = xmlGetProp(representation_node, "frameRate");
847 enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
849 // try get information from representation
850 if (type == AVMEDIA_TYPE_UNKNOWN)
851 type = get_content_type(representation_node);
852 // try get information from contentComponen
853 if (type == AVMEDIA_TYPE_UNKNOWN)
854 type = get_content_type(content_component_node);
855 // try get information from adaption set
856 if (type == AVMEDIA_TYPE_UNKNOWN)
857 type = get_content_type(adaptionset_node);
858 if (type == AVMEDIA_TYPE_UNKNOWN) {
859 av_log(s, AV_LOG_VERBOSE, "Parsing '%s' - skipp not supported representation type\n", url);
860 } else if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO || type == AVMEDIA_TYPE_SUBTITLE) {
861 // convert selected representation to our internal struct
862 rep = av_mallocz(sizeof(struct representation));
864 ret = AVERROR(ENOMEM);
867 if (c->adaptionset_lang) {
868 rep->lang = av_strdup(c->adaptionset_lang);
870 av_log(s, AV_LOG_ERROR, "alloc language memory failure\n");
872 ret = AVERROR(ENOMEM);
877 representation_segmenttemplate_node = find_child_node_by_name(representation_node, "SegmentTemplate");
878 representation_baseurl_node = find_child_node_by_name(representation_node, "BaseURL");
879 representation_segmentlist_node = find_child_node_by_name(representation_node, "SegmentList");
881 baseurl_nodes[0] = mpd_baseurl_node;
882 baseurl_nodes[1] = period_baseurl_node;
883 baseurl_nodes[2] = adaptionset_baseurl_node;
884 baseurl_nodes[3] = representation_baseurl_node;
886 ret = resolve_content_path(s, url, &c->max_url_size, baseurl_nodes, 4);
887 c->max_url_size = aligned(c->max_url_size
888 + (rep_id_val ? strlen(rep_id_val) : 0)
889 + (rep_bandwidth_val ? strlen(rep_bandwidth_val) : 0));
890 if (ret == AVERROR(ENOMEM) || ret == 0)
892 if (representation_segmenttemplate_node || fragment_template_node || period_segmenttemplate_node) {
893 fragment_timeline_node = NULL;
894 fragment_templates_tab[0] = representation_segmenttemplate_node;
895 fragment_templates_tab[1] = adaptionset_segmentlist_node;
896 fragment_templates_tab[2] = fragment_template_node;
897 fragment_templates_tab[3] = period_segmenttemplate_node;
898 fragment_templates_tab[4] = period_segmentlist_node;
900 initialization_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "initialization");
901 if (initialization_val) {
902 rep->init_section = av_mallocz(sizeof(struct fragment));
903 if (!rep->init_section) {
904 xmlFree(initialization_val);
907 c->max_url_size = aligned(c->max_url_size + strlen(initialization_val));
908 rep->init_section->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, initialization_val);
909 xmlFree(initialization_val);
910 if (!rep->init_section->url)
912 rep->init_section->size = -1;
914 media_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "media");
916 c->max_url_size = aligned(c->max_url_size + strlen(media_val));
917 rep->url_template = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, media_val);
920 presentation_timeoffset_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "presentationTimeOffset");
921 if (presentation_timeoffset_val) {
922 rep->presentation_timeoffset = (int64_t) strtoll(presentation_timeoffset_val, NULL, 10);
923 av_log(s, AV_LOG_TRACE, "rep->presentation_timeoffset = [%"PRId64"]\n", rep->presentation_timeoffset);
924 xmlFree(presentation_timeoffset_val);
926 duration_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "duration");
928 rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
929 av_log(s, AV_LOG_TRACE, "rep->fragment_duration = [%"PRId64"]\n", rep->fragment_duration);
930 xmlFree(duration_val);
932 timescale_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "timescale");
934 rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
935 av_log(s, AV_LOG_TRACE, "rep->fragment_timescale = [%"PRId64"]\n", rep->fragment_timescale);
936 xmlFree(timescale_val);
938 startnumber_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "startNumber");
939 if (startnumber_val) {
940 rep->start_number = rep->first_seq_no = (int64_t) strtoll(startnumber_val, NULL, 10);
941 av_log(s, AV_LOG_TRACE, "rep->first_seq_no = [%"PRId64"]\n", rep->first_seq_no);
942 xmlFree(startnumber_val);
944 if (adaptionset_supplementalproperty_node) {
945 if (!av_strcasecmp(xmlGetProp(adaptionset_supplementalproperty_node,"schemeIdUri"), "http://dashif.org/guidelines/last-segment-number")) {
946 val = xmlGetProp(adaptionset_supplementalproperty_node,"value");
948 av_log(s, AV_LOG_ERROR, "Missing value attribute in adaptionset_supplementalproperty_node\n");
950 rep->last_seq_no =(int64_t) strtoll(val, NULL, 10) - 1;
956 fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
958 if (!fragment_timeline_node)
959 fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
960 if (!fragment_timeline_node)
961 fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
962 if (!fragment_timeline_node)
963 fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
964 if (fragment_timeline_node) {
965 fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
966 while (fragment_timeline_node) {
967 ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
970 fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
973 } else if (representation_baseurl_node && !representation_segmentlist_node) {
974 seg = av_mallocz(sizeof(struct fragment));
977 seg->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, NULL);
983 dynarray_add(&rep->fragments, &rep->n_fragments, seg);
984 } else if (representation_segmentlist_node) {
985 // TODO: https://www.brendanlong.com/the-structure-of-an-mpeg-dash-mpd.html
986 // http://www-itec.uni-klu.ac.at/dash/ddash/mpdGenerator.php?fragmentlength=15&type=full
987 xmlNodePtr fragmenturl_node = NULL;
988 segmentlists_tab[0] = representation_segmentlist_node;
989 segmentlists_tab[1] = adaptionset_segmentlist_node;
990 segmentlists_tab[2] = period_segmentlist_node;
992 duration_val = get_val_from_nodes_tab(segmentlists_tab, 3, "duration");
993 timescale_val = get_val_from_nodes_tab(segmentlists_tab, 3, "timescale");
994 startnumber_val = get_val_from_nodes_tab(segmentlists_tab, 3, "startNumber");
996 rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
997 av_log(s, AV_LOG_TRACE, "rep->fragment_duration = [%"PRId64"]\n", rep->fragment_duration);
998 xmlFree(duration_val);
1000 if (timescale_val) {
1001 rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
1002 av_log(s, AV_LOG_TRACE, "rep->fragment_timescale = [%"PRId64"]\n", rep->fragment_timescale);
1003 xmlFree(timescale_val);
1005 if (startnumber_val) {
1006 rep->start_number = rep->first_seq_no = (int64_t) strtoll(startnumber_val, NULL, 10);
1007 av_log(s, AV_LOG_TRACE, "rep->first_seq_no = [%"PRId64"]\n", rep->first_seq_no);
1008 xmlFree(startnumber_val);
1011 fragmenturl_node = xmlFirstElementChild(representation_segmentlist_node);
1012 while (fragmenturl_node) {
1013 ret = parse_manifest_segmenturlnode(s, rep, fragmenturl_node,
1019 fragmenturl_node = xmlNextElementSibling(fragmenturl_node);
1022 fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
1023 if (!fragment_timeline_node)
1024 fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
1025 if (fragment_timeline_node) {
1026 fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
1027 while (fragment_timeline_node) {
1028 ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
1031 fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
1035 av_log(s, AV_LOG_ERROR, "Unknown format of Representation node id[%s] \n", (const char *)rep_id_val);
1040 if (rep->fragment_duration > 0 && !rep->fragment_timescale)
1041 rep->fragment_timescale = 1;
1042 rep->bandwidth = rep_bandwidth_val ? atoi(rep_bandwidth_val) : 0;
1043 strncpy(rep->id, rep_id_val ? rep_id_val : "", sizeof(rep->id));
1044 rep->framerate = av_make_q(0, 0);
1045 if (type == AVMEDIA_TYPE_VIDEO && rep_framerate_val) {
1046 ret = av_parse_video_rate(&rep->framerate, rep_framerate_val);
1048 av_log(s, AV_LOG_VERBOSE, "Ignoring invalid frame rate '%s'\n", rep_framerate_val);
1052 case AVMEDIA_TYPE_VIDEO:
1053 dynarray_add(&c->videos, &c->n_videos, rep);
1055 case AVMEDIA_TYPE_AUDIO:
1056 dynarray_add(&c->audios, &c->n_audios, rep);
1058 case AVMEDIA_TYPE_SUBTITLE:
1059 dynarray_add(&c->subtitles, &c->n_subtitles, rep);
1062 av_log(s, AV_LOG_WARNING, "Unsupported the stream type %d\n", type);
1070 xmlFree(rep_id_val);
1071 if (rep_bandwidth_val)
1072 xmlFree(rep_bandwidth_val);
1073 if (rep_framerate_val)
1074 xmlFree(rep_framerate_val);
1078 ret = AVERROR(ENOMEM);
1080 free_representation(rep);
1084 static int parse_manifest_adaptationset_attr(AVFormatContext *s, xmlNodePtr adaptionset_node)
1086 DASHContext *c = s->priv_data;
1088 if (!adaptionset_node) {
1089 av_log(s, AV_LOG_WARNING, "Cannot get AdaptionSet\n");
1090 return AVERROR(EINVAL);
1092 c->adaptionset_lang = xmlGetProp(adaptionset_node, "lang");
1097 static int parse_manifest_adaptationset(AVFormatContext *s, const char *url,
1098 xmlNodePtr adaptionset_node,
1099 xmlNodePtr mpd_baseurl_node,
1100 xmlNodePtr period_baseurl_node,
1101 xmlNodePtr period_segmenttemplate_node,
1102 xmlNodePtr period_segmentlist_node)
1105 DASHContext *c = s->priv_data;
1106 xmlNodePtr fragment_template_node = NULL;
1107 xmlNodePtr content_component_node = NULL;
1108 xmlNodePtr adaptionset_baseurl_node = NULL;
1109 xmlNodePtr adaptionset_segmentlist_node = NULL;
1110 xmlNodePtr adaptionset_supplementalproperty_node = NULL;
1111 xmlNodePtr node = NULL;
1113 ret = parse_manifest_adaptationset_attr(s, adaptionset_node);
1117 node = xmlFirstElementChild(adaptionset_node);
1119 if (!av_strcasecmp(node->name, (const char *)"SegmentTemplate")) {
1120 fragment_template_node = node;
1121 } else if (!av_strcasecmp(node->name, (const char *)"ContentComponent")) {
1122 content_component_node = node;
1123 } else if (!av_strcasecmp(node->name, (const char *)"BaseURL")) {
1124 adaptionset_baseurl_node = node;
1125 } else if (!av_strcasecmp(node->name, (const char *)"SegmentList")) {
1126 adaptionset_segmentlist_node = node;
1127 } else if (!av_strcasecmp(node->name, (const char *)"SupplementalProperty")) {
1128 adaptionset_supplementalproperty_node = node;
1129 } else if (!av_strcasecmp(node->name, (const char *)"Representation")) {
1130 ret = parse_manifest_representation(s, url, node,
1133 period_baseurl_node,
1134 period_segmenttemplate_node,
1135 period_segmentlist_node,
1136 fragment_template_node,
1137 content_component_node,
1138 adaptionset_baseurl_node,
1139 adaptionset_segmentlist_node,
1140 adaptionset_supplementalproperty_node);
1144 node = xmlNextElementSibling(node);
1148 av_freep(&c->adaptionset_lang);
1152 static int parse_programinformation(AVFormatContext *s, xmlNodePtr node)
1154 xmlChar *val = NULL;
1156 node = xmlFirstElementChild(node);
1158 if (!av_strcasecmp(node->name, "Title")) {
1159 val = xmlNodeGetContent(node);
1161 av_dict_set(&s->metadata, "Title", val, 0);
1163 } else if (!av_strcasecmp(node->name, "Source")) {
1164 val = xmlNodeGetContent(node);
1166 av_dict_set(&s->metadata, "Source", val, 0);
1168 } else if (!av_strcasecmp(node->name, "Copyright")) {
1169 val = xmlNodeGetContent(node);
1171 av_dict_set(&s->metadata, "Copyright", val, 0);
1174 node = xmlNextElementSibling(node);
1181 static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
1183 DASHContext *c = s->priv_data;
1186 uint8_t *new_url = NULL;
1187 int64_t filesize = 0;
1189 AVDictionary *opts = NULL;
1191 xmlNodePtr root_element = NULL;
1192 xmlNodePtr node = NULL;
1193 xmlNodePtr period_node = NULL;
1194 xmlNodePtr tmp_node = NULL;
1195 xmlNodePtr mpd_baseurl_node = NULL;
1196 xmlNodePtr period_baseurl_node = NULL;
1197 xmlNodePtr period_segmenttemplate_node = NULL;
1198 xmlNodePtr period_segmentlist_node = NULL;
1199 xmlNodePtr adaptionset_node = NULL;
1200 xmlAttrPtr attr = NULL;
1202 uint32_t period_duration_sec = 0;
1203 uint32_t period_start_sec = 0;
1208 av_dict_copy(&opts, c->avio_opts, 0);
1209 ret = avio_open2(&in, url, AVIO_FLAG_READ, c->interrupt_callback, &opts);
1210 av_dict_free(&opts);
1215 if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, &new_url) >= 0) {
1216 c->base_url = av_strdup(new_url);
1218 c->base_url = av_strdup(url);
1221 filesize = avio_size(in);
1222 filesize = filesize > 0 ? filesize : DEFAULT_MANIFEST_SIZE;
1224 if (filesize > MAX_BPRINT_READ_SIZE) {
1225 av_log(s, AV_LOG_ERROR, "Manifest too large: %"PRId64"\n", filesize);
1226 return AVERROR_INVALIDDATA;
1229 av_bprint_init(&buf, filesize + 1, AV_BPRINT_SIZE_UNLIMITED);
1231 if ((ret = avio_read_to_bprint(in, &buf, MAX_BPRINT_READ_SIZE)) < 0 ||
1233 (filesize = buf.len) == 0) {
1234 av_log(s, AV_LOG_ERROR, "Unable to read to manifest '%s'\n", url);
1236 ret = AVERROR_INVALIDDATA;
1240 doc = xmlReadMemory(buf.str, filesize, c->base_url, NULL, 0);
1241 root_element = xmlDocGetRootElement(doc);
1242 node = root_element;
1245 ret = AVERROR_INVALIDDATA;
1246 av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing root node\n", url);
1250 if (node->type != XML_ELEMENT_NODE ||
1251 av_strcasecmp(node->name, (const char *)"MPD")) {
1252 ret = AVERROR_INVALIDDATA;
1253 av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - wrong root node name[%s] type[%d]\n", url, node->name, (int)node->type);
1257 val = xmlGetProp(node, "type");
1259 av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing type attrib\n", url);
1260 ret = AVERROR_INVALIDDATA;
1263 if (!av_strcasecmp(val, (const char *)"dynamic"))
1267 attr = node->properties;
1269 val = xmlGetProp(node, attr->name);
1271 if (!av_strcasecmp(attr->name, (const char *)"availabilityStartTime")) {
1272 c->availability_start_time = get_utc_date_time_insec(s, (const char *)val);
1273 av_log(s, AV_LOG_TRACE, "c->availability_start_time = [%"PRId64"]\n", c->availability_start_time);
1274 } else if (!av_strcasecmp(attr->name, (const char *)"availabilityEndTime")) {
1275 c->availability_end_time = get_utc_date_time_insec(s, (const char *)val);
1276 av_log(s, AV_LOG_TRACE, "c->availability_end_time = [%"PRId64"]\n", c->availability_end_time);
1277 } else if (!av_strcasecmp(attr->name, (const char *)"publishTime")) {
1278 c->publish_time = get_utc_date_time_insec(s, (const char *)val);
1279 av_log(s, AV_LOG_TRACE, "c->publish_time = [%"PRId64"]\n", c->publish_time);
1280 } else if (!av_strcasecmp(attr->name, (const char *)"minimumUpdatePeriod")) {
1281 c->minimum_update_period = get_duration_insec(s, (const char *)val);
1282 av_log(s, AV_LOG_TRACE, "c->minimum_update_period = [%"PRId64"]\n", c->minimum_update_period);
1283 } else if (!av_strcasecmp(attr->name, (const char *)"timeShiftBufferDepth")) {
1284 c->time_shift_buffer_depth = get_duration_insec(s, (const char *)val);
1285 av_log(s, AV_LOG_TRACE, "c->time_shift_buffer_depth = [%"PRId64"]\n", c->time_shift_buffer_depth);
1286 } else if (!av_strcasecmp(attr->name, (const char *)"minBufferTime")) {
1287 c->min_buffer_time = get_duration_insec(s, (const char *)val);
1288 av_log(s, AV_LOG_TRACE, "c->min_buffer_time = [%"PRId64"]\n", c->min_buffer_time);
1289 } else if (!av_strcasecmp(attr->name, (const char *)"suggestedPresentationDelay")) {
1290 c->suggested_presentation_delay = get_duration_insec(s, (const char *)val);
1291 av_log(s, AV_LOG_TRACE, "c->suggested_presentation_delay = [%"PRId64"]\n", c->suggested_presentation_delay);
1292 } else if (!av_strcasecmp(attr->name, (const char *)"mediaPresentationDuration")) {
1293 c->media_presentation_duration = get_duration_insec(s, (const char *)val);
1294 av_log(s, AV_LOG_TRACE, "c->media_presentation_duration = [%"PRId64"]\n", c->media_presentation_duration);
1300 tmp_node = find_child_node_by_name(node, "BaseURL");
1302 mpd_baseurl_node = xmlCopyNode(tmp_node,1);
1304 mpd_baseurl_node = xmlNewNode(NULL, "BaseURL");
1307 // at now we can handle only one period, with the longest duration
1308 node = xmlFirstElementChild(node);
1310 if (!av_strcasecmp(node->name, (const char *)"Period")) {
1311 period_duration_sec = 0;
1312 period_start_sec = 0;
1313 attr = node->properties;
1315 val = xmlGetProp(node, attr->name);
1316 if (!av_strcasecmp(attr->name, (const char *)"duration")) {
1317 period_duration_sec = get_duration_insec(s, (const char *)val);
1318 } else if (!av_strcasecmp(attr->name, (const char *)"start")) {
1319 period_start_sec = get_duration_insec(s, (const char *)val);
1324 if ((period_duration_sec) >= (c->period_duration)) {
1326 c->period_duration = period_duration_sec;
1327 c->period_start = period_start_sec;
1328 if (c->period_start > 0)
1329 c->media_presentation_duration = c->period_duration;
1331 } else if (!av_strcasecmp(node->name, "ProgramInformation")) {
1332 parse_programinformation(s, node);
1334 node = xmlNextElementSibling(node);
1337 av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing Period node\n", url);
1338 ret = AVERROR_INVALIDDATA;
1342 adaptionset_node = xmlFirstElementChild(period_node);
1343 while (adaptionset_node) {
1344 if (!av_strcasecmp(adaptionset_node->name, (const char *)"BaseURL")) {
1345 period_baseurl_node = adaptionset_node;
1346 } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"SegmentTemplate")) {
1347 period_segmenttemplate_node = adaptionset_node;
1348 } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"SegmentList")) {
1349 period_segmentlist_node = adaptionset_node;
1350 } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"AdaptationSet")) {
1351 parse_manifest_adaptationset(s, url, adaptionset_node, mpd_baseurl_node, period_baseurl_node, period_segmenttemplate_node, period_segmentlist_node);
1353 adaptionset_node = xmlNextElementSibling(adaptionset_node);
1356 /*free the document */
1359 xmlFreeNode(mpd_baseurl_node);
1363 av_bprint_finalize(&buf, NULL);
1370 static int64_t calc_cur_seg_no(AVFormatContext *s, struct representation *pls)
1372 DASHContext *c = s->priv_data;
1374 int64_t start_time_offset = 0;
1377 if (pls->n_fragments) {
1378 av_log(s, AV_LOG_TRACE, "in n_fragments mode\n");
1379 num = pls->first_seq_no;
1380 } else if (pls->n_timelines) {
1381 av_log(s, AV_LOG_TRACE, "in n_timelines mode\n");
1382 start_time_offset = get_segment_start_time_based_on_timeline(pls, 0xFFFFFFFF) - 60 * pls->fragment_timescale; // 60 seconds before end
1383 num = calc_next_seg_no_from_timelines(pls, start_time_offset);
1385 num = pls->first_seq_no;
1387 num += pls->first_seq_no;
1388 } else if (pls->fragment_duration){
1389 av_log(s, AV_LOG_TRACE, "in fragment_duration mode fragment_timescale = %"PRId64", presentation_timeoffset = %"PRId64"\n", pls->fragment_timescale, pls->presentation_timeoffset);
1390 if (pls->presentation_timeoffset) {
1391 num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time) * pls->fragment_timescale)-pls->presentation_timeoffset) / pls->fragment_duration - c->min_buffer_time;
1392 } else if (c->publish_time > 0 && !c->availability_start_time) {
1393 if (c->min_buffer_time) {
1394 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;
1396 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;
1399 num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time) - c->suggested_presentation_delay) * pls->fragment_timescale) / pls->fragment_duration;
1403 num = pls->first_seq_no;
1408 static int64_t calc_min_seg_no(AVFormatContext *s, struct representation *pls)
1410 DASHContext *c = s->priv_data;
1413 if (c->is_live && pls->fragment_duration) {
1414 av_log(s, AV_LOG_TRACE, "in live mode\n");
1415 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;
1417 num = pls->first_seq_no;
1422 static int64_t calc_max_seg_no(struct representation *pls, DASHContext *c)
1426 if (pls->n_fragments) {
1427 num = pls->first_seq_no + pls->n_fragments - 1;
1428 } else if (pls->n_timelines) {
1430 num = pls->first_seq_no + pls->n_timelines - 1;
1431 for (i = 0; i < pls->n_timelines; i++) {
1432 if (pls->timelines[i]->repeat == -1) {
1433 int length_of_each_segment = pls->timelines[i]->duration / pls->fragment_timescale;
1434 num = c->period_duration / length_of_each_segment;
1436 num += pls->timelines[i]->repeat;
1439 } else if (c->is_live && pls->fragment_duration) {
1440 num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time)) * pls->fragment_timescale) / pls->fragment_duration;
1441 } else if (pls->fragment_duration) {
1442 num = pls->first_seq_no + (c->media_presentation_duration * pls->fragment_timescale) / pls->fragment_duration;
1448 static void move_timelines(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
1450 if (rep_dest && rep_src ) {
1451 free_timelines_list(rep_dest);
1452 rep_dest->timelines = rep_src->timelines;
1453 rep_dest->n_timelines = rep_src->n_timelines;
1454 rep_dest->first_seq_no = rep_src->first_seq_no;
1455 rep_dest->last_seq_no = calc_max_seg_no(rep_dest, c);
1456 rep_src->timelines = NULL;
1457 rep_src->n_timelines = 0;
1458 rep_dest->cur_seq_no = rep_src->cur_seq_no;
1462 static void move_segments(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
1464 if (rep_dest && rep_src ) {
1465 free_fragment_list(rep_dest);
1466 if (rep_src->start_number > (rep_dest->start_number + rep_dest->n_fragments))
1467 rep_dest->cur_seq_no = 0;
1469 rep_dest->cur_seq_no += rep_src->start_number - rep_dest->start_number;
1470 rep_dest->fragments = rep_src->fragments;
1471 rep_dest->n_fragments = rep_src->n_fragments;
1472 rep_dest->parent = rep_src->parent;
1473 rep_dest->last_seq_no = calc_max_seg_no(rep_dest, c);
1474 rep_src->fragments = NULL;
1475 rep_src->n_fragments = 0;
1480 static int refresh_manifest(AVFormatContext *s)
1483 DASHContext *c = s->priv_data;
1484 // save current context
1485 int n_videos = c->n_videos;
1486 struct representation **videos = c->videos;
1487 int n_audios = c->n_audios;
1488 struct representation **audios = c->audios;
1489 int n_subtitles = c->n_subtitles;
1490 struct representation **subtitles = c->subtitles;
1491 char *base_url = c->base_url;
1499 c->subtitles = NULL;
1500 ret = parse_manifest(s, s->url, NULL);
1504 if (c->n_videos != n_videos) {
1505 av_log(c, AV_LOG_ERROR,
1506 "new manifest has mismatched no. of video representations, %d -> %d\n",
1507 n_videos, c->n_videos);
1508 return AVERROR_INVALIDDATA;
1510 if (c->n_audios != n_audios) {
1511 av_log(c, AV_LOG_ERROR,
1512 "new manifest has mismatched no. of audio representations, %d -> %d\n",
1513 n_audios, c->n_audios);
1514 return AVERROR_INVALIDDATA;
1516 if (c->n_subtitles != n_subtitles) {
1517 av_log(c, AV_LOG_ERROR,
1518 "new manifest has mismatched no. of subtitles representations, %d -> %d\n",
1519 n_subtitles, c->n_subtitles);
1520 return AVERROR_INVALIDDATA;
1523 for (i = 0; i < n_videos; i++) {
1524 struct representation *cur_video = videos[i];
1525 struct representation *ccur_video = c->videos[i];
1526 if (cur_video->timelines) {
1527 // calc current time
1528 int64_t currentTime = get_segment_start_time_based_on_timeline(cur_video, cur_video->cur_seq_no) / cur_video->fragment_timescale;
1530 ccur_video->cur_seq_no = calc_next_seg_no_from_timelines(ccur_video, currentTime * cur_video->fragment_timescale - 1);
1531 if (ccur_video->cur_seq_no >= 0) {
1532 move_timelines(ccur_video, cur_video, c);
1535 if (cur_video->fragments) {
1536 move_segments(ccur_video, cur_video, c);
1539 for (i = 0; i < n_audios; i++) {
1540 struct representation *cur_audio = audios[i];
1541 struct representation *ccur_audio = c->audios[i];
1542 if (cur_audio->timelines) {
1543 // calc current time
1544 int64_t currentTime = get_segment_start_time_based_on_timeline(cur_audio, cur_audio->cur_seq_no) / cur_audio->fragment_timescale;
1546 ccur_audio->cur_seq_no = calc_next_seg_no_from_timelines(ccur_audio, currentTime * cur_audio->fragment_timescale - 1);
1547 if (ccur_audio->cur_seq_no >= 0) {
1548 move_timelines(ccur_audio, cur_audio, c);
1551 if (cur_audio->fragments) {
1552 move_segments(ccur_audio, cur_audio, c);
1561 c->base_url = base_url;
1564 free_subtitle_list(c);
1570 c->n_subtitles = n_subtitles;
1571 c->subtitles = subtitles;
1572 c->n_audios = n_audios;
1574 c->n_videos = n_videos;
1579 static struct fragment *get_current_fragment(struct representation *pls)
1581 int64_t min_seq_no = 0;
1582 int64_t max_seq_no = 0;
1583 struct fragment *seg = NULL;
1584 struct fragment *seg_ptr = NULL;
1585 DASHContext *c = pls->parent->priv_data;
1587 while (( !ff_check_interrupt(c->interrupt_callback)&& pls->n_fragments > 0)) {
1588 if (pls->cur_seq_no < pls->n_fragments) {
1589 seg_ptr = pls->fragments[pls->cur_seq_no];
1590 seg = av_mallocz(sizeof(struct fragment));
1594 seg->url = av_strdup(seg_ptr->url);
1599 seg->size = seg_ptr->size;
1600 seg->url_offset = seg_ptr->url_offset;
1602 } else if (c->is_live) {
1603 refresh_manifest(pls->parent);
1609 min_seq_no = calc_min_seg_no(pls->parent, pls);
1610 max_seq_no = calc_max_seg_no(pls, c);
1612 if (pls->timelines || pls->fragments) {
1613 refresh_manifest(pls->parent);
1615 if (pls->cur_seq_no <= min_seq_no) {
1616 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);
1617 pls->cur_seq_no = calc_cur_seg_no(pls->parent, pls);
1618 } else if (pls->cur_seq_no > max_seq_no) {
1619 av_log(pls->parent, AV_LOG_VERBOSE, "new fragment: min[%"PRId64"] max[%"PRId64"]\n", min_seq_no, max_seq_no);
1621 seg = av_mallocz(sizeof(struct fragment));
1625 } else if (pls->cur_seq_no <= pls->last_seq_no) {
1626 seg = av_mallocz(sizeof(struct fragment));
1632 char *tmpfilename= av_mallocz(c->max_url_size);
1636 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));
1637 seg->url = av_strireplace(pls->url_template, pls->url_template, tmpfilename);
1639 av_log(pls->parent, AV_LOG_WARNING, "Unable to resolve template url '%s', try to use origin template\n", pls->url_template);
1640 seg->url = av_strdup(pls->url_template);
1642 av_log(pls->parent, AV_LOG_ERROR, "Cannot resolve template url '%s'\n", pls->url_template);
1643 av_free(tmpfilename);
1647 av_free(tmpfilename);
1654 static int read_from_url(struct representation *pls, struct fragment *seg,
1655 uint8_t *buf, int buf_size)
1659 /* limit read if the fragment was only a part of a file */
1661 buf_size = FFMIN(buf_size, pls->cur_seg_size - pls->cur_seg_offset);
1663 ret = avio_read(pls->input, buf, buf_size);
1665 pls->cur_seg_offset += ret;
1670 static int open_input(DASHContext *c, struct representation *pls, struct fragment *seg)
1672 AVDictionary *opts = NULL;
1676 url = av_mallocz(c->max_url_size);
1678 ret = AVERROR(ENOMEM);
1682 if (seg->size >= 0) {
1683 /* try to restrict the HTTP request to the part we want
1684 * (if this is in fact a HTTP request) */
1685 av_dict_set_int(&opts, "offset", seg->url_offset, 0);
1686 av_dict_set_int(&opts, "end_offset", seg->url_offset + seg->size, 0);
1689 ff_make_absolute_url(url, c->max_url_size, c->base_url, seg->url);
1690 av_log(pls->parent, AV_LOG_VERBOSE, "DASH request for url '%s', offset %"PRId64"\n",
1691 url, seg->url_offset);
1692 ret = open_url(pls->parent, &pls->input, url, &c->avio_opts, opts, NULL);
1696 av_dict_free(&opts);
1697 pls->cur_seg_offset = 0;
1698 pls->cur_seg_size = seg->size;
1702 static int update_init_section(struct representation *pls)
1704 static const int max_init_section_size = 1024 * 1024;
1705 DASHContext *c = pls->parent->priv_data;
1710 if (!pls->init_section || pls->init_sec_buf)
1713 ret = open_input(c, pls, pls->init_section);
1715 av_log(pls->parent, AV_LOG_WARNING,
1716 "Failed to open an initialization section\n");
1720 if (pls->init_section->size >= 0)
1721 sec_size = pls->init_section->size;
1722 else if ((urlsize = avio_size(pls->input)) >= 0)
1725 sec_size = max_init_section_size;
1727 av_log(pls->parent, AV_LOG_DEBUG,
1728 "Downloading an initialization section of size %"PRId64"\n",
1731 sec_size = FFMIN(sec_size, max_init_section_size);
1733 av_fast_malloc(&pls->init_sec_buf, &pls->init_sec_buf_size, sec_size);
1735 ret = read_from_url(pls, pls->init_section, pls->init_sec_buf,
1736 pls->init_sec_buf_size);
1737 ff_format_io_close(pls->parent, &pls->input);
1742 pls->init_sec_data_len = ret;
1743 pls->init_sec_buf_read_offset = 0;
1748 static int64_t seek_data(void *opaque, int64_t offset, int whence)
1750 struct representation *v = opaque;
1751 if (v->n_fragments && !v->init_sec_data_len) {
1752 return avio_seek(v->input, offset, whence);
1755 return AVERROR(ENOSYS);
1758 static int read_data(void *opaque, uint8_t *buf, int buf_size)
1761 struct representation *v = opaque;
1762 DASHContext *c = v->parent->priv_data;
1766 free_fragment(&v->cur_seg);
1767 v->cur_seg = get_current_fragment(v);
1773 /* load/update Media Initialization Section, if any */
1774 ret = update_init_section(v);
1778 ret = open_input(c, v, v->cur_seg);
1780 if (ff_check_interrupt(c->interrupt_callback)) {
1784 av_log(v->parent, AV_LOG_WARNING, "Failed to open fragment of playlist\n");
1790 if (v->init_sec_buf_read_offset < v->init_sec_data_len) {
1791 /* Push init section out first before first actual fragment */
1792 int copy_size = FFMIN(v->init_sec_data_len - v->init_sec_buf_read_offset, buf_size);
1793 memcpy(buf, v->init_sec_buf, copy_size);
1794 v->init_sec_buf_read_offset += copy_size;
1799 /* check the v->cur_seg, if it is null, get current and double check if the new v->cur_seg*/
1801 v->cur_seg = get_current_fragment(v);
1807 ret = read_from_url(v, v->cur_seg, buf, buf_size);
1811 if (c->is_live || v->cur_seq_no < v->last_seq_no) {
1812 if (!v->is_restart_needed)
1814 v->is_restart_needed = 1;
1821 static int save_avio_options(AVFormatContext *s)
1823 DASHContext *c = s->priv_data;
1824 const char *opts[] = {
1825 "headers", "user_agent", "cookies", "http_proxy", "referer", "rw_timeout", "icy", NULL };
1826 const char **opt = opts;
1827 uint8_t *buf = NULL;
1831 if (av_opt_get(s->pb, *opt, AV_OPT_SEARCH_CHILDREN, &buf) >= 0) {
1832 if (buf[0] != '\0') {
1833 ret = av_dict_set(&c->avio_opts, *opt, buf, AV_DICT_DONT_STRDUP_VAL);
1846 static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
1847 int flags, AVDictionary **opts)
1849 av_log(s, AV_LOG_ERROR,
1850 "A DASH playlist item '%s' referred to an external file '%s'. "
1851 "Opening this file was forbidden for security reasons\n",
1853 return AVERROR(EPERM);
1856 static void close_demux_for_component(struct representation *pls)
1858 /* note: the internal buffer could have changed */
1859 av_freep(&pls->pb.buffer);
1860 memset(&pls->pb, 0x00, sizeof(AVIOContext));
1861 pls->ctx->pb = NULL;
1862 avformat_close_input(&pls->ctx);
1865 static int reopen_demux_for_component(AVFormatContext *s, struct representation *pls)
1867 DASHContext *c = s->priv_data;
1868 ff_const59 AVInputFormat *in_fmt = NULL;
1869 AVDictionary *in_fmt_opts = NULL;
1870 uint8_t *avio_ctx_buffer = NULL;
1874 close_demux_for_component(pls);
1877 if (ff_check_interrupt(&s->interrupt_callback)) {
1882 if (!(pls->ctx = avformat_alloc_context())) {
1883 ret = AVERROR(ENOMEM);
1887 avio_ctx_buffer = av_malloc(INITIAL_BUFFER_SIZE);
1888 if (!avio_ctx_buffer ) {
1889 ret = AVERROR(ENOMEM);
1890 avformat_free_context(pls->ctx);
1895 ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, NULL);
1897 ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, seek_data);
1899 pls->pb.seekable = 0;
1901 if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0)
1904 pls->ctx->flags = AVFMT_FLAG_CUSTOM_IO;
1905 pls->ctx->probesize = s->probesize > 0 ? s->probesize : 1024 * 4;
1906 pls->ctx->max_analyze_duration = s->max_analyze_duration > 0 ? s->max_analyze_duration : 4 * AV_TIME_BASE;
1907 pls->ctx->interrupt_callback = s->interrupt_callback;
1908 ret = av_probe_input_buffer(&pls->pb, &in_fmt, "", NULL, 0, 0);
1910 av_log(s, AV_LOG_ERROR, "Error when loading first fragment of playlist\n");
1911 avformat_free_context(pls->ctx);
1916 pls->ctx->pb = &pls->pb;
1917 pls->ctx->io_open = nested_io_open;
1919 // provide additional information from mpd if available
1920 ret = avformat_open_input(&pls->ctx, "", in_fmt, &in_fmt_opts); //pls->init_section->url
1921 av_dict_free(&in_fmt_opts);
1924 if (pls->n_fragments) {
1925 #if FF_API_R_FRAME_RATE
1926 if (pls->framerate.den) {
1927 for (i = 0; i < pls->ctx->nb_streams; i++)
1928 pls->ctx->streams[i]->r_frame_rate = pls->framerate;
1931 ret = avformat_find_stream_info(pls->ctx, NULL);
1940 static int open_demux_for_component(AVFormatContext *s, struct representation *pls)
1946 pls->cur_seq_no = calc_cur_seg_no(s, pls);
1948 if (!pls->last_seq_no) {
1949 pls->last_seq_no = calc_max_seg_no(pls, s->priv_data);
1952 ret = reopen_demux_for_component(s, pls);
1956 for (i = 0; i < pls->ctx->nb_streams; i++) {
1957 AVStream *st = avformat_new_stream(s, NULL);
1958 AVStream *ist = pls->ctx->streams[i];
1960 ret = AVERROR(ENOMEM);
1964 avcodec_parameters_copy(st->codecpar, ist->codecpar);
1965 avpriv_set_pts_info(st, ist->pts_wrap_bits, ist->time_base.num, ist->time_base.den);
1968 st->disposition = ist->disposition;
1971 for (int i = 0; i < ist->nb_side_data; i++) {
1972 const AVPacketSideData *sd_src = &ist->side_data[i];
1975 dst_data = av_stream_new_side_data(st, sd_src->type, sd_src->size);
1977 return AVERROR(ENOMEM);
1978 memcpy(dst_data, sd_src->data, sd_src->size);
1987 static int is_common_init_section_exist(struct representation **pls, int n_pls)
1989 struct fragment *first_init_section = pls[0]->init_section;
1991 int64_t url_offset = -1;
1995 if (first_init_section == NULL || n_pls == 0)
1998 url = first_init_section->url;
1999 url_offset = first_init_section->url_offset;
2000 size = pls[0]->init_section->size;
2001 for (i=0;i<n_pls;i++) {
2002 if (av_strcasecmp(pls[i]->init_section->url,url) || pls[i]->init_section->url_offset != url_offset || pls[i]->init_section->size != size) {
2009 static int copy_init_section(struct representation *rep_dest, struct representation *rep_src)
2011 rep_dest->init_sec_buf = av_mallocz(rep_src->init_sec_buf_size);
2012 if (!rep_dest->init_sec_buf) {
2013 av_log(rep_dest->ctx, AV_LOG_WARNING, "Cannot alloc memory for init_sec_buf\n");
2014 return AVERROR(ENOMEM);
2016 memcpy(rep_dest->init_sec_buf, rep_src->init_sec_buf, rep_src->init_sec_data_len);
2017 rep_dest->init_sec_buf_size = rep_src->init_sec_buf_size;
2018 rep_dest->init_sec_data_len = rep_src->init_sec_data_len;
2019 rep_dest->cur_timestamp = rep_src->cur_timestamp;
2024 static int dash_close(AVFormatContext *s);
2026 static int dash_read_header(AVFormatContext *s)
2028 DASHContext *c = s->priv_data;
2029 struct representation *rep;
2031 int stream_index = 0;
2034 c->interrupt_callback = &s->interrupt_callback;
2036 if ((ret = save_avio_options(s)) < 0)
2039 if ((ret = parse_manifest(s, s->url, s->pb)) < 0)
2042 /* If this isn't a live stream, fill the total duration of the
2045 s->duration = (int64_t) c->media_presentation_duration * AV_TIME_BASE;
2047 av_dict_set(&c->avio_opts, "seekable", "0", 0);
2051 c->is_init_section_common_video = is_common_init_section_exist(c->videos, c->n_videos);
2053 /* Open the demuxer for video and audio components if available */
2054 for (i = 0; i < c->n_videos; i++) {
2056 if (i > 0 && c->is_init_section_common_video) {
2057 ret = copy_init_section(rep, c->videos[0]);
2061 ret = open_demux_for_component(s, rep);
2065 rep->stream_index = stream_index;
2070 c->is_init_section_common_audio = is_common_init_section_exist(c->audios, c->n_audios);
2072 for (i = 0; i < c->n_audios; i++) {
2074 if (i > 0 && c->is_init_section_common_audio) {
2075 ret = copy_init_section(rep, c->audios[0]);
2079 ret = open_demux_for_component(s, rep);
2083 rep->stream_index = stream_index;
2088 c->is_init_section_common_audio = is_common_init_section_exist(c->subtitles, c->n_subtitles);
2090 for (i = 0; i < c->n_subtitles; i++) {
2091 rep = c->subtitles[i];
2092 if (i > 0 && c->is_init_section_common_audio) {
2093 ret = copy_init_section(rep, c->subtitles[0]);
2097 ret = open_demux_for_component(s, rep);
2101 rep->stream_index = stream_index;
2105 if (!stream_index) {
2106 ret = AVERROR_INVALIDDATA;
2110 /* Create a program */
2113 program = av_new_program(s, 0);
2115 ret = AVERROR(ENOMEM);
2119 for (i = 0; i < c->n_videos; i++) {
2121 av_program_add_stream_index(s, 0, rep->stream_index);
2122 rep->assoc_stream = s->streams[rep->stream_index];
2123 if (rep->bandwidth > 0)
2124 av_dict_set_int(&rep->assoc_stream->metadata, "variant_bitrate", rep->bandwidth, 0);
2126 av_dict_set(&rep->assoc_stream->metadata, "id", rep->id, 0);
2128 for (i = 0; i < c->n_audios; i++) {
2130 av_program_add_stream_index(s, 0, rep->stream_index);
2131 rep->assoc_stream = s->streams[rep->stream_index];
2132 if (rep->bandwidth > 0)
2133 av_dict_set_int(&rep->assoc_stream->metadata, "variant_bitrate", rep->bandwidth, 0);
2135 av_dict_set(&rep->assoc_stream->metadata, "id", rep->id, 0);
2137 av_dict_set(&rep->assoc_stream->metadata, "language", rep->lang, 0);
2138 av_freep(&rep->lang);
2141 for (i = 0; i < c->n_subtitles; i++) {
2142 rep = c->subtitles[i];
2143 av_program_add_stream_index(s, 0, rep->stream_index);
2144 rep->assoc_stream = s->streams[rep->stream_index];
2146 av_dict_set(&rep->assoc_stream->metadata, "id", rep->id, 0);
2148 av_dict_set(&rep->assoc_stream->metadata, "language", rep->lang, 0);
2149 av_freep(&rep->lang);
2160 static void recheck_discard_flags(AVFormatContext *s, struct representation **p, int n)
2164 for (i = 0; i < n; i++) {
2165 struct representation *pls = p[i];
2166 int needed = !pls->assoc_stream || pls->assoc_stream->discard < AVDISCARD_ALL;
2168 if (needed && !pls->ctx) {
2169 pls->cur_seg_offset = 0;
2170 pls->init_sec_buf_read_offset = 0;
2172 for (j = 0; j < n; j++) {
2173 pls->cur_seq_no = FFMAX(pls->cur_seq_no, p[j]->cur_seq_no);
2175 reopen_demux_for_component(s, pls);
2176 av_log(s, AV_LOG_INFO, "Now receiving stream_index %d\n", pls->stream_index);
2177 } else if (!needed && pls->ctx) {
2178 close_demux_for_component(pls);
2179 ff_format_io_close(pls->parent, &pls->input);
2180 av_log(s, AV_LOG_INFO, "No longer receiving stream_index %d\n", pls->stream_index);
2185 static int dash_read_packet(AVFormatContext *s, AVPacket *pkt)
2187 DASHContext *c = s->priv_data;
2190 struct representation *cur = NULL;
2191 struct representation *rep = NULL;
2193 recheck_discard_flags(s, c->videos, c->n_videos);
2194 recheck_discard_flags(s, c->audios, c->n_audios);
2195 recheck_discard_flags(s, c->subtitles, c->n_subtitles);
2197 for (i = 0; i < c->n_videos; i++) {
2201 if (!cur || rep->cur_timestamp < mints) {
2203 mints = rep->cur_timestamp;
2206 for (i = 0; i < c->n_audios; i++) {
2210 if (!cur || rep->cur_timestamp < mints) {
2212 mints = rep->cur_timestamp;
2216 for (i = 0; i < c->n_subtitles; i++) {
2217 rep = c->subtitles[i];
2220 if (!cur || rep->cur_timestamp < mints) {
2222 mints = rep->cur_timestamp;
2227 return AVERROR_INVALIDDATA;
2229 while (!ff_check_interrupt(c->interrupt_callback) && !ret) {
2230 ret = av_read_frame(cur->ctx, pkt);
2232 /* If we got a packet, return it */
2233 cur->cur_timestamp = av_rescale(pkt->pts, (int64_t)cur->ctx->streams[0]->time_base.num * 90000, cur->ctx->streams[0]->time_base.den);
2234 pkt->stream_index = cur->stream_index;
2237 if (cur->is_restart_needed) {
2238 cur->cur_seg_offset = 0;
2239 cur->init_sec_buf_read_offset = 0;
2240 ff_format_io_close(cur->parent, &cur->input);
2241 ret = reopen_demux_for_component(s, cur);
2242 cur->is_restart_needed = 0;
2248 static int dash_close(AVFormatContext *s)
2250 DASHContext *c = s->priv_data;
2253 free_subtitle_list(c);
2254 av_dict_free(&c->avio_opts);
2255 av_freep(&c->base_url);
2259 static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos_msec, int flags, int dry_run)
2264 int64_t duration = 0;
2266 av_log(pls->parent, AV_LOG_VERBOSE, "DASH seek pos[%"PRId64"ms] %s\n",
2267 seek_pos_msec, dry_run ? " (dry)" : "");
2269 // single fragment mode
2270 if (pls->n_fragments == 1) {
2271 pls->cur_timestamp = 0;
2272 pls->cur_seg_offset = 0;
2275 ff_read_frame_flush(pls->ctx);
2276 return av_seek_frame(pls->ctx, -1, seek_pos_msec * 1000, flags);
2279 ff_format_io_close(pls->parent, &pls->input);
2281 // find the nearest fragment
2282 if (pls->n_timelines > 0 && pls->fragment_timescale > 0) {
2283 int64_t num = pls->first_seq_no;
2284 av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline start n_timelines[%d] "
2285 "last_seq_no[%"PRId64"].\n",
2286 (int)pls->n_timelines, (int64_t)pls->last_seq_no);
2287 for (i = 0; i < pls->n_timelines; i++) {
2288 if (pls->timelines[i]->starttime > 0) {
2289 duration = pls->timelines[i]->starttime;
2291 duration += pls->timelines[i]->duration;
2292 if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
2295 for (j = 0; j < pls->timelines[i]->repeat; j++) {
2296 duration += pls->timelines[i]->duration;
2298 if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
2306 pls->cur_seq_no = num > pls->last_seq_no ? pls->last_seq_no : num;
2307 av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline end cur_seq_no[%"PRId64"].\n",
2308 (int64_t)pls->cur_seq_no);
2309 } else if (pls->fragment_duration > 0) {
2310 pls->cur_seq_no = pls->first_seq_no + ((seek_pos_msec * pls->fragment_timescale) / pls->fragment_duration) / 1000;
2312 av_log(pls->parent, AV_LOG_ERROR, "dash_seek missing timeline or fragment_duration\n");
2313 pls->cur_seq_no = pls->first_seq_no;
2315 pls->cur_timestamp = 0;
2316 pls->cur_seg_offset = 0;
2317 pls->init_sec_buf_read_offset = 0;
2318 ret = dry_run ? 0 : reopen_demux_for_component(s, pls);
2323 static int dash_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
2326 DASHContext *c = s->priv_data;
2327 int64_t seek_pos_msec = av_rescale_rnd(timestamp, 1000,
2328 s->streams[stream_index]->time_base.den,
2329 flags & AVSEEK_FLAG_BACKWARD ?
2330 AV_ROUND_DOWN : AV_ROUND_UP);
2331 if ((flags & AVSEEK_FLAG_BYTE) || c->is_live)
2332 return AVERROR(ENOSYS);
2334 /* Seek in discarded streams with dry_run=1 to avoid reopening them */
2335 for (i = 0; i < c->n_videos; i++) {
2337 ret = dash_seek(s, c->videos[i], seek_pos_msec, flags, !c->videos[i]->ctx);
2339 for (i = 0; i < c->n_audios; i++) {
2341 ret = dash_seek(s, c->audios[i], seek_pos_msec, flags, !c->audios[i]->ctx);
2343 for (i = 0; i < c->n_subtitles; i++) {
2345 ret = dash_seek(s, c->subtitles[i], seek_pos_msec, flags, !c->subtitles[i]->ctx);
2351 static int dash_probe(const AVProbeData *p)
2353 if (!av_stristr(p->buf, "<MPD"))
2356 if (av_stristr(p->buf, "dash:profile:isoff-on-demand:2011") ||
2357 av_stristr(p->buf, "dash:profile:isoff-live:2011") ||
2358 av_stristr(p->buf, "dash:profile:isoff-live:2012") ||
2359 av_stristr(p->buf, "dash:profile:isoff-main:2011") ||
2360 av_stristr(p->buf, "3GPP:PSS:profile:DASH1")) {
2361 return AVPROBE_SCORE_MAX;
2363 if (av_stristr(p->buf, "dash:profile")) {
2364 return AVPROBE_SCORE_MAX;
2370 #define OFFSET(x) offsetof(DASHContext, x)
2371 #define FLAGS AV_OPT_FLAG_DECODING_PARAM
2372 static const AVOption dash_options[] = {
2373 {"allowed_extensions", "List of file extensions that dash is allowed to access",
2374 OFFSET(allowed_extensions), AV_OPT_TYPE_STRING,
2375 {.str = "aac,m4a,m4s,m4v,mov,mp4,webm,ts"},
2376 INT_MIN, INT_MAX, FLAGS},
2380 static const AVClass dash_class = {
2381 .class_name = "dash",
2382 .item_name = av_default_item_name,
2383 .option = dash_options,
2384 .version = LIBAVUTIL_VERSION_INT,
2387 AVInputFormat ff_dash_demuxer = {
2389 .long_name = NULL_IF_CONFIG_SMALL("Dynamic Adaptive Streaming over HTTP"),
2390 .priv_class = &dash_class,
2391 .priv_data_size = sizeof(DASHContext),
2392 .read_probe = dash_probe,
2393 .read_header = dash_read_header,
2394 .read_packet = dash_read_packet,
2395 .read_close = dash_close,
2396 .read_seek = dash_read_seek,
2397 .flags = AVFMT_NO_BYTE_SEEK,