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.

329 lines
10KB

  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 "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 int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
  66. {
  67. int len = ff_get_line(s, buf, maxlen);
  68. while (len > 0 && av_isspace(buf[len - 1]))
  69. buf[--len] = '\0';
  70. return len;
  71. }
  72. static void free_segment_list(HLSContext *s)
  73. {
  74. int i;
  75. for (i = 0; i < s->n_segments; i++)
  76. av_freep(&s->segments[i]);
  77. av_freep(&s->segments);
  78. s->n_segments = 0;
  79. }
  80. static void free_variant_list(HLSContext *s)
  81. {
  82. int i;
  83. for (i = 0; i < s->n_variants; i++)
  84. av_freep(&s->variants[i]);
  85. av_freep(&s->variants);
  86. s->n_variants = 0;
  87. }
  88. struct variant_info {
  89. char bandwidth[20];
  90. };
  91. static void handle_variant_args(struct variant_info *info, const char *key,
  92. int key_len, char **dest, int *dest_len)
  93. {
  94. if (!strncmp(key, "BANDWIDTH=", key_len)) {
  95. *dest = info->bandwidth;
  96. *dest_len = sizeof(info->bandwidth);
  97. }
  98. }
  99. static int parse_playlist(URLContext *h, const char *url)
  100. {
  101. HLSContext *s = h->priv_data;
  102. AVIOContext *in;
  103. int ret = 0, is_segment = 0, is_variant = 0, bandwidth = 0;
  104. int64_t duration = 0;
  105. char line[1024];
  106. const char *ptr;
  107. if ((ret = ffio_open_whitelist(&in, url, AVIO_FLAG_READ,
  108. &h->interrupt_callback, NULL,
  109. h->protocol_whitelist)) < 0)
  110. return ret;
  111. read_chomp_line(in, line, sizeof(line));
  112. if (strcmp(line, "#EXTM3U")) {
  113. ret = AVERROR_INVALIDDATA;
  114. goto fail;
  115. }
  116. free_segment_list(s);
  117. s->finished = 0;
  118. while (!avio_feof(in)) {
  119. read_chomp_line(in, line, sizeof(line));
  120. if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
  121. struct variant_info info = {{0}};
  122. is_variant = 1;
  123. ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,
  124. &info);
  125. bandwidth = atoi(info.bandwidth);
  126. } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
  127. s->target_duration = atoi(ptr) * AV_TIME_BASE;
  128. } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
  129. s->start_seq_no = atoi(ptr);
  130. } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
  131. s->finished = 1;
  132. } else if (av_strstart(line, "#EXTINF:", &ptr)) {
  133. is_segment = 1;
  134. duration = atof(ptr) * AV_TIME_BASE;
  135. } else if (av_strstart(line, "#", NULL)) {
  136. continue;
  137. } else if (line[0]) {
  138. if (is_segment) {
  139. struct segment *seg = av_malloc(sizeof(struct segment));
  140. if (!seg) {
  141. ret = AVERROR(ENOMEM);
  142. goto fail;
  143. }
  144. seg->duration = duration;
  145. ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);
  146. dynarray_add(&s->segments, &s->n_segments, seg);
  147. is_segment = 0;
  148. } else if (is_variant) {
  149. struct variant *var = av_malloc(sizeof(struct variant));
  150. if (!var) {
  151. ret = AVERROR(ENOMEM);
  152. goto fail;
  153. }
  154. var->bandwidth = bandwidth;
  155. ff_make_absolute_url(var->url, sizeof(var->url), url, line);
  156. dynarray_add(&s->variants, &s->n_variants, var);
  157. is_variant = 0;
  158. }
  159. }
  160. }
  161. s->last_load_time = av_gettime_relative();
  162. fail:
  163. avio_close(in);
  164. return ret;
  165. }
  166. static int hls_close(URLContext *h)
  167. {
  168. HLSContext *s = h->priv_data;
  169. free_segment_list(s);
  170. free_variant_list(s);
  171. ffurl_close(s->seg_hd);
  172. return 0;
  173. }
  174. static int hls_open(URLContext *h, const char *uri, int flags)
  175. {
  176. HLSContext *s = h->priv_data;
  177. int ret, i;
  178. const char *nested_url;
  179. if (flags & AVIO_FLAG_WRITE)
  180. return AVERROR(ENOSYS);
  181. h->is_streamed = 1;
  182. if (av_strstart(uri, "hls+", &nested_url)) {
  183. av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
  184. } else if (av_strstart(uri, "hls://", &nested_url)) {
  185. av_log(h, AV_LOG_ERROR,
  186. "No nested protocol specified. Specify e.g. hls+http://%s\n",
  187. nested_url);
  188. ret = AVERROR(EINVAL);
  189. goto fail;
  190. } else {
  191. av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
  192. ret = AVERROR(EINVAL);
  193. goto fail;
  194. }
  195. av_log(h, AV_LOG_WARNING,
  196. "Using the hls protocol is discouraged, please try using the "
  197. "hls demuxer instead. The hls demuxer should be more complete "
  198. "and work as well as the protocol implementation. (If not, "
  199. "please report it.) To use the demuxer, simply use %s as url.\n",
  200. s->playlisturl);
  201. if ((ret = parse_playlist(h, s->playlisturl)) < 0)
  202. goto fail;
  203. if (s->n_segments == 0 && s->n_variants > 0) {
  204. int max_bandwidth = 0, maxvar = -1;
  205. for (i = 0; i < s->n_variants; i++) {
  206. if (s->variants[i]->bandwidth > max_bandwidth || i == 0) {
  207. max_bandwidth = s->variants[i]->bandwidth;
  208. maxvar = i;
  209. }
  210. }
  211. av_strlcpy(s->playlisturl, s->variants[maxvar]->url,
  212. sizeof(s->playlisturl));
  213. if ((ret = parse_playlist(h, s->playlisturl)) < 0)
  214. goto fail;
  215. }
  216. if (s->n_segments == 0) {
  217. av_log(h, AV_LOG_WARNING, "Empty playlist\n");
  218. ret = AVERROR(EIO);
  219. goto fail;
  220. }
  221. s->cur_seq_no = s->start_seq_no;
  222. if (!s->finished && s->n_segments >= 3)
  223. s->cur_seq_no = s->start_seq_no + s->n_segments - 3;
  224. return 0;
  225. fail:
  226. hls_close(h);
  227. return ret;
  228. }
  229. static int hls_read(URLContext *h, uint8_t *buf, int size)
  230. {
  231. HLSContext *s = h->priv_data;
  232. const char *url;
  233. int ret;
  234. int64_t reload_interval;
  235. start:
  236. if (s->seg_hd) {
  237. ret = ffurl_read(s->seg_hd, buf, size);
  238. if (ret > 0)
  239. return ret;
  240. }
  241. if (s->seg_hd) {
  242. ffurl_close(s->seg_hd);
  243. s->seg_hd = NULL;
  244. s->cur_seq_no++;
  245. }
  246. reload_interval = s->n_segments > 0 ?
  247. s->segments[s->n_segments - 1]->duration :
  248. s->target_duration;
  249. retry:
  250. if (!s->finished) {
  251. int64_t now = av_gettime_relative();
  252. if (now - s->last_load_time >= reload_interval) {
  253. if ((ret = parse_playlist(h, s->playlisturl)) < 0)
  254. return ret;
  255. /* If we need to reload the playlist again below (if
  256. * there's still no more segments), switch to a reload
  257. * interval of half the target duration. */
  258. reload_interval = s->target_duration / 2;
  259. }
  260. }
  261. if (s->cur_seq_no < s->start_seq_no) {
  262. av_log(h, AV_LOG_WARNING,
  263. "skipping %d segments ahead, expired from playlist\n",
  264. s->start_seq_no - s->cur_seq_no);
  265. s->cur_seq_no = s->start_seq_no;
  266. }
  267. if (s->cur_seq_no - s->start_seq_no >= s->n_segments) {
  268. if (s->finished)
  269. return AVERROR_EOF;
  270. while (av_gettime_relative() - s->last_load_time < reload_interval) {
  271. if (ff_check_interrupt(&h->interrupt_callback))
  272. return AVERROR_EXIT;
  273. av_usleep(100*1000);
  274. }
  275. goto retry;
  276. }
  277. url = s->segments[s->cur_seq_no - s->start_seq_no]->url,
  278. av_log(h, AV_LOG_DEBUG, "opening %s\n", url);
  279. ret = ffurl_open_whitelist(&s->seg_hd, url, AVIO_FLAG_READ,
  280. &h->interrupt_callback, NULL,
  281. h->protocol_whitelist);
  282. if (ret < 0) {
  283. if (ff_check_interrupt(&h->interrupt_callback))
  284. return AVERROR_EXIT;
  285. av_log(h, AV_LOG_WARNING, "Unable to open %s\n", url);
  286. s->cur_seq_no++;
  287. goto retry;
  288. }
  289. goto start;
  290. }
  291. const URLProtocol ff_hls_protocol = {
  292. .name = "hls",
  293. .url_open = hls_open,
  294. .url_read = hls_read,
  295. .url_close = hls_close,
  296. .flags = URL_PROTOCOL_FLAG_NESTED_SCHEME,
  297. .priv_data_size = sizeof(HLSContext),
  298. };