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.

320 lines
9.9KB

  1. /*
  2. * Apple HTTP Live Streaming Protocol Handler
  3. * Copyright (c) 2010 Martin Storsjo
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Apple HTTP Live Streaming Protocol Handler
  24. * https://www.rfc-editor.org/rfc/rfc8216.txt
  25. */
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/time.h"
  28. #include "avformat.h"
  29. #include "avio_internal.h"
  30. #include "internal.h"
  31. #include "url.h"
  32. #include "version.h"
  33. /*
  34. * An apple http stream consists of a playlist with media segment files,
  35. * played sequentially. There may be several playlists with the same
  36. * video content, in different bandwidth variants, that are played in
  37. * parallel (preferably only one bandwidth variant at a time). In this case,
  38. * the user supplied the url to a main playlist that only lists the variant
  39. * playlists.
  40. *
  41. * If the main playlist doesn't point at any variants, we still create
  42. * one anonymous toplevel variant for this, to maintain the structure.
  43. */
  44. struct segment {
  45. int64_t duration;
  46. char url[MAX_URL_SIZE];
  47. };
  48. struct variant {
  49. int bandwidth;
  50. char url[MAX_URL_SIZE];
  51. };
  52. typedef struct HLSContext {
  53. char playlisturl[MAX_URL_SIZE];
  54. int64_t target_duration;
  55. int start_seq_no;
  56. int finished;
  57. int n_segments;
  58. struct segment **segments;
  59. int n_variants;
  60. struct variant **variants;
  61. int cur_seq_no;
  62. URLContext *seg_hd;
  63. int64_t last_load_time;
  64. } HLSContext;
  65. static void free_segment_list(HLSContext *s)
  66. {
  67. int i;
  68. for (i = 0; i < s->n_segments; i++)
  69. av_freep(&s->segments[i]);
  70. av_freep(&s->segments);
  71. s->n_segments = 0;
  72. }
  73. static void free_variant_list(HLSContext *s)
  74. {
  75. int i;
  76. for (i = 0; i < s->n_variants; i++)
  77. av_freep(&s->variants[i]);
  78. av_freep(&s->variants);
  79. s->n_variants = 0;
  80. }
  81. struct variant_info {
  82. char bandwidth[20];
  83. };
  84. static void handle_variant_args(struct variant_info *info, const char *key,
  85. int key_len, char **dest, int *dest_len)
  86. {
  87. if (!strncmp(key, "BANDWIDTH=", key_len)) {
  88. *dest = info->bandwidth;
  89. *dest_len = sizeof(info->bandwidth);
  90. }
  91. }
  92. static int parse_playlist(URLContext *h, const char *url)
  93. {
  94. HLSContext *s = h->priv_data;
  95. AVIOContext *in;
  96. int ret = 0, is_segment = 0, is_variant = 0, bandwidth = 0;
  97. int64_t duration = 0;
  98. char line[1024];
  99. const char *ptr;
  100. if ((ret = ffio_open_whitelist(&in, url, AVIO_FLAG_READ,
  101. &h->interrupt_callback, NULL,
  102. h->protocol_whitelist, h->protocol_blacklist)) < 0)
  103. return ret;
  104. ff_get_chomp_line(in, line, sizeof(line));
  105. if (strcmp(line, "#EXTM3U")) {
  106. ret = AVERROR_INVALIDDATA;
  107. goto fail;
  108. }
  109. free_segment_list(s);
  110. s->finished = 0;
  111. while (!avio_feof(in)) {
  112. ff_get_chomp_line(in, line, sizeof(line));
  113. if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
  114. struct variant_info info = {{0}};
  115. is_variant = 1;
  116. ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,
  117. &info);
  118. bandwidth = atoi(info.bandwidth);
  119. } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
  120. s->target_duration = atoi(ptr) * AV_TIME_BASE;
  121. } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
  122. s->start_seq_no = atoi(ptr);
  123. } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
  124. s->finished = 1;
  125. } else if (av_strstart(line, "#EXTINF:", &ptr)) {
  126. is_segment = 1;
  127. duration = atof(ptr) * AV_TIME_BASE;
  128. } else if (av_strstart(line, "#", NULL)) {
  129. continue;
  130. } else if (line[0]) {
  131. if (is_segment) {
  132. struct segment *seg = av_malloc(sizeof(struct segment));
  133. if (!seg) {
  134. ret = AVERROR(ENOMEM);
  135. goto fail;
  136. }
  137. seg->duration = duration;
  138. ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);
  139. dynarray_add(&s->segments, &s->n_segments, seg);
  140. is_segment = 0;
  141. } else if (is_variant) {
  142. struct variant *var = av_malloc(sizeof(struct variant));
  143. if (!var) {
  144. ret = AVERROR(ENOMEM);
  145. goto fail;
  146. }
  147. var->bandwidth = bandwidth;
  148. ff_make_absolute_url(var->url, sizeof(var->url), url, line);
  149. dynarray_add(&s->variants, &s->n_variants, var);
  150. is_variant = 0;
  151. }
  152. }
  153. }
  154. s->last_load_time = av_gettime_relative();
  155. fail:
  156. avio_close(in);
  157. return ret;
  158. }
  159. static int hls_close(URLContext *h)
  160. {
  161. HLSContext *s = h->priv_data;
  162. free_segment_list(s);
  163. free_variant_list(s);
  164. ffurl_closep(&s->seg_hd);
  165. return 0;
  166. }
  167. static int hls_open(URLContext *h, const char *uri, int flags)
  168. {
  169. HLSContext *s = h->priv_data;
  170. int ret, i;
  171. const char *nested_url;
  172. if (flags & AVIO_FLAG_WRITE)
  173. return AVERROR(ENOSYS);
  174. h->is_streamed = 1;
  175. if (av_strstart(uri, "hls+", &nested_url)) {
  176. av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
  177. } else if (av_strstart(uri, "hls://", &nested_url)) {
  178. av_log(h, AV_LOG_ERROR,
  179. "No nested protocol specified. Specify e.g. hls+http://%s\n",
  180. nested_url);
  181. ret = AVERROR(EINVAL);
  182. goto fail;
  183. } else {
  184. av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
  185. ret = AVERROR(EINVAL);
  186. goto fail;
  187. }
  188. av_log(h, AV_LOG_WARNING,
  189. "Using the hls protocol is discouraged, please try using the "
  190. "hls demuxer instead. The hls demuxer should be more complete "
  191. "and work as well as the protocol implementation. (If not, "
  192. "please report it.) To use the demuxer, simply use %s as url.\n",
  193. s->playlisturl);
  194. if ((ret = parse_playlist(h, s->playlisturl)) < 0)
  195. goto fail;
  196. if (s->n_segments == 0 && s->n_variants > 0) {
  197. int max_bandwidth = 0, maxvar = -1;
  198. for (i = 0; i < s->n_variants; i++) {
  199. if (s->variants[i]->bandwidth > max_bandwidth || i == 0) {
  200. max_bandwidth = s->variants[i]->bandwidth;
  201. maxvar = i;
  202. }
  203. }
  204. av_strlcpy(s->playlisturl, s->variants[maxvar]->url,
  205. sizeof(s->playlisturl));
  206. if ((ret = parse_playlist(h, s->playlisturl)) < 0)
  207. goto fail;
  208. }
  209. if (s->n_segments == 0) {
  210. av_log(h, AV_LOG_WARNING, "Empty playlist\n");
  211. ret = AVERROR(EIO);
  212. goto fail;
  213. }
  214. s->cur_seq_no = s->start_seq_no;
  215. if (!s->finished && s->n_segments >= 3)
  216. s->cur_seq_no = s->start_seq_no + s->n_segments - 3;
  217. return 0;
  218. fail:
  219. hls_close(h);
  220. return ret;
  221. }
  222. static int hls_read(URLContext *h, uint8_t *buf, int size)
  223. {
  224. HLSContext *s = h->priv_data;
  225. const char *url;
  226. int ret;
  227. int64_t reload_interval;
  228. start:
  229. if (s->seg_hd) {
  230. ret = ffurl_read(s->seg_hd, buf, size);
  231. if (ret > 0)
  232. return ret;
  233. }
  234. if (s->seg_hd) {
  235. ffurl_closep(&s->seg_hd);
  236. s->cur_seq_no++;
  237. }
  238. reload_interval = s->n_segments > 0 ?
  239. s->segments[s->n_segments - 1]->duration :
  240. s->target_duration;
  241. retry:
  242. if (!s->finished) {
  243. int64_t now = av_gettime_relative();
  244. if (now - s->last_load_time >= reload_interval) {
  245. if ((ret = parse_playlist(h, s->playlisturl)) < 0)
  246. return ret;
  247. /* If we need to reload the playlist again below (if
  248. * there's still no more segments), switch to a reload
  249. * interval of half the target duration. */
  250. reload_interval = s->target_duration / 2;
  251. }
  252. }
  253. if (s->cur_seq_no < s->start_seq_no) {
  254. av_log(h, AV_LOG_WARNING,
  255. "skipping %d segments ahead, expired from playlist\n",
  256. s->start_seq_no - s->cur_seq_no);
  257. s->cur_seq_no = s->start_seq_no;
  258. }
  259. if (s->cur_seq_no - s->start_seq_no >= s->n_segments) {
  260. if (s->finished)
  261. return AVERROR_EOF;
  262. while (av_gettime_relative() - s->last_load_time < reload_interval) {
  263. if (ff_check_interrupt(&h->interrupt_callback))
  264. return AVERROR_EXIT;
  265. av_usleep(100*1000);
  266. }
  267. goto retry;
  268. }
  269. url = s->segments[s->cur_seq_no - s->start_seq_no]->url;
  270. av_log(h, AV_LOG_DEBUG, "opening %s\n", url);
  271. ret = ffurl_open_whitelist(&s->seg_hd, url, AVIO_FLAG_READ,
  272. &h->interrupt_callback, NULL,
  273. h->protocol_whitelist, h->protocol_blacklist, h);
  274. if (ret < 0) {
  275. if (ff_check_interrupt(&h->interrupt_callback))
  276. return AVERROR_EXIT;
  277. av_log(h, AV_LOG_WARNING, "Unable to open %s\n", url);
  278. s->cur_seq_no++;
  279. goto retry;
  280. }
  281. goto start;
  282. }
  283. const URLProtocol ff_hls_protocol = {
  284. .name = "hls",
  285. .url_open = hls_open,
  286. .url_read = hls_read,
  287. .url_close = hls_close,
  288. .flags = URL_PROTOCOL_FLAG_NESTED_SCHEME,
  289. .priv_data_size = sizeof(HLSContext),
  290. };