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