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.

2414 lines
83KB

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