FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dashdec.c
Go to the documentation of this file.
1 /*
2  * Dynamic Adaptive Streaming over HTTP demux
3  * Copyright (c) 2017 samsamsam@o2.pl based on HLS demux
4  * Copyright (c) 2017 Steven Liu
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #include <libxml/parser.h>
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/time.h"
26 #include "libavutil/parseutils.h"
27 #include "internal.h"
28 #include "avio_internal.h"
29 #include "dash.h"
30 
31 #define INITIAL_BUFFER_SIZE 32768
32 
33 struct fragment {
34  int64_t url_offset;
35  int64_t size;
36  char *url;
37 };
38 
39 /*
40  * reference to : ISO_IEC_23009-1-DASH-2012
41  * Section: 5.3.9.6.2
42  * Table: Table 17 — Semantics of SegmentTimeline element
43  * */
44 struct timeline {
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)).
56  * */
57  int64_t starttime;
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).
62  * */
63  int64_t repeat;
64  /* duration: Element or Attribute Name
65  * specifies the Segment duration, in units of the value of the @timescale.
66  * */
67  int64_t duration;
68 };
69 
70 /*
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.
74  */
76  char *url_template;
82  int rep_idx;
83  int rep_count;
85 
87 
89  struct fragment **fragments; /* VOD list of fragment for profile */
90 
92  struct timeline **timelines;
93 
94  int64_t first_seq_no;
95  int64_t last_seq_no;
96  int64_t start_number; /* used in case when we have dynamic list of segment to know which segments are new one*/
97 
100 
102 
103  int64_t cur_seq_no;
104  int64_t cur_seg_offset;
105  int64_t cur_seg_size;
106  struct fragment *cur_seg;
107 
108  /* Currently active Media Initialization Section */
114  int64_t cur_timestamp;
116 };
117 
118 typedef struct DASHContext {
119  const AVClass *class;
120  char *base_url;
123 
124  /* MediaPresentationDescription Attribute */
128  uint64_t publish_time;
131  uint64_t min_buffer_time;
132 
133  /* Period Attribute */
134  uint64_t period_duration;
135  uint64_t period_start;
136 
137  int is_live;
139  char *user_agent; ///< holds HTTP user agent set as an AVOption to the HTTP protocol context
140  char *cookies; ///< holds HTTP cookie values set in either the initial response or as an AVOption to the HTTP protocol context
141  char *headers; ///< holds HTTP headers set as an AVOption to the HTTP protocol context
144 } DASHContext;
145 
146 static uint64_t get_current_time_in_sec(void)
147 {
148  return av_gettime() / 1000000;
149 }
150 
151 static uint64_t get_utc_date_time_insec(AVFormatContext *s, const char *datetime)
152 {
153  struct tm timeinfo;
154  int year = 0;
155  int month = 0;
156  int day = 0;
157  int hour = 0;
158  int minute = 0;
159  int ret = 0;
160  float second = 0.0;
161 
162  /* ISO-8601 date parser */
163  if (!datetime)
164  return 0;
165 
166  ret = sscanf(datetime, "%d-%d-%dT%d:%d:%fZ", &year, &month, &day, &hour, &minute, &second);
167  /* year, month, day, hour, minute, second 6 arguments */
168  if (ret != 6) {
169  av_log(s, AV_LOG_WARNING, "get_utc_date_time_insec get a wrong time format\n");
170  }
171  timeinfo.tm_year = year - 1900;
172  timeinfo.tm_mon = month - 1;
173  timeinfo.tm_mday = day;
174  timeinfo.tm_hour = hour;
175  timeinfo.tm_min = minute;
176  timeinfo.tm_sec = (int)second;
177 
178  return av_timegm(&timeinfo);
179 }
180 
181 static uint32_t get_duration_insec(AVFormatContext *s, const char *duration)
182 {
183  /* ISO-8601 duration parser */
184  uint32_t days = 0;
185  uint32_t hours = 0;
186  uint32_t mins = 0;
187  uint32_t secs = 0;
188  int size = 0;
189  float value = 0;
190  char type = '\0';
191  const char *ptr = duration;
192 
193  while (*ptr) {
194  if (*ptr == 'P' || *ptr == 'T') {
195  ptr++;
196  continue;
197  }
198 
199  if (sscanf(ptr, "%f%c%n", &value, &type, &size) != 2) {
200  av_log(s, AV_LOG_WARNING, "get_duration_insec get a wrong time format\n");
201  return 0; /* parser error */
202  }
203  switch (type) {
204  case 'D':
205  days = (uint32_t)value;
206  break;
207  case 'H':
208  hours = (uint32_t)value;
209  break;
210  case 'M':
211  mins = (uint32_t)value;
212  break;
213  case 'S':
214  secs = (uint32_t)value;
215  break;
216  default:
217  // handle invalid type
218  break;
219  }
220  ptr += size;
221  }
222  return ((days * 24 + hours) * 60 + mins) * 60 + secs;
223 }
224 
225 static int64_t get_segment_start_time_based_on_timeline(struct representation *pls, int64_t cur_seq_no)
226 {
227  int64_t start_time = 0;
228  int64_t i = 0;
229  int64_t j = 0;
230  int64_t num = 0;
231 
232  if (pls->n_timelines) {
233  for (i = 0; i < pls->n_timelines; i++) {
234  if (pls->timelines[i]->starttime > 0) {
235  start_time = pls->timelines[i]->starttime;
236  }
237  if (num == cur_seq_no)
238  goto finish;
239 
240  start_time += pls->timelines[i]->duration;
241  for (j = 0; j < pls->timelines[i]->repeat; j++) {
242  num++;
243  if (num == cur_seq_no)
244  goto finish;
245  start_time += pls->timelines[i]->duration;
246  }
247  num++;
248  }
249  }
250 finish:
251  return start_time;
252 }
253 
254 static int64_t calc_next_seg_no_from_timelines(struct representation *pls, int64_t cur_time)
255 {
256  int64_t i = 0;
257  int64_t j = 0;
258  int64_t num = 0;
259  int64_t start_time = 0;
260 
261  for (i = 0; i < pls->n_timelines; i++) {
262  if (pls->timelines[i]->starttime > 0) {
263  start_time = pls->timelines[i]->starttime;
264  }
265  if (start_time > cur_time)
266  goto finish;
267 
268  start_time += pls->timelines[i]->duration;
269  for (j = 0; j < pls->timelines[i]->repeat; j++) {
270  num++;
271  if (start_time > cur_time)
272  goto finish;
273  start_time += pls->timelines[i]->duration;
274  }
275  num++;
276  }
277 
278  return -1;
279 
280 finish:
281  return num;
282 }
283 
284 static void free_fragment(struct fragment **seg)
285 {
286  if (!(*seg)) {
287  return;
288  }
289  av_freep(&(*seg)->url);
290  av_freep(seg);
291 }
292 
293 static void free_fragment_list(struct representation *pls)
294 {
295  int i;
296 
297  for (i = 0; i < pls->n_fragments; i++) {
298  free_fragment(&pls->fragments[i]);
299  }
300  av_freep(&pls->fragments);
301  pls->n_fragments = 0;
302 }
303 
304 static void free_timelines_list(struct representation *pls)
305 {
306  int i;
307 
308  for (i = 0; i < pls->n_timelines; i++) {
309  av_freep(&pls->timelines[i]);
310  }
311  av_freep(&pls->timelines);
312  pls->n_timelines = 0;
313 }
314 
315 static void free_representation(struct representation *pls)
316 {
317  free_fragment_list(pls);
318  free_timelines_list(pls);
319  free_fragment(&pls->cur_seg);
321  av_freep(&pls->init_sec_buf);
322  av_freep(&pls->pb.buffer);
323  if (pls->input)
324  ff_format_io_close(pls->parent, &pls->input);
325  if (pls->ctx) {
326  pls->ctx->pb = NULL;
327  avformat_close_input(&pls->ctx);
328  }
329 
330  av_freep(&pls->url_template);
331  av_freep(pls);
332 }
333 
335 {
336  // broker prior HTTP options that should be consistent across requests
337  av_dict_set(&opts, "user-agent", c->user_agent, 0);
338  av_dict_set(&opts, "cookies", c->cookies, 0);
339  av_dict_set(&opts, "headers", c->headers, 0);
340  if (c->is_live) {
341  av_dict_set(&opts, "seekable", "0", 0);
342  }
343 }
344 static void update_options(char **dest, const char *name, void *src)
345 {
346  av_freep(dest);
347  av_opt_get(src, name, AV_OPT_SEARCH_CHILDREN, (uint8_t**)dest);
348  if (*dest)
349  av_freep(dest);
350 }
351 
352 static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
353  AVDictionary *opts, AVDictionary *opts2, int *is_http)
354 {
355  DASHContext *c = s->priv_data;
356  AVDictionary *tmp = NULL;
357  const char *proto_name = NULL;
358  int ret;
359 
360  av_dict_copy(&tmp, opts, 0);
361  av_dict_copy(&tmp, opts2, 0);
362 
363  if (av_strstart(url, "crypto", NULL)) {
364  if (url[6] == '+' || url[6] == ':')
365  proto_name = avio_find_protocol_name(url + 7);
366  }
367 
368  if (!proto_name)
369  proto_name = avio_find_protocol_name(url);
370 
371  if (!proto_name)
372  return AVERROR_INVALIDDATA;
373 
374  // only http(s) & file are allowed
375  if (av_strstart(proto_name, "file", NULL)) {
376  if (strcmp(c->allowed_extensions, "ALL") && !av_match_ext(url, c->allowed_extensions)) {
377  av_log(s, AV_LOG_ERROR,
378  "Filename extension of \'%s\' is not a common multimedia extension, blocked for security reasons.\n"
379  "If you wish to override this adjust allowed_extensions, you can set it to \'ALL\' to allow all\n",
380  url);
381  return AVERROR_INVALIDDATA;
382  }
383  } else if (av_strstart(proto_name, "http", NULL)) {
384  ;
385  } else
386  return AVERROR_INVALIDDATA;
387 
388  if (!strncmp(proto_name, url, strlen(proto_name)) && url[strlen(proto_name)] == ':')
389  ;
390  else if (av_strstart(url, "crypto", NULL) && !strncmp(proto_name, url + 7, strlen(proto_name)) && url[7 + strlen(proto_name)] == ':')
391  ;
392  else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5))
393  return AVERROR_INVALIDDATA;
394 
395  ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp);
396  if (ret >= 0) {
397  // update cookies on http response with setcookies.
398  char *new_cookies = NULL;
399 
400  if (!(s->flags & AVFMT_FLAG_CUSTOM_IO))
401  av_opt_get(*pb, "cookies", AV_OPT_SEARCH_CHILDREN, (uint8_t**)&new_cookies);
402 
403  if (new_cookies) {
404  av_free(c->cookies);
405  c->cookies = new_cookies;
406  }
407 
408  av_dict_set(&opts, "cookies", c->cookies, 0);
409  }
410 
411  av_dict_free(&tmp);
412 
413  if (is_http)
414  *is_http = av_strstart(proto_name, "http", NULL);
415 
416  return ret;
417 }
418 
419 static char *get_content_url(xmlNodePtr *baseurl_nodes,
420  int n_baseurl_nodes,
421  char *rep_id_val,
422  char *rep_bandwidth_val,
423  char *val)
424 {
425  int i;
426  char *text;
427  char *url = NULL;
428  char tmp_str[MAX_URL_SIZE];
429  char tmp_str_2[MAX_URL_SIZE];
430 
431  memset(tmp_str, 0, sizeof(tmp_str));
432 
433  for (i = 0; i < n_baseurl_nodes; ++i) {
434  if (baseurl_nodes[i] &&
435  baseurl_nodes[i]->children &&
436  baseurl_nodes[i]->children->type == XML_TEXT_NODE) {
437  text = xmlNodeGetContent(baseurl_nodes[i]->children);
438  if (text) {
439  memset(tmp_str, 0, sizeof(tmp_str));
440  memset(tmp_str_2, 0, sizeof(tmp_str_2));
441  ff_make_absolute_url(tmp_str_2, MAX_URL_SIZE, tmp_str, text);
442  av_strlcpy(tmp_str, tmp_str_2, sizeof(tmp_str));
443  xmlFree(text);
444  }
445  }
446  }
447 
448  if (val)
449  av_strlcat(tmp_str, (const char*)val, sizeof(tmp_str));
450 
451  if (rep_id_val) {
452  url = av_strireplace(tmp_str, "$RepresentationID$", (const char*)rep_id_val);
453  if (!url) {
454  return NULL;
455  }
456  av_strlcpy(tmp_str, url, sizeof(tmp_str));
457  av_free(url);
458  }
459  if (rep_bandwidth_val && tmp_str[0] != '\0') {
460  url = av_strireplace(tmp_str, "$Bandwidth$", (const char*)rep_bandwidth_val);
461  if (!url) {
462  return NULL;
463  }
464  }
465  return url;
466 }
467 
468 static char *get_val_from_nodes_tab(xmlNodePtr *nodes, const int n_nodes, const char *attrname)
469 {
470  int i;
471  char *val;
472 
473  for (i = 0; i < n_nodes; ++i) {
474  if (nodes[i]) {
475  val = xmlGetProp(nodes[i], attrname);
476  if (val)
477  return val;
478  }
479  }
480 
481  return NULL;
482 }
483 
484 static xmlNodePtr find_child_node_by_name(xmlNodePtr rootnode, const char *nodename)
485 {
486  xmlNodePtr node = rootnode;
487  if (!node) {
488  return NULL;
489  }
490 
491  node = xmlFirstElementChild(node);
492  while (node) {
493  if (!av_strcasecmp(node->name, nodename)) {
494  return node;
495  }
496  node = xmlNextElementSibling(node);
497  }
498  return NULL;
499 }
500 
501 static enum AVMediaType get_content_type(xmlNodePtr node)
502 {
504  int i = 0;
505  const char *attr;
506  char *val = NULL;
507 
508  if (node) {
509  for (i = 0; i < 2; i++) {
510  attr = i ? "mimeType" : "contentType";
511  val = xmlGetProp(node, attr);
512  if (val) {
513  if (av_stristr((const char *)val, "video")) {
514  type = AVMEDIA_TYPE_VIDEO;
515  } else if (av_stristr((const char *)val, "audio")) {
516  type = AVMEDIA_TYPE_AUDIO;
517  }
518  xmlFree(val);
519  }
520  }
521  }
522  return type;
523 }
524 
526  xmlNodePtr fragmenturl_node,
527  xmlNodePtr *baseurl_nodes,
528  char *rep_id_val,
529  char *rep_bandwidth_val)
530 {
531  char *initialization_val = NULL;
532  char *media_val = NULL;
533 
534  if (!av_strcasecmp(fragmenturl_node->name, (const char *)"Initialization")) {
535  initialization_val = xmlGetProp(fragmenturl_node, "sourceURL");
536  if (initialization_val) {
537  rep->init_section = av_mallocz(sizeof(struct fragment));
538  if (!rep->init_section) {
539  xmlFree(initialization_val);
540  return AVERROR(ENOMEM);
541  }
542  rep->init_section->url = get_content_url(baseurl_nodes, 4,
543  rep_id_val,
544  rep_bandwidth_val,
545  initialization_val);
546  if (!rep->init_section->url) {
547  av_free(rep->init_section);
548  xmlFree(initialization_val);
549  return AVERROR(ENOMEM);
550  }
551  rep->init_section->size = -1;
552  xmlFree(initialization_val);
553  }
554  } else if (!av_strcasecmp(fragmenturl_node->name, (const char *)"SegmentURL")) {
555  media_val = xmlGetProp(fragmenturl_node, "media");
556  if (media_val) {
557  struct fragment *seg = av_mallocz(sizeof(struct fragment));
558  if (!seg) {
559  xmlFree(media_val);
560  return AVERROR(ENOMEM);
561  }
562  seg->url = get_content_url(baseurl_nodes, 4,
563  rep_id_val,
564  rep_bandwidth_val,
565  media_val);
566  if (!seg->url) {
567  av_free(seg);
568  xmlFree(media_val);
569  return AVERROR(ENOMEM);
570  }
571  seg->size = -1;
572  dynarray_add(&rep->fragments, &rep->n_fragments, seg);
573  xmlFree(media_val);
574  }
575  }
576 
577  return 0;
578 }
579 
581  xmlNodePtr fragment_timeline_node)
582 {
583  xmlAttrPtr attr = NULL;
584  char *val = NULL;
585 
586  if (!av_strcasecmp(fragment_timeline_node->name, (const char *)"S")) {
587  struct timeline *tml = av_mallocz(sizeof(struct timeline));
588  if (!tml) {
589  return AVERROR(ENOMEM);
590  }
591  attr = fragment_timeline_node->properties;
592  while (attr) {
593  val = xmlGetProp(fragment_timeline_node, attr->name);
594 
595  if (!val) {
596  av_log(s, AV_LOG_WARNING, "parse_manifest_segmenttimeline attr->name = %s val is NULL\n", attr->name);
597  continue;
598  }
599 
600  if (!av_strcasecmp(attr->name, (const char *)"t")) {
601  tml->starttime = (int64_t)strtoll(val, NULL, 10);
602  } else if (!av_strcasecmp(attr->name, (const char *)"r")) {
603  tml->repeat =(int64_t) strtoll(val, NULL, 10);
604  } else if (!av_strcasecmp(attr->name, (const char *)"d")) {
605  tml->duration = (int64_t)strtoll(val, NULL, 10);
606  }
607  attr = attr->next;
608  xmlFree(val);
609  }
610  dynarray_add(&rep->timelines, &rep->n_timelines, tml);
611  }
612 
613  return 0;
614 }
615 
616 static int parse_manifest_representation(AVFormatContext *s, const char *url,
617  xmlNodePtr node,
618  xmlNodePtr adaptionset_node,
619  xmlNodePtr mpd_baseurl_node,
620  xmlNodePtr period_baseurl_node,
621  xmlNodePtr fragment_template_node,
622  xmlNodePtr content_component_node,
623  xmlNodePtr adaptionset_baseurl_node)
624 {
625  int32_t ret = 0;
626  int32_t audio_rep_idx = 0;
627  int32_t video_rep_idx = 0;
628  DASHContext *c = s->priv_data;
629  struct representation *rep = NULL;
630  struct fragment *seg = NULL;
631  xmlNodePtr representation_segmenttemplate_node = NULL;
632  xmlNodePtr representation_baseurl_node = NULL;
633  xmlNodePtr representation_segmentlist_node = NULL;
634  xmlNodePtr fragment_timeline_node = NULL;
635  xmlNodePtr fragment_templates_tab[2];
636  char *duration_val = NULL;
637  char *presentation_timeoffset_val = NULL;
638  char *startnumber_val = NULL;
639  char *timescale_val = NULL;
640  char *initialization_val = NULL;
641  char *media_val = NULL;
642  xmlNodePtr baseurl_nodes[4];
643  xmlNodePtr representation_node = node;
644  char *rep_id_val = xmlGetProp(representation_node, "id");
645  char *rep_bandwidth_val = xmlGetProp(representation_node, "bandwidth");
647 
648  // try get information from representation
649  if (type == AVMEDIA_TYPE_UNKNOWN)
650  type = get_content_type(representation_node);
651  // try get information from contentComponen
652  if (type == AVMEDIA_TYPE_UNKNOWN)
653  type = get_content_type(content_component_node);
654  // try get information from adaption set
655  if (type == AVMEDIA_TYPE_UNKNOWN)
656  type = get_content_type(adaptionset_node);
657  if (type == AVMEDIA_TYPE_UNKNOWN) {
658  av_log(s, AV_LOG_VERBOSE, "Parsing '%s' - skipp not supported representation type\n", url);
659  } else if ((type == AVMEDIA_TYPE_VIDEO && !c->cur_video) || (type == AVMEDIA_TYPE_AUDIO && !c->cur_audio)) {
660  // convert selected representation to our internal struct
661  rep = av_mallocz(sizeof(struct representation));
662  if (!rep) {
663  ret = AVERROR(ENOMEM);
664  goto end;
665  }
666  representation_segmenttemplate_node = find_child_node_by_name(representation_node, "SegmentTemplate");
667  representation_baseurl_node = find_child_node_by_name(representation_node, "BaseURL");
668  representation_segmentlist_node = find_child_node_by_name(representation_node, "SegmentList");
669 
670  baseurl_nodes[0] = mpd_baseurl_node;
671  baseurl_nodes[1] = period_baseurl_node;
672  baseurl_nodes[2] = adaptionset_baseurl_node;
673  baseurl_nodes[3] = representation_baseurl_node;
674 
675  if (representation_segmenttemplate_node || fragment_template_node) {
676  fragment_timeline_node = NULL;
677  fragment_templates_tab[0] = representation_segmenttemplate_node;
678  fragment_templates_tab[1] = fragment_template_node;
679 
680  presentation_timeoffset_val = get_val_from_nodes_tab(fragment_templates_tab, 2, "presentationTimeOffset");
681  duration_val = get_val_from_nodes_tab(fragment_templates_tab, 2, "duration");
682  startnumber_val = get_val_from_nodes_tab(fragment_templates_tab, 2, "startNumber");
683  timescale_val = get_val_from_nodes_tab(fragment_templates_tab, 2, "timescale");
684  initialization_val = get_val_from_nodes_tab(fragment_templates_tab, 2, "initialization");
685  media_val = get_val_from_nodes_tab(fragment_templates_tab, 2, "media");
686 
687  if (initialization_val) {
688  rep->init_section = av_mallocz(sizeof(struct fragment));
689  if (!rep->init_section) {
690  av_free(rep);
691  ret = AVERROR(ENOMEM);
692  goto end;
693  }
694  rep->init_section->url = get_content_url(baseurl_nodes, 4, rep_id_val, rep_bandwidth_val, initialization_val);
695  if (!rep->init_section->url) {
696  av_free(rep->init_section);
697  av_free(rep);
698  ret = AVERROR(ENOMEM);
699  goto end;
700  }
701  rep->init_section->size = -1;
702  xmlFree(initialization_val);
703  }
704 
705  if (media_val) {
706  rep->url_template = get_content_url(baseurl_nodes, 4, rep_id_val, rep_bandwidth_val, media_val);
707  xmlFree(media_val);
708  }
709 
710  if (presentation_timeoffset_val) {
711  rep->presentation_timeoffset = (int64_t) strtoll(presentation_timeoffset_val, NULL, 10);
712  xmlFree(presentation_timeoffset_val);
713  }
714  if (duration_val) {
715  rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
716  xmlFree(duration_val);
717  }
718  if (timescale_val) {
719  rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
720  xmlFree(timescale_val);
721  }
722  if (startnumber_val) {
723  rep->first_seq_no = (int64_t) strtoll(startnumber_val, NULL, 10);
724  xmlFree(startnumber_val);
725  }
726 
727  fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
728 
729  if (!fragment_timeline_node)
730  fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
731  if (fragment_timeline_node) {
732  fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
733  while (fragment_timeline_node) {
734  ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
735  if (ret < 0) {
736  return ret;
737  }
738  fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
739  }
740  }
741  } else if (representation_baseurl_node && !representation_segmentlist_node) {
742  seg = av_mallocz(sizeof(struct fragment));
743  if (!seg) {
744  ret = AVERROR(ENOMEM);
745  goto end;
746  }
747  seg->url = get_content_url(baseurl_nodes, 4, rep_id_val, rep_bandwidth_val, NULL);
748  if (!seg->url) {
749  av_free(seg);
750  ret = AVERROR(ENOMEM);
751  goto end;
752  }
753  seg->size = -1;
754  dynarray_add(&rep->fragments, &rep->n_fragments, seg);
755  } else if (representation_segmentlist_node) {
756  // TODO: https://www.brendanlong.com/the-structure-of-an-mpeg-dash-mpd.html
757  // http://www-itec.uni-klu.ac.at/dash/ddash/mpdGenerator.php?fragmentlength=15&type=full
758  xmlNodePtr fragmenturl_node = NULL;
759  duration_val = xmlGetProp(representation_segmentlist_node, "duration");
760  timescale_val = xmlGetProp(representation_segmentlist_node, "timescale");
761  if (duration_val) {
762  rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
763  xmlFree(duration_val);
764  }
765  if (timescale_val) {
766  rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
767  xmlFree(timescale_val);
768  }
769  fragmenturl_node = xmlFirstElementChild(representation_segmentlist_node);
770  while (fragmenturl_node) {
771  ret = parse_manifest_segmenturlnode(s, rep, fragmenturl_node,
772  baseurl_nodes,
773  rep_id_val,
774  rep_bandwidth_val);
775  if (ret < 0) {
776  return ret;
777  }
778  fragmenturl_node = xmlNextElementSibling(fragmenturl_node);
779  }
780 
781  fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
782 
783  if (!fragment_timeline_node)
784  fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
785  if (fragment_timeline_node) {
786  fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
787  while (fragment_timeline_node) {
788  ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
789  if (ret < 0) {
790  return ret;
791  }
792  fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
793  }
794  }
795  } else {
796  free_representation(rep);
797  rep = NULL;
798  av_log(s, AV_LOG_ERROR, "Unknown format of Representation node id[%s] \n", (const char *)rep_id_val);
799  }
800 
801  if (rep) {
802  if (rep->fragment_duration > 0 && !rep->fragment_timescale)
803  rep->fragment_timescale = 1;
804  if (type == AVMEDIA_TYPE_VIDEO) {
805  rep->rep_idx = video_rep_idx;
806  c->cur_video = rep;
807  } else {
808  rep->rep_idx = audio_rep_idx;
809  c->cur_audio = rep;
810  }
811  }
812  }
813 
814  video_rep_idx += type == AVMEDIA_TYPE_VIDEO;
815  audio_rep_idx += type == AVMEDIA_TYPE_AUDIO;
816 
817 end:
818  if (rep_id_val)
819  xmlFree(rep_id_val);
820  if (rep_bandwidth_val)
821  xmlFree(rep_bandwidth_val);
822 
823  return ret;
824 }
825 
827  xmlNodePtr adaptionset_node,
828  xmlNodePtr mpd_baseurl_node,
829  xmlNodePtr period_baseurl_node)
830 {
831  int ret = 0;
832  xmlNodePtr fragment_template_node = NULL;
833  xmlNodePtr content_component_node = NULL;
834  xmlNodePtr adaptionset_baseurl_node = NULL;
835  xmlNodePtr node = NULL;
836 
837  node = xmlFirstElementChild(adaptionset_node);
838  while (node) {
839  if (!av_strcasecmp(node->name, (const char *)"SegmentTemplate")) {
840  fragment_template_node = node;
841  } else if (!av_strcasecmp(node->name, (const char *)"ContentComponent")) {
842  content_component_node = node;
843  } else if (!av_strcasecmp(node->name, (const char *)"BaseURL")) {
844  adaptionset_baseurl_node = node;
845  } else if (!av_strcasecmp(node->name, (const char *)"Representation")) {
846  ret = parse_manifest_representation(s, url, node,
847  adaptionset_node,
848  mpd_baseurl_node,
849  period_baseurl_node,
850  fragment_template_node,
851  content_component_node,
852  adaptionset_baseurl_node);
853  if (ret < 0) {
854  return ret;
855  }
856  }
857  node = xmlNextElementSibling(node);
858  }
859  return 0;
860 }
861 
862 static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
863 {
864  DASHContext *c = s->priv_data;
865  int ret = 0;
866  int close_in = 0;
867  uint8_t *new_url = NULL;
868  int64_t filesize = 0;
869  char *buffer = NULL;
871  xmlDoc *doc = NULL;
872  xmlNodePtr root_element = NULL;
873  xmlNodePtr node = NULL;
874  xmlNodePtr period_node = NULL;
875  xmlNodePtr mpd_baseurl_node = NULL;
876  xmlNodePtr period_baseurl_node = NULL;
877  xmlNodePtr adaptionset_node = NULL;
878  xmlAttrPtr attr = NULL;
879  char *val = NULL;
880  uint32_t perdiod_duration_sec = 0;
881  uint32_t perdiod_start_sec = 0;
882  int32_t audio_rep_idx = 0;
883  int32_t video_rep_idx = 0;
884 
885  if (!in) {
886  close_in = 1;
887 
888  set_httpheader_options(c, opts);
889  ret = avio_open2(&in, url, AVIO_FLAG_READ, c->interrupt_callback, &opts);
890  av_dict_free(&opts);
891  if (ret < 0)
892  return ret;
893  }
894 
895  if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, &new_url) >= 0) {
896  c->base_url = av_strdup(new_url);
897  } else {
898  c->base_url = av_strdup(url);
899  }
900 
901  filesize = avio_size(in);
902  if (filesize <= 0) {
903  filesize = 8 * 1024;
904  }
905 
906  buffer = av_mallocz(filesize);
907  if (!buffer) {
908  av_free(c->base_url);
909  return AVERROR(ENOMEM);
910  }
911 
912  filesize = avio_read(in, buffer, filesize);
913  if (filesize <= 0) {
914  av_log(s, AV_LOG_ERROR, "Unable to read to offset '%s'\n", url);
915  ret = AVERROR_INVALIDDATA;
916  } else {
917  LIBXML_TEST_VERSION
918 
919  doc = xmlReadMemory(buffer, filesize, c->base_url, NULL, 0);
920  root_element = xmlDocGetRootElement(doc);
921  node = root_element;
922 
923  if (!node) {
924  ret = AVERROR_INVALIDDATA;
925  av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing root node\n", url);
926  goto cleanup;
927  }
928 
929  if (node->type != XML_ELEMENT_NODE ||
930  av_strcasecmp(node->name, (const char *)"MPD")) {
931  ret = AVERROR_INVALIDDATA;
932  av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - wrong root node name[%s] type[%d]\n", url, node->name, (int)node->type);
933  goto cleanup;
934  }
935 
936  val = xmlGetProp(node, "type");
937  if (!val) {
938  av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing type attrib\n", url);
939  ret = AVERROR_INVALIDDATA;
940  goto cleanup;
941  }
942  if (!av_strcasecmp(val, (const char *)"dynamic"))
943  c->is_live = 1;
944  xmlFree(val);
945 
946  attr = node->properties;
947  while (attr) {
948  val = xmlGetProp(node, attr->name);
949 
950  if (!av_strcasecmp(attr->name, (const char *)"availabilityStartTime")) {
951  c->availability_start_time = get_utc_date_time_insec(s, (const char *)val);
952  } else if (!av_strcasecmp(attr->name, (const char *)"publishTime")) {
953  c->publish_time = get_utc_date_time_insec(s, (const char *)val);
954  } else if (!av_strcasecmp(attr->name, (const char *)"minimumUpdatePeriod")) {
955  c->minimum_update_period = get_duration_insec(s, (const char *)val);
956  } else if (!av_strcasecmp(attr->name, (const char *)"timeShiftBufferDepth")) {
957  c->time_shift_buffer_depth = get_duration_insec(s, (const char *)val);
958  } else if (!av_strcasecmp(attr->name, (const char *)"minBufferTime")) {
959  c->min_buffer_time = get_duration_insec(s, (const char *)val);
960  } else if (!av_strcasecmp(attr->name, (const char *)"suggestedPresentationDelay")) {
961  c->suggested_presentation_delay = get_duration_insec(s, (const char *)val);
962  } else if (!av_strcasecmp(attr->name, (const char *)"mediaPresentationDuration")) {
963  c->media_presentation_duration = get_duration_insec(s, (const char *)val);
964  }
965  attr = attr->next;
966  xmlFree(val);
967  }
968 
969  mpd_baseurl_node = find_child_node_by_name(node, "BaseURL");
970 
971  // at now we can handle only one period, with the longest duration
972  node = xmlFirstElementChild(node);
973  while (node) {
974  if (!av_strcasecmp(node->name, (const char *)"Period")) {
975  perdiod_duration_sec = 0;
976  perdiod_start_sec = 0;
977  attr = node->properties;
978  while (attr) {
979  val = xmlGetProp(node, attr->name);
980  if (!av_strcasecmp(attr->name, (const char *)"duration")) {
981  perdiod_duration_sec = get_duration_insec(s, (const char *)val);
982  } else if (!av_strcasecmp(attr->name, (const char *)"start")) {
983  perdiod_start_sec = get_duration_insec(s, (const char *)val);
984  }
985  attr = attr->next;
986  xmlFree(val);
987  }
988  if ((perdiod_duration_sec) >= (c->period_duration)) {
989  period_node = node;
990  c->period_duration = perdiod_duration_sec;
991  c->period_start = perdiod_start_sec;
992  if (c->period_start > 0)
994  }
995  }
996  node = xmlNextElementSibling(node);
997  }
998  if (!period_node) {
999  av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing Period node\n", url);
1000  ret = AVERROR_INVALIDDATA;
1001  goto cleanup;
1002  }
1003 
1004  adaptionset_node = xmlFirstElementChild(period_node);
1005  while (adaptionset_node) {
1006  if (!av_strcasecmp(adaptionset_node->name, (const char *)"BaseURL")) {
1007  period_baseurl_node = adaptionset_node;
1008  } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"AdaptationSet")) {
1009  parse_manifest_adaptationset(s, url, adaptionset_node, mpd_baseurl_node, period_baseurl_node);
1010  }
1011  adaptionset_node = xmlNextElementSibling(adaptionset_node);
1012  }
1013  if (c->cur_video) {
1014  c->cur_video->rep_count = video_rep_idx;
1015  av_log(s, AV_LOG_VERBOSE, "rep_idx[%d]\n", (int)c->cur_video->rep_idx);
1016  av_log(s, AV_LOG_VERBOSE, "rep_count[%d]\n", (int)video_rep_idx);
1017  }
1018  if (c->cur_audio) {
1019  c->cur_audio->rep_count = audio_rep_idx;
1020  }
1021 cleanup:
1022  /*free the document */
1023  xmlFreeDoc(doc);
1024  xmlCleanupParser();
1025  }
1026 
1027  av_free(new_url);
1028  av_free(buffer);
1029  if (close_in) {
1030  avio_close(in);
1031  }
1032  return ret;
1033 }
1034 
1035 static int64_t calc_cur_seg_no(AVFormatContext *s, struct representation *pls)
1036 {
1037  DASHContext *c = s->priv_data;
1038  int64_t num = 0;
1039  int64_t start_time_offset = 0;
1040 
1041  if (c->is_live) {
1042  if (pls->n_fragments) {
1043  num = pls->first_seq_no;
1044  } else if (pls->n_timelines) {
1045  start_time_offset = get_segment_start_time_based_on_timeline(pls, 0xFFFFFFFF) - pls->timelines[pls->first_seq_no]->starttime; // total duration of playlist
1046  if (start_time_offset < 60 * pls->fragment_timescale)
1047  start_time_offset = 0;
1048  else
1049  start_time_offset = start_time_offset - 60 * pls->fragment_timescale;
1050 
1051  num = calc_next_seg_no_from_timelines(pls, pls->timelines[pls->first_seq_no]->starttime + start_time_offset);
1052  if (num == -1)
1053  num = pls->first_seq_no;
1054  } else if (pls->fragment_duration){
1055  if (pls->presentation_timeoffset) {
1057  } else if (c->publish_time > 0 && !c->availability_start_time) {
1059  } else {
1061  }
1062  }
1063  } else {
1064  num = pls->first_seq_no;
1065  }
1066  return num;
1067 }
1068 
1069 static int64_t calc_min_seg_no(AVFormatContext *s, struct representation *pls)
1070 {
1071  DASHContext *c = s->priv_data;
1072  int64_t num = 0;
1073 
1074  if (c->is_live && pls->fragment_duration) {
1076  } else {
1077  num = pls->first_seq_no;
1078  }
1079  return num;
1080 }
1081 
1082 static int64_t calc_max_seg_no(struct representation *pls)
1083 {
1084  DASHContext *c = pls->parent->priv_data;
1085  int64_t num = 0;
1086 
1087  if (pls->n_fragments) {
1088  num = pls->first_seq_no + pls->n_fragments - 1;
1089  } else if (pls->n_timelines) {
1090  int i = 0;
1091  num = pls->first_seq_no + pls->n_timelines - 1;
1092  for (i = 0; i < pls->n_timelines; i++) {
1093  num += pls->timelines[i]->repeat;
1094  }
1095  } else if (c->is_live && pls->fragment_duration) {
1097  } else if (pls->fragment_duration) {
1099  }
1100 
1101  return num;
1102 }
1103 
1104 static void move_timelines(struct representation *rep_src, struct representation *rep_dest)
1105 {
1106  if (rep_dest && rep_src ) {
1107  free_timelines_list(rep_dest);
1108  rep_dest->timelines = rep_src->timelines;
1109  rep_dest->n_timelines = rep_src->n_timelines;
1110  rep_dest->first_seq_no = rep_src->first_seq_no;
1111  rep_dest->last_seq_no = calc_max_seg_no(rep_dest);
1112  rep_src->timelines = NULL;
1113  rep_src->n_timelines = 0;
1114  rep_dest->cur_seq_no = rep_src->cur_seq_no;
1115  }
1116 }
1117 
1118 static void move_segments(struct representation *rep_src, struct representation *rep_dest)
1119 {
1120  if (rep_dest && rep_src ) {
1121  free_fragment_list(rep_dest);
1122  if (rep_src->start_number > (rep_dest->start_number + rep_dest->n_fragments))
1123  rep_dest->cur_seq_no = 0;
1124  else
1125  rep_dest->cur_seq_no += rep_src->start_number - rep_dest->start_number;
1126  rep_dest->fragments = rep_src->fragments;
1127  rep_dest->n_fragments = rep_src->n_fragments;
1128  rep_dest->parent = rep_src->parent;
1129  rep_dest->last_seq_no = calc_max_seg_no(rep_dest);
1130  rep_src->fragments = NULL;
1131  rep_src->n_fragments = 0;
1132  }
1133 }
1134 
1135 
1137 {
1138 
1139  int ret = 0;
1140  DASHContext *c = s->priv_data;
1141 
1142  // save current context
1143  struct representation *cur_video = c->cur_video;
1144  struct representation *cur_audio = c->cur_audio;
1145  char *base_url = c->base_url;
1146 
1147  c->base_url = NULL;
1148  c->cur_video = NULL;
1149  c->cur_audio = NULL;
1150  ret = parse_manifest(s, s->filename, NULL);
1151  if (ret)
1152  goto finish;
1153 
1154  if (cur_video && cur_video->timelines || cur_audio && cur_audio->timelines) {
1155  // calc current time
1156  int64_t currentVideoTime = 0;
1157  int64_t currentAudioTime = 0;
1158  if (cur_video && cur_video->timelines)
1159  currentVideoTime = get_segment_start_time_based_on_timeline(cur_video, cur_video->cur_seq_no) / cur_video->fragment_timescale;
1160  if (cur_audio && cur_audio->timelines)
1161  currentAudioTime = get_segment_start_time_based_on_timeline(cur_audio, cur_audio->cur_seq_no) / cur_audio->fragment_timescale;
1162  // update segments
1163  if (cur_video && cur_video->timelines) {
1164  c->cur_video->cur_seq_no = calc_next_seg_no_from_timelines(c->cur_video, currentVideoTime * cur_video->fragment_timescale - 1);
1165  if (c->cur_video->cur_seq_no >= 0) {
1166  move_timelines(c->cur_video, cur_video);
1167  }
1168  }
1169  if (cur_audio && cur_audio->timelines) {
1170  c->cur_audio->cur_seq_no = calc_next_seg_no_from_timelines(c->cur_audio, currentAudioTime * cur_audio->fragment_timescale - 1);
1171  if (c->cur_audio->cur_seq_no >= 0) {
1172  move_timelines(c->cur_audio, cur_audio);
1173  }
1174  }
1175  }
1176  if (cur_video && cur_video->fragments) {
1177  move_segments(c->cur_video, cur_video);
1178  }
1179  if (cur_audio && cur_audio->fragments) {
1180  move_segments(c->cur_audio, cur_audio);
1181  }
1182 
1183 finish:
1184  // restore context
1185  if (c->base_url)
1186  av_free(base_url);
1187  else
1188  c->base_url = base_url;
1189  if (c->cur_audio)
1191  if (c->cur_video)
1193  c->cur_audio = cur_audio;
1194  c->cur_video = cur_video;
1195  return ret;
1196 }
1197 
1198 static struct fragment *get_current_fragment(struct representation *pls)
1199 {
1200  int64_t min_seq_no = 0;
1201  int64_t max_seq_no = 0;
1202  struct fragment *seg = NULL;
1203  struct fragment *seg_ptr = NULL;
1204  DASHContext *c = pls->parent->priv_data;
1205 
1206  while (( !ff_check_interrupt(c->interrupt_callback)&& pls->n_fragments > 0)) {
1207  if (pls->cur_seq_no < pls->n_fragments) {
1208  seg_ptr = pls->fragments[pls->cur_seq_no];
1209  seg = av_mallocz(sizeof(struct fragment));
1210  if (!seg) {
1211  return NULL;
1212  }
1213  seg->url = av_strdup(seg_ptr->url);
1214  if (!seg->url) {
1215  av_free(seg);
1216  return NULL;
1217  }
1218  seg->size = seg_ptr->size;
1219  seg->url_offset = seg_ptr->url_offset;
1220  return seg;
1221  } else if (c->is_live) {
1222  refresh_manifest(pls->parent);
1223  } else {
1224  break;
1225  }
1226  }
1227  if (c->is_live) {
1228  min_seq_no = calc_min_seg_no(pls->parent, pls);
1229  max_seq_no = calc_max_seg_no(pls);
1230 
1231  if (pls->timelines || pls->fragments) {
1232  refresh_manifest(pls->parent);
1233  }
1234  if (pls->cur_seq_no <= min_seq_no) {
1235  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);
1236  pls->cur_seq_no = calc_cur_seg_no(pls->parent, pls);
1237  } else if (pls->cur_seq_no > max_seq_no) {
1238  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);
1239  }
1240  seg = av_mallocz(sizeof(struct fragment));
1241  if (!seg) {
1242  return NULL;
1243  }
1244  } else if (pls->cur_seq_no <= pls->last_seq_no) {
1245  seg = av_mallocz(sizeof(struct fragment));
1246  if (!seg) {
1247  return NULL;
1248  }
1249  }
1250  if (seg) {
1251  char tmpfilename[MAX_URL_SIZE];
1252 
1253  ff_dash_fill_tmpl_params(tmpfilename, sizeof(tmpfilename), pls->url_template, 0, pls->cur_seq_no, 0, get_segment_start_time_based_on_timeline(pls, pls->cur_seq_no));
1254  seg->url = av_strireplace(pls->url_template, pls->url_template, tmpfilename);
1255  if (!seg->url) {
1256  av_log(pls->parent, AV_LOG_WARNING, "Unable to resolve template url '%s', try to use origin template\n", pls->url_template);
1257  seg->url = av_strdup(pls->url_template);
1258  if (!seg->url) {
1259  av_log(pls->parent, AV_LOG_ERROR, "Cannot resolve template url '%s'\n", pls->url_template);
1260  return NULL;
1261  }
1262  }
1263 
1264  seg->size = -1;
1265  }
1266 
1267  return seg;
1268 }
1269 
1273 };
1274 
1275 static int read_from_url(struct representation *pls, struct fragment *seg,
1276  uint8_t *buf, int buf_size,
1277  enum ReadFromURLMode mode)
1278 {
1279  int ret;
1280 
1281  /* limit read if the fragment was only a part of a file */
1282  if (seg->size >= 0)
1283  buf_size = FFMIN(buf_size, pls->cur_seg_size - pls->cur_seg_offset);
1284 
1285  if (mode == READ_COMPLETE) {
1286  ret = avio_read(pls->input, buf, buf_size);
1287  if (ret < buf_size) {
1288  av_log(pls->parent, AV_LOG_WARNING, "Could not read complete fragment.\n");
1289  }
1290  } else {
1291  ret = avio_read(pls->input, buf, buf_size);
1292  }
1293  if (ret > 0)
1294  pls->cur_seg_offset += ret;
1295 
1296  return ret;
1297 }
1298 
1299 static int open_input(DASHContext *c, struct representation *pls, struct fragment *seg)
1300 {
1301  AVDictionary *opts = NULL;
1302  char url[MAX_URL_SIZE];
1303  int ret;
1304 
1305  set_httpheader_options(c, opts);
1306  if (seg->size >= 0) {
1307  /* try to restrict the HTTP request to the part we want
1308  * (if this is in fact a HTTP request) */
1309  av_dict_set_int(&opts, "offset", seg->url_offset, 0);
1310  av_dict_set_int(&opts, "end_offset", seg->url_offset + seg->size, 0);
1311  }
1312 
1314  av_log(pls->parent, AV_LOG_VERBOSE, "DASH request for url '%s', offset %"PRId64", playlist %d\n",
1315  url, seg->url_offset, pls->rep_idx);
1316  ret = open_url(pls->parent, &pls->input, url, c->avio_opts, opts, NULL);
1317  if (ret < 0) {
1318  goto cleanup;
1319  }
1320 
1321  /* Seek to the requested position. If this was a HTTP request, the offset
1322  * should already be where want it to, but this allows e.g. local testing
1323  * without a HTTP server. */
1324  if (!ret && seg->url_offset) {
1325  int64_t seekret = avio_seek(pls->input, seg->url_offset, SEEK_SET);
1326  if (seekret < 0) {
1327  av_log(pls->parent, AV_LOG_ERROR, "Unable to seek to offset %"PRId64" of DASH fragment '%s'\n", seg->url_offset, seg->url);
1328  ret = (int) seekret;
1329  ff_format_io_close(pls->parent, &pls->input);
1330  }
1331  }
1332 
1333 cleanup:
1334  av_dict_free(&opts);
1335  pls->cur_seg_offset = 0;
1336  pls->cur_seg_size = seg->size;
1337  return ret;
1338 }
1339 
1340 static int update_init_section(struct representation *pls)
1341 {
1342  static const int max_init_section_size = 1024 * 1024;
1343  DASHContext *c = pls->parent->priv_data;
1344  int64_t sec_size;
1345  int64_t urlsize;
1346  int ret;
1347 
1348  if (!pls->init_section || pls->init_sec_buf)
1349  return 0;
1350 
1351  ret = open_input(c, pls, pls->init_section);
1352  if (ret < 0) {
1354  "Failed to open an initialization section in playlist %d\n",
1355  pls->rep_idx);
1356  return ret;
1357  }
1358 
1359  if (pls->init_section->size >= 0)
1360  sec_size = pls->init_section->size;
1361  else if ((urlsize = avio_size(pls->input)) >= 0)
1362  sec_size = urlsize;
1363  else
1364  sec_size = max_init_section_size;
1365 
1366  av_log(pls->parent, AV_LOG_DEBUG,
1367  "Downloading an initialization section of size %"PRId64"\n",
1368  sec_size);
1369 
1370  sec_size = FFMIN(sec_size, max_init_section_size);
1371 
1372  av_fast_malloc(&pls->init_sec_buf, &pls->init_sec_buf_size, sec_size);
1373 
1374  ret = read_from_url(pls, pls->init_section, pls->init_sec_buf,
1376  ff_format_io_close(pls->parent, &pls->input);
1377 
1378  if (ret < 0)
1379  return ret;
1380 
1381  pls->init_sec_data_len = ret;
1382  pls->init_sec_buf_read_offset = 0;
1383 
1384  return 0;
1385 }
1386 
1387 static int64_t seek_data(void *opaque, int64_t offset, int whence)
1388 {
1389  struct representation *v = opaque;
1390  if (v->n_fragments && !v->init_sec_data_len) {
1391  return avio_seek(v->input, offset, whence);
1392  }
1393 
1394  return AVERROR(ENOSYS);
1395 }
1396 
1397 static int read_data(void *opaque, uint8_t *buf, int buf_size)
1398 {
1399  int ret = 0;
1400  struct representation *v = opaque;
1401  DASHContext *c = v->parent->priv_data;
1402 
1403 restart:
1404  if (!v->input) {
1405  free_fragment(&v->cur_seg);
1406  v->cur_seg = get_current_fragment(v);
1407  if (!v->cur_seg) {
1408  ret = AVERROR_EOF;
1409  goto end;
1410  }
1411 
1412  /* load/update Media Initialization Section, if any */
1413  ret = update_init_section(v);
1414  if (ret)
1415  goto end;
1416 
1417  ret = open_input(c, v, v->cur_seg);
1418  if (ret < 0) {
1420  goto end;
1421  ret = AVERROR_EXIT;
1422  }
1423  av_log(v->parent, AV_LOG_WARNING, "Failed to open fragment of playlist %d\n", v->rep_idx);
1424  v->cur_seq_no++;
1425  goto restart;
1426  }
1427  }
1428 
1430  /* Push init section out first before first actual fragment */
1431  int copy_size = FFMIN(v->init_sec_data_len - v->init_sec_buf_read_offset, buf_size);
1432  memcpy(buf, v->init_sec_buf, copy_size);
1433  v->init_sec_buf_read_offset += copy_size;
1434  ret = copy_size;
1435  goto end;
1436  }
1437 
1438  /* check the v->cur_seg, if it is null, get current and double check if the new v->cur_seg*/
1439  if (!v->cur_seg) {
1440  v->cur_seg = get_current_fragment(v);
1441  }
1442  if (!v->cur_seg) {
1443  ret = AVERROR_EOF;
1444  goto end;
1445  }
1446  ret = read_from_url(v, v->cur_seg, buf, buf_size, READ_NORMAL);
1447  if (ret > 0)
1448  goto end;
1449 
1450  if (!v->is_restart_needed)
1451  v->cur_seq_no++;
1452  v->is_restart_needed = 1;
1453 
1454 end:
1455  return ret;
1456 }
1457 
1459 {
1460  DASHContext *c = s->priv_data;
1461  const char *opts[] = { "headers", "user_agent", "user-agent", "cookies", NULL }, **opt = opts;
1462  uint8_t *buf = NULL;
1463  int ret = 0;
1464 
1465  while (*opt) {
1466  if (av_opt_get(s->pb, *opt, AV_OPT_SEARCH_CHILDREN, &buf) >= 0) {
1467  if (buf[0] != '\0') {
1468  ret = av_dict_set(&c->avio_opts, *opt, buf, AV_DICT_DONT_STRDUP_VAL);
1469  if (ret < 0)
1470  return ret;
1471  }
1472  }
1473  opt++;
1474  }
1475 
1476  return ret;
1477 }
1478 
1479 static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
1480  int flags, AVDictionary **opts)
1481 {
1482  av_log(s, AV_LOG_ERROR,
1483  "A DASH playlist item '%s' referred to an external file '%s'. "
1484  "Opening this file was forbidden for security reasons\n",
1485  s->filename, url);
1486  return AVERROR(EPERM);
1487 }
1488 
1490 {
1491  DASHContext *c = s->priv_data;
1492  AVInputFormat *in_fmt = NULL;
1493  AVDictionary *in_fmt_opts = NULL;
1494  uint8_t *avio_ctx_buffer = NULL;
1495  int ret = 0;
1496 
1497  if (pls->ctx) {
1498  /* note: the internal buffer could have changed, and be != avio_ctx_buffer */
1499  av_freep(&pls->pb.buffer);
1500  memset(&pls->pb, 0x00, sizeof(AVIOContext));
1501  pls->ctx->pb = NULL;
1502  avformat_close_input(&pls->ctx);
1503  pls->ctx = NULL;
1504  }
1505  if (!(pls->ctx = avformat_alloc_context())) {
1506  ret = AVERROR(ENOMEM);
1507  goto fail;
1508  }
1509 
1510  avio_ctx_buffer = av_malloc(INITIAL_BUFFER_SIZE);
1511  if (!avio_ctx_buffer ) {
1512  ret = AVERROR(ENOMEM);
1513  avformat_free_context(pls->ctx);
1514  pls->ctx = NULL;
1515  goto fail;
1516  }
1517  if (c->is_live) {
1518  ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, NULL);
1519  } else {
1520  ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, seek_data);
1521  }
1522  pls->pb.seekable = 0;
1523 
1524  if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0)
1525  goto fail;
1526 
1527  pls->ctx->flags = AVFMT_FLAG_CUSTOM_IO;
1528  pls->ctx->probesize = 1024 * 4;
1530  ret = av_probe_input_buffer(&pls->pb, &in_fmt, "", NULL, 0, 0);
1531  if (ret < 0) {
1532  av_log(s, AV_LOG_ERROR, "Error when loading first fragment, playlist %d\n", (int)pls->rep_idx);
1533  avformat_free_context(pls->ctx);
1534  pls->ctx = NULL;
1535  goto fail;
1536  }
1537 
1538  pls->ctx->pb = &pls->pb;
1539  pls->ctx->io_open = nested_io_open;
1540 
1541  // provide additional information from mpd if available
1542  ret = avformat_open_input(&pls->ctx, "", in_fmt, &in_fmt_opts); //pls->init_section->url
1543  av_dict_free(&in_fmt_opts);
1544  if (ret < 0)
1545  goto fail;
1546  if (pls->n_fragments) {
1547  ret = avformat_find_stream_info(pls->ctx, NULL);
1548  if (ret < 0)
1549  goto fail;
1550  }
1551 
1552 fail:
1553  return ret;
1554 }
1555 
1557 {
1558  int ret = 0;
1559  int i;
1560 
1561  pls->parent = s;
1562  pls->cur_seq_no = calc_cur_seg_no(s, pls);
1563  pls->last_seq_no = calc_max_seg_no(pls);
1564 
1565  ret = reopen_demux_for_component(s, pls);
1566  if (ret < 0) {
1567  goto fail;
1568  }
1569  for (i = 0; i < pls->ctx->nb_streams; i++) {
1570  AVStream *st = avformat_new_stream(s, NULL);
1571  AVStream *ist = pls->ctx->streams[i];
1572  if (!st) {
1573  ret = AVERROR(ENOMEM);
1574  goto fail;
1575  }
1576  st->id = i;
1579  }
1580 
1581  return 0;
1582 fail:
1583  return ret;
1584 }
1585 
1587 {
1588  void *u = (s->flags & AVFMT_FLAG_CUSTOM_IO) ? NULL : s->pb;
1589  DASHContext *c = s->priv_data;
1590  int ret = 0;
1591  int stream_index = 0;
1592 
1593  c->interrupt_callback = &s->interrupt_callback;
1594  // if the URL context is good, read important options we must broker later
1595  if (u) {
1596  update_options(&c->user_agent, "user-agent", u);
1597  update_options(&c->cookies, "cookies", u);
1598  update_options(&c->headers, "headers", u);
1599  }
1600 
1601  if ((ret = parse_manifest(s, s->filename, s->pb)) < 0)
1602  goto fail;
1603 
1604  if ((ret = save_avio_options(s)) < 0)
1605  goto fail;
1606 
1607  /* If this isn't a live stream, fill the total duration of the
1608  * stream. */
1609  if (!c->is_live) {
1610  s->duration = (int64_t) c->media_presentation_duration * AV_TIME_BASE;
1611  }
1612 
1613  /* Open the demuxer for curent video and current audio components if available */
1614  if (!ret && c->cur_video) {
1615  ret = open_demux_for_component(s, c->cur_video);
1616  if (!ret) {
1617  c->cur_video->stream_index = stream_index;
1618  ++stream_index;
1619  } else {
1620  free_representation(c->cur_video);
1621  c->cur_video = NULL;
1622  }
1623  }
1624 
1625  if (!ret && c->cur_audio) {
1626  ret = open_demux_for_component(s, c->cur_audio);
1627  if (!ret) {
1628  c->cur_audio->stream_index = stream_index;
1629  ++stream_index;
1630  } else {
1631  free_representation(c->cur_audio);
1632  c->cur_audio = NULL;
1633  }
1634  }
1635 
1636  if (!stream_index) {
1637  ret = AVERROR_INVALIDDATA;
1638  goto fail;
1639  }
1640 
1641  /* Create a program */
1642  if (!ret) {
1643  AVProgram *program;
1644  program = av_new_program(s, 0);
1645  if (!program) {
1646  goto fail;
1647  }
1648 
1649  if (c->cur_video) {
1650  av_program_add_stream_index(s, 0, c->cur_video->stream_index);
1651  }
1652  if (c->cur_audio) {
1653  av_program_add_stream_index(s, 0, c->cur_audio->stream_index);
1654  }
1655  }
1656 
1657  return 0;
1658 fail:
1659  return ret;
1660 }
1661 
1663 {
1664  DASHContext *c = s->priv_data;
1665  int ret = 0;
1666  struct representation *cur = NULL;
1667 
1668  if (!c->cur_audio && !c->cur_video ) {
1669  return AVERROR_INVALIDDATA;
1670  }
1671  if (c->cur_audio && !c->cur_video) {
1672  cur = c->cur_audio;
1673  } else if (!c->cur_audio && c->cur_video) {
1674  cur = c->cur_video;
1675  } else if (c->cur_video->cur_timestamp < c->cur_audio->cur_timestamp) {
1676  cur = c->cur_video;
1677  } else {
1678  cur = c->cur_audio;
1679  }
1680 
1681  if (cur->ctx) {
1682  while (!ff_check_interrupt(c->interrupt_callback) && !ret) {
1683  ret = av_read_frame(cur->ctx, pkt);
1684  if (ret >= 0) {
1685  /* If we got a packet, return it */
1686  cur->cur_timestamp = av_rescale(pkt->pts, (int64_t)cur->ctx->streams[0]->time_base.num * 90000, cur->ctx->streams[0]->time_base.den);
1687  pkt->stream_index = cur->stream_index;
1688  return 0;
1689  }
1690  if (cur->is_restart_needed) {
1691  cur->cur_seg_offset = 0;
1692  cur->init_sec_buf_read_offset = 0;
1693  if (cur->input)
1694  ff_format_io_close(cur->parent, &cur->input);
1695  ret = reopen_demux_for_component(s, cur);
1696  cur->is_restart_needed = 0;
1697  }
1698 
1699  }
1700  }
1701  return AVERROR_EOF;
1702 }
1703 
1705 {
1706  DASHContext *c = s->priv_data;
1707  if (c->cur_audio) {
1709  }
1710  if (c->cur_video) {
1712  }
1713 
1714  av_freep(&c->cookies);
1715  av_freep(&c->user_agent);
1716  av_dict_free(&c->avio_opts);
1717  av_freep(&c->base_url);
1718  return 0;
1719 }
1720 
1721 static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos_msec, int flags)
1722 {
1723  int ret = 0;
1724  int i = 0;
1725  int j = 0;
1726  int64_t duration = 0;
1727 
1728  av_log(pls->parent, AV_LOG_VERBOSE, "DASH seek pos[%"PRId64"ms], playlist %d\n", seek_pos_msec, pls->rep_idx);
1729 
1730  // single fragment mode
1731  if (pls->n_fragments == 1) {
1732  pls->cur_timestamp = 0;
1733  pls->cur_seg_offset = 0;
1734  ff_read_frame_flush(pls->ctx);
1735  return av_seek_frame(pls->ctx, -1, seek_pos_msec * 1000, flags);
1736  }
1737 
1738  if (pls->input)
1739  ff_format_io_close(pls->parent, &pls->input);
1740 
1741  // find the nearest fragment
1742  if (pls->n_timelines > 0 && pls->fragment_timescale > 0) {
1743  int64_t num = pls->first_seq_no;
1744  av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline start n_timelines[%d] "
1745  "last_seq_no[%"PRId64"], playlist %d.\n",
1746  (int)pls->n_timelines, (int64_t)pls->last_seq_no, (int)pls->rep_idx);
1747  for (i = 0; i < pls->n_timelines; i++) {
1748  if (pls->timelines[i]->starttime > 0) {
1749  duration = pls->timelines[i]->starttime;
1750  }
1751  duration += pls->timelines[i]->duration;
1752  if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
1753  goto set_seq_num;
1754  }
1755  for (j = 0; j < pls->timelines[i]->repeat; j++) {
1756  duration += pls->timelines[i]->duration;
1757  num++;
1758  if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
1759  goto set_seq_num;
1760  }
1761  }
1762  num++;
1763  }
1764 
1765 set_seq_num:
1766  pls->cur_seq_no = num > pls->last_seq_no ? pls->last_seq_no : num;
1767  av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline end cur_seq_no[%"PRId64"], playlist %d.\n",
1768  (int64_t)pls->cur_seq_no, (int)pls->rep_idx);
1769  } else if (pls->fragment_duration > 0) {
1770  pls->cur_seq_no = pls->first_seq_no + ((seek_pos_msec * pls->fragment_timescale) / pls->fragment_duration) / 1000;
1771  } else {
1772  av_log(pls->parent, AV_LOG_ERROR, "dash_seek missing fragment_duration\n");
1773  pls->cur_seq_no = pls->first_seq_no;
1774  }
1775  pls->cur_timestamp = 0;
1776  pls->cur_seg_offset = 0;
1777  pls->init_sec_buf_read_offset = 0;
1778  ret = reopen_demux_for_component(s, pls);
1779 
1780  return ret;
1781 }
1782 
1783 static int dash_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
1784 {
1785  int ret = 0;
1786  DASHContext *c = s->priv_data;
1787  int64_t seek_pos_msec = av_rescale_rnd(timestamp, 1000,
1788  s->streams[stream_index]->time_base.den,
1789  flags & AVSEEK_FLAG_BACKWARD ?
1791  if ((flags & AVSEEK_FLAG_BYTE) || c->is_live)
1792  return AVERROR(ENOSYS);
1793  if (c->cur_audio) {
1794  ret = dash_seek(s, c->cur_audio, seek_pos_msec, flags);
1795  }
1796  if (!ret && c->cur_video) {
1797  ret = dash_seek(s, c->cur_video, seek_pos_msec, flags);
1798  }
1799  return ret;
1800 }
1801 
1802 static int dash_probe(AVProbeData *p)
1803 {
1804  if (!av_stristr(p->buf, "<MPD"))
1805  return 0;
1806 
1807  if (av_stristr(p->buf, "dash:profile:isoff-on-demand:2011") ||
1808  av_stristr(p->buf, "dash:profile:isoff-live:2011") ||
1809  av_stristr(p->buf, "dash:profile:isoff-live:2012") ||
1810  av_stristr(p->buf, "dash:profile:isoff-main:2011")) {
1811  return AVPROBE_SCORE_MAX;
1812  }
1813  if (av_stristr(p->buf, "dash:profile")) {
1814  return AVPROBE_SCORE_MAX;
1815  }
1816 
1817  return 0;
1818 }
1819 
1820 #define OFFSET(x) offsetof(DASHContext, x)
1821 #define FLAGS AV_OPT_FLAG_DECODING_PARAM
1822 static const AVOption dash_options[] = {
1823  {"allowed_extensions", "List of file extensions that dash is allowed to access",
1824  OFFSET(allowed_extensions), AV_OPT_TYPE_STRING,
1825  {.str = "aac,m4a,m4s,m4v,mov,mp4"},
1826  INT_MIN, INT_MAX, FLAGS},
1827  {NULL}
1828 };
1829 
1830 static const AVClass dash_class = {
1831  .class_name = "dash",
1832  .item_name = av_default_item_name,
1833  .option = dash_options,
1834  .version = LIBAVUTIL_VERSION_INT,
1835 };
1836 
1838  .name = "dash",
1839  .long_name = NULL_IF_CONFIG_SMALL("Dynamic Adaptive Streaming over HTTP"),
1840  .priv_class = &dash_class,
1841  .priv_data_size = sizeof(DASHContext),
1848 };
time_t av_timegm(struct tm *tm)
Convert the decomposed UTC time in tm to a time_t value.
Definition: parseutils.c:568
int64_t cur_seg_size
Definition: dashdec.c:105
#define FLAGS
Definition: dashdec.c:1821
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2423
int64_t probesize
Maximum size of the data read from input for determining the input container format.
Definition: avformat.h:1493
AVIOContext * input
Definition: dashdec.c:78
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
void ff_make_absolute_url(char *buf, int size, const char *base, const char *rel)
Convert a relative url into an absolute url, given a base url.
Definition: url.c:80
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:334
int64_t url_offset
Definition: dashdec.c:34
int n_fragments
Definition: dashdec.c:88
char * allowed_extensions
Definition: dashdec.c:142
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1605
AVOption.
Definition: opt.h:246
ReadFromURLMode
Definition: dashdec.c:1270
static void move_segments(struct representation *rep_src, struct representation *rep_dest)
Definition: dashdec.c:1118
static int64_t get_segment_start_time_based_on_timeline(struct representation *pls, int64_t cur_seq_no)
Definition: dashdec.c:225
int n_timelines
Definition: dashdec.c:91
struct representation * cur_audio
Definition: dashdec.c:122
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:86
AVPacket pkt
Definition: dashdec.c:81
static int read_data(void *opaque, uint8_t *buf, int buf_size)
Definition: dashdec.c:1397
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4737
int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
Copies the whilelists from one context to the other.
Definition: utils.c:145
char * av_stristr(const char *s1, const char *s2)
Locate the first case-independent occurrence in the string haystack of the string needle...
Definition: avstring.c:56
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
static int64_t cur_time
Definition: ffserver.c:252
int num
Numerator.
Definition: rational.h:59
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:244
#define AVIO_FLAG_READ
read-only
Definition: avio.h:660
int64_t size
Definition: dashdec.c:35
unsigned char * buffer
Start of the buffer.
Definition: avio.h:226
static struct fragment * get_current_fragment(struct representation *pls)
Definition: dashdec.c:1198
static void set_httpheader_options(DASHContext *c, AVDictionary *opts)
Definition: dashdec.c:334
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:217
static int64_t calc_max_seg_no(struct representation *pls)
Definition: dashdec.c:1082
static const AVOption dash_options[]
Definition: dashdec.c:1822
static int64_t seek_data(void *opaque, int64_t offset, int whence)
Definition: dashdec.c:1387
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
static AVPacket pkt
int64_t cur_timestamp
Definition: dashdec.c:114
#define src
Definition: vp8dsp.c:254
char * headers
holds HTTP headers set as an AVOption to the HTTP protocol context
Definition: dashdec.c:141
static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representation *rep, xmlNodePtr fragmenturl_node, xmlNodePtr *baseurl_nodes, char *rep_id_val, char *rep_bandwidth_val)
Definition: dashdec.c:525
uint64_t min_buffer_time
Definition: dashdec.c:131
static void free_fragment(struct fragment **seg)
Definition: dashdec.c:284
Format I/O context.
Definition: avformat.h:1349
#define MAX_URL_SIZE
Definition: internal.h:30
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
void ff_read_frame_flush(AVFormatContext *s)
Flush the frame reader.
Definition: utils.c:1862
struct fragment * init_section
Definition: dashdec.c:109
uint32_t init_sec_buf_read_offset
Definition: dashdec.c:113
int stream_index
Definition: dashdec.c:84
static uint64_t get_utc_date_time_insec(AVFormatContext *s, const char *datetime)
Definition: dashdec.c:151
static int64_t start_time
Definition: ffplay.c:327
uint64_t suggested_presentation_delay
Definition: dashdec.c:126
uint8_t
Round toward +infinity.
Definition: mathematics.h:83
#define av_malloc(s)
uint64_t media_presentation_duration
Definition: dashdec.c:125
AVOptions.
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int64_t presentation_timeoffset
Definition: dashdec.c:101
int id
Format-specific stream ID.
Definition: avformat.h:896
static int dash_close(AVFormatContext *s)
Definition: dashdec.c:1704
void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition: utils.c:5449
uint64_t period_duration
Definition: dashdec.c:134
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4367
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1417
int64_t duration
Definition: movenc.c:63
int64_t first_seq_no
Definition: dashdec.c:94
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:144
AVIOContext pb
Definition: dashdec.c:77
static void finish(void)
Definition: movenc.c:344
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1460
static int parse_manifest_representation(AVFormatContext *s, const char *url, xmlNodePtr node, xmlNodePtr adaptionset_node, xmlNodePtr mpd_baseurl_node, xmlNodePtr period_baseurl_node, xmlNodePtr fragment_template_node, xmlNodePtr content_component_node, xmlNodePtr adaptionset_baseurl_node)
Definition: dashdec.c:616
AVProgram * av_new_program(AVFormatContext *s, int id)
Definition: utils.c:4466
struct timeline ** timelines
Definition: dashdec.c:92
static int flags
Definition: log.c:57
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
int av_match_ext(const char *filename, const char *extensions)
Return a positive value if the given filename has one of the given extensions, 0 otherwise.
Definition: format.c:85
uint64_t publish_time
Definition: dashdec.c:128
ptrdiff_t size
Definition: opengl_enc.c:101
uint64_t availability_start_time
Definition: dashdec.c:127
static enum AVMediaType get_content_type(xmlNodePtr node)
Definition: dashdec.c:501
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:637
#define INITIAL_BUFFER_SIZE
Definition: dashdec.c:31
static xmlNodePtr find_child_node_by_name(xmlNodePtr rootnode, const char *nodename)
Definition: dashdec.c:484
Callback for checking whether to abort blocking functions.
Definition: avio.h:58
static char * get_content_url(xmlNodePtr *baseurl_nodes, int n_baseurl_nodes, char *rep_id_val, char *rep_bandwidth_val, char *val)
Definition: dashdec.c:419
struct representation * cur_video
Definition: dashdec.c:121
int av_probe_input_buffer(AVIOContext *pb, AVInputFormat **fmt, const char *url, void *logctx, unsigned int offset, unsigned int max_probe_size)
Like av_probe_input_buffer2() but returns 0 on success.
Definition: format.c:364
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2279
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
uint32_t init_sec_data_len
Definition: dashdec.c:112
static void free_timelines_list(struct representation *pls)
Definition: dashdec.c:304
int64_t starttime
Definition: dashdec.c:57
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
static int64_t calc_cur_seg_no(AVFormatContext *s, struct representation *pls)
Definition: dashdec.c:1035
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:1112
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
static int dash_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: dashdec.c:1783
char * av_strireplace(const char *str, const char *from, const char *to)
Locale-independent strings replace.
Definition: avstring.c:234
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
New fields can be added to the end with minor version bumps.
Definition: avformat.h:1276
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:83
#define fail()
Definition: checkasm.h:109
static int read_from_url(struct representation *pls, struct fragment *seg, uint8_t *buf, int buf_size, enum ReadFromURLMode mode)
Definition: dashdec.c:1275
static int parse_manifest_adaptationset(AVFormatContext *s, const char *url, xmlNodePtr adaptionset_node, xmlNodePtr mpd_baseurl_node, xmlNodePtr period_baseurl_node)
Definition: dashdec.c:826
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:469
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:463
uint64_t minimum_update_period
Definition: dashdec.c:129
struct fragment ** fragments
Definition: dashdec.c:89
static void free_representation(struct representation *pls)
Definition: dashdec.c:315
AVIOInterruptCB * interrupt_callback
Definition: dashdec.c:138
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1405
AVDictionary * opts
Definition: movenc.c:50
char * user_agent
holds HTTP user agent set as an AVOption to the HTTP protocol context
Definition: dashdec.c:139
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:261
static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos_msec, int flags)
Definition: dashdec.c:1721
#define dynarray_add(tab, nb_ptr, elem)
Definition: internal.h:202
char filename[1024]
input or output filename
Definition: avformat.h:1425
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned int idx)
#define FFMIN(a, b)
Definition: common.h:96
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:213
static void free_fragment_list(struct representation *pls)
Definition: dashdec.c:293
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:557
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:76
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
int32_t
static uint32_t get_duration_insec(AVFormatContext *s, const char *duration)
Definition: dashdec.c:181
int is_live
Definition: dashdec.c:137
enum AVMediaType type
Definition: dashdec.c:86
#define OFFSET(x)
Definition: dashdec.c:1820
static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url, AVDictionary *opts, AVDictionary *opts2, int *is_http)
Definition: dashdec.c:352
static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
Definition: dashdec.c:862
#define AVFMT_FLAG_CUSTOM_IO
The caller has supplied a custom AVIOContext, don't avio_close() it.
Definition: avformat.h:1468
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:200
static int save_avio_options(AVFormatContext *s)
Definition: dashdec.c:1458
char * url
Definition: dashdec.c:36
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:58
uint64_t period_start
Definition: dashdec.c:135
int64_t max_analyze_duration
Maximum duration (in AV_TIME_BASE units) of the data read from input in avformat_find_stream_info().
Definition: avformat.h:1501
static char * get_val_from_nodes_tab(xmlNodePtr *nodes, const int n_nodes, const char *attrname)
Definition: dashdec.c:468
static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **opts)
Definition: dashdec.c:1479
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:528
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
static int dash_probe(AVProbeData *p)
Definition: dashdec.c:1802
Stream structure.
Definition: avformat.h:889
void ff_dash_fill_tmpl_params(char *dst, size_t buffer_size, const char *template, int rep_id, int number, int bit_rate, int64_t time)
Definition: dash.c:96
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVFormatContext * parent
Definition: dashdec.c:79
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:237
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrupt a blocking function associated with cb.
Definition: avio.c:660
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
int64_t last_seq_no
Definition: dashdec.c:95
uint32_t init_sec_buf_size
Definition: dashdec.c:111
int64_t cur_seq_no
Definition: dashdec.c:103
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
static int dash_read_header(AVFormatContext *s)
Definition: dashdec.c:1586
void * buf
Definition: avisynth_c.h:690
GLint GLenum type
Definition: opengl_enc.c:105
uint64_t time_shift_buffer_depth
Definition: dashdec.c:130
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
Describe the class of an AVClass context structure.
Definition: log.h:67
static void update_options(char **dest, const char *name, void *src)
Definition: dashdec.c:344
static int open_input(DASHContext *c, struct representation *pls, struct fragment *seg)
Definition: dashdec.c:1299
static int dash_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dashdec.c:1662
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition: avformat.h:2424
AVMediaType
Definition: avutil.h:199
static int parse_manifest_segmenttimeline(AVFormatContext *s, struct representation *rep, xmlNodePtr fragment_timeline_node)
Definition: dashdec.c:580
int avio_open2(AVIOContext **s, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:1100
static void move_timelines(struct representation *rep_src, struct representation *rep_dest)
Definition: dashdec.c:1104
AVDictionary * avio_opts
Definition: dashdec.c:143
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:4302
#define u(width,...)
This structure contains the data a format has to probe a file.
Definition: avformat.h:461
misc parsing utilities
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1713
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
Definition: avstring.c:93
Round toward -infinity.
Definition: mathematics.h:82
const char * avio_find_protocol_name(const char *url)
Return the name of the protocol that will handle the passed URL.
Definition: avio.c:471
AVInputFormat ff_dash_demuxer
Definition: dashdec.c:1837
int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Seek to the keyframe at timestamp.
Definition: utils.c:2450
static int update_init_section(struct representation *pls)
Definition: dashdec.c:1340
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:473
char * cookies
holds HTTP cookie values set in either the initial response or as an AVOption to the HTTP protocol co...
Definition: dashdec.c:140
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:34
int
int64_t duration
Definition: dashdec.c:67
if(ret< 0)
Definition: vf_mcdeint.c:279
int ffio_init_context(AVIOContext *s, unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition: aviobuf.c:81
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: utils.c:3498
static const AVClass dash_class
Definition: dashdec.c:1830
static double c[64]
int64_t fragment_duration
Definition: dashdec.c:98
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set that converts the value to a string and stores it...
Definition: dict.c:147
struct fragment * cur_seg
Definition: dashdec.c:106
int pts_wrap_bits
number of bits in pts (used for wrapping control)
Definition: avformat.h:1055
int den
Denominator.
Definition: rational.h:60
AVFormatContext * ctx
Definition: dashdec.c:80
int rep_count
Definition: dashdec.c:83
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:4339
int64_t fragment_timescale
Definition: dashdec.c:99
int is_restart_needed
Definition: dashdec.c:115
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
Definition: opt.c:751
#define av_free(p)
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:494
uint8_t * init_sec_buf
Definition: dashdec.c:110
void * priv_data
Format private data.
Definition: avformat.h:1377
int64_t start_number
Definition: dashdec.c:96
static uint64_t get_current_time_in_sec(void)
Definition: dashdec.c:146
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:510
static int64_t calc_min_seg_no(AVFormatContext *s, struct representation *pls)
Definition: dashdec.c:1069
int64_t cur_seg_offset
Definition: dashdec.c:104
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1444
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:664
AVCodecParameters * codecpar
Definition: avformat.h:1252
char * url_template
Definition: dashdec.c:76
int stream_index
Definition: avcodec.h:1681
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:926
static int reopen_demux_for_component(AVFormatContext *s, struct representation *pls)
Definition: dashdec.c:1489
int64_t repeat
Definition: dashdec.c:63
int(* io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
Definition: avformat.h:1909
This structure stores compressed data.
Definition: avcodec.h:1656
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
static int64_t calc_next_seg_no_from_timelines(struct representation *pls, int64_t cur_time)
Definition: dashdec.c:254
char * base_url
Definition: dashdec.c:120
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1672
static int open_demux_for_component(AVFormatContext *s, struct representation *pls)
Definition: dashdec.c:1556
GLuint buffer
Definition: opengl_enc.c:102
static av_cold void cleanup(FlashSV2Context *s)
Definition: flashsv2enc.c:127
const char * name
Definition: opengl_enc.c:103
static int refresh_manifest(AVFormatContext *s)
Definition: dashdec.c:1136
static uint8_t tmp[11]
Definition: aes_ctr.c:26