]> git.sesse.net Git - vlc/blobdiff - src/input/meta.c
Remove META_ENGINE object type
[vlc] / src / input / meta.c
index 16f9db92a4ffb4a5b62ac2ca55cf00ec7cafa295..538951f372dc524b863922cf5ea1d4e99bc9aeb4 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include <vlc/vlc.h>
 #include <vlc_input.h>
 #include <vlc_stream.h>
@@ -31,6 +35,8 @@
 #include <vlc_strings.h>
 #include "../playlist/playlist_internal.h"
 #include <errno.h>
+#include <limits.h>                                             /* PATH_MAX */
+#include <assert.h>
 
 #ifdef HAVE_SYS_STAT_H
 #   include <sys/stat.h>
@@ -69,7 +75,7 @@ static int __input_FindArtInCache( vlc_object_t *, input_item_t *p_item );
 vlc_bool_t input_MetaSatisfied( playlist_t *p_playlist, input_item_t *p_item,
                                 uint32_t *pi_mandatory, uint32_t *pi_optional )
 {
-    (void)p_playlist;
+    VLC_UNUSED(p_playlist);
     *pi_mandatory = VLC_META_ENGINE_TITLE | VLC_META_ENGINE_ARTIST;
 
     uint32_t i_meta = input_CurrentMetaFlags( p_item->p_meta );
@@ -88,7 +94,8 @@ int input_MetaFetch( playlist_t *p_playlist, input_item_t *p_item )
     assert( i_mandatory );
 
     /* FIXME: object creation is overkill, use p_private */
-    p_me = vlc_object_create( p_playlist, VLC_OBJECT_META_ENGINE );
+    p_me = vlc_custom_create( VLC_OBJECT(p_playlist), sizeof( *p_me ),
+                              VLC_OBJECT_GENERIC, "meta engine" );
     p_me->i_flags |= OBJECT_FLAGS_NOINTERACT;
     p_me->i_flags |= OBJECT_FLAGS_QUIET;
     p_me->i_mandatory = i_mandatory;
@@ -98,11 +105,11 @@ int input_MetaFetch( playlist_t *p_playlist, input_item_t *p_item )
     p_me->p_module = module_Need( p_me, "meta fetcher", 0, VLC_FALSE );
     if( !p_me->p_module )
     {
-        vlc_object_destroy( p_me );
+        vlc_object_release( p_me );
         return VLC_EGENERIC;
     }
     module_Unneed( p_me, p_me->p_module );
-    vlc_object_destroy( p_me );
+    vlc_object_release( p_me );
 
     input_item_SetMetaFetched( p_item, VLC_TRUE );
 
@@ -164,12 +171,20 @@ int input_ArtFind( playlist_t *p_playlist, input_item_t *p_item )
     input_FindArtInCache( p_playlist, p_item );
 
     char *psz_arturl = input_item_GetArtURL( p_item );
-    if( !EMPTY_STR( psz_arturl ) )
+    if( psz_arturl )
     {
+        /* We already have an URL */
+        if( !strncmp( psz_arturl, "file://", strlen( "file://" ) ) )
+        {
+            free( psz_arturl );
+            return 0; /* Art is in cache, no need to go further */
+        }
+
         free( psz_arturl );
-        return 0; /* Art is in cache, no need to go further */
+        
+        /* Art need to be put in cache */
+        return 1;
     }
-    free( psz_arturl );
 
     PL_LOCK;
     p_playlist->p_private = p_item;
@@ -222,12 +237,9 @@ int input_ArtFind( playlist_t *p_playlist, input_item_t *p_item )
     return i_ret;
 }
 
-#ifndef MAX_PATH
-#   define MAX_PATH 250
-#endif
 static void ArtCacheCreateDir( const char *psz_dir )
 {
-    char newdir[MAX_PATH+1];
+    char newdir[strlen( psz_dir ) + 1];
     strcpy( newdir, psz_dir );
     char * psz_newdir = newdir;
     char * psz = psz_newdir;
@@ -236,12 +248,13 @@ static void ArtCacheCreateDir( const char *psz_dir )
     {
         while( *psz && *psz != DIR_SEP_CHAR) psz++;
         if( !*psz ) break;
-        *psz = 0;        
-        if( !EMPTY_STR( psz_newdir ) ) utf8_mkdir( psz_newdir );
+        *psz = 0;
+        if( !EMPTY_STR( psz_newdir ) )
+            utf8_mkdir( psz_newdir, 0700 );
         *psz = DIR_SEP_CHAR;
-        psz++;   
+        psz++;
     }
-    utf8_mkdir( psz_dir );
+    utf8_mkdir( psz_dir, 0700 );
 }
 
 static char * ArtCacheGetSanitizedFileName( const char *psz )
@@ -273,9 +286,9 @@ static void __ArtCacheGetDirPath( vlc_object_t *p_obj,
         char * psz_album_sanitized = ArtCacheGetSanitizedFileName( psz_album );
         char * psz_artist_sanitized = ArtCacheGetSanitizedFileName( psz_artist );
 
-        snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
+        snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
                   "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
-                      p_obj->p_libvlc->psz_homedir,
+                      p_obj->p_libvlc->psz_cachedir,
                       psz_artist_sanitized, psz_album_sanitized );
         free( psz_album_sanitized );
         free( psz_artist_sanitized );
@@ -283,9 +296,9 @@ static void __ArtCacheGetDirPath( vlc_object_t *p_obj,
     else
     {
         char * psz_title_sanitized = ArtCacheGetSanitizedFileName( psz_title );
-        snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
+        snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
                   "art" DIR_SEP "title" DIR_SEP "%s",
-                  p_obj->p_libvlc->psz_homedir,
+                  p_obj->p_libvlc->psz_cachedir,
                   psz_title_sanitized );
         free( psz_title_sanitized );
     }
@@ -300,7 +313,7 @@ static void __ArtCacheGetFilePath( vlc_object_t *p_obj,
                                    const char *psz_artist, const char *psz_album,
                                    const char *psz_extension )
 {
-    char psz_dir[MAX_PATH+1];
+    char psz_dir[PATH_MAX+1];
     char * psz_ext;
     ArtCacheGetDirPath( p_obj, psz_dir, psz_title, psz_artist, psz_album );
 
@@ -311,7 +324,7 @@ static void __ArtCacheGetFilePath( vlc_object_t *p_obj,
     }
     else psz_ext = strdup( "" );
 
-    snprintf( psz_filename, MAX_PATH, "file://%s" DIR_SEP "art%s",
+    snprintf( psz_filename, PATH_MAX, "file://%s" DIR_SEP "art%s",
               psz_dir, psz_ext );
 
     free( psz_ext );
@@ -322,8 +335,8 @@ static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
     char *psz_artist;
     char *psz_album;
     char *psz_title;
-    char psz_dirpath[MAX_PATH+1];
-    char psz_filepath[MAX_PATH+1];
+    char psz_dirpath[PATH_MAX+1];
+    char psz_filepath[PATH_MAX+1];
     char * psz_filename;
     DIR * p_dir;
 
@@ -356,7 +369,7 @@ static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
     {
         if( !strncmp( psz_filename, "art", 3 ) )
         {
-            snprintf( psz_filepath, MAX_PATH, "file://%s" DIR_SEP "%s",
+            snprintf( psz_filepath, PATH_MAX, "file://%s" DIR_SEP "%s",
                       psz_dirpath, psz_filename );
             input_item_SetArtURL( p_item, psz_filepath );
             free( psz_filename );
@@ -379,7 +392,7 @@ int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
 {
     int i_status = VLC_EGENERIC;
     stream_t *p_stream;
-    char psz_filename[MAX_PATH+1];
+    char psz_filename[PATH_MAX+1];
     char *psz_artist = NULL;
     char *psz_album = NULL;
     char *psz_title = NULL;
@@ -417,7 +430,7 @@ int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
     }
 
     psz_type = strrchr( psz_arturl, '.' );
-    if( strlen( psz_type ) > 5 ) 
+    if( psz_type && strlen( psz_type ) > 5 )
         psz_type = NULL; /* remove extension if it's > to 4 characters */
 
     /* Warning: psz_title, psz_artist, psz_album may change in ArtCache*() */
@@ -458,7 +471,10 @@ int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
         stream_Delete( p_stream );
 
         if( err )
-            msg_Err( p_playlist, "%s: %s", psz_filename, strerror( err ) );
+        {
+            errno = err;
+            msg_Err( p_playlist, "%s: %m", psz_filename );
+        }
         else
             msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
 
@@ -472,12 +488,12 @@ int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
 {
     input_item_t *p_item = p_input->p->input.p_item;
-    char *psz_arturl;
-    char *psz_artist = NULL;
-    char *psz_album = NULL;
-    char *psz_title = NULL;
+    const char *psz_arturl;
+    const char *psz_artist = NULL;
+    const char *psz_album = NULL;
+    const char *psz_title = NULL;
     char *psz_type = NULL;
-    char psz_filename[MAX_PATH+1];
+    char psz_filename[PATH_MAX+1];
     FILE *f;
     input_attachment_t *p_attachment;
     struct stat s;
@@ -486,14 +502,13 @@ void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
     /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH
      * and then set it here to to be faster */
 
-    psz_arturl = input_item_GetArtURL( p_item );
+    psz_arturl = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
+
     if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
     {
-        free( psz_arturl );
         msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
         return;
     }
-    input_item_SetArtURL( p_item, NULL );
 
     if( input_item_IsArtFetched( p_item ) )
     {
@@ -501,7 +516,6 @@ void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
          * condition */
         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
         input_FindArtInCache( p_input, p_item );
-        free( psz_arturl );
         return;
     }
 
@@ -518,48 +532,51 @@ void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
     if( !p_attachment || p_attachment->i_data <= 0 )
     {
         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
-        goto end;
+        return;
     }
 
-    psz_artist = input_item_GetArtist( p_item );
-    psz_album = input_item_GetAlbum( p_item );
-    psz_title = input_item_GetTitle( p_item );
+    psz_artist = vlc_meta_Get( p_item->p_meta, vlc_meta_Artist );
+    psz_album = vlc_meta_Get( p_item->p_meta, vlc_meta_Album );
+    psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
+    if( !strcmp( p_attachment->psz_mime, "image/jpeg" ) )
+        psz_type = strdup( ".jpg" );
+    else if( !strcmp( p_attachment->psz_mime, "image/png" ) )
+        psz_type = strdup( ".png" );
+
     if( !psz_title )
-        psz_title = input_item_GetName( p_item );
+        psz_title = p_item->psz_name;
 
     if( (!psz_artist || !psz_album ) && !psz_title )
-        goto end;
-
-    /* */
-    psz_type = strrchr( psz_arturl, '.' );
+        return;
 
     ArtCacheGetDirPath( p_input, psz_filename, psz_title, psz_artist, psz_album );
     ArtCacheCreateDir( psz_filename );
     ArtCacheGetFilePath( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
+    free( psz_type );
 
     /* Check if we already dumped it */
     if( !utf8_stat( psz_filename+7, &s ) )
-        goto end;
+    {
+        vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_filename );
+        return;
+    }
 
     f = utf8_fopen( psz_filename+7, "w" );
     if( f )
     {
         if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 )
-            msg_Err( p_input, "%s: %s", psz_filename, strerror( errno ) );
+            msg_Err( p_input, "%s: %m", psz_filename );
         else
+        {
             msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
+            vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_filename );
+        }
         fclose( f );
     }
-
-end:
-    if( psz_artist ) free( psz_artist );
-    if( psz_album ) free( psz_album );
-    if( psz_title ) free( psz_title );
-    if( psz_arturl ) free( psz_arturl );
 }
 
 
-uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
+uint32_t input_CurrentMetaFlags( const vlc_meta_t *p_meta )
 {
     uint32_t i_meta = 0;