]> git.sesse.net Git - vlc/blob - src/input/meta.c
input/meta.c: Be a bit more selective on meta pre-requirement for art.
[vlc] / src / input / meta.c
1 /*****************************************************************************
2  * meta.c : Metadata handling
3  *****************************************************************************
4  * Copyright (C) 1998-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea@videolan.org>
8  *          ClĂ©ment Stenac <zorglub@videolan.org
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #include <vlc/vlc.h>
26 #include <vlc_input.h>
27 #include <vlc_stream.h>
28 #include <vlc_meta.h>
29 #include <vlc_playlist.h>
30 #include <vlc_charset.h>
31 #include "../playlist/playlist_internal.h"
32 #include <errno.h>
33
34 #ifdef HAVE_SYS_STAT_H
35 #   include <sys/stat.h>
36 #endif
37
38 // FIXME be sure to not touch p_meta without lock on p_item
39
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 );
42
43 vlc_bool_t input_MetaSatisfied( playlist_t *p_playlist, input_item_t *p_item,
44                                 uint32_t *pi_mandatory, uint32_t *pi_optional )
45 {
46     (void)p_playlist;
47     *pi_mandatory = VLC_META_ENGINE_TITLE | VLC_META_ENGINE_ARTIST;
48     assert( p_item->p_meta );
49
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;
54 }
55
56 int input_MetaFetch( playlist_t *p_playlist, input_item_t *p_item )
57 {
58     struct meta_engine_t *p_me;
59     uint32_t i_mandatory, i_optional;
60
61     assert( p_item->p_meta );
62
63     input_MetaSatisfied( p_playlist, p_item, &i_mandatory, &i_optional );
64     // Meta shouldn't magically appear
65     assert( i_mandatory );
66
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;
73
74     p_me->p_item = p_item;
75     p_me->p_module = module_Need( p_me, "meta fetcher", 0, VLC_FALSE );
76     if( !p_me->p_module )
77     {
78         vlc_object_destroy( p_me );
79         return VLC_EGENERIC;
80     }
81     module_Unneed( p_me, p_me->p_module );
82     vlc_object_destroy( p_me );
83     return VLC_SUCCESS;
84 }
85
86 /* Return codes:
87  *   0 : Art is in cache
88  *   1 : Art found, need to download
89  *  -X : Error/not found
90  */
91 int input_ArtFind( playlist_t *p_playlist, input_item_t *p_item )
92 {
93     int i_ret = VLC_EGENERIC;
94     module_t *p_module;
95
96     if( !p_item->p_meta )
97         return VLC_EGENERIC;
98
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) )
101         return VLC_EGENERIC;
102
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 )
105     {
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 ) )
109             {
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 */
113                 if( album.b_found )
114                 {
115                     /* Actually get URL from cache */
116                     input_FindArtInCache( p_playlist, p_item );
117                     return 0;
118                 }
119                 else
120                 {
121                     return VLC_EGENERIC;
122                 }
123             }
124         FOREACH_END();
125     }
126
127     input_FindArtInCache( p_playlist, p_item );
128     if( !EMPTY_STR( p_item->p_meta->psz_arturl ) )
129         return 0;
130
131     PL_LOCK;
132     p_playlist->p_private = p_item;
133     if( p_item->p_meta->psz_artist && p_item->p_meta->psz_album )
134     {
135         msg_Dbg( p_playlist, "searching art for %s - %s",
136              p_item->p_meta->psz_artist,  p_item->p_meta->psz_album );
137     }
138     else
139     {
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 );
142     }
143
144     p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
145
146     if( p_module )
147         i_ret = 1;
148     else
149         msg_Dbg( p_playlist, "unable to find art" );
150
151     /* Record this album */
152     if( p_item->p_meta->psz_artist && p_item->p_meta->psz_album )
153     {
154         playlist_album_t a;
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 );
159     }
160
161     if( p_module )
162         module_Unneed( p_playlist, p_module );
163     p_playlist->p_private = NULL;
164     PL_UNLOCK;
165
166     return i_ret;
167 }
168
169 #ifndef MAX_PATH
170 #   define MAX_PATH 250
171 #endif
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 )
178 {
179     if( psz_artist && psz_artist )
180     {
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 : "" );
186     }
187     else
188     {
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 : "" );
195     }
196 }
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 )
201 {
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 );
209
210     if( psz_artist && psz_artist )
211     {
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 );
225     }
226     else
227     {
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 );
236     }
237 }
238 static char *ArtCacheCreateString( const char *psz )
239 {
240     char *dup = strdup(psz);
241     int i;
242
243     /* Doesn't create a filename with invalid characters
244      * TODO: several filesystems forbid several characters: list them all
245      */
246     for( i = 0; dup[i] != '\0'; i++ )
247     {
248         if( dup[i] == '/' )
249             dup[i] = ' ';
250     }
251     return dup;
252 }
253
254 static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
255 {
256     char *psz_artist;
257     char *psz_album;
258     char *psz_title;
259     char psz_filename[MAX_PATH+1];
260     int i;
261     struct stat a;
262     const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
263
264     if( !p_item->p_meta ) return VLC_EGENERIC;
265
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;
270
271     if( (!psz_artist || !psz_album) && !psz_title ) return VLC_EGENERIC;
272
273     for( i = 0; i < 5; i++ )
274     {
275         ArtCacheCreateName( p_obj, psz_filename, psz_title /* Used if none artist nor album is defined */,
276                             psz_artist, psz_album, ppsz_type[i] );
277
278         /* Check if file exists */
279         if( utf8_stat( psz_filename+7, &a ) == 0 )
280         {
281             vlc_meta_SetArtURL( p_item->p_meta, psz_filename );
282             return VLC_SUCCESS;
283         }
284     }
285     return VLC_EGENERIC;
286 }
287
288 /**
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
291  */
292 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
293 {
294     int i_status = VLC_EGENERIC;
295     stream_t *p_stream;
296     char psz_filename[MAX_PATH+1];
297     char *psz_artist = NULL;
298     char *psz_album = NULL;
299     char *psz_title = NULL;
300     char *psz_type;
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 );
309
310     if( (!psz_title || !psz_album) && !psz_artist )
311     {
312         free( psz_title );
313         free( psz_album );
314         free( psz_artist );
315         return VLC_EGENERIC;
316     }
317
318     assert( p_item->p_meta && !EMPTY_STR(p_item->p_meta->psz_arturl) );
319
320     psz_type = strrchr( p_item->p_meta->psz_arturl, '.' );
321
322     /* */
323     ArtCacheCreateName( p_playlist, psz_filename, psz_title /* Used only if needed*/,
324                         psz_artist, psz_album, psz_type );
325
326     /* */
327     ArtCacheCreatePath( p_playlist, psz_title, psz_artist, psz_album );
328
329     /* */
330     free( psz_artist );
331     free( psz_album );
332     free( psz_title );
333
334     if( !strncmp( p_item->p_meta->psz_arturl , "APIC", 4 ) )
335     {
336         msg_Warn( p_playlist, "APIC fetch not supported yet" );
337         return VLC_EGENERIC;
338     }
339
340     p_stream = stream_UrlNew( p_playlist, p_item->p_meta->psz_arturl );
341     if( p_stream )
342     {
343         uint8_t p_buffer[65536];
344         long int l_read;
345         FILE *p_file = utf8_fopen( psz_filename+7, "w" );
346         int err = 0;
347         while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
348         {
349             if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
350             {
351                 err = errno;
352                 break;
353             }
354         }
355         if( fclose( p_file ) && !err )
356             err = errno;
357         stream_Delete( p_stream );
358         free( p_item->p_meta->psz_arturl );
359
360         if( err )
361             msg_Err( p_playlist, "%s: %s", psz_filename, strerror( err ) );
362         else
363             msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
364
365         p_item->p_meta->psz_arturl = strdup( psz_filename );
366         i_status = VLC_SUCCESS;
367     }
368     return i_status;
369 }
370
371 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
372 {
373     input_item_t *p_item = p_input->p->input.p_item;
374     char *psz_arturl;
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];
380     FILE *f;
381     input_attachment_t *p_attachment;
382     struct stat s;
383     int i_idx;
384
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 */
387
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://") ) )
391     {
392         msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
393         return;
394     }
395     p_item->p_meta->psz_arturl = NULL;
396
397     if( p_item->p_meta->i_status & ITEM_ART_FETCHED )
398     {
399         /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
400          * condition */
401         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
402         input_FindArtInCache( p_input, p_item );
403         free( psz_arturl );
404         return;
405     }
406
407     /* */
408     for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ )
409     {
410         if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
411                      &psz_arturl[strlen("attachment://")] ) )
412         {
413             p_attachment = p_input->p->attachment[i_idx];
414             break;
415         }
416     }
417     if( !p_attachment || p_attachment->i_data <= 0 )
418     {
419         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
420         goto end;
421     }
422
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 );
431
432     if( (!psz_artist || !psz_album ) && !psz_title )
433         goto end;
434
435     /* */
436     psz_type = strrchr( psz_arturl, '.' );
437     ArtCacheCreateName( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
438
439     /* Check if we already dumped it */
440     if( !utf8_stat( psz_filename+7, &s ) )
441         goto end;
442
443     ArtCacheCreatePath( p_input, psz_title, psz_artist, psz_album );
444
445     f = utf8_fopen( psz_filename+7, "w" );
446     if( f )
447     {
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 ) );
450         else
451             msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
452         fclose( f );
453     }
454
455 end:
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 );
460 }
461
462
463 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
464 {
465     uint32_t i_meta = 0;
466
467 #define CHECK( a, b ) \
468     if( p_meta->psz_ ## a && *p_meta->psz_ ## a ) \
469         i_meta |= VLC_META_ENGINE_ ## b;
470
471     CHECK( title, TITLE )
472     CHECK( artist, ARTIST )
473     CHECK( album, COLLECTION )
474 #if 0
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 )
482     CHECK( date, DATE )
483     CHECK( url, URL )
484     CHECK( language, LANGUAGE )
485 #endif
486     CHECK( arturl, ART_URL )
487
488     return i_meta;
489 }