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;
75 assert( p_item->p_meta );
77 uint32_t i_meta = input_CurrentMetaFlags( p_item->p_meta );
78 *pi_mandatory &= ~i_meta;
79 *pi_optional = 0; /// Todo
80 return *pi_mandatory ? VLC_FALSE:VLC_TRUE;
83 int input_MetaFetch( playlist_t *p_playlist, input_item_t *p_item )
85 struct meta_engine_t *p_me;
86 uint32_t i_mandatory, i_optional;
88 assert( p_item->p_meta );
90 input_MetaSatisfied( p_playlist, p_item, &i_mandatory, &i_optional );
91 // Meta shouldn't magically appear
92 assert( i_mandatory );
94 /* FIXME: object creation is overkill, use p_private */
95 p_me = vlc_object_create( p_playlist, VLC_OBJECT_META_ENGINE );
96 p_me->i_flags |= OBJECT_FLAGS_NOINTERACT;
97 p_me->i_flags |= OBJECT_FLAGS_QUIET;
98 p_me->i_mandatory = i_mandatory;
99 p_me->i_optional = i_optional;
101 p_me->p_item = p_item;
102 p_me->p_module = module_Need( p_me, "meta fetcher", 0, VLC_FALSE );
103 if( !p_me->p_module )
105 vlc_object_destroy( p_me );
108 module_Unneed( p_me, p_me->p_module );
109 vlc_object_destroy( p_me );
111 input_item_SetMetaFetched( p_item, VLC_TRUE );
117 * 0 : Art is in cache
118 * 1 : Art found, need to download
119 * -X : Error/not found
121 int input_ArtFind( playlist_t *p_playlist, input_item_t *p_item )
123 int i_ret = VLC_EGENERIC;
126 if( !p_item->p_meta )
129 if( !p_item->psz_name && !input_item_GetTitle( p_item ) &&
130 (!input_item_GetArtist( p_item ) || !input_item_GetAlbum( p_item )) )
133 /* If we already checked this album in this session, skip */
134 if( input_item_GetArtist( p_item ) && input_item_GetAlbum( p_item ) )
136 FOREACH_ARRAY( playlist_album_t album, p_playlist->p_fetcher->albums )
137 if( !strcmp( album.psz_artist, input_item_GetArtist( p_item ) ) &&
138 !strcmp( album.psz_album, input_item_GetAlbum( p_item ) ) )
140 msg_Dbg( p_playlist, " %s - %s has already been searched",
141 input_item_GetArtist( p_item ), input_item_GetAlbum( p_item ) );
142 /* TODO-fenrir if we cache art filename too, we can go faster */
145 /* Actually get URL from cache */
146 input_FindArtInCache( p_playlist, p_item );
157 input_FindArtInCache( p_playlist, p_item );
158 if( !EMPTY_STR(input_item_GetArtURL( p_item )) )
162 p_playlist->p_private = p_item;
163 if( input_item_GetAlbum( p_item ) && input_item_GetArtist( p_item ) )
165 msg_Dbg( p_playlist, "searching art for %s - %s",
166 input_item_GetArtist( p_item ), input_item_GetAlbum( p_item ) );
170 msg_Dbg( p_playlist, "searching art for %s",
171 input_item_GetTitle( p_item ) ? input_item_GetTitle( p_item ) : p_item->psz_name );
174 p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
179 msg_Dbg( p_playlist, "unable to find art" );
181 /* Record this album */
182 if( input_item_GetArtist( p_item ) && input_item_GetAlbum( p_item ) )
185 a.psz_artist = strdup( input_item_GetArtist( p_item ) );
186 a.psz_album = strdup( input_item_GetAlbum( p_item ) );
187 a.b_found = (i_ret == VLC_EGENERIC ? VLC_FALSE : VLC_TRUE );
188 ARRAY_APPEND( p_playlist->p_fetcher->albums, a );
192 module_Unneed( p_playlist, p_module );
193 p_playlist->p_private = NULL;
200 # define MAX_PATH 250
202 #define ArtCacheCreateName(a,b,c,d,e,f) __ArtCacheCreateName(VLC_OBJECT(a),b,c,d,e,f)
203 static void __ArtCacheCreateName( vlc_object_t *p_obj,
204 char psz_filename[MAX_PATH+1],
205 const char *psz_title,
206 const char *psz_artist, const char *psz_album,
207 const char *psz_extension )
209 if( psz_artist && psz_artist )
211 snprintf( psz_filename, MAX_PATH,
212 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art" DIR_SEP "artistalbum"
213 DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
214 p_obj->p_libvlc->psz_homedir,
215 psz_artist, psz_album, psz_extension ? psz_extension : "" );
219 /* We will use the psz_title name to store the art */
220 snprintf( psz_filename, MAX_PATH,
221 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art" DIR_SEP "title"
222 DIR_SEP "%s" DIR_SEP "art%s",
223 p_obj->p_libvlc->psz_homedir,
224 psz_title, psz_extension ? psz_extension : "" );
227 #define ArtCacheCreatePath(a,b,c,d) __ArtCacheCreatePath(VLC_OBJECT(a),b,c,d)
228 static void __ArtCacheCreatePath( vlc_object_t *p_obj,
229 const char *psz_title,
230 const char *psz_artist, const char *psz_album )
232 char psz_dir[MAX_PATH+1];
233 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
234 p_obj->p_libvlc->psz_homedir );
235 utf8_mkdir( psz_dir );
236 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
237 p_obj->p_libvlc->psz_homedir );
238 utf8_mkdir( psz_dir );
240 if( psz_artist && psz_artist )
242 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
243 "art" DIR_SEP "artistalbum",
244 p_obj->p_libvlc->psz_homedir );
245 utf8_mkdir( psz_dir );
246 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
247 "art" DIR_SEP "artistalbum" DIR_SEP "%s",
248 p_obj->p_libvlc->psz_homedir, psz_artist );
249 utf8_mkdir( psz_dir );
250 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
251 "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
252 p_obj->p_libvlc->psz_homedir,
253 psz_artist, psz_album );
254 utf8_mkdir( psz_dir );
258 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
259 "art" DIR_SEP "title",
260 p_obj->p_libvlc->psz_homedir );
261 utf8_mkdir( psz_dir );
262 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
263 "art" DIR_SEP "title" DIR_SEP "%s",
264 p_obj->p_libvlc->psz_homedir, psz_title );
265 utf8_mkdir( psz_dir );
268 static char *ArtCacheCreateString( const char *psz )
270 char *dup = strdup(psz);
273 /* Doesn't create a filename with invalid characters
274 * TODO: several filesystems forbid several characters: list them all
276 for( i = 0; dup[i] != '\0'; i++ )
284 static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
286 const char *psz_artist;
287 const char *psz_album;
288 const char *psz_title;
289 char psz_filename[MAX_PATH+1];
292 const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
294 if( !p_item->p_meta ) return VLC_EGENERIC;
296 psz_artist = input_item_GetArtist( p_item );
297 psz_album = input_item_GetAlbum( p_item );
298 psz_title = input_item_GetTitle( p_item );
299 if( !psz_title ) psz_title = p_item->psz_name;
301 if( (!psz_artist || !psz_album) && !psz_title ) return VLC_EGENERIC;
303 for( i = 0; i < 5; i++ )
305 ArtCacheCreateName( p_obj, psz_filename, psz_title /* Used if none artist nor album is defined */,
306 psz_artist, psz_album, ppsz_type[i] );
308 /* Check if file exists */
309 if( utf8_stat( psz_filename+7, &a ) == 0 )
311 input_item_SetArtURL( p_item, psz_filename );
319 * Download the art using the URL or an art downloaded
320 * This function should be called only if data is not already in cache
322 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
324 int i_status = VLC_EGENERIC;
326 char psz_filename[MAX_PATH+1];
327 char *psz_artist = NULL;
328 char *psz_album = NULL;
329 char *psz_title = NULL;
331 if( input_item_GetArtist( p_item ) )
332 psz_artist = ArtCacheCreateString( input_item_GetArtist( p_item ) );
333 if( input_item_GetAlbum( p_item ) )
334 psz_album = ArtCacheCreateString( input_item_GetAlbum( p_item ) );
335 if( input_item_GetTitle( p_item ) )
336 psz_title = ArtCacheCreateString( input_item_GetTitle( p_item ) );
337 else if( p_item->psz_name )
338 psz_title = ArtCacheCreateString( p_item->psz_name );
340 if( (!psz_title || !psz_album) && !psz_artist )
348 assert( !EMPTY_STR(input_item_GetArtURL( p_item )) );
350 psz_type = strrchr( input_item_GetArtURL( p_item ), '.' );
353 ArtCacheCreateName( p_playlist, psz_filename, psz_title /* Used only if needed*/,
354 psz_artist, psz_album, psz_type );
357 ArtCacheCreatePath( p_playlist, psz_title, psz_artist, psz_album );
364 if( !strncmp( input_item_GetArtURL( p_item ) , "APIC", 4 ) )
366 msg_Warn( p_playlist, "APIC fetch not supported yet" );
370 p_stream = stream_UrlNew( p_playlist, input_item_GetArtURL( p_item ) );
373 uint8_t p_buffer[65536];
375 FILE *p_file = utf8_fopen( psz_filename+7, "w" );
377 while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
379 if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
385 if( fclose( p_file ) && !err )
387 stream_Delete( p_stream );
390 msg_Err( p_playlist, "%s: %s", psz_filename, strerror( err ) );
392 msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
394 input_item_SetArtURL( p_item, psz_filename );
395 i_status = VLC_SUCCESS;
400 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
402 input_item_t *p_item = p_input->p->input.p_item;
404 char *psz_artist = NULL;
405 char *psz_album = NULL;
406 char *psz_title = NULL;
407 char *psz_type = NULL;
408 char psz_filename[MAX_PATH+1];
410 input_attachment_t *p_attachment;
414 /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH
415 * and then set it here to to be faster */
417 psz_arturl = strdup( input_item_GetArtURL( p_item ) );
418 if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
420 msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
423 input_item_SetArtURL( p_item, NULL );
425 if( input_item_IsArtFetched( p_item ) )
427 /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
429 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
430 input_FindArtInCache( p_input, p_item );
436 for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ )
438 if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
439 &psz_arturl[strlen("attachment://")] ) )
441 p_attachment = p_input->p->attachment[i_idx];
445 if( !p_attachment || p_attachment->i_data <= 0 )
447 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
451 if( input_item_GetArtist( p_item ) )
452 psz_artist = ArtCacheCreateString( input_item_GetArtist( p_item ) );
453 if( input_item_GetAlbum( p_item ) )
454 psz_album = ArtCacheCreateString( input_item_GetAlbum( p_item ) );
455 if( input_item_GetTitle( p_item ) )
456 psz_title = ArtCacheCreateString( input_item_GetTitle( p_item ) );
457 else if( p_item->psz_name )
458 psz_title = ArtCacheCreateString( p_item->psz_name );
460 if( (!psz_artist || !psz_album ) && !psz_title )
464 psz_type = strrchr( psz_arturl, '.' );
465 ArtCacheCreateName( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
467 /* Check if we already dumped it */
468 if( !utf8_stat( psz_filename+7, &s ) )
471 ArtCacheCreatePath( p_input, psz_title, psz_artist, psz_album );
473 f = utf8_fopen( psz_filename+7, "w" );
476 if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 )
477 msg_Err( p_input, "%s: %s", psz_filename, strerror( errno ) );
479 msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
484 if( psz_artist ) free( psz_artist );
485 if( psz_album ) free( psz_album );
486 if( psz_title ) free( psz_title );
487 if( psz_arturl ) free( psz_arturl );
491 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 )