applehttpproto.c 9.5 KB

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