[vlc-commits] playlist: m3u: unbreak HLS

Francois Cartegnie git at videolan.org
Tue Jun 27 14:38:01 CEST 2017


vlc | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Tue Jun 27 14:35:22 2017 +0200| [9103426c3593bb23a46230754c519a18dc474e68] | committer: Francois Cartegnie

playlist: m3u: unbreak HLS

adaptive broken by 84a0cc9fa09855b92d2c179f8976b2ef7f3368e7

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

 modules/demux/playlist/m3u.c | 85 +++++++++++++++++++++++++++++---------------
 1 file changed, 56 insertions(+), 29 deletions(-)

diff --git a/modules/demux/playlist/m3u.c b/modules/demux/playlist/m3u.c
index 4e25c2487d..6b1ec3840b 100644
--- a/modules/demux/playlist/m3u.c
+++ b/modules/demux/playlist/m3u.c
@@ -58,58 +58,85 @@ static char *CheckUnicode (const char *str)
  *****************************************************************************/
 int Import_M3U( vlc_object_t *p_this )
 {
-    stream_t *p_demux = (stream_t *)p_this;
+    stream_t *p_stream = (stream_t *)p_this;
     const uint8_t *p_peek;
+    ssize_t i_peek;
     char *(*pf_dup) (const char *) = GuessEncoding;
     int offset = 0;
 
-    CHECK_FILE(p_demux);
-    if( vlc_stream_Peek( p_demux->p_source, &p_peek, 3 ) == 3
-     && !memcmp( p_peek, "\xef\xbb\xbf", 3) )
+    CHECK_FILE(p_stream);
+    i_peek = vlc_stream_Peek( p_stream->p_source, &p_peek, 512 );
+    if( i_peek < 8 )
+        return VLC_EGENERIC;
+
+    if( !memcmp( p_peek, "\xef\xbb\xbf", 3) )
     {
+        if( i_peek < 12 )
+            return VLC_EGENERIC;
         pf_dup = CheckUnicode; /* UTF-8 Byte Order Mark */
         offset = 3;
     }
 
-    char *type = stream_MimeType(p_demux->p_source);
+    char *type = stream_MimeType(p_stream->p_source);
+    bool b_check_hls = true;
+
+    if( (type != NULL && !strcasecmp(type, "application/x-mpegurl") ) )
+    {
+        free( type );
+        return VLC_EGENERIC;
+    }
 
-    if( stream_HasExtension( p_demux, ".m3u8" )
+    if( stream_HasExtension( p_stream, ".m3u8" )
      || (type != NULL && !strcasecmp(type, "application/vnd.apple.mpegurl")))
     {
         pf_dup = CheckUnicode; /* UTF-8 file type */
-        free(type);
     }
-    else
-    if( stream_HasExtension( p_demux, ".m3u" )
-     || stream_HasExtension( p_demux, ".vlc" )
-     || ContainsURL( p_demux )
-     || (type != NULL && !strcasecmp(type, "audio/x-mpegurl"))
-     || p_demux->obj.force )
-        free(type); /* Guess encoding */
-    else
+    else if( stream_HasExtension( p_stream, ".vlc" ) )
     {
-        free(type);
-
-        if( vlc_stream_Peek( p_demux->p_source, &p_peek, 8 + offset ) < (8 + offset) )
-            return VLC_EGENERIC;
-
+        b_check_hls = false;
+    }
+    else if( !stream_HasExtension( p_stream, ".m3u" ) &&
+             !ContainsURL( p_stream ) &&
+             (type == NULL || strcasecmp(type, "audio/x-mpegurl") ) &&
+             !p_stream->obj.force )
+    {
+        /* Guess encoding */
         p_peek += offset;
 
         if( !strncasecmp( (const char *)p_peek, "RTSPtext", 8 ) ) /* QuickTime */
+        {
             pf_dup = CheckUnicode; /* UTF-8 */
-        else
-        if( !memcmp( p_peek, "#EXTM3U", 7 ) )
-            ; /* Guess encoding */
-        else
+            b_check_hls = false;
+        }
+        else if( memcmp( p_peek, "#EXTM3U", 7 ) )
+        {
+            free( type );
             return VLC_EGENERIC;
+        }
+    }
+
+    free(type);
+
+    if( b_check_hls )
+    {
+        const char * ppsz_hlsexts[] =
+        {
+            "#EXT-X-MEDIA:",
+            "#EXT-X-VERSION:",
+            "#EXT-X-TARGETDURATION:",
+            "#EXT-X-MEDIA-SEQUENCE:",
+        };
+        for( size_t i=0; i<ARRAY_SIZE(ppsz_hlsexts); i++ )
+            if( strnstr( (const char *) p_peek, ppsz_hlsexts[i], i_peek - offset ) )
+                return VLC_EGENERIC;
     }
 
-    vlc_stream_Seek( p_demux->p_source, offset );
+    vlc_stream_Seek( p_stream->p_source, offset );
 
-    msg_Dbg( p_demux, "found valid M3U playlist" );
-    p_demux->p_sys = pf_dup;
-    p_demux->pf_readdir = ReadDir;
-    p_demux->pf_control = access_vaDirectoryControlHelper;
+    msg_Dbg( p_stream, "found valid M3U playlist" );
+    p_stream->p_sys = pf_dup;
+    p_stream->pf_readdir = ReadDir;
+    p_stream->pf_control = access_vaDirectoryControlHelper;
 
     return VLC_SUCCESS;
 }



More information about the vlc-commits mailing list