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