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.

2392 lines
82KB

  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. #define MAX_MANIFEST_SIZE 50 * 1024
  32. #define DEFAULT_MANIFEST_SIZE 8 * 1024
  33. struct fragment {
  34. int64_t url_offset;
  35. int64_t size;
  36. char *url;
  37. };
  38. /*
  39. * reference to : ISO_IEC_23009-1-DASH-2012
  40. * Section: 5.3.9.6.2
  41. * Table: Table 17 — Semantics of SegmentTimeline element
  42. * */
  43. struct timeline {
  44. /* starttime: Element or Attribute Name
  45. * specifies the MPD start time, in @timescale units,
  46. * the first Segment in the series starts relative to the beginning of the Period.
  47. * The value of this attribute must be equal to or greater than the sum of the previous S
  48. * element earliest presentation time and the sum of the contiguous Segment durations.
  49. * If the value of the attribute is greater than what is expressed by the previous S element,
  50. * it expresses discontinuities in the timeline.
  51. * If not present then the value shall be assumed to be zero for the first S element
  52. * and for the subsequent S elements, the value shall be assumed to be the sum of
  53. * the previous S element's earliest presentation time and contiguous duration
  54. * (i.e. previous S@starttime + @duration * (@repeat + 1)).
  55. * */
  56. int64_t starttime;
  57. /* repeat: Element or Attribute Name
  58. * specifies the repeat count of the number of following contiguous Segments with
  59. * the same duration expressed by the value of @duration. This value is zero-based
  60. * (e.g. a value of three means four Segments in the contiguous series).
  61. * */
  62. int64_t repeat;
  63. /* duration: Element or Attribute Name
  64. * specifies the Segment duration, in units of the value of the @timescale.
  65. * */
  66. int64_t duration;
  67. };
  68. /*
  69. * Each playlist has its own demuxer. If it is currently active,
  70. * it has an opened AVIOContext too, and potentially an AVPacket
  71. * containing the next packet from this stream.
  72. */
  73. struct representation {
  74. char *url_template;
  75. AVIOContext pb;
  76. AVIOContext *input;
  77. AVFormatContext *parent;
  78. AVFormatContext *ctx;
  79. int stream_index;
  80. char id[20];
  81. char *lang;
  82. int bandwidth;
  83. AVRational framerate;
  84. AVStream *assoc_stream; /* demuxer stream associated with this representation */
  85. int n_fragments;
  86. struct fragment **fragments; /* VOD list of fragment for profile */
  87. int n_timelines;
  88. struct timeline **timelines;
  89. int64_t first_seq_no;
  90. int64_t last_seq_no;
  91. int64_t start_number; /* used in case when we have dynamic list of segment to know which segments are new one*/
  92. int64_t fragment_duration;
  93. int64_t fragment_timescale;
  94. int64_t presentation_timeoffset;
  95. int64_t cur_seq_no;
  96. int64_t cur_seg_offset;
  97. int64_t cur_seg_size;
  98. struct fragment *cur_seg;
  99. /* Currently active Media Initialization Section */
  100. struct fragment *init_section;
  101. uint8_t *init_sec_buf;
  102. uint32_t init_sec_buf_size;
  103. uint32_t init_sec_data_len;
  104. uint32_t init_sec_buf_read_offset;
  105. int64_t cur_timestamp;
  106. int is_restart_needed;
  107. };
  108. typedef struct DASHContext {
  109. const AVClass *class;
  110. char *base_url;
  111. int n_videos;
  112. struct representation **videos;
  113. int n_audios;
  114. struct representation **audios;
  115. int n_subtitles;
  116. struct representation **subtitles;
  117. /* MediaPresentationDescription Attribute */
  118. uint64_t media_presentation_duration;
  119. uint64_t suggested_presentation_delay;
  120. uint64_t availability_start_time;
  121. uint64_t availability_end_time;
  122. uint64_t publish_time;
  123. uint64_t minimum_update_period;
  124. uint64_t time_shift_buffer_depth;
  125. uint64_t min_buffer_time;
  126. /* Period Attribute */
  127. uint64_t period_duration;
  128. uint64_t period_start;
  129. /* AdaptationSet Attribute */
  130. char *adaptionset_lang;
  131. int is_live;
  132. AVIOInterruptCB *interrupt_callback;
  133. char *allowed_extensions;
  134. AVDictionary *avio_opts;
  135. int max_url_size;
  136. /* Flags for init section*/
  137. int is_init_section_common_video;
  138. int is_init_section_common_audio;
  139. } DASHContext;
  140. static int ishttp(char *url)
  141. {
  142. const char *proto_name = avio_find_protocol_name(url);
  143. return av_strstart(proto_name, "http", NULL);
  144. }
  145. static int aligned(int val)
  146. {
  147. return ((val + 0x3F) >> 6) << 6;
  148. }
  149. static uint64_t get_current_time_in_sec(void)
  150. {
  151. return av_gettime() / 1000000;
  152. }
  153. static uint64_t get_utc_date_time_insec(AVFormatContext *s, const char *datetime)
  154. {
  155. struct tm timeinfo;
  156. int year = 0;
  157. int month = 0;
  158. int day = 0;
  159. int hour = 0;
  160. int minute = 0;
  161. int ret = 0;
  162. float second = 0.0;
  163. /* ISO-8601 date parser */
  164. if (!datetime)
  165. return 0;
  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. return av_timegm(&timeinfo);
  178. }
  179. static uint32_t get_duration_insec(AVFormatContext *s, const char *duration)
  180. {
  181. /* ISO-8601 duration parser */
  182. uint32_t days = 0;
  183. uint32_t hours = 0;
  184. uint32_t mins = 0;
  185. uint32_t secs = 0;
  186. int size = 0;
  187. float value = 0;
  188. char type = '\0';
  189. const char *ptr = duration;
  190. while (*ptr) {
  191. if (*ptr == 'P' || *ptr == 'T') {
  192. ptr++;
  193. continue;
  194. }
  195. if (sscanf(ptr, "%f%c%n", &value, &type, &size) != 2) {
  196. av_log(s, AV_LOG_WARNING, "get_duration_insec get a wrong time format\n");
  197. return 0; /* parser error */
  198. }
  199. switch (type) {
  200. case 'D':
  201. days = (uint32_t)value;
  202. break;
  203. case 'H':
  204. hours = (uint32_t)value;
  205. break;
  206. case 'M':
  207. mins = (uint32_t)value;
  208. break;
  209. case 'S':
  210. secs = (uint32_t)value;
  211. break;
  212. default:
  213. // handle invalid type
  214. break;
  215. }
  216. ptr += size;
  217. }
  218. return ((days * 24 + hours) * 60 + mins) * 60 + secs;
  219. }
  220. static int64_t get_segment_start_time_based_on_timeline(struct representation *pls, int64_t cur_seq_no)
  221. {
  222. int64_t start_time = 0;
  223. int64_t i = 0;
  224. int64_t j = 0;
  225. int64_t num = 0;
  226. if (pls->n_timelines) {
  227. for (i = 0; i < pls->n_timelines; i++) {
  228. if (pls->timelines[i]->starttime > 0) {
  229. start_time = pls->timelines[i]->starttime;
  230. }
  231. if (num == cur_seq_no)
  232. goto finish;
  233. start_time += pls->timelines[i]->duration;
  234. if (pls->timelines[i]->repeat == -1) {
  235. start_time = pls->timelines[i]->duration * cur_seq_no;
  236. goto finish;
  237. }
  238. for (j = 0; j < pls->timelines[i]->repeat; j++) {
  239. num++;
  240. if (num == cur_seq_no)
  241. goto finish;
  242. start_time += pls->timelines[i]->duration;
  243. }
  244. num++;
  245. }
  246. }
  247. finish:
  248. return start_time;
  249. }
  250. static int64_t calc_next_seg_no_from_timelines(struct representation *pls, int64_t cur_time)
  251. {
  252. int64_t i = 0;
  253. int64_t j = 0;
  254. int64_t num = 0;
  255. int64_t start_time = 0;
  256. for (i = 0; i < pls->n_timelines; i++) {
  257. if (pls->timelines[i]->starttime > 0) {
  258. start_time = pls->timelines[i]->starttime;
  259. }
  260. if (start_time > cur_time)
  261. goto finish;
  262. start_time += pls->timelines[i]->duration;
  263. for (j = 0; j < pls->timelines[i]->repeat; j++) {
  264. num++;
  265. if (start_time > cur_time)
  266. goto finish;
  267. start_time += pls->timelines[i]->duration;
  268. }
  269. num++;
  270. }
  271. return -1;
  272. finish:
  273. return num;
  274. }
  275. static void free_fragment(struct fragment **seg)
  276. {
  277. if (!(*seg)) {
  278. return;
  279. }
  280. av_freep(&(*seg)->url);
  281. av_freep(seg);
  282. }
  283. static void free_fragment_list(struct representation *pls)
  284. {
  285. int i;
  286. for (i = 0; i < pls->n_fragments; i++) {
  287. free_fragment(&pls->fragments[i]);
  288. }
  289. av_freep(&pls->fragments);
  290. pls->n_fragments = 0;
  291. }
  292. static void free_timelines_list(struct representation *pls)
  293. {
  294. int i;
  295. for (i = 0; i < pls->n_timelines; i++) {
  296. av_freep(&pls->timelines[i]);
  297. }
  298. av_freep(&pls->timelines);
  299. pls->n_timelines = 0;
  300. }
  301. static void free_representation(struct representation *pls)
  302. {
  303. free_fragment_list(pls);
  304. free_timelines_list(pls);
  305. free_fragment(&pls->cur_seg);
  306. free_fragment(&pls->init_section);
  307. av_freep(&pls->init_sec_buf);
  308. av_freep(&pls->pb.buffer);
  309. ff_format_io_close(pls->parent, &pls->input);
  310. if (pls->ctx) {
  311. pls->ctx->pb = NULL;
  312. avformat_close_input(&pls->ctx);
  313. }
  314. av_freep(&pls->url_template);
  315. av_freep(&pls->lang);
  316. av_freep(&pls);
  317. }
  318. static void free_video_list(DASHContext *c)
  319. {
  320. int i;
  321. for (i = 0; i < c->n_videos; i++) {
  322. struct representation *pls = c->videos[i];
  323. free_representation(pls);
  324. }
  325. av_freep(&c->videos);
  326. c->n_videos = 0;
  327. }
  328. static void free_audio_list(DASHContext *c)
  329. {
  330. int i;
  331. for (i = 0; i < c->n_audios; i++) {
  332. struct representation *pls = c->audios[i];
  333. free_representation(pls);
  334. }
  335. av_freep(&c->audios);
  336. c->n_audios = 0;
  337. }
  338. static void free_subtitle_list(DASHContext *c)
  339. {
  340. int i;
  341. for (i = 0; i < c->n_subtitles; i++) {
  342. struct representation *pls = c->subtitles[i];
  343. free_representation(pls);
  344. }
  345. av_freep(&c->subtitles);
  346. c->n_subtitles = 0;
  347. }
  348. static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
  349. AVDictionary **opts, AVDictionary *opts2, int *is_http)
  350. {
  351. DASHContext *c = s->priv_data;
  352. AVDictionary *tmp = NULL;
  353. const char *proto_name = NULL;
  354. int ret;
  355. if (av_strstart(url, "crypto", NULL)) {
  356. if (url[6] == '+' || url[6] == ':')
  357. proto_name = avio_find_protocol_name(url + 7);
  358. }
  359. if (!proto_name)
  360. proto_name = avio_find_protocol_name(url);
  361. if (!proto_name)
  362. return AVERROR_INVALIDDATA;
  363. // only http(s) & file are allowed
  364. if (av_strstart(proto_name, "file", NULL)) {
  365. if (strcmp(c->allowed_extensions, "ALL") && !av_match_ext(url, c->allowed_extensions)) {
  366. av_log(s, AV_LOG_ERROR,
  367. "Filename extension of \'%s\' is not a common multimedia extension, blocked for security reasons.\n"
  368. "If you wish to override this adjust allowed_extensions, you can set it to \'ALL\' to allow all\n",
  369. url);
  370. return AVERROR_INVALIDDATA;
  371. }
  372. } else if (av_strstart(proto_name, "http", NULL)) {
  373. ;
  374. } else
  375. return AVERROR_INVALIDDATA;
  376. if (!strncmp(proto_name, url, strlen(proto_name)) && url[strlen(proto_name)] == ':')
  377. ;
  378. else if (av_strstart(url, "crypto", NULL) && !strncmp(proto_name, url + 7, strlen(proto_name)) && url[7 + strlen(proto_name)] == ':')
  379. ;
  380. else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5))
  381. return AVERROR_INVALIDDATA;
  382. av_freep(pb);
  383. av_dict_copy(&tmp, *opts, 0);
  384. av_dict_copy(&tmp, opts2, 0);
  385. ret = avio_open2(pb, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp);
  386. if (ret >= 0) {
  387. // update cookies on http response with setcookies.
  388. char *new_cookies = NULL;
  389. if (!(s->flags & AVFMT_FLAG_CUSTOM_IO))
  390. av_opt_get(*pb, "cookies", AV_OPT_SEARCH_CHILDREN, (uint8_t**)&new_cookies);
  391. if (new_cookies) {
  392. av_dict_set(opts, "cookies", new_cookies, AV_DICT_DONT_STRDUP_VAL);
  393. }
  394. }
  395. av_dict_free(&tmp);
  396. if (is_http)
  397. *is_http = av_strstart(proto_name, "http", NULL);
  398. return ret;
  399. }
  400. static char *get_content_url(xmlNodePtr *baseurl_nodes,
  401. int n_baseurl_nodes,
  402. int max_url_size,
  403. char *rep_id_val,
  404. char *rep_bandwidth_val,
  405. char *val)
  406. {
  407. int i;
  408. char *text;
  409. char *url = NULL;
  410. char *tmp_str = av_mallocz(max_url_size);
  411. if (!tmp_str)
  412. return NULL;
  413. for (i = 0; i < n_baseurl_nodes; ++i) {
  414. if (baseurl_nodes[i] &&
  415. baseurl_nodes[i]->children &&
  416. baseurl_nodes[i]->children->type == XML_TEXT_NODE) {
  417. text = xmlNodeGetContent(baseurl_nodes[i]->children);
  418. if (text) {
  419. memset(tmp_str, 0, max_url_size);
  420. ff_make_absolute_url(tmp_str, max_url_size, "", text);
  421. xmlFree(text);
  422. }
  423. }
  424. }
  425. if (val)
  426. ff_make_absolute_url(tmp_str, max_url_size, tmp_str, val);
  427. if (rep_id_val) {
  428. url = av_strireplace(tmp_str, "$RepresentationID$", (const char*)rep_id_val);
  429. if (!url) {
  430. goto end;
  431. }
  432. av_strlcpy(tmp_str, url, max_url_size);
  433. }
  434. if (rep_bandwidth_val && tmp_str[0] != '\0') {
  435. // free any previously assigned url before reassigning
  436. av_free(url);
  437. url = av_strireplace(tmp_str, "$Bandwidth$", (const char*)rep_bandwidth_val);
  438. if (!url) {
  439. goto end;
  440. }
  441. }
  442. end:
  443. av_free(tmp_str);
  444. return url;
  445. }
  446. static char *get_val_from_nodes_tab(xmlNodePtr *nodes, const int n_nodes, const char *attrname)
  447. {
  448. int i;
  449. char *val;
  450. for (i = 0; i < n_nodes; ++i) {
  451. if (nodes[i]) {
  452. val = xmlGetProp(nodes[i], attrname);
  453. if (val)
  454. return val;
  455. }
  456. }
  457. return NULL;
  458. }
  459. static xmlNodePtr find_child_node_by_name(xmlNodePtr rootnode, const char *nodename)
  460. {
  461. xmlNodePtr node = rootnode;
  462. if (!node) {
  463. return NULL;
  464. }
  465. node = xmlFirstElementChild(node);
  466. while (node) {
  467. if (!av_strcasecmp(node->name, nodename)) {
  468. return node;
  469. }
  470. node = xmlNextElementSibling(node);
  471. }
  472. return NULL;
  473. }
  474. static enum AVMediaType get_content_type(xmlNodePtr node)
  475. {
  476. enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
  477. int i = 0;
  478. const char *attr;
  479. char *val = NULL;
  480. if (node) {
  481. for (i = 0; i < 2; i++) {
  482. attr = i ? "mimeType" : "contentType";
  483. val = xmlGetProp(node, attr);
  484. if (val) {
  485. if (av_stristr((const char *)val, "video")) {
  486. type = AVMEDIA_TYPE_VIDEO;
  487. } else if (av_stristr((const char *)val, "audio")) {
  488. type = AVMEDIA_TYPE_AUDIO;
  489. } else if (av_stristr((const char *)val, "text")) {
  490. type = AVMEDIA_TYPE_SUBTITLE;
  491. }
  492. xmlFree(val);
  493. }
  494. }
  495. }
  496. return type;
  497. }
  498. static struct fragment * get_Fragment(char *range)
  499. {
  500. struct fragment * seg = av_mallocz(sizeof(struct fragment));
  501. if (!seg)
  502. return NULL;
  503. seg->size = -1;
  504. if (range) {
  505. char *str_end_offset;
  506. char *str_offset = av_strtok(range, "-", &str_end_offset);
  507. seg->url_offset = strtoll(str_offset, NULL, 10);
  508. seg->size = strtoll(str_end_offset, NULL, 10) - seg->url_offset + 1;
  509. }
  510. return seg;
  511. }
  512. static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representation *rep,
  513. xmlNodePtr fragmenturl_node,
  514. xmlNodePtr *baseurl_nodes,
  515. char *rep_id_val,
  516. char *rep_bandwidth_val)
  517. {
  518. DASHContext *c = s->priv_data;
  519. char *initialization_val = NULL;
  520. char *media_val = NULL;
  521. char *range_val = NULL;
  522. int max_url_size = c ? c->max_url_size: MAX_URL_SIZE;
  523. int err;
  524. if (!av_strcasecmp(fragmenturl_node->name, (const char *)"Initialization")) {
  525. initialization_val = xmlGetProp(fragmenturl_node, "sourceURL");
  526. range_val = xmlGetProp(fragmenturl_node, "range");
  527. if (initialization_val || range_val) {
  528. free_fragment(&rep->init_section);
  529. rep->init_section = get_Fragment(range_val);
  530. xmlFree(range_val);
  531. if (!rep->init_section) {
  532. xmlFree(initialization_val);
  533. return AVERROR(ENOMEM);
  534. }
  535. rep->init_section->url = get_content_url(baseurl_nodes, 4,
  536. max_url_size,
  537. rep_id_val,
  538. rep_bandwidth_val,
  539. initialization_val);
  540. xmlFree(initialization_val);
  541. if (!rep->init_section->url) {
  542. av_freep(&rep->init_section);
  543. return AVERROR(ENOMEM);
  544. }
  545. }
  546. } else if (!av_strcasecmp(fragmenturl_node->name, (const char *)"SegmentURL")) {
  547. media_val = xmlGetProp(fragmenturl_node, "media");
  548. range_val = xmlGetProp(fragmenturl_node, "mediaRange");
  549. if (media_val || range_val) {
  550. struct fragment *seg = get_Fragment(range_val);
  551. xmlFree(range_val);
  552. if (!seg) {
  553. xmlFree(media_val);
  554. return AVERROR(ENOMEM);
  555. }
  556. seg->url = get_content_url(baseurl_nodes, 4,
  557. max_url_size,
  558. rep_id_val,
  559. rep_bandwidth_val,
  560. media_val);
  561. xmlFree(media_val);
  562. if (!seg->url) {
  563. av_free(seg);
  564. return AVERROR(ENOMEM);
  565. }
  566. err = av_dynarray_add_nofree(&rep->fragments, &rep->n_fragments, seg);
  567. if (err < 0) {
  568. free_fragment(&seg);
  569. return err;
  570. }
  571. }
  572. }
  573. return 0;
  574. }
  575. static int parse_manifest_segmenttimeline(AVFormatContext *s, struct representation *rep,
  576. xmlNodePtr fragment_timeline_node)
  577. {
  578. xmlAttrPtr attr = NULL;
  579. char *val = NULL;
  580. int err;
  581. if (!av_strcasecmp(fragment_timeline_node->name, (const char *)"S")) {
  582. struct timeline *tml = av_mallocz(sizeof(struct timeline));
  583. if (!tml) {
  584. return AVERROR(ENOMEM);
  585. }
  586. attr = fragment_timeline_node->properties;
  587. while (attr) {
  588. val = xmlGetProp(fragment_timeline_node, attr->name);
  589. if (!val) {
  590. av_log(s, AV_LOG_WARNING, "parse_manifest_segmenttimeline attr->name = %s val is NULL\n", attr->name);
  591. continue;
  592. }
  593. if (!av_strcasecmp(attr->name, (const char *)"t")) {
  594. tml->starttime = (int64_t)strtoll(val, NULL, 10);
  595. } else if (!av_strcasecmp(attr->name, (const char *)"r")) {
  596. tml->repeat =(int64_t) strtoll(val, NULL, 10);
  597. } else if (!av_strcasecmp(attr->name, (const char *)"d")) {
  598. tml->duration = (int64_t)strtoll(val, NULL, 10);
  599. }
  600. attr = attr->next;
  601. xmlFree(val);
  602. }
  603. err = av_dynarray_add_nofree(&rep->timelines, &rep->n_timelines, tml);
  604. if (err < 0) {
  605. av_free(tml);
  606. return err;
  607. }
  608. }
  609. return 0;
  610. }
  611. static int resolve_content_path(AVFormatContext *s, const char *url, int *max_url_size, xmlNodePtr *baseurl_nodes, int n_baseurl_nodes)
  612. {
  613. char *tmp_str = NULL;
  614. char *path = NULL;
  615. char *mpdName = NULL;
  616. xmlNodePtr node = NULL;
  617. char *baseurl = NULL;
  618. char *root_url = NULL;
  619. char *text = NULL;
  620. char *tmp = NULL;
  621. int isRootHttp = 0;
  622. char token ='/';
  623. int start = 0;
  624. int rootId = 0;
  625. int updated = 0;
  626. int size = 0;
  627. int i;
  628. int tmp_max_url_size = strlen(url);
  629. for (i = n_baseurl_nodes-1; i >= 0 ; i--) {
  630. text = xmlNodeGetContent(baseurl_nodes[i]);
  631. if (!text)
  632. continue;
  633. tmp_max_url_size += strlen(text);
  634. if (ishttp(text)) {
  635. xmlFree(text);
  636. break;
  637. }
  638. xmlFree(text);
  639. }
  640. tmp_max_url_size = aligned(tmp_max_url_size);
  641. text = av_mallocz(tmp_max_url_size);
  642. if (!text) {
  643. updated = AVERROR(ENOMEM);
  644. goto end;
  645. }
  646. av_strlcpy(text, url, strlen(url)+1);
  647. tmp = text;
  648. while (mpdName = av_strtok(tmp, "/", &tmp)) {
  649. size = strlen(mpdName);
  650. }
  651. av_free(text);
  652. path = av_mallocz(tmp_max_url_size);
  653. tmp_str = av_mallocz(tmp_max_url_size);
  654. if (!tmp_str || !path) {
  655. updated = AVERROR(ENOMEM);
  656. goto end;
  657. }
  658. av_strlcpy (path, url, strlen(url) - size + 1);
  659. for (rootId = n_baseurl_nodes - 1; rootId > 0; rootId --) {
  660. if (!(node = baseurl_nodes[rootId])) {
  661. continue;
  662. }
  663. text = xmlNodeGetContent(node);
  664. if (ishttp(text)) {
  665. xmlFree(text);
  666. break;
  667. }
  668. xmlFree(text);
  669. }
  670. node = baseurl_nodes[rootId];
  671. baseurl = xmlNodeGetContent(node);
  672. root_url = (av_strcasecmp(baseurl, "")) ? baseurl : path;
  673. if (node) {
  674. xmlNodeSetContent(node, root_url);
  675. updated = 1;
  676. }
  677. size = strlen(root_url);
  678. isRootHttp = ishttp(root_url);
  679. if (root_url[size - 1] != token) {
  680. av_strlcat(root_url, "/", size + 2);
  681. size += 2;
  682. }
  683. for (i = 0; i < n_baseurl_nodes; ++i) {
  684. if (i == rootId) {
  685. continue;
  686. }
  687. text = xmlNodeGetContent(baseurl_nodes[i]);
  688. if (text && !av_strstart(text, "/", NULL)) {
  689. memset(tmp_str, 0, strlen(tmp_str));
  690. if (!ishttp(text) && isRootHttp) {
  691. av_strlcpy(tmp_str, root_url, size + 1);
  692. }
  693. start = (text[0] == token);
  694. if (start && av_stristr(tmp_str, text)) {
  695. char *p = tmp_str;
  696. if (!av_strncasecmp(tmp_str, "http://", 7)) {
  697. p += 7;
  698. } else if (!av_strncasecmp(tmp_str, "https://", 8)) {
  699. p += 8;
  700. }
  701. p = strchr(p, '/');
  702. memset(p + 1, 0, strlen(p));
  703. }
  704. av_strlcat(tmp_str, text + start, tmp_max_url_size);
  705. xmlNodeSetContent(baseurl_nodes[i], tmp_str);
  706. updated = 1;
  707. xmlFree(text);
  708. }
  709. }
  710. end:
  711. if (tmp_max_url_size > *max_url_size) {
  712. *max_url_size = tmp_max_url_size;
  713. }
  714. av_free(path);
  715. av_free(tmp_str);
  716. xmlFree(baseurl);
  717. return updated;
  718. }
  719. static int parse_manifest_representation(AVFormatContext *s, const char *url,
  720. xmlNodePtr node,
  721. xmlNodePtr adaptionset_node,
  722. xmlNodePtr mpd_baseurl_node,
  723. xmlNodePtr period_baseurl_node,
  724. xmlNodePtr period_segmenttemplate_node,
  725. xmlNodePtr period_segmentlist_node,
  726. xmlNodePtr fragment_template_node,
  727. xmlNodePtr content_component_node,
  728. xmlNodePtr adaptionset_baseurl_node,
  729. xmlNodePtr adaptionset_segmentlist_node,
  730. xmlNodePtr adaptionset_supplementalproperty_node)
  731. {
  732. int32_t ret = 0;
  733. DASHContext *c = s->priv_data;
  734. struct representation *rep = NULL;
  735. struct fragment *seg = NULL;
  736. xmlNodePtr representation_segmenttemplate_node = NULL;
  737. xmlNodePtr representation_baseurl_node = NULL;
  738. xmlNodePtr representation_segmentlist_node = NULL;
  739. xmlNodePtr segmentlists_tab[3];
  740. xmlNodePtr fragment_timeline_node = NULL;
  741. xmlNodePtr fragment_templates_tab[5];
  742. char *duration_val = NULL;
  743. char *presentation_timeoffset_val = NULL;
  744. char *startnumber_val = NULL;
  745. char *timescale_val = NULL;
  746. char *initialization_val = NULL;
  747. char *media_val = NULL;
  748. char *val = NULL;
  749. xmlNodePtr baseurl_nodes[4];
  750. xmlNodePtr representation_node = node;
  751. char *rep_id_val = xmlGetProp(representation_node, "id");
  752. char *rep_bandwidth_val = xmlGetProp(representation_node, "bandwidth");
  753. char *rep_framerate_val = xmlGetProp(representation_node, "frameRate");
  754. enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
  755. // try get information from representation
  756. if (type == AVMEDIA_TYPE_UNKNOWN)
  757. type = get_content_type(representation_node);
  758. // try get information from contentComponen
  759. if (type == AVMEDIA_TYPE_UNKNOWN)
  760. type = get_content_type(content_component_node);
  761. // try get information from adaption set
  762. if (type == AVMEDIA_TYPE_UNKNOWN)
  763. type = get_content_type(adaptionset_node);
  764. if (type == AVMEDIA_TYPE_UNKNOWN) {
  765. av_log(s, AV_LOG_VERBOSE, "Parsing '%s' - skipp not supported representation type\n", url);
  766. } else if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO || type == AVMEDIA_TYPE_SUBTITLE) {
  767. // convert selected representation to our internal struct
  768. rep = av_mallocz(sizeof(struct representation));
  769. if (!rep) {
  770. ret = AVERROR(ENOMEM);
  771. goto end;
  772. }
  773. if (c->adaptionset_lang) {
  774. rep->lang = av_strdup(c->adaptionset_lang);
  775. if (!rep->lang) {
  776. av_log(s, AV_LOG_ERROR, "alloc language memory failure\n");
  777. av_freep(&rep);
  778. ret = AVERROR(ENOMEM);
  779. goto end;
  780. }
  781. }
  782. rep->parent = s;
  783. representation_segmenttemplate_node = find_child_node_by_name(representation_node, "SegmentTemplate");
  784. representation_baseurl_node = find_child_node_by_name(representation_node, "BaseURL");
  785. representation_segmentlist_node = find_child_node_by_name(representation_node, "SegmentList");
  786. baseurl_nodes[0] = mpd_baseurl_node;
  787. baseurl_nodes[1] = period_baseurl_node;
  788. baseurl_nodes[2] = adaptionset_baseurl_node;
  789. baseurl_nodes[3] = representation_baseurl_node;
  790. ret = resolve_content_path(s, url, &c->max_url_size, baseurl_nodes, 4);
  791. c->max_url_size = aligned(c->max_url_size
  792. + (rep_id_val ? strlen(rep_id_val) : 0)
  793. + (rep_bandwidth_val ? strlen(rep_bandwidth_val) : 0));
  794. if (ret == AVERROR(ENOMEM) || ret == 0)
  795. goto free;
  796. if (representation_segmenttemplate_node || fragment_template_node || period_segmenttemplate_node) {
  797. fragment_timeline_node = NULL;
  798. fragment_templates_tab[0] = representation_segmenttemplate_node;
  799. fragment_templates_tab[1] = adaptionset_segmentlist_node;
  800. fragment_templates_tab[2] = fragment_template_node;
  801. fragment_templates_tab[3] = period_segmenttemplate_node;
  802. fragment_templates_tab[4] = period_segmentlist_node;
  803. initialization_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "initialization");
  804. if (initialization_val) {
  805. rep->init_section = av_mallocz(sizeof(struct fragment));
  806. if (!rep->init_section) {
  807. xmlFree(initialization_val);
  808. goto enomem;
  809. }
  810. c->max_url_size = aligned(c->max_url_size + strlen(initialization_val));
  811. rep->init_section->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, initialization_val);
  812. xmlFree(initialization_val);
  813. if (!rep->init_section->url)
  814. goto enomem;
  815. rep->init_section->size = -1;
  816. }
  817. media_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "media");
  818. if (media_val) {
  819. c->max_url_size = aligned(c->max_url_size + strlen(media_val));
  820. rep->url_template = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, media_val);
  821. xmlFree(media_val);
  822. }
  823. presentation_timeoffset_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "presentationTimeOffset");
  824. if (presentation_timeoffset_val) {
  825. rep->presentation_timeoffset = (int64_t) strtoll(presentation_timeoffset_val, NULL, 10);
  826. av_log(s, AV_LOG_TRACE, "rep->presentation_timeoffset = [%"PRId64"]\n", rep->presentation_timeoffset);
  827. xmlFree(presentation_timeoffset_val);
  828. }
  829. duration_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "duration");
  830. if (duration_val) {
  831. rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
  832. av_log(s, AV_LOG_TRACE, "rep->fragment_duration = [%"PRId64"]\n", rep->fragment_duration);
  833. xmlFree(duration_val);
  834. }
  835. timescale_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "timescale");
  836. if (timescale_val) {
  837. rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
  838. av_log(s, AV_LOG_TRACE, "rep->fragment_timescale = [%"PRId64"]\n", rep->fragment_timescale);
  839. xmlFree(timescale_val);
  840. }
  841. startnumber_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "startNumber");
  842. if (startnumber_val) {
  843. rep->start_number = rep->first_seq_no = (int64_t) strtoll(startnumber_val, NULL, 10);
  844. av_log(s, AV_LOG_TRACE, "rep->first_seq_no = [%"PRId64"]\n", rep->first_seq_no);
  845. xmlFree(startnumber_val);
  846. }
  847. if (adaptionset_supplementalproperty_node) {
  848. if (!av_strcasecmp(xmlGetProp(adaptionset_supplementalproperty_node,"schemeIdUri"), "http://dashif.org/guidelines/last-segment-number")) {
  849. val = xmlGetProp(adaptionset_supplementalproperty_node,"value");
  850. if (!val) {
  851. av_log(s, AV_LOG_ERROR, "Missing value attribute in adaptionset_supplementalproperty_node\n");
  852. } else {
  853. rep->last_seq_no =(int64_t) strtoll(val, NULL, 10) - 1;
  854. xmlFree(val);
  855. }
  856. }
  857. }
  858. fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
  859. if (!fragment_timeline_node)
  860. fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
  861. if (!fragment_timeline_node)
  862. fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
  863. if (!fragment_timeline_node)
  864. fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
  865. if (fragment_timeline_node) {
  866. fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
  867. while (fragment_timeline_node) {
  868. ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
  869. if (ret < 0)
  870. goto free;
  871. fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
  872. }
  873. }
  874. } else if (representation_baseurl_node && !representation_segmentlist_node) {
  875. seg = av_mallocz(sizeof(struct fragment));
  876. if (!seg)
  877. goto enomem;
  878. ret = av_dynarray_add_nofree(&rep->fragments, &rep->n_fragments, seg);
  879. if (ret < 0) {
  880. av_free(seg);
  881. goto free;
  882. }
  883. seg->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, NULL);
  884. if (!seg->url)
  885. goto enomem;
  886. seg->size = -1;
  887. } else if (representation_segmentlist_node) {
  888. // TODO: https://www.brendanlong.com/the-structure-of-an-mpeg-dash-mpd.html
  889. // http://www-itec.uni-klu.ac.at/dash/ddash/mpdGenerator.php?fragmentlength=15&type=full
  890. xmlNodePtr fragmenturl_node = NULL;
  891. segmentlists_tab[0] = representation_segmentlist_node;
  892. segmentlists_tab[1] = adaptionset_segmentlist_node;
  893. segmentlists_tab[2] = period_segmentlist_node;
  894. duration_val = get_val_from_nodes_tab(segmentlists_tab, 3, "duration");
  895. timescale_val = get_val_from_nodes_tab(segmentlists_tab, 3, "timescale");
  896. startnumber_val = get_val_from_nodes_tab(segmentlists_tab, 3, "startNumber");
  897. if (duration_val) {
  898. rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
  899. av_log(s, AV_LOG_TRACE, "rep->fragment_duration = [%"PRId64"]\n", rep->fragment_duration);
  900. xmlFree(duration_val);
  901. }
  902. if (timescale_val) {
  903. rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
  904. av_log(s, AV_LOG_TRACE, "rep->fragment_timescale = [%"PRId64"]\n", rep->fragment_timescale);
  905. xmlFree(timescale_val);
  906. }
  907. if (startnumber_val) {
  908. rep->start_number = rep->first_seq_no = (int64_t) strtoll(startnumber_val, NULL, 10);
  909. av_log(s, AV_LOG_TRACE, "rep->first_seq_no = [%"PRId64"]\n", rep->first_seq_no);
  910. xmlFree(startnumber_val);
  911. }
  912. fragmenturl_node = xmlFirstElementChild(representation_segmentlist_node);
  913. while (fragmenturl_node) {
  914. ret = parse_manifest_segmenturlnode(s, rep, fragmenturl_node,
  915. baseurl_nodes,
  916. rep_id_val,
  917. rep_bandwidth_val);
  918. if (ret < 0)
  919. goto free;
  920. fragmenturl_node = xmlNextElementSibling(fragmenturl_node);
  921. }
  922. fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
  923. if (!fragment_timeline_node)
  924. fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
  925. if (fragment_timeline_node) {
  926. fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
  927. while (fragment_timeline_node) {
  928. ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
  929. if (ret < 0)
  930. goto free;
  931. fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
  932. }
  933. }
  934. } else {
  935. av_log(s, AV_LOG_ERROR, "Unknown format of Representation node id[%s] \n", (const char *)rep_id_val);
  936. goto free;
  937. }
  938. if (rep) {
  939. if (rep->fragment_duration > 0 && !rep->fragment_timescale)
  940. rep->fragment_timescale = 1;
  941. rep->bandwidth = rep_bandwidth_val ? atoi(rep_bandwidth_val) : 0;
  942. strncpy(rep->id, rep_id_val ? rep_id_val : "", sizeof(rep->id));
  943. rep->framerate = av_make_q(0, 0);
  944. if (type == AVMEDIA_TYPE_VIDEO && rep_framerate_val) {
  945. ret = av_parse_video_rate(&rep->framerate, rep_framerate_val);
  946. if (ret < 0)
  947. av_log(s, AV_LOG_VERBOSE, "Ignoring invalid frame rate '%s'\n", rep_framerate_val);
  948. }
  949. switch (type) {
  950. case AVMEDIA_TYPE_VIDEO:
  951. ret = av_dynarray_add_nofree(&c->videos, &c->n_videos, rep);
  952. break;
  953. case AVMEDIA_TYPE_AUDIO:
  954. ret = av_dynarray_add_nofree(&c->audios, &c->n_audios, rep);
  955. break;
  956. case AVMEDIA_TYPE_SUBTITLE:
  957. ret = av_dynarray_add_nofree(&c->subtitles, &c->n_subtitles, rep);
  958. break;
  959. default:
  960. av_log(s, AV_LOG_WARNING, "Unsupported the stream type %d\n", type);
  961. break;
  962. }
  963. if (ret < 0)
  964. goto free;
  965. }
  966. }
  967. end:
  968. if (rep_id_val)
  969. xmlFree(rep_id_val);
  970. if (rep_bandwidth_val)
  971. xmlFree(rep_bandwidth_val);
  972. if (rep_framerate_val)
  973. xmlFree(rep_framerate_val);
  974. return ret;
  975. enomem:
  976. ret = AVERROR(ENOMEM);
  977. free:
  978. free_representation(rep);
  979. goto end;
  980. }
  981. static int parse_manifest_adaptationset_attr(AVFormatContext *s, xmlNodePtr adaptionset_node)
  982. {
  983. DASHContext *c = s->priv_data;
  984. if (!adaptionset_node) {
  985. av_log(s, AV_LOG_WARNING, "Cannot get AdaptionSet\n");
  986. return AVERROR(EINVAL);
  987. }
  988. c->adaptionset_lang = xmlGetProp(adaptionset_node, "lang");
  989. return 0;
  990. }
  991. static int parse_manifest_adaptationset(AVFormatContext *s, const char *url,
  992. xmlNodePtr adaptionset_node,
  993. xmlNodePtr mpd_baseurl_node,
  994. xmlNodePtr period_baseurl_node,
  995. xmlNodePtr period_segmenttemplate_node,
  996. xmlNodePtr period_segmentlist_node)
  997. {
  998. int ret = 0;
  999. DASHContext *c = s->priv_data;
  1000. xmlNodePtr fragment_template_node = NULL;
  1001. xmlNodePtr content_component_node = NULL;
  1002. xmlNodePtr adaptionset_baseurl_node = NULL;
  1003. xmlNodePtr adaptionset_segmentlist_node = NULL;
  1004. xmlNodePtr adaptionset_supplementalproperty_node = NULL;
  1005. xmlNodePtr node = NULL;
  1006. ret = parse_manifest_adaptationset_attr(s, adaptionset_node);
  1007. if (ret < 0)
  1008. return ret;
  1009. node = xmlFirstElementChild(adaptionset_node);
  1010. while (node) {
  1011. if (!av_strcasecmp(node->name, (const char *)"SegmentTemplate")) {
  1012. fragment_template_node = node;
  1013. } else if (!av_strcasecmp(node->name, (const char *)"ContentComponent")) {
  1014. content_component_node = node;
  1015. } else if (!av_strcasecmp(node->name, (const char *)"BaseURL")) {
  1016. adaptionset_baseurl_node = node;
  1017. } else if (!av_strcasecmp(node->name, (const char *)"SegmentList")) {
  1018. adaptionset_segmentlist_node = node;
  1019. } else if (!av_strcasecmp(node->name, (const char *)"SupplementalProperty")) {
  1020. adaptionset_supplementalproperty_node = node;
  1021. } else if (!av_strcasecmp(node->name, (const char *)"Representation")) {
  1022. ret = parse_manifest_representation(s, url, node,
  1023. adaptionset_node,
  1024. mpd_baseurl_node,
  1025. period_baseurl_node,
  1026. period_segmenttemplate_node,
  1027. period_segmentlist_node,
  1028. fragment_template_node,
  1029. content_component_node,
  1030. adaptionset_baseurl_node,
  1031. adaptionset_segmentlist_node,
  1032. adaptionset_supplementalproperty_node);
  1033. if (ret < 0)
  1034. goto err;
  1035. }
  1036. node = xmlNextElementSibling(node);
  1037. }
  1038. err:
  1039. av_freep(&c->adaptionset_lang);
  1040. return ret;
  1041. }
  1042. static int parse_programinformation(AVFormatContext *s, xmlNodePtr node)
  1043. {
  1044. xmlChar *val = NULL;
  1045. node = xmlFirstElementChild(node);
  1046. while (node) {
  1047. if (!av_strcasecmp(node->name, "Title")) {
  1048. val = xmlNodeGetContent(node);
  1049. if (val) {
  1050. av_dict_set(&s->metadata, "Title", val, 0);
  1051. }
  1052. } else if (!av_strcasecmp(node->name, "Source")) {
  1053. val = xmlNodeGetContent(node);
  1054. if (val) {
  1055. av_dict_set(&s->metadata, "Source", val, 0);
  1056. }
  1057. } else if (!av_strcasecmp(node->name, "Copyright")) {
  1058. val = xmlNodeGetContent(node);
  1059. if (val) {
  1060. av_dict_set(&s->metadata, "Copyright", val, 0);
  1061. }
  1062. }
  1063. node = xmlNextElementSibling(node);
  1064. xmlFree(val);
  1065. val = NULL;
  1066. }
  1067. return 0;
  1068. }
  1069. static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
  1070. {
  1071. DASHContext *c = s->priv_data;
  1072. int ret = 0;
  1073. int close_in = 0;
  1074. uint8_t *new_url = NULL;
  1075. int64_t filesize = 0;
  1076. AVBPrint buf;
  1077. AVDictionary *opts = NULL;
  1078. xmlDoc *doc = NULL;
  1079. xmlNodePtr root_element = NULL;
  1080. xmlNodePtr node = NULL;
  1081. xmlNodePtr period_node = NULL;
  1082. xmlNodePtr tmp_node = NULL;
  1083. xmlNodePtr mpd_baseurl_node = NULL;
  1084. xmlNodePtr period_baseurl_node = NULL;
  1085. xmlNodePtr period_segmenttemplate_node = NULL;
  1086. xmlNodePtr period_segmentlist_node = NULL;
  1087. xmlNodePtr adaptionset_node = NULL;
  1088. xmlAttrPtr attr = NULL;
  1089. char *val = NULL;
  1090. uint32_t period_duration_sec = 0;
  1091. uint32_t period_start_sec = 0;
  1092. if (!in) {
  1093. close_in = 1;
  1094. av_dict_copy(&opts, c->avio_opts, 0);
  1095. ret = avio_open2(&in, url, AVIO_FLAG_READ, c->interrupt_callback, &opts);
  1096. av_dict_free(&opts);
  1097. if (ret < 0)
  1098. return ret;
  1099. }
  1100. if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, &new_url) >= 0) {
  1101. c->base_url = av_strdup(new_url);
  1102. } else {
  1103. c->base_url = av_strdup(url);
  1104. }
  1105. filesize = avio_size(in);
  1106. if (filesize > MAX_MANIFEST_SIZE) {
  1107. av_log(s, AV_LOG_ERROR, "Manifest too large: %"PRId64"\n", filesize);
  1108. return AVERROR_INVALIDDATA;
  1109. }
  1110. av_bprint_init(&buf, (filesize > 0) ? filesize + 1 : DEFAULT_MANIFEST_SIZE, AV_BPRINT_SIZE_UNLIMITED);
  1111. if ((ret = avio_read_to_bprint(in, &buf, MAX_MANIFEST_SIZE)) < 0 ||
  1112. !avio_feof(in) ||
  1113. (filesize = buf.len) == 0) {
  1114. av_log(s, AV_LOG_ERROR, "Unable to read to manifest '%s'\n", url);
  1115. if (ret == 0)
  1116. ret = AVERROR_INVALIDDATA;
  1117. } else {
  1118. LIBXML_TEST_VERSION
  1119. doc = xmlReadMemory(buf.str, filesize, c->base_url, NULL, 0);
  1120. root_element = xmlDocGetRootElement(doc);
  1121. node = root_element;
  1122. if (!node) {
  1123. ret = AVERROR_INVALIDDATA;
  1124. av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing root node\n", url);
  1125. goto cleanup;
  1126. }
  1127. if (node->type != XML_ELEMENT_NODE ||
  1128. av_strcasecmp(node->name, (const char *)"MPD")) {
  1129. ret = AVERROR_INVALIDDATA;
  1130. av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - wrong root node name[%s] type[%d]\n", url, node->name, (int)node->type);
  1131. goto cleanup;
  1132. }
  1133. val = xmlGetProp(node, "type");
  1134. if (!val) {
  1135. av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing type attrib\n", url);
  1136. ret = AVERROR_INVALIDDATA;
  1137. goto cleanup;
  1138. }
  1139. if (!av_strcasecmp(val, (const char *)"dynamic"))
  1140. c->is_live = 1;
  1141. xmlFree(val);
  1142. attr = node->properties;
  1143. while (attr) {
  1144. val = xmlGetProp(node, attr->name);
  1145. if (!av_strcasecmp(attr->name, (const char *)"availabilityStartTime")) {
  1146. c->availability_start_time = get_utc_date_time_insec(s, (const char *)val);
  1147. av_log(s, AV_LOG_TRACE, "c->availability_start_time = [%"PRId64"]\n", c->availability_start_time);
  1148. } else if (!av_strcasecmp(attr->name, (const char *)"availabilityEndTime")) {
  1149. c->availability_end_time = get_utc_date_time_insec(s, (const char *)val);
  1150. av_log(s, AV_LOG_TRACE, "c->availability_end_time = [%"PRId64"]\n", c->availability_end_time);
  1151. } else if (!av_strcasecmp(attr->name, (const char *)"publishTime")) {
  1152. c->publish_time = get_utc_date_time_insec(s, (const char *)val);
  1153. av_log(s, AV_LOG_TRACE, "c->publish_time = [%"PRId64"]\n", c->publish_time);
  1154. } else if (!av_strcasecmp(attr->name, (const char *)"minimumUpdatePeriod")) {
  1155. c->minimum_update_period = get_duration_insec(s, (const char *)val);
  1156. av_log(s, AV_LOG_TRACE, "c->minimum_update_period = [%"PRId64"]\n", c->minimum_update_period);
  1157. } else if (!av_strcasecmp(attr->name, (const char *)"timeShiftBufferDepth")) {
  1158. c->time_shift_buffer_depth = get_duration_insec(s, (const char *)val);
  1159. av_log(s, AV_LOG_TRACE, "c->time_shift_buffer_depth = [%"PRId64"]\n", c->time_shift_buffer_depth);
  1160. } else if (!av_strcasecmp(attr->name, (const char *)"minBufferTime")) {
  1161. c->min_buffer_time = get_duration_insec(s, (const char *)val);
  1162. av_log(s, AV_LOG_TRACE, "c->min_buffer_time = [%"PRId64"]\n", c->min_buffer_time);
  1163. } else if (!av_strcasecmp(attr->name, (const char *)"suggestedPresentationDelay")) {
  1164. c->suggested_presentation_delay = get_duration_insec(s, (const char *)val);
  1165. av_log(s, AV_LOG_TRACE, "c->suggested_presentation_delay = [%"PRId64"]\n", c->suggested_presentation_delay);
  1166. } else if (!av_strcasecmp(attr->name, (const char *)"mediaPresentationDuration")) {
  1167. c->media_presentation_duration = get_duration_insec(s, (const char *)val);
  1168. av_log(s, AV_LOG_TRACE, "c->media_presentation_duration = [%"PRId64"]\n", c->media_presentation_duration);
  1169. }
  1170. attr = attr->next;
  1171. xmlFree(val);
  1172. }
  1173. tmp_node = find_child_node_by_name(node, "BaseURL");
  1174. if (tmp_node) {
  1175. mpd_baseurl_node = xmlCopyNode(tmp_node,1);
  1176. } else {
  1177. mpd_baseurl_node = xmlNewNode(NULL, "BaseURL");
  1178. }
  1179. // at now we can handle only one period, with the longest duration
  1180. node = xmlFirstElementChild(node);
  1181. while (node) {
  1182. if (!av_strcasecmp(node->name, (const char *)"Period")) {
  1183. period_duration_sec = 0;
  1184. period_start_sec = 0;
  1185. attr = node->properties;
  1186. while (attr) {
  1187. val = xmlGetProp(node, attr->name);
  1188. if (!av_strcasecmp(attr->name, (const char *)"duration")) {
  1189. period_duration_sec = get_duration_insec(s, (const char *)val);
  1190. } else if (!av_strcasecmp(attr->name, (const char *)"start")) {
  1191. period_start_sec = get_duration_insec(s, (const char *)val);
  1192. }
  1193. attr = attr->next;
  1194. xmlFree(val);
  1195. }
  1196. if ((period_duration_sec) >= (c->period_duration)) {
  1197. period_node = node;
  1198. c->period_duration = period_duration_sec;
  1199. c->period_start = period_start_sec;
  1200. if (c->period_start > 0)
  1201. c->media_presentation_duration = c->period_duration;
  1202. }
  1203. } else if (!av_strcasecmp(node->name, "ProgramInformation")) {
  1204. parse_programinformation(s, node);
  1205. }
  1206. node = xmlNextElementSibling(node);
  1207. }
  1208. if (!period_node) {
  1209. av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing Period node\n", url);
  1210. ret = AVERROR_INVALIDDATA;
  1211. goto cleanup;
  1212. }
  1213. adaptionset_node = xmlFirstElementChild(period_node);
  1214. while (adaptionset_node) {
  1215. if (!av_strcasecmp(adaptionset_node->name, (const char *)"BaseURL")) {
  1216. period_baseurl_node = adaptionset_node;
  1217. } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"SegmentTemplate")) {
  1218. period_segmenttemplate_node = adaptionset_node;
  1219. } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"SegmentList")) {
  1220. period_segmentlist_node = adaptionset_node;
  1221. } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"AdaptationSet")) {
  1222. parse_manifest_adaptationset(s, url, adaptionset_node, mpd_baseurl_node, period_baseurl_node, period_segmenttemplate_node, period_segmentlist_node);
  1223. }
  1224. adaptionset_node = xmlNextElementSibling(adaptionset_node);
  1225. }
  1226. cleanup:
  1227. /*free the document */
  1228. xmlFreeDoc(doc);
  1229. xmlCleanupParser();
  1230. xmlFreeNode(mpd_baseurl_node);
  1231. }
  1232. av_free(new_url);
  1233. av_bprint_finalize(&buf, NULL);
  1234. if (close_in) {
  1235. avio_close(in);
  1236. }
  1237. return ret;
  1238. }
  1239. static int64_t calc_cur_seg_no(AVFormatContext *s, struct representation *pls)
  1240. {
  1241. DASHContext *c = s->priv_data;
  1242. int64_t num = 0;
  1243. int64_t start_time_offset = 0;
  1244. if (c->is_live) {
  1245. if (pls->n_fragments) {
  1246. av_log(s, AV_LOG_TRACE, "in n_fragments mode\n");
  1247. num = pls->first_seq_no;
  1248. } else if (pls->n_timelines) {
  1249. av_log(s, AV_LOG_TRACE, "in n_timelines mode\n");
  1250. start_time_offset = get_segment_start_time_based_on_timeline(pls, 0xFFFFFFFF) - 60 * pls->fragment_timescale; // 60 seconds before end
  1251. num = calc_next_seg_no_from_timelines(pls, start_time_offset);
  1252. if (num == -1)
  1253. num = pls->first_seq_no;
  1254. else
  1255. num += pls->first_seq_no;
  1256. } else if (pls->fragment_duration){
  1257. av_log(s, AV_LOG_TRACE, "in fragment_duration mode fragment_timescale = %"PRId64", presentation_timeoffset = %"PRId64"\n", pls->fragment_timescale, pls->presentation_timeoffset);
  1258. if (pls->presentation_timeoffset) {
  1259. num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time) * pls->fragment_timescale)-pls->presentation_timeoffset) / pls->fragment_duration - c->min_buffer_time;
  1260. } else if (c->publish_time > 0 && !c->availability_start_time) {
  1261. if (c->min_buffer_time) {
  1262. num = pls->first_seq_no + (((c->publish_time + pls->fragment_duration) - c->suggested_presentation_delay) * pls->fragment_timescale) / pls->fragment_duration - c->min_buffer_time;
  1263. } else {
  1264. num = pls->first_seq_no + (((c->publish_time - c->time_shift_buffer_depth + pls->fragment_duration) - c->suggested_presentation_delay) * pls->fragment_timescale) / pls->fragment_duration;
  1265. }
  1266. } else {
  1267. num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time) - c->suggested_presentation_delay) * pls->fragment_timescale) / pls->fragment_duration;
  1268. }
  1269. }
  1270. } else {
  1271. num = pls->first_seq_no;
  1272. }
  1273. return num;
  1274. }
  1275. static int64_t calc_min_seg_no(AVFormatContext *s, struct representation *pls)
  1276. {
  1277. DASHContext *c = s->priv_data;
  1278. int64_t num = 0;
  1279. if (c->is_live && pls->fragment_duration) {
  1280. av_log(s, AV_LOG_TRACE, "in live mode\n");
  1281. 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;
  1282. } else {
  1283. num = pls->first_seq_no;
  1284. }
  1285. return num;
  1286. }
  1287. static int64_t calc_max_seg_no(struct representation *pls, DASHContext *c)
  1288. {
  1289. int64_t num = 0;
  1290. if (pls->n_fragments) {
  1291. num = pls->first_seq_no + pls->n_fragments - 1;
  1292. } else if (pls->n_timelines) {
  1293. int i = 0;
  1294. num = pls->first_seq_no + pls->n_timelines - 1;
  1295. for (i = 0; i < pls->n_timelines; i++) {
  1296. if (pls->timelines[i]->repeat == -1) {
  1297. int length_of_each_segment = pls->timelines[i]->duration / pls->fragment_timescale;
  1298. num = c->period_duration / length_of_each_segment;
  1299. } else {
  1300. num += pls->timelines[i]->repeat;
  1301. }
  1302. }
  1303. } else if (c->is_live && pls->fragment_duration) {
  1304. num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time)) * pls->fragment_timescale) / pls->fragment_duration;
  1305. } else if (pls->fragment_duration) {
  1306. num = pls->first_seq_no + (c->media_presentation_duration * pls->fragment_timescale) / pls->fragment_duration;
  1307. }
  1308. return num;
  1309. }
  1310. static void move_timelines(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
  1311. {
  1312. if (rep_dest && rep_src ) {
  1313. free_timelines_list(rep_dest);
  1314. rep_dest->timelines = rep_src->timelines;
  1315. rep_dest->n_timelines = rep_src->n_timelines;
  1316. rep_dest->first_seq_no = rep_src->first_seq_no;
  1317. rep_dest->last_seq_no = calc_max_seg_no(rep_dest, c);
  1318. rep_src->timelines = NULL;
  1319. rep_src->n_timelines = 0;
  1320. rep_dest->cur_seq_no = rep_src->cur_seq_no;
  1321. }
  1322. }
  1323. static void move_segments(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
  1324. {
  1325. if (rep_dest && rep_src ) {
  1326. free_fragment_list(rep_dest);
  1327. if (rep_src->start_number > (rep_dest->start_number + rep_dest->n_fragments))
  1328. rep_dest->cur_seq_no = 0;
  1329. else
  1330. rep_dest->cur_seq_no += rep_src->start_number - rep_dest->start_number;
  1331. rep_dest->fragments = rep_src->fragments;
  1332. rep_dest->n_fragments = rep_src->n_fragments;
  1333. rep_dest->parent = rep_src->parent;
  1334. rep_dest->last_seq_no = calc_max_seg_no(rep_dest, c);
  1335. rep_src->fragments = NULL;
  1336. rep_src->n_fragments = 0;
  1337. }
  1338. }
  1339. static int refresh_manifest(AVFormatContext *s)
  1340. {
  1341. int ret = 0, i;
  1342. DASHContext *c = s->priv_data;
  1343. // save current context
  1344. int n_videos = c->n_videos;
  1345. struct representation **videos = c->videos;
  1346. int n_audios = c->n_audios;
  1347. struct representation **audios = c->audios;
  1348. int n_subtitles = c->n_subtitles;
  1349. struct representation **subtitles = c->subtitles;
  1350. char *base_url = c->base_url;
  1351. c->base_url = NULL;
  1352. c->n_videos = 0;
  1353. c->videos = NULL;
  1354. c->n_audios = 0;
  1355. c->audios = NULL;
  1356. c->n_subtitles = 0;
  1357. c->subtitles = NULL;
  1358. ret = parse_manifest(s, s->url, NULL);
  1359. if (ret)
  1360. goto finish;
  1361. if (c->n_videos != n_videos) {
  1362. av_log(c, AV_LOG_ERROR,
  1363. "new manifest has mismatched no. of video representations, %d -> %d\n",
  1364. n_videos, c->n_videos);
  1365. return AVERROR_INVALIDDATA;
  1366. }
  1367. if (c->n_audios != n_audios) {
  1368. av_log(c, AV_LOG_ERROR,
  1369. "new manifest has mismatched no. of audio representations, %d -> %d\n",
  1370. n_audios, c->n_audios);
  1371. return AVERROR_INVALIDDATA;
  1372. }
  1373. if (c->n_subtitles != n_subtitles) {
  1374. av_log(c, AV_LOG_ERROR,
  1375. "new manifest has mismatched no. of subtitles representations, %d -> %d\n",
  1376. n_subtitles, c->n_subtitles);
  1377. return AVERROR_INVALIDDATA;
  1378. }
  1379. for (i = 0; i < n_videos; i++) {
  1380. struct representation *cur_video = videos[i];
  1381. struct representation *ccur_video = c->videos[i];
  1382. if (cur_video->timelines) {
  1383. // calc current time
  1384. int64_t currentTime = get_segment_start_time_based_on_timeline(cur_video, cur_video->cur_seq_no) / cur_video->fragment_timescale;
  1385. // update segments
  1386. ccur_video->cur_seq_no = calc_next_seg_no_from_timelines(ccur_video, currentTime * cur_video->fragment_timescale - 1);
  1387. if (ccur_video->cur_seq_no >= 0) {
  1388. move_timelines(ccur_video, cur_video, c);
  1389. }
  1390. }
  1391. if (cur_video->fragments) {
  1392. move_segments(ccur_video, cur_video, c);
  1393. }
  1394. }
  1395. for (i = 0; i < n_audios; i++) {
  1396. struct representation *cur_audio = audios[i];
  1397. struct representation *ccur_audio = c->audios[i];
  1398. if (cur_audio->timelines) {
  1399. // calc current time
  1400. int64_t currentTime = get_segment_start_time_based_on_timeline(cur_audio, cur_audio->cur_seq_no) / cur_audio->fragment_timescale;
  1401. // update segments
  1402. ccur_audio->cur_seq_no = calc_next_seg_no_from_timelines(ccur_audio, currentTime * cur_audio->fragment_timescale - 1);
  1403. if (ccur_audio->cur_seq_no >= 0) {
  1404. move_timelines(ccur_audio, cur_audio, c);
  1405. }
  1406. }
  1407. if (cur_audio->fragments) {
  1408. move_segments(ccur_audio, cur_audio, c);
  1409. }
  1410. }
  1411. finish:
  1412. // restore context
  1413. if (c->base_url)
  1414. av_free(base_url);
  1415. else
  1416. c->base_url = base_url;
  1417. if (c->subtitles)
  1418. free_subtitle_list(c);
  1419. if (c->audios)
  1420. free_audio_list(c);
  1421. if (c->videos)
  1422. free_video_list(c);
  1423. c->n_subtitles = n_subtitles;
  1424. c->subtitles = subtitles;
  1425. c->n_audios = n_audios;
  1426. c->audios = audios;
  1427. c->n_videos = n_videos;
  1428. c->videos = videos;
  1429. return ret;
  1430. }
  1431. static struct fragment *get_current_fragment(struct representation *pls)
  1432. {
  1433. int64_t min_seq_no = 0;
  1434. int64_t max_seq_no = 0;
  1435. struct fragment *seg = NULL;
  1436. struct fragment *seg_ptr = NULL;
  1437. DASHContext *c = pls->parent->priv_data;
  1438. while (( !ff_check_interrupt(c->interrupt_callback)&& pls->n_fragments > 0)) {
  1439. if (pls->cur_seq_no < pls->n_fragments) {
  1440. seg_ptr = pls->fragments[pls->cur_seq_no];
  1441. seg = av_mallocz(sizeof(struct fragment));
  1442. if (!seg) {
  1443. return NULL;
  1444. }
  1445. seg->url = av_strdup(seg_ptr->url);
  1446. if (!seg->url) {
  1447. av_free(seg);
  1448. return NULL;
  1449. }
  1450. seg->size = seg_ptr->size;
  1451. seg->url_offset = seg_ptr->url_offset;
  1452. return seg;
  1453. } else if (c->is_live) {
  1454. refresh_manifest(pls->parent);
  1455. } else {
  1456. break;
  1457. }
  1458. }
  1459. if (c->is_live) {
  1460. min_seq_no = calc_min_seg_no(pls->parent, pls);
  1461. max_seq_no = calc_max_seg_no(pls, c);
  1462. if (pls->timelines || pls->fragments) {
  1463. refresh_manifest(pls->parent);
  1464. }
  1465. if (pls->cur_seq_no <= min_seq_no) {
  1466. av_log(pls->parent, AV_LOG_VERBOSE, "old fragment: cur[%"PRId64"] min[%"PRId64"] max[%"PRId64"]\n", (int64_t)pls->cur_seq_no, min_seq_no, max_seq_no);
  1467. pls->cur_seq_no = calc_cur_seg_no(pls->parent, pls);
  1468. } else if (pls->cur_seq_no > max_seq_no) {
  1469. av_log(pls->parent, AV_LOG_VERBOSE, "new fragment: min[%"PRId64"] max[%"PRId64"]\n", min_seq_no, max_seq_no);
  1470. }
  1471. seg = av_mallocz(sizeof(struct fragment));
  1472. if (!seg) {
  1473. return NULL;
  1474. }
  1475. } else if (pls->cur_seq_no <= pls->last_seq_no) {
  1476. seg = av_mallocz(sizeof(struct fragment));
  1477. if (!seg) {
  1478. return NULL;
  1479. }
  1480. }
  1481. if (seg) {
  1482. char *tmpfilename= av_mallocz(c->max_url_size);
  1483. if (!tmpfilename) {
  1484. return NULL;
  1485. }
  1486. ff_dash_fill_tmpl_params(tmpfilename, c->max_url_size, pls->url_template, 0, pls->cur_seq_no, 0, get_segment_start_time_based_on_timeline(pls, pls->cur_seq_no));
  1487. seg->url = av_strireplace(pls->url_template, pls->url_template, tmpfilename);
  1488. if (!seg->url) {
  1489. av_log(pls->parent, AV_LOG_WARNING, "Unable to resolve template url '%s', try to use origin template\n", pls->url_template);
  1490. seg->url = av_strdup(pls->url_template);
  1491. if (!seg->url) {
  1492. av_log(pls->parent, AV_LOG_ERROR, "Cannot resolve template url '%s'\n", pls->url_template);
  1493. av_free(tmpfilename);
  1494. return NULL;
  1495. }
  1496. }
  1497. av_free(tmpfilename);
  1498. seg->size = -1;
  1499. }
  1500. return seg;
  1501. }
  1502. static int read_from_url(struct representation *pls, struct fragment *seg,
  1503. uint8_t *buf, int buf_size)
  1504. {
  1505. int ret;
  1506. /* limit read if the fragment was only a part of a file */
  1507. if (seg->size >= 0)
  1508. buf_size = FFMIN(buf_size, pls->cur_seg_size - pls->cur_seg_offset);
  1509. ret = avio_read(pls->input, buf, buf_size);
  1510. if (ret > 0)
  1511. pls->cur_seg_offset += ret;
  1512. return ret;
  1513. }
  1514. static int open_input(DASHContext *c, struct representation *pls, struct fragment *seg)
  1515. {
  1516. AVDictionary *opts = NULL;
  1517. char *url = NULL;
  1518. int ret = 0;
  1519. url = av_mallocz(c->max_url_size);
  1520. if (!url) {
  1521. ret = AVERROR(ENOMEM);
  1522. goto cleanup;
  1523. }
  1524. if (seg->size >= 0) {
  1525. /* try to restrict the HTTP request to the part we want
  1526. * (if this is in fact a HTTP request) */
  1527. av_dict_set_int(&opts, "offset", seg->url_offset, 0);
  1528. av_dict_set_int(&opts, "end_offset", seg->url_offset + seg->size, 0);
  1529. }
  1530. ff_make_absolute_url(url, c->max_url_size, c->base_url, seg->url);
  1531. av_log(pls->parent, AV_LOG_VERBOSE, "DASH request for url '%s', offset %"PRId64"\n",
  1532. url, seg->url_offset);
  1533. ret = open_url(pls->parent, &pls->input, url, &c->avio_opts, opts, NULL);
  1534. cleanup:
  1535. av_free(url);
  1536. av_dict_free(&opts);
  1537. pls->cur_seg_offset = 0;
  1538. pls->cur_seg_size = seg->size;
  1539. return ret;
  1540. }
  1541. static int update_init_section(struct representation *pls)
  1542. {
  1543. static const int max_init_section_size = 1024 * 1024;
  1544. DASHContext *c = pls->parent->priv_data;
  1545. int64_t sec_size;
  1546. int64_t urlsize;
  1547. int ret;
  1548. if (!pls->init_section || pls->init_sec_buf)
  1549. return 0;
  1550. ret = open_input(c, pls, pls->init_section);
  1551. if (ret < 0) {
  1552. av_log(pls->parent, AV_LOG_WARNING,
  1553. "Failed to open an initialization section\n");
  1554. return ret;
  1555. }
  1556. if (pls->init_section->size >= 0)
  1557. sec_size = pls->init_section->size;
  1558. else if ((urlsize = avio_size(pls->input)) >= 0)
  1559. sec_size = urlsize;
  1560. else
  1561. sec_size = max_init_section_size;
  1562. av_log(pls->parent, AV_LOG_DEBUG,
  1563. "Downloading an initialization section of size %"PRId64"\n",
  1564. sec_size);
  1565. sec_size = FFMIN(sec_size, max_init_section_size);
  1566. av_fast_malloc(&pls->init_sec_buf, &pls->init_sec_buf_size, sec_size);
  1567. ret = read_from_url(pls, pls->init_section, pls->init_sec_buf,
  1568. pls->init_sec_buf_size);
  1569. ff_format_io_close(pls->parent, &pls->input);
  1570. if (ret < 0)
  1571. return ret;
  1572. pls->init_sec_data_len = ret;
  1573. pls->init_sec_buf_read_offset = 0;
  1574. return 0;
  1575. }
  1576. static int64_t seek_data(void *opaque, int64_t offset, int whence)
  1577. {
  1578. struct representation *v = opaque;
  1579. if (v->n_fragments && !v->init_sec_data_len) {
  1580. return avio_seek(v->input, offset, whence);
  1581. }
  1582. return AVERROR(ENOSYS);
  1583. }
  1584. static int read_data(void *opaque, uint8_t *buf, int buf_size)
  1585. {
  1586. int ret = 0;
  1587. struct representation *v = opaque;
  1588. DASHContext *c = v->parent->priv_data;
  1589. restart:
  1590. if (!v->input) {
  1591. free_fragment(&v->cur_seg);
  1592. v->cur_seg = get_current_fragment(v);
  1593. if (!v->cur_seg) {
  1594. ret = AVERROR_EOF;
  1595. goto end;
  1596. }
  1597. /* load/update Media Initialization Section, if any */
  1598. ret = update_init_section(v);
  1599. if (ret)
  1600. goto end;
  1601. ret = open_input(c, v, v->cur_seg);
  1602. if (ret < 0) {
  1603. if (ff_check_interrupt(c->interrupt_callback)) {
  1604. ret = AVERROR_EXIT;
  1605. goto end;
  1606. }
  1607. av_log(v->parent, AV_LOG_WARNING, "Failed to open fragment of playlist\n");
  1608. v->cur_seq_no++;
  1609. goto restart;
  1610. }
  1611. }
  1612. if (v->init_sec_buf_read_offset < v->init_sec_data_len) {
  1613. /* Push init section out first before first actual fragment */
  1614. int copy_size = FFMIN(v->init_sec_data_len - v->init_sec_buf_read_offset, buf_size);
  1615. memcpy(buf, v->init_sec_buf, copy_size);
  1616. v->init_sec_buf_read_offset += copy_size;
  1617. ret = copy_size;
  1618. goto end;
  1619. }
  1620. /* check the v->cur_seg, if it is null, get current and double check if the new v->cur_seg*/
  1621. if (!v->cur_seg) {
  1622. v->cur_seg = get_current_fragment(v);
  1623. }
  1624. if (!v->cur_seg) {
  1625. ret = AVERROR_EOF;
  1626. goto end;
  1627. }
  1628. ret = read_from_url(v, v->cur_seg, buf, buf_size);
  1629. if (ret > 0)
  1630. goto end;
  1631. if (c->is_live || v->cur_seq_no < v->last_seq_no) {
  1632. if (!v->is_restart_needed)
  1633. v->cur_seq_no++;
  1634. v->is_restart_needed = 1;
  1635. }
  1636. end:
  1637. return ret;
  1638. }
  1639. static int save_avio_options(AVFormatContext *s)
  1640. {
  1641. DASHContext *c = s->priv_data;
  1642. const char *opts[] = {
  1643. "headers", "user_agent", "cookies", "http_proxy", "referer", "rw_timeout", "icy", NULL };
  1644. const char **opt = opts;
  1645. uint8_t *buf = NULL;
  1646. int ret = 0;
  1647. while (*opt) {
  1648. if (av_opt_get(s->pb, *opt, AV_OPT_SEARCH_CHILDREN, &buf) >= 0) {
  1649. if (buf[0] != '\0') {
  1650. ret = av_dict_set(&c->avio_opts, *opt, buf, AV_DICT_DONT_STRDUP_VAL);
  1651. if (ret < 0)
  1652. return ret;
  1653. } else {
  1654. av_freep(&buf);
  1655. }
  1656. }
  1657. opt++;
  1658. }
  1659. return ret;
  1660. }
  1661. static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
  1662. int flags, AVDictionary **opts)
  1663. {
  1664. av_log(s, AV_LOG_ERROR,
  1665. "A DASH playlist item '%s' referred to an external file '%s'. "
  1666. "Opening this file was forbidden for security reasons\n",
  1667. s->url, url);
  1668. return AVERROR(EPERM);
  1669. }
  1670. static void close_demux_for_component(struct representation *pls)
  1671. {
  1672. /* note: the internal buffer could have changed */
  1673. av_freep(&pls->pb.buffer);
  1674. memset(&pls->pb, 0x00, sizeof(AVIOContext));
  1675. pls->ctx->pb = NULL;
  1676. avformat_close_input(&pls->ctx);
  1677. }
  1678. static int reopen_demux_for_component(AVFormatContext *s, struct representation *pls)
  1679. {
  1680. DASHContext *c = s->priv_data;
  1681. ff_const59 AVInputFormat *in_fmt = NULL;
  1682. AVDictionary *in_fmt_opts = NULL;
  1683. uint8_t *avio_ctx_buffer = NULL;
  1684. int ret = 0, i;
  1685. if (pls->ctx) {
  1686. close_demux_for_component(pls);
  1687. }
  1688. if (ff_check_interrupt(&s->interrupt_callback)) {
  1689. ret = AVERROR_EXIT;
  1690. goto fail;
  1691. }
  1692. if (!(pls->ctx = avformat_alloc_context())) {
  1693. ret = AVERROR(ENOMEM);
  1694. goto fail;
  1695. }
  1696. avio_ctx_buffer = av_malloc(INITIAL_BUFFER_SIZE);
  1697. if (!avio_ctx_buffer ) {
  1698. ret = AVERROR(ENOMEM);
  1699. avformat_free_context(pls->ctx);
  1700. pls->ctx = NULL;
  1701. goto fail;
  1702. }
  1703. if (c->is_live) {
  1704. ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, NULL);
  1705. } else {
  1706. ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, seek_data);
  1707. }
  1708. pls->pb.seekable = 0;
  1709. if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0)
  1710. goto fail;
  1711. pls->ctx->flags = AVFMT_FLAG_CUSTOM_IO;
  1712. pls->ctx->probesize = s->probesize > 0 ? s->probesize : 1024 * 4;
  1713. pls->ctx->max_analyze_duration = s->max_analyze_duration > 0 ? s->max_analyze_duration : 4 * AV_TIME_BASE;
  1714. ret = av_probe_input_buffer(&pls->pb, &in_fmt, "", NULL, 0, 0);
  1715. if (ret < 0) {
  1716. av_log(s, AV_LOG_ERROR, "Error when loading first fragment of playlist\n");
  1717. avformat_free_context(pls->ctx);
  1718. pls->ctx = NULL;
  1719. goto fail;
  1720. }
  1721. pls->ctx->pb = &pls->pb;
  1722. pls->ctx->io_open = nested_io_open;
  1723. // provide additional information from mpd if available
  1724. ret = avformat_open_input(&pls->ctx, "", in_fmt, &in_fmt_opts); //pls->init_section->url
  1725. av_dict_free(&in_fmt_opts);
  1726. if (ret < 0)
  1727. goto fail;
  1728. if (pls->n_fragments) {
  1729. #if FF_API_R_FRAME_RATE
  1730. if (pls->framerate.den) {
  1731. for (i = 0; i < pls->ctx->nb_streams; i++)
  1732. pls->ctx->streams[i]->r_frame_rate = pls->framerate;
  1733. }
  1734. #endif
  1735. ret = avformat_find_stream_info(pls->ctx, NULL);
  1736. if (ret < 0)
  1737. goto fail;
  1738. }
  1739. fail:
  1740. return ret;
  1741. }
  1742. static int open_demux_for_component(AVFormatContext *s, struct representation *pls)
  1743. {
  1744. int ret = 0;
  1745. int i;
  1746. pls->parent = s;
  1747. pls->cur_seq_no = calc_cur_seg_no(s, pls);
  1748. if (!pls->last_seq_no) {
  1749. pls->last_seq_no = calc_max_seg_no(pls, s->priv_data);
  1750. }
  1751. ret = reopen_demux_for_component(s, pls);
  1752. if (ret < 0) {
  1753. goto fail;
  1754. }
  1755. for (i = 0; i < pls->ctx->nb_streams; i++) {
  1756. AVStream *st = avformat_new_stream(s, NULL);
  1757. AVStream *ist = pls->ctx->streams[i];
  1758. if (!st) {
  1759. ret = AVERROR(ENOMEM);
  1760. goto fail;
  1761. }
  1762. st->id = i;
  1763. avcodec_parameters_copy(st->codecpar, ist->codecpar);
  1764. avpriv_set_pts_info(st, ist->pts_wrap_bits, ist->time_base.num, ist->time_base.den);
  1765. }
  1766. return 0;
  1767. fail:
  1768. return ret;
  1769. }
  1770. static int is_common_init_section_exist(struct representation **pls, int n_pls)
  1771. {
  1772. struct fragment *first_init_section = pls[0]->init_section;
  1773. char *url =NULL;
  1774. int64_t url_offset = -1;
  1775. int64_t size = -1;
  1776. int i = 0;
  1777. if (first_init_section == NULL || n_pls == 0)
  1778. return 0;
  1779. url = first_init_section->url;
  1780. url_offset = first_init_section->url_offset;
  1781. size = pls[0]->init_section->size;
  1782. for (i=0;i<n_pls;i++) {
  1783. if (av_strcasecmp(pls[i]->init_section->url,url) || pls[i]->init_section->url_offset != url_offset || pls[i]->init_section->size != size) {
  1784. return 0;
  1785. }
  1786. }
  1787. return 1;
  1788. }
  1789. static int copy_init_section(struct representation *rep_dest, struct representation *rep_src)
  1790. {
  1791. rep_dest->init_sec_buf = av_mallocz(rep_src->init_sec_buf_size);
  1792. if (!rep_dest->init_sec_buf) {
  1793. av_log(rep_dest->ctx, AV_LOG_WARNING, "Cannot alloc memory for init_sec_buf\n");
  1794. return AVERROR(ENOMEM);
  1795. }
  1796. memcpy(rep_dest->init_sec_buf, rep_src->init_sec_buf, rep_src->init_sec_data_len);
  1797. rep_dest->init_sec_buf_size = rep_src->init_sec_buf_size;
  1798. rep_dest->init_sec_data_len = rep_src->init_sec_data_len;
  1799. rep_dest->cur_timestamp = rep_src->cur_timestamp;
  1800. return 0;
  1801. }
  1802. static int dash_close(AVFormatContext *s);
  1803. static int dash_read_header(AVFormatContext *s)
  1804. {
  1805. DASHContext *c = s->priv_data;
  1806. struct representation *rep;
  1807. int ret = 0;
  1808. int stream_index = 0;
  1809. int i;
  1810. c->interrupt_callback = &s->interrupt_callback;
  1811. if ((ret = save_avio_options(s)) < 0)
  1812. goto fail;
  1813. if ((ret = parse_manifest(s, s->url, s->pb)) < 0)
  1814. goto fail;
  1815. /* If this isn't a live stream, fill the total duration of the
  1816. * stream. */
  1817. if (!c->is_live) {
  1818. s->duration = (int64_t) c->media_presentation_duration * AV_TIME_BASE;
  1819. } else {
  1820. av_dict_set(&c->avio_opts, "seekable", "0", 0);
  1821. }
  1822. if(c->n_videos)
  1823. c->is_init_section_common_video = is_common_init_section_exist(c->videos, c->n_videos);
  1824. /* Open the demuxer for video and audio components if available */
  1825. for (i = 0; i < c->n_videos; i++) {
  1826. rep = c->videos[i];
  1827. if (i > 0 && c->is_init_section_common_video) {
  1828. ret = copy_init_section(rep, c->videos[0]);
  1829. if (ret < 0)
  1830. goto fail;
  1831. }
  1832. ret = open_demux_for_component(s, rep);
  1833. if (ret)
  1834. goto fail;
  1835. rep->stream_index = stream_index;
  1836. ++stream_index;
  1837. }
  1838. if(c->n_audios)
  1839. c->is_init_section_common_audio = is_common_init_section_exist(c->audios, c->n_audios);
  1840. for (i = 0; i < c->n_audios; i++) {
  1841. rep = c->audios[i];
  1842. if (i > 0 && c->is_init_section_common_audio) {
  1843. ret = copy_init_section(rep, c->audios[0]);
  1844. if (ret < 0)
  1845. goto fail;
  1846. }
  1847. ret = open_demux_for_component(s, rep);
  1848. if (ret)
  1849. goto fail;
  1850. rep->stream_index = stream_index;
  1851. ++stream_index;
  1852. }
  1853. if (c->n_subtitles)
  1854. c->is_init_section_common_audio = is_common_init_section_exist(c->subtitles, c->n_subtitles);
  1855. for (i = 0; i < c->n_subtitles; i++) {
  1856. rep = c->subtitles[i];
  1857. if (i > 0 && c->is_init_section_common_audio) {
  1858. ret = copy_init_section(rep, c->subtitles[0]);
  1859. if (ret < 0)
  1860. goto fail;
  1861. }
  1862. ret = open_demux_for_component(s, rep);
  1863. if (ret)
  1864. goto fail;
  1865. rep->stream_index = stream_index;
  1866. ++stream_index;
  1867. }
  1868. if (!stream_index) {
  1869. ret = AVERROR_INVALIDDATA;
  1870. goto fail;
  1871. }
  1872. /* Create a program */
  1873. if (!ret) {
  1874. AVProgram *program;
  1875. program = av_new_program(s, 0);
  1876. if (!program) {
  1877. ret = AVERROR(ENOMEM);
  1878. goto fail;
  1879. }
  1880. for (i = 0; i < c->n_videos; i++) {
  1881. rep = c->videos[i];
  1882. av_program_add_stream_index(s, 0, rep->stream_index);
  1883. rep->assoc_stream = s->streams[rep->stream_index];
  1884. if (rep->bandwidth > 0)
  1885. av_dict_set_int(&rep->assoc_stream->metadata, "variant_bitrate", rep->bandwidth, 0);
  1886. if (rep->id[0])
  1887. av_dict_set(&rep->assoc_stream->metadata, "id", rep->id, 0);
  1888. }
  1889. for (i = 0; i < c->n_audios; i++) {
  1890. rep = c->audios[i];
  1891. av_program_add_stream_index(s, 0, rep->stream_index);
  1892. rep->assoc_stream = s->streams[rep->stream_index];
  1893. if (rep->bandwidth > 0)
  1894. av_dict_set_int(&rep->assoc_stream->metadata, "variant_bitrate", rep->bandwidth, 0);
  1895. if (rep->id[0])
  1896. av_dict_set(&rep->assoc_stream->metadata, "id", rep->id, 0);
  1897. if (rep->lang) {
  1898. av_dict_set(&rep->assoc_stream->metadata, "language", rep->lang, 0);
  1899. av_freep(&rep->lang);
  1900. }
  1901. }
  1902. for (i = 0; i < c->n_subtitles; i++) {
  1903. rep = c->subtitles[i];
  1904. av_program_add_stream_index(s, 0, rep->stream_index);
  1905. rep->assoc_stream = s->streams[rep->stream_index];
  1906. if (rep->id[0])
  1907. av_dict_set(&rep->assoc_stream->metadata, "id", rep->id, 0);
  1908. if (rep->lang) {
  1909. av_dict_set(&rep->assoc_stream->metadata, "language", rep->lang, 0);
  1910. av_freep(&rep->lang);
  1911. }
  1912. }
  1913. }
  1914. return 0;
  1915. fail:
  1916. dash_close(s);
  1917. return ret;
  1918. }
  1919. static void recheck_discard_flags(AVFormatContext *s, struct representation **p, int n)
  1920. {
  1921. int i, j;
  1922. for (i = 0; i < n; i++) {
  1923. struct representation *pls = p[i];
  1924. int needed = !pls->assoc_stream || pls->assoc_stream->discard < AVDISCARD_ALL;
  1925. if (needed && !pls->ctx) {
  1926. pls->cur_seg_offset = 0;
  1927. pls->init_sec_buf_read_offset = 0;
  1928. /* Catch up */
  1929. for (j = 0; j < n; j++) {
  1930. pls->cur_seq_no = FFMAX(pls->cur_seq_no, p[j]->cur_seq_no);
  1931. }
  1932. reopen_demux_for_component(s, pls);
  1933. av_log(s, AV_LOG_INFO, "Now receiving stream_index %d\n", pls->stream_index);
  1934. } else if (!needed && pls->ctx) {
  1935. close_demux_for_component(pls);
  1936. ff_format_io_close(pls->parent, &pls->input);
  1937. av_log(s, AV_LOG_INFO, "No longer receiving stream_index %d\n", pls->stream_index);
  1938. }
  1939. }
  1940. }
  1941. static int dash_read_packet(AVFormatContext *s, AVPacket *pkt)
  1942. {
  1943. DASHContext *c = s->priv_data;
  1944. int ret = 0, i;
  1945. int64_t mints = 0;
  1946. struct representation *cur = NULL;
  1947. struct representation *rep = NULL;
  1948. recheck_discard_flags(s, c->videos, c->n_videos);
  1949. recheck_discard_flags(s, c->audios, c->n_audios);
  1950. recheck_discard_flags(s, c->subtitles, c->n_subtitles);
  1951. for (i = 0; i < c->n_videos; i++) {
  1952. rep = c->videos[i];
  1953. if (!rep->ctx)
  1954. continue;
  1955. if (!cur || rep->cur_timestamp < mints) {
  1956. cur = rep;
  1957. mints = rep->cur_timestamp;
  1958. }
  1959. }
  1960. for (i = 0; i < c->n_audios; i++) {
  1961. rep = c->audios[i];
  1962. if (!rep->ctx)
  1963. continue;
  1964. if (!cur || rep->cur_timestamp < mints) {
  1965. cur = rep;
  1966. mints = rep->cur_timestamp;
  1967. }
  1968. }
  1969. for (i = 0; i < c->n_subtitles; i++) {
  1970. rep = c->subtitles[i];
  1971. if (!rep->ctx)
  1972. continue;
  1973. if (!cur || rep->cur_timestamp < mints) {
  1974. cur = rep;
  1975. mints = rep->cur_timestamp;
  1976. }
  1977. }
  1978. if (!cur) {
  1979. return AVERROR_INVALIDDATA;
  1980. }
  1981. while (!ff_check_interrupt(c->interrupt_callback) && !ret) {
  1982. ret = av_read_frame(cur->ctx, pkt);
  1983. if (ret >= 0) {
  1984. /* If we got a packet, return it */
  1985. cur->cur_timestamp = av_rescale(pkt->pts, (int64_t)cur->ctx->streams[0]->time_base.num * 90000, cur->ctx->streams[0]->time_base.den);
  1986. pkt->stream_index = cur->stream_index;
  1987. return 0;
  1988. }
  1989. if (cur->is_restart_needed) {
  1990. cur->cur_seg_offset = 0;
  1991. cur->init_sec_buf_read_offset = 0;
  1992. ff_format_io_close(cur->parent, &cur->input);
  1993. ret = reopen_demux_for_component(s, cur);
  1994. cur->is_restart_needed = 0;
  1995. }
  1996. }
  1997. return AVERROR_EOF;
  1998. }
  1999. static int dash_close(AVFormatContext *s)
  2000. {
  2001. DASHContext *c = s->priv_data;
  2002. free_audio_list(c);
  2003. free_video_list(c);
  2004. free_subtitle_list(c);
  2005. av_dict_free(&c->avio_opts);
  2006. av_freep(&c->base_url);
  2007. return 0;
  2008. }
  2009. static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos_msec, int flags, int dry_run)
  2010. {
  2011. int ret = 0;
  2012. int i = 0;
  2013. int j = 0;
  2014. int64_t duration = 0;
  2015. av_log(pls->parent, AV_LOG_VERBOSE, "DASH seek pos[%"PRId64"ms] %s\n",
  2016. seek_pos_msec, dry_run ? " (dry)" : "");
  2017. // single fragment mode
  2018. if (pls->n_fragments == 1) {
  2019. pls->cur_timestamp = 0;
  2020. pls->cur_seg_offset = 0;
  2021. if (dry_run)
  2022. return 0;
  2023. ff_read_frame_flush(pls->ctx);
  2024. return av_seek_frame(pls->ctx, -1, seek_pos_msec * 1000, flags);
  2025. }
  2026. ff_format_io_close(pls->parent, &pls->input);
  2027. // find the nearest fragment
  2028. if (pls->n_timelines > 0 && pls->fragment_timescale > 0) {
  2029. int64_t num = pls->first_seq_no;
  2030. av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline start n_timelines[%d] "
  2031. "last_seq_no[%"PRId64"].\n",
  2032. (int)pls->n_timelines, (int64_t)pls->last_seq_no);
  2033. for (i = 0; i < pls->n_timelines; i++) {
  2034. if (pls->timelines[i]->starttime > 0) {
  2035. duration = pls->timelines[i]->starttime;
  2036. }
  2037. duration += pls->timelines[i]->duration;
  2038. if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
  2039. goto set_seq_num;
  2040. }
  2041. for (j = 0; j < pls->timelines[i]->repeat; j++) {
  2042. duration += pls->timelines[i]->duration;
  2043. num++;
  2044. if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
  2045. goto set_seq_num;
  2046. }
  2047. }
  2048. num++;
  2049. }
  2050. set_seq_num:
  2051. pls->cur_seq_no = num > pls->last_seq_no ? pls->last_seq_no : num;
  2052. av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline end cur_seq_no[%"PRId64"].\n",
  2053. (int64_t)pls->cur_seq_no);
  2054. } else if (pls->fragment_duration > 0) {
  2055. pls->cur_seq_no = pls->first_seq_no + ((seek_pos_msec * pls->fragment_timescale) / pls->fragment_duration) / 1000;
  2056. } else {
  2057. av_log(pls->parent, AV_LOG_ERROR, "dash_seek missing timeline or fragment_duration\n");
  2058. pls->cur_seq_no = pls->first_seq_no;
  2059. }
  2060. pls->cur_timestamp = 0;
  2061. pls->cur_seg_offset = 0;
  2062. pls->init_sec_buf_read_offset = 0;
  2063. ret = dry_run ? 0 : reopen_demux_for_component(s, pls);
  2064. return ret;
  2065. }
  2066. static int dash_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  2067. {
  2068. int ret = 0, i;
  2069. DASHContext *c = s->priv_data;
  2070. int64_t seek_pos_msec = av_rescale_rnd(timestamp, 1000,
  2071. s->streams[stream_index]->time_base.den,
  2072. flags & AVSEEK_FLAG_BACKWARD ?
  2073. AV_ROUND_DOWN : AV_ROUND_UP);
  2074. if ((flags & AVSEEK_FLAG_BYTE) || c->is_live)
  2075. return AVERROR(ENOSYS);
  2076. /* Seek in discarded streams with dry_run=1 to avoid reopening them */
  2077. for (i = 0; i < c->n_videos; i++) {
  2078. if (!ret)
  2079. ret = dash_seek(s, c->videos[i], seek_pos_msec, flags, !c->videos[i]->ctx);
  2080. }
  2081. for (i = 0; i < c->n_audios; i++) {
  2082. if (!ret)
  2083. ret = dash_seek(s, c->audios[i], seek_pos_msec, flags, !c->audios[i]->ctx);
  2084. }
  2085. for (i = 0; i < c->n_subtitles; i++) {
  2086. if (!ret)
  2087. ret = dash_seek(s, c->subtitles[i], seek_pos_msec, flags, !c->subtitles[i]->ctx);
  2088. }
  2089. return ret;
  2090. }
  2091. static int dash_probe(const AVProbeData *p)
  2092. {
  2093. if (!av_stristr(p->buf, "<MPD"))
  2094. return 0;
  2095. if (av_stristr(p->buf, "dash:profile:isoff-on-demand:2011") ||
  2096. av_stristr(p->buf, "dash:profile:isoff-live:2011") ||
  2097. av_stristr(p->buf, "dash:profile:isoff-live:2012") ||
  2098. av_stristr(p->buf, "dash:profile:isoff-main:2011") ||
  2099. av_stristr(p->buf, "3GPP:PSS:profile:DASH1")) {
  2100. return AVPROBE_SCORE_MAX;
  2101. }
  2102. if (av_stristr(p->buf, "dash:profile")) {
  2103. return AVPROBE_SCORE_MAX;
  2104. }
  2105. return 0;
  2106. }
  2107. #define OFFSET(x) offsetof(DASHContext, x)
  2108. #define FLAGS AV_OPT_FLAG_DECODING_PARAM
  2109. static const AVOption dash_options[] = {
  2110. {"allowed_extensions", "List of file extensions that dash is allowed to access",
  2111. OFFSET(allowed_extensions), AV_OPT_TYPE_STRING,
  2112. {.str = "aac,m4a,m4s,m4v,mov,mp4,webm,ts"},
  2113. INT_MIN, INT_MAX, FLAGS},
  2114. {NULL}
  2115. };
  2116. static const AVClass dash_class = {
  2117. .class_name = "dash",
  2118. .item_name = av_default_item_name,
  2119. .option = dash_options,
  2120. .version = LIBAVUTIL_VERSION_INT,
  2121. };
  2122. AVInputFormat ff_dash_demuxer = {
  2123. .name = "dash",
  2124. .long_name = NULL_IF_CONFIG_SMALL("Dynamic Adaptive Streaming over HTTP"),
  2125. .priv_class = &dash_class,
  2126. .priv_data_size = sizeof(DASHContext),
  2127. .read_probe = dash_probe,
  2128. .read_header = dash_read_header,
  2129. .read_packet = dash_read_packet,
  2130. .read_close = dash_close,
  2131. .read_seek = dash_read_seek,
  2132. .flags = AVFMT_NO_BYTE_SEEK,
  2133. };