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