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;
95 if( !p_item->p_meta || !p_item->p_meta->psz_album ||
96 !p_item->p_meta->psz_artist )
99 /* If we already checked this album in this session, skip */
100 FOREACH_ARRAY( playlist_album_t album, p_playlist->p_fetcher->albums )
101 if( !strcmp( album.psz_artist, p_item->p_meta->psz_artist ) &&
102 !strcmp( album.psz_album, p_item->p_meta->psz_album ) )
104 msg_Dbg( p_playlist, " %s - %s has already been searched",
105 p_item->p_meta->psz_artist, p_item->p_meta->psz_album );
106 /* TODO-fenrir if we cache art filename too, we can go faster */
109 /* Actually get URL from cache */
110 input_FindArtInCache( p_playlist, p_item );
120 input_FindArtInCache( p_playlist, p_item );
121 if( !EMPTY_STR( p_item->p_meta->psz_arturl ) )
125 p_playlist->p_private = p_item;
126 msg_Dbg( p_playlist, "searching art for %s - %s",
127 p_item->p_meta->psz_artist, p_item->p_meta->psz_album );
128 p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
133 msg_Dbg( p_playlist, "unable to find art" );
135 /* Record this album */
137 a.psz_artist = strdup( p_item->p_meta->psz_artist );
138 a.psz_album = strdup( p_item->p_meta->psz_album );
139 a.b_found = (i_ret == VLC_EGENERIC ? VLC_FALSE : VLC_TRUE );
140 ARRAY_APPEND( p_playlist->p_fetcher->albums, a );
143 module_Unneed( p_playlist, p_module );
144 p_playlist->p_private = NULL;
151 # define MAX_PATH 250
153 #define ArtCacheCreateName( a,b,c,d,e) __ArtCacheCreateName(VLC_OBJECT(a),b,c,d,e)
154 static void __ArtCacheCreateName( vlc_object_t *p_obj,
155 char psz_filename[MAX_PATH+1],
156 const char *psz_artist, const char *psz_album,
157 const char *psz_extension )
159 snprintf( psz_filename, MAX_PATH,
160 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
161 DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
162 p_obj->p_libvlc->psz_homedir,
163 psz_artist, psz_album, psz_extension ? psz_extension : "" );
165 #define ArtCacheCreatePath(a,b,c) __ArtCacheCreatePath(VLC_OBJECT(a),b,c)
166 static void __ArtCacheCreatePath( vlc_object_t *p_obj,
167 const char *psz_artist, const char *psz_album )
169 char psz_dir[MAX_PATH+1];
170 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
171 p_obj->p_libvlc->psz_homedir );
172 utf8_mkdir( psz_dir );
173 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
174 p_obj->p_libvlc->psz_homedir );
175 utf8_mkdir( psz_dir );
176 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
178 p_obj->p_libvlc->psz_homedir, psz_artist );
179 utf8_mkdir( psz_dir );
180 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
181 "art" DIR_SEP "%s" DIR_SEP "%s",
182 p_obj->p_libvlc->psz_homedir,
183 psz_artist, psz_album );
184 utf8_mkdir( psz_dir );
186 static char *ArtCacheCreateString( const char *psz )
188 char *dup = strdup(psz);
191 /* Doesn't create a filename with invalid characters
192 * TODO: several filesystems forbid several characters: list them all
194 for( i = 0; dup[i] != '\0'; i++ )
202 static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
206 char psz_filename[MAX_PATH+1];
209 const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
211 if( !p_item->p_meta ) return VLC_EGENERIC;
213 psz_artist = p_item->p_meta->psz_artist;
214 psz_album = p_item->p_meta->psz_album;
216 for( i = 0; i < 5; i++ )
218 ArtCacheCreateName( p_obj, psz_filename, psz_artist, psz_album, ppsz_type[i] );
220 /* Check if file exists */
221 if( utf8_stat( psz_filename+7, &a ) == 0 )
223 vlc_meta_SetArtURL( p_item->p_meta, psz_filename );
231 * Download the art using the URL or an art downloaded
232 * This function should be called only if data is not already in cache
234 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
236 int i_status = VLC_EGENERIC;
238 char psz_filename[MAX_PATH+1];
239 char *psz_artist = NULL;
240 char *psz_album = NULL;
242 if( p_item->p_meta->psz_artist )
243 psz_artist = ArtCacheCreateString( p_item->p_meta->psz_artist );
244 if( p_item->p_meta->psz_album )
245 psz_album = ArtCacheCreateString( p_item->p_meta->psz_album );
247 assert( p_item->p_meta && !EMPTY_STR(p_item->p_meta->psz_arturl) );
249 /* FIXME: use an alternate saving filename scheme if we don't have
250 * the artist or album name */
251 if( !psz_artist || !psz_album )
253 if( psz_artist ) free( psz_artist );
254 if( psz_album ) free( psz_album );
258 psz_type = strrchr( p_item->p_meta->psz_arturl, '.' );
261 ArtCacheCreateName( p_playlist, psz_filename, psz_artist, psz_album, psz_type );
264 ArtCacheCreatePath( p_playlist, psz_artist, psz_album );
270 if( !strncmp( p_item->p_meta->psz_arturl , "APIC", 4 ) )
272 msg_Warn( p_playlist, "APIC fetch not supported yet" );
276 p_stream = stream_UrlNew( p_playlist, p_item->p_meta->psz_arturl );
279 uint8_t p_buffer[65536];
281 FILE *p_file = utf8_fopen( psz_filename+7, "w" );
283 while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
285 if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
291 if( fclose( p_file ) && !err )
293 stream_Delete( p_stream );
294 free( p_item->p_meta->psz_arturl );
297 msg_Err( p_playlist, "%s: %s", psz_filename, strerror( err ) );
299 msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
301 p_item->p_meta->psz_arturl = strdup( psz_filename );
302 i_status = VLC_SUCCESS;
307 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
309 input_item_t *p_item = p_input->p->input.p_item;
311 char *psz_artist = NULL;
312 char *psz_album = NULL;
313 char *psz_type = NULL;
314 char psz_filename[MAX_PATH+1];
316 input_attachment_t *p_attachment;
320 /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH
321 * and then set it here to to be faster */
323 assert( p_item->p_meta );
324 psz_arturl = p_item->p_meta->psz_arturl;
325 if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
327 msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
330 p_item->p_meta->psz_arturl = NULL;
332 if( p_item->p_meta->i_status & ITEM_ART_FETCHED )
334 /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
336 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
337 input_FindArtInCache( p_input, p_item );
343 for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ )
345 if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
346 &psz_arturl[strlen("attachment://")] ) )
348 p_attachment = p_input->p->attachment[i_idx];
352 if( !p_attachment || p_attachment->i_data <= 0 )
354 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
358 if( p_item->p_meta->psz_artist )
359 psz_artist = ArtCacheCreateString( p_item->p_meta->psz_artist );
360 if( p_item->p_meta->psz_album )
361 psz_album = ArtCacheCreateString( p_item->p_meta->psz_album );
363 if( !psz_artist || !psz_album )
367 psz_type = strrchr( psz_arturl, '.' );
368 ArtCacheCreateName( p_input, psz_filename, psz_artist, psz_album, psz_type );
370 /* Check if we already dumped it */
371 if( !utf8_stat( psz_filename+7, &s ) )
374 ArtCacheCreatePath( p_input, psz_artist, psz_album );
376 f = utf8_fopen( psz_filename+7, "w" );
379 if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 )
380 msg_Err( p_input, "%s: %s", psz_filename, strerror( errno ) );
382 msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
387 if( psz_artist ) free( psz_artist );
388 if( psz_album ) free( psz_album );
389 if( psz_arturl ) free( psz_arturl );
393 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
397 #define CHECK( a, b ) \
398 if( p_meta->psz_ ## a && *p_meta->psz_ ## a ) \
399 i_meta |= VLC_META_ENGINE_ ## b;
401 CHECK( title, TITLE )
402 CHECK( artist, ARTIST )
403 CHECK( album, COLLECTION )
405 /* As this is not used at the moment, don't uselessly check for it.
406 * Re-enable this when it is used */
407 CHECK( genre, GENRE )
408 CHECK( copyright, COPYRIGHT )
409 CHECK( tracknum, SEQ_NUM )
410 CHECK( description, DESCRIPTION )
411 CHECK( rating, RATING )
414 CHECK( language, LANGUAGE )
416 CHECK( arturl, ART_URL )