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.

2407 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. val = NULL;
  1085. }
  1086. return 0;
  1087. }
  1088. static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
  1089. {
  1090. DASHContext *c = s->priv_data;
  1091. int ret = 0;
  1092. int close_in = 0;
  1093. uint8_t *new_url = NULL;
  1094. int64_t filesize = 0;
  1095. char *buffer = NULL;
  1096. AVDictionary *opts = NULL;
  1097. xmlDoc *doc = NULL;
  1098. xmlNodePtr root_element = NULL;
  1099. xmlNodePtr node = NULL;
  1100. xmlNodePtr period_node = NULL;
  1101. xmlNodePtr tmp_node = NULL;
  1102. xmlNodePtr mpd_baseurl_node = NULL;
  1103. xmlNodePtr period_baseurl_node = NULL;
  1104. xmlNodePtr period_segmenttemplate_node = NULL;
  1105. xmlNodePtr period_segmentlist_node = NULL;
  1106. xmlNodePtr adaptionset_node = NULL;
  1107. xmlAttrPtr attr = NULL;
  1108. char *val = NULL;
  1109. uint32_t period_duration_sec = 0;
  1110. uint32_t period_start_sec = 0;
  1111. if (!in) {
  1112. close_in = 1;
  1113. av_dict_copy(&opts, c->avio_opts, 0);
  1114. ret = avio_open2(&in, url, AVIO_FLAG_READ, c->interrupt_callback, &opts);
  1115. av_dict_free(&opts);
  1116. if (ret < 0)
  1117. return ret;
  1118. }
  1119. if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, &new_url) >= 0) {
  1120. c->base_url = av_strdup(new_url);
  1121. } else {
  1122. c->base_url = av_strdup(url);
  1123. }
  1124. filesize = avio_size(in);
  1125. if (filesize <= 0) {
  1126. filesize = 8 * 1024;
  1127. }
  1128. buffer = av_mallocz(filesize);
  1129. if (!buffer) {
  1130. av_free(c->base_url);
  1131. return AVERROR(ENOMEM);
  1132. }
  1133. filesize = avio_read(in, buffer, filesize);
  1134. if (filesize <= 0) {
  1135. av_log(s, AV_LOG_ERROR, "Unable to read to offset '%s'\n", url);
  1136. ret = AVERROR_INVALIDDATA;
  1137. } else {
  1138. LIBXML_TEST_VERSION
  1139. doc = xmlReadMemory(buffer, filesize, c->base_url, NULL, 0);
  1140. root_element = xmlDocGetRootElement(doc);
  1141. node = root_element;
  1142. if (!node) {
  1143. ret = AVERROR_INVALIDDATA;
  1144. av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing root node\n", url);
  1145. goto cleanup;
  1146. }
  1147. if (node->type != XML_ELEMENT_NODE ||
  1148. av_strcasecmp(node->name, (const char *)"MPD")) {
  1149. ret = AVERROR_INVALIDDATA;
  1150. av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - wrong root node name[%s] type[%d]\n", url, node->name, (int)node->type);
  1151. goto cleanup;
  1152. }
  1153. val = xmlGetProp(node, "type");
  1154. if (!val) {
  1155. av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing type attrib\n", url);
  1156. ret = AVERROR_INVALIDDATA;
  1157. goto cleanup;
  1158. }
  1159. if (!av_strcasecmp(val, (const char *)"dynamic"))
  1160. c->is_live = 1;
  1161. xmlFree(val);
  1162. attr = node->properties;
  1163. while (attr) {
  1164. val = xmlGetProp(node, attr->name);
  1165. if (!av_strcasecmp(attr->name, (const char *)"availabilityStartTime")) {
  1166. c->availability_start_time = get_utc_date_time_insec(s, (const char *)val);
  1167. av_log(s, AV_LOG_TRACE, "c->availability_start_time = [%"PRId64"]\n", c->availability_start_time);
  1168. } else if (!av_strcasecmp(attr->name, (const char *)"availabilityEndTime")) {
  1169. c->availability_end_time = get_utc_date_time_insec(s, (const char *)val);
  1170. av_log(s, AV_LOG_TRACE, "c->availability_end_time = [%"PRId64"]\n", c->availability_end_time);
  1171. } else if (!av_strcasecmp(attr->name, (const char *)"publishTime")) {
  1172. c->publish_time = get_utc_date_time_insec(s, (const char *)val);
  1173. av_log(s, AV_LOG_TRACE, "c->publish_time = [%"PRId64"]\n", c->publish_time);
  1174. } else if (!av_strcasecmp(attr->name, (const char *)"minimumUpdatePeriod")) {
  1175. c->minimum_update_period = get_duration_insec(s, (const char *)val);
  1176. av_log(s, AV_LOG_TRACE, "c->minimum_update_period = [%"PRId64"]\n", c->minimum_update_period);
  1177. } else if (!av_strcasecmp(attr->name, (const char *)"timeShiftBufferDepth")) {
  1178. c->time_shift_buffer_depth = get_duration_insec(s, (const char *)val);
  1179. av_log(s, AV_LOG_TRACE, "c->time_shift_buffer_depth = [%"PRId64"]\n", c->time_shift_buffer_depth);
  1180. } else if (!av_strcasecmp(attr->name, (const char *)"minBufferTime")) {
  1181. c->min_buffer_time = get_duration_insec(s, (const char *)val);
  1182. av_log(s, AV_LOG_TRACE, "c->min_buffer_time = [%"PRId64"]\n", c->min_buffer_time);
  1183. } else if (!av_strcasecmp(attr->name, (const char *)"suggestedPresentationDelay")) {
  1184. c->suggested_presentation_delay = get_duration_insec(s, (const char *)val);
  1185. av_log(s, AV_LOG_TRACE, "c->suggested_presentation_delay = [%"PRId64"]\n", c->suggested_presentation_delay);
  1186. } else if (!av_strcasecmp(attr->name, (const char *)"mediaPresentationDuration")) {
  1187. c->media_presentation_duration = get_duration_insec(s, (const char *)val);
  1188. av_log(s, AV_LOG_TRACE, "c->media_presentation_duration = [%"PRId64"]\n", c->media_presentation_duration);
  1189. }
  1190. attr = attr->next;
  1191. xmlFree(val);
  1192. }
  1193. tmp_node = find_child_node_by_name(node, "BaseURL");
  1194. if (tmp_node) {
  1195. mpd_baseurl_node = xmlCopyNode(tmp_node,1);
  1196. } else {
  1197. mpd_baseurl_node = xmlNewNode(NULL, "BaseURL");
  1198. }
  1199. // at now we can handle only one period, with the longest duration
  1200. node = xmlFirstElementChild(node);
  1201. while (node) {
  1202. if (!av_strcasecmp(node->name, (const char *)"Period")) {
  1203. period_duration_sec = 0;
  1204. period_start_sec = 0;
  1205. attr = node->properties;
  1206. while (attr) {
  1207. val = xmlGetProp(node, attr->name);
  1208. if (!av_strcasecmp(attr->name, (const char *)"duration")) {
  1209. period_duration_sec = get_duration_insec(s, (const char *)val);
  1210. } else if (!av_strcasecmp(attr->name, (const char *)"start")) {
  1211. period_start_sec = get_duration_insec(s, (const char *)val);
  1212. }
  1213. attr = attr->next;
  1214. xmlFree(val);
  1215. }
  1216. if ((period_duration_sec) >= (c->period_duration)) {
  1217. period_node = node;
  1218. c->period_duration = period_duration_sec;
  1219. c->period_start = period_start_sec;
  1220. if (c->period_start > 0)
  1221. c->media_presentation_duration = c->period_duration;
  1222. }
  1223. } else if (!av_strcasecmp(node->name, "ProgramInformation")) {
  1224. parse_programinformation(s, node);
  1225. }
  1226. node = xmlNextElementSibling(node);
  1227. }
  1228. if (!period_node) {
  1229. av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing Period node\n", url);
  1230. ret = AVERROR_INVALIDDATA;
  1231. goto cleanup;
  1232. }
  1233. adaptionset_node = xmlFirstElementChild(period_node);
  1234. while (adaptionset_node) {
  1235. if (!av_strcasecmp(adaptionset_node->name, (const char *)"BaseURL")) {
  1236. period_baseurl_node = adaptionset_node;
  1237. } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"SegmentTemplate")) {
  1238. period_segmenttemplate_node = adaptionset_node;
  1239. } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"SegmentList")) {
  1240. period_segmentlist_node = adaptionset_node;
  1241. } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"AdaptationSet")) {
  1242. parse_manifest_adaptationset(s, url, adaptionset_node, mpd_baseurl_node, period_baseurl_node, period_segmenttemplate_node, period_segmentlist_node);
  1243. }
  1244. adaptionset_node = xmlNextElementSibling(adaptionset_node);
  1245. }
  1246. cleanup:
  1247. /*free the document */
  1248. xmlFreeDoc(doc);
  1249. xmlCleanupParser();
  1250. xmlFreeNode(mpd_baseurl_node);
  1251. }
  1252. av_free(new_url);
  1253. av_free(buffer);
  1254. if (close_in) {
  1255. avio_close(in);
  1256. }
  1257. return ret;
  1258. }
  1259. static int64_t calc_cur_seg_no(AVFormatContext *s, struct representation *pls)
  1260. {
  1261. DASHContext *c = s->priv_data;
  1262. int64_t num = 0;
  1263. int64_t start_time_offset = 0;
  1264. if (c->is_live) {
  1265. if (pls->n_fragments) {
  1266. av_log(s, AV_LOG_TRACE, "in n_fragments mode\n");
  1267. num = pls->first_seq_no;
  1268. } else if (pls->n_timelines) {
  1269. av_log(s, AV_LOG_TRACE, "in n_timelines mode\n");
  1270. start_time_offset = get_segment_start_time_based_on_timeline(pls, 0xFFFFFFFF) - 60 * pls->fragment_timescale; // 60 seconds before end
  1271. num = calc_next_seg_no_from_timelines(pls, start_time_offset);
  1272. if (num == -1)
  1273. num = pls->first_seq_no;
  1274. else
  1275. num += pls->first_seq_no;
  1276. } else if (pls->fragment_duration){
  1277. av_log(s, AV_LOG_TRACE, "in fragment_duration mode fragment_timescale = %"PRId64", presentation_timeoffset = %"PRId64"\n", pls->fragment_timescale, pls->presentation_timeoffset);
  1278. if (pls->presentation_timeoffset) {
  1279. 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;
  1280. } else if (c->publish_time > 0 && !c->availability_start_time) {
  1281. if (c->min_buffer_time) {
  1282. 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;
  1283. } else {
  1284. 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;
  1285. }
  1286. } else {
  1287. num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time) - c->suggested_presentation_delay) * pls->fragment_timescale) / pls->fragment_duration;
  1288. }
  1289. }
  1290. } else {
  1291. num = pls->first_seq_no;
  1292. }
  1293. return num;
  1294. }
  1295. static int64_t calc_min_seg_no(AVFormatContext *s, struct representation *pls)
  1296. {
  1297. DASHContext *c = s->priv_data;
  1298. int64_t num = 0;
  1299. if (c->is_live && pls->fragment_duration) {
  1300. av_log(s, AV_LOG_TRACE, "in live mode\n");
  1301. 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;
  1302. } else {
  1303. num = pls->first_seq_no;
  1304. }
  1305. return num;
  1306. }
  1307. static int64_t calc_max_seg_no(struct representation *pls, DASHContext *c)
  1308. {
  1309. int64_t num = 0;
  1310. if (pls->n_fragments) {
  1311. num = pls->first_seq_no + pls->n_fragments - 1;
  1312. } else if (pls->n_timelines) {
  1313. int i = 0;
  1314. num = pls->first_seq_no + pls->n_timelines - 1;
  1315. for (i = 0; i < pls->n_timelines; i++) {
  1316. if (pls->timelines[i]->repeat == -1) {
  1317. int length_of_each_segment = pls->timelines[i]->duration / pls->fragment_timescale;
  1318. num = c->period_duration / length_of_each_segment;
  1319. } else {
  1320. num += pls->timelines[i]->repeat;
  1321. }
  1322. }
  1323. } else if (c->is_live && pls->fragment_duration) {
  1324. num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time)) * pls->fragment_timescale) / pls->fragment_duration;
  1325. } else if (pls->fragment_duration) {
  1326. num = pls->first_seq_no + (c->media_presentation_duration * pls->fragment_timescale) / pls->fragment_duration;
  1327. }
  1328. return num;
  1329. }
  1330. static void move_timelines(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
  1331. {
  1332. if (rep_dest && rep_src ) {
  1333. free_timelines_list(rep_dest);
  1334. rep_dest->timelines = rep_src->timelines;
  1335. rep_dest->n_timelines = rep_src->n_timelines;
  1336. rep_dest->first_seq_no = rep_src->first_seq_no;
  1337. rep_dest->last_seq_no = calc_max_seg_no(rep_dest, c);
  1338. rep_src->timelines = NULL;
  1339. rep_src->n_timelines = 0;
  1340. rep_dest->cur_seq_no = rep_src->cur_seq_no;
  1341. }
  1342. }
  1343. static void move_segments(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
  1344. {
  1345. if (rep_dest && rep_src ) {
  1346. free_fragment_list(rep_dest);
  1347. if (rep_src->start_number > (rep_dest->start_number + rep_dest->n_fragments))
  1348. rep_dest->cur_seq_no = 0;
  1349. else
  1350. rep_dest->cur_seq_no += rep_src->start_number - rep_dest->start_number;
  1351. rep_dest->fragments = rep_src->fragments;
  1352. rep_dest->n_fragments = rep_src->n_fragments;
  1353. rep_dest->parent = rep_src->parent;
  1354. rep_dest->last_seq_no = calc_max_seg_no(rep_dest, c);
  1355. rep_src->fragments = NULL;
  1356. rep_src->n_fragments = 0;
  1357. }
  1358. }
  1359. static int refresh_manifest(AVFormatContext *s)
  1360. {
  1361. int ret = 0, i;
  1362. DASHContext *c = s->priv_data;
  1363. // save current context
  1364. int n_videos = c->n_videos;
  1365. struct representation **videos = c->videos;
  1366. int n_audios = c->n_audios;
  1367. struct representation **audios = c->audios;
  1368. int n_subtitles = c->n_subtitles;
  1369. struct representation **subtitles = c->subtitles;
  1370. char *base_url = c->base_url;
  1371. c->base_url = NULL;
  1372. c->n_videos = 0;
  1373. c->videos = NULL;
  1374. c->n_audios = 0;
  1375. c->audios = NULL;
  1376. c->n_subtitles = 0;
  1377. c->subtitles = NULL;
  1378. ret = parse_manifest(s, s->url, NULL);
  1379. if (ret)
  1380. goto finish;
  1381. if (c->n_videos != n_videos) {
  1382. av_log(c, AV_LOG_ERROR,
  1383. "new manifest has mismatched no. of video representations, %d -> %d\n",
  1384. n_videos, c->n_videos);
  1385. return AVERROR_INVALIDDATA;
  1386. }
  1387. if (c->n_audios != n_audios) {
  1388. av_log(c, AV_LOG_ERROR,
  1389. "new manifest has mismatched no. of audio representations, %d -> %d\n",
  1390. n_audios, c->n_audios);
  1391. return AVERROR_INVALIDDATA;
  1392. }
  1393. if (c->n_subtitles != n_subtitles) {
  1394. av_log(c, AV_LOG_ERROR,
  1395. "new manifest has mismatched no. of subtitles representations, %d -> %d\n",
  1396. n_subtitles, c->n_subtitles);
  1397. return AVERROR_INVALIDDATA;
  1398. }
  1399. for (i = 0; i < n_videos; i++) {
  1400. struct representation *cur_video = videos[i];
  1401. struct representation *ccur_video = c->videos[i];
  1402. if (cur_video->timelines) {
  1403. // calc current time
  1404. int64_t currentTime = get_segment_start_time_based_on_timeline(cur_video, cur_video->cur_seq_no) / cur_video->fragment_timescale;
  1405. // update segments
  1406. ccur_video->cur_seq_no = calc_next_seg_no_from_timelines(ccur_video, currentTime * cur_video->fragment_timescale - 1);
  1407. if (ccur_video->cur_seq_no >= 0) {
  1408. move_timelines(ccur_video, cur_video, c);
  1409. }
  1410. }
  1411. if (cur_video->fragments) {
  1412. move_segments(ccur_video, cur_video, c);
  1413. }
  1414. }
  1415. for (i = 0; i < n_audios; i++) {
  1416. struct representation *cur_audio = audios[i];
  1417. struct representation *ccur_audio = c->audios[i];
  1418. if (cur_audio->timelines) {
  1419. // calc current time
  1420. int64_t currentTime = get_segment_start_time_based_on_timeline(cur_audio, cur_audio->cur_seq_no) / cur_audio->fragment_timescale;
  1421. // update segments
  1422. ccur_audio->cur_seq_no = calc_next_seg_no_from_timelines(ccur_audio, currentTime * cur_audio->fragment_timescale - 1);
  1423. if (ccur_audio->cur_seq_no >= 0) {
  1424. move_timelines(ccur_audio, cur_audio, c);
  1425. }
  1426. }
  1427. if (cur_audio->fragments) {
  1428. move_segments(ccur_audio, cur_audio, c);
  1429. }
  1430. }
  1431. finish:
  1432. // restore context
  1433. if (c->base_url)
  1434. av_free(base_url);
  1435. else
  1436. c->base_url = base_url;
  1437. if (c->subtitles)
  1438. free_subtitle_list(c);
  1439. if (c->audios)
  1440. free_audio_list(c);
  1441. if (c->videos)
  1442. free_video_list(c);
  1443. c->n_subtitles = n_subtitles;
  1444. c->subtitles = subtitles;
  1445. c->n_audios = n_audios;
  1446. c->audios = audios;
  1447. c->n_videos = n_videos;
  1448. c->videos = videos;
  1449. return ret;
  1450. }
  1451. static struct fragment *get_current_fragment(struct representation *pls)
  1452. {
  1453. int64_t min_seq_no = 0;
  1454. int64_t max_seq_no = 0;
  1455. struct fragment *seg = NULL;
  1456. struct fragment *seg_ptr = NULL;
  1457. DASHContext *c = pls->parent->priv_data;
  1458. while (( !ff_check_interrupt(c->interrupt_callback)&& pls->n_fragments > 0)) {
  1459. if (pls->cur_seq_no < pls->n_fragments) {
  1460. seg_ptr = pls->fragments[pls->cur_seq_no];
  1461. seg = av_mallocz(sizeof(struct fragment));
  1462. if (!seg) {
  1463. return NULL;
  1464. }
  1465. seg->url = av_strdup(seg_ptr->url);
  1466. if (!seg->url) {
  1467. av_free(seg);
  1468. return NULL;
  1469. }
  1470. seg->size = seg_ptr->size;
  1471. seg->url_offset = seg_ptr->url_offset;
  1472. return seg;
  1473. } else if (c->is_live) {
  1474. refresh_manifest(pls->parent);
  1475. } else {
  1476. break;
  1477. }
  1478. }
  1479. if (c->is_live) {
  1480. min_seq_no = calc_min_seg_no(pls->parent, pls);
  1481. max_seq_no = calc_max_seg_no(pls, c);
  1482. if (pls->timelines || pls->fragments) {
  1483. refresh_manifest(pls->parent);
  1484. }
  1485. if (pls->cur_seq_no <= min_seq_no) {
  1486. 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);
  1487. pls->cur_seq_no = calc_cur_seg_no(pls->parent, pls);
  1488. } else if (pls->cur_seq_no > max_seq_no) {
  1489. 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);
  1490. }
  1491. seg = av_mallocz(sizeof(struct fragment));
  1492. if (!seg) {
  1493. return NULL;
  1494. }
  1495. } else if (pls->cur_seq_no <= pls->last_seq_no) {
  1496. seg = av_mallocz(sizeof(struct fragment));
  1497. if (!seg) {
  1498. return NULL;
  1499. }
  1500. }
  1501. if (seg) {
  1502. char *tmpfilename= av_mallocz(c->max_url_size);
  1503. if (!tmpfilename) {
  1504. return NULL;
  1505. }
  1506. 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));
  1507. seg->url = av_strireplace(pls->url_template, pls->url_template, tmpfilename);
  1508. if (!seg->url) {
  1509. av_log(pls->parent, AV_LOG_WARNING, "Unable to resolve template url '%s', try to use origin template\n", pls->url_template);
  1510. seg->url = av_strdup(pls->url_template);
  1511. if (!seg->url) {
  1512. av_log(pls->parent, AV_LOG_ERROR, "Cannot resolve template url '%s'\n", pls->url_template);
  1513. av_free(tmpfilename);
  1514. return NULL;
  1515. }
  1516. }
  1517. av_free(tmpfilename);
  1518. seg->size = -1;
  1519. }
  1520. return seg;
  1521. }
  1522. static int read_from_url(struct representation *pls, struct fragment *seg,
  1523. uint8_t *buf, int buf_size)
  1524. {
  1525. int ret;
  1526. /* limit read if the fragment was only a part of a file */
  1527. if (seg->size >= 0)
  1528. buf_size = FFMIN(buf_size, pls->cur_seg_size - pls->cur_seg_offset);
  1529. ret = avio_read(pls->input, buf, buf_size);
  1530. if (ret > 0)
  1531. pls->cur_seg_offset += ret;
  1532. return ret;
  1533. }
  1534. static int open_input(DASHContext *c, struct representation *pls, struct fragment *seg)
  1535. {
  1536. AVDictionary *opts = NULL;
  1537. char *url = NULL;
  1538. int ret = 0;
  1539. url = av_mallocz(c->max_url_size);
  1540. if (!url) {
  1541. ret = AVERROR(ENOMEM);
  1542. goto cleanup;
  1543. }
  1544. if (seg->size >= 0) {
  1545. /* try to restrict the HTTP request to the part we want
  1546. * (if this is in fact a HTTP request) */
  1547. av_dict_set_int(&opts, "offset", seg->url_offset, 0);
  1548. av_dict_set_int(&opts, "end_offset", seg->url_offset + seg->size, 0);
  1549. }
  1550. ff_make_absolute_url(url, c->max_url_size, c->base_url, seg->url);
  1551. av_log(pls->parent, AV_LOG_VERBOSE, "DASH request for url '%s', offset %"PRId64", playlist %d\n",
  1552. url, seg->url_offset, pls->rep_idx);
  1553. ret = open_url(pls->parent, &pls->input, url, c->avio_opts, opts, NULL);
  1554. cleanup:
  1555. av_free(url);
  1556. av_dict_free(&opts);
  1557. pls->cur_seg_offset = 0;
  1558. pls->cur_seg_size = seg->size;
  1559. return ret;
  1560. }
  1561. static int update_init_section(struct representation *pls)
  1562. {
  1563. static const int max_init_section_size = 1024 * 1024;
  1564. DASHContext *c = pls->parent->priv_data;
  1565. int64_t sec_size;
  1566. int64_t urlsize;
  1567. int ret;
  1568. if (!pls->init_section || pls->init_sec_buf)
  1569. return 0;
  1570. ret = open_input(c, pls, pls->init_section);
  1571. if (ret < 0) {
  1572. av_log(pls->parent, AV_LOG_WARNING,
  1573. "Failed to open an initialization section in playlist %d\n",
  1574. pls->rep_idx);
  1575. return ret;
  1576. }
  1577. if (pls->init_section->size >= 0)
  1578. sec_size = pls->init_section->size;
  1579. else if ((urlsize = avio_size(pls->input)) >= 0)
  1580. sec_size = urlsize;
  1581. else
  1582. sec_size = max_init_section_size;
  1583. av_log(pls->parent, AV_LOG_DEBUG,
  1584. "Downloading an initialization section of size %"PRId64"\n",
  1585. sec_size);
  1586. sec_size = FFMIN(sec_size, max_init_section_size);
  1587. av_fast_malloc(&pls->init_sec_buf, &pls->init_sec_buf_size, sec_size);
  1588. ret = read_from_url(pls, pls->init_section, pls->init_sec_buf,
  1589. pls->init_sec_buf_size);
  1590. ff_format_io_close(pls->parent, &pls->input);
  1591. if (ret < 0)
  1592. return ret;
  1593. pls->init_sec_data_len = ret;
  1594. pls->init_sec_buf_read_offset = 0;
  1595. return 0;
  1596. }
  1597. static int64_t seek_data(void *opaque, int64_t offset, int whence)
  1598. {
  1599. struct representation *v = opaque;
  1600. if (v->n_fragments && !v->init_sec_data_len) {
  1601. return avio_seek(v->input, offset, whence);
  1602. }
  1603. return AVERROR(ENOSYS);
  1604. }
  1605. static int read_data(void *opaque, uint8_t *buf, int buf_size)
  1606. {
  1607. int ret = 0;
  1608. struct representation *v = opaque;
  1609. DASHContext *c = v->parent->priv_data;
  1610. restart:
  1611. if (!v->input) {
  1612. free_fragment(&v->cur_seg);
  1613. v->cur_seg = get_current_fragment(v);
  1614. if (!v->cur_seg) {
  1615. ret = AVERROR_EOF;
  1616. goto end;
  1617. }
  1618. /* load/update Media Initialization Section, if any */
  1619. ret = update_init_section(v);
  1620. if (ret)
  1621. goto end;
  1622. ret = open_input(c, v, v->cur_seg);
  1623. if (ret < 0) {
  1624. if (ff_check_interrupt(c->interrupt_callback)) {
  1625. ret = AVERROR_EXIT;
  1626. goto end;
  1627. }
  1628. av_log(v->parent, AV_LOG_WARNING, "Failed to open fragment of playlist %d\n", v->rep_idx);
  1629. v->cur_seq_no++;
  1630. goto restart;
  1631. }
  1632. }
  1633. if (v->init_sec_buf_read_offset < v->init_sec_data_len) {
  1634. /* Push init section out first before first actual fragment */
  1635. int copy_size = FFMIN(v->init_sec_data_len - v->init_sec_buf_read_offset, buf_size);
  1636. memcpy(buf, v->init_sec_buf, copy_size);
  1637. v->init_sec_buf_read_offset += copy_size;
  1638. ret = copy_size;
  1639. goto end;
  1640. }
  1641. /* check the v->cur_seg, if it is null, get current and double check if the new v->cur_seg*/
  1642. if (!v->cur_seg) {
  1643. v->cur_seg = get_current_fragment(v);
  1644. }
  1645. if (!v->cur_seg) {
  1646. ret = AVERROR_EOF;
  1647. goto end;
  1648. }
  1649. ret = read_from_url(v, v->cur_seg, buf, buf_size);
  1650. if (ret > 0)
  1651. goto end;
  1652. if (c->is_live || v->cur_seq_no < v->last_seq_no) {
  1653. if (!v->is_restart_needed)
  1654. v->cur_seq_no++;
  1655. v->is_restart_needed = 1;
  1656. }
  1657. end:
  1658. return ret;
  1659. }
  1660. static int save_avio_options(AVFormatContext *s)
  1661. {
  1662. DASHContext *c = s->priv_data;
  1663. const char *opts[] = {
  1664. "headers", "user_agent", "cookies", "http_proxy", "referer", "rw_timeout", NULL };
  1665. const char **opt = opts;
  1666. uint8_t *buf = NULL;
  1667. int ret = 0;
  1668. while (*opt) {
  1669. if (av_opt_get(s->pb, *opt, AV_OPT_SEARCH_CHILDREN, &buf) >= 0) {
  1670. if (buf[0] != '\0') {
  1671. ret = av_dict_set(&c->avio_opts, *opt, buf, AV_DICT_DONT_STRDUP_VAL);
  1672. if (ret < 0) {
  1673. av_freep(&buf);
  1674. return ret;
  1675. }
  1676. } else {
  1677. av_freep(&buf);
  1678. }
  1679. }
  1680. opt++;
  1681. }
  1682. return ret;
  1683. }
  1684. static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
  1685. int flags, AVDictionary **opts)
  1686. {
  1687. av_log(s, AV_LOG_ERROR,
  1688. "A DASH playlist item '%s' referred to an external file '%s'. "
  1689. "Opening this file was forbidden for security reasons\n",
  1690. s->url, url);
  1691. return AVERROR(EPERM);
  1692. }
  1693. static void close_demux_for_component(struct representation *pls)
  1694. {
  1695. /* note: the internal buffer could have changed */
  1696. av_freep(&pls->pb.buffer);
  1697. memset(&pls->pb, 0x00, sizeof(AVIOContext));
  1698. pls->ctx->pb = NULL;
  1699. avformat_close_input(&pls->ctx);
  1700. pls->ctx = NULL;
  1701. }
  1702. static int reopen_demux_for_component(AVFormatContext *s, struct representation *pls)
  1703. {
  1704. DASHContext *c = s->priv_data;
  1705. ff_const59 AVInputFormat *in_fmt = NULL;
  1706. AVDictionary *in_fmt_opts = NULL;
  1707. uint8_t *avio_ctx_buffer = NULL;
  1708. int ret = 0, i;
  1709. if (pls->ctx) {
  1710. close_demux_for_component(pls);
  1711. }
  1712. if (ff_check_interrupt(&s->interrupt_callback)) {
  1713. ret = AVERROR_EXIT;
  1714. goto fail;
  1715. }
  1716. if (!(pls->ctx = avformat_alloc_context())) {
  1717. ret = AVERROR(ENOMEM);
  1718. goto fail;
  1719. }
  1720. avio_ctx_buffer = av_malloc(INITIAL_BUFFER_SIZE);
  1721. if (!avio_ctx_buffer ) {
  1722. ret = AVERROR(ENOMEM);
  1723. avformat_free_context(pls->ctx);
  1724. pls->ctx = NULL;
  1725. goto fail;
  1726. }
  1727. if (c->is_live) {
  1728. ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, NULL);
  1729. } else {
  1730. ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, seek_data);
  1731. }
  1732. pls->pb.seekable = 0;
  1733. if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0)
  1734. goto fail;
  1735. pls->ctx->flags = AVFMT_FLAG_CUSTOM_IO;
  1736. pls->ctx->probesize = 1024 * 4;
  1737. pls->ctx->max_analyze_duration = 4 * AV_TIME_BASE;
  1738. ret = av_probe_input_buffer(&pls->pb, &in_fmt, "", NULL, 0, 0);
  1739. if (ret < 0) {
  1740. av_log(s, AV_LOG_ERROR, "Error when loading first fragment, playlist %d\n", (int)pls->rep_idx);
  1741. avformat_free_context(pls->ctx);
  1742. pls->ctx = NULL;
  1743. goto fail;
  1744. }
  1745. pls->ctx->pb = &pls->pb;
  1746. pls->ctx->io_open = nested_io_open;
  1747. // provide additional information from mpd if available
  1748. ret = avformat_open_input(&pls->ctx, "", in_fmt, &in_fmt_opts); //pls->init_section->url
  1749. av_dict_free(&in_fmt_opts);
  1750. if (ret < 0)
  1751. goto fail;
  1752. if (pls->n_fragments) {
  1753. #if FF_API_R_FRAME_RATE
  1754. if (pls->framerate.den) {
  1755. for (i = 0; i < pls->ctx->nb_streams; i++)
  1756. pls->ctx->streams[i]->r_frame_rate = pls->framerate;
  1757. }
  1758. #endif
  1759. ret = avformat_find_stream_info(pls->ctx, NULL);
  1760. if (ret < 0)
  1761. goto fail;
  1762. }
  1763. fail:
  1764. return ret;
  1765. }
  1766. static int open_demux_for_component(AVFormatContext *s, struct representation *pls)
  1767. {
  1768. int ret = 0;
  1769. int i;
  1770. pls->parent = s;
  1771. pls->cur_seq_no = calc_cur_seg_no(s, pls);
  1772. if (!pls->last_seq_no) {
  1773. pls->last_seq_no = calc_max_seg_no(pls, s->priv_data);
  1774. }
  1775. ret = reopen_demux_for_component(s, pls);
  1776. if (ret < 0) {
  1777. goto fail;
  1778. }
  1779. for (i = 0; i < pls->ctx->nb_streams; i++) {
  1780. AVStream *st = avformat_new_stream(s, NULL);
  1781. AVStream *ist = pls->ctx->streams[i];
  1782. if (!st) {
  1783. ret = AVERROR(ENOMEM);
  1784. goto fail;
  1785. }
  1786. st->id = i;
  1787. avcodec_parameters_copy(st->codecpar, ist->codecpar);
  1788. avpriv_set_pts_info(st, ist->pts_wrap_bits, ist->time_base.num, ist->time_base.den);
  1789. }
  1790. return 0;
  1791. fail:
  1792. return ret;
  1793. }
  1794. static int is_common_init_section_exist(struct representation **pls, int n_pls)
  1795. {
  1796. struct fragment *first_init_section = pls[0]->init_section;
  1797. char *url =NULL;
  1798. int64_t url_offset = -1;
  1799. int64_t size = -1;
  1800. int i = 0;
  1801. if (first_init_section == NULL || n_pls == 0)
  1802. return 0;
  1803. url = first_init_section->url;
  1804. url_offset = first_init_section->url_offset;
  1805. size = pls[0]->init_section->size;
  1806. for (i=0;i<n_pls;i++) {
  1807. if (av_strcasecmp(pls[i]->init_section->url,url) || pls[i]->init_section->url_offset != url_offset || pls[i]->init_section->size != size) {
  1808. return 0;
  1809. }
  1810. }
  1811. return 1;
  1812. }
  1813. static int copy_init_section(struct representation *rep_dest, struct representation *rep_src)
  1814. {
  1815. rep_dest->init_sec_buf = av_mallocz(rep_src->init_sec_buf_size);
  1816. if (!rep_dest->init_sec_buf) {
  1817. av_log(rep_dest->ctx, AV_LOG_WARNING, "Cannot alloc memory for init_sec_buf\n");
  1818. return AVERROR(ENOMEM);
  1819. }
  1820. memcpy(rep_dest->init_sec_buf, rep_src->init_sec_buf, rep_src->init_sec_data_len);
  1821. rep_dest->init_sec_buf_size = rep_src->init_sec_buf_size;
  1822. rep_dest->init_sec_data_len = rep_src->init_sec_data_len;
  1823. rep_dest->cur_timestamp = rep_src->cur_timestamp;
  1824. return 0;
  1825. }
  1826. static int dash_read_header(AVFormatContext *s)
  1827. {
  1828. DASHContext *c = s->priv_data;
  1829. struct representation *rep;
  1830. int ret = 0;
  1831. int stream_index = 0;
  1832. int i;
  1833. c->interrupt_callback = &s->interrupt_callback;
  1834. if ((ret = save_avio_options(s)) < 0)
  1835. goto fail;
  1836. if ((ret = parse_manifest(s, s->url, s->pb)) < 0)
  1837. goto fail;
  1838. /* If this isn't a live stream, fill the total duration of the
  1839. * stream. */
  1840. if (!c->is_live) {
  1841. s->duration = (int64_t) c->media_presentation_duration * AV_TIME_BASE;
  1842. } else {
  1843. av_dict_set(&c->avio_opts, "seekable", "0", 0);
  1844. }
  1845. if(c->n_videos)
  1846. c->is_init_section_common_video = is_common_init_section_exist(c->videos, c->n_videos);
  1847. /* Open the demuxer for video and audio components if available */
  1848. for (i = 0; i < c->n_videos; i++) {
  1849. rep = c->videos[i];
  1850. if (i > 0 && c->is_init_section_common_video) {
  1851. ret = copy_init_section(rep, c->videos[0]);
  1852. if (ret < 0)
  1853. goto fail;
  1854. }
  1855. ret = open_demux_for_component(s, rep);
  1856. if (ret)
  1857. goto fail;
  1858. rep->stream_index = stream_index;
  1859. ++stream_index;
  1860. }
  1861. if(c->n_audios)
  1862. c->is_init_section_common_audio = is_common_init_section_exist(c->audios, c->n_audios);
  1863. for (i = 0; i < c->n_audios; i++) {
  1864. rep = c->audios[i];
  1865. if (i > 0 && c->is_init_section_common_audio) {
  1866. ret = copy_init_section(rep, c->audios[0]);
  1867. if (ret < 0)
  1868. goto fail;
  1869. }
  1870. ret = open_demux_for_component(s, rep);
  1871. if (ret)
  1872. goto fail;
  1873. rep->stream_index = stream_index;
  1874. ++stream_index;
  1875. }
  1876. if (c->n_subtitles)
  1877. c->is_init_section_common_audio = is_common_init_section_exist(c->subtitles, c->n_subtitles);
  1878. for (i = 0; i < c->n_subtitles; i++) {
  1879. rep = c->subtitles[i];
  1880. if (i > 0 && c->is_init_section_common_audio) {
  1881. ret = copy_init_section(rep, c->subtitles[0]);
  1882. if (ret < 0)
  1883. goto fail;
  1884. }
  1885. ret = open_demux_for_component(s, rep);
  1886. if (ret)
  1887. goto fail;
  1888. rep->stream_index = stream_index;
  1889. ++stream_index;
  1890. }
  1891. if (!stream_index) {
  1892. ret = AVERROR_INVALIDDATA;
  1893. goto fail;
  1894. }
  1895. /* Create a program */
  1896. if (!ret) {
  1897. AVProgram *program;
  1898. program = av_new_program(s, 0);
  1899. if (!program) {
  1900. goto fail;
  1901. }
  1902. for (i = 0; i < c->n_videos; i++) {
  1903. rep = c->videos[i];
  1904. av_program_add_stream_index(s, 0, rep->stream_index);
  1905. rep->assoc_stream = s->streams[rep->stream_index];
  1906. if (rep->bandwidth > 0)
  1907. av_dict_set_int(&rep->assoc_stream->metadata, "variant_bitrate", rep->bandwidth, 0);
  1908. if (rep->id[0])
  1909. av_dict_set(&rep->assoc_stream->metadata, "id", rep->id, 0);
  1910. }
  1911. for (i = 0; i < c->n_audios; i++) {
  1912. rep = c->audios[i];
  1913. av_program_add_stream_index(s, 0, rep->stream_index);
  1914. rep->assoc_stream = s->streams[rep->stream_index];
  1915. if (rep->bandwidth > 0)
  1916. av_dict_set_int(&rep->assoc_stream->metadata, "variant_bitrate", rep->bandwidth, 0);
  1917. if (rep->id[0])
  1918. av_dict_set(&rep->assoc_stream->metadata, "id", rep->id, 0);
  1919. }
  1920. for (i = 0; i < c->n_subtitles; i++) {
  1921. rep = c->subtitles[i];
  1922. av_program_add_stream_index(s, 0, rep->stream_index);
  1923. rep->assoc_stream = s->streams[rep->stream_index];
  1924. if (rep->id[0])
  1925. av_dict_set(&rep->assoc_stream->metadata, "id", rep->id, 0);
  1926. }
  1927. }
  1928. return 0;
  1929. fail:
  1930. return ret;
  1931. }
  1932. static void recheck_discard_flags(AVFormatContext *s, struct representation **p, int n)
  1933. {
  1934. int i, j;
  1935. for (i = 0; i < n; i++) {
  1936. struct representation *pls = p[i];
  1937. int needed = !pls->assoc_stream || pls->assoc_stream->discard < AVDISCARD_ALL;
  1938. if (needed && !pls->ctx) {
  1939. pls->cur_seg_offset = 0;
  1940. pls->init_sec_buf_read_offset = 0;
  1941. /* Catch up */
  1942. for (j = 0; j < n; j++) {
  1943. pls->cur_seq_no = FFMAX(pls->cur_seq_no, p[j]->cur_seq_no);
  1944. }
  1945. reopen_demux_for_component(s, pls);
  1946. av_log(s, AV_LOG_INFO, "Now receiving stream_index %d\n", pls->stream_index);
  1947. } else if (!needed && pls->ctx) {
  1948. close_demux_for_component(pls);
  1949. if (pls->input)
  1950. ff_format_io_close(pls->parent, &pls->input);
  1951. av_log(s, AV_LOG_INFO, "No longer receiving stream_index %d\n", pls->stream_index);
  1952. }
  1953. }
  1954. }
  1955. static int dash_read_packet(AVFormatContext *s, AVPacket *pkt)
  1956. {
  1957. DASHContext *c = s->priv_data;
  1958. int ret = 0, i;
  1959. int64_t mints = 0;
  1960. struct representation *cur = NULL;
  1961. struct representation *rep = NULL;
  1962. recheck_discard_flags(s, c->videos, c->n_videos);
  1963. recheck_discard_flags(s, c->audios, c->n_audios);
  1964. recheck_discard_flags(s, c->subtitles, c->n_subtitles);
  1965. for (i = 0; i < c->n_videos; i++) {
  1966. rep = c->videos[i];
  1967. if (!rep->ctx)
  1968. continue;
  1969. if (!cur || rep->cur_timestamp < mints) {
  1970. cur = rep;
  1971. mints = rep->cur_timestamp;
  1972. }
  1973. }
  1974. for (i = 0; i < c->n_audios; i++) {
  1975. rep = c->audios[i];
  1976. if (!rep->ctx)
  1977. continue;
  1978. if (!cur || rep->cur_timestamp < mints) {
  1979. cur = rep;
  1980. mints = rep->cur_timestamp;
  1981. }
  1982. }
  1983. for (i = 0; i < c->n_subtitles; i++) {
  1984. rep = c->subtitles[i];
  1985. if (!rep->ctx)
  1986. continue;
  1987. if (!cur || rep->cur_timestamp < mints) {
  1988. cur = rep;
  1989. mints = rep->cur_timestamp;
  1990. }
  1991. }
  1992. if (!cur) {
  1993. return AVERROR_INVALIDDATA;
  1994. }
  1995. while (!ff_check_interrupt(c->interrupt_callback) && !ret) {
  1996. ret = av_read_frame(cur->ctx, pkt);
  1997. if (ret >= 0) {
  1998. /* If we got a packet, return it */
  1999. cur->cur_timestamp = av_rescale(pkt->pts, (int64_t)cur->ctx->streams[0]->time_base.num * 90000, cur->ctx->streams[0]->time_base.den);
  2000. pkt->stream_index = cur->stream_index;
  2001. return 0;
  2002. }
  2003. if (cur->is_restart_needed) {
  2004. cur->cur_seg_offset = 0;
  2005. cur->init_sec_buf_read_offset = 0;
  2006. if (cur->input)
  2007. ff_format_io_close(cur->parent, &cur->input);
  2008. ret = reopen_demux_for_component(s, cur);
  2009. cur->is_restart_needed = 0;
  2010. }
  2011. }
  2012. return AVERROR_EOF;
  2013. }
  2014. static int dash_close(AVFormatContext *s)
  2015. {
  2016. DASHContext *c = s->priv_data;
  2017. free_audio_list(c);
  2018. free_video_list(c);
  2019. av_dict_free(&c->avio_opts);
  2020. av_freep(&c->base_url);
  2021. return 0;
  2022. }
  2023. static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos_msec, int flags, int dry_run)
  2024. {
  2025. int ret = 0;
  2026. int i = 0;
  2027. int j = 0;
  2028. int64_t duration = 0;
  2029. av_log(pls->parent, AV_LOG_VERBOSE, "DASH seek pos[%"PRId64"ms], playlist %d%s\n",
  2030. seek_pos_msec, pls->rep_idx, dry_run ? " (dry)" : "");
  2031. // single fragment mode
  2032. if (pls->n_fragments == 1) {
  2033. pls->cur_timestamp = 0;
  2034. pls->cur_seg_offset = 0;
  2035. if (dry_run)
  2036. return 0;
  2037. ff_read_frame_flush(pls->ctx);
  2038. return av_seek_frame(pls->ctx, -1, seek_pos_msec * 1000, flags);
  2039. }
  2040. if (pls->input)
  2041. ff_format_io_close(pls->parent, &pls->input);
  2042. // find the nearest fragment
  2043. if (pls->n_timelines > 0 && pls->fragment_timescale > 0) {
  2044. int64_t num = pls->first_seq_no;
  2045. av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline start n_timelines[%d] "
  2046. "last_seq_no[%"PRId64"], playlist %d.\n",
  2047. (int)pls->n_timelines, (int64_t)pls->last_seq_no, (int)pls->rep_idx);
  2048. for (i = 0; i < pls->n_timelines; i++) {
  2049. if (pls->timelines[i]->starttime > 0) {
  2050. duration = pls->timelines[i]->starttime;
  2051. }
  2052. duration += pls->timelines[i]->duration;
  2053. if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
  2054. goto set_seq_num;
  2055. }
  2056. for (j = 0; j < pls->timelines[i]->repeat; j++) {
  2057. duration += pls->timelines[i]->duration;
  2058. num++;
  2059. if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
  2060. goto set_seq_num;
  2061. }
  2062. }
  2063. num++;
  2064. }
  2065. set_seq_num:
  2066. pls->cur_seq_no = num > pls->last_seq_no ? pls->last_seq_no : num;
  2067. av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline end cur_seq_no[%"PRId64"], playlist %d.\n",
  2068. (int64_t)pls->cur_seq_no, (int)pls->rep_idx);
  2069. } else if (pls->fragment_duration > 0) {
  2070. pls->cur_seq_no = pls->first_seq_no + ((seek_pos_msec * pls->fragment_timescale) / pls->fragment_duration) / 1000;
  2071. } else {
  2072. av_log(pls->parent, AV_LOG_ERROR, "dash_seek missing timeline or fragment_duration\n");
  2073. pls->cur_seq_no = pls->first_seq_no;
  2074. }
  2075. pls->cur_timestamp = 0;
  2076. pls->cur_seg_offset = 0;
  2077. pls->init_sec_buf_read_offset = 0;
  2078. ret = dry_run ? 0 : reopen_demux_for_component(s, pls);
  2079. return ret;
  2080. }
  2081. static int dash_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  2082. {
  2083. int ret = 0, i;
  2084. DASHContext *c = s->priv_data;
  2085. int64_t seek_pos_msec = av_rescale_rnd(timestamp, 1000,
  2086. s->streams[stream_index]->time_base.den,
  2087. flags & AVSEEK_FLAG_BACKWARD ?
  2088. AV_ROUND_DOWN : AV_ROUND_UP);
  2089. if ((flags & AVSEEK_FLAG_BYTE) || c->is_live)
  2090. return AVERROR(ENOSYS);
  2091. /* Seek in discarded streams with dry_run=1 to avoid reopening them */
  2092. for (i = 0; i < c->n_videos; i++) {
  2093. if (!ret)
  2094. ret = dash_seek(s, c->videos[i], seek_pos_msec, flags, !c->videos[i]->ctx);
  2095. }
  2096. for (i = 0; i < c->n_audios; i++) {
  2097. if (!ret)
  2098. ret = dash_seek(s, c->audios[i], seek_pos_msec, flags, !c->audios[i]->ctx);
  2099. }
  2100. for (i = 0; i < c->n_subtitles; i++) {
  2101. if (!ret)
  2102. ret = dash_seek(s, c->subtitles[i], seek_pos_msec, flags, !c->subtitles[i]->ctx);
  2103. }
  2104. return ret;
  2105. }
  2106. static int dash_probe(const AVProbeData *p)
  2107. {
  2108. if (!av_stristr(p->buf, "<MPD"))
  2109. return 0;
  2110. if (av_stristr(p->buf, "dash:profile:isoff-on-demand:2011") ||
  2111. av_stristr(p->buf, "dash:profile:isoff-live:2011") ||
  2112. av_stristr(p->buf, "dash:profile:isoff-live:2012") ||
  2113. av_stristr(p->buf, "dash:profile:isoff-main:2011")) {
  2114. return AVPROBE_SCORE_MAX;
  2115. }
  2116. if (av_stristr(p->buf, "dash:profile")) {
  2117. return AVPROBE_SCORE_MAX;
  2118. }
  2119. return 0;
  2120. }
  2121. #define OFFSET(x) offsetof(DASHContext, x)
  2122. #define FLAGS AV_OPT_FLAG_DECODING_PARAM
  2123. static const AVOption dash_options[] = {
  2124. {"allowed_extensions", "List of file extensions that dash is allowed to access",
  2125. OFFSET(allowed_extensions), AV_OPT_TYPE_STRING,
  2126. {.str = "aac,m4a,m4s,m4v,mov,mp4,webm"},
  2127. INT_MIN, INT_MAX, FLAGS},
  2128. {NULL}
  2129. };
  2130. static const AVClass dash_class = {
  2131. .class_name = "dash",
  2132. .item_name = av_default_item_name,
  2133. .option = dash_options,
  2134. .version = LIBAVUTIL_VERSION_INT,
  2135. };
  2136. AVInputFormat ff_dash_demuxer = {
  2137. .name = "dash",
  2138. .long_name = NULL_IF_CONFIG_SMALL("Dynamic Adaptive Streaming over HTTP"),
  2139. .priv_class = &dash_class,
  2140. .priv_data_size = sizeof(DASHContext),
  2141. .read_probe = dash_probe,
  2142. .read_header = dash_read_header,
  2143. .read_packet = dash_read_packet,
  2144. .read_close = dash_close,
  2145. .read_seek = dash_read_seek,
  2146. .flags = AVFMT_NO_BYTE_SEEK,
  2147. };