]> git.sesse.net Git - vlc/blob - src/input/meta.c
input/meta.c: Make sure the artwork cache is used.
[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 <vlc_strings.h>
32 #include "../playlist/playlist_internal.h"
33 #include <errno.h>
34
35 #ifdef HAVE_SYS_STAT_H
36 #   include <sys/stat.h>
37 #endif
38
39 static const char * meta_type_to_string[VLC_META_TYPE_COUNT] =
40 {
41     [vlc_meta_Title]            = N_("Title"),
42     [vlc_meta_Artist]           = N_("Artist"),
43     [vlc_meta_Genre]            = N_("Genre"),
44     [vlc_meta_Copyright]        = N_("Copyright"),
45     [vlc_meta_Album]            = N_("Album"),
46     [vlc_meta_TrackNumber]      = N_("Track number"),
47     [vlc_meta_Description]      = N_("Description"),
48     [vlc_meta_Rating]           = N_("Rating"),
49     [vlc_meta_Date]             = N_("Date"),
50     [vlc_meta_Setting]          = N_("Setting"),
51     [vlc_meta_URL]              = N_("URL"),
52     [vlc_meta_Language]         = N_("Language"),
53     [vlc_meta_NowPlaying]       = N_("Now Playing"),
54     [vlc_meta_Publisher]        = N_("Publisher"),
55     [vlc_meta_EncodedBy]        = N_("Encoded by"),
56     [vlc_meta_ArtworkURL]       = N_("Artwork URL"),
57     [vlc_meta_TrackID]          = N_("Track ID"),
58 };
59
60 const char *
61 input_MetaTypeToLocalizedString( vlc_meta_type_t meta_type )
62 {
63     return _(meta_type_to_string[meta_type]);
64 }
65
66 #define input_FindArtInCache(a,b) __input_FindArtInCache(VLC_OBJECT(a),b)
67 static int __input_FindArtInCache( vlc_object_t *, input_item_t *p_item );
68
69 vlc_bool_t input_MetaSatisfied( playlist_t *p_playlist, input_item_t *p_item,
70                                 uint32_t *pi_mandatory, uint32_t *pi_optional )
71 {
72     (void)p_playlist;
73     *pi_mandatory = VLC_META_ENGINE_TITLE | VLC_META_ENGINE_ARTIST;
74
75     uint32_t i_meta = input_CurrentMetaFlags( p_item->p_meta );
76     *pi_mandatory &= ~i_meta;
77     *pi_optional = 0; /// Todo
78     return *pi_mandatory ? VLC_FALSE:VLC_TRUE;
79 }
80
81 int input_MetaFetch( playlist_t *p_playlist, input_item_t *p_item )
82 {
83     struct meta_engine_t *p_me;
84     uint32_t i_mandatory, i_optional;
85
86     input_MetaSatisfied( p_playlist, p_item, &i_mandatory, &i_optional );
87     // Meta shouldn't magically appear
88     assert( i_mandatory );
89
90     /* FIXME: object creation is overkill, use p_private */
91     p_me = vlc_object_create( p_playlist, VLC_OBJECT_META_ENGINE );
92     p_me->i_flags |= OBJECT_FLAGS_NOINTERACT;
93     p_me->i_flags |= OBJECT_FLAGS_QUIET;
94     p_me->i_mandatory = i_mandatory;
95     p_me->i_optional = i_optional;
96
97     p_me->p_item = p_item;
98     p_me->p_module = module_Need( p_me, "meta fetcher", 0, VLC_FALSE );
99     if( !p_me->p_module )
100     {
101         vlc_object_destroy( p_me );
102         return VLC_EGENERIC;
103     }
104     module_Unneed( p_me, p_me->p_module );
105     vlc_object_destroy( p_me );
106
107     input_item_SetMetaFetched( p_item, VLC_TRUE );
108
109     return VLC_SUCCESS;
110 }
111
112 /* Return codes:
113  *   0 : Art is in cache
114  *   1 : Art found, need to download
115  *  -X : Error/not found
116  */
117 int input_ArtFind( playlist_t *p_playlist, input_item_t *p_item )
118 {
119     int i_ret = VLC_EGENERIC;
120     module_t *p_module;
121     char *psz_title, *psz_artist, *psz_album;
122
123     if( !p_item->p_meta )
124         return VLC_EGENERIC;
125
126     psz_artist = input_item_GetArtist( p_item );
127     psz_album = input_item_GetAlbum( p_item );
128     psz_title = input_item_GetTitle( p_item );
129     if(!psz_title)
130         psz_title = input_item_GetName( p_item );
131
132     if( !psz_title && !psz_artist && !psz_album )
133         return VLC_EGENERIC;
134
135     free( psz_title );
136
137     /* If we already checked this album in this session, skip */
138     if( psz_artist && psz_album )
139     {
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 ) )
143             {
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 */
147                 free( psz_artist );
148                 free( psz_album );
149                 if( album.b_found )
150                 {
151                     /* Actually get URL from cache */
152                     input_FindArtInCache( p_playlist, p_item );
153                     return 0;
154                 }
155                 else
156                 {
157                     return VLC_EGENERIC;
158                 }
159             }
160         FOREACH_END();
161     }
162     free( psz_artist );
163     free( psz_album );
164
165     input_FindArtInCache( p_playlist, p_item );
166
167     char *psz_arturl = input_item_GetArtURL( p_item );
168     if( !EMPTY_STR( psz_arturl ) )
169     {
170         free( psz_arturl );
171         return 0; /* Art is in cache, no need to go further */
172     }
173     free( psz_arturl );
174
175     PL_LOCK;
176     p_playlist->p_private = p_item;
177     psz_album = input_item_GetAlbum( p_item );
178     psz_artist = input_item_GetArtist( p_item );
179     psz_title = input_item_GetTitle( p_item );
180     if( !psz_title )
181         psz_title = input_item_GetName( p_item );
182
183     if( psz_album && psz_artist )
184     {
185         msg_Dbg( p_playlist, "searching art for %s - %s",
186              psz_artist, psz_album );
187     }
188     else
189     {
190         msg_Dbg( p_playlist, "searching art for %s",
191              psz_title );
192     }
193     free( psz_title );
194
195     p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
196
197     if( p_module )
198         i_ret = 1;
199     else
200         msg_Dbg( p_playlist, "unable to find art" );
201
202     /* Record this album */
203     if( psz_artist && psz_album )
204     {
205         playlist_album_t a;
206         a.psz_artist = psz_artist;
207         a.psz_album = psz_album;
208         a.b_found = (i_ret == VLC_EGENERIC ? VLC_FALSE : VLC_TRUE );
209         ARRAY_APPEND( p_playlist->p_fetcher->albums, a );
210     }
211     else
212     {
213         free( psz_artist );
214         free( psz_album );
215     }
216
217     if( p_module )
218         module_Unneed( p_playlist, p_module );
219     p_playlist->p_private = NULL;
220     PL_UNLOCK;
221
222     return i_ret;
223 }
224
225 #ifndef MAX_PATH
226 #   define MAX_PATH 250
227 #endif
228 static void ArtCacheCreateDir( const char *psz_dir )
229 {
230     char newdir[MAX_PATH+1];
231     strcpy( newdir, psz_dir );
232     char * psz_newdir = newdir;
233     char * psz = psz_newdir;
234
235     while( *psz_newdir )
236     {
237         while( *psz && *psz != '/') psz++;
238         if( !*psz ) break;
239         *psz = 0;        
240         if( !EMPTY_STR( psz_newdir ) ) utf8_mkdir( psz_newdir );
241         psz_newdir = psz+1;
242     }
243 }
244
245 static char * ArtCacheGetSanitizedFileName( const char *psz )
246 {
247     char *dup = strdup(psz);
248     int i;
249
250     filename_sanitize( dup );
251
252     /* Doesn't create a filename with invalid characters
253      * TODO: several filesystems forbid several characters: list them all
254      */
255     for( i = 0; dup[i] != '\0'; i++ )
256     {
257         if( dup[i] == '/' )
258             dup[i] = ' ';
259     }
260     return dup;
261 }
262
263 #define ArtCacheGetDirPath(a,b,c,d,e) __ArtCacheGetDirPath(VLC_OBJECT(a),b,c,d,e)
264 static void __ArtCacheGetDirPath( vlc_object_t *p_obj,
265                                   char *psz_dir,
266                                   const char *psz_title,
267                                   const char *psz_artist, const char *psz_album )
268 {
269     if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
270     {
271         char * psz_album_sanitized = ArtCacheGetSanitizedFileName( psz_album );
272         char * psz_artist_sanitized = ArtCacheGetSanitizedFileName( psz_artist );
273
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_sanitized, psz_album_sanitized );
278         free( psz_album_sanitized );
279         free( psz_artist_sanitized );
280     }
281     else
282     {
283         char * psz_title_sanitized = ArtCacheGetSanitizedFileName( psz_title );
284         snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
285                   "art" DIR_SEP "title" DIR_SEP "%s",
286                   p_obj->p_libvlc->psz_homedir,
287                   psz_title_sanitized );
288         free( psz_title_sanitized );
289     }
290 }
291
292
293
294 #define ArtCacheGetFilePath(a,b,c,d,e,f) __ArtCacheGetFilePath(VLC_OBJECT(a),b,c,d,e,f)
295 static void __ArtCacheGetFilePath( vlc_object_t *p_obj,
296                                    char * psz_filename,
297                                    const char *psz_title,
298                                    const char *psz_artist, const char *psz_album,
299                                    const char *psz_extension )
300 {
301     char psz_dir[MAX_PATH+1];
302     char * psz_ext;
303     ArtCacheGetDirPath( p_obj, psz_dir, psz_title, psz_artist, psz_album );
304
305     if( psz_extension )
306     {
307         psz_ext = strndup( psz_extension, 6 );
308         filename_sanitize( psz_ext );
309     }
310     else psz_ext = strdup( "" );
311
312     snprintf( psz_filename, MAX_PATH, "file://%s" DIR_SEP "art%s",
313               psz_dir, psz_ext );
314
315     free( psz_ext );
316 }
317
318 static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
319 {
320     char *psz_artist;
321     char *psz_album;
322     char *psz_title;
323     char psz_dirpath[MAX_PATH+1];
324     char psz_filepath[MAX_PATH+1];
325     char * psz_filename;
326     DIR * p_dir;
327
328     psz_artist = input_item_GetArtist( p_item );
329     psz_album = input_item_GetAlbum( p_item );
330     psz_title = input_item_GetTitle( p_item );
331     if( !psz_title ) psz_title = input_item_GetName( p_item );
332
333     if( !psz_title && ( !psz_album || !psz_artist ) )
334     {
335         free( psz_artist );
336         free( psz_album );
337         free( psz_title );
338         return VLC_EGENERIC;
339     }
340
341     ArtCacheGetDirPath( p_obj, psz_dirpath, psz_title,
342                            psz_artist, psz_album );
343
344     free( psz_artist );
345     free( psz_album );
346     free( psz_title );
347
348     /* Check if file exists */
349     p_dir = utf8_opendir( psz_dirpath );
350     if( !p_dir )
351         return VLC_EGENERIC;
352
353     while( (psz_filename = utf8_readdir( p_dir )) )
354     {
355         if( !strncmp( psz_filename, "art", 3 ) )
356         {
357             snprintf( psz_filepath, MAX_PATH, "%s" DIR_SEP "%s",
358                       psz_dirpath, psz_filename );
359             input_item_SetArtURL( p_item, psz_filepath );
360             free( psz_filepath );
361             free( psz_filename );
362             closedir( p_dir );
363             return VLC_SUCCESS;
364         }
365         free( psz_filename );
366     }
367
368     /* Not found */
369     closedir( p_dir );
370     return VLC_EGENERIC;
371 }
372
373 /**
374  * Download the art using the URL or an art downloaded
375  * This function should be called only if data is not already in cache
376  */
377 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
378 {
379     int i_status = VLC_EGENERIC;
380     stream_t *p_stream;
381     char psz_filename[MAX_PATH+1];
382     char *psz_artist = NULL;
383     char *psz_album = NULL;
384     char *psz_title = NULL;
385     char *psz_arturl;
386     char *psz_type;
387
388     psz_artist = input_item_GetArtist( p_item );
389     psz_album = input_item_GetAlbum( p_item );
390     psz_title = input_item_GetTitle( p_item );
391     if( !psz_title )
392         psz_title = input_item_GetName( p_item );
393
394     if( !psz_title && (!psz_artist || !psz_album) )
395     {
396         free( psz_title );
397         free( psz_album );
398         free( psz_artist );
399         return VLC_EGENERIC;
400     }
401
402     psz_arturl = input_item_GetArtURL( p_item );
403     assert( !EMPTY_STR( psz_arturl ) );
404
405     if( !strncmp( psz_arturl , "APIC", 4 ) )
406     {
407         msg_Warn( p_playlist, "APIC fetch not supported yet" );
408         free( psz_arturl );
409         return VLC_EGENERIC;
410     }
411
412     psz_type = strrchr( psz_arturl, '.' );
413     if( strlen( psz_type ) > 5 ) 
414         psz_type = NULL; /* remove extension if it's > to 4 characters */
415
416     /* Warning: psz_title, psz_artist, psz_album may change in ArtCache*() */
417
418     ArtCacheGetDirPath( p_playlist, psz_filename, psz_title, psz_artist,
419                         psz_album );
420     ArtCacheCreateDir( psz_filename );
421     ArtCacheGetFilePath( p_playlist, psz_filename, psz_title, psz_artist,
422                          psz_album, psz_type );
423
424     free( psz_artist );
425     free( psz_album );
426     free( psz_title );
427
428     p_stream = stream_UrlNew( p_playlist, psz_arturl );
429     if( p_stream )
430     {
431         uint8_t p_buffer[65536];
432         long int l_read;
433         FILE *p_file = utf8_fopen( psz_filename+7, "w" );
434         if( p_file == NULL ) {
435             msg_Err( p_playlist, "Unable write album art in %s" );
436             free( psz_arturl );
437             return VLC_EGENERIC;
438         }
439         int err = 0;
440         while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
441         {
442             if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
443             {
444                 err = errno;
445                 break;
446             }
447         }
448         if( fclose( p_file ) && !err )
449             err = errno;
450         stream_Delete( p_stream );
451
452         if( err )
453             msg_Err( p_playlist, "%s: %s", psz_filename, strerror( err ) );
454         else
455             msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
456
457         input_item_SetArtURL( p_item, psz_filename );
458         i_status = VLC_SUCCESS;
459     }
460     free( psz_arturl );
461     return i_status;
462 }
463
464 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
465 {
466     input_item_t *p_item = p_input->p->input.p_item;
467     char *psz_arturl;
468     char *psz_artist = NULL;
469     char *psz_album = NULL;
470     char *psz_title = NULL;
471     char *psz_type = NULL;
472     char psz_filename[MAX_PATH+1];
473     FILE *f;
474     input_attachment_t *p_attachment;
475     struct stat s;
476     int i_idx;
477
478     /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH
479      * and then set it here to to be faster */
480
481     psz_arturl = input_item_GetArtURL( p_item );
482     if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
483     {
484         free( psz_arturl );
485         msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
486         return;
487     }
488     input_item_SetArtURL( p_item, NULL );
489
490     if( input_item_IsArtFetched( p_item ) )
491     {
492         /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
493          * condition */
494         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
495         input_FindArtInCache( p_input, p_item );
496         free( psz_arturl );
497         return;
498     }
499
500     /* */
501     for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ )
502     {
503         if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
504                      &psz_arturl[strlen("attachment://")] ) )
505         {
506             p_attachment = p_input->p->attachment[i_idx];
507             break;
508         }
509     }
510     if( !p_attachment || p_attachment->i_data <= 0 )
511     {
512         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
513         goto end;
514     }
515
516     psz_artist = input_item_GetArtist( p_item );
517     psz_album = input_item_GetAlbum( p_item );
518     psz_title = input_item_GetTitle( p_item );
519     if( !psz_title )
520         psz_title = input_item_GetName( p_item );
521
522     if( (!psz_artist || !psz_album ) && !psz_title )
523         goto end;
524
525     /* */
526     psz_type = strrchr( psz_arturl, '.' );
527
528     ArtCacheGetDirPath( p_input, psz_filename, psz_title, psz_artist, psz_album );
529     ArtCacheCreateDir( psz_filename );
530     ArtCacheGetFilePath( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
531
532     /* Check if we already dumped it */
533     if( !utf8_stat( psz_filename+7, &s ) )
534         goto end;
535
536     f = utf8_fopen( psz_filename+7, "w" );
537     if( f )
538     {
539         if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 )
540             msg_Err( p_input, "%s: %s", psz_filename, strerror( errno ) );
541         else
542             msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
543         fclose( f );
544     }
545
546 end:
547     if( psz_artist ) free( psz_artist );
548     if( psz_album ) free( psz_album );
549     if( psz_title ) free( psz_title );
550     if( psz_arturl ) free( psz_arturl );
551 }
552
553
554 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
555 {
556     uint32_t i_meta = 0;
557
558     if( !p_meta )
559         return 0;
560
561 #define CHECK( a, b ) \
562     if( !EMPTY_STR( vlc_meta_Get( p_meta, vlc_meta_ ## a ) ) ) \
563         i_meta |= VLC_META_ENGINE_ ## b;
564
565     CHECK( Title, TITLE )
566     CHECK( Artist, ARTIST )
567     CHECK( Album, COLLECTION )
568 #if 0
569     /* As this is not used at the moment, don't uselessly check for it.
570      * Re-enable this when it is used */
571     CHECK( Genre, GENRE )
572     CHECK( Copyright, COPYRIGHT )
573     CHECK( Tracknum, SEQ_NUM )
574     CHECK( Description, DESCRIPTION )
575     CHECK( Rating, RATING )
576     CHECK( Date, DATE )
577     CHECK( URL, URL )
578     CHECK( Language, LANGUAGE )
579 #endif
580     CHECK( ArtworkURL, ART_URL )
581
582     return i_meta;
583 }