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.

2423 lines
83KB

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