libavformat: Rename the applehttp protocol to hls
authorMartin Storsjö <martin@martin.st>
Tue, 14 Feb 2012 10:00:49 +0000 (12:00 +0200)
committerMartin Storsjö <martin@martin.st>
Tue, 14 Feb 2012 18:05:32 +0000 (20:05 +0200)
Keep the old protocol name around for backwards compatibility
until the next bump.

Deprecate the method of implicitly assuming the nested protocol.
For applehttp://server/path, it might have felt logical, but
supporting hls://server/path isn't quite as intuitive. Therefore
only support hls+http://server/path from now on.

Using this protocol at all is discouraged, since the hls demuxer
is more complete and fits into the architecture better. There
have been cases where the protocol implementation worked better
than the demuxer, but this should no longer be the case.

Signed-off-by: Martin Storsjö <martin@martin.st>
doc/protocols.texi
libavformat/Makefile
libavformat/allformats.c
libavformat/applehttpproto.c [deleted file]
libavformat/hlsproto.c [new file with mode: 0644]
libavformat/version.h

index f5bb5324be93576e8e44a057dcd5bc9f91a37916..e00c1e11b25f11b84fb89321678f22d2eca13cea 100644 (file)
@@ -19,20 +19,19 @@ supported protocols.
 
 A description of the currently available protocols follows.
 
-@section applehttp
+@section hls
 
 Read Apple HTTP Live Streaming compliant segmented stream as
 a uniform one. The M3U8 playlists describing the segments can be
 remote HTTP resources or local files, accessed using the standard
 file protocol.
-HTTP is default, specific protocol can be declared by specifying
-"+@var{proto}" after the applehttp URI scheme name, where @var{proto}
+The nested protocol is declared by specifying
+"+@var{proto}" after the hls URI scheme name, where @var{proto}
 is either "file" or "http".
 
 @example
-applehttp://host/path/to/remote/resource.m3u8
-applehttp+http://host/path/to/remote/resource.m3u8
-applehttp+file://path/to/local/resource.m3u8
+hls+http://host/path/to/remote/resource.m3u8
+hls+file://path/to/local/resource.m3u8
 @end example
 
 @section concat
index a54a8f569a7e8ec763768d1ce1cfe7f563183cf0..dd55e42db427d23876c013fc540cba93178c9962 100644 (file)
@@ -329,11 +329,12 @@ OBJS-$(CONFIG_LIBRTMP)                   += librtmp.o
 # protocols I/O
 OBJS+= avio.o aviobuf.o
 
-OBJS-$(CONFIG_APPLEHTTP_PROTOCOL)        += applehttpproto.o
+OBJS-$(CONFIG_APPLEHTTP_PROTOCOL)        += hlsproto.o
 OBJS-$(CONFIG_CONCAT_PROTOCOL)           += concat.o
 OBJS-$(CONFIG_CRYPTO_PROTOCOL)           += crypto.o
 OBJS-$(CONFIG_FILE_PROTOCOL)             += file.o
 OBJS-$(CONFIG_GOPHER_PROTOCOL)           += gopher.o
+OBJS-$(CONFIG_HLS_PROTOCOL)              += hlsproto.o
 OBJS-$(CONFIG_HTTP_PROTOCOL)             += http.o httpauth.o
 OBJS-$(CONFIG_HTTPPROXY_PROTOCOL)        += http.o httpauth.o
 OBJS-$(CONFIG_HTTPS_PROTOCOL)            += http.o httpauth.o
index af6ad97e928c62e5b7a11517fb3cac1d08ce0fd9..a60688c3622f0e5e5c25bb3b2d9f18b8d3b8800e 100644 (file)
@@ -22,6 +22,7 @@
 #include "rtp.h"
 #include "rdt.h"
 #include "url.h"
+#include "version.h"
 
 #define REGISTER_MUXER(X,x) { \
     extern AVOutputFormat ff_##x##_muxer; \
@@ -238,11 +239,14 @@ void av_register_all(void)
     REGISTER_MUXDEMUX (YUV4MPEGPIPE, yuv4mpegpipe);
 
     /* protocols */
+#if FF_API_APPLEHTTP_PROTO
     REGISTER_PROTOCOL (APPLEHTTP, applehttp);
+#endif
     REGISTER_PROTOCOL (CONCAT, concat);
     REGISTER_PROTOCOL (CRYPTO, crypto);
     REGISTER_PROTOCOL (FILE, file);
     REGISTER_PROTOCOL (GOPHER, gopher);
+    REGISTER_PROTOCOL (HLS, hls);
     REGISTER_PROTOCOL (HTTP, http);
     REGISTER_PROTOCOL (HTTPPROXY, httpproxy);
     REGISTER_PROTOCOL (HTTPS, https);
diff --git a/libavformat/applehttpproto.c b/libavformat/applehttpproto.c
deleted file mode 100644 (file)
index 46758ba..0000000
+++ /dev/null
@@ -1,313 +0,0 @@
-/*
- * Apple HTTP Live Streaming Protocol Handler
- * Copyright (c) 2010 Martin Storsjo
- *
- * This file is part of Libav.
- *
- * Libav is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * Libav is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/**
- * @file
- * Apple HTTP Live Streaming Protocol Handler
- * http://tools.ietf.org/html/draft-pantos-http-live-streaming
- */
-
-#include "libavutil/avstring.h"
-#include "avformat.h"
-#include "internal.h"
-#include "url.h"
-#include <unistd.h>
-
-/*
- * An apple http stream consists of a playlist with media segment files,
- * played sequentially. There may be several playlists with the same
- * video content, in different bandwidth variants, that are played in
- * parallel (preferrably only one bandwidth variant at a time). In this case,
- * the user supplied the url to a main playlist that only lists the variant
- * playlists.
- *
- * If the main playlist doesn't point at any variants, we still create
- * one anonymous toplevel variant for this, to maintain the structure.
- */
-
-struct segment {
-    int duration;
-    char url[MAX_URL_SIZE];
-};
-
-struct variant {
-    int bandwidth;
-    char url[MAX_URL_SIZE];
-};
-
-typedef struct AppleHTTPContext {
-    char playlisturl[MAX_URL_SIZE];
-    int target_duration;
-    int start_seq_no;
-    int finished;
-    int n_segments;
-    struct segment **segments;
-    int n_variants;
-    struct variant **variants;
-    int cur_seq_no;
-    URLContext *seg_hd;
-    int64_t last_load_time;
-} AppleHTTPContext;
-
-static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
-{
-    int len = ff_get_line(s, buf, maxlen);
-    while (len > 0 && isspace(buf[len - 1]))
-        buf[--len] = '\0';
-    return len;
-}
-
-static void free_segment_list(AppleHTTPContext *s)
-{
-    int i;
-    for (i = 0; i < s->n_segments; i++)
-        av_free(s->segments[i]);
-    av_freep(&s->segments);
-    s->n_segments = 0;
-}
-
-static void free_variant_list(AppleHTTPContext *s)
-{
-    int i;
-    for (i = 0; i < s->n_variants; i++)
-        av_free(s->variants[i]);
-    av_freep(&s->variants);
-    s->n_variants = 0;
-}
-
-struct variant_info {
-    char bandwidth[20];
-};
-
-static void handle_variant_args(struct variant_info *info, const char *key,
-                                int key_len, char **dest, int *dest_len)
-{
-    if (!strncmp(key, "BANDWIDTH=", key_len)) {
-        *dest     =        info->bandwidth;
-        *dest_len = sizeof(info->bandwidth);
-    }
-}
-
-static int parse_playlist(URLContext *h, const char *url)
-{
-    AppleHTTPContext *s = h->priv_data;
-    AVIOContext *in;
-    int ret = 0, duration = 0, is_segment = 0, is_variant = 0, bandwidth = 0;
-    char line[1024];
-    const char *ptr;
-
-    if ((ret = avio_open2(&in, url, AVIO_FLAG_READ,
-                          &h->interrupt_callback, NULL)) < 0)
-        return ret;
-
-    read_chomp_line(in, line, sizeof(line));
-    if (strcmp(line, "#EXTM3U"))
-        return AVERROR_INVALIDDATA;
-
-    free_segment_list(s);
-    s->finished = 0;
-    while (!in->eof_reached) {
-        read_chomp_line(in, line, sizeof(line));
-        if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
-            struct variant_info info = {{0}};
-            is_variant = 1;
-            ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,
-                               &info);
-            bandwidth = atoi(info.bandwidth);
-        } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
-            s->target_duration = atoi(ptr);
-        } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
-            s->start_seq_no = atoi(ptr);
-        } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
-            s->finished = 1;
-        } else if (av_strstart(line, "#EXTINF:", &ptr)) {
-            is_segment = 1;
-            duration = atoi(ptr);
-        } else if (av_strstart(line, "#", NULL)) {
-            continue;
-        } else if (line[0]) {
-            if (is_segment) {
-                struct segment *seg = av_malloc(sizeof(struct segment));
-                if (!seg) {
-                    ret = AVERROR(ENOMEM);
-                    goto fail;
-                }
-                seg->duration = duration;
-                ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);
-                dynarray_add(&s->segments, &s->n_segments, seg);
-                is_segment = 0;
-            } else if (is_variant) {
-                struct variant *var = av_malloc(sizeof(struct variant));
-                if (!var) {
-                    ret = AVERROR(ENOMEM);
-                    goto fail;
-                }
-                var->bandwidth = bandwidth;
-                ff_make_absolute_url(var->url, sizeof(var->url), url, line);
-                dynarray_add(&s->variants, &s->n_variants, var);
-                is_variant = 0;
-            }
-        }
-    }
-    s->last_load_time = av_gettime();
-
-fail:
-    avio_close(in);
-    return ret;
-}
-
-static int applehttp_close(URLContext *h)
-{
-    AppleHTTPContext *s = h->priv_data;
-
-    free_segment_list(s);
-    free_variant_list(s);
-    ffurl_close(s->seg_hd);
-    return 0;
-}
-
-static int applehttp_open(URLContext *h, const char *uri, int flags)
-{
-    AppleHTTPContext *s = h->priv_data;
-    int ret, i;
-    const char *nested_url;
-
-    if (flags & AVIO_FLAG_WRITE)
-        return AVERROR(ENOSYS);
-
-    h->is_streamed = 1;
-
-    if (av_strstart(uri, "applehttp+", &nested_url)) {
-        av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
-    } else if (av_strstart(uri, "applehttp://", &nested_url)) {
-        av_strlcpy(s->playlisturl, "http://", sizeof(s->playlisturl));
-        av_strlcat(s->playlisturl, nested_url, sizeof(s->playlisturl));
-    } else {
-        av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
-        ret = AVERROR(EINVAL);
-        goto fail;
-    }
-
-    if ((ret = parse_playlist(h, s->playlisturl)) < 0)
-        goto fail;
-
-    if (s->n_segments == 0 && s->n_variants > 0) {
-        int max_bandwidth = 0, maxvar = -1;
-        for (i = 0; i < s->n_variants; i++) {
-            if (s->variants[i]->bandwidth > max_bandwidth || i == 0) {
-                max_bandwidth = s->variants[i]->bandwidth;
-                maxvar = i;
-            }
-        }
-        av_strlcpy(s->playlisturl, s->variants[maxvar]->url,
-                   sizeof(s->playlisturl));
-        if ((ret = parse_playlist(h, s->playlisturl)) < 0)
-            goto fail;
-    }
-
-    if (s->n_segments == 0) {
-        av_log(h, AV_LOG_WARNING, "Empty playlist\n");
-        ret = AVERROR(EIO);
-        goto fail;
-    }
-    s->cur_seq_no = s->start_seq_no;
-    if (!s->finished && s->n_segments >= 3)
-        s->cur_seq_no = s->start_seq_no + s->n_segments - 3;
-
-    return 0;
-
-fail:
-    applehttp_close(h);
-    return ret;
-}
-
-static int applehttp_read(URLContext *h, uint8_t *buf, int size)
-{
-    AppleHTTPContext *s = h->priv_data;
-    const char *url;
-    int ret;
-    int64_t reload_interval;
-
-start:
-    if (s->seg_hd) {
-        ret = ffurl_read(s->seg_hd, buf, size);
-        if (ret > 0)
-            return ret;
-    }
-    if (s->seg_hd) {
-        ffurl_close(s->seg_hd);
-        s->seg_hd = NULL;
-        s->cur_seq_no++;
-    }
-    reload_interval = s->n_segments > 0 ?
-                      s->segments[s->n_segments - 1]->duration :
-                      s->target_duration;
-    reload_interval *= 1000000;
-retry:
-    if (!s->finished) {
-        int64_t now = av_gettime();
-        if (now - s->last_load_time >= reload_interval) {
-            if ((ret = parse_playlist(h, s->playlisturl)) < 0)
-                return ret;
-            /* If we need to reload the playlist again below (if
-             * there's still no more segments), switch to a reload
-             * interval of half the target duration. */
-            reload_interval = s->target_duration * 500000;
-        }
-    }
-    if (s->cur_seq_no < s->start_seq_no) {
-        av_log(h, AV_LOG_WARNING,
-               "skipping %d segments ahead, expired from playlist\n",
-               s->start_seq_no - s->cur_seq_no);
-        s->cur_seq_no = s->start_seq_no;
-    }
-    if (s->cur_seq_no - s->start_seq_no >= s->n_segments) {
-        if (s->finished)
-            return AVERROR_EOF;
-        while (av_gettime() - s->last_load_time < reload_interval) {
-            if (ff_check_interrupt(&h->interrupt_callback))
-                return AVERROR_EXIT;
-            usleep(100*1000);
-        }
-        goto retry;
-    }
-    url = s->segments[s->cur_seq_no - s->start_seq_no]->url,
-    av_log(h, AV_LOG_DEBUG, "opening %s\n", url);
-    ret = ffurl_open(&s->seg_hd, url, AVIO_FLAG_READ,
-                     &h->interrupt_callback, NULL);
-    if (ret < 0) {
-        if (ff_check_interrupt(&h->interrupt_callback))
-            return AVERROR_EXIT;
-        av_log(h, AV_LOG_WARNING, "Unable to open %s\n", url);
-        s->cur_seq_no++;
-        goto retry;
-    }
-    goto start;
-}
-
-URLProtocol ff_applehttp_protocol = {
-    .name           = "applehttp",
-    .url_open       = applehttp_open,
-    .url_read       = applehttp_read,
-    .url_close      = applehttp_close,
-    .flags          = URL_PROTOCOL_FLAG_NESTED_SCHEME,
-    .priv_data_size = sizeof(AppleHTTPContext),
-};
diff --git a/libavformat/hlsproto.c b/libavformat/hlsproto.c
new file mode 100644 (file)
index 0000000..244f270
--- /dev/null
@@ -0,0 +1,341 @@
+/*
+ * Apple HTTP Live Streaming Protocol Handler
+ * Copyright (c) 2010 Martin Storsjo
+ *
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * Libav is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * Apple HTTP Live Streaming Protocol Handler
+ * http://tools.ietf.org/html/draft-pantos-http-live-streaming
+ */
+
+#include "libavutil/avstring.h"
+#include "avformat.h"
+#include "internal.h"
+#include "url.h"
+#include "version.h"
+#include <unistd.h>
+
+/*
+ * An apple http stream consists of a playlist with media segment files,
+ * played sequentially. There may be several playlists with the same
+ * video content, in different bandwidth variants, that are played in
+ * parallel (preferrably only one bandwidth variant at a time). In this case,
+ * the user supplied the url to a main playlist that only lists the variant
+ * playlists.
+ *
+ * If the main playlist doesn't point at any variants, we still create
+ * one anonymous toplevel variant for this, to maintain the structure.
+ */
+
+struct segment {
+    int duration;
+    char url[MAX_URL_SIZE];
+};
+
+struct variant {
+    int bandwidth;
+    char url[MAX_URL_SIZE];
+};
+
+typedef struct AppleHTTPContext {
+    char playlisturl[MAX_URL_SIZE];
+    int target_duration;
+    int start_seq_no;
+    int finished;
+    int n_segments;
+    struct segment **segments;
+    int n_variants;
+    struct variant **variants;
+    int cur_seq_no;
+    URLContext *seg_hd;
+    int64_t last_load_time;
+} AppleHTTPContext;
+
+static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
+{
+    int len = ff_get_line(s, buf, maxlen);
+    while (len > 0 && isspace(buf[len - 1]))
+        buf[--len] = '\0';
+    return len;
+}
+
+static void free_segment_list(AppleHTTPContext *s)
+{
+    int i;
+    for (i = 0; i < s->n_segments; i++)
+        av_free(s->segments[i]);
+    av_freep(&s->segments);
+    s->n_segments = 0;
+}
+
+static void free_variant_list(AppleHTTPContext *s)
+{
+    int i;
+    for (i = 0; i < s->n_variants; i++)
+        av_free(s->variants[i]);
+    av_freep(&s->variants);
+    s->n_variants = 0;
+}
+
+struct variant_info {
+    char bandwidth[20];
+};
+
+static void handle_variant_args(struct variant_info *info, const char *key,
+                                int key_len, char **dest, int *dest_len)
+{
+    if (!strncmp(key, "BANDWIDTH=", key_len)) {
+        *dest     =        info->bandwidth;
+        *dest_len = sizeof(info->bandwidth);
+    }
+}
+
+static int parse_playlist(URLContext *h, const char *url)
+{
+    AppleHTTPContext *s = h->priv_data;
+    AVIOContext *in;
+    int ret = 0, duration = 0, is_segment = 0, is_variant = 0, bandwidth = 0;
+    char line[1024];
+    const char *ptr;
+
+    if ((ret = avio_open2(&in, url, AVIO_FLAG_READ,
+                          &h->interrupt_callback, NULL)) < 0)
+        return ret;
+
+    read_chomp_line(in, line, sizeof(line));
+    if (strcmp(line, "#EXTM3U"))
+        return AVERROR_INVALIDDATA;
+
+    free_segment_list(s);
+    s->finished = 0;
+    while (!in->eof_reached) {
+        read_chomp_line(in, line, sizeof(line));
+        if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
+            struct variant_info info = {{0}};
+            is_variant = 1;
+            ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,
+                               &info);
+            bandwidth = atoi(info.bandwidth);
+        } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
+            s->target_duration = atoi(ptr);
+        } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
+            s->start_seq_no = atoi(ptr);
+        } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
+            s->finished = 1;
+        } else if (av_strstart(line, "#EXTINF:", &ptr)) {
+            is_segment = 1;
+            duration = atoi(ptr);
+        } else if (av_strstart(line, "#", NULL)) {
+            continue;
+        } else if (line[0]) {
+            if (is_segment) {
+                struct segment *seg = av_malloc(sizeof(struct segment));
+                if (!seg) {
+                    ret = AVERROR(ENOMEM);
+                    goto fail;
+                }
+                seg->duration = duration;
+                ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);
+                dynarray_add(&s->segments, &s->n_segments, seg);
+                is_segment = 0;
+            } else if (is_variant) {
+                struct variant *var = av_malloc(sizeof(struct variant));
+                if (!var) {
+                    ret = AVERROR(ENOMEM);
+                    goto fail;
+                }
+                var->bandwidth = bandwidth;
+                ff_make_absolute_url(var->url, sizeof(var->url), url, line);
+                dynarray_add(&s->variants, &s->n_variants, var);
+                is_variant = 0;
+            }
+        }
+    }
+    s->last_load_time = av_gettime();
+
+fail:
+    avio_close(in);
+    return ret;
+}
+
+static int applehttp_close(URLContext *h)
+{
+    AppleHTTPContext *s = h->priv_data;
+
+    free_segment_list(s);
+    free_variant_list(s);
+    ffurl_close(s->seg_hd);
+    return 0;
+}
+
+static int applehttp_open(URLContext *h, const char *uri, int flags)
+{
+    AppleHTTPContext *s = h->priv_data;
+    int ret, i;
+    const char *nested_url;
+
+    if (flags & AVIO_FLAG_WRITE)
+        return AVERROR(ENOSYS);
+
+    h->is_streamed = 1;
+
+    if (av_strstart(uri, "hls+", &nested_url)) {
+        av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
+    } else if (av_strstart(uri, "hls://", &nested_url)) {
+        av_log(h, AV_LOG_ERROR,
+               "No nested protocol specified. Specify e.g. hls+http://%s\n",
+               nested_url);
+        ret = AVERROR(EINVAL);
+        goto fail;
+#if FF_API_APPLEHTTP_PROTO
+    } else if (av_strstart(uri, "applehttp+", &nested_url)) {
+        av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
+        av_log(h, AV_LOG_WARNING,
+               "The applehttp protocol is deprecated, use hls+%s as url "
+               "instead.\n", nested_url);
+    } else if (av_strstart(uri, "applehttp://", &nested_url)) {
+        av_strlcpy(s->playlisturl, "http://", sizeof(s->playlisturl));
+        av_strlcat(s->playlisturl, nested_url, sizeof(s->playlisturl));
+        av_log(h, AV_LOG_WARNING,
+               "The applehttp protocol is deprecated, use hls+http://%s as url "
+               "instead.\n", nested_url);
+#endif
+    } else {
+        av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
+        ret = AVERROR(EINVAL);
+        goto fail;
+    }
+
+    if ((ret = parse_playlist(h, s->playlisturl)) < 0)
+        goto fail;
+
+    if (s->n_segments == 0 && s->n_variants > 0) {
+        int max_bandwidth = 0, maxvar = -1;
+        for (i = 0; i < s->n_variants; i++) {
+            if (s->variants[i]->bandwidth > max_bandwidth || i == 0) {
+                max_bandwidth = s->variants[i]->bandwidth;
+                maxvar = i;
+            }
+        }
+        av_strlcpy(s->playlisturl, s->variants[maxvar]->url,
+                   sizeof(s->playlisturl));
+        if ((ret = parse_playlist(h, s->playlisturl)) < 0)
+            goto fail;
+    }
+
+    if (s->n_segments == 0) {
+        av_log(h, AV_LOG_WARNING, "Empty playlist\n");
+        ret = AVERROR(EIO);
+        goto fail;
+    }
+    s->cur_seq_no = s->start_seq_no;
+    if (!s->finished && s->n_segments >= 3)
+        s->cur_seq_no = s->start_seq_no + s->n_segments - 3;
+
+    return 0;
+
+fail:
+    applehttp_close(h);
+    return ret;
+}
+
+static int applehttp_read(URLContext *h, uint8_t *buf, int size)
+{
+    AppleHTTPContext *s = h->priv_data;
+    const char *url;
+    int ret;
+    int64_t reload_interval;
+
+start:
+    if (s->seg_hd) {
+        ret = ffurl_read(s->seg_hd, buf, size);
+        if (ret > 0)
+            return ret;
+    }
+    if (s->seg_hd) {
+        ffurl_close(s->seg_hd);
+        s->seg_hd = NULL;
+        s->cur_seq_no++;
+    }
+    reload_interval = s->n_segments > 0 ?
+                      s->segments[s->n_segments - 1]->duration :
+                      s->target_duration;
+    reload_interval *= 1000000;
+retry:
+    if (!s->finished) {
+        int64_t now = av_gettime();
+        if (now - s->last_load_time >= reload_interval) {
+            if ((ret = parse_playlist(h, s->playlisturl)) < 0)
+                return ret;
+            /* If we need to reload the playlist again below (if
+             * there's still no more segments), switch to a reload
+             * interval of half the target duration. */
+            reload_interval = s->target_duration * 500000;
+        }
+    }
+    if (s->cur_seq_no < s->start_seq_no) {
+        av_log(h, AV_LOG_WARNING,
+               "skipping %d segments ahead, expired from playlist\n",
+               s->start_seq_no - s->cur_seq_no);
+        s->cur_seq_no = s->start_seq_no;
+    }
+    if (s->cur_seq_no - s->start_seq_no >= s->n_segments) {
+        if (s->finished)
+            return AVERROR_EOF;
+        while (av_gettime() - s->last_load_time < reload_interval) {
+            if (ff_check_interrupt(&h->interrupt_callback))
+                return AVERROR_EXIT;
+            usleep(100*1000);
+        }
+        goto retry;
+    }
+    url = s->segments[s->cur_seq_no - s->start_seq_no]->url,
+    av_log(h, AV_LOG_DEBUG, "opening %s\n", url);
+    ret = ffurl_open(&s->seg_hd, url, AVIO_FLAG_READ,
+                     &h->interrupt_callback, NULL);
+    if (ret < 0) {
+        if (ff_check_interrupt(&h->interrupt_callback))
+            return AVERROR_EXIT;
+        av_log(h, AV_LOG_WARNING, "Unable to open %s\n", url);
+        s->cur_seq_no++;
+        goto retry;
+    }
+    goto start;
+}
+
+#if FF_API_APPLEHTTP_PROTO
+URLProtocol ff_applehttp_protocol = {
+    .name           = "applehttp",
+    .url_open       = applehttp_open,
+    .url_read       = applehttp_read,
+    .url_close      = applehttp_close,
+    .flags          = URL_PROTOCOL_FLAG_NESTED_SCHEME,
+    .priv_data_size = sizeof(AppleHTTPContext),
+};
+#endif
+
+URLProtocol ff_hls_protocol = {
+    .name           = "hls",
+    .url_open       = applehttp_open,
+    .url_read       = applehttp_read,
+    .url_close      = applehttp_close,
+    .flags          = URL_PROTOCOL_FLAG_NESTED_SCHEME,
+    .priv_data_size = sizeof(AppleHTTPContext),
+};
index 6f3b387ddc536e7c2ce3a9784899aa2107fc07b2..443d752b8a0b93e2717415b013409fc6e651a6bb 100644 (file)
@@ -50,5 +50,8 @@
 #ifndef FF_API_CLOSE_INPUT_FILE
 #define FF_API_CLOSE_INPUT_FILE        (LIBAVFORMAT_VERSION_MAJOR < 55)
 #endif
+#ifndef FF_API_APPLEHTTP_PROTO
+#define FF_API_APPLEHTTP_PROTO         (LIBAVFORMAT_VERSION_MAJOR < 55)
+#endif
 
 #endif /* AVFORMAT_VERSION_H */