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"
34 #include <limits.h> /* PATH_MAX */
36 #ifdef HAVE_SYS_STAT_H
37 # include <sys/stat.h>
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"),
47 [vlc_meta_TrackNumber] = N_("Track number"),
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_("Now Playing"),
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 )
73 VLC_UNUSED(p_playlist);
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 or is a local file
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_title, *psz_artist, *psz_album;
124 psz_artist = input_item_GetArtist( p_item );
125 psz_album = input_item_GetAlbum( p_item );
126 psz_title = input_item_GetTitle( p_item );
128 psz_title = input_item_GetName( p_item );
130 if( !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 if( !strncmp( album.psz_arturl, "file://", 7 ) )
150 input_item_SetArtURL( p_item, album.psz_arturl );
151 else /* Actually get URL from cache */
152 input_FindArtInCache( p_playlist, p_item );
165 input_FindArtInCache( p_playlist, p_item );
167 char *psz_arturl = input_item_GetArtURL( p_item );
170 /* We already have an URL */
171 if( !strncmp( psz_arturl, "file://", strlen( "file://" ) ) )
174 return 0; /* Art is in cache, no need to go further */
179 /* Art need to be put in cache */
184 p_playlist->p_private = p_item;
185 psz_album = input_item_GetAlbum( p_item );
186 psz_artist = input_item_GetArtist( p_item );
187 psz_title = input_item_GetTitle( p_item );
189 psz_title = input_item_GetName( p_item );
191 if( psz_album && psz_artist )
193 msg_Dbg( p_playlist, "searching art for %s - %s",
194 psz_artist, psz_album );
198 msg_Dbg( p_playlist, "searching art for %s",
203 p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
208 msg_Dbg( p_playlist, "unable to find art" );
210 /* Record this album */
211 if( psz_artist && psz_album )
214 a.psz_artist = psz_artist;
215 a.psz_album = psz_album;
216 a.psz_arturl = input_item_GetArtURL( p_item );
217 a.b_found = (i_ret == VLC_EGENERIC ? VLC_FALSE : VLC_TRUE );
218 ARRAY_APPEND( p_playlist->p_fetcher->albums, a );
227 module_Unneed( p_playlist, p_module );
228 p_playlist->p_private = NULL;
234 static void ArtCacheCreateDir( const char *psz_dir )
236 char newdir[strlen( psz_dir ) + 1];
237 strcpy( newdir, psz_dir );
238 char * psz_newdir = newdir;
239 char * psz = psz_newdir;
243 while( *psz && *psz != DIR_SEP_CHAR) psz++;
246 if( !EMPTY_STR( psz_newdir ) )
247 utf8_mkdir( psz_newdir, 0700 );
251 utf8_mkdir( psz_dir, 0700 );
254 static char * ArtCacheGetSanitizedFileName( const char *psz )
256 char *dup = strdup(psz);
259 filename_sanitize( dup );
261 /* Doesn't create a filename with invalid characters
262 * TODO: several filesystems forbid several characters: list them all
264 for( i = 0; dup[i] != '\0'; i++ )
266 if( dup[i] == DIR_SEP_CHAR )
272 #define ArtCacheGetDirPath(a,b,c,d,e) __ArtCacheGetDirPath(VLC_OBJECT(a),b,c,d,e)
273 static void __ArtCacheGetDirPath( vlc_object_t *p_obj,
275 const char *psz_title,
276 const char *psz_artist, const char *psz_album )
278 if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
280 char * psz_album_sanitized = ArtCacheGetSanitizedFileName( psz_album );
281 char * psz_artist_sanitized = ArtCacheGetSanitizedFileName( psz_artist );
283 snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
284 "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
285 p_obj->p_libvlc->psz_cachedir,
286 psz_artist_sanitized, psz_album_sanitized );
287 free( psz_album_sanitized );
288 free( psz_artist_sanitized );
292 char * psz_title_sanitized = ArtCacheGetSanitizedFileName( psz_title );
293 snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
294 "art" DIR_SEP "title" DIR_SEP "%s",
295 p_obj->p_libvlc->psz_cachedir,
296 psz_title_sanitized );
297 free( psz_title_sanitized );
303 #define ArtCacheGetFilePath(a,b,c,d,e,f) __ArtCacheGetFilePath(VLC_OBJECT(a),b,c,d,e,f)
304 static void __ArtCacheGetFilePath( vlc_object_t *p_obj,
306 const char *psz_title,
307 const char *psz_artist, const char *psz_album,
308 const char *psz_extension )
310 char psz_dir[PATH_MAX+1];
312 ArtCacheGetDirPath( p_obj, psz_dir, psz_title, psz_artist, psz_album );
316 psz_ext = strndup( psz_extension, 6 );
317 filename_sanitize( psz_ext );
319 else psz_ext = strdup( "" );
321 snprintf( psz_filename, PATH_MAX, "file://%s" DIR_SEP "art%s",
327 static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
332 char psz_dirpath[PATH_MAX+1];
333 char psz_filepath[PATH_MAX+1];
337 psz_artist = input_item_GetArtist( p_item );
338 psz_album = input_item_GetAlbum( p_item );
339 psz_title = input_item_GetTitle( p_item );
340 if( !psz_title ) psz_title = input_item_GetName( p_item );
342 if( !psz_title && ( !psz_album || !psz_artist ) )
350 ArtCacheGetDirPath( p_obj, psz_dirpath, psz_title,
351 psz_artist, psz_album );
357 /* Check if file exists */
358 p_dir = utf8_opendir( psz_dirpath );
362 while( (psz_filename = utf8_readdir( p_dir )) )
364 if( !strncmp( psz_filename, "art", 3 ) )
366 snprintf( psz_filepath, PATH_MAX, "file://%s" DIR_SEP "%s",
367 psz_dirpath, psz_filename );
368 input_item_SetArtURL( p_item, psz_filepath );
369 free( psz_filename );
373 free( psz_filename );
382 * Download the art using the URL or an art downloaded
383 * This function should be called only if data is not already in cache
385 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
387 int i_status = VLC_EGENERIC;
389 char psz_filename[PATH_MAX+1];
390 char *psz_artist = NULL;
391 char *psz_album = NULL;
392 char *psz_title = NULL;
396 psz_artist = input_item_GetArtist( p_item );
397 psz_album = input_item_GetAlbum( p_item );
398 psz_title = input_item_GetTitle( p_item );
400 psz_title = input_item_GetName( p_item );
402 if( !psz_title && (!psz_artist || !psz_album) )
410 psz_arturl = input_item_GetArtURL( p_item );
411 assert( !EMPTY_STR( psz_arturl ) );
413 if( !strncmp( psz_arturl , "file://", 7 ) )
415 msg_Dbg( p_playlist, "Album art is local file, no need to cache" );
419 else if( !strncmp( psz_arturl , "APIC", 4 ) )
421 msg_Warn( p_playlist, "APIC fetch not supported yet" );
426 psz_type = strrchr( psz_arturl, '.' );
427 if( strlen( psz_type ) > 5 )
428 psz_type = NULL; /* remove extension if it's > to 4 characters */
430 /* Warning: psz_title, psz_artist, psz_album may change in ArtCache*() */
432 ArtCacheGetDirPath( p_playlist, psz_filename, psz_title, psz_artist,
434 ArtCacheCreateDir( psz_filename );
435 ArtCacheGetFilePath( p_playlist, psz_filename, psz_title, psz_artist,
436 psz_album, psz_type );
442 p_stream = stream_UrlNew( p_playlist, psz_arturl );
445 uint8_t p_buffer[65536];
447 FILE *p_file = utf8_fopen( psz_filename+7, "w" );
448 if( p_file == NULL ) {
449 msg_Err( p_playlist, "Unable write album art in %s",
455 while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
457 if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
463 if( fclose( p_file ) && !err )
465 stream_Delete( p_stream );
470 msg_Err( p_playlist, "%s: %m", psz_filename );
473 msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
475 input_item_SetArtURL( p_item, psz_filename );
476 i_status = VLC_SUCCESS;
482 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
484 input_item_t *p_item = p_input->p->input.p_item;
485 const char *psz_arturl;
486 const char *psz_artist = NULL;
487 const char *psz_album = NULL;
488 const char *psz_title = NULL;
489 char *psz_type = NULL;
490 char psz_filename[PATH_MAX+1];
492 input_attachment_t *p_attachment;
496 /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH
497 * and then set it here to to be faster */
499 psz_arturl = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
501 if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
503 msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
507 if( input_item_IsArtFetched( p_item ) )
509 /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
511 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
512 input_FindArtInCache( p_input, p_item );
517 for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ )
519 if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
520 &psz_arturl[strlen("attachment://")] ) )
522 p_attachment = p_input->p->attachment[i_idx];
526 if( !p_attachment || p_attachment->i_data <= 0 )
528 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
532 psz_artist = vlc_meta_Get( p_item->p_meta, vlc_meta_Artist );
533 psz_album = vlc_meta_Get( p_item->p_meta, vlc_meta_Album );
534 psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
535 if( !strcmp( p_attachment->psz_mime, "image/jpeg" ) )
536 psz_type = strdup( ".jpg" );
537 else if( !strcmp( p_attachment->psz_mime, "image/png" ) )
538 psz_type = strdup( ".png" );
541 psz_title = p_item->psz_name;
543 if( (!psz_artist || !psz_album ) && !psz_title )
546 ArtCacheGetDirPath( p_input, psz_filename, psz_title, psz_artist, psz_album );
547 ArtCacheCreateDir( psz_filename );
548 ArtCacheGetFilePath( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
551 /* Check if we already dumped it */
552 if( !utf8_stat( psz_filename+7, &s ) )
554 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_filename );
558 f = utf8_fopen( psz_filename+7, "w" );
561 if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 )
562 msg_Err( p_input, "%s: %m", psz_filename );
565 msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
566 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_filename );
573 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
580 #define CHECK( a, b ) \
581 if( !EMPTY_STR( vlc_meta_Get( p_meta, vlc_meta_ ## a ) ) ) \
582 i_meta |= VLC_META_ENGINE_ ## b;
584 CHECK( Title, TITLE )
585 CHECK( Artist, ARTIST )
586 CHECK( Album, COLLECTION )
588 /* As this is not used at the moment, don't uselessly check for it.
589 * Re-enable this when it is used */
590 CHECK( Genre, GENRE )
591 CHECK( Copyright, COPYRIGHT )
592 CHECK( Tracknum, SEQ_NUM )
593 CHECK( Description, DESCRIPTION )
594 CHECK( Rating, RATING )
597 CHECK( Language, LANGUAGE )
599 CHECK( ArtworkURL, ART_URL )