]> git.sesse.net Git - vlc/blobdiff - modules/demux/m3u.c
* modules/demux/mpeg/ts.c: fixed ts_dvbpsi demux forcing (with
[vlc] / modules / demux / m3u.c
index 29158f3aa3701fc868c76a89497432923f092bb0..cdc9c993621941befb39b582a5d9f67deff23ff0 100644 (file)
@@ -2,7 +2,7 @@
  * m3u.c: a meta demux to parse m3u and asx playlists
  *****************************************************************************
  * Copyright (C) 2001 VideoLAN
- * $Id: m3u.c,v 1.2 2002/11/13 11:09:56 gbazin Exp $
+ * $Id: m3u.c,v 1.10 2002/12/14 01:05:53 babal Exp $
  *
  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
  *          Gildas Bazin <gbazin@netcourrier.com>
@@ -42,6 +42,7 @@
 
 #define TYPE_M3U 1
 #define TYPE_ASX 2
+#define TYPE_HTML 3
 
 struct demux_sys_t
 {
@@ -51,7 +52,8 @@ struct demux_sys_t
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-static int  Init  ( vlc_object_t * );
+static int  Activate  ( vlc_object_t * );
+static void Deactivate( vlc_object_t * );
 static int  Demux ( input_thread_t * );
 
 /*****************************************************************************
@@ -60,13 +62,16 @@ static int  Demux ( input_thread_t * );
 vlc_module_begin();
     set_description( "m3u/asx metademux" );
     set_capability( "demux", 10 );
-    set_callbacks( Init, NULL );
+    set_callbacks( Activate, Deactivate );
+    add_shortcut( "m3u" );
+    add_shortcut( "asx" );
+    add_shortcut( "html" );
 vlc_module_end();
 
 /*****************************************************************************
- * Init: initializes ES structures
+ * Activate: initializes m3u demux structures
  *****************************************************************************/
-static int Init( vlc_object_t * p_this )
+static int Activate( vlc_object_t * p_this )
 {
     input_thread_t *p_input = (input_thread_t *)p_this;
     char           *psz_ext;
@@ -85,18 +90,53 @@ static int Init( vlc_object_t * p_this )
 
     /* Check for m3u/asx file extension */
     psz_ext = strrchr ( p_input->psz_name, '.' );
-    if( !strcasecmp( psz_ext, ".m3u") )
+    if( !strcasecmp( psz_ext, ".m3u") ||
+        ( p_input->psz_demux && !strncmp(p_input->psz_demux, "m3u", 3) ) )
     {
         i_type = TYPE_M3U;
     }
-    else if( !strcasecmp( psz_ext, ".asx") )
+    else if( !strcasecmp( psz_ext, ".asx") ||
+             ( p_input->psz_demux && !strncmp(p_input->psz_demux, "asx", 3) ) )
     {
         i_type = TYPE_ASX;
     }
+    else if( !strcasecmp( psz_ext, ".html") ||
+             ( p_input->psz_demux && !strncmp(p_input->psz_demux, "html", 4) ) )
+    {
+        i_type = TYPE_HTML;
+    }
 
+    /* we had no luck looking at the file extention, so we have a look
+     * at the content. This is useful for .asp, .php and similar files
+     * that are actually html. Also useful for som asx files that have
+     * another extention */
     if( !i_type )
-        return -1;
-
+    {
+        byte_t *p_peek;
+        int i_size = input_Peek( p_input, &p_peek, MAX_LINE );
+        i_size -= sizeof("<html>") - 1;
+        if ( i_size > 0 ) {
+            while ( i_size
+                    && strncasecmp( p_peek, "<html>", sizeof("<html>") - 1 )
+                    && strncasecmp( p_peek, "<asx", sizeof("<asx") - 1 ) )
+            {
+                p_peek++;
+                i_size--;
+            }
+            if ( !i_size )
+            {
+                return -1;
+            }
+            else if ( !strncasecmp( p_peek, "<html>", sizeof("<html>") -1 ) )
+            {
+                i_type = TYPE_HTML;
+            }
+            else if ( !strncasecmp( p_peek, "<asx", sizeof("<asx") -1 ) )
+            {
+                i_type = TYPE_ASX;
+            }
+        }
+    }
     /* Allocate p_m3u */
     if( !( p_m3u = malloc( sizeof( demux_sys_t ) ) ) )
     {
@@ -110,10 +150,170 @@ static int Init( vlc_object_t * p_this )
     return 0;
 }
 
+/*****************************************************************************
+ * Deactivate: frees unused data
+ *****************************************************************************/
+static void Deactivate( vlc_object_t *p_this )
+{
+    input_thread_t *p_input = (input_thread_t *)p_this;
+    demux_sys_t *p_m3u = (demux_sys_t *)p_input->p_demux_data  ; 
+
+    free( p_m3u );
+}
+
+/*****************************************************************************
+ * Demux: reads and demuxes data packets
+ *****************************************************************************
+ * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
+ *****************************************************************************/
+static void ProcessLine ( input_thread_t *p_input , demux_sys_t *p_m3u
+        , playlist_t *p_playlist , char psz_line[MAX_LINE] )
+{
+    char          *psz_bol, *psz_name;
+
+    psz_bol = psz_line;
+
+    /* Remove unnecessary tabs or spaces at the beginning of line */
+    while( *psz_bol == ' ' || *psz_bol == '\t' ||
+           *psz_bol == '\n' || *psz_bol == '\r' )
+        psz_bol++;
+
+    if( p_m3u->i_type == TYPE_M3U )
+    {
+        /* Check for comment line */
+        if( *psz_bol == '#' )
+            /*line is comment or extended info, ignored for now */
+            return;
+    }
+    else if ( p_m3u->i_type == TYPE_ASX )
+    {
+        /* We are dealing with ASX files.
+         * We are looking for "<ref href=" xml markups that
+         * begins with "mms://", "http://" or "file://" */
+        char *psz_eol;
+
+        while( *psz_bol &&
+               strncasecmp( psz_bol, "ref", sizeof("ref") - 1 ) )
+            psz_bol++;
+
+        if( !*psz_bol ) return;
+
+        while( *psz_bol &&
+               strncasecmp( psz_bol, "href", sizeof("href") - 1 ) )
+            psz_bol++;
+
+        if( !*psz_bol ) return;
+
+        while( *psz_bol &&
+               strncasecmp( psz_bol, "mms://",
+                            sizeof("mms://") - 1 ) &&
+               strncasecmp( psz_bol, "http://",
+                            sizeof("http://") - 1 ) &&
+               strncasecmp( psz_bol, "file://",
+                            sizeof("file://") - 1 ) )
+            psz_bol++;
+
+        if( !*psz_bol ) return;
+
+        psz_eol = strchr( psz_bol, '"');
+        if( !psz_eol )
+          return;
+
+        *psz_eol = '\0';
+    }
+    else
+    {
+        /* We are dealing with a html file with embedded
+         * video.  We are looking for "<param name="filename"
+         * value=" html markups that begin with "http://" */
+        char *psz_eol;
+
+        while( *psz_bol &&
+               strncasecmp( psz_bol, "param", sizeof("param") - 1 ) )
+            psz_bol++;
+
+        if( !*psz_bol ) return;
+
+        while( *psz_bol &&
+               strncasecmp( psz_bol, "filename", sizeof("filename") - 1 ) )
+            psz_bol++;
+
+        if( !*psz_bol ) return;
+
+        while( *psz_bol &&
+               strncasecmp( psz_bol, "http://",
+                            sizeof("http://") - 1 ) )
+            psz_bol++;
+
+        if( !*psz_bol ) return;
+
+        psz_eol = strchr( psz_bol, '"');
+        if( !psz_eol )
+          return;
+
+        *psz_eol = '\0';
+
+    }
+
+    /*
+     * From now on, we know we've got a meaningful line
+     */
+
+    /* Check if the line has an absolute or relative path */
+    psz_name = psz_bol;
+    while( *psz_name && strncmp( psz_name, "://", sizeof("://") - 1 ) )
+    {
+        psz_name++;
+    }
+#ifndef WIN32
+    if( !*psz_name && *psz_bol != '/' )
+#else
+    if( !*psz_name && (strlen(psz_bol) < 2 ||
+                ( *(psz_bol+1) != ':' &&
+                  strncmp( psz_bol, "\\\\", 2 ) ) ) )
+#endif
+    {
+        /* the line doesn't specify a protocol name.
+         * If this line doesn't begin with a '/' then assume the path
+         * is relative to the path of the m3u file. */
+        char *psz_path = strdup( p_input->psz_name );
+
+#ifndef WIN32
+        psz_name = strrchr( psz_path, '/' );
+#else
+        psz_name = strrchr( psz_path, '\\' );
+        if ( ! psz_name ) psz_name = strrchr( psz_path, '/' );
+#endif
+        if( psz_name ) *psz_name = '\0';
+        else *psz_path = '\0';
+#ifndef WIN32
+        psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
+        sprintf( psz_name, "%s/%s", psz_path, psz_bol );
+#else
+        if ( *psz_path != '\0' )
+        {
+            psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
+            sprintf( psz_name, "%s\\%s", psz_path, psz_bol );
+        }
+        else psz_name = strdup( psz_bol );
+#endif
+        free( psz_path );
+    }
+    else
+    {
+        psz_name = strdup( psz_bol );
+    }
+
+    playlist_Add( p_playlist, psz_name,
+                  PLAYLIST_APPEND, PLAYLIST_END );
+
+    free( psz_name );
+}
+
 static int Demux ( input_thread_t *p_input )
 {
     data_packet_t *p_data;
-    char          *p_buf, psz_line[MAX_LINE], *psz_bol, eol_tok;
+    char          *p_buf, psz_line[MAX_LINE], eol_tok;
     int           i_size, i_bufpos, i_linepos = 0;
     playlist_t    *p_playlist;
     vlc_bool_t    b_discard = VLC_FALSE;
@@ -132,9 +332,9 @@ static int Demux ( input_thread_t *p_input )
 
     /* Depending on wether we are dealing with an m3u/asf file, the end of
      * line token will be different */
-    if( p_m3u->i_type == TYPE_ASX )
+    if( p_m3u->i_type == TYPE_ASX || p_m3u->i_type == TYPE_HTML )
         eol_tok = '>';
-    else  
+    else
         eol_tok = '\n';
 
     while( ( i_size = input_SplitBuffer( p_input, &p_data, MAX_LINE ) ) > 0 )
@@ -154,8 +354,11 @@ static int Demux ( input_thread_t *p_input )
                 }
                 else
                 {
-                    psz_line[i_linepos] = p_buf[i_bufpos];
-                    i_linepos++;
+                    if ( eol_tok != '\n' || p_buf[i_bufpos] != '\r' )
+                    {
+                        psz_line[i_linepos] = p_buf[i_bufpos];
+                        i_linepos++;
+                    }
                 }
 
                 i_size--; i_bufpos++;
@@ -171,59 +374,20 @@ static int Demux ( input_thread_t *p_input )
             if( !i_linepos ) continue;
 
             psz_line[i_linepos] = '\0';
-            psz_bol = psz_line;
             i_linepos = 0;
-
-            /* Remove unnecessary tabs or spaces at the beginning of line */
-            while( *psz_bol == ' ' || *psz_bol == '\t' ||
-                   *psz_bol == '\n' || *psz_bol == '\r' )
-                psz_bol++;
-
-            if( p_m3u->i_type == TYPE_M3U )
-            {
-                /* Check for comment line */
-                if( *psz_bol == '#' )
-                    /*line is comment or extended info, ignored for now */
-                    continue;
-            }
-            else
-            {
-                /* We are dealing with ASX files.
-                 * We are looking for href html markups that begins with
-                 * "mms://" */
-                char *psz_eol;
-
-                while( *psz_bol && strncasecmp( psz_bol, "href",
-                                                sizeof("href") - 1 )  )
-                    psz_bol++;
-
-                if( !*psz_bol ) continue;
-
-                while( *psz_bol && strncasecmp( psz_bol, "mms://",
-                                               sizeof("mms://") - 1 )  )
-                    psz_bol++;
-
-                if( !*psz_bol ) continue;
-
-                psz_eol = strchr( psz_bol, '"');
-                if( !psz_eol )
-                  continue;
-
-                *psz_eol = '\0';
-            }
-
-            /*
-             * From now on, we know we've got a meaningful line
-             */
-            playlist_Add( p_playlist, psz_bol,
-                          PLAYLIST_APPEND, PLAYLIST_END );
-
-            continue;
+            ProcessLine ( p_input, p_m3u , p_playlist , psz_line );
         }
 
         input_DeletePacket( p_input->p_method_data, p_data );
     }
 
+    if ( i_linepos && b_discard != VLC_TRUE && eol_tok == '\n' )
+    {
+        psz_line[i_linepos] = '\0';
+        i_linepos = 0;
+        ProcessLine ( p_input, p_m3u , p_playlist , psz_line );
+    }
+
     vlc_object_release( p_playlist );
 
     return 0;