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.

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