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);
362 av_freep(&pls->lang);
366 static void free_video_list(DASHContext *c)
369 for (i = 0; i < c->n_videos; i++) {
370 struct representation *pls = c->videos[i];
371 free_representation(pls);
373 av_freep(&c->videos);
377 static void free_audio_list(DASHContext *c)
380 for (i = 0; i < c->n_audios; i++) {
381 struct representation *pls = c->audios[i];
382 free_representation(pls);
384 av_freep(&c->audios);
388 static void free_subtitle_list(DASHContext *c)
391 for (i = 0; i < c->n_subtitles; i++) {
392 struct representation *pls = c->subtitles[i];
393 free_representation(pls);
395 av_freep(&c->subtitles);
399 static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
400 AVDictionary **opts, AVDictionary *opts2, int *is_http)
402 DASHContext *c = s->priv_data;
403 AVDictionary *tmp = NULL;
404 const char *proto_name = NULL;
407 if (av_strstart(url, "crypto", NULL)) {
408 if (url[6] == '+' || url[6] == ':')
409 proto_name = avio_find_protocol_name(url + 7);
413 proto_name = avio_find_protocol_name(url);
416 return AVERROR_INVALIDDATA;
418 // only http(s) & file are allowed
419 if (av_strstart(proto_name, "file", NULL)) {
420 if (strcmp(c->allowed_extensions, "ALL") && !av_match_ext(url, c->allowed_extensions)) {
421 av_log(s, AV_LOG_ERROR,
422 "Filename extension of \'%s\' is not a common multimedia extension, blocked for security reasons.\n"
423 "If you wish to override this adjust allowed_extensions, you can set it to \'ALL\' to allow all\n",
425 return AVERROR_INVALIDDATA;
427 } else if (av_strstart(proto_name, "http", NULL)) {
430 return AVERROR_INVALIDDATA;
432 if (!strncmp(proto_name, url, strlen(proto_name)) && url[strlen(proto_name)] == ':')
434 else if (av_strstart(url, "crypto", NULL) && !strncmp(proto_name, url + 7, strlen(proto_name)) && url[7 + strlen(proto_name)] == ':')
436 else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5))
437 return AVERROR_INVALIDDATA;
440 av_dict_copy(&tmp, *opts, 0);
441 av_dict_copy(&tmp, opts2, 0);
442 ret = avio_open2(pb, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp);
444 // update cookies on http response with setcookies.
445 char *new_cookies = NULL;
447 if (!(s->flags & AVFMT_FLAG_CUSTOM_IO))
448 av_opt_get(*pb, "cookies", AV_OPT_SEARCH_CHILDREN, (uint8_t**)&new_cookies);
451 av_dict_set(opts, "cookies", new_cookies, AV_DICT_DONT_STRDUP_VAL);
459 *is_http = av_strstart(proto_name, "http", NULL);
464 static char *get_content_url(xmlNodePtr *baseurl_nodes,
468 char *rep_bandwidth_val,
474 char *tmp_str = av_mallocz(max_url_size);
475 char *tmp_str_2 = av_mallocz(max_url_size);
477 if (!tmp_str || !tmp_str_2) {
481 for (i = 0; i < n_baseurl_nodes; ++i) {
482 if (baseurl_nodes[i] &&
483 baseurl_nodes[i]->children &&
484 baseurl_nodes[i]->children->type == XML_TEXT_NODE) {
485 text = xmlNodeGetContent(baseurl_nodes[i]->children);
487 memset(tmp_str, 0, max_url_size);
488 memset(tmp_str_2, 0, max_url_size);
489 ff_make_absolute_url(tmp_str_2, max_url_size, tmp_str, text);
490 av_strlcpy(tmp_str, tmp_str_2, max_url_size);
497 ff_make_absolute_url(tmp_str, max_url_size, tmp_str, val);
500 url = av_strireplace(tmp_str, "$RepresentationID$", (const char*)rep_id_val);
504 av_strlcpy(tmp_str, url, max_url_size);
506 if (rep_bandwidth_val && tmp_str[0] != '\0') {
507 // free any previously assigned url before reassigning
509 url = av_strireplace(tmp_str, "$Bandwidth$", (const char*)rep_bandwidth_val);
520 static char *get_val_from_nodes_tab(xmlNodePtr *nodes, const int n_nodes, const char *attrname)
525 for (i = 0; i < n_nodes; ++i) {
527 val = xmlGetProp(nodes[i], attrname);
536 static xmlNodePtr find_child_node_by_name(xmlNodePtr rootnode, const char *nodename)
538 xmlNodePtr node = rootnode;
543 node = xmlFirstElementChild(node);
545 if (!av_strcasecmp(node->name, nodename)) {
548 node = xmlNextElementSibling(node);
553 static enum AVMediaType get_content_type(xmlNodePtr node)
555 enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
561 for (i = 0; i < 2; i++) {
562 attr = i ? "mimeType" : "contentType";
563 val = xmlGetProp(node, attr);
565 if (av_stristr((const char *)val, "video")) {
566 type = AVMEDIA_TYPE_VIDEO;
567 } else if (av_stristr((const char *)val, "audio")) {
568 type = AVMEDIA_TYPE_AUDIO;
569 } else if (av_stristr((const char *)val, "text")) {
570 type = AVMEDIA_TYPE_SUBTITLE;
579 static struct fragment * get_Fragment(char *range)
581 struct fragment * seg = av_mallocz(sizeof(struct fragment));
588 char *str_end_offset;
589 char *str_offset = av_strtok(range, "-", &str_end_offset);
590 seg->url_offset = strtoll(str_offset, NULL, 10);
591 seg->size = strtoll(str_end_offset, NULL, 10) - seg->url_offset + 1;
597 static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representation *rep,
598 xmlNodePtr fragmenturl_node,
599 xmlNodePtr *baseurl_nodes,
601 char *rep_bandwidth_val)
603 DASHContext *c = s->priv_data;
604 char *initialization_val = NULL;
605 char *media_val = NULL;
606 char *range_val = NULL;
607 int max_url_size = c ? c->max_url_size: MAX_URL_SIZE;
610 if (!av_strcasecmp(fragmenturl_node->name, (const char *)"Initialization")) {
611 initialization_val = xmlGetProp(fragmenturl_node, "sourceURL");
612 range_val = xmlGetProp(fragmenturl_node, "range");
613 if (initialization_val || range_val) {
614 free_fragment(&rep->init_section);
615 rep->init_section = get_Fragment(range_val);
617 if (!rep->init_section) {
618 xmlFree(initialization_val);
619 return AVERROR(ENOMEM);
621 rep->init_section->url = get_content_url(baseurl_nodes, 4,
626 xmlFree(initialization_val);
627 if (!rep->init_section->url) {
628 av_freep(&rep->init_section);
629 return AVERROR(ENOMEM);
632 } else if (!av_strcasecmp(fragmenturl_node->name, (const char *)"SegmentURL")) {
633 media_val = xmlGetProp(fragmenturl_node, "media");
634 range_val = xmlGetProp(fragmenturl_node, "mediaRange");
635 if (media_val || range_val) {
636 struct fragment *seg = get_Fragment(range_val);
640 return AVERROR(ENOMEM);
642 seg->url = get_content_url(baseurl_nodes, 4,
650 return AVERROR(ENOMEM);
652 err = av_dynarray_add_nofree(&rep->fragments, &rep->n_fragments, seg);
663 static int parse_manifest_segmenttimeline(AVFormatContext *s, struct representation *rep,
664 xmlNodePtr fragment_timeline_node)
666 xmlAttrPtr attr = NULL;
670 if (!av_strcasecmp(fragment_timeline_node->name, (const char *)"S")) {
671 struct timeline *tml = av_mallocz(sizeof(struct timeline));
673 return AVERROR(ENOMEM);
675 attr = fragment_timeline_node->properties;
677 val = xmlGetProp(fragment_timeline_node, attr->name);
680 av_log(s, AV_LOG_WARNING, "parse_manifest_segmenttimeline attr->name = %s val is NULL\n", attr->name);
684 if (!av_strcasecmp(attr->name, (const char *)"t")) {
685 tml->starttime = (int64_t)strtoll(val, NULL, 10);
686 } else if (!av_strcasecmp(attr->name, (const char *)"r")) {
687 tml->repeat =(int64_t) strtoll(val, NULL, 10);
688 } else if (!av_strcasecmp(attr->name, (const char *)"d")) {
689 tml->duration = (int64_t)strtoll(val, NULL, 10);
694 err = av_dynarray_add_nofree(&rep->timelines, &rep->n_timelines, tml);
704 static int resolve_content_path(AVFormatContext *s, const char *url, int *max_url_size, xmlNodePtr *baseurl_nodes, int n_baseurl_nodes)
706 char *tmp_str = NULL;
708 char *mpdName = NULL;
709 xmlNodePtr node = NULL;
710 char *baseurl = NULL;
711 char *root_url = NULL;
721 int tmp_max_url_size = strlen(url);
723 for (i = n_baseurl_nodes-1; i >= 0 ; i--) {
724 text = xmlNodeGetContent(baseurl_nodes[i]);
727 tmp_max_url_size += strlen(text);
735 tmp_max_url_size = aligned(tmp_max_url_size);
736 text = av_mallocz(tmp_max_url_size);
738 updated = AVERROR(ENOMEM);
741 av_strlcpy(text, url, strlen(url)+1);
743 while (mpdName = av_strtok(tmp, "/", &tmp)) {
744 size = strlen(mpdName);
748 path = av_mallocz(tmp_max_url_size);
749 tmp_str = av_mallocz(tmp_max_url_size);
750 if (!tmp_str || !path) {
751 updated = AVERROR(ENOMEM);
755 av_strlcpy (path, url, strlen(url) - size + 1);
756 for (rootId = n_baseurl_nodes - 1; rootId > 0; rootId --) {
757 if (!(node = baseurl_nodes[rootId])) {
760 text = xmlNodeGetContent(node);
768 node = baseurl_nodes[rootId];
769 baseurl = xmlNodeGetContent(node);
770 root_url = (av_strcasecmp(baseurl, "")) ? baseurl : path;
772 xmlNodeSetContent(node, root_url);
776 size = strlen(root_url);
777 isRootHttp = ishttp(root_url);
779 if (root_url[size - 1] != token) {
780 av_strlcat(root_url, "/", size + 2);
784 for (i = 0; i < n_baseurl_nodes; ++i) {
788 text = xmlNodeGetContent(baseurl_nodes[i]);
789 if (text && !av_strstart(text, "/", NULL)) {
790 memset(tmp_str, 0, strlen(tmp_str));
791 if (!ishttp(text) && isRootHttp) {
792 av_strlcpy(tmp_str, root_url, size + 1);
794 start = (text[0] == token);
795 if (start && av_stristr(tmp_str, text)) {
797 if (!av_strncasecmp(tmp_str, "http://", 7)) {
799 } else if (!av_strncasecmp(tmp_str, "https://", 8)) {
803 memset(p + 1, 0, strlen(p));
805 av_strlcat(tmp_str, text + start, tmp_max_url_size);
806 xmlNodeSetContent(baseurl_nodes[i], tmp_str);
813 if (tmp_max_url_size > *max_url_size) {
814 *max_url_size = tmp_max_url_size;
823 static int parse_manifest_representation(AVFormatContext *s, const char *url,
825 xmlNodePtr adaptionset_node,
826 xmlNodePtr mpd_baseurl_node,
827 xmlNodePtr period_baseurl_node,
828 xmlNodePtr period_segmenttemplate_node,
829 xmlNodePtr period_segmentlist_node,
830 xmlNodePtr fragment_template_node,
831 xmlNodePtr content_component_node,
832 xmlNodePtr adaptionset_baseurl_node,
833 xmlNodePtr adaptionset_segmentlist_node,
834 xmlNodePtr adaptionset_supplementalproperty_node)
837 DASHContext *c = s->priv_data;
838 struct representation *rep = NULL;
839 struct fragment *seg = NULL;
840 xmlNodePtr representation_segmenttemplate_node = NULL;
841 xmlNodePtr representation_baseurl_node = NULL;
842 xmlNodePtr representation_segmentlist_node = NULL;
843 xmlNodePtr segmentlists_tab[3];
844 xmlNodePtr fragment_timeline_node = NULL;
845 xmlNodePtr fragment_templates_tab[5];
846 char *duration_val = NULL;
847 char *presentation_timeoffset_val = NULL;
848 char *startnumber_val = NULL;
849 char *timescale_val = NULL;
850 char *initialization_val = NULL;
851 char *media_val = NULL;
853 xmlNodePtr baseurl_nodes[4];
854 xmlNodePtr representation_node = node;
855 char *rep_id_val, *rep_bandwidth_val;
856 enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
858 // try get information from representation
859 if (type == AVMEDIA_TYPE_UNKNOWN)
860 type = get_content_type(representation_node);
861 // try get information from contentComponen
862 if (type == AVMEDIA_TYPE_UNKNOWN)
863 type = get_content_type(content_component_node);
864 // try get information from adaption set
865 if (type == AVMEDIA_TYPE_UNKNOWN)
866 type = get_content_type(adaptionset_node);
867 if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO &&
868 type != AVMEDIA_TYPE_SUBTITLE) {
869 av_log(s, AV_LOG_VERBOSE, "Parsing '%s' - skipp not supported representation type\n", url);
872 // convert selected representation to our internal struct
873 rep = av_mallocz(sizeof(struct representation));
875 return AVERROR(ENOMEM);
876 if (c->adaptionset_lang) {
877 rep->lang = av_strdup(c->adaptionset_lang);
879 av_log(s, AV_LOG_ERROR, "alloc language memory failure\n");
881 return AVERROR(ENOMEM);
885 representation_segmenttemplate_node = find_child_node_by_name(representation_node, "SegmentTemplate");
886 representation_baseurl_node = find_child_node_by_name(representation_node, "BaseURL");
887 representation_segmentlist_node = find_child_node_by_name(representation_node, "SegmentList");
888 rep_id_val = xmlGetProp(representation_node, "id");
889 rep_bandwidth_val = xmlGetProp(representation_node, "bandwidth");
891 baseurl_nodes[0] = mpd_baseurl_node;
892 baseurl_nodes[1] = period_baseurl_node;
893 baseurl_nodes[2] = adaptionset_baseurl_node;
894 baseurl_nodes[3] = representation_baseurl_node;
896 ret = resolve_content_path(s, url, &c->max_url_size, baseurl_nodes, 4);
897 c->max_url_size = aligned(c->max_url_size
898 + (rep_id_val ? strlen(rep_id_val) : 0)
899 + (rep_bandwidth_val ? strlen(rep_bandwidth_val) : 0));
900 if (ret == AVERROR(ENOMEM) || ret == 0)
902 if (representation_segmenttemplate_node || fragment_template_node || period_segmenttemplate_node) {
903 fragment_timeline_node = NULL;
904 fragment_templates_tab[0] = representation_segmenttemplate_node;
905 fragment_templates_tab[1] = adaptionset_segmentlist_node;
906 fragment_templates_tab[2] = fragment_template_node;
907 fragment_templates_tab[3] = period_segmenttemplate_node;
908 fragment_templates_tab[4] = period_segmentlist_node;
910 initialization_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "initialization");
911 if (initialization_val) {
912 rep->init_section = av_mallocz(sizeof(struct fragment));
913 if (!rep->init_section) {
914 xmlFree(initialization_val);
917 c->max_url_size = aligned(c->max_url_size + strlen(initialization_val));
918 rep->init_section->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, initialization_val);
919 xmlFree(initialization_val);
920 if (!rep->init_section->url)
922 rep->init_section->size = -1;
924 media_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "media");
926 c->max_url_size = aligned(c->max_url_size + strlen(media_val));
927 rep->url_template = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, media_val);
930 presentation_timeoffset_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "presentationTimeOffset");
931 if (presentation_timeoffset_val) {
932 rep->presentation_timeoffset = (int64_t) strtoll(presentation_timeoffset_val, NULL, 10);
933 av_log(s, AV_LOG_TRACE, "rep->presentation_timeoffset = [%"PRId64"]\n", rep->presentation_timeoffset);
934 xmlFree(presentation_timeoffset_val);
936 duration_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "duration");
938 rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
939 av_log(s, AV_LOG_TRACE, "rep->fragment_duration = [%"PRId64"]\n", rep->fragment_duration);
940 xmlFree(duration_val);
942 timescale_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "timescale");
944 rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
945 av_log(s, AV_LOG_TRACE, "rep->fragment_timescale = [%"PRId64"]\n", rep->fragment_timescale);
946 xmlFree(timescale_val);
948 startnumber_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "startNumber");
949 if (startnumber_val) {
950 rep->start_number = rep->first_seq_no = (int64_t) strtoll(startnumber_val, NULL, 10);
951 av_log(s, AV_LOG_TRACE, "rep->first_seq_no = [%"PRId64"]\n", rep->first_seq_no);
952 xmlFree(startnumber_val);
954 if (adaptionset_supplementalproperty_node) {
955 if (!av_strcasecmp(xmlGetProp(adaptionset_supplementalproperty_node,"schemeIdUri"), "http://dashif.org/guidelines/last-segment-number")) {
956 val = xmlGetProp(adaptionset_supplementalproperty_node,"value");
958 av_log(s, AV_LOG_ERROR, "Missing value attribute in adaptionset_supplementalproperty_node\n");
960 rep->last_seq_no =(int64_t) strtoll(val, NULL, 10) - 1;
966 fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
968 if (!fragment_timeline_node)
969 fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
970 if (!fragment_timeline_node)
971 fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
972 if (!fragment_timeline_node)
973 fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
974 if (fragment_timeline_node) {
975 fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
976 while (fragment_timeline_node) {
977 ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
980 fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
983 } else if (representation_baseurl_node && !representation_segmentlist_node) {
984 seg = av_mallocz(sizeof(struct fragment));
987 ret = av_dynarray_add_nofree(&rep->fragments, &rep->n_fragments, seg);
992 seg->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, NULL);
996 } else if (representation_segmentlist_node) {
997 // TODO: https://www.brendanlong.com/the-structure-of-an-mpeg-dash-mpd.html
998 // http://www-itec.uni-klu.ac.at/dash/ddash/mpdGenerator.php?fragmentlength=15&type=full
999 xmlNodePtr fragmenturl_node = NULL;
1000 segmentlists_tab[0] = representation_segmentlist_node;
1001 segmentlists_tab[1] = adaptionset_segmentlist_node;
1002 segmentlists_tab[2] = period_segmentlist_node;
1004 duration_val = get_val_from_nodes_tab(segmentlists_tab, 3, "duration");
1005 timescale_val = get_val_from_nodes_tab(segmentlists_tab, 3, "timescale");
1006 startnumber_val = get_val_from_nodes_tab(segmentlists_tab, 3, "startNumber");
1008 rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
1009 av_log(s, AV_LOG_TRACE, "rep->fragment_duration = [%"PRId64"]\n", rep->fragment_duration);
1010 xmlFree(duration_val);
1012 if (timescale_val) {
1013 rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
1014 av_log(s, AV_LOG_TRACE, "rep->fragment_timescale = [%"PRId64"]\n", rep->fragment_timescale);
1015 xmlFree(timescale_val);
1017 if (startnumber_val) {
1018 rep->start_number = rep->first_seq_no = (int64_t) strtoll(startnumber_val, NULL, 10);
1019 av_log(s, AV_LOG_TRACE, "rep->first_seq_no = [%"PRId64"]\n", rep->first_seq_no);
1020 xmlFree(startnumber_val);
1023 fragmenturl_node = xmlFirstElementChild(representation_segmentlist_node);
1024 while (fragmenturl_node) {
1025 ret = parse_manifest_segmenturlnode(s, rep, fragmenturl_node,
1031 fragmenturl_node = xmlNextElementSibling(fragmenturl_node);
1034 fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
1035 if (!fragment_timeline_node)
1036 fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
1037 if (fragment_timeline_node) {
1038 fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
1039 while (fragment_timeline_node) {
1040 ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
1043 fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
1047 av_log(s, AV_LOG_ERROR, "Unknown format of Representation node id[%s] \n", (const char *)rep_id_val);
1051 if (rep->fragment_duration > 0 && !rep->fragment_timescale)
1052 rep->fragment_timescale = 1;
1053 rep->bandwidth = rep_bandwidth_val ? atoi(rep_bandwidth_val) : 0;
1054 strncpy(rep->id, rep_id_val ? rep_id_val : "", sizeof(rep->id));
1055 rep->framerate = av_make_q(0, 0);
1056 if (type == AVMEDIA_TYPE_VIDEO) {
1057 char *rep_framerate_val = xmlGetProp(representation_node, "frameRate");
1058 if (rep_framerate_val) {
1059 ret = av_parse_video_rate(&rep->framerate, rep_framerate_val);
1061 av_log(s, AV_LOG_VERBOSE, "Ignoring invalid frame rate '%s'\n", rep_framerate_val);
1062 xmlFree(rep_framerate_val);
1067 case AVMEDIA_TYPE_VIDEO:
1068 ret = av_dynarray_add_nofree(&c->videos, &c->n_videos, rep);
1070 case AVMEDIA_TYPE_AUDIO:
1071 ret = av_dynarray_add_nofree(&c->audios, &c->n_audios, rep);
1073 case AVMEDIA_TYPE_SUBTITLE:
1074 ret = av_dynarray_add_nofree(&c->subtitles, &c->n_subtitles, rep);
1082 xmlFree(rep_id_val);
1083 if (rep_bandwidth_val)
1084 xmlFree(rep_bandwidth_val);
1088 ret = AVERROR(ENOMEM);
1090 free_representation(rep);
1094 static int parse_manifest_adaptationset_attr(AVFormatContext *s, xmlNodePtr adaptionset_node)
1096 DASHContext *c = s->priv_data;
1098 if (!adaptionset_node) {
1099 av_log(s, AV_LOG_WARNING, "Cannot get AdaptionSet\n");
1100 return AVERROR(EINVAL);
1102 c->adaptionset_lang = xmlGetProp(adaptionset_node, "lang");
1107 static int parse_manifest_adaptationset(AVFormatContext *s, const char *url,
1108 xmlNodePtr adaptionset_node,
1109 xmlNodePtr mpd_baseurl_node,
1110 xmlNodePtr period_baseurl_node,
1111 xmlNodePtr period_segmenttemplate_node,
1112 xmlNodePtr period_segmentlist_node)
1115 DASHContext *c = s->priv_data;
1116 xmlNodePtr fragment_template_node = NULL;
1117 xmlNodePtr content_component_node = NULL;
1118 xmlNodePtr adaptionset_baseurl_node = NULL;
1119 xmlNodePtr adaptionset_segmentlist_node = NULL;
1120 xmlNodePtr adaptionset_supplementalproperty_node = NULL;
1121 xmlNodePtr node = NULL;
1123 ret = parse_manifest_adaptationset_attr(s, adaptionset_node);
1127 node = xmlFirstElementChild(adaptionset_node);
1129 if (!av_strcasecmp(node->name, (const char *)"SegmentTemplate")) {
1130 fragment_template_node = node;
1131 } else if (!av_strcasecmp(node->name, (const char *)"ContentComponent")) {
1132 content_component_node = node;
1133 } else if (!av_strcasecmp(node->name, (const char *)"BaseURL")) {
1134 adaptionset_baseurl_node = node;
1135 } else if (!av_strcasecmp(node->name, (const char *)"SegmentList")) {
1136 adaptionset_segmentlist_node = node;
1137 } else if (!av_strcasecmp(node->name, (const char *)"SupplementalProperty")) {
1138 adaptionset_supplementalproperty_node = node;
1139 } else if (!av_strcasecmp(node->name, (const char *)"Representation")) {
1140 ret = parse_manifest_representation(s, url, node,
1143 period_baseurl_node,
1144 period_segmenttemplate_node,
1145 period_segmentlist_node,
1146 fragment_template_node,
1147 content_component_node,
1148 adaptionset_baseurl_node,
1149 adaptionset_segmentlist_node,
1150 adaptionset_supplementalproperty_node);
1154 node = xmlNextElementSibling(node);
1158 av_freep(&c->adaptionset_lang);
1162 static int parse_programinformation(AVFormatContext *s, xmlNodePtr node)
1164 xmlChar *val = NULL;
1166 node = xmlFirstElementChild(node);
1168 if (!av_strcasecmp(node->name, "Title")) {
1169 val = xmlNodeGetContent(node);
1171 av_dict_set(&s->metadata, "Title", val, 0);
1173 } else if (!av_strcasecmp(node->name, "Source")) {
1174 val = xmlNodeGetContent(node);
1176 av_dict_set(&s->metadata, "Source", val, 0);
1178 } else if (!av_strcasecmp(node->name, "Copyright")) {
1179 val = xmlNodeGetContent(node);
1181 av_dict_set(&s->metadata, "Copyright", val, 0);
1184 node = xmlNextElementSibling(node);
1191 static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
1193 DASHContext *c = s->priv_data;
1196 uint8_t *new_url = NULL;
1197 int64_t filesize = 0;
1199 AVDictionary *opts = NULL;
1201 xmlNodePtr root_element = NULL;
1202 xmlNodePtr node = NULL;
1203 xmlNodePtr period_node = NULL;
1204 xmlNodePtr tmp_node = NULL;
1205 xmlNodePtr mpd_baseurl_node = NULL;
1206 xmlNodePtr period_baseurl_node = NULL;
1207 xmlNodePtr period_segmenttemplate_node = NULL;
1208 xmlNodePtr period_segmentlist_node = NULL;
1209 xmlNodePtr adaptionset_node = NULL;
1210 xmlAttrPtr attr = NULL;
1212 uint32_t period_duration_sec = 0;
1213 uint32_t period_start_sec = 0;
1218 av_dict_copy(&opts, c->avio_opts, 0);
1219 ret = avio_open2(&in, url, AVIO_FLAG_READ, c->interrupt_callback, &opts);
1220 av_dict_free(&opts);
1225 if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, &new_url) >= 0) {
1226 c->base_url = av_strdup(new_url);
1228 c->base_url = av_strdup(url);
1231 filesize = avio_size(in);
1232 filesize = filesize > 0 ? filesize : DEFAULT_MANIFEST_SIZE;
1234 if (filesize > MAX_BPRINT_READ_SIZE) {
1235 av_log(s, AV_LOG_ERROR, "Manifest too large: %"PRId64"\n", filesize);
1236 return AVERROR_INVALIDDATA;
1239 av_bprint_init(&buf, filesize + 1, AV_BPRINT_SIZE_UNLIMITED);
1241 if ((ret = avio_read_to_bprint(in, &buf, MAX_BPRINT_READ_SIZE)) < 0 ||
1243 (filesize = buf.len) == 0) {
1244 av_log(s, AV_LOG_ERROR, "Unable to read to manifest '%s'\n", url);
1246 ret = AVERROR_INVALIDDATA;
1250 doc = xmlReadMemory(buf.str, 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);
1373 av_bprint_finalize(&buf, NULL);
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)
1493 DASHContext *c = s->priv_data;
1494 // save current context
1495 int n_videos = c->n_videos;
1496 struct representation **videos = c->videos;
1497 int n_audios = c->n_audios;
1498 struct representation **audios = c->audios;
1499 int n_subtitles = c->n_subtitles;
1500 struct representation **subtitles = c->subtitles;
1501 char *base_url = c->base_url;
1509 c->subtitles = NULL;
1510 ret = parse_manifest(s, s->url, NULL);
1514 if (c->n_videos != n_videos) {
1515 av_log(c, AV_LOG_ERROR,
1516 "new manifest has mismatched no. of video representations, %d -> %d\n",
1517 n_videos, c->n_videos);
1518 return AVERROR_INVALIDDATA;
1520 if (c->n_audios != n_audios) {
1521 av_log(c, AV_LOG_ERROR,
1522 "new manifest has mismatched no. of audio representations, %d -> %d\n",
1523 n_audios, c->n_audios);
1524 return AVERROR_INVALIDDATA;
1526 if (c->n_subtitles != n_subtitles) {
1527 av_log(c, AV_LOG_ERROR,
1528 "new manifest has mismatched no. of subtitles representations, %d -> %d\n",
1529 n_subtitles, c->n_subtitles);
1530 return AVERROR_INVALIDDATA;
1533 for (i = 0; i < n_videos; i++) {
1534 struct representation *cur_video = videos[i];
1535 struct representation *ccur_video = c->videos[i];
1536 if (cur_video->timelines) {
1537 // calc current time
1538 int64_t currentTime = get_segment_start_time_based_on_timeline(cur_video, cur_video->cur_seq_no) / cur_video->fragment_timescale;
1540 ccur_video->cur_seq_no = calc_next_seg_no_from_timelines(ccur_video, currentTime * cur_video->fragment_timescale - 1);
1541 if (ccur_video->cur_seq_no >= 0) {
1542 move_timelines(ccur_video, cur_video, c);
1545 if (cur_video->fragments) {
1546 move_segments(ccur_video, cur_video, c);
1549 for (i = 0; i < n_audios; i++) {
1550 struct representation *cur_audio = audios[i];
1551 struct representation *ccur_audio = c->audios[i];
1552 if (cur_audio->timelines) {
1553 // calc current time
1554 int64_t currentTime = get_segment_start_time_based_on_timeline(cur_audio, cur_audio->cur_seq_no) / cur_audio->fragment_timescale;
1556 ccur_audio->cur_seq_no = calc_next_seg_no_from_timelines(ccur_audio, currentTime * cur_audio->fragment_timescale - 1);
1557 if (ccur_audio->cur_seq_no >= 0) {
1558 move_timelines(ccur_audio, cur_audio, c);
1561 if (cur_audio->fragments) {
1562 move_segments(ccur_audio, cur_audio, c);
1571 c->base_url = base_url;
1574 free_subtitle_list(c);
1580 c->n_subtitles = n_subtitles;
1581 c->subtitles = subtitles;
1582 c->n_audios = n_audios;
1584 c->n_videos = n_videos;
1589 static struct fragment *get_current_fragment(struct representation *pls)
1591 int64_t min_seq_no = 0;
1592 int64_t max_seq_no = 0;
1593 struct fragment *seg = NULL;
1594 struct fragment *seg_ptr = NULL;
1595 DASHContext *c = pls->parent->priv_data;
1597 while (( !ff_check_interrupt(c->interrupt_callback)&& pls->n_fragments > 0)) {
1598 if (pls->cur_seq_no < pls->n_fragments) {
1599 seg_ptr = pls->fragments[pls->cur_seq_no];
1600 seg = av_mallocz(sizeof(struct fragment));
1604 seg->url = av_strdup(seg_ptr->url);
1609 seg->size = seg_ptr->size;
1610 seg->url_offset = seg_ptr->url_offset;
1612 } else if (c->is_live) {
1613 refresh_manifest(pls->parent);
1619 min_seq_no = calc_min_seg_no(pls->parent, pls);
1620 max_seq_no = calc_max_seg_no(pls, c);
1622 if (pls->timelines || pls->fragments) {
1623 refresh_manifest(pls->parent);
1625 if (pls->cur_seq_no <= min_seq_no) {
1626 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);
1627 pls->cur_seq_no = calc_cur_seg_no(pls->parent, pls);
1628 } else if (pls->cur_seq_no > max_seq_no) {
1629 av_log(pls->parent, AV_LOG_VERBOSE, "new fragment: min[%"PRId64"] max[%"PRId64"]\n", min_seq_no, max_seq_no);
1631 seg = av_mallocz(sizeof(struct fragment));
1635 } else if (pls->cur_seq_no <= pls->last_seq_no) {
1636 seg = av_mallocz(sizeof(struct fragment));
1642 char *tmpfilename= av_mallocz(c->max_url_size);
1646 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));
1647 seg->url = av_strireplace(pls->url_template, pls->url_template, tmpfilename);
1649 av_log(pls->parent, AV_LOG_WARNING, "Unable to resolve template url '%s', try to use origin template\n", pls->url_template);
1650 seg->url = av_strdup(pls->url_template);
1652 av_log(pls->parent, AV_LOG_ERROR, "Cannot resolve template url '%s'\n", pls->url_template);
1653 av_free(tmpfilename);
1657 av_free(tmpfilename);
1664 static int read_from_url(struct representation *pls, struct fragment *seg,
1665 uint8_t *buf, int buf_size)
1669 /* limit read if the fragment was only a part of a file */
1671 buf_size = FFMIN(buf_size, pls->cur_seg_size - pls->cur_seg_offset);
1673 ret = avio_read(pls->input, buf, buf_size);
1675 pls->cur_seg_offset += ret;
1680 static int open_input(DASHContext *c, struct representation *pls, struct fragment *seg)
1682 AVDictionary *opts = NULL;
1686 url = av_mallocz(c->max_url_size);
1688 ret = AVERROR(ENOMEM);
1692 if (seg->size >= 0) {
1693 /* try to restrict the HTTP request to the part we want
1694 * (if this is in fact a HTTP request) */
1695 av_dict_set_int(&opts, "offset", seg->url_offset, 0);
1696 av_dict_set_int(&opts, "end_offset", seg->url_offset + seg->size, 0);
1699 ff_make_absolute_url(url, c->max_url_size, c->base_url, seg->url);
1700 av_log(pls->parent, AV_LOG_VERBOSE, "DASH request for url '%s', offset %"PRId64"\n",
1701 url, seg->url_offset);
1702 ret = open_url(pls->parent, &pls->input, url, &c->avio_opts, opts, NULL);
1706 av_dict_free(&opts);
1707 pls->cur_seg_offset = 0;
1708 pls->cur_seg_size = seg->size;
1712 static int update_init_section(struct representation *pls)
1714 static const int max_init_section_size = 1024 * 1024;
1715 DASHContext *c = pls->parent->priv_data;
1720 if (!pls->init_section || pls->init_sec_buf)
1723 ret = open_input(c, pls, pls->init_section);
1725 av_log(pls->parent, AV_LOG_WARNING,
1726 "Failed to open an initialization section\n");
1730 if (pls->init_section->size >= 0)
1731 sec_size = pls->init_section->size;
1732 else if ((urlsize = avio_size(pls->input)) >= 0)
1735 sec_size = max_init_section_size;
1737 av_log(pls->parent, AV_LOG_DEBUG,
1738 "Downloading an initialization section of size %"PRId64"\n",
1741 sec_size = FFMIN(sec_size, max_init_section_size);
1743 av_fast_malloc(&pls->init_sec_buf, &pls->init_sec_buf_size, sec_size);
1745 ret = read_from_url(pls, pls->init_section, pls->init_sec_buf,
1746 pls->init_sec_buf_size);
1747 ff_format_io_close(pls->parent, &pls->input);
1752 pls->init_sec_data_len = ret;
1753 pls->init_sec_buf_read_offset = 0;
1758 static int64_t seek_data(void *opaque, int64_t offset, int whence)
1760 struct representation *v = opaque;
1761 if (v->n_fragments && !v->init_sec_data_len) {
1762 return avio_seek(v->input, offset, whence);
1765 return AVERROR(ENOSYS);
1768 static int read_data(void *opaque, uint8_t *buf, int buf_size)
1771 struct representation *v = opaque;
1772 DASHContext *c = v->parent->priv_data;
1776 free_fragment(&v->cur_seg);
1777 v->cur_seg = get_current_fragment(v);
1783 /* load/update Media Initialization Section, if any */
1784 ret = update_init_section(v);
1788 ret = open_input(c, v, v->cur_seg);
1790 if (ff_check_interrupt(c->interrupt_callback)) {
1794 av_log(v->parent, AV_LOG_WARNING, "Failed to open fragment of playlist\n");
1800 if (v->init_sec_buf_read_offset < v->init_sec_data_len) {
1801 /* Push init section out first before first actual fragment */
1802 int copy_size = FFMIN(v->init_sec_data_len - v->init_sec_buf_read_offset, buf_size);
1803 memcpy(buf, v->init_sec_buf, copy_size);
1804 v->init_sec_buf_read_offset += copy_size;
1809 /* check the v->cur_seg, if it is null, get current and double check if the new v->cur_seg*/
1811 v->cur_seg = get_current_fragment(v);
1817 ret = read_from_url(v, v->cur_seg, buf, buf_size);
1821 if (c->is_live || v->cur_seq_no < v->last_seq_no) {
1822 if (!v->is_restart_needed)
1824 v->is_restart_needed = 1;
1831 static int save_avio_options(AVFormatContext *s)
1833 DASHContext *c = s->priv_data;
1834 const char *opts[] = {
1835 "headers", "user_agent", "cookies", "http_proxy", "referer", "rw_timeout", "icy", NULL };
1836 const char **opt = opts;
1837 uint8_t *buf = NULL;
1841 if (av_opt_get(s->pb, *opt, AV_OPT_SEARCH_CHILDREN, &buf) >= 0) {
1842 if (buf[0] != '\0') {
1843 ret = av_dict_set(&c->avio_opts, *opt, buf, AV_DICT_DONT_STRDUP_VAL);
1856 static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
1857 int flags, AVDictionary **opts)
1859 av_log(s, AV_LOG_ERROR,
1860 "A DASH playlist item '%s' referred to an external file '%s'. "
1861 "Opening this file was forbidden for security reasons\n",
1863 return AVERROR(EPERM);
1866 static void close_demux_for_component(struct representation *pls)
1868 /* note: the internal buffer could have changed */
1869 av_freep(&pls->pb.buffer);
1870 memset(&pls->pb, 0x00, sizeof(AVIOContext));
1871 pls->ctx->pb = NULL;
1872 avformat_close_input(&pls->ctx);
1875 static int reopen_demux_for_component(AVFormatContext *s, struct representation *pls)
1877 DASHContext *c = s->priv_data;
1878 ff_const59 AVInputFormat *in_fmt = NULL;
1879 AVDictionary *in_fmt_opts = NULL;
1880 uint8_t *avio_ctx_buffer = NULL;
1884 close_demux_for_component(pls);
1887 if (ff_check_interrupt(&s->interrupt_callback)) {
1892 if (!(pls->ctx = avformat_alloc_context())) {
1893 ret = AVERROR(ENOMEM);
1897 avio_ctx_buffer = av_malloc(INITIAL_BUFFER_SIZE);
1898 if (!avio_ctx_buffer ) {
1899 ret = AVERROR(ENOMEM);
1900 avformat_free_context(pls->ctx);
1905 ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, NULL);
1907 ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, seek_data);
1909 pls->pb.seekable = 0;
1911 if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0)
1914 pls->ctx->flags = AVFMT_FLAG_CUSTOM_IO;
1915 pls->ctx->probesize = s->probesize > 0 ? s->probesize : 1024 * 4;
1916 pls->ctx->max_analyze_duration = s->max_analyze_duration > 0 ? s->max_analyze_duration : 4 * AV_TIME_BASE;
1917 pls->ctx->interrupt_callback = s->interrupt_callback;
1918 ret = av_probe_input_buffer(&pls->pb, &in_fmt, "", NULL, 0, 0);
1920 av_log(s, AV_LOG_ERROR, "Error when loading first fragment of playlist\n");
1921 avformat_free_context(pls->ctx);
1926 pls->ctx->pb = &pls->pb;
1927 pls->ctx->io_open = nested_io_open;
1929 // provide additional information from mpd if available
1930 ret = avformat_open_input(&pls->ctx, "", in_fmt, &in_fmt_opts); //pls->init_section->url
1931 av_dict_free(&in_fmt_opts);
1934 if (pls->n_fragments) {
1935 #if FF_API_R_FRAME_RATE
1936 if (pls->framerate.den) {
1937 for (i = 0; i < pls->ctx->nb_streams; i++)
1938 pls->ctx->streams[i]->r_frame_rate = pls->framerate;
1941 ret = avformat_find_stream_info(pls->ctx, NULL);
1950 static int open_demux_for_component(AVFormatContext *s, struct representation *pls)
1956 pls->cur_seq_no = calc_cur_seg_no(s, pls);
1958 if (!pls->last_seq_no) {
1959 pls->last_seq_no = calc_max_seg_no(pls, s->priv_data);
1962 ret = reopen_demux_for_component(s, pls);
1966 for (i = 0; i < pls->ctx->nb_streams; i++) {
1967 AVStream *st = avformat_new_stream(s, NULL);
1968 AVStream *ist = pls->ctx->streams[i];
1970 ret = AVERROR(ENOMEM);
1974 avcodec_parameters_copy(st->codecpar, ist->codecpar);
1975 avpriv_set_pts_info(st, ist->pts_wrap_bits, ist->time_base.num, ist->time_base.den);
1978 st->disposition = ist->disposition;
1981 for (int i = 0; i < ist->nb_side_data; i++) {
1982 const AVPacketSideData *sd_src = &ist->side_data[i];
1985 dst_data = av_stream_new_side_data(st, sd_src->type, sd_src->size);
1987 return AVERROR(ENOMEM);
1988 memcpy(dst_data, sd_src->data, sd_src->size);
1997 static int is_common_init_section_exist(struct representation **pls, int n_pls)
1999 struct fragment *first_init_section = pls[0]->init_section;
2001 int64_t url_offset = -1;
2005 if (first_init_section == NULL || n_pls == 0)
2008 url = first_init_section->url;
2009 url_offset = first_init_section->url_offset;
2010 size = pls[0]->init_section->size;
2011 for (i=0;i<n_pls;i++) {
2012 if (av_strcasecmp(pls[i]->init_section->url,url) || pls[i]->init_section->url_offset != url_offset || pls[i]->init_section->size != size) {
2019 static int copy_init_section(struct representation *rep_dest, struct representation *rep_src)
2021 rep_dest->init_sec_buf = av_mallocz(rep_src->init_sec_buf_size);
2022 if (!rep_dest->init_sec_buf) {
2023 av_log(rep_dest->ctx, AV_LOG_WARNING, "Cannot alloc memory for init_sec_buf\n");
2024 return AVERROR(ENOMEM);
2026 memcpy(rep_dest->init_sec_buf, rep_src->init_sec_buf, rep_src->init_sec_data_len);
2027 rep_dest->init_sec_buf_size = rep_src->init_sec_buf_size;
2028 rep_dest->init_sec_data_len = rep_src->init_sec_data_len;
2029 rep_dest->cur_timestamp = rep_src->cur_timestamp;
2034 static int dash_close(AVFormatContext *s);
2036 static int dash_read_header(AVFormatContext *s)
2038 DASHContext *c = s->priv_data;
2039 struct representation *rep;
2042 int stream_index = 0;
2045 c->interrupt_callback = &s->interrupt_callback;
2047 if ((ret = save_avio_options(s)) < 0)
2050 if ((ret = parse_manifest(s, s->url, s->pb)) < 0)
2053 /* If this isn't a live stream, fill the total duration of the
2056 s->duration = (int64_t) c->media_presentation_duration * AV_TIME_BASE;
2058 av_dict_set(&c->avio_opts, "seekable", "0", 0);
2062 c->is_init_section_common_video = is_common_init_section_exist(c->videos, c->n_videos);
2064 /* Open the demuxer for video and audio components if available */
2065 for (i = 0; i < c->n_videos; i++) {
2067 if (i > 0 && c->is_init_section_common_video) {
2068 ret = copy_init_section(rep, c->videos[0]);
2072 ret = open_demux_for_component(s, rep);
2076 rep->stream_index = stream_index;
2081 c->is_init_section_common_audio = is_common_init_section_exist(c->audios, c->n_audios);
2083 for (i = 0; i < c->n_audios; i++) {
2085 if (i > 0 && c->is_init_section_common_audio) {
2086 ret = copy_init_section(rep, c->audios[0]);
2090 ret = open_demux_for_component(s, rep);
2094 rep->stream_index = stream_index;
2099 c->is_init_section_common_audio = is_common_init_section_exist(c->subtitles, c->n_subtitles);
2101 for (i = 0; i < c->n_subtitles; i++) {
2102 rep = c->subtitles[i];
2103 if (i > 0 && c->is_init_section_common_audio) {
2104 ret = copy_init_section(rep, c->subtitles[0]);
2108 ret = open_demux_for_component(s, rep);
2112 rep->stream_index = stream_index;
2116 if (!stream_index) {
2117 ret = AVERROR_INVALIDDATA;
2121 /* Create a program */
2122 program = av_new_program(s, 0);
2124 ret = AVERROR(ENOMEM);
2128 for (i = 0; i < c->n_videos; 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 for (i = 0; i < c->n_audios; i++) {
2139 av_program_add_stream_index(s, 0, rep->stream_index);
2140 rep->assoc_stream = s->streams[rep->stream_index];
2141 if (rep->bandwidth > 0)
2142 av_dict_set_int(&rep->assoc_stream->metadata, "variant_bitrate", rep->bandwidth, 0);
2144 av_dict_set(&rep->assoc_stream->metadata, "id", rep->id, 0);
2146 av_dict_set(&rep->assoc_stream->metadata, "language", rep->lang, 0);
2147 av_freep(&rep->lang);
2150 for (i = 0; i < c->n_subtitles; i++) {
2151 rep = c->subtitles[i];
2152 av_program_add_stream_index(s, 0, rep->stream_index);
2153 rep->assoc_stream = s->streams[rep->stream_index];
2155 av_dict_set(&rep->assoc_stream->metadata, "id", rep->id, 0);
2157 av_dict_set(&rep->assoc_stream->metadata, "language", rep->lang, 0);
2158 av_freep(&rep->lang);
2168 static void recheck_discard_flags(AVFormatContext *s, struct representation **p, int n)
2172 for (i = 0; i < n; i++) {
2173 struct representation *pls = p[i];
2174 int needed = !pls->assoc_stream || pls->assoc_stream->discard < AVDISCARD_ALL;
2176 if (needed && !pls->ctx) {
2177 pls->cur_seg_offset = 0;
2178 pls->init_sec_buf_read_offset = 0;
2180 for (j = 0; j < n; j++) {
2181 pls->cur_seq_no = FFMAX(pls->cur_seq_no, p[j]->cur_seq_no);
2183 reopen_demux_for_component(s, pls);
2184 av_log(s, AV_LOG_INFO, "Now receiving stream_index %d\n", pls->stream_index);
2185 } else if (!needed && pls->ctx) {
2186 close_demux_for_component(pls);
2187 ff_format_io_close(pls->parent, &pls->input);
2188 av_log(s, AV_LOG_INFO, "No longer receiving stream_index %d\n", pls->stream_index);
2193 static int dash_read_packet(AVFormatContext *s, AVPacket *pkt)
2195 DASHContext *c = s->priv_data;
2198 struct representation *cur = NULL;
2199 struct representation *rep = NULL;
2201 recheck_discard_flags(s, c->videos, c->n_videos);
2202 recheck_discard_flags(s, c->audios, c->n_audios);
2203 recheck_discard_flags(s, c->subtitles, c->n_subtitles);
2205 for (i = 0; i < c->n_videos; i++) {
2209 if (!cur || rep->cur_timestamp < mints) {
2211 mints = rep->cur_timestamp;
2214 for (i = 0; i < c->n_audios; i++) {
2218 if (!cur || rep->cur_timestamp < mints) {
2220 mints = rep->cur_timestamp;
2224 for (i = 0; i < c->n_subtitles; i++) {
2225 rep = c->subtitles[i];
2228 if (!cur || rep->cur_timestamp < mints) {
2230 mints = rep->cur_timestamp;
2235 return AVERROR_INVALIDDATA;
2237 while (!ff_check_interrupt(c->interrupt_callback) && !ret) {
2238 ret = av_read_frame(cur->ctx, pkt);
2240 /* If we got a packet, return it */
2241 cur->cur_timestamp = av_rescale(pkt->pts, (int64_t)cur->ctx->streams[0]->time_base.num * 90000, cur->ctx->streams[0]->time_base.den);
2242 pkt->stream_index = cur->stream_index;
2245 if (cur->is_restart_needed) {
2246 cur->cur_seg_offset = 0;
2247 cur->init_sec_buf_read_offset = 0;
2248 ff_format_io_close(cur->parent, &cur->input);
2249 ret = reopen_demux_for_component(s, cur);
2250 cur->is_restart_needed = 0;
2256 static int dash_close(AVFormatContext *s)
2258 DASHContext *c = s->priv_data;
2261 free_subtitle_list(c);
2262 av_dict_free(&c->avio_opts);
2263 av_freep(&c->base_url);
2267 static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos_msec, int flags, int dry_run)
2272 int64_t duration = 0;
2274 av_log(pls->parent, AV_LOG_VERBOSE, "DASH seek pos[%"PRId64"ms] %s\n",
2275 seek_pos_msec, dry_run ? " (dry)" : "");
2277 // single fragment mode
2278 if (pls->n_fragments == 1) {
2279 pls->cur_timestamp = 0;
2280 pls->cur_seg_offset = 0;
2283 ff_read_frame_flush(pls->ctx);
2284 return av_seek_frame(pls->ctx, -1, seek_pos_msec * 1000, flags);
2287 ff_format_io_close(pls->parent, &pls->input);
2289 // find the nearest fragment
2290 if (pls->n_timelines > 0 && pls->fragment_timescale > 0) {
2291 int64_t num = pls->first_seq_no;
2292 av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline start n_timelines[%d] "
2293 "last_seq_no[%"PRId64"].\n",
2294 (int)pls->n_timelines, (int64_t)pls->last_seq_no);
2295 for (i = 0; i < pls->n_timelines; i++) {
2296 if (pls->timelines[i]->starttime > 0) {
2297 duration = pls->timelines[i]->starttime;
2299 duration += pls->timelines[i]->duration;
2300 if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
2303 for (j = 0; j < pls->timelines[i]->repeat; j++) {
2304 duration += pls->timelines[i]->duration;
2306 if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
2314 pls->cur_seq_no = num > pls->last_seq_no ? pls->last_seq_no : num;
2315 av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline end cur_seq_no[%"PRId64"].\n",
2316 (int64_t)pls->cur_seq_no);
2317 } else if (pls->fragment_duration > 0) {
2318 pls->cur_seq_no = pls->first_seq_no + ((seek_pos_msec * pls->fragment_timescale) / pls->fragment_duration) / 1000;
2320 av_log(pls->parent, AV_LOG_ERROR, "dash_seek missing timeline or fragment_duration\n");
2321 pls->cur_seq_no = pls->first_seq_no;
2323 pls->cur_timestamp = 0;
2324 pls->cur_seg_offset = 0;
2325 pls->init_sec_buf_read_offset = 0;
2326 ret = dry_run ? 0 : reopen_demux_for_component(s, pls);
2331 static int dash_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
2334 DASHContext *c = s->priv_data;
2335 int64_t seek_pos_msec = av_rescale_rnd(timestamp, 1000,
2336 s->streams[stream_index]->time_base.den,
2337 flags & AVSEEK_FLAG_BACKWARD ?
2338 AV_ROUND_DOWN : AV_ROUND_UP);
2339 if ((flags & AVSEEK_FLAG_BYTE) || c->is_live)
2340 return AVERROR(ENOSYS);
2342 /* Seek in discarded streams with dry_run=1 to avoid reopening them */
2343 for (i = 0; i < c->n_videos; i++) {
2345 ret = dash_seek(s, c->videos[i], seek_pos_msec, flags, !c->videos[i]->ctx);
2347 for (i = 0; i < c->n_audios; i++) {
2349 ret = dash_seek(s, c->audios[i], seek_pos_msec, flags, !c->audios[i]->ctx);
2351 for (i = 0; i < c->n_subtitles; i++) {
2353 ret = dash_seek(s, c->subtitles[i], seek_pos_msec, flags, !c->subtitles[i]->ctx);
2359 static int dash_probe(const AVProbeData *p)
2361 if (!av_stristr(p->buf, "<MPD"))
2364 if (av_stristr(p->buf, "dash:profile:isoff-on-demand:2011") ||
2365 av_stristr(p->buf, "dash:profile:isoff-live:2011") ||
2366 av_stristr(p->buf, "dash:profile:isoff-live:2012") ||
2367 av_stristr(p->buf, "dash:profile:isoff-main:2011") ||
2368 av_stristr(p->buf, "3GPP:PSS:profile:DASH1")) {
2369 return AVPROBE_SCORE_MAX;
2371 if (av_stristr(p->buf, "dash:profile")) {
2372 return AVPROBE_SCORE_MAX;
2378 #define OFFSET(x) offsetof(DASHContext, x)
2379 #define FLAGS AV_OPT_FLAG_DECODING_PARAM
2380 static const AVOption dash_options[] = {
2381 {"allowed_extensions", "List of file extensions that dash is allowed to access",
2382 OFFSET(allowed_extensions), AV_OPT_TYPE_STRING,
2383 {.str = "aac,m4a,m4s,m4v,mov,mp4,webm,ts"},
2384 INT_MIN, INT_MAX, FLAGS},
2388 static const AVClass dash_class = {
2389 .class_name = "dash",
2390 .item_name = av_default_item_name,
2391 .option = dash_options,
2392 .version = LIBAVUTIL_VERSION_INT,
2395 AVInputFormat ff_dash_demuxer = {
2397 .long_name = NULL_IF_CONFIG_SMALL("Dynamic Adaptive Streaming over HTTP"),
2398 .priv_class = &dash_class,
2399 .priv_data_size = sizeof(DASHContext),
2400 .read_probe = dash_probe,
2401 .read_header = dash_read_header,
2402 .read_packet = dash_read_packet,
2403 .read_close = dash_close,
2404 .read_seek = dash_read_seek,
2405 .flags = AVFMT_NO_BYTE_SEEK,