[vlc-commits] Flag some input_item with uid to speed up cached elements based on lua retrievals

Francois Cartegnie git at videolan.org
Sun Jun 3 22:21:52 CEST 2012


vlc | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Wed May 30 20:04:03 2012 +0200| [8fa822f747921b509857cd3701fbf1942e86d52b] | committer: Francois Cartegnie

Flag some input_item with uid to speed up cached elements based on lua retrievals

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

 modules/lua/libs/sd.c    |   22 +++++++++++++
 share/lua/sd/icecast.lua |    2 ++
 src/playlist/art.c       |   81 +++++++++++++++++++++++++++++++++++++++++++++-
 src/playlist/art.h       |    1 +
 src/playlist/fetcher.c   |    5 ++-
 5 files changed, 109 insertions(+), 2 deletions(-)

diff --git a/modules/lua/libs/sd.c b/modules/lua/libs/sd.c
index 591ef46..fd9c385 100644
--- a/modules/lua/libs/sd.c
+++ b/modules/lua/libs/sd.c
@@ -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;
diff --git a/share/lua/sd/icecast.lua b/share/lua/sd/icecast.lua
index b23a33c..ee10dd6 100644
--- a/share/lua/sd/icecast.lua
+++ b/share/lua/sd/icecast.lua
@@ -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],
diff --git a/src/playlist/art.c b/src/playlist/art.c
index dc71212..d88d98b 100644
--- a/src/playlist/art.c
+++ b/src/playlist/art.c
@@ -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;
 }
 
diff --git a/src/playlist/art.h b/src/playlist/art.h
index eb3aa23..98f2968 100644
--- a/src/playlist/art.h
+++ b/src/playlist/art.h
@@ -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 );
 
diff --git a/src/playlist/fetcher.c b/src/playlist/fetcher.c
index 1b202dc..f77a814 100644
--- a/src/playlist/fetcher.c
+++ b/src/playlist/fetcher.c
@@ -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 )



More information about the vlc-commits mailing list