Signed-off-by: Luca Barbato <lu_zero@gentoo.org>
| ... | ... |
@@ -308,6 +308,7 @@ OBJS-$(CONFIG_LIBNUT_MUXER) += libnut.o riff.o |
| 308 | 308 |
# protocols I/O |
| 309 | 309 |
OBJS+= avio.o aviobuf.o |
| 310 | 310 |
|
| 311 |
+OBJS-$(CONFIG_APPLEHTTP_PROTOCOL) += applehttpproto.o |
|
| 311 | 312 |
OBJS-$(CONFIG_CONCAT_PROTOCOL) += concat.o |
| 312 | 313 |
OBJS-$(CONFIG_FILE_PROTOCOL) += file.o |
| 313 | 314 |
OBJS-$(CONFIG_GOPHER_PROTOCOL) += gopher.o |
| ... | ... |
@@ -229,6 +229,7 @@ void av_register_all(void) |
| 229 | 229 |
REGISTER_MUXDEMUX (LIBNUT, libnut); |
| 230 | 230 |
|
| 231 | 231 |
/* protocols */ |
| 232 |
+ REGISTER_PROTOCOL (APPLEHTTP, applehttp); |
|
| 232 | 233 |
REGISTER_PROTOCOL (CONCAT, concat); |
| 233 | 234 |
REGISTER_PROTOCOL (FILE, file); |
| 234 | 235 |
REGISTER_PROTOCOL (GOPHER, gopher); |
| 235 | 236 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,358 @@ |
| 0 |
+/* |
|
| 1 |
+ * Apple HTTP Live Streaming Protocol Handler |
|
| 2 |
+ * Copyright (c) 2010 Martin Storsjo |
|
| 3 |
+ * |
|
| 4 |
+ * This file is part of FFmpeg. |
|
| 5 |
+ * |
|
| 6 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
| 7 |
+ * modify it under the terms of the GNU Lesser General Public |
|
| 8 |
+ * License as published by the Free Software Foundation; either |
|
| 9 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
| 10 |
+ * |
|
| 11 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
| 12 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 13 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
| 14 |
+ * Lesser General Public License for more details. |
|
| 15 |
+ * |
|
| 16 |
+ * You should have received a copy of the GNU Lesser General Public |
|
| 17 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
| 18 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
| 19 |
+ */ |
|
| 20 |
+ |
|
| 21 |
+/** |
|
| 22 |
+ * @file |
|
| 23 |
+ * Apple HTTP Live Streaming Protocol Handler |
|
| 24 |
+ * http://tools.ietf.org/html/draft-pantos-http-live-streaming |
|
| 25 |
+ */ |
|
| 26 |
+ |
|
| 27 |
+#define _XOPEN_SOURCE 600 |
|
| 28 |
+#include "libavutil/avstring.h" |
|
| 29 |
+#include "avformat.h" |
|
| 30 |
+#include "internal.h" |
|
| 31 |
+#include <unistd.h> |
|
| 32 |
+ |
|
| 33 |
+/* |
|
| 34 |
+ * An apple http stream consists of a playlist with media segment files, |
|
| 35 |
+ * played sequentially. There may be several playlists with the same |
|
| 36 |
+ * video content, in different bandwidth variants, that are played in |
|
| 37 |
+ * parallel (preferrably only one bandwidth variant at a time). In this case, |
|
| 38 |
+ * the user supplied the url to a main playlist that only lists the variant |
|
| 39 |
+ * playlists. |
|
| 40 |
+ * |
|
| 41 |
+ * If the main playlist doesn't point at any variants, we still create |
|
| 42 |
+ * one anonymous toplevel variant for this, to maintain the structure. |
|
| 43 |
+ */ |
|
| 44 |
+ |
|
| 45 |
+struct segment {
|
|
| 46 |
+ int duration; |
|
| 47 |
+ char url[MAX_URL_SIZE]; |
|
| 48 |
+}; |
|
| 49 |
+ |
|
| 50 |
+struct variant {
|
|
| 51 |
+ int bandwidth; |
|
| 52 |
+ char url[MAX_URL_SIZE]; |
|
| 53 |
+}; |
|
| 54 |
+ |
|
| 55 |
+typedef struct AppleHTTPContext {
|
|
| 56 |
+ char playlisturl[MAX_URL_SIZE]; |
|
| 57 |
+ int target_duration; |
|
| 58 |
+ int start_seq_no; |
|
| 59 |
+ int finished; |
|
| 60 |
+ int n_segments; |
|
| 61 |
+ struct segment **segments; |
|
| 62 |
+ int n_variants; |
|
| 63 |
+ struct variant **variants; |
|
| 64 |
+ int cur_seq_no; |
|
| 65 |
+ URLContext *seg_hd; |
|
| 66 |
+ int64_t last_load_time; |
|
| 67 |
+} AppleHTTPContext; |
|
| 68 |
+ |
|
| 69 |
+static int read_chomp_line(AVIOContext *s, char *buf, int maxlen) |
|
| 70 |
+{
|
|
| 71 |
+ int len = ff_get_line(s, buf, maxlen); |
|
| 72 |
+ while (len > 0 && isspace(buf[len - 1])) |
|
| 73 |
+ buf[--len] = '\0'; |
|
| 74 |
+ return len; |
|
| 75 |
+} |
|
| 76 |
+ |
|
| 77 |
+static void make_absolute_url(char *buf, int size, const char *base, |
|
| 78 |
+ const char *rel) |
|
| 79 |
+{
|
|
| 80 |
+ char *sep; |
|
| 81 |
+ /* Absolute path, relative to the current server */ |
|
| 82 |
+ if (base && strstr(base, "://") && rel[0] == '/') {
|
|
| 83 |
+ if (base != buf) |
|
| 84 |
+ av_strlcpy(buf, base, size); |
|
| 85 |
+ sep = strstr(buf, "://"); |
|
| 86 |
+ if (sep) {
|
|
| 87 |
+ sep += 3; |
|
| 88 |
+ sep = strchr(sep, '/'); |
|
| 89 |
+ if (sep) |
|
| 90 |
+ *sep = '\0'; |
|
| 91 |
+ } |
|
| 92 |
+ av_strlcat(buf, rel, size); |
|
| 93 |
+ return; |
|
| 94 |
+ } |
|
| 95 |
+ /* If rel actually is an absolute url, just copy it */ |
|
| 96 |
+ if (!base || strstr(rel, "://") || rel[0] == '/') {
|
|
| 97 |
+ av_strlcpy(buf, rel, size); |
|
| 98 |
+ return; |
|
| 99 |
+ } |
|
| 100 |
+ if (base != buf) |
|
| 101 |
+ av_strlcpy(buf, base, size); |
|
| 102 |
+ /* Remove the file name from the base url */ |
|
| 103 |
+ sep = strrchr(buf, '/'); |
|
| 104 |
+ if (sep) |
|
| 105 |
+ sep[1] = '\0'; |
|
| 106 |
+ else |
|
| 107 |
+ buf[0] = '\0'; |
|
| 108 |
+ while (av_strstart(rel, "../", NULL) && sep) {
|
|
| 109 |
+ /* Remove the path delimiter at the end */ |
|
| 110 |
+ sep[0] = '\0'; |
|
| 111 |
+ sep = strrchr(buf, '/'); |
|
| 112 |
+ /* If the next directory name to pop off is "..", break here */ |
|
| 113 |
+ if (!strcmp(sep ? &sep[1] : buf, "..")) {
|
|
| 114 |
+ /* Readd the slash we just removed */ |
|
| 115 |
+ av_strlcat(buf, "/", size); |
|
| 116 |
+ break; |
|
| 117 |
+ } |
|
| 118 |
+ /* Cut off the directory name */ |
|
| 119 |
+ if (sep) |
|
| 120 |
+ sep[1] = '\0'; |
|
| 121 |
+ else |
|
| 122 |
+ buf[0] = '\0'; |
|
| 123 |
+ rel += 3; |
|
| 124 |
+ } |
|
| 125 |
+ av_strlcat(buf, rel, size); |
|
| 126 |
+} |
|
| 127 |
+ |
|
| 128 |
+static void free_segment_list(AppleHTTPContext *s) |
|
| 129 |
+{
|
|
| 130 |
+ int i; |
|
| 131 |
+ for (i = 0; i < s->n_segments; i++) |
|
| 132 |
+ av_free(s->segments[i]); |
|
| 133 |
+ av_freep(&s->segments); |
|
| 134 |
+ s->n_segments = 0; |
|
| 135 |
+} |
|
| 136 |
+ |
|
| 137 |
+static void free_variant_list(AppleHTTPContext *s) |
|
| 138 |
+{
|
|
| 139 |
+ int i; |
|
| 140 |
+ for (i = 0; i < s->n_variants; i++) |
|
| 141 |
+ av_free(s->variants[i]); |
|
| 142 |
+ av_freep(&s->variants); |
|
| 143 |
+ s->n_variants = 0; |
|
| 144 |
+} |
|
| 145 |
+ |
|
| 146 |
+struct variant_info {
|
|
| 147 |
+ char bandwidth[20]; |
|
| 148 |
+}; |
|
| 149 |
+ |
|
| 150 |
+static void handle_variant_args(struct variant_info *info, const char *key, |
|
| 151 |
+ int key_len, char **dest, int *dest_len) |
|
| 152 |
+{
|
|
| 153 |
+ if (!strncmp(key, "BANDWIDTH=", key_len)) {
|
|
| 154 |
+ *dest = info->bandwidth; |
|
| 155 |
+ *dest_len = sizeof(info->bandwidth); |
|
| 156 |
+ } |
|
| 157 |
+} |
|
| 158 |
+ |
|
| 159 |
+static int parse_playlist(URLContext *h, const char *url) |
|
| 160 |
+{
|
|
| 161 |
+ AppleHTTPContext *s = h->priv_data; |
|
| 162 |
+ AVIOContext *in; |
|
| 163 |
+ int ret = 0, duration = 0, is_segment = 0, is_variant = 0, bandwidth = 0; |
|
| 164 |
+ char line[1024]; |
|
| 165 |
+ const char *ptr; |
|
| 166 |
+ |
|
| 167 |
+ if ((ret = avio_open(&in, url, URL_RDONLY)) < 0) |
|
| 168 |
+ return ret; |
|
| 169 |
+ |
|
| 170 |
+ read_chomp_line(in, line, sizeof(line)); |
|
| 171 |
+ if (strcmp(line, "#EXTM3U")) |
|
| 172 |
+ return AVERROR_INVALIDDATA; |
|
| 173 |
+ |
|
| 174 |
+ free_segment_list(s); |
|
| 175 |
+ s->finished = 0; |
|
| 176 |
+ while (!url_feof(in)) {
|
|
| 177 |
+ read_chomp_line(in, line, sizeof(line)); |
|
| 178 |
+ if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
|
|
| 179 |
+ struct variant_info info = {{0}};
|
|
| 180 |
+ is_variant = 1; |
|
| 181 |
+ ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args, |
|
| 182 |
+ &info); |
|
| 183 |
+ bandwidth = atoi(info.bandwidth); |
|
| 184 |
+ } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
|
|
| 185 |
+ s->target_duration = atoi(ptr); |
|
| 186 |
+ } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
|
|
| 187 |
+ s->start_seq_no = atoi(ptr); |
|
| 188 |
+ } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
|
|
| 189 |
+ s->finished = 1; |
|
| 190 |
+ } else if (av_strstart(line, "#EXTINF:", &ptr)) {
|
|
| 191 |
+ is_segment = 1; |
|
| 192 |
+ duration = atoi(ptr); |
|
| 193 |
+ } else if (av_strstart(line, "#", NULL)) {
|
|
| 194 |
+ continue; |
|
| 195 |
+ } else if (line[0]) {
|
|
| 196 |
+ if (is_segment) {
|
|
| 197 |
+ struct segment *seg = av_malloc(sizeof(struct segment)); |
|
| 198 |
+ if (!seg) {
|
|
| 199 |
+ ret = AVERROR(ENOMEM); |
|
| 200 |
+ goto fail; |
|
| 201 |
+ } |
|
| 202 |
+ seg->duration = duration; |
|
| 203 |
+ make_absolute_url(seg->url, sizeof(seg->url), url, line); |
|
| 204 |
+ dynarray_add(&s->segments, &s->n_segments, seg); |
|
| 205 |
+ is_segment = 0; |
|
| 206 |
+ } else if (is_variant) {
|
|
| 207 |
+ struct variant *var = av_malloc(sizeof(struct variant)); |
|
| 208 |
+ if (!var) {
|
|
| 209 |
+ ret = AVERROR(ENOMEM); |
|
| 210 |
+ goto fail; |
|
| 211 |
+ } |
|
| 212 |
+ var->bandwidth = bandwidth; |
|
| 213 |
+ make_absolute_url(var->url, sizeof(var->url), url, line); |
|
| 214 |
+ dynarray_add(&s->variants, &s->n_variants, var); |
|
| 215 |
+ is_variant = 0; |
|
| 216 |
+ } |
|
| 217 |
+ } |
|
| 218 |
+ } |
|
| 219 |
+ s->last_load_time = av_gettime(); |
|
| 220 |
+ |
|
| 221 |
+fail: |
|
| 222 |
+ avio_close(in); |
|
| 223 |
+ return ret; |
|
| 224 |
+} |
|
| 225 |
+ |
|
| 226 |
+static int applehttp_open(URLContext *h, const char *uri, int flags) |
|
| 227 |
+{
|
|
| 228 |
+ AppleHTTPContext *s; |
|
| 229 |
+ int ret, i; |
|
| 230 |
+ const char *nested_url; |
|
| 231 |
+ |
|
| 232 |
+ if (flags & (URL_WRONLY | URL_RDWR)) |
|
| 233 |
+ return AVERROR_NOTSUPP; |
|
| 234 |
+ |
|
| 235 |
+ s = av_mallocz(sizeof(AppleHTTPContext)); |
|
| 236 |
+ if (!s) |
|
| 237 |
+ return AVERROR(ENOMEM); |
|
| 238 |
+ h->priv_data = s; |
|
| 239 |
+ h->is_streamed = 1; |
|
| 240 |
+ |
|
| 241 |
+ if (av_strstart(uri, "applehttp+", &nested_url)) {
|
|
| 242 |
+ av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl)); |
|
| 243 |
+ } else if (av_strstart(uri, "applehttp://", &nested_url)) {
|
|
| 244 |
+ av_strlcpy(s->playlisturl, "http://", sizeof(s->playlisturl)); |
|
| 245 |
+ av_strlcat(s->playlisturl, nested_url, sizeof(s->playlisturl)); |
|
| 246 |
+ } else {
|
|
| 247 |
+ av_log(NULL, AV_LOG_ERROR, "Unsupported url %s\n", uri); |
|
| 248 |
+ ret = AVERROR(EINVAL); |
|
| 249 |
+ goto fail; |
|
| 250 |
+ } |
|
| 251 |
+ |
|
| 252 |
+ if ((ret = parse_playlist(h, s->playlisturl)) < 0) |
|
| 253 |
+ goto fail; |
|
| 254 |
+ |
|
| 255 |
+ if (s->n_segments == 0 && s->n_variants > 0) {
|
|
| 256 |
+ int max_bandwidth = 0, maxvar = -1; |
|
| 257 |
+ for (i = 0; i < s->n_variants; i++) {
|
|
| 258 |
+ if (s->variants[i]->bandwidth > max_bandwidth || i == 0) {
|
|
| 259 |
+ max_bandwidth = s->variants[i]->bandwidth; |
|
| 260 |
+ maxvar = i; |
|
| 261 |
+ } |
|
| 262 |
+ } |
|
| 263 |
+ av_strlcpy(s->playlisturl, s->variants[maxvar]->url, |
|
| 264 |
+ sizeof(s->playlisturl)); |
|
| 265 |
+ if ((ret = parse_playlist(h, s->playlisturl)) < 0) |
|
| 266 |
+ goto fail; |
|
| 267 |
+ } |
|
| 268 |
+ |
|
| 269 |
+ if (s->n_segments == 0) {
|
|
| 270 |
+ av_log(NULL, AV_LOG_WARNING, "Empty playlist\n"); |
|
| 271 |
+ ret = AVERROR(EIO); |
|
| 272 |
+ goto fail; |
|
| 273 |
+ } |
|
| 274 |
+ s->cur_seq_no = s->start_seq_no; |
|
| 275 |
+ if (!s->finished && s->n_segments >= 3) |
|
| 276 |
+ s->cur_seq_no = s->start_seq_no + s->n_segments - 3; |
|
| 277 |
+ |
|
| 278 |
+ return 0; |
|
| 279 |
+ |
|
| 280 |
+fail: |
|
| 281 |
+ av_free(s); |
|
| 282 |
+ return ret; |
|
| 283 |
+} |
|
| 284 |
+ |
|
| 285 |
+static int applehttp_read(URLContext *h, uint8_t *buf, int size) |
|
| 286 |
+{
|
|
| 287 |
+ AppleHTTPContext *s = h->priv_data; |
|
| 288 |
+ const char *url; |
|
| 289 |
+ int ret; |
|
| 290 |
+ |
|
| 291 |
+start: |
|
| 292 |
+ if (s->seg_hd) {
|
|
| 293 |
+ ret = url_read(s->seg_hd, buf, size); |
|
| 294 |
+ if (ret > 0) |
|
| 295 |
+ return ret; |
|
| 296 |
+ } |
|
| 297 |
+ if (s->seg_hd) {
|
|
| 298 |
+ url_close(s->seg_hd); |
|
| 299 |
+ s->seg_hd = NULL; |
|
| 300 |
+ s->cur_seq_no++; |
|
| 301 |
+ } |
|
| 302 |
+retry: |
|
| 303 |
+ if (!s->finished) {
|
|
| 304 |
+ int64_t now = av_gettime(); |
|
| 305 |
+ if (now - s->last_load_time >= s->target_duration*1000000) |
|
| 306 |
+ if ((ret = parse_playlist(h, s->playlisturl)) < 0) |
|
| 307 |
+ return ret; |
|
| 308 |
+ } |
|
| 309 |
+ if (s->cur_seq_no < s->start_seq_no) {
|
|
| 310 |
+ av_log(NULL, AV_LOG_WARNING, |
|
| 311 |
+ "skipping %d segments ahead, expired from playlist\n", |
|
| 312 |
+ s->start_seq_no - s->cur_seq_no); |
|
| 313 |
+ s->cur_seq_no = s->start_seq_no; |
|
| 314 |
+ } |
|
| 315 |
+ if (s->cur_seq_no - s->start_seq_no >= s->n_segments) {
|
|
| 316 |
+ if (s->finished) |
|
| 317 |
+ return AVERROR_EOF; |
|
| 318 |
+ while (av_gettime() - s->last_load_time < s->target_duration*1000000) {
|
|
| 319 |
+ if (url_interrupt_cb()) |
|
| 320 |
+ return AVERROR(EINTR); |
|
| 321 |
+ usleep(100*1000); |
|
| 322 |
+ } |
|
| 323 |
+ goto retry; |
|
| 324 |
+ } |
|
| 325 |
+ url = s->segments[s->cur_seq_no - s->start_seq_no]->url, |
|
| 326 |
+ av_log(NULL, AV_LOG_DEBUG, "opening %s\n", url); |
|
| 327 |
+ ret = url_open(&s->seg_hd, url, URL_RDONLY); |
|
| 328 |
+ if (ret < 0) {
|
|
| 329 |
+ if (url_interrupt_cb()) |
|
| 330 |
+ return AVERROR(EINTR); |
|
| 331 |
+ av_log(NULL, AV_LOG_WARNING, "Unable to open %s\n", url); |
|
| 332 |
+ s->cur_seq_no++; |
|
| 333 |
+ goto retry; |
|
| 334 |
+ } |
|
| 335 |
+ goto start; |
|
| 336 |
+} |
|
| 337 |
+ |
|
| 338 |
+static int applehttp_close(URLContext *h) |
|
| 339 |
+{
|
|
| 340 |
+ AppleHTTPContext *s = h->priv_data; |
|
| 341 |
+ |
|
| 342 |
+ free_segment_list(s); |
|
| 343 |
+ free_variant_list(s); |
|
| 344 |
+ url_close(s->seg_hd); |
|
| 345 |
+ av_free(s); |
|
| 346 |
+ return 0; |
|
| 347 |
+} |
|
| 348 |
+ |
|
| 349 |
+URLProtocol ff_applehttp_protocol = {
|
|
| 350 |
+ "applehttp", |
|
| 351 |
+ applehttp_open, |
|
| 352 |
+ applehttp_read, |
|
| 353 |
+ NULL, /* write */ |
|
| 354 |
+ NULL, /* seek */ |
|
| 355 |
+ applehttp_close, |
|
| 356 |
+ .flags = URL_PROTOCOL_FLAG_NESTED_SCHEME, |
|
| 357 |
+}; |