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 #define input_FindArtInCache(a,b) __input_FindArtInCache(VLC_OBJECT(a),b)
41 static int __input_FindArtInCache( vlc_object_t *, input_item_t *p_item );
43 vlc_bool_t input_MetaSatisfied( playlist_t *p_playlist, input_item_t *p_item,
44 uint32_t *pi_mandatory, uint32_t *pi_optional )
47 *pi_mandatory = VLC_META_ENGINE_TITLE | VLC_META_ENGINE_ARTIST;
48 assert( p_item->p_meta );
50 uint32_t i_meta = input_CurrentMetaFlags( p_item->p_meta );
51 *pi_mandatory &= ~i_meta;
52 *pi_optional = 0; /// Todo
53 return *pi_mandatory ? VLC_FALSE:VLC_TRUE;
56 int input_MetaFetch( playlist_t *p_playlist, input_item_t *p_item )
58 struct meta_engine_t *p_me;
59 uint32_t i_mandatory, i_optional;
61 assert( p_item->p_meta );
63 input_MetaSatisfied( p_playlist, p_item, &i_mandatory, &i_optional );
64 // Meta shouldn't magically appear
65 assert( i_mandatory );
67 /* FIXME: object creation is overkill, use p_private */
68 p_me = vlc_object_create( p_playlist, VLC_OBJECT_META_ENGINE );
69 p_me->i_flags |= OBJECT_FLAGS_NOINTERACT;
70 p_me->i_flags |= OBJECT_FLAGS_QUIET;
71 p_me->i_mandatory = i_mandatory;
72 p_me->i_optional = i_optional;
74 p_me->p_item = p_item;
75 p_me->p_module = module_Need( p_me, "meta fetcher", 0, VLC_FALSE );
78 vlc_object_destroy( p_me );
81 module_Unneed( p_me, p_me->p_module );
82 vlc_object_destroy( p_me );
88 * 1 : Art found, need to download
89 * -X : Error/not found
91 int input_ArtFind( playlist_t *p_playlist, input_item_t *p_item )
93 int i_ret = VLC_EGENERIC;
99 if( !p_item->psz_name && !p_item->p_meta->psz_title &&
100 (!p_item->p_meta->psz_artist || !p_item->p_meta->psz_album) )
103 /* If we already checked this album in this session, skip */
104 if( p_item->p_meta->psz_artist && p_item->p_meta->psz_album )
106 FOREACH_ARRAY( playlist_album_t album, p_playlist->p_fetcher->albums )
107 if( !strcmp( album.psz_artist, p_item->p_meta->psz_artist ) &&
108 !strcmp( album.psz_album, p_item->p_meta->psz_album ) )
110 msg_Dbg( p_playlist, " %s - %s has already been searched",
111 p_item->p_meta->psz_artist, p_item->p_meta->psz_album );
112 /* TODO-fenrir if we cache art filename too, we can go faster */
115 /* Actually get URL from cache */
116 input_FindArtInCache( p_playlist, p_item );
127 input_FindArtInCache( p_playlist, p_item );
128 if( !EMPTY_STR( p_item->p_meta->psz_arturl ) )
132 p_playlist->p_private = p_item;
133 if( p_item->p_meta->psz_artist && p_item->p_meta->psz_album )
135 msg_Dbg( p_playlist, "searching art for %s - %s",
136 p_item->p_meta->psz_artist, p_item->p_meta->psz_album );
140 msg_Dbg( p_playlist, "searching art for %s",
141 p_item->p_meta->psz_title ? p_item->p_meta->psz_title : p_item->psz_name );
144 p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
149 msg_Dbg( p_playlist, "unable to find art" );
151 /* Record this album */
152 if( p_item->p_meta->psz_artist && p_item->p_meta->psz_album )
155 a.psz_artist = strdup( p_item->p_meta->psz_artist );
156 a.psz_album = strdup( p_item->p_meta->psz_album );
157 a.b_found = (i_ret == VLC_EGENERIC ? VLC_FALSE : VLC_TRUE );
158 ARRAY_APPEND( p_playlist->p_fetcher->albums, a );
162 module_Unneed( p_playlist, p_module );
163 p_playlist->p_private = NULL;
170 # define MAX_PATH 250
172 #define ArtCacheCreateName(a,b,c,d,e,f) __ArtCacheCreateName(VLC_OBJECT(a),b,c,d,e,f)
173 static void __ArtCacheCreateName( vlc_object_t *p_obj,
174 char psz_filename[MAX_PATH+1],
175 const char *psz_title,
176 const char *psz_artist, const char *psz_album,
177 const char *psz_extension )
179 if( psz_artist && psz_artist )
181 snprintf( psz_filename, MAX_PATH,
182 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art" DIR_SEP "artistalbum"
183 DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
184 p_obj->p_libvlc->psz_homedir,
185 psz_artist, psz_album, psz_extension ? psz_extension : "" );
189 /* We will use the psz_title name to store the art */
190 snprintf( psz_filename, MAX_PATH,
191 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art" DIR_SEP "title"
192 DIR_SEP "%s" DIR_SEP "art%s",
193 p_obj->p_libvlc->psz_homedir,
194 psz_title, psz_extension ? psz_extension : "" );
197 #define ArtCacheCreatePath(a,b,c,d) __ArtCacheCreatePath(VLC_OBJECT(a),b,c,d)
198 static void __ArtCacheCreatePath( vlc_object_t *p_obj,
199 const char *psz_title,
200 const char *psz_artist, const char *psz_album )
202 char psz_dir[MAX_PATH+1];
203 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
204 p_obj->p_libvlc->psz_homedir );
205 utf8_mkdir( psz_dir );
206 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
207 p_obj->p_libvlc->psz_homedir );
208 utf8_mkdir( psz_dir );
210 if( psz_artist && psz_artist )
212 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
213 "art" DIR_SEP "artistalbum",
214 p_obj->p_libvlc->psz_homedir );
215 utf8_mkdir( psz_dir );
216 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
217 "art" DIR_SEP "artistalbum" DIR_SEP "%s",
218 p_obj->p_libvlc->psz_homedir, psz_artist );
219 utf8_mkdir( psz_dir );
220 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
221 "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
222 p_obj->p_libvlc->psz_homedir,
223 psz_artist, psz_album );
224 utf8_mkdir( psz_dir );
228 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
229 "art" DIR_SEP "title",
230 p_obj->p_libvlc->psz_homedir );
231 utf8_mkdir( psz_dir );
232 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
233 "art" DIR_SEP "title" DIR_SEP "%s",
234 p_obj->p_libvlc->psz_homedir, psz_title );
235 utf8_mkdir( psz_dir );
238 static char *ArtCacheCreateString( const char *psz )
240 char *dup = strdup(psz);
243 /* Doesn't create a filename with invalid characters
244 * TODO: several filesystems forbid several characters: list them all
246 for( i = 0; dup[i] != '\0'; i++ )
254 static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
259 char psz_filename[MAX_PATH+1];
262 const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
264 if( !p_item->p_meta ) return VLC_EGENERIC;
266 psz_artist = p_item->p_meta->psz_artist;
267 psz_album = p_item->p_meta->psz_album;
268 psz_title = p_item->p_meta->psz_title;
269 if( !psz_title ) psz_title = p_item->psz_name;
271 if( (!psz_artist || !psz_album) && !psz_title ) return VLC_EGENERIC;
273 for( i = 0; i < 5; i++ )
275 ArtCacheCreateName( p_obj, psz_filename, psz_title /* Used if none artist nor album is defined */,
276 psz_artist, psz_album, ppsz_type[i] );
278 /* Check if file exists */
279 if( utf8_stat( psz_filename+7, &a ) == 0 )
281 vlc_meta_SetArtURL( p_item->p_meta, psz_filename );
289 * Download the art using the URL or an art downloaded
290 * This function should be called only if data is not already in cache
292 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
294 int i_status = VLC_EGENERIC;
296 char psz_filename[MAX_PATH+1];
297 char *psz_artist = NULL;
298 char *psz_album = NULL;
299 char *psz_title = NULL;
301 if( p_item->p_meta->psz_artist )
302 psz_artist = ArtCacheCreateString( p_item->p_meta->psz_artist );
303 if( p_item->p_meta->psz_album )
304 psz_album = ArtCacheCreateString( p_item->p_meta->psz_album );
305 if( p_item->p_meta->psz_title )
306 psz_title = ArtCacheCreateString( p_item->p_meta->psz_title );
307 else if( p_item->psz_name )
308 psz_title = ArtCacheCreateString( p_item->psz_name );
310 if( (!psz_title || !psz_album) && !psz_artist )
318 assert( p_item->p_meta && !EMPTY_STR(p_item->p_meta->psz_arturl) );
320 psz_type = strrchr( p_item->p_meta->psz_arturl, '.' );
323 ArtCacheCreateName( p_playlist, psz_filename, psz_title /* Used only if needed*/,
324 psz_artist, psz_album, psz_type );
327 ArtCacheCreatePath( p_playlist, psz_title, psz_artist, psz_album );
334 if( !strncmp( p_item->p_meta->psz_arturl , "APIC", 4 ) )
336 msg_Warn( p_playlist, "APIC fetch not supported yet" );
340 p_stream = stream_UrlNew( p_playlist, p_item->p_meta->psz_arturl );
343 uint8_t p_buffer[65536];
345 FILE *p_file = utf8_fopen( psz_filename+7, "w" );
347 while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
349 if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
355 if( fclose( p_file ) && !err )
357 stream_Delete( p_stream );
358 free( p_item->p_meta->psz_arturl );
361 msg_Err( p_playlist, "%s: %s", psz_filename, strerror( err ) );
363 msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
365 p_item->p_meta->psz_arturl = strdup( psz_filename );
366 i_status = VLC_SUCCESS;
371 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
373 input_item_t *p_item = p_input->p->input.p_item;
375 char *psz_artist = NULL;
376 char *psz_album = NULL;
377 char *psz_title = NULL;
378 char *psz_type = NULL;
379 char psz_filename[MAX_PATH+1];
381 input_attachment_t *p_attachment;
385 /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH
386 * and then set it here to to be faster */
388 assert( p_item->p_meta );
389 psz_arturl = p_item->p_meta->psz_arturl;
390 if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
392 msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
395 p_item->p_meta->psz_arturl = NULL;
397 if( p_item->p_meta->i_status & ITEM_ART_FETCHED )
399 /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
401 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
402 input_FindArtInCache( p_input, p_item );
408 for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ )
410 if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
411 &psz_arturl[strlen("attachment://")] ) )
413 p_attachment = p_input->p->attachment[i_idx];
417 if( !p_attachment || p_attachment->i_data <= 0 )
419 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
423 if( p_item->p_meta->psz_artist )
424 psz_artist = ArtCacheCreateString( p_item->p_meta->psz_artist );
425 if( p_item->p_meta->psz_album )
426 psz_album = ArtCacheCreateString( p_item->p_meta->psz_album );
427 if( p_item->p_meta->psz_title )
428 psz_title = ArtCacheCreateString( p_item->p_meta->psz_title );
429 else if( p_item->psz_name )
430 psz_title = ArtCacheCreateString( p_item->psz_name );
432 if( (!psz_artist || !psz_album ) && !psz_title )
436 psz_type = strrchr( psz_arturl, '.' );
437 ArtCacheCreateName( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
439 /* Check if we already dumped it */
440 if( !utf8_stat( psz_filename+7, &s ) )
443 ArtCacheCreatePath( p_input, psz_title, psz_artist, psz_album );
445 f = utf8_fopen( psz_filename+7, "w" );
448 if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 )
449 msg_Err( p_input, "%s: %s", psz_filename, strerror( errno ) );
451 msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
456 if( psz_artist ) free( psz_artist );
457 if( psz_album ) free( psz_album );
458 if( psz_title ) free( psz_title );
459 if( psz_arturl ) free( psz_arturl );
463 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
467 #define CHECK( a, b ) \
468 if( p_meta->psz_ ## a && *p_meta->psz_ ## a ) \
469 i_meta |= VLC_META_ENGINE_ ## b;
471 CHECK( title, TITLE )
472 CHECK( artist, ARTIST )
473 CHECK( album, COLLECTION )
475 /* As this is not used at the moment, don't uselessly check for it.
476 * Re-enable this when it is used */
477 CHECK( genre, GENRE )
478 CHECK( copyright, COPYRIGHT )
479 CHECK( tracknum, SEQ_NUM )
480 CHECK( description, DESCRIPTION )
481 CHECK( rating, RATING )
484 CHECK( language, LANGUAGE )
486 CHECK( arturl, ART_URL )