You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2406 lines
83KB

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