You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1849 lines
62KB

  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. #define INITIAL_BUFFER_SIZE 32768
  31. struct fragment {
  32. int64_t url_offset;
  33. int64_t size;
  34. char *url;
  35. };
  36. /*
  37. * reference to : ISO_IEC_23009-1-DASH-2012
  38. * Section: 5.3.9.6.2
  39. * Table: Table 17 — Semantics of SegmentTimeline element
  40. * */
  41. struct timeline {
  42. /* starttime: Element or Attribute Name
  43. * specifies the MPD start time, in @timescale units,
  44. * the first Segment in the series starts relative to the beginning of the Period.
  45. * The value of this attribute must be equal to or greater than the sum of the previous S
  46. * element earliest presentation time and the sum of the contiguous Segment durations.
  47. * If the value of the attribute is greater than what is expressed by the previous S element,
  48. * it expresses discontinuities in the timeline.
  49. * If not present then the value shall be assumed to be zero for the first S element
  50. * and for the subsequent S elements, the value shall be assumed to be the sum of
  51. * the previous S element's earliest presentation time and contiguous duration
  52. * (i.e. previous S@starttime + @duration * (@repeat + 1)).
  53. * */
  54. int64_t starttime;
  55. /* repeat: Element or Attribute Name
  56. * specifies the repeat count of the number of following contiguous Segments with
  57. * the same duration expressed by the value of @duration. This value is zero-based
  58. * (e.g. a value of three means four Segments in the contiguous series).
  59. * */
  60. int64_t repeat;
  61. /* duration: Element or Attribute Name
  62. * specifies the Segment duration, in units of the value of the @timescale.
  63. * */
  64. int64_t duration;
  65. };
  66. /*
  67. * Each playlist has its own demuxer. If it is currently active,
  68. * it has an opened AVIOContext too, and potentially an AVPacket
  69. * containing the next packet from this stream.
  70. */
  71. struct representation {
  72. char *url_template;
  73. AVIOContext pb;
  74. AVIOContext *input;
  75. AVFormatContext *parent;
  76. AVFormatContext *ctx;
  77. AVPacket pkt;
  78. int rep_idx;
  79. int rep_count;
  80. int stream_index;
  81. enum AVMediaType type;
  82. int n_fragments;
  83. struct fragment **fragments; /* VOD list of fragment for profile */
  84. int n_timelines;
  85. struct timeline **timelines;
  86. int64_t first_seq_no;
  87. int64_t last_seq_no;
  88. int64_t start_number; /* used in case when we have dynamic list of segment to know which segments are new one*/
  89. int64_t fragment_duration;
  90. int64_t fragment_timescale;
  91. int64_t presentation_timeoffset;
  92. int64_t cur_seq_no;
  93. int64_t cur_seg_offset;
  94. int64_t cur_seg_size;
  95. struct fragment *cur_seg;
  96. /* Currently active Media Initialization Section */
  97. struct fragment *init_section;
  98. uint8_t *init_sec_buf;
  99. uint32_t init_sec_buf_size;
  100. uint32_t init_sec_data_len;
  101. uint32_t init_sec_buf_read_offset;
  102. int64_t cur_timestamp;
  103. int is_restart_needed;
  104. };
  105. typedef struct DASHContext {
  106. const AVClass *class;
  107. char *base_url;
  108. struct representation *cur_video;
  109. struct representation *cur_audio;
  110. /* MediaPresentationDescription Attribute */
  111. uint64_t media_presentation_duration;
  112. uint64_t suggested_presentation_delay;
  113. uint64_t availability_start_time;
  114. uint64_t publish_time;
  115. uint64_t minimum_update_period;
  116. uint64_t time_shift_buffer_depth;
  117. uint64_t min_buffer_time;
  118. /* Period Attribute */
  119. uint64_t period_duration;
  120. uint64_t period_start;
  121. int is_live;
  122. AVIOInterruptCB *interrupt_callback;
  123. char *user_agent; ///< holds HTTP user agent set as an AVOption to the HTTP protocol context
  124. char *cookies; ///< holds HTTP cookie values set in either the initial response or as an AVOption to the HTTP protocol context
  125. char *headers; ///< holds HTTP headers set as an AVOption to the HTTP protocol context
  126. char *allowed_extensions;
  127. AVDictionary *avio_opts;
  128. } DASHContext;
  129. static uint64_t get_current_time_in_sec(void)
  130. {
  131. return av_gettime() / 1000000;
  132. }
  133. static uint64_t get_utc_date_time_insec(AVFormatContext *s, const char *datetime)
  134. {
  135. struct tm timeinfo;
  136. int year = 0;
  137. int month = 0;
  138. int day = 0;
  139. int hour = 0;
  140. int minute = 0;
  141. int ret = 0;
  142. float second = 0.0;
  143. /* ISO-8601 date parser */
  144. if (!datetime)
  145. return 0;
  146. ret = sscanf(datetime, "%d-%d-%dT%d:%d:%fZ", &year, &month, &day, &hour, &minute, &second);
  147. /* year, month, day, hour, minute, second 6 arguments */
  148. if (ret != 6) {
  149. av_log(s, AV_LOG_WARNING, "get_utc_date_time_insec get a wrong time format\n");
  150. }
  151. timeinfo.tm_year = year - 1900;
  152. timeinfo.tm_mon = month - 1;
  153. timeinfo.tm_mday = day;
  154. timeinfo.tm_hour = hour;
  155. timeinfo.tm_min = minute;
  156. timeinfo.tm_sec = (int)second;
  157. return av_timegm(&timeinfo);
  158. }
  159. static uint32_t get_duration_insec(AVFormatContext *s, const char *duration)
  160. {
  161. /* ISO-8601 duration parser */
  162. uint32_t days = 0;
  163. uint32_t hours = 0;
  164. uint32_t mins = 0;
  165. uint32_t secs = 0;
  166. int size = 0;
  167. float value = 0;
  168. char type = '\0';
  169. const char *ptr = duration;
  170. while (*ptr) {
  171. if (*ptr == 'P' || *ptr == 'T') {
  172. ptr++;
  173. continue;
  174. }
  175. if (sscanf(ptr, "%f%c%n", &value, &type, &size) != 2) {
  176. av_log(s, AV_LOG_WARNING, "get_duration_insec get a wrong time format\n");
  177. return 0; /* parser error */
  178. }
  179. switch (type) {
  180. case 'D':
  181. days = (uint32_t)value;
  182. break;
  183. case 'H':
  184. hours = (uint32_t)value;
  185. break;
  186. case 'M':
  187. mins = (uint32_t)value;
  188. break;
  189. case 'S':
  190. secs = (uint32_t)value;
  191. break;
  192. default:
  193. // handle invalid type
  194. break;
  195. }
  196. ptr += size;
  197. }
  198. return ((days * 24 + hours) * 60 + mins) * 60 + secs;
  199. }
  200. static int64_t get_segment_start_time_based_on_timeline(struct representation *pls, int64_t cur_seq_no)
  201. {
  202. int64_t start_time = 0;
  203. int64_t i = 0;
  204. int64_t j = 0;
  205. int64_t num = 0;
  206. if (pls->n_timelines) {
  207. for (i = 0; i < pls->n_timelines; i++) {
  208. if (pls->timelines[i]->starttime > 0) {
  209. start_time = pls->timelines[i]->starttime;
  210. }
  211. if (num == cur_seq_no)
  212. goto finish;
  213. start_time += pls->timelines[i]->duration;
  214. for (j = 0; j < pls->timelines[i]->repeat; j++) {
  215. num++;
  216. if (num == cur_seq_no)
  217. goto finish;
  218. start_time += pls->timelines[i]->duration;
  219. }
  220. num++;
  221. }
  222. }
  223. finish:
  224. return start_time;
  225. }
  226. static int64_t calc_next_seg_no_from_timelines(struct representation *pls, int64_t cur_time)
  227. {
  228. int64_t i = 0;
  229. int64_t j = 0;
  230. int64_t num = 0;
  231. int64_t start_time = 0;
  232. for (i = 0; i < pls->n_timelines; i++) {
  233. if (pls->timelines[i]->starttime > 0) {
  234. start_time = pls->timelines[i]->starttime;
  235. }
  236. if (start_time > cur_time)
  237. goto finish;
  238. start_time += pls->timelines[i]->duration;
  239. for (j = 0; j < pls->timelines[i]->repeat; j++) {
  240. num++;
  241. if (start_time > cur_time)
  242. goto finish;
  243. start_time += pls->timelines[i]->duration;
  244. }
  245. num++;
  246. }
  247. return -1;
  248. finish:
  249. return num;
  250. }
  251. static void free_fragment(struct fragment **seg)
  252. {
  253. if (!(*seg)) {
  254. return;
  255. }
  256. av_freep(&(*seg)->url);
  257. av_freep(seg);
  258. }
  259. static void free_fragment_list(struct representation *pls)
  260. {
  261. int i;
  262. for (i = 0; i < pls->n_fragments; i++) {
  263. free_fragment(&pls->fragments[i]);
  264. }
  265. av_freep(&pls->fragments);
  266. pls->n_fragments = 0;
  267. }
  268. static void free_timelines_list(struct representation *pls)
  269. {
  270. int i;
  271. for (i = 0; i < pls->n_timelines; i++) {
  272. av_freep(&pls->timelines[i]);
  273. }
  274. av_freep(&pls->timelines);
  275. pls->n_timelines = 0;
  276. }
  277. static void free_representation(struct representation *pls)
  278. {
  279. free_fragment_list(pls);
  280. free_timelines_list(pls);
  281. free_fragment(&pls->cur_seg);
  282. free_fragment(&pls->init_section);
  283. av_freep(&pls->init_sec_buf);
  284. av_freep(&pls->pb.buffer);
  285. if (pls->input)
  286. ff_format_io_close(pls->parent, &pls->input);
  287. if (pls->ctx) {
  288. pls->ctx->pb = NULL;
  289. avformat_close_input(&pls->ctx);
  290. }
  291. av_freep(&pls->url_template);
  292. av_freep(pls);
  293. }
  294. static void set_httpheader_options(DASHContext *c, AVDictionary *opts)
  295. {
  296. // broker prior HTTP options that should be consistent across requests
  297. av_dict_set(&opts, "user-agent", c->user_agent, 0);
  298. av_dict_set(&opts, "cookies", c->cookies, 0);
  299. av_dict_set(&opts, "headers", c->headers, 0);
  300. if (c->is_live) {
  301. av_dict_set(&opts, "seekable", "0", 0);
  302. }
  303. }
  304. static void update_options(char **dest, const char *name, void *src)
  305. {
  306. av_freep(dest);
  307. av_opt_get(src, name, AV_OPT_SEARCH_CHILDREN, (uint8_t**)dest);
  308. if (*dest)
  309. av_freep(dest);
  310. }
  311. static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
  312. AVDictionary *opts, AVDictionary *opts2, int *is_http)
  313. {
  314. DASHContext *c = s->priv_data;
  315. AVDictionary *tmp = NULL;
  316. const char *proto_name = NULL;
  317. int ret;
  318. av_dict_copy(&tmp, opts, 0);
  319. av_dict_copy(&tmp, opts2, 0);
  320. if (av_strstart(url, "crypto", NULL)) {
  321. if (url[6] == '+' || url[6] == ':')
  322. proto_name = avio_find_protocol_name(url + 7);
  323. }
  324. if (!proto_name)
  325. proto_name = avio_find_protocol_name(url);
  326. if (!proto_name)
  327. return AVERROR_INVALIDDATA;
  328. // only http(s) & file are allowed
  329. if (av_strstart(proto_name, "file", NULL)) {
  330. if (strcmp(c->allowed_extensions, "ALL") && !av_match_ext(url, c->allowed_extensions)) {
  331. av_log(s, AV_LOG_ERROR,
  332. "Filename extension of \'%s\' is not a common multimedia extension, blocked for security reasons.\n"
  333. "If you wish to override this adjust allowed_extensions, you can set it to \'ALL\' to allow all\n",
  334. url);
  335. return AVERROR_INVALIDDATA;
  336. }
  337. } else if (av_strstart(proto_name, "http", NULL)) {
  338. ;
  339. } else
  340. return AVERROR_INVALIDDATA;
  341. if (!strncmp(proto_name, url, strlen(proto_name)) && url[strlen(proto_name)] == ':')
  342. ;
  343. else if (av_strstart(url, "crypto", NULL) && !strncmp(proto_name, url + 7, strlen(proto_name)) && url[7 + strlen(proto_name)] == ':')
  344. ;
  345. else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5))
  346. return AVERROR_INVALIDDATA;
  347. ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp);
  348. if (ret >= 0) {
  349. // update cookies on http response with setcookies.
  350. char *new_cookies = NULL;
  351. if (!(s->flags & AVFMT_FLAG_CUSTOM_IO))
  352. av_opt_get(*pb, "cookies", AV_OPT_SEARCH_CHILDREN, (uint8_t**)&new_cookies);
  353. if (new_cookies) {
  354. av_free(c->cookies);
  355. c->cookies = new_cookies;
  356. }
  357. av_dict_set(&opts, "cookies", c->cookies, 0);
  358. }
  359. av_dict_free(&tmp);
  360. if (is_http)
  361. *is_http = av_strstart(proto_name, "http", NULL);
  362. return ret;
  363. }
  364. static char *get_content_url(xmlNodePtr *baseurl_nodes,
  365. int n_baseurl_nodes,
  366. char *rep_id_val,
  367. char *rep_bandwidth_val,
  368. char *val)
  369. {
  370. int i;
  371. char *text;
  372. char *url = NULL;
  373. char tmp_str[MAX_URL_SIZE];
  374. char tmp_str_2[MAX_URL_SIZE];
  375. memset(tmp_str, 0, sizeof(tmp_str));
  376. for (i = 0; i < n_baseurl_nodes; ++i) {
  377. if (baseurl_nodes[i] &&
  378. baseurl_nodes[i]->children &&
  379. baseurl_nodes[i]->children->type == XML_TEXT_NODE) {
  380. text = xmlNodeGetContent(baseurl_nodes[i]->children);
  381. if (text) {
  382. memset(tmp_str, 0, sizeof(tmp_str));
  383. memset(tmp_str_2, 0, sizeof(tmp_str_2));
  384. ff_make_absolute_url(tmp_str_2, MAX_URL_SIZE, tmp_str, text);
  385. av_strlcpy(tmp_str, tmp_str_2, sizeof(tmp_str));
  386. xmlFree(text);
  387. }
  388. }
  389. }
  390. if (val)
  391. av_strlcat(tmp_str, (const char*)val, sizeof(tmp_str));
  392. if (rep_id_val) {
  393. url = av_strireplace(tmp_str, "$RepresentationID$", (const char*)rep_id_val);
  394. if (!url) {
  395. return NULL;
  396. }
  397. av_strlcpy(tmp_str, url, sizeof(tmp_str));
  398. av_free(url);
  399. }
  400. if (rep_bandwidth_val && tmp_str[0] != '\0') {
  401. url = av_strireplace(tmp_str, "$Bandwidth$", (const char*)rep_bandwidth_val);
  402. if (!url) {
  403. return NULL;
  404. }
  405. }
  406. return url;
  407. }
  408. static char *get_val_from_nodes_tab(xmlNodePtr *nodes, const int n_nodes, const char *attrname)
  409. {
  410. int i;
  411. char *val;
  412. for (i = 0; i < n_nodes; ++i) {
  413. if (nodes[i]) {
  414. val = xmlGetProp(nodes[i], attrname);
  415. if (val)
  416. return val;
  417. }
  418. }
  419. return NULL;
  420. }
  421. static xmlNodePtr find_child_node_by_name(xmlNodePtr rootnode, const char *nodename)
  422. {
  423. xmlNodePtr node = rootnode;
  424. if (!node) {
  425. return NULL;
  426. }
  427. node = xmlFirstElementChild(node);
  428. while (node) {
  429. if (!av_strcasecmp(node->name, nodename)) {
  430. return node;
  431. }
  432. node = xmlNextElementSibling(node);
  433. }
  434. return NULL;
  435. }
  436. static enum AVMediaType get_content_type(xmlNodePtr node)
  437. {
  438. enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
  439. int i = 0;
  440. const char *attr;
  441. char *val = NULL;
  442. if (node) {
  443. for (i = 0; i < 2; i++) {
  444. attr = i ? "mimeType" : "contentType";
  445. val = xmlGetProp(node, attr);
  446. if (val) {
  447. if (av_stristr((const char *)val, "video")) {
  448. type = AVMEDIA_TYPE_VIDEO;
  449. } else if (av_stristr((const char *)val, "audio")) {
  450. type = AVMEDIA_TYPE_AUDIO;
  451. }
  452. xmlFree(val);
  453. }
  454. }
  455. }
  456. return type;
  457. }
  458. static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representation *rep,
  459. xmlNodePtr fragmenturl_node,
  460. xmlNodePtr *baseurl_nodes,
  461. char *rep_id_val,
  462. char *rep_bandwidth_val)
  463. {
  464. char *initialization_val = NULL;
  465. char *media_val = NULL;
  466. if (!av_strcasecmp(fragmenturl_node->name, (const char *)"Initialization")) {
  467. initialization_val = xmlGetProp(fragmenturl_node, "sourceURL");
  468. if (initialization_val) {
  469. rep->init_section = av_mallocz(sizeof(struct fragment));
  470. if (!rep->init_section) {
  471. xmlFree(initialization_val);
  472. return AVERROR(ENOMEM);
  473. }
  474. rep->init_section->url = get_content_url(baseurl_nodes, 4,
  475. rep_id_val,
  476. rep_bandwidth_val,
  477. initialization_val);
  478. if (!rep->init_section->url) {
  479. av_free(rep->init_section);
  480. xmlFree(initialization_val);
  481. return AVERROR(ENOMEM);
  482. }
  483. rep->init_section->size = -1;
  484. xmlFree(initialization_val);
  485. }
  486. } else if (!av_strcasecmp(fragmenturl_node->name, (const char *)"SegmentURL")) {
  487. media_val = xmlGetProp(fragmenturl_node, "media");
  488. if (media_val) {
  489. struct fragment *seg = av_mallocz(sizeof(struct fragment));
  490. if (!seg) {
  491. xmlFree(media_val);
  492. return AVERROR(ENOMEM);
  493. }
  494. seg->url = get_content_url(baseurl_nodes, 4,
  495. rep_id_val,
  496. rep_bandwidth_val,
  497. media_val);
  498. if (!seg->url) {
  499. av_free(seg);
  500. xmlFree(media_val);
  501. return AVERROR(ENOMEM);
  502. }
  503. seg->size = -1;
  504. dynarray_add(&rep->fragments, &rep->n_fragments, seg);
  505. xmlFree(media_val);
  506. }
  507. }
  508. return 0;
  509. }
  510. static int parse_manifest_segmenttimeline(AVFormatContext *s, struct representation *rep,
  511. xmlNodePtr fragment_timeline_node)
  512. {
  513. xmlAttrPtr attr = NULL;
  514. char *val = NULL;
  515. if (!av_strcasecmp(fragment_timeline_node->name, (const char *)"S")) {
  516. struct timeline *tml = av_mallocz(sizeof(struct timeline));
  517. if (!tml) {
  518. return AVERROR(ENOMEM);
  519. }
  520. attr = fragment_timeline_node->properties;
  521. while (attr) {
  522. val = xmlGetProp(fragment_timeline_node, attr->name);
  523. if (!val) {
  524. av_log(s, AV_LOG_WARNING, "parse_manifest_segmenttimeline attr->name = %s val is NULL\n", attr->name);
  525. continue;
  526. }
  527. if (!av_strcasecmp(attr->name, (const char *)"t")) {
  528. tml->starttime = (int64_t)strtoll(val, NULL, 10);
  529. } else if (!av_strcasecmp(attr->name, (const char *)"r")) {
  530. tml->repeat =(int64_t) strtoll(val, NULL, 10);
  531. } else if (!av_strcasecmp(attr->name, (const char *)"d")) {
  532. tml->duration = (int64_t)strtoll(val, NULL, 10);
  533. }
  534. attr = attr->next;
  535. xmlFree(val);
  536. }
  537. dynarray_add(&rep->timelines, &rep->n_timelines, tml);
  538. }
  539. return 0;
  540. }
  541. static int parse_manifest_representation(AVFormatContext *s, const char *url,
  542. xmlNodePtr node,
  543. xmlNodePtr adaptionset_node,
  544. xmlNodePtr mpd_baseurl_node,
  545. xmlNodePtr period_baseurl_node,
  546. xmlNodePtr fragment_template_node,
  547. xmlNodePtr content_component_node,
  548. xmlNodePtr adaptionset_baseurl_node)
  549. {
  550. int32_t ret = 0;
  551. int32_t audio_rep_idx = 0;
  552. int32_t video_rep_idx = 0;
  553. DASHContext *c = s->priv_data;
  554. struct representation *rep = NULL;
  555. struct fragment *seg = NULL;
  556. xmlNodePtr representation_segmenttemplate_node = NULL;
  557. xmlNodePtr representation_baseurl_node = NULL;
  558. xmlNodePtr representation_segmentlist_node = NULL;
  559. xmlNodePtr fragment_timeline_node = NULL;
  560. xmlNodePtr fragment_templates_tab[2];
  561. char *duration_val = NULL;
  562. char *presentation_timeoffset_val = NULL;
  563. char *startnumber_val = NULL;
  564. char *timescale_val = NULL;
  565. char *initialization_val = NULL;
  566. char *media_val = NULL;
  567. xmlNodePtr baseurl_nodes[4];
  568. xmlNodePtr representation_node = node;
  569. char *rep_id_val = xmlGetProp(representation_node, "id");
  570. char *rep_bandwidth_val = xmlGetProp(representation_node, "bandwidth");
  571. enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
  572. // try get information from representation
  573. if (type == AVMEDIA_TYPE_UNKNOWN)
  574. type = get_content_type(representation_node);
  575. // try get information from contentComponen
  576. if (type == AVMEDIA_TYPE_UNKNOWN)
  577. type = get_content_type(content_component_node);
  578. // try get information from adaption set
  579. if (type == AVMEDIA_TYPE_UNKNOWN)
  580. type = get_content_type(adaptionset_node);
  581. if (type == AVMEDIA_TYPE_UNKNOWN) {
  582. av_log(s, AV_LOG_VERBOSE, "Parsing '%s' - skipp not supported representation type\n", url);
  583. } else if ((type == AVMEDIA_TYPE_VIDEO && !c->cur_video) || (type == AVMEDIA_TYPE_AUDIO && !c->cur_audio)) {
  584. // convert selected representation to our internal struct
  585. rep = av_mallocz(sizeof(struct representation));
  586. if (!rep) {
  587. ret = AVERROR(ENOMEM);
  588. goto end;
  589. }
  590. representation_segmenttemplate_node = find_child_node_by_name(representation_node, "SegmentTemplate");
  591. representation_baseurl_node = find_child_node_by_name(representation_node, "BaseURL");
  592. representation_segmentlist_node = find_child_node_by_name(representation_node, "SegmentList");
  593. baseurl_nodes[0] = mpd_baseurl_node;
  594. baseurl_nodes[1] = period_baseurl_node;
  595. baseurl_nodes[2] = adaptionset_baseurl_node;
  596. baseurl_nodes[3] = representation_baseurl_node;
  597. if (representation_segmenttemplate_node || fragment_template_node) {
  598. fragment_timeline_node = NULL;
  599. fragment_templates_tab[0] = representation_segmenttemplate_node;
  600. fragment_templates_tab[1] = fragment_template_node;
  601. presentation_timeoffset_val = get_val_from_nodes_tab(fragment_templates_tab, 2, "presentationTimeOffset");
  602. duration_val = get_val_from_nodes_tab(fragment_templates_tab, 2, "duration");
  603. startnumber_val = get_val_from_nodes_tab(fragment_templates_tab, 2, "startNumber");
  604. timescale_val = get_val_from_nodes_tab(fragment_templates_tab, 2, "timescale");
  605. initialization_val = get_val_from_nodes_tab(fragment_templates_tab, 2, "initialization");
  606. media_val = get_val_from_nodes_tab(fragment_templates_tab, 2, "media");
  607. if (initialization_val) {
  608. rep->init_section = av_mallocz(sizeof(struct fragment));
  609. if (!rep->init_section) {
  610. av_free(rep);
  611. ret = AVERROR(ENOMEM);
  612. goto end;
  613. }
  614. rep->init_section->url = get_content_url(baseurl_nodes, 4, rep_id_val, rep_bandwidth_val, initialization_val);
  615. if (!rep->init_section->url) {
  616. av_free(rep->init_section);
  617. av_free(rep);
  618. ret = AVERROR(ENOMEM);
  619. goto end;
  620. }
  621. rep->init_section->size = -1;
  622. xmlFree(initialization_val);
  623. }
  624. if (media_val) {
  625. rep->url_template = get_content_url(baseurl_nodes, 4, rep_id_val, rep_bandwidth_val, media_val);
  626. xmlFree(media_val);
  627. }
  628. if (presentation_timeoffset_val) {
  629. rep->presentation_timeoffset = (int64_t) strtoll(presentation_timeoffset_val, NULL, 10);
  630. xmlFree(presentation_timeoffset_val);
  631. }
  632. if (duration_val) {
  633. rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
  634. xmlFree(duration_val);
  635. }
  636. if (timescale_val) {
  637. rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
  638. xmlFree(timescale_val);
  639. }
  640. if (startnumber_val) {
  641. rep->first_seq_no = (int64_t) strtoll(startnumber_val, NULL, 10);
  642. xmlFree(startnumber_val);
  643. }
  644. fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
  645. if (!fragment_timeline_node)
  646. fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
  647. if (fragment_timeline_node) {
  648. fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
  649. while (fragment_timeline_node) {
  650. ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
  651. if (ret < 0) {
  652. return ret;
  653. }
  654. fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
  655. }
  656. }
  657. } else if (representation_baseurl_node && !representation_segmentlist_node) {
  658. seg = av_mallocz(sizeof(struct fragment));
  659. if (!seg) {
  660. ret = AVERROR(ENOMEM);
  661. goto end;
  662. }
  663. seg->url = get_content_url(baseurl_nodes, 4, rep_id_val, rep_bandwidth_val, NULL);
  664. if (!seg->url) {
  665. av_free(seg);
  666. ret = AVERROR(ENOMEM);
  667. goto end;
  668. }
  669. seg->size = -1;
  670. dynarray_add(&rep->fragments, &rep->n_fragments, seg);
  671. } else if (representation_segmentlist_node) {
  672. // TODO: https://www.brendanlong.com/the-structure-of-an-mpeg-dash-mpd.html
  673. // http://www-itec.uni-klu.ac.at/dash/ddash/mpdGenerator.php?fragmentlength=15&type=full
  674. xmlNodePtr fragmenturl_node = NULL;
  675. duration_val = xmlGetProp(representation_segmentlist_node, "duration");
  676. timescale_val = xmlGetProp(representation_segmentlist_node, "timescale");
  677. if (duration_val) {
  678. rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
  679. xmlFree(duration_val);
  680. }
  681. if (timescale_val) {
  682. rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
  683. xmlFree(timescale_val);
  684. }
  685. fragmenturl_node = xmlFirstElementChild(representation_segmentlist_node);
  686. while (fragmenturl_node) {
  687. ret = parse_manifest_segmenturlnode(s, rep, fragmenturl_node,
  688. baseurl_nodes,
  689. rep_id_val,
  690. rep_bandwidth_val);
  691. if (ret < 0) {
  692. return ret;
  693. }
  694. fragmenturl_node = xmlNextElementSibling(fragmenturl_node);
  695. }
  696. fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
  697. if (!fragment_timeline_node)
  698. fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
  699. if (fragment_timeline_node) {
  700. fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
  701. while (fragment_timeline_node) {
  702. ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
  703. if (ret < 0) {
  704. return ret;
  705. }
  706. fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
  707. }
  708. }
  709. } else {
  710. free_representation(rep);
  711. rep = NULL;
  712. av_log(s, AV_LOG_ERROR, "Unknown format of Representation node id[%s] \n", (const char *)rep_id_val);
  713. }
  714. if (rep) {
  715. if (rep->fragment_duration > 0 && !rep->fragment_timescale)
  716. rep->fragment_timescale = 1;
  717. if (type == AVMEDIA_TYPE_VIDEO) {
  718. rep->rep_idx = video_rep_idx;
  719. c->cur_video = rep;
  720. } else {
  721. rep->rep_idx = audio_rep_idx;
  722. c->cur_audio = rep;
  723. }
  724. }
  725. }
  726. video_rep_idx += type == AVMEDIA_TYPE_VIDEO;
  727. audio_rep_idx += type == AVMEDIA_TYPE_AUDIO;
  728. end:
  729. if (rep_id_val)
  730. xmlFree(rep_id_val);
  731. if (rep_bandwidth_val)
  732. xmlFree(rep_bandwidth_val);
  733. return ret;
  734. }
  735. static int parse_manifest_adaptationset(AVFormatContext *s, const char *url,
  736. xmlNodePtr adaptionset_node,
  737. xmlNodePtr mpd_baseurl_node,
  738. xmlNodePtr period_baseurl_node)
  739. {
  740. int ret = 0;
  741. xmlNodePtr fragment_template_node = NULL;
  742. xmlNodePtr content_component_node = NULL;
  743. xmlNodePtr adaptionset_baseurl_node = NULL;
  744. xmlNodePtr node = NULL;
  745. node = xmlFirstElementChild(adaptionset_node);
  746. while (node) {
  747. if (!av_strcasecmp(node->name, (const char *)"SegmentTemplate")) {
  748. fragment_template_node = node;
  749. } else if (!av_strcasecmp(node->name, (const char *)"ContentComponent")) {
  750. content_component_node = node;
  751. } else if (!av_strcasecmp(node->name, (const char *)"BaseURL")) {
  752. adaptionset_baseurl_node = node;
  753. } else if (!av_strcasecmp(node->name, (const char *)"Representation")) {
  754. ret = parse_manifest_representation(s, url, node,
  755. adaptionset_node,
  756. mpd_baseurl_node,
  757. period_baseurl_node,
  758. fragment_template_node,
  759. content_component_node,
  760. adaptionset_baseurl_node);
  761. if (ret < 0) {
  762. return ret;
  763. }
  764. }
  765. node = xmlNextElementSibling(node);
  766. }
  767. return 0;
  768. }
  769. static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
  770. {
  771. DASHContext *c = s->priv_data;
  772. int ret = 0;
  773. int close_in = 0;
  774. uint8_t *new_url = NULL;
  775. int64_t filesize = 0;
  776. char *buffer = NULL;
  777. AVDictionary *opts = NULL;
  778. xmlDoc *doc = NULL;
  779. xmlNodePtr root_element = NULL;
  780. xmlNodePtr node = NULL;
  781. xmlNodePtr period_node = NULL;
  782. xmlNodePtr mpd_baseurl_node = NULL;
  783. xmlNodePtr period_baseurl_node = NULL;
  784. xmlNodePtr adaptionset_node = NULL;
  785. xmlAttrPtr attr = NULL;
  786. char *val = NULL;
  787. uint32_t perdiod_duration_sec = 0;
  788. uint32_t perdiod_start_sec = 0;
  789. int32_t audio_rep_idx = 0;
  790. int32_t video_rep_idx = 0;
  791. if (!in) {
  792. close_in = 1;
  793. set_httpheader_options(c, opts);
  794. ret = avio_open2(&in, url, AVIO_FLAG_READ, c->interrupt_callback, &opts);
  795. av_dict_free(&opts);
  796. if (ret < 0)
  797. return ret;
  798. }
  799. if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, &new_url) >= 0) {
  800. c->base_url = av_strdup(new_url);
  801. } else {
  802. c->base_url = av_strdup(url);
  803. }
  804. filesize = avio_size(in);
  805. if (filesize <= 0) {
  806. filesize = 8 * 1024;
  807. }
  808. buffer = av_mallocz(filesize);
  809. if (!buffer) {
  810. av_free(c->base_url);
  811. return AVERROR(ENOMEM);
  812. }
  813. filesize = avio_read(in, buffer, filesize);
  814. if (filesize <= 0) {
  815. av_log(s, AV_LOG_ERROR, "Unable to read to offset '%s'\n", url);
  816. ret = AVERROR_INVALIDDATA;
  817. } else {
  818. LIBXML_TEST_VERSION
  819. doc = xmlReadMemory(buffer, filesize, c->base_url, NULL, 0);
  820. root_element = xmlDocGetRootElement(doc);
  821. node = root_element;
  822. if (!node) {
  823. ret = AVERROR_INVALIDDATA;
  824. av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing root node\n", url);
  825. goto cleanup;
  826. }
  827. if (node->type != XML_ELEMENT_NODE ||
  828. av_strcasecmp(node->name, (const char *)"MPD")) {
  829. ret = AVERROR_INVALIDDATA;
  830. av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - wrong root node name[%s] type[%d]\n", url, node->name, (int)node->type);
  831. goto cleanup;
  832. }
  833. val = xmlGetProp(node, "type");
  834. if (!val) {
  835. av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing type attrib\n", url);
  836. ret = AVERROR_INVALIDDATA;
  837. goto cleanup;
  838. }
  839. if (!av_strcasecmp(val, (const char *)"dynamic"))
  840. c->is_live = 1;
  841. xmlFree(val);
  842. attr = node->properties;
  843. while (attr) {
  844. val = xmlGetProp(node, attr->name);
  845. if (!av_strcasecmp(attr->name, (const char *)"availabilityStartTime")) {
  846. c->availability_start_time = get_utc_date_time_insec(s, (const char *)val);
  847. } else if (!av_strcasecmp(attr->name, (const char *)"publishTime")) {
  848. c->publish_time = get_utc_date_time_insec(s, (const char *)val);
  849. } else if (!av_strcasecmp(attr->name, (const char *)"minimumUpdatePeriod")) {
  850. c->minimum_update_period = get_duration_insec(s, (const char *)val);
  851. } else if (!av_strcasecmp(attr->name, (const char *)"timeShiftBufferDepth")) {
  852. c->time_shift_buffer_depth = get_duration_insec(s, (const char *)val);
  853. } else if (!av_strcasecmp(attr->name, (const char *)"minBufferTime")) {
  854. c->min_buffer_time = get_duration_insec(s, (const char *)val);
  855. } else if (!av_strcasecmp(attr->name, (const char *)"suggestedPresentationDelay")) {
  856. c->suggested_presentation_delay = get_duration_insec(s, (const char *)val);
  857. } else if (!av_strcasecmp(attr->name, (const char *)"mediaPresentationDuration")) {
  858. c->media_presentation_duration = get_duration_insec(s, (const char *)val);
  859. }
  860. attr = attr->next;
  861. xmlFree(val);
  862. }
  863. mpd_baseurl_node = find_child_node_by_name(node, "BaseURL");
  864. // at now we can handle only one period, with the longest duration
  865. node = xmlFirstElementChild(node);
  866. while (node) {
  867. if (!av_strcasecmp(node->name, (const char *)"Period")) {
  868. perdiod_duration_sec = 0;
  869. perdiod_start_sec = 0;
  870. attr = node->properties;
  871. while (attr) {
  872. val = xmlGetProp(node, attr->name);
  873. if (!av_strcasecmp(attr->name, (const char *)"duration")) {
  874. perdiod_duration_sec = get_duration_insec(s, (const char *)val);
  875. } else if (!av_strcasecmp(attr->name, (const char *)"start")) {
  876. perdiod_start_sec = get_duration_insec(s, (const char *)val);
  877. }
  878. attr = attr->next;
  879. xmlFree(val);
  880. }
  881. if ((perdiod_duration_sec) >= (c->period_duration)) {
  882. period_node = node;
  883. c->period_duration = perdiod_duration_sec;
  884. c->period_start = perdiod_start_sec;
  885. if (c->period_start > 0)
  886. c->media_presentation_duration = c->period_duration;
  887. }
  888. }
  889. node = xmlNextElementSibling(node);
  890. }
  891. if (!period_node) {
  892. av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing Period node\n", url);
  893. ret = AVERROR_INVALIDDATA;
  894. goto cleanup;
  895. }
  896. adaptionset_node = xmlFirstElementChild(period_node);
  897. while (adaptionset_node) {
  898. if (!av_strcasecmp(adaptionset_node->name, (const char *)"BaseURL")) {
  899. period_baseurl_node = adaptionset_node;
  900. } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"AdaptationSet")) {
  901. parse_manifest_adaptationset(s, url, adaptionset_node, mpd_baseurl_node, period_baseurl_node);
  902. }
  903. adaptionset_node = xmlNextElementSibling(adaptionset_node);
  904. }
  905. if (c->cur_video) {
  906. c->cur_video->rep_count = video_rep_idx;
  907. av_log(s, AV_LOG_VERBOSE, "rep_idx[%d]\n", (int)c->cur_video->rep_idx);
  908. av_log(s, AV_LOG_VERBOSE, "rep_count[%d]\n", (int)video_rep_idx);
  909. }
  910. if (c->cur_audio) {
  911. c->cur_audio->rep_count = audio_rep_idx;
  912. }
  913. cleanup:
  914. /*free the document */
  915. xmlFreeDoc(doc);
  916. xmlCleanupParser();
  917. }
  918. av_free(new_url);
  919. av_free(buffer);
  920. if (close_in) {
  921. avio_close(in);
  922. }
  923. return ret;
  924. }
  925. static int64_t calc_cur_seg_no(AVFormatContext *s, struct representation *pls)
  926. {
  927. DASHContext *c = s->priv_data;
  928. int64_t num = 0;
  929. int64_t start_time_offset = 0;
  930. if (c->is_live) {
  931. if (pls->n_fragments) {
  932. num = pls->first_seq_no;
  933. } else if (pls->n_timelines) {
  934. start_time_offset = get_segment_start_time_based_on_timeline(pls, 0xFFFFFFFF) - pls->timelines[pls->first_seq_no]->starttime; // total duration of playlist
  935. if (start_time_offset < 60 * pls->fragment_timescale)
  936. start_time_offset = 0;
  937. else
  938. start_time_offset = start_time_offset - 60 * pls->fragment_timescale;
  939. num = calc_next_seg_no_from_timelines(pls, pls->timelines[pls->first_seq_no]->starttime + start_time_offset);
  940. if (num == -1)
  941. num = pls->first_seq_no;
  942. } else if (pls->fragment_duration){
  943. if (pls->presentation_timeoffset) {
  944. num = pls->presentation_timeoffset * pls->fragment_timescale / pls->fragment_duration;
  945. } else if (c->publish_time > 0 && !c->availability_start_time) {
  946. num = pls->first_seq_no + (((c->publish_time - c->availability_start_time) - c->suggested_presentation_delay) * pls->fragment_timescale) / pls->fragment_duration;
  947. } else {
  948. num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time) - c->suggested_presentation_delay) * pls->fragment_timescale) / pls->fragment_duration;
  949. }
  950. }
  951. } else {
  952. num = pls->first_seq_no;
  953. }
  954. return num;
  955. }
  956. static int64_t calc_min_seg_no(AVFormatContext *s, struct representation *pls)
  957. {
  958. DASHContext *c = s->priv_data;
  959. int64_t num = 0;
  960. if (c->is_live && pls->fragment_duration) {
  961. 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;
  962. } else {
  963. num = pls->first_seq_no;
  964. }
  965. return num;
  966. }
  967. static int64_t calc_max_seg_no(struct representation *pls)
  968. {
  969. DASHContext *c = pls->parent->priv_data;
  970. int64_t num = 0;
  971. if (pls->n_fragments) {
  972. num = pls->first_seq_no + pls->n_fragments - 1;
  973. } else if (pls->n_timelines) {
  974. int i = 0;
  975. num = pls->first_seq_no + pls->n_timelines - 1;
  976. for (i = 0; i < pls->n_timelines; i++) {
  977. num += pls->timelines[i]->repeat;
  978. }
  979. } else if (c->is_live && pls->fragment_duration) {
  980. num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time)) * pls->fragment_timescale) / pls->fragment_duration;
  981. } else if (pls->fragment_duration) {
  982. num = pls->first_seq_no + (c->media_presentation_duration * pls->fragment_timescale) / pls->fragment_duration;
  983. }
  984. return num;
  985. }
  986. static void move_timelines(struct representation *rep_src, struct representation *rep_dest)
  987. {
  988. if (rep_dest && rep_src ) {
  989. free_timelines_list(rep_dest);
  990. rep_dest->timelines = rep_src->timelines;
  991. rep_dest->n_timelines = rep_src->n_timelines;
  992. rep_dest->first_seq_no = rep_src->first_seq_no;
  993. rep_dest->last_seq_no = calc_max_seg_no(rep_dest);
  994. rep_src->timelines = NULL;
  995. rep_src->n_timelines = 0;
  996. rep_dest->cur_seq_no = rep_src->cur_seq_no;
  997. }
  998. }
  999. static void move_segments(struct representation *rep_src, struct representation *rep_dest)
  1000. {
  1001. if (rep_dest && rep_src ) {
  1002. free_fragment_list(rep_dest);
  1003. if (rep_src->start_number > (rep_dest->start_number + rep_dest->n_fragments))
  1004. rep_dest->cur_seq_no = 0;
  1005. else
  1006. rep_dest->cur_seq_no += rep_src->start_number - rep_dest->start_number;
  1007. rep_dest->fragments = rep_src->fragments;
  1008. rep_dest->n_fragments = rep_src->n_fragments;
  1009. rep_dest->parent = rep_src->parent;
  1010. rep_dest->last_seq_no = calc_max_seg_no(rep_dest);
  1011. rep_src->fragments = NULL;
  1012. rep_src->n_fragments = 0;
  1013. }
  1014. }
  1015. static int refresh_manifest(AVFormatContext *s)
  1016. {
  1017. int ret = 0;
  1018. DASHContext *c = s->priv_data;
  1019. // save current context
  1020. struct representation *cur_video = c->cur_video;
  1021. struct representation *cur_audio = c->cur_audio;
  1022. char *base_url = c->base_url;
  1023. c->base_url = NULL;
  1024. c->cur_video = NULL;
  1025. c->cur_audio = NULL;
  1026. ret = parse_manifest(s, s->filename, NULL);
  1027. if (ret)
  1028. goto finish;
  1029. if (cur_video && cur_video->timelines || cur_audio && cur_audio->timelines) {
  1030. // calc current time
  1031. int64_t currentVideoTime = 0;
  1032. int64_t currentAudioTime = 0;
  1033. if (cur_video && cur_video->timelines)
  1034. currentVideoTime = get_segment_start_time_based_on_timeline(cur_video, cur_video->cur_seq_no) / cur_video->fragment_timescale;
  1035. if (cur_audio && cur_audio->timelines)
  1036. currentAudioTime = get_segment_start_time_based_on_timeline(cur_audio, cur_audio->cur_seq_no) / cur_audio->fragment_timescale;
  1037. // update segments
  1038. if (cur_video && cur_video->timelines) {
  1039. c->cur_video->cur_seq_no = calc_next_seg_no_from_timelines(c->cur_video, currentVideoTime * cur_video->fragment_timescale - 1);
  1040. if (c->cur_video->cur_seq_no >= 0) {
  1041. move_timelines(c->cur_video, cur_video);
  1042. }
  1043. }
  1044. if (cur_audio && cur_audio->timelines) {
  1045. c->cur_audio->cur_seq_no = calc_next_seg_no_from_timelines(c->cur_audio, currentAudioTime * cur_audio->fragment_timescale - 1);
  1046. if (c->cur_audio->cur_seq_no >= 0) {
  1047. move_timelines(c->cur_audio, cur_audio);
  1048. }
  1049. }
  1050. }
  1051. if (cur_video && cur_video->fragments) {
  1052. move_segments(c->cur_video, cur_video);
  1053. }
  1054. if (cur_audio && cur_audio->fragments) {
  1055. move_segments(c->cur_audio, cur_audio);
  1056. }
  1057. finish:
  1058. // restore context
  1059. if (c->base_url)
  1060. av_free(base_url);
  1061. else
  1062. c->base_url = base_url;
  1063. if (c->cur_audio)
  1064. free_representation(c->cur_audio);
  1065. if (c->cur_video)
  1066. free_representation(c->cur_video);
  1067. c->cur_audio = cur_audio;
  1068. c->cur_video = cur_video;
  1069. return ret;
  1070. }
  1071. static struct fragment *get_current_fragment(struct representation *pls)
  1072. {
  1073. int64_t min_seq_no = 0;
  1074. int64_t max_seq_no = 0;
  1075. struct fragment *seg = NULL;
  1076. struct fragment *seg_ptr = NULL;
  1077. DASHContext *c = pls->parent->priv_data;
  1078. while (( !ff_check_interrupt(c->interrupt_callback)&& pls->n_fragments > 0)) {
  1079. if (pls->cur_seq_no < pls->n_fragments) {
  1080. seg_ptr = pls->fragments[pls->cur_seq_no];
  1081. seg = av_mallocz(sizeof(struct fragment));
  1082. if (!seg) {
  1083. return NULL;
  1084. }
  1085. seg->url = av_strdup(seg_ptr->url);
  1086. if (!seg->url) {
  1087. av_free(seg);
  1088. return NULL;
  1089. }
  1090. seg->size = seg_ptr->size;
  1091. seg->url_offset = seg_ptr->url_offset;
  1092. return seg;
  1093. } else if (c->is_live) {
  1094. refresh_manifest(pls->parent);
  1095. } else {
  1096. break;
  1097. }
  1098. }
  1099. if (c->is_live) {
  1100. min_seq_no = calc_min_seg_no(pls->parent, pls);
  1101. max_seq_no = calc_max_seg_no(pls);
  1102. if (pls->timelines || pls->fragments) {
  1103. refresh_manifest(pls->parent);
  1104. }
  1105. if (pls->cur_seq_no <= min_seq_no) {
  1106. 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);
  1107. pls->cur_seq_no = calc_cur_seg_no(pls->parent, pls);
  1108. } else if (pls->cur_seq_no > max_seq_no) {
  1109. 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);
  1110. }
  1111. seg = av_mallocz(sizeof(struct fragment));
  1112. if (!seg) {
  1113. return NULL;
  1114. }
  1115. } else if (pls->cur_seq_no <= pls->last_seq_no) {
  1116. seg = av_mallocz(sizeof(struct fragment));
  1117. if (!seg) {
  1118. return NULL;
  1119. }
  1120. }
  1121. if (seg) {
  1122. char tmpfilename[MAX_URL_SIZE];
  1123. 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));
  1124. seg->url = av_strireplace(pls->url_template, pls->url_template, tmpfilename);
  1125. if (!seg->url) {
  1126. av_log(pls->parent, AV_LOG_WARNING, "Unable to resolve template url '%s', try to use origin template\n", pls->url_template);
  1127. seg->url = av_strdup(pls->url_template);
  1128. if (!seg->url) {
  1129. av_log(pls->parent, AV_LOG_ERROR, "Cannot resolve template url '%s'\n", pls->url_template);
  1130. return NULL;
  1131. }
  1132. }
  1133. seg->size = -1;
  1134. }
  1135. return seg;
  1136. }
  1137. enum ReadFromURLMode {
  1138. READ_NORMAL,
  1139. READ_COMPLETE,
  1140. };
  1141. static int read_from_url(struct representation *pls, struct fragment *seg,
  1142. uint8_t *buf, int buf_size,
  1143. enum ReadFromURLMode mode)
  1144. {
  1145. int ret;
  1146. /* limit read if the fragment was only a part of a file */
  1147. if (seg->size >= 0)
  1148. buf_size = FFMIN(buf_size, pls->cur_seg_size - pls->cur_seg_offset);
  1149. if (mode == READ_COMPLETE) {
  1150. ret = avio_read(pls->input, buf, buf_size);
  1151. if (ret < buf_size) {
  1152. av_log(pls->parent, AV_LOG_WARNING, "Could not read complete fragment.\n");
  1153. }
  1154. } else {
  1155. ret = avio_read(pls->input, buf, buf_size);
  1156. }
  1157. if (ret > 0)
  1158. pls->cur_seg_offset += ret;
  1159. return ret;
  1160. }
  1161. static int open_input(DASHContext *c, struct representation *pls, struct fragment *seg)
  1162. {
  1163. AVDictionary *opts = NULL;
  1164. char url[MAX_URL_SIZE];
  1165. int ret;
  1166. set_httpheader_options(c, opts);
  1167. if (seg->size >= 0) {
  1168. /* try to restrict the HTTP request to the part we want
  1169. * (if this is in fact a HTTP request) */
  1170. av_dict_set_int(&opts, "offset", seg->url_offset, 0);
  1171. av_dict_set_int(&opts, "end_offset", seg->url_offset + seg->size, 0);
  1172. }
  1173. ff_make_absolute_url(url, MAX_URL_SIZE, c->base_url, seg->url);
  1174. av_log(pls->parent, AV_LOG_VERBOSE, "DASH request for url '%s', offset %"PRId64", playlist %d\n",
  1175. url, seg->url_offset, pls->rep_idx);
  1176. ret = open_url(pls->parent, &pls->input, url, c->avio_opts, opts, NULL);
  1177. if (ret < 0) {
  1178. goto cleanup;
  1179. }
  1180. /* Seek to the requested position. If this was a HTTP request, the offset
  1181. * should already be where want it to, but this allows e.g. local testing
  1182. * without a HTTP server. */
  1183. if (!ret && seg->url_offset) {
  1184. int64_t seekret = avio_seek(pls->input, seg->url_offset, SEEK_SET);
  1185. if (seekret < 0) {
  1186. av_log(pls->parent, AV_LOG_ERROR, "Unable to seek to offset %"PRId64" of DASH fragment '%s'\n", seg->url_offset, seg->url);
  1187. ret = (int) seekret;
  1188. ff_format_io_close(pls->parent, &pls->input);
  1189. }
  1190. }
  1191. cleanup:
  1192. av_dict_free(&opts);
  1193. pls->cur_seg_offset = 0;
  1194. pls->cur_seg_size = seg->size;
  1195. return ret;
  1196. }
  1197. static int update_init_section(struct representation *pls)
  1198. {
  1199. static const int max_init_section_size = 1024 * 1024;
  1200. DASHContext *c = pls->parent->priv_data;
  1201. int64_t sec_size;
  1202. int64_t urlsize;
  1203. int ret;
  1204. if (!pls->init_section || pls->init_sec_buf)
  1205. return 0;
  1206. ret = open_input(c, pls, pls->init_section);
  1207. if (ret < 0) {
  1208. av_log(pls->parent, AV_LOG_WARNING,
  1209. "Failed to open an initialization section in playlist %d\n",
  1210. pls->rep_idx);
  1211. return ret;
  1212. }
  1213. if (pls->init_section->size >= 0)
  1214. sec_size = pls->init_section->size;
  1215. else if ((urlsize = avio_size(pls->input)) >= 0)
  1216. sec_size = urlsize;
  1217. else
  1218. sec_size = max_init_section_size;
  1219. av_log(pls->parent, AV_LOG_DEBUG,
  1220. "Downloading an initialization section of size %"PRId64"\n",
  1221. sec_size);
  1222. sec_size = FFMIN(sec_size, max_init_section_size);
  1223. av_fast_malloc(&pls->init_sec_buf, &pls->init_sec_buf_size, sec_size);
  1224. ret = read_from_url(pls, pls->init_section, pls->init_sec_buf,
  1225. pls->init_sec_buf_size, READ_COMPLETE);
  1226. ff_format_io_close(pls->parent, &pls->input);
  1227. if (ret < 0)
  1228. return ret;
  1229. pls->init_sec_data_len = ret;
  1230. pls->init_sec_buf_read_offset = 0;
  1231. return 0;
  1232. }
  1233. static int64_t seek_data(void *opaque, int64_t offset, int whence)
  1234. {
  1235. struct representation *v = opaque;
  1236. if (v->n_fragments && !v->init_sec_data_len) {
  1237. return avio_seek(v->input, offset, whence);
  1238. }
  1239. return AVERROR(ENOSYS);
  1240. }
  1241. static int read_data(void *opaque, uint8_t *buf, int buf_size)
  1242. {
  1243. int ret = 0;
  1244. struct representation *v = opaque;
  1245. DASHContext *c = v->parent->priv_data;
  1246. restart:
  1247. if (!v->input) {
  1248. free_fragment(&v->cur_seg);
  1249. v->cur_seg = get_current_fragment(v);
  1250. if (!v->cur_seg) {
  1251. ret = AVERROR_EOF;
  1252. goto end;
  1253. }
  1254. /* load/update Media Initialization Section, if any */
  1255. ret = update_init_section(v);
  1256. if (ret)
  1257. goto end;
  1258. ret = open_input(c, v, v->cur_seg);
  1259. if (ret < 0) {
  1260. if (ff_check_interrupt(c->interrupt_callback)) {
  1261. goto end;
  1262. ret = AVERROR_EXIT;
  1263. }
  1264. av_log(v->parent, AV_LOG_WARNING, "Failed to open fragment of playlist %d\n", v->rep_idx);
  1265. v->cur_seq_no++;
  1266. goto restart;
  1267. }
  1268. }
  1269. if (v->init_sec_buf_read_offset < v->init_sec_data_len) {
  1270. /* Push init section out first before first actual fragment */
  1271. int copy_size = FFMIN(v->init_sec_data_len - v->init_sec_buf_read_offset, buf_size);
  1272. memcpy(buf, v->init_sec_buf, copy_size);
  1273. v->init_sec_buf_read_offset += copy_size;
  1274. ret = copy_size;
  1275. goto end;
  1276. }
  1277. /* check the v->cur_seg, if it is null, get current and double check if the new v->cur_seg*/
  1278. if (!v->cur_seg) {
  1279. v->cur_seg = get_current_fragment(v);
  1280. }
  1281. if (!v->cur_seg) {
  1282. ret = AVERROR_EOF;
  1283. goto end;
  1284. }
  1285. ret = read_from_url(v, v->cur_seg, buf, buf_size, READ_NORMAL);
  1286. if (ret > 0)
  1287. goto end;
  1288. if (!v->is_restart_needed)
  1289. v->cur_seq_no++;
  1290. v->is_restart_needed = 1;
  1291. end:
  1292. return ret;
  1293. }
  1294. static int save_avio_options(AVFormatContext *s)
  1295. {
  1296. DASHContext *c = s->priv_data;
  1297. const char *opts[] = { "headers", "user_agent", "user-agent", "cookies", NULL }, **opt = opts;
  1298. uint8_t *buf = NULL;
  1299. int ret = 0;
  1300. while (*opt) {
  1301. if (av_opt_get(s->pb, *opt, AV_OPT_SEARCH_CHILDREN, &buf) >= 0) {
  1302. if (buf[0] != '\0') {
  1303. ret = av_dict_set(&c->avio_opts, *opt, buf, AV_DICT_DONT_STRDUP_VAL);
  1304. if (ret < 0)
  1305. return ret;
  1306. }
  1307. }
  1308. opt++;
  1309. }
  1310. return ret;
  1311. }
  1312. static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
  1313. int flags, AVDictionary **opts)
  1314. {
  1315. av_log(s, AV_LOG_ERROR,
  1316. "A DASH playlist item '%s' referred to an external file '%s'. "
  1317. "Opening this file was forbidden for security reasons\n",
  1318. s->filename, url);
  1319. return AVERROR(EPERM);
  1320. }
  1321. static int reopen_demux_for_component(AVFormatContext *s, struct representation *pls)
  1322. {
  1323. DASHContext *c = s->priv_data;
  1324. AVInputFormat *in_fmt = NULL;
  1325. AVDictionary *in_fmt_opts = NULL;
  1326. uint8_t *avio_ctx_buffer = NULL;
  1327. int ret = 0;
  1328. if (pls->ctx) {
  1329. /* note: the internal buffer could have changed, and be != avio_ctx_buffer */
  1330. av_freep(&pls->pb.buffer);
  1331. memset(&pls->pb, 0x00, sizeof(AVIOContext));
  1332. pls->ctx->pb = NULL;
  1333. avformat_close_input(&pls->ctx);
  1334. pls->ctx = NULL;
  1335. }
  1336. if (!(pls->ctx = avformat_alloc_context())) {
  1337. ret = AVERROR(ENOMEM);
  1338. goto fail;
  1339. }
  1340. avio_ctx_buffer = av_malloc(INITIAL_BUFFER_SIZE);
  1341. if (!avio_ctx_buffer ) {
  1342. ret = AVERROR(ENOMEM);
  1343. avformat_free_context(pls->ctx);
  1344. pls->ctx = NULL;
  1345. goto fail;
  1346. }
  1347. if (c->is_live) {
  1348. ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, NULL);
  1349. } else {
  1350. ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, seek_data);
  1351. }
  1352. pls->pb.seekable = 0;
  1353. if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0)
  1354. goto fail;
  1355. pls->ctx->flags = AVFMT_FLAG_CUSTOM_IO;
  1356. pls->ctx->probesize = 1024 * 4;
  1357. pls->ctx->max_analyze_duration = 4 * AV_TIME_BASE;
  1358. ret = av_probe_input_buffer(&pls->pb, &in_fmt, "", NULL, 0, 0);
  1359. if (ret < 0) {
  1360. av_log(s, AV_LOG_ERROR, "Error when loading first fragment, playlist %d\n", (int)pls->rep_idx);
  1361. avformat_free_context(pls->ctx);
  1362. pls->ctx = NULL;
  1363. goto fail;
  1364. }
  1365. pls->ctx->pb = &pls->pb;
  1366. pls->ctx->io_open = nested_io_open;
  1367. // provide additional information from mpd if available
  1368. ret = avformat_open_input(&pls->ctx, "", in_fmt, &in_fmt_opts); //pls->init_section->url
  1369. av_dict_free(&in_fmt_opts);
  1370. if (ret < 0)
  1371. goto fail;
  1372. if (pls->n_fragments) {
  1373. ret = avformat_find_stream_info(pls->ctx, NULL);
  1374. if (ret < 0)
  1375. goto fail;
  1376. }
  1377. fail:
  1378. return ret;
  1379. }
  1380. static int open_demux_for_component(AVFormatContext *s, struct representation *pls)
  1381. {
  1382. int ret = 0;
  1383. int i;
  1384. pls->parent = s;
  1385. pls->cur_seq_no = calc_cur_seg_no(s, pls);
  1386. pls->last_seq_no = calc_max_seg_no(pls);
  1387. ret = reopen_demux_for_component(s, pls);
  1388. if (ret < 0) {
  1389. goto fail;
  1390. }
  1391. for (i = 0; i < pls->ctx->nb_streams; i++) {
  1392. AVStream *st = avformat_new_stream(s, NULL);
  1393. AVStream *ist = pls->ctx->streams[i];
  1394. if (!st) {
  1395. ret = AVERROR(ENOMEM);
  1396. goto fail;
  1397. }
  1398. st->id = i;
  1399. avcodec_parameters_copy(st->codecpar, pls->ctx->streams[i]->codecpar);
  1400. avpriv_set_pts_info(st, ist->pts_wrap_bits, ist->time_base.num, ist->time_base.den);
  1401. }
  1402. return 0;
  1403. fail:
  1404. return ret;
  1405. }
  1406. static int dash_read_header(AVFormatContext *s)
  1407. {
  1408. void *u = (s->flags & AVFMT_FLAG_CUSTOM_IO) ? NULL : s->pb;
  1409. DASHContext *c = s->priv_data;
  1410. int ret = 0;
  1411. int stream_index = 0;
  1412. c->interrupt_callback = &s->interrupt_callback;
  1413. // if the URL context is good, read important options we must broker later
  1414. if (u) {
  1415. update_options(&c->user_agent, "user-agent", u);
  1416. update_options(&c->cookies, "cookies", u);
  1417. update_options(&c->headers, "headers", u);
  1418. }
  1419. if ((ret = parse_manifest(s, s->filename, s->pb)) < 0)
  1420. goto fail;
  1421. if ((ret = save_avio_options(s)) < 0)
  1422. goto fail;
  1423. /* If this isn't a live stream, fill the total duration of the
  1424. * stream. */
  1425. if (!c->is_live) {
  1426. s->duration = (int64_t) c->media_presentation_duration * AV_TIME_BASE;
  1427. }
  1428. /* Open the demuxer for curent video and current audio components if available */
  1429. if (!ret && c->cur_video) {
  1430. ret = open_demux_for_component(s, c->cur_video);
  1431. if (!ret) {
  1432. c->cur_video->stream_index = stream_index;
  1433. ++stream_index;
  1434. } else {
  1435. free_representation(c->cur_video);
  1436. c->cur_video = NULL;
  1437. }
  1438. }
  1439. if (!ret && c->cur_audio) {
  1440. ret = open_demux_for_component(s, c->cur_audio);
  1441. if (!ret) {
  1442. c->cur_audio->stream_index = stream_index;
  1443. ++stream_index;
  1444. } else {
  1445. free_representation(c->cur_audio);
  1446. c->cur_audio = NULL;
  1447. }
  1448. }
  1449. if (!stream_index) {
  1450. ret = AVERROR_INVALIDDATA;
  1451. goto fail;
  1452. }
  1453. /* Create a program */
  1454. if (!ret) {
  1455. AVProgram *program;
  1456. program = av_new_program(s, 0);
  1457. if (!program) {
  1458. goto fail;
  1459. }
  1460. if (c->cur_video) {
  1461. av_program_add_stream_index(s, 0, c->cur_video->stream_index);
  1462. }
  1463. if (c->cur_audio) {
  1464. av_program_add_stream_index(s, 0, c->cur_audio->stream_index);
  1465. }
  1466. }
  1467. return 0;
  1468. fail:
  1469. return ret;
  1470. }
  1471. static int dash_read_packet(AVFormatContext *s, AVPacket *pkt)
  1472. {
  1473. DASHContext *c = s->priv_data;
  1474. int ret = 0;
  1475. struct representation *cur = NULL;
  1476. if (!c->cur_audio && !c->cur_video ) {
  1477. return AVERROR_INVALIDDATA;
  1478. }
  1479. if (c->cur_audio && !c->cur_video) {
  1480. cur = c->cur_audio;
  1481. } else if (!c->cur_audio && c->cur_video) {
  1482. cur = c->cur_video;
  1483. } else if (c->cur_video->cur_timestamp < c->cur_audio->cur_timestamp) {
  1484. cur = c->cur_video;
  1485. } else {
  1486. cur = c->cur_audio;
  1487. }
  1488. if (cur->ctx) {
  1489. while (!ff_check_interrupt(c->interrupt_callback) && !ret) {
  1490. ret = av_read_frame(cur->ctx, pkt);
  1491. if (ret >= 0) {
  1492. /* If we got a packet, return it */
  1493. cur->cur_timestamp = av_rescale(pkt->pts, (int64_t)cur->ctx->streams[0]->time_base.num * 90000, cur->ctx->streams[0]->time_base.den);
  1494. pkt->stream_index = cur->stream_index;
  1495. return 0;
  1496. }
  1497. if (cur->is_restart_needed) {
  1498. cur->cur_seg_offset = 0;
  1499. cur->init_sec_buf_read_offset = 0;
  1500. if (cur->input)
  1501. ff_format_io_close(cur->parent, &cur->input);
  1502. ret = reopen_demux_for_component(s, cur);
  1503. cur->is_restart_needed = 0;
  1504. }
  1505. }
  1506. }
  1507. return AVERROR_EOF;
  1508. }
  1509. static int dash_close(AVFormatContext *s)
  1510. {
  1511. DASHContext *c = s->priv_data;
  1512. if (c->cur_audio) {
  1513. free_representation(c->cur_audio);
  1514. }
  1515. if (c->cur_video) {
  1516. free_representation(c->cur_video);
  1517. }
  1518. av_freep(&c->cookies);
  1519. av_freep(&c->user_agent);
  1520. av_dict_free(&c->avio_opts);
  1521. av_freep(&c->base_url);
  1522. return 0;
  1523. }
  1524. static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos_msec, int flags)
  1525. {
  1526. int ret = 0;
  1527. int i = 0;
  1528. int j = 0;
  1529. int64_t duration = 0;
  1530. av_log(pls->parent, AV_LOG_VERBOSE, "DASH seek pos[%"PRId64"ms], playlist %d\n", seek_pos_msec, pls->rep_idx);
  1531. // single fragment mode
  1532. if (pls->n_fragments == 1) {
  1533. pls->cur_timestamp = 0;
  1534. pls->cur_seg_offset = 0;
  1535. ff_read_frame_flush(pls->ctx);
  1536. return av_seek_frame(pls->ctx, -1, seek_pos_msec * 1000, flags);
  1537. }
  1538. if (pls->input)
  1539. ff_format_io_close(pls->parent, &pls->input);
  1540. // find the nearest fragment
  1541. if (pls->n_timelines > 0 && pls->fragment_timescale > 0) {
  1542. int64_t num = pls->first_seq_no;
  1543. av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline start n_timelines[%d] "
  1544. "last_seq_no[%"PRId64"], playlist %d.\n",
  1545. (int)pls->n_timelines, (int64_t)pls->last_seq_no, (int)pls->rep_idx);
  1546. for (i = 0; i < pls->n_timelines; i++) {
  1547. if (pls->timelines[i]->starttime > 0) {
  1548. duration = pls->timelines[i]->starttime;
  1549. }
  1550. duration += pls->timelines[i]->duration;
  1551. if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
  1552. goto set_seq_num;
  1553. }
  1554. for (j = 0; j < pls->timelines[i]->repeat; j++) {
  1555. duration += pls->timelines[i]->duration;
  1556. num++;
  1557. if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
  1558. goto set_seq_num;
  1559. }
  1560. }
  1561. num++;
  1562. }
  1563. set_seq_num:
  1564. pls->cur_seq_no = num > pls->last_seq_no ? pls->last_seq_no : num;
  1565. av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline end cur_seq_no[%"PRId64"], playlist %d.\n",
  1566. (int64_t)pls->cur_seq_no, (int)pls->rep_idx);
  1567. } else if (pls->fragment_duration > 0) {
  1568. pls->cur_seq_no = pls->first_seq_no + ((seek_pos_msec * pls->fragment_timescale) / pls->fragment_duration) / 1000;
  1569. } else {
  1570. av_log(pls->parent, AV_LOG_ERROR, "dash_seek missing fragment_duration\n");
  1571. pls->cur_seq_no = pls->first_seq_no;
  1572. }
  1573. pls->cur_timestamp = 0;
  1574. pls->cur_seg_offset = 0;
  1575. pls->init_sec_buf_read_offset = 0;
  1576. ret = reopen_demux_for_component(s, pls);
  1577. return ret;
  1578. }
  1579. static int dash_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  1580. {
  1581. int ret = 0;
  1582. DASHContext *c = s->priv_data;
  1583. int64_t seek_pos_msec = av_rescale_rnd(timestamp, 1000,
  1584. s->streams[stream_index]->time_base.den,
  1585. flags & AVSEEK_FLAG_BACKWARD ?
  1586. AV_ROUND_DOWN : AV_ROUND_UP);
  1587. if ((flags & AVSEEK_FLAG_BYTE) || c->is_live)
  1588. return AVERROR(ENOSYS);
  1589. if (c->cur_audio) {
  1590. ret = dash_seek(s, c->cur_audio, seek_pos_msec, flags);
  1591. }
  1592. if (!ret && c->cur_video) {
  1593. ret = dash_seek(s, c->cur_video, seek_pos_msec, flags);
  1594. }
  1595. return ret;
  1596. }
  1597. static int dash_probe(AVProbeData *p)
  1598. {
  1599. if (!av_stristr(p->buf, "<MPD"))
  1600. return 0;
  1601. if (av_stristr(p->buf, "dash:profile:isoff-on-demand:2011") ||
  1602. av_stristr(p->buf, "dash:profile:isoff-live:2011") ||
  1603. av_stristr(p->buf, "dash:profile:isoff-live:2012") ||
  1604. av_stristr(p->buf, "dash:profile:isoff-main:2011")) {
  1605. return AVPROBE_SCORE_MAX;
  1606. }
  1607. if (av_stristr(p->buf, "dash:profile")) {
  1608. return AVPROBE_SCORE_MAX;
  1609. }
  1610. return 0;
  1611. }
  1612. #define OFFSET(x) offsetof(DASHContext, x)
  1613. #define FLAGS AV_OPT_FLAG_DECODING_PARAM
  1614. static const AVOption dash_options[] = {
  1615. {"allowed_extensions", "List of file extensions that dash is allowed to access",
  1616. OFFSET(allowed_extensions), AV_OPT_TYPE_STRING,
  1617. {.str = "aac,m4a,m4s,m4v,mov,mp4"},
  1618. INT_MIN, INT_MAX, FLAGS},
  1619. {NULL}
  1620. };
  1621. static const AVClass dash_class = {
  1622. .class_name = "dash",
  1623. .item_name = av_default_item_name,
  1624. .option = dash_options,
  1625. .version = LIBAVUTIL_VERSION_INT,
  1626. };
  1627. AVInputFormat ff_dash_demuxer = {
  1628. .name = "dash",
  1629. .long_name = NULL_IF_CONFIG_SMALL("Dynamic Adaptive Streaming over HTTP"),
  1630. .priv_class = &dash_class,
  1631. .priv_data_size = sizeof(DASHContext),
  1632. .read_probe = dash_probe,
  1633. .read_header = dash_read_header,
  1634. .read_packet = dash_read_packet,
  1635. .read_close = dash_close,
  1636. .read_seek = dash_read_seek,
  1637. .flags = AVFMT_NO_BYTE_SEEK,
  1638. };