]> git.sesse.net Git - vlc/blobdiff - modules/stream_filter/httplive.c
demux: mp4: don't NULL dereference on failed realloc
[vlc] / modules / stream_filter / httplive.c
index e2a8834e88c20cfd3ce530cfb572307f5ad9ad0e..fa4201b4968a97fc29b8832ca4ecd679f4b36b38 100644 (file)
@@ -1,24 +1,24 @@
 /*****************************************************************************
  * httplive.c: HTTP Live Streaming stream filter
  *****************************************************************************
- * Copyright (C) 2010-2011 M2X BV
+ * Copyright (C) 2010-2012 M2X BV
  * $Id$
  *
  * Author: Jean-Paul Saman <jpsaman _AT_ videolan _DOT_ org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
 #endif
 
 #include <limits.h>
-#include <errno.h>
 
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 
 #include <assert.h>
+#include <errno.h>
 #include <gcrypt.h>
 
 #include <vlc_threads.h>
 #include <vlc_arrays.h>
 #include <vlc_stream.h>
-#include <vlc_url.h>
 #include <vlc_memory.h>
 #include <vlc_gcrypt.h>
 
@@ -69,10 +68,12 @@ typedef struct segment_s
     uint64_t    size;       /* segment size in bytes */
     uint64_t    bandwidth;  /* bandwidth usage of segments (bits per second)*/
 
-    vlc_url_t   url;
+    char        *url;
     char       *psz_key_path;         /* url key path */
-    uint8_t     psz_AES_key[16];      /* AES-128 */
+    uint8_t     aes_key[16];      /* AES-128 */
     bool        b_key_loaded;
+    uint8_t     psz_AES_IV[AES_BLOCK_SIZE];    /* IV used when decypher the block */
+    bool        b_iv_loaded;
 
     vlc_mutex_t lock;
     block_t     *data;      /* data */
@@ -84,23 +85,24 @@ typedef struct hls_stream_s
     int         version;    /* protocol version should be 1 */
     int         sequence;   /* media sequence number */
     int         duration;   /* maximum duration per segment (s) */
+    int         max_segment_length;   /* maximum duration segments */
     uint64_t    bandwidth;  /* bandwidth usage of segments (bits per second)*/
     uint64_t    size;       /* stream length is calculated by taking the sum
                                foreach segment of (segment->duration * hls->bandwidth/8) */
 
     vlc_array_t *segments;  /* list of segments */
-    vlc_url_t   url;        /* uri to m3u8 */
+    char        *url;        /* uri to m3u8 */
     vlc_mutex_t lock;
     bool        b_cache;    /* allow caching */
 
     char        *psz_current_key_path;          /* URL path of the encrypted key */
-    uint8_t      psz_AES_IV[AES_BLOCK_SIZE];    /* IV used when decypher the block */
-    bool         b_iv_loaded;
+    uint8_t      psz_current_AES_IV[AES_BLOCK_SIZE];    /* IV used when decypher the block */
+    bool         b_current_iv_loaded;
 } hls_stream_t;
 
 struct stream_sys_t
 {
-    vlc_url_t     m3u8;         /* M3U8 url */
+    char         *m3u8;         /* M3U8 url */
     vlc_thread_t  reload;       /* HLS m3u8 reload thread */
     vlc_thread_t  thread;       /* HLS segment download thread */
 
@@ -136,12 +138,23 @@ struct stream_sys_t
         int         tries;      /* times it was not changed */
     } playlist;
 
+    struct hls_read_s
+    {
+        vlc_mutex_t lock_wait;  /* used by read condition variable */
+        vlc_cond_t  wait;       /* some condition to wait on during read */
+    } read;
+
     /* state */
     bool        b_cache;    /* can cache files */
     bool        b_meta;     /* meta playlist */
     bool        b_live;     /* live stream? or vod? */
     bool        b_error;    /* parsing error */
     bool        b_aesmsg;   /* only print one time that the media is encrypted */
+
+    /* Shared data */
+    vlc_cond_t   wait;
+    vlc_mutex_t  lock;
+    bool         paused;
 };
 
 /****************************************************************************
@@ -152,7 +165,7 @@ 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 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 ssize_t read_M3U8_from_url(stream_t *s, const char *psz_url, uint8_t **buffer);
 static char *ReadLine(uint8_t *buffer, uint8_t **pos, size_t len);
 
 static int hls_Download(stream_t *s, segment_t *segment);
@@ -163,50 +176,60 @@ 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);
-
 /****************************************************************************
  *
  ****************************************************************************/
-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;
+    const uint8_t *peek;
 
-    int64_t i_size = stream_Peek(s->p_source, &peek, 46);
-    if (i_size < 1)
+    int size = stream_Peek(s->p_source, &peek, 46);
+    if (size < 7)
         return false;
 
-    if (strncasecmp((const char*)peek, "#EXTM3U", 7) != 0)
+    if (memcmp(peek, "#EXTM3U", 7) != 0)
         return false;
 
+    peek += 7;
+    size -= 7;
+
     /* Parse stream and search for
      * EXT-X-TARGETDURATION or EXT-X-STREAM-INF tag, see
      * http://tools.ietf.org/html/draft-pantos-http-live-streaming-04#page-8 */
-    peek_end = peek + i_size;
-    while(peek <= peek_end)
-    {
-        if (*peek == '#')
+    while (size--)
+    {
+        static const char *const ext[] = {
+            "TARGETDURATION",
+            "MEDIA-SEQUENCE",
+            "KEY",
+            "ALLOW-CACHE",
+            "ENDLIST",
+            "STREAM-INF",
+            "DISCONTINUITY",
+            "VERSION"
+        };
+
+        if (*peek++ != '#')
+            continue;
+
+        if (size < 6)
+            continue;
+
+        if (memcmp(peek, "EXT-X-", 6))
+            continue;
+
+        peek += 6;
+        size -= 6;
+
+        for (size_t i = 0; i < ARRAY_SIZE(ext); i++)
         {
-            for (unsigned int i = 0; i < ARRAY_SIZE(ext); i++)
-            {
-                char *p = strstr((const char*)peek, ext[i]);
-                if (p != NULL)
-                    return true;
-            }
+            size_t len = strlen(ext[i]);
+            if (size < 0 || (size_t)size < len)
+                continue;
+            if (!memcmp(peek, ext[i], len))
+                return true;
         }
-        peek++;
-    };
+    }
 
     return false;
 }
@@ -220,11 +243,17 @@ static hls_stream_t *hls_New(vlc_array_t *hls_stream, const int id, const uint64
     hls->id = id;
     hls->bandwidth = bw;
     hls->duration = -1;/* unknown */
+    hls->max_segment_length = -1;/* unknown */
     hls->size = 0;
     hls->sequence = 0; /* default is 0 */
     hls->version = 1;  /* default protocol version */
     hls->b_cache = true;
-    vlc_UrlParse(&hls->url, uri, 0);
+    hls->url = strdup(uri);
+    if (hls->url == NULL)
+    {
+        free(hls);
+        return NULL;
+    }
     hls->psz_current_key_path = NULL;
     hls->segments = vlc_array_new();
     vlc_array_append(hls_stream, hls);
@@ -245,7 +274,7 @@ static void hls_Free(hls_stream_t *hls)
         }
         vlc_array_destroy(hls->segments);
     }
-    vlc_UrlClean(&hls->url);
+    free(hls->url);
     free(hls->psz_current_key_path);
     free(hls);
 }
@@ -267,13 +296,14 @@ static hls_stream_t *hls_Copy(hls_stream_t *src, const bool b_cp_segments)
     dst->b_cache = src->b_cache;
     dst->psz_current_key_path = src->psz_current_key_path ?
                 strdup( src->psz_current_key_path ) : NULL;
-    char *uri = ConstructUrl(&src->url);
-    if (uri == NULL)
+    memcpy(dst->psz_current_AES_IV, src->psz_current_AES_IV, AES_BLOCK_SIZE);
+    dst->b_current_iv_loaded = src->b_current_iv_loaded;
+    dst->url = strdup(src->url);
+    if (dst->url == 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);
@@ -314,7 +344,7 @@ static hls_stream_t *hls_Find(vlc_array_t *hls_stream, hls_stream_t *hls_new)
         {
             /* compare */
             if ((hls->id == hls_new->id) &&
-                (hls->bandwidth == hls_new->bandwidth))
+                ((hls->bandwidth == hls_new->bandwidth)||(hls_new->bandwidth==0)))
                 return hls;
         }
     }
@@ -357,7 +387,12 @@ static segment_t *segment_New(hls_stream_t* hls, const int duration, const char
     segment->size = 0; /* bytes */
     segment->sequence = 0;
     segment->bandwidth = 0;
-    vlc_UrlParse(&segment->url, uri, 0);
+    segment->url = strdup(uri);
+    if (segment->url == NULL)
+    {
+        free(segment);
+        return NULL;
+    }
     segment->data = NULL;
     vlc_array_append(hls->segments, segment);
     vlc_mutex_init(&segment->lock);
@@ -365,6 +400,8 @@ static segment_t *segment_New(hls_stream_t* hls, const int duration, const char
     segment->psz_key_path = NULL;
     if (hls->psz_current_key_path)
         segment->psz_key_path = strdup(hls->psz_current_key_path);
+    segment->b_iv_loaded = hls->b_current_iv_loaded;
+    memcpy(segment->psz_AES_IV, hls->psz_current_AES_IV, AES_BLOCK_SIZE);
     return segment;
 }
 
@@ -372,7 +409,7 @@ static void segment_Free(segment_t *segment)
 {
     vlc_mutex_destroy(&segment->lock);
 
-    vlc_UrlClean(&segment->url);
+    free(segment->url);
     free(segment->psz_key_path);
     if (segment->data)
         block_Release(segment->data);
@@ -420,9 +457,10 @@ static int ChooseSegment(stream_t *s, const int current)
     int duration = 0;
     int sequence = 0;
     int count = vlc_array_count(hls->segments);
-    int i = p_sys->b_live ? count - 1 : 0;
+    int i = p_sys->b_live ? count - 1 : -1;
 
-    while((i >= 0) && (i < count) && vlc_object_alive(s))
+    /* We do while loop only with live case, otherwise return 0*/
+    while((i >= 0) && (i < count))
     {
         segment_t *segment = segment_GetSegment(hls, i);
         assert(segment);
@@ -437,18 +475,15 @@ static int ChooseSegment(stream_t *s, const int current)
         if (duration >= 3 * hls->duration)
         {
             /* Start point found */
-            wanted = p_sys->b_live ? i : 0;
+            wanted = i;
             sequence = segment->sequence;
             break;
         }
 
-        if (p_sys->b_live)
-            i-- ;
-        else
-          i++;
+        i-- ;
     }
 
-    msg_Info(s, "Choose segment %d/%d (sequence=%d)", wanted, count, sequence);
+    msg_Dbg(s, "Choose segment %d/%d (sequence=%d)", wanted, count, sequence);
     return wanted;
 }
 
@@ -466,11 +501,24 @@ static char *parse_Attributes(const char *line, const char *attr)
     begin = p;
     do
     {
-        if (strncasecmp(begin, attr, strlen(attr)) == 0)
+        if (strncasecmp(begin, attr, strlen(attr)) == 0
+          && begin[strlen(attr)] == '=')
         {
-            /* <attr>=<value>[,]* */
+            /* <attr>="<value>"[,]* */
             p = strchr(begin, ',');
             begin += strlen(attr) + 1;
+
+            /* Check if we have " " marked value*/
+            if( begin[0] == '"' )
+            {
+                char *valueend = strchr( begin+1, '"');
+
+                /* No ending " so bail out */
+                if( unlikely( !valueend ) )
+                    return NULL;
+
+                p = strchr( valueend, ',');
+            }
             if (begin >= end)
                 return NULL;
             if (p == NULL) /* last attribute */
@@ -484,156 +532,91 @@ static char *parse_Attributes(const char *line, const char *attr)
     return NULL;
 }
 
-static int hex2int(char c)
-{
-    if (c >= '0' && c <= '9')
-        return c - '0';
-    if (c >= 'A' && c <= 'F')
-        return c - 'A' + 10;
-    if (c >= 'a' && c <= 'f')
-        return c - 'a' + 10;
-    return -1;
-}
-
-static int string_to_IV(const char *string_hexa, uint8_t iv[AES_BLOCK_SIZE])
+static int string_to_IV(char *string_hexa, uint8_t iv[AES_BLOCK_SIZE])
 {
-    const char *p = string_hexa;
-    uint8_t *d = iv;
-    unsigned int c;
-
-    if (*p++ != '0')
+    unsigned long long iv_hi, iv_lo;
+    char *end = NULL;
+    if (*string_hexa++ != '0')
         return VLC_EGENERIC;
-    if (*p++ != 'x')
+    if (*string_hexa != 'x' && *string_hexa != 'X')
         return VLC_EGENERIC;
 
-    while (*p && *(p+1))
-    {
-        c = hex2int(*p++) << 4;
-        c |= hex2int(*p++);
-        *d++ = c;
+    string_hexa++;
+
+    size_t len = strlen(string_hexa);
+    if (len <= 16) {
+        iv_hi = 0;
+        iv_lo = strtoull(string_hexa, &end, 16);
+        if (*end)
+            return VLC_EGENERIC;
+    } else {
+        iv_lo = strtoull(&string_hexa[len-16], &end, 16);
+        if (*end)
+            return VLC_EGENERIC;
+        string_hexa[len-16] = '\0';
+        iv_hi = strtoull(string_hexa, &end, 16);
+        if (*end)
+            return VLC_EGENERIC;
+    }
+
+    for (int i = 7; i >= 0 ; --i) {
+        iv[  i] = iv_hi & 0xff;
+        iv[8+i] = iv_lo & 0xff;
+        iv_hi >>= 8;
+        iv_lo >>= 8;
     }
 
     return VLC_SUCCESS;
 }
 
-static char *relative_URI(stream_t *s, const char *uri, const vlc_url_t *url)
+static char *relative_URI(const char *psz_url, const char *psz_path)
 {
-    stream_sys_t *p_sys = s->p_sys;
-    char *psz_password = NULL;
-    char *psz_username = NULL;
-    char *psz_protocol = NULL;
-    char *psz_path = NULL;
-    char *psz_host = NULL;
-    char *psz_uri = NULL;
-    int  i_port = -1;
+    char *ret = NULL;
+    const char *fmt;
+    assert(psz_url != NULL && psz_path != NULL);
 
-    char *p = strchr(uri, ':');
-    if (p != NULL)
-        return NULL;
-
-    /* Determine protocol to use */
-    if (url && url->psz_protocol)
-    {
-        psz_protocol = strdup(url->psz_protocol);
-        i_port = url->i_port;
-    }
-    else if (p_sys->m3u8.psz_protocol)
-    {
-        psz_protocol = strdup(p_sys->m3u8.psz_protocol);
-        i_port = p_sys->m3u8.i_port;
-    }
-
-    /* Determine host to use */
-    if (url && url->psz_host)
-        psz_host = strdup(url->psz_host);
-    else if (p_sys->m3u8.psz_host)
-        psz_host = strdup(p_sys->m3u8.psz_host);
-
-    /* Determine path to use */
-    if (url && url->psz_path != NULL)
-        psz_path = strdup(url->psz_path);
-    else if (p_sys->m3u8.psz_path != NULL)
-        psz_path = strdup(p_sys->m3u8.psz_path);
 
-    if ((psz_protocol == NULL) ||
-        (psz_path == NULL) ||
-        (psz_host == NULL))
-        goto fail;
-
-    p = strrchr(psz_path, '/');
-    if (p) *p = '\0';
-
-    /* Determine credentials to use */
-    if (url && url->psz_username)
-        psz_username = strdup(url->psz_username);
-    else if (p_sys->m3u8.psz_username)
-        psz_username = strdup(p_sys->m3u8.psz_username);
-
-    if (url && url->psz_password)
-        psz_password = strdup(url->psz_password);
-    else if (p_sys->m3u8.psz_password)
-        psz_password = strdup(p_sys->m3u8.psz_password);
-
-    /* */
-    if (psz_password || psz_username)
-    {
-        if (asprintf(&psz_uri, "%s://%s:%s@%s:%d%s/%s",
-                     psz_protocol,
-                     psz_username ? psz_username : "",
-                     psz_password ? psz_password : "",
-                     psz_host, i_port,
-                     psz_path, uri) < 0)
-            goto fail;
-    }
-    else
-    {
-        if (asprintf(&psz_uri, "%s://%s:%d%s/%s",
-                     psz_protocol, psz_host, i_port,
-                     psz_path, uri) < 0)
-           goto fail;
-    }
+    //If the path is actually an absolute URL, don't do anything.
+    if (strncmp(psz_path, "http", 4) == 0)
+        return NULL;
 
-fail:
-    free(psz_password);
-    free(psz_username);
-    free(psz_protocol);
-    free(psz_path);
-    free(psz_host);
-    return psz_uri;
-}
+    size_t len = strlen(psz_path);
 
-static char *ConstructUrl(vlc_url_t *url)
-{
-    if ((url->psz_protocol == NULL) ||
-        (url->psz_path == NULL))
+    char *new_url = strdup(psz_url);
+    if (unlikely(!new_url))
         return NULL;
 
-    if (url->i_port <= 0)
-    {
-        if (strncmp(url->psz_protocol, "https", 5) == 0)
-            url->i_port = 443;
-        else
-            url->i_port = 80;
+    if( psz_path[0] == '/' ) //Relative URL with absolute path
+    {
+        //Try to find separator for name and path, try to skip
+        //access and first ://
+        char *slash = strchr(&new_url[8], '/');
+        if (unlikely(slash == NULL))
+            goto end;
+        *slash = '\0';
+        fmt = "%s%s";
+    } else {
+        int levels = 0;
+        while(len >= 3 && !strncmp(psz_path, "../", 3)) {
+            psz_path += 3;
+            len -= 3;
+            levels++;
+        }
+        do {
+            char *slash = strrchr(new_url, '/');
+            if (unlikely(slash == NULL))
+                goto end;
+            *slash = '\0';
+        } while (levels--);
+       fmt = "%s/%s";
     }
 
-    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;
-    }
+    if (asprintf(&ret, fmt, new_url, psz_path) < 0)
+        ret = NULL;
 
-    return psz_url;
+end:
+    free(new_url);
+    return ret;
 }
 
 static int parse_SegmentInformation(hls_stream_t *hls, char *p_read, int *duration)
@@ -653,20 +636,23 @@ static int parse_SegmentInformation(hls_stream_t *hls, char *p_read, int *durati
         return VLC_EGENERIC;
 
     int value;
+    char *endptr;
     if (hls->version < 3)
     {
-       value = strtol(token, NULL, 10);
-       if (errno == ERANGE)
-       {
-           *duration = -1;
-           return VLC_EGENERIC;
-       }
-       *duration = value;
+        errno = 0;
+        value = strtol(token, &endptr, 10);
+        if (token == endptr || errno == ERANGE)
+        {
+            *duration = -1;
+            return VLC_EGENERIC;
+        }
+        *duration = value;
     }
     else
     {
-        double d = strtod(token, (char **) NULL);
-        if (errno == ERANGE)
+        errno = 0;
+        double d = strtof(token, &endptr);
+        if (token == endptr || errno == ERANGE)
         {
             *duration = -1;
             return VLC_EGENERIC;
@@ -675,14 +661,16 @@ static int parse_SegmentInformation(hls_stream_t *hls, char *p_read, int *durati
             value = ((int)d) + 1;
         else
             value = ((int)d);
+        *duration = value;
     }
+    if( *duration > hls->max_segment_length)
+        hls->max_segment_length = *duration;
 
     /* 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)
+static int parse_AddSegment(hls_stream_t *hls, const int duration, const char *uri)
 {
     assert(hls);
     assert(uri);
@@ -690,7 +678,7 @@ static int parse_AddSegment(stream_t *s, hls_stream_t *hls, const int duration,
     /* Store segment information */
     vlc_mutex_lock(&hls->lock);
 
-    char *psz_uri = relative_URI(s, uri, &hls->url);
+    char *psz_uri = relative_URI(hls->url, uri);
 
     segment_t *segment = segment_New(hls, duration, psz_uri ? psz_uri : uri);
     if (segment)
@@ -728,13 +716,13 @@ static int parse_StreamInformation(stream_t *s, vlc_array_t **hls_stream,
     assert(*hls == NULL);
 
     attr = parse_Attributes(p_read, "PROGRAM-ID");
-    if (attr == NULL)
+    if (attr)
     {
-        msg_Err(s, "#EXT-X-STREAM-INF: expected PROGRAM-ID=<value>");
-        return VLC_EGENERIC;
+        id = atol(attr);
+        free(attr);
     }
-    id = atol(attr);
-    free(attr);
+    else
+        id = 0;
 
     attr = parse_Attributes(p_read, "BANDWIDTH");
     if (attr == NULL)
@@ -751,9 +739,9 @@ static int parse_StreamInformation(stream_t *s, vlc_array_t **hls_stream,
         return VLC_EGENERIC;
     }
 
-    msg_Info(s, "bandwidth adaptation detected (program-id=%d, bandwidth=%"PRIu64").", id, bw);
+    msg_Dbg(s, "bandwidth adaptation detected (program-id=%d, bandwidth=%"PRIu64").", id, bw);
 
-    char *psz_uri = relative_URI(s, uri, NULL);
+    char *psz_uri = relative_URI(s->p_sys->m3u8, uri);
 
     *hls = hls_New(*hls_stream, id, bw, psz_uri ? psz_uri : uri);
 
@@ -779,9 +767,11 @@ static int parse_MediaSequence(stream_t *s, hls_stream_t *hls, char *p_read)
         if (s->p_sys->b_live)
         {
             hls_stream_t *last = hls_GetLast(s->p_sys->hls_stream);
-            if ((last->sequence < sequence) && (sequence - last->sequence != 1))
+            segment_t *last_segment = segment_GetSegment( last, vlc_array_count( last->segments ) - 1 );
+            if ( ( last_segment->sequence < sequence) &&
+                 ( sequence - last_segment->sequence >= 1 ))
                 msg_Err(s, "EXT-X-MEDIA-SEQUENCE gap in playlist (new=%d, old=%d)",
-                            sequence, last->sequence);
+                            sequence, last_segment->sequence);
         }
         else
             msg_Err(s, "EXT-X-MEDIA-SEQUENCE already present in playlist (new=%d, old=%d)",
@@ -830,7 +820,7 @@ static int parse_Key(stream_t *s, hls_stream_t *hls, char *p_read)
         char *value, *uri, *iv;
         if (s->p_sys->b_aesmsg == false)
         {
-            msg_Info(s, "playback of AES-128 encrypted HTTP Live media detected.");
+            msg_Dbg(s, "playback of AES-128 encrypted HTTP Live media detected.");
             s->p_sys->b_aesmsg = true;
         }
         value = uri = parse_Attributes(p_read, "URI");
@@ -850,7 +840,13 @@ static int parse_Key(stream_t *s, hls_stream_t *hls, char *p_read)
             if (end != NULL)
                 *end = 0;
         }
-        hls->psz_current_key_path = strdup(uri);
+        /* For absolute URI, just duplicate it
+         * don't limit to HTTP, maybe some sanity checking
+         * should be done more in here? */
+        if( strstr( uri , "://" ) )
+            hls->psz_current_key_path = strdup( uri );
+        else
+            hls->psz_current_key_path = relative_URI(hls->url, uri);
         free(value);
 
         value = iv = parse_Attributes(p_read, "IV");
@@ -863,7 +859,7 @@ static int parse_Key(stream_t *s, hls_stream_t *hls, char *p_read)
             * representation of the sequence number SHALL be placed in a 16-octet
             * buffer and padded (on the left) with zeros.
             */
-            hls->b_iv_loaded = false;
+            hls->b_current_iv_loaded = false;
         }
         else
         {
@@ -874,10 +870,13 @@ static int parse_Key(stream_t *s, hls_stream_t *hls, char *p_read)
             * and MUST be prefixed with 0x or 0X.
             */
 
-            if (string_to_IV(iv, hls->psz_AES_IV) == VLC_EGENERIC)
+            if (string_to_IV(iv, hls->psz_current_AES_IV) == VLC_EGENERIC)
+            {
+                msg_Err(s, "IV invalid");
                 err = VLC_EGENERIC;
+            }
             else
-                hls->b_iv_loaded = true;
+                hls->b_current_iv_loaded = true;
             free(value);
         }
     }
@@ -940,7 +939,7 @@ static int parse_EndList(stream_t *s, hls_stream_t *hls)
     assert(hls);
 
     s->p_sys->b_live = false;
-    msg_Info(s, "video on demand (vod) mode");
+    msg_Dbg(s, "video on demand (vod) mode");
     return VLC_SUCCESS;
 }
 
@@ -953,6 +952,14 @@ static int parse_Discontinuity(stream_t *s, hls_stream_t *hls, char *p_read)
     return VLC_SUCCESS;
 }
 
+static int hls_CompareStreams( const void* a, const void* b )
+{
+    hls_stream_t*   stream_a = *(hls_stream_t**)a;
+    hls_stream_t*   stream_b = *(hls_stream_t**)b;
+
+    return stream_a->bandwidth - stream_b->bandwidth;
+}
+
 /* 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-
@@ -1015,7 +1022,7 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, const
 
     if (b_meta)
     {
-        msg_Info(s, "Meta playlist");
+        msg_Dbg(s, "Meta playlist");
 
         /* M3U8 Meta Index file */
         do {
@@ -1036,29 +1043,42 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, const
                 {
                     if (*uri == '#')
                     {
-                        msg_Info(s, "Skipping invalid stream-inf: %s", uri);
+                        msg_Warn(s, "Skipping invalid stream-inf: %s", uri);
                         free(uri);
                     }
                     else
                     {
+                        bool new_stream_added = false;
                         hls_stream_t *hls = NULL;
                         err = parse_StreamInformation(s, &streams, &hls, line, uri);
-                        free(uri);
+                        if (err == VLC_SUCCESS)
+                            new_stream_added = true;
 
-                        /* 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);
-                        }
+                        free(uri);
 
                         if (hls)
                         {
+                            /* Download playlist file from server */
+                            uint8_t *buf = NULL;
+                            ssize_t len = read_M3U8_from_url(s, hls->url, &buf);
+                            if (len < 0)
+                            {
+                                msg_Warn(s, "failed to read %s, continue for other streams", hls->url);
+
+                                /* remove stream just added */
+                                if (new_stream_added)
+                                    vlc_array_remove(streams, vlc_array_count(streams) - 1);
+
+                                /* ignore download error, so we have chance to try other streams */
+                                err = VLC_SUCCESS;
+                            }
+                            else
+                            {
+                                /* Parse HLS m3u8 content. */
+                                err = parse_M3U8(s, streams, buf, len);
+                                free(buf);
+                            }
+
                             hls->version = version;
                             if (!p_sys->b_live)
                                 hls->size = hls_GetStreamSize(hls); /* Stream size (approximate) */
@@ -1074,12 +1094,19 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, const
             if (p_begin >= p_end)
                 break;
 
-        } while ((err == VLC_SUCCESS) && vlc_object_alive(s));
+        } while (err == VLC_SUCCESS);
 
+        size_t stream_count = vlc_array_count(streams);
+        msg_Dbg(s, "%d streams loaded in Meta playlist", (int)stream_count);
+        if (stream_count == 0)
+        {
+            msg_Err(s, "No playable streams found in Meta playlist");
+            err = VLC_EGENERIC;
+        }
     }
     else
     {
-        msg_Info(s, "%s Playlist HLS protocol version: %d", p_sys->b_live ? "Live": "VOD", version);
+        msg_Dbg(s, "%s Playlist HLS protocol version: %d", p_sys->b_live ? "Live": "VOD", version);
 
         hls_stream_t *hls = NULL;
         if (p_sys->b_meta)
@@ -1087,7 +1114,7 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, const
         else
         {
             /* No Meta playlist used */
-            hls = hls_New(streams, 0, 0, NULL);
+            hls = hls_New(streams, 0, 0, p_sys->m3u8);
             if (hls)
             {
                 /* Get TARGET-DURATION first */
@@ -1111,6 +1138,7 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, const
         assert(hls);
 
         /* */
+        bool media_sequence_loaded = false;
         int segment_duration = -1;
         do
         {
@@ -1125,7 +1153,15 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, const
             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);
+            {
+                /* A Playlist file MUST NOT contain more than one EXT-X-MEDIA-SEQUENCE tag. */
+                /* We only care about first one */
+                if (!media_sequence_loaded)
+                {
+                    err = parse_MediaSequence(s, hls, line);
+                    media_sequence_loaded = true;
+                }
+            }
             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)
@@ -1140,7 +1176,7 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, const
                 err = parse_EndList(s, hls);
             else if ((strncmp(line, "#", 1) != 0) && (*line != '\0') )
             {
-                err = parse_AddSegment(s, hls, segment_duration, line);
+                err = parse_AddSegment(hls, segment_duration, line);
                 segment_duration = -1; /* reset duration */
             }
 
@@ -1150,7 +1186,7 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, const
             if (p_begin >= p_end)
                 break;
 
-        } while ((err == VLC_SUCCESS) && vlc_object_alive(s));
+        } while (err == VLC_SUCCESS);
 
         free(line);
     }
@@ -1161,9 +1197,6 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, const
 
 static int hls_DownloadSegmentKey(stream_t *s, segment_t *seg)
 {
-    uint8_t         aeskey[32]; /* AES-512 can use up to 32 bytes */
-    ssize_t len;
-
     stream_t *p_m3u8 = stream_UrlNew(s, seg->psz_key_path);
     if (p_m3u8 == NULL)
     {
@@ -1171,18 +1204,14 @@ static int hls_DownloadSegmentKey(stream_t *s, segment_t *seg)
         return VLC_EGENERIC;
     }
 
-    len = stream_Read(p_m3u8, aeskey, sizeof(aeskey));
+    int len = stream_Read(p_m3u8, seg->aes_key, sizeof(seg->aes_key));
+    stream_Delete(p_m3u8);
     if (len != AES_BLOCK_SIZE)
     {
-        msg_Err(s, "The AES key loaded doesn't have the right size (%zd)", len);
-        stream_Delete(p_m3u8);
+        msg_Err(s, "The AES key loaded doesn't have the right size (%d)", len);
         return VLC_EGENERIC;
     }
 
-    memcpy(seg->psz_AES_key, aeskey, AES_BLOCK_SIZE);
-
-    stream_Delete(p_m3u8);
-
     return VLC_SUCCESS;
 }
 
@@ -1196,6 +1225,8 @@ static int hls_ManageSegmentKeys(stream_t *s, hls_stream_t *hls)
     {
         prev_seg = seg;
         seg = segment_GetSegment(hls, i);
+        if (seg == NULL )
+            continue;
         if (seg->psz_key_path == NULL)
             continue;   /* No key to load ? continue */
         if (seg->b_key_loaded)
@@ -1205,7 +1236,7 @@ static int hls_ManageSegmentKeys(stream_t *s, hls_stream_t *hls)
          * try to copy it, and don't load the key */
         if (prev_seg && prev_seg->b_key_loaded && strcmp(seg->psz_key_path, prev_seg->psz_key_path) == 0)
         {
-            memcpy(seg->psz_AES_key, prev_seg->psz_AES_key, AES_BLOCK_SIZE);
+            memcpy(seg->aes_key, prev_seg->aes_key, AES_BLOCK_SIZE);
             seg->b_key_loaded = true;
             continue;
         }
@@ -1244,8 +1275,8 @@ static int hls_DecodeSegmentData(stream_t *s, hls_stream_t *hls, segment_t *segm
     }
 
     /* Set key */
-    i_gcrypt_err = gcry_cipher_setkey(aes_ctx, segment->psz_AES_key,
-                                       sizeof(segment->psz_AES_key));
+    i_gcrypt_err = gcry_cipher_setkey(aes_ctx, segment->aes_key,
+                                       sizeof(segment->aes_key));
     if (i_gcrypt_err)
     {
         msg_Err(s, "gcry_cipher_setkey failed: %s", gpg_strerror(i_gcrypt_err));
@@ -1253,17 +1284,17 @@ static int hls_DecodeSegmentData(stream_t *s, hls_stream_t *hls, segment_t *segm
         return VLC_EGENERIC;
     }
 
-    if (hls->b_iv_loaded == false)
+    if (segment->b_iv_loaded == false)
     {
-        memset(hls->psz_AES_IV, 0, AES_BLOCK_SIZE);
-        hls->psz_AES_IV[15] = segment->sequence & 0xff;
-        hls->psz_AES_IV[14] = (segment->sequence >> 8)& 0xff;
-        hls->psz_AES_IV[13] = (segment->sequence >> 16)& 0xff;
-        hls->psz_AES_IV[12] = (segment->sequence >> 24)& 0xff;
+        memset(segment->psz_AES_IV, 0, AES_BLOCK_SIZE);
+        segment->psz_AES_IV[15] = segment->sequence & 0xff;
+        segment->psz_AES_IV[14] = (segment->sequence >> 8)& 0xff;
+        segment->psz_AES_IV[13] = (segment->sequence >> 16)& 0xff;
+        segment->psz_AES_IV[12] = (segment->sequence >> 24)& 0xff;
     }
 
-    i_gcrypt_err = gcry_cipher_setiv(aes_ctx, hls->psz_AES_IV,
-                                      sizeof(hls->psz_AES_IV));
+    i_gcrypt_err = gcry_cipher_setiv(aes_ctx, segment->psz_AES_IV,
+                                      sizeof(segment->psz_AES_IV));
 
     if (i_gcrypt_err)
     {
@@ -1328,7 +1359,7 @@ static int get_HTTPLiveMetaPlaylist(stream_t *s, vlc_array_t **streams)
 
         /* Download playlist file from server */
         uint8_t *buf = NULL;
-        ssize_t len = read_M3U8_from_url(s, &dst->url, &buf);
+        ssize_t len = read_M3U8_from_url(s, dst->url, &buf);
         if (len < 0)
             err = VLC_EGENERIC;
         else
@@ -1341,54 +1372,60 @@ static int get_HTTPLiveMetaPlaylist(stream_t *s, vlc_array_t **streams)
     return err;
 }
 
-/* Reload playlist */
-static int hls_UpdatePlaylist(stream_t *s, hls_stream_t *hls_new, hls_stream_t **hls)
+/* Update hls_old (an existing member of p_sys->hls_stream) to match hls_new
+   (which represents a downloaded, perhaps newer version of the same playlist) */
+static int hls_UpdatePlaylist(stream_t *s, hls_stream_t *hls_new, hls_stream_t *hls_old, bool *stream_appended)
 {
     int count = vlc_array_count(hls_new->segments);
 
-    msg_Info(s, "updating hls stream (program-id=%d, bandwidth=%"PRIu64") has %d segments",
+    msg_Dbg(s, "updating hls stream (program-id=%d, bandwidth=%"PRIu64") has %d segments",
              hls_new->id, hls_new->bandwidth, count);
 
+    vlc_mutex_lock(&hls_old->lock);
+    hls_old->max_segment_length=-1;
     for (int n = 0; n < count; n++)
     {
         segment_t *p = segment_GetSegment(hls_new, n);
-        if (p == NULL) return VLC_EGENERIC;
+        if (p == NULL)
+        {
+            vlc_mutex_unlock(&hls_old->lock);
+            return VLC_EGENERIC;
+        }
 
-        vlc_mutex_lock(&(*hls)->lock);
-        segment_t *segment = segment_Find(*hls, p->sequence);
+        segment_t *segment = segment_Find(hls_old, p->sequence);
         if (segment)
         {
             vlc_mutex_lock(&segment->lock);
 
-            assert(p->url.psz_path);
-            assert(segment->url.psz_path);
+            assert(p->url);
+            assert(segment->url);
 
             /* they should be the same */
             if ((p->sequence != segment->sequence) ||
                 (p->duration != segment->duration) ||
-                (strcmp(p->url.psz_path, segment->url.psz_path) != 0))
+                (strcmp(p->url, segment->url) != 0))
             {
                 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);
+                msg_Warn(s, "- file: new=%s", p->url);
+                msg_Warn(s, "        old=%s", segment->url);
 
                 /* Resetting content */
-                char *psz_url = ConstructUrl(&p->url);
-                if (psz_url == NULL)
+                segment->sequence = p->sequence;
+                segment->duration = p->duration;
+                free(segment->url);
+                segment->url = strdup(p->url);
+                if ( segment->url == NULL )
                 {
                     msg_Err(s, "Failed updating segment %d - skipping it",  p->sequence);
                     segment_Free(p);
                     vlc_mutex_unlock(&segment->lock);
                     continue;
                 }
-                segment->sequence = p->sequence;
-                segment->duration = p->duration;
-                vlc_UrlClean(&segment->url);
-                vlc_UrlParse(&segment->url, psz_url, 0);
                 /* We must free the content, because if the key was not downloaded, content can't be decrypted */
-                if (segment->data)
+                if ((p->psz_key_path || p->b_key_loaded) &&
+                    segment->data)
                 {
                     block_Release(segment->data);
                     segment->data = NULL;
@@ -1396,50 +1433,54 @@ static int hls_UpdatePlaylist(stream_t *s, hls_stream_t *hls_new, hls_stream_t *
                 free(segment->psz_key_path);
                 segment->psz_key_path = p->psz_key_path ? strdup(p->psz_key_path) : NULL;
                 segment_Free(p);
-                free(psz_url);
             }
             vlc_mutex_unlock(&segment->lock);
         }
         else
         {
-            int last = vlc_array_count((*hls)->segments) - 1;
-            segment_t *l = segment_GetSegment(*hls, last);
-            if (l == NULL) goto fail_and_unlock;
+            int last = vlc_array_count(hls_old->segments) - 1;
+            segment_t *l = segment_GetSegment(hls_old, last);
+            if (l == NULL) {
+                vlc_mutex_unlock(&hls_old->lock);
+                return VLC_EGENERIC;
+            }
 
             if ((l->sequence + 1) != p->sequence)
             {
                 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_array_append(hls_old->segments, p);
+            msg_Dbg(s, "- segment %d appended", p->sequence);
+            hls_old->max_segment_length = __MAX(hls_old->max_segment_length, p->duration);
+            msg_Dbg(s, "  - segments new max duration %d", hls_old->max_segment_length);
+
+            // Signal download thread otherwise the segment will not get downloaded
+            *stream_appended = true;
         }
-        vlc_mutex_unlock(&(*hls)->lock);
     }
 
     /* update meta information */
-    vlc_mutex_lock(&(*hls)->lock);
-    (*hls)->sequence = hls_new->sequence;
-    (*hls)->duration = (hls_new->duration == -1) ? (*hls)->duration : hls_new->duration;
-    (*hls)->b_cache = hls_new->b_cache;
-    vlc_mutex_unlock(&(*hls)->lock);
+    hls_old->sequence = hls_new->sequence;
+    hls_old->duration = (hls_new->duration == -1) ? hls_old->duration : hls_new->duration;
+    hls_old->b_cache = hls_new->b_cache;
+    vlc_mutex_unlock(&hls_old->lock);
     return VLC_SUCCESS;
 
-fail_and_unlock:
-    assert(0);
-    vlc_mutex_unlock(&(*hls)->lock);
-    return VLC_EGENERIC;
 }
 
 static int hls_ReloadPlaylist(stream_t *s)
 {
     stream_sys_t *p_sys = s->p_sys;
 
+    // Flag to indicate if we should signal download thread
+    bool stream_appended = false;
+
     vlc_array_t *hls_streams = vlc_array_new();
     if (hls_streams == NULL)
         return VLC_ENOMEM;
 
-    msg_Info(s, "Reloading HLS live meta playlist");
+    msg_Dbg(s, "Reloading HLS live meta playlist");
 
     if (get_HTTPLiveMetaPlaylist(s, &hls_streams) != VLC_SUCCESS)
     {
@@ -1468,14 +1509,26 @@ static int hls_ReloadPlaylist(stream_t *s)
         if (hls_old == NULL)
         {   /* new hls stream - append */
             vlc_array_append(p_sys->hls_stream, hls_new);
-            msg_Info(s, "new HLS stream appended (id=%d, bandwidth=%"PRIu64")",
+            msg_Dbg(s, "new HLS stream appended (id=%d, bandwidth=%"PRIu64")",
                      hls_new->id, hls_new->bandwidth);
+
+            // New segment available -  signal download thread
+            stream_appended = true;
         }
-        else if (hls_UpdatePlaylist(s, hls_new, &hls_old) != VLC_SUCCESS)
-            msg_Info(s, "failed updating HLS stream (id=%d, bandwidth=%"PRIu64")",
+        else if (hls_UpdatePlaylist(s, hls_new, hls_old, &stream_appended) != VLC_SUCCESS)
+            msg_Warn(s, "failed updating HLS stream (id=%d, bandwidth=%"PRIu64")",
                      hls_new->id, hls_new->bandwidth);
     }
     vlc_array_destroy(hls_streams);
+
+    // Must signal the download thread otherwise new segments will not be downloaded at all!
+    if (stream_appended == true)
+    {
+        vlc_mutex_lock(&p_sys->download.lock_wait);
+        vlc_cond_signal(&p_sys->download.wait);
+        vlc_mutex_unlock(&p_sys->download.lock_wait);
+    }
+
     return VLC_SUCCESS;
 }
 
@@ -1534,7 +1587,7 @@ static int hls_DownloadSegmentData(stream_t *s, hls_stream_t *hls, segment_t *se
         int estimated = (int)(size / p_sys->bandwidth);
         if (estimated > segment->duration)
         {
-            msg_Warn(s,"downloading of segment %d takes %ds, which is longer than its playback (%ds)",
+            msg_Warn(s,"downloading segment %d predicted to take %ds, which exceeds its length (%ds)",
                         segment->sequence, estimated, segment->duration);
         }
     }
@@ -1542,16 +1595,16 @@ static int hls_DownloadSegmentData(stream_t *s, hls_stream_t *hls, segment_t *se
     mtime_t start = mdate();
     if (hls_Download(s, segment) != VLC_SUCCESS)
     {
-        msg_Err(s, "downloaded segment %d from stream %d failed",
+        msg_Err(s, "downloading segment %d from stream %d failed",
                     segment->sequence, *cur_stream);
         vlc_mutex_unlock(&segment->lock);
         return VLC_EGENERIC;
     }
     mtime_t duration = mdate() - start;
-    if (hls->bandwidth == 0)
+    if (hls->bandwidth == 0 && segment->duration > 0)
     {
         /* Try to estimate the bandwidth for this stream */
-        hls->bandwidth = (uint64_t)((double)segment->size / ((double)duration / 1000000.0));
+        hls->bandwidth = (uint64_t)(((double)segment->size * 8) / ((double)segment->duration));
     }
 
     /* If the segment is encrypted, decode it */
@@ -1563,15 +1616,10 @@ static int hls_DownloadSegmentData(stream_t *s, hls_stream_t *hls, segment_t *se
 
     vlc_mutex_unlock(&segment->lock);
 
-    msg_Info(s, "downloaded segment %d from stream %d",
+    msg_Dbg(s, "downloaded segment %d from stream %d",
                 segment->sequence, *cur_stream);
 
-    /* check for division by zero */
-    double ms = (double)duration / 1000.0; /* ms */
-    if (ms <= 0.0)
-        return VLC_SUCCESS;
-
-    uint64_t bw = ((double)(segment->size * 8) / ms) * 1000; /* bits / s */
+    uint64_t bw = segment->size * 8 * 1000000 / __MAX(1, duration); /* bits / s */
     p_sys->bandwidth = bw;
     if (p_sys->b_meta && (hls->bandwidth != bw))
     {
@@ -1580,7 +1628,7 @@ static int hls_DownloadSegmentData(stream_t *s, hls_stream_t *hls, segment_t *se
         /* FIXME: we need an average here */
         if ((newstream >= 0) && (newstream != *cur_stream))
         {
-            msg_Info(s, "detected %s bandwidth (%"PRIu64") stream",
+            msg_Dbg(s, "detected %s bandwidth (%"PRIu64") stream",
                      (bw >= hls->bandwidth) ? "faster" : "lower", bw);
             *cur_stream = newstream;
         }
@@ -1660,6 +1708,11 @@ static void* hls_Thread(void *p_this)
             p_sys->download.segment++;
         vlc_cond_signal(&p_sys->download.wait);
         vlc_mutex_unlock(&p_sys->download.lock_wait);
+
+        // In case of a successful download signal the read thread that data is available
+        vlc_mutex_lock(&p_sys->read.lock_wait);
+        vlc_cond_signal(&p_sys->read.wait);
+        vlc_mutex_unlock(&p_sys->read.lock_wait);
     }
 
     vlc_restorecancel(canc);
@@ -1675,20 +1728,21 @@ static void* hls_Reload(void *p_this)
 
     int canc = vlc_savecancel();
 
-    double wait = 0.5;
+    double wait = 1.0;
     while (vlc_object_alive(s))
     {
         mtime_t now = mdate();
         if (now >= p_sys->playlist.wakeup)
         {
-            /* reload the m3u8 */
-            if (hls_ReloadPlaylist(s) != VLC_SUCCESS)
+            /* reload the m3u8 if there are less than 3 segments what aren't downloaded */
+            if ( ( p_sys->download.segment - p_sys->playback.segment < 3 ) &&
+                 ( 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;
+                else if (p_sys->playlist.tries >= 3) wait = 1.5;
 
                 /* Can we afford to backoff? */
                 if (p_sys->download.segment - p_sys->playback.segment < 3)
@@ -1700,7 +1754,7 @@ static void* hls_Reload(void *p_this)
             else
             {
                 p_sys->playlist.tries = 0;
-                wait = 0.5;
+                wait = 1.0;
             }
 
             hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->download.stream);
@@ -1708,8 +1762,12 @@ static void* hls_Reload(void *p_this)
 
             /* determine next time to update playlist */
             p_sys->playlist.last = now;
-            p_sys->playlist.wakeup = now + ((mtime_t)(hls->duration * wait)
-                                                   * (mtime_t)1000000);
+            p_sys->playlist.wakeup = now;
+            /* If there is no new segments,use playlist duration as sleep period base */
+            if( likely( hls->max_segment_length > 0 ) )
+                p_sys->playlist.wakeup += (mtime_t)((hls->max_segment_length * wait) * CLOCK_FREQ);
+            else
+                p_sys->playlist.wakeup += (mtime_t)((hls->duration * wait) * CLOCK_FREQ);
         }
 
         mwait(p_sys->playlist.wakeup);
@@ -1722,35 +1780,27 @@ static void* hls_Reload(void *p_this)
 static int Prefetch(stream_t *s, int *current)
 {
     stream_sys_t *p_sys = s->p_sys;
-    int stream;
+    int stream = *current;
 
-    /* Try to pick best matching stream */;
-again:
-    stream = *current;
-
-    hls_stream_t *hls = hls_Get(p_sys->hls_stream, *current);
+    hls_stream_t *hls = hls_Get(p_sys->hls_stream, stream);
     if (hls == NULL)
         return VLC_EGENERIC;
 
-    segment_t *segment = segment_GetSegment(hls, p_sys->download.segment);
-    if (segment == NULL )
-        return VLC_EGENERIC;
-
-    if (hls_DownloadSegmentData(s, hls, segment, current) != VLC_SUCCESS)
+    if (vlc_array_count(hls->segments) == 0)
         return VLC_EGENERIC;
+    else if (vlc_array_count(hls->segments) == 1 && p_sys->b_live)
+        msg_Warn(s, "Only 1 segment available to prefetch in live stream; may stall");
 
-    /* Found better bandwidth match, try again */
-    if (*current != stream)
-        goto again;
-
-    /* Download first 2 segments of this HLS stream */
-    stream = *current;
-    for (int i = 0; i < 2; i++)
+    /* Download ~10s worth of segments of this HLS stream if they exist */
+    unsigned segment_amount = (0.5f + 10/hls->duration);
+    for (int i = 0; i < __MIN(vlc_array_count(hls->segments), segment_amount); i++)
     {
         segment_t *segment = segment_GetSegment(hls, p_sys->download.segment);
         if (segment == NULL )
             return VLC_EGENERIC;
 
+        /* It is useless to lock the segment here, as Prefetch is called before
+           download and playlit thread are started. */
         if (segment->data)
         {
             p_sys->download.segment++;
@@ -1781,52 +1831,79 @@ again:
  ****************************************************************************/
 static int hls_Download(stream_t *s, segment_t *segment)
 {
+    stream_sys_t *p_sys = s->p_sys;
     assert(segment);
 
-    /* Construct URL */
-    char *psz_url = ConstructUrl(&segment->url);
-    if (psz_url == NULL)
-           return VLC_ENOMEM;
+    vlc_mutex_lock(&p_sys->lock);
+    while (p_sys->paused)
+        vlc_cond_wait(&p_sys->wait, &p_sys->lock);
+    vlc_mutex_unlock(&p_sys->lock);
 
-    stream_t *p_ts = stream_UrlNew(s, psz_url);
-    free(psz_url);
+    stream_t *p_ts = stream_UrlNew(s, segment->url);
     if (p_ts == NULL)
         return VLC_EGENERIC;
 
     segment->size = stream_Size(p_ts);
-    assert(segment->size > 0);
 
-    segment->data = block_Alloc(segment->size);
-    if (segment->data == NULL)
-    {
+    if (segment->size == 0) {
+        int chunk_size = 65536;
+        segment->data = block_Alloc(chunk_size);
+        if (!segment->data)
+            goto nomem;
+        do {
+            if (segment->data->i_buffer - segment->size < chunk_size) {
+                chunk_size *= 2;
+                block_t *p_block = block_Realloc(segment->data, 0, segment->data->i_buffer + chunk_size);
+                if (!p_block) {
+                    block_Release(segment->data);
+                    segment->data = NULL;
+                    goto nomem;
+                }
+                segment->data = p_block;
+            }
+
+            ssize_t length = stream_Read(p_ts, segment->data->p_buffer + segment->size, chunk_size);
+            if (length <= 0) {
+                segment->data->i_buffer = segment->size;
+                break;
+            }
+            segment->size += length;
+        } while (vlc_object_alive(s));
+
         stream_Delete(p_ts);
-        return VLC_ENOMEM;
+        return VLC_SUCCESS;
     }
 
+    segment->data = block_Alloc(segment->size);
+    if (segment->data == NULL)
+        goto nomem;
+
     assert(segment->data->i_buffer == segment->size);
 
-    ssize_t length = 0, curlen = 0;
-    uint64_t size;
+    ssize_t curlen = 0;
     do
     {
-        size = stream_Size(p_ts);
+        /* NOTE: Beware the size reported for a segment by the HLS server may not
+         * be correct, when downloading the segment data. Therefore check the size
+         * and enlarge the segment data block if necessary.
+         */
+        uint64_t size = stream_Size(p_ts);
         if (size > segment->size)
         {
             msg_Dbg(s, "size changed %"PRIu64, segment->size);
             block_t *p_block = block_Realloc(segment->data, 0, size);
             if (p_block == NULL)
             {
-                stream_Delete(p_ts);
                 block_Release(segment->data);
                 segment->data = NULL;
-                return VLC_ENOMEM;
+                goto nomem;
             }
             segment->data = p_block;
             segment->size = size;
             assert(segment->data->i_buffer == segment->size);
             p_block = NULL;
         }
-        length = stream_Read(p_ts, segment->data->p_buffer + curlen, segment->size - curlen);
+        ssize_t length = stream_Read(p_ts, segment->data->p_buffer + curlen, segment->size - curlen);
         if (length <= 0)
             break;
         curlen += length;
@@ -1834,6 +1911,10 @@ static int hls_Download(stream_t *s, segment_t *segment)
 
     stream_Delete(p_ts);
     return VLC_SUCCESS;
+
+nomem:
+    stream_Delete(p_ts);
+    return VLC_ENOMEM;
 }
 
 /* Read M3U8 file */
@@ -1852,7 +1933,10 @@ static ssize_t read_M3U8_from_stream(stream_t *s, uint8_t **buffer)
         if (bytes == 0)
             break;      /* EOF ? */
         else if (bytes < 0)
+        {
+            free (p);
             return bytes;
+        }
 
         if ( (total_bytes + bytes + 1) > total_allocated )
         {
@@ -1879,17 +1963,12 @@ static ssize_t read_M3U8_from_stream(stream_t *s, uint8_t **buffer)
     return total_bytes;
 }
 
-static ssize_t read_M3U8_from_url(stream_t *s, vlc_url_t *url, uint8_t **buffer)
+static ssize_t read_M3U8_from_url(stream_t *s, const char* psz_url, uint8_t **buffer)
 {
     assert(*buffer == NULL);
 
     /* Construct URL */
-    char *psz_url = ConstructUrl(url);
-    if (psz_url == NULL)
-           return VLC_ENOMEM;
-
     stream_t *p_m3u8 = stream_UrlNew(s, psz_url);
-    free(psz_url);
     if (p_m3u8 == NULL)
         return VLC_EGENERIC;
 
@@ -1910,21 +1989,27 @@ static char *ReadLine(uint8_t *buffer, uint8_t **pos, const size_t len)
 
     while (p < end)
     {
-        if ((*p == '\n') || (*p == '\0'))
+        if ((*p == '\r') || (*p == '\n') || (*p == '\0'))
             break;
         p++;
     }
 
-    /* copy line excluding \n or \0 */
+    /* copy line excluding \r \n or \0 */
     line = strndup((char *)begin, p - begin);
 
-    if (*p == '\0')
-        *pos = end;
-    else
+    while ((*p == '\r') || (*p == '\n') || (*p == '\0'))
     {
-        /* next pass start after \n */
-        p++;
-        *pos = p;
+        if (*p == '\0')
+        {
+            *pos = end;
+            break;
+        }
+        else
+        {
+            /* next pass start after \r and \n */
+            p++;
+            *pos = p;
+        }
     }
 
     return line;
@@ -1957,8 +2042,17 @@ static int Open(vlc_object_t *p_this)
         free(p_sys);
         return VLC_ENOMEM;
     }
-    vlc_UrlParse(&p_sys->m3u8, psz_uri, 0);
-    free(psz_uri);
+    p_sys->m3u8 = psz_uri;
+
+    char *new_path;
+    if (asprintf(&new_path, "%s.ts", s->psz_path) < 0)
+    {
+        free(p_sys->m3u8);
+        free(p_sys);
+        return VLC_ENOMEM;
+    }
+    free(s->psz_path);
+    s->psz_path = new_path;
 
     p_sys->bandwidth = 0;
     p_sys->b_live = true;
@@ -1968,7 +2062,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->m3u8);
         free(p_sys);
         return VLC_ENOMEM;
     }
@@ -1978,6 +2072,11 @@ static int Open(vlc_object_t *p_this)
     s->pf_peek = Peek;
     s->pf_control = Control;
 
+    p_sys->paused = false;
+
+    vlc_cond_init(&p_sys->wait);
+    vlc_mutex_init(&p_sys->lock);
+
     /* Parse HLS m3u8 content. */
     uint8_t *buffer = NULL;
     ssize_t len = read_M3U8_from_stream(s->p_source, &buffer);
@@ -1989,26 +2088,24 @@ static int Open(vlc_object_t *p_this)
         goto fail;
     }
     free(buffer);
+    /* HLS standard doesn't provide any guaranty about streams
+       being sorted by bandwidth, so we sort them */
+    qsort( p_sys->hls_stream->pp_elems, p_sys->hls_stream->i_count,
+           sizeof( hls_stream_t* ), &hls_CompareStreams );
 
     /* Choose first HLS stream to start with */
-    int current = p_sys->playback.stream = 0;
+    int current = p_sys->playback.stream = p_sys->hls_stream->i_count-1;
     p_sys->playback.segment = p_sys->download.segment = ChooseSegment(s, current);
 
     /* manage encryption key if needed */
     hls_ManageSegmentKeys(s, hls_Get(p_sys->hls_stream, current));
 
-    if (p_sys->b_live && (p_sys->playback.segment < 0))
-    {
-        msg_Warn(s, "less data than 3 times 'target duration' available for live playback, playback may stall");
-    }
-
     if (Prefetch(s, &current) != VLC_SUCCESS)
     {
         msg_Err(s, "fetching first segment failed.");
         goto fail;
     }
 
-
     p_sys->download.stream = current;
     p_sys->playback.stream = current;
     p_sys->download.seek = -1;
@@ -2016,13 +2113,16 @@ static int Open(vlc_object_t *p_this)
     vlc_mutex_init(&p_sys->download.lock_wait);
     vlc_cond_init(&p_sys->download.wait);
 
+    vlc_mutex_init(&p_sys->read.lock_wait);
+    vlc_cond_init(&p_sys->read.wait);
+
     /* Initialize HLS live stream */
     if (p_sys->b_live)
     {
         hls_stream_t *hls = hls_Get(p_sys->hls_stream, current);
         p_sys->playlist.last = mdate();
         p_sys->playlist.wakeup = p_sys->playlist.last +
-                ((mtime_t)hls->duration * UINT64_C(1000000));
+                ((mtime_t)hls->duration * CLOCK_FREQ );
 
         if (vlc_clone(&p_sys->reload, hls_Reload, s, VLC_THREAD_PRIORITY_LOW))
         {
@@ -2043,6 +2143,9 @@ fail_thread:
     vlc_mutex_destroy(&p_sys->download.lock_wait);
     vlc_cond_destroy(&p_sys->download.wait);
 
+    vlc_mutex_destroy(&p_sys->read.lock_wait);
+    vlc_cond_destroy(&p_sys->read.wait);
+
 fail:
     /* Free hls streams */
     for (int i = 0; i < vlc_array_count(p_sys->hls_stream); i++)
@@ -2052,8 +2155,11 @@ fail:
     }
     vlc_array_destroy(p_sys->hls_stream);
 
+    vlc_mutex_destroy(&p_sys->lock);
+    vlc_cond_destroy(&p_sys->wait);
+
     /* */
-    vlc_UrlClean(&p_sys->m3u8);
+    free(p_sys->m3u8);
     free(p_sys);
     return VLC_EGENERIC;
 }
@@ -2068,8 +2174,16 @@ static void Close(vlc_object_t *p_this)
 
     assert(p_sys->hls_stream);
 
+    vlc_mutex_lock(&p_sys->lock);
+    p_sys->paused = false;
+    vlc_cond_signal(&p_sys->wait);
+    vlc_mutex_unlock(&p_sys->lock);
+
     /* */
     vlc_mutex_lock(&p_sys->download.lock_wait);
+    /* negate the condition variable's predicate */
+    p_sys->download.segment = p_sys->playback.segment = 0;
+    p_sys->download.seek = 0; /* better safe than sorry */
     vlc_cond_signal(&p_sys->download.wait);
     vlc_mutex_unlock(&p_sys->download.lock_wait);
 
@@ -2080,6 +2194,9 @@ static void Close(vlc_object_t *p_this)
     vlc_mutex_destroy(&p_sys->download.lock_wait);
     vlc_cond_destroy(&p_sys->download.wait);
 
+    vlc_mutex_destroy(&p_sys->read.lock_wait);
+    vlc_cond_destroy(&p_sys->read.wait);
+
     /* Free hls streams */
     for (int i = 0; i < vlc_array_count(p_sys->hls_stream); i++)
     {
@@ -2089,7 +2206,11 @@ static void Close(vlc_object_t *p_this)
     vlc_array_destroy(p_sys->hls_stream);
 
     /* */
-    vlc_UrlClean(&p_sys->m3u8);
+
+    vlc_mutex_destroy(&p_sys->lock);
+    vlc_cond_destroy(&p_sys->wait);
+
+    free(p_sys->m3u8);
     if (p_sys->peeked)
         block_Release (p_sys->peeked);
     free(p_sys);
@@ -2111,21 +2232,23 @@ static segment_t *GetSegment(stream_t *s)
         segment = segment_GetSegment(hls, p_sys->playback.segment);
         if (segment != NULL)
         {
+            vlc_mutex_lock(&segment->lock);
             /* This segment is ready? */
             if (segment->data != NULL)
             {
+                vlc_mutex_unlock(&segment->lock);
                 p_sys->b_cache = hls->b_cache;
                 vlc_mutex_unlock(&hls->lock);
                 goto check;
             }
+            vlc_mutex_unlock(&segment->lock);
         }
         vlc_mutex_unlock(&hls->lock);
     }
 
     /* Was the HLS stream changed to another bitrate? */
-    int i_stream = 0;
     segment = NULL;
-    while(vlc_object_alive(s))
+    for (int i_stream = 0; i_stream < vlc_array_count(p_sys->hls_stream); i_stream++)
     {
         /* Is the next segment ready */
         hls_stream_t *hls = hls_Get(p_sys->hls_stream, i_stream);
@@ -2144,30 +2267,29 @@ static segment_t *GetSegment(stream_t *s)
         int i_segment = p_sys->download.segment;
         vlc_mutex_unlock(&p_sys->download.lock_wait);
 
+        vlc_mutex_lock(&segment->lock);
         /* This segment is ready? */
         if ((segment->data != NULL) &&
             (p_sys->playback.segment < i_segment))
         {
             p_sys->playback.stream = i_stream;
             p_sys->b_cache = hls->b_cache;
+            vlc_mutex_unlock(&segment->lock);
             vlc_mutex_unlock(&hls->lock);
             goto check;
         }
+        vlc_mutex_unlock(&segment->lock);
         vlc_mutex_unlock(&hls->lock);
 
         if (!p_sys->b_meta)
             break;
-
-        /* Was the stream changed to another bitrate? */
-        i_stream++;
-        if (i_stream >= vlc_array_count(p_sys->hls_stream))
-            break;
     }
     /* */
     return NULL;
 
 check:
     /* sanity check */
+    assert(segment->data);
     if (segment->data->i_buffer == 0)
     {
         vlc_mutex_lock(&hls->lock);
@@ -2184,12 +2306,12 @@ check:
     return segment;
 }
 
-static int segment_RestorePos( segment_t *segment )
+static int segment_RestorePos(segment_t *segment)
 {
-    if( segment->data )
+    if (segment->data)
     {
-        uint64_t size = segment->size -segment->data->i_buffer;
-        if( size > 0 )
+        uint64_t size = segment->size - segment->data->i_buffer;
+        if (size > 0)
         {
             segment->data->i_buffer += size;
             segment->data->p_buffer -= size;
@@ -2198,10 +2320,11 @@ static int segment_RestorePos( segment_t *segment )
     return VLC_SUCCESS;
 }
 
+/* p_read might be NULL if caller wants to skip data */
 static ssize_t hls_Read(stream_t *s, uint8_t *p_read, unsigned int i_read)
 {
     stream_sys_t *p_sys = s->p_sys;
-    ssize_t copied = 0;
+    ssize_t used = 0;
 
     do
     {
@@ -2221,20 +2344,20 @@ static ssize_t hls_Read(stream_t *s, uint8_t *p_read, unsigned int i_read)
                 segment->data = NULL;
             }
             else
-                segment_RestorePos( segment );
+                segment_RestorePos(segment);
 
-            p_sys->playback.segment++;
             vlc_mutex_unlock(&segment->lock);
 
             /* signal download thread */
             vlc_mutex_lock(&p_sys->download.lock_wait);
+            p_sys->playback.segment++;
             vlc_cond_signal(&p_sys->download.wait);
             vlc_mutex_unlock(&p_sys->download.lock_wait);
             continue;
         }
 
         if (segment->size == segment->data->i_buffer)
-            msg_Info(s, "playing segment %d from stream %d",
+            msg_Dbg(s, "playing segment %d from stream %d",
                      segment->sequence, p_sys->playback.stream);
 
         ssize_t len = -1;
@@ -2245,17 +2368,18 @@ static ssize_t hls_Read(stream_t *s, uint8_t *p_read, unsigned int i_read)
 
         if (len > 0)
         {
-            memcpy(p_read + copied, segment->data->p_buffer, len);
+            if (p_read) /* if NULL, then caller skips data */
+                memcpy(p_read + used, segment->data->p_buffer, len);
             segment->data->i_buffer -= len;
             segment->data->p_buffer += len;
-            copied += len;
+            used += len;
             i_read -= len;
         }
         vlc_mutex_unlock(&segment->lock);
 
-    } while ((i_read > 0) && vlc_object_alive(s));
+    } while (i_read > 0);
 
-    return copied;
+    return used;
 }
 
 static int Read(stream_t *s, void *buffer, unsigned int i_read)
@@ -2265,21 +2389,55 @@ static int Read(stream_t *s, void *buffer, unsigned int i_read)
 
     assert(p_sys->hls_stream);
 
-    if (p_sys->b_error)
-        return 0;
-
-    if (buffer == NULL)
+    while (length == 0)
     {
-        /* caller skips data, get big enough buffer */
-        msg_Warn(s, "buffer is NULL (allocate %d)", i_read);
-        buffer = calloc(1, i_read);
-        if (buffer == NULL)
-            return 0; /* NO MEMORY left*/
-    }
+        // In case an error occurred or the stream was closed return 0
+        if (p_sys->b_error || !vlc_object_alive(s))
+            return 0;
 
-    length = hls_Read(s, (uint8_t*) buffer, i_read);
-    if (length < 0)
-        return 0;
+        // Lock the mutex before trying to read to avoid a race condition with the download thread
+        vlc_mutex_lock(&p_sys->read.lock_wait);
+
+        /* NOTE: buffer might be NULL if caller wants to skip data */
+        length = hls_Read(s, (uint8_t*) buffer, i_read);
+
+        // An error has occurred in hls_Read
+        if (length < 0)
+        {
+            vlc_mutex_unlock(&p_sys->read.lock_wait);
+
+            return 0;
+        }
+
+        // There is no data available yet for the demuxer so we need to wait until reload and
+        // download operation are over.
+        // Download thread will signal once download is finished.
+        // A timed wait is used to avoid deadlock in case data never arrives since the thread
+        // running this read operation is also responsible for closing the stream
+        if (length == 0)
+        {
+            mtime_t start = mdate();
+
+            // Wait for 10 seconds
+            mtime_t timeout_limit = start + (10 * CLOCK_FREQ);
+
+            int res = vlc_cond_timedwait(&p_sys->read.wait, &p_sys->read.lock_wait, timeout_limit);
+
+            // Error - reached a timeout of 10 seconds without data arriving - kill the stream
+            if (res == ETIMEDOUT)
+            {
+                msg_Warn(s, "timeout limit reached!");
+
+                vlc_mutex_unlock(&p_sys->read.lock_wait);
+
+                return 0;
+            }
+            else if (res == EINVAL)
+                return 0; // Error - lock is not locked so we can just return
+        }
+
+        vlc_mutex_unlock(&p_sys->read.lock_wait);
+    }
 
     p_sys->playback.offset += length;
     return length;
@@ -2304,7 +2462,7 @@ static int Peek(stream_t *s, const uint8_t **pp_peek, unsigned int i_peek)
     size_t i_buff = segment->data->i_buffer;
     uint8_t *p_buff = segment->data->p_buffer;
 
-    if (i_peek < i_buff)
+    if ( likely(i_peek < i_buff))
     {
         *pp_peek = p_buff;
         vlc_mutex_unlock(&segment->lock);
@@ -2325,7 +2483,11 @@ static int Peek(stream_t *s, const uint8_t **pp_peek, unsigned int i_peek)
         else if (peeked->i_buffer < i_peek)
             peeked = block_Realloc (peeked, 0, i_peek);
         if (peeked == NULL)
+        {
+            vlc_mutex_unlock(&segment->lock);
             return 0;
+        }
+        p_sys->peeked = peeked;
 
         memcpy(peeked->p_buffer, p_buff, i_buff);
         curlen = i_buff;
@@ -2336,7 +2498,7 @@ static int Peek(stream_t *s, const uint8_t **pp_peek, unsigned int i_peek)
         p_buff = peeked->p_buffer;
         *pp_peek = p_buff;
 
-        while ((curlen < i_peek) && vlc_object_alive(s))
+        while (curlen < i_peek)
         {
             nsegment = GetSegment(s);
             if (nsegment == NULL)
@@ -2417,7 +2579,6 @@ static uint64_t GetStreamSize(stream_t *s)
     return size;
 }
 
-
 static int segment_Seek(stream_t *s, const uint64_t pos)
 {
     stream_sys_t *p_sys = s->p_sys;
@@ -2433,16 +2594,12 @@ static int segment_Seek(stream_t *s, const uint64_t pos)
     uint64_t size = hls->size;
     int count = vlc_array_count(hls->segments);
 
-    /* restore current segment to start position */
-    segment_t *segment = segment_GetSegment(hls, p_sys->playback.segment);
-    if (segment == NULL)
+    segment_t *currentSegment = segment_GetSegment(hls, p_sys->playback.segment);
+    if (currentSegment == NULL)
     {
         vlc_mutex_unlock(&hls->lock);
         return VLC_EGENERIC;
     }
-    vlc_mutex_lock(&segment->lock);
-    segment_RestorePos( segment );
-    vlc_mutex_unlock(&segment->lock);
 
     for (int n = 0; n < count; n++)
     {
@@ -2457,7 +2614,7 @@ static int segment_Seek(stream_t *s, const uint64_t pos)
         length += segment->duration * (hls->bandwidth/8);
         vlc_mutex_unlock(&segment->lock);
 
-        if (!b_found && (pos <= length))
+        if (pos <= length)
         {
             if (count - n >= 3)
             {
@@ -2481,15 +2638,22 @@ static int segment_Seek(stream_t *s, const uint64_t pos)
     /* */
     if (b_found)
     {
-        /* restore segment to start position */
+
+        /* restore current segment to start position */
+        vlc_mutex_lock(&currentSegment->lock);
+        segment_RestorePos(currentSegment);
+        vlc_mutex_unlock(&currentSegment->lock);
+
+        /* restore seeked segment to start position */
         segment_t *segment = segment_GetSegment(hls, p_sys->playback.segment);
         if (segment == NULL)
         {
             vlc_mutex_unlock(&hls->lock);
             return VLC_EGENERIC;
         }
+
         vlc_mutex_lock(&segment->lock);
-        segment_RestorePos( segment );
+        segment_RestorePos(segment);
         vlc_mutex_unlock(&segment->lock);
 
         /* start download at current playback segment */
@@ -2499,14 +2663,12 @@ static int segment_Seek(stream_t *s, const uint64_t pos)
         vlc_mutex_lock(&p_sys->download.lock_wait);
         p_sys->download.seek = p_sys->playback.segment;
         vlc_cond_signal(&p_sys->download.wait);
-        vlc_mutex_unlock(&p_sys->download.lock_wait);
 
         /* Wait for download to be finished */
-        vlc_mutex_lock(&p_sys->download.lock_wait);
-        msg_Info(s, "seek to segment %d", p_sys->playback.segment);
-        while (((p_sys->download.seek != -1) ||
-                (p_sys->download.segment - p_sys->playback.segment < 3)) &&
-                (p_sys->download.segment < (count - 6)))
+        msg_Dbg(s, "seek to segment %d", p_sys->playback.segment);
+        while ((p_sys->download.seek != -1) ||
+           ((p_sys->download.segment - p_sys->playback.segment < 3) &&
+                (p_sys->download.segment < count)))
         {
             vlc_cond_wait(&p_sys->download.wait, &p_sys->download.lock_wait);
             if (!vlc_object_alive(s) || s->b_error) break;
@@ -2529,9 +2691,26 @@ static int Control(stream_t *s, int i_query, va_list args)
         case STREAM_CAN_SEEK:
             *(va_arg (args, bool *)) = hls_MaySeek(s);
             break;
+        case STREAM_CAN_CONTROL_PACE:
+        case STREAM_CAN_PAUSE:
+            *(va_arg (args, bool *)) = true;
+            break;
+        case STREAM_CAN_FASTSEEK:
+            *(va_arg (args, bool *)) = false;
+            break;
         case STREAM_GET_POSITION:
             *(va_arg (args, uint64_t *)) = p_sys->playback.offset;
             break;
+        case STREAM_SET_PAUSE_STATE:
+        {
+            bool paused = va_arg (args, unsigned);
+
+            vlc_mutex_lock(&p_sys->lock);
+            p_sys->paused = paused;
+            vlc_cond_signal(&p_sys->wait);
+            vlc_mutex_unlock(&p_sys->lock);
+            break;
+        }
         case STREAM_SET_POSITION:
             if (hls_MaySeek(s))
             {
@@ -2546,6 +2725,10 @@ static int Control(stream_t *s, int i_query, va_list args)
         case STREAM_GET_SIZE:
             *(va_arg (args, uint64_t *)) = GetStreamSize(s);
             break;
+        case STREAM_GET_PTS_DELAY:
+            *va_arg (args, int64_t *) = INT64_C(1000) *
+                var_InheritInteger(s, "network-caching");
+             break;
         default:
             return VLC_EGENERIC;
     }