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 static const char * meta_type_to_string[VLC_META_TYPE_COUNT] =
40 [vlc_meta_Title] = N_("Title"),
41 [vlc_meta_Artist] = N_("Artist"),
42 [vlc_meta_Genre] = N_("Genre"),
43 [vlc_meta_Copyright] = N_("Copyright"),
44 [vlc_meta_Album] = N_("Album"),
45 [vlc_meta_TrackNumber] = N_("Track number"),
46 [vlc_meta_Description] = N_("Description"),
47 [vlc_meta_Rating] = N_("Rating"),
48 [vlc_meta_Date] = N_("Date"),
49 [vlc_meta_Setting] = N_("Setting"),
50 [vlc_meta_URL] = N_("URL"),
51 [vlc_meta_Language] = N_("Language"),
52 [vlc_meta_NowPlaying] = N_("Now Playing"),
53 [vlc_meta_Publisher] = N_("Publisher"),
54 [vlc_meta_EncodedBy] = N_("Encoded by"),
55 [vlc_meta_ArtworkURL] = N_("Artwork URL"),
56 [vlc_meta_TrackID] = N_("Track ID"),
60 input_MetaTypeToLocalizedString( vlc_meta_type_t meta_type )
62 return _(meta_type_to_string[meta_type]);
65 #define input_FindArtInCache(a,b) __input_FindArtInCache(VLC_OBJECT(a),b)
66 static int __input_FindArtInCache( vlc_object_t *, input_item_t *p_item );
68 vlc_bool_t input_MetaSatisfied( playlist_t *p_playlist, input_item_t *p_item,
69 uint32_t *pi_mandatory, uint32_t *pi_optional )
72 *pi_mandatory = VLC_META_ENGINE_TITLE | VLC_META_ENGINE_ARTIST;
74 uint32_t i_meta = input_CurrentMetaFlags( p_item->p_meta );
75 *pi_mandatory &= ~i_meta;
76 *pi_optional = 0; /// Todo
77 return *pi_mandatory ? VLC_FALSE:VLC_TRUE;
80 int input_MetaFetch( playlist_t *p_playlist, input_item_t *p_item )
82 struct meta_engine_t *p_me;
83 uint32_t i_mandatory, i_optional;
85 input_MetaSatisfied( p_playlist, p_item, &i_mandatory, &i_optional );
86 // Meta shouldn't magically appear
87 assert( i_mandatory );
89 /* FIXME: object creation is overkill, use p_private */
90 p_me = vlc_object_create( p_playlist, VLC_OBJECT_META_ENGINE );
91 p_me->i_flags |= OBJECT_FLAGS_NOINTERACT;
92 p_me->i_flags |= OBJECT_FLAGS_QUIET;
93 p_me->i_mandatory = i_mandatory;
94 p_me->i_optional = i_optional;
96 p_me->p_item = p_item;
97 p_me->p_module = module_Need( p_me, "meta fetcher", 0, VLC_FALSE );
100 vlc_object_destroy( p_me );
103 module_Unneed( p_me, p_me->p_module );
104 vlc_object_destroy( p_me );
106 input_item_SetMetaFetched( p_item, VLC_TRUE );
112 * 0 : Art is in cache
113 * 1 : Art found, need to download
114 * -X : Error/not found
116 int input_ArtFind( playlist_t *p_playlist, input_item_t *p_item )
118 int i_ret = VLC_EGENERIC;
120 char *psz_name, *psz_title, *psz_artist, *psz_album;
122 if( !p_item->p_meta )
125 psz_name = input_item_GetName( p_item );
126 psz_title = input_item_GetTitle( p_item );
127 psz_artist = input_item_GetArtist( p_item );
128 psz_album = input_item_GetAlbum( p_item );
130 if( !psz_name && !psz_title && !psz_artist && !psz_album )
135 /* If we already checked this album in this session, skip */
136 if( psz_artist && psz_album )
138 FOREACH_ARRAY( playlist_album_t album, p_playlist->p_fetcher->albums )
139 if( !strcmp( album.psz_artist, psz_artist ) &&
140 !strcmp( album.psz_album, psz_album ) )
142 msg_Dbg( p_playlist, " %s - %s has already been searched",
143 psz_artist, psz_album );
144 /* TODO-fenrir if we cache art filename too, we can go faster */
149 /* Actually get URL from cache */
150 input_FindArtInCache( p_playlist, p_item );
163 char *psz_arturl = input_item_GetArtURL( p_item );
164 input_FindArtInCache( p_playlist, p_item );
165 if( !EMPTY_STR( psz_arturl ) )
173 p_playlist->p_private = p_item;
174 psz_album = input_item_GetAlbum( p_item );
175 psz_artist = input_item_GetArtist( p_item );
176 psz_name = input_item_GetName( p_item );
177 psz_title = input_item_GetTitle( p_item );
178 if( psz_album && psz_artist )
180 msg_Dbg( p_playlist, "searching art for %s - %s",
181 psz_artist, psz_album );
185 msg_Dbg( p_playlist, "searching art for %s",
186 psz_title ? psz_title : psz_name );
191 p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
196 msg_Dbg( p_playlist, "unable to find art" );
198 /* Record this album */
199 if( psz_artist && psz_album )
202 a.psz_artist = psz_artist;
203 a.psz_album = psz_album;
204 a.b_found = (i_ret == VLC_EGENERIC ? VLC_FALSE : VLC_TRUE );
205 ARRAY_APPEND( p_playlist->p_fetcher->albums, a );
214 module_Unneed( p_playlist, p_module );
215 p_playlist->p_private = NULL;
222 # define MAX_PATH 250
224 #define ArtCacheCreateName(a,b,c,d,e,f) __ArtCacheCreateName(VLC_OBJECT(a),b,c,d,e,f)
225 static void __ArtCacheCreateName( vlc_object_t *p_obj,
226 char psz_filename[MAX_PATH+1],
227 const char *psz_title,
228 const char *psz_artist, const char *psz_album,
229 const char *psz_extension )
231 if( psz_artist && psz_artist )
233 snprintf( psz_filename, MAX_PATH,
234 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art" DIR_SEP "artistalbum"
235 DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
236 p_obj->p_libvlc->psz_homedir,
237 psz_artist, psz_album, psz_extension ? psz_extension : "" );
241 /* We will use the psz_title name to store the art */
242 snprintf( psz_filename, MAX_PATH,
243 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art" DIR_SEP "title"
244 DIR_SEP "%s" DIR_SEP "art%s",
245 p_obj->p_libvlc->psz_homedir,
246 psz_title, psz_extension ? psz_extension : "" );
249 #define ArtCacheCreatePath(a,b,c,d) __ArtCacheCreatePath(VLC_OBJECT(a),b,c,d)
250 static void __ArtCacheCreatePath( vlc_object_t *p_obj,
251 const char *psz_title,
252 const char *psz_artist, const char *psz_album )
254 char psz_dir[MAX_PATH+1];
255 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
256 p_obj->p_libvlc->psz_homedir );
257 utf8_mkdir( psz_dir );
258 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
259 p_obj->p_libvlc->psz_homedir );
260 utf8_mkdir( psz_dir );
262 if( psz_artist && psz_artist )
264 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
265 "art" DIR_SEP "artistalbum",
266 p_obj->p_libvlc->psz_homedir );
267 utf8_mkdir( psz_dir );
268 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
269 "art" DIR_SEP "artistalbum" DIR_SEP "%s",
270 p_obj->p_libvlc->psz_homedir, psz_artist );
271 utf8_mkdir( psz_dir );
272 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
273 "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
274 p_obj->p_libvlc->psz_homedir,
275 psz_artist, psz_album );
276 utf8_mkdir( psz_dir );
280 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
281 "art" DIR_SEP "title",
282 p_obj->p_libvlc->psz_homedir );
283 utf8_mkdir( psz_dir );
284 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
285 "art" DIR_SEP "title" DIR_SEP "%s",
286 p_obj->p_libvlc->psz_homedir, psz_title );
287 utf8_mkdir( psz_dir );
290 static char *ArtCacheCreateString( const char *psz )
292 char *dup = strdup(psz);
295 /* Doesn't create a filename with invalid characters
296 * TODO: several filesystems forbid several characters: list them all
298 for( i = 0; dup[i] != '\0'; i++ )
306 static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
311 char psz_filename[MAX_PATH+1];
314 const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
316 if( !p_item->p_meta ) return VLC_EGENERIC;
318 psz_artist = input_item_GetArtist( p_item );
319 psz_album = input_item_GetAlbum( p_item );
320 psz_title = input_item_GetTitle( p_item );
321 if( !psz_title ) psz_title = input_item_GetName( p_item );
323 if( !psz_title && ( !psz_album || !psz_artist ) )
332 for( i = 0; i < 5; i++ )
334 ArtCacheCreateName( p_obj, psz_filename, psz_title /* Used if none artist nor album is defined */,
335 psz_artist, psz_album, ppsz_type[i] );
337 /* Check if file exists */
338 if( utf8_stat( psz_filename+7, &a ) == 0 )
340 input_item_SetArtURL( p_item, psz_filename );
352 * Download the art using the URL or an art downloaded
353 * This function should be called only if data is not already in cache
355 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
357 int i_status = VLC_EGENERIC;
359 char psz_filename[MAX_PATH+1];
360 char *psz_artist = NULL;
361 char *psz_album = NULL;
362 char *psz_title = NULL;
363 char *psz_artist_m, *psz_album_m, *psz_title_m, *psz_name_m, *psz_arturl_m;
366 psz_artist_m = input_item_GetArtist( p_item );
367 psz_album_m = input_item_GetAlbum( p_item );
368 psz_title_m = input_item_GetTitle( p_item );
369 psz_name_m = input_item_GetName( p_item );
371 if( psz_artist_m ) psz_artist = ArtCacheCreateString( psz_artist_m );
372 if( psz_album_m ) psz_album = ArtCacheCreateString( psz_album_m );
373 if( psz_title_m ) psz_title = ArtCacheCreateString( psz_title_m );
374 else if( psz_name_m ) psz_title = ArtCacheCreateString( psz_name_m );
376 free( psz_artist_m );
381 if( !psz_title && (!psz_artist || !psz_album) )
389 psz_arturl_m = input_item_GetArtURL( p_item );
390 assert( !EMPTY_STR( psz_arturl_m ) );
392 psz_type = strrchr( psz_arturl_m, '.' );
395 ArtCacheCreateName( p_playlist, psz_filename, psz_title /* Used only if needed*/,
396 psz_artist, psz_album, psz_type );
399 ArtCacheCreatePath( p_playlist, psz_title, psz_artist, psz_album );
406 if( !strncmp( psz_arturl_m , "APIC", 4 ) )
408 msg_Warn( p_playlist, "APIC fetch not supported yet" );
409 free( psz_arturl_m );
413 p_stream = stream_UrlNew( p_playlist, psz_arturl_m );
416 uint8_t p_buffer[65536];
418 FILE *p_file = utf8_fopen( psz_filename+7, "w" );
419 if( p_file == NULL ) {
420 msg_Err( p_playlist, "Unable write album art in %s" );
421 free( psz_arturl_m );
425 while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
427 if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
433 if( fclose( p_file ) && !err )
435 stream_Delete( p_stream );
438 msg_Err( p_playlist, "%s: %s", psz_filename, strerror( err ) );
440 msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
442 input_item_SetArtURL( p_item, psz_filename );
443 i_status = VLC_SUCCESS;
445 free( psz_arturl_m );
449 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
451 input_item_t *p_item = p_input->p->input.p_item;
453 char *psz_artist = NULL;
454 char *psz_album = NULL;
455 char *psz_title = NULL;
456 char *psz_type = NULL;
457 char *psz_artist_m, *psz_album_m, *psz_title_m, *psz_name_m;
458 char psz_filename[MAX_PATH+1];
460 input_attachment_t *p_attachment;
464 /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH
465 * and then set it here to to be faster */
467 psz_arturl = input_item_GetArtURL( p_item );
468 if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
471 msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
474 input_item_SetArtURL( p_item, NULL );
476 if( input_item_IsArtFetched( p_item ) )
478 /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
480 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
481 input_FindArtInCache( p_input, p_item );
487 for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ )
489 if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
490 &psz_arturl[strlen("attachment://")] ) )
492 p_attachment = p_input->p->attachment[i_idx];
496 if( !p_attachment || p_attachment->i_data <= 0 )
498 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
502 psz_artist_m = input_item_GetArtist( p_item );
503 psz_album_m = input_item_GetAlbum( p_item );
504 psz_title_m = input_item_GetTitle( p_item );
505 psz_name_m = input_item_GetName( p_item );
507 if( psz_artist_m ) psz_artist = ArtCacheCreateString( psz_artist_m );
508 if( psz_album_m ) psz_album = ArtCacheCreateString( psz_album_m );
509 if( psz_title_m ) psz_title = ArtCacheCreateString( psz_title_m );
510 else if( psz_name_m ) psz_title = ArtCacheCreateString( psz_name_m );
512 free( psz_artist_m );
517 if( (!psz_artist || !psz_album ) && !psz_title )
521 psz_type = strrchr( psz_arturl, '.' );
522 ArtCacheCreateName( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
524 /* Check if we already dumped it */
525 if( !utf8_stat( psz_filename+7, &s ) )
528 ArtCacheCreatePath( p_input, psz_title, psz_artist, psz_album );
530 f = utf8_fopen( psz_filename+7, "w" );
533 if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 )
534 msg_Err( p_input, "%s: %s", psz_filename, strerror( errno ) );
536 msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
541 if( psz_artist ) free( psz_artist );
542 if( psz_album ) free( psz_album );
543 if( psz_title ) free( psz_title );
544 if( psz_arturl ) free( psz_arturl );
548 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
555 #define CHECK( a, b ) \
556 if( !EMPTY_STR( vlc_meta_Get( p_meta, vlc_meta_ ## a ) ) ) \
557 i_meta |= VLC_META_ENGINE_ ## b;
559 CHECK( Title, TITLE )
560 CHECK( Artist, ARTIST )
561 CHECK( Album, COLLECTION )
563 /* As this is not used at the moment, don't uselessly check for it.
564 * Re-enable this when it is used */
565 CHECK( Genre, GENRE )
566 CHECK( Copyright, COPYRIGHT )
567 CHECK( Tracknum, SEQ_NUM )
568 CHECK( Description, DESCRIPTION )
569 CHECK( Rating, RATING )
572 CHECK( Language, LANGUAGE )
574 CHECK( ArtworkURL, ART_URL )