]> git.sesse.net Git - vlc/blobdiff - modules/stream_filter/httplive.c
Update NEWS, LIST and po for DASH
[vlc] / modules / stream_filter / httplive.c
index f92528f06f6027397eecefd42f97f2f675eb7cd9..299c23b1100b983d21f6ff3c825332de4786fbb6 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * httplive.c: HTTP Live Streaming stream filter
  *****************************************************************************
- * Copyright (C) 2010 M2X BV
+ * Copyright (C) 2010-2011 M2X BV
  * $Id$
  *
  * Author: Jean-Paul Saman <jpsaman _AT_ videolan _DOT_ org>
@@ -29,6 +29,7 @@
 #endif
 
 #include <limits.h>
+#include <errno.h>
 
 #include <vlc_common.h>
 #include <vlc_plugin.h>
@@ -39,9 +40,7 @@
 #include <vlc_arrays.h>
 #include <vlc_stream.h>
 #include <vlc_url.h>
-
-#include <vlc_modules.h>
-#include <vlc_access.h>
+#include <vlc_memory.h>
 
 /*****************************************************************************
  * Module descriptor
@@ -89,12 +88,15 @@ typedef struct hls_stream_s
 
 struct stream_sys_t
 {
-    access_t    *p_access;  /* HTTP access input */
-    vlc_url_t   m3u8;       /* M3U8 url */
+    vlc_url_t     m3u8;         /* M3U8 url */
+    vlc_thread_t  reload;       /* HLS m3u8 reload thread */
+    vlc_thread_t  thread;       /* HLS segment download thread */
+
+    block_t      *peeked;
 
     /* */
-    vlc_array_t  *hls_stream;/* bandwidth adaptation */
-    uint64_t      bandwidth; /* measured bandwidth (bits per second) */
+    vlc_array_t  *hls_stream;   /* bandwidth adaptation */
+    uint64_t      bandwidth;    /* measured bandwidth (bits per second) */
 
     /* Download */
     struct hls_download_s
@@ -136,20 +138,20 @@ static int  Read   (stream_t *, void *p_read, unsigned int i_read);
 static int  Peek   (stream_t *, const uint8_t **pp_peek, unsigned int i_peek);
 static int  Control(stream_t *, int i_query, va_list);
 
-static ssize_t access_ReadM3U8(stream_t *s, vlc_url_t *url, uint8_t **buffer);
-static ssize_t ReadM3U8(stream_t *s, uint8_t **buffer);
-static char *ReadLine(uint8_t *buffer, uint8_t **remain, size_t len);
-
-static int  AccessOpen(stream_t *s, vlc_url_t *url);
-static void AccessClose(stream_t *s);
+static ssize_t read_M3U8_from_stream(stream_t *s, uint8_t **buffer);
+static ssize_t read_M3U8_from_url(stream_t *s, vlc_url_t *url, uint8_t **buffer);
+static char *ReadLine(uint8_t *buffer, uint8_t **pos, size_t len);
 
-static int AccessDownload(stream_t *s, segment_t *segment);
+static int hls_Download(stream_t *s, segment_t *segment);
 
-static void* hls_Thread(vlc_object_t *);
+static void* hls_Thread(void *);
+static void* hls_Reload(void *);
 
 static segment_t *segment_GetSegment(hls_stream_t *hls, int wanted);
 static void segment_Free(segment_t *segment);
 
+static char *ConstructUrl(vlc_url_t *url);
+
 /****************************************************************************
  *
  ****************************************************************************/
@@ -197,7 +199,7 @@ static bool isHTTPLiveStreaming(stream_t *s)
 }
 
 /* HTTP Live Streaming */
-static hls_stream_t *hls_New(vlc_array_t *hls_stream, int id, uint64_t bw, char *uri)
+static hls_stream_t *hls_New(vlc_array_t *hls_stream, const int id, const uint64_t bw, const char *uri)
 {
     hls_stream_t *hls = (hls_stream_t *)malloc(sizeof(hls_stream_t));
     if (hls == NULL) return NULL;
@@ -235,7 +237,35 @@ static void hls_Free(hls_stream_t *hls)
     hls = NULL;
 }
 
-static hls_stream_t *hls_Get(vlc_array_t *hls_stream, int wanted)
+static hls_stream_t *hls_Copy(hls_stream_t *src, const bool b_cp_segments)
+{
+    assert(src);
+    assert(!b_cp_segments); /* FIXME: copying segments is not implemented */
+
+    hls_stream_t *dst = (hls_stream_t *)malloc(sizeof(hls_stream_t));
+    if (dst == NULL) return NULL;
+
+    dst->id = src->id;
+    dst->bandwidth = src->bandwidth;
+    dst->duration = src->duration;
+    dst->size = src->size;
+    dst->sequence = src->sequence;
+    dst->version = src->version;
+    dst->b_cache = src->b_cache;
+    char *uri = ConstructUrl(&src->url);
+    if (uri == NULL)
+    {
+        free(dst);
+        return NULL;
+    }
+    vlc_UrlParse(&dst->url, uri, 0);
+    if (!b_cp_segments)
+        dst->segments = vlc_array_new();
+    vlc_mutex_init(&dst->lock);
+    return dst;
+}
+
+static hls_stream_t *hls_Get(vlc_array_t *hls_stream, const int wanted)
 {
     int count = vlc_array_count(hls_stream);
     if (count <= 0)
@@ -296,7 +326,7 @@ static uint64_t hls_GetStreamSize(hls_stream_t *hls)
 }
 
 /* Segment */
-static segment_t *segment_New(hls_stream_t* hls, int duration, char *uri)
+static segment_t *segment_New(hls_stream_t* hls, const int duration, const char *uri)
 {
     segment_t *segment = (segment_t *)malloc(sizeof(segment_t));
     if (segment == NULL)
@@ -324,7 +354,7 @@ static void segment_Free(segment_t *segment)
     segment = NULL;
 }
 
-static segment_t *segment_GetSegment(hls_stream_t *hls, int wanted)
+static segment_t *segment_GetSegment(hls_stream_t *hls, const int wanted)
 {
     assert(hls);
 
@@ -336,7 +366,7 @@ static segment_t *segment_GetSegment(hls_stream_t *hls, int wanted)
     return (segment_t *) vlc_array_item_at_index(hls->segments, wanted);
 }
 
-static segment_t *segment_Find(hls_stream_t *hls, int sequence)
+static segment_t *segment_Find(hls_stream_t *hls, const int sequence)
 {
     assert(hls);
 
@@ -352,17 +382,18 @@ static segment_t *segment_Find(hls_stream_t *hls, int sequence)
     return NULL;
 }
 
-static int ChooseSegment(stream_t *s, int current)
+static int ChooseSegment(stream_t *s, const int current)
 {
     stream_sys_t *p_sys = (stream_sys_t *)s->p_sys;
     hls_stream_t *hls = hls_Get(p_sys->hls_stream, current);
     if (hls == NULL) return 0;
 
-    /* Choose a segment to start which is no closer then
+    /* Choose a segment to start which is no closer than
      * 3 times the target duration from the end of the playlist.
      */
-    int wanted = -1;
+    int wanted = 0;
     int duration = 0;
+    int sequence = 0;
     int count = vlc_array_count(hls->segments);
     int i = p_sys->b_live ? count - 1 : 0;
 
@@ -373,7 +404,7 @@ static int ChooseSegment(stream_t *s, int current)
 
         if (segment->duration > hls->duration)
         {
-            msg_Err(s, "EXTINF:%d duration is larger then EXT-X-TARGETDURATION:%d",
+            msg_Err(s, "EXTINF:%d duration is larger than EXT-X-TARGETDURATION:%d",
                     segment->duration, hls->duration);
         }
 
@@ -382,6 +413,7 @@ static int ChooseSegment(stream_t *s, int current)
         {
             /* Start point found */
             wanted = p_sys->b_live ? i : 0;
+            sequence = segment->sequence;
             break;
         }
 
@@ -391,7 +423,7 @@ static int ChooseSegment(stream_t *s, int current)
           i++;
     }
 
-    msg_Info(s, "Choose segment %d/%d", wanted, count);
+    msg_Info(s, "Choose segment %d/%d (sequence=%d)", wanted, count, sequence);
     return wanted;
 }
 
@@ -467,7 +499,41 @@ fail:
     return NULL;
 }
 
-static int parse_SegmentInformation(stream_t *s, hls_stream_t *hls, char *p_read, char *uri)
+static char *ConstructUrl(vlc_url_t *url)
+{
+    if ((url->psz_protocol == NULL) ||
+        (url->psz_path == NULL))
+        return NULL;
+
+    if (url->i_port <= 0)
+    {
+        if (strncmp(url->psz_protocol, "https", 5) == 0)
+            url->i_port = 443;
+        else
+            url->i_port = 80;
+    }
+
+    char *psz_url = NULL;
+    if (url->psz_password || url->psz_username)
+    {
+        if (asprintf(&psz_url, "%s://%s:%s@%s:%d%s",
+                     url->psz_protocol,
+                     url->psz_username, url->psz_password,
+                     url->psz_host, url->i_port, url->psz_path) < 0)
+            return NULL;
+    }
+    else
+    {
+        if (asprintf(&psz_url, "%s://%s:%d%s",
+                     url->psz_protocol,
+                     url->psz_host, url->i_port, url->psz_path) < 0)
+            return NULL;
+    }
+
+    return psz_url;
+}
+
+static int parse_SegmentInformation(hls_stream_t *hls, char *p_read, int *duration)
 {
     assert(hls);
     assert(p_read);
@@ -482,10 +548,42 @@ static int parse_SegmentInformation(stream_t *s, hls_stream_t *hls, char *p_read
     token = strtok_r(NULL, ",", &p_next);
     if (token == NULL)
         return VLC_EGENERIC;
-    int duration = atoi(token);
+
+    int value;
+    if (hls->version < 3)
+    {
+       value = strtol(token, NULL, 10);
+       if (errno == ERANGE)
+       {
+           *duration = -1;
+           return VLC_EGENERIC;
+       }
+       *duration = value;
+    }
+    else
+    {
+        double d = strtod(token, (char **) NULL);
+        if (errno == ERANGE)
+        {
+            *duration = -1;
+            return VLC_EGENERIC;
+        }
+        if ((d) - ((int)d) >= 0.5)
+            value = ((int)d) + 1;
+        else
+            value = ((int)d);
+    }
 
     /* Ignore the rest of the line */
 
+    return VLC_SUCCESS;
+}
+
+static int parse_AddSegment(stream_t *s, hls_stream_t *hls, const int duration, const char *uri)
+{
+    assert(hls);
+    assert(uri);
+
     /* Store segment information */
     char *psz_path = NULL;
     if (hls->url.psz_path != NULL)
@@ -526,7 +624,7 @@ static int parse_TargetDuration(stream_t *s, hls_stream_t *hls, char *p_read)
 }
 
 static int parse_StreamInformation(stream_t *s, vlc_array_t **hls_stream,
-                                   hls_stream_t **hls, char *p_read, char *uri)
+                                   hls_stream_t **hls, char *p_read, const char *uri)
 {
     int id;
     uint64_t bw;
@@ -558,7 +656,7 @@ static int parse_StreamInformation(stream_t *s, vlc_array_t **hls_stream,
         return VLC_EGENERIC;
     }
 
-    msg_Info(s, "bandwidth adaption detected (program-id=%d, bandwidth=%"PRIu64").", id, bw);
+    msg_Info(s, "bandwidth adaptation detected (program-id=%d, bandwidth=%"PRIu64").", id, bw);
 
     char *psz_uri = relative_URI(s, uri, NULL);
 
@@ -582,7 +680,8 @@ static int parse_MediaSequence(stream_t *s, hls_stream_t *hls, char *p_read)
     }
 
     if (hls->sequence > 0)
-        msg_Err(s, "EXT-X-MEDIA-SEQUENCE already present in playlist");
+        msg_Err(s, "EXT-X-MEDIA-SEQUENCE already present in playlist (new=%d, old=%d)",
+                    sequence, hls->sequence);
 
     hls->sequence = sequence;
     return VLC_SUCCESS;
@@ -701,7 +800,7 @@ static int parse_Discontinuity(stream_t *s, hls_stream_t *hls, char *p_read)
  * ALLOW-CACHE, EXT-X-STREAM-INF, EXT-X-ENDLIST, EXT-X-DISCONTINUITY,
  * and EXT-X-VERSION.
  */
-static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, ssize_t len)
+static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, const ssize_t len)
 {
     stream_sys_t *p_sys = s->p_sys;
     uint8_t *p_read, *p_begin, *p_end;
@@ -716,6 +815,7 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, ssize_
     char *line = ReadLine(p_begin, &p_read, p_end - p_begin);
     if (line == NULL)
         return VLC_ENOMEM;
+    p_begin = p_read;
 
     if (strncmp(line, "#EXTM3U", 7) != 0)
     {
@@ -752,27 +852,21 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, ssize_
     /* Is it a meta index file ? */
     bool b_meta = (strstr((const char *)buffer, "#EXT-X-STREAM-INF") == NULL) ? false : true;
 
-    if (b_meta)
-        msg_Info(s, "Meta playlist");
-    else
-        msg_Info(s, "%s Playlist HLS protocol version: %d", p_sys->b_live ? "Live": "VOD", version);
-
-    /* */
     int err = VLC_SUCCESS;
-    do
-    {
-        /* Next line */
-        p_begin = p_read;
-        line = ReadLine(p_begin, &p_read, p_end - p_begin);
-        if (line == NULL)
-            break;
 
-        /* */
-        p_begin = p_read;
+    if (b_meta)
+    {
+        msg_Info(s, "Meta playlist");
 
         /* M3U8 Meta Index file */
-        if (b_meta)
-        {
+        do {
+            /* Next line */
+            line = ReadLine(p_begin, &p_read, p_end - p_begin);
+            if (line == NULL)
+                break;
+            p_begin = p_read;
+
+            /* */
             if (strncmp(line, "#EXT-X-STREAM-INF", 17) == 0)
             {
                 p_sys->b_meta = true;
@@ -787,7 +881,7 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, ssize_
 
                     /* Download playlist file from server */
                     uint8_t *buf = NULL;
-                    ssize_t len = access_ReadM3U8(s, &hls->url, &buf);
+                    ssize_t len = read_M3U8_from_url(s, &hls->url, &buf);
                     if (len < 0)
                         err = VLC_EGENERIC;
                     else
@@ -804,51 +898,63 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, ssize_
                             hls->size = hls_GetStreamSize(hls); /* Stream size (approximate) */
                     }
                 }
+                p_begin = p_read;
             }
-        }
+
+            free(line);
+            line = NULL;
+
+            if (p_begin >= p_end)
+                break;
+
+        } while ((err == VLC_SUCCESS) && vlc_object_alive(s));
+
+    }
+    else
+    {
+        msg_Info(s, "%s Playlist HLS protocol version: %d", p_sys->b_live ? "Live": "VOD", version);
+
+        hls_stream_t *hls = NULL;
+        if (p_sys->b_meta)
+            hls = hls_GetLast(streams);
         else
         {
-            hls_stream_t *hls = hls_GetLast(streams);
-            if (hls == NULL)
+            /* No Meta playlist used */
+            hls = hls_New(streams, 0, -1, NULL);
+            if (hls)
             {
-                /* No Meta playlist used */
-                hls = hls_New(streams, 0, -1, NULL);
-                if (hls == NULL)
-                {
-                    msg_Err(s, "No HLS structure created");
-                    err = VLC_ENOMEM;
-                    break;
-                }
-
                 /* Get TARGET-DURATION first */
                 p = (uint8_t *)strstr((const char *)buffer, "#EXT-X-TARGETDURATION:");
                 if (p)
                 {
                     uint8_t *p_rest = NULL;
                     char *psz_duration = ReadLine(p, &p_rest,  p_end - p);
-                    if (psz_duration)
-                    {
-                          err = parse_TargetDuration(s, hls, psz_duration);
-                          free(psz_duration);
-                          p = NULL;
-                    }
+                    if (psz_duration == NULL)
+                        return VLC_EGENERIC;
+                    err = parse_TargetDuration(s, hls, psz_duration);
+                    free(psz_duration);
+                    p = NULL;
                 }
 
+                /* Store version */
                 hls->version = version;
             }
+            else return VLC_ENOMEM;
+        }
+        assert(hls);
+
+        /* */
+        int segment_duration = -1;
+        do
+        {
+            /* Next line */
+            line = ReadLine(p_begin, &p_read, p_end - p_begin);
+            if (line == NULL)
+                break;
+            p_begin = p_read;
 
             if (strncmp(line, "#EXTINF", 7) == 0)
-            {
-                char *uri = ReadLine(p_begin, &p_read, p_end - p_begin);
-                if (uri == NULL)
-                    err = VLC_EGENERIC;
-                else
-                {
-                    err = parse_SegmentInformation(s, hls, line, uri);
-                    free(uri);
-                }
-                p_begin = p_read;
-            }
+                err = parse_SegmentInformation(hls, line, &segment_duration);
             else if (strncmp(line, "#EXT-X-TARGETDURATION", 21) == 0)
                 err = parse_TargetDuration(s, hls, line);
             else if (strncmp(line, "#EXT-X-MEDIA-SEQUENCE", 21) == 0)
@@ -865,17 +971,22 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, ssize_
                 err = parse_Version(s, hls, line);
             else if (strncmp(line, "#EXT-X-ENDLIST", 14) == 0)
                 err = parse_EndList(s, hls);
-        }
+            else if ((strncmp(line, "#", 1) != 0) && (*line != '\0') )
+            {
+                err = parse_AddSegment(s, hls, segment_duration, line);
+                segment_duration = -1; /* reset duration */
+            }
 
-        free(line);
-        line = NULL;
+            free(line);
+            line = NULL;
 
-        if (p_begin >= p_end)
-            break;
+            if (p_begin >= p_end)
+                break;
 
-    } while ((err == VLC_SUCCESS) && vlc_object_alive(s));
+        } while ((err == VLC_SUCCESS) && vlc_object_alive(s));
 
-    free(line);
+        free(line);
+    }
 
     return err;
 }
@@ -884,17 +995,43 @@ static int get_HTTPLiveMetaPlaylist(stream_t *s, vlc_array_t **streams)
 {
     stream_sys_t *p_sys = s->p_sys;
     assert(*streams);
+    int err = VLC_EGENERIC;
 
-    /* Download new playlist file from server */
-    uint8_t *buffer = NULL;
-    ssize_t len = access_ReadM3U8(s, &p_sys->m3u8, &buffer);
-    if (len < 0)
-        return VLC_EGENERIC;
+    /* Duplicate HLS stream META information */
+    for (int i = 0; i < vlc_array_count(p_sys->hls_stream); i++)
+    {
+        hls_stream_t *src, *dst;
+        src = (hls_stream_t *)vlc_array_item_at_index(p_sys->hls_stream, i);
+        if (src == NULL)
+            return VLC_EGENERIC;
 
-    /* Parse HLS m3u8 content. */
-    int err = parse_M3U8(s, *streams, buffer, len);
-    free(buffer);
+        dst = hls_Copy(src, false);
+        if (dst == NULL)
+            return VLC_ENOMEM;
+
+        vlc_array_append(*streams, dst);
+    }
+
+    /* Download new playlist file from server */
+    for (int i = 0; i < vlc_array_count(*streams); i++)
+    {
+        hls_stream_t *hls;
+        hls = (hls_stream_t *)vlc_array_item_at_index(*streams, i);
+        if (hls == NULL)
+            return VLC_EGENERIC;
 
+        /* Download playlist file from server */
+        uint8_t *buf = NULL;
+        ssize_t len = read_M3U8_from_url(s, &hls->url, &buf);
+        if (len < 0)
+            err = VLC_EGENERIC;
+        else
+        {
+            /* Parse HLS m3u8 content. */
+            err = parse_M3U8(s, *streams, buf, len);
+            free(buf);
+        }
+    }
     return err;
 }
 
@@ -905,6 +1042,7 @@ static int hls_UpdatePlaylist(stream_t *s, hls_stream_t *hls_new, hls_stream_t *
 
     msg_Info(s, "updating hls stream (program-id=%d, bandwidth=%"PRIu64") has %d segments",
              hls_new->id, hls_new->bandwidth, count);
+
     for (int n = 0; n < count; n++)
     {
         segment_t *p = segment_GetSegment(hls_new, n);
@@ -914,16 +1052,34 @@ static int hls_UpdatePlaylist(stream_t *s, hls_stream_t *hls_new, hls_stream_t *
         segment_t *segment = segment_Find(*hls, p->sequence);
         if (segment)
         {
+            assert(p->url.psz_path);
+            assert(segment->url.psz_path);
+
             /* they should be the same */
             if ((p->sequence != segment->sequence) ||
                 (p->duration != segment->duration) ||
                 (strcmp(p->url.psz_path, segment->url.psz_path) != 0))
             {
-                msg_Err(s, "existing segment %d found with different content",
-                        p->sequence);
-                msg_Err(s, "- sequence: new=%d, old=%d", p->sequence, segment->sequence);
-                msg_Err(s, "- duration: new=%d, old=%d", p->duration, segment->duration);
-                msg_Err(s, "- file: new=%s, old=%s", p->url.psz_path, segment->url.psz_path);
+                msg_Warn(s, "existing segment found with different content - resetting");
+                msg_Warn(s, "- sequence: new=%d, old=%d", p->sequence, segment->sequence);
+                msg_Warn(s, "- duration: new=%d, old=%d", p->duration, segment->duration);
+                msg_Warn(s, "- file: new=%s", p->url.psz_path);
+                msg_Warn(s, "        old=%s", segment->url.psz_path);
+
+                /* Resetting content */
+                char *psz_url = ConstructUrl(&p->url);
+                if (psz_url == NULL)
+                {
+                    msg_Err(s, "Failed updating segment %d - skipping it",  p->sequence);
+                    segment_Free(p);
+                    continue;
+                }
+                segment->sequence = p->sequence;
+                segment->duration = p->duration;
+                vlc_UrlClean(&segment->url);
+                vlc_UrlParse(&segment->url, psz_url, 0);
+                segment_Free(p);
+                free(psz_url);
             }
         }
         else
@@ -932,23 +1088,20 @@ static int hls_UpdatePlaylist(stream_t *s, hls_stream_t *hls_new, hls_stream_t *
             segment_t *l = segment_GetSegment(*hls, last);
             if (l == NULL) goto fail_and_unlock;
 
-            if ((l->sequence + 1) == p->sequence)
-            {
-                vlc_array_append((*hls)->segments, p);
-                msg_Info(s, "- segment %d appended", p->sequence);
-            }
-            else /* there is a gap */
+            if ((l->sequence + 1) != p->sequence)
             {
-                msg_Err(s, "gap in sequence numbers found: new=%d expected old=%d",
-                        p->sequence, l->sequence);
-                goto fail_and_unlock;
+                msg_Err(s, "gap in sequence numbers found: new=%d expected %d",
+                        p->sequence, l->sequence+1);
             }
+            vlc_array_append((*hls)->segments, p);
+            msg_Info(s, "- segment %d appended", p->sequence);
         }
         vlc_mutex_unlock(&(*hls)->lock);
     }
     return VLC_SUCCESS;
 
 fail_and_unlock:
+    assert(0);
     vlc_mutex_unlock(&(*hls)->lock);
     return VLC_EGENERIC;
 }
@@ -962,16 +1115,29 @@ static int hls_ReloadPlaylist(stream_t *s)
         return VLC_ENOMEM;
 
     msg_Info(s, "Reloading HLS live meta playlist");
+
     if (get_HTTPLiveMetaPlaylist(s, &hls_streams) != VLC_SUCCESS)
-        goto fail;
+    {
+        /* Free hls streams */
+        for (int i = 0; i < vlc_array_count(hls_streams); i++)
+        {
+            hls_stream_t *hls;
+            hls = (hls_stream_t *)vlc_array_item_at_index(hls_streams, i);
+            if (hls) hls_Free(hls);
+        }
+        vlc_array_destroy(hls_streams);
 
-    int count = vlc_array_count(hls_streams);
+        msg_Err(s, "reloading playlist failed");
+        return VLC_EGENERIC;
+    }
 
     /* merge playlists */
+    int count = vlc_array_count(hls_streams);
     for (int n = 0; n < count; n++)
     {
         hls_stream_t *hls_new = hls_Get(hls_streams, n);
-        if (hls_new == NULL) goto fail;
+        if (hls_new == NULL)
+            continue;
 
         hls_stream_t *hls_old = hls_Find(p_sys->hls_stream, hls_new);
         if (hls_old == NULL)
@@ -981,16 +1147,11 @@ static int hls_ReloadPlaylist(stream_t *s)
                      hls_new->id, hls_new->bandwidth);
         }
         else if (hls_UpdatePlaylist(s, hls_new, &hls_old) != VLC_SUCCESS)
-            goto fail;
+            msg_Info(s, "failed updating HLS stream (id=%d, bandwidth=%"PRIu64")",
+                     hls_new->id, hls_new->bandwidth);
     }
-
     vlc_array_destroy(hls_streams);
     return VLC_SUCCESS;
-
-fail:
-    msg_Err(s, "reloading playlist failed");
-    vlc_array_destroy(hls_streams);
-    return VLC_EGENERIC;
 }
 
 /****************************************************************************
@@ -1048,14 +1209,16 @@ static int Download(stream_t *s, hls_stream_t *hls, segment_t *segment, int *cur
         int estimated = (int)(size / p_sys->bandwidth);
         if (estimated > segment->duration)
         {
-            msg_Warn(s,"downloading of segment %d takes %ds, which is longer then its playback (%ds)",
+            msg_Warn(s,"downloading of segment %d takes %ds, which is longer than its playback (%ds)",
                         segment->sequence, estimated, segment->duration);
         }
     }
 
     mtime_t start = mdate();
-    if (AccessDownload(s, segment) != VLC_SUCCESS)
+    if (hls_Download(s, segment) != VLC_SUCCESS)
     {
+        msg_Err(s, "downloaded segment %d from stream %d failed",
+                    segment->sequence, *cur_stream);
         vlc_mutex_unlock(&segment->lock);
         return VLC_EGENERIC;
     }
@@ -1088,7 +1251,7 @@ static int Download(stream_t *s, hls_stream_t *hls, segment_t *segment, int *cur
     return VLC_SUCCESS;
 }
 
-static void* hls_Thread(vlc_object_t *p_this)
+static void* hls_Thread(void *p_this)
 {
     stream_t *s = (stream_t *)p_this;
     stream_sys_t *p_sys = s->p_sys;
@@ -1115,10 +1278,11 @@ static void* hls_Thread(vlc_object_t *p_this)
                     (p_sys->download.segment >= count)) &&
                    (p_sys->download.seek == -1))
             {
-                if (p_sys->b_live && (mdate() >= p_sys->playlist.wakeup))
-                    break;
                 vlc_cond_wait(&p_sys->download.wait, &p_sys->download.lock_wait);
-                if (!vlc_object_alive(s)) break;
+                if (p_sys->b_live /*&& (mdate() >= p_sys->playlist.wakeup)*/)
+                    break;
+                if (!vlc_object_alive(s))
+                    break;
             }
             /* */
             if (p_sys->download.seek >= 0)
@@ -1131,32 +1295,6 @@ static void* hls_Thread(vlc_object_t *p_this)
 
         if (!vlc_object_alive(s)) break;
 
-        /* reload the m3u8 index file */
-        if (p_sys->b_live)
-        {
-            double wait = 1;
-            mtime_t now = mdate();
-            if (now >= p_sys->playlist.wakeup)
-            {
-                if (hls_ReloadPlaylist(s) != VLC_SUCCESS)
-                {
-                    /* No change in playlist, then backoff */
-                    p_sys->playlist.tries++;
-                    if (p_sys->playlist.tries == 1) wait = 0.5;
-                    else if (p_sys->playlist.tries == 2) wait = 1;
-                    else if (p_sys->playlist.tries >= 3) wait = 3;
-                }
-                else p_sys->playlist.tries = 0;
-
-                /* determine next time to update playlist */
-                p_sys->playlist.last = now;
-                p_sys->playlist.wakeup = now + ((mtime_t)(hls->duration * wait)
-                                                * (mtime_t)1000000);
-            }
-
-            if (!vlc_object_alive(s)) break;
-        }
-
         vlc_mutex_lock(&hls->lock);
         segment_t *segment = segment_GetSegment(hls, p_sys->download.segment);
         vlc_mutex_unlock(&hls->lock);
@@ -1191,6 +1329,59 @@ static void* hls_Thread(vlc_object_t *p_this)
     return NULL;
 }
 
+static void* hls_Reload(void *p_this)
+{
+    stream_t *s = (stream_t *)p_this;
+    stream_sys_t *p_sys = s->p_sys;
+
+    assert(p_sys->b_live);
+
+    int canc = vlc_savecancel();
+
+    double wait = 0.5;
+    while (vlc_object_alive(s))
+    {
+        mtime_t now = mdate();
+        if (now >= p_sys->playlist.wakeup)
+        {
+            /* reload the m3u8 */
+            if (hls_ReloadPlaylist(s) != VLC_SUCCESS)
+            {
+                /* No change in playlist, then backoff */
+                p_sys->playlist.tries++;
+                if (p_sys->playlist.tries == 1) wait = 0.5;
+                else if (p_sys->playlist.tries == 2) wait = 1;
+                else if (p_sys->playlist.tries >= 3) wait = 2;
+
+                /* Can we afford to backoff? */
+                if (p_sys->download.segment - p_sys->playback.segment < 3)
+                {
+                    p_sys->playlist.tries = 0;
+                    wait = 0.5;
+                }
+            }
+            else
+            {
+                p_sys->playlist.tries = 0;
+                wait = 0.5;
+            }
+
+            hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->download.stream);
+            assert(hls);
+
+            /* determine next time to update playlist */
+            p_sys->playlist.last = now;
+            p_sys->playlist.wakeup = now + ((mtime_t)(hls->duration * wait)
+                                                   * (mtime_t)1000000);
+        }
+
+        mwait(p_sys->playlist.wakeup);
+    }
+
+    vlc_restorecancel(canc);
+    return NULL;
+}
+
 static int Prefetch(stream_t *s, int *current)
 {
     stream_sys_t *p_sys = s->p_sys;
@@ -1249,206 +1440,129 @@ again:
 }
 
 /****************************************************************************
- * Access
+ *
  ****************************************************************************/
-static int AccessOpen(stream_t *s, vlc_url_t *url)
-{
-    stream_sys_t *p_sys = (stream_sys_t *) s->p_sys;
-
-    if ((url->psz_protocol == NULL) ||
-        (url->psz_path == NULL))
-        return VLC_EGENERIC;
-
-    p_sys->p_access = vlc_object_create(s, sizeof(access_t));
-    if (p_sys->p_access == NULL)
-        return VLC_ENOMEM;
-
-    if (url->i_port <= 0)
-    {
-        if (strncmp(url->psz_protocol, "https", 5) == 0)
-            url->i_port = 443;
-        else
-            url->i_port = 80;
-    }
-
-    p_sys->p_access->psz_access = strdup(url->psz_protocol);
-    p_sys->p_access->psz_filepath = strdup(url->psz_path);
-    if (url->psz_password || url->psz_username)
-    {
-        if (asprintf(&p_sys->p_access->psz_location, "%s:%s@%s:%d%s",
-                     url->psz_username, url->psz_password,
-                     url->psz_host, url->i_port, url->psz_path) < 0)
-        {
-            msg_Err(s, "creating http access module");
-            goto fail;
-        }
-    }
-    else
-    {
-        if (asprintf(&p_sys->p_access->psz_location, "%s:%d%s",
-                     url->psz_host, url->i_port, url->psz_path) < 0)
-        {
-            msg_Err(s, "creating http access module");
-            goto fail;
-        }
-    }
-
-    vlc_object_attach(p_sys->p_access, s);
-    p_sys->p_access->p_module =
-        module_need(p_sys->p_access, "access", p_sys->p_access->psz_access, true);
-    if (p_sys->p_access->p_module == NULL)
-    {
-        msg_Err(s, "could not load http access module");
-        goto fail;
-    }
-
-    return VLC_SUCCESS;
-
-fail:
-    vlc_object_release(p_sys->p_access);
-    p_sys->p_access = NULL;
-    return VLC_EGENERIC;
-}
-
-static void AccessClose(stream_t *s)
-{
-    stream_sys_t *p_sys = (stream_sys_t *) s->p_sys;
-
-    if (p_sys->p_access)
-    {
-        vlc_object_kill(p_sys->p_access);
-        free(p_sys->p_access->psz_access);
-        if (p_sys->p_access->p_module)
-            module_unneed(p_sys->p_access,
-                          p_sys->p_access->p_module);
-
-        vlc_object_release(p_sys->p_access);
-        p_sys->p_access = NULL;
-    }
-}
-
-static int AccessDownload(stream_t *s, segment_t *segment)
+static int hls_Download(stream_t *s, segment_t *segment)
 {
-    stream_sys_t *p_sys = (stream_sys_t *) s->p_sys;
-
     assert(segment);
 
-    /* Download new playlist file from server */
-    if (AccessOpen(s, &segment->url) != VLC_SUCCESS)
+    /* Construct URL */
+    char *psz_url = ConstructUrl(&segment->url);
+    if (psz_url == NULL)
+           return VLC_ENOMEM;
+
+    stream_t *p_ts = stream_UrlNew(s, psz_url);
+    free(psz_url);
+    if (p_ts == NULL)
         return VLC_EGENERIC;
 
-    segment->size = p_sys->p_access->info.i_size;
+    segment->size = stream_Size(p_ts);
     assert(segment->size > 0);
 
     segment->data = block_Alloc(segment->size);
     if (segment->data == NULL)
     {
-        AccessClose(s);
+        stream_Delete(p_ts);
         return VLC_ENOMEM;
     }
 
     assert(segment->data->i_buffer == segment->size);
 
     ssize_t length = 0, curlen = 0;
+    uint64_t size;
     do
     {
-        if (p_sys->p_access->info.i_size > segment->size)
+        size = stream_Size(p_ts);
+        if (size > segment->size)
         {
             msg_Dbg(s, "size changed %"PRIu64, segment->size);
-            segment->data = block_Realloc(segment->data, 0, p_sys->p_access->info.i_size);
-            if (segment->data == NULL)
+            block_t *p_block = block_Realloc(segment->data, 0, size);
+            if (p_block == NULL)
             {
-                AccessClose(s);
+                stream_Delete(p_ts);
+                block_Release(segment->data);
+                segment->data = NULL;
                 return VLC_ENOMEM;
             }
-            segment->size = p_sys->p_access->info.i_size;
+            segment->data = p_block;
+            segment->size = size;
             assert(segment->data->i_buffer == segment->size);
+            p_block = NULL;
         }
-        length = p_sys->p_access->pf_read(p_sys->p_access,
-                    segment->data->p_buffer + curlen, segment->size - curlen);
-        if ((length <= 0) || ((uint64_t)length >= segment->size))
+        length = stream_Read(p_ts, segment->data->p_buffer + curlen, segment->size - curlen);
+        if (length <= 0)
             break;
         curlen += length;
     } while (vlc_object_alive(s));
 
-    AccessClose(s);
+    stream_Delete(p_ts);
     return VLC_SUCCESS;
 }
 
 /* Read M3U8 file */
-static ssize_t access_ReadM3U8(stream_t *s, vlc_url_t *url, uint8_t **buffer)
+static ssize_t read_M3U8_from_stream(stream_t *s, uint8_t **buffer)
 {
-    stream_sys_t *p_sys = (stream_sys_t *) s->p_sys;
+    int64_t total_bytes = 0;
+    int64_t total_allocated = 0;
+    uint8_t *p = NULL;
 
-    assert(*buffer == NULL);
+    while (1)
+    {
+        char buf[4096];
+        int64_t bytes;
 
-    /* Download new playlist file from server */
-    if (AccessOpen(s, url) != VLC_SUCCESS)
-        return VLC_EGENERIC;
+        bytes = stream_Read(s, buf, sizeof(buf));
+        if (bytes == 0)
+            break;      /* EOF ? */
+        else if (bytes < 0)
+            return bytes;
 
-    ssize_t size = p_sys->p_access->info.i_size;
-    if (size == 0) size = 1024; /* no Content-Length */
+        if ( (total_bytes + bytes + 1) > total_allocated )
+        {
+            if (total_allocated)
+                total_allocated *= 2;
+            else
+                total_allocated = __MIN(bytes+1, sizeof(buf));
 
-    *buffer = calloc(1, size);
-    if (*buffer == NULL)
-    {
-        AccessClose(s);
-        return VLC_ENOMEM;
+            p = realloc_or_free(p, total_allocated);
+            if (p == NULL)
+                return VLC_ENOMEM;
+        }
+
+        memcpy(p+total_bytes, buf, bytes);
+        total_bytes += bytes;
     }
 
-    ssize_t length = 0, curlen = 0;
-    do
-    {
-        length = p_sys->p_access->pf_read(p_sys->p_access, *buffer + curlen, size - curlen);
-        if ((length <= 0) || (length >= size))
-            break;
-        curlen += length;
-        if (curlen >= size)
-        {
-            uint8_t *tmp = realloc(*buffer, size + 1024);
-            if (tmp == NULL)
-                break;
-            size += 1024;
-            *buffer = tmp;
-        }
-    } while (vlc_object_alive(s));
+    if (total_allocated == 0)
+        return VLC_EGENERIC;
 
-    AccessClose(s);
-    return size;
+    p[total_bytes] = '\0';
+    *buffer = p;
+
+    return total_bytes;
 }
 
-static ssize_t ReadM3U8(stream_t *s, uint8_t **buffer)
+static ssize_t read_M3U8_from_url(stream_t *s, vlc_url_t *url, uint8_t **buffer)
 {
     assert(*buffer == NULL);
 
-    int64_t size = stream_Size(s->p_source);
-    if (size == 0) size = 1024; /* no Content-Length */
+    /* Construct URL */
+    char *psz_url = ConstructUrl(url);
+    if (psz_url == NULL)
+           return VLC_ENOMEM;
 
-    *buffer = calloc(1, size);
-    if (*buffer == NULL)
-        return VLC_ENOMEM;
+    stream_t *p_m3u8 = stream_UrlNew(s, psz_url);
+    free(psz_url);
+    if (p_m3u8 == NULL)
+        return VLC_EGENERIC;
 
-    int64_t len = 0, curlen = 0;
-    do {
-        int read = ((size - curlen) >= INT_MAX) ? INT_MAX : (size - curlen);
-        len = stream_Read(s->p_source, *buffer + curlen, read);
-        if (len <= 0)
-            break;
-        curlen += len;
-        if (curlen >= size)
-        {
-            uint8_t *tmp = realloc(*buffer, size + 1024);
-            if (tmp == NULL)
-                break;
-            size += 1024;
-            *buffer = tmp;
-        }
-    } while (vlc_object_alive(s));
+    ssize_t size = read_M3U8_from_stream(p_m3u8, buffer);
+    stream_Delete(p_m3u8);
 
     return size;
 }
 
-static char *ReadLine(uint8_t *buffer, uint8_t **remain, size_t len)
+static char *ReadLine(uint8_t *buffer, uint8_t **pos, const size_t len)
 {
     assert(buffer);
 
@@ -1459,17 +1573,22 @@ static char *ReadLine(uint8_t *buffer, uint8_t **remain, size_t len)
 
     while (p < end)
     {
-        if (*p == '\n')
+        if ((*p == '\n') || (*p == '\0'))
             break;
         p++;
     }
 
-    /* copy line excluding \n */
+    /* copy line excluding \n or \0 */
     line = strndup((char *)begin, p - begin);
 
-    /* next pass start after \n */
-    p++;
-    *remain = p;
+    if (*p == '\0')
+        *pos = end;
+    else
+    {
+        /* next pass start after \n */
+        p++;
+        *pos = p;
+    }
 
     return line;
 }
@@ -1521,7 +1640,7 @@ static int Open(vlc_object_t *p_this)
 
     /* Parse HLS m3u8 content. */
     uint8_t *buffer = NULL;
-    ssize_t len = ReadM3U8(s, &buffer);
+    ssize_t len = read_M3U8_from_stream(s->p_source, &buffer);
     if (len < 0)
         goto fail;
     if (parse_M3U8(s, p_sys->hls_stream, buffer, len) != VLC_SUCCESS)
@@ -1537,7 +1656,7 @@ static int Open(vlc_object_t *p_this)
 
     if (p_sys->b_live && (p_sys->playback.segment < 0))
     {
-        msg_Warn(s, "less data then 3 times 'target duration' available for live playback, playback may stall");
+        msg_Warn(s, "less data than 3 times 'target duration' available for live playback, playback may stall");
     }
 
     if (Prefetch(s, &current) != VLC_SUCCESS)
@@ -1546,6 +1665,14 @@ static int Open(vlc_object_t *p_this)
         goto fail;
     }
 
+
+    p_sys->download.stream = current;
+    p_sys->playback.stream = current;
+    p_sys->download.seek = -1;
+
+    vlc_mutex_init(&p_sys->download.lock_wait);
+    vlc_cond_init(&p_sys->download.wait);
+
     /* Initialize HLS live stream */
     if (p_sys->b_live)
     {
@@ -1553,18 +1680,17 @@ static int Open(vlc_object_t *p_this)
         p_sys->playlist.last = mdate();
         p_sys->playlist.wakeup = p_sys->playlist.last +
                 ((mtime_t)hls->duration * UINT64_C(1000000));
-    }
 
-    p_sys->download.stream = current;
-    p_sys->playback.stream = current;
-    p_sys->download.seek = -1;
-
-    vlc_mutex_init(&p_sys->download.lock_wait);
-    vlc_cond_init(&p_sys->download.wait);
+        if (vlc_clone(&p_sys->reload, hls_Reload, s, VLC_THREAD_PRIORITY_LOW))
+        {
+            goto fail_thread;
+        }
+    }
 
-    if (vlc_thread_create(s, "HTTP Live Streaming client",
-                          hls_Thread, VLC_THREAD_PRIORITY_INPUT))
+    if (vlc_clone(&p_sys->thread, hls_Thread, s, VLC_THREAD_PRIORITY_INPUT))
     {
+        if (p_sys->b_live)
+            vlc_join(p_sys->reload, NULL);
         goto fail_thread;
     }
 
@@ -1606,7 +1732,9 @@ static void Close(vlc_object_t *p_this)
     vlc_mutex_unlock(&p_sys->download.lock_wait);
 
     /* */
-    vlc_thread_join(s);
+    if (p_sys->b_live)
+        vlc_join(p_sys->reload, NULL);
+    vlc_join(p_sys->thread, NULL);
     vlc_mutex_destroy(&p_sys->download.lock_wait);
     vlc_cond_destroy(&p_sys->download.wait);
 
@@ -1621,6 +1749,8 @@ static void Close(vlc_object_t *p_this)
 
     /* */
     vlc_UrlClean(&p_sys->m3u8);
+    if (p_sys->peeked)
+        block_Release (p_sys->peeked);
     free(p_sys);
 }
 
@@ -1809,10 +1939,9 @@ static int Read(stream_t *s, void *buffer, unsigned int i_read)
 static int Peek(stream_t *s, const uint8_t **pp_peek, unsigned int i_peek)
 {
     stream_sys_t *p_sys = s->p_sys;
-    size_t curlen = 0;
     segment_t *segment;
+    unsigned int len = i_peek;
 
-again:
     segment = GetSegment(s);
     if (segment == NULL)
     {
@@ -1823,29 +1952,77 @@ again:
 
     vlc_mutex_lock(&segment->lock);
 
-    /* remember segment to peek */
-    int peek_segment = p_sys->playback.segment;
-    do
+    size_t i_buff = segment->data->i_buffer;
+    uint8_t *p_buff = segment->data->p_buffer;
+
+    if (i_peek < i_buff)
     {
-        if (i_peek < segment->data->i_buffer)
-        {
-            *pp_peek = segment->data->p_buffer;
-            curlen += i_peek;
-        }
-        else
+        *pp_peek = p_buff;
+        vlc_mutex_unlock(&segment->lock);
+        return i_peek;
+    }
+
+    else /* This will seldom be run */
+    {
+        /* remember segment to read */
+        int peek_segment = p_sys->playback.segment;
+        size_t curlen = 0;
+        segment_t *nsegment;
+        p_sys->playback.segment++;
+        block_t *peeked = p_sys->peeked;
+
+        if (peeked == NULL)
+            peeked = block_Alloc (i_peek);
+        else if (peeked->i_buffer < i_peek)
+            peeked = block_Realloc (peeked, 0, i_peek);
+        if (peeked == NULL)
+            return 0;
+
+        memcpy(peeked->p_buffer, p_buff, i_buff);
+        curlen = i_buff;
+        len -= i_buff;
+        vlc_mutex_unlock(&segment->lock);
+
+        i_buff = peeked->i_buffer;
+        p_buff = peeked->p_buffer;
+        *pp_peek = p_buff;
+
+        while ((curlen < i_peek) && vlc_object_alive(s))
         {
-            p_sys->playback.segment++;
-            vlc_mutex_unlock(&segment->lock);
-            goto again;
-        }
-    } while ((curlen < i_peek) && vlc_object_alive(s));
+            nsegment = GetSegment(s);
+            if (nsegment == NULL)
+            {
+                msg_Err(s, "segment %d should have been available (stream %d)",
+                        p_sys->playback.segment, p_sys->playback.stream);
+                /* restore segment to read */
+                p_sys->playback.segment = peek_segment;
+                return curlen; /* eof? */
+            }
 
-    /* restore segment to read */
-    p_sys->playback.segment = peek_segment;
+            vlc_mutex_lock(&nsegment->lock);
 
-    vlc_mutex_unlock(&segment->lock);
+            if (len < nsegment->data->i_buffer)
+            {
+                memcpy(p_buff + curlen, nsegment->data->p_buffer, len);
+                curlen += len;
+            }
+            else
+            {
+                size_t i_nbuff = nsegment->data->i_buffer;
+                memcpy(p_buff + curlen, nsegment->data->p_buffer, i_nbuff);
+                curlen += i_nbuff;
+                len -= i_nbuff;
 
-    return curlen;
+                p_sys->playback.segment++;
+            }
+
+            vlc_mutex_unlock(&nsegment->lock);
+        }
+
+        /* restore segment to read */
+        p_sys->playback.segment = peek_segment;
+        return curlen;
+    }
 }
 
 static bool hls_MaySeek(stream_t *s)
@@ -1889,7 +2066,7 @@ static uint64_t GetStreamSize(stream_t *s)
     return size;
 }
 
-static int segment_Seek(stream_t *s, uint64_t pos)
+static int segment_Seek(stream_t *s, const uint64_t pos)
 {
     stream_sys_t *p_sys = s->p_sys;
 
@@ -1996,7 +2173,6 @@ static int Control(stream_t *s, int i_query, va_list args)
     switch (i_query)
     {
         case STREAM_CAN_SEEK:
-        case STREAM_CAN_FASTSEEK:
             *(va_arg (args, bool *)) = hls_MaySeek(s);
             break;
         case STREAM_GET_POSITION: