]> git.sesse.net Git - vlc/blobdiff - modules/stream_filter/httplive.c
stream_filter/httplive.c: add usefull comment.
[vlc] / modules / stream_filter / httplive.c
index 224e312e6bb090fc916c7f85d9bd06c36829a99c..26f6d743ab594d0ae6c8615c0758c13e97630a07 100644 (file)
 #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>
@@ -70,7 +70,7 @@ typedef struct segment_s
 
     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;
 
     vlc_mutex_t lock;
@@ -165,45 +165,57 @@ 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;
+    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 < len)
+                continue;
+            if (!memcmp(peek, ext[i], len))
+                return true;
         }
-        peek++;
-    };
+    }
 
     return false;
 }
@@ -472,7 +484,8 @@ 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>[,]* */
             p = strchr(begin, ',');
@@ -490,33 +503,38 @@ 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], NULL, 16);
+        if (end)
+            return VLC_EGENERIC;
+        string_hexa[len-16] = '\0';
+        iv_hi = strtoull(string_hexa, NULL, 16);
+        if (end)
+            return VLC_EGENERIC;
+    }
+
+    for (int i = 8; i ; --i) {
+        iv[  i] = iv_hi & 0xff;
+        iv[8+i] = iv_lo & 0xff;
+        iv_hi >>= 8;
+        iv_lo >>= 8;
     }
 
     return VLC_SUCCESS;
@@ -557,20 +575,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;
@@ -579,6 +600,7 @@ static int parse_SegmentInformation(hls_stream_t *hls, char *p_read, int *durati
             value = ((int)d) + 1;
         else
             value = ((int)d);
+        *duration = value;
     }
 
     /* Ignore the rest of the line */
@@ -778,7 +800,10 @@ static int parse_Key(stream_t *s, hls_stream_t *hls, char *p_read)
             */
 
             if (string_to_IV(iv, hls->psz_AES_IV) == VLC_EGENERIC)
+            {
+                msg_Err(s, "IV invalid");
                 err = VLC_EGENERIC;
+            }
             else
                 hls->b_iv_loaded = true;
             free(value);
@@ -1071,9 +1096,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)
     {
@@ -1081,18 +1103,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;
 }
 
@@ -1117,7 +1135,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;
         }
@@ -1156,8 +1174,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));
@@ -1458,10 +1476,10 @@ static int hls_DownloadSegmentData(stream_t *s, hls_stream_t *hls, segment_t *se
         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 */
@@ -1699,6 +1717,10 @@ static int hls_Download(stream_t *s, segment_t *segment)
     uint64_t size;
     do
     {
+        /* 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.
+         */
         size = stream_Size(p_ts);
         if (size > segment->size)
         {
@@ -1795,21 +1817,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;
@@ -2392,14 +2420,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)))
+        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;