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.

1663 lines
56KB

  1. /*
  2. * Apple HTTP Live Streaming demuxer
  3. * Copyright (c) 2010 Martin Storsjo
  4. * Copyright (c) 2013 Anssi Hannula
  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. /**
  23. * @file
  24. * Apple HTTP Live Streaming demuxer
  25. * http://tools.ietf.org/html/draft-pantos-http-live-streaming
  26. */
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "libavutil/mathematics.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/dict.h"
  33. #include "libavutil/time.h"
  34. #include "avformat.h"
  35. #include "internal.h"
  36. #include "avio_internal.h"
  37. #include "url.h"
  38. #include "id3v2.h"
  39. #define INITIAL_BUFFER_SIZE 32768
  40. #define MAX_FIELD_LEN 64
  41. #define MAX_CHARACTERISTICS_LEN 512
  42. #define MPEG_TIME_BASE 90000
  43. #define MPEG_TIME_BASE_Q (AVRational){1, MPEG_TIME_BASE}
  44. /*
  45. * An apple http stream consists of a playlist with media segment files,
  46. * played sequentially. There may be several playlists with the same
  47. * video content, in different bandwidth variants, that are played in
  48. * parallel (preferably only one bandwidth variant at a time). In this case,
  49. * the user supplied the url to a main playlist that only lists the variant
  50. * playlists.
  51. *
  52. * If the main playlist doesn't point at any variants, we still create
  53. * one anonymous toplevel variant for this, to maintain the structure.
  54. */
  55. enum KeyType {
  56. KEY_NONE,
  57. KEY_AES_128,
  58. };
  59. struct segment {
  60. int64_t duration;
  61. int64_t url_offset;
  62. int64_t size;
  63. char url[MAX_URL_SIZE];
  64. char key[MAX_URL_SIZE];
  65. enum KeyType key_type;
  66. uint8_t iv[16];
  67. };
  68. struct rendition;
  69. /*
  70. * Each playlist has its own demuxer. If it currently is active,
  71. * it has an open AVIOContext too, and potentially an AVPacket
  72. * containing the next packet from this stream.
  73. */
  74. struct playlist {
  75. char url[MAX_URL_SIZE];
  76. AVIOContext pb;
  77. uint8_t* read_buffer;
  78. URLContext *input;
  79. AVFormatContext *parent;
  80. int index;
  81. AVFormatContext *ctx;
  82. AVPacket pkt;
  83. int stream_offset;
  84. int finished;
  85. int64_t target_duration;
  86. int start_seq_no;
  87. int n_segments;
  88. struct segment **segments;
  89. int needed, cur_needed;
  90. int cur_seq_no;
  91. int64_t cur_seg_offset;
  92. int64_t last_load_time;
  93. char key_url[MAX_URL_SIZE];
  94. uint8_t key[16];
  95. /* ID3 timestamp handling (elementary audio streams have ID3 timestamps
  96. * (and possibly other ID3 tags) in the beginning of each segment) */
  97. int is_id3_timestamped; /* -1: not yet known */
  98. int64_t id3_mpegts_timestamp; /* in mpegts tb */
  99. int64_t id3_offset; /* in stream original tb */
  100. uint8_t* id3_buf; /* temp buffer for id3 parsing */
  101. unsigned int id3_buf_size;
  102. AVDictionary *id3_initial; /* data from first id3 tag */
  103. int id3_found; /* ID3 tag found at some point */
  104. int id3_changed; /* ID3 tag data has changed at some point */
  105. ID3v2ExtraMeta *id3_deferred_extra; /* stored here until subdemuxer is opened */
  106. int64_t seek_timestamp;
  107. int seek_flags;
  108. /* Renditions associated with this playlist, if any.
  109. * Alternative rendition playlists have a single rendition associated
  110. * with them, and variant main Media Playlists may have
  111. * multiple (playlist-less) renditions associated with them. */
  112. int n_renditions;
  113. struct rendition **renditions;
  114. };
  115. /*
  116. * Renditions are e.g. alternative subtitle or audio streams.
  117. * The rendition may either be an external playlist or it may be
  118. * contained in the main Media Playlist of the variant (in which case
  119. * playlist is NULL).
  120. */
  121. struct rendition {
  122. enum AVMediaType type;
  123. struct playlist *playlist;
  124. char group_id[MAX_FIELD_LEN];
  125. char language[MAX_FIELD_LEN];
  126. char name[MAX_FIELD_LEN];
  127. int disposition;
  128. };
  129. struct variant {
  130. int bandwidth;
  131. /* every variant contains at least the main Media Playlist in index 0 */
  132. int n_playlists;
  133. struct playlist **playlists;
  134. char audio_group[MAX_FIELD_LEN];
  135. char video_group[MAX_FIELD_LEN];
  136. char subtitles_group[MAX_FIELD_LEN];
  137. };
  138. typedef struct HLSContext {
  139. int n_variants;
  140. struct variant **variants;
  141. int n_playlists;
  142. struct playlist **playlists;
  143. int n_renditions;
  144. struct rendition **renditions;
  145. int cur_seq_no;
  146. int end_of_segment;
  147. int first_packet;
  148. int64_t first_timestamp;
  149. int64_t cur_timestamp;
  150. AVIOInterruptCB *interrupt_callback;
  151. char *user_agent; ///< holds HTTP user agent set as an AVOption to the HTTP protocol context
  152. char *cookies; ///< holds HTTP cookie values set in either the initial response or as an AVOption to the HTTP protocol context
  153. char *headers; ///< holds HTTP headers set as an AVOption to the HTTP protocol context
  154. } HLSContext;
  155. static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
  156. {
  157. int len = ff_get_line(s, buf, maxlen);
  158. while (len > 0 && av_isspace(buf[len - 1]))
  159. buf[--len] = '\0';
  160. return len;
  161. }
  162. static void free_segment_list(struct playlist *pls)
  163. {
  164. int i;
  165. for (i = 0; i < pls->n_segments; i++)
  166. av_free(pls->segments[i]);
  167. av_freep(&pls->segments);
  168. pls->n_segments = 0;
  169. }
  170. static void free_playlist_list(HLSContext *c)
  171. {
  172. int i;
  173. for (i = 0; i < c->n_playlists; i++) {
  174. struct playlist *pls = c->playlists[i];
  175. free_segment_list(pls);
  176. av_freep(&pls->renditions);
  177. av_freep(&pls->id3_buf);
  178. av_dict_free(&pls->id3_initial);
  179. ff_id3v2_free_extra_meta(&pls->id3_deferred_extra);
  180. av_free_packet(&pls->pkt);
  181. av_free(pls->pb.buffer);
  182. if (pls->input)
  183. ffurl_close(pls->input);
  184. if (pls->ctx) {
  185. pls->ctx->pb = NULL;
  186. avformat_close_input(&pls->ctx);
  187. }
  188. av_free(pls);
  189. }
  190. av_freep(&c->playlists);
  191. av_freep(&c->cookies);
  192. av_freep(&c->user_agent);
  193. c->n_playlists = 0;
  194. }
  195. static void free_variant_list(HLSContext *c)
  196. {
  197. int i;
  198. for (i = 0; i < c->n_variants; i++) {
  199. struct variant *var = c->variants[i];
  200. av_freep(&var->playlists);
  201. av_free(var);
  202. }
  203. av_freep(&c->variants);
  204. c->n_variants = 0;
  205. }
  206. static void free_rendition_list(HLSContext *c)
  207. {
  208. int i;
  209. for (i = 0; i < c->n_renditions; i++)
  210. av_free(c->renditions[i]);
  211. av_freep(&c->renditions);
  212. c->n_renditions = 0;
  213. }
  214. /*
  215. * Used to reset a statically allocated AVPacket to a clean slate,
  216. * containing no data.
  217. */
  218. static void reset_packet(AVPacket *pkt)
  219. {
  220. av_init_packet(pkt);
  221. pkt->data = NULL;
  222. }
  223. static struct playlist *new_playlist(HLSContext *c, const char *url,
  224. const char *base)
  225. {
  226. struct playlist *pls = av_mallocz(sizeof(struct playlist));
  227. if (!pls)
  228. return NULL;
  229. reset_packet(&pls->pkt);
  230. ff_make_absolute_url(pls->url, sizeof(pls->url), base, url);
  231. pls->seek_timestamp = AV_NOPTS_VALUE;
  232. pls->is_id3_timestamped = -1;
  233. pls->id3_mpegts_timestamp = AV_NOPTS_VALUE;
  234. dynarray_add(&c->playlists, &c->n_playlists, pls);
  235. return pls;
  236. }
  237. struct variant_info {
  238. char bandwidth[20];
  239. /* variant group ids: */
  240. char audio[MAX_FIELD_LEN];
  241. char video[MAX_FIELD_LEN];
  242. char subtitles[MAX_FIELD_LEN];
  243. };
  244. static struct variant *new_variant(HLSContext *c, struct variant_info *info,
  245. const char *url, const char *base)
  246. {
  247. struct variant *var;
  248. struct playlist *pls;
  249. pls = new_playlist(c, url, base);
  250. if (!pls)
  251. return NULL;
  252. var = av_mallocz(sizeof(struct variant));
  253. if (!var)
  254. return NULL;
  255. if (info) {
  256. var->bandwidth = atoi(info->bandwidth);
  257. strcpy(var->audio_group, info->audio);
  258. strcpy(var->video_group, info->video);
  259. strcpy(var->subtitles_group, info->subtitles);
  260. }
  261. dynarray_add(&c->variants, &c->n_variants, var);
  262. dynarray_add(&var->playlists, &var->n_playlists, pls);
  263. return var;
  264. }
  265. static void handle_variant_args(struct variant_info *info, const char *key,
  266. int key_len, char **dest, int *dest_len)
  267. {
  268. if (!strncmp(key, "BANDWIDTH=", key_len)) {
  269. *dest = info->bandwidth;
  270. *dest_len = sizeof(info->bandwidth);
  271. } else if (!strncmp(key, "AUDIO=", key_len)) {
  272. *dest = info->audio;
  273. *dest_len = sizeof(info->audio);
  274. } else if (!strncmp(key, "VIDEO=", key_len)) {
  275. *dest = info->video;
  276. *dest_len = sizeof(info->video);
  277. } else if (!strncmp(key, "SUBTITLES=", key_len)) {
  278. *dest = info->subtitles;
  279. *dest_len = sizeof(info->subtitles);
  280. }
  281. }
  282. struct key_info {
  283. char uri[MAX_URL_SIZE];
  284. char method[10];
  285. char iv[35];
  286. };
  287. static void handle_key_args(struct key_info *info, const char *key,
  288. int key_len, char **dest, int *dest_len)
  289. {
  290. if (!strncmp(key, "METHOD=", key_len)) {
  291. *dest = info->method;
  292. *dest_len = sizeof(info->method);
  293. } else if (!strncmp(key, "URI=", key_len)) {
  294. *dest = info->uri;
  295. *dest_len = sizeof(info->uri);
  296. } else if (!strncmp(key, "IV=", key_len)) {
  297. *dest = info->iv;
  298. *dest_len = sizeof(info->iv);
  299. }
  300. }
  301. struct rendition_info {
  302. char type[16];
  303. char uri[MAX_URL_SIZE];
  304. char group_id[MAX_FIELD_LEN];
  305. char language[MAX_FIELD_LEN];
  306. char assoc_language[MAX_FIELD_LEN];
  307. char name[MAX_FIELD_LEN];
  308. char defaultr[4];
  309. char forced[4];
  310. char characteristics[MAX_CHARACTERISTICS_LEN];
  311. };
  312. static struct rendition *new_rendition(HLSContext *c, struct rendition_info *info,
  313. const char *url_base)
  314. {
  315. struct rendition *rend;
  316. enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
  317. char *characteristic;
  318. char *chr_ptr;
  319. char *saveptr;
  320. if (!strcmp(info->type, "AUDIO"))
  321. type = AVMEDIA_TYPE_AUDIO;
  322. else if (!strcmp(info->type, "VIDEO"))
  323. type = AVMEDIA_TYPE_VIDEO;
  324. else if (!strcmp(info->type, "SUBTITLES"))
  325. type = AVMEDIA_TYPE_SUBTITLE;
  326. else if (!strcmp(info->type, "CLOSED-CAPTIONS"))
  327. /* CLOSED-CAPTIONS is ignored since we do not support CEA-608 CC in
  328. * AVC SEI RBSP anyway */
  329. return NULL;
  330. if (type == AVMEDIA_TYPE_UNKNOWN)
  331. return NULL;
  332. /* URI is mandatory for subtitles as per spec */
  333. if (type == AVMEDIA_TYPE_SUBTITLE && !info->uri[0])
  334. return NULL;
  335. /* TODO: handle subtitles (each segment has to parsed separately) */
  336. if (type == AVMEDIA_TYPE_SUBTITLE)
  337. return NULL;
  338. rend = av_mallocz(sizeof(struct rendition));
  339. if (!rend)
  340. return NULL;
  341. dynarray_add(&c->renditions, &c->n_renditions, rend);
  342. rend->type = type;
  343. strcpy(rend->group_id, info->group_id);
  344. strcpy(rend->language, info->language);
  345. strcpy(rend->name, info->name);
  346. /* add the playlist if this is an external rendition */
  347. if (info->uri[0]) {
  348. rend->playlist = new_playlist(c, info->uri, url_base);
  349. if (rend->playlist)
  350. dynarray_add(&rend->playlist->renditions,
  351. &rend->playlist->n_renditions, rend);
  352. }
  353. if (info->assoc_language[0]) {
  354. int langlen = strlen(rend->language);
  355. if (langlen < sizeof(rend->language) - 3) {
  356. rend->language[langlen] = ',';
  357. strncpy(rend->language + langlen + 1, info->assoc_language,
  358. sizeof(rend->language) - langlen - 2);
  359. }
  360. }
  361. if (!strcmp(info->defaultr, "YES"))
  362. rend->disposition |= AV_DISPOSITION_DEFAULT;
  363. if (!strcmp(info->forced, "YES"))
  364. rend->disposition |= AV_DISPOSITION_FORCED;
  365. chr_ptr = info->characteristics;
  366. while ((characteristic = av_strtok(chr_ptr, ",", &saveptr))) {
  367. if (!strcmp(characteristic, "public.accessibility.describes-music-and-sound"))
  368. rend->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
  369. else if (!strcmp(characteristic, "public.accessibility.describes-video"))
  370. rend->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED;
  371. chr_ptr = NULL;
  372. }
  373. return rend;
  374. }
  375. static void handle_rendition_args(struct rendition_info *info, const char *key,
  376. int key_len, char **dest, int *dest_len)
  377. {
  378. if (!strncmp(key, "TYPE=", key_len)) {
  379. *dest = info->type;
  380. *dest_len = sizeof(info->type);
  381. } else if (!strncmp(key, "URI=", key_len)) {
  382. *dest = info->uri;
  383. *dest_len = sizeof(info->uri);
  384. } else if (!strncmp(key, "GROUP-ID=", key_len)) {
  385. *dest = info->group_id;
  386. *dest_len = sizeof(info->group_id);
  387. } else if (!strncmp(key, "LANGUAGE=", key_len)) {
  388. *dest = info->language;
  389. *dest_len = sizeof(info->language);
  390. } else if (!strncmp(key, "ASSOC-LANGUAGE=", key_len)) {
  391. *dest = info->assoc_language;
  392. *dest_len = sizeof(info->assoc_language);
  393. } else if (!strncmp(key, "NAME=", key_len)) {
  394. *dest = info->name;
  395. *dest_len = sizeof(info->name);
  396. } else if (!strncmp(key, "DEFAULT=", key_len)) {
  397. *dest = info->defaultr;
  398. *dest_len = sizeof(info->defaultr);
  399. } else if (!strncmp(key, "FORCED=", key_len)) {
  400. *dest = info->forced;
  401. *dest_len = sizeof(info->forced);
  402. } else if (!strncmp(key, "CHARACTERISTICS=", key_len)) {
  403. *dest = info->characteristics;
  404. *dest_len = sizeof(info->characteristics);
  405. }
  406. /*
  407. * ignored:
  408. * - AUTOSELECT: client may autoselect based on e.g. system language
  409. * - INSTREAM-ID: EIA-608 closed caption number ("CC1".."CC4")
  410. */
  411. }
  412. static int parse_playlist(HLSContext *c, const char *url,
  413. struct playlist *pls, AVIOContext *in)
  414. {
  415. int ret = 0, is_segment = 0, is_variant = 0;
  416. int64_t duration = 0;
  417. enum KeyType key_type = KEY_NONE;
  418. uint8_t iv[16] = "";
  419. int has_iv = 0;
  420. char key[MAX_URL_SIZE] = "";
  421. char line[MAX_URL_SIZE];
  422. const char *ptr;
  423. int close_in = 0;
  424. int64_t seg_offset = 0;
  425. int64_t seg_size = -1;
  426. uint8_t *new_url = NULL;
  427. struct variant_info variant_info;
  428. if (!in) {
  429. AVDictionary *opts = NULL;
  430. close_in = 1;
  431. /* Some HLS servers don't like being sent the range header */
  432. av_dict_set(&opts, "seekable", "0", 0);
  433. // broker prior HTTP options that should be consistent across requests
  434. av_dict_set(&opts, "user-agent", c->user_agent, 0);
  435. av_dict_set(&opts, "cookies", c->cookies, 0);
  436. av_dict_set(&opts, "headers", c->headers, 0);
  437. ret = avio_open2(&in, url, AVIO_FLAG_READ,
  438. c->interrupt_callback, &opts);
  439. av_dict_free(&opts);
  440. if (ret < 0)
  441. return ret;
  442. }
  443. if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, &new_url) >= 0)
  444. url = new_url;
  445. read_chomp_line(in, line, sizeof(line));
  446. if (strcmp(line, "#EXTM3U")) {
  447. ret = AVERROR_INVALIDDATA;
  448. goto fail;
  449. }
  450. if (pls) {
  451. free_segment_list(pls);
  452. pls->finished = 0;
  453. }
  454. while (!url_feof(in)) {
  455. read_chomp_line(in, line, sizeof(line));
  456. if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
  457. is_variant = 1;
  458. memset(&variant_info, 0, sizeof(variant_info));
  459. ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,
  460. &variant_info);
  461. } else if (av_strstart(line, "#EXT-X-KEY:", &ptr)) {
  462. struct key_info info = {{0}};
  463. ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_key_args,
  464. &info);
  465. key_type = KEY_NONE;
  466. has_iv = 0;
  467. if (!strcmp(info.method, "AES-128"))
  468. key_type = KEY_AES_128;
  469. if (!strncmp(info.iv, "0x", 2) || !strncmp(info.iv, "0X", 2)) {
  470. ff_hex_to_data(iv, info.iv + 2);
  471. has_iv = 1;
  472. }
  473. av_strlcpy(key, info.uri, sizeof(key));
  474. } else if (av_strstart(line, "#EXT-X-MEDIA:", &ptr)) {
  475. struct rendition_info info = {{0}};
  476. ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_rendition_args,
  477. &info);
  478. new_rendition(c, &info, url);
  479. } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
  480. if (!pls) {
  481. if (!new_variant(c, NULL, url, NULL)) {
  482. ret = AVERROR(ENOMEM);
  483. goto fail;
  484. }
  485. pls = c->playlists[c->n_playlists - 1];
  486. }
  487. pls->target_duration = atoi(ptr) * AV_TIME_BASE;
  488. } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
  489. if (!pls) {
  490. if (!new_variant(c, NULL, url, NULL)) {
  491. ret = AVERROR(ENOMEM);
  492. goto fail;
  493. }
  494. pls = c->playlists[c->n_playlists - 1];
  495. }
  496. pls->start_seq_no = atoi(ptr);
  497. } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
  498. if (pls)
  499. pls->finished = 1;
  500. } else if (av_strstart(line, "#EXTINF:", &ptr)) {
  501. is_segment = 1;
  502. duration = atof(ptr) * AV_TIME_BASE;
  503. } else if (av_strstart(line, "#EXT-X-BYTERANGE:", &ptr)) {
  504. seg_size = atoi(ptr);
  505. ptr = strchr(ptr, '@');
  506. if (ptr)
  507. seg_offset = atoi(ptr+1);
  508. } else if (av_strstart(line, "#", NULL)) {
  509. continue;
  510. } else if (line[0]) {
  511. if (is_variant) {
  512. if (!new_variant(c, &variant_info, line, url)) {
  513. ret = AVERROR(ENOMEM);
  514. goto fail;
  515. }
  516. is_variant = 0;
  517. }
  518. if (is_segment) {
  519. struct segment *seg;
  520. if (!pls) {
  521. if (!new_variant(c, 0, url, NULL)) {
  522. ret = AVERROR(ENOMEM);
  523. goto fail;
  524. }
  525. pls = c->playlists[c->n_playlists - 1];
  526. }
  527. seg = av_malloc(sizeof(struct segment));
  528. if (!seg) {
  529. ret = AVERROR(ENOMEM);
  530. goto fail;
  531. }
  532. seg->duration = duration;
  533. seg->key_type = key_type;
  534. if (has_iv) {
  535. memcpy(seg->iv, iv, sizeof(iv));
  536. } else {
  537. int seq = pls->start_seq_no + pls->n_segments;
  538. memset(seg->iv, 0, sizeof(seg->iv));
  539. AV_WB32(seg->iv + 12, seq);
  540. }
  541. ff_make_absolute_url(seg->key, sizeof(seg->key), url, key);
  542. ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);
  543. dynarray_add(&pls->segments, &pls->n_segments, seg);
  544. is_segment = 0;
  545. seg->size = seg_size;
  546. if (seg_size >= 0) {
  547. seg->url_offset = seg_offset;
  548. seg_offset += seg_size;
  549. seg_size = -1;
  550. } else {
  551. seg->url_offset = 0;
  552. seg_offset = 0;
  553. }
  554. }
  555. }
  556. }
  557. if (pls)
  558. pls->last_load_time = av_gettime();
  559. fail:
  560. av_free(new_url);
  561. if (close_in)
  562. avio_close(in);
  563. return ret;
  564. }
  565. enum ReadFromURLMode {
  566. READ_NORMAL,
  567. READ_COMPLETE,
  568. };
  569. /* read from URLContext, limiting read to current segment */
  570. static int read_from_url(struct playlist *pls, uint8_t *buf, int buf_size,
  571. enum ReadFromURLMode mode)
  572. {
  573. int ret;
  574. struct segment *seg = pls->segments[pls->cur_seq_no - pls->start_seq_no];
  575. /* limit read if the segment was only a part of a file */
  576. if (seg->size >= 0)
  577. buf_size = FFMIN(buf_size, seg->size - pls->cur_seg_offset);
  578. if (mode == READ_COMPLETE)
  579. ret = ffurl_read_complete(pls->input, buf, buf_size);
  580. else
  581. ret = ffurl_read(pls->input, buf, buf_size);
  582. if (ret > 0)
  583. pls->cur_seg_offset += ret;
  584. return ret;
  585. }
  586. /* Parse the raw ID3 data and pass contents to caller */
  587. static void parse_id3(AVFormatContext *s, AVIOContext *pb,
  588. AVDictionary **metadata, int64_t *dts,
  589. ID3v2ExtraMetaAPIC **apic, ID3v2ExtraMeta **extra_meta)
  590. {
  591. static const char id3_priv_owner_ts[] = "com.apple.streaming.transportStreamTimestamp";
  592. ID3v2ExtraMeta *meta;
  593. ff_id3v2_read_dict(pb, metadata, ID3v2_DEFAULT_MAGIC, extra_meta);
  594. for (meta = *extra_meta; meta; meta = meta->next) {
  595. if (!strcmp(meta->tag, "PRIV")) {
  596. ID3v2ExtraMetaPRIV *priv = meta->data;
  597. if (priv->datasize == 8 && !strcmp(priv->owner, id3_priv_owner_ts)) {
  598. /* 33-bit MPEG timestamp */
  599. int64_t ts = AV_RB64(priv->data);
  600. av_log(s, AV_LOG_DEBUG, "HLS ID3 audio timestamp %"PRId64"\n", ts);
  601. if ((ts & ~((1ULL << 33) - 1)) == 0)
  602. *dts = ts;
  603. else
  604. av_log(s, AV_LOG_ERROR, "Invalid HLS ID3 audio timestamp %"PRId64"\n", ts);
  605. }
  606. } else if (!strcmp(meta->tag, "APIC") && apic)
  607. *apic = meta->data;
  608. }
  609. }
  610. /* Check if the ID3 metadata contents have changed */
  611. static int id3_has_changed_values(struct playlist *pls, AVDictionary *metadata,
  612. ID3v2ExtraMetaAPIC *apic)
  613. {
  614. AVDictionaryEntry *entry = NULL;
  615. AVDictionaryEntry *oldentry;
  616. /* check that no keys have changed values */
  617. while ((entry = av_dict_get(metadata, "", entry, AV_DICT_IGNORE_SUFFIX))) {
  618. oldentry = av_dict_get(pls->id3_initial, entry->key, NULL, AV_DICT_MATCH_CASE);
  619. if (!oldentry || strcmp(oldentry->value, entry->value) != 0)
  620. return 1;
  621. }
  622. /* check if apic appeared */
  623. if (apic && (pls->ctx->nb_streams != 2 || !pls->ctx->streams[1]->attached_pic.data))
  624. return 1;
  625. if (apic) {
  626. int size = pls->ctx->streams[1]->attached_pic.size;
  627. if (size != apic->buf->size - FF_INPUT_BUFFER_PADDING_SIZE)
  628. return 1;
  629. if (memcmp(apic->buf->data, pls->ctx->streams[1]->attached_pic.data, size) != 0)
  630. return 1;
  631. }
  632. return 0;
  633. }
  634. /* Parse ID3 data and handle the found data */
  635. static void handle_id3(AVIOContext *pb, struct playlist *pls)
  636. {
  637. AVDictionary *metadata = NULL;
  638. ID3v2ExtraMetaAPIC *apic = NULL;
  639. ID3v2ExtraMeta *extra_meta = NULL;
  640. int64_t timestamp = AV_NOPTS_VALUE;
  641. parse_id3(pls->ctx, pb, &metadata, &timestamp, &apic, &extra_meta);
  642. if (timestamp != AV_NOPTS_VALUE) {
  643. pls->id3_mpegts_timestamp = timestamp;
  644. pls->id3_offset = 0;
  645. }
  646. if (!pls->id3_found) {
  647. /* initial ID3 tags */
  648. av_assert0(!pls->id3_deferred_extra);
  649. pls->id3_found = 1;
  650. /* get picture attachment and set text metadata */
  651. if (pls->ctx->nb_streams)
  652. ff_id3v2_parse_apic(pls->ctx, &extra_meta);
  653. else
  654. /* demuxer not yet opened, defer picture attachment */
  655. pls->id3_deferred_extra = extra_meta;
  656. av_dict_copy(&pls->ctx->metadata, metadata, 0);
  657. pls->id3_initial = metadata;
  658. } else {
  659. if (!pls->id3_changed && id3_has_changed_values(pls, metadata, apic)) {
  660. avpriv_report_missing_feature(pls->ctx, "Changing ID3 metadata in HLS audio elementary stream");
  661. pls->id3_changed = 1;
  662. }
  663. av_dict_free(&metadata);
  664. }
  665. if (!pls->id3_deferred_extra)
  666. ff_id3v2_free_extra_meta(&extra_meta);
  667. }
  668. /* Intercept and handle ID3 tags between URLContext and AVIOContext */
  669. static void intercept_id3(struct playlist *pls, uint8_t *buf,
  670. int buf_size, int *len)
  671. {
  672. /* intercept id3 tags, we do not want to pass them to the raw
  673. * demuxer on all segment switches */
  674. int bytes;
  675. int id3_buf_pos = 0;
  676. int fill_buf = 0;
  677. /* gather all the id3 tags */
  678. while (1) {
  679. /* see if we can retrieve enough data for ID3 header */
  680. if (*len < ID3v2_HEADER_SIZE && buf_size >= ID3v2_HEADER_SIZE) {
  681. bytes = read_from_url(pls, buf + *len, ID3v2_HEADER_SIZE - *len, READ_COMPLETE);
  682. if (bytes > 0) {
  683. if (bytes == ID3v2_HEADER_SIZE - *len)
  684. /* no EOF yet, so fill the caller buffer again after
  685. * we have stripped the ID3 tags */
  686. fill_buf = 1;
  687. *len += bytes;
  688. } else if (*len <= 0) {
  689. /* error/EOF */
  690. *len = bytes;
  691. fill_buf = 0;
  692. }
  693. }
  694. if (*len < ID3v2_HEADER_SIZE)
  695. break;
  696. if (ff_id3v2_match(buf, ID3v2_DEFAULT_MAGIC)) {
  697. struct segment *seg = pls->segments[pls->cur_seq_no - pls->start_seq_no];
  698. int64_t segsize = seg->size >= 0 ? seg->size : ffurl_size(pls->input);
  699. int taglen = ff_id3v2_tag_len(buf);
  700. int tag_got_bytes = FFMIN(taglen, *len);
  701. int remaining = taglen - tag_got_bytes;
  702. if (taglen > segsize) {
  703. av_log(pls->ctx, AV_LOG_ERROR, "Too large HLS ID3 tag (%d vs %"PRId64")\n",
  704. taglen, segsize);
  705. break;
  706. }
  707. /*
  708. * Copy the id3 tag to our temporary id3 buffer.
  709. * We could read a small id3 tag directly without memcpy, but
  710. * we would still need to copy the large tags, and handling
  711. * both of those cases together with the possibility for multiple
  712. * tags would make the handling a bit complex.
  713. */
  714. pls->id3_buf = av_fast_realloc(pls->id3_buf, &pls->id3_buf_size, id3_buf_pos + taglen);
  715. if (!pls->id3_buf)
  716. break;
  717. memcpy(pls->id3_buf + id3_buf_pos, buf, tag_got_bytes);
  718. id3_buf_pos += tag_got_bytes;
  719. /* strip the intercepted bytes */
  720. *len -= tag_got_bytes;
  721. memmove(buf, buf + tag_got_bytes, *len);
  722. av_log(pls->ctx, AV_LOG_DEBUG, "Stripped %d HLS ID3 bytes\n", tag_got_bytes);
  723. if (remaining > 0) {
  724. /* read the rest of the tag in */
  725. if (read_from_url(pls, pls->id3_buf + id3_buf_pos, remaining, READ_COMPLETE) != remaining)
  726. break;
  727. id3_buf_pos += remaining;
  728. av_log(pls->ctx, AV_LOG_DEBUG, "Stripped additional %d HLS ID3 bytes\n", remaining);
  729. }
  730. } else {
  731. /* no more ID3 tags */
  732. break;
  733. }
  734. }
  735. /* re-fill buffer for the caller unless EOF */
  736. if (*len >= 0 && (fill_buf || *len == 0)) {
  737. bytes = read_from_url(pls, buf + *len, buf_size - *len, READ_NORMAL);
  738. /* ignore error if we already had some data */
  739. if (bytes >= 0)
  740. *len += bytes;
  741. else if (*len == 0)
  742. *len = bytes;
  743. }
  744. if (pls->id3_buf) {
  745. /* Now parse all the ID3 tags */
  746. AVIOContext id3ioctx;
  747. ffio_init_context(&id3ioctx, pls->id3_buf, id3_buf_pos, 0, NULL, NULL, NULL, NULL);
  748. handle_id3(&id3ioctx, pls);
  749. }
  750. if (pls->is_id3_timestamped == -1)
  751. pls->is_id3_timestamped = (pls->id3_mpegts_timestamp != AV_NOPTS_VALUE);
  752. }
  753. static int open_input(HLSContext *c, struct playlist *pls)
  754. {
  755. AVDictionary *opts = NULL;
  756. AVDictionary *opts2 = NULL;
  757. int ret;
  758. struct segment *seg = pls->segments[pls->cur_seq_no - pls->start_seq_no];
  759. // broker prior HTTP options that should be consistent across requests
  760. av_dict_set(&opts, "user-agent", c->user_agent, 0);
  761. av_dict_set(&opts, "cookies", c->cookies, 0);
  762. av_dict_set(&opts, "headers", c->headers, 0);
  763. av_dict_set(&opts, "seekable", "0", 0);
  764. // Same opts for key request (ffurl_open mutilates the opts so it cannot be used twice)
  765. av_dict_copy(&opts2, opts, 0);
  766. if (seg->size >= 0) {
  767. /* try to restrict the HTTP request to the part we want
  768. * (if this is in fact a HTTP request) */
  769. char offset[24] = { 0 };
  770. char end_offset[24] = { 0 };
  771. snprintf(offset, sizeof(offset) - 1, "%"PRId64,
  772. seg->url_offset);
  773. snprintf(end_offset, sizeof(end_offset) - 1, "%"PRId64,
  774. seg->url_offset + seg->size);
  775. av_dict_set(&opts, "offset", offset, 0);
  776. av_dict_set(&opts, "end_offset", end_offset, 0);
  777. }
  778. av_log(pls->parent, AV_LOG_VERBOSE, "HLS request for url '%s', offset %"PRId64", playlist %d\n",
  779. seg->url, seg->url_offset, pls->index);
  780. if (seg->key_type == KEY_NONE) {
  781. ret = ffurl_open(&pls->input, seg->url, AVIO_FLAG_READ,
  782. &pls->parent->interrupt_callback, &opts);
  783. } else if (seg->key_type == KEY_AES_128) {
  784. char iv[33], key[33], url[MAX_URL_SIZE];
  785. if (strcmp(seg->key, pls->key_url)) {
  786. URLContext *uc;
  787. if (ffurl_open(&uc, seg->key, AVIO_FLAG_READ,
  788. &pls->parent->interrupt_callback, &opts2) == 0) {
  789. if (ffurl_read_complete(uc, pls->key, sizeof(pls->key))
  790. != sizeof(pls->key)) {
  791. av_log(NULL, AV_LOG_ERROR, "Unable to read key file %s\n",
  792. seg->key);
  793. }
  794. ffurl_close(uc);
  795. } else {
  796. av_log(NULL, AV_LOG_ERROR, "Unable to open key file %s\n",
  797. seg->key);
  798. }
  799. av_strlcpy(pls->key_url, seg->key, sizeof(pls->key_url));
  800. }
  801. ff_data_to_hex(iv, seg->iv, sizeof(seg->iv), 0);
  802. ff_data_to_hex(key, pls->key, sizeof(pls->key), 0);
  803. iv[32] = key[32] = '\0';
  804. if (strstr(seg->url, "://"))
  805. snprintf(url, sizeof(url), "crypto+%s", seg->url);
  806. else
  807. snprintf(url, sizeof(url), "crypto:%s", seg->url);
  808. if ((ret = ffurl_alloc(&pls->input, url, AVIO_FLAG_READ,
  809. &pls->parent->interrupt_callback)) < 0)
  810. goto cleanup;
  811. av_opt_set(pls->input->priv_data, "key", key, 0);
  812. av_opt_set(pls->input->priv_data, "iv", iv, 0);
  813. if ((ret = ffurl_connect(pls->input, &opts)) < 0) {
  814. ffurl_close(pls->input);
  815. pls->input = NULL;
  816. goto cleanup;
  817. }
  818. ret = 0;
  819. }
  820. else
  821. ret = AVERROR(ENOSYS);
  822. /* Seek to the requested position. If this was a HTTP request, the offset
  823. * should already be where want it to, but this allows e.g. local testing
  824. * without a HTTP server. */
  825. if (ret == 0) {
  826. int seekret = ffurl_seek(pls->input, seg->url_offset, SEEK_SET);
  827. if (seekret < 0) {
  828. av_log(pls->parent, AV_LOG_ERROR, "Unable to seek to offset %"PRId64" of HLS segment '%s'\n", seg->url_offset, seg->url);
  829. ret = seekret;
  830. ffurl_close(pls->input);
  831. pls->input = NULL;
  832. }
  833. }
  834. cleanup:
  835. av_dict_free(&opts);
  836. av_dict_free(&opts2);
  837. pls->cur_seg_offset = 0;
  838. return ret;
  839. }
  840. static int64_t default_reload_interval(struct playlist *pls)
  841. {
  842. return pls->n_segments > 0 ?
  843. pls->segments[pls->n_segments - 1]->duration :
  844. pls->target_duration;
  845. }
  846. static int read_data(void *opaque, uint8_t *buf, int buf_size)
  847. {
  848. struct playlist *v = opaque;
  849. HLSContext *c = v->parent->priv_data;
  850. int ret, i;
  851. int just_opened = 0;
  852. if (!v->needed)
  853. return AVERROR_EOF;
  854. restart:
  855. if (!v->input) {
  856. /* If this is a live stream and the reload interval has elapsed since
  857. * the last playlist reload, reload the playlists now. */
  858. int64_t reload_interval = default_reload_interval(v);
  859. reload:
  860. if (!v->finished &&
  861. av_gettime() - v->last_load_time >= reload_interval) {
  862. if ((ret = parse_playlist(c, v->url, v, NULL)) < 0) {
  863. av_log(v->parent, AV_LOG_WARNING, "Failed to reload playlist %d\n",
  864. v->index);
  865. return ret;
  866. }
  867. /* If we need to reload the playlist again below (if
  868. * there's still no more segments), switch to a reload
  869. * interval of half the target duration. */
  870. reload_interval = v->target_duration / 2;
  871. }
  872. if (v->cur_seq_no < v->start_seq_no) {
  873. av_log(NULL, AV_LOG_WARNING,
  874. "skipping %d segments ahead, expired from playlists\n",
  875. v->start_seq_no - v->cur_seq_no);
  876. v->cur_seq_no = v->start_seq_no;
  877. }
  878. if (v->cur_seq_no >= v->start_seq_no + v->n_segments) {
  879. if (v->finished)
  880. return AVERROR_EOF;
  881. while (av_gettime() - v->last_load_time < reload_interval) {
  882. if (ff_check_interrupt(c->interrupt_callback))
  883. return AVERROR_EXIT;
  884. av_usleep(100*1000);
  885. }
  886. /* Enough time has elapsed since the last reload */
  887. goto reload;
  888. }
  889. ret = open_input(c, v);
  890. if (ret < 0) {
  891. av_log(v->parent, AV_LOG_WARNING, "Failed to open segment of playlist %d\n",
  892. v->index);
  893. return ret;
  894. }
  895. just_opened = 1;
  896. }
  897. ret = read_from_url(v, buf, buf_size, READ_NORMAL);
  898. if (ret > 0) {
  899. if (just_opened && v->is_id3_timestamped != 0) {
  900. /* Intercept ID3 tags here, elementary audio streams are required
  901. * to convey timestamps using them in the beginning of each segment. */
  902. intercept_id3(v, buf, buf_size, &ret);
  903. }
  904. return ret;
  905. }
  906. ffurl_close(v->input);
  907. v->input = NULL;
  908. v->cur_seq_no++;
  909. c->end_of_segment = 1;
  910. c->cur_seq_no = v->cur_seq_no;
  911. if (v->ctx && v->ctx->nb_streams &&
  912. v->parent->nb_streams >= v->stream_offset + v->ctx->nb_streams) {
  913. v->needed = 0;
  914. for (i = v->stream_offset; i < v->stream_offset + v->ctx->nb_streams;
  915. i++) {
  916. if (v->parent->streams[i]->discard < AVDISCARD_ALL)
  917. v->needed = 1;
  918. }
  919. }
  920. if (!v->needed) {
  921. av_log(v->parent, AV_LOG_INFO, "No longer receiving playlist %d\n",
  922. v->index);
  923. return AVERROR_EOF;
  924. }
  925. goto restart;
  926. }
  927. static int playlist_in_multiple_variants(HLSContext *c, struct playlist *pls)
  928. {
  929. int variant_count = 0;
  930. int i, j;
  931. for (i = 0; i < c->n_variants && variant_count < 2; i++) {
  932. struct variant *v = c->variants[i];
  933. for (j = 0; j < v->n_playlists; j++) {
  934. if (v->playlists[j] == pls) {
  935. variant_count++;
  936. break;
  937. }
  938. }
  939. }
  940. return variant_count >= 2;
  941. }
  942. static void add_renditions_to_variant(HLSContext *c, struct variant *var,
  943. enum AVMediaType type, const char *group_id)
  944. {
  945. int i;
  946. for (i = 0; i < c->n_renditions; i++) {
  947. struct rendition *rend = c->renditions[i];
  948. if (rend->type == type && !strcmp(rend->group_id, group_id)) {
  949. if (rend->playlist)
  950. /* rendition is an external playlist
  951. * => add the playlist to the variant */
  952. dynarray_add(&var->playlists, &var->n_playlists, rend->playlist);
  953. else
  954. /* rendition is part of the variant main Media Playlist
  955. * => add the rendition to the main Media Playlist */
  956. dynarray_add(&var->playlists[0]->renditions,
  957. &var->playlists[0]->n_renditions,
  958. rend);
  959. }
  960. }
  961. }
  962. static void add_metadata_from_renditions(AVFormatContext *s, struct playlist *pls,
  963. enum AVMediaType type)
  964. {
  965. int rend_idx = 0;
  966. int i;
  967. for (i = 0; i < pls->ctx->nb_streams; i++) {
  968. AVStream *st = s->streams[pls->stream_offset + i];
  969. if (st->codec->codec_type != type)
  970. continue;
  971. for (; rend_idx < pls->n_renditions; rend_idx++) {
  972. struct rendition *rend = pls->renditions[rend_idx];
  973. if (rend->type != type)
  974. continue;
  975. if (rend->language[0])
  976. av_dict_set(&st->metadata, "language", rend->language, 0);
  977. if (rend->name[0])
  978. av_dict_set(&st->metadata, "comment", rend->name, 0);
  979. st->disposition |= rend->disposition;
  980. }
  981. if (rend_idx >=pls->n_renditions)
  982. break;
  983. }
  984. }
  985. /* if timestamp was in valid range: returns 1 and sets seq_no
  986. * if not: returns 0 and sets seq_no to closest segment */
  987. static int find_timestamp_in_playlist(HLSContext *c, struct playlist *pls,
  988. int64_t timestamp, int *seq_no)
  989. {
  990. int i;
  991. int64_t pos = c->first_timestamp == AV_NOPTS_VALUE ?
  992. 0 : c->first_timestamp;
  993. if (timestamp < pos) {
  994. *seq_no = pls->start_seq_no;
  995. return 0;
  996. }
  997. for (i = 0; i < pls->n_segments; i++) {
  998. int64_t diff = pos + pls->segments[i]->duration - timestamp;
  999. if (diff > 0) {
  1000. *seq_no = pls->start_seq_no + i;
  1001. return 1;
  1002. }
  1003. pos += pls->segments[i]->duration;
  1004. }
  1005. *seq_no = pls->start_seq_no + pls->n_segments - 1;
  1006. return 0;
  1007. }
  1008. static int select_cur_seq_no(HLSContext *c, struct playlist *pls)
  1009. {
  1010. int seq_no;
  1011. if (!pls->finished && !c->first_packet &&
  1012. av_gettime() - pls->last_load_time >= default_reload_interval(pls))
  1013. /* reload the playlist since it was suspended */
  1014. parse_playlist(c, pls->url, pls, NULL);
  1015. /* If playback is already in progress (we are just selecting a new
  1016. * playlist) and this is a complete file, find the matching segment
  1017. * by counting durations. */
  1018. if (pls->finished && c->cur_timestamp != AV_NOPTS_VALUE) {
  1019. find_timestamp_in_playlist(c, pls, c->cur_timestamp, &seq_no);
  1020. return seq_no;
  1021. }
  1022. if (!pls->finished) {
  1023. if (!c->first_packet && /* we are doing a segment selection during playback */
  1024. c->cur_seq_no >= pls->start_seq_no &&
  1025. c->cur_seq_no < pls->start_seq_no + pls->n_segments)
  1026. /* While spec 3.4.3 says that we cannot assume anything about the
  1027. * content at the same sequence number on different playlists,
  1028. * in practice this seems to work and doing it otherwise would
  1029. * require us to download a segment to inspect its timestamps. */
  1030. return c->cur_seq_no;
  1031. /* If this is a live stream with more than 3 segments, start at the
  1032. * third last segment. */
  1033. if (pls->n_segments > 3)
  1034. return pls->start_seq_no + pls->n_segments - 3;
  1035. }
  1036. /* Otherwise just start on the first segment. */
  1037. return pls->start_seq_no;
  1038. }
  1039. static int hls_read_header(AVFormatContext *s)
  1040. {
  1041. URLContext *u = (s->flags & AVFMT_FLAG_CUSTOM_IO) ? NULL : s->pb->opaque;
  1042. HLSContext *c = s->priv_data;
  1043. int ret = 0, i, j, stream_offset = 0;
  1044. c->interrupt_callback = &s->interrupt_callback;
  1045. c->first_packet = 1;
  1046. c->first_timestamp = AV_NOPTS_VALUE;
  1047. c->cur_timestamp = AV_NOPTS_VALUE;
  1048. // if the URL context is good, read important options we must broker later
  1049. if (u && u->prot->priv_data_class) {
  1050. // get the previous user agent & set back to null if string size is zero
  1051. av_freep(&c->user_agent);
  1052. av_opt_get(u->priv_data, "user-agent", 0, (uint8_t**)&(c->user_agent));
  1053. if (c->user_agent && !strlen(c->user_agent))
  1054. av_freep(&c->user_agent);
  1055. // get the previous cookies & set back to null if string size is zero
  1056. av_freep(&c->cookies);
  1057. av_opt_get(u->priv_data, "cookies", 0, (uint8_t**)&(c->cookies));
  1058. if (c->cookies && !strlen(c->cookies))
  1059. av_freep(&c->cookies);
  1060. // get the previous headers & set back to null if string size is zero
  1061. av_freep(&c->headers);
  1062. av_opt_get(u->priv_data, "headers", 0, (uint8_t**)&(c->headers));
  1063. if (c->headers && !strlen(c->headers))
  1064. av_freep(&c->headers);
  1065. }
  1066. if ((ret = parse_playlist(c, s->filename, NULL, s->pb)) < 0)
  1067. goto fail;
  1068. if (c->n_variants == 0) {
  1069. av_log(NULL, AV_LOG_WARNING, "Empty playlist\n");
  1070. ret = AVERROR_EOF;
  1071. goto fail;
  1072. }
  1073. /* If the playlist only contained playlists (Master Playlist),
  1074. * parse each individual playlist. */
  1075. if (c->n_playlists > 1 || c->playlists[0]->n_segments == 0) {
  1076. for (i = 0; i < c->n_playlists; i++) {
  1077. struct playlist *pls = c->playlists[i];
  1078. if ((ret = parse_playlist(c, pls->url, pls, NULL)) < 0)
  1079. goto fail;
  1080. }
  1081. }
  1082. if (c->variants[0]->playlists[0]->n_segments == 0) {
  1083. av_log(NULL, AV_LOG_WARNING, "Empty playlist\n");
  1084. ret = AVERROR_EOF;
  1085. goto fail;
  1086. }
  1087. /* If this isn't a live stream, calculate the total duration of the
  1088. * stream. */
  1089. if (c->variants[0]->playlists[0]->finished) {
  1090. int64_t duration = 0;
  1091. for (i = 0; i < c->variants[0]->playlists[0]->n_segments; i++)
  1092. duration += c->variants[0]->playlists[0]->segments[i]->duration;
  1093. s->duration = duration;
  1094. }
  1095. /* Associate renditions with variants */
  1096. for (i = 0; i < c->n_variants; i++) {
  1097. struct variant *var = c->variants[i];
  1098. if (var->audio_group[0])
  1099. add_renditions_to_variant(c, var, AVMEDIA_TYPE_AUDIO, var->audio_group);
  1100. if (var->video_group[0])
  1101. add_renditions_to_variant(c, var, AVMEDIA_TYPE_VIDEO, var->video_group);
  1102. if (var->subtitles_group[0])
  1103. add_renditions_to_variant(c, var, AVMEDIA_TYPE_SUBTITLE, var->subtitles_group);
  1104. }
  1105. /* Open the demuxer for each playlist */
  1106. for (i = 0; i < c->n_playlists; i++) {
  1107. struct playlist *pls = c->playlists[i];
  1108. AVInputFormat *in_fmt = NULL;
  1109. if (pls->n_segments == 0)
  1110. continue;
  1111. if (!(pls->ctx = avformat_alloc_context())) {
  1112. ret = AVERROR(ENOMEM);
  1113. goto fail;
  1114. }
  1115. pls->index = i;
  1116. pls->needed = 1;
  1117. pls->parent = s;
  1118. pls->cur_seq_no = select_cur_seq_no(c, pls);
  1119. pls->read_buffer = av_malloc(INITIAL_BUFFER_SIZE);
  1120. ffio_init_context(&pls->pb, pls->read_buffer, INITIAL_BUFFER_SIZE, 0, pls,
  1121. read_data, NULL, NULL);
  1122. pls->pb.seekable = 0;
  1123. ret = av_probe_input_buffer(&pls->pb, &in_fmt, pls->segments[0]->url,
  1124. NULL, 0, 0);
  1125. if (ret < 0) {
  1126. /* Free the ctx - it isn't initialized properly at this point,
  1127. * so avformat_close_input shouldn't be called. If
  1128. * avformat_open_input fails below, it frees and zeros the
  1129. * context, so it doesn't need any special treatment like this. */
  1130. av_log(s, AV_LOG_ERROR, "Error when loading first segment '%s'\n", pls->segments[0]->url);
  1131. avformat_free_context(pls->ctx);
  1132. pls->ctx = NULL;
  1133. goto fail;
  1134. }
  1135. pls->ctx->pb = &pls->pb;
  1136. pls->stream_offset = stream_offset;
  1137. ret = avformat_open_input(&pls->ctx, pls->segments[0]->url, in_fmt, NULL);
  1138. if (ret < 0)
  1139. goto fail;
  1140. if (pls->id3_deferred_extra && pls->ctx->nb_streams == 1) {
  1141. ff_id3v2_parse_apic(pls->ctx, &pls->id3_deferred_extra);
  1142. avformat_queue_attached_pictures(pls->ctx);
  1143. ff_id3v2_free_extra_meta(&pls->id3_deferred_extra);
  1144. pls->id3_deferred_extra = NULL;
  1145. }
  1146. pls->ctx->ctx_flags &= ~AVFMTCTX_NOHEADER;
  1147. ret = avformat_find_stream_info(pls->ctx, NULL);
  1148. if (ret < 0)
  1149. goto fail;
  1150. if (pls->is_id3_timestamped == -1)
  1151. av_log(s, AV_LOG_WARNING, "No expected HTTP requests have been made\n");
  1152. /* Create new AVStreams for each stream in this playlist */
  1153. for (j = 0; j < pls->ctx->nb_streams; j++) {
  1154. AVStream *st = avformat_new_stream(s, NULL);
  1155. AVStream *ist = pls->ctx->streams[j];
  1156. if (!st) {
  1157. ret = AVERROR(ENOMEM);
  1158. goto fail;
  1159. }
  1160. st->id = i;
  1161. avcodec_copy_context(st->codec, pls->ctx->streams[j]->codec);
  1162. if (pls->is_id3_timestamped) /* custom timestamps via id3 */
  1163. avpriv_set_pts_info(st, 33, 1, MPEG_TIME_BASE);
  1164. else
  1165. avpriv_set_pts_info(st, ist->pts_wrap_bits, ist->time_base.num, ist->time_base.den);
  1166. }
  1167. add_metadata_from_renditions(s, pls, AVMEDIA_TYPE_AUDIO);
  1168. add_metadata_from_renditions(s, pls, AVMEDIA_TYPE_VIDEO);
  1169. add_metadata_from_renditions(s, pls, AVMEDIA_TYPE_SUBTITLE);
  1170. stream_offset += pls->ctx->nb_streams;
  1171. }
  1172. /* Create a program for each variant */
  1173. for (i = 0; i < c->n_variants; i++) {
  1174. struct variant *v = c->variants[i];
  1175. char bitrate_str[20];
  1176. AVProgram *program;
  1177. snprintf(bitrate_str, sizeof(bitrate_str), "%d", v->bandwidth);
  1178. program = av_new_program(s, i);
  1179. if (!program)
  1180. goto fail;
  1181. av_dict_set(&program->metadata, "variant_bitrate", bitrate_str, 0);
  1182. for (j = 0; j < v->n_playlists; j++) {
  1183. struct playlist *pls = v->playlists[j];
  1184. int is_shared = playlist_in_multiple_variants(c, pls);
  1185. int k;
  1186. for (k = 0; k < pls->ctx->nb_streams; k++) {
  1187. struct AVStream *st = s->streams[pls->stream_offset + k];
  1188. ff_program_add_stream_index(s, i, pls->stream_offset + k);
  1189. /* Set variant_bitrate for streams unique to this variant */
  1190. if (!is_shared && v->bandwidth)
  1191. av_dict_set(&st->metadata, "variant_bitrate", bitrate_str, 0);
  1192. }
  1193. }
  1194. }
  1195. return 0;
  1196. fail:
  1197. free_playlist_list(c);
  1198. free_variant_list(c);
  1199. free_rendition_list(c);
  1200. return ret;
  1201. }
  1202. static int recheck_discard_flags(AVFormatContext *s, int first)
  1203. {
  1204. HLSContext *c = s->priv_data;
  1205. int i, changed = 0;
  1206. /* Check if any new streams are needed */
  1207. for (i = 0; i < c->n_playlists; i++)
  1208. c->playlists[i]->cur_needed = 0;
  1209. for (i = 0; i < s->nb_streams; i++) {
  1210. AVStream *st = s->streams[i];
  1211. struct playlist *pls = c->playlists[s->streams[i]->id];
  1212. if (st->discard < AVDISCARD_ALL)
  1213. pls->cur_needed = 1;
  1214. }
  1215. for (i = 0; i < c->n_playlists; i++) {
  1216. struct playlist *pls = c->playlists[i];
  1217. if (pls->cur_needed && !pls->needed) {
  1218. pls->needed = 1;
  1219. changed = 1;
  1220. pls->cur_seq_no = select_cur_seq_no(c, pls);
  1221. pls->pb.eof_reached = 0;
  1222. if (c->cur_timestamp != AV_NOPTS_VALUE) {
  1223. /* catch up */
  1224. pls->seek_timestamp = c->cur_timestamp;
  1225. pls->seek_flags = AVSEEK_FLAG_ANY;
  1226. }
  1227. av_log(s, AV_LOG_INFO, "Now receiving playlist %d, segment %d\n", i, pls->cur_seq_no);
  1228. } else if (first && !pls->cur_needed && pls->needed) {
  1229. if (pls->input)
  1230. ffurl_close(pls->input);
  1231. pls->input = NULL;
  1232. pls->needed = 0;
  1233. changed = 1;
  1234. av_log(s, AV_LOG_INFO, "No longer receiving playlist %d\n", i);
  1235. }
  1236. }
  1237. return changed;
  1238. }
  1239. static void fill_timing_for_id3_timestamped_stream(struct playlist *pls)
  1240. {
  1241. if (pls->id3_offset >= 0) {
  1242. pls->pkt.dts = pls->id3_mpegts_timestamp +
  1243. av_rescale_q(pls->id3_offset,
  1244. pls->ctx->streams[pls->pkt.stream_index]->time_base,
  1245. MPEG_TIME_BASE_Q);
  1246. if (pls->pkt.duration)
  1247. pls->id3_offset += pls->pkt.duration;
  1248. else
  1249. pls->id3_offset = -1;
  1250. } else {
  1251. /* there have been packets with unknown duration
  1252. * since the last id3 tag, should not normally happen */
  1253. pls->pkt.dts = AV_NOPTS_VALUE;
  1254. }
  1255. if (pls->pkt.duration)
  1256. pls->pkt.duration = av_rescale_q(pls->pkt.duration,
  1257. pls->ctx->streams[pls->pkt.stream_index]->time_base,
  1258. MPEG_TIME_BASE_Q);
  1259. pls->pkt.pts = AV_NOPTS_VALUE;
  1260. }
  1261. static AVRational get_timebase(struct playlist *pls)
  1262. {
  1263. if (pls->is_id3_timestamped)
  1264. return MPEG_TIME_BASE_Q;
  1265. return pls->ctx->streams[pls->pkt.stream_index]->time_base;
  1266. }
  1267. static int hls_read_packet(AVFormatContext *s, AVPacket *pkt)
  1268. {
  1269. HLSContext *c = s->priv_data;
  1270. int ret, i, minplaylist = -1;
  1271. if (c->first_packet) {
  1272. recheck_discard_flags(s, 1);
  1273. c->first_packet = 0;
  1274. }
  1275. start:
  1276. c->end_of_segment = 0;
  1277. for (i = 0; i < c->n_playlists; i++) {
  1278. struct playlist *pls = c->playlists[i];
  1279. /* Make sure we've got one buffered packet from each open playlist
  1280. * stream */
  1281. if (pls->needed && !pls->pkt.data) {
  1282. while (1) {
  1283. int64_t ts_diff;
  1284. AVRational tb;
  1285. ret = av_read_frame(pls->ctx, &pls->pkt);
  1286. if (ret < 0) {
  1287. if (!url_feof(&pls->pb) && ret != AVERROR_EOF)
  1288. return ret;
  1289. reset_packet(&pls->pkt);
  1290. break;
  1291. } else {
  1292. /* stream_index check prevents matching picture attachments etc. */
  1293. if (pls->is_id3_timestamped && pls->pkt.stream_index == 0) {
  1294. /* audio elementary streams are id3 timestamped */
  1295. fill_timing_for_id3_timestamped_stream(pls);
  1296. }
  1297. if (c->first_timestamp == AV_NOPTS_VALUE &&
  1298. pls->pkt.dts != AV_NOPTS_VALUE)
  1299. c->first_timestamp = av_rescale_q(pls->pkt.dts,
  1300. get_timebase(pls), AV_TIME_BASE_Q);
  1301. }
  1302. if (pls->seek_timestamp == AV_NOPTS_VALUE)
  1303. break;
  1304. if (pls->pkt.dts == AV_NOPTS_VALUE) {
  1305. pls->seek_timestamp = AV_NOPTS_VALUE;
  1306. break;
  1307. }
  1308. tb = get_timebase(pls);
  1309. ts_diff = av_rescale_rnd(pls->pkt.dts, AV_TIME_BASE,
  1310. tb.den, AV_ROUND_DOWN) -
  1311. pls->seek_timestamp;
  1312. if (ts_diff >= 0 && (pls->seek_flags & AVSEEK_FLAG_ANY ||
  1313. pls->pkt.flags & AV_PKT_FLAG_KEY)) {
  1314. pls->seek_timestamp = AV_NOPTS_VALUE;
  1315. break;
  1316. }
  1317. av_free_packet(&pls->pkt);
  1318. reset_packet(&pls->pkt);
  1319. }
  1320. }
  1321. /* Check if this stream still is on an earlier segment number, or
  1322. * has the packet with the lowest dts */
  1323. if (pls->pkt.data) {
  1324. struct playlist *minpls = minplaylist < 0 ?
  1325. NULL : c->playlists[minplaylist];
  1326. if (minplaylist < 0 || pls->cur_seq_no < minpls->cur_seq_no) {
  1327. minplaylist = i;
  1328. } else if (pls->cur_seq_no == minpls->cur_seq_no) {
  1329. int64_t dts = pls->pkt.dts;
  1330. int64_t mindts = minpls->pkt.dts;
  1331. AVStream *st = pls->ctx->streams[pls->pkt.stream_index];
  1332. AVStream *minst = minpls->ctx->streams[minpls->pkt.stream_index];
  1333. AVRational tb = get_timebase( pls);
  1334. AVRational mintb = get_timebase(minpls);
  1335. if (dts == AV_NOPTS_VALUE) {
  1336. minplaylist = i;
  1337. } else if (mindts != AV_NOPTS_VALUE) {
  1338. if (st->start_time != AV_NOPTS_VALUE)
  1339. dts -= st->start_time;
  1340. if (minst->start_time != AV_NOPTS_VALUE)
  1341. mindts -= minst->start_time;
  1342. if (av_compare_ts(dts, tb,
  1343. mindts, mintb) < 0)
  1344. minplaylist = i;
  1345. }
  1346. }
  1347. }
  1348. }
  1349. if (c->end_of_segment) {
  1350. if (recheck_discard_flags(s, 0))
  1351. goto start;
  1352. }
  1353. /* If we got a packet, return it */
  1354. if (minplaylist >= 0) {
  1355. struct playlist *pls = c->playlists[minplaylist];
  1356. *pkt = pls->pkt;
  1357. pkt->stream_index += pls->stream_offset;
  1358. reset_packet(&c->playlists[minplaylist]->pkt);
  1359. if (pkt->dts != AV_NOPTS_VALUE)
  1360. c->cur_timestamp = av_rescale_q(pkt->dts,
  1361. pls->ctx->streams[pls->pkt.stream_index]->time_base,
  1362. AV_TIME_BASE_Q);
  1363. return 0;
  1364. }
  1365. return AVERROR_EOF;
  1366. }
  1367. static int hls_close(AVFormatContext *s)
  1368. {
  1369. HLSContext *c = s->priv_data;
  1370. free_playlist_list(c);
  1371. free_variant_list(c);
  1372. free_rendition_list(c);
  1373. return 0;
  1374. }
  1375. static int hls_read_seek(AVFormatContext *s, int stream_index,
  1376. int64_t timestamp, int flags)
  1377. {
  1378. HLSContext *c = s->priv_data;
  1379. int i;
  1380. int64_t seek_timestamp;
  1381. int valid_for = -1;
  1382. if ((flags & AVSEEK_FLAG_BYTE) || !c->variants[0]->playlists[0]->finished)
  1383. return AVERROR(ENOSYS);
  1384. seek_timestamp = stream_index < 0 ? timestamp :
  1385. av_rescale_rnd(timestamp, AV_TIME_BASE,
  1386. s->streams[stream_index]->time_base.den,
  1387. flags & AVSEEK_FLAG_BACKWARD ?
  1388. AV_ROUND_DOWN : AV_ROUND_UP);
  1389. if (s->duration < seek_timestamp)
  1390. return AVERROR(EIO);
  1391. for (i = 0; i < c->n_playlists; i++) {
  1392. /* check first that the timestamp is valid for some playlist */
  1393. struct playlist *pls = c->playlists[i];
  1394. int seq_no;
  1395. if (find_timestamp_in_playlist(c, pls, seek_timestamp, &seq_no)) {
  1396. /* set segment now so we do not need to search again below */
  1397. pls->cur_seq_no = seq_no;
  1398. valid_for = i;
  1399. break;
  1400. }
  1401. }
  1402. if (valid_for < 0)
  1403. return AVERROR(EIO);
  1404. for (i = 0; i < c->n_playlists; i++) {
  1405. /* Reset reading */
  1406. struct playlist *pls = c->playlists[i];
  1407. if (pls->input) {
  1408. ffurl_close(pls->input);
  1409. pls->input = NULL;
  1410. }
  1411. av_free_packet(&pls->pkt);
  1412. reset_packet(&pls->pkt);
  1413. pls->pb.eof_reached = 0;
  1414. /* Clear any buffered data */
  1415. pls->pb.buf_end = pls->pb.buf_ptr = pls->pb.buffer;
  1416. /* Reset the pos, to let the mpegts demuxer know we've seeked. */
  1417. pls->pb.pos = 0;
  1418. pls->seek_timestamp = seek_timestamp;
  1419. pls->seek_flags = flags;
  1420. /* set closest segment seq_no for playlists not handled above */
  1421. if (valid_for != i)
  1422. find_timestamp_in_playlist(c, pls, seek_timestamp, &pls->cur_seq_no);
  1423. }
  1424. c->cur_timestamp = seek_timestamp;
  1425. return 0;
  1426. }
  1427. static int hls_probe(AVProbeData *p)
  1428. {
  1429. /* Require #EXTM3U at the start, and either one of the ones below
  1430. * somewhere for a proper match. */
  1431. if (strncmp(p->buf, "#EXTM3U", 7))
  1432. return 0;
  1433. if (strstr(p->buf, "#EXT-X-STREAM-INF:") ||
  1434. strstr(p->buf, "#EXT-X-TARGETDURATION:") ||
  1435. strstr(p->buf, "#EXT-X-MEDIA-SEQUENCE:"))
  1436. return AVPROBE_SCORE_MAX;
  1437. return 0;
  1438. }
  1439. AVInputFormat ff_hls_demuxer = {
  1440. .name = "hls,applehttp",
  1441. .long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
  1442. .priv_data_size = sizeof(HLSContext),
  1443. .read_probe = hls_probe,
  1444. .read_header = hls_read_header,
  1445. .read_packet = hls_read_packet,
  1446. .read_close = hls_close,
  1447. .read_seek = hls_read_seek,
  1448. };