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.

2434 lines
84KB

  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_BPRINT_READ_SIZE (UINT_MAX - 1)
  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. rep->init_section = get_Fragment(range_val);
  536. if (!rep->init_section) {
  537. xmlFree(initialization_val);
  538. xmlFree(range_val);
  539. return AVERROR(ENOMEM);
  540. }
  541. rep->init_section->url = get_content_url(baseurl_nodes, 4,
  542. max_url_size,
  543. rep_id_val,
  544. rep_bandwidth_val,
  545. initialization_val);
  546. if (!rep->init_section->url) {
  547. av_free(rep->init_section);
  548. xmlFree(initialization_val);
  549. xmlFree(range_val);
  550. return AVERROR(ENOMEM);
  551. }
  552. xmlFree(initialization_val);
  553. xmlFree(range_val);
  554. }
  555. } else if (!av_strcasecmp(fragmenturl_node->name, (const char *)"SegmentURL")) {
  556. media_val = xmlGetProp(fragmenturl_node, "media");
  557. range_val = xmlGetProp(fragmenturl_node, "mediaRange");
  558. if (media_val || range_val) {
  559. struct fragment *seg = get_Fragment(range_val);
  560. if (!seg) {
  561. xmlFree(media_val);
  562. xmlFree(range_val);
  563. return AVERROR(ENOMEM);
  564. }
  565. seg->url = get_content_url(baseurl_nodes, 4,
  566. max_url_size,
  567. rep_id_val,
  568. rep_bandwidth_val,
  569. media_val);
  570. if (!seg->url) {
  571. av_free(seg);
  572. xmlFree(media_val);
  573. xmlFree(range_val);
  574. return AVERROR(ENOMEM);
  575. }
  576. dynarray_add(&rep->fragments, &rep->n_fragments, seg);
  577. xmlFree(media_val);
  578. xmlFree(range_val);
  579. }
  580. }
  581. return 0;
  582. }
  583. static int parse_manifest_segmenttimeline(AVFormatContext *s, struct representation *rep,
  584. xmlNodePtr fragment_timeline_node)
  585. {
  586. xmlAttrPtr attr = NULL;
  587. char *val = NULL;
  588. if (!av_strcasecmp(fragment_timeline_node->name, (const char *)"S")) {
  589. struct timeline *tml = av_mallocz(sizeof(struct timeline));
  590. if (!tml) {
  591. return AVERROR(ENOMEM);
  592. }
  593. attr = fragment_timeline_node->properties;
  594. while (attr) {
  595. val = xmlGetProp(fragment_timeline_node, attr->name);
  596. if (!val) {
  597. av_log(s, AV_LOG_WARNING, "parse_manifest_segmenttimeline attr->name = %s val is NULL\n", attr->name);
  598. continue;
  599. }
  600. if (!av_strcasecmp(attr->name, (const char *)"t")) {
  601. tml->starttime = (int64_t)strtoll(val, NULL, 10);
  602. } else if (!av_strcasecmp(attr->name, (const char *)"r")) {
  603. tml->repeat =(int64_t) strtoll(val, NULL, 10);
  604. } else if (!av_strcasecmp(attr->name, (const char *)"d")) {
  605. tml->duration = (int64_t)strtoll(val, NULL, 10);
  606. }
  607. attr = attr->next;
  608. xmlFree(val);
  609. }
  610. dynarray_add(&rep->timelines, &rep->n_timelines, tml);
  611. }
  612. return 0;
  613. }
  614. static int resolve_content_path(AVFormatContext *s, const char *url, int *max_url_size, xmlNodePtr *baseurl_nodes, int n_baseurl_nodes)
  615. {
  616. char *tmp_str = NULL;
  617. char *path = NULL;
  618. char *mpdName = NULL;
  619. xmlNodePtr node = NULL;
  620. char *baseurl = NULL;
  621. char *root_url = NULL;
  622. char *text = NULL;
  623. char *tmp = NULL;
  624. int isRootHttp = 0;
  625. char token ='/';
  626. int start = 0;
  627. int rootId = 0;
  628. int updated = 0;
  629. int size = 0;
  630. int i;
  631. int tmp_max_url_size = strlen(url);
  632. for (i = n_baseurl_nodes-1; i >= 0 ; i--) {
  633. text = xmlNodeGetContent(baseurl_nodes[i]);
  634. if (!text)
  635. continue;
  636. tmp_max_url_size += strlen(text);
  637. if (ishttp(text)) {
  638. xmlFree(text);
  639. break;
  640. }
  641. xmlFree(text);
  642. }
  643. tmp_max_url_size = aligned(tmp_max_url_size);
  644. text = av_mallocz(tmp_max_url_size);
  645. if (!text) {
  646. updated = AVERROR(ENOMEM);
  647. goto end;
  648. }
  649. av_strlcpy(text, url, strlen(url)+1);
  650. tmp = text;
  651. while (mpdName = av_strtok(tmp, "/", &tmp)) {
  652. size = strlen(mpdName);
  653. }
  654. av_free(text);
  655. path = av_mallocz(tmp_max_url_size);
  656. tmp_str = av_mallocz(tmp_max_url_size);
  657. if (!tmp_str || !path) {
  658. updated = AVERROR(ENOMEM);
  659. goto end;
  660. }
  661. av_strlcpy (path, url, strlen(url) - size + 1);
  662. for (rootId = n_baseurl_nodes - 1; rootId > 0; rootId --) {
  663. if (!(node = baseurl_nodes[rootId])) {
  664. continue;
  665. }
  666. text = xmlNodeGetContent(node);
  667. if (ishttp(text)) {
  668. xmlFree(text);
  669. break;
  670. }
  671. xmlFree(text);
  672. }
  673. node = baseurl_nodes[rootId];
  674. baseurl = xmlNodeGetContent(node);
  675. root_url = (av_strcasecmp(baseurl, "")) ? baseurl : path;
  676. if (node) {
  677. xmlNodeSetContent(node, root_url);
  678. updated = 1;
  679. }
  680. size = strlen(root_url);
  681. isRootHttp = ishttp(root_url);
  682. if (root_url[size - 1] != token) {
  683. av_strlcat(root_url, "/", size + 2);
  684. size += 2;
  685. }
  686. for (i = 0; i < n_baseurl_nodes; ++i) {
  687. if (i == rootId) {
  688. continue;
  689. }
  690. text = xmlNodeGetContent(baseurl_nodes[i]);
  691. if (text && !av_strstart(text, "/", NULL)) {
  692. memset(tmp_str, 0, strlen(tmp_str));
  693. if (!ishttp(text) && isRootHttp) {
  694. av_strlcpy(tmp_str, root_url, size + 1);
  695. }
  696. start = (text[0] == token);
  697. if (start && av_stristr(tmp_str, text)) {
  698. char *p = tmp_str;
  699. if (!av_strncasecmp(tmp_str, "http://", 7)) {
  700. p += 7;
  701. } else if (!av_strncasecmp(tmp_str, "https://", 8)) {
  702. p += 8;
  703. }
  704. p = strchr(p, '/');
  705. memset(p + 1, 0, strlen(p));
  706. }
  707. av_strlcat(tmp_str, text + start, tmp_max_url_size);
  708. xmlNodeSetContent(baseurl_nodes[i], tmp_str);
  709. updated = 1;
  710. xmlFree(text);
  711. }
  712. }
  713. end:
  714. if (tmp_max_url_size > *max_url_size) {
  715. *max_url_size = tmp_max_url_size;
  716. }
  717. av_free(path);
  718. av_free(tmp_str);
  719. xmlFree(baseurl);
  720. return updated;
  721. }
  722. static int parse_manifest_representation(AVFormatContext *s, const char *url,
  723. xmlNodePtr node,
  724. xmlNodePtr adaptionset_node,
  725. xmlNodePtr mpd_baseurl_node,
  726. xmlNodePtr period_baseurl_node,
  727. xmlNodePtr period_segmenttemplate_node,
  728. xmlNodePtr period_segmentlist_node,
  729. xmlNodePtr fragment_template_node,
  730. xmlNodePtr content_component_node,
  731. xmlNodePtr adaptionset_baseurl_node,
  732. xmlNodePtr adaptionset_segmentlist_node,
  733. xmlNodePtr adaptionset_supplementalproperty_node)
  734. {
  735. int32_t ret = 0;
  736. int32_t subtitle_rep_idx = 0;
  737. int32_t audio_rep_idx = 0;
  738. int32_t video_rep_idx = 0;
  739. DASHContext *c = s->priv_data;
  740. struct representation *rep = NULL;
  741. struct fragment *seg = NULL;
  742. xmlNodePtr representation_segmenttemplate_node = NULL;
  743. xmlNodePtr representation_baseurl_node = NULL;
  744. xmlNodePtr representation_segmentlist_node = NULL;
  745. xmlNodePtr segmentlists_tab[3];
  746. xmlNodePtr fragment_timeline_node = NULL;
  747. xmlNodePtr fragment_templates_tab[5];
  748. char *duration_val = NULL;
  749. char *presentation_timeoffset_val = NULL;
  750. char *startnumber_val = NULL;
  751. char *timescale_val = NULL;
  752. char *initialization_val = NULL;
  753. char *media_val = NULL;
  754. char *val = NULL;
  755. xmlNodePtr baseurl_nodes[4];
  756. xmlNodePtr representation_node = node;
  757. char *rep_id_val = xmlGetProp(representation_node, "id");
  758. char *rep_bandwidth_val = xmlGetProp(representation_node, "bandwidth");
  759. char *rep_framerate_val = xmlGetProp(representation_node, "frameRate");
  760. enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
  761. // try get information from representation
  762. if (type == AVMEDIA_TYPE_UNKNOWN)
  763. type = get_content_type(representation_node);
  764. // try get information from contentComponen
  765. if (type == AVMEDIA_TYPE_UNKNOWN)
  766. type = get_content_type(content_component_node);
  767. // try get information from adaption set
  768. if (type == AVMEDIA_TYPE_UNKNOWN)
  769. type = get_content_type(adaptionset_node);
  770. if (type == AVMEDIA_TYPE_UNKNOWN) {
  771. av_log(s, AV_LOG_VERBOSE, "Parsing '%s' - skipp not supported representation type\n", url);
  772. } else if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO || type == AVMEDIA_TYPE_SUBTITLE) {
  773. // convert selected representation to our internal struct
  774. rep = av_mallocz(sizeof(struct representation));
  775. if (!rep) {
  776. ret = AVERROR(ENOMEM);
  777. goto end;
  778. }
  779. if (c->adaptionset_lang) {
  780. rep->lang = av_strdup(c->adaptionset_lang);
  781. if (!rep->lang) {
  782. av_log(s, AV_LOG_ERROR, "alloc language memory failure\n");
  783. av_freep(&rep);
  784. ret = AVERROR(ENOMEM);
  785. goto end;
  786. }
  787. }
  788. rep->parent = s;
  789. representation_segmenttemplate_node = find_child_node_by_name(representation_node, "SegmentTemplate");
  790. representation_baseurl_node = find_child_node_by_name(representation_node, "BaseURL");
  791. representation_segmentlist_node = find_child_node_by_name(representation_node, "SegmentList");
  792. baseurl_nodes[0] = mpd_baseurl_node;
  793. baseurl_nodes[1] = period_baseurl_node;
  794. baseurl_nodes[2] = adaptionset_baseurl_node;
  795. baseurl_nodes[3] = representation_baseurl_node;
  796. ret = resolve_content_path(s, url, &c->max_url_size, baseurl_nodes, 4);
  797. c->max_url_size = aligned(c->max_url_size
  798. + (rep_id_val ? strlen(rep_id_val) : 0)
  799. + (rep_bandwidth_val ? strlen(rep_bandwidth_val) : 0));
  800. if (ret == AVERROR(ENOMEM) || ret == 0) {
  801. goto end;
  802. }
  803. if (representation_segmenttemplate_node || fragment_template_node || period_segmenttemplate_node) {
  804. fragment_timeline_node = NULL;
  805. fragment_templates_tab[0] = representation_segmenttemplate_node;
  806. fragment_templates_tab[1] = adaptionset_segmentlist_node;
  807. fragment_templates_tab[2] = fragment_template_node;
  808. fragment_templates_tab[3] = period_segmenttemplate_node;
  809. fragment_templates_tab[4] = period_segmentlist_node;
  810. presentation_timeoffset_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "presentationTimeOffset");
  811. duration_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "duration");
  812. startnumber_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "startNumber");
  813. timescale_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "timescale");
  814. initialization_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "initialization");
  815. media_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "media");
  816. if (initialization_val) {
  817. rep->init_section = av_mallocz(sizeof(struct fragment));
  818. if (!rep->init_section) {
  819. av_free(rep);
  820. ret = AVERROR(ENOMEM);
  821. goto end;
  822. }
  823. c->max_url_size = aligned(c->max_url_size + strlen(initialization_val));
  824. rep->init_section->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, initialization_val);
  825. if (!rep->init_section->url) {
  826. av_free(rep->init_section);
  827. av_free(rep);
  828. ret = AVERROR(ENOMEM);
  829. goto end;
  830. }
  831. rep->init_section->size = -1;
  832. xmlFree(initialization_val);
  833. }
  834. if (media_val) {
  835. c->max_url_size = aligned(c->max_url_size + strlen(media_val));
  836. rep->url_template = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, media_val);
  837. xmlFree(media_val);
  838. }
  839. if (presentation_timeoffset_val) {
  840. rep->presentation_timeoffset = (int64_t) strtoll(presentation_timeoffset_val, NULL, 10);
  841. av_log(s, AV_LOG_TRACE, "rep->presentation_timeoffset = [%"PRId64"]\n", rep->presentation_timeoffset);
  842. xmlFree(presentation_timeoffset_val);
  843. }
  844. if (duration_val) {
  845. rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
  846. av_log(s, AV_LOG_TRACE, "rep->fragment_duration = [%"PRId64"]\n", rep->fragment_duration);
  847. xmlFree(duration_val);
  848. }
  849. if (timescale_val) {
  850. rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
  851. av_log(s, AV_LOG_TRACE, "rep->fragment_timescale = [%"PRId64"]\n", rep->fragment_timescale);
  852. xmlFree(timescale_val);
  853. }
  854. if (startnumber_val) {
  855. rep->start_number = rep->first_seq_no = (int64_t) strtoll(startnumber_val, NULL, 10);
  856. av_log(s, AV_LOG_TRACE, "rep->first_seq_no = [%"PRId64"]\n", rep->first_seq_no);
  857. xmlFree(startnumber_val);
  858. }
  859. if (adaptionset_supplementalproperty_node) {
  860. if (!av_strcasecmp(xmlGetProp(adaptionset_supplementalproperty_node,"schemeIdUri"), "http://dashif.org/guidelines/last-segment-number")) {
  861. val = xmlGetProp(adaptionset_supplementalproperty_node,"value");
  862. if (!val) {
  863. av_log(s, AV_LOG_ERROR, "Missing value attribute in adaptionset_supplementalproperty_node\n");
  864. } else {
  865. rep->last_seq_no =(int64_t) strtoll(val, NULL, 10) - 1;
  866. xmlFree(val);
  867. }
  868. }
  869. }
  870. fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
  871. if (!fragment_timeline_node)
  872. fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
  873. if (!fragment_timeline_node)
  874. fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
  875. if (!fragment_timeline_node)
  876. fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
  877. if (fragment_timeline_node) {
  878. fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
  879. while (fragment_timeline_node) {
  880. ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
  881. if (ret < 0) {
  882. return ret;
  883. }
  884. fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
  885. }
  886. }
  887. } else if (representation_baseurl_node && !representation_segmentlist_node) {
  888. seg = av_mallocz(sizeof(struct fragment));
  889. if (!seg) {
  890. ret = AVERROR(ENOMEM);
  891. goto end;
  892. }
  893. seg->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, NULL);
  894. if (!seg->url) {
  895. av_free(seg);
  896. ret = AVERROR(ENOMEM);
  897. goto end;
  898. }
  899. seg->size = -1;
  900. dynarray_add(&rep->fragments, &rep->n_fragments, seg);
  901. } else if (representation_segmentlist_node) {
  902. // TODO: https://www.brendanlong.com/the-structure-of-an-mpeg-dash-mpd.html
  903. // http://www-itec.uni-klu.ac.at/dash/ddash/mpdGenerator.php?fragmentlength=15&type=full
  904. xmlNodePtr fragmenturl_node = NULL;
  905. segmentlists_tab[0] = representation_segmentlist_node;
  906. segmentlists_tab[1] = adaptionset_segmentlist_node;
  907. segmentlists_tab[2] = period_segmentlist_node;
  908. duration_val = get_val_from_nodes_tab(segmentlists_tab, 3, "duration");
  909. timescale_val = get_val_from_nodes_tab(segmentlists_tab, 3, "timescale");
  910. startnumber_val = get_val_from_nodes_tab(segmentlists_tab, 3, "startNumber");
  911. if (duration_val) {
  912. rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
  913. av_log(s, AV_LOG_TRACE, "rep->fragment_duration = [%"PRId64"]\n", rep->fragment_duration);
  914. xmlFree(duration_val);
  915. }
  916. if (timescale_val) {
  917. rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
  918. av_log(s, AV_LOG_TRACE, "rep->fragment_timescale = [%"PRId64"]\n", rep->fragment_timescale);
  919. xmlFree(timescale_val);
  920. }
  921. if (startnumber_val) {
  922. rep->start_number = rep->first_seq_no = (int64_t) strtoll(startnumber_val, NULL, 10);
  923. av_log(s, AV_LOG_TRACE, "rep->first_seq_no = [%"PRId64"]\n", rep->first_seq_no);
  924. xmlFree(startnumber_val);
  925. }
  926. fragmenturl_node = xmlFirstElementChild(representation_segmentlist_node);
  927. while (fragmenturl_node) {
  928. ret = parse_manifest_segmenturlnode(s, rep, fragmenturl_node,
  929. baseurl_nodes,
  930. rep_id_val,
  931. rep_bandwidth_val);
  932. if (ret < 0) {
  933. return ret;
  934. }
  935. fragmenturl_node = xmlNextElementSibling(fragmenturl_node);
  936. }
  937. fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
  938. if (!fragment_timeline_node)
  939. fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
  940. if (!fragment_timeline_node)
  941. fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
  942. if (!fragment_timeline_node)
  943. fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
  944. if (fragment_timeline_node) {
  945. fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
  946. while (fragment_timeline_node) {
  947. ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
  948. if (ret < 0) {
  949. return ret;
  950. }
  951. fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
  952. }
  953. }
  954. } else {
  955. free_representation(rep);
  956. rep = NULL;
  957. av_log(s, AV_LOG_ERROR, "Unknown format of Representation node id[%s] \n", (const char *)rep_id_val);
  958. }
  959. if (rep) {
  960. if (rep->fragment_duration > 0 && !rep->fragment_timescale)
  961. rep->fragment_timescale = 1;
  962. rep->bandwidth = rep_bandwidth_val ? atoi(rep_bandwidth_val) : 0;
  963. strncpy(rep->id, rep_id_val ? rep_id_val : "", sizeof(rep->id));
  964. rep->framerate = av_make_q(0, 0);
  965. if (type == AVMEDIA_TYPE_VIDEO && rep_framerate_val) {
  966. ret = av_parse_video_rate(&rep->framerate, rep_framerate_val);
  967. if (ret < 0)
  968. av_log(s, AV_LOG_VERBOSE, "Ignoring invalid frame rate '%s'\n", rep_framerate_val);
  969. }
  970. switch (type) {
  971. case AVMEDIA_TYPE_VIDEO:
  972. rep->rep_idx = video_rep_idx;
  973. dynarray_add(&c->videos, &c->n_videos, rep);
  974. break;
  975. case AVMEDIA_TYPE_AUDIO:
  976. rep->rep_idx = audio_rep_idx;
  977. dynarray_add(&c->audios, &c->n_audios, rep);
  978. break;
  979. case AVMEDIA_TYPE_SUBTITLE:
  980. rep->rep_idx = subtitle_rep_idx;
  981. dynarray_add(&c->subtitles, &c->n_subtitles, rep);
  982. break;
  983. default:
  984. av_log(s, AV_LOG_WARNING, "Unsupported the stream type %d\n", type);
  985. break;
  986. }
  987. }
  988. }
  989. video_rep_idx += type == AVMEDIA_TYPE_VIDEO;
  990. audio_rep_idx += type == AVMEDIA_TYPE_AUDIO;
  991. subtitle_rep_idx += type == AVMEDIA_TYPE_SUBTITLE;
  992. end:
  993. if (rep_id_val)
  994. xmlFree(rep_id_val);
  995. if (rep_bandwidth_val)
  996. xmlFree(rep_bandwidth_val);
  997. if (rep_framerate_val)
  998. xmlFree(rep_framerate_val);
  999. return ret;
  1000. }
  1001. static int parse_manifest_adaptationset_attr(AVFormatContext *s, xmlNodePtr adaptionset_node)
  1002. {
  1003. DASHContext *c = s->priv_data;
  1004. if (!adaptionset_node) {
  1005. av_log(s, AV_LOG_WARNING, "Cannot get AdaptionSet\n");
  1006. return AVERROR(EINVAL);
  1007. }
  1008. c->adaptionset_lang = xmlGetProp(adaptionset_node, "lang");
  1009. return 0;
  1010. }
  1011. static int parse_manifest_adaptationset(AVFormatContext *s, const char *url,
  1012. xmlNodePtr adaptionset_node,
  1013. xmlNodePtr mpd_baseurl_node,
  1014. xmlNodePtr period_baseurl_node,
  1015. xmlNodePtr period_segmenttemplate_node,
  1016. xmlNodePtr period_segmentlist_node)
  1017. {
  1018. int ret = 0;
  1019. DASHContext *c = s->priv_data;
  1020. xmlNodePtr fragment_template_node = NULL;
  1021. xmlNodePtr content_component_node = NULL;
  1022. xmlNodePtr adaptionset_baseurl_node = NULL;
  1023. xmlNodePtr adaptionset_segmentlist_node = NULL;
  1024. xmlNodePtr adaptionset_supplementalproperty_node = NULL;
  1025. xmlNodePtr node = NULL;
  1026. ret = parse_manifest_adaptationset_attr(s, adaptionset_node);
  1027. if (ret < 0)
  1028. return ret;
  1029. node = xmlFirstElementChild(adaptionset_node);
  1030. while (node) {
  1031. if (!av_strcasecmp(node->name, (const char *)"SegmentTemplate")) {
  1032. fragment_template_node = node;
  1033. } else if (!av_strcasecmp(node->name, (const char *)"ContentComponent")) {
  1034. content_component_node = node;
  1035. } else if (!av_strcasecmp(node->name, (const char *)"BaseURL")) {
  1036. adaptionset_baseurl_node = node;
  1037. } else if (!av_strcasecmp(node->name, (const char *)"SegmentList")) {
  1038. adaptionset_segmentlist_node = node;
  1039. } else if (!av_strcasecmp(node->name, (const char *)"SupplementalProperty")) {
  1040. adaptionset_supplementalproperty_node = node;
  1041. } else if (!av_strcasecmp(node->name, (const char *)"Representation")) {
  1042. ret = parse_manifest_representation(s, url, node,
  1043. adaptionset_node,
  1044. mpd_baseurl_node,
  1045. period_baseurl_node,
  1046. period_segmenttemplate_node,
  1047. period_segmentlist_node,
  1048. fragment_template_node,
  1049. content_component_node,
  1050. adaptionset_baseurl_node,
  1051. adaptionset_segmentlist_node,
  1052. adaptionset_supplementalproperty_node);
  1053. if (ret < 0)
  1054. goto err;
  1055. }
  1056. node = xmlNextElementSibling(node);
  1057. }
  1058. err:
  1059. av_freep(&c->adaptionset_lang);
  1060. return ret;
  1061. }
  1062. static int parse_programinformation(AVFormatContext *s, xmlNodePtr node)
  1063. {
  1064. xmlChar *val = NULL;
  1065. node = xmlFirstElementChild(node);
  1066. while (node) {
  1067. if (!av_strcasecmp(node->name, "Title")) {
  1068. val = xmlNodeGetContent(node);
  1069. if (val) {
  1070. av_dict_set(&s->metadata, "Title", val, 0);
  1071. }
  1072. } else if (!av_strcasecmp(node->name, "Source")) {
  1073. val = xmlNodeGetContent(node);
  1074. if (val) {
  1075. av_dict_set(&s->metadata, "Source", val, 0);
  1076. }
  1077. } else if (!av_strcasecmp(node->name, "Copyright")) {
  1078. val = xmlNodeGetContent(node);
  1079. if (val) {
  1080. av_dict_set(&s->metadata, "Copyright", val, 0);
  1081. }
  1082. }
  1083. node = xmlNextElementSibling(node);
  1084. xmlFree(val);
  1085. val = NULL;
  1086. }
  1087. return 0;
  1088. }
  1089. static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
  1090. {
  1091. DASHContext *c = s->priv_data;
  1092. int ret = 0;
  1093. int close_in = 0;
  1094. uint8_t *new_url = NULL;
  1095. int64_t filesize = 0;
  1096. AVBPrint buf;
  1097. AVDictionary *opts = NULL;
  1098. xmlDoc *doc = NULL;
  1099. xmlNodePtr root_element = NULL;
  1100. xmlNodePtr node = NULL;
  1101. xmlNodePtr period_node = NULL;
  1102. xmlNodePtr tmp_node = NULL;
  1103. xmlNodePtr mpd_baseurl_node = NULL;
  1104. xmlNodePtr period_baseurl_node = NULL;
  1105. xmlNodePtr period_segmenttemplate_node = NULL;
  1106. xmlNodePtr period_segmentlist_node = NULL;
  1107. xmlNodePtr adaptionset_node = NULL;
  1108. xmlAttrPtr attr = NULL;
  1109. char *val = NULL;
  1110. uint32_t period_duration_sec = 0;
  1111. uint32_t period_start_sec = 0;
  1112. if (!in) {
  1113. close_in = 1;
  1114. av_dict_copy(&opts, c->avio_opts, 0);
  1115. ret = avio_open2(&in, url, AVIO_FLAG_READ, c->interrupt_callback, &opts);
  1116. av_dict_free(&opts);
  1117. if (ret < 0)
  1118. return ret;
  1119. }
  1120. if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, &new_url) >= 0) {
  1121. c->base_url = av_strdup(new_url);
  1122. } else {
  1123. c->base_url = av_strdup(url);
  1124. }
  1125. filesize = avio_size(in);
  1126. filesize = filesize > 0 ? filesize : DEFAULT_MANIFEST_SIZE;
  1127. if (filesize > MAX_BPRINT_READ_SIZE) {
  1128. av_log(s, AV_LOG_ERROR, "Manifest too large: %"PRId64"\n", filesize);
  1129. return AVERROR_INVALIDDATA;
  1130. }
  1131. av_bprint_init(&buf, filesize + 1, AV_BPRINT_SIZE_UNLIMITED);
  1132. if ((ret = avio_read_to_bprint(in, &buf, MAX_BPRINT_READ_SIZE)) < 0 ||
  1133. !avio_feof(in) ||
  1134. (filesize = buf.len) == 0) {
  1135. av_log(s, AV_LOG_ERROR, "Unable to read to manifest '%s'\n", url);
  1136. if (ret == 0)
  1137. ret = AVERROR_INVALIDDATA;
  1138. } else {
  1139. LIBXML_TEST_VERSION
  1140. doc = xmlReadMemory(buf.str, filesize, c->base_url, NULL, 0);
  1141. root_element = xmlDocGetRootElement(doc);
  1142. node = root_element;
  1143. if (!node) {
  1144. ret = AVERROR_INVALIDDATA;
  1145. av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing root node\n", url);
  1146. goto cleanup;
  1147. }
  1148. if (node->type != XML_ELEMENT_NODE ||
  1149. av_strcasecmp(node->name, (const char *)"MPD")) {
  1150. ret = AVERROR_INVALIDDATA;
  1151. av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - wrong root node name[%s] type[%d]\n", url, node->name, (int)node->type);
  1152. goto cleanup;
  1153. }
  1154. val = xmlGetProp(node, "type");
  1155. if (!val) {
  1156. av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing type attrib\n", url);
  1157. ret = AVERROR_INVALIDDATA;
  1158. goto cleanup;
  1159. }
  1160. if (!av_strcasecmp(val, (const char *)"dynamic"))
  1161. c->is_live = 1;
  1162. xmlFree(val);
  1163. attr = node->properties;
  1164. while (attr) {
  1165. val = xmlGetProp(node, attr->name);
  1166. if (!av_strcasecmp(attr->name, (const char *)"availabilityStartTime")) {
  1167. c->availability_start_time = get_utc_date_time_insec(s, (const char *)val);
  1168. av_log(s, AV_LOG_TRACE, "c->availability_start_time = [%"PRId64"]\n", c->availability_start_time);
  1169. } else if (!av_strcasecmp(attr->name, (const char *)"availabilityEndTime")) {
  1170. c->availability_end_time = get_utc_date_time_insec(s, (const char *)val);
  1171. av_log(s, AV_LOG_TRACE, "c->availability_end_time = [%"PRId64"]\n", c->availability_end_time);
  1172. } else if (!av_strcasecmp(attr->name, (const char *)"publishTime")) {
  1173. c->publish_time = get_utc_date_time_insec(s, (const char *)val);
  1174. av_log(s, AV_LOG_TRACE, "c->publish_time = [%"PRId64"]\n", c->publish_time);
  1175. } else if (!av_strcasecmp(attr->name, (const char *)"minimumUpdatePeriod")) {
  1176. c->minimum_update_period = get_duration_insec(s, (const char *)val);
  1177. av_log(s, AV_LOG_TRACE, "c->minimum_update_period = [%"PRId64"]\n", c->minimum_update_period);
  1178. } else if (!av_strcasecmp(attr->name, (const char *)"timeShiftBufferDepth")) {
  1179. c->time_shift_buffer_depth = get_duration_insec(s, (const char *)val);
  1180. av_log(s, AV_LOG_TRACE, "c->time_shift_buffer_depth = [%"PRId64"]\n", c->time_shift_buffer_depth);
  1181. } else if (!av_strcasecmp(attr->name, (const char *)"minBufferTime")) {
  1182. c->min_buffer_time = get_duration_insec(s, (const char *)val);
  1183. av_log(s, AV_LOG_TRACE, "c->min_buffer_time = [%"PRId64"]\n", c->min_buffer_time);
  1184. } else if (!av_strcasecmp(attr->name, (const char *)"suggestedPresentationDelay")) {
  1185. c->suggested_presentation_delay = get_duration_insec(s, (const char *)val);
  1186. av_log(s, AV_LOG_TRACE, "c->suggested_presentation_delay = [%"PRId64"]\n", c->suggested_presentation_delay);
  1187. } else if (!av_strcasecmp(attr->name, (const char *)"mediaPresentationDuration")) {
  1188. c->media_presentation_duration = get_duration_insec(s, (const char *)val);
  1189. av_log(s, AV_LOG_TRACE, "c->media_presentation_duration = [%"PRId64"]\n", c->media_presentation_duration);
  1190. }
  1191. attr = attr->next;
  1192. xmlFree(val);
  1193. }
  1194. tmp_node = find_child_node_by_name(node, "BaseURL");
  1195. if (tmp_node) {
  1196. mpd_baseurl_node = xmlCopyNode(tmp_node,1);
  1197. } else {
  1198. mpd_baseurl_node = xmlNewNode(NULL, "BaseURL");
  1199. }
  1200. // at now we can handle only one period, with the longest duration
  1201. node = xmlFirstElementChild(node);
  1202. while (node) {
  1203. if (!av_strcasecmp(node->name, (const char *)"Period")) {
  1204. period_duration_sec = 0;
  1205. period_start_sec = 0;
  1206. attr = node->properties;
  1207. while (attr) {
  1208. val = xmlGetProp(node, attr->name);
  1209. if (!av_strcasecmp(attr->name, (const char *)"duration")) {
  1210. period_duration_sec = get_duration_insec(s, (const char *)val);
  1211. } else if (!av_strcasecmp(attr->name, (const char *)"start")) {
  1212. period_start_sec = get_duration_insec(s, (const char *)val);
  1213. }
  1214. attr = attr->next;
  1215. xmlFree(val);
  1216. }
  1217. if ((period_duration_sec) >= (c->period_duration)) {
  1218. period_node = node;
  1219. c->period_duration = period_duration_sec;
  1220. c->period_start = period_start_sec;
  1221. if (c->period_start > 0)
  1222. c->media_presentation_duration = c->period_duration;
  1223. }
  1224. } else if (!av_strcasecmp(node->name, "ProgramInformation")) {
  1225. parse_programinformation(s, node);
  1226. }
  1227. node = xmlNextElementSibling(node);
  1228. }
  1229. if (!period_node) {
  1230. av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing Period node\n", url);
  1231. ret = AVERROR_INVALIDDATA;
  1232. goto cleanup;
  1233. }
  1234. adaptionset_node = xmlFirstElementChild(period_node);
  1235. while (adaptionset_node) {
  1236. if (!av_strcasecmp(adaptionset_node->name, (const char *)"BaseURL")) {
  1237. period_baseurl_node = adaptionset_node;
  1238. } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"SegmentTemplate")) {
  1239. period_segmenttemplate_node = adaptionset_node;
  1240. } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"SegmentList")) {
  1241. period_segmentlist_node = adaptionset_node;
  1242. } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"AdaptationSet")) {
  1243. parse_manifest_adaptationset(s, url, adaptionset_node, mpd_baseurl_node, period_baseurl_node, period_segmenttemplate_node, period_segmentlist_node);
  1244. }
  1245. adaptionset_node = xmlNextElementSibling(adaptionset_node);
  1246. }
  1247. cleanup:
  1248. /*free the document */
  1249. xmlFreeDoc(doc);
  1250. xmlCleanupParser();
  1251. xmlFreeNode(mpd_baseurl_node);
  1252. }
  1253. av_free(new_url);
  1254. av_bprint_finalize(&buf, NULL);
  1255. if (close_in) {
  1256. avio_close(in);
  1257. }
  1258. return ret;
  1259. }
  1260. static int64_t calc_cur_seg_no(AVFormatContext *s, struct representation *pls)
  1261. {
  1262. DASHContext *c = s->priv_data;
  1263. int64_t num = 0;
  1264. int64_t start_time_offset = 0;
  1265. if (c->is_live) {
  1266. if (pls->n_fragments) {
  1267. av_log(s, AV_LOG_TRACE, "in n_fragments mode\n");
  1268. num = pls->first_seq_no;
  1269. } else if (pls->n_timelines) {
  1270. av_log(s, AV_LOG_TRACE, "in n_timelines mode\n");
  1271. start_time_offset = get_segment_start_time_based_on_timeline(pls, 0xFFFFFFFF) - 60 * pls->fragment_timescale; // 60 seconds before end
  1272. num = calc_next_seg_no_from_timelines(pls, start_time_offset);
  1273. if (num == -1)
  1274. num = pls->first_seq_no;
  1275. else
  1276. num += pls->first_seq_no;
  1277. } else if (pls->fragment_duration){
  1278. av_log(s, AV_LOG_TRACE, "in fragment_duration mode fragment_timescale = %"PRId64", presentation_timeoffset = %"PRId64"\n", pls->fragment_timescale, pls->presentation_timeoffset);
  1279. if (pls->presentation_timeoffset) {
  1280. 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;
  1281. } else if (c->publish_time > 0 && !c->availability_start_time) {
  1282. if (c->min_buffer_time) {
  1283. 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;
  1284. } else {
  1285. 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;
  1286. }
  1287. } else {
  1288. num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time) - c->suggested_presentation_delay) * pls->fragment_timescale) / pls->fragment_duration;
  1289. }
  1290. }
  1291. } else {
  1292. num = pls->first_seq_no;
  1293. }
  1294. return num;
  1295. }
  1296. static int64_t calc_min_seg_no(AVFormatContext *s, struct representation *pls)
  1297. {
  1298. DASHContext *c = s->priv_data;
  1299. int64_t num = 0;
  1300. if (c->is_live && pls->fragment_duration) {
  1301. av_log(s, AV_LOG_TRACE, "in live mode\n");
  1302. 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;
  1303. } else {
  1304. num = pls->first_seq_no;
  1305. }
  1306. return num;
  1307. }
  1308. static int64_t calc_max_seg_no(struct representation *pls, DASHContext *c)
  1309. {
  1310. int64_t num = 0;
  1311. if (pls->n_fragments) {
  1312. num = pls->first_seq_no + pls->n_fragments - 1;
  1313. } else if (pls->n_timelines) {
  1314. int i = 0;
  1315. num = pls->first_seq_no + pls->n_timelines - 1;
  1316. for (i = 0; i < pls->n_timelines; i++) {
  1317. if (pls->timelines[i]->repeat == -1) {
  1318. int length_of_each_segment = pls->timelines[i]->duration / pls->fragment_timescale;
  1319. num = c->period_duration / length_of_each_segment;
  1320. } else {
  1321. num += pls->timelines[i]->repeat;
  1322. }
  1323. }
  1324. } else if (c->is_live && pls->fragment_duration) {
  1325. num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time)) * pls->fragment_timescale) / pls->fragment_duration;
  1326. } else if (pls->fragment_duration) {
  1327. num = pls->first_seq_no + (c->media_presentation_duration * pls->fragment_timescale) / pls->fragment_duration;
  1328. }
  1329. return num;
  1330. }
  1331. static void move_timelines(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
  1332. {
  1333. if (rep_dest && rep_src ) {
  1334. free_timelines_list(rep_dest);
  1335. rep_dest->timelines = rep_src->timelines;
  1336. rep_dest->n_timelines = rep_src->n_timelines;
  1337. rep_dest->first_seq_no = rep_src->first_seq_no;
  1338. rep_dest->last_seq_no = calc_max_seg_no(rep_dest, c);
  1339. rep_src->timelines = NULL;
  1340. rep_src->n_timelines = 0;
  1341. rep_dest->cur_seq_no = rep_src->cur_seq_no;
  1342. }
  1343. }
  1344. static void move_segments(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
  1345. {
  1346. if (rep_dest && rep_src ) {
  1347. free_fragment_list(rep_dest);
  1348. if (rep_src->start_number > (rep_dest->start_number + rep_dest->n_fragments))
  1349. rep_dest->cur_seq_no = 0;
  1350. else
  1351. rep_dest->cur_seq_no += rep_src->start_number - rep_dest->start_number;
  1352. rep_dest->fragments = rep_src->fragments;
  1353. rep_dest->n_fragments = rep_src->n_fragments;
  1354. rep_dest->parent = rep_src->parent;
  1355. rep_dest->last_seq_no = calc_max_seg_no(rep_dest, c);
  1356. rep_src->fragments = NULL;
  1357. rep_src->n_fragments = 0;
  1358. }
  1359. }
  1360. static int refresh_manifest(AVFormatContext *s)
  1361. {
  1362. int ret = 0, i;
  1363. DASHContext *c = s->priv_data;
  1364. // save current context
  1365. int n_videos = c->n_videos;
  1366. struct representation **videos = c->videos;
  1367. int n_audios = c->n_audios;
  1368. struct representation **audios = c->audios;
  1369. int n_subtitles = c->n_subtitles;
  1370. struct representation **subtitles = c->subtitles;
  1371. char *base_url = c->base_url;
  1372. c->base_url = NULL;
  1373. c->n_videos = 0;
  1374. c->videos = NULL;
  1375. c->n_audios = 0;
  1376. c->audios = NULL;
  1377. c->n_subtitles = 0;
  1378. c->subtitles = NULL;
  1379. ret = parse_manifest(s, s->url, NULL);
  1380. if (ret)
  1381. goto finish;
  1382. if (c->n_videos != n_videos) {
  1383. av_log(c, AV_LOG_ERROR,
  1384. "new manifest has mismatched no. of video representations, %d -> %d\n",
  1385. n_videos, c->n_videos);
  1386. return AVERROR_INVALIDDATA;
  1387. }
  1388. if (c->n_audios != n_audios) {
  1389. av_log(c, AV_LOG_ERROR,
  1390. "new manifest has mismatched no. of audio representations, %d -> %d\n",
  1391. n_audios, c->n_audios);
  1392. return AVERROR_INVALIDDATA;
  1393. }
  1394. if (c->n_subtitles != n_subtitles) {
  1395. av_log(c, AV_LOG_ERROR,
  1396. "new manifest has mismatched no. of subtitles representations, %d -> %d\n",
  1397. n_subtitles, c->n_subtitles);
  1398. return AVERROR_INVALIDDATA;
  1399. }
  1400. for (i = 0; i < n_videos; i++) {
  1401. struct representation *cur_video = videos[i];
  1402. struct representation *ccur_video = c->videos[i];
  1403. if (cur_video->timelines) {
  1404. // calc current time
  1405. int64_t currentTime = get_segment_start_time_based_on_timeline(cur_video, cur_video->cur_seq_no) / cur_video->fragment_timescale;
  1406. // update segments
  1407. ccur_video->cur_seq_no = calc_next_seg_no_from_timelines(ccur_video, currentTime * cur_video->fragment_timescale - 1);
  1408. if (ccur_video->cur_seq_no >= 0) {
  1409. move_timelines(ccur_video, cur_video, c);
  1410. }
  1411. }
  1412. if (cur_video->fragments) {
  1413. move_segments(ccur_video, cur_video, c);
  1414. }
  1415. }
  1416. for (i = 0; i < n_audios; i++) {
  1417. struct representation *cur_audio = audios[i];
  1418. struct representation *ccur_audio = c->audios[i];
  1419. if (cur_audio->timelines) {
  1420. // calc current time
  1421. int64_t currentTime = get_segment_start_time_based_on_timeline(cur_audio, cur_audio->cur_seq_no) / cur_audio->fragment_timescale;
  1422. // update segments
  1423. ccur_audio->cur_seq_no = calc_next_seg_no_from_timelines(ccur_audio, currentTime * cur_audio->fragment_timescale - 1);
  1424. if (ccur_audio->cur_seq_no >= 0) {
  1425. move_timelines(ccur_audio, cur_audio, c);
  1426. }
  1427. }
  1428. if (cur_audio->fragments) {
  1429. move_segments(ccur_audio, cur_audio, c);
  1430. }
  1431. }
  1432. finish:
  1433. // restore context
  1434. if (c->base_url)
  1435. av_free(base_url);
  1436. else
  1437. c->base_url = base_url;
  1438. if (c->subtitles)
  1439. free_subtitle_list(c);
  1440. if (c->audios)
  1441. free_audio_list(c);
  1442. if (c->videos)
  1443. free_video_list(c);
  1444. c->n_subtitles = n_subtitles;
  1445. c->subtitles = subtitles;
  1446. c->n_audios = n_audios;
  1447. c->audios = audios;
  1448. c->n_videos = n_videos;
  1449. c->videos = videos;
  1450. return ret;
  1451. }
  1452. static struct fragment *get_current_fragment(struct representation *pls)
  1453. {
  1454. int64_t min_seq_no = 0;
  1455. int64_t max_seq_no = 0;
  1456. struct fragment *seg = NULL;
  1457. struct fragment *seg_ptr = NULL;
  1458. DASHContext *c = pls->parent->priv_data;
  1459. while (( !ff_check_interrupt(c->interrupt_callback)&& pls->n_fragments > 0)) {
  1460. if (pls->cur_seq_no < pls->n_fragments) {
  1461. seg_ptr = pls->fragments[pls->cur_seq_no];
  1462. seg = av_mallocz(sizeof(struct fragment));
  1463. if (!seg) {
  1464. return NULL;
  1465. }
  1466. seg->url = av_strdup(seg_ptr->url);
  1467. if (!seg->url) {
  1468. av_free(seg);
  1469. return NULL;
  1470. }
  1471. seg->size = seg_ptr->size;
  1472. seg->url_offset = seg_ptr->url_offset;
  1473. return seg;
  1474. } else if (c->is_live) {
  1475. refresh_manifest(pls->parent);
  1476. } else {
  1477. break;
  1478. }
  1479. }
  1480. if (c->is_live) {
  1481. min_seq_no = calc_min_seg_no(pls->parent, pls);
  1482. max_seq_no = calc_max_seg_no(pls, c);
  1483. if (pls->timelines || pls->fragments) {
  1484. refresh_manifest(pls->parent);
  1485. }
  1486. if (pls->cur_seq_no <= min_seq_no) {
  1487. 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);
  1488. pls->cur_seq_no = calc_cur_seg_no(pls->parent, pls);
  1489. } else if (pls->cur_seq_no > max_seq_no) {
  1490. 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);
  1491. }
  1492. seg = av_mallocz(sizeof(struct fragment));
  1493. if (!seg) {
  1494. return NULL;
  1495. }
  1496. } else if (pls->cur_seq_no <= pls->last_seq_no) {
  1497. seg = av_mallocz(sizeof(struct fragment));
  1498. if (!seg) {
  1499. return NULL;
  1500. }
  1501. }
  1502. if (seg) {
  1503. char *tmpfilename= av_mallocz(c->max_url_size);
  1504. if (!tmpfilename) {
  1505. return NULL;
  1506. }
  1507. 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));
  1508. seg->url = av_strireplace(pls->url_template, pls->url_template, tmpfilename);
  1509. if (!seg->url) {
  1510. av_log(pls->parent, AV_LOG_WARNING, "Unable to resolve template url '%s', try to use origin template\n", pls->url_template);
  1511. seg->url = av_strdup(pls->url_template);
  1512. if (!seg->url) {
  1513. av_log(pls->parent, AV_LOG_ERROR, "Cannot resolve template url '%s'\n", pls->url_template);
  1514. av_free(tmpfilename);
  1515. return NULL;
  1516. }
  1517. }
  1518. av_free(tmpfilename);
  1519. seg->size = -1;
  1520. }
  1521. return seg;
  1522. }
  1523. static int read_from_url(struct representation *pls, struct fragment *seg,
  1524. uint8_t *buf, int buf_size)
  1525. {
  1526. int ret;
  1527. /* limit read if the fragment was only a part of a file */
  1528. if (seg->size >= 0)
  1529. buf_size = FFMIN(buf_size, pls->cur_seg_size - pls->cur_seg_offset);
  1530. ret = avio_read(pls->input, buf, buf_size);
  1531. if (ret > 0)
  1532. pls->cur_seg_offset += ret;
  1533. return ret;
  1534. }
  1535. static int open_input(DASHContext *c, struct representation *pls, struct fragment *seg)
  1536. {
  1537. AVDictionary *opts = NULL;
  1538. char *url = NULL;
  1539. int ret = 0;
  1540. url = av_mallocz(c->max_url_size);
  1541. if (!url) {
  1542. ret = AVERROR(ENOMEM);
  1543. goto cleanup;
  1544. }
  1545. if (seg->size >= 0) {
  1546. /* try to restrict the HTTP request to the part we want
  1547. * (if this is in fact a HTTP request) */
  1548. av_dict_set_int(&opts, "offset", seg->url_offset, 0);
  1549. av_dict_set_int(&opts, "end_offset", seg->url_offset + seg->size, 0);
  1550. }
  1551. ff_make_absolute_url(url, c->max_url_size, c->base_url, seg->url);
  1552. av_log(pls->parent, AV_LOG_VERBOSE, "DASH request for url '%s', offset %"PRId64", playlist %d\n",
  1553. url, seg->url_offset, pls->rep_idx);
  1554. ret = open_url(pls->parent, &pls->input, url, c->avio_opts, opts, NULL);
  1555. cleanup:
  1556. av_free(url);
  1557. av_dict_free(&opts);
  1558. pls->cur_seg_offset = 0;
  1559. pls->cur_seg_size = seg->size;
  1560. return ret;
  1561. }
  1562. static int update_init_section(struct representation *pls)
  1563. {
  1564. static const int max_init_section_size = 1024 * 1024;
  1565. DASHContext *c = pls->parent->priv_data;
  1566. int64_t sec_size;
  1567. int64_t urlsize;
  1568. int ret;
  1569. if (!pls->init_section || pls->init_sec_buf)
  1570. return 0;
  1571. ret = open_input(c, pls, pls->init_section);
  1572. if (ret < 0) {
  1573. av_log(pls->parent, AV_LOG_WARNING,
  1574. "Failed to open an initialization section in playlist %d\n",
  1575. pls->rep_idx);
  1576. return ret;
  1577. }
  1578. if (pls->init_section->size >= 0)
  1579. sec_size = pls->init_section->size;
  1580. else if ((urlsize = avio_size(pls->input)) >= 0)
  1581. sec_size = urlsize;
  1582. else
  1583. sec_size = max_init_section_size;
  1584. av_log(pls->parent, AV_LOG_DEBUG,
  1585. "Downloading an initialization section of size %"PRId64"\n",
  1586. sec_size);
  1587. sec_size = FFMIN(sec_size, max_init_section_size);
  1588. av_fast_malloc(&pls->init_sec_buf, &pls->init_sec_buf_size, sec_size);
  1589. ret = read_from_url(pls, pls->init_section, pls->init_sec_buf,
  1590. pls->init_sec_buf_size);
  1591. ff_format_io_close(pls->parent, &pls->input);
  1592. if (ret < 0)
  1593. return ret;
  1594. pls->init_sec_data_len = ret;
  1595. pls->init_sec_buf_read_offset = 0;
  1596. return 0;
  1597. }
  1598. static int64_t seek_data(void *opaque, int64_t offset, int whence)
  1599. {
  1600. struct representation *v = opaque;
  1601. if (v->n_fragments && !v->init_sec_data_len) {
  1602. return avio_seek(v->input, offset, whence);
  1603. }
  1604. return AVERROR(ENOSYS);
  1605. }
  1606. static int read_data(void *opaque, uint8_t *buf, int buf_size)
  1607. {
  1608. int ret = 0;
  1609. struct representation *v = opaque;
  1610. DASHContext *c = v->parent->priv_data;
  1611. restart:
  1612. if (!v->input) {
  1613. free_fragment(&v->cur_seg);
  1614. v->cur_seg = get_current_fragment(v);
  1615. if (!v->cur_seg) {
  1616. ret = AVERROR_EOF;
  1617. goto end;
  1618. }
  1619. /* load/update Media Initialization Section, if any */
  1620. ret = update_init_section(v);
  1621. if (ret)
  1622. goto end;
  1623. ret = open_input(c, v, v->cur_seg);
  1624. if (ret < 0) {
  1625. if (ff_check_interrupt(c->interrupt_callback)) {
  1626. ret = AVERROR_EXIT;
  1627. goto end;
  1628. }
  1629. av_log(v->parent, AV_LOG_WARNING, "Failed to open fragment of playlist %d\n", v->rep_idx);
  1630. v->cur_seq_no++;
  1631. goto restart;
  1632. }
  1633. }
  1634. if (v->init_sec_buf_read_offset < v->init_sec_data_len) {
  1635. /* Push init section out first before first actual fragment */
  1636. int copy_size = FFMIN(v->init_sec_data_len - v->init_sec_buf_read_offset, buf_size);
  1637. memcpy(buf, v->init_sec_buf, copy_size);
  1638. v->init_sec_buf_read_offset += copy_size;
  1639. ret = copy_size;
  1640. goto end;
  1641. }
  1642. /* check the v->cur_seg, if it is null, get current and double check if the new v->cur_seg*/
  1643. if (!v->cur_seg) {
  1644. v->cur_seg = get_current_fragment(v);
  1645. }
  1646. if (!v->cur_seg) {
  1647. ret = AVERROR_EOF;
  1648. goto end;
  1649. }
  1650. ret = read_from_url(v, v->cur_seg, buf, buf_size);
  1651. if (ret > 0)
  1652. goto end;
  1653. if (c->is_live || v->cur_seq_no < v->last_seq_no) {
  1654. if (!v->is_restart_needed)
  1655. v->cur_seq_no++;
  1656. v->is_restart_needed = 1;
  1657. }
  1658. end:
  1659. return ret;
  1660. }
  1661. static int save_avio_options(AVFormatContext *s)
  1662. {
  1663. DASHContext *c = s->priv_data;
  1664. const char *opts[] = {
  1665. "headers", "user_agent", "cookies", "http_proxy", "referer", "rw_timeout", "icy", NULL };
  1666. const char **opt = opts;
  1667. uint8_t *buf = NULL;
  1668. int ret = 0;
  1669. while (*opt) {
  1670. if (av_opt_get(s->pb, *opt, AV_OPT_SEARCH_CHILDREN, &buf) >= 0) {
  1671. if (buf[0] != '\0') {
  1672. ret = av_dict_set(&c->avio_opts, *opt, buf, AV_DICT_DONT_STRDUP_VAL);
  1673. if (ret < 0)
  1674. return ret;
  1675. } else {
  1676. av_freep(&buf);
  1677. }
  1678. }
  1679. opt++;
  1680. }
  1681. return ret;
  1682. }
  1683. static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
  1684. int flags, AVDictionary **opts)
  1685. {
  1686. av_log(s, AV_LOG_ERROR,
  1687. "A DASH playlist item '%s' referred to an external file '%s'. "
  1688. "Opening this file was forbidden for security reasons\n",
  1689. s->url, url);
  1690. return AVERROR(EPERM);
  1691. }
  1692. static void close_demux_for_component(struct representation *pls)
  1693. {
  1694. /* note: the internal buffer could have changed */
  1695. av_freep(&pls->pb.buffer);
  1696. memset(&pls->pb, 0x00, sizeof(AVIOContext));
  1697. pls->ctx->pb = NULL;
  1698. avformat_close_input(&pls->ctx);
  1699. pls->ctx = NULL;
  1700. }
  1701. static int reopen_demux_for_component(AVFormatContext *s, struct representation *pls)
  1702. {
  1703. DASHContext *c = s->priv_data;
  1704. ff_const59 AVInputFormat *in_fmt = NULL;
  1705. AVDictionary *in_fmt_opts = NULL;
  1706. uint8_t *avio_ctx_buffer = NULL;
  1707. int ret = 0, i;
  1708. if (pls->ctx) {
  1709. close_demux_for_component(pls);
  1710. }
  1711. if (ff_check_interrupt(&s->interrupt_callback)) {
  1712. ret = AVERROR_EXIT;
  1713. goto fail;
  1714. }
  1715. if (!(pls->ctx = avformat_alloc_context())) {
  1716. ret = AVERROR(ENOMEM);
  1717. goto fail;
  1718. }
  1719. avio_ctx_buffer = av_malloc(INITIAL_BUFFER_SIZE);
  1720. if (!avio_ctx_buffer ) {
  1721. ret = AVERROR(ENOMEM);
  1722. avformat_free_context(pls->ctx);
  1723. pls->ctx = NULL;
  1724. goto fail;
  1725. }
  1726. if (c->is_live) {
  1727. ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, NULL);
  1728. } else {
  1729. ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, seek_data);
  1730. }
  1731. pls->pb.seekable = 0;
  1732. if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0)
  1733. goto fail;
  1734. pls->ctx->flags = AVFMT_FLAG_CUSTOM_IO;
  1735. pls->ctx->probesize = s->probesize > 0 ? s->probesize : 1024 * 4;
  1736. pls->ctx->max_analyze_duration = s->max_analyze_duration > 0 ? s->max_analyze_duration : 4 * AV_TIME_BASE;
  1737. pls->ctx->interrupt_callback = s->interrupt_callback;
  1738. ret = av_probe_input_buffer(&pls->pb, &in_fmt, "", NULL, 0, 0);
  1739. if (ret < 0) {
  1740. av_log(s, AV_LOG_ERROR, "Error when loading first fragment, playlist %d\n", (int)pls->rep_idx);
  1741. avformat_free_context(pls->ctx);
  1742. pls->ctx = NULL;
  1743. goto fail;
  1744. }
  1745. pls->ctx->pb = &pls->pb;
  1746. pls->ctx->io_open = nested_io_open;
  1747. // provide additional information from mpd if available
  1748. ret = avformat_open_input(&pls->ctx, "", in_fmt, &in_fmt_opts); //pls->init_section->url
  1749. av_dict_free(&in_fmt_opts);
  1750. if (ret < 0)
  1751. goto fail;
  1752. if (pls->n_fragments) {
  1753. #if FF_API_R_FRAME_RATE
  1754. if (pls->framerate.den) {
  1755. for (i = 0; i < pls->ctx->nb_streams; i++)
  1756. pls->ctx->streams[i]->r_frame_rate = pls->framerate;
  1757. }
  1758. #endif
  1759. ret = avformat_find_stream_info(pls->ctx, NULL);
  1760. if (ret < 0)
  1761. goto fail;
  1762. }
  1763. fail:
  1764. return ret;
  1765. }
  1766. static int open_demux_for_component(AVFormatContext *s, struct representation *pls)
  1767. {
  1768. int ret = 0;
  1769. int i;
  1770. pls->parent = s;
  1771. pls->cur_seq_no = calc_cur_seg_no(s, pls);
  1772. if (!pls->last_seq_no) {
  1773. pls->last_seq_no = calc_max_seg_no(pls, s->priv_data);
  1774. }
  1775. ret = reopen_demux_for_component(s, pls);
  1776. if (ret < 0) {
  1777. goto fail;
  1778. }
  1779. for (i = 0; i < pls->ctx->nb_streams; i++) {
  1780. AVStream *st = avformat_new_stream(s, NULL);
  1781. AVStream *ist = pls->ctx->streams[i];
  1782. if (!st) {
  1783. ret = AVERROR(ENOMEM);
  1784. goto fail;
  1785. }
  1786. st->id = i;
  1787. avcodec_parameters_copy(st->codecpar, ist->codecpar);
  1788. avpriv_set_pts_info(st, ist->pts_wrap_bits, ist->time_base.num, ist->time_base.den);
  1789. // copy disposition
  1790. st->disposition = ist->disposition;
  1791. // copy side data
  1792. for (int i = 0; i < ist->nb_side_data; i++) {
  1793. const AVPacketSideData *sd_src = &ist->side_data[i];
  1794. uint8_t *dst_data;
  1795. dst_data = av_stream_new_side_data(st, sd_src->type, sd_src->size);
  1796. if (!dst_data)
  1797. return AVERROR(ENOMEM);
  1798. memcpy(dst_data, sd_src->data, sd_src->size);
  1799. }
  1800. }
  1801. return 0;
  1802. fail:
  1803. return ret;
  1804. }
  1805. static int is_common_init_section_exist(struct representation **pls, int n_pls)
  1806. {
  1807. struct fragment *first_init_section = pls[0]->init_section;
  1808. char *url =NULL;
  1809. int64_t url_offset = -1;
  1810. int64_t size = -1;
  1811. int i = 0;
  1812. if (first_init_section == NULL || n_pls == 0)
  1813. return 0;
  1814. url = first_init_section->url;
  1815. url_offset = first_init_section->url_offset;
  1816. size = pls[0]->init_section->size;
  1817. for (i=0;i<n_pls;i++) {
  1818. if (av_strcasecmp(pls[i]->init_section->url,url) || pls[i]->init_section->url_offset != url_offset || pls[i]->init_section->size != size) {
  1819. return 0;
  1820. }
  1821. }
  1822. return 1;
  1823. }
  1824. static int copy_init_section(struct representation *rep_dest, struct representation *rep_src)
  1825. {
  1826. rep_dest->init_sec_buf = av_mallocz(rep_src->init_sec_buf_size);
  1827. if (!rep_dest->init_sec_buf) {
  1828. av_log(rep_dest->ctx, AV_LOG_WARNING, "Cannot alloc memory for init_sec_buf\n");
  1829. return AVERROR(ENOMEM);
  1830. }
  1831. memcpy(rep_dest->init_sec_buf, rep_src->init_sec_buf, rep_src->init_sec_data_len);
  1832. rep_dest->init_sec_buf_size = rep_src->init_sec_buf_size;
  1833. rep_dest->init_sec_data_len = rep_src->init_sec_data_len;
  1834. rep_dest->cur_timestamp = rep_src->cur_timestamp;
  1835. return 0;
  1836. }
  1837. static int dash_read_header(AVFormatContext *s)
  1838. {
  1839. DASHContext *c = s->priv_data;
  1840. struct representation *rep;
  1841. int ret = 0;
  1842. int stream_index = 0;
  1843. int i;
  1844. c->interrupt_callback = &s->interrupt_callback;
  1845. if ((ret = save_avio_options(s)) < 0)
  1846. goto fail;
  1847. if ((ret = parse_manifest(s, s->url, s->pb)) < 0)
  1848. goto fail;
  1849. /* If this isn't a live stream, fill the total duration of the
  1850. * stream. */
  1851. if (!c->is_live) {
  1852. s->duration = (int64_t) c->media_presentation_duration * AV_TIME_BASE;
  1853. } else {
  1854. av_dict_set(&c->avio_opts, "seekable", "0", 0);
  1855. }
  1856. if(c->n_videos)
  1857. c->is_init_section_common_video = is_common_init_section_exist(c->videos, c->n_videos);
  1858. /* Open the demuxer for video and audio components if available */
  1859. for (i = 0; i < c->n_videos; i++) {
  1860. rep = c->videos[i];
  1861. if (i > 0 && c->is_init_section_common_video) {
  1862. ret = copy_init_section(rep, c->videos[0]);
  1863. if (ret < 0)
  1864. goto fail;
  1865. }
  1866. ret = open_demux_for_component(s, rep);
  1867. if (ret)
  1868. goto fail;
  1869. rep->stream_index = stream_index;
  1870. ++stream_index;
  1871. }
  1872. if(c->n_audios)
  1873. c->is_init_section_common_audio = is_common_init_section_exist(c->audios, c->n_audios);
  1874. for (i = 0; i < c->n_audios; i++) {
  1875. rep = c->audios[i];
  1876. if (i > 0 && c->is_init_section_common_audio) {
  1877. ret = copy_init_section(rep, c->audios[0]);
  1878. if (ret < 0)
  1879. goto fail;
  1880. }
  1881. ret = open_demux_for_component(s, rep);
  1882. if (ret)
  1883. goto fail;
  1884. rep->stream_index = stream_index;
  1885. ++stream_index;
  1886. }
  1887. if (c->n_subtitles)
  1888. c->is_init_section_common_audio = is_common_init_section_exist(c->subtitles, c->n_subtitles);
  1889. for (i = 0; i < c->n_subtitles; i++) {
  1890. rep = c->subtitles[i];
  1891. if (i > 0 && c->is_init_section_common_audio) {
  1892. ret = copy_init_section(rep, c->subtitles[0]);
  1893. if (ret < 0)
  1894. goto fail;
  1895. }
  1896. ret = open_demux_for_component(s, rep);
  1897. if (ret)
  1898. goto fail;
  1899. rep->stream_index = stream_index;
  1900. ++stream_index;
  1901. }
  1902. if (!stream_index) {
  1903. ret = AVERROR_INVALIDDATA;
  1904. goto fail;
  1905. }
  1906. /* Create a program */
  1907. if (!ret) {
  1908. AVProgram *program;
  1909. program = av_new_program(s, 0);
  1910. if (!program) {
  1911. goto fail;
  1912. }
  1913. for (i = 0; i < c->n_videos; i++) {
  1914. rep = c->videos[i];
  1915. av_program_add_stream_index(s, 0, rep->stream_index);
  1916. rep->assoc_stream = s->streams[rep->stream_index];
  1917. if (rep->bandwidth > 0)
  1918. av_dict_set_int(&rep->assoc_stream->metadata, "variant_bitrate", rep->bandwidth, 0);
  1919. if (rep->id[0])
  1920. av_dict_set(&rep->assoc_stream->metadata, "id", rep->id, 0);
  1921. }
  1922. for (i = 0; i < c->n_audios; i++) {
  1923. rep = c->audios[i];
  1924. av_program_add_stream_index(s, 0, rep->stream_index);
  1925. rep->assoc_stream = s->streams[rep->stream_index];
  1926. if (rep->bandwidth > 0)
  1927. av_dict_set_int(&rep->assoc_stream->metadata, "variant_bitrate", rep->bandwidth, 0);
  1928. if (rep->id[0])
  1929. av_dict_set(&rep->assoc_stream->metadata, "id", rep->id, 0);
  1930. if (rep->lang) {
  1931. av_dict_set(&rep->assoc_stream->metadata, "language", rep->lang, 0);
  1932. av_freep(&rep->lang);
  1933. }
  1934. }
  1935. for (i = 0; i < c->n_subtitles; i++) {
  1936. rep = c->subtitles[i];
  1937. av_program_add_stream_index(s, 0, rep->stream_index);
  1938. rep->assoc_stream = s->streams[rep->stream_index];
  1939. if (rep->id[0])
  1940. av_dict_set(&rep->assoc_stream->metadata, "id", rep->id, 0);
  1941. if (rep->lang) {
  1942. av_dict_set(&rep->assoc_stream->metadata, "language", rep->lang, 0);
  1943. av_freep(&rep->lang);
  1944. }
  1945. }
  1946. }
  1947. return 0;
  1948. fail:
  1949. return ret;
  1950. }
  1951. static void recheck_discard_flags(AVFormatContext *s, struct representation **p, int n)
  1952. {
  1953. int i, j;
  1954. for (i = 0; i < n; i++) {
  1955. struct representation *pls = p[i];
  1956. int needed = !pls->assoc_stream || pls->assoc_stream->discard < AVDISCARD_ALL;
  1957. if (needed && !pls->ctx) {
  1958. pls->cur_seg_offset = 0;
  1959. pls->init_sec_buf_read_offset = 0;
  1960. /* Catch up */
  1961. for (j = 0; j < n; j++) {
  1962. pls->cur_seq_no = FFMAX(pls->cur_seq_no, p[j]->cur_seq_no);
  1963. }
  1964. reopen_demux_for_component(s, pls);
  1965. av_log(s, AV_LOG_INFO, "Now receiving stream_index %d\n", pls->stream_index);
  1966. } else if (!needed && pls->ctx) {
  1967. close_demux_for_component(pls);
  1968. ff_format_io_close(pls->parent, &pls->input);
  1969. av_log(s, AV_LOG_INFO, "No longer receiving stream_index %d\n", pls->stream_index);
  1970. }
  1971. }
  1972. }
  1973. static int dash_read_packet(AVFormatContext *s, AVPacket *pkt)
  1974. {
  1975. DASHContext *c = s->priv_data;
  1976. int ret = 0, i;
  1977. int64_t mints = 0;
  1978. struct representation *cur = NULL;
  1979. struct representation *rep = NULL;
  1980. recheck_discard_flags(s, c->videos, c->n_videos);
  1981. recheck_discard_flags(s, c->audios, c->n_audios);
  1982. recheck_discard_flags(s, c->subtitles, c->n_subtitles);
  1983. for (i = 0; i < c->n_videos; i++) {
  1984. rep = c->videos[i];
  1985. if (!rep->ctx)
  1986. continue;
  1987. if (!cur || rep->cur_timestamp < mints) {
  1988. cur = rep;
  1989. mints = rep->cur_timestamp;
  1990. }
  1991. }
  1992. for (i = 0; i < c->n_audios; i++) {
  1993. rep = c->audios[i];
  1994. if (!rep->ctx)
  1995. continue;
  1996. if (!cur || rep->cur_timestamp < mints) {
  1997. cur = rep;
  1998. mints = rep->cur_timestamp;
  1999. }
  2000. }
  2001. for (i = 0; i < c->n_subtitles; i++) {
  2002. rep = c->subtitles[i];
  2003. if (!rep->ctx)
  2004. continue;
  2005. if (!cur || rep->cur_timestamp < mints) {
  2006. cur = rep;
  2007. mints = rep->cur_timestamp;
  2008. }
  2009. }
  2010. if (!cur) {
  2011. return AVERROR_INVALIDDATA;
  2012. }
  2013. while (!ff_check_interrupt(c->interrupt_callback) && !ret) {
  2014. ret = av_read_frame(cur->ctx, pkt);
  2015. if (ret >= 0) {
  2016. /* If we got a packet, return it */
  2017. cur->cur_timestamp = av_rescale(pkt->pts, (int64_t)cur->ctx->streams[0]->time_base.num * 90000, cur->ctx->streams[0]->time_base.den);
  2018. pkt->stream_index = cur->stream_index;
  2019. return 0;
  2020. }
  2021. if (cur->is_restart_needed) {
  2022. cur->cur_seg_offset = 0;
  2023. cur->init_sec_buf_read_offset = 0;
  2024. ff_format_io_close(cur->parent, &cur->input);
  2025. ret = reopen_demux_for_component(s, cur);
  2026. cur->is_restart_needed = 0;
  2027. }
  2028. }
  2029. return AVERROR_EOF;
  2030. }
  2031. static int dash_close(AVFormatContext *s)
  2032. {
  2033. DASHContext *c = s->priv_data;
  2034. free_audio_list(c);
  2035. free_video_list(c);
  2036. av_dict_free(&c->avio_opts);
  2037. av_freep(&c->base_url);
  2038. return 0;
  2039. }
  2040. static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos_msec, int flags, int dry_run)
  2041. {
  2042. int ret = 0;
  2043. int i = 0;
  2044. int j = 0;
  2045. int64_t duration = 0;
  2046. av_log(pls->parent, AV_LOG_VERBOSE, "DASH seek pos[%"PRId64"ms], playlist %d%s\n",
  2047. seek_pos_msec, pls->rep_idx, dry_run ? " (dry)" : "");
  2048. // single fragment mode
  2049. if (pls->n_fragments == 1) {
  2050. pls->cur_timestamp = 0;
  2051. pls->cur_seg_offset = 0;
  2052. if (dry_run)
  2053. return 0;
  2054. ff_read_frame_flush(pls->ctx);
  2055. return av_seek_frame(pls->ctx, -1, seek_pos_msec * 1000, flags);
  2056. }
  2057. ff_format_io_close(pls->parent, &pls->input);
  2058. // find the nearest fragment
  2059. if (pls->n_timelines > 0 && pls->fragment_timescale > 0) {
  2060. int64_t num = pls->first_seq_no;
  2061. av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline start n_timelines[%d] "
  2062. "last_seq_no[%"PRId64"], playlist %d.\n",
  2063. (int)pls->n_timelines, (int64_t)pls->last_seq_no, (int)pls->rep_idx);
  2064. for (i = 0; i < pls->n_timelines; i++) {
  2065. if (pls->timelines[i]->starttime > 0) {
  2066. duration = pls->timelines[i]->starttime;
  2067. }
  2068. duration += pls->timelines[i]->duration;
  2069. if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
  2070. goto set_seq_num;
  2071. }
  2072. for (j = 0; j < pls->timelines[i]->repeat; j++) {
  2073. duration += pls->timelines[i]->duration;
  2074. num++;
  2075. if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
  2076. goto set_seq_num;
  2077. }
  2078. }
  2079. num++;
  2080. }
  2081. set_seq_num:
  2082. pls->cur_seq_no = num > pls->last_seq_no ? pls->last_seq_no : num;
  2083. av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline end cur_seq_no[%"PRId64"], playlist %d.\n",
  2084. (int64_t)pls->cur_seq_no, (int)pls->rep_idx);
  2085. } else if (pls->fragment_duration > 0) {
  2086. pls->cur_seq_no = pls->first_seq_no + ((seek_pos_msec * pls->fragment_timescale) / pls->fragment_duration) / 1000;
  2087. } else {
  2088. av_log(pls->parent, AV_LOG_ERROR, "dash_seek missing timeline or fragment_duration\n");
  2089. pls->cur_seq_no = pls->first_seq_no;
  2090. }
  2091. pls->cur_timestamp = 0;
  2092. pls->cur_seg_offset = 0;
  2093. pls->init_sec_buf_read_offset = 0;
  2094. ret = dry_run ? 0 : reopen_demux_for_component(s, pls);
  2095. return ret;
  2096. }
  2097. static int dash_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  2098. {
  2099. int ret = 0, i;
  2100. DASHContext *c = s->priv_data;
  2101. int64_t seek_pos_msec = av_rescale_rnd(timestamp, 1000,
  2102. s->streams[stream_index]->time_base.den,
  2103. flags & AVSEEK_FLAG_BACKWARD ?
  2104. AV_ROUND_DOWN : AV_ROUND_UP);
  2105. if ((flags & AVSEEK_FLAG_BYTE) || c->is_live)
  2106. return AVERROR(ENOSYS);
  2107. /* Seek in discarded streams with dry_run=1 to avoid reopening them */
  2108. for (i = 0; i < c->n_videos; i++) {
  2109. if (!ret)
  2110. ret = dash_seek(s, c->videos[i], seek_pos_msec, flags, !c->videos[i]->ctx);
  2111. }
  2112. for (i = 0; i < c->n_audios; i++) {
  2113. if (!ret)
  2114. ret = dash_seek(s, c->audios[i], seek_pos_msec, flags, !c->audios[i]->ctx);
  2115. }
  2116. for (i = 0; i < c->n_subtitles; i++) {
  2117. if (!ret)
  2118. ret = dash_seek(s, c->subtitles[i], seek_pos_msec, flags, !c->subtitles[i]->ctx);
  2119. }
  2120. return ret;
  2121. }
  2122. static int dash_probe(const AVProbeData *p)
  2123. {
  2124. if (!av_stristr(p->buf, "<MPD"))
  2125. return 0;
  2126. if (av_stristr(p->buf, "dash:profile:isoff-on-demand:2011") ||
  2127. av_stristr(p->buf, "dash:profile:isoff-live:2011") ||
  2128. av_stristr(p->buf, "dash:profile:isoff-live:2012") ||
  2129. av_stristr(p->buf, "dash:profile:isoff-main:2011") ||
  2130. av_stristr(p->buf, "3GPP:PSS:profile:DASH1")) {
  2131. return AVPROBE_SCORE_MAX;
  2132. }
  2133. if (av_stristr(p->buf, "dash:profile")) {
  2134. return AVPROBE_SCORE_MAX;
  2135. }
  2136. return 0;
  2137. }
  2138. #define OFFSET(x) offsetof(DASHContext, x)
  2139. #define FLAGS AV_OPT_FLAG_DECODING_PARAM
  2140. static const AVOption dash_options[] = {
  2141. {"allowed_extensions", "List of file extensions that dash is allowed to access",
  2142. OFFSET(allowed_extensions), AV_OPT_TYPE_STRING,
  2143. {.str = "aac,m4a,m4s,m4v,mov,mp4,webm,ts"},
  2144. INT_MIN, INT_MAX, FLAGS},
  2145. {NULL}
  2146. };
  2147. static const AVClass dash_class = {
  2148. .class_name = "dash",
  2149. .item_name = av_default_item_name,
  2150. .option = dash_options,
  2151. .version = LIBAVUTIL_VERSION_INT,
  2152. };
  2153. AVInputFormat ff_dash_demuxer = {
  2154. .name = "dash",
  2155. .long_name = NULL_IF_CONFIG_SMALL("Dynamic Adaptive Streaming over HTTP"),
  2156. .priv_class = &dash_class,
  2157. .priv_data_size = sizeof(DASHContext),
  2158. .read_probe = dash_probe,
  2159. .read_header = dash_read_header,
  2160. .read_packet = dash_read_packet,
  2161. .read_close = dash_close,
  2162. .read_seek = dash_read_seek,
  2163. .flags = AVFMT_NO_BYTE_SEEK,
  2164. };