[vlc-commits] stream_filter/httplive.c: read a HLS m3u8 file in one go, parse later

Jean-Paul Saman git at videolan.org
Fri Feb 11 10:14:07 CET 2011


vlc | branch: master | Jean-Paul Saman <jean-paul.saman at m2x.nl> | Wed Jan 26 17:36:48 2011 +0100| [9ad9fe48bb492fe9773418b5b8f888a88b914f23] | committer: Jean-Paul Saman

stream_filter/httplive.c: read a HLS m3u8 file in one go, parse later

Functions to read an entire M3U8 meta or playlist file from the HLS server into a buffer.
The buffer will be parsed later.

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

 modules/stream_filter/httplive.c |  100 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 100 insertions(+), 0 deletions(-)

diff --git a/modules/stream_filter/httplive.c b/modules/stream_filter/httplive.c
index 3af6d1a..ee9f3c2 100644
--- a/modules/stream_filter/httplive.c
+++ b/modules/stream_filter/httplive.c
@@ -28,6 +28,8 @@
 # include "config.h"
 #endif
 
+#include <limits.h>
+
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 
@@ -1536,6 +1538,104 @@ static int AccessDownload(stream_t *s, segment_t *segment)
     return VLC_SUCCESS;
 }
 
+/* Read M3U8 file */
+static uint8_t *access_ReadM3U8(stream_t *s, vlc_url_t *url)
+{
+    stream_sys_t *p_sys = (stream_sys_t *) s->p_sys;
+
+    /* Download new playlist file from server */
+    if (AccessOpen(s, url) != VLC_SUCCESS)
+        return NULL;
+
+    ssize_t size = p_sys->p_access->info.i_size;
+    if (size == 0) size = 1024; /* no Content-Length */
+
+    msg_Err(s, "Stream size is %"PRId64, size);
+
+    uint8_t *buffer = calloc(1, size);
+    if (buffer == NULL)
+    {
+        AccessClose(s);
+        return NULL;
+    }
+
+    size_t length = 0, curlen = 0;
+    do
+    {
+        length = p_sys->p_access->pf_read(p_sys->p_access, buffer + curlen, size - curlen);
+        if ((length <= 0) || (length >= size))
+            break;
+        curlen += length;
+        if (curlen >= size)
+        {
+            uint8_t *tmp = realloc(*buffer, size + 1024);
+            if (tmp == NULL)
+                break;
+            size += 1024;
+            *buffer = tmp;
+        }
+    } while (vlc_object_alive(s));
+
+    AccessClose(s);
+    return buffer;
+}
+
+static uint8_t *ReadM3U8(stream_t *s)
+{
+    int64_t size = stream_Size(s->p_source);
+
+    msg_Err(s, "Stream size is %"PRId64, size);
+    if (size == 0) size = 1024; /* no Content-Length */
+
+    uint8_t *buffer = calloc(1, size);
+    if (buffer == NULL)
+        return NULL;
+
+    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 buffer;
+}
+
+static char *ReadLine(uint8_t *buffer, uint8_t *remain, size_t len)
+{
+    assert(buffer);
+
+    char *line = NULL;
+    uint8_t *begin = buffer;
+    uint8_t *p = begin;
+    uint8_t *end = p + len;
+
+    while (p < end)
+    {
+        if (*p == '\n')
+            break;
+        p++;
+    }
+
+    /* copy line excluding \n */
+    line = strndup((char *)begin, p - begin);
+
+    /* next pass start after \n */
+    p++;
+    remain = p;
+
+    return line;
+}
+
 /****************************************************************************
  * Open
  ****************************************************************************/



More information about the vlc-commits mailing list