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.

2406 lines
83KB

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