[vlc-commits] stream_filter: httplive: use stream size for m3u8

Francois Cartegnie git at videolan.org
Wed Apr 22 16:37:56 CEST 2015


vlc/vlc-2.2 | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Wed Apr 22 14:50:00 2015 +0200| [9d16134339854214051d60f323a57d829e69dc76] | committer: Jean-Baptiste Kempf

stream_filter: httplive: use stream size for m3u8

(cherry picked from commit f162660200e8f706a101963745010dccc0e77540)
Signed-off-by: Jean-Baptiste Kempf <jb at videolan.org>

> http://git.videolan.org/gitweb.cgi/vlc/vlc-2.2.git/?a=commit;h=9d16134339854214051d60f323a57d829e69dc76
---

 modules/stream_filter/httplive.c |   55 +++++++++++++++++++-------------------
 1 file changed, 28 insertions(+), 27 deletions(-)

diff --git a/modules/stream_filter/httplive.c b/modules/stream_filter/httplive.c
index 2960344..035c367 100644
--- a/modules/stream_filter/httplive.c
+++ b/modules/stream_filter/httplive.c
@@ -1909,47 +1909,48 @@ end:
 /* Read M3U8 file */
 static ssize_t read_M3U8_from_stream(stream_t *s, uint8_t **buffer)
 {
-    int64_t total_bytes = 0;
-    int64_t total_allocated = 0;
     uint8_t *p = NULL;
+    int64_t size = stream_Size(s);
+    size = VLC_CLIP(size, 0, INT64_MAX - 1);
+    int64_t i_alloc_size = 0;
+    ssize_t i_total_read = 0;
+    unsigned i_chunk_size = HLS_READ_SIZE;
 
-    while (1)
+    for( ;; )
     {
-        char buf[4096];
-        int64_t bytes;
+        int i_toread = (size) ? size - i_total_read : i_chunk_size;
+        if(i_toread + i_total_read > INT64_MAX - 1)
+            break;
+        if(i_alloc_size < i_toread)
+        {
+            i_alloc_size += i_toread;
+            p = realloc_or_free(p, 1 + i_alloc_size);
+            if (p == NULL)
+                return VLC_ENOMEM;
+            if (i_chunk_size < (1 << 26))
+                i_chunk_size <<= 1;
+        }
 
-        bytes = stream_Read(s, buf, sizeof(buf));
-        if (bytes == 0)
+        int i_read = stream_Read(s, & p[i_total_read], i_toread);
+        if (i_read == 0)
+        {
             break;      /* EOF ? */
-        else if (bytes < 0)
+        }
+        else if (i_read < 0)
         {
             free (p);
-            return bytes;
+            return i_read;
         }
-
-        if ( (total_bytes + bytes + 1) > total_allocated )
+        else
         {
-            if (total_allocated)
-                total_allocated *= 2;
-            else
-                total_allocated = __MIN((uint64_t)bytes+1, sizeof(buf));
-
-            p = realloc_or_free(p, total_allocated);
-            if (p == NULL)
-                return VLC_ENOMEM;
+            i_total_read += i_read;
         }
-
-        memcpy(p+total_bytes, buf, bytes);
-        total_bytes += bytes;
     }
 
-    if (total_allocated == 0)
-        return VLC_EGENERIC;
-
-    p[total_bytes] = '\0';
+    p[i_total_read] = '\0';
     *buffer = p;
 
-    return total_bytes;
+    return i_total_read;
 }
 
 static ssize_t read_M3U8_from_url(stream_t *s, const char* psz_url, uint8_t **buffer)



More information about the vlc-commits mailing list