]> git.sesse.net Git - vlc/blob - src/input/meta.c
input/meta.c: * Remove psz_name
[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_title = input_item_GetTitle( p_item );
128     if(!psz_title)
129         psz_title = input_item_GetName( p_item );
130
131     if(  !psz_title && !psz_artist && !psz_album )
132         return VLC_EGENERIC;
133
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_title = input_item_GetTitle( p_item );
178     if( !psz_title )
179         psz_title = input_item_GetName( p_item );
180
181     if( psz_album && psz_artist )
182     {
183         msg_Dbg( p_playlist, "searching art for %s - %s",
184              psz_artist, psz_album );
185     }
186     else
187     {
188         msg_Dbg( p_playlist, "searching art for %s",
189              psz_title );
190     }
191     free( psz_title );
192
193     p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
194
195     if( p_module )
196         i_ret = 1;
197     else
198         msg_Dbg( p_playlist, "unable to find art" );
199
200     /* Record this album */
201     if( psz_artist && psz_album )
202     {
203         playlist_album_t a;
204         a.psz_artist = psz_artist;
205         a.psz_album = psz_album;
206         a.b_found = (i_ret == VLC_EGENERIC ? VLC_FALSE : VLC_TRUE );
207         ARRAY_APPEND( p_playlist->p_fetcher->albums, a );
208     }
209     else
210     {
211         free( psz_artist );
212         free( psz_album );
213     }
214
215     if( p_module )
216         module_Unneed( p_playlist, p_module );
217     p_playlist->p_private = NULL;
218     PL_UNLOCK;
219
220     return i_ret;
221 }
222
223 #ifndef MAX_PATH
224 #   define MAX_PATH 250
225 #endif
226 static void ArtCacheCreateDir( const char *psz_dir )
227 {
228     char newdir[MAX_PATH+1];
229     strcpy( newdir, psz_dir );
230     char * psz_newdir = newdir;
231     char * psz = psz_newdir;
232
233     while( *psz_newdir )
234     {
235         while( *psz && *psz != '/') psz++;
236         if( !*psz ) break;
237         *psz = 0;        
238         if( !EMPTY_STR( psz_newdir ) ) utf8_mkdir( psz_newdir );
239         psz_newdir = psz+1;
240     }
241 }
242
243 static char * ArtCacheGetSanitizedFileName( const char *psz )
244 {
245     char *dup = strdup(psz);
246     int i;
247
248     filename_sanitize( dup );
249
250     /* Doesn't create a filename with invalid characters
251      * TODO: several filesystems forbid several characters: list them all
252      */
253     for( i = 0; dup[i] != '\0'; i++ )
254     {
255         if( dup[i] == '/' )
256             dup[i] = ' ';
257     }
258     return dup;
259 }
260
261 #define ArtCacheGetDirPath(a,b,c,d,e) __ArtCacheGetDirPath(VLC_OBJECT(a),b,c,d,e)
262 static void __ArtCacheGetDirPath( vlc_object_t *p_obj,
263                                   char *psz_dir,
264                                   const char *psz_title,
265                                   const char *psz_artist, const char *psz_album )
266 {
267     if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
268     {
269         char * psz_album_sanitized = ArtCacheGetSanitizedFileName( psz_album );
270         char * psz_artist_sanitized = ArtCacheGetSanitizedFileName( psz_artist );
271
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_sanitized, psz_album_sanitized );
276         free( psz_album_sanitized );
277         free( psz_artist_sanitized );
278     }
279     else
280     {
281         char * psz_title_sanitized = ArtCacheGetSanitizedFileName( psz_title );
282         snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
283                   "art" DIR_SEP "title" DIR_SEP "%s",
284                   p_obj->p_libvlc->psz_homedir,
285                   psz_title_sanitized );
286         free( psz_title_sanitized );
287     }
288 }
289
290
291
292 #define ArtCacheGetFilePath(a,b,c,d,e,f) __ArtCacheGetFilePath(VLC_OBJECT(a),b,c,d,e,f)
293 static void __ArtCacheGetFilePath( vlc_object_t *p_obj,
294                                    char * psz_filename,
295                                    const char *psz_title,
296                                    const char *psz_artist, const char *psz_album,
297                                    const char *psz_extension )
298 {
299     char psz_dir[MAX_PATH+1];
300     char * psz_ext;
301     ArtCacheGetDirPath( p_obj, psz_dir, psz_title, psz_artist, psz_album );
302
303     if( psz_extension )
304     {
305         psz_ext = strndup( psz_extension, 6 );
306         filename_sanitize( psz_ext );
307     }
308     else psz_ext = strdup( "" );
309
310     snprintf( psz_filename, MAX_PATH, "file://%s" DIR_SEP "art%s",
311               psz_dir, psz_ext );
312
313     free( psz_ext );
314 }
315
316 static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
317 {
318     char *psz_artist;
319     char *psz_album;
320     char *psz_title;
321     char psz_dirname[MAX_PATH+1];
322     char psz_filename[MAX_PATH+1];
323     struct dirent * p_de;
324     DIR * p_dir;
325
326     if( !p_item->p_meta ) return VLC_EGENERIC;
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_dirname, 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_dirname );
350     if( !p_dir )
351         return VLC_EGENERIC;
352
353     while( (p_de = readdir( p_dir )) )
354     {
355         if( strncmp( p_de->d_name, "art", 3 ) )
356         {
357             snprintf( psz_filename, MAX_PATH, "%s" DIR_SEP "%s",
358                             psz_dirname, p_de->d_name );
359             input_item_SetArtURL( p_item, psz_filename );
360             closedir( p_dir );
361             return VLC_SUCCESS;
362         }
363     }
364
365     closedir( p_dir );
366     return VLC_EGENERIC;
367 }
368
369 /**
370  * Download the art using the URL or an art downloaded
371  * This function should be called only if data is not already in cache
372  */
373 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
374 {
375     int i_status = VLC_EGENERIC;
376     stream_t *p_stream;
377     char psz_filename[MAX_PATH+1];
378     char *psz_artist = NULL;
379     char *psz_album = NULL;
380     char *psz_title = NULL;
381     char *psz_arturl;
382     char *psz_type;
383
384     psz_artist = input_item_GetArtist( p_item );
385     psz_album = input_item_GetAlbum( p_item );
386     psz_title = input_item_GetTitle( p_item );
387     if( !psz_title )
388         psz_title = input_item_GetName( p_item );
389
390     if( !psz_title && (!psz_artist || !psz_album) )
391     {
392         free( psz_title );
393         free( psz_album );
394         free( psz_artist );
395         return VLC_EGENERIC;
396     }
397
398     psz_arturl = input_item_GetArtURL( p_item );
399     assert( !EMPTY_STR( psz_arturl ) );
400
401     if( !strncmp( psz_arturl , "APIC", 4 ) )
402     {
403         msg_Warn( p_playlist, "APIC fetch not supported yet" );
404         free( psz_arturl );
405         return VLC_EGENERIC;
406     }
407
408     psz_type = strrchr( psz_arturl, '.' );
409     if( strlen( psz_type ) > 5 ) 
410         psz_type = NULL; /* remove extension if it's > to 4 characters */
411
412     /* Warning: psz_title, psz_artist, psz_album may change in ArtCache*() */
413
414     ArtCacheGetDirPath( p_playlist, psz_filename, psz_title, psz_artist,
415                         psz_album );
416     ArtCacheCreateDir( psz_filename );
417     ArtCacheGetFilePath( p_playlist, psz_filename, psz_title, psz_artist,
418                          psz_album, psz_type );
419
420     free( psz_artist );
421     free( psz_album );
422     free( psz_title );
423
424     p_stream = stream_UrlNew( p_playlist, psz_arturl );
425     if( p_stream )
426     {
427         uint8_t p_buffer[65536];
428         long int l_read;
429         FILE *p_file = utf8_fopen( psz_filename+7, "w" );
430         if( p_file == NULL ) {
431             msg_Err( p_playlist, "Unable write album art in %s" );
432             free( psz_arturl );
433             return VLC_EGENERIC;
434         }
435         int err = 0;
436         while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
437         {
438             if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
439             {
440                 err = errno;
441                 break;
442             }
443         }
444         if( fclose( p_file ) && !err )
445             err = errno;
446         stream_Delete( p_stream );
447
448         if( err )
449             msg_Err( p_playlist, "%s: %s", psz_filename, strerror( err ) );
450         else
451             msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
452
453         input_item_SetArtURL( p_item, psz_filename );
454         i_status = VLC_SUCCESS;
455     }
456     free( psz_arturl );
457     return i_status;
458 }
459
460 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
461 {
462     input_item_t *p_item = p_input->p->input.p_item;
463     char *psz_arturl;
464     char *psz_artist = NULL;
465     char *psz_album = NULL;
466     char *psz_title = NULL;
467     char *psz_type = NULL;
468     char psz_filename[MAX_PATH+1];
469     FILE *f;
470     input_attachment_t *p_attachment;
471     struct stat s;
472     int i_idx;
473
474     /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH
475      * and then set it here to to be faster */
476
477     psz_arturl = input_item_GetArtURL( p_item );
478     if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
479     {
480         free( psz_arturl );
481         msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
482         return;
483     }
484     input_item_SetArtURL( p_item, NULL );
485
486     if( input_item_IsArtFetched( p_item ) )
487     {
488         /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
489          * condition */
490         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
491         input_FindArtInCache( p_input, p_item );
492         free( psz_arturl );
493         return;
494     }
495
496     /* */
497     for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ )
498     {
499         if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
500                      &psz_arturl[strlen("attachment://")] ) )
501         {
502             p_attachment = p_input->p->attachment[i_idx];
503             break;
504         }
505     }
506     if( !p_attachment || p_attachment->i_data <= 0 )
507     {
508         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
509         goto end;
510     }
511
512     psz_artist = input_item_GetArtist( p_item );
513     psz_album = input_item_GetAlbum( p_item );
514     psz_title = input_item_GetTitle( p_item );
515     if( !psz_title )
516         psz_title = input_item_GetName( p_item );
517
518     if( (!psz_artist || !psz_album ) && !psz_title )
519         goto end;
520
521     /* */
522     psz_type = strrchr( psz_arturl, '.' );
523
524     ArtCacheGetDirPath( p_input, psz_filename, psz_title, psz_artist, psz_album );
525     ArtCacheCreateDir( psz_filename );
526     ArtCacheGetFilePath( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
527
528     /* Check if we already dumped it */
529     if( !utf8_stat( psz_filename+7, &s ) )
530         goto end;
531
532     f = utf8_fopen( psz_filename+7, "w" );
533     if( f )
534     {
535         if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 )
536             msg_Err( p_input, "%s: %s", psz_filename, strerror( errno ) );
537         else
538             msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
539         fclose( f );
540     }
541
542 end:
543     if( psz_artist ) free( psz_artist );
544     if( psz_album ) free( psz_album );
545     if( psz_title ) free( psz_title );
546     if( psz_arturl ) free( psz_arturl );
547 }
548
549
550 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
551 {
552     uint32_t i_meta = 0;
553
554     if( !p_meta )
555         return 0;
556
557 #define CHECK( a, b ) \
558     if( !EMPTY_STR( vlc_meta_Get( p_meta, vlc_meta_ ## a ) ) ) \
559         i_meta |= VLC_META_ENGINE_ ## b;
560
561     CHECK( Title, TITLE )
562     CHECK( Artist, ARTIST )
563     CHECK( Album, COLLECTION )
564 #if 0
565     /* As this is not used at the moment, don't uselessly check for it.
566      * Re-enable this when it is used */
567     CHECK( Genre, GENRE )
568     CHECK( Copyright, COPYRIGHT )
569     CHECK( Tracknum, SEQ_NUM )
570     CHECK( Description, DESCRIPTION )
571     CHECK( Rating, RATING )
572     CHECK( Date, DATE )
573     CHECK( URL, URL )
574     CHECK( Language, LANGUAGE )
575 #endif
576     CHECK( ArtworkURL, ART_URL )
577
578     return i_meta;
579 }