hlsproto.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 && 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. #if FF_API_APPLEHTTP_PROTO
  186. } else if (av_strstart(uri, "applehttp+", &nested_url)) {
  187. av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
  188. av_log(h, AV_LOG_WARNING,
  189. "The applehttp protocol is deprecated, use hls+%s as url "
  190. "instead.\n", nested_url);
  191. } else if (av_strstart(uri, "applehttp://", &nested_url)) {
  192. av_strlcpy(s->playlisturl, "http://", sizeof(s->playlisturl));
  193. av_strlcat(s->playlisturl, nested_url, sizeof(s->playlisturl));
  194. av_log(h, AV_LOG_WARNING,
  195. "The applehttp protocol is deprecated, use hls+http://%s as url "
  196. "instead.\n", nested_url);
  197. #endif
  198. } else {
  199. av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
  200. ret = AVERROR(EINVAL);
  201. goto fail;
  202. }
  203. av_log(h, AV_LOG_WARNING,
  204. "Using the hls protocol is discouraged, please try using the "
  205. "hls demuxer instead. The hls demuxer should be more complete "
  206. "and work as well as the protocol implementation. (If not, "
  207. "please report it.) To use the demuxer, simply use %s as url.\n",
  208. s->playlisturl);
  209. if ((ret = parse_playlist(h, s->playlisturl)) < 0)
  210. goto fail;
  211. if (s->n_segments == 0 && s->n_variants > 0) {
  212. int max_bandwidth = 0, maxvar = -1;
  213. for (i = 0; i < s->n_variants; i++) {
  214. if (s->variants[i]->bandwidth > max_bandwidth || i == 0) {
  215. max_bandwidth = s->variants[i]->bandwidth;
  216. maxvar = i;
  217. }
  218. }
  219. av_strlcpy(s->playlisturl, s->variants[maxvar]->url,
  220. sizeof(s->playlisturl));
  221. if ((ret = parse_playlist(h, s->playlisturl)) < 0)
  222. goto fail;
  223. }
  224. if (s->n_segments == 0) {
  225. av_log(h, AV_LOG_WARNING, "Empty playlist\n");
  226. ret = AVERROR(EIO);
  227. goto fail;
  228. }
  229. s->cur_seq_no = s->start_seq_no;
  230. if (!s->finished && s->n_segments >= 3)
  231. s->cur_seq_no = s->start_seq_no + s->n_segments - 3;
  232. return 0;
  233. fail:
  234. hls_close(h);
  235. return ret;
  236. }
  237. static int hls_read(URLContext *h, uint8_t *buf, int size)
  238. {
  239. HLSContext *s = h->priv_data;
  240. const char *url;
  241. int ret;
  242. int64_t reload_interval;
  243. start:
  244. if (s->seg_hd) {
  245. ret = ffurl_read(s->seg_hd, buf, size);
  246. if (ret > 0)
  247. return ret;
  248. }
  249. if (s->seg_hd) {
  250. ffurl_close(s->seg_hd);
  251. s->seg_hd = NULL;
  252. s->cur_seq_no++;
  253. }
  254. reload_interval = s->n_segments > 0 ?
  255. s->segments[s->n_segments - 1]->duration :
  256. s->target_duration;
  257. reload_interval *= 1000000;
  258. retry:
  259. if (!s->finished) {
  260. int64_t now = av_gettime();
  261. if (now - s->last_load_time >= reload_interval) {
  262. if ((ret = parse_playlist(h, s->playlisturl)) < 0)
  263. return ret;
  264. /* If we need to reload the playlist again below (if
  265. * there's still no more segments), switch to a reload
  266. * interval of half the target duration. */
  267. reload_interval = s->target_duration * 500000;
  268. }
  269. }
  270. if (s->cur_seq_no < s->start_seq_no) {
  271. av_log(h, AV_LOG_WARNING,
  272. "skipping %d segments ahead, expired from playlist\n",
  273. s->start_seq_no - s->cur_seq_no);
  274. s->cur_seq_no = s->start_seq_no;
  275. }
  276. if (s->cur_seq_no - s->start_seq_no >= s->n_segments) {
  277. if (s->finished)
  278. return AVERROR_EOF;
  279. while (av_gettime() - s->last_load_time < reload_interval) {
  280. if (ff_check_interrupt(&h->interrupt_callback))
  281. return AVERROR_EXIT;
  282. av_usleep(100*1000);
  283. }
  284. goto retry;
  285. }
  286. url = s->segments[s->cur_seq_no - s->start_seq_no]->url,
  287. av_log(h, AV_LOG_DEBUG, "opening %s\n", url);
  288. ret = ffurl_open(&s->seg_hd, url, AVIO_FLAG_READ,
  289. &h->interrupt_callback, NULL);
  290. if (ret < 0) {
  291. if (ff_check_interrupt(&h->interrupt_callback))
  292. return AVERROR_EXIT;
  293. av_log(h, AV_LOG_WARNING, "Unable to open %s\n", url);
  294. s->cur_seq_no++;
  295. goto retry;
  296. }
  297. goto start;
  298. }
  299. #if FF_API_APPLEHTTP_PROTO
  300. URLProtocol ff_applehttp_protocol = {
  301. .name = "applehttp",
  302. .url_open = hls_open,
  303. .url_read = hls_read,
  304. .url_close = hls_close,
  305. .flags = URL_PROTOCOL_FLAG_NESTED_SCHEME,
  306. .priv_data_size = sizeof(HLSContext),
  307. };
  308. #endif
  309. URLProtocol ff_hls_protocol = {
  310. .name = "hls",
  311. .url_open = hls_open,
  312. .url_read = hls_read,
  313. .url_close = hls_close,
  314. .flags = URL_PROTOCOL_FLAG_NESTED_SCHEME,
  315. .priv_data_size = sizeof(HLSContext),
  316. };