applehttpproto.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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_open(&in, url, AVIO_RDONLY)) < 0)
  105. return ret;
  106. read_chomp_line(in, line, sizeof(line));
  107. if (strcmp(line, "#EXTM3U"))
  108. return AVERROR_INVALIDDATA;
  109. free_segment_list(s);
  110. s->finished = 0;
  111. while (!url_feof(in)) {
  112. read_chomp_line(in, line, sizeof(line));
  113. if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
  114. struct variant_info info = {{0}};
  115. is_variant = 1;
  116. ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,
  117. &info);
  118. bandwidth = atoi(info.bandwidth);
  119. } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
  120. s->target_duration = atoi(ptr);
  121. } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
  122. s->start_seq_no = atoi(ptr);
  123. } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
  124. s->finished = 1;
  125. } else if (av_strstart(line, "#EXTINF:", &ptr)) {
  126. is_segment = 1;
  127. duration = atoi(ptr);
  128. } else if (av_strstart(line, "#", NULL)) {
  129. continue;
  130. } else if (line[0]) {
  131. if (is_segment) {
  132. struct segment *seg = av_malloc(sizeof(struct segment));
  133. if (!seg) {
  134. ret = AVERROR(ENOMEM);
  135. goto fail;
  136. }
  137. seg->duration = duration;
  138. ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);
  139. dynarray_add(&s->segments, &s->n_segments, seg);
  140. is_segment = 0;
  141. } else if (is_variant) {
  142. struct variant *var = av_malloc(sizeof(struct variant));
  143. if (!var) {
  144. ret = AVERROR(ENOMEM);
  145. goto fail;
  146. }
  147. var->bandwidth = bandwidth;
  148. ff_make_absolute_url(var->url, sizeof(var->url), url, line);
  149. dynarray_add(&s->variants, &s->n_variants, var);
  150. is_variant = 0;
  151. }
  152. }
  153. }
  154. s->last_load_time = av_gettime();
  155. fail:
  156. avio_close(in);
  157. return ret;
  158. }
  159. static int applehttp_open(URLContext *h, const char *uri, int flags)
  160. {
  161. AppleHTTPContext *s;
  162. int ret, i;
  163. const char *nested_url;
  164. if (flags & (AVIO_WRONLY | AVIO_RDWR))
  165. return AVERROR(ENOSYS);
  166. s = av_mallocz(sizeof(AppleHTTPContext));
  167. if (!s)
  168. return AVERROR(ENOMEM);
  169. h->priv_data = s;
  170. h->is_streamed = 1;
  171. if (av_strstart(uri, "applehttp+", &nested_url)) {
  172. av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
  173. } else if (av_strstart(uri, "applehttp://", &nested_url)) {
  174. av_strlcpy(s->playlisturl, "http://", sizeof(s->playlisturl));
  175. av_strlcat(s->playlisturl, nested_url, sizeof(s->playlisturl));
  176. } else {
  177. av_log(NULL, AV_LOG_ERROR, "Unsupported url %s\n", uri);
  178. ret = AVERROR(EINVAL);
  179. goto fail;
  180. }
  181. if ((ret = parse_playlist(h, s->playlisturl)) < 0)
  182. goto fail;
  183. if (s->n_segments == 0 && s->n_variants > 0) {
  184. int max_bandwidth = 0, maxvar = -1;
  185. for (i = 0; i < s->n_variants; i++) {
  186. if (s->variants[i]->bandwidth > max_bandwidth || i == 0) {
  187. max_bandwidth = s->variants[i]->bandwidth;
  188. maxvar = i;
  189. }
  190. }
  191. av_strlcpy(s->playlisturl, s->variants[maxvar]->url,
  192. sizeof(s->playlisturl));
  193. if ((ret = parse_playlist(h, s->playlisturl)) < 0)
  194. goto fail;
  195. }
  196. if (s->n_segments == 0) {
  197. av_log(NULL, AV_LOG_WARNING, "Empty playlist\n");
  198. ret = AVERROR(EIO);
  199. goto fail;
  200. }
  201. s->cur_seq_no = s->start_seq_no;
  202. if (!s->finished && s->n_segments >= 3)
  203. s->cur_seq_no = s->start_seq_no + s->n_segments - 3;
  204. return 0;
  205. fail:
  206. av_free(s);
  207. return ret;
  208. }
  209. static int applehttp_read(URLContext *h, uint8_t *buf, int size)
  210. {
  211. AppleHTTPContext *s = h->priv_data;
  212. const char *url;
  213. int ret;
  214. start:
  215. if (s->seg_hd) {
  216. ret = ffurl_read(s->seg_hd, buf, size);
  217. if (ret > 0)
  218. return ret;
  219. }
  220. if (s->seg_hd) {
  221. ffurl_close(s->seg_hd);
  222. s->seg_hd = NULL;
  223. s->cur_seq_no++;
  224. }
  225. retry:
  226. if (!s->finished) {
  227. int64_t now = av_gettime();
  228. if (now - s->last_load_time >= s->target_duration*1000000)
  229. if ((ret = parse_playlist(h, s->playlisturl)) < 0)
  230. return ret;
  231. }
  232. if (s->cur_seq_no < s->start_seq_no) {
  233. av_log(NULL, AV_LOG_WARNING,
  234. "skipping %d segments ahead, expired from playlist\n",
  235. s->start_seq_no - s->cur_seq_no);
  236. s->cur_seq_no = s->start_seq_no;
  237. }
  238. if (s->cur_seq_no - s->start_seq_no >= s->n_segments) {
  239. if (s->finished)
  240. return AVERROR_EOF;
  241. while (av_gettime() - s->last_load_time < s->target_duration*1000000) {
  242. if (url_interrupt_cb())
  243. return AVERROR_EXIT;
  244. usleep(100*1000);
  245. }
  246. goto retry;
  247. }
  248. url = s->segments[s->cur_seq_no - s->start_seq_no]->url,
  249. av_log(NULL, AV_LOG_DEBUG, "opening %s\n", url);
  250. ret = ffurl_open(&s->seg_hd, url, AVIO_RDONLY);
  251. if (ret < 0) {
  252. if (url_interrupt_cb())
  253. return AVERROR_EXIT;
  254. av_log(NULL, AV_LOG_WARNING, "Unable to open %s\n", url);
  255. s->cur_seq_no++;
  256. goto retry;
  257. }
  258. goto start;
  259. }
  260. static int applehttp_close(URLContext *h)
  261. {
  262. AppleHTTPContext *s = h->priv_data;
  263. free_segment_list(s);
  264. free_variant_list(s);
  265. ffurl_close(s->seg_hd);
  266. av_free(s);
  267. return 0;
  268. }
  269. URLProtocol ff_applehttp_protocol = {
  270. .name = "applehttp",
  271. .url_open = applehttp_open,
  272. .url_read = applehttp_read,
  273. .url_close = applehttp_close,
  274. .flags = URL_PROTOCOL_FLAG_NESTED_SCHEME,
  275. };