]> git.sesse.net Git - vlc/blobdiff - src/input/meta.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / input / meta.c
index 3f3b9d3af519335bdd9a8cef47e541a4e3225c86..bff1b7d2eb7bce2b55397e9814059ddc06cc389f 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-#include <vlc/vlc.h>
-#include <vlc_input.h>
-#include <vlc_stream.h>
-#include <vlc_meta.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
 #include <vlc_playlist.h>
-#include <vlc_charset.h>
-#include "../playlist/playlist_internal.h"
+#include <vlc_url.h>
+#include <vlc_arrays.h>
+#include <vlc_modules.h>
 
-#ifdef HAVE_SYS_STAT_H
-#   include <sys/stat.h>
-#endif
+#include "input_internal.h"
+#include "../playlist/art.h"
+
+struct vlc_meta_t
+{
+    char * ppsz_meta[VLC_META_TYPE_COUNT];
+    
+    vlc_dictionary_t extra_tags;
+    
+    int i_status;
+};
+
+/* FIXME bad name convention */
+const char * vlc_meta_TypeToLocalizedString( vlc_meta_type_t meta_type )
+{
+    switch( meta_type )
+    {
+    case vlc_meta_Title:        return _("Title");
+    case vlc_meta_Artist:       return _("Artist");
+    case vlc_meta_Genre:        return _("Genre");
+    case vlc_meta_Copyright:    return _("Copyright");
+    case vlc_meta_Album:        return _("Album");
+    case vlc_meta_TrackNumber:  return _("Track number");
+    case vlc_meta_Description:  return _("Description");
+    case vlc_meta_Rating:       return _("Rating");
+    case vlc_meta_Date:         return _("Date");
+    case vlc_meta_Setting:      return _("Setting");
+    case vlc_meta_URL:          return _("URL");
+    case vlc_meta_Language:     return _("Language");
+    case vlc_meta_NowPlaying:   return _("Now Playing");
+    case vlc_meta_Publisher:    return _("Publisher");
+    case vlc_meta_EncodedBy:    return _("Encoded by");
+    case vlc_meta_ArtworkURL:   return _("Artwork URL");
+    case vlc_meta_TrackID:      return _("Track ID");
+
+    default: abort();
+    }
+};
 
-int input_FindArtInCache( playlist_t *p_playlist, 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 )
+/**
+ * vlc_meta contructor.
+ * vlc_meta_Delete() will free the returned pointer.
+ */ 
+vlc_meta_t *vlc_meta_New( void )
 {
-    *pi_mandatory = VLC_META_ENGINE_TITLE | VLC_META_ENGINE_ARTIST;
-    assert( p_item->p_meta );
+    vlc_meta_t *m = (vlc_meta_t*)malloc( sizeof(*m) );
+    if( !m )
+        return NULL;
+    memset( m->ppsz_meta, 0, sizeof(m->ppsz_meta) );
+    m->i_status = 0;
+    vlc_dictionary_init( &m->extra_tags, 0 );
+    return m;
+}
 
-    uint32_t i_meta = input_CurrentMetaFlags( p_item->p_meta );
-    *pi_mandatory &= ~i_meta;
-    *pi_optional = 0; /// Todo
-    return *pi_mandatory ? VLC_FALSE:VLC_TRUE;
+/* Free a dictonary key allocated by strdup() in vlc_meta_AddExtra() */
+static void vlc_meta_FreeExtraKey( void *p_data, void *p_obj )
+{
+    VLC_UNUSED( p_obj );
+    free( p_data );
 }
 
-int input_MetaFetch( playlist_t *p_playlist, input_item_t *p_item )
+void vlc_meta_Delete( vlc_meta_t *m )
 {
-    struct meta_engine_t *p_me;
-    uint32_t i_mandatory, i_optional;
+    int i;
+    for( i = 0; i < VLC_META_TYPE_COUNT ; i++ )
+        free( m->ppsz_meta[i] );
+    vlc_dictionary_clear( &m->extra_tags, vlc_meta_FreeExtraKey, NULL );
+    free( m );
+}
 
-    assert( p_item->p_meta );
+/**
+ * vlc_meta has two kinds of meta, the one in a table, and the one in a
+ * dictionary.
+ * FIXME - Why don't we merge those two?
+ */ 
 
-    input_MetaSatisfied( p_playlist, p_item, &i_mandatory, &i_optional );
-    // Meta shouldn't magically appear
-    assert( i_mandatory );
+void vlc_meta_Set( vlc_meta_t *p_meta, vlc_meta_type_t meta_type, const char *psz_val )
+{
+    free( p_meta->ppsz_meta[meta_type] );
+    p_meta->ppsz_meta[meta_type] = psz_val ? strdup( psz_val ) : NULL;
+}
 
-    /* FIXME: object creation is overkill, use p_private */
-    p_me = vlc_object_create( p_playlist, VLC_OBJECT_META_ENGINE );
-    p_me->i_flags |= OBJECT_FLAGS_NOINTERACT;
-    p_me->i_flags |= OBJECT_FLAGS_QUIET;
-    p_me->i_mandatory = i_mandatory;
-    p_me->i_optional = i_optional;
+const char *vlc_meta_Get( const vlc_meta_t *p_meta, vlc_meta_type_t meta_type )
+{
+    return p_meta->ppsz_meta[meta_type];
+}
 
-    p_me->p_item = 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 );
-        return VLC_EGENERIC;
-    }
-    module_Unneed( p_me, p_me->p_module );
-    vlc_object_destroy( p_me );
-    return VLC_SUCCESS;
+void vlc_meta_AddExtra( vlc_meta_t *m, const char *psz_name, const char *psz_value )
+{
+    char *psz_oldvalue = (char *)vlc_dictionary_value_for_key( &m->extra_tags, psz_name );
+    if( psz_oldvalue != kVLCDictionaryNotFound )
+        vlc_dictionary_remove_value_for_key( &m->extra_tags, psz_name,
+                                            vlc_meta_FreeExtraKey, NULL );
+    vlc_dictionary_insert( &m->extra_tags, psz_name, strdup(psz_value) );
 }
 
-/* Return codes:
- *   0 : Art is in cache
- *   1 : Art found, need to download
- *  -X : Error/not found
- */
-int input_ArtFind( playlist_t *p_playlist, input_item_t *p_item )
+const char * vlc_meta_GetExtra( const vlc_meta_t *m, const char *psz_name )
 {
-    int i_ret = VLC_EGENERIC;
-    module_t *p_module;
-    if( !p_item->p_meta || !p_item->p_meta->psz_album ||
-                           !p_item->p_meta->psz_artist )
-        return VLC_EGENERIC;
-
-    /* If we already checked this album in this session, skip */
-    FOREACH_ARRAY( playlist_album_t album, p_playlist->p_fetcher->albums )
-        if( !strcmp( album.psz_artist, p_item->p_meta->psz_artist ) &&
-            !strcmp( album.psz_album, p_item->p_meta->psz_album ) )
-        {
-            msg_Dbg( p_playlist, " %s - %s has already been searched",
-                     p_item->p_meta->psz_artist,  p_item->p_meta->psz_album );
-            if( album.b_found )
-            {
-                /* Actually get URL from cache */
-                input_FindArtInCache( p_playlist, p_item );
-                return 0;
-            }
-            else
-                return VLC_EGENERIC;
-        }
-    FOREACH_END();
-
-    input_FindArtInCache( p_playlist, p_item );
-    if( !EMPTY_STR( p_item->p_meta->psz_arturl ) )
-        return 0;
-
-    PL_LOCK;
-    p_playlist->p_private = p_item;
-    msg_Dbg( p_playlist, "searching art for %s - %s",
-             p_item->p_meta->psz_artist,  p_item->p_meta->psz_album );
-    p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
-
-    if( p_module )
-        i_ret = 1;
-    else
-        msg_Dbg( p_playlist, "unable to find art" );
-
-    /* Record this album */
-    playlist_album_t a;
-    a.psz_artist = strdup( p_item->p_meta->psz_artist );
-    a.psz_album = strdup( p_item->p_meta->psz_album );
-    a.b_found = (i_ret == VLC_EGENERIC ? VLC_FALSE : VLC_TRUE );
-    ARRAY_APPEND( p_playlist->p_fetcher->albums, a );
-
-    if( p_module )
-        module_Unneed( p_playlist, p_module );
-    p_playlist->p_private = NULL;
-    PL_UNLOCK;
-
-    return i_ret;
+    return (char *)vlc_dictionary_value_for_key(&m->extra_tags, psz_name);
 }
 
-#ifndef MAX_PATH
-#   define MAX_PATH 250
-#endif
-int input_FindArtInCache( playlist_t *p_playlist, input_item_t *p_item )
+unsigned vlc_meta_GetExtraCount( const vlc_meta_t *m )
 {
-    char *psz_artist;
-    char *psz_album;
-    char psz_filename[MAX_PATH+1];
-    int i;
-    struct stat a;
-    const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
+    return vlc_dictionary_keys_count(&m->extra_tags);
+}
 
-    if( !p_item->p_meta ) return VLC_EGENERIC;
+char** vlc_meta_CopyExtraNames( const vlc_meta_t *m )
+{
+    return vlc_dictionary_all_keys(&m->extra_tags);
+}
 
-    psz_artist = p_item->p_meta->psz_artist;
-    psz_album = p_item->p_meta->psz_album;
+/**
+ * vlc_meta status (see vlc_meta_status_e)
+ */
+int vlc_meta_GetStatus( vlc_meta_t *m )
+{
+    return m->i_status;
+}
+
+void vlc_meta_SetStatus( vlc_meta_t *m, int status )
+{
+    m->i_status = status;
+}
 
-    for( i = 0; i < 5; i++ )
+
+/**
+ * Merging meta
+ */
+void vlc_meta_Merge( vlc_meta_t *dst, const vlc_meta_t *src )
+{
+    char **ppsz_all_keys;
+    int i;
+    
+    if( !dst || !src )
+        return;
+    
+    for( i = 0; i < VLC_META_TYPE_COUNT; i++ )
     {
-        snprintf( psz_filename, MAX_PATH,
-                  "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
-                  DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
-                  p_playlist->p_libvlc->psz_homedir,
-                  psz_artist, psz_album, ppsz_type[i] );
-
-        /* Check if file exists */
-        if( utf8_stat( psz_filename+7, &a ) == 0 )
+        if( src->ppsz_meta[i] )
         {
-            vlc_meta_SetArtURL( p_item->p_meta, psz_filename );
-            return VLC_SUCCESS;
+            free( dst->ppsz_meta[i] );
+            dst->ppsz_meta[i] = strdup( src->ppsz_meta[i] );
         }
     }
-    return VLC_EGENERIC;
+    
+    /* XXX: If speed up are needed, it is possible */
+    ppsz_all_keys = vlc_dictionary_all_keys( &src->extra_tags );
+    for( i = 0; ppsz_all_keys && ppsz_all_keys[i]; i++ )
+    {
+        /* Always try to remove the previous value */
+        vlc_dictionary_remove_value_for_key( &dst->extra_tags, ppsz_all_keys[i], vlc_meta_FreeExtraKey, NULL );
+        
+        void *p_value = vlc_dictionary_value_for_key( &src->extra_tags, ppsz_all_keys[i] );
+        vlc_dictionary_insert( &dst->extra_tags, ppsz_all_keys[i], strdup( (const char*)p_value ) );
+        free( ppsz_all_keys[i] );
+    }
+    free( ppsz_all_keys );
 }
 
-/**
- * Download the art using the URL or an art downloaded
- * This function should be called only if data is not already in cache
- */
-int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
+
+void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
 {
-    int i_status = VLC_EGENERIC;
-    stream_t *p_stream;
-    char psz_filename[MAX_PATH+1], psz_dir[MAX_PATH+1];
-    char *psz_artist = NULL;
-    char *psz_album = NULL;
-    char *psz_type;
-    unsigned int  i;
-    if( p_item->p_meta->psz_artist )
-        psz_artist = strdup( p_item->p_meta->psz_artist );
-    if( p_item->p_meta->psz_album )
-        psz_album = strdup( p_item->p_meta->psz_album );
-
-    assert( p_item->p_meta && !EMPTY_STR(p_item->p_meta->psz_arturl) );
-
-    /* FIXME: use an alternate saving filename scheme if we don't have
-     * the artist or album name */
-    if( !p_item->p_meta->psz_artist || !p_item->p_meta->psz_album )
+    input_item_t *p_item = p_input->p->p_item;
+
+    /* */
+    char *psz_arturl = input_item_GetArtURL( p_item );
+    if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
     {
-        free( psz_artist );
-        free( psz_album );
-        return VLC_EGENERIC;
+        msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
+        free( psz_arturl );
+        return;
     }
 
-    /* Doesn't create a filename with invalid characters
-     * TODO: several filesystems forbid several characters: list them all
-     */
-    for( i = 0 ; i < strlen( psz_artist ) ; i++ )
-        if( psz_artist[i] == '/' )
-            psz_artist[i] = ' ';
-
-    for( i = 0 ; i < strlen( psz_album ) ; i++ )
-        if( psz_album[i] == '/' )
-            psz_album[i] = ' ';
-
-    psz_type = strrchr( p_item->p_meta->psz_arturl, '.' );
-
-    /* Todo: get a helper to do this */
-    snprintf( psz_filename, MAX_PATH,
-              "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
-              DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
-              p_playlist->p_libvlc->psz_homedir,
-              psz_artist, psz_album, psz_type );
-
-    snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
-              p_playlist->p_libvlc->psz_homedir );
-    utf8_mkdir( psz_dir );
-    snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
-              p_playlist->p_libvlc->psz_homedir );
-    utf8_mkdir( psz_dir );
-    snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
-              "art" DIR_SEP "%s",
-                 p_playlist->p_libvlc->psz_homedir, psz_artist );
-    utf8_mkdir( psz_dir );
-    snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
-              "art" DIR_SEP "%s" DIR_SEP "%s",
-                      p_playlist->p_libvlc->psz_homedir,
-                      psz_artist, psz_album );
-    utf8_mkdir( psz_dir );
-
-    if( !strncmp( p_item->p_meta->psz_arturl , "APIC", 4 ) )
+    playlist_t *p_playlist = pl_Get( p_input );
+
+    if( input_item_IsArtFetched( p_item ) )
     {
-        msg_Warn( p_playlist, "APIC fetch not supported yet" );
-        free( psz_artist );
-        free( psz_album );
-        return VLC_EGENERIC;
+        /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
+         * condition */
+        msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
+        playlist_FindArtInCache( p_item );
+        goto exit;
     }
 
-    p_stream = stream_UrlNew( p_playlist, p_item->p_meta->psz_arturl );
-    if( p_stream )
+    /* */
+    input_attachment_t *p_attachment = NULL;
+
+    vlc_mutex_lock( &p_item->lock );
+    for( int i_idx = 0; i_idx < p_input->p->i_attachment; i_idx++ )
     {
-        void *p_buffer = malloc( 1<<16 );
-        long int l_read;
-        FILE *p_file = utf8_fopen( psz_filename+7, "w" );
-        while( ( l_read = stream_Read( p_stream, p_buffer, 1<<16 ) ) )
+        if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
+                     &psz_arturl[strlen("attachment://")] ) )
         {
-            fwrite( p_buffer, l_read, 1, p_file );
+            p_attachment = vlc_input_attachment_Duplicate( p_input->p->attachment[i_idx] );
+            break;
         }
-        free( p_buffer );
-        fclose( p_file );
-        stream_Delete( p_stream );
-        msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
-        free( p_item->p_meta->psz_arturl );
-        p_item->p_meta->psz_arturl = strdup( psz_filename );
-        i_status = VLC_SUCCESS;
     }
-    free( psz_artist );
-    free( psz_album );
-    return i_status;
+    vlc_mutex_unlock( &p_item->lock );
+
+    if( !p_attachment || p_attachment->i_data <= 0 )
+    {
+        if( p_attachment )
+            vlc_input_attachment_Delete( p_attachment );
+        msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
+        goto exit;
+    }
+
+    /* */
+    const char *psz_type = NULL;
+    if( !strcmp( p_attachment->psz_mime, "image/jpeg" ) )
+        psz_type = ".jpg";
+    else if( !strcmp( p_attachment->psz_mime, "image/png" ) )
+        psz_type = ".png";
+
+    /* */
+    playlist_SaveArt( p_playlist, p_item,
+                      p_attachment->p_data, p_attachment->i_data, psz_type );
+
+    vlc_input_attachment_Delete( p_attachment );
+
+exit:
+    free( psz_arturl );
 }
 
-uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
+int input_item_WriteMeta( vlc_object_t *obj, input_item_t *p_item )
 {
-    uint32_t i_meta = 0;
-
-#define CHECK( a, b ) \
-    if( p_meta->psz_ ## a && *p_meta->psz_ ## a ) \
-        i_meta |= VLC_META_ENGINE_ ## b;
-
-    CHECK( title, TITLE )
-    CHECK( artist, ARTIST )
-    CHECK( album, COLLECTION )
-#if 0
-    /* As this is not used at the moment, don't uselessly check for it.
-     * Re-enable this when it is used */
-    CHECK( genre, GENRE )
-    CHECK( copyright, COPYRIGHT )
-    CHECK( tracknum, SEQ_NUM )
-    CHECK( description, DESCRIPTION )
-    CHECK( rating, RATING )
-    CHECK( date, DATE )
-    CHECK( url, URL )
-    CHECK( language, LANGUAGE )
-#endif
-    CHECK( arturl, ART_URL )
+    meta_export_t *p_export =
+        vlc_custom_create( obj, sizeof( *p_export ), VLC_OBJECT_GENERIC,
+                           "meta writer" );
+    if( p_export == NULL )
+        return VLC_ENOMEM;
+    vlc_object_attach( p_export, obj );
+    p_export->p_item = p_item;
+
+    int type;
+    vlc_mutex_lock( &p_item->lock );
+    type = p_item->i_type;
+    vlc_mutex_unlock( &p_item->lock );
+    if( type != ITEM_TYPE_FILE )
+        goto error;
+
+    char *psz_uri = input_item_GetURI( p_item );
+    p_export->psz_file = make_path( psz_uri );
+    if( p_export->psz_file == NULL )
+        msg_Err( p_export, "cannot write meta to remote media %s", psz_uri );
+    free( psz_uri );
+    if( p_export->psz_file == NULL )
+        goto error;
+
+    module_t *p_mod = module_need( p_export, "meta writer", NULL, false );
+    if( p_mod )
+        module_unneed( p_export, p_mod );
+    vlc_object_release( p_export );
+    return VLC_SUCCESS;
 
-    return i_meta;
+error:
+    vlc_object_release( p_export );
+    return VLC_EGENERIC;
 }