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.

326 lines
9.9KB

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