]> git.sesse.net Git - vlc/blobdiff - modules/stream_filter/httplive.c
Remove constant { true } aout_instance_t.mixer_allocation
[vlc] / modules / stream_filter / httplive.c
index 5da0bf69e266f32915f8f950a3165a9fdbff590f..5f07c0db6c59a6a2427460fed8f4d32a43f571d6 100644 (file)
@@ -29,6 +29,7 @@
 #endif
 
 #include <limits.h>
+#include <errno.h>
 
 #include <vlc_common.h>
 #include <vlc_plugin.h>
@@ -136,7 +137,7 @@ 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 char *ReadLine(uint8_t *buffer, uint8_t **remain, size_t len);
+static char *ReadLine(uint8_t *buffer, uint8_t **pos, size_t len);
 
 static int hls_Download(stream_t *s, segment_t *segment);
 
@@ -193,7 +194,7 @@ static bool isHTTPLiveStreaming(stream_t *s)
 }
 
 /* HTTP Live Streaming */
-static hls_stream_t *hls_New(vlc_array_t *hls_stream, int id, uint64_t bw, char *uri)
+static hls_stream_t *hls_New(vlc_array_t *hls_stream, const int id, const uint64_t bw, const char *uri)
 {
     hls_stream_t *hls = (hls_stream_t *)malloc(sizeof(hls_stream_t));
     if (hls == NULL) return NULL;
@@ -231,7 +232,7 @@ static void hls_Free(hls_stream_t *hls)
     hls = NULL;
 }
 
-static hls_stream_t *hls_Get(vlc_array_t *hls_stream, int wanted)
+static hls_stream_t *hls_Get(vlc_array_t *hls_stream, const int wanted)
 {
     int count = vlc_array_count(hls_stream);
     if (count <= 0)
@@ -292,7 +293,7 @@ static uint64_t hls_GetStreamSize(hls_stream_t *hls)
 }
 
 /* Segment */
-static segment_t *segment_New(hls_stream_t* hls, int duration, char *uri)
+static segment_t *segment_New(hls_stream_t* hls, const int duration, const char *uri)
 {
     segment_t *segment = (segment_t *)malloc(sizeof(segment_t));
     if (segment == NULL)
@@ -320,7 +321,7 @@ static void segment_Free(segment_t *segment)
     segment = NULL;
 }
 
-static segment_t *segment_GetSegment(hls_stream_t *hls, int wanted)
+static segment_t *segment_GetSegment(hls_stream_t *hls, const int wanted)
 {
     assert(hls);
 
@@ -332,7 +333,7 @@ static segment_t *segment_GetSegment(hls_stream_t *hls, int wanted)
     return (segment_t *) vlc_array_item_at_index(hls->segments, wanted);
 }
 
-static segment_t *segment_Find(hls_stream_t *hls, int sequence)
+static segment_t *segment_Find(hls_stream_t *hls, const int sequence)
 {
     assert(hls);
 
@@ -348,7 +349,7 @@ static segment_t *segment_Find(hls_stream_t *hls, int sequence)
     return NULL;
 }
 
-static int ChooseSegment(stream_t *s, int current)
+static int ChooseSegment(stream_t *s, const int current)
 {
     stream_sys_t *p_sys = (stream_sys_t *)s->p_sys;
     hls_stream_t *hls = hls_Get(p_sys->hls_stream, current);
@@ -359,6 +360,7 @@ static int ChooseSegment(stream_t *s, int current)
      */
     int wanted = 0;
     int duration = 0;
+    int sequence = 0;
     int count = vlc_array_count(hls->segments);
     int i = p_sys->b_live ? count - 1 : 0;
 
@@ -378,6 +380,7 @@ static int ChooseSegment(stream_t *s, int current)
         {
             /* Start point found */
             wanted = p_sys->b_live ? i : 0;
+            sequence = segment->sequence;
             break;
         }
 
@@ -387,7 +390,7 @@ static int ChooseSegment(stream_t *s, int current)
           i++;
     }
 
-    msg_Info(s, "Choose segment %d/%d", wanted, count);
+    msg_Info(s, "Choose segment %d/%d (sequence=%d)", wanted, count, sequence);
     return wanted;
 }
 
@@ -497,7 +500,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, char *uri)
+static int parse_SegmentInformation(hls_stream_t *hls, char *p_read, int *duration)
 {
     assert(hls);
     assert(p_read);
@@ -512,10 +515,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)
@@ -556,7 +591,7 @@ static int parse_TargetDuration(stream_t *s, hls_stream_t *hls, char *p_read)
 }
 
 static int parse_StreamInformation(stream_t *s, vlc_array_t **hls_stream,
-                                   hls_stream_t **hls, char *p_read, char *uri)
+                                   hls_stream_t **hls, char *p_read, const char *uri)
 {
     int id;
     uint64_t bw;
@@ -612,7 +647,8 @@ static int parse_MediaSequence(stream_t *s, hls_stream_t *hls, char *p_read)
     }
 
     if (hls->sequence > 0)
-        msg_Err(s, "EXT-X-MEDIA-SEQUENCE already present in playlist");
+        msg_Err(s, "EXT-X-MEDIA-SEQUENCE already present in playlist (new=%d, old=%d)",
+                    sequence, hls->sequence);
 
     hls->sequence = sequence;
     return VLC_SUCCESS;
@@ -731,7 +767,7 @@ static int parse_Discontinuity(stream_t *s, hls_stream_t *hls, char *p_read)
  * ALLOW-CACHE, EXT-X-STREAM-INF, EXT-X-ENDLIST, EXT-X-DISCONTINUITY,
  * and EXT-X-VERSION.
  */
-static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, ssize_t len)
+static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, const ssize_t len)
 {
     stream_sys_t *p_sys = s->p_sys;
     uint8_t *p_read, *p_begin, *p_end;
@@ -746,6 +782,7 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, ssize_
     char *line = ReadLine(p_begin, &p_read, p_end - p_begin);
     if (line == NULL)
         return VLC_ENOMEM;
+    p_begin = p_read;
 
     if (strncmp(line, "#EXTM3U", 7) != 0)
     {
@@ -782,27 +819,21 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, ssize_
     /* Is it a meta index file ? */
     bool b_meta = (strstr((const char *)buffer, "#EXT-X-STREAM-INF") == NULL) ? false : true;
 
-    if (b_meta)
-        msg_Info(s, "Meta playlist");
-    else
-        msg_Info(s, "%s Playlist HLS protocol version: %d", p_sys->b_live ? "Live": "VOD", version);
-
-    /* */
     int err = VLC_SUCCESS;
-    do
-    {
-        /* Next line */
-        p_begin = p_read;
-        line = ReadLine(p_begin, &p_read, p_end - p_begin);
-        if (line == NULL)
-            break;
 
-        /* */
-        p_begin = p_read;
+    if (b_meta)
+    {
+        msg_Info(s, "Meta playlist");
 
         /* M3U8 Meta Index file */
-        if (b_meta)
-        {
+        do {
+            /* Next line */
+            line = ReadLine(p_begin, &p_read, p_end - p_begin);
+            if (line == NULL)
+                break;
+            p_begin = p_read;
+
+            /* */
             if (strncmp(line, "#EXT-X-STREAM-INF", 17) == 0)
             {
                 p_sys->b_meta = true;
@@ -834,51 +865,63 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, ssize_
                             hls->size = hls_GetStreamSize(hls); /* Stream size (approximate) */
                     }
                 }
+                p_begin = p_read;
             }
-        }
+
+            free(line);
+            line = NULL;
+
+            if (p_begin >= p_end)
+                break;
+
+        } while ((err == VLC_SUCCESS) && vlc_object_alive(s));
+
+    }
+    else
+    {
+        msg_Info(s, "%s Playlist HLS protocol version: %d", p_sys->b_live ? "Live": "VOD", version);
+
+        hls_stream_t *hls = NULL;
+        if (p_sys->b_meta)
+            hls = hls_GetLast(streams);
         else
         {
-            hls_stream_t *hls = hls_GetLast(streams);
-            if (hls == NULL)
+            /* No Meta playlist used */
+            hls = hls_New(streams, 0, -1, NULL);
+            if (hls)
             {
-                /* No Meta playlist used */
-                hls = hls_New(streams, 0, -1, NULL);
-                if (hls == NULL)
-                {
-                    msg_Err(s, "No HLS structure created");
-                    err = VLC_ENOMEM;
-                    break;
-                }
-
                 /* Get TARGET-DURATION first */
                 p = (uint8_t *)strstr((const char *)buffer, "#EXT-X-TARGETDURATION:");
                 if (p)
                 {
                     uint8_t *p_rest = NULL;
                     char *psz_duration = ReadLine(p, &p_rest,  p_end - p);
-                    if (psz_duration)
-                    {
-                          err = parse_TargetDuration(s, hls, psz_duration);
-                          free(psz_duration);
-                          p = NULL;
-                    }
+                    if (psz_duration == NULL)
+                        return VLC_EGENERIC;
+                    err = parse_TargetDuration(s, hls, psz_duration);
+                    free(psz_duration);
+                    p = NULL;
                 }
 
+                /* Store version */
                 hls->version = version;
             }
+            else return VLC_ENOMEM;
+        }
+        assert(hls);
+
+        /* */
+        int segment_duration = -1;
+        do
+        {
+            /* Next line */
+            line = ReadLine(p_begin, &p_read, p_end - p_begin);
+            if (line == NULL)
+                break;
+            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)
@@ -895,17 +938,22 @@ static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, ssize_
                 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)
+            {
+                err = parse_AddSegment(s, hls, segment_duration, line);
+                segment_duration = -1; /* reset duration */
+            }
 
-        free(line);
-        line = NULL;
+            free(line);
+            line = NULL;
 
-        if (p_begin >= p_end)
-            break;
+            if (p_begin >= p_end)
+                break;
 
-    } while ((err == VLC_SUCCESS) && vlc_object_alive(s));
+        } while ((err == VLC_SUCCESS) && vlc_object_alive(s));
 
-    free(line);
+        free(line);
+    }
 
     return err;
 }
@@ -949,11 +997,18 @@ static int hls_UpdatePlaylist(stream_t *s, hls_stream_t *hls_new, hls_stream_t *
                 (p->duration != segment->duration) ||
                 (strcmp(p->url.psz_path, segment->url.psz_path) != 0))
             {
-                msg_Err(s, "existing segment %d found with different content",
-                        p->sequence);
+                msg_Err(s, "existing segment found with different content - resetting");
                 msg_Err(s, "- sequence: new=%d, old=%d", p->sequence, segment->sequence);
                 msg_Err(s, "- duration: new=%d, old=%d", p->duration, segment->duration);
-                msg_Err(s, "- file: new=%s, old=%s", p->url.psz_path, segment->url.psz_path);
+                msg_Err(s, "- file: new=%s", p->url.psz_path);
+                msg_Err(s, "        old=%s", segment->url.psz_path);
+
+                /* Resetting content */
+                segment->sequence = p->sequence;
+                segment->duration = p->duration;
+                vlc_UrlClean(&segment->url);
+                vlc_UrlParse(&segment->url, p->url.psz_buffer, 0);
+                segment_Free(p);
             }
         }
         else
@@ -962,17 +1017,13 @@ static int hls_UpdatePlaylist(stream_t *s, hls_stream_t *hls_new, hls_stream_t *
             segment_t *l = segment_GetSegment(*hls, last);
             if (l == NULL) goto fail_and_unlock;
 
-            if ((l->sequence + 1) == p->sequence)
-            {
-                vlc_array_append((*hls)->segments, p);
-                msg_Info(s, "- segment %d appended", p->sequence);
-            }
-            else /* there is a gap */
+            if ((l->sequence + 1) != p->sequence)
             {
-                msg_Err(s, "gap in sequence numbers found: new=%d expected old=%d",
-                        p->sequence, l->sequence);
-                goto fail_and_unlock;
+                msg_Err(s, "gap in sequence numbers found: new=%d expected %d",
+                        p->sequence, l->sequence+1);
             }
+            vlc_array_append((*hls)->segments, p);
+            msg_Info(s, "- segment %d appended", p->sequence);
         }
         vlc_mutex_unlock(&(*hls)->lock);
     }
@@ -1402,6 +1453,7 @@ static ssize_t read_M3U8(stream_t *s, vlc_url_t *url, uint8_t **buffer)
     } while (vlc_object_alive(s));
     stream_Delete(p_m3u8);
 
+    (*buffer)[curlen-1] = '\0';
     return size;
 }
 
@@ -1436,7 +1488,7 @@ static ssize_t ReadM3U8(stream_t *s, uint8_t **buffer)
     return size;
 }
 
-static char *ReadLine(uint8_t *buffer, uint8_t **remain, size_t len)
+static char *ReadLine(uint8_t *buffer, uint8_t **pos, const size_t len)
 {
     assert(buffer);
 
@@ -1447,17 +1499,22 @@ static char *ReadLine(uint8_t *buffer, uint8_t **remain, size_t len)
 
     while (p < end)
     {
-        if (*p == '\n')
+        if ((*p == '\n') || (*p == '\0'))
             break;
         p++;
     }
 
-    /* copy line excluding \n */
+    /* copy line excluding \n or \0 */
     line = strndup((char *)begin, p - begin);
 
-    /* next pass start after \n */
-    p++;
-    *remain = p;
+    if (*p == '\0')
+        *pos = end;
+    else
+    {
+        /* next pass start after \n */
+        p++;
+        *pos = p;
+    }
 
     return line;
 }
@@ -1886,7 +1943,7 @@ static uint64_t GetStreamSize(stream_t *s)
     return size;
 }
 
-static int segment_Seek(stream_t *s, uint64_t pos)
+static int segment_Seek(stream_t *s, const uint64_t pos)
 {
     stream_sys_t *p_sys = s->p_sys;