]> git.sesse.net Git - vlc/blobdiff - modules/stream_filter/httplive.c
stream_filter/httplive.c: Cleanup hls_thread_t and stream_sys_t structs.
[vlc] / modules / stream_filter / httplive.c
index 4048a951f43b603f80f4629e710f17cb5030d8c4..27d027a11528c796632fe2c09f824a69e338dda7 100644 (file)
@@ -90,13 +90,6 @@ typedef struct
     VLC_COMMON_MEMBERS
 
     /* */
-    int         current;    /* current hls_stream  */
-    int         segment;    /* current segment for downloading */
-    int         seek;       /* segment requested by seek (default -1) */
-    vlc_mutex_t lock_wait;  /* protect segment download counter */
-    vlc_cond_t  wait;       /* some condition to wait on */
-    vlc_array_t *hls_stream;/* bandwidth adaptation */
-
     stream_t    *s;
 } hls_thread_t;
 
@@ -110,8 +103,18 @@ struct stream_sys_t
     vlc_array_t  *hls_stream;/* bandwidth adaptation */
     uint64_t      bandwidth; /* measured bandwidth (bits per second) */
 
+    /* Download */
+    struct hls_download_s
+    {
+        int         current;    /* current hls_stream  */
+        int         segment;    /* current segment for downloading */
+        int         seek;       /* segment requested by seek (default -1) */
+        vlc_mutex_t lock_wait;  /* protect segment download counter */
+        vlc_cond_t  wait;       /* some condition to wait on */
+    } download;
+
     /* Playback */
-    struct hls_playback_t
+    struct hls_playback_s
     {
         uint64_t    offset;     /* current offset in media */
         int         current;    /* current hls_stream  */
@@ -447,10 +450,8 @@ static void parse_SegmentInformation(stream_t *s, hls_stream_t *hls, char *p_rea
     free(psz_uri);
 }
 
-static void parse_TargetDuration(stream_t *s, hls_stream_t *hls, char *p_read)
+static int parse_TargetDuration(stream_t *s, hls_stream_t *hls, char *p_read)
 {
-    stream_sys_t *p_sys = s->p_sys;
-
     assert(hls);
 
     int duration = -1;
@@ -458,11 +459,11 @@ static void parse_TargetDuration(stream_t *s, hls_stream_t *hls, char *p_read)
     if (ret != 1)
     {
         msg_Err(s, "expected #EXT-X-TARGETDURATION:<s>");
-        p_sys->b_error = true;
-        return;
+        return VLC_EGENERIC;
     }
 
     hls->duration = duration; /* seconds */
+    return VLC_SUCCESS;
 }
 
 static void parse_StreamInformation(stream_t *s, vlc_array_t **hls_stream,
@@ -512,10 +513,8 @@ static void parse_StreamInformation(stream_t *s, vlc_array_t **hls_stream,
     free(psz_uri);
 }
 
-static void parse_MediaSequence(stream_t *s, hls_stream_t *hls, char *p_read)
+static int parse_MediaSequence(stream_t *s, hls_stream_t *hls, char *p_read)
 {
-    stream_sys_t *p_sys = s->p_sys;
-
     assert(hls);
 
     int sequence;
@@ -523,38 +522,37 @@ static void parse_MediaSequence(stream_t *s, hls_stream_t *hls, char *p_read)
     if (ret != 1)
     {
         msg_Err(s, "expected #EXT-X-MEDIA-SEQUENCE:<s>");
-        p_sys->b_error = true;
-        return;
+        return VLC_EGENERIC;
     }
 
     if (hls->sequence > 0)
         msg_Err(s, "EXT-X-MEDIA-SEQUENCE already present in playlist");
 
-    msg_Info(s, "#EXT-X-MEDIA-SEQUENCE:%d", sequence);
     hls->sequence = sequence;
+    return VLC_SUCCESS;
 }
 
-static void parse_Key(stream_t *s, hls_stream_t *hls, char *p_read)
+static int parse_Key(stream_t *s, hls_stream_t *hls, char *p_read)
 {
-    stream_sys_t *p_sys = s->p_sys;
-
     assert(hls);
 
     /* #EXT-X-KEY:METHOD=<method>[,URI="<URI>"][,IV=<IV>] */
-    char *attr;
-    attr = parse_Attributes(p_read, "METHOD");
+    int err = VLC_SUCCESS;
+    char *attr = parse_Attributes(p_read, "METHOD");
     if (attr == NULL)
     {
         msg_Err(s, "#EXT-X-KEY: expected METHOD=<value>");
-        p_sys->b_error = true;
+        return err;
     }
-    else if (strncasecmp(attr, "NONE", 4) == 0)
+
+    if (strncasecmp(attr, "NONE", 4) == 0)
     {
+
         char *uri = parse_Attributes(p_read, "URI");
         if (uri != NULL)
         {
             msg_Err(s, "#EXT-X-KEY: URI not expected");
-            p_sys->b_error = true;
+            err = VLC_EGENERIC;
         }
         free(uri);
         /* IV is only supported in version 2 and above */
@@ -564,7 +562,7 @@ static void parse_Key(stream_t *s, hls_stream_t *hls, char *p_read)
             if (iv != NULL)
             {
                 msg_Err(s, "#EXT-X-KEY: IV not expected");
-                p_sys->b_error = true;
+                err = VLC_EGENERIC;
             }
             free(iv);
         }
@@ -572,21 +570,21 @@ static void parse_Key(stream_t *s, hls_stream_t *hls, char *p_read)
     else
     {
         msg_Warn(s, "playback of encrypted HTTP Live media is not supported.");
-        p_sys->b_error = true;
+        err = VLC_EGENERIC;
     }
     free(attr);
+    return err;
 }
 
-static void parse_ProgramDateTime(stream_t *s, hls_stream_t *hls, char *p_read)
+static int parse_ProgramDateTime(stream_t *s, hls_stream_t *hls, char *p_read)
 {
     VLC_UNUSED(hls);
     msg_Dbg(s, "tag not supported: #EXT-X-PROGRAM-DATE-TIME %s", p_read);
+    return VLC_SUCCESS;
 }
 
-static void parse_AllowCache(stream_t *s, hls_stream_t *hls, char *p_read)
+static int parse_AllowCache(stream_t *s, hls_stream_t *hls, char *p_read)
 {
-    stream_sys_t *p_sys = s->p_sys;
-
     assert(hls);
 
     char answer[4] = "\0";
@@ -594,17 +592,15 @@ static void parse_AllowCache(stream_t *s, hls_stream_t *hls, char *p_read)
     if (ret != 1)
     {
         msg_Err(s, "#EXT-X-ALLOW-CACHE, ignoring ...");
-        p_sys->b_error = true;
-        return;
+        return VLC_EGENERIC;
     }
 
     hls->b_cache = (strncmp(answer, "NO", 2) != 0);
+    return VLC_SUCCESS;
 }
 
-static void parse_Version(stream_t *s, hls_stream_t *hls, char *p_read)
+static int parse_Version(stream_t *s, hls_stream_t *hls, char *p_read)
 {
-    stream_sys_t *p_sys = s->p_sys;
-
     assert(hls);
 
     int version;
@@ -612,8 +608,7 @@ static void parse_Version(stream_t *s, hls_stream_t *hls, char *p_read)
     if (ret != 1)
     {
         msg_Err(s, "#EXT-X-VERSION: no protocol version found, should be version 1.");
-        p_sys->b_error = true;
-        return;
+        return VLC_EGENERIC;
     }
 
     /* Check version */
@@ -621,48 +616,53 @@ static void parse_Version(stream_t *s, hls_stream_t *hls, char *p_read)
     if (hls->version != 1)
     {
         msg_Err(s, "#EXT-X-VERSION should be version 1 iso %d", version);
-        p_sys->b_error = true;
+        return VLC_EGENERIC;
     }
+    return VLC_SUCCESS;
 }
 
-static void parse_EndList(stream_t *s, hls_stream_t *hls)
+static int parse_EndList(stream_t *s, hls_stream_t *hls)
 {
-    stream_sys_t *p_sys = s->p_sys;
-
     assert(hls);
 
-    p_sys->b_live = false;
+    s->p_sys->b_live = false;
     msg_Info(s, "video on demand (vod) mode");
+    return VLC_SUCCESS;
 }
 
-static void parse_Discontinuity(stream_t *s, hls_stream_t *hls, char *p_read)
+static int parse_Discontinuity(stream_t *s, hls_stream_t *hls, char *p_read)
 {
     assert(hls);
 
     /* FIXME: Do we need to act on discontinuity ?? */
     msg_Dbg(s, "#EXT-X-DISCONTINUITY %s", p_read);
+    return VLC_SUCCESS;
 }
 
 static void parse_M3U8ExtLine(stream_t *s, hls_stream_t *hls, char *line)
 {
     if (*line == '#')
     {
+        int err = VLC_SUCCESS;
         if (strncmp(line, "#EXT-X-TARGETDURATION", 21) == 0)
-            parse_TargetDuration(s, hls, line);
+            err = parse_TargetDuration(s, hls, line);
         else if (strncmp(line, "#EXT-X-MEDIA-SEQUENCE", 21) == 0)
-            parse_MediaSequence(s, hls, line);
+            err = parse_MediaSequence(s, hls, line);
         else if (strncmp(line, "#EXT-X-KEY", 10) == 0)
-            parse_Key(s, hls, line);
+            err = parse_Key(s, hls, line);
         else if (strncmp(line, "#EXT-X-PROGRAM-DATE-TIME", 24) == 0)
-            parse_ProgramDateTime(s, hls, line);
+            err = parse_ProgramDateTime(s, hls, line);
         else if (strncmp(line, "#EXT-X-ALLOW-CACHE", 18) == 0)
-            parse_AllowCache(s, hls, line);
+            err = parse_AllowCache(s, hls, line);
         else if (strncmp(line, "#EXT-X-DISCONTINUITY", 20) == 0)
-            parse_Discontinuity(s, hls, line);
+            err = parse_Discontinuity(s, hls, line);
         else if (strncmp(line, "#EXT-X-VERSION", 14) == 0)
-            parse_Version(s, hls, line);
+            err = parse_Version(s, hls, line);
         else if (strncmp(line, "#EXT-X-ENDLIST", 14) == 0)
-            parse_EndList(s, hls);
+            err = parse_EndList(s, hls);
+
+        if (err != VLC_SUCCESS)
+            s->p_sys->b_error = true;
     }
 }
 
@@ -1016,8 +1016,6 @@ 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;
 
-            msg_Info(s, "adding segment %d after %d",
-                     p->sequence, l->sequence);
             if ((l->sequence + 1) == p->sequence)
             {
                 vlc_array_append((*hls)->segments, p);
@@ -1025,7 +1023,7 @@ static int hls_UpdatePlaylist(stream_t *s, hls_stream_t *hls_new, hls_stream_t *
             }
             else /* there is a gap */
             {
-                msg_Err(s, "gap in sequence numbers found: new=%d, old=%d",
+                msg_Err(s, "gap in sequence numbers found: new=%d expected old=%d",
                         p->sequence, l->sequence);
                 goto fail_and_unlock;
             }
@@ -1071,7 +1069,6 @@ static int hls_ReloadPlaylist(stream_t *s)
     }
 
     /* merge playlists */
-    msg_Info(s, "Merging playlist");
     for (int n = 0; n < count; n++)
     {
         hls_stream_t *hls_new = hls_Get(hls_streams, n);
@@ -1081,7 +1078,7 @@ static int hls_ReloadPlaylist(stream_t *s)
         if (hls_old == NULL)
         {   /* new hls stream - append */
             vlc_array_append(p_sys->hls_stream, hls_new);
-            msg_Info(s, "New HLS stream appended (id=%d, bandwidth=%"PRIu64")",
+            msg_Info(s, "new HLS stream appended (id=%d, bandwidth=%"PRIu64")",
                      hls_new->id, hls_new->bandwidth);
         }
         else if (hls_UpdatePlaylist(s, hls_new, &hls_old) != VLC_SUCCESS)
@@ -1203,7 +1200,7 @@ static void* hls_Thread(vlc_object_t *p_this)
 
     while (vlc_object_alive(p_this))
     {
-        hls_stream_t *hls = hls_Get(client->hls_stream, client->current);
+        hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->download.current);
         assert(hls);
 
         /* Sliding window (~60 seconds worth of movie) */
@@ -1212,56 +1209,30 @@ static void* hls_Thread(vlc_object_t *p_this)
         vlc_mutex_unlock(&hls->lock);
 
         /* Is there a new segment to process? */
-        if ((p_sys->playback.segment < (count - 6)) ||
-            (client->segment >= count))
+        if ((!p_sys->b_live && (p_sys->playback.segment < (count - 6))) ||
+            (p_sys->download.segment >= count))
         {
             /* wait */
-            vlc_mutex_lock(&client->lock_wait);
-            while (((client->segment - p_sys->playback.segment > 6) ||
-                    (client->segment >= count)) &&
-                   (client->seek == -1))
+            vlc_mutex_lock(&p_sys->download.lock_wait);
+            while (((p_sys->download.segment - p_sys->playback.segment > 6) ||
+                    (p_sys->download.segment >= count)) &&
+                   (p_sys->download.seek == -1) &&
+                   (mdate() < p_sys->playlist.wakeup))
             {
-                vlc_cond_wait(&client->wait, &client->lock_wait);
+                vlc_cond_wait(&p_sys->download.wait, &p_sys->download.lock_wait);
                 if (!vlc_object_alive(p_this)) break;
             }
             /* */
-            if (client->seek >= 0)
+            if (p_sys->download.seek >= 0)
             {
-                client->segment = client->seek;
-                client->seek = -1;
+                p_sys->download.segment = p_sys->download.seek;
+                p_sys->download.seek = -1;
             }
-            vlc_mutex_unlock(&client->lock_wait);
+            vlc_mutex_unlock(&p_sys->download.lock_wait);
         }
 
         if (!vlc_object_alive(p_this)) break;
 
-        vlc_mutex_lock(&hls->lock);
-        segment_t *segment = segment_GetSegment(hls, client->segment);
-        assert(segment);
-        vlc_mutex_unlock(&hls->lock);
-
-        if (Download(client->s, hls, segment, &client->current) != VLC_SUCCESS)
-        {
-            if (!p_sys->b_live)
-            {
-                p_sys->b_error = true;
-                break;
-            }
-        }
-
-        /* download succeeded */
-        /* determine next segment to download */
-        vlc_mutex_lock(&client->lock_wait);
-        if (client->seek >= 0)
-        {
-            client->segment = client->seek;
-            client->seek = -1;
-        }
-        else if (client->segment < count)
-            client->segment++;
-        vlc_cond_signal(&client->wait);
-        vlc_mutex_unlock(&client->lock_wait);
-
         /* reload the m3u8 index file */
         if (p_sys->b_live)
         {
@@ -1284,7 +1255,38 @@ static void* hls_Thread(vlc_object_t *p_this)
                 p_sys->playlist.wakeup = now + ((mtime_t)(hls->duration * wait)
                                                 * (mtime_t)1000000);
             }
+
+            if (!vlc_object_alive(p_this)) break;
+        }
+
+        vlc_mutex_lock(&hls->lock);
+        segment_t *segment = segment_GetSegment(hls, p_sys->download.segment);
+        assert(segment);
+        vlc_mutex_unlock(&hls->lock);
+
+        if (Download(client->s, hls, segment, &p_sys->download.current) != VLC_SUCCESS)
+        {
+            if (!vlc_object_alive(p_this)) break;
+
+            if (!p_sys->b_live)
+            {
+                p_sys->b_error = true;
+                break;
+            }
+        }
+
+        /* download succeeded */
+        /* determine next segment to download */
+        vlc_mutex_lock(&p_sys->download.lock_wait);
+        if (p_sys->download.seek >= 0)
+        {
+            p_sys->download.segment = p_sys->download.seek;
+            p_sys->download.seek = -1;
         }
+        else if (p_sys->download.segment < count)
+            p_sys->download.segment++;
+        vlc_cond_signal(&p_sys->download.wait);
+        vlc_mutex_unlock(&p_sys->download.lock_wait);
     }
 
     vlc_restorecancel(canc);
@@ -1580,16 +1582,15 @@ static int Open(vlc_object_t *p_this)
                 ((mtime_t)hls->duration * UINT64_C(1000000));
     }
 
-    p_sys->thread->hls_stream = p_sys->hls_stream;
-    p_sys->thread->current = current;
+    p_sys->download.current = current;
     p_sys->playback.current = current;
-    p_sys->thread->segment = p_sys->playback.segment;
-    p_sys->thread->seek = -1;
+    p_sys->download.segment = p_sys->playback.segment;
+    p_sys->download.seek = -1;
     p_sys->playback.segment = 0; /* reset to first segment */
     p_sys->thread->s = s;
 
-    vlc_mutex_init(&p_sys->thread->lock_wait);
-    vlc_cond_init(&p_sys->thread->wait);
+    vlc_mutex_init(&p_sys->download.lock_wait);
+    vlc_cond_init(&p_sys->download.wait);
 
     if (vlc_thread_create(p_sys->thread, "HTTP Live Streaming client",
                           hls_Thread, VLC_THREAD_PRIORITY_INPUT))
@@ -1619,15 +1620,15 @@ static void Close(vlc_object_t *p_this)
     /* */
     if (p_sys->thread)
     {
-        vlc_mutex_lock(&p_sys->thread->lock_wait);
+        vlc_mutex_lock(&p_sys->download.lock_wait);
         vlc_object_kill(p_sys->thread);
-        vlc_cond_signal(&p_sys->thread->wait);
-        vlc_mutex_unlock(&p_sys->thread->lock_wait);
+        vlc_cond_signal(&p_sys->download.wait);
+        vlc_mutex_unlock(&p_sys->download.lock_wait);
 
         /* */
         vlc_thread_join(p_sys->thread);
-        vlc_mutex_destroy(&p_sys->thread->lock_wait);
-        vlc_cond_destroy(&p_sys->thread->wait);
+        vlc_mutex_destroy(&p_sys->download.lock_wait);
+        vlc_cond_destroy(&p_sys->download.wait);
         vlc_object_release(p_sys->thread);
     }
 
@@ -1666,7 +1667,7 @@ static segment_t *GetSegment(stream_t *s)
             {
                 p_sys->b_cache = hls->b_cache;
                 vlc_mutex_unlock(&hls->lock);
-                return segment;
+                goto check;
             }
         }
         vlc_mutex_unlock(&hls->lock);
@@ -1690,9 +1691,9 @@ static segment_t *GetSegment(stream_t *s)
             break;
         }
 
-        vlc_mutex_lock(&p_sys->thread->lock_wait);
-        int i_segment = p_sys->thread->segment;
-        vlc_mutex_unlock(&p_sys->thread->lock_wait);
+        vlc_mutex_lock(&p_sys->download.lock_wait);
+        int i_segment = p_sys->download.segment;
+        vlc_mutex_unlock(&p_sys->download.lock_wait);
 
         /* This segment is ready? */
         if ((segment->data != NULL) &&
@@ -1701,7 +1702,7 @@ static segment_t *GetSegment(stream_t *s)
             p_sys->playback.current = i_stream;
             p_sys->b_cache = hls->b_cache;
             vlc_mutex_unlock(&hls->lock);
-            return segment;
+            goto check;
         }
         vlc_mutex_unlock(&hls->lock);
 
@@ -1715,6 +1716,14 @@ static segment_t *GetSegment(stream_t *s)
     }
     /* */
     return NULL;
+
+check:
+    /* sanity check */
+    if (p_sys->download.segment - p_sys->playback.segment == 0)
+        msg_Err(s, "playback will stall");
+    else if (p_sys->download.segment - p_sys->playback.segment < 3)
+        msg_Warn(s, "playback in danger of stalling");
+    return segment;
 }
 
 static ssize_t hls_Read(stream_t *s, uint8_t *p_read, unsigned int i_read)
@@ -1752,15 +1761,15 @@ static ssize_t hls_Read(stream_t *s, uint8_t *p_read, unsigned int i_read)
             vlc_mutex_unlock(&segment->lock);
 
             /* signal download thread */
-            vlc_mutex_lock(&p_sys->thread->lock_wait);
-            vlc_cond_signal(&p_sys->thread->wait);
-            vlc_mutex_unlock(&p_sys->thread->lock_wait);
+            vlc_mutex_lock(&p_sys->download.lock_wait);
+            vlc_cond_signal(&p_sys->download.wait);
+            vlc_mutex_unlock(&p_sys->download.lock_wait);
             continue;
         }
 
         if (segment->size == segment->data->i_buffer)
             msg_Info(s, "playing segment %d from stream %d",
-                        p_sys->playback.segment, p_sys->playback.current);
+                     segment->sequence, p_sys->playback.current);
 
         ssize_t len = -1;
         if (i_read <= segment->data->i_buffer)
@@ -1856,8 +1865,7 @@ static bool hls_MaySeek(stream_t *s)
 {
     stream_sys_t *p_sys = s->p_sys;
 
-    if ((p_sys->hls_stream == NULL) ||
-        (p_sys->thread == NULL))
+    if (p_sys->hls_stream == NULL)
         return false;
 
     hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->playback.current);
@@ -1869,9 +1877,9 @@ static bool hls_MaySeek(stream_t *s)
         int count = vlc_array_count(hls->segments);
         vlc_mutex_unlock(&hls->lock);
 
-        vlc_mutex_lock(&p_sys->thread->lock_wait);
-        bool may_seek = (p_sys->thread->segment < (count - 2));
-        vlc_mutex_unlock(&p_sys->thread->lock_wait);
+        vlc_mutex_lock(&p_sys->download.lock_wait);
+        bool may_seek = (p_sys->download.segment < (count - 2));
+        vlc_mutex_unlock(&p_sys->download.lock_wait);
         return may_seek;
     }
     return true;
@@ -1967,30 +1975,27 @@ static int segment_Seek(stream_t *s, uint64_t pos)
         vlc_mutex_unlock(&segment->lock);
 
         /* start download at current playback segment */
-        if (p_sys->thread)
-        {
-            vlc_mutex_unlock(&hls->lock);
-
-            /* Wake up download thread */
-            vlc_mutex_lock(&p_sys->thread->lock_wait);
-            p_sys->thread->seek = p_sys->playback.segment;
-            vlc_cond_signal(&p_sys->thread->wait);
-            vlc_mutex_unlock(&p_sys->thread->lock_wait);
-
-            /* Wait for download to be finished */
-            vlc_mutex_lock(&p_sys->thread->lock_wait);
-            msg_Info(s, "seek to segment %d", p_sys->playback.segment);
-            while (((p_sys->thread->seek != -1) ||
-                    (p_sys->thread->segment - p_sys->playback.segment < 3)) &&
-                    (p_sys->thread->segment < (count - 6)))
-            {
-                vlc_cond_wait(&p_sys->thread->wait, &p_sys->thread->lock_wait);
-                if (!vlc_object_alive(s) || s->b_error) break;
-            }
-            vlc_mutex_unlock(&p_sys->thread->lock_wait);
+        vlc_mutex_unlock(&hls->lock);
 
-            return VLC_SUCCESS;
+        /* Wake up download thread */
+        vlc_mutex_lock(&p_sys->download.lock_wait);
+        p_sys->download.seek = p_sys->playback.segment;
+        vlc_cond_signal(&p_sys->download.wait);
+        vlc_mutex_unlock(&p_sys->download.lock_wait);
+
+        /* Wait for download to be finished */
+        vlc_mutex_lock(&p_sys->download.lock_wait);
+        msg_Info(s, "seek to segment %d", p_sys->playback.segment);
+        while (((p_sys->download.seek != -1) ||
+                (p_sys->download.segment - p_sys->playback.segment < 3)) &&
+                (p_sys->download.segment < (count - 6)))
+        {
+            vlc_cond_wait(&p_sys->download.wait, &p_sys->download.lock_wait);
+            if (!vlc_object_alive(s) || s->b_error) break;
         }
+        vlc_mutex_unlock(&p_sys->download.lock_wait);
+
+        return VLC_SUCCESS;
     }
     vlc_mutex_unlock(&hls->lock);