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.

324 lines
9.8KB

  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. * http://tools.ietf.org/html/draft-pantos-http-live-streaming
  25. */
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/time.h"
  28. #include "avformat.h"
  29. #include "internal.h"
  30. #include "url.h"
  31. #include "version.h"
  32. /*
  33. * An apple http stream consists of a playlist with media segment files,
  34. * played sequentially. There may be several playlists with the same
  35. * video content, in different bandwidth variants, that are played in
  36. * parallel (preferably only one bandwidth variant at a time). In this case,
  37. * the user supplied the url to a main playlist that only lists the variant
  38. * playlists.
  39. *
  40. * If the main playlist doesn't point at any variants, we still create
  41. * one anonymous toplevel variant for this, to maintain the structure.
  42. */
  43. struct segment {
  44. int duration;
  45. char url[MAX_URL_SIZE];
  46. };
  47. struct variant {
  48. int bandwidth;
  49. char url[MAX_URL_SIZE];
  50. };
  51. typedef struct HLSContext {
  52. char playlisturl[MAX_URL_SIZE];
  53. int target_duration;
  54. int start_seq_no;
  55. int finished;
  56. int n_segments;
  57. struct segment **segments;
  58. int n_variants;
  59. struct variant **variants;
  60. int cur_seq_no;
  61. URLContext *seg_hd;
  62. int64_t last_load_time;
  63. } HLSContext;
  64. static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
  65. {
  66. int len = ff_get_line(s, buf, maxlen);
  67. while (len > 0 && av_isspace(buf[len - 1]))
  68. buf[--len] = '\0';
  69. return len;
  70. }
  71. static void free_segment_list(HLSContext *s)
  72. {
  73. int i;
  74. for (i = 0; i < s->n_segments; i++)
  75. av_free(s->segments[i]);
  76. av_freep(&s->segments);
  77. s->n_segments = 0;
  78. }
  79. static void free_variant_list(HLSContext *s)
  80. {
  81. int i;
  82. for (i = 0; i < s->n_variants; i++)
  83. av_free(s->variants[i]);
  84. av_freep(&s->variants);
  85. s->n_variants = 0;
  86. }
  87. struct variant_info {
  88. char bandwidth[20];
  89. };
  90. static void handle_variant_args(struct variant_info *info, const char *key,
  91. int key_len, char **dest, int *dest_len)
  92. {
  93. if (!strncmp(key, "BANDWIDTH=", key_len)) {
  94. *dest = info->bandwidth;
  95. *dest_len = sizeof(info->bandwidth);
  96. }
  97. }
  98. static int parse_playlist(URLContext *h, const char *url)
  99. {
  100. HLSContext *s = h->priv_data;
  101. AVIOContext *in;
  102. int ret = 0, duration = 0, is_segment = 0, is_variant = 0, bandwidth = 0;
  103. char line[1024];
  104. const char *ptr;
  105. if ((ret = avio_open2(&in, url, AVIO_FLAG_READ,
  106. &h->interrupt_callback, NULL)) < 0)
  107. return ret;
  108. read_chomp_line(in, line, sizeof(line));
  109. if (strcmp(line, "#EXTM3U"))
  110. return AVERROR_INVALIDDATA;
  111. free_segment_list(s);
  112. s->finished = 0;
  113. while (!url_feof(in)) {
  114. read_chomp_line(in, line, sizeof(line));
  115. if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
  116. struct variant_info info = {{0}};
  117. is_variant = 1;
  118. ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,
  119. &info);
  120. bandwidth = atoi(info.bandwidth);
  121. } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
  122. s->target_duration = atoi(ptr);
  123. } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
  124. s->start_seq_no = atoi(ptr);
  125. } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
  126. s->finished = 1;
  127. } else if (av_strstart(line, "#EXTINF:", &ptr)) {
  128. is_segment = 1;
  129. duration = atoi(ptr);
  130. } else if (av_strstart(line, "#", NULL)) {
  131. continue;
  132. } else if (line[0]) {
  133. if (is_segment) {
  134. struct segment *seg = av_malloc(sizeof(struct segment));
  135. if (!seg) {
  136. ret = AVERROR(ENOMEM);
  137. goto fail;
  138. }
  139. seg->duration = duration;
  140. ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);
  141. dynarray_add(&s->segments, &s->n_segments, seg);
  142. is_segment = 0;
  143. } else if (is_variant) {
  144. struct variant *var = av_malloc(sizeof(struct variant));
  145. if (!var) {
  146. ret = AVERROR(ENOMEM);
  147. goto fail;
  148. }
  149. var->bandwidth = bandwidth;
  150. ff_make_absolute_url(var->url, sizeof(var->url), url, line);
  151. dynarray_add(&s->variants, &s->n_variants, var);
  152. is_variant = 0;
  153. }
  154. }
  155. }
  156. s->last_load_time = av_gettime();
  157. fail:
  158. avio_close(in);
  159. return ret;
  160. }
  161. static int hls_close(URLContext *h)
  162. {
  163. HLSContext *s = h->priv_data;
  164. free_segment_list(s);
  165. free_variant_list(s);
  166. ffurl_close(s->seg_hd);
  167. return 0;
  168. }
  169. static int hls_open(URLContext *h, const char *uri, int flags)
  170. {
  171. HLSContext *s = h->priv_data;
  172. int ret, i;
  173. const char *nested_url;
  174. if (flags & AVIO_FLAG_WRITE)
  175. return AVERROR(ENOSYS);
  176. h->is_streamed = 1;
  177. if (av_strstart(uri, "hls+", &nested_url)) {
  178. av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
  179. } else if (av_strstart(uri, "hls://", &nested_url)) {
  180. av_log(h, AV_LOG_ERROR,
  181. "No nested protocol specified. Specify e.g. hls+http://%s\n",
  182. nested_url);
  183. ret = AVERROR(EINVAL);
  184. goto fail;
  185. } else {
  186. av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
  187. ret = AVERROR(EINVAL);
  188. goto fail;
  189. }
  190. av_log(h, AV_LOG_WARNING,
  191. "Using the hls protocol is discouraged, please try using the "
  192. "hls demuxer instead. The hls demuxer should be more complete "
  193. "and work as well as the protocol implementation. (If not, "
  194. "please report it.) To use the demuxer, simply use %s as url.\n",
  195. s->playlisturl);
  196. if ((ret = parse_playlist(h, s->playlisturl)) < 0)
  197. goto fail;
  198. if (s->n_segments == 0 && s->n_variants > 0) {
  199. int max_bandwidth = 0, maxvar = -1;
  200. for (i = 0; i < s->n_variants; i++) {
  201. if (s->variants[i]->bandwidth > max_bandwidth || i == 0) {
  202. max_bandwidth = s->variants[i]->bandwidth;
  203. maxvar = i;
  204. }
  205. }
  206. av_strlcpy(s->playlisturl, s->variants[maxvar]->url,
  207. sizeof(s->playlisturl));
  208. if ((ret = parse_playlist(h, s->playlisturl)) < 0)
  209. goto fail;
  210. }
  211. if (s->n_segments == 0) {
  212. av_log(h, AV_LOG_WARNING, "Empty playlist\n");
  213. ret = AVERROR(EIO);
  214. goto fail;
  215. }
  216. s->cur_seq_no = s->start_seq_no;
  217. if (!s->finished && s->n_segments >= 3)
  218. s->cur_seq_no = s->start_seq_no + s->n_segments - 3;
  219. return 0;
  220. fail:
  221. hls_close(h);
  222. return ret;
  223. }
  224. static int hls_read(URLContext *h, uint8_t *buf, int size)
  225. {
  226. HLSContext *s = h->priv_data;
  227. const char *url;
  228. int ret;
  229. int64_t reload_interval;
  230. start:
  231. if (s->seg_hd) {
  232. ret = ffurl_read(s->seg_hd, buf, size);
  233. if (ret > 0)
  234. return ret;
  235. }
  236. if (s->seg_hd) {
  237. ffurl_close(s->seg_hd);
  238. s->seg_hd = NULL;
  239. s->cur_seq_no++;
  240. }
  241. reload_interval = s->n_segments > 0 ?
  242. s->segments[s->n_segments - 1]->duration :
  243. s->target_duration;
  244. reload_interval *= 1000000;
  245. retry:
  246. if (!s->finished) {
  247. int64_t now = av_gettime();
  248. if (now - s->last_load_time >= reload_interval) {
  249. if ((ret = parse_playlist(h, s->playlisturl)) < 0)
  250. return ret;
  251. /* If we need to reload the playlist again below (if
  252. * there's still no more segments), switch to a reload
  253. * interval of half the target duration. */
  254. reload_interval = s->target_duration * 500000LL;
  255. }
  256. }
  257. if (s->cur_seq_no < s->start_seq_no) {
  258. av_log(h, AV_LOG_WARNING,
  259. "skipping %d segments ahead, expired from playlist\n",
  260. s->start_seq_no - s->cur_seq_no);
  261. s->cur_seq_no = s->start_seq_no;
  262. }
  263. if (s->cur_seq_no - s->start_seq_no >= s->n_segments) {
  264. if (s->finished)
  265. return AVERROR_EOF;
  266. while (av_gettime() - s->last_load_time < reload_interval) {
  267. if (ff_check_interrupt(&h->interrupt_callback))
  268. return AVERROR_EXIT;
  269. av_usleep(100*1000);
  270. }
  271. goto retry;
  272. }
  273. url = s->segments[s->cur_seq_no - s->start_seq_no]->url,
  274. av_log(h, AV_LOG_DEBUG, "opening %s\n", url);
  275. ret = ffurl_open(&s->seg_hd, url, AVIO_FLAG_READ,
  276. &h->interrupt_callback, NULL);
  277. if (ret < 0) {
  278. if (ff_check_interrupt(&h->interrupt_callback))
  279. return AVERROR_EXIT;
  280. av_log(h, AV_LOG_WARNING, "Unable to open %s\n", url);
  281. s->cur_seq_no++;
  282. goto retry;
  283. }
  284. goto start;
  285. }
  286. URLProtocol ff_hls_protocol = {
  287. .name = "hls",
  288. .url_open = hls_open,
  289. .url_read = hls_read,
  290. .url_close = hls_close,
  291. .flags = URL_PROTOCOL_FLAG_NESTED_SCHEME,
  292. .priv_data_size = sizeof(HLSContext),
  293. };