1 /*****************************************************************************
2 * meta.c : Metadata handling
3 *****************************************************************************
4 * Copyright (C) 1998-2004 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea@videolan.org>
8 * Clément Stenac <zorglub@videolan.org
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
26 #include <vlc_input.h>
27 #include <vlc_stream.h>
29 #include <vlc_playlist.h>
30 #include <vlc_charset.h>
31 #include "../playlist/playlist_internal.h"
34 #ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
38 // FIXME be sure to not touch p_meta without lock on p_item
40 static const char * meta_type_to_string[VLC_META_TYPE_COUNT] =
42 [vlc_meta_Title] = N_("Title"),
43 [vlc_meta_Artist] = N_("Artist"),
44 [vlc_meta_Genre] = N_("Genre"),
45 [vlc_meta_Copyright] = N_("Copyright"),
46 [vlc_meta_Album] = N_("Album/movie/show title"),
47 [vlc_meta_TrackNumber] = N_("Track number/position in set"),
48 [vlc_meta_Description] = N_("Description"),
49 [vlc_meta_Rating] = N_("Rating"),
50 [vlc_meta_Date] = N_("Date"),
51 [vlc_meta_Setting] = N_("Setting"),
52 [vlc_meta_URL] = N_("URL"),
53 [vlc_meta_Language] = N_("Language"),
54 [vlc_meta_NowPlaying] = N_("Language"),
55 [vlc_meta_Publisher] = N_("Publisher"),
56 [vlc_meta_EncodedBy] = N_("Encoded by"),
57 [vlc_meta_ArtworkURL] = N_("Artwork URL"),
58 [vlc_meta_TrackID] = N_("Track ID"),
62 input_MetaTypeToLocalizedString( vlc_meta_type_t meta_type )
64 return _(meta_type_to_string[meta_type]);
67 #define input_FindArtInCache(a,b) __input_FindArtInCache(VLC_OBJECT(a),b)
68 static int __input_FindArtInCache( vlc_object_t *, input_item_t *p_item );
70 vlc_bool_t input_MetaSatisfied( playlist_t *p_playlist, input_item_t *p_item,
71 uint32_t *pi_mandatory, uint32_t *pi_optional )
74 *pi_mandatory = VLC_META_ENGINE_TITLE | VLC_META_ENGINE_ARTIST;
76 uint32_t i_meta = input_CurrentMetaFlags( p_item->p_meta );
77 *pi_mandatory &= ~i_meta;
78 *pi_optional = 0; /// Todo
79 return *pi_mandatory ? VLC_FALSE:VLC_TRUE;
82 int input_MetaFetch( playlist_t *p_playlist, input_item_t *p_item )
84 struct meta_engine_t *p_me;
85 uint32_t i_mandatory, i_optional;
87 input_MetaSatisfied( p_playlist, p_item, &i_mandatory, &i_optional );
88 // Meta shouldn't magically appear
89 assert( i_mandatory );
91 /* FIXME: object creation is overkill, use p_private */
92 p_me = vlc_object_create( p_playlist, VLC_OBJECT_META_ENGINE );
93 p_me->i_flags |= OBJECT_FLAGS_NOINTERACT;
94 p_me->i_flags |= OBJECT_FLAGS_QUIET;
95 p_me->i_mandatory = i_mandatory;
96 p_me->i_optional = i_optional;
98 p_me->p_item = p_item;
99 p_me->p_module = module_Need( p_me, "meta fetcher", 0, VLC_FALSE );
100 if( !p_me->p_module )
102 vlc_object_destroy( p_me );
105 module_Unneed( p_me, p_me->p_module );
106 vlc_object_destroy( p_me );
108 input_item_SetMetaFetched( p_item, VLC_TRUE );
114 * 0 : Art is in cache
115 * 1 : Art found, need to download
116 * -X : Error/not found
118 int input_ArtFind( playlist_t *p_playlist, input_item_t *p_item )
120 int i_ret = VLC_EGENERIC;
123 if( !p_item->p_meta )
126 if( !p_item->psz_name && !input_item_GetTitle( p_item ) &&
127 (!input_item_GetArtist( p_item ) || !input_item_GetAlbum( p_item )) )
130 /* If we already checked this album in this session, skip */
131 if( input_item_GetArtist( p_item ) && input_item_GetAlbum( p_item ) )
133 FOREACH_ARRAY( playlist_album_t album, p_playlist->p_fetcher->albums )
134 if( !strcmp( album.psz_artist, input_item_GetArtist( p_item ) ) &&
135 !strcmp( album.psz_album, input_item_GetAlbum( p_item ) ) )
137 msg_Dbg( p_playlist, " %s - %s has already been searched",
138 input_item_GetArtist( p_item ), input_item_GetAlbum( p_item ) );
139 /* TODO-fenrir if we cache art filename too, we can go faster */
142 /* Actually get URL from cache */
143 input_FindArtInCache( p_playlist, p_item );
154 input_FindArtInCache( p_playlist, p_item );
155 if( !EMPTY_STR(input_item_GetArtURL( p_item )) )
159 p_playlist->p_private = p_item;
160 if( input_item_GetAlbum( p_item ) && input_item_GetArtist( p_item ) )
162 msg_Dbg( p_playlist, "searching art for %s - %s",
163 input_item_GetArtist( p_item ), input_item_GetAlbum( p_item ) );
167 msg_Dbg( p_playlist, "searching art for %s",
168 input_item_GetTitle( p_item ) ? input_item_GetTitle( p_item ) : p_item->psz_name );
171 p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
176 msg_Dbg( p_playlist, "unable to find art" );
178 /* Record this album */
179 if( input_item_GetArtist( p_item ) && input_item_GetAlbum( p_item ) )
182 a.psz_artist = strdup( input_item_GetArtist( p_item ) );
183 a.psz_album = strdup( input_item_GetAlbum( p_item ) );
184 a.b_found = (i_ret == VLC_EGENERIC ? VLC_FALSE : VLC_TRUE );
185 ARRAY_APPEND( p_playlist->p_fetcher->albums, a );
189 module_Unneed( p_playlist, p_module );
190 p_playlist->p_private = NULL;
197 # define MAX_PATH 250
199 #define ArtCacheCreateName(a,b,c,d,e,f) __ArtCacheCreateName(VLC_OBJECT(a),b,c,d,e,f)
200 static void __ArtCacheCreateName( vlc_object_t *p_obj,
201 char psz_filename[MAX_PATH+1],
202 const char *psz_title,
203 const char *psz_artist, const char *psz_album,
204 const char *psz_extension )
206 if( psz_artist && psz_artist )
208 snprintf( psz_filename, MAX_PATH,
209 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art" DIR_SEP "artistalbum"
210 DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
211 p_obj->p_libvlc->psz_homedir,
212 psz_artist, psz_album, psz_extension ? psz_extension : "" );
216 /* We will use the psz_title name to store the art */
217 snprintf( psz_filename, MAX_PATH,
218 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art" DIR_SEP "title"
219 DIR_SEP "%s" DIR_SEP "art%s",
220 p_obj->p_libvlc->psz_homedir,
221 psz_title, psz_extension ? psz_extension : "" );
224 #define ArtCacheCreatePath(a,b,c,d) __ArtCacheCreatePath(VLC_OBJECT(a),b,c,d)
225 static void __ArtCacheCreatePath( vlc_object_t *p_obj,
226 const char *psz_title,
227 const char *psz_artist, const char *psz_album )
229 char psz_dir[MAX_PATH+1];
230 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
231 p_obj->p_libvlc->psz_homedir );
232 utf8_mkdir( psz_dir );
233 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
234 p_obj->p_libvlc->psz_homedir );
235 utf8_mkdir( psz_dir );
237 if( psz_artist && psz_artist )
239 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
240 "art" DIR_SEP "artistalbum",
241 p_obj->p_libvlc->psz_homedir );
242 utf8_mkdir( psz_dir );
243 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
244 "art" DIR_SEP "artistalbum" DIR_SEP "%s",
245 p_obj->p_libvlc->psz_homedir, psz_artist );
246 utf8_mkdir( psz_dir );
247 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
248 "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
249 p_obj->p_libvlc->psz_homedir,
250 psz_artist, psz_album );
251 utf8_mkdir( psz_dir );
255 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
256 "art" DIR_SEP "title",
257 p_obj->p_libvlc->psz_homedir );
258 utf8_mkdir( psz_dir );
259 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
260 "art" DIR_SEP "title" DIR_SEP "%s",
261 p_obj->p_libvlc->psz_homedir, psz_title );
262 utf8_mkdir( psz_dir );
265 static char *ArtCacheCreateString( const char *psz )
267 char *dup = strdup(psz);
270 /* Doesn't create a filename with invalid characters
271 * TODO: several filesystems forbid several characters: list them all
273 for( i = 0; dup[i] != '\0'; i++ )
281 static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
283 const char *psz_artist;
284 const char *psz_album;
285 const char *psz_title;
286 char psz_filename[MAX_PATH+1];
289 const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
291 if( !p_item->p_meta ) return VLC_EGENERIC;
293 psz_artist = input_item_GetArtist( p_item );
294 psz_album = input_item_GetAlbum( p_item );
295 psz_title = input_item_GetTitle( p_item );
296 if( !psz_title ) psz_title = p_item->psz_name;
298 if( (!psz_artist || !psz_album) && !psz_title ) return VLC_EGENERIC;
300 for( i = 0; i < 5; i++ )
302 ArtCacheCreateName( p_obj, psz_filename, psz_title /* Used if none artist nor album is defined */,
303 psz_artist, psz_album, ppsz_type[i] );
305 /* Check if file exists */
306 if( utf8_stat( psz_filename+7, &a ) == 0 )
308 input_item_SetArtURL( p_item, psz_filename );
316 * Download the art using the URL or an art downloaded
317 * This function should be called only if data is not already in cache
319 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
321 int i_status = VLC_EGENERIC;
323 char psz_filename[MAX_PATH+1];
324 char *psz_artist = NULL;
325 char *psz_album = NULL;
326 char *psz_title = NULL;
328 if( input_item_GetArtist( p_item ) )
329 psz_artist = ArtCacheCreateString( input_item_GetArtist( p_item ) );
330 if( input_item_GetAlbum( p_item ) )
331 psz_album = ArtCacheCreateString( input_item_GetAlbum( p_item ) );
332 if( input_item_GetTitle( p_item ) )
333 psz_title = ArtCacheCreateString( input_item_GetTitle( p_item ) );
334 else if( p_item->psz_name )
335 psz_title = ArtCacheCreateString( p_item->psz_name );
337 if( !psz_title && (!psz_artist || !psz_album) )
345 assert( !EMPTY_STR(input_item_GetArtURL( p_item )) );
347 psz_type = strrchr( input_item_GetArtURL( p_item ), '.' );
350 ArtCacheCreateName( p_playlist, psz_filename, psz_title /* Used only if needed*/,
351 psz_artist, psz_album, psz_type );
354 ArtCacheCreatePath( p_playlist, psz_title, psz_artist, psz_album );
361 if( !strncmp( input_item_GetArtURL( p_item ) , "APIC", 4 ) )
363 msg_Warn( p_playlist, "APIC fetch not supported yet" );
367 p_stream = stream_UrlNew( p_playlist, input_item_GetArtURL( p_item ) );
370 uint8_t p_buffer[65536];
372 FILE *p_file = utf8_fopen( psz_filename+7, "w" );
374 while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
376 if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
382 if( fclose( p_file ) && !err )
384 stream_Delete( p_stream );
387 msg_Err( p_playlist, "%s: %s", psz_filename, strerror( err ) );
389 msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
391 input_item_SetArtURL( p_item, psz_filename );
392 i_status = VLC_SUCCESS;
397 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
399 input_item_t *p_item = p_input->p->input.p_item;
401 char *psz_artist = NULL;
402 char *psz_album = NULL;
403 char *psz_title = NULL;
404 char *psz_type = NULL;
405 char psz_filename[MAX_PATH+1];
407 input_attachment_t *p_attachment;
411 /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH
412 * and then set it here to to be faster */
414 psz_arturl = strdup( input_item_GetArtURL( p_item ) );
415 if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
417 msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
420 input_item_SetArtURL( p_item, NULL );
422 if( input_item_IsArtFetched( p_item ) )
424 /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
426 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
427 input_FindArtInCache( p_input, p_item );
433 for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ )
435 if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
436 &psz_arturl[strlen("attachment://")] ) )
438 p_attachment = p_input->p->attachment[i_idx];
442 if( !p_attachment || p_attachment->i_data <= 0 )
444 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
448 if( input_item_GetArtist( p_item ) )
449 psz_artist = ArtCacheCreateString( input_item_GetArtist( p_item ) );
450 if( input_item_GetAlbum( p_item ) )
451 psz_album = ArtCacheCreateString( input_item_GetAlbum( p_item ) );
452 if( input_item_GetTitle( p_item ) )
453 psz_title = ArtCacheCreateString( input_item_GetTitle( p_item ) );
454 else if( p_item->psz_name )
455 psz_title = ArtCacheCreateString( p_item->psz_name );
457 if( (!psz_artist || !psz_album ) && !psz_title )
461 psz_type = strrchr( psz_arturl, '.' );
462 ArtCacheCreateName( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
464 /* Check if we already dumped it */
465 if( !utf8_stat( psz_filename+7, &s ) )
468 ArtCacheCreatePath( p_input, psz_title, psz_artist, psz_album );
470 f = utf8_fopen( psz_filename+7, "w" );
473 if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 )
474 msg_Err( p_input, "%s: %s", psz_filename, strerror( errno ) );
476 msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
481 if( psz_artist ) free( psz_artist );
482 if( psz_album ) free( psz_album );
483 if( psz_title ) free( psz_title );
484 if( psz_arturl ) free( psz_arturl );
488 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
495 #define CHECK( a, b ) \
496 if( !EMPTY_STR( vlc_meta_Get( p_meta, vlc_meta_ ## a ) ) ) \
497 i_meta |= VLC_META_ENGINE_ ## b;
499 CHECK( Title, TITLE )
500 CHECK( Artist, ARTIST )
501 CHECK( Album, COLLECTION )
503 /* As this is not used at the moment, don't uselessly check for it.
504 * Re-enable this when it is used */
505 CHECK( Genre, GENRE )
506 CHECK( Copyright, COPYRIGHT )
507 CHECK( Tracknum, SEQ_NUM )
508 CHECK( Description, DESCRIPTION )
509 CHECK( Rating, RATING )
512 CHECK( Language, LANGUAGE )
514 CHECK( ArtworkURL, ART_URL )