]> git.sesse.net Git - vlc/blobdiff - modules/stream_filter/httplive.c
Remove constant { true } aout_instance_t.mixer_allocation
[vlc] / modules / stream_filter / httplive.c
index 792080fb3331b5554c912cf23f94e5fab0853d46..5f07c0db6c59a6a2427460fed8f4d32a43f571d6 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>
@@ -28,6 +28,9 @@
 # include "config.h"
 #endif
 
+#include <limits.h>
+#include <errno.h>
+
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 
@@ -38,9 +41,6 @@
 #include <vlc_stream.h>
 #include <vlc_url.h>
 
-#include <vlc_modules.h>
-#include <vlc_access.h>
-
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
@@ -87,12 +87,13 @@ 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 */
 
     /* */
-    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
@@ -134,13 +135,14 @@ 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 int  AccessOpen(stream_t *s, vlc_url_t *url);
-static void AccessClose(stream_t *s);
-static char *AccessReadLine(access_t *p_access, uint8_t *psz_tmp, size_t i_len);
-static int AccessDownload(stream_t *s, segment_t *segment);
+static ssize_t read_M3U8(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 **pos, size_t len);
 
-static void* hls_Thread(vlc_object_t *);
-static int get_HTTPLivePlaylist(stream_t *s, hls_stream_t *hls);
+static int hls_Download(stream_t *s, segment_t *segment);
+
+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);
@@ -148,6 +150,17 @@ static void segment_Free(segment_t *segment);
 /****************************************************************************
  *
  ****************************************************************************/
+static const char *const ext[] = {
+    "#EXT-X-TARGETDURATION",
+    "#EXT-X-MEDIA-SEQUENCE",
+    "#EXT-X-KEY",
+    "#EXT-X-ALLOW-CACHE",
+    "#EXT-X-ENDLIST",
+    "#EXT-X-STREAM-INF",
+    "#EXT-X-DISCONTINUITY",
+    "#EXT-X-VERSION"
+};
+
 static bool isHTTPLiveStreaming(stream_t *s)
 {
     const uint8_t *peek, *peek_end;
@@ -167,10 +180,12 @@ static bool isHTTPLiveStreaming(stream_t *s)
     {
         if (*peek == '#')
         {
-            if (strncasecmp((const char*)peek, "#EXT-X-TARGETDURATION", 21) == 0)
-                return true;
-            else if (strncasecmp((const char*)peek, "#EXT-X-STREAM-INF", 17) == 0)
-                return true;
+            for (unsigned int i = 0; i < ARRAY_SIZE(ext); i++)
+            {
+                char *p = strstr((const char*)peek, ext[i]);
+                if (p != NULL)
+                    return true;
+            }
         }
         peek++;
     };
@@ -179,7 +194,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;
@@ -217,7 +232,7 @@ 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_Get(vlc_array_t *hls_stream, const int wanted)
 {
     int count = vlc_array_count(hls_stream);
     if (count <= 0)
@@ -278,7 +293,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)
@@ -306,7 +321,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);
 
@@ -318,7 +333,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);
 
@@ -334,13 +349,49 @@ static segment_t *segment_Find(hls_stream_t *hls, int sequence)
     return NULL;
 }
 
-static int live_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;
-    int wanted = vlc_array_count(hls->segments) - 4;
-    return (wanted < 0) ? 0 : wanted;
+
+    /* Choose a segment to start which is no closer then
+     * 3 times the target duration from the end of the playlist.
+     */
+    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;
+
+    while((i >= 0) && (i < count) && vlc_object_alive(s))
+    {
+        segment_t *segment = segment_GetSegment(hls, i);
+        assert(segment);
+
+        if (segment->duration > hls->duration)
+        {
+            msg_Err(s, "EXTINF:%d duration is larger then EXT-X-TARGETDURATION:%d",
+                    segment->duration, hls->duration);
+        }
+
+        duration += segment->duration;
+        if (duration >= 3 * hls->duration)
+        {
+            /* Start point found */
+            wanted = p_sys->b_live ? i : 0;
+            sequence = segment->sequence;
+            break;
+        }
+
+        if (p_sys->b_live)
+            i-- ;
+        else
+          i++;
+    }
+
+    msg_Info(s, "Choose segment %d/%d (sequence=%d)", wanted, count, sequence);
+    return wanted;
 }
 
 /* Parsing */
@@ -383,6 +434,9 @@ static char *relative_URI(stream_t *s, const char *uri, const char *path)
     if (p != NULL)
         return NULL;
 
+    if (p_sys->m3u8.psz_path == NULL)
+        return NULL;
+
     char *psz_path = strdup(p_sys->m3u8.psz_path);
     if (psz_path == NULL) return NULL;
     p = strrchr(psz_path, '/');
@@ -391,15 +445,17 @@ static char *relative_URI(stream_t *s, const char *uri, const char *path)
     char *psz_uri = NULL;
     if (p_sys->m3u8.psz_password || p_sys->m3u8.psz_username)
     {
-        if (asprintf(&psz_uri, "%s://%s:%s@%s%s/%s", p_sys->m3u8.psz_protocol,
+        if (asprintf(&psz_uri, "%s://%s:%s@%s:%d%s/%s", p_sys->m3u8.psz_protocol,
                      p_sys->m3u8.psz_username, p_sys->m3u8.psz_password,
-                     p_sys->m3u8.psz_host, path ? path : psz_path, uri) < 0)
+                     p_sys->m3u8.psz_host, p_sys->m3u8.i_port,
+                     path ? path : psz_path, uri) < 0)
             goto fail;
     }
     else
     {
-        if (asprintf(&psz_uri, "%s://%s%s/%s", p_sys->m3u8.psz_protocol,
-                 p_sys->m3u8.psz_host, path ? path : psz_path, uri) < 0)
+        if (asprintf(&psz_uri, "%s://%s:%d%s/%s", p_sys->m3u8.psz_protocol,
+                     p_sys->m3u8.psz_host, p_sys->m3u8.i_port,
+                     path ? path : psz_path, uri) < 0)
            goto fail;
     }
     free(psz_path);
@@ -410,29 +466,101 @@ fail:
     return NULL;
 }
 
-static void parse_SegmentInformation(stream_t *s, hls_stream_t *hls, char *p_read, char *uri)
+static char *ConstructUrl(vlc_url_t *url)
 {
-    stream_sys_t *p_sys = s->p_sys;
+    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);
 
-    int duration;
-    int ret = sscanf(p_read, "#EXTINF:%d,", &duration);
-    if (ret != 1)
+    /* strip of #EXTINF: */
+    char *p_next = NULL;
+    char *token = strtok_r(p_read, ":", &p_next);
+    if (token == NULL)
+        return VLC_EGENERIC;
+
+    /* read duration */
+    token = strtok_r(NULL, ",", &p_next);
+    if (token == NULL)
+        return VLC_EGENERIC;
+
+    int value;
+    if (hls->version < 3)
     {
-        msg_Err(s, "expected #EXTINF:<s>,");
-        p_sys->b_error = true;
-        return;
+       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);
     }
 
-    char *psz_path = strdup(hls->url.psz_path);
-    if (psz_path == NULL)
+    /* 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)
     {
-        p_sys->b_error = true;
-        return;
+        psz_path = strdup(hls->url.psz_path);
+        if (psz_path == NULL)
+            return VLC_ENOMEM;
+        char *p = strrchr(psz_path, '/');
+        if (p) *p = '\0';
     }
-    char *p = strrchr(psz_path, '/');
-    if (p) *p = '\0';
     char *psz_uri = relative_URI(s, uri, psz_path);
     free(psz_path);
 
@@ -440,14 +568,10 @@ static void parse_SegmentInformation(stream_t *s, hls_stream_t *hls, char *p_rea
     segment_t *segment = segment_New(hls, duration, psz_uri ? psz_uri : uri);
     if (segment)
         segment->sequence = hls->sequence + vlc_array_count(hls->segments) - 1;
-    if (duration > hls->duration)
-    {
-        msg_Err(s, "EXTINF:%d duration is larger then EXT-X-TARGETDURATION:%d",
-                duration, hls->duration);
-    }
     vlc_mutex_unlock(&hls->lock);
 
     free(psz_uri);
+    return segment ? VLC_SUCCESS : VLC_ENOMEM;
 }
 
 static int parse_TargetDuration(stream_t *s, hls_stream_t *hls, char *p_read)
@@ -466,21 +590,20 @@ static int parse_TargetDuration(stream_t *s, hls_stream_t *hls, char *p_read)
     return VLC_SUCCESS;
 }
 
-static void parse_StreamInformation(stream_t *s, vlc_array_t **hls_stream,
-                                    char *p_read, char *uri)
+static int parse_StreamInformation(stream_t *s, vlc_array_t **hls_stream,
+                                   hls_stream_t **hls, char *p_read, const char *uri)
 {
-    stream_sys_t *p_sys = s->p_sys;
-
     int id;
     uint64_t bw;
     char *attr;
 
+    assert(*hls == NULL);
+
     attr = parse_Attributes(p_read, "PROGRAM-ID");
     if (attr == NULL)
     {
         msg_Err(s, "#EXT-X-STREAM-INF: expected PROGRAM-ID=<value>");
-        p_sys->b_error = true;
-        return;
+        return VLC_EGENERIC;
     }
     id = atol(attr);
     free(attr);
@@ -489,8 +612,7 @@ static void parse_StreamInformation(stream_t *s, vlc_array_t **hls_stream,
     if (attr == NULL)
     {
         msg_Err(s, "#EXT-X-STREAM-INF: expected BANDWIDTH=<value>");
-        p_sys->b_error = true;
-        return;
+        return VLC_EGENERIC;
     }
     bw = atoll(attr);
     free(attr);
@@ -498,19 +620,18 @@ static void parse_StreamInformation(stream_t *s, vlc_array_t **hls_stream,
     if (bw == 0)
     {
         msg_Err(s, "#EXT-X-STREAM-INF: bandwidth cannot be 0");
-        p_sys->b_error = true;
-        return;
+        return VLC_EGENERIC;
     }
 
     msg_Info(s, "bandwidth adaption detected (program-id=%d, bandwidth=%"PRIu64").", id, bw);
 
     char *psz_uri = relative_URI(s, uri, NULL);
 
-    hls_stream_t *hls = hls_New(*hls_stream, id, bw, psz_uri ? psz_uri : uri);
-    if (hls == NULL)
-        p_sys->b_error = true;
+    *hls = hls_New(*hls_stream, id, bw, psz_uri ? psz_uri : uri);
 
     free(psz_uri);
+
+    return (*hls == NULL) ? VLC_ENOMEM : VLC_SUCCESS;
 }
 
 static int parse_MediaSequence(stream_t *s, hls_stream_t *hls, char *p_read)
@@ -526,7 +647,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;
@@ -639,347 +761,219 @@ static int parse_Discontinuity(stream_t *s, hls_stream_t *hls, char *p_read)
     return VLC_SUCCESS;
 }
 
-static void parse_M3U8ExtLine(stream_t *s, hls_stream_t *hls, char *line)
-{
-    if (*line == '#')
-    {
-        int err = VLC_SUCCESS;
-        if (strncmp(line, "#EXT-X-TARGETDURATION", 21) == 0)
-            err = parse_TargetDuration(s, hls, line);
-        else if (strncmp(line, "#EXT-X-MEDIA-SEQUENCE", 21) == 0)
-            err = parse_MediaSequence(s, hls, line);
-        else if (strncmp(line, "#EXT-X-KEY", 10) == 0)
-            err = parse_Key(s, hls, line);
-        else if (strncmp(line, "#EXT-X-PROGRAM-DATE-TIME", 24) == 0)
-            err = parse_ProgramDateTime(s, hls, line);
-        else if (strncmp(line, "#EXT-X-ALLOW-CACHE", 18) == 0)
-            err = parse_AllowCache(s, hls, line);
-        else if (strncmp(line, "#EXT-X-DISCONTINUITY", 20) == 0)
-            err = parse_Discontinuity(s, hls, line);
-        else if (strncmp(line, "#EXT-X-VERSION", 14) == 0)
-            err = parse_Version(s, hls, line);
-        else if (strncmp(line, "#EXT-X-ENDLIST", 14) == 0)
-            err = parse_EndList(s, hls);
-
-        if (err != VLC_SUCCESS)
-            s->p_sys->b_error = true;
-    }
-}
-
-#define HTTPLIVE_MAX_LINE 4096
-static int get_HTTPLivePlaylist(stream_t *s, hls_stream_t *hls)
+/* The http://tools.ietf.org/html/draft-pantos-http-live-streaming-04#page-8
+ * document defines the following new tags: EXT-X-TARGETDURATION,
+ * EXT-X-MEDIA-SEQUENCE, EXT-X-KEY, EXT-X-PROGRAM-DATE-TIME, EXT-X-
+ * 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, const ssize_t len)
 {
     stream_sys_t *p_sys = s->p_sys;
+    uint8_t *p_read, *p_begin, *p_end;
 
-    /* Download new playlist file from server */
-    if (AccessOpen(s, &hls->url) != VLC_SUCCESS)
-        return VLC_EGENERIC;
+    assert(streams);
+    assert(buffer);
 
-    /* Parse the rest of the reply */
-    uint8_t *tmp = calloc(1, HTTPLIVE_MAX_LINE);
-    if (tmp == NULL)
-    {
-        AccessClose(s);
+    msg_Dbg(s, "parse_M3U8\n%s", buffer);
+    p_begin = buffer;
+    p_end = p_begin + len;
+
+    char *line = ReadLine(p_begin, &p_read, p_end - p_begin);
+    if (line == NULL)
         return VLC_ENOMEM;
-    }
+    p_begin = p_read;
 
-    char *line = AccessReadLine(p_sys->p_access, tmp, HTTPLIVE_MAX_LINE);
     if (strncmp(line, "#EXTM3U", 7) != 0)
     {
-        msg_Err(s, "missing #EXTM3U tag");
-        goto error;
+        msg_Err(s, "missing #EXTM3U tag .. aborting");
+        free(line);
+        return VLC_EGENERIC;
     }
+
     free(line);
     line = NULL;
 
-    for( ; ; )
+    /* What is the version ? */
+    int version = 1;
+    uint8_t *p = (uint8_t *)strstr((const char *)buffer, "#EXT-X-VERSION:");
+    if (p != NULL)
     {
-        line = AccessReadLine(p_sys->p_access, tmp, HTTPLIVE_MAX_LINE);
-        if (line == NULL)
-        {
-            msg_Dbg(s, "end of data");
-            break;
-        }
-
-        if (!vlc_object_alive(s))
-            goto error;
-
-        /* some more checks for actual data */
-        if (strncmp(line, "#EXTINF", 7) == 0)
+        uint8_t *tmp = NULL;
+        char *psz_version = ReadLine(p, &tmp, p_end - p);
+        if (psz_version == NULL)
+            return VLC_ENOMEM;
+        int ret = sscanf((const char*)psz_version, "#EXT-X-VERSION:%d", &version);
+        if (ret != 1)
         {
-            char *uri = AccessReadLine(p_sys->p_access, tmp, HTTPLIVE_MAX_LINE);
-            if (uri == NULL)
-                p_sys->b_error = true;
-            else
-            {
-                parse_SegmentInformation(s, hls, line, uri);
-                free(uri);
-            }
-        }
-        else
-        {
-            parse_M3U8ExtLine(s, hls, line);
+            msg_Warn(s, "#EXT-X-VERSION: no protocol version found, assuming version 1.");
+            version = 1;
         }
-
-        /* Error during m3u8 parsing abort */
-        if (p_sys->b_error)
-            goto error;
-
-        free(line);
+        free(psz_version);
+        p = NULL;
     }
 
-    free(line);
-    free(tmp);
-    AccessClose(s);
-    return VLC_SUCCESS;
-
-error:
-    free(line);
-    free(tmp);
-    AccessClose(s);
-    return VLC_EGENERIC;
-}
-
-static int get_HTTPLiveMetaPlaylist(stream_t *s, vlc_array_t **streams)
-{
-    stream_sys_t *p_sys = s->p_sys;
-    assert(*streams);
+    /* Is it a live stream ? */
+    p_sys->b_live = (strstr((const char *)buffer, "#EXT-X-ENDLIST") == NULL) ? true : false;
 
-    /* Download new playlist file from server */
-    if (AccessOpen(s, &p_sys->m3u8) != VLC_SUCCESS)
-        return VLC_EGENERIC;
+    /* Is it a meta index file ? */
+    bool b_meta = (strstr((const char *)buffer, "#EXT-X-STREAM-INF") == NULL) ? false : true;
 
-    /* Parse the rest of the reply */
-    uint8_t *tmp = calloc(1, HTTPLIVE_MAX_LINE);
-    if (tmp == NULL)
-    {
-        AccessClose(s);
-        return VLC_ENOMEM;
-    }
-
-    char *line = AccessReadLine(p_sys->p_access, tmp, HTTPLIVE_MAX_LINE);
-    if (strncmp(line, "#EXTM3U", 7) != 0)
-    {
-        msg_Err(s, "missing #EXTM3U tag");
-        goto error;
-    }
-    free(line);
-    line = NULL;
+    int err = VLC_SUCCESS;
 
-    for( ; ; )
+    if (b_meta)
     {
-        line = AccessReadLine(p_sys->p_access, tmp, HTTPLIVE_MAX_LINE);
-        if (line == NULL)
-        {
-            msg_Dbg(s, "end of data");
-            break;
-        }
+        msg_Info(s, "Meta playlist");
 
-        if (!vlc_object_alive(s))
-            goto error;
+        /* M3U8 Meta Index file */
+        do {
+            /* Next line */
+            line = ReadLine(p_begin, &p_read, p_end - p_begin);
+            if (line == NULL)
+                break;
+            p_begin = p_read;
 
-        /* some more checks for actual data */
-        if (strncmp(line, "#EXT-X-STREAM-INF", 17) == 0)
-        {
-            p_sys->b_meta = true;
-            char *uri = AccessReadLine(p_sys->p_access, tmp, HTTPLIVE_MAX_LINE);
-            if (uri == NULL)
-                p_sys->b_error = true;
-            else
-            {
-                parse_StreamInformation(s, streams, line, uri);
-                free(uri);
-            }
-        }
-        else if (strncmp(line, "#EXTINF", 7) == 0)
-        {
-            char *uri = AccessReadLine(p_sys->p_access, tmp, HTTPLIVE_MAX_LINE);
-            if (uri == NULL)
-                p_sys->b_error = true;
-            else
+            /* */
+            if (strncmp(line, "#EXT-X-STREAM-INF", 17) == 0)
             {
-                hls_stream_t *hls = hls_GetLast(*streams);
-                if (hls)
-                    parse_SegmentInformation(s, hls, line, uri);
+                p_sys->b_meta = true;
+                char *uri = ReadLine(p_begin, &p_read, p_end - p_begin);
+                if (uri == NULL)
+                    err = VLC_ENOMEM;
                 else
-                    p_sys->b_error = true;
-                free(uri);
-            }
-        }
-        else
-        {
-            hls_stream_t *hls = hls_GetLast(*streams);
-            if ((hls == NULL) && (!p_sys->b_meta))
-            {
-                hls = hls_New(*streams, -1, -1, NULL);
-                if (hls == NULL)
                 {
-                    p_sys->b_error = true;
-                    return VLC_ENOMEM;
+                    hls_stream_t *hls = NULL;
+                    err = parse_StreamInformation(s, &streams, &hls, line, uri);
+                    free(uri);
+
+                    /* Download playlist file from server */
+                    uint8_t *buf = NULL;
+                    ssize_t len = read_M3U8(s, &hls->url, &buf);
+                    if (len < 0)
+                        err = VLC_EGENERIC;
+                    else
+                    {
+                        /* Parse HLS m3u8 content. */
+                        err = parse_M3U8(s, streams, buf, len);
+                        free(buf);
+                    }
+
+                    if (hls)
+                    {
+                        hls->version = version;
+                        if (!p_sys->b_live)
+                            hls->size = hls_GetStreamSize(hls); /* Stream size (approximate) */
+                    }
                 }
+                p_begin = p_read;
             }
-            parse_M3U8ExtLine(s, hls, line);
-        }
 
-        /* Error during m3u8 parsing abort */
-        if (p_sys->b_error)
-            goto error;
+            free(line);
+            line = NULL;
 
-        free(line);
-    }
-
-    free(line);
-    free(tmp);
-    AccessClose(s);
-    return VLC_SUCCESS;
-
-error:
-    free(line);
-    free(tmp);
-    AccessClose(s);
-    return VLC_EGENERIC;
-}
-#undef HTTPLIVE_MAX_LINE
-
-/* The http://tools.ietf.org/html/draft-pantos-http-live-streaming-04#page-8
- * document defines the following new tags: EXT-X-TARGETDURATION,
- * EXT-X-MEDIA-SEQUENCE, EXT-X-KEY, EXT-X-PROGRAM-DATE-TIME, EXT-X-
- * ALLOW-CACHE, EXT-X-STREAM-INF, EXT-X-ENDLIST, EXT-X-DISCONTINUITY,
- * and EXT-X-VERSION.
- */
-static int parse_HTTPLiveStreaming(stream_t *s)
-{
-    stream_sys_t *p_sys = s->p_sys;
-    char *p_read, *p_begin, *p_end;
-
-    assert(p_sys->hls_stream);
-
-    p_begin = p_read = stream_ReadLine(s->p_source);
-    if (!p_begin)
-        return VLC_ENOMEM;
+            if (p_begin >= p_end)
+                break;
 
-    /* */
-    int i_len = strlen(p_begin);
-    p_end = p_read + i_len;
+        } while ((err == VLC_SUCCESS) && vlc_object_alive(s));
 
-    if (strncmp(p_read, "#EXTM3U", 7) != 0)
-    {
-        msg_Err(s, "missing #EXTM3U tag .. aborting");
-        free(p_begin);
-        return VLC_EGENERIC;
     }
+    else
+    {
+        msg_Info(s, "%s Playlist HLS protocol version: %d", p_sys->b_live ? "Live": "VOD", version);
 
-    do {
-        free(p_begin);
-
-        if (p_sys->b_error)
-            return VLC_EGENERIC;
-
-        /* Next line */
-        p_begin = stream_ReadLine(s->p_source);
-        if (p_begin == NULL)
-            break;
-
-        i_len = strlen(p_begin);
-        p_read = p_begin;
-        p_end = p_read + i_len;
-
-        if (strncmp(p_read, "#EXT-X-STREAM-INF", 17) == 0)
-        {
-            p_sys->b_meta = true;
-            char *uri = stream_ReadLine(s->p_source);
-            if (uri == NULL)
-                p_sys->b_error = true;
-            else
-            {
-                parse_StreamInformation(s, &p_sys->hls_stream, p_read, uri);
-                free(uri);
-            }
-        }
-        else if (strncmp(p_read, "#EXTINF", 7) == 0)
-        {
-            char *uri = stream_ReadLine(s->p_source);
-            if (uri == NULL)
-                p_sys->b_error = true;
-            else
-            {
-                hls_stream_t *hls = hls_GetLast(p_sys->hls_stream);
-                if (hls)
-                    parse_SegmentInformation(s, hls, p_read, uri);
-                else
-                    p_sys->b_error = true;
-                free(uri);
-            }
-        }
+        hls_stream_t *hls = NULL;
+        if (p_sys->b_meta)
+            hls = hls_GetLast(streams);
         else
         {
-            hls_stream_t *hls = hls_GetLast(p_sys->hls_stream);
-            if (hls == NULL)
+            /* No Meta playlist used */
+            hls = hls_New(streams, 0, -1, NULL);
+            if (hls)
             {
-                if (!p_sys->b_meta)
+                /* Get TARGET-DURATION first */
+                p = (uint8_t *)strstr((const char *)buffer, "#EXT-X-TARGETDURATION:");
+                if (p)
                 {
-                    hls = hls_New(p_sys->hls_stream, -1, -1, NULL);
-                    if (hls == NULL)
-                    {
-                        p_sys->b_error = true;
-                        return VLC_ENOMEM;
-                    }
+                    uint8_t *p_rest = NULL;
+                    char *psz_duration = ReadLine(p, &p_rest,  p_end - p);
+                    if (psz_duration == NULL)
+                        return VLC_EGENERIC;
+                    err = parse_TargetDuration(s, hls, psz_duration);
+                    free(psz_duration);
+                    p = NULL;
                 }
+
+                /* Store version */
+                hls->version = version;
             }
-            /* Parse M3U8 Ext Line */
-            parse_M3U8ExtLine(s, hls, p_read);
+            else return VLC_ENOMEM;
         }
-    } while(p_read < p_end);
-
-    free(p_begin);
-
-    /* */
-    int count = vlc_array_count(p_sys->hls_stream);
-    for (int n = 0; n < count; n++)
-    {
-        hls_stream_t *hls = hls_Get(p_sys->hls_stream, n);
-        if (hls == NULL) break;
+        assert(hls);
 
-        /* Is it a meta playlist? */
-        if (p_sys->b_meta)
+        /* */
+        int segment_duration = -1;
+        do
         {
-            msg_Dbg(s, "parsing %s", hls->url.psz_path);
-            if (get_HTTPLivePlaylist(s, hls) != VLC_SUCCESS)
+            /* 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)
+                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)
+                err = parse_MediaSequence(s, hls, line);
+            else if (strncmp(line, "#EXT-X-KEY", 10) == 0)
+                err = parse_Key(s, hls, line);
+            else if (strncmp(line, "#EXT-X-PROGRAM-DATE-TIME", 24) == 0)
+                err = parse_ProgramDateTime(s, hls, line);
+            else if (strncmp(line, "#EXT-X-ALLOW-CACHE", 18) == 0)
+                err = parse_AllowCache(s, hls, line);
+            else if (strncmp(line, "#EXT-X-DISCONTINUITY", 20) == 0)
+                err = parse_Discontinuity(s, hls, line);
+            else if (strncmp(line, "#EXT-X-VERSION", 14) == 0)
+                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)
             {
-                msg_Err(s, "could not parse playlist file from meta index." );
-                return VLC_EGENERIC;
+                err = parse_AddSegment(s, hls, segment_duration, line);
+                segment_duration = -1; /* reset duration */
             }
-        }
 
-        vlc_mutex_lock(&hls->lock);
-        if (p_sys->b_live)
-        {
+            free(line);
+            line = NULL;
 
-            /* There should at least be 3 segments of hls->duration */
-            int ok = 0;
-            int num = vlc_array_count(hls->segments);
-            for (int i = 0; i < num; i++)
-            {
-                segment_t *segment = segment_GetSegment(hls, i);
-                if (segment && segment->duration >= hls->duration)
-                    ok++;
-            }
-            if (ok < 3)
-            {
-                msg_Err(s, "cannot start live playback at this time, try again later.");
-                vlc_mutex_unlock(&hls->lock);
-                return VLC_EGENERIC;
-            }
-        }
-        else
-        {
-            /* Stream size (approximate) */
-            hls->size = hls_GetStreamSize(hls);
-        }
-        vlc_mutex_unlock(&hls->lock);
+            if (p_begin >= p_end)
+                break;
+
+        } while ((err == VLC_SUCCESS) && vlc_object_alive(s));
+
+        free(line);
     }
 
-    return VLC_SUCCESS;
+    return err;
+}
+
+static int get_HTTPLiveMetaPlaylist(stream_t *s, vlc_array_t **streams)
+{
+    stream_sys_t *p_sys = s->p_sys;
+    assert(*streams);
+
+    /* Download new playlist file from server */
+    uint8_t *buffer = NULL;
+    ssize_t len = read_M3U8(s, &p_sys->m3u8, &buffer);
+    if (len < 0)
+        return VLC_EGENERIC;
+
+    /* Parse HLS m3u8 content. */
+    int err = parse_M3U8(s, *streams, buffer, len);
+    free(buffer);
+
+    return err;
 }
 
 /* Reload playlist */
@@ -1003,11 +997,18 @@ static int hls_UpdatePlaylist(stream_t *s, hls_stream_t *hls_new, hls_stream_t *
                 (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, "existing segment found with different content - resetting");
                 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_Err(s, "- file: new=%s", p->url.psz_path);
+                msg_Err(s, "        old=%s", segment->url.psz_path);
+
+                /* Resetting content */
+                segment->sequence = p->sequence;
+                segment->duration = p->duration;
+                vlc_UrlClean(&segment->url);
+                vlc_UrlParse(&segment->url, p->url.psz_buffer, 0);
+                segment_Free(p);
             }
         }
         else
@@ -1016,17 +1017,13 @@ 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);
     }
@@ -1046,33 +1043,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;
-
-    int count = vlc_array_count(hls_streams);
 
-    /* Is it a meta playlist? */
-    if (p_sys->b_meta)
+    if (get_HTTPLiveMetaPlaylist(s, &hls_streams) != VLC_SUCCESS)
     {
-        for (int n = 0; n < count; n++)
+        /* Free hls streams */
+        for (int i = 0; i < vlc_array_count(hls_streams); i++)
         {
-            hls_stream_t *hls = hls_Get(hls_streams, n);
-            if (hls == NULL) goto fail;
-
-            msg_Info(s, "parsing %s", hls->url.psz_path);
-            if (get_HTTPLivePlaylist(s, hls) != VLC_SUCCESS)
-            {
-                msg_Err(s, "could not parse playlist file from meta index." );
-                goto fail;
-            }
+            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);
+
+        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)
@@ -1082,16 +1075,12 @@ 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;
 }
 
 /****************************************************************************
@@ -1129,6 +1118,8 @@ static int BandwidthAdaptation(stream_t *s, int progid, uint64_t *bandwidth)
 
 static int Download(stream_t *s, hls_stream_t *hls, segment_t *segment, int *cur_stream)
 {
+    stream_sys_t *p_sys = s->p_sys;
+
     assert(hls);
     assert(segment);
 
@@ -1141,22 +1132,19 @@ static int Download(stream_t *s, hls_stream_t *hls, segment_t *segment, int *cur
     }
 
     /* sanity check - can we download this segment on time? */
-    if (s->p_sys->bandwidth > 0)
+    if ((p_sys->bandwidth > 0) && (hls->bandwidth > 0))
     {
         uint64_t size = (segment->duration * hls->bandwidth); /* bits */
-        int estimated = (int)(size / s->p_sys->bandwidth);
+        int estimated = (int)(size / p_sys->bandwidth);
         if (estimated > segment->duration)
         {
-            msg_Err(s, "cannot quarantee smooth playback");
             msg_Warn(s,"downloading of segment %d takes %ds, which is longer then its playback (%ds)",
                         segment->sequence, estimated, segment->duration);
-            vlc_mutex_unlock(&segment->lock);
-            return VLC_EGENERIC;
         }
     }
 
     mtime_t start = mdate();
-    if (AccessDownload(s, segment) != VLC_SUCCESS)
+    if (hls_Download(s, segment) != VLC_SUCCESS)
     {
         vlc_mutex_unlock(&segment->lock);
         return VLC_EGENERIC;
@@ -1174,8 +1162,8 @@ static int Download(stream_t *s, hls_stream_t *hls, segment_t *segment, int *cur
         return VLC_SUCCESS;
 
     uint64_t bw = ((double)(segment->size * 8) / ms) * 1000; /* bits / s */
-    s->p_sys->bandwidth = bw;
-    if (hls->bandwidth != bw)
+    p_sys->bandwidth = bw;
+    if (p_sys->b_meta && (hls->bandwidth != bw))
     {
         int newstream = BandwidthAdaptation(s, hls->id, &bw);
 
@@ -1190,7 +1178,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;
@@ -1217,7 +1205,7 @@ 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))
+                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;
@@ -1233,32 +1221,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);
@@ -1293,12 +1255,54 @@ 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();
+
+    while (vlc_object_alive(s))
+    {
+        double wait = 1;
+        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 = 3;
+            }
+            else p_sys->playlist.tries = 0;
+
+            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;
     int stream;
 
-    /* Try to pick best matching stream */
+    /* Try to pick best matching stream */;
 again:
     stream = *current;
 
@@ -1351,159 +1355,168 @@ again:
 }
 
 /****************************************************************************
- * Access
+ *
  ****************************************************************************/
-static int AccessOpen(stream_t *s, vlc_url_t *url)
+static int hls_Download(stream_t *s, segment_t *segment)
 {
-    stream_sys_t *p_sys = (stream_sys_t *) s->p_sys;
+    assert(segment);
 
-    if ((url->psz_protocol == NULL) ||
-        (url->psz_path == NULL))
+    /* 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;
 
-    p_sys->p_access = vlc_object_create(s, sizeof(access_t));
-    if (p_sys->p_access == NULL)
-        return VLC_ENOMEM;
+    segment->size = stream_Size(p_ts);
+    assert(segment->size > 0);
 
-    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)
+    segment->data = block_Alloc(segment->size);
+    if (segment->data == NULL)
     {
-        if (asprintf(&p_sys->p_access->psz_location, "%s:%s@%s%s",
-                     url->psz_username, url->psz_password,
-                     url->psz_host, url->psz_path) < 0)
-        {
-            msg_Err(s, "creating http access module");
-            goto fail;
-        }
+        stream_Delete(p_ts);
+        return VLC_ENOMEM;
     }
-    else
+
+    assert(segment->data->i_buffer == segment->size);
+
+    ssize_t length = 0, curlen = 0;
+    uint64_t size;
+    do
     {
-        if (asprintf(&p_sys->p_access->psz_location, "%s%s",
-                     url->psz_host, url->psz_path) < 0)
+        size = stream_Size(p_ts);
+        if (size > segment->size)
         {
-            msg_Err(s, "creating http access module");
-            goto fail;
+            msg_Dbg(s, "size changed %"PRIu64, segment->size);
+            segment->data = block_Realloc(segment->data, 0, size);
+            if (segment->data == NULL)
+            {
+                stream_Delete(p_ts);
+                return VLC_ENOMEM;
+            }
+            segment->size = size;
+            assert(segment->data->i_buffer == segment->size);
         }
-    }
-    vlc_object_attach(p_sys->p_access, s);
-    p_sys->p_access->p_module =
-        module_need(p_sys->p_access, "access", "http", true);
-    if (p_sys->p_access->p_module == NULL)
-    {
-        msg_Err(s, "could not load http access module");
-        goto fail;
-    }
+        length = stream_Read(p_ts, segment->data->p_buffer + curlen, segment->size - curlen);
+        if (length <= 0)
+            break;
+        curlen += length;
+    } while (vlc_object_alive(s));
 
+    stream_Delete(p_ts);
     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)
+/* Read M3U8 file */
+static ssize_t read_M3U8(stream_t *s, vlc_url_t *url, uint8_t **buffer)
 {
-    stream_sys_t *p_sys = (stream_sys_t *) s->p_sys;
+    assert(*buffer == NULL);
 
-    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);
+    /* Construct URL */
+    char *psz_url = ConstructUrl(url);
+    if (psz_url == NULL)
+           return VLC_ENOMEM;
 
-        vlc_object_release(p_sys->p_access);
-        p_sys->p_access = NULL;
-    }
-}
+    stream_t *p_m3u8 = stream_UrlNew(s, psz_url);
+    free(psz_url);
+    if (p_m3u8 == NULL)
+        return VLC_EGENERIC;
 
-static char *AccessReadLine(access_t *p_access, uint8_t *psz_tmp, size_t i_len)
-{
-    char *line = NULL;
-    char *begin = (char *)psz_tmp;
+    int64_t size = stream_Size(p_m3u8);
+    if (size == 0) size = 8192; /* no Content-Length */
 
-    assert(psz_tmp);
+    *buffer = calloc(1, size);
+    if (*buffer == NULL)
+    {
+        stream_Delete(p_m3u8);
+        return VLC_ENOMEM;
+    }
 
-    int skip = strlen(begin);
-    ssize_t len = p_access->pf_read(p_access, psz_tmp + skip, i_len - skip);
-    if (len < 0) return NULL;
-    if ((len == 0) && (skip == 0))
-        return NULL;
+    int64_t len = 0, curlen = 0;
+    do {
+        int read = ((size - curlen) >= INT_MAX) ? INT_MAX : (size - curlen);
+        len = stream_Read(p_m3u8, *buffer + curlen, read);
+        if (len <= 0)
+            break;
+        curlen += len;
+        if (curlen >= size)
+        {
+            uint8_t *tmp = realloc(*buffer, size + 8192);
+            if (tmp == NULL)
+                break;
+            size += 8192;
+            *buffer = tmp;
+        }
+    } while (vlc_object_alive(s));
+    stream_Delete(p_m3u8);
 
-    char *p = begin;
-    char *end = p + len + skip;
+    (*buffer)[curlen-1] = '\0';
+    return size;
+}
 
-    while (p < end)
-    {
-        if (*p == '\n')
-            break;
+static ssize_t ReadM3U8(stream_t *s, uint8_t **buffer)
+{
+    assert(*buffer == NULL);
 
-        p++;
-    }
+    int64_t size = stream_Size(s->p_source);
+    if (size == 0) size = 1024; /* no Content-Length */
 
-    /* copy line excluding \n */
-    line = strndup(begin, p - begin);
+    *buffer = calloc(1, size);
+    if (*buffer == NULL)
+        return VLC_ENOMEM;
 
-    p++;
-    if (p < end)
-    {
-        psz_tmp = memmove(begin, p, end - p);
-        psz_tmp[end - p] = '\0';
-    }
-    else memset(psz_tmp, 0, i_len);
+    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));
 
-    return line;
+    return size;
 }
 
-static int AccessDownload(stream_t *s, segment_t *segment)
+static char *ReadLine(uint8_t *buffer, uint8_t **pos, const size_t len)
 {
-    stream_sys_t *p_sys = (stream_sys_t *) s->p_sys;
+    assert(buffer);
 
-    assert(segment);
-
-    /* Download new playlist file from server */
-    if (AccessOpen(s, &segment->url) != VLC_SUCCESS)
-        return VLC_EGENERIC;
-
-    segment->size = p_sys->p_access->info.i_size;
-    assert(segment->size > 0);
+    char *line = NULL;
+    uint8_t *begin = buffer;
+    uint8_t *p = begin;
+    uint8_t *end = p + len;
 
-    segment->data = block_Alloc(segment->size);
-    if (segment->data == NULL)
+    while (p < end)
     {
-        AccessClose(s);
-        return VLC_ENOMEM;
+        if ((*p == '\n') || (*p == '\0'))
+            break;
+        p++;
     }
 
-    assert(segment->data->i_buffer == segment->size);
+    /* copy line excluding \n or \0 */
+    line = strndup((char *)begin, p - begin);
 
-    ssize_t length = 0, curlen = 0;
-    do
+    if (*p == '\0')
+        *pos = end;
+    else
     {
-        if (p_sys->p_access->info.i_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)
-            {
-                AccessClose(s);
-                return VLC_ENOMEM;
-            }
-            segment->size = p_sys->p_access->info.i_size;
-            assert(segment->data->i_buffer == segment->size);
-        }
-        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))
-            break;
-        curlen += length;
-    } while (vlc_object_alive(s));
+        /* next pass start after \n */
+        p++;
+        *pos = p;
+    }
 
-    AccessClose(s);
-    return VLC_SUCCESS;
+    return line;
 }
 
 /****************************************************************************
@@ -1541,6 +1554,7 @@ static int Open(vlc_object_t *p_this)
     p_sys->hls_stream = vlc_array_new();
     if (p_sys->hls_stream == NULL)
     {
+        vlc_UrlClean(&p_sys->m3u8);
         free(p_sys);
         return VLC_ENOMEM;
     }
@@ -1550,23 +1564,41 @@ static int Open(vlc_object_t *p_this)
     s->pf_peek = Peek;
     s->pf_control = Control;
 
-    /* Select first segment to play */
-    if (parse_HTTPLiveStreaming(s) != VLC_SUCCESS)
+    /* Parse HLS m3u8 content. */
+    uint8_t *buffer = NULL;
+    ssize_t len = ReadM3U8(s, &buffer);
+    if (len < 0)
+        goto fail;
+    if (parse_M3U8(s, p_sys->hls_stream, buffer, len) != VLC_SUCCESS)
     {
+        free(buffer);
         goto fail;
     }
+    free(buffer);
 
     /* Choose first HLS stream to start with */
     int current = p_sys->playback.stream = 0;
-    p_sys->playback.segment = p_sys->download.segment =
-            p_sys->b_live ? live_ChooseSegment(s, current) : 0;
+    p_sys->playback.segment = p_sys->download.segment = ChooseSegment(s, current);
+
+    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");
+    }
 
     if (Prefetch(s, &current) != VLC_SUCCESS)
     {
-        msg_Err(s, "fetching first segment.");
+        msg_Err(s, "fetching first segment failed.");
         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)
     {
@@ -1574,25 +1606,39 @@ 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))
     {
-        goto fail;
+        if (p_sys->b_live)
+            vlc_join(p_sys->reload, NULL);
+        goto fail_thread;
     }
 
     return VLC_SUCCESS;
 
+fail_thread:
+    vlc_mutex_destroy(&p_sys->download.lock_wait);
+    vlc_cond_destroy(&p_sys->download.wait);
+
 fail:
-    Close(p_this);
+    /* Free hls streams */
+    for (int i = 0; i < vlc_array_count(p_sys->hls_stream); i++)
+    {
+        hls_stream_t *hls;
+        hls = (hls_stream_t *)vlc_array_item_at_index(p_sys->hls_stream, i);
+        if (hls) hls_Free(hls);
+    }
+    vlc_array_destroy(p_sys->hls_stream);
+
+    /* */
+    vlc_UrlClean(&p_sys->m3u8);
+    free(p_sys);
     return VLC_EGENERIC;
 }
 
@@ -1612,7 +1658,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);
 
@@ -1895,7 +1943,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;