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.

308 lines
9.1KB

  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_open(URLContext *h, const char *uri, int flags)
  161. {
  162. AppleHTTPContext *s;
  163. int ret, i;
  164. const char *nested_url;
  165. if (flags & AVIO_FLAG_WRITE)
  166. return AVERROR(ENOSYS);
  167. s = av_mallocz(sizeof(AppleHTTPContext));
  168. if (!s)
  169. return AVERROR(ENOMEM);
  170. h->priv_data = s;
  171. h->is_streamed = 1;
  172. if (av_strstart(uri, "applehttp+", &nested_url)) {
  173. av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
  174. } else if (av_strstart(uri, "applehttp://", &nested_url)) {
  175. av_strlcpy(s->playlisturl, "http://", sizeof(s->playlisturl));
  176. av_strlcat(s->playlisturl, nested_url, sizeof(s->playlisturl));
  177. } else {
  178. av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
  179. ret = AVERROR(EINVAL);
  180. goto fail;
  181. }
  182. if ((ret = parse_playlist(h, s->playlisturl)) < 0)
  183. goto fail;
  184. if (s->n_segments == 0 && s->n_variants > 0) {
  185. int max_bandwidth = 0, maxvar = -1;
  186. for (i = 0; i < s->n_variants; i++) {
  187. if (s->variants[i]->bandwidth > max_bandwidth || i == 0) {
  188. max_bandwidth = s->variants[i]->bandwidth;
  189. maxvar = i;
  190. }
  191. }
  192. av_strlcpy(s->playlisturl, s->variants[maxvar]->url,
  193. sizeof(s->playlisturl));
  194. if ((ret = parse_playlist(h, s->playlisturl)) < 0)
  195. goto fail;
  196. }
  197. if (s->n_segments == 0) {
  198. av_log(h, AV_LOG_WARNING, "Empty playlist\n");
  199. ret = AVERROR(EIO);
  200. goto fail;
  201. }
  202. s->cur_seq_no = s->start_seq_no;
  203. if (!s->finished && s->n_segments >= 3)
  204. s->cur_seq_no = s->start_seq_no + s->n_segments - 3;
  205. return 0;
  206. fail:
  207. av_free(s);
  208. return ret;
  209. }
  210. static int applehttp_read(URLContext *h, uint8_t *buf, int size)
  211. {
  212. AppleHTTPContext *s = h->priv_data;
  213. const char *url;
  214. int ret;
  215. start:
  216. if (s->seg_hd) {
  217. ret = ffurl_read(s->seg_hd, buf, size);
  218. if (ret > 0)
  219. return ret;
  220. }
  221. if (s->seg_hd) {
  222. ffurl_close(s->seg_hd);
  223. s->seg_hd = NULL;
  224. s->cur_seq_no++;
  225. }
  226. retry:
  227. if (!s->finished) {
  228. int64_t now = av_gettime();
  229. if (now - s->last_load_time >= s->target_duration*1000000)
  230. if ((ret = parse_playlist(h, s->playlisturl)) < 0)
  231. return ret;
  232. }
  233. if (s->cur_seq_no < s->start_seq_no) {
  234. av_log(h, AV_LOG_WARNING,
  235. "skipping %d segments ahead, expired from playlist\n",
  236. s->start_seq_no - s->cur_seq_no);
  237. s->cur_seq_no = s->start_seq_no;
  238. }
  239. if (s->cur_seq_no - s->start_seq_no >= s->n_segments) {
  240. if (s->finished)
  241. return AVERROR_EOF;
  242. while (av_gettime() - s->last_load_time < s->target_duration*1000000) {
  243. if (ff_check_interrupt(&h->interrupt_callback))
  244. return AVERROR_EXIT;
  245. usleep(100*1000);
  246. }
  247. goto retry;
  248. }
  249. url = s->segments[s->cur_seq_no - s->start_seq_no]->url,
  250. av_log(h, AV_LOG_DEBUG, "opening %s\n", url);
  251. ret = ffurl_open(&s->seg_hd, url, AVIO_FLAG_READ,
  252. &h->interrupt_callback, NULL);
  253. if (ret < 0) {
  254. if (ff_check_interrupt(&h->interrupt_callback))
  255. return AVERROR_EXIT;
  256. av_log(h, AV_LOG_WARNING, "Unable to open %s\n", url);
  257. s->cur_seq_no++;
  258. goto retry;
  259. }
  260. goto start;
  261. }
  262. static int applehttp_close(URLContext *h)
  263. {
  264. AppleHTTPContext *s = h->priv_data;
  265. free_segment_list(s);
  266. free_variant_list(s);
  267. ffurl_close(s->seg_hd);
  268. av_free(s);
  269. return 0;
  270. }
  271. URLProtocol ff_applehttp_protocol = {
  272. .name = "applehttp",
  273. .url_open = applehttp_open,
  274. .url_read = applehttp_read,
  275. .url_close = applehttp_close,
  276. .flags = URL_PROTOCOL_FLAG_NESTED_SCHEME,
  277. };