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
40 * reference to : ISO_IEC_23009-1-DASH-2012
42 * Table: Table 17 — Semantics of SegmentTimeline element
45 /* starttime: Element or Attribute Name
46 * specifies the MPD start time, in @timescale units,
47 * the first Segment in the series starts relative to the beginning of the Period.
48 * The value of this attribute must be equal to or greater than the sum of the previous S
49 * element earliest presentation time and the sum of the contiguous Segment durations.
50 * If the value of the attribute is greater than what is expressed by the previous S element,
51 * it expresses discontinuities in the timeline.
52 * If not present then the value shall be assumed to be zero for the first S element
53 * and for the subsequent S elements, the value shall be assumed to be the sum of
54 * the previous S element's earliest presentation time and contiguous duration
55 * (i.e. previous S@starttime + @duration * (@repeat + 1)).
58 /* repeat: Element or Attribute Name
59 * specifies the repeat count of the number of following contiguous Segments with
60 * the same duration expressed by the value of @duration. This value is zero-based
61 * (e.g. a value of three means four Segments in the contiguous series).
64 /* duration: Element or Attribute Name
65 * specifies the Segment duration, in units of the value of the @timescale.
71 * Each playlist has its own demuxer. If it is currently active,
72 * it has an opened AVIOContext too, and potentially an AVPacket
73 * containing the next packet from this stream.
75 struct representation {
79 AVFormatContext *parent;
86 enum AVMediaType type;
90 AVStream *assoc_stream; /* demuxer stream associated with this representation */
93 struct fragment **fragments; /* VOD list of fragment for profile */
96 struct timeline **timelines;
100 int64_t start_number; /* used in case when we have dynamic list of segment to know which segments are new one*/
102 int64_t fragment_duration;
103 int64_t fragment_timescale;
105 int64_t presentation_timeoffset;
108 int64_t cur_seg_offset;
109 int64_t cur_seg_size;
110 struct fragment *cur_seg;
112 /* Currently active Media Initialization Section */
113 struct fragment *init_section;
114 uint8_t *init_sec_buf;
115 uint32_t init_sec_buf_size;
116 uint32_t init_sec_data_len;
117 uint32_t init_sec_buf_read_offset;
118 int64_t cur_timestamp;
119 int is_restart_needed;
122 typedef struct DASHContext {
123 const AVClass *class;
125 char *adaptionset_contenttype_val;
126 char *adaptionset_par_val;
127 char *adaptionset_lang_val;
128 char *adaptionset_minbw_val;
129 char *adaptionset_maxbw_val;
130 char *adaptionset_minwidth_val;
131 char *adaptionset_maxwidth_val;
132 char *adaptionset_minheight_val;
133 char *adaptionset_maxheight_val;
134 char *adaptionset_minframerate_val;
135 char *adaptionset_maxframerate_val;
136 char *adaptionset_segmentalignment_val;
137 char *adaptionset_bitstreamswitching_val;
140 struct representation **videos;
142 struct representation **audios;
144 struct representation **subtitles;
146 /* MediaPresentationDescription Attribute */
147 uint64_t media_presentation_duration;
148 uint64_t suggested_presentation_delay;
149 uint64_t availability_start_time;
150 uint64_t availability_end_time;
151 uint64_t publish_time;
152 uint64_t minimum_update_period;
153 uint64_t time_shift_buffer_depth;
154 uint64_t min_buffer_time;
156 /* Period Attribute */
157 uint64_t period_duration;
158 uint64_t period_start;
161 AVIOInterruptCB *interrupt_callback;
162 char *allowed_extensions;
163 AVDictionary *avio_opts;
166 /* Flags for init section*/
167 int is_init_section_common_video;
168 int is_init_section_common_audio;
172 static int ishttp(char *url)
174 const char *proto_name = avio_find_protocol_name(url);
175 return av_strstart(proto_name, "http", NULL);
178 static int aligned(int val)
180 return ((val + 0x3F) >> 6) << 6;
183 static uint64_t get_current_time_in_sec(void)
185 return av_gettime() / 1000000;
188 static uint64_t get_utc_date_time_insec(AVFormatContext *s, const char *datetime)
199 /* ISO-8601 date parser */
203 ret = sscanf(datetime, "%d-%d-%dT%d:%d:%fZ", &year, &month, &day, &hour, &minute, &second);
204 /* year, month, day, hour, minute, second 6 arguments */
206 av_log(s, AV_LOG_WARNING, "get_utc_date_time_insec get a wrong time format\n");
208 timeinfo.tm_year = year - 1900;
209 timeinfo.tm_mon = month - 1;
210 timeinfo.tm_mday = day;
211 timeinfo.tm_hour = hour;
212 timeinfo.tm_min = minute;
213 timeinfo.tm_sec = (int)second;
215 return av_timegm(&timeinfo);
218 static uint32_t get_duration_insec(AVFormatContext *s, const char *duration)
220 /* ISO-8601 duration parser */
228 const char *ptr = duration;
231 if (*ptr == 'P' || *ptr == 'T') {
236 if (sscanf(ptr, "%f%c%n", &value, &type, &size) != 2) {
237 av_log(s, AV_LOG_WARNING, "get_duration_insec get a wrong time format\n");
238 return 0; /* parser error */
242 days = (uint32_t)value;
245 hours = (uint32_t)value;
248 mins = (uint32_t)value;
251 secs = (uint32_t)value;
254 // handle invalid type
259 return ((days * 24 + hours) * 60 + mins) * 60 + secs;
262 static int64_t get_segment_start_time_based_on_timeline(struct representation *pls, int64_t cur_seq_no)
264 int64_t start_time = 0;
269 if (pls->n_timelines) {
270 for (i = 0; i < pls->n_timelines; i++) {
271 if (pls->timelines[i]->starttime > 0) {
272 start_time = pls->timelines[i]->starttime;
274 if (num == cur_seq_no)
277 start_time += pls->timelines[i]->duration;
279 if (pls->timelines[i]->repeat == -1) {
280 start_time = pls->timelines[i]->duration * cur_seq_no;
284 for (j = 0; j < pls->timelines[i]->repeat; j++) {
286 if (num == cur_seq_no)
288 start_time += pls->timelines[i]->duration;
297 static int64_t calc_next_seg_no_from_timelines(struct representation *pls, int64_t cur_time)
302 int64_t start_time = 0;
304 for (i = 0; i < pls->n_timelines; i++) {
305 if (pls->timelines[i]->starttime > 0) {
306 start_time = pls->timelines[i]->starttime;
308 if (start_time > cur_time)
311 start_time += pls->timelines[i]->duration;
312 for (j = 0; j < pls->timelines[i]->repeat; j++) {
314 if (start_time > cur_time)
316 start_time += pls->timelines[i]->duration;
327 static void free_fragment(struct fragment **seg)
332 av_freep(&(*seg)->url);
336 static void free_fragment_list(struct representation *pls)
340 for (i = 0; i < pls->n_fragments; i++) {
341 free_fragment(&pls->fragments[i]);
343 av_freep(&pls->fragments);
344 pls->n_fragments = 0;
347 static void free_timelines_list(struct representation *pls)
351 for (i = 0; i < pls->n_timelines; i++) {
352 av_freep(&pls->timelines[i]);
354 av_freep(&pls->timelines);
355 pls->n_timelines = 0;
358 static void free_representation(struct representation *pls)
360 free_fragment_list(pls);
361 free_timelines_list(pls);
362 free_fragment(&pls->cur_seg);
363 free_fragment(&pls->init_section);
364 av_freep(&pls->init_sec_buf);
365 av_freep(&pls->pb.buffer);
367 ff_format_io_close(pls->parent, &pls->input);
370 avformat_close_input(&pls->ctx);
373 av_freep(&pls->url_template);
377 static void free_video_list(DASHContext *c)
380 for (i = 0; i < c->n_videos; i++) {
381 struct representation *pls = c->videos[i];
382 free_representation(pls);
384 av_freep(&c->videos);
388 static void free_audio_list(DASHContext *c)
391 for (i = 0; i < c->n_audios; i++) {
392 struct representation *pls = c->audios[i];
393 free_representation(pls);
395 av_freep(&c->audios);
399 static void free_subtitle_list(DASHContext *c)
402 for (i = 0; i < c->n_subtitles; i++) {
403 struct representation *pls = c->subtitles[i];
404 free_representation(pls);
406 av_freep(&c->subtitles);
410 static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
411 AVDictionary *opts, AVDictionary *opts2, int *is_http)
413 DASHContext *c = s->priv_data;
414 AVDictionary *tmp = NULL;
415 const char *proto_name = NULL;
418 av_dict_copy(&tmp, opts, 0);
419 av_dict_copy(&tmp, opts2, 0);
421 if (av_strstart(url, "crypto", NULL)) {
422 if (url[6] == '+' || url[6] == ':')
423 proto_name = avio_find_protocol_name(url + 7);
427 proto_name = avio_find_protocol_name(url);
430 return AVERROR_INVALIDDATA;
432 // only http(s) & file are allowed
433 if (av_strstart(proto_name, "file", NULL)) {
434 if (strcmp(c->allowed_extensions, "ALL") && !av_match_ext(url, c->allowed_extensions)) {
435 av_log(s, AV_LOG_ERROR,
436 "Filename extension of \'%s\' is not a common multimedia extension, blocked for security reasons.\n"
437 "If you wish to override this adjust allowed_extensions, you can set it to \'ALL\' to allow all\n",
439 return AVERROR_INVALIDDATA;
441 } else if (av_strstart(proto_name, "http", NULL)) {
444 return AVERROR_INVALIDDATA;
446 if (!strncmp(proto_name, url, strlen(proto_name)) && url[strlen(proto_name)] == ':')
448 else if (av_strstart(url, "crypto", NULL) && !strncmp(proto_name, url + 7, strlen(proto_name)) && url[7 + strlen(proto_name)] == ':')
450 else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5))
451 return AVERROR_INVALIDDATA;
454 ret = avio_open2(pb, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp);
456 // update cookies on http response with setcookies.
457 char *new_cookies = NULL;
459 if (!(s->flags & AVFMT_FLAG_CUSTOM_IO))
460 av_opt_get(*pb, "cookies", AV_OPT_SEARCH_CHILDREN, (uint8_t**)&new_cookies);
463 av_dict_set(&opts, "cookies", new_cookies, AV_DICT_DONT_STRDUP_VAL);
471 *is_http = av_strstart(proto_name, "http", NULL);
476 static char *get_content_url(xmlNodePtr *baseurl_nodes,
480 char *rep_bandwidth_val,
486 char *tmp_str = av_mallocz(max_url_size);
487 char *tmp_str_2 = av_mallocz(max_url_size);
489 if (!tmp_str || !tmp_str_2) {
493 for (i = 0; i < n_baseurl_nodes; ++i) {
494 if (baseurl_nodes[i] &&
495 baseurl_nodes[i]->children &&
496 baseurl_nodes[i]->children->type == XML_TEXT_NODE) {
497 text = xmlNodeGetContent(baseurl_nodes[i]->children);
499 memset(tmp_str, 0, max_url_size);
500 memset(tmp_str_2, 0, max_url_size);
501 ff_make_absolute_url(tmp_str_2, max_url_size, tmp_str, text);
502 av_strlcpy(tmp_str, tmp_str_2, max_url_size);
509 ff_make_absolute_url(tmp_str, max_url_size, tmp_str, val);
512 url = av_strireplace(tmp_str, "$RepresentationID$", (const char*)rep_id_val);
516 av_strlcpy(tmp_str, url, max_url_size);
518 if (rep_bandwidth_val && tmp_str[0] != '\0') {
519 // free any previously assigned url before reassigning
521 url = av_strireplace(tmp_str, "$Bandwidth$", (const char*)rep_bandwidth_val);
532 static char *get_val_from_nodes_tab(xmlNodePtr *nodes, const int n_nodes, const char *attrname)
537 for (i = 0; i < n_nodes; ++i) {
539 val = xmlGetProp(nodes[i], attrname);
548 static xmlNodePtr find_child_node_by_name(xmlNodePtr rootnode, const char *nodename)
550 xmlNodePtr node = rootnode;
555 node = xmlFirstElementChild(node);
557 if (!av_strcasecmp(node->name, nodename)) {
560 node = xmlNextElementSibling(node);
565 static enum AVMediaType get_content_type(xmlNodePtr node)
567 enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
573 for (i = 0; i < 2; i++) {
574 attr = i ? "mimeType" : "contentType";
575 val = xmlGetProp(node, attr);
577 if (av_stristr((const char *)val, "video")) {
578 type = AVMEDIA_TYPE_VIDEO;
579 } else if (av_stristr((const char *)val, "audio")) {
580 type = AVMEDIA_TYPE_AUDIO;
581 } else if (av_stristr((const char *)val, "text")) {
582 type = AVMEDIA_TYPE_SUBTITLE;
591 static struct fragment * get_Fragment(char *range)
593 struct fragment * seg = av_mallocz(sizeof(struct fragment));
600 char *str_end_offset;
601 char *str_offset = av_strtok(range, "-", &str_end_offset);
602 seg->url_offset = strtoll(str_offset, NULL, 10);
603 seg->size = strtoll(str_end_offset, NULL, 10) - seg->url_offset;
609 static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representation *rep,
610 xmlNodePtr fragmenturl_node,
611 xmlNodePtr *baseurl_nodes,
613 char *rep_bandwidth_val)
615 DASHContext *c = s->priv_data;
616 char *initialization_val = NULL;
617 char *media_val = NULL;
618 char *range_val = NULL;
619 int max_url_size = c ? c->max_url_size: MAX_URL_SIZE;
621 if (!av_strcasecmp(fragmenturl_node->name, (const char *)"Initialization")) {
622 initialization_val = xmlGetProp(fragmenturl_node, "sourceURL");
623 range_val = xmlGetProp(fragmenturl_node, "range");
624 if (initialization_val || range_val) {
625 rep->init_section = get_Fragment(range_val);
626 if (!rep->init_section) {
627 xmlFree(initialization_val);
629 return AVERROR(ENOMEM);
631 rep->init_section->url = get_content_url(baseurl_nodes, 4,
637 if (!rep->init_section->url) {
638 av_free(rep->init_section);
639 xmlFree(initialization_val);
641 return AVERROR(ENOMEM);
643 xmlFree(initialization_val);
646 } else if (!av_strcasecmp(fragmenturl_node->name, (const char *)"SegmentURL")) {
647 media_val = xmlGetProp(fragmenturl_node, "media");
648 range_val = xmlGetProp(fragmenturl_node, "mediaRange");
649 if (media_val || range_val) {
650 struct fragment *seg = get_Fragment(range_val);
654 return AVERROR(ENOMEM);
656 seg->url = get_content_url(baseurl_nodes, 4,
665 return AVERROR(ENOMEM);
667 dynarray_add(&rep->fragments, &rep->n_fragments, seg);
676 static int parse_manifest_segmenttimeline(AVFormatContext *s, struct representation *rep,
677 xmlNodePtr fragment_timeline_node)
679 xmlAttrPtr attr = NULL;
682 if (!av_strcasecmp(fragment_timeline_node->name, (const char *)"S")) {
683 struct timeline *tml = av_mallocz(sizeof(struct timeline));
685 return AVERROR(ENOMEM);
687 attr = fragment_timeline_node->properties;
689 val = xmlGetProp(fragment_timeline_node, attr->name);
692 av_log(s, AV_LOG_WARNING, "parse_manifest_segmenttimeline attr->name = %s val is NULL\n", attr->name);
696 if (!av_strcasecmp(attr->name, (const char *)"t")) {
697 tml->starttime = (int64_t)strtoll(val, NULL, 10);
698 } else if (!av_strcasecmp(attr->name, (const char *)"r")) {
699 tml->repeat =(int64_t) strtoll(val, NULL, 10);
700 } else if (!av_strcasecmp(attr->name, (const char *)"d")) {
701 tml->duration = (int64_t)strtoll(val, NULL, 10);
706 dynarray_add(&rep->timelines, &rep->n_timelines, tml);
712 static int resolve_content_path(AVFormatContext *s, const char *url, int *max_url_size, xmlNodePtr *baseurl_nodes, int n_baseurl_nodes) {
714 char *tmp_str = NULL;
716 char *mpdName = NULL;
717 xmlNodePtr node = NULL;
718 char *baseurl = NULL;
719 char *root_url = NULL;
730 int tmp_max_url_size = strlen(url);
732 for (i = n_baseurl_nodes-1; i >= 0 ; i--) {
733 text = xmlNodeGetContent(baseurl_nodes[i]);
736 tmp_max_url_size += strlen(text);
744 tmp_max_url_size = aligned(tmp_max_url_size);
745 text = av_mallocz(tmp_max_url_size);
747 updated = AVERROR(ENOMEM);
750 av_strlcpy(text, url, strlen(url)+1);
752 while (mpdName = av_strtok(tmp, "/", &tmp)) {
753 size = strlen(mpdName);
757 path = av_mallocz(tmp_max_url_size);
758 tmp_str = av_mallocz(tmp_max_url_size);
759 if (!tmp_str || !path) {
760 updated = AVERROR(ENOMEM);
764 av_strlcpy (path, url, strlen(url) - size + 1);
765 for (rootId = n_baseurl_nodes - 1; rootId > 0; rootId --) {
766 if (!(node = baseurl_nodes[rootId])) {
769 text = xmlNodeGetContent(node);
777 node = baseurl_nodes[rootId];
778 baseurl = xmlNodeGetContent(node);
779 root_url = (av_strcasecmp(baseurl, "")) ? baseurl : path;
781 xmlNodeSetContent(node, root_url);
785 size = strlen(root_url);
786 isRootHttp = ishttp(root_url);
788 if (root_url[size - 1] != token) {
789 av_strlcat(root_url, "/", size + 2);
793 for (i = 0; i < n_baseurl_nodes; ++i) {
797 text = xmlNodeGetContent(baseurl_nodes[i]);
799 memset(tmp_str, 0, strlen(tmp_str));
800 if (!ishttp(text) && isRootHttp) {
801 av_strlcpy(tmp_str, root_url, size + 1);
803 start = (text[0] == token);
804 av_strlcat(tmp_str, text + start, tmp_max_url_size);
805 xmlNodeSetContent(baseurl_nodes[i], tmp_str);
812 if (tmp_max_url_size > *max_url_size) {
813 *max_url_size = tmp_max_url_size;
822 static int parse_manifest_representation(AVFormatContext *s, const char *url,
824 xmlNodePtr adaptionset_node,
825 xmlNodePtr mpd_baseurl_node,
826 xmlNodePtr period_baseurl_node,
827 xmlNodePtr period_segmenttemplate_node,
828 xmlNodePtr period_segmentlist_node,
829 xmlNodePtr fragment_template_node,
830 xmlNodePtr content_component_node,
831 xmlNodePtr adaptionset_baseurl_node,
832 xmlNodePtr adaptionset_segmentlist_node,
833 xmlNodePtr adaptionset_supplementalproperty_node)
836 int32_t subtitle_rep_idx = 0;
837 int32_t audio_rep_idx = 0;
838 int32_t video_rep_idx = 0;
839 DASHContext *c = s->priv_data;
840 struct representation *rep = NULL;
841 struct fragment *seg = NULL;
842 xmlNodePtr representation_segmenttemplate_node = NULL;
843 xmlNodePtr representation_baseurl_node = NULL;
844 xmlNodePtr representation_segmentlist_node = NULL;
845 xmlNodePtr segmentlists_tab[2];
846 xmlNodePtr fragment_timeline_node = NULL;
847 xmlNodePtr fragment_templates_tab[5];
848 char *duration_val = NULL;
849 char *presentation_timeoffset_val = NULL;
850 char *startnumber_val = NULL;
851 char *timescale_val = NULL;
852 char *initialization_val = NULL;
853 char *media_val = NULL;
855 xmlNodePtr baseurl_nodes[4];
856 xmlNodePtr representation_node = node;
857 char *rep_id_val = xmlGetProp(representation_node, "id");
858 char *rep_bandwidth_val = xmlGetProp(representation_node, "bandwidth");
859 char *rep_framerate_val = xmlGetProp(representation_node, "frameRate");
860 enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
862 // try get information from representation
863 if (type == AVMEDIA_TYPE_UNKNOWN)
864 type = get_content_type(representation_node);
865 // try get information from contentComponen
866 if (type == AVMEDIA_TYPE_UNKNOWN)
867 type = get_content_type(content_component_node);
868 // try get information from adaption set
869 if (type == AVMEDIA_TYPE_UNKNOWN)
870 type = get_content_type(adaptionset_node);
871 if (type == AVMEDIA_TYPE_UNKNOWN) {
872 av_log(s, AV_LOG_VERBOSE, "Parsing '%s' - skipp not supported representation type\n", url);
873 } else if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO || type == AVMEDIA_TYPE_SUBTITLE) {
874 // convert selected representation to our internal struct
875 rep = av_mallocz(sizeof(struct representation));
877 ret = AVERROR(ENOMEM);
880 representation_segmenttemplate_node = find_child_node_by_name(representation_node, "SegmentTemplate");
881 representation_baseurl_node = find_child_node_by_name(representation_node, "BaseURL");
882 representation_segmentlist_node = find_child_node_by_name(representation_node, "SegmentList");
884 baseurl_nodes[0] = mpd_baseurl_node;
885 baseurl_nodes[1] = period_baseurl_node;
886 baseurl_nodes[2] = adaptionset_baseurl_node;
887 baseurl_nodes[3] = representation_baseurl_node;
889 ret = resolve_content_path(s, url, &c->max_url_size, baseurl_nodes, 4);
890 c->max_url_size = aligned(c->max_url_size
891 + (rep_id_val ? strlen(rep_id_val) : 0)
892 + (rep_bandwidth_val ? strlen(rep_bandwidth_val) : 0));
893 if (ret == AVERROR(ENOMEM) || ret == 0) {
896 if (representation_segmenttemplate_node || fragment_template_node || period_segmenttemplate_node) {
897 fragment_timeline_node = NULL;
898 fragment_templates_tab[0] = representation_segmenttemplate_node;
899 fragment_templates_tab[1] = adaptionset_segmentlist_node;
900 fragment_templates_tab[2] = fragment_template_node;
901 fragment_templates_tab[3] = period_segmenttemplate_node;
902 fragment_templates_tab[4] = period_segmentlist_node;
904 presentation_timeoffset_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "presentationTimeOffset");
905 duration_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "duration");
906 startnumber_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "startNumber");
907 timescale_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "timescale");
908 initialization_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "initialization");
909 media_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "media");
911 if (initialization_val) {
912 rep->init_section = av_mallocz(sizeof(struct fragment));
913 if (!rep->init_section) {
915 ret = AVERROR(ENOMEM);
918 c->max_url_size = aligned(c->max_url_size + strlen(initialization_val));
919 rep->init_section->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, initialization_val);
920 if (!rep->init_section->url) {
921 av_free(rep->init_section);
923 ret = AVERROR(ENOMEM);
926 rep->init_section->size = -1;
927 xmlFree(initialization_val);
931 c->max_url_size = aligned(c->max_url_size + strlen(media_val));
932 rep->url_template = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, media_val);
936 if (presentation_timeoffset_val) {
937 rep->presentation_timeoffset = (int64_t) strtoll(presentation_timeoffset_val, NULL, 10);
938 av_log(s, AV_LOG_TRACE, "rep->presentation_timeoffset = [%"PRId64"]\n", rep->presentation_timeoffset);
939 xmlFree(presentation_timeoffset_val);
942 rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
943 av_log(s, AV_LOG_TRACE, "rep->fragment_duration = [%"PRId64"]\n", rep->fragment_duration);
944 xmlFree(duration_val);
947 rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
948 av_log(s, AV_LOG_TRACE, "rep->fragment_timescale = [%"PRId64"]\n", rep->fragment_timescale);
949 xmlFree(timescale_val);
951 if (startnumber_val) {
952 rep->first_seq_no = (int64_t) strtoll(startnumber_val, NULL, 10);
953 av_log(s, AV_LOG_TRACE, "rep->first_seq_no = [%"PRId64"]\n", rep->first_seq_no);
954 xmlFree(startnumber_val);
956 if (adaptionset_supplementalproperty_node) {
957 if (!av_strcasecmp(xmlGetProp(adaptionset_supplementalproperty_node,"schemeIdUri"), "http://dashif.org/guidelines/last-segment-number")) {
958 val = xmlGetProp(adaptionset_supplementalproperty_node,"value");
960 av_log(s, AV_LOG_ERROR, "Missing value attribute in adaptionset_supplementalproperty_node\n");
962 rep->last_seq_no =(int64_t) strtoll(val, NULL, 10) - 1;
968 fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
970 if (!fragment_timeline_node)
971 fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
972 if (!fragment_timeline_node)
973 fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
974 if (!fragment_timeline_node)
975 fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
976 if (fragment_timeline_node) {
977 fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
978 while (fragment_timeline_node) {
979 ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
983 fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
986 } else if (representation_baseurl_node && !representation_segmentlist_node) {
987 seg = av_mallocz(sizeof(struct fragment));
989 ret = AVERROR(ENOMEM);
992 seg->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, NULL);
995 ret = AVERROR(ENOMEM);
999 dynarray_add(&rep->fragments, &rep->n_fragments, seg);
1000 } else if (representation_segmentlist_node) {
1001 // TODO: https://www.brendanlong.com/the-structure-of-an-mpeg-dash-mpd.html
1002 // http://www-itec.uni-klu.ac.at/dash/ddash/mpdGenerator.php?fragmentlength=15&type=full
1003 xmlNodePtr fragmenturl_node = NULL;
1004 segmentlists_tab[0] = representation_segmentlist_node;
1005 segmentlists_tab[1] = adaptionset_segmentlist_node;
1007 duration_val = get_val_from_nodes_tab(segmentlists_tab, 2, "duration");
1008 timescale_val = get_val_from_nodes_tab(segmentlists_tab, 2, "timescale");
1010 rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
1011 av_log(s, AV_LOG_TRACE, "rep->fragment_duration = [%"PRId64"]\n", rep->fragment_duration);
1012 xmlFree(duration_val);
1014 if (timescale_val) {
1015 rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
1016 av_log(s, AV_LOG_TRACE, "rep->fragment_timescale = [%"PRId64"]\n", rep->fragment_timescale);
1017 xmlFree(timescale_val);
1019 fragmenturl_node = xmlFirstElementChild(representation_segmentlist_node);
1020 while (fragmenturl_node) {
1021 ret = parse_manifest_segmenturlnode(s, rep, fragmenturl_node,
1028 fragmenturl_node = xmlNextElementSibling(fragmenturl_node);
1031 fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
1033 if (!fragment_timeline_node)
1034 fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
1035 if (!fragment_timeline_node)
1036 fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
1037 if (!fragment_timeline_node)
1038 fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
1039 if (fragment_timeline_node) {
1040 fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
1041 while (fragment_timeline_node) {
1042 ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
1046 fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
1050 free_representation(rep);
1052 av_log(s, AV_LOG_ERROR, "Unknown format of Representation node id[%s] \n", (const char *)rep_id_val);
1056 if (rep->fragment_duration > 0 && !rep->fragment_timescale)
1057 rep->fragment_timescale = 1;
1058 rep->bandwidth = rep_bandwidth_val ? atoi(rep_bandwidth_val) : 0;
1059 strncpy(rep->id, rep_id_val ? rep_id_val : "", sizeof(rep->id));
1060 rep->framerate = av_make_q(0, 0);
1061 if (type == AVMEDIA_TYPE_VIDEO && rep_framerate_val) {
1062 ret = av_parse_video_rate(&rep->framerate, rep_framerate_val);
1064 av_log(s, AV_LOG_VERBOSE, "Ignoring invalid frame rate '%s'\n", rep_framerate_val);
1068 case AVMEDIA_TYPE_VIDEO:
1069 rep->rep_idx = video_rep_idx;
1070 dynarray_add(&c->videos, &c->n_videos, rep);
1072 case AVMEDIA_TYPE_AUDIO:
1073 rep->rep_idx = audio_rep_idx;
1074 dynarray_add(&c->audios, &c->n_audios, rep);
1076 case AVMEDIA_TYPE_SUBTITLE:
1077 rep->rep_idx = subtitle_rep_idx;
1078 dynarray_add(&c->subtitles, &c->n_subtitles, rep);
1081 av_log(s, AV_LOG_WARNING, "Unsupported the stream type %d\n", type);
1087 video_rep_idx += type == AVMEDIA_TYPE_VIDEO;
1088 audio_rep_idx += type == AVMEDIA_TYPE_AUDIO;
1089 subtitle_rep_idx += type == AVMEDIA_TYPE_SUBTITLE;
1093 xmlFree(rep_id_val);
1094 if (rep_bandwidth_val)
1095 xmlFree(rep_bandwidth_val);
1096 if (rep_framerate_val)
1097 xmlFree(rep_framerate_val);
1102 static int parse_manifest_adaptationset(AVFormatContext *s, const char *url,
1103 xmlNodePtr adaptionset_node,
1104 xmlNodePtr mpd_baseurl_node,
1105 xmlNodePtr period_baseurl_node,
1106 xmlNodePtr period_segmenttemplate_node,
1107 xmlNodePtr period_segmentlist_node)
1110 DASHContext *c = s->priv_data;
1111 xmlNodePtr fragment_template_node = NULL;
1112 xmlNodePtr content_component_node = NULL;
1113 xmlNodePtr adaptionset_baseurl_node = NULL;
1114 xmlNodePtr adaptionset_segmentlist_node = NULL;
1115 xmlNodePtr adaptionset_supplementalproperty_node = NULL;
1116 xmlNodePtr node = NULL;
1117 c->adaptionset_contenttype_val = xmlGetProp(adaptionset_node, "contentType");
1118 c->adaptionset_par_val = xmlGetProp(adaptionset_node, "par");
1119 c->adaptionset_lang_val = xmlGetProp(adaptionset_node, "lang");
1120 c->adaptionset_minbw_val = xmlGetProp(adaptionset_node, "minBandwidth");
1121 c->adaptionset_maxbw_val = xmlGetProp(adaptionset_node, "maxBandwidth");
1122 c->adaptionset_minwidth_val = xmlGetProp(adaptionset_node, "minWidth");
1123 c->adaptionset_maxwidth_val = xmlGetProp(adaptionset_node, "maxWidth");
1124 c->adaptionset_minheight_val = xmlGetProp(adaptionset_node, "minHeight");
1125 c->adaptionset_maxheight_val = xmlGetProp(adaptionset_node, "maxHeight");
1126 c->adaptionset_minframerate_val = xmlGetProp(adaptionset_node, "minFrameRate");
1127 c->adaptionset_maxframerate_val = xmlGetProp(adaptionset_node, "maxFrameRate");
1128 c->adaptionset_segmentalignment_val = xmlGetProp(adaptionset_node, "segmentAlignment");
1129 c->adaptionset_bitstreamswitching_val = xmlGetProp(adaptionset_node, "bitstreamSwitching");
1131 node = xmlFirstElementChild(adaptionset_node);
1133 if (!av_strcasecmp(node->name, (const char *)"SegmentTemplate")) {
1134 fragment_template_node = node;
1135 } else if (!av_strcasecmp(node->name, (const char *)"ContentComponent")) {
1136 content_component_node = node;
1137 } else if (!av_strcasecmp(node->name, (const char *)"BaseURL")) {
1138 adaptionset_baseurl_node = node;
1139 } else if (!av_strcasecmp(node->name, (const char *)"SegmentList")) {
1140 adaptionset_segmentlist_node = node;
1141 } else if (!av_strcasecmp(node->name, (const char *)"SupplementalProperty")) {
1142 adaptionset_supplementalproperty_node = node;
1143 } else if (!av_strcasecmp(node->name, (const char *)"Representation")) {
1144 ret = parse_manifest_representation(s, url, node,
1147 period_baseurl_node,
1148 period_segmenttemplate_node,
1149 period_segmentlist_node,
1150 fragment_template_node,
1151 content_component_node,
1152 adaptionset_baseurl_node,
1153 adaptionset_segmentlist_node,
1154 adaptionset_supplementalproperty_node);
1159 node = xmlNextElementSibling(node);
1164 static int parse_programinformation(AVFormatContext *s, xmlNodePtr node)
1166 xmlChar *val = NULL;
1168 node = xmlFirstElementChild(node);
1170 if (!av_strcasecmp(node->name, "Title")) {
1171 val = xmlNodeGetContent(node);
1173 av_dict_set(&s->metadata, "Title", val, 0);
1175 } else if (!av_strcasecmp(node->name, "Source")) {
1176 val = xmlNodeGetContent(node);
1178 av_dict_set(&s->metadata, "Source", val, 0);
1180 } else if (!av_strcasecmp(node->name, "Copyright")) {
1181 val = xmlNodeGetContent(node);
1183 av_dict_set(&s->metadata, "Copyright", val, 0);
1186 node = xmlNextElementSibling(node);
1192 static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
1194 DASHContext *c = s->priv_data;
1197 uint8_t *new_url = NULL;
1198 int64_t filesize = 0;
1199 char *buffer = NULL;
1200 AVDictionary *opts = NULL;
1202 xmlNodePtr root_element = NULL;
1203 xmlNodePtr node = NULL;
1204 xmlNodePtr period_node = NULL;
1205 xmlNodePtr tmp_node = NULL;
1206 xmlNodePtr mpd_baseurl_node = NULL;
1207 xmlNodePtr period_baseurl_node = NULL;
1208 xmlNodePtr period_segmenttemplate_node = NULL;
1209 xmlNodePtr period_segmentlist_node = NULL;
1210 xmlNodePtr adaptionset_node = NULL;
1211 xmlAttrPtr attr = NULL;
1213 uint32_t period_duration_sec = 0;
1214 uint32_t period_start_sec = 0;
1219 av_dict_copy(&opts, c->avio_opts, 0);
1220 ret = avio_open2(&in, url, AVIO_FLAG_READ, c->interrupt_callback, &opts);
1221 av_dict_free(&opts);
1226 if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, &new_url) >= 0) {
1227 c->base_url = av_strdup(new_url);
1229 c->base_url = av_strdup(url);
1232 filesize = avio_size(in);
1233 if (filesize <= 0) {
1234 filesize = 8 * 1024;
1237 buffer = av_mallocz(filesize);
1239 av_free(c->base_url);
1240 return AVERROR(ENOMEM);
1243 filesize = avio_read(in, buffer, filesize);
1244 if (filesize <= 0) {
1245 av_log(s, AV_LOG_ERROR, "Unable to read to offset '%s'\n", url);
1246 ret = AVERROR_INVALIDDATA;
1250 doc = xmlReadMemory(buffer, filesize, c->base_url, NULL, 0);
1251 root_element = xmlDocGetRootElement(doc);
1252 node = root_element;
1255 ret = AVERROR_INVALIDDATA;
1256 av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing root node\n", url);
1260 if (node->type != XML_ELEMENT_NODE ||
1261 av_strcasecmp(node->name, (const char *)"MPD")) {
1262 ret = AVERROR_INVALIDDATA;
1263 av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - wrong root node name[%s] type[%d]\n", url, node->name, (int)node->type);
1267 val = xmlGetProp(node, "type");
1269 av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing type attrib\n", url);
1270 ret = AVERROR_INVALIDDATA;
1273 if (!av_strcasecmp(val, (const char *)"dynamic"))
1277 attr = node->properties;
1279 val = xmlGetProp(node, attr->name);
1281 if (!av_strcasecmp(attr->name, (const char *)"availabilityStartTime")) {
1282 c->availability_start_time = get_utc_date_time_insec(s, (const char *)val);
1283 av_log(s, AV_LOG_TRACE, "c->availability_start_time = [%"PRId64"]\n", c->availability_start_time);
1284 } else if (!av_strcasecmp(attr->name, (const char *)"availabilityEndTime")) {
1285 c->availability_end_time = get_utc_date_time_insec(s, (const char *)val);
1286 av_log(s, AV_LOG_TRACE, "c->availability_end_time = [%"PRId64"]\n", c->availability_end_time);
1287 } else if (!av_strcasecmp(attr->name, (const char *)"publishTime")) {
1288 c->publish_time = get_utc_date_time_insec(s, (const char *)val);
1289 av_log(s, AV_LOG_TRACE, "c->publish_time = [%"PRId64"]\n", c->publish_time);
1290 } else if (!av_strcasecmp(attr->name, (const char *)"minimumUpdatePeriod")) {
1291 c->minimum_update_period = get_duration_insec(s, (const char *)val);
1292 av_log(s, AV_LOG_TRACE, "c->minimum_update_period = [%"PRId64"]\n", c->minimum_update_period);
1293 } else if (!av_strcasecmp(attr->name, (const char *)"timeShiftBufferDepth")) {
1294 c->time_shift_buffer_depth = get_duration_insec(s, (const char *)val);
1295 av_log(s, AV_LOG_TRACE, "c->time_shift_buffer_depth = [%"PRId64"]\n", c->time_shift_buffer_depth);
1296 } else if (!av_strcasecmp(attr->name, (const char *)"minBufferTime")) {
1297 c->min_buffer_time = get_duration_insec(s, (const char *)val);
1298 av_log(s, AV_LOG_TRACE, "c->min_buffer_time = [%"PRId64"]\n", c->min_buffer_time);
1299 } else if (!av_strcasecmp(attr->name, (const char *)"suggestedPresentationDelay")) {
1300 c->suggested_presentation_delay = get_duration_insec(s, (const char *)val);
1301 av_log(s, AV_LOG_TRACE, "c->suggested_presentation_delay = [%"PRId64"]\n", c->suggested_presentation_delay);
1302 } else if (!av_strcasecmp(attr->name, (const char *)"mediaPresentationDuration")) {
1303 c->media_presentation_duration = get_duration_insec(s, (const char *)val);
1304 av_log(s, AV_LOG_TRACE, "c->media_presentation_duration = [%"PRId64"]\n", c->media_presentation_duration);
1310 tmp_node = find_child_node_by_name(node, "BaseURL");
1312 mpd_baseurl_node = xmlCopyNode(tmp_node,1);
1314 mpd_baseurl_node = xmlNewNode(NULL, "BaseURL");
1317 // at now we can handle only one period, with the longest duration
1318 node = xmlFirstElementChild(node);
1320 if (!av_strcasecmp(node->name, (const char *)"Period")) {
1321 period_duration_sec = 0;
1322 period_start_sec = 0;
1323 attr = node->properties;
1325 val = xmlGetProp(node, attr->name);
1326 if (!av_strcasecmp(attr->name, (const char *)"duration")) {
1327 period_duration_sec = get_duration_insec(s, (const char *)val);
1328 } else if (!av_strcasecmp(attr->name, (const char *)"start")) {
1329 period_start_sec = get_duration_insec(s, (const char *)val);
1334 if ((period_duration_sec) >= (c->period_duration)) {
1336 c->period_duration = period_duration_sec;
1337 c->period_start = period_start_sec;
1338 if (c->period_start > 0)
1339 c->media_presentation_duration = c->period_duration;
1341 } else if (!av_strcasecmp(node->name, "ProgramInformation")) {
1342 parse_programinformation(s, node);
1344 node = xmlNextElementSibling(node);
1347 av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing Period node\n", url);
1348 ret = AVERROR_INVALIDDATA;
1352 adaptionset_node = xmlFirstElementChild(period_node);
1353 while (adaptionset_node) {
1354 if (!av_strcasecmp(adaptionset_node->name, (const char *)"BaseURL")) {
1355 period_baseurl_node = adaptionset_node;
1356 } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"SegmentTemplate")) {
1357 period_segmenttemplate_node = adaptionset_node;
1358 } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"SegmentList")) {
1359 period_segmentlist_node = adaptionset_node;
1360 } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"AdaptationSet")) {
1361 parse_manifest_adaptationset(s, url, adaptionset_node, mpd_baseurl_node, period_baseurl_node, period_segmenttemplate_node, period_segmentlist_node);
1363 adaptionset_node = xmlNextElementSibling(adaptionset_node);
1366 /*free the document */
1369 xmlFreeNode(mpd_baseurl_node);
1380 static int64_t calc_cur_seg_no(AVFormatContext *s, struct representation *pls)
1382 DASHContext *c = s->priv_data;
1384 int64_t start_time_offset = 0;
1387 if (pls->n_fragments) {
1388 av_log(s, AV_LOG_TRACE, "in n_fragments mode\n");
1389 num = pls->first_seq_no;
1390 } else if (pls->n_timelines) {
1391 av_log(s, AV_LOG_TRACE, "in n_timelines mode\n");
1392 start_time_offset = get_segment_start_time_based_on_timeline(pls, 0xFFFFFFFF) - 60 * pls->fragment_timescale; // 60 seconds before end
1393 num = calc_next_seg_no_from_timelines(pls, start_time_offset);
1395 num = pls->first_seq_no;
1397 num += pls->first_seq_no;
1398 } else if (pls->fragment_duration){
1399 av_log(s, AV_LOG_TRACE, "in fragment_duration mode fragment_timescale = %"PRId64", presentation_timeoffset = %"PRId64"\n", pls->fragment_timescale, pls->presentation_timeoffset);
1400 if (pls->presentation_timeoffset) {
1401 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;
1402 } else if (c->publish_time > 0 && !c->availability_start_time) {
1403 if (c->min_buffer_time) {
1404 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;
1406 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;
1409 num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time) - c->suggested_presentation_delay) * pls->fragment_timescale) / pls->fragment_duration;
1413 num = pls->first_seq_no;
1418 static int64_t calc_min_seg_no(AVFormatContext *s, struct representation *pls)
1420 DASHContext *c = s->priv_data;
1423 if (c->is_live && pls->fragment_duration) {
1424 av_log(s, AV_LOG_TRACE, "in live mode\n");
1425 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;
1427 num = pls->first_seq_no;
1432 static int64_t calc_max_seg_no(struct representation *pls, DASHContext *c)
1436 if (pls->n_fragments) {
1437 num = pls->first_seq_no + pls->n_fragments - 1;
1438 } else if (pls->n_timelines) {
1440 num = pls->first_seq_no + pls->n_timelines - 1;
1441 for (i = 0; i < pls->n_timelines; i++) {
1442 if (pls->timelines[i]->repeat == -1) {
1443 int length_of_each_segment = pls->timelines[i]->duration / pls->fragment_timescale;
1444 num = c->period_duration / length_of_each_segment;
1446 num += pls->timelines[i]->repeat;
1449 } else if (c->is_live && pls->fragment_duration) {
1450 num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time)) * pls->fragment_timescale) / pls->fragment_duration;
1451 } else if (pls->fragment_duration) {
1452 num = pls->first_seq_no + (c->media_presentation_duration * pls->fragment_timescale) / pls->fragment_duration;
1458 static void move_timelines(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
1460 if (rep_dest && rep_src ) {
1461 free_timelines_list(rep_dest);
1462 rep_dest->timelines = rep_src->timelines;
1463 rep_dest->n_timelines = rep_src->n_timelines;
1464 rep_dest->first_seq_no = rep_src->first_seq_no;
1465 rep_dest->last_seq_no = calc_max_seg_no(rep_dest, c);
1466 rep_src->timelines = NULL;
1467 rep_src->n_timelines = 0;
1468 rep_dest->cur_seq_no = rep_src->cur_seq_no;
1472 static void move_segments(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
1474 if (rep_dest && rep_src ) {
1475 free_fragment_list(rep_dest);
1476 if (rep_src->start_number > (rep_dest->start_number + rep_dest->n_fragments))
1477 rep_dest->cur_seq_no = 0;
1479 rep_dest->cur_seq_no += rep_src->start_number - rep_dest->start_number;
1480 rep_dest->fragments = rep_src->fragments;
1481 rep_dest->n_fragments = rep_src->n_fragments;
1482 rep_dest->parent = rep_src->parent;
1483 rep_dest->last_seq_no = calc_max_seg_no(rep_dest, c);
1484 rep_src->fragments = NULL;
1485 rep_src->n_fragments = 0;
1490 static int refresh_manifest(AVFormatContext *s)
1494 DASHContext *c = s->priv_data;
1496 // save current context
1497 int n_videos = c->n_videos;
1498 struct representation **videos = c->videos;
1499 int n_audios = c->n_audios;
1500 struct representation **audios = c->audios;
1501 int n_subtitles = c->n_subtitles;
1502 struct representation **subtitles = c->subtitles;
1503 char *base_url = c->base_url;
1511 c->subtitles = NULL;
1512 ret = parse_manifest(s, s->url, NULL);
1516 if (c->n_videos != n_videos) {
1517 av_log(c, AV_LOG_ERROR,
1518 "new manifest has mismatched no. of video representations, %d -> %d\n",
1519 n_videos, c->n_videos);
1520 return AVERROR_INVALIDDATA;
1522 if (c->n_audios != n_audios) {
1523 av_log(c, AV_LOG_ERROR,
1524 "new manifest has mismatched no. of audio representations, %d -> %d\n",
1525 n_audios, c->n_audios);
1526 return AVERROR_INVALIDDATA;
1528 if (c->n_subtitles != n_subtitles) {
1529 av_log(c, AV_LOG_ERROR,
1530 "new manifest has mismatched no. of subtitles representations, %d -> %d\n",
1531 n_subtitles, c->n_subtitles);
1532 return AVERROR_INVALIDDATA;
1535 for (i = 0; i < n_videos; i++) {
1536 struct representation *cur_video = videos[i];
1537 struct representation *ccur_video = c->videos[i];
1538 if (cur_video->timelines) {
1539 // calc current time
1540 int64_t currentTime = get_segment_start_time_based_on_timeline(cur_video, cur_video->cur_seq_no) / cur_video->fragment_timescale;
1542 ccur_video->cur_seq_no = calc_next_seg_no_from_timelines(ccur_video, currentTime * cur_video->fragment_timescale - 1);
1543 if (ccur_video->cur_seq_no >= 0) {
1544 move_timelines(ccur_video, cur_video, c);
1547 if (cur_video->fragments) {
1548 move_segments(ccur_video, cur_video, c);
1551 for (i = 0; i < n_audios; i++) {
1552 struct representation *cur_audio = audios[i];
1553 struct representation *ccur_audio = c->audios[i];
1554 if (cur_audio->timelines) {
1555 // calc current time
1556 int64_t currentTime = get_segment_start_time_based_on_timeline(cur_audio, cur_audio->cur_seq_no) / cur_audio->fragment_timescale;
1558 ccur_audio->cur_seq_no = calc_next_seg_no_from_timelines(ccur_audio, currentTime * cur_audio->fragment_timescale - 1);
1559 if (ccur_audio->cur_seq_no >= 0) {
1560 move_timelines(ccur_audio, cur_audio, c);
1563 if (cur_audio->fragments) {
1564 move_segments(ccur_audio, cur_audio, c);
1573 c->base_url = base_url;
1576 free_subtitle_list(c);
1582 c->n_subtitles = n_subtitles;
1583 c->subtitles = subtitles;
1584 c->n_audios = n_audios;
1586 c->n_videos = n_videos;
1591 static struct fragment *get_current_fragment(struct representation *pls)
1593 int64_t min_seq_no = 0;
1594 int64_t max_seq_no = 0;
1595 struct fragment *seg = NULL;
1596 struct fragment *seg_ptr = NULL;
1597 DASHContext *c = pls->parent->priv_data;
1599 while (( !ff_check_interrupt(c->interrupt_callback)&& pls->n_fragments > 0)) {
1600 if (pls->cur_seq_no < pls->n_fragments) {
1601 seg_ptr = pls->fragments[pls->cur_seq_no];
1602 seg = av_mallocz(sizeof(struct fragment));
1606 seg->url = av_strdup(seg_ptr->url);
1611 seg->size = seg_ptr->size;
1612 seg->url_offset = seg_ptr->url_offset;
1614 } else if (c->is_live) {
1615 refresh_manifest(pls->parent);
1621 min_seq_no = calc_min_seg_no(pls->parent, pls);
1622 max_seq_no = calc_max_seg_no(pls, c);
1624 if (pls->timelines || pls->fragments) {
1625 refresh_manifest(pls->parent);
1627 if (pls->cur_seq_no <= min_seq_no) {
1628 av_log(pls->parent, AV_LOG_VERBOSE, "old fragment: cur[%"PRId64"] min[%"PRId64"] max[%"PRId64"], playlist %d\n", (int64_t)pls->cur_seq_no, min_seq_no, max_seq_no, (int)pls->rep_idx);
1629 pls->cur_seq_no = calc_cur_seg_no(pls->parent, pls);
1630 } else if (pls->cur_seq_no > max_seq_no) {
1631 av_log(pls->parent, AV_LOG_VERBOSE, "new fragment: min[%"PRId64"] max[%"PRId64"], playlist %d\n", min_seq_no, max_seq_no, (int)pls->rep_idx);
1633 seg = av_mallocz(sizeof(struct fragment));
1637 } else if (pls->cur_seq_no <= pls->last_seq_no) {
1638 seg = av_mallocz(sizeof(struct fragment));
1644 char *tmpfilename= av_mallocz(c->max_url_size);
1648 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));
1649 seg->url = av_strireplace(pls->url_template, pls->url_template, tmpfilename);
1651 av_log(pls->parent, AV_LOG_WARNING, "Unable to resolve template url '%s', try to use origin template\n", pls->url_template);
1652 seg->url = av_strdup(pls->url_template);
1654 av_log(pls->parent, AV_LOG_ERROR, "Cannot resolve template url '%s'\n", pls->url_template);
1655 av_free(tmpfilename);
1659 av_free(tmpfilename);
1666 static int read_from_url(struct representation *pls, struct fragment *seg,
1667 uint8_t *buf, int buf_size)
1671 /* limit read if the fragment was only a part of a file */
1673 buf_size = FFMIN(buf_size, pls->cur_seg_size - pls->cur_seg_offset);
1675 ret = avio_read(pls->input, buf, buf_size);
1677 pls->cur_seg_offset += ret;
1682 static int open_input(DASHContext *c, struct representation *pls, struct fragment *seg)
1684 AVDictionary *opts = NULL;
1688 url = av_mallocz(c->max_url_size);
1690 ret = AVERROR(ENOMEM);
1694 if (seg->size >= 0) {
1695 /* try to restrict the HTTP request to the part we want
1696 * (if this is in fact a HTTP request) */
1697 av_dict_set_int(&opts, "offset", seg->url_offset, 0);
1698 av_dict_set_int(&opts, "end_offset", seg->url_offset + seg->size, 0);
1701 ff_make_absolute_url(url, c->max_url_size, c->base_url, seg->url);
1702 av_log(pls->parent, AV_LOG_VERBOSE, "DASH request for url '%s', offset %"PRId64", playlist %d\n",
1703 url, seg->url_offset, pls->rep_idx);
1704 ret = open_url(pls->parent, &pls->input, url, c->avio_opts, opts, NULL);
1708 av_dict_free(&opts);
1709 pls->cur_seg_offset = 0;
1710 pls->cur_seg_size = seg->size;
1714 static int update_init_section(struct representation *pls)
1716 static const int max_init_section_size = 1024 * 1024;
1717 DASHContext *c = pls->parent->priv_data;
1722 if (!pls->init_section || pls->init_sec_buf)
1725 ret = open_input(c, pls, pls->init_section);
1727 av_log(pls->parent, AV_LOG_WARNING,
1728 "Failed to open an initialization section in playlist %d\n",
1733 if (pls->init_section->size >= 0)
1734 sec_size = pls->init_section->size;
1735 else if ((urlsize = avio_size(pls->input)) >= 0)
1738 sec_size = max_init_section_size;
1740 av_log(pls->parent, AV_LOG_DEBUG,
1741 "Downloading an initialization section of size %"PRId64"\n",
1744 sec_size = FFMIN(sec_size, max_init_section_size);
1746 av_fast_malloc(&pls->init_sec_buf, &pls->init_sec_buf_size, sec_size);
1748 ret = read_from_url(pls, pls->init_section, pls->init_sec_buf,
1749 pls->init_sec_buf_size);
1750 ff_format_io_close(pls->parent, &pls->input);
1755 pls->init_sec_data_len = ret;
1756 pls->init_sec_buf_read_offset = 0;
1761 static int64_t seek_data(void *opaque, int64_t offset, int whence)
1763 struct representation *v = opaque;
1764 if (v->n_fragments && !v->init_sec_data_len) {
1765 return avio_seek(v->input, offset, whence);
1768 return AVERROR(ENOSYS);
1771 static int read_data(void *opaque, uint8_t *buf, int buf_size)
1774 struct representation *v = opaque;
1775 DASHContext *c = v->parent->priv_data;
1779 free_fragment(&v->cur_seg);
1780 v->cur_seg = get_current_fragment(v);
1786 /* load/update Media Initialization Section, if any */
1787 ret = update_init_section(v);
1791 ret = open_input(c, v, v->cur_seg);
1793 if (ff_check_interrupt(c->interrupt_callback)) {
1797 av_log(v->parent, AV_LOG_WARNING, "Failed to open fragment of playlist %d\n", v->rep_idx);
1803 if (v->init_sec_buf_read_offset < v->init_sec_data_len) {
1804 /* Push init section out first before first actual fragment */
1805 int copy_size = FFMIN(v->init_sec_data_len - v->init_sec_buf_read_offset, buf_size);
1806 memcpy(buf, v->init_sec_buf, copy_size);
1807 v->init_sec_buf_read_offset += copy_size;
1812 /* check the v->cur_seg, if it is null, get current and double check if the new v->cur_seg*/
1814 v->cur_seg = get_current_fragment(v);
1820 ret = read_from_url(v, v->cur_seg, buf, buf_size);
1824 if (c->is_live || v->cur_seq_no < v->last_seq_no) {
1825 if (!v->is_restart_needed)
1827 v->is_restart_needed = 1;
1834 static int save_avio_options(AVFormatContext *s)
1836 DASHContext *c = s->priv_data;
1837 const char *opts[] = {
1838 "headers", "user_agent", "cookies", "http_proxy", "referer", "rw_timeout", NULL };
1839 const char **opt = opts;
1840 uint8_t *buf = NULL;
1844 if (av_opt_get(s->pb, *opt, AV_OPT_SEARCH_CHILDREN, &buf) >= 0) {
1845 if (buf[0] != '\0') {
1846 ret = av_dict_set(&c->avio_opts, *opt, buf, AV_DICT_DONT_STRDUP_VAL);
1861 static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
1862 int flags, AVDictionary **opts)
1864 av_log(s, AV_LOG_ERROR,
1865 "A DASH playlist item '%s' referred to an external file '%s'. "
1866 "Opening this file was forbidden for security reasons\n",
1868 return AVERROR(EPERM);
1871 static void close_demux_for_component(struct representation *pls)
1873 /* note: the internal buffer could have changed */
1874 av_freep(&pls->pb.buffer);
1875 memset(&pls->pb, 0x00, sizeof(AVIOContext));
1876 pls->ctx->pb = NULL;
1877 avformat_close_input(&pls->ctx);
1881 static int reopen_demux_for_component(AVFormatContext *s, struct representation *pls)
1883 DASHContext *c = s->priv_data;
1884 ff_const59 AVInputFormat *in_fmt = NULL;
1885 AVDictionary *in_fmt_opts = NULL;
1886 uint8_t *avio_ctx_buffer = NULL;
1890 close_demux_for_component(pls);
1893 if (ff_check_interrupt(&s->interrupt_callback)) {
1898 if (!(pls->ctx = avformat_alloc_context())) {
1899 ret = AVERROR(ENOMEM);
1903 avio_ctx_buffer = av_malloc(INITIAL_BUFFER_SIZE);
1904 if (!avio_ctx_buffer ) {
1905 ret = AVERROR(ENOMEM);
1906 avformat_free_context(pls->ctx);
1911 ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, NULL);
1913 ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, seek_data);
1915 pls->pb.seekable = 0;
1917 if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0)
1920 pls->ctx->flags = AVFMT_FLAG_CUSTOM_IO;
1921 pls->ctx->probesize = 1024 * 4;
1922 pls->ctx->max_analyze_duration = 4 * AV_TIME_BASE;
1923 ret = av_probe_input_buffer(&pls->pb, &in_fmt, "", NULL, 0, 0);
1925 av_log(s, AV_LOG_ERROR, "Error when loading first fragment, playlist %d\n", (int)pls->rep_idx);
1926 avformat_free_context(pls->ctx);
1931 pls->ctx->pb = &pls->pb;
1932 pls->ctx->io_open = nested_io_open;
1934 // provide additional information from mpd if available
1935 ret = avformat_open_input(&pls->ctx, "", in_fmt, &in_fmt_opts); //pls->init_section->url
1936 av_dict_free(&in_fmt_opts);
1939 if (pls->n_fragments) {
1940 #if FF_API_R_FRAME_RATE
1941 if (pls->framerate.den) {
1942 for (i = 0; i < pls->ctx->nb_streams; i++)
1943 pls->ctx->streams[i]->r_frame_rate = pls->framerate;
1947 ret = avformat_find_stream_info(pls->ctx, NULL);
1956 static int open_demux_for_component(AVFormatContext *s, struct representation *pls)
1962 pls->cur_seq_no = calc_cur_seg_no(s, pls);
1964 if (!pls->last_seq_no) {
1965 pls->last_seq_no = calc_max_seg_no(pls, s->priv_data);
1968 ret = reopen_demux_for_component(s, pls);
1972 for (i = 0; i < pls->ctx->nb_streams; i++) {
1973 AVStream *st = avformat_new_stream(s, NULL);
1974 AVStream *ist = pls->ctx->streams[i];
1976 ret = AVERROR(ENOMEM);
1980 avcodec_parameters_copy(st->codecpar, pls->ctx->streams[i]->codecpar);
1981 avpriv_set_pts_info(st, ist->pts_wrap_bits, ist->time_base.num, ist->time_base.den);
1989 static int is_common_init_section_exist(struct representation **pls, int n_pls)
1991 struct fragment *first_init_section = pls[0]->init_section;
1993 int64_t url_offset = -1;
1997 if (first_init_section == NULL || n_pls == 0)
2000 url = first_init_section->url;
2001 url_offset = first_init_section->url_offset;
2002 size = pls[0]->init_section->size;
2003 for (i=0;i<n_pls;i++) {
2004 if (av_strcasecmp(pls[i]->init_section->url,url) || pls[i]->init_section->url_offset != url_offset || pls[i]->init_section->size != size) {
2011 static void copy_init_section(struct representation *rep_dest, struct representation *rep_src)
2013 rep_dest->init_sec_buf = av_mallocz(rep_src->init_sec_buf_size);
2014 memcpy(rep_dest->init_sec_buf, rep_src->init_sec_buf, rep_src->init_sec_data_len);
2015 rep_dest->init_sec_buf_size = rep_src->init_sec_buf_size;
2016 rep_dest->init_sec_data_len = rep_src->init_sec_data_len;
2017 rep_dest->cur_timestamp = rep_src->cur_timestamp;
2021 static int dash_read_header(AVFormatContext *s)
2023 DASHContext *c = s->priv_data;
2025 int stream_index = 0;
2028 c->interrupt_callback = &s->interrupt_callback;
2030 if ((ret = save_avio_options(s)) < 0)
2033 if ((ret = parse_manifest(s, s->url, s->pb)) < 0)
2036 /* If this isn't a live stream, fill the total duration of the
2039 s->duration = (int64_t) c->media_presentation_duration * AV_TIME_BASE;
2041 av_dict_set(&c->avio_opts, "seekable", "0", 0);
2045 c->is_init_section_common_video = is_common_init_section_exist(c->videos, c->n_videos);
2047 /* Open the demuxer for video and audio components if available */
2048 for (i = 0; i < c->n_videos; i++) {
2049 struct representation *cur_video = c->videos[i];
2050 if (i > 0 && c->is_init_section_common_video) {
2051 copy_init_section(cur_video,c->videos[0]);
2053 ret = open_demux_for_component(s, cur_video);
2057 cur_video->stream_index = stream_index;
2062 c->is_init_section_common_audio = is_common_init_section_exist(c->audios, c->n_audios);
2064 for (i = 0; i < c->n_audios; i++) {
2065 struct representation *cur_audio = c->audios[i];
2066 if (i > 0 && c->is_init_section_common_audio) {
2067 copy_init_section(cur_audio,c->audios[0]);
2069 ret = open_demux_for_component(s, cur_audio);
2073 cur_audio->stream_index = stream_index;
2078 c->is_init_section_common_audio = is_common_init_section_exist(c->subtitles, c->n_subtitles);
2080 for (i = 0; i < c->n_subtitles; i++) {
2081 struct representation *cur_subtitle = c->subtitles[i];
2082 if (i > 0 && c->is_init_section_common_audio) {
2083 copy_init_section(cur_subtitle,c->subtitles[0]);
2085 ret = open_demux_for_component(s, cur_subtitle);
2089 cur_subtitle->stream_index = stream_index;
2094 if (!stream_index) {
2095 ret = AVERROR_INVALIDDATA;
2099 /* Create a program */
2102 program = av_new_program(s, 0);
2107 for (i = 0; i < c->n_videos; i++) {
2108 struct representation *pls = c->videos[i];
2110 av_program_add_stream_index(s, 0, pls->stream_index);
2111 pls->assoc_stream = s->streams[pls->stream_index];
2112 if (pls->bandwidth > 0)
2113 av_dict_set_int(&pls->assoc_stream->metadata, "variant_bitrate", pls->bandwidth, 0);
2115 av_dict_set(&pls->assoc_stream->metadata, "id", pls->id, 0);
2117 for (i = 0; i < c->n_audios; i++) {
2118 struct representation *pls = c->audios[i];
2120 av_program_add_stream_index(s, 0, pls->stream_index);
2121 pls->assoc_stream = s->streams[pls->stream_index];
2122 if (pls->bandwidth > 0)
2123 av_dict_set_int(&pls->assoc_stream->metadata, "variant_bitrate", pls->bandwidth, 0);
2125 av_dict_set(&pls->assoc_stream->metadata, "id", pls->id, 0);
2127 for (i = 0; i < c->n_subtitles; i++) {
2128 struct representation *pls = c->subtitles[i];
2129 av_program_add_stream_index(s, 0, pls->stream_index);
2130 pls->assoc_stream = s->streams[pls->stream_index];
2132 av_dict_set(&pls->assoc_stream->metadata, "id", pls->id, 0);
2142 static void recheck_discard_flags(AVFormatContext *s, struct representation **p, int n)
2146 for (i = 0; i < n; i++) {
2147 struct representation *pls = p[i];
2149 int needed = !pls->assoc_stream || pls->assoc_stream->discard < AVDISCARD_ALL;
2150 if (needed && !pls->ctx) {
2151 pls->cur_seg_offset = 0;
2152 pls->init_sec_buf_read_offset = 0;
2154 for (j = 0; j < n; j++) {
2155 pls->cur_seq_no = FFMAX(pls->cur_seq_no, p[j]->cur_seq_no);
2157 reopen_demux_for_component(s, pls);
2158 av_log(s, AV_LOG_INFO, "Now receiving stream_index %d\n", pls->stream_index);
2159 } else if (!needed && pls->ctx) {
2160 close_demux_for_component(pls);
2162 ff_format_io_close(pls->parent, &pls->input);
2163 av_log(s, AV_LOG_INFO, "No longer receiving stream_index %d\n", pls->stream_index);
2168 static int dash_read_packet(AVFormatContext *s, AVPacket *pkt)
2170 DASHContext *c = s->priv_data;
2173 struct representation *cur = NULL;
2175 recheck_discard_flags(s, c->videos, c->n_videos);
2176 recheck_discard_flags(s, c->audios, c->n_audios);
2177 recheck_discard_flags(s, c->subtitles, c->n_subtitles);
2179 for (i = 0; i < c->n_videos; i++) {
2180 struct representation *pls = c->videos[i];
2183 if (!cur || pls->cur_timestamp < mints) {
2185 mints = pls->cur_timestamp;
2188 for (i = 0; i < c->n_audios; i++) {
2189 struct representation *pls = c->audios[i];
2192 if (!cur || pls->cur_timestamp < mints) {
2194 mints = pls->cur_timestamp;
2198 for (i = 0; i < c->n_subtitles; i++) {
2199 struct representation *pls = c->subtitles[i];
2202 if (!cur || pls->cur_timestamp < mints) {
2204 mints = pls->cur_timestamp;
2209 return AVERROR_INVALIDDATA;
2211 while (!ff_check_interrupt(c->interrupt_callback) && !ret) {
2212 ret = av_read_frame(cur->ctx, pkt);
2214 /* If we got a packet, return it */
2215 cur->cur_timestamp = av_rescale(pkt->pts, (int64_t)cur->ctx->streams[0]->time_base.num * 90000, cur->ctx->streams[0]->time_base.den);
2216 pkt->stream_index = cur->stream_index;
2219 if (cur->is_restart_needed) {
2220 cur->cur_seg_offset = 0;
2221 cur->init_sec_buf_read_offset = 0;
2223 ff_format_io_close(cur->parent, &cur->input);
2224 ret = reopen_demux_for_component(s, cur);
2225 cur->is_restart_needed = 0;
2231 static int dash_close(AVFormatContext *s)
2233 DASHContext *c = s->priv_data;
2237 av_dict_free(&c->avio_opts);
2238 av_freep(&c->base_url);
2242 static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos_msec, int flags, int dry_run)
2247 int64_t duration = 0;
2249 av_log(pls->parent, AV_LOG_VERBOSE, "DASH seek pos[%"PRId64"ms], playlist %d%s\n",
2250 seek_pos_msec, pls->rep_idx, dry_run ? " (dry)" : "");
2252 // single fragment mode
2253 if (pls->n_fragments == 1) {
2254 pls->cur_timestamp = 0;
2255 pls->cur_seg_offset = 0;
2258 ff_read_frame_flush(pls->ctx);
2259 return av_seek_frame(pls->ctx, -1, seek_pos_msec * 1000, flags);
2263 ff_format_io_close(pls->parent, &pls->input);
2265 // find the nearest fragment
2266 if (pls->n_timelines > 0 && pls->fragment_timescale > 0) {
2267 int64_t num = pls->first_seq_no;
2268 av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline start n_timelines[%d] "
2269 "last_seq_no[%"PRId64"], playlist %d.\n",
2270 (int)pls->n_timelines, (int64_t)pls->last_seq_no, (int)pls->rep_idx);
2271 for (i = 0; i < pls->n_timelines; i++) {
2272 if (pls->timelines[i]->starttime > 0) {
2273 duration = pls->timelines[i]->starttime;
2275 duration += pls->timelines[i]->duration;
2276 if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
2279 for (j = 0; j < pls->timelines[i]->repeat; j++) {
2280 duration += pls->timelines[i]->duration;
2282 if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
2290 pls->cur_seq_no = num > pls->last_seq_no ? pls->last_seq_no : num;
2291 av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline end cur_seq_no[%"PRId64"], playlist %d.\n",
2292 (int64_t)pls->cur_seq_no, (int)pls->rep_idx);
2293 } else if (pls->fragment_duration > 0) {
2294 pls->cur_seq_no = pls->first_seq_no + ((seek_pos_msec * pls->fragment_timescale) / pls->fragment_duration) / 1000;
2296 av_log(pls->parent, AV_LOG_ERROR, "dash_seek missing timeline or fragment_duration\n");
2297 pls->cur_seq_no = pls->first_seq_no;
2299 pls->cur_timestamp = 0;
2300 pls->cur_seg_offset = 0;
2301 pls->init_sec_buf_read_offset = 0;
2302 ret = dry_run ? 0 : reopen_demux_for_component(s, pls);
2307 static int dash_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
2310 DASHContext *c = s->priv_data;
2311 int64_t seek_pos_msec = av_rescale_rnd(timestamp, 1000,
2312 s->streams[stream_index]->time_base.den,
2313 flags & AVSEEK_FLAG_BACKWARD ?
2314 AV_ROUND_DOWN : AV_ROUND_UP);
2315 if ((flags & AVSEEK_FLAG_BYTE) || c->is_live)
2316 return AVERROR(ENOSYS);
2318 /* Seek in discarded streams with dry_run=1 to avoid reopening them */
2319 for (i = 0; i < c->n_videos; i++) {
2321 ret = dash_seek(s, c->videos[i], seek_pos_msec, flags, !c->videos[i]->ctx);
2323 for (i = 0; i < c->n_audios; i++) {
2325 ret = dash_seek(s, c->audios[i], seek_pos_msec, flags, !c->audios[i]->ctx);
2327 for (i = 0; i < c->n_subtitles; i++) {
2329 ret = dash_seek(s, c->subtitles[i], seek_pos_msec, flags, !c->subtitles[i]->ctx);
2335 static int dash_probe(const AVProbeData *p)
2337 if (!av_stristr(p->buf, "<MPD"))
2340 if (av_stristr(p->buf, "dash:profile:isoff-on-demand:2011") ||
2341 av_stristr(p->buf, "dash:profile:isoff-live:2011") ||
2342 av_stristr(p->buf, "dash:profile:isoff-live:2012") ||
2343 av_stristr(p->buf, "dash:profile:isoff-main:2011")) {
2344 return AVPROBE_SCORE_MAX;
2346 if (av_stristr(p->buf, "dash:profile")) {
2347 return AVPROBE_SCORE_MAX;
2353 #define OFFSET(x) offsetof(DASHContext, x)
2354 #define FLAGS AV_OPT_FLAG_DECODING_PARAM
2355 static const AVOption dash_options[] = {
2356 {"allowed_extensions", "List of file extensions that dash is allowed to access",
2357 OFFSET(allowed_extensions), AV_OPT_TYPE_STRING,
2358 {.str = "aac,m4a,m4s,m4v,mov,mp4,webm"},
2359 INT_MIN, INT_MAX, FLAGS},
2363 static const AVClass dash_class = {
2364 .class_name = "dash",
2365 .item_name = av_default_item_name,
2366 .option = dash_options,
2367 .version = LIBAVUTIL_VERSION_INT,
2370 AVInputFormat ff_dash_demuxer = {
2372 .long_name = NULL_IF_CONFIG_SMALL("Dynamic Adaptive Streaming over HTTP"),
2373 .priv_class = &dash_class,
2374 .priv_data_size = sizeof(DASHContext),
2375 .read_probe = dash_probe,
2376 .read_header = dash_read_header,
2377 .read_packet = dash_read_packet,
2378 .read_close = dash_close,
2379 .read_seek = dash_read_seek,
2380 .flags = AVFMT_NO_BYTE_SEEK,