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;
122 char *psz_name, *psz_title, *psz_artist, *psz_album;
124 if( !p_item->p_meta )
127 psz_name = input_item_GetName( p_item );
128 psz_title = input_item_GetTitle( p_item );
129 psz_artist = input_item_GetArtist( p_item );
130 psz_album = input_item_GetAlbum( p_item );
132 if( !psz_name && !psz_title && !psz_artist && !psz_album )
137 /* If we already checked this album in this session, skip */
138 if( psz_artist && psz_album )
140 FOREACH_ARRAY( playlist_album_t album, p_playlist->p_fetcher->albums )
141 if( !strcmp( album.psz_artist, psz_artist ) &&
142 !strcmp( album.psz_album, psz_album ) )
144 msg_Dbg( p_playlist, " %s - %s has already been searched",
145 psz_artist, psz_album );
146 /* TODO-fenrir if we cache art filename too, we can go faster */
151 /* Actually get URL from cache */
152 input_FindArtInCache( p_playlist, p_item );
165 char *psz_arturl = input_item_GetArtURL( p_item );
166 input_FindArtInCache( p_playlist, p_item );
167 if( !EMPTY_STR( psz_arturl ) )
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_name = input_item_GetName( p_item );
179 psz_title = input_item_GetTitle( p_item );
180 if( psz_album && psz_artist )
182 msg_Dbg( p_playlist, "searching art for %s - %s",
183 psz_artist, psz_album );
187 msg_Dbg( p_playlist, "searching art for %s",
188 psz_title ? psz_title : psz_name );
193 p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
198 msg_Dbg( p_playlist, "unable to find art" );
200 /* Record this album */
201 if( psz_artist && psz_album )
204 a.psz_artist = psz_artist;
205 a.psz_album = psz_album;
206 a.b_found = (i_ret == VLC_EGENERIC ? VLC_FALSE : VLC_TRUE );
207 ARRAY_APPEND( p_playlist->p_fetcher->albums, a );
216 module_Unneed( p_playlist, p_module );
217 p_playlist->p_private = NULL;
224 # define MAX_PATH 250
226 #define ArtCacheCreateName(a,b,c,d,e,f) __ArtCacheCreateName(VLC_OBJECT(a),b,c,d,e,f)
227 static void __ArtCacheCreateName( vlc_object_t *p_obj,
228 char psz_filename[MAX_PATH+1],
229 const char *psz_title,
230 const char *psz_artist, const char *psz_album,
231 const char *psz_extension )
233 if( psz_artist && psz_artist )
235 snprintf( psz_filename, MAX_PATH,
236 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art" DIR_SEP "artistalbum"
237 DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
238 p_obj->p_libvlc->psz_homedir,
239 psz_artist, psz_album, psz_extension ? psz_extension : "" );
243 /* We will use the psz_title name to store the art */
244 snprintf( psz_filename, MAX_PATH,
245 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art" DIR_SEP "title"
246 DIR_SEP "%s" DIR_SEP "art%s",
247 p_obj->p_libvlc->psz_homedir,
248 psz_title, psz_extension ? psz_extension : "" );
251 #define ArtCacheCreatePath(a,b,c,d) __ArtCacheCreatePath(VLC_OBJECT(a),b,c,d)
252 static void __ArtCacheCreatePath( vlc_object_t *p_obj,
253 const char *psz_title,
254 const char *psz_artist, const char *psz_album )
256 char psz_dir[MAX_PATH+1];
257 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
258 p_obj->p_libvlc->psz_homedir );
259 utf8_mkdir( psz_dir );
260 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
261 p_obj->p_libvlc->psz_homedir );
262 utf8_mkdir( psz_dir );
264 if( psz_artist && psz_artist )
266 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
267 "art" DIR_SEP "artistalbum",
268 p_obj->p_libvlc->psz_homedir );
269 utf8_mkdir( psz_dir );
270 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
271 "art" DIR_SEP "artistalbum" DIR_SEP "%s",
272 p_obj->p_libvlc->psz_homedir, psz_artist );
273 utf8_mkdir( psz_dir );
274 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
275 "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
276 p_obj->p_libvlc->psz_homedir,
277 psz_artist, psz_album );
278 utf8_mkdir( psz_dir );
282 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
283 "art" DIR_SEP "title",
284 p_obj->p_libvlc->psz_homedir );
285 utf8_mkdir( psz_dir );
286 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
287 "art" DIR_SEP "title" DIR_SEP "%s",
288 p_obj->p_libvlc->psz_homedir, psz_title );
289 utf8_mkdir( psz_dir );
292 static char *ArtCacheCreateString( const char *psz )
294 char *dup = strdup(psz);
297 /* Doesn't create a filename with invalid characters
298 * TODO: several filesystems forbid several characters: list them all
300 for( i = 0; dup[i] != '\0'; i++ )
308 static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
313 char psz_filename[MAX_PATH+1];
316 const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
318 if( !p_item->p_meta ) return VLC_EGENERIC;
320 psz_artist = input_item_GetArtist( p_item );
321 psz_album = input_item_GetAlbum( p_item );
322 psz_title = input_item_GetTitle( p_item );
323 if( !psz_title ) psz_title = input_item_GetName( p_item );
325 if( !psz_title && ( !psz_album || !psz_artist ) )
334 for( i = 0; i < 5; i++ )
336 ArtCacheCreateName( p_obj, psz_filename, psz_title /* Used if none artist nor album is defined */,
337 psz_artist, psz_album, ppsz_type[i] );
339 /* Check if file exists */
340 if( utf8_stat( psz_filename+7, &a ) == 0 )
342 input_item_SetArtURL( p_item, psz_filename );
354 * Download the art using the URL or an art downloaded
355 * This function should be called only if data is not already in cache
357 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
359 int i_status = VLC_EGENERIC;
361 char psz_filename[MAX_PATH+1];
362 char *psz_artist = NULL;
363 char *psz_album = NULL;
364 char *psz_title = NULL;
365 char *psz_artist_m, *psz_album_m, *psz_title_m, *psz_name_m, *psz_arturl_m;
368 psz_artist_m = input_item_GetArtist( p_item );
369 psz_album_m = input_item_GetAlbum( p_item );
370 psz_title_m = input_item_GetTitle( p_item );
371 psz_name_m = input_item_GetName( p_item );
373 if( psz_artist_m ) psz_artist = ArtCacheCreateString( psz_artist_m );
374 if( psz_album_m ) psz_album = ArtCacheCreateString( psz_album_m );
375 if( psz_title_m ) psz_title = ArtCacheCreateString( psz_title_m );
376 else if( psz_name_m ) psz_title = ArtCacheCreateString( psz_name_m );
378 free( psz_artist_m );
383 if( !psz_title && (!psz_artist || !psz_album) )
391 psz_arturl_m = input_item_GetArtURL( p_item );
392 assert( !EMPTY_STR( psz_arturl_m ) );
394 psz_type = strrchr( psz_arturl_m, '.' );
397 ArtCacheCreateName( p_playlist, psz_filename, psz_title /* Used only if needed*/,
398 psz_artist, psz_album, psz_type );
401 ArtCacheCreatePath( p_playlist, psz_title, psz_artist, psz_album );
408 if( !strncmp( psz_arturl_m , "APIC", 4 ) )
410 msg_Warn( p_playlist, "APIC fetch not supported yet" );
411 free( psz_arturl_m );
415 p_stream = stream_UrlNew( p_playlist, psz_arturl_m );
418 uint8_t p_buffer[65536];
420 FILE *p_file = utf8_fopen( psz_filename+7, "w" );
422 while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
424 if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
430 if( fclose( p_file ) && !err )
432 stream_Delete( p_stream );
435 msg_Err( p_playlist, "%s: %s", psz_filename, strerror( err ) );
437 msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
439 input_item_SetArtURL( p_item, psz_filename );
440 i_status = VLC_SUCCESS;
442 free( psz_arturl_m );
446 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
448 input_item_t *p_item = p_input->p->input.p_item;
450 char *psz_artist = NULL;
451 char *psz_album = NULL;
452 char *psz_title = NULL;
453 char *psz_type = NULL;
454 char *psz_artist_m, *psz_album_m, *psz_title_m, *psz_name_m;
455 char psz_filename[MAX_PATH+1];
457 input_attachment_t *p_attachment;
461 /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH
462 * and then set it here to to be faster */
464 psz_arturl = input_item_GetArtURL( p_item );
465 if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
468 msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
471 input_item_SetArtURL( p_item, NULL );
473 if( input_item_IsArtFetched( p_item ) )
475 /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
477 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
478 input_FindArtInCache( p_input, p_item );
484 for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ )
486 if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
487 &psz_arturl[strlen("attachment://")] ) )
489 p_attachment = p_input->p->attachment[i_idx];
493 if( !p_attachment || p_attachment->i_data <= 0 )
495 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
499 psz_artist_m = input_item_GetArtist( p_item );
500 psz_album_m = input_item_GetAlbum( p_item );
501 psz_title_m = input_item_GetTitle( p_item );
502 psz_name_m = input_item_GetName( p_item );
504 if( psz_artist_m ) psz_artist = ArtCacheCreateString( psz_artist_m );
505 if( psz_album_m ) psz_album = ArtCacheCreateString( psz_album_m );
506 if( psz_title_m ) psz_title = ArtCacheCreateString( psz_title_m );
507 else if( psz_name_m ) psz_title = ArtCacheCreateString( psz_name_m );
509 free( psz_artist_m );
514 if( (!psz_artist || !psz_album ) && !psz_title )
518 psz_type = strrchr( psz_arturl, '.' );
519 ArtCacheCreateName( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
521 /* Check if we already dumped it */
522 if( !utf8_stat( psz_filename+7, &s ) )
525 ArtCacheCreatePath( p_input, psz_title, psz_artist, psz_album );
527 f = utf8_fopen( psz_filename+7, "w" );
530 if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 )
531 msg_Err( p_input, "%s: %s", psz_filename, strerror( errno ) );
533 msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
538 if( psz_artist ) free( psz_artist );
539 if( psz_album ) free( psz_album );
540 if( psz_title ) free( psz_title );
541 if( psz_arturl ) free( psz_arturl );
545 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
552 #define CHECK( a, b ) \
553 if( !EMPTY_STR( vlc_meta_Get( p_meta, vlc_meta_ ## a ) ) ) \
554 i_meta |= VLC_META_ENGINE_ ## b;
556 CHECK( Title, TITLE )
557 CHECK( Artist, ARTIST )
558 CHECK( Album, COLLECTION )
560 /* As this is not used at the moment, don't uselessly check for it.
561 * Re-enable this when it is used */
562 CHECK( Genre, GENRE )
563 CHECK( Copyright, COPYRIGHT )
564 CHECK( Tracknum, SEQ_NUM )
565 CHECK( Description, DESCRIPTION )
566 CHECK( Rating, RATING )
569 CHECK( Language, LANGUAGE )
571 CHECK( ArtworkURL, ART_URL )