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.

2403 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. ff_format_io_close(pls->parent, &pls->input);
  322. if (pls->ctx) {
  323. pls->ctx->pb = NULL;
  324. avformat_close_input(&pls->ctx);
  325. }
  326. av_freep(&pls->url_template);
  327. av_freep(&pls);
  328. }
  329. static void free_video_list(DASHContext *c)
  330. {
  331. int i;
  332. for (i = 0; i < c->n_videos; i++) {
  333. struct representation *pls = c->videos[i];
  334. free_representation(pls);
  335. }
  336. av_freep(&c->videos);
  337. c->n_videos = 0;
  338. }
  339. static void free_audio_list(DASHContext *c)
  340. {
  341. int i;
  342. for (i = 0; i < c->n_audios; i++) {
  343. struct representation *pls = c->audios[i];
  344. free_representation(pls);
  345. }
  346. av_freep(&c->audios);
  347. c->n_audios = 0;
  348. }
  349. static void free_subtitle_list(DASHContext *c)
  350. {
  351. int i;
  352. for (i = 0; i < c->n_subtitles; i++) {
  353. struct representation *pls = c->subtitles[i];
  354. free_representation(pls);
  355. }
  356. av_freep(&c->subtitles);
  357. c->n_subtitles = 0;
  358. }
  359. static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
  360. AVDictionary *opts, AVDictionary *opts2, int *is_http)
  361. {
  362. DASHContext *c = s->priv_data;
  363. AVDictionary *tmp = NULL;
  364. const char *proto_name = NULL;
  365. int ret;
  366. av_dict_copy(&tmp, opts, 0);
  367. av_dict_copy(&tmp, opts2, 0);
  368. if (av_strstart(url, "crypto", NULL)) {
  369. if (url[6] == '+' || url[6] == ':')
  370. proto_name = avio_find_protocol_name(url + 7);
  371. }
  372. if (!proto_name)
  373. proto_name = avio_find_protocol_name(url);
  374. if (!proto_name)
  375. return AVERROR_INVALIDDATA;
  376. // only http(s) & file are allowed
  377. if (av_strstart(proto_name, "file", NULL)) {
  378. if (strcmp(c->allowed_extensions, "ALL") && !av_match_ext(url, c->allowed_extensions)) {
  379. av_log(s, AV_LOG_ERROR,
  380. "Filename extension of \'%s\' is not a common multimedia extension, blocked for security reasons.\n"
  381. "If you wish to override this adjust allowed_extensions, you can set it to \'ALL\' to allow all\n",
  382. url);
  383. return AVERROR_INVALIDDATA;
  384. }
  385. } else if (av_strstart(proto_name, "http", NULL)) {
  386. ;
  387. } else
  388. return AVERROR_INVALIDDATA;
  389. if (!strncmp(proto_name, url, strlen(proto_name)) && url[strlen(proto_name)] == ':')
  390. ;
  391. else if (av_strstart(url, "crypto", NULL) && !strncmp(proto_name, url + 7, strlen(proto_name)) && url[7 + strlen(proto_name)] == ':')
  392. ;
  393. else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5))
  394. return AVERROR_INVALIDDATA;
  395. av_freep(pb);
  396. ret = avio_open2(pb, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp);
  397. if (ret >= 0) {
  398. // update cookies on http response with setcookies.
  399. char *new_cookies = NULL;
  400. if (!(s->flags & AVFMT_FLAG_CUSTOM_IO))
  401. av_opt_get(*pb, "cookies", AV_OPT_SEARCH_CHILDREN, (uint8_t**)&new_cookies);
  402. if (new_cookies) {
  403. av_dict_set(&opts, "cookies", new_cookies, AV_DICT_DONT_STRDUP_VAL);
  404. }
  405. }
  406. av_dict_free(&tmp);
  407. if (is_http)
  408. *is_http = av_strstart(proto_name, "http", NULL);
  409. return ret;
  410. }
  411. static char *get_content_url(xmlNodePtr *baseurl_nodes,
  412. int n_baseurl_nodes,
  413. int max_url_size,
  414. char *rep_id_val,
  415. char *rep_bandwidth_val,
  416. char *val)
  417. {
  418. int i;
  419. char *text;
  420. char *url = NULL;
  421. char *tmp_str = av_mallocz(max_url_size);
  422. char *tmp_str_2 = av_mallocz(max_url_size);
  423. if (!tmp_str || !tmp_str_2) {
  424. return NULL;
  425. }
  426. for (i = 0; i < n_baseurl_nodes; ++i) {
  427. if (baseurl_nodes[i] &&
  428. baseurl_nodes[i]->children &&
  429. baseurl_nodes[i]->children->type == XML_TEXT_NODE) {
  430. text = xmlNodeGetContent(baseurl_nodes[i]->children);
  431. if (text) {
  432. memset(tmp_str, 0, max_url_size);
  433. memset(tmp_str_2, 0, max_url_size);
  434. ff_make_absolute_url(tmp_str_2, max_url_size, tmp_str, text);
  435. av_strlcpy(tmp_str, tmp_str_2, max_url_size);
  436. xmlFree(text);
  437. }
  438. }
  439. }
  440. if (val)
  441. ff_make_absolute_url(tmp_str, max_url_size, tmp_str, val);
  442. if (rep_id_val) {
  443. url = av_strireplace(tmp_str, "$RepresentationID$", (const char*)rep_id_val);
  444. if (!url) {
  445. goto end;
  446. }
  447. av_strlcpy(tmp_str, url, max_url_size);
  448. }
  449. if (rep_bandwidth_val && tmp_str[0] != '\0') {
  450. // free any previously assigned url before reassigning
  451. av_free(url);
  452. url = av_strireplace(tmp_str, "$Bandwidth$", (const char*)rep_bandwidth_val);
  453. if (!url) {
  454. goto end;
  455. }
  456. }
  457. end:
  458. av_free(tmp_str);
  459. av_free(tmp_str_2);
  460. return url;
  461. }
  462. static char *get_val_from_nodes_tab(xmlNodePtr *nodes, const int n_nodes, const char *attrname)
  463. {
  464. int i;
  465. char *val;
  466. for (i = 0; i < n_nodes; ++i) {
  467. if (nodes[i]) {
  468. val = xmlGetProp(nodes[i], attrname);
  469. if (val)
  470. return val;
  471. }
  472. }
  473. return NULL;
  474. }
  475. static xmlNodePtr find_child_node_by_name(xmlNodePtr rootnode, const char *nodename)
  476. {
  477. xmlNodePtr node = rootnode;
  478. if (!node) {
  479. return NULL;
  480. }
  481. node = xmlFirstElementChild(node);
  482. while (node) {
  483. if (!av_strcasecmp(node->name, nodename)) {
  484. return node;
  485. }
  486. node = xmlNextElementSibling(node);
  487. }
  488. return NULL;
  489. }
  490. static enum AVMediaType get_content_type(xmlNodePtr node)
  491. {
  492. enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
  493. int i = 0;
  494. const char *attr;
  495. char *val = NULL;
  496. if (node) {
  497. for (i = 0; i < 2; i++) {
  498. attr = i ? "mimeType" : "contentType";
  499. val = xmlGetProp(node, attr);
  500. if (val) {
  501. if (av_stristr((const char *)val, "video")) {
  502. type = AVMEDIA_TYPE_VIDEO;
  503. } else if (av_stristr((const char *)val, "audio")) {
  504. type = AVMEDIA_TYPE_AUDIO;
  505. } else if (av_stristr((const char *)val, "text")) {
  506. type = AVMEDIA_TYPE_SUBTITLE;
  507. }
  508. xmlFree(val);
  509. }
  510. }
  511. }
  512. return type;
  513. }
  514. static struct fragment * get_Fragment(char *range)
  515. {
  516. struct fragment * seg = av_mallocz(sizeof(struct fragment));
  517. if (!seg)
  518. return NULL;
  519. seg->size = -1;
  520. if (range) {
  521. char *str_end_offset;
  522. char *str_offset = av_strtok(range, "-", &str_end_offset);
  523. seg->url_offset = strtoll(str_offset, NULL, 10);
  524. seg->size = strtoll(str_end_offset, NULL, 10) - seg->url_offset;
  525. }
  526. return seg;
  527. }
  528. static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representation *rep,
  529. xmlNodePtr fragmenturl_node,
  530. xmlNodePtr *baseurl_nodes,
  531. char *rep_id_val,
  532. char *rep_bandwidth_val)
  533. {
  534. DASHContext *c = s->priv_data;
  535. char *initialization_val = NULL;
  536. char *media_val = NULL;
  537. char *range_val = NULL;
  538. int max_url_size = c ? c->max_url_size: MAX_URL_SIZE;
  539. if (!av_strcasecmp(fragmenturl_node->name, (const char *)"Initialization")) {
  540. initialization_val = xmlGetProp(fragmenturl_node, "sourceURL");
  541. range_val = xmlGetProp(fragmenturl_node, "range");
  542. if (initialization_val || range_val) {
  543. rep->init_section = get_Fragment(range_val);
  544. if (!rep->init_section) {
  545. xmlFree(initialization_val);
  546. xmlFree(range_val);
  547. return AVERROR(ENOMEM);
  548. }
  549. rep->init_section->url = get_content_url(baseurl_nodes, 4,
  550. max_url_size,
  551. rep_id_val,
  552. rep_bandwidth_val,
  553. initialization_val);
  554. if (!rep->init_section->url) {
  555. av_free(rep->init_section);
  556. xmlFree(initialization_val);
  557. xmlFree(range_val);
  558. return AVERROR(ENOMEM);
  559. }
  560. xmlFree(initialization_val);
  561. xmlFree(range_val);
  562. }
  563. } else if (!av_strcasecmp(fragmenturl_node->name, (const char *)"SegmentURL")) {
  564. media_val = xmlGetProp(fragmenturl_node, "media");
  565. range_val = xmlGetProp(fragmenturl_node, "mediaRange");
  566. if (media_val || range_val) {
  567. struct fragment *seg = get_Fragment(range_val);
  568. if (!seg) {
  569. xmlFree(media_val);
  570. xmlFree(range_val);
  571. return AVERROR(ENOMEM);
  572. }
  573. seg->url = get_content_url(baseurl_nodes, 4,
  574. max_url_size,
  575. rep_id_val,
  576. rep_bandwidth_val,
  577. media_val);
  578. if (!seg->url) {
  579. av_free(seg);
  580. xmlFree(media_val);
  581. xmlFree(range_val);
  582. return AVERROR(ENOMEM);
  583. }
  584. dynarray_add(&rep->fragments, &rep->n_fragments, seg);
  585. xmlFree(media_val);
  586. xmlFree(range_val);
  587. }
  588. }
  589. return 0;
  590. }
  591. static int parse_manifest_segmenttimeline(AVFormatContext *s, struct representation *rep,
  592. xmlNodePtr fragment_timeline_node)
  593. {
  594. xmlAttrPtr attr = NULL;
  595. char *val = NULL;
  596. if (!av_strcasecmp(fragment_timeline_node->name, (const char *)"S")) {
  597. struct timeline *tml = av_mallocz(sizeof(struct timeline));
  598. if (!tml) {
  599. return AVERROR(ENOMEM);
  600. }
  601. attr = fragment_timeline_node->properties;
  602. while (attr) {
  603. val = xmlGetProp(fragment_timeline_node, attr->name);
  604. if (!val) {
  605. av_log(s, AV_LOG_WARNING, "parse_manifest_segmenttimeline attr->name = %s val is NULL\n", attr->name);
  606. continue;
  607. }
  608. if (!av_strcasecmp(attr->name, (const char *)"t")) {
  609. tml->starttime = (int64_t)strtoll(val, NULL, 10);
  610. } else if (!av_strcasecmp(attr->name, (const char *)"r")) {
  611. tml->repeat =(int64_t) strtoll(val, NULL, 10);
  612. } else if (!av_strcasecmp(attr->name, (const char *)"d")) {
  613. tml->duration = (int64_t)strtoll(val, NULL, 10);
  614. }
  615. attr = attr->next;
  616. xmlFree(val);
  617. }
  618. dynarray_add(&rep->timelines, &rep->n_timelines, tml);
  619. }
  620. return 0;
  621. }
  622. static int resolve_content_path(AVFormatContext *s, const char *url, int *max_url_size, xmlNodePtr *baseurl_nodes, int n_baseurl_nodes)
  623. {
  624. char *tmp_str = NULL;
  625. char *path = NULL;
  626. char *mpdName = NULL;
  627. xmlNodePtr node = NULL;
  628. char *baseurl = NULL;
  629. char *root_url = NULL;
  630. char *text = NULL;
  631. char *tmp = NULL;
  632. int isRootHttp = 0;
  633. char token ='/';
  634. int start = 0;
  635. int rootId = 0;
  636. int updated = 0;
  637. int size = 0;
  638. int i;
  639. int tmp_max_url_size = strlen(url);
  640. for (i = n_baseurl_nodes-1; i >= 0 ; i--) {
  641. text = xmlNodeGetContent(baseurl_nodes[i]);
  642. if (!text)
  643. continue;
  644. tmp_max_url_size += strlen(text);
  645. if (ishttp(text)) {
  646. xmlFree(text);
  647. break;
  648. }
  649. xmlFree(text);
  650. }
  651. tmp_max_url_size = aligned(tmp_max_url_size);
  652. text = av_mallocz(tmp_max_url_size);
  653. if (!text) {
  654. updated = AVERROR(ENOMEM);
  655. goto end;
  656. }
  657. av_strlcpy(text, url, strlen(url)+1);
  658. tmp = text;
  659. while (mpdName = av_strtok(tmp, "/", &tmp)) {
  660. size = strlen(mpdName);
  661. }
  662. av_free(text);
  663. path = av_mallocz(tmp_max_url_size);
  664. tmp_str = av_mallocz(tmp_max_url_size);
  665. if (!tmp_str || !path) {
  666. updated = AVERROR(ENOMEM);
  667. goto end;
  668. }
  669. av_strlcpy (path, url, strlen(url) - size + 1);
  670. for (rootId = n_baseurl_nodes - 1; rootId > 0; rootId --) {
  671. if (!(node = baseurl_nodes[rootId])) {
  672. continue;
  673. }
  674. text = xmlNodeGetContent(node);
  675. if (ishttp(text)) {
  676. xmlFree(text);
  677. break;
  678. }
  679. xmlFree(text);
  680. }
  681. node = baseurl_nodes[rootId];
  682. baseurl = xmlNodeGetContent(node);
  683. root_url = (av_strcasecmp(baseurl, "")) ? baseurl : path;
  684. if (node) {
  685. xmlNodeSetContent(node, root_url);
  686. updated = 1;
  687. }
  688. size = strlen(root_url);
  689. isRootHttp = ishttp(root_url);
  690. if (root_url[size - 1] != token) {
  691. av_strlcat(root_url, "/", size + 2);
  692. size += 2;
  693. }
  694. for (i = 0; i < n_baseurl_nodes; ++i) {
  695. if (i == rootId) {
  696. continue;
  697. }
  698. text = xmlNodeGetContent(baseurl_nodes[i]);
  699. if (text && !av_strstart(text, "/", NULL)) {
  700. memset(tmp_str, 0, strlen(tmp_str));
  701. if (!ishttp(text) && isRootHttp) {
  702. av_strlcpy(tmp_str, root_url, size + 1);
  703. }
  704. start = (text[0] == token);
  705. if (start && av_stristr(tmp_str, text)) {
  706. char *p = tmp_str;
  707. if (!av_strncasecmp(tmp_str, "http://", 7)) {
  708. p += 7;
  709. } else if (!av_strncasecmp(tmp_str, "https://", 8)) {
  710. p += 8;
  711. }
  712. p = strchr(p, '/');
  713. memset(p + 1, 0, strlen(p));
  714. }
  715. av_strlcat(tmp_str, text + start, tmp_max_url_size);
  716. xmlNodeSetContent(baseurl_nodes[i], tmp_str);
  717. updated = 1;
  718. xmlFree(text);
  719. }
  720. }
  721. end:
  722. if (tmp_max_url_size > *max_url_size) {
  723. *max_url_size = tmp_max_url_size;
  724. }
  725. av_free(path);
  726. av_free(tmp_str);
  727. xmlFree(baseurl);
  728. return updated;
  729. }
  730. static int parse_manifest_representation(AVFormatContext *s, const char *url,
  731. xmlNodePtr node,
  732. xmlNodePtr adaptionset_node,
  733. xmlNodePtr mpd_baseurl_node,
  734. xmlNodePtr period_baseurl_node,
  735. xmlNodePtr period_segmenttemplate_node,
  736. xmlNodePtr period_segmentlist_node,
  737. xmlNodePtr fragment_template_node,
  738. xmlNodePtr content_component_node,
  739. xmlNodePtr adaptionset_baseurl_node,
  740. xmlNodePtr adaptionset_segmentlist_node,
  741. xmlNodePtr adaptionset_supplementalproperty_node)
  742. {
  743. int32_t ret = 0;
  744. int32_t subtitle_rep_idx = 0;
  745. int32_t audio_rep_idx = 0;
  746. int32_t video_rep_idx = 0;
  747. DASHContext *c = s->priv_data;
  748. struct representation *rep = NULL;
  749. struct fragment *seg = NULL;
  750. xmlNodePtr representation_segmenttemplate_node = NULL;
  751. xmlNodePtr representation_baseurl_node = NULL;
  752. xmlNodePtr representation_segmentlist_node = NULL;
  753. xmlNodePtr segmentlists_tab[3];
  754. xmlNodePtr fragment_timeline_node = NULL;
  755. xmlNodePtr fragment_templates_tab[5];
  756. char *duration_val = NULL;
  757. char *presentation_timeoffset_val = NULL;
  758. char *startnumber_val = NULL;
  759. char *timescale_val = NULL;
  760. char *initialization_val = NULL;
  761. char *media_val = NULL;
  762. char *val = NULL;
  763. xmlNodePtr baseurl_nodes[4];
  764. xmlNodePtr representation_node = node;
  765. char *rep_id_val = xmlGetProp(representation_node, "id");
  766. char *rep_bandwidth_val = xmlGetProp(representation_node, "bandwidth");
  767. char *rep_framerate_val = xmlGetProp(representation_node, "frameRate");
  768. enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
  769. // try get information from representation
  770. if (type == AVMEDIA_TYPE_UNKNOWN)
  771. type = get_content_type(representation_node);
  772. // try get information from contentComponen
  773. if (type == AVMEDIA_TYPE_UNKNOWN)
  774. type = get_content_type(content_component_node);
  775. // try get information from adaption set
  776. if (type == AVMEDIA_TYPE_UNKNOWN)
  777. type = get_content_type(adaptionset_node);
  778. if (type == AVMEDIA_TYPE_UNKNOWN) {
  779. av_log(s, AV_LOG_VERBOSE, "Parsing '%s' - skipp not supported representation type\n", url);
  780. } else if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO || type == AVMEDIA_TYPE_SUBTITLE) {
  781. // convert selected representation to our internal struct
  782. rep = av_mallocz(sizeof(struct representation));
  783. if (!rep) {
  784. ret = AVERROR(ENOMEM);
  785. goto end;
  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 end;
  801. }
  802. if (representation_segmenttemplate_node || fragment_template_node || period_segmenttemplate_node) {
  803. fragment_timeline_node = NULL;
  804. fragment_templates_tab[0] = representation_segmenttemplate_node;
  805. fragment_templates_tab[1] = adaptionset_segmentlist_node;
  806. fragment_templates_tab[2] = fragment_template_node;
  807. fragment_templates_tab[3] = period_segmenttemplate_node;
  808. fragment_templates_tab[4] = period_segmentlist_node;
  809. presentation_timeoffset_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "presentationTimeOffset");
  810. duration_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "duration");
  811. startnumber_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "startNumber");
  812. timescale_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "timescale");
  813. initialization_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "initialization");
  814. media_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "media");
  815. if (initialization_val) {
  816. rep->init_section = av_mallocz(sizeof(struct fragment));
  817. if (!rep->init_section) {
  818. av_free(rep);
  819. ret = AVERROR(ENOMEM);
  820. goto end;
  821. }
  822. c->max_url_size = aligned(c->max_url_size + strlen(initialization_val));
  823. rep->init_section->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, initialization_val);
  824. if (!rep->init_section->url) {
  825. av_free(rep->init_section);
  826. av_free(rep);
  827. ret = AVERROR(ENOMEM);
  828. goto end;
  829. }
  830. rep->init_section->size = -1;
  831. xmlFree(initialization_val);
  832. }
  833. if (media_val) {
  834. c->max_url_size = aligned(c->max_url_size + strlen(media_val));
  835. rep->url_template = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, media_val);
  836. xmlFree(media_val);
  837. }
  838. if (presentation_timeoffset_val) {
  839. rep->presentation_timeoffset = (int64_t) strtoll(presentation_timeoffset_val, NULL, 10);
  840. av_log(s, AV_LOG_TRACE, "rep->presentation_timeoffset = [%"PRId64"]\n", rep->presentation_timeoffset);
  841. xmlFree(presentation_timeoffset_val);
  842. }
  843. if (duration_val) {
  844. rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
  845. av_log(s, AV_LOG_TRACE, "rep->fragment_duration = [%"PRId64"]\n", rep->fragment_duration);
  846. xmlFree(duration_val);
  847. }
  848. if (timescale_val) {
  849. rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
  850. av_log(s, AV_LOG_TRACE, "rep->fragment_timescale = [%"PRId64"]\n", rep->fragment_timescale);
  851. xmlFree(timescale_val);
  852. }
  853. if (startnumber_val) {
  854. rep->start_number = rep->first_seq_no = (int64_t) strtoll(startnumber_val, NULL, 10);
  855. av_log(s, AV_LOG_TRACE, "rep->first_seq_no = [%"PRId64"]\n", rep->first_seq_no);
  856. xmlFree(startnumber_val);
  857. }
  858. if (adaptionset_supplementalproperty_node) {
  859. if (!av_strcasecmp(xmlGetProp(adaptionset_supplementalproperty_node,"schemeIdUri"), "http://dashif.org/guidelines/last-segment-number")) {
  860. val = xmlGetProp(adaptionset_supplementalproperty_node,"value");
  861. if (!val) {
  862. av_log(s, AV_LOG_ERROR, "Missing value attribute in adaptionset_supplementalproperty_node\n");
  863. } else {
  864. rep->last_seq_no =(int64_t) strtoll(val, NULL, 10) - 1;
  865. xmlFree(val);
  866. }
  867. }
  868. }
  869. fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
  870. if (!fragment_timeline_node)
  871. fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
  872. if (!fragment_timeline_node)
  873. fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
  874. if (!fragment_timeline_node)
  875. fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
  876. if (fragment_timeline_node) {
  877. fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
  878. while (fragment_timeline_node) {
  879. ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
  880. if (ret < 0) {
  881. return ret;
  882. }
  883. fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
  884. }
  885. }
  886. } else if (representation_baseurl_node && !representation_segmentlist_node) {
  887. seg = av_mallocz(sizeof(struct fragment));
  888. if (!seg) {
  889. ret = AVERROR(ENOMEM);
  890. goto end;
  891. }
  892. seg->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, NULL);
  893. if (!seg->url) {
  894. av_free(seg);
  895. ret = AVERROR(ENOMEM);
  896. goto end;
  897. }
  898. seg->size = -1;
  899. dynarray_add(&rep->fragments, &rep->n_fragments, seg);
  900. } else if (representation_segmentlist_node) {
  901. // TODO: https://www.brendanlong.com/the-structure-of-an-mpeg-dash-mpd.html
  902. // http://www-itec.uni-klu.ac.at/dash/ddash/mpdGenerator.php?fragmentlength=15&type=full
  903. xmlNodePtr fragmenturl_node = NULL;
  904. segmentlists_tab[0] = representation_segmentlist_node;
  905. segmentlists_tab[1] = adaptionset_segmentlist_node;
  906. segmentlists_tab[2] = period_segmentlist_node;
  907. duration_val = get_val_from_nodes_tab(segmentlists_tab, 3, "duration");
  908. timescale_val = get_val_from_nodes_tab(segmentlists_tab, 3, "timescale");
  909. startnumber_val = get_val_from_nodes_tab(segmentlists_tab, 3, "startNumber");
  910. if (duration_val) {
  911. rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
  912. av_log(s, AV_LOG_TRACE, "rep->fragment_duration = [%"PRId64"]\n", rep->fragment_duration);
  913. xmlFree(duration_val);
  914. }
  915. if (timescale_val) {
  916. rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
  917. av_log(s, AV_LOG_TRACE, "rep->fragment_timescale = [%"PRId64"]\n", rep->fragment_timescale);
  918. xmlFree(timescale_val);
  919. }
  920. if (startnumber_val) {
  921. rep->start_number = rep->first_seq_no = (int64_t) strtoll(startnumber_val, NULL, 10);
  922. av_log(s, AV_LOG_TRACE, "rep->first_seq_no = [%"PRId64"]\n", rep->first_seq_no);
  923. xmlFree(startnumber_val);
  924. }
  925. fragmenturl_node = xmlFirstElementChild(representation_segmentlist_node);
  926. while (fragmenturl_node) {
  927. ret = parse_manifest_segmenturlnode(s, rep, fragmenturl_node,
  928. baseurl_nodes,
  929. rep_id_val,
  930. rep_bandwidth_val);
  931. if (ret < 0) {
  932. return ret;
  933. }
  934. fragmenturl_node = xmlNextElementSibling(fragmenturl_node);
  935. }
  936. fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
  937. if (!fragment_timeline_node)
  938. fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
  939. if (!fragment_timeline_node)
  940. fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
  941. if (!fragment_timeline_node)
  942. fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
  943. if (fragment_timeline_node) {
  944. fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
  945. while (fragment_timeline_node) {
  946. ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
  947. if (ret < 0) {
  948. return ret;
  949. }
  950. fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
  951. }
  952. }
  953. } else {
  954. free_representation(rep);
  955. rep = NULL;
  956. av_log(s, AV_LOG_ERROR, "Unknown format of Representation node id[%s] \n", (const char *)rep_id_val);
  957. }
  958. if (rep) {
  959. if (rep->fragment_duration > 0 && !rep->fragment_timescale)
  960. rep->fragment_timescale = 1;
  961. rep->bandwidth = rep_bandwidth_val ? atoi(rep_bandwidth_val) : 0;
  962. strncpy(rep->id, rep_id_val ? rep_id_val : "", sizeof(rep->id));
  963. rep->framerate = av_make_q(0, 0);
  964. if (type == AVMEDIA_TYPE_VIDEO && rep_framerate_val) {
  965. ret = av_parse_video_rate(&rep->framerate, rep_framerate_val);
  966. if (ret < 0)
  967. av_log(s, AV_LOG_VERBOSE, "Ignoring invalid frame rate '%s'\n", rep_framerate_val);
  968. }
  969. switch (type) {
  970. case AVMEDIA_TYPE_VIDEO:
  971. rep->rep_idx = video_rep_idx;
  972. dynarray_add(&c->videos, &c->n_videos, rep);
  973. break;
  974. case AVMEDIA_TYPE_AUDIO:
  975. rep->rep_idx = audio_rep_idx;
  976. dynarray_add(&c->audios, &c->n_audios, rep);
  977. break;
  978. case AVMEDIA_TYPE_SUBTITLE:
  979. rep->rep_idx = subtitle_rep_idx;
  980. dynarray_add(&c->subtitles, &c->n_subtitles, rep);
  981. break;
  982. default:
  983. av_log(s, AV_LOG_WARNING, "Unsupported the stream type %d\n", type);
  984. break;
  985. }
  986. }
  987. }
  988. video_rep_idx += type == AVMEDIA_TYPE_VIDEO;
  989. audio_rep_idx += type == AVMEDIA_TYPE_AUDIO;
  990. subtitle_rep_idx += type == AVMEDIA_TYPE_SUBTITLE;
  991. end:
  992. if (rep_id_val)
  993. xmlFree(rep_id_val);
  994. if (rep_bandwidth_val)
  995. xmlFree(rep_bandwidth_val);
  996. if (rep_framerate_val)
  997. xmlFree(rep_framerate_val);
  998. return ret;
  999. }
  1000. static int parse_manifest_adaptationset(AVFormatContext *s, const char *url,
  1001. xmlNodePtr adaptionset_node,
  1002. xmlNodePtr mpd_baseurl_node,
  1003. xmlNodePtr period_baseurl_node,
  1004. xmlNodePtr period_segmenttemplate_node,
  1005. xmlNodePtr period_segmentlist_node)
  1006. {
  1007. int ret = 0;
  1008. DASHContext *c = s->priv_data;
  1009. xmlNodePtr fragment_template_node = NULL;
  1010. xmlNodePtr content_component_node = NULL;
  1011. xmlNodePtr adaptionset_baseurl_node = NULL;
  1012. xmlNodePtr adaptionset_segmentlist_node = NULL;
  1013. xmlNodePtr adaptionset_supplementalproperty_node = NULL;
  1014. xmlNodePtr node = NULL;
  1015. c->adaptionset_contenttype_val = xmlGetProp(adaptionset_node, "contentType");
  1016. c->adaptionset_par_val = xmlGetProp(adaptionset_node, "par");
  1017. c->adaptionset_lang_val = xmlGetProp(adaptionset_node, "lang");
  1018. c->adaptionset_minbw_val = xmlGetProp(adaptionset_node, "minBandwidth");
  1019. c->adaptionset_maxbw_val = xmlGetProp(adaptionset_node, "maxBandwidth");
  1020. c->adaptionset_minwidth_val = xmlGetProp(adaptionset_node, "minWidth");
  1021. c->adaptionset_maxwidth_val = xmlGetProp(adaptionset_node, "maxWidth");
  1022. c->adaptionset_minheight_val = xmlGetProp(adaptionset_node, "minHeight");
  1023. c->adaptionset_maxheight_val = xmlGetProp(adaptionset_node, "maxHeight");
  1024. c->adaptionset_minframerate_val = xmlGetProp(adaptionset_node, "minFrameRate");
  1025. c->adaptionset_maxframerate_val = xmlGetProp(adaptionset_node, "maxFrameRate");
  1026. c->adaptionset_segmentalignment_val = xmlGetProp(adaptionset_node, "segmentAlignment");
  1027. c->adaptionset_bitstreamswitching_val = xmlGetProp(adaptionset_node, "bitstreamSwitching");
  1028. node = xmlFirstElementChild(adaptionset_node);
  1029. while (node) {
  1030. if (!av_strcasecmp(node->name, (const char *)"SegmentTemplate")) {
  1031. fragment_template_node = node;
  1032. } else if (!av_strcasecmp(node->name, (const char *)"ContentComponent")) {
  1033. content_component_node = node;
  1034. } else if (!av_strcasecmp(node->name, (const char *)"BaseURL")) {
  1035. adaptionset_baseurl_node = node;
  1036. } else if (!av_strcasecmp(node->name, (const char *)"SegmentList")) {
  1037. adaptionset_segmentlist_node = node;
  1038. } else if (!av_strcasecmp(node->name, (const char *)"SupplementalProperty")) {
  1039. adaptionset_supplementalproperty_node = node;
  1040. } else if (!av_strcasecmp(node->name, (const char *)"Representation")) {
  1041. ret = parse_manifest_representation(s, url, node,
  1042. adaptionset_node,
  1043. mpd_baseurl_node,
  1044. period_baseurl_node,
  1045. period_segmenttemplate_node,
  1046. period_segmentlist_node,
  1047. fragment_template_node,
  1048. content_component_node,
  1049. adaptionset_baseurl_node,
  1050. adaptionset_segmentlist_node,
  1051. adaptionset_supplementalproperty_node);
  1052. if (ret < 0) {
  1053. return ret;
  1054. }
  1055. }
  1056. node = xmlNextElementSibling(node);
  1057. }
  1058. return 0;
  1059. }
  1060. static int parse_programinformation(AVFormatContext *s, xmlNodePtr node)
  1061. {
  1062. xmlChar *val = NULL;
  1063. node = xmlFirstElementChild(node);
  1064. while (node) {
  1065. if (!av_strcasecmp(node->name, "Title")) {
  1066. val = xmlNodeGetContent(node);
  1067. if (val) {
  1068. av_dict_set(&s->metadata, "Title", val, 0);
  1069. }
  1070. } else if (!av_strcasecmp(node->name, "Source")) {
  1071. val = xmlNodeGetContent(node);
  1072. if (val) {
  1073. av_dict_set(&s->metadata, "Source", val, 0);
  1074. }
  1075. } else if (!av_strcasecmp(node->name, "Copyright")) {
  1076. val = xmlNodeGetContent(node);
  1077. if (val) {
  1078. av_dict_set(&s->metadata, "Copyright", val, 0);
  1079. }
  1080. }
  1081. node = xmlNextElementSibling(node);
  1082. xmlFree(val);
  1083. val = NULL;
  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 = s->probesize > 0 ? s->probesize : 1024 * 4;
  1736. pls->ctx->max_analyze_duration = s->max_analyze_duration > 0 ? s->max_analyze_duration : 4 * AV_TIME_BASE;
  1737. 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. ff_format_io_close(pls->parent, &pls->input);
  1949. av_log(s, AV_LOG_INFO, "No longer receiving stream_index %d\n", pls->stream_index);
  1950. }
  1951. }
  1952. }
  1953. static int dash_read_packet(AVFormatContext *s, AVPacket *pkt)
  1954. {
  1955. DASHContext *c = s->priv_data;
  1956. int ret = 0, i;
  1957. int64_t mints = 0;
  1958. struct representation *cur = NULL;
  1959. struct representation *rep = NULL;
  1960. recheck_discard_flags(s, c->videos, c->n_videos);
  1961. recheck_discard_flags(s, c->audios, c->n_audios);
  1962. recheck_discard_flags(s, c->subtitles, c->n_subtitles);
  1963. for (i = 0; i < c->n_videos; i++) {
  1964. rep = c->videos[i];
  1965. if (!rep->ctx)
  1966. continue;
  1967. if (!cur || rep->cur_timestamp < mints) {
  1968. cur = rep;
  1969. mints = rep->cur_timestamp;
  1970. }
  1971. }
  1972. for (i = 0; i < c->n_audios; i++) {
  1973. rep = c->audios[i];
  1974. if (!rep->ctx)
  1975. continue;
  1976. if (!cur || rep->cur_timestamp < mints) {
  1977. cur = rep;
  1978. mints = rep->cur_timestamp;
  1979. }
  1980. }
  1981. for (i = 0; i < c->n_subtitles; i++) {
  1982. rep = c->subtitles[i];
  1983. if (!rep->ctx)
  1984. continue;
  1985. if (!cur || rep->cur_timestamp < mints) {
  1986. cur = rep;
  1987. mints = rep->cur_timestamp;
  1988. }
  1989. }
  1990. if (!cur) {
  1991. return AVERROR_INVALIDDATA;
  1992. }
  1993. while (!ff_check_interrupt(c->interrupt_callback) && !ret) {
  1994. ret = av_read_frame(cur->ctx, pkt);
  1995. if (ret >= 0) {
  1996. /* If we got a packet, return it */
  1997. cur->cur_timestamp = av_rescale(pkt->pts, (int64_t)cur->ctx->streams[0]->time_base.num * 90000, cur->ctx->streams[0]->time_base.den);
  1998. pkt->stream_index = cur->stream_index;
  1999. return 0;
  2000. }
  2001. if (cur->is_restart_needed) {
  2002. cur->cur_seg_offset = 0;
  2003. cur->init_sec_buf_read_offset = 0;
  2004. ff_format_io_close(cur->parent, &cur->input);
  2005. ret = reopen_demux_for_component(s, cur);
  2006. cur->is_restart_needed = 0;
  2007. }
  2008. }
  2009. return AVERROR_EOF;
  2010. }
  2011. static int dash_close(AVFormatContext *s)
  2012. {
  2013. DASHContext *c = s->priv_data;
  2014. free_audio_list(c);
  2015. free_video_list(c);
  2016. av_dict_free(&c->avio_opts);
  2017. av_freep(&c->base_url);
  2018. return 0;
  2019. }
  2020. static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos_msec, int flags, int dry_run)
  2021. {
  2022. int ret = 0;
  2023. int i = 0;
  2024. int j = 0;
  2025. int64_t duration = 0;
  2026. av_log(pls->parent, AV_LOG_VERBOSE, "DASH seek pos[%"PRId64"ms], playlist %d%s\n",
  2027. seek_pos_msec, pls->rep_idx, dry_run ? " (dry)" : "");
  2028. // single fragment mode
  2029. if (pls->n_fragments == 1) {
  2030. pls->cur_timestamp = 0;
  2031. pls->cur_seg_offset = 0;
  2032. if (dry_run)
  2033. return 0;
  2034. ff_read_frame_flush(pls->ctx);
  2035. return av_seek_frame(pls->ctx, -1, seek_pos_msec * 1000, flags);
  2036. }
  2037. ff_format_io_close(pls->parent, &pls->input);
  2038. // find the nearest fragment
  2039. if (pls->n_timelines > 0 && pls->fragment_timescale > 0) {
  2040. int64_t num = pls->first_seq_no;
  2041. av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline start n_timelines[%d] "
  2042. "last_seq_no[%"PRId64"], playlist %d.\n",
  2043. (int)pls->n_timelines, (int64_t)pls->last_seq_no, (int)pls->rep_idx);
  2044. for (i = 0; i < pls->n_timelines; i++) {
  2045. if (pls->timelines[i]->starttime > 0) {
  2046. duration = pls->timelines[i]->starttime;
  2047. }
  2048. duration += pls->timelines[i]->duration;
  2049. if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
  2050. goto set_seq_num;
  2051. }
  2052. for (j = 0; j < pls->timelines[i]->repeat; j++) {
  2053. duration += pls->timelines[i]->duration;
  2054. num++;
  2055. if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
  2056. goto set_seq_num;
  2057. }
  2058. }
  2059. num++;
  2060. }
  2061. set_seq_num:
  2062. pls->cur_seq_no = num > pls->last_seq_no ? pls->last_seq_no : num;
  2063. av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline end cur_seq_no[%"PRId64"], playlist %d.\n",
  2064. (int64_t)pls->cur_seq_no, (int)pls->rep_idx);
  2065. } else if (pls->fragment_duration > 0) {
  2066. pls->cur_seq_no = pls->first_seq_no + ((seek_pos_msec * pls->fragment_timescale) / pls->fragment_duration) / 1000;
  2067. } else {
  2068. av_log(pls->parent, AV_LOG_ERROR, "dash_seek missing timeline or fragment_duration\n");
  2069. pls->cur_seq_no = pls->first_seq_no;
  2070. }
  2071. pls->cur_timestamp = 0;
  2072. pls->cur_seg_offset = 0;
  2073. pls->init_sec_buf_read_offset = 0;
  2074. ret = dry_run ? 0 : reopen_demux_for_component(s, pls);
  2075. return ret;
  2076. }
  2077. static int dash_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  2078. {
  2079. int ret = 0, i;
  2080. DASHContext *c = s->priv_data;
  2081. int64_t seek_pos_msec = av_rescale_rnd(timestamp, 1000,
  2082. s->streams[stream_index]->time_base.den,
  2083. flags & AVSEEK_FLAG_BACKWARD ?
  2084. AV_ROUND_DOWN : AV_ROUND_UP);
  2085. if ((flags & AVSEEK_FLAG_BYTE) || c->is_live)
  2086. return AVERROR(ENOSYS);
  2087. /* Seek in discarded streams with dry_run=1 to avoid reopening them */
  2088. for (i = 0; i < c->n_videos; i++) {
  2089. if (!ret)
  2090. ret = dash_seek(s, c->videos[i], seek_pos_msec, flags, !c->videos[i]->ctx);
  2091. }
  2092. for (i = 0; i < c->n_audios; i++) {
  2093. if (!ret)
  2094. ret = dash_seek(s, c->audios[i], seek_pos_msec, flags, !c->audios[i]->ctx);
  2095. }
  2096. for (i = 0; i < c->n_subtitles; i++) {
  2097. if (!ret)
  2098. ret = dash_seek(s, c->subtitles[i], seek_pos_msec, flags, !c->subtitles[i]->ctx);
  2099. }
  2100. return ret;
  2101. }
  2102. static int dash_probe(const AVProbeData *p)
  2103. {
  2104. if (!av_stristr(p->buf, "<MPD"))
  2105. return 0;
  2106. if (av_stristr(p->buf, "dash:profile:isoff-on-demand:2011") ||
  2107. av_stristr(p->buf, "dash:profile:isoff-live:2011") ||
  2108. av_stristr(p->buf, "dash:profile:isoff-live:2012") ||
  2109. av_stristr(p->buf, "dash:profile:isoff-main:2011")) {
  2110. return AVPROBE_SCORE_MAX;
  2111. }
  2112. if (av_stristr(p->buf, "dash:profile")) {
  2113. return AVPROBE_SCORE_MAX;
  2114. }
  2115. return 0;
  2116. }
  2117. #define OFFSET(x) offsetof(DASHContext, x)
  2118. #define FLAGS AV_OPT_FLAG_DECODING_PARAM
  2119. static const AVOption dash_options[] = {
  2120. {"allowed_extensions", "List of file extensions that dash is allowed to access",
  2121. OFFSET(allowed_extensions), AV_OPT_TYPE_STRING,
  2122. {.str = "aac,m4a,m4s,m4v,mov,mp4,webm"},
  2123. INT_MIN, INT_MAX, FLAGS},
  2124. {NULL}
  2125. };
  2126. static const AVClass dash_class = {
  2127. .class_name = "dash",
  2128. .item_name = av_default_item_name,
  2129. .option = dash_options,
  2130. .version = LIBAVUTIL_VERSION_INT,
  2131. };
  2132. AVInputFormat ff_dash_demuxer = {
  2133. .name = "dash",
  2134. .long_name = NULL_IF_CONFIG_SMALL("Dynamic Adaptive Streaming over HTTP"),
  2135. .priv_class = &dash_class,
  2136. .priv_data_size = sizeof(DASHContext),
  2137. .read_probe = dash_probe,
  2138. .read_header = dash_read_header,
  2139. .read_packet = dash_read_packet,
  2140. .read_close = dash_close,
  2141. .read_seek = dash_read_seek,
  2142. .flags = AVFMT_NO_BYTE_SEEK,
  2143. };