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