]> git.sesse.net Git - vlc/blob - src/input/meta.c
Better extensions checks for album art file creation
[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_name, *psz_title, *psz_artist, *psz_album;
122
123     if( !p_item->p_meta )
124         return VLC_EGENERIC;
125
126     psz_name = input_item_GetName( p_item );
127     psz_title = input_item_GetTitle( p_item );
128     psz_artist = input_item_GetArtist( p_item );
129     psz_album = input_item_GetAlbum( p_item );
130
131     if(  !psz_name && !psz_title && !psz_artist && !psz_album )
132         return VLC_EGENERIC;
133     free( psz_name );
134     free( psz_title );
135
136     /* If we already checked this album in this session, skip */
137     if( psz_artist && psz_album )
138     {
139         FOREACH_ARRAY( playlist_album_t album, p_playlist->p_fetcher->albums )
140             if( !strcmp( album.psz_artist, psz_artist ) &&
141                 !strcmp( album.psz_album, psz_album ) )
142             {
143                 msg_Dbg( p_playlist, " %s - %s has already been searched",
144                          psz_artist, psz_album );
145         /* TODO-fenrir if we cache art filename too, we can go faster */
146                 free( psz_artist );
147                 free( psz_album );
148                 if( album.b_found )
149                 {
150                     /* Actually get URL from cache */
151                     input_FindArtInCache( p_playlist, p_item );
152                     return 0;
153                 }
154                 else
155                 {
156                     return VLC_EGENERIC;
157                 }
158             }
159         FOREACH_END();
160     }
161     free( psz_artist );
162     free( psz_album );
163
164     char *psz_arturl = input_item_GetArtURL( p_item );
165     input_FindArtInCache( p_playlist, p_item );
166     if( !EMPTY_STR( psz_arturl ) )
167     {
168         free( psz_arturl );
169         return 0;
170     }
171     free( psz_arturl );
172
173     PL_LOCK;
174     p_playlist->p_private = p_item;
175     psz_album = input_item_GetAlbum( p_item );
176     psz_artist = input_item_GetArtist( p_item );
177     psz_name = input_item_GetName( p_item );
178     psz_title = input_item_GetTitle( p_item );
179     if( psz_album && psz_artist )
180     {
181         msg_Dbg( p_playlist, "searching art for %s - %s",
182              psz_artist, psz_album );
183     }
184     else
185     {
186         msg_Dbg( p_playlist, "searching art for %s",
187              psz_title ? psz_title : psz_name );
188     }
189     free( psz_title );
190     free( psz_name );
191
192     p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
193
194     if( p_module )
195         i_ret = 1;
196     else
197         msg_Dbg( p_playlist, "unable to find art" );
198
199     /* Record this album */
200     if( psz_artist && psz_album )
201     {
202         playlist_album_t a;
203         a.psz_artist = psz_artist;
204         a.psz_album = psz_album;
205         a.b_found = (i_ret == VLC_EGENERIC ? VLC_FALSE : VLC_TRUE );
206         ARRAY_APPEND( p_playlist->p_fetcher->albums, a );
207     }
208     else
209     {
210         free( psz_artist );
211         free( psz_album );
212     }
213
214     if( p_module )
215         module_Unneed( p_playlist, p_module );
216     p_playlist->p_private = NULL;
217     PL_UNLOCK;
218
219     return i_ret;
220 }
221
222 #ifndef MAX_PATH
223 #   define MAX_PATH 250
224 #endif
225 #define ArtCacheCreateName(a,b,c,d,e,f) __ArtCacheCreateName(VLC_OBJECT(a),b,c,d,e,f)
226 static void __ArtCacheCreateName( vlc_object_t *p_obj,
227                                   char psz_filename[MAX_PATH+1],
228                                   char *psz_title,
229                                   char *psz_artist, char *psz_album,
230                                   const char *psz_extension )
231 {
232     char *psz_ext;
233     if( psz_extension )
234     {
235         psz_ext = strndup( psz_extension, 6 );
236         filename_sanitize( psz_ext );
237     }
238     else psz_ext = strdup( "" );
239     if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
240     {
241         filename_sanitize( psz_title );
242         filename_sanitize( psz_artist );
243         snprintf( psz_filename, MAX_PATH,
244               "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art" DIR_SEP "artistalbum"
245               DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
246               p_obj->p_libvlc->psz_homedir,
247               psz_artist, psz_album, psz_ext );
248     }
249     else
250     {
251         /* We will use the psz_title name to store the art */
252         filename_sanitize( psz_title );
253         snprintf( psz_filename, MAX_PATH,
254               "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art" DIR_SEP "title"
255               DIR_SEP "%s" DIR_SEP "art%s",
256               p_obj->p_libvlc->psz_homedir,
257               psz_title, psz_ext );
258     }
259     free( psz_ext );
260 }
261 #define ArtCacheCreatePath(a,b,c,d) __ArtCacheCreatePath(VLC_OBJECT(a),b,c,d)
262 static void __ArtCacheCreatePath( vlc_object_t *p_obj,
263                                   char *psz_title,
264                                   char *psz_artist, char *psz_album )
265 {
266     char psz_dir[MAX_PATH+1];
267     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
268               p_obj->p_libvlc->psz_homedir );
269     utf8_mkdir( psz_dir );
270     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
271               p_obj->p_libvlc->psz_homedir );
272     utf8_mkdir( psz_dir );
273
274     if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
275     {
276         snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
277                   "art" DIR_SEP "artistalbum",
278                        p_obj->p_libvlc->psz_homedir );
279         utf8_mkdir( psz_dir );
280         snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
281                   "art" DIR_SEP "artistalbum" DIR_SEP "%s",
282                       p_obj->p_libvlc->psz_homedir, psz_artist );
283         utf8_mkdir( psz_dir );
284         snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
285                   "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
286                       p_obj->p_libvlc->psz_homedir,
287                       psz_artist, psz_album );
288         utf8_mkdir( psz_dir );
289     }
290     else
291     {
292         snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
293                   "art" DIR_SEP "title",
294                       p_obj->p_libvlc->psz_homedir );
295         utf8_mkdir( psz_dir );
296         snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
297                   "art" DIR_SEP "title" DIR_SEP "%s",
298                       p_obj->p_libvlc->psz_homedir, psz_title );
299         utf8_mkdir( psz_dir );
300     }
301 }
302 static char *ArtCacheCreateString( const char *psz )
303 {
304     char *dup = strdup(psz);
305     int i;
306
307     /* Doesn't create a filename with invalid characters
308      * TODO: several filesystems forbid several characters: list them all
309      */
310     for( i = 0; dup[i] != '\0'; i++ )
311     {
312         if( dup[i] == '/' )
313             dup[i] = ' ';
314     }
315     return dup;
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_filename[MAX_PATH+1];
324     int i;
325     struct stat a;
326     const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
327
328     if( !p_item->p_meta ) return VLC_EGENERIC;
329
330     psz_artist = input_item_GetArtist( p_item );
331     psz_album = input_item_GetAlbum( p_item );
332     psz_title = input_item_GetTitle( p_item );
333     if( !psz_title ) psz_title = input_item_GetName( p_item );
334
335     if( !psz_title && ( !psz_album || !psz_artist ) )
336     {
337         free( psz_artist );
338         free( psz_album );
339         free( psz_title );
340         return VLC_EGENERIC;
341     }
342     free( psz_title );
343
344     for( i = 0; i < 5; i++ )
345     {
346         ArtCacheCreateName( p_obj, psz_filename, psz_title /* Used if none artist nor album is defined */,
347                             psz_artist, psz_album, ppsz_type[i] );
348
349         /* Check if file exists */
350         if( utf8_stat( psz_filename+7, &a ) == 0 )
351         {
352             input_item_SetArtURL( p_item, psz_filename );
353             free( psz_artist );
354             free( psz_album );
355             return VLC_SUCCESS;
356         }
357     }
358     free( psz_artist );
359     free( psz_album );
360     return VLC_EGENERIC;
361 }
362
363 /**
364  * Download the art using the URL or an art downloaded
365  * This function should be called only if data is not already in cache
366  */
367 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
368 {
369     int i_status = VLC_EGENERIC;
370     stream_t *p_stream;
371     char psz_filename[MAX_PATH+1];
372     char *psz_artist = NULL;
373     char *psz_album = NULL;
374     char *psz_title = NULL;
375     char *psz_name = NULL;
376     char *psz_arturl;
377     char *psz_type;
378
379     psz_artist = input_item_GetArtist( p_item );
380     psz_album = input_item_GetAlbum( p_item );
381     psz_title = input_item_GetTitle( p_item );
382     psz_name = input_item_GetName( p_item );
383
384     if( !psz_title && (!psz_artist || !psz_album) )
385     {
386         free( psz_title );
387         free( psz_album );
388         free( psz_artist );
389         return VLC_EGENERIC;
390     }
391
392     psz_arturl = input_item_GetArtURL( p_item );
393     assert( !EMPTY_STR( psz_arturl ) );
394
395     if( !strncmp( psz_arturl , "APIC", 4 ) )
396     {
397         msg_Warn( p_playlist, "APIC fetch not supported yet" );
398         free( psz_arturl );
399         return VLC_EGENERIC;
400     }
401
402     psz_type = strrchr( psz_arturl, '.' );
403     if( strlen( psz_type ) > 5 ) 
404         psz_type = NULL; /* remove extension if it's > to 4 characters */
405
406     /* */
407     ArtCacheCreateName( p_playlist, psz_filename, psz_title /* Used only if needed*/,
408                         psz_artist, psz_album, psz_type );
409
410     /* */
411     ArtCacheCreatePath( p_playlist, psz_title, psz_artist, psz_album );
412
413     /* */
414     free( psz_artist );
415     free( psz_album );
416     free( psz_title );
417
418     p_stream = stream_UrlNew( p_playlist, psz_arturl );
419     if( p_stream )
420     {
421         uint8_t p_buffer[65536];
422         long int l_read;
423         FILE *p_file = utf8_fopen( psz_filename+7, "w" );
424         if( p_file == NULL ) {
425             msg_Err( p_playlist, "Unable write album art in %s" );
426             free( psz_arturl );
427             return VLC_EGENERIC;
428         }
429         int err = 0;
430         while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
431         {
432             if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
433             {
434                 err = errno;
435                 break;
436             }
437         }
438         if( fclose( p_file ) && !err )
439             err = errno;
440         stream_Delete( p_stream );
441
442         if( err )
443             msg_Err( p_playlist, "%s: %s", psz_filename, strerror( err ) );
444         else
445             msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
446
447         input_item_SetArtURL( p_item, psz_filename );
448         i_status = VLC_SUCCESS;
449     }
450     free( psz_arturl );
451     return i_status;
452 }
453
454 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
455 {
456     input_item_t *p_item = p_input->p->input.p_item;
457     char *psz_arturl;
458     char *psz_artist = NULL;
459     char *psz_album = NULL;
460     char *psz_title = NULL;
461     char *psz_type = NULL;
462     char *psz_artist_m, *psz_album_m, *psz_title_m, *psz_name_m;
463     char psz_filename[MAX_PATH+1];
464     FILE *f;
465     input_attachment_t *p_attachment;
466     struct stat s;
467     int i_idx;
468
469     /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH
470      * and then set it here to to be faster */
471
472     psz_arturl = input_item_GetArtURL( p_item );
473     if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
474     {
475         free( psz_arturl );
476         msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
477         return;
478     }
479     input_item_SetArtURL( p_item, NULL );
480
481     if( input_item_IsArtFetched( p_item ) )
482     {
483         /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
484          * condition */
485         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
486         input_FindArtInCache( p_input, p_item );
487         free( psz_arturl );
488         return;
489     }
490
491     /* */
492     for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ )
493     {
494         if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
495                      &psz_arturl[strlen("attachment://")] ) )
496         {
497             p_attachment = p_input->p->attachment[i_idx];
498             break;
499         }
500     }
501     if( !p_attachment || p_attachment->i_data <= 0 )
502     {
503         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
504         goto end;
505     }
506
507     psz_artist_m = input_item_GetArtist( p_item );
508     psz_album_m = input_item_GetAlbum( p_item );
509     psz_title_m = input_item_GetTitle( p_item );
510     psz_name_m = input_item_GetName( p_item );
511
512     if( psz_artist_m ) psz_artist = ArtCacheCreateString( psz_artist_m );
513     if( psz_album_m ) psz_album = ArtCacheCreateString( psz_album_m );
514     if( psz_title_m ) psz_title = ArtCacheCreateString( psz_title_m );
515     else if( psz_name_m ) psz_title = ArtCacheCreateString( psz_name_m );
516
517     free( psz_artist_m );
518     free( psz_album_m );
519     free( psz_title_m );
520     free( psz_name_m );
521
522     if( (!psz_artist || !psz_album ) && !psz_title )
523         goto end;
524
525     /* */
526     psz_type = strrchr( psz_arturl, '.' );
527     ArtCacheCreateName( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
528
529     /* Check if we already dumped it */
530     if( !utf8_stat( psz_filename+7, &s ) )
531         goto end;
532
533     ArtCacheCreatePath( p_input, psz_title, psz_artist, psz_album );
534
535     f = utf8_fopen( psz_filename+7, "w" );
536     if( f )
537     {
538         if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 )
539             msg_Err( p_input, "%s: %s", psz_filename, strerror( errno ) );
540         else
541             msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
542         fclose( f );
543     }
544
545 end:
546     if( psz_artist ) free( psz_artist );
547     if( psz_album ) free( psz_album );
548     if( psz_title ) free( psz_title );
549     if( psz_arturl ) free( psz_arturl );
550 }
551
552
553 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
554 {
555     uint32_t i_meta = 0;
556
557     if( !p_meta )
558         return 0;
559
560 #define CHECK( a, b ) \
561     if( !EMPTY_STR( vlc_meta_Get( p_meta, vlc_meta_ ## a ) ) ) \
562         i_meta |= VLC_META_ENGINE_ ## b;
563
564     CHECK( Title, TITLE )
565     CHECK( Artist, ARTIST )
566     CHECK( Album, COLLECTION )
567 #if 0
568     /* As this is not used at the moment, don't uselessly check for it.
569      * Re-enable this when it is used */
570     CHECK( Genre, GENRE )
571     CHECK( Copyright, COPYRIGHT )
572     CHECK( Tracknum, SEQ_NUM )
573     CHECK( Description, DESCRIPTION )
574     CHECK( Rating, RATING )
575     CHECK( Date, DATE )
576     CHECK( URL, URL )
577     CHECK( Language, LANGUAGE )
578 #endif
579     CHECK( ArtworkURL, ART_URL )
580
581     return i_meta;
582 }