]> git.sesse.net Git - vlc/blobdiff - modules/stream_filter/httplive.c
l10n: Khmer update
[vlc] / modules / stream_filter / httplive.c
index ce2cbbf908e32d45cfa647cf70b464a1875dbcd9..489a359d0683990f27f5b5d51e7d9b6545725948 100644 (file)
@@ -29,6 +29,7 @@
 #endif
 
 #include <limits.h>
+#include <errno.h>
 
 #include <vlc_common.h>
 #include <vlc_plugin.h>
@@ -39,6 +40,7 @@
 #include <vlc_arrays.h>
 #include <vlc_stream.h>
 #include <vlc_url.h>
+#include <vlc_memory.h>
 
 /*****************************************************************************
  * Module descriptor
@@ -90,6 +92,8 @@ struct stream_sys_t
     vlc_thread_t  reload;       /* HLS m3u8 reload thread */
     vlc_thread_t  thread;       /* HLS segment download thread */
 
+    block_t      *peeked;
+
     /* */
     vlc_array_t  *hls_stream;   /* bandwidth adaptation */
     uint64_t      bandwidth;    /* measured bandwidth (bits per second) */
@@ -134,8 +138,8 @@ static int  Read   (stream_t *, void *p_read, unsigned int i_read);
 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(stream_t *s, vlc_url_t *url, uint8_t **buffer);
-static ssize_t ReadM3U8(stream_t *s, uint8_t **buffer);
+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 char *ReadLine(uint8_t *buffer, uint8_t **pos, size_t len);
 
 static int hls_Download(stream_t *s, segment_t *segment);
@@ -354,7 +358,7 @@ static int ChooseSegment(stream_t *s, const int current)
     hls_stream_t *hls = hls_Get(p_sys->hls_stream, current);
     if (hls == NULL) return 0;
 
-    /* Choose a segment to start which is no closer then
+    /* Choose a segment to start which is no closer than
      * 3 times the target duration from the end of the playlist.
      */
     int wanted = 0;
@@ -370,7 +374,7 @@ static int ChooseSegment(stream_t *s, const int current)
 
         if (segment->duration > hls->duration)
         {
-            msg_Err(s, "EXTINF:%d duration is larger then EXT-X-TARGETDURATION:%d",
+            msg_Err(s, "EXTINF:%d duration is larger than EXT-X-TARGETDURATION:%d",
                     segment->duration, hls->duration);
         }
 
@@ -499,7 +503,7 @@ static char *ConstructUrl(vlc_url_t *url)
     return psz_url;
 }
 
-static int parse_SegmentInformation(stream_t *s, hls_stream_t *hls, char *p_read, const char *uri)
+static int parse_SegmentInformation(hls_stream_t *hls, char *p_read, int *duration)
 {
     assert(hls);
     assert(p_read);
@@ -514,10 +518,42 @@ static int parse_SegmentInformation(stream_t *s, hls_stream_t *hls, char *p_read
     token = strtok_r(NULL, ",", &p_next);
     if (token == NULL)
         return VLC_EGENERIC;
-    int duration = atoi(token);
+
+    int value;
+    if (hls->version < 3)
+    {
+       value = strtol(token, NULL, 10);
+       if (errno == ERANGE)
+       {
+           *duration = -1;
+           return VLC_EGENERIC;
+       }
+       *duration = value;
+    }
+    else
+    {
+        double d = strtod(token, (char **) NULL);
+        if (errno == ERANGE)
+        {
+            *duration = -1;
+            return VLC_EGENERIC;
+        }
+        if ((d) - ((int)d) >= 0.5)
+            value = ((int)d) + 1;
+        else
+            value = ((int)d);
+    }
 
     /* 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)
+{
+    assert(hls);
+    assert(uri);
+
     /* Store segment information */
     char *psz_path = NULL;
     if (hls->url.psz_path != NULL)
@@ -590,7 +626,7 @@ static int parse_StreamInformation(stream_t *s, vlc_array_t **hls_stream,
         return VLC_EGENERIC;
     }
 
-    msg_Info(s, "bandwidth adaption detected (program-id=%d, bandwidth=%"PRIu64").", id, bw);
+    msg_Info(s, "bandwidth adaptation detected (program-id=%d, bandwidth=%"PRIu64").", id, bw);
 
     char *psz_uri = relative_URI(s, uri, NULL);
 
@@ -815,7 +851,7 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, const
 
                     /* Download playlist file from server */
                     uint8_t *buf = NULL;
-                    ssize_t len = read_M3U8(s, &hls->url, &buf);
+                    ssize_t len = read_M3U8_from_url(s, &hls->url, &buf);
                     if (len < 0)
                         err = VLC_EGENERIC;
                     else
@@ -878,6 +914,7 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, const
         assert(hls);
 
         /* */
+        int segment_duration = -1;
         do
         {
             /* Next line */
@@ -887,17 +924,7 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, const
             p_begin = p_read;
 
             if (strncmp(line, "#EXTINF", 7) == 0)
-            {
-                char *uri = ReadLine(p_begin, &p_read, p_end - p_begin);
-                if (uri == NULL)
-                    err = VLC_EGENERIC;
-                else
-                {
-                    err = parse_SegmentInformation(s, hls, line, uri);
-                    free(uri);
-                }
-                p_begin = p_read;
-            }
+                err = parse_SegmentInformation(hls, line, &segment_duration);
             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)
@@ -914,6 +941,11 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, const
                 err = parse_Version(s, hls, line);
             else if (strncmp(line, "#EXT-X-ENDLIST", 14) == 0)
                 err = parse_EndList(s, hls);
+            else if ((strncmp(line, "#", 1) != 0) && (*line != '\0') )
+            {
+                err = parse_AddSegment(s, hls, segment_duration, line);
+                segment_duration = -1; /* reset duration */
+            }
 
             free(line);
             line = NULL;
@@ -936,7 +968,7 @@ static int get_HTTPLiveMetaPlaylist(stream_t *s, vlc_array_t **streams)
 
     /* Download new playlist file from server */
     uint8_t *buffer = NULL;
-    ssize_t len = read_M3U8(s, &p_sys->m3u8, &buffer);
+    ssize_t len = read_M3U8_from_url(s, &p_sys->m3u8, &buffer);
     if (len < 0)
         return VLC_EGENERIC;
 
@@ -1109,7 +1141,7 @@ static int Download(stream_t *s, hls_stream_t *hls, segment_t *segment, int *cur
         int estimated = (int)(size / p_sys->bandwidth);
         if (estimated > segment->duration)
         {
-            msg_Warn(s,"downloading of segment %d takes %ds, which is longer then its playback (%ds)",
+            msg_Warn(s,"downloading of segment %d takes %ds, which is longer than its playback (%ds)",
                         segment->sequence, estimated, segment->duration);
         }
     }
@@ -1176,10 +1208,11 @@ static void* hls_Thread(void *p_this)
                     (p_sys->download.segment >= count)) &&
                    (p_sys->download.seek == -1))
             {
+                vlc_cond_wait(&p_sys->download.wait, &p_sys->download.lock_wait);
                 if (p_sys->b_live /*&& (mdate() >= p_sys->playlist.wakeup)*/)
                     break;
-                vlc_cond_wait(&p_sys->download.wait, &p_sys->download.lock_wait);
-                if (!vlc_object_alive(s)) break;
+                if (!vlc_object_alive(s))
+                    break;
             }
             /* */
             if (p_sys->download.seek >= 0)
@@ -1382,79 +1415,64 @@ static int hls_Download(stream_t *s, segment_t *segment)
 }
 
 /* Read M3U8 file */
-static ssize_t read_M3U8(stream_t *s, vlc_url_t *url, uint8_t **buffer)
+static ssize_t read_M3U8_from_stream(stream_t *s, uint8_t **buffer)
 {
-    assert(*buffer == NULL);
+    int64_t total_bytes = 0;
+    int64_t total_allocated = 0;
+    uint8_t *p = NULL;
 
-    /* Construct URL */
-    char *psz_url = ConstructUrl(url);
-    if (psz_url == NULL)
-           return VLC_ENOMEM;
+    while (1)
+    {
+        char buf[4096];
+        int64_t bytes;
 
-    stream_t *p_m3u8 = stream_UrlNew(s, psz_url);
-    free(psz_url);
-    if (p_m3u8 == NULL)
-        return VLC_EGENERIC;
+        bytes = stream_Read(s, buf, sizeof(buf));
+        if (bytes == 0)
+            break;      /* EOF ? */
+        else if (bytes < 0)
+            return bytes;
 
-    int64_t size = stream_Size(p_m3u8);
-    if (size == 0) size = 8192; /* no Content-Length */
+        if ( (total_bytes + bytes + 1) > total_allocated )
+        {
+            if (total_allocated)
+                total_allocated *= 2;
+            else
+                total_allocated = __MIN(bytes+1, sizeof(buf));
 
-    *buffer = calloc(1, size);
-    if (*buffer == NULL)
-    {
-        stream_Delete(p_m3u8);
-        return VLC_ENOMEM;
+            p = realloc_or_free(p, total_allocated);
+            if (p == NULL)
+                return VLC_ENOMEM;
+        }
+
+        memcpy(p+total_bytes, buf, bytes);
+        total_bytes += bytes;
     }
 
-    int64_t len = 0, curlen = 0;
-    do {
-        int read = ((size - curlen) >= INT_MAX) ? INT_MAX : (size - curlen);
-        len = stream_Read(p_m3u8, *buffer + curlen, read);
-        if (len <= 0)
-            break;
-        curlen += len;
-        if (curlen >= size)
-        {
-            uint8_t *tmp = realloc(*buffer, size + 8192);
-            if (tmp == NULL)
-                break;
-            size += 8192;
-            *buffer = tmp;
-        }
-    } while (vlc_object_alive(s));
-    stream_Delete(p_m3u8);
+    if (total_allocated == 0)
+        return VLC_EGENERIC;
 
-    (*buffer)[curlen-1] = '\0';
-    return size;
+    p[total_bytes] = '\0';
+    *buffer = p;
+
+    return total_bytes;
 }
 
-static ssize_t ReadM3U8(stream_t *s, uint8_t **buffer)
+static ssize_t read_M3U8_from_url(stream_t *s, vlc_url_t *url, uint8_t **buffer)
 {
     assert(*buffer == NULL);
 
-    int64_t size = stream_Size(s->p_source);
-    if (size == 0) size = 1024; /* no Content-Length */
+    /* Construct URL */
+    char *psz_url = ConstructUrl(url);
+    if (psz_url == NULL)
+           return VLC_ENOMEM;
 
-    *buffer = calloc(1, size);
-    if (*buffer == NULL)
-        return VLC_ENOMEM;
+    stream_t *p_m3u8 = stream_UrlNew(s, psz_url);
+    free(psz_url);
+    if (p_m3u8 == NULL)
+        return VLC_EGENERIC;
 
-    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));
+    ssize_t size = read_M3U8_from_stream(p_m3u8, buffer);
+    stream_Delete(p_m3u8);
 
     return size;
 }
@@ -1537,7 +1555,7 @@ static int Open(vlc_object_t *p_this)
 
     /* Parse HLS m3u8 content. */
     uint8_t *buffer = NULL;
-    ssize_t len = ReadM3U8(s, &buffer);
+    ssize_t len = read_M3U8_from_stream(s->p_source, &buffer);
     if (len < 0)
         goto fail;
     if (parse_M3U8(s, p_sys->hls_stream, buffer, len) != VLC_SUCCESS)
@@ -1553,7 +1571,7 @@ static int Open(vlc_object_t *p_this)
 
     if (p_sys->b_live && (p_sys->playback.segment < 0))
     {
-        msg_Warn(s, "less data then 3 times 'target duration' available for live playback, playback may stall");
+        msg_Warn(s, "less data than 3 times 'target duration' available for live playback, playback may stall");
     }
 
     if (Prefetch(s, &current) != VLC_SUCCESS)
@@ -1646,6 +1664,8 @@ static void Close(vlc_object_t *p_this)
 
     /* */
     vlc_UrlClean(&p_sys->m3u8);
+    if (p_sys->peeked)
+        block_Release (p_sys->peeked);
     free(p_sys);
 }
 
@@ -1834,10 +1854,9 @@ static int Read(stream_t *s, void *buffer, unsigned int i_read)
 static int Peek(stream_t *s, const uint8_t **pp_peek, unsigned int i_peek)
 {
     stream_sys_t *p_sys = s->p_sys;
-    size_t curlen = 0;
     segment_t *segment;
+    unsigned int len = i_peek;
 
-again:
     segment = GetSegment(s);
     if (segment == NULL)
     {
@@ -1848,29 +1867,77 @@ again:
 
     vlc_mutex_lock(&segment->lock);
 
-    /* remember segment to peek */
-    int peek_segment = p_sys->playback.segment;
-    do
+    size_t i_buff = segment->data->i_buffer;
+    uint8_t *p_buff = segment->data->p_buffer;
+
+    if (i_peek < i_buff)
     {
-        if (i_peek < segment->data->i_buffer)
-        {
-            *pp_peek = segment->data->p_buffer;
-            curlen += i_peek;
-        }
-        else
+        *pp_peek = p_buff;
+        vlc_mutex_unlock(&segment->lock);
+        return i_peek;
+    }
+
+    else /* This will seldom be run */
+    {
+        /* remember segment to read */
+        int peek_segment = p_sys->playback.segment;
+        size_t curlen = 0;
+        segment_t *nsegment;
+        p_sys->playback.segment++;
+        block_t *peeked = p_sys->peeked;
+
+        if (peeked == NULL)
+            peeked = block_Alloc (i_peek);
+        else if (peeked->i_buffer < i_peek)
+            peeked = block_Realloc (peeked, 0, i_peek);
+        if (peeked == NULL)
+            return 0;
+
+        memcpy(peeked->p_buffer, p_buff, i_buff);
+        curlen = i_buff;
+        len -= i_buff;
+        vlc_mutex_unlock(&segment->lock);
+
+        i_buff = peeked->i_buffer;
+        p_buff = peeked->p_buffer;
+        *pp_peek = p_buff;
+
+        while ((curlen < i_peek) && vlc_object_alive(s))
         {
-            p_sys->playback.segment++;
-            vlc_mutex_unlock(&segment->lock);
-            goto again;
-        }
-    } while ((curlen < i_peek) && vlc_object_alive(s));
+            nsegment = GetSegment(s);
+            if (nsegment == NULL)
+            {
+                msg_Err(s, "segment %d should have been available (stream %d)",
+                        p_sys->playback.segment, p_sys->playback.stream);
+                /* restore segment to read */
+                p_sys->playback.segment = peek_segment;
+                return curlen; /* eof? */
+            }
 
-    /* restore segment to read */
-    p_sys->playback.segment = peek_segment;
+            vlc_mutex_lock(&nsegment->lock);
 
-    vlc_mutex_unlock(&segment->lock);
+            if (len < nsegment->data->i_buffer)
+            {
+                memcpy(p_buff + curlen, nsegment->data->p_buffer, len);
+                curlen += len;
+            }
+            else
+            {
+                size_t i_nbuff = nsegment->data->i_buffer;
+                memcpy(p_buff + curlen, nsegment->data->p_buffer, i_nbuff);
+                curlen += i_nbuff;
+                len -= i_nbuff;
+
+                p_sys->playback.segment++;
+            }
 
-    return curlen;
+            vlc_mutex_unlock(&nsegment->lock);
+        }
+
+        /* restore segment to read */
+        p_sys->playback.segment = peek_segment;
+        return curlen;
+    }
 }
 
 static bool hls_MaySeek(stream_t *s)