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;
127 struct representation **videos;
129 struct representation **audios;
131 struct representation **subtitles;
133 /* MediaPresentationDescription Attribute */
134 uint64_t media_presentation_duration;
135 uint64_t suggested_presentation_delay;
136 uint64_t availability_start_time;
137 uint64_t availability_end_time;
138 uint64_t publish_time;
139 uint64_t minimum_update_period;
140 uint64_t time_shift_buffer_depth;
141 uint64_t min_buffer_time;
143 /* Period Attribute */
144 uint64_t period_duration;
145 uint64_t period_start;
148 AVIOInterruptCB *interrupt_callback;
149 char *allowed_extensions;
150 AVDictionary *avio_opts;
153 /* Flags for init section*/
154 int is_init_section_common_video;
155 int is_init_section_common_audio;
159 static int ishttp(char *url)
161 const char *proto_name = avio_find_protocol_name(url);
162 return av_strstart(proto_name, "http", NULL);
165 static int aligned(int val)
167 return ((val + 0x3F) >> 6) << 6;
170 static uint64_t get_current_time_in_sec(void)
172 return av_gettime() / 1000000;
175 static uint64_t get_utc_date_time_insec(AVFormatContext *s, const char *datetime)
186 /* ISO-8601 date parser */
190 ret = sscanf(datetime, "%d-%d-%dT%d:%d:%fZ", &year, &month, &day, &hour, &minute, &second);
191 /* year, month, day, hour, minute, second 6 arguments */
193 av_log(s, AV_LOG_WARNING, "get_utc_date_time_insec get a wrong time format\n");
195 timeinfo.tm_year = year - 1900;
196 timeinfo.tm_mon = month - 1;
197 timeinfo.tm_mday = day;
198 timeinfo.tm_hour = hour;
199 timeinfo.tm_min = minute;
200 timeinfo.tm_sec = (int)second;
202 return av_timegm(&timeinfo);
205 static uint32_t get_duration_insec(AVFormatContext *s, const char *duration)
207 /* ISO-8601 duration parser */
215 const char *ptr = duration;
218 if (*ptr == 'P' || *ptr == 'T') {
223 if (sscanf(ptr, "%f%c%n", &value, &type, &size) != 2) {
224 av_log(s, AV_LOG_WARNING, "get_duration_insec get a wrong time format\n");
225 return 0; /* parser error */
229 days = (uint32_t)value;
232 hours = (uint32_t)value;
235 mins = (uint32_t)value;
238 secs = (uint32_t)value;
241 // handle invalid type
246 return ((days * 24 + hours) * 60 + mins) * 60 + secs;
249 static int64_t get_segment_start_time_based_on_timeline(struct representation *pls, int64_t cur_seq_no)
251 int64_t start_time = 0;
256 if (pls->n_timelines) {
257 for (i = 0; i < pls->n_timelines; i++) {
258 if (pls->timelines[i]->starttime > 0) {
259 start_time = pls->timelines[i]->starttime;
261 if (num == cur_seq_no)
264 start_time += pls->timelines[i]->duration;
266 if (pls->timelines[i]->repeat == -1) {
267 start_time = pls->timelines[i]->duration * cur_seq_no;
271 for (j = 0; j < pls->timelines[i]->repeat; j++) {
273 if (num == cur_seq_no)
275 start_time += pls->timelines[i]->duration;
284 static int64_t calc_next_seg_no_from_timelines(struct representation *pls, int64_t cur_time)
289 int64_t start_time = 0;
291 for (i = 0; i < pls->n_timelines; i++) {
292 if (pls->timelines[i]->starttime > 0) {
293 start_time = pls->timelines[i]->starttime;
295 if (start_time > cur_time)
298 start_time += pls->timelines[i]->duration;
299 for (j = 0; j < pls->timelines[i]->repeat; j++) {
301 if (start_time > cur_time)
303 start_time += pls->timelines[i]->duration;
314 static void free_fragment(struct fragment **seg)
319 av_freep(&(*seg)->url);
323 static void free_fragment_list(struct representation *pls)
327 for (i = 0; i < pls->n_fragments; i++) {
328 free_fragment(&pls->fragments[i]);
330 av_freep(&pls->fragments);
331 pls->n_fragments = 0;
334 static void free_timelines_list(struct representation *pls)
338 for (i = 0; i < pls->n_timelines; i++) {
339 av_freep(&pls->timelines[i]);
341 av_freep(&pls->timelines);
342 pls->n_timelines = 0;
345 static void free_representation(struct representation *pls)
347 free_fragment_list(pls);
348 free_timelines_list(pls);
349 free_fragment(&pls->cur_seg);
350 free_fragment(&pls->init_section);
351 av_freep(&pls->init_sec_buf);
352 av_freep(&pls->pb.buffer);
353 ff_format_io_close(pls->parent, &pls->input);
356 avformat_close_input(&pls->ctx);
359 av_freep(&pls->url_template);
363 static void free_video_list(DASHContext *c)
366 for (i = 0; i < c->n_videos; i++) {
367 struct representation *pls = c->videos[i];
368 free_representation(pls);
370 av_freep(&c->videos);
374 static void free_audio_list(DASHContext *c)
377 for (i = 0; i < c->n_audios; i++) {
378 struct representation *pls = c->audios[i];
379 free_representation(pls);
381 av_freep(&c->audios);
385 static void free_subtitle_list(DASHContext *c)
388 for (i = 0; i < c->n_subtitles; i++) {
389 struct representation *pls = c->subtitles[i];
390 free_representation(pls);
392 av_freep(&c->subtitles);
396 static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
397 AVDictionary *opts, AVDictionary *opts2, int *is_http)
399 DASHContext *c = s->priv_data;
400 AVDictionary *tmp = NULL;
401 const char *proto_name = NULL;
404 av_dict_copy(&tmp, opts, 0);
405 av_dict_copy(&tmp, opts2, 0);
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 ret = avio_open2(pb, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp);
442 // update cookies on http response with setcookies.
443 char *new_cookies = NULL;
445 if (!(s->flags & AVFMT_FLAG_CUSTOM_IO))
446 av_opt_get(*pb, "cookies", AV_OPT_SEARCH_CHILDREN, (uint8_t**)&new_cookies);
449 av_dict_set(&opts, "cookies", new_cookies, AV_DICT_DONT_STRDUP_VAL);
457 *is_http = av_strstart(proto_name, "http", NULL);
462 static char *get_content_url(xmlNodePtr *baseurl_nodes,
466 char *rep_bandwidth_val,
472 char *tmp_str = av_mallocz(max_url_size);
473 char *tmp_str_2 = av_mallocz(max_url_size);
475 if (!tmp_str || !tmp_str_2) {
479 for (i = 0; i < n_baseurl_nodes; ++i) {
480 if (baseurl_nodes[i] &&
481 baseurl_nodes[i]->children &&
482 baseurl_nodes[i]->children->type == XML_TEXT_NODE) {
483 text = xmlNodeGetContent(baseurl_nodes[i]->children);
485 memset(tmp_str, 0, max_url_size);
486 memset(tmp_str_2, 0, max_url_size);
487 ff_make_absolute_url(tmp_str_2, max_url_size, tmp_str, text);
488 av_strlcpy(tmp_str, tmp_str_2, max_url_size);
495 ff_make_absolute_url(tmp_str, max_url_size, tmp_str, val);
498 url = av_strireplace(tmp_str, "$RepresentationID$", (const char*)rep_id_val);
502 av_strlcpy(tmp_str, url, max_url_size);
504 if (rep_bandwidth_val && tmp_str[0] != '\0') {
505 // free any previously assigned url before reassigning
507 url = av_strireplace(tmp_str, "$Bandwidth$", (const char*)rep_bandwidth_val);
518 static char *get_val_from_nodes_tab(xmlNodePtr *nodes, const int n_nodes, const char *attrname)
523 for (i = 0; i < n_nodes; ++i) {
525 val = xmlGetProp(nodes[i], attrname);
534 static xmlNodePtr find_child_node_by_name(xmlNodePtr rootnode, const char *nodename)
536 xmlNodePtr node = rootnode;
541 node = xmlFirstElementChild(node);
543 if (!av_strcasecmp(node->name, nodename)) {
546 node = xmlNextElementSibling(node);
551 static enum AVMediaType get_content_type(xmlNodePtr node)
553 enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
559 for (i = 0; i < 2; i++) {
560 attr = i ? "mimeType" : "contentType";
561 val = xmlGetProp(node, attr);
563 if (av_stristr((const char *)val, "video")) {
564 type = AVMEDIA_TYPE_VIDEO;
565 } else if (av_stristr((const char *)val, "audio")) {
566 type = AVMEDIA_TYPE_AUDIO;
567 } else if (av_stristr((const char *)val, "text")) {
568 type = AVMEDIA_TYPE_SUBTITLE;
577 static struct fragment * get_Fragment(char *range)
579 struct fragment * seg = av_mallocz(sizeof(struct fragment));
586 char *str_end_offset;
587 char *str_offset = av_strtok(range, "-", &str_end_offset);
588 seg->url_offset = strtoll(str_offset, NULL, 10);
589 seg->size = strtoll(str_end_offset, NULL, 10) - seg->url_offset;
595 static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representation *rep,
596 xmlNodePtr fragmenturl_node,
597 xmlNodePtr *baseurl_nodes,
599 char *rep_bandwidth_val)
601 DASHContext *c = s->priv_data;
602 char *initialization_val = NULL;
603 char *media_val = NULL;
604 char *range_val = NULL;
605 int max_url_size = c ? c->max_url_size: MAX_URL_SIZE;
607 if (!av_strcasecmp(fragmenturl_node->name, (const char *)"Initialization")) {
608 initialization_val = xmlGetProp(fragmenturl_node, "sourceURL");
609 range_val = xmlGetProp(fragmenturl_node, "range");
610 if (initialization_val || range_val) {
611 rep->init_section = get_Fragment(range_val);
612 if (!rep->init_section) {
613 xmlFree(initialization_val);
615 return AVERROR(ENOMEM);
617 rep->init_section->url = get_content_url(baseurl_nodes, 4,
623 if (!rep->init_section->url) {
624 av_free(rep->init_section);
625 xmlFree(initialization_val);
627 return AVERROR(ENOMEM);
629 xmlFree(initialization_val);
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,
651 return AVERROR(ENOMEM);
653 dynarray_add(&rep->fragments, &rep->n_fragments, seg);
662 static int parse_manifest_segmenttimeline(AVFormatContext *s, struct representation *rep,
663 xmlNodePtr fragment_timeline_node)
665 xmlAttrPtr attr = NULL;
668 if (!av_strcasecmp(fragment_timeline_node->name, (const char *)"S")) {
669 struct timeline *tml = av_mallocz(sizeof(struct timeline));
671 return AVERROR(ENOMEM);
673 attr = fragment_timeline_node->properties;
675 val = xmlGetProp(fragment_timeline_node, attr->name);
678 av_log(s, AV_LOG_WARNING, "parse_manifest_segmenttimeline attr->name = %s val is NULL\n", attr->name);
682 if (!av_strcasecmp(attr->name, (const char *)"t")) {
683 tml->starttime = (int64_t)strtoll(val, NULL, 10);
684 } else if (!av_strcasecmp(attr->name, (const char *)"r")) {
685 tml->repeat =(int64_t) strtoll(val, NULL, 10);
686 } else if (!av_strcasecmp(attr->name, (const char *)"d")) {
687 tml->duration = (int64_t)strtoll(val, NULL, 10);
692 dynarray_add(&rep->timelines, &rep->n_timelines, tml);
698 static int resolve_content_path(AVFormatContext *s, const char *url, int *max_url_size, xmlNodePtr *baseurl_nodes, int n_baseurl_nodes)
700 char *tmp_str = NULL;
702 char *mpdName = NULL;
703 xmlNodePtr node = NULL;
704 char *baseurl = NULL;
705 char *root_url = NULL;
715 int tmp_max_url_size = strlen(url);
717 for (i = n_baseurl_nodes-1; i >= 0 ; i--) {
718 text = xmlNodeGetContent(baseurl_nodes[i]);
721 tmp_max_url_size += strlen(text);
729 tmp_max_url_size = aligned(tmp_max_url_size);
730 text = av_mallocz(tmp_max_url_size);
732 updated = AVERROR(ENOMEM);
735 av_strlcpy(text, url, strlen(url)+1);
737 while (mpdName = av_strtok(tmp, "/", &tmp)) {
738 size = strlen(mpdName);
742 path = av_mallocz(tmp_max_url_size);
743 tmp_str = av_mallocz(tmp_max_url_size);
744 if (!tmp_str || !path) {
745 updated = AVERROR(ENOMEM);
749 av_strlcpy (path, url, strlen(url) - size + 1);
750 for (rootId = n_baseurl_nodes - 1; rootId > 0; rootId --) {
751 if (!(node = baseurl_nodes[rootId])) {
754 text = xmlNodeGetContent(node);
762 node = baseurl_nodes[rootId];
763 baseurl = xmlNodeGetContent(node);
764 root_url = (av_strcasecmp(baseurl, "")) ? baseurl : path;
766 xmlNodeSetContent(node, root_url);
770 size = strlen(root_url);
771 isRootHttp = ishttp(root_url);
773 if (root_url[size - 1] != token) {
774 av_strlcat(root_url, "/", size + 2);
778 for (i = 0; i < n_baseurl_nodes; ++i) {
782 text = xmlNodeGetContent(baseurl_nodes[i]);
783 if (text && !av_strstart(text, "/", NULL)) {
784 memset(tmp_str, 0, strlen(tmp_str));
785 if (!ishttp(text) && isRootHttp) {
786 av_strlcpy(tmp_str, root_url, size + 1);
788 start = (text[0] == token);
789 if (start && av_stristr(tmp_str, text)) {
791 if (!av_strncasecmp(tmp_str, "http://", 7)) {
793 } else if (!av_strncasecmp(tmp_str, "https://", 8)) {
797 memset(p + 1, 0, strlen(p));
799 av_strlcat(tmp_str, text + start, tmp_max_url_size);
800 xmlNodeSetContent(baseurl_nodes[i], tmp_str);
807 if (tmp_max_url_size > *max_url_size) {
808 *max_url_size = tmp_max_url_size;
817 static int parse_manifest_representation(AVFormatContext *s, const char *url,
819 xmlNodePtr adaptionset_node,
820 xmlNodePtr mpd_baseurl_node,
821 xmlNodePtr period_baseurl_node,
822 xmlNodePtr period_segmenttemplate_node,
823 xmlNodePtr period_segmentlist_node,
824 xmlNodePtr fragment_template_node,
825 xmlNodePtr content_component_node,
826 xmlNodePtr adaptionset_baseurl_node,
827 xmlNodePtr adaptionset_segmentlist_node,
828 xmlNodePtr adaptionset_supplementalproperty_node)
831 int32_t subtitle_rep_idx = 0;
832 int32_t audio_rep_idx = 0;
833 int32_t video_rep_idx = 0;
834 DASHContext *c = s->priv_data;
835 struct representation *rep = NULL;
836 struct fragment *seg = NULL;
837 xmlNodePtr representation_segmenttemplate_node = NULL;
838 xmlNodePtr representation_baseurl_node = NULL;
839 xmlNodePtr representation_segmentlist_node = NULL;
840 xmlNodePtr segmentlists_tab[3];
841 xmlNodePtr fragment_timeline_node = NULL;
842 xmlNodePtr fragment_templates_tab[5];
843 char *duration_val = NULL;
844 char *presentation_timeoffset_val = NULL;
845 char *startnumber_val = NULL;
846 char *timescale_val = NULL;
847 char *initialization_val = NULL;
848 char *media_val = NULL;
850 xmlNodePtr baseurl_nodes[4];
851 xmlNodePtr representation_node = node;
852 char *rep_id_val = xmlGetProp(representation_node, "id");
853 char *rep_bandwidth_val = xmlGetProp(representation_node, "bandwidth");
854 char *rep_framerate_val = xmlGetProp(representation_node, "frameRate");
855 enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
857 // try get information from representation
858 if (type == AVMEDIA_TYPE_UNKNOWN)
859 type = get_content_type(representation_node);
860 // try get information from contentComponen
861 if (type == AVMEDIA_TYPE_UNKNOWN)
862 type = get_content_type(content_component_node);
863 // try get information from adaption set
864 if (type == AVMEDIA_TYPE_UNKNOWN)
865 type = get_content_type(adaptionset_node);
866 if (type == AVMEDIA_TYPE_UNKNOWN) {
867 av_log(s, AV_LOG_VERBOSE, "Parsing '%s' - skipp not supported representation type\n", url);
868 } else if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO || type == AVMEDIA_TYPE_SUBTITLE) {
869 // convert selected representation to our internal struct
870 rep = av_mallocz(sizeof(struct representation));
872 ret = AVERROR(ENOMEM);
876 representation_segmenttemplate_node = find_child_node_by_name(representation_node, "SegmentTemplate");
877 representation_baseurl_node = find_child_node_by_name(representation_node, "BaseURL");
878 representation_segmentlist_node = find_child_node_by_name(representation_node, "SegmentList");
880 baseurl_nodes[0] = mpd_baseurl_node;
881 baseurl_nodes[1] = period_baseurl_node;
882 baseurl_nodes[2] = adaptionset_baseurl_node;
883 baseurl_nodes[3] = representation_baseurl_node;
885 ret = resolve_content_path(s, url, &c->max_url_size, baseurl_nodes, 4);
886 c->max_url_size = aligned(c->max_url_size
887 + (rep_id_val ? strlen(rep_id_val) : 0)
888 + (rep_bandwidth_val ? strlen(rep_bandwidth_val) : 0));
889 if (ret == AVERROR(ENOMEM) || ret == 0) {
892 if (representation_segmenttemplate_node || fragment_template_node || period_segmenttemplate_node) {
893 fragment_timeline_node = NULL;
894 fragment_templates_tab[0] = representation_segmenttemplate_node;
895 fragment_templates_tab[1] = adaptionset_segmentlist_node;
896 fragment_templates_tab[2] = fragment_template_node;
897 fragment_templates_tab[3] = period_segmenttemplate_node;
898 fragment_templates_tab[4] = period_segmentlist_node;
900 presentation_timeoffset_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "presentationTimeOffset");
901 duration_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "duration");
902 startnumber_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "startNumber");
903 timescale_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "timescale");
904 initialization_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "initialization");
905 media_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "media");
907 if (initialization_val) {
908 rep->init_section = av_mallocz(sizeof(struct fragment));
909 if (!rep->init_section) {
911 ret = AVERROR(ENOMEM);
914 c->max_url_size = aligned(c->max_url_size + strlen(initialization_val));
915 rep->init_section->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, initialization_val);
916 if (!rep->init_section->url) {
917 av_free(rep->init_section);
919 ret = AVERROR(ENOMEM);
922 rep->init_section->size = -1;
923 xmlFree(initialization_val);
927 c->max_url_size = aligned(c->max_url_size + strlen(media_val));
928 rep->url_template = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, media_val);
932 if (presentation_timeoffset_val) {
933 rep->presentation_timeoffset = (int64_t) strtoll(presentation_timeoffset_val, NULL, 10);
934 av_log(s, AV_LOG_TRACE, "rep->presentation_timeoffset = [%"PRId64"]\n", rep->presentation_timeoffset);
935 xmlFree(presentation_timeoffset_val);
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);
943 rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
944 av_log(s, AV_LOG_TRACE, "rep->fragment_timescale = [%"PRId64"]\n", rep->fragment_timescale);
945 xmlFree(timescale_val);
947 if (startnumber_val) {
948 rep->start_number = rep->first_seq_no = (int64_t) strtoll(startnumber_val, NULL, 10);
949 av_log(s, AV_LOG_TRACE, "rep->first_seq_no = [%"PRId64"]\n", rep->first_seq_no);
950 xmlFree(startnumber_val);
952 if (adaptionset_supplementalproperty_node) {
953 if (!av_strcasecmp(xmlGetProp(adaptionset_supplementalproperty_node,"schemeIdUri"), "http://dashif.org/guidelines/last-segment-number")) {
954 val = xmlGetProp(adaptionset_supplementalproperty_node,"value");
956 av_log(s, AV_LOG_ERROR, "Missing value attribute in adaptionset_supplementalproperty_node\n");
958 rep->last_seq_no =(int64_t) strtoll(val, NULL, 10) - 1;
964 fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
966 if (!fragment_timeline_node)
967 fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
968 if (!fragment_timeline_node)
969 fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
970 if (!fragment_timeline_node)
971 fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
972 if (fragment_timeline_node) {
973 fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
974 while (fragment_timeline_node) {
975 ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
979 fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
982 } else if (representation_baseurl_node && !representation_segmentlist_node) {
983 seg = av_mallocz(sizeof(struct fragment));
985 ret = AVERROR(ENOMEM);
988 seg->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, NULL);
991 ret = AVERROR(ENOMEM);
995 dynarray_add(&rep->fragments, &rep->n_fragments, seg);
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,
1032 fragmenturl_node = xmlNextElementSibling(fragmenturl_node);
1035 fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
1037 if (!fragment_timeline_node)
1038 fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
1039 if (!fragment_timeline_node)
1040 fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
1041 if (!fragment_timeline_node)
1042 fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
1043 if (fragment_timeline_node) {
1044 fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
1045 while (fragment_timeline_node) {
1046 ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
1050 fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
1054 free_representation(rep);
1056 av_log(s, AV_LOG_ERROR, "Unknown format of Representation node id[%s] \n", (const char *)rep_id_val);
1060 if (rep->fragment_duration > 0 && !rep->fragment_timescale)
1061 rep->fragment_timescale = 1;
1062 rep->bandwidth = rep_bandwidth_val ? atoi(rep_bandwidth_val) : 0;
1063 strncpy(rep->id, rep_id_val ? rep_id_val : "", sizeof(rep->id));
1064 rep->framerate = av_make_q(0, 0);
1065 if (type == AVMEDIA_TYPE_VIDEO && rep_framerate_val) {
1066 ret = av_parse_video_rate(&rep->framerate, rep_framerate_val);
1068 av_log(s, AV_LOG_VERBOSE, "Ignoring invalid frame rate '%s'\n", rep_framerate_val);
1072 case AVMEDIA_TYPE_VIDEO:
1073 rep->rep_idx = video_rep_idx;
1074 dynarray_add(&c->videos, &c->n_videos, rep);
1076 case AVMEDIA_TYPE_AUDIO:
1077 rep->rep_idx = audio_rep_idx;
1078 dynarray_add(&c->audios, &c->n_audios, rep);
1080 case AVMEDIA_TYPE_SUBTITLE:
1081 rep->rep_idx = subtitle_rep_idx;
1082 dynarray_add(&c->subtitles, &c->n_subtitles, rep);
1085 av_log(s, AV_LOG_WARNING, "Unsupported the stream type %d\n", type);
1091 video_rep_idx += type == AVMEDIA_TYPE_VIDEO;
1092 audio_rep_idx += type == AVMEDIA_TYPE_AUDIO;
1093 subtitle_rep_idx += type == AVMEDIA_TYPE_SUBTITLE;
1097 xmlFree(rep_id_val);
1098 if (rep_bandwidth_val)
1099 xmlFree(rep_bandwidth_val);
1100 if (rep_framerate_val)
1101 xmlFree(rep_framerate_val);
1106 static int parse_manifest_adaptationset(AVFormatContext *s, const char *url,
1107 xmlNodePtr adaptionset_node,
1108 xmlNodePtr mpd_baseurl_node,
1109 xmlNodePtr period_baseurl_node,
1110 xmlNodePtr period_segmenttemplate_node,
1111 xmlNodePtr period_segmentlist_node)
1114 xmlNodePtr fragment_template_node = NULL;
1115 xmlNodePtr content_component_node = NULL;
1116 xmlNodePtr adaptionset_baseurl_node = NULL;
1117 xmlNodePtr adaptionset_segmentlist_node = NULL;
1118 xmlNodePtr adaptionset_supplementalproperty_node = NULL;
1119 xmlNodePtr node = NULL;
1121 node = xmlFirstElementChild(adaptionset_node);
1123 if (!av_strcasecmp(node->name, (const char *)"SegmentTemplate")) {
1124 fragment_template_node = node;
1125 } else if (!av_strcasecmp(node->name, (const char *)"ContentComponent")) {
1126 content_component_node = node;
1127 } else if (!av_strcasecmp(node->name, (const char *)"BaseURL")) {
1128 adaptionset_baseurl_node = node;
1129 } else if (!av_strcasecmp(node->name, (const char *)"SegmentList")) {
1130 adaptionset_segmentlist_node = node;
1131 } else if (!av_strcasecmp(node->name, (const char *)"SupplementalProperty")) {
1132 adaptionset_supplementalproperty_node = node;
1133 } else if (!av_strcasecmp(node->name, (const char *)"Representation")) {
1134 ret = parse_manifest_representation(s, url, node,
1137 period_baseurl_node,
1138 period_segmenttemplate_node,
1139 period_segmentlist_node,
1140 fragment_template_node,
1141 content_component_node,
1142 adaptionset_baseurl_node,
1143 adaptionset_segmentlist_node,
1144 adaptionset_supplementalproperty_node);
1149 node = xmlNextElementSibling(node);
1154 static int parse_programinformation(AVFormatContext *s, xmlNodePtr node)
1156 xmlChar *val = NULL;
1158 node = xmlFirstElementChild(node);
1160 if (!av_strcasecmp(node->name, "Title")) {
1161 val = xmlNodeGetContent(node);
1163 av_dict_set(&s->metadata, "Title", val, 0);
1165 } else if (!av_strcasecmp(node->name, "Source")) {
1166 val = xmlNodeGetContent(node);
1168 av_dict_set(&s->metadata, "Source", val, 0);
1170 } else if (!av_strcasecmp(node->name, "Copyright")) {
1171 val = xmlNodeGetContent(node);
1173 av_dict_set(&s->metadata, "Copyright", val, 0);
1176 node = xmlNextElementSibling(node);
1183 static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
1185 DASHContext *c = s->priv_data;
1188 uint8_t *new_url = NULL;
1189 int64_t filesize = 0;
1190 char *buffer = NULL;
1191 AVDictionary *opts = NULL;
1193 xmlNodePtr root_element = NULL;
1194 xmlNodePtr node = NULL;
1195 xmlNodePtr period_node = NULL;
1196 xmlNodePtr tmp_node = NULL;
1197 xmlNodePtr mpd_baseurl_node = NULL;
1198 xmlNodePtr period_baseurl_node = NULL;
1199 xmlNodePtr period_segmenttemplate_node = NULL;
1200 xmlNodePtr period_segmentlist_node = NULL;
1201 xmlNodePtr adaptionset_node = NULL;
1202 xmlAttrPtr attr = NULL;
1204 uint32_t period_duration_sec = 0;
1205 uint32_t period_start_sec = 0;
1210 av_dict_copy(&opts, c->avio_opts, 0);
1211 ret = avio_open2(&in, url, AVIO_FLAG_READ, c->interrupt_callback, &opts);
1212 av_dict_free(&opts);
1217 if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, &new_url) >= 0) {
1218 c->base_url = av_strdup(new_url);
1220 c->base_url = av_strdup(url);
1223 filesize = avio_size(in);
1224 if (filesize <= 0) {
1225 filesize = 8 * 1024;
1228 buffer = av_mallocz(filesize);
1230 av_free(c->base_url);
1231 return AVERROR(ENOMEM);
1234 filesize = avio_read(in, buffer, filesize);
1235 if (filesize <= 0) {
1236 av_log(s, AV_LOG_ERROR, "Unable to read to offset '%s'\n", url);
1237 ret = AVERROR_INVALIDDATA;
1241 doc = xmlReadMemory(buffer, filesize, c->base_url, NULL, 0);
1242 root_element = xmlDocGetRootElement(doc);
1243 node = root_element;
1246 ret = AVERROR_INVALIDDATA;
1247 av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing root node\n", url);
1251 if (node->type != XML_ELEMENT_NODE ||
1252 av_strcasecmp(node->name, (const char *)"MPD")) {
1253 ret = AVERROR_INVALIDDATA;
1254 av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - wrong root node name[%s] type[%d]\n", url, node->name, (int)node->type);
1258 val = xmlGetProp(node, "type");
1260 av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing type attrib\n", url);
1261 ret = AVERROR_INVALIDDATA;
1264 if (!av_strcasecmp(val, (const char *)"dynamic"))
1268 attr = node->properties;
1270 val = xmlGetProp(node, attr->name);
1272 if (!av_strcasecmp(attr->name, (const char *)"availabilityStartTime")) {
1273 c->availability_start_time = get_utc_date_time_insec(s, (const char *)val);
1274 av_log(s, AV_LOG_TRACE, "c->availability_start_time = [%"PRId64"]\n", c->availability_start_time);
1275 } else if (!av_strcasecmp(attr->name, (const char *)"availabilityEndTime")) {
1276 c->availability_end_time = get_utc_date_time_insec(s, (const char *)val);
1277 av_log(s, AV_LOG_TRACE, "c->availability_end_time = [%"PRId64"]\n", c->availability_end_time);
1278 } else if (!av_strcasecmp(attr->name, (const char *)"publishTime")) {
1279 c->publish_time = get_utc_date_time_insec(s, (const char *)val);
1280 av_log(s, AV_LOG_TRACE, "c->publish_time = [%"PRId64"]\n", c->publish_time);
1281 } else if (!av_strcasecmp(attr->name, (const char *)"minimumUpdatePeriod")) {
1282 c->minimum_update_period = get_duration_insec(s, (const char *)val);
1283 av_log(s, AV_LOG_TRACE, "c->minimum_update_period = [%"PRId64"]\n", c->minimum_update_period);
1284 } else if (!av_strcasecmp(attr->name, (const char *)"timeShiftBufferDepth")) {
1285 c->time_shift_buffer_depth = get_duration_insec(s, (const char *)val);
1286 av_log(s, AV_LOG_TRACE, "c->time_shift_buffer_depth = [%"PRId64"]\n", c->time_shift_buffer_depth);
1287 } else if (!av_strcasecmp(attr->name, (const char *)"minBufferTime")) {
1288 c->min_buffer_time = get_duration_insec(s, (const char *)val);
1289 av_log(s, AV_LOG_TRACE, "c->min_buffer_time = [%"PRId64"]\n", c->min_buffer_time);
1290 } else if (!av_strcasecmp(attr->name, (const char *)"suggestedPresentationDelay")) {
1291 c->suggested_presentation_delay = get_duration_insec(s, (const char *)val);
1292 av_log(s, AV_LOG_TRACE, "c->suggested_presentation_delay = [%"PRId64"]\n", c->suggested_presentation_delay);
1293 } else if (!av_strcasecmp(attr->name, (const char *)"mediaPresentationDuration")) {
1294 c->media_presentation_duration = get_duration_insec(s, (const char *)val);
1295 av_log(s, AV_LOG_TRACE, "c->media_presentation_duration = [%"PRId64"]\n", c->media_presentation_duration);
1301 tmp_node = find_child_node_by_name(node, "BaseURL");
1303 mpd_baseurl_node = xmlCopyNode(tmp_node,1);
1305 mpd_baseurl_node = xmlNewNode(NULL, "BaseURL");
1308 // at now we can handle only one period, with the longest duration
1309 node = xmlFirstElementChild(node);
1311 if (!av_strcasecmp(node->name, (const char *)"Period")) {
1312 period_duration_sec = 0;
1313 period_start_sec = 0;
1314 attr = node->properties;
1316 val = xmlGetProp(node, attr->name);
1317 if (!av_strcasecmp(attr->name, (const char *)"duration")) {
1318 period_duration_sec = get_duration_insec(s, (const char *)val);
1319 } else if (!av_strcasecmp(attr->name, (const char *)"start")) {
1320 period_start_sec = get_duration_insec(s, (const char *)val);
1325 if ((period_duration_sec) >= (c->period_duration)) {
1327 c->period_duration = period_duration_sec;
1328 c->period_start = period_start_sec;
1329 if (c->period_start > 0)
1330 c->media_presentation_duration = c->period_duration;
1332 } else if (!av_strcasecmp(node->name, "ProgramInformation")) {
1333 parse_programinformation(s, node);
1335 node = xmlNextElementSibling(node);
1338 av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing Period node\n", url);
1339 ret = AVERROR_INVALIDDATA;
1343 adaptionset_node = xmlFirstElementChild(period_node);
1344 while (adaptionset_node) {
1345 if (!av_strcasecmp(adaptionset_node->name, (const char *)"BaseURL")) {
1346 period_baseurl_node = adaptionset_node;
1347 } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"SegmentTemplate")) {
1348 period_segmenttemplate_node = adaptionset_node;
1349 } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"SegmentList")) {
1350 period_segmentlist_node = adaptionset_node;
1351 } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"AdaptationSet")) {
1352 parse_manifest_adaptationset(s, url, adaptionset_node, mpd_baseurl_node, period_baseurl_node, period_segmenttemplate_node, period_segmentlist_node);
1354 adaptionset_node = xmlNextElementSibling(adaptionset_node);
1357 /*free the document */
1360 xmlFreeNode(mpd_baseurl_node);
1371 static int64_t calc_cur_seg_no(AVFormatContext *s, struct representation *pls)
1373 DASHContext *c = s->priv_data;
1375 int64_t start_time_offset = 0;
1378 if (pls->n_fragments) {
1379 av_log(s, AV_LOG_TRACE, "in n_fragments mode\n");
1380 num = pls->first_seq_no;
1381 } else if (pls->n_timelines) {
1382 av_log(s, AV_LOG_TRACE, "in n_timelines mode\n");
1383 start_time_offset = get_segment_start_time_based_on_timeline(pls, 0xFFFFFFFF) - 60 * pls->fragment_timescale; // 60 seconds before end
1384 num = calc_next_seg_no_from_timelines(pls, start_time_offset);
1386 num = pls->first_seq_no;
1388 num += pls->first_seq_no;
1389 } else if (pls->fragment_duration){
1390 av_log(s, AV_LOG_TRACE, "in fragment_duration mode fragment_timescale = %"PRId64", presentation_timeoffset = %"PRId64"\n", pls->fragment_timescale, pls->presentation_timeoffset);
1391 if (pls->presentation_timeoffset) {
1392 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;
1393 } else if (c->publish_time > 0 && !c->availability_start_time) {
1394 if (c->min_buffer_time) {
1395 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;
1397 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;
1400 num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time) - c->suggested_presentation_delay) * pls->fragment_timescale) / pls->fragment_duration;
1404 num = pls->first_seq_no;
1409 static int64_t calc_min_seg_no(AVFormatContext *s, struct representation *pls)
1411 DASHContext *c = s->priv_data;
1414 if (c->is_live && pls->fragment_duration) {
1415 av_log(s, AV_LOG_TRACE, "in live mode\n");
1416 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;
1418 num = pls->first_seq_no;
1423 static int64_t calc_max_seg_no(struct representation *pls, DASHContext *c)
1427 if (pls->n_fragments) {
1428 num = pls->first_seq_no + pls->n_fragments - 1;
1429 } else if (pls->n_timelines) {
1431 num = pls->first_seq_no + pls->n_timelines - 1;
1432 for (i = 0; i < pls->n_timelines; i++) {
1433 if (pls->timelines[i]->repeat == -1) {
1434 int length_of_each_segment = pls->timelines[i]->duration / pls->fragment_timescale;
1435 num = c->period_duration / length_of_each_segment;
1437 num += pls->timelines[i]->repeat;
1440 } else if (c->is_live && pls->fragment_duration) {
1441 num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time)) * pls->fragment_timescale) / pls->fragment_duration;
1442 } else if (pls->fragment_duration) {
1443 num = pls->first_seq_no + (c->media_presentation_duration * pls->fragment_timescale) / pls->fragment_duration;
1449 static void move_timelines(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
1451 if (rep_dest && rep_src ) {
1452 free_timelines_list(rep_dest);
1453 rep_dest->timelines = rep_src->timelines;
1454 rep_dest->n_timelines = rep_src->n_timelines;
1455 rep_dest->first_seq_no = rep_src->first_seq_no;
1456 rep_dest->last_seq_no = calc_max_seg_no(rep_dest, c);
1457 rep_src->timelines = NULL;
1458 rep_src->n_timelines = 0;
1459 rep_dest->cur_seq_no = rep_src->cur_seq_no;
1463 static void move_segments(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
1465 if (rep_dest && rep_src ) {
1466 free_fragment_list(rep_dest);
1467 if (rep_src->start_number > (rep_dest->start_number + rep_dest->n_fragments))
1468 rep_dest->cur_seq_no = 0;
1470 rep_dest->cur_seq_no += rep_src->start_number - rep_dest->start_number;
1471 rep_dest->fragments = rep_src->fragments;
1472 rep_dest->n_fragments = rep_src->n_fragments;
1473 rep_dest->parent = rep_src->parent;
1474 rep_dest->last_seq_no = calc_max_seg_no(rep_dest, c);
1475 rep_src->fragments = NULL;
1476 rep_src->n_fragments = 0;
1481 static int refresh_manifest(AVFormatContext *s)
1484 DASHContext *c = s->priv_data;
1485 // save current context
1486 int n_videos = c->n_videos;
1487 struct representation **videos = c->videos;
1488 int n_audios = c->n_audios;
1489 struct representation **audios = c->audios;
1490 int n_subtitles = c->n_subtitles;
1491 struct representation **subtitles = c->subtitles;
1492 char *base_url = c->base_url;
1500 c->subtitles = NULL;
1501 ret = parse_manifest(s, s->url, NULL);
1505 if (c->n_videos != n_videos) {
1506 av_log(c, AV_LOG_ERROR,
1507 "new manifest has mismatched no. of video representations, %d -> %d\n",
1508 n_videos, c->n_videos);
1509 return AVERROR_INVALIDDATA;
1511 if (c->n_audios != n_audios) {
1512 av_log(c, AV_LOG_ERROR,
1513 "new manifest has mismatched no. of audio representations, %d -> %d\n",
1514 n_audios, c->n_audios);
1515 return AVERROR_INVALIDDATA;
1517 if (c->n_subtitles != n_subtitles) {
1518 av_log(c, AV_LOG_ERROR,
1519 "new manifest has mismatched no. of subtitles representations, %d -> %d\n",
1520 n_subtitles, c->n_subtitles);
1521 return AVERROR_INVALIDDATA;
1524 for (i = 0; i < n_videos; i++) {
1525 struct representation *cur_video = videos[i];
1526 struct representation *ccur_video = c->videos[i];
1527 if (cur_video->timelines) {
1528 // calc current time
1529 int64_t currentTime = get_segment_start_time_based_on_timeline(cur_video, cur_video->cur_seq_no) / cur_video->fragment_timescale;
1531 ccur_video->cur_seq_no = calc_next_seg_no_from_timelines(ccur_video, currentTime * cur_video->fragment_timescale - 1);
1532 if (ccur_video->cur_seq_no >= 0) {
1533 move_timelines(ccur_video, cur_video, c);
1536 if (cur_video->fragments) {
1537 move_segments(ccur_video, cur_video, c);
1540 for (i = 0; i < n_audios; i++) {
1541 struct representation *cur_audio = audios[i];
1542 struct representation *ccur_audio = c->audios[i];
1543 if (cur_audio->timelines) {
1544 // calc current time
1545 int64_t currentTime = get_segment_start_time_based_on_timeline(cur_audio, cur_audio->cur_seq_no) / cur_audio->fragment_timescale;
1547 ccur_audio->cur_seq_no = calc_next_seg_no_from_timelines(ccur_audio, currentTime * cur_audio->fragment_timescale - 1);
1548 if (ccur_audio->cur_seq_no >= 0) {
1549 move_timelines(ccur_audio, cur_audio, c);
1552 if (cur_audio->fragments) {
1553 move_segments(ccur_audio, cur_audio, c);
1562 c->base_url = base_url;
1565 free_subtitle_list(c);
1571 c->n_subtitles = n_subtitles;
1572 c->subtitles = subtitles;
1573 c->n_audios = n_audios;
1575 c->n_videos = n_videos;
1580 static struct fragment *get_current_fragment(struct representation *pls)
1582 int64_t min_seq_no = 0;
1583 int64_t max_seq_no = 0;
1584 struct fragment *seg = NULL;
1585 struct fragment *seg_ptr = NULL;
1586 DASHContext *c = pls->parent->priv_data;
1588 while (( !ff_check_interrupt(c->interrupt_callback)&& pls->n_fragments > 0)) {
1589 if (pls->cur_seq_no < pls->n_fragments) {
1590 seg_ptr = pls->fragments[pls->cur_seq_no];
1591 seg = av_mallocz(sizeof(struct fragment));
1595 seg->url = av_strdup(seg_ptr->url);
1600 seg->size = seg_ptr->size;
1601 seg->url_offset = seg_ptr->url_offset;
1603 } else if (c->is_live) {
1604 refresh_manifest(pls->parent);
1610 min_seq_no = calc_min_seg_no(pls->parent, pls);
1611 max_seq_no = calc_max_seg_no(pls, c);
1613 if (pls->timelines || pls->fragments) {
1614 refresh_manifest(pls->parent);
1616 if (pls->cur_seq_no <= min_seq_no) {
1617 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);
1618 pls->cur_seq_no = calc_cur_seg_no(pls->parent, pls);
1619 } else if (pls->cur_seq_no > max_seq_no) {
1620 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);
1622 seg = av_mallocz(sizeof(struct fragment));
1626 } else if (pls->cur_seq_no <= pls->last_seq_no) {
1627 seg = av_mallocz(sizeof(struct fragment));
1633 char *tmpfilename= av_mallocz(c->max_url_size);
1637 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));
1638 seg->url = av_strireplace(pls->url_template, pls->url_template, tmpfilename);
1640 av_log(pls->parent, AV_LOG_WARNING, "Unable to resolve template url '%s', try to use origin template\n", pls->url_template);
1641 seg->url = av_strdup(pls->url_template);
1643 av_log(pls->parent, AV_LOG_ERROR, "Cannot resolve template url '%s'\n", pls->url_template);
1644 av_free(tmpfilename);
1648 av_free(tmpfilename);
1655 static int read_from_url(struct representation *pls, struct fragment *seg,
1656 uint8_t *buf, int buf_size)
1660 /* limit read if the fragment was only a part of a file */
1662 buf_size = FFMIN(buf_size, pls->cur_seg_size - pls->cur_seg_offset);
1664 ret = avio_read(pls->input, buf, buf_size);
1666 pls->cur_seg_offset += ret;
1671 static int open_input(DASHContext *c, struct representation *pls, struct fragment *seg)
1673 AVDictionary *opts = NULL;
1677 url = av_mallocz(c->max_url_size);
1679 ret = AVERROR(ENOMEM);
1683 if (seg->size >= 0) {
1684 /* try to restrict the HTTP request to the part we want
1685 * (if this is in fact a HTTP request) */
1686 av_dict_set_int(&opts, "offset", seg->url_offset, 0);
1687 av_dict_set_int(&opts, "end_offset", seg->url_offset + seg->size, 0);
1690 ff_make_absolute_url(url, c->max_url_size, c->base_url, seg->url);
1691 av_log(pls->parent, AV_LOG_VERBOSE, "DASH request for url '%s', offset %"PRId64", playlist %d\n",
1692 url, seg->url_offset, pls->rep_idx);
1693 ret = open_url(pls->parent, &pls->input, url, c->avio_opts, opts, NULL);
1697 av_dict_free(&opts);
1698 pls->cur_seg_offset = 0;
1699 pls->cur_seg_size = seg->size;
1703 static int update_init_section(struct representation *pls)
1705 static const int max_init_section_size = 1024 * 1024;
1706 DASHContext *c = pls->parent->priv_data;
1711 if (!pls->init_section || pls->init_sec_buf)
1714 ret = open_input(c, pls, pls->init_section);
1716 av_log(pls->parent, AV_LOG_WARNING,
1717 "Failed to open an initialization section in playlist %d\n",
1722 if (pls->init_section->size >= 0)
1723 sec_size = pls->init_section->size;
1724 else if ((urlsize = avio_size(pls->input)) >= 0)
1727 sec_size = max_init_section_size;
1729 av_log(pls->parent, AV_LOG_DEBUG,
1730 "Downloading an initialization section of size %"PRId64"\n",
1733 sec_size = FFMIN(sec_size, max_init_section_size);
1735 av_fast_malloc(&pls->init_sec_buf, &pls->init_sec_buf_size, sec_size);
1737 ret = read_from_url(pls, pls->init_section, pls->init_sec_buf,
1738 pls->init_sec_buf_size);
1739 ff_format_io_close(pls->parent, &pls->input);
1744 pls->init_sec_data_len = ret;
1745 pls->init_sec_buf_read_offset = 0;
1750 static int64_t seek_data(void *opaque, int64_t offset, int whence)
1752 struct representation *v = opaque;
1753 if (v->n_fragments && !v->init_sec_data_len) {
1754 return avio_seek(v->input, offset, whence);
1757 return AVERROR(ENOSYS);
1760 static int read_data(void *opaque, uint8_t *buf, int buf_size)
1763 struct representation *v = opaque;
1764 DASHContext *c = v->parent->priv_data;
1768 free_fragment(&v->cur_seg);
1769 v->cur_seg = get_current_fragment(v);
1775 /* load/update Media Initialization Section, if any */
1776 ret = update_init_section(v);
1780 ret = open_input(c, v, v->cur_seg);
1782 if (ff_check_interrupt(c->interrupt_callback)) {
1786 av_log(v->parent, AV_LOG_WARNING, "Failed to open fragment of playlist %d\n", v->rep_idx);
1792 if (v->init_sec_buf_read_offset < v->init_sec_data_len) {
1793 /* Push init section out first before first actual fragment */
1794 int copy_size = FFMIN(v->init_sec_data_len - v->init_sec_buf_read_offset, buf_size);
1795 memcpy(buf, v->init_sec_buf, copy_size);
1796 v->init_sec_buf_read_offset += copy_size;
1801 /* check the v->cur_seg, if it is null, get current and double check if the new v->cur_seg*/
1803 v->cur_seg = get_current_fragment(v);
1809 ret = read_from_url(v, v->cur_seg, buf, buf_size);
1813 if (c->is_live || v->cur_seq_no < v->last_seq_no) {
1814 if (!v->is_restart_needed)
1816 v->is_restart_needed = 1;
1823 static int save_avio_options(AVFormatContext *s)
1825 DASHContext *c = s->priv_data;
1826 const char *opts[] = {
1827 "headers", "user_agent", "cookies", "http_proxy", "referer", "rw_timeout", "icy", NULL };
1828 const char **opt = opts;
1829 uint8_t *buf = NULL;
1833 if (av_opt_get(s->pb, *opt, AV_OPT_SEARCH_CHILDREN, &buf) >= 0) {
1834 if (buf[0] != '\0') {
1835 ret = av_dict_set(&c->avio_opts, *opt, buf, AV_DICT_DONT_STRDUP_VAL);
1850 static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
1851 int flags, AVDictionary **opts)
1853 av_log(s, AV_LOG_ERROR,
1854 "A DASH playlist item '%s' referred to an external file '%s'. "
1855 "Opening this file was forbidden for security reasons\n",
1857 return AVERROR(EPERM);
1860 static void close_demux_for_component(struct representation *pls)
1862 /* note: the internal buffer could have changed */
1863 av_freep(&pls->pb.buffer);
1864 memset(&pls->pb, 0x00, sizeof(AVIOContext));
1865 pls->ctx->pb = NULL;
1866 avformat_close_input(&pls->ctx);
1870 static int reopen_demux_for_component(AVFormatContext *s, struct representation *pls)
1872 DASHContext *c = s->priv_data;
1873 ff_const59 AVInputFormat *in_fmt = NULL;
1874 AVDictionary *in_fmt_opts = NULL;
1875 uint8_t *avio_ctx_buffer = NULL;
1879 close_demux_for_component(pls);
1882 if (ff_check_interrupt(&s->interrupt_callback)) {
1887 if (!(pls->ctx = avformat_alloc_context())) {
1888 ret = AVERROR(ENOMEM);
1892 avio_ctx_buffer = av_malloc(INITIAL_BUFFER_SIZE);
1893 if (!avio_ctx_buffer ) {
1894 ret = AVERROR(ENOMEM);
1895 avformat_free_context(pls->ctx);
1900 ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, NULL);
1902 ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, seek_data);
1904 pls->pb.seekable = 0;
1906 if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0)
1909 pls->ctx->flags = AVFMT_FLAG_CUSTOM_IO;
1910 pls->ctx->probesize = s->probesize > 0 ? s->probesize : 1024 * 4;
1911 pls->ctx->max_analyze_duration = s->max_analyze_duration > 0 ? s->max_analyze_duration : 4 * AV_TIME_BASE;
1912 ret = av_probe_input_buffer(&pls->pb, &in_fmt, "", NULL, 0, 0);
1914 av_log(s, AV_LOG_ERROR, "Error when loading first fragment, playlist %d\n", (int)pls->rep_idx);
1915 avformat_free_context(pls->ctx);
1920 pls->ctx->pb = &pls->pb;
1921 pls->ctx->io_open = nested_io_open;
1923 // provide additional information from mpd if available
1924 ret = avformat_open_input(&pls->ctx, "", in_fmt, &in_fmt_opts); //pls->init_section->url
1925 av_dict_free(&in_fmt_opts);
1928 if (pls->n_fragments) {
1929 #if FF_API_R_FRAME_RATE
1930 if (pls->framerate.den) {
1931 for (i = 0; i < pls->ctx->nb_streams; i++)
1932 pls->ctx->streams[i]->r_frame_rate = pls->framerate;
1935 ret = avformat_find_stream_info(pls->ctx, NULL);
1944 static int open_demux_for_component(AVFormatContext *s, struct representation *pls)
1950 pls->cur_seq_no = calc_cur_seg_no(s, pls);
1952 if (!pls->last_seq_no) {
1953 pls->last_seq_no = calc_max_seg_no(pls, s->priv_data);
1956 ret = reopen_demux_for_component(s, pls);
1960 for (i = 0; i < pls->ctx->nb_streams; i++) {
1961 AVStream *st = avformat_new_stream(s, NULL);
1962 AVStream *ist = pls->ctx->streams[i];
1964 ret = AVERROR(ENOMEM);
1968 avcodec_parameters_copy(st->codecpar, ist->codecpar);
1969 avpriv_set_pts_info(st, ist->pts_wrap_bits, ist->time_base.num, ist->time_base.den);
1977 static int is_common_init_section_exist(struct representation **pls, int n_pls)
1979 struct fragment *first_init_section = pls[0]->init_section;
1981 int64_t url_offset = -1;
1985 if (first_init_section == NULL || n_pls == 0)
1988 url = first_init_section->url;
1989 url_offset = first_init_section->url_offset;
1990 size = pls[0]->init_section->size;
1991 for (i=0;i<n_pls;i++) {
1992 if (av_strcasecmp(pls[i]->init_section->url,url) || pls[i]->init_section->url_offset != url_offset || pls[i]->init_section->size != size) {
1999 static int copy_init_section(struct representation *rep_dest, struct representation *rep_src)
2001 rep_dest->init_sec_buf = av_mallocz(rep_src->init_sec_buf_size);
2002 if (!rep_dest->init_sec_buf) {
2003 av_log(rep_dest->ctx, AV_LOG_WARNING, "Cannot alloc memory for init_sec_buf\n");
2004 return AVERROR(ENOMEM);
2006 memcpy(rep_dest->init_sec_buf, rep_src->init_sec_buf, rep_src->init_sec_data_len);
2007 rep_dest->init_sec_buf_size = rep_src->init_sec_buf_size;
2008 rep_dest->init_sec_data_len = rep_src->init_sec_data_len;
2009 rep_dest->cur_timestamp = rep_src->cur_timestamp;
2015 static int dash_read_header(AVFormatContext *s)
2017 DASHContext *c = s->priv_data;
2018 struct representation *rep;
2020 int stream_index = 0;
2023 c->interrupt_callback = &s->interrupt_callback;
2025 if ((ret = save_avio_options(s)) < 0)
2028 if ((ret = parse_manifest(s, s->url, s->pb)) < 0)
2031 /* If this isn't a live stream, fill the total duration of the
2034 s->duration = (int64_t) c->media_presentation_duration * AV_TIME_BASE;
2036 av_dict_set(&c->avio_opts, "seekable", "0", 0);
2040 c->is_init_section_common_video = is_common_init_section_exist(c->videos, c->n_videos);
2042 /* Open the demuxer for video and audio components if available */
2043 for (i = 0; i < c->n_videos; i++) {
2045 if (i > 0 && c->is_init_section_common_video) {
2046 ret = copy_init_section(rep, c->videos[0]);
2050 ret = open_demux_for_component(s, rep);
2054 rep->stream_index = stream_index;
2059 c->is_init_section_common_audio = is_common_init_section_exist(c->audios, c->n_audios);
2061 for (i = 0; i < c->n_audios; i++) {
2063 if (i > 0 && c->is_init_section_common_audio) {
2064 ret = copy_init_section(rep, c->audios[0]);
2068 ret = open_demux_for_component(s, rep);
2072 rep->stream_index = stream_index;
2077 c->is_init_section_common_audio = is_common_init_section_exist(c->subtitles, c->n_subtitles);
2079 for (i = 0; i < c->n_subtitles; i++) {
2080 rep = c->subtitles[i];
2081 if (i > 0 && c->is_init_section_common_audio) {
2082 ret = copy_init_section(rep, c->subtitles[0]);
2086 ret = open_demux_for_component(s, rep);
2090 rep->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++) {
2109 av_program_add_stream_index(s, 0, rep->stream_index);
2110 rep->assoc_stream = s->streams[rep->stream_index];
2111 if (rep->bandwidth > 0)
2112 av_dict_set_int(&rep->assoc_stream->metadata, "variant_bitrate", rep->bandwidth, 0);
2114 av_dict_set(&rep->assoc_stream->metadata, "id", rep->id, 0);
2116 for (i = 0; i < c->n_audios; i++) {
2118 av_program_add_stream_index(s, 0, rep->stream_index);
2119 rep->assoc_stream = s->streams[rep->stream_index];
2120 if (rep->bandwidth > 0)
2121 av_dict_set_int(&rep->assoc_stream->metadata, "variant_bitrate", rep->bandwidth, 0);
2123 av_dict_set(&rep->assoc_stream->metadata, "id", rep->id, 0);
2125 for (i = 0; i < c->n_subtitles; i++) {
2126 rep = c->subtitles[i];
2127 av_program_add_stream_index(s, 0, rep->stream_index);
2128 rep->assoc_stream = s->streams[rep->stream_index];
2130 av_dict_set(&rep->assoc_stream->metadata, "id", rep->id, 0);
2139 static void recheck_discard_flags(AVFormatContext *s, struct representation **p, int n)
2143 for (i = 0; i < n; i++) {
2144 struct representation *pls = p[i];
2145 int needed = !pls->assoc_stream || pls->assoc_stream->discard < AVDISCARD_ALL;
2147 if (needed && !pls->ctx) {
2148 pls->cur_seg_offset = 0;
2149 pls->init_sec_buf_read_offset = 0;
2151 for (j = 0; j < n; j++) {
2152 pls->cur_seq_no = FFMAX(pls->cur_seq_no, p[j]->cur_seq_no);
2154 reopen_demux_for_component(s, pls);
2155 av_log(s, AV_LOG_INFO, "Now receiving stream_index %d\n", pls->stream_index);
2156 } else if (!needed && pls->ctx) {
2157 close_demux_for_component(pls);
2158 ff_format_io_close(pls->parent, &pls->input);
2159 av_log(s, AV_LOG_INFO, "No longer receiving stream_index %d\n", pls->stream_index);
2164 static int dash_read_packet(AVFormatContext *s, AVPacket *pkt)
2166 DASHContext *c = s->priv_data;
2169 struct representation *cur = NULL;
2170 struct representation *rep = NULL;
2172 recheck_discard_flags(s, c->videos, c->n_videos);
2173 recheck_discard_flags(s, c->audios, c->n_audios);
2174 recheck_discard_flags(s, c->subtitles, c->n_subtitles);
2176 for (i = 0; i < c->n_videos; i++) {
2180 if (!cur || rep->cur_timestamp < mints) {
2182 mints = rep->cur_timestamp;
2185 for (i = 0; i < c->n_audios; i++) {
2189 if (!cur || rep->cur_timestamp < mints) {
2191 mints = rep->cur_timestamp;
2195 for (i = 0; i < c->n_subtitles; i++) {
2196 rep = c->subtitles[i];
2199 if (!cur || rep->cur_timestamp < mints) {
2201 mints = rep->cur_timestamp;
2206 return AVERROR_INVALIDDATA;
2208 while (!ff_check_interrupt(c->interrupt_callback) && !ret) {
2209 ret = av_read_frame(cur->ctx, pkt);
2211 /* If we got a packet, return it */
2212 cur->cur_timestamp = av_rescale(pkt->pts, (int64_t)cur->ctx->streams[0]->time_base.num * 90000, cur->ctx->streams[0]->time_base.den);
2213 pkt->stream_index = cur->stream_index;
2216 if (cur->is_restart_needed) {
2217 cur->cur_seg_offset = 0;
2218 cur->init_sec_buf_read_offset = 0;
2219 ff_format_io_close(cur->parent, &cur->input);
2220 ret = reopen_demux_for_component(s, cur);
2221 cur->is_restart_needed = 0;
2227 static int dash_close(AVFormatContext *s)
2229 DASHContext *c = s->priv_data;
2232 av_dict_free(&c->avio_opts);
2233 av_freep(&c->base_url);
2237 static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos_msec, int flags, int dry_run)
2242 int64_t duration = 0;
2244 av_log(pls->parent, AV_LOG_VERBOSE, "DASH seek pos[%"PRId64"ms], playlist %d%s\n",
2245 seek_pos_msec, pls->rep_idx, dry_run ? " (dry)" : "");
2247 // single fragment mode
2248 if (pls->n_fragments == 1) {
2249 pls->cur_timestamp = 0;
2250 pls->cur_seg_offset = 0;
2253 ff_read_frame_flush(pls->ctx);
2254 return av_seek_frame(pls->ctx, -1, seek_pos_msec * 1000, flags);
2257 ff_format_io_close(pls->parent, &pls->input);
2259 // find the nearest fragment
2260 if (pls->n_timelines > 0 && pls->fragment_timescale > 0) {
2261 int64_t num = pls->first_seq_no;
2262 av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline start n_timelines[%d] "
2263 "last_seq_no[%"PRId64"], playlist %d.\n",
2264 (int)pls->n_timelines, (int64_t)pls->last_seq_no, (int)pls->rep_idx);
2265 for (i = 0; i < pls->n_timelines; i++) {
2266 if (pls->timelines[i]->starttime > 0) {
2267 duration = pls->timelines[i]->starttime;
2269 duration += pls->timelines[i]->duration;
2270 if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
2273 for (j = 0; j < pls->timelines[i]->repeat; j++) {
2274 duration += pls->timelines[i]->duration;
2276 if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
2284 pls->cur_seq_no = num > pls->last_seq_no ? pls->last_seq_no : num;
2285 av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline end cur_seq_no[%"PRId64"], playlist %d.\n",
2286 (int64_t)pls->cur_seq_no, (int)pls->rep_idx);
2287 } else if (pls->fragment_duration > 0) {
2288 pls->cur_seq_no = pls->first_seq_no + ((seek_pos_msec * pls->fragment_timescale) / pls->fragment_duration) / 1000;
2290 av_log(pls->parent, AV_LOG_ERROR, "dash_seek missing timeline or fragment_duration\n");
2291 pls->cur_seq_no = pls->first_seq_no;
2293 pls->cur_timestamp = 0;
2294 pls->cur_seg_offset = 0;
2295 pls->init_sec_buf_read_offset = 0;
2296 ret = dry_run ? 0 : reopen_demux_for_component(s, pls);
2301 static int dash_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
2304 DASHContext *c = s->priv_data;
2305 int64_t seek_pos_msec = av_rescale_rnd(timestamp, 1000,
2306 s->streams[stream_index]->time_base.den,
2307 flags & AVSEEK_FLAG_BACKWARD ?
2308 AV_ROUND_DOWN : AV_ROUND_UP);
2309 if ((flags & AVSEEK_FLAG_BYTE) || c->is_live)
2310 return AVERROR(ENOSYS);
2312 /* Seek in discarded streams with dry_run=1 to avoid reopening them */
2313 for (i = 0; i < c->n_videos; i++) {
2315 ret = dash_seek(s, c->videos[i], seek_pos_msec, flags, !c->videos[i]->ctx);
2317 for (i = 0; i < c->n_audios; i++) {
2319 ret = dash_seek(s, c->audios[i], seek_pos_msec, flags, !c->audios[i]->ctx);
2321 for (i = 0; i < c->n_subtitles; i++) {
2323 ret = dash_seek(s, c->subtitles[i], seek_pos_msec, flags, !c->subtitles[i]->ctx);
2329 static int dash_probe(const AVProbeData *p)
2331 if (!av_stristr(p->buf, "<MPD"))
2334 if (av_stristr(p->buf, "dash:profile:isoff-on-demand:2011") ||
2335 av_stristr(p->buf, "dash:profile:isoff-live:2011") ||
2336 av_stristr(p->buf, "dash:profile:isoff-live:2012") ||
2337 av_stristr(p->buf, "dash:profile:isoff-main:2011") ||
2338 av_stristr(p->buf, "3GPP:PSS:profile:DASH1")) {
2339 return AVPROBE_SCORE_MAX;
2341 if (av_stristr(p->buf, "dash:profile")) {
2342 return AVPROBE_SCORE_MAX;
2348 #define OFFSET(x) offsetof(DASHContext, x)
2349 #define FLAGS AV_OPT_FLAG_DECODING_PARAM
2350 static const AVOption dash_options[] = {
2351 {"allowed_extensions", "List of file extensions that dash is allowed to access",
2352 OFFSET(allowed_extensions), AV_OPT_TYPE_STRING,
2353 {.str = "aac,m4a,m4s,m4v,mov,mp4,webm,ts"},
2354 INT_MIN, INT_MAX, FLAGS},
2358 static const AVClass dash_class = {
2359 .class_name = "dash",
2360 .item_name = av_default_item_name,
2361 .option = dash_options,
2362 .version = LIBAVUTIL_VERSION_INT,
2365 AVInputFormat ff_dash_demuxer = {
2367 .long_name = NULL_IF_CONFIG_SMALL("Dynamic Adaptive Streaming over HTTP"),
2368 .priv_class = &dash_class,
2369 .priv_data_size = sizeof(DASHContext),
2370 .read_probe = dash_probe,
2371 .read_header = dash_read_header,
2372 .read_packet = dash_read_packet,
2373 .read_close = dash_close,
2374 .read_seek = dash_read_seek,
2375 .flags = AVFMT_NO_BYTE_SEEK,