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