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

Francois Cartegnie git at videolan.org
Wed Apr 22 14:58:48 CEST 2015


vlc | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Wed Apr 22 14:50:00 2015 +0200| [f162660200e8f706a101963745010dccc0e77540] | committer: Francois Cartegnie

stream_filter: httplive: use stream size for m3u8

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

 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 e3e8f9b..aab46e0 100644
--- a/modules/stream_filter/httplive.c
+++ b/modules/stream_filter/httplive.c
@@ -1915,47 +1915,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