hlsproto.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. return AVERROR_INVALIDDATA;
  112. free_segment_list(s);
  113. s->finished = 0;
  114. while (!in->eof_reached) {
  115. read_chomp_line(in, line, sizeof(line));
  116. if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
  117. struct variant_info info = {{0}};
  118. is_variant = 1;
  119. ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,
  120. &info);
  121. bandwidth = atoi(info.bandwidth);
  122. } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
  123. s->target_duration = atoi(ptr) * AV_TIME_BASE;
  124. } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
  125. s->start_seq_no = atoi(ptr);
  126. } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
  127. s->finished = 1;
  128. } else if (av_strstart(line, "#EXTINF:", &ptr)) {
  129. is_segment = 1;
  130. duration = atof(ptr) * AV_TIME_BASE;
  131. } else if (av_strstart(line, "#", NULL)) {
  132. continue;
  133. } else if (line[0]) {
  134. if (is_segment) {
  135. struct segment *seg = av_malloc(sizeof(struct segment));
  136. if (!seg) {
  137. ret = AVERROR(ENOMEM);
  138. goto fail;
  139. }
  140. seg->duration = duration;
  141. ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);
  142. dynarray_add(&s->segments, &s->n_segments, seg);
  143. is_segment = 0;
  144. } else if (is_variant) {
  145. struct variant *var = av_malloc(sizeof(struct variant));
  146. if (!var) {
  147. ret = AVERROR(ENOMEM);
  148. goto fail;
  149. }
  150. var->bandwidth = bandwidth;
  151. ff_make_absolute_url(var->url, sizeof(var->url), url, line);
  152. dynarray_add(&s->variants, &s->n_variants, var);
  153. is_variant = 0;
  154. }
  155. }
  156. }
  157. s->last_load_time = av_gettime();
  158. fail:
  159. avio_close(in);
  160. return ret;
  161. }
  162. static int hls_close(URLContext *h)
  163. {
  164. HLSContext *s = h->priv_data;
  165. free_segment_list(s);
  166. free_variant_list(s);
  167. ffurl_close(s->seg_hd);
  168. return 0;
  169. }
  170. static int hls_open(URLContext *h, const char *uri, int flags)
  171. {
  172. HLSContext *s = h->priv_data;
  173. int ret, i;
  174. const char *nested_url;
  175. if (flags & AVIO_FLAG_WRITE)
  176. return AVERROR(ENOSYS);
  177. h->is_streamed = 1;
  178. if (av_strstart(uri, "hls+", &nested_url)) {
  179. av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
  180. } else if (av_strstart(uri, "hls://", &nested_url)) {
  181. av_log(h, AV_LOG_ERROR,
  182. "No nested protocol specified. Specify e.g. hls+http://%s\n",
  183. nested_url);
  184. ret = AVERROR(EINVAL);
  185. goto fail;
  186. } else {
  187. av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
  188. ret = AVERROR(EINVAL);
  189. goto fail;
  190. }
  191. av_log(h, AV_LOG_WARNING,
  192. "Using the hls protocol is discouraged, please try using the "
  193. "hls demuxer instead. The hls demuxer should be more complete "
  194. "and work as well as the protocol implementation. (If not, "
  195. "please report it.) To use the demuxer, simply use %s as url.\n",
  196. s->playlisturl);
  197. if ((ret = parse_playlist(h, s->playlisturl)) < 0)
  198. goto fail;
  199. if (s->n_segments == 0 && s->n_variants > 0) {
  200. int max_bandwidth = 0, maxvar = -1;
  201. for (i = 0; i < s->n_variants; i++) {
  202. if (s->variants[i]->bandwidth > max_bandwidth || i == 0) {
  203. max_bandwidth = s->variants[i]->bandwidth;
  204. maxvar = i;
  205. }
  206. }
  207. av_strlcpy(s->playlisturl, s->variants[maxvar]->url,
  208. sizeof(s->playlisturl));
  209. if ((ret = parse_playlist(h, s->playlisturl)) < 0)
  210. goto fail;
  211. }
  212. if (s->n_segments == 0) {
  213. av_log(h, AV_LOG_WARNING, "Empty playlist\n");
  214. ret = AVERROR(EIO);
  215. goto fail;
  216. }
  217. s->cur_seq_no = s->start_seq_no;
  218. if (!s->finished && s->n_segments >= 3)
  219. s->cur_seq_no = s->start_seq_no + s->n_segments - 3;
  220. return 0;
  221. fail:
  222. hls_close(h);
  223. return ret;
  224. }
  225. static int hls_read(URLContext *h, uint8_t *buf, int size)
  226. {
  227. HLSContext *s = h->priv_data;
  228. const char *url;
  229. int ret;
  230. int64_t reload_interval;
  231. start:
  232. if (s->seg_hd) {
  233. ret = ffurl_read(s->seg_hd, buf, size);
  234. if (ret > 0)
  235. return ret;
  236. }
  237. if (s->seg_hd) {
  238. ffurl_close(s->seg_hd);
  239. s->seg_hd = NULL;
  240. s->cur_seq_no++;
  241. }
  242. reload_interval = s->n_segments > 0 ?
  243. s->segments[s->n_segments - 1]->duration :
  244. s->target_duration;
  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 / 2;
  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. };