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 <vlc_strings.h>
32 #include "../playlist/playlist_internal.h"
35 #ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
39 static const char * meta_type_to_string[VLC_META_TYPE_COUNT] =
41 [vlc_meta_Title] = N_("Title"),
42 [vlc_meta_Artist] = N_("Artist"),
43 [vlc_meta_Genre] = N_("Genre"),
44 [vlc_meta_Copyright] = N_("Copyright"),
45 [vlc_meta_Album] = N_("Album"),
46 [vlc_meta_TrackNumber] = N_("Track number"),
47 [vlc_meta_Description] = N_("Description"),
48 [vlc_meta_Rating] = N_("Rating"),
49 [vlc_meta_Date] = N_("Date"),
50 [vlc_meta_Setting] = N_("Setting"),
51 [vlc_meta_URL] = N_("URL"),
52 [vlc_meta_Language] = N_("Language"),
53 [vlc_meta_NowPlaying] = N_("Now Playing"),
54 [vlc_meta_Publisher] = N_("Publisher"),
55 [vlc_meta_EncodedBy] = N_("Encoded by"),
56 [vlc_meta_ArtworkURL] = N_("Artwork URL"),
57 [vlc_meta_TrackID] = N_("Track ID"),
61 input_MetaTypeToLocalizedString( vlc_meta_type_t meta_type )
63 return _(meta_type_to_string[meta_type]);
66 #define input_FindArtInCache(a,b) __input_FindArtInCache(VLC_OBJECT(a),b)
67 static int __input_FindArtInCache( vlc_object_t *, input_item_t *p_item );
69 vlc_bool_t input_MetaSatisfied( playlist_t *p_playlist, input_item_t *p_item,
70 uint32_t *pi_mandatory, uint32_t *pi_optional )
73 *pi_mandatory = VLC_META_ENGINE_TITLE | VLC_META_ENGINE_ARTIST;
75 uint32_t i_meta = input_CurrentMetaFlags( p_item->p_meta );
76 *pi_mandatory &= ~i_meta;
77 *pi_optional = 0; /// Todo
78 return *pi_mandatory ? VLC_FALSE:VLC_TRUE;
81 int input_MetaFetch( playlist_t *p_playlist, input_item_t *p_item )
83 struct meta_engine_t *p_me;
84 uint32_t i_mandatory, i_optional;
86 input_MetaSatisfied( p_playlist, p_item, &i_mandatory, &i_optional );
87 // Meta shouldn't magically appear
88 assert( i_mandatory );
90 /* FIXME: object creation is overkill, use p_private */
91 p_me = vlc_object_create( p_playlist, VLC_OBJECT_META_ENGINE );
92 p_me->i_flags |= OBJECT_FLAGS_NOINTERACT;
93 p_me->i_flags |= OBJECT_FLAGS_QUIET;
94 p_me->i_mandatory = i_mandatory;
95 p_me->i_optional = i_optional;
97 p_me->p_item = p_item;
98 p_me->p_module = module_Need( p_me, "meta fetcher", 0, VLC_FALSE );
101 vlc_object_destroy( p_me );
104 module_Unneed( p_me, p_me->p_module );
105 vlc_object_destroy( p_me );
107 input_item_SetMetaFetched( p_item, VLC_TRUE );
113 * 0 : Art is in cache or is a local file
114 * 1 : Art found, need to download
115 * -X : Error/not found
117 int input_ArtFind( playlist_t *p_playlist, input_item_t *p_item )
119 int i_ret = VLC_EGENERIC;
121 char *psz_title, *psz_artist, *psz_album;
123 psz_artist = input_item_GetArtist( p_item );
124 psz_album = input_item_GetAlbum( p_item );
125 psz_title = input_item_GetTitle( p_item );
127 psz_title = input_item_GetName( p_item );
129 if( !psz_title && !psz_artist && !psz_album )
134 /* If we already checked this album in this session, skip */
135 if( psz_artist && psz_album )
137 FOREACH_ARRAY( playlist_album_t album, p_playlist->p_fetcher->albums )
138 if( !strcmp( album.psz_artist, psz_artist ) &&
139 !strcmp( album.psz_album, psz_album ) )
141 msg_Dbg( p_playlist, " %s - %s has already been searched",
142 psz_artist, psz_album );
143 /* TODO-fenrir if we cache art filename too, we can go faster */
148 if( !strncmp( album.psz_arturl, "file://", 7 ) )
149 input_item_SetArtURL( p_item, album.psz_arturl );
150 else /* Actually get URL from cache */
151 input_FindArtInCache( p_playlist, p_item );
164 input_FindArtInCache( p_playlist, p_item );
166 char *psz_arturl = input_item_GetArtURL( p_item );
167 if( !EMPTY_STR( psz_arturl ) )
170 return 0; /* Art is in cache, no need to go further */
175 p_playlist->p_private = p_item;
176 psz_album = input_item_GetAlbum( p_item );
177 psz_artist = input_item_GetArtist( p_item );
178 psz_title = input_item_GetTitle( p_item );
180 psz_title = input_item_GetName( p_item );
182 if( psz_album && psz_artist )
184 msg_Dbg( p_playlist, "searching art for %s - %s",
185 psz_artist, psz_album );
189 msg_Dbg( p_playlist, "searching art for %s",
194 p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
199 msg_Dbg( p_playlist, "unable to find art" );
201 /* Record this album */
202 if( psz_artist && psz_album )
205 a.psz_artist = psz_artist;
206 a.psz_album = psz_album;
207 a.psz_arturl = input_item_GetArtURL( p_item );
208 a.b_found = (i_ret == VLC_EGENERIC ? VLC_FALSE : VLC_TRUE );
209 ARRAY_APPEND( p_playlist->p_fetcher->albums, a );
218 module_Unneed( p_playlist, p_module );
219 p_playlist->p_private = NULL;
226 # define MAX_PATH 250
228 static void ArtCacheCreateDir( const char *psz_dir )
230 char newdir[MAX_PATH+1];
231 strcpy( newdir, psz_dir );
232 char * psz_newdir = newdir;
233 char * psz = psz_newdir;
237 while( *psz && *psz != DIR_SEP_CHAR) psz++;
240 if( !EMPTY_STR( psz_newdir ) ) utf8_mkdir( psz_newdir );
244 utf8_mkdir( psz_dir );
247 static char * ArtCacheGetSanitizedFileName( const char *psz )
249 char *dup = strdup(psz);
252 filename_sanitize( dup );
254 /* Doesn't create a filename with invalid characters
255 * TODO: several filesystems forbid several characters: list them all
257 for( i = 0; dup[i] != '\0'; i++ )
259 if( dup[i] == DIR_SEP_CHAR )
265 #define ArtCacheGetDirPath(a,b,c,d,e) __ArtCacheGetDirPath(VLC_OBJECT(a),b,c,d,e)
266 static void __ArtCacheGetDirPath( vlc_object_t *p_obj,
268 const char *psz_title,
269 const char *psz_artist, const char *psz_album )
271 if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
273 char * psz_album_sanitized = ArtCacheGetSanitizedFileName( psz_album );
274 char * psz_artist_sanitized = ArtCacheGetSanitizedFileName( psz_artist );
276 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP
277 "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
278 p_obj->p_libvlc->psz_cachedir,
279 psz_artist_sanitized, psz_album_sanitized );
280 free( psz_album_sanitized );
281 free( psz_artist_sanitized );
285 char * psz_title_sanitized = ArtCacheGetSanitizedFileName( psz_title );
286 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP
287 "art" DIR_SEP "title" DIR_SEP "%s",
288 p_obj->p_libvlc->psz_cachedir,
289 psz_title_sanitized );
290 free( psz_title_sanitized );
296 #define ArtCacheGetFilePath(a,b,c,d,e,f) __ArtCacheGetFilePath(VLC_OBJECT(a),b,c,d,e,f)
297 static void __ArtCacheGetFilePath( vlc_object_t *p_obj,
299 const char *psz_title,
300 const char *psz_artist, const char *psz_album,
301 const char *psz_extension )
303 char psz_dir[MAX_PATH+1];
305 ArtCacheGetDirPath( p_obj, psz_dir, psz_title, psz_artist, psz_album );
309 psz_ext = strndup( psz_extension, 6 );
310 filename_sanitize( psz_ext );
312 else psz_ext = strdup( "" );
314 snprintf( psz_filename, MAX_PATH, "file://%s" DIR_SEP "art%s",
320 static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
325 char psz_dirpath[MAX_PATH+1];
326 char psz_filepath[MAX_PATH+1];
330 psz_artist = input_item_GetArtist( p_item );
331 psz_album = input_item_GetAlbum( p_item );
332 psz_title = input_item_GetTitle( p_item );
333 if( !psz_title ) psz_title = input_item_GetName( p_item );
335 if( !psz_title && ( !psz_album || !psz_artist ) )
343 ArtCacheGetDirPath( p_obj, psz_dirpath, psz_title,
344 psz_artist, psz_album );
350 /* Check if file exists */
351 p_dir = utf8_opendir( psz_dirpath );
355 while( (psz_filename = utf8_readdir( p_dir )) )
357 if( !strncmp( psz_filename, "art", 3 ) )
359 snprintf( psz_filepath, MAX_PATH, "file://%s" DIR_SEP "%s",
360 psz_dirpath, psz_filename );
361 input_item_SetArtURL( p_item, psz_filepath );
362 free( psz_filename );
366 free( psz_filename );
375 * Download the art using the URL or an art downloaded
376 * This function should be called only if data is not already in cache
378 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
380 int i_status = VLC_EGENERIC;
382 char psz_filename[MAX_PATH+1];
383 char *psz_artist = NULL;
384 char *psz_album = NULL;
385 char *psz_title = NULL;
389 psz_artist = input_item_GetArtist( p_item );
390 psz_album = input_item_GetAlbum( p_item );
391 psz_title = input_item_GetTitle( p_item );
393 psz_title = input_item_GetName( p_item );
395 if( !psz_title && (!psz_artist || !psz_album) )
403 psz_arturl = input_item_GetArtURL( p_item );
404 assert( !EMPTY_STR( psz_arturl ) );
406 if( !strncmp( psz_arturl , "file://", 7 ) )
408 msg_Dbg( p_playlist, "Album art is local file, no need to cache" );
412 else if( !strncmp( psz_arturl , "APIC", 4 ) )
414 msg_Warn( p_playlist, "APIC fetch not supported yet" );
419 psz_type = strrchr( psz_arturl, '.' );
420 if( strlen( psz_type ) > 5 )
421 psz_type = NULL; /* remove extension if it's > to 4 characters */
423 /* Warning: psz_title, psz_artist, psz_album may change in ArtCache*() */
425 ArtCacheGetDirPath( p_playlist, psz_filename, psz_title, psz_artist,
427 ArtCacheCreateDir( psz_filename );
428 ArtCacheGetFilePath( p_playlist, psz_filename, psz_title, psz_artist,
429 psz_album, psz_type );
435 p_stream = stream_UrlNew( p_playlist, psz_arturl );
438 uint8_t p_buffer[65536];
440 FILE *p_file = utf8_fopen( psz_filename+7, "w" );
441 if( p_file == NULL ) {
442 msg_Err( p_playlist, "Unable write album art in %s",
448 while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
450 if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
456 if( fclose( p_file ) && !err )
458 stream_Delete( p_stream );
463 msg_Err( p_playlist, "%s: %m", psz_filename );
466 msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
468 input_item_SetArtURL( p_item, psz_filename );
469 i_status = VLC_SUCCESS;
475 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
477 input_item_t *p_item = p_input->p->input.p_item;
479 char *psz_artist = NULL;
480 char *psz_album = NULL;
481 char *psz_title = NULL;
482 char *psz_type = NULL;
483 char psz_filename[MAX_PATH+1];
485 input_attachment_t *p_attachment;
489 /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH
490 * and then set it here to to be faster */
492 psz_arturl = input_item_GetArtURL( p_item );
493 if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
496 msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
499 input_item_SetArtURL( p_item, NULL );
501 if( input_item_IsArtFetched( p_item ) )
503 /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
505 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
506 input_FindArtInCache( p_input, p_item );
512 for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ )
514 if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
515 &psz_arturl[strlen("attachment://")] ) )
517 p_attachment = p_input->p->attachment[i_idx];
521 if( !p_attachment || p_attachment->i_data <= 0 )
523 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
527 psz_artist = input_item_GetArtist( p_item );
528 psz_album = input_item_GetAlbum( p_item );
529 psz_title = input_item_GetTitle( p_item );
531 psz_title = input_item_GetName( p_item );
533 if( (!psz_artist || !psz_album ) && !psz_title )
537 psz_type = strrchr( psz_arturl, '.' );
539 ArtCacheGetDirPath( p_input, psz_filename, psz_title, psz_artist, psz_album );
540 ArtCacheCreateDir( psz_filename );
541 ArtCacheGetFilePath( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
543 /* Check if we already dumped it */
544 if( !utf8_stat( psz_filename+7, &s ) )
547 f = utf8_fopen( psz_filename+7, "w" );
550 if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 )
551 msg_Err( p_input, "%s: %m", psz_filename );
553 msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
558 if( psz_artist ) free( psz_artist );
559 if( psz_album ) free( psz_album );
560 if( psz_title ) free( psz_title );
561 if( psz_arturl ) free( psz_arturl );
565 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
572 #define CHECK( a, b ) \
573 if( !EMPTY_STR( vlc_meta_Get( p_meta, vlc_meta_ ## a ) ) ) \
574 i_meta |= VLC_META_ENGINE_ ## b;
576 CHECK( Title, TITLE )
577 CHECK( Artist, ARTIST )
578 CHECK( Album, COLLECTION )
580 /* As this is not used at the moment, don't uselessly check for it.
581 * Re-enable this when it is used */
582 CHECK( Genre, GENRE )
583 CHECK( Copyright, COPYRIGHT )
584 CHECK( Tracknum, SEQ_NUM )
585 CHECK( Description, DESCRIPTION )
586 CHECK( Rating, RATING )
589 CHECK( Language, LANGUAGE )
591 CHECK( ArtworkURL, ART_URL )