]> git.sesse.net Git - vlc/blobdiff - modules/demux/playlist/asx.c
vlc_path2uri(): rename from make_URI() and always convert path
[vlc] / modules / demux / playlist / asx.c
index 8aad20a7bd822e38baa494d3225f65ea6818cb2d..cc7c15a4e06b46d16966905923e814301f6d5416 100644 (file)
@@ -37,7 +37,7 @@
 #include <ctype.h>
 #include <vlc_charset.h>
 #include "playlist.h"
-#include "vlc_meta.h"
+#include <vlc_meta.h>
 
 struct demux_sys_t
 {
@@ -52,7 +52,6 @@ struct demux_sys_t
  * Local prototypes
  *****************************************************************************/
 static int Demux( demux_t *p_demux);
-static int Control( demux_t *p_demux, int i_query, va_list args );
 
 static int StoreString( demux_t *p_demux, char **ppsz_string,
                         const char *psz_source_start,
@@ -89,7 +88,9 @@ static int StoreString( demux_t *p_demux, char **ppsz_string,
         }
         *buf++ = '\0';
 
-        buf = *ppsz_string = realloc (*ppsz_string, buf - *ppsz_string);
+        buf = realloc (*ppsz_string, buf - *ppsz_string);
+        if( buf )
+            *ppsz_string = buf;
     }
     return VLC_SUCCESS;
 }
@@ -123,7 +124,7 @@ static int ParseTime(char *s, size_t i_strlen)
     s = SkipBlanks(s, i_strlen);
 
     val = 0;
-    while( (s < end) && isdigit(*s) )
+    while( (s < end) && isdigit((unsigned char)*s) )
     {
         int newval = val*10 + (*s - '0');
         if( newval < val )
@@ -143,7 +144,7 @@ static int ParseTime(char *s, size_t i_strlen)
         s = SkipBlanks(s, end-s);
         result = result * 60;
         val = 0;
-        while( (s < end) && isdigit(*s) )
+        while( (s < end) && isdigit((unsigned char)*s) )
         {
             int newval = val*10 + (*s - '0');
             if( newval < val )
@@ -163,7 +164,7 @@ static int ParseTime(char *s, size_t i_strlen)
             s = SkipBlanks(s, end-s);
             result = result * 60;
             val = 0;
-            while( (s < end) && isdigit(*s) )
+            while( (s < end) && isdigit((unsigned char)*s) )
             {
                 int newval = val*10 + (*s - '0');
                 if( newval < val )
@@ -208,7 +209,8 @@ int Import_ASX( vlc_object_t *p_this )
     p_demux->p_sys->psz_data = NULL;
     p_demux->p_sys->i_data_len = -1;
     p_demux->p_sys->b_utf8 = false;
-    p_demux->p_sys->b_skip_ads = config_GetInt( p_demux, "playlist-skip-ads" );
+    p_demux->p_sys->b_skip_ads =
+        var_InheritBool( p_demux, "playlist-skip-ads" );
 
     return VLC_SUCCESS;
 }
@@ -232,16 +234,15 @@ static int Demux( demux_t *p_demux )
     char        *psz_parse = NULL;
     char        *psz_backup = NULL;
     bool  b_entry = false;
-    input_item_t *p_input;
-    INIT_PLAYLIST_STUFF;
+    input_item_t *p_current_input = GetCurrentItem(p_demux);
 
     /* init txt */
     if( p_sys->i_data_len < 0 )
     {
         int64_t i_pos = 0;
-        p_sys->i_data_len = stream_Size( p_demux->s ) +1; /* This is a cheat to prevent unnecessary realloc */
-        if( p_sys->i_data_len <= 0 && p_sys->i_data_len < 16384 ) p_sys->i_data_len = 1024;
-        p_sys->psz_data = malloc( p_sys->i_data_len * sizeof(char) +1);
+        p_sys->i_data_len = stream_Size( p_demux->s ) + 1; /* This is a cheat to prevent unnecessary realloc */
+        if( p_sys->i_data_len <= 0 || p_sys->i_data_len > 16384 ) p_sys->i_data_len = 1024;
+        p_sys->psz_data = xmalloc( p_sys->i_data_len +1);
 
         /* load the complete file */
         for( ;; )
@@ -252,12 +253,15 @@ static int Demux( demux_t *p_demux )
             if( i_read < p_sys->i_data_len - i_pos ) break; /* Done */
 
             i_pos += i_read;
-            p_sys->i_data_len += 1024;
-            p_sys->psz_data = realloc( p_sys->psz_data, p_sys->i_data_len * sizeof( char * ) +1 );
+            p_sys->i_data_len <<= 1 ;
+            p_sys->psz_data = xrealloc( p_sys->psz_data,
+                                   p_sys->i_data_len * sizeof( char * ) + 1 );
         }
         if( p_sys->i_data_len <= 0 ) return -1;
     }
 
+    input_item_node_t *p_subitems = input_item_node_Create( p_current_input );
+
     psz_parse = p_sys->psz_data;
     /* Find first element */
     if( ( psz_parse = strcasestr( psz_parse, "<ASX" ) ) )
@@ -288,6 +292,10 @@ static int Demux( demux_t *p_demux )
 
         psz_parse = strcasestr( psz_parse, ">" );
 
+        /* counter for single ad item */
+        input_item_t *uniq_entry_ad_backup = NULL;
+        int i_inserted_entries = 0;
+
         while( psz_parse && ( psz_parse = strcasestr( psz_parse, "<" ) ) )
         {
             if( !strncasecmp( psz_parse, "<!--", 4 ) )
@@ -311,7 +319,7 @@ static int Demux( demux_t *p_demux )
                             i_strlen = psz_parse-psz_backup;
                             if( i_strlen < 1 ) continue;
                             msg_Dbg( p_demux, "param name strlen: %d", i_strlen);
-                            psz_string = malloc( i_strlen *sizeof( char ) +1);
+                            psz_string = xmalloc( i_strlen + 1);
                             memcpy( psz_string, psz_backup, i_strlen );
                             psz_string[i_strlen] = '\0';
                             msg_Dbg( p_demux, "param name: %s", psz_string);
@@ -333,7 +341,7 @@ static int Demux( demux_t *p_demux )
                             i_strlen = psz_parse-psz_backup;
                             if( i_strlen < 1 ) continue;
                             msg_Dbg( p_demux, "param value strlen: %d", i_strlen);
-                            psz_string = malloc( i_strlen *sizeof( char ) +1);
+                            psz_string = xmalloc( i_strlen +1);
                             memcpy( psz_string, psz_backup, i_strlen );
                             psz_string[i_strlen] = '\0';
                             msg_Dbg( p_demux, "param value: %s", psz_string);
@@ -430,9 +438,15 @@ static int Demux( demux_t *p_demux )
                     }
                     else continue;
                 }
-                if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
-                    psz_parse += 2;
-                else continue;
+                if( ( psz_backup = strcasestr( psz_parse, "/>" ) ) )
+                    psz_parse = psz_backup + 2;
+                else if( ( psz_backup = strcasestr( psz_parse, "</MoreInfo>") ) )
+                    psz_parse = psz_backup + 11;
+                else
+                {
+                    psz_parse = NULL;
+                    continue;
+                }
             }
             else if( !strncasecmp( psz_parse, "<ABSTRACT>", 10 ) )
             {
@@ -456,12 +470,14 @@ static int Demux( demux_t *p_demux )
                         {
                             i_strlen = psz_parse-psz_backup;
                             if( i_strlen < 1 ) continue;
-                            psz_string = malloc( i_strlen*sizeof( char ) +1);
+                            psz_string = xmalloc( i_strlen +1);
                             memcpy( psz_string, psz_backup, i_strlen );
                             psz_string[i_strlen] = '\0';
-                            p_input = input_item_New( p_demux, psz_string, psz_title_asx );
+                            input_item_t *p_input;
+                            p_input = input_item_New( psz_string, psz_title_asx );
                             input_item_CopyOptions( p_current_input, p_input );
-                            input_item_AddSubItem( p_current_input, p_input );
+                            input_item_node_AppendItem( p_subitems, p_input );
+                            vlc_gc_decref( p_input );
                             free( psz_string );
                         }
                         else continue;
@@ -470,7 +486,6 @@ static int Demux( demux_t *p_demux )
                 }
                 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
                     psz_parse += 2;
-                else continue;
             }
             else if( !strncasecmp( psz_parse, "</Entry>", 8 ) )
             {
@@ -493,45 +508,76 @@ static int Demux( demux_t *p_demux )
                     msg_Err( p_demux, "entry without href?" );
                     continue;
                 }
-
-                if( p_sys->b_skip_ads && b_skip_entry )
+                /* An skip entry is an ad only if other entries exist without skip */
+                if( p_sys->b_skip_ads && b_skip_entry && i_inserted_entries != 0 )
                 {
+                    char *psz_current_input_name = input_item_GetName( p_current_input );
                     msg_Dbg( p_demux, "skipped entry %d %s (%s)",
-                    i_entry_count, ( psz_title_entry ? psz_title_entry : p_current_input->psz_name ), psz_href );
+                             i_entry_count,
+                             ( psz_title_entry ? psz_title_entry : psz_current_input_name ), psz_href );
+                    free( psz_current_input_name );
                 }
                 else
                 {
                     if( i_starttime || i_duration )
                     {
-                        if( i_starttime ) {
-                            asprintf(ppsz_options+i_options, ":start-time=%d", i_starttime);
-                            ++i_options;
+                        if( i_starttime )
+                        {
+                            if( asprintf(ppsz_options+i_options, ":start-time=%d", i_starttime) == -1 )
+                                *(ppsz_options+i_options) = NULL;
+                            else
+                                ++i_options;
                         }
-                        if( i_duration ) {
-                            asprintf(ppsz_options+i_options, ":stop-time=%d", i_starttime + i_duration);
-                            ++i_options;
+                        if( i_duration )
+                        {
+                            if( asprintf(ppsz_options+i_options, ":stop-time=%d", i_starttime + i_duration) == -1 )
+                                *(ppsz_options+i_options) = NULL;
+                            else
+                                ++i_options;
                         }
                     }
 
                     /* create the new entry */
-                    asprintf( &psz_name, "%d %s", i_entry_count, ( psz_title_entry ? psz_title_entry : p_current_input->psz_name ) );
-
-                    p_entry = input_item_NewExt( p_demux, psz_href, psz_name, i_options, (const char * const *)ppsz_options, -1 );
-                    FREENULL( psz_name );
-                    input_item_CopyOptions( p_current_input, p_entry );
-                    while( i_options )
+                    char *psz_current_input_name = input_item_GetName( p_current_input );
+                    if( asprintf( &psz_name, "%d %s", i_entry_count, ( psz_title_entry ? psz_title_entry : psz_current_input_name ) ) != -1 )
                     {
-                        psz_name = ppsz_options[--i_options];
-                        FREENULL(psz_name);
-                    }
+                        char *psz_mrl = ProcessMRL( psz_href, p_demux->p_sys->psz_prefix );
+                        p_entry = input_item_NewExt( psz_mrl, psz_name,
+                                                     i_options, (const char * const *)ppsz_options, VLC_INPUT_OPTION_TRUSTED, -1 );
+                        free( psz_name );
+                        free( psz_mrl );
+                        input_item_CopyOptions( p_current_input, p_entry );
+                        while( i_options )
+                        {
+                            psz_name = ppsz_options[--i_options];
+                            free( psz_name );
+                        }
+                        psz_name = NULL;
+
+                        if( psz_title_entry ) input_item_SetTitle( p_entry, psz_title_entry );
+                        if( psz_artist_entry ) input_item_SetArtist( p_entry, psz_artist_entry );
+                        if( psz_copyright_entry ) input_item_SetCopyright( p_entry, psz_copyright_entry );
+                        if( psz_moreinfo_entry ) input_item_SetURL( p_entry, psz_moreinfo_entry );
+                        if( psz_abstract_entry ) input_item_SetDescription( p_entry, psz_abstract_entry );
 
-                    if( psz_title_entry ) input_item_SetTitle( p_entry, psz_title_entry );
-                    if( psz_artist_entry ) input_item_SetArtist( p_entry, psz_artist_entry );
-                    if( psz_copyright_entry ) input_item_SetCopyright( p_entry, psz_copyright_entry );
-                    if( psz_moreinfo_entry ) input_item_SetURL( p_entry, psz_moreinfo_entry );
-                    if( psz_abstract_entry ) input_item_SetDescription( p_entry, psz_abstract_entry );
-                    input_item_AddSubItem( p_current_input, p_entry );
-                    vlc_gc_decref( p_entry );
+                        i_inserted_entries++;
+                        if( p_sys->b_skip_ads && b_skip_entry )
+                        {
+                            // We put the entry as a backup for unique ad case
+                            uniq_entry_ad_backup = p_entry;
+                        }
+                        else
+                        {
+                            if( uniq_entry_ad_backup != NULL )
+                            {
+                                vlc_gc_decref( uniq_entry_ad_backup );
+                                uniq_entry_ad_backup = NULL;
+                            }
+                            input_item_node_AppendItem( p_subitems, p_entry );
+                            vlc_gc_decref( p_entry );
+                        }
+                    }
+                    free( psz_current_input_name );
                 }
 
                 /* cleanup entry */;
@@ -563,7 +609,6 @@ static int Demux( demux_t *p_demux )
 
                 // init entry details
                 FREENULL(psz_href);
-                psz_href = NULL;
                 i_starttime = 0;
                 i_duration = 0;
             }
@@ -588,8 +633,29 @@ static int Demux( demux_t *p_demux )
                             i_strlen = psz_parse-psz_backup;
                             if( i_strlen < 1 ) continue;
 
-                            FREENULL(psz_href);
-                            psz_href = malloc( i_strlen*sizeof( char ) +1);
+                            if( psz_href )
+                            {
+                                /* we have allready one href in this entry, lets make new input from it and
+                                continue with new href, don't free meta/options*/
+                                input_item_t *p_entry = NULL;
+                                char *psz_name = input_item_GetName( p_current_input );
+
+                                char *psz_mrl = ProcessMRL( psz_href, p_demux->p_sys->psz_prefix );
+                                p_entry = input_item_NewExt( psz_mrl, psz_name,
+                                                     0, NULL, VLC_INPUT_OPTION_TRUSTED, -1 );
+                                free( psz_mrl );
+                                input_item_CopyOptions( p_current_input, p_entry );
+                                if( psz_title_entry ) input_item_SetTitle( p_entry, psz_title_entry );
+                                if( psz_artist_entry ) input_item_SetArtist( p_entry, psz_artist_entry );
+                                if( psz_copyright_entry ) input_item_SetCopyright( p_entry, psz_copyright_entry );
+                                if( psz_moreinfo_entry ) input_item_SetURL( p_entry, psz_moreinfo_entry );
+                                if( psz_abstract_entry ) input_item_SetDescription( p_entry, psz_abstract_entry );
+                                input_item_node_AppendItem( p_subitems, p_entry );
+                                vlc_gc_decref( p_entry );
+                            }
+
+                            free( psz_href );
+                            psz_href = xmalloc( i_strlen +1);
                             memcpy( psz_href, psz_backup, i_strlen );
                             psz_href[i_strlen] = '\0';
                             psz_tmp = psz_href + (i_strlen-1);
@@ -683,6 +749,13 @@ static int Demux( demux_t *p_demux )
             }
             else psz_parse++;
         }
+        if ( uniq_entry_ad_backup != NULL )
+        {
+            msg_Dbg( p_demux, "added unique entry even if ad");
+            /* If ASX contains a unique entry, we add it, it is probably not an ad */
+            input_item_node_AppendItem( p_subitems, uniq_entry_ad_backup );
+            vlc_gc_decref( uniq_entry_ad_backup);
+        }
 #if 0
 /* FIXME Unsupported elements */
             PARAM
@@ -692,12 +765,9 @@ static int Demux( demux_t *p_demux )
             STARTMARK
 #endif
     }
-    HANDLE_PLAY_AND_RELEASE;
-    return 0; /* Needed for correct operation of go back */
-}
 
-static int Control( demux_t *p_demux, int i_query, va_list args )
-{
-    VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
-    return VLC_EGENERIC;
+    input_item_node_PostAndDelete( p_subitems );
+
+    vlc_gc_decref(p_current_input);
+    return 0; /* Needed for correct operation of go back */
 }