[vlc-devel] [PATCH 05/48] hls: Don't reload main playlist

Hugo Beauzée-Luyssen beauze.h at gmail.com
Mon Jan 9 16:16:14 CET 2012


From: Luc Saillard <luc.saillard at sfr.com>

---
 modules/stream_filter/httplive.c |   95 ++++++++++++++++++++------------------
 1 files changed, 50 insertions(+), 45 deletions(-)

diff --git a/modules/stream_filter/httplive.c b/modules/stream_filter/httplive.c
index 1df8b62..b380d05 100644
--- a/modules/stream_filter/httplive.c
+++ b/modules/stream_filter/httplive.c
@@ -89,7 +89,7 @@ typedef struct hls_stream_s
                                foreach segment of (segment->duration * hls->bandwidth/8) */
 
     vlc_array_t *segments;  /* list of segments */
-    vlc_url_t   url;        /* uri to m3u8 */
+    char        *uri;       /* uri to m3u8 */
     vlc_mutex_t lock;
     bool        b_cache;    /* allow caching */
 
@@ -163,8 +163,6 @@ static void* hls_Reload(void *);
 static segment_t *segment_GetSegment(hls_stream_t *hls, int wanted);
 static void segment_Free(segment_t *segment);
 
-static char *ConstructUrl(vlc_url_t *url);
-
 /****************************************************************************
  *
  ****************************************************************************/
@@ -225,7 +223,7 @@ static hls_stream_t *hls_New(vlc_array_t *hls_stream, const int id, const uint64
     hls->version = 1;  /* default protocol version */
     hls->b_cache = true;
     hls->psz_current_key_path = NULL;
-    vlc_UrlParse(&hls->url, uri, 0);
+    hls->uri = uri?strdup(uri):NULL;
     hls->segments = vlc_array_new();
     vlc_array_append(hls_stream, hls);
     vlc_mutex_init(&hls->lock);
@@ -247,8 +245,9 @@ static void hls_Free(hls_stream_t *hls)
     }
     if(hls->psz_current_key_path)
         free(hls->psz_current_key_path);
+    if(hls->uri)
+        free(hls->uri);
 
-    vlc_UrlClean(&hls->url);
     free(hls);
     hls = NULL;
 }
@@ -268,13 +267,7 @@ static hls_stream_t *hls_Copy(hls_stream_t *src, const bool b_cp_segments)
     dst->sequence = src->sequence;
     dst->version = src->version;
     dst->b_cache = src->b_cache;
-    char *uri = ConstructUrl(&src->url);
-    if (uri == NULL)
-    {
-        free(dst);
-        return NULL;
-    }
-    vlc_UrlParse(&dst->url, uri, 0);
+    dst->uri = strdup( src->uri );
     if (!b_cp_segments)
         dst->segments = vlc_array_new();
     vlc_mutex_init(&dst->lock);
@@ -1306,8 +1299,8 @@ static int hls_UpdatePlaylist(stream_t *s, hls_stream_t *hls_new, hls_stream_t *
         segment_t *segment = segment_Find(*hls, p->sequence);
         if (segment)
         {
-            assert(p->url.psz_path);
-            assert(segment->url.psz_path);
+            assert( p->uri );
+            assert( segment->uri );
 
             /* they should be the same */
             if ((p->sequence != segment->sequence) ||
@@ -1317,8 +1310,8 @@ static int hls_UpdatePlaylist(stream_t *s, hls_stream_t *hls_new, hls_stream_t *
                 msg_Warn(s, "existing segment found with different content - resetting");
                 msg_Warn(s, "- sequence: new=%d, old=%d", p->sequence, segment->sequence);
                 msg_Warn(s, "- duration: new=%d, old=%d", p->duration, segment->duration);
-                msg_Warn(s, "- file: new=%s", p->url.psz_path);
-                msg_Warn(s, "        old=%s", segment->url.psz_path);
+                msg_Warn(s, "- file: new=%s", p->uri );
+                msg_Warn(s, "        old=%s", segment->uri );
 
                 /* Resetting content */
                 char *psz_url = ConstructUrl(&p->url);
@@ -1385,44 +1378,56 @@ static int hls_ReloadPlaylist(stream_t *s)
     if (hls_streams == NULL)
         return VLC_ENOMEM;
 
-    msg_Info(s, "Reloading HLS live meta playlist");
-
-    if (get_HTTPLiveMetaPlaylist(s, &hls_streams) != VLC_SUCCESS)
-    {
-        /* Free hls streams */
-        for (int i = 0; i < vlc_array_count(hls_streams); i++)
-        {
-            hls_stream_t *hls;
-            hls = (hls_stream_t *)vlc_array_item_at_index(hls_streams, i);
-            if (hls) hls_Free(hls);
-        }
-        vlc_array_destroy(hls_streams);
-
-        msg_Err(s, "reloading playlist failed");
-        return VLC_EGENERIC;
-    }
-
-    /* merge playlists */
-    int count = vlc_array_count(hls_streams);
+    int count = vlc_array_count(p_sys->hls_stream);
     for (int n = 0; n < count; n++)
     {
-        hls_stream_t *hls_new = hls_Get(hls_streams, n);
-        if (hls_new == NULL)
+        int err;
+        hls_stream_t *hls = vlc_array_item_at_index(p_sys->hls_stream, n);
+        if (!hls)
             continue;
 
-        hls_stream_t *hls_old = hls_Find(p_sys->hls_stream, hls_new);
-        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")",
-                     hls_new->id, hls_new->bandwidth);
+        /* create a fake stream, so get_HTTPLivePlaylist() can updated it */
+        hls_stream_t *hls_new = hls_New(hls_streams, hls->id, hls->bandwidth, hls->uri);
+        msg_Info(s, "reloading sub-playlist %s", hls->uri);
+        {
+            /* Download playlist file from server */
+            uint8_t *buf = NULL;
+            ssize_t len = read_M3U8_from_url( s, hls->uri, &buf );
+            if (len < 0)
+            {
+                msg_Err(s, "could not get playlist file at %s", hls_new->uri );
+                goto fail;
+            }
+            /* Parse HLS m3u8 content. */
+            err = parse_M3U8(s, hls_streams, buf, len);
+            free(buf);
+
+            if (err != VLC_SUCCESS)
+            {
+                msg_Err(s, "could not parse playlist file at %s", hls_new->uri );
+                goto fail;
+            }
         }
-        else if (hls_UpdatePlaylist(s, hls_new, &hls_old) != VLC_SUCCESS)
+        if (hls_UpdatePlaylist( s, hls_new, &hls ) != VLC_SUCCESS)
+        {
             msg_Info(s, "failed updating HLS stream (id=%d, bandwidth=%"PRIu64")",
                      hls_new->id, hls_new->bandwidth);
-    }
+            goto fail;
+        }
+        /* Free the temp stream */
+        vlc_array_destroy(hls_new->segments);
+        hls_new->segments = NULL;
+        hls_Free(hls_new);
+     }
+
     vlc_array_destroy(hls_streams);
+
     return VLC_SUCCESS;
+
+fail:
+    msg_Err(s, "reloading playlists failed");
+    vlc_array_destroy(hls_streams);
+    return VLC_EGENERIC;
 }
 
 /****************************************************************************
-- 
1.7.8.3




More information about the vlc-devel mailing list