]> git.sesse.net Git - vlc/commitdiff
Flag some input_item with uid to speed up cached elements based on lua retrievals
authorFrancois Cartegnie <fcvlcdev@free.fr>
Wed, 30 May 2012 18:04:03 +0000 (20:04 +0200)
committerFrancois Cartegnie <fcvlcdev@free.fr>
Sun, 3 Jun 2012 20:21:28 +0000 (22:21 +0200)
modules/lua/libs/sd.c
share/lua/sd/icecast.lua
src/playlist/art.c
src/playlist/art.h
src/playlist/fetcher.c

index 591ef468afd4bd915e49559f4abe33d388a0a183..fd9c385da3222bbce8599b2f1701e350a9b28d1e 100644 (file)
@@ -37,6 +37,7 @@
 #include <vlc_services_discovery.h>
 #include <vlc_playlist.h>
 #include <vlc_charset.h>
+#include <vlc_md5.h>
 
 #include "../vlc.h"
 #include "../libs.h"
@@ -256,6 +257,27 @@ static int vlclua_sd_add_item( lua_State *L )
                 else
                     services_discovery_AddItem( p_sd, p_input, NULL );
                 lua_pop( L, 1 );
+
+                /* string to build the input item uid */
+                lua_getfield( L, -1, "uiddata" );
+                if( lua_isstring( L, -1 ) )
+                {
+                    char *s = strdup( luaL_checkstring( L, -1 ) );
+                    if ( s )
+                    {
+                        struct md5_s md5;
+                        InitMD5( &md5 );
+                        AddMD5( &md5, s, strlen( s ) );
+                        EndMD5( &md5 );
+                        free( s );
+                        s = psz_md5_hash( &md5 );
+                        if ( s )
+                            input_item_AddInfo( p_input, "uid", "md5", "%s", s );
+                        free( s );
+                    }
+                }
+                lua_pop( L, 1 );
+
                 input_item_t **udata = (input_item_t **)
                                        lua_newuserdata( L, sizeof( input_item_t * ) );
                 *udata = p_input;
index b23a33c8e06afe568ba1d0e8bf66c407df93bd69..ee10dd6260006a92d7685b0151f808246af5b015 100644 (file)
@@ -43,6 +43,8 @@ function main()
                           title=station_name,
                           genre=station.children_map["genre"][1].children[1],
                           nowplaying=station.children_map["current_song"][1].children[1],
+                          uiddata=station.children_map["listen_url"][1].children[1]
+                                  .. station.children_map["listen_url"][1].children[1],
                           meta={
                                   ["Listing Source"]="dir.xiph.org",
                                   ["Icecast Bitrate"]=station.children_map["bitrate"][1].children[1],
index dc71212b62ac7bd68eaff9011ce3bccc775a2dad..d88d98bcd72ea7a0e2d0ec29156812b547bd236e 100644 (file)
@@ -204,6 +204,61 @@ int playlist_FindArtInCache( input_item_t *p_item )
     return b_found ? VLC_SUCCESS : VLC_EGENERIC;
 }
 
+static char * GetDirByItemUIDs( char *psz_uid )
+{
+    char *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
+    char *psz_dir;
+    if( asprintf( &psz_dir, "%s" DIR_SEP
+                  "by-iiuid" DIR_SEP
+                  "%s",
+                  psz_cachedir, psz_uid ) == -1 )
+    {
+        psz_dir = NULL;
+    }
+    free( psz_cachedir );
+    return psz_dir;
+}
+
+static char * GetFileByItemUID( char *psz_dir, const char *psz_type )
+{
+    char *psz_file;
+    if( asprintf( &psz_file, "%s" DIR_SEP "%s", psz_dir, psz_type ) == -1 )
+    {
+        psz_file = NULL;
+    }
+    return psz_file;
+}
+
+int playlist_FindArtInCacheUsingItemUID( input_item_t *p_item )
+{
+    char *uid = input_item_GetInfo( p_item, "uid", "md5" );
+    if ( !uid ) return VLC_EGENERIC;
+    /* we have an input item uid set */
+    bool b_done = false;
+    char *psz_byuiddir = GetDirByItemUIDs( uid );
+    char *psz_byuidfile = GetFileByItemUID( psz_byuiddir, "arturl" );
+    free( psz_byuiddir );
+    if( psz_byuidfile )
+    {
+        FILE *fd = vlc_fopen( psz_byuidfile, "rb" );
+        if ( fd )
+        {
+            char sz_cachefile[2049];
+            /* read the cache hash url */
+            if ( fgets( sz_cachefile, 2048, fd ) != NULL )
+            {
+                input_item_SetArtURL( p_item, sz_cachefile );
+                b_done = true;
+            }
+            fclose( fd );
+        }
+        free( psz_byuidfile );
+    }
+    free( uid );
+    if ( b_done ) return VLC_SUCCESS;
+
+    return VLC_EGENERIC;
+}
 
 /* */
 int playlist_SaveArt( playlist_t *p_playlist, input_item_t *p_item,
@@ -246,8 +301,32 @@ int playlist_SaveArt( playlist_t *p_playlist, input_item_t *p_item,
         }
         fclose( f );
     }
-    free( psz_filename );
     free( psz_uri );
+
+    /* save uid info */
+    char *uid = input_item_GetInfo( p_item, "uid", "md5" );
+    if ( !uid ) goto end;
+
+    char *psz_byuiddir = GetDirByItemUIDs( uid );
+    char *psz_byuidfile = GetFileByItemUID( psz_byuiddir, "arturl" );
+    ArtCacheCreateDir( psz_byuiddir );
+    free( psz_byuiddir );
+
+    if ( psz_byuidfile )
+    {
+        f = vlc_fopen( psz_byuidfile, "wb" );
+        if ( f )
+        {
+            if( fputs( "file://", f ) < 0 || fputs( psz_filename, f ) < 0 )
+                msg_Err( p_playlist, "Error writing %s: %m", psz_byuidfile );
+            fclose( f );
+        }
+        free( psz_byuidfile );
+    }
+    free( uid );
+    /* !save uid info */
+end:
+    free( psz_filename );
     return VLC_SUCCESS;
 }
 
index eb3aa231c989e2ee91098db119055c372e79da66..98f2968b6255f6dcabbb27b79681eb150c0dba49 100644 (file)
@@ -35,6 +35,7 @@ typedef struct
 } playlist_album_t;
 
 int playlist_FindArtInCache( input_item_t * );
+int playlist_FindArtInCacheUsingItemUID( input_item_t * );
 
 int playlist_SaveArt( playlist_t *, input_item_t *, const uint8_t *p_buffer, int i_buffer, const char *psz_type );
 
index 1b202dce64c5cfacfe47bdbd09573aa454ccfbfc..f77a814d146082a590096ae28bde1ca22c57f758 100644 (file)
@@ -177,7 +177,10 @@ static int FindArt( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
     free( psz_artist );
     free( psz_album );
 
-    playlist_FindArtInCache( p_item );
+    if ( playlist_FindArtInCacheUsingItemUID( p_item ) != VLC_SUCCESS )
+        playlist_FindArtInCache( p_item );
+    else
+        msg_Dbg( p_fetcher->p_playlist, "successfully retrieved arturl by uid" );
 
     char *psz_arturl = input_item_GetArtURL( p_item );
     if( psz_arturl )