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.

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