]> git.sesse.net Git - vlc/blob - src/input/meta.c
Input access locking, part 2.
[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
76     uint32_t i_meta = input_CurrentMetaFlags( p_item->p_meta );
77     *pi_mandatory &= ~i_meta;
78     *pi_optional = 0; /// Todo
79     return *pi_mandatory ? VLC_FALSE:VLC_TRUE;
80 }
81
82 int input_MetaFetch( playlist_t *p_playlist, input_item_t *p_item )
83 {
84     struct meta_engine_t *p_me;
85     uint32_t i_mandatory, i_optional;
86
87     input_MetaSatisfied( p_playlist, p_item, &i_mandatory, &i_optional );
88     // Meta shouldn't magically appear
89     assert( i_mandatory );
90
91     /* FIXME: object creation is overkill, use p_private */
92     p_me = vlc_object_create( p_playlist, VLC_OBJECT_META_ENGINE );
93     p_me->i_flags |= OBJECT_FLAGS_NOINTERACT;
94     p_me->i_flags |= OBJECT_FLAGS_QUIET;
95     p_me->i_mandatory = i_mandatory;
96     p_me->i_optional = i_optional;
97
98     p_me->p_item = p_item;
99     p_me->p_module = module_Need( p_me, "meta fetcher", 0, VLC_FALSE );
100     if( !p_me->p_module )
101     {
102         vlc_object_destroy( p_me );
103         return VLC_EGENERIC;
104     }
105     module_Unneed( p_me, p_me->p_module );
106     vlc_object_destroy( p_me );
107
108     input_item_SetMetaFetched( p_item, VLC_TRUE );
109
110     return VLC_SUCCESS;
111 }
112
113 /* Return codes:
114  *   0 : Art is in cache
115  *   1 : Art found, need to download
116  *  -X : Error/not found
117  */
118 int input_ArtFind( playlist_t *p_playlist, input_item_t *p_item )
119 {
120     int i_ret = VLC_EGENERIC;
121     module_t *p_module;
122     char *psz_name, *psz_title, *psz_artist, *psz_album;
123
124     if( !p_item->p_meta )
125         return VLC_EGENERIC;
126
127     psz_name = input_item_GetName( p_item );
128     psz_title = input_item_GetTitle( p_item );
129     psz_artist = input_item_GetArtist( p_item );
130     psz_album = input_item_GetAlbum( p_item );
131
132     if(  !psz_name && !psz_title && !psz_artist && !psz_album )
133         return VLC_EGENERIC;
134     free( psz_name );
135     free( psz_title );
136
137     /* If we already checked this album in this session, skip */
138     if( psz_artist && psz_album )
139     {
140         FOREACH_ARRAY( playlist_album_t album, p_playlist->p_fetcher->albums )
141             if( !strcmp( album.psz_artist, psz_artist ) &&
142                 !strcmp( album.psz_album, psz_album ) )
143             {
144                 msg_Dbg( p_playlist, " %s - %s has already been searched",
145                          psz_artist, psz_album );
146         /* TODO-fenrir if we cache art filename too, we can go faster */
147                 free( psz_artist );
148                 free( psz_album );
149                 if( album.b_found )
150                 {
151                     /* Actually get URL from cache */
152                     input_FindArtInCache( p_playlist, p_item );
153                     return 0;
154                 }
155                 else
156                 {
157                     return VLC_EGENERIC;
158                 }
159             }
160         FOREACH_END();
161     }
162     free( psz_artist );
163     free( psz_album );
164
165     char *psz_arturl = input_item_GetArtURL( p_item );
166     input_FindArtInCache( p_playlist, p_item );
167     if( !EMPTY_STR( psz_arturl ) )
168     {
169         free( psz_arturl );
170         return 0;
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_name = input_item_GetName( p_item );
179     psz_title = input_item_GetTitle( p_item );
180     if( psz_album && psz_artist )
181     {
182         msg_Dbg( p_playlist, "searching art for %s - %s",
183              psz_artist, psz_album );
184     }
185     else
186     {
187         msg_Dbg( p_playlist, "searching art for %s",
188              psz_title ? psz_title : psz_name );
189     }
190     free( psz_title );
191     free( psz_name );
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 #define ArtCacheCreateName(a,b,c,d,e,f) __ArtCacheCreateName(VLC_OBJECT(a),b,c,d,e,f)
227 static void __ArtCacheCreateName( vlc_object_t *p_obj,
228                                   char psz_filename[MAX_PATH+1],
229                                   const char *psz_title,
230                                   const char *psz_artist, const char *psz_album,
231                                   const char *psz_extension )
232 {
233     if( psz_artist && psz_artist )
234     {
235         snprintf( psz_filename, MAX_PATH,
236               "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art" DIR_SEP "artistalbum"
237               DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
238               p_obj->p_libvlc->psz_homedir,
239               psz_artist, psz_album, psz_extension ? psz_extension : "" );
240     }
241     else
242     {
243         /* We will use the psz_title name to store the art */
244         snprintf( psz_filename, MAX_PATH,
245               "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art" DIR_SEP "title"
246               DIR_SEP "%s" DIR_SEP "art%s",
247               p_obj->p_libvlc->psz_homedir,
248               psz_title, psz_extension ? psz_extension : "" );
249     }
250 }
251 #define ArtCacheCreatePath(a,b,c,d) __ArtCacheCreatePath(VLC_OBJECT(a),b,c,d)
252 static void __ArtCacheCreatePath( vlc_object_t *p_obj,
253                                   const char *psz_title,
254                                   const char *psz_artist, const char *psz_album )
255 {
256     char psz_dir[MAX_PATH+1];
257     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
258               p_obj->p_libvlc->psz_homedir );
259     utf8_mkdir( psz_dir );
260     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
261               p_obj->p_libvlc->psz_homedir );
262     utf8_mkdir( psz_dir );
263
264     if( psz_artist && psz_artist )
265     {
266         snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
267                   "art" DIR_SEP "artistalbum",
268                        p_obj->p_libvlc->psz_homedir );
269         utf8_mkdir( psz_dir );
270         snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
271                   "art" DIR_SEP "artistalbum" DIR_SEP "%s",
272                       p_obj->p_libvlc->psz_homedir, psz_artist );
273         utf8_mkdir( psz_dir );
274         snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
275                   "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
276                       p_obj->p_libvlc->psz_homedir,
277                       psz_artist, psz_album );
278         utf8_mkdir( psz_dir );
279     }
280     else
281     {
282         snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
283                   "art" DIR_SEP "title",
284                       p_obj->p_libvlc->psz_homedir );
285         utf8_mkdir( psz_dir );
286         snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
287                   "art" DIR_SEP "title" DIR_SEP "%s",
288                       p_obj->p_libvlc->psz_homedir, psz_title );
289         utf8_mkdir( psz_dir );
290     }
291 }
292 static char *ArtCacheCreateString( const char *psz )
293 {
294     char *dup = strdup(psz);
295     int i;
296
297     /* Doesn't create a filename with invalid characters
298      * TODO: several filesystems forbid several characters: list them all
299      */
300     for( i = 0; dup[i] != '\0'; i++ )
301     {
302         if( dup[i] == '/' )
303             dup[i] = ' ';
304     }
305     return dup;
306 }
307
308 static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
309 {
310     char *psz_artist;
311     char *psz_album;
312     char *psz_title;
313     char psz_filename[MAX_PATH+1];
314     int i;
315     struct stat a;
316     const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
317
318     if( !p_item->p_meta ) return VLC_EGENERIC;
319
320     psz_artist = input_item_GetArtist( p_item );
321     psz_album = input_item_GetAlbum( p_item );
322     psz_title = input_item_GetTitle( p_item );
323     if( !psz_title ) psz_title = input_item_GetName( p_item );
324
325     if( !psz_title && ( !psz_album || !psz_artist ) )
326     {
327         free( psz_artist );
328         free( psz_album );
329         free( psz_title );
330         return VLC_EGENERIC;
331     }
332     free( psz_title );
333
334     for( i = 0; i < 5; i++ )
335     {
336         ArtCacheCreateName( p_obj, psz_filename, psz_title /* Used if none artist nor album is defined */,
337                             psz_artist, psz_album, ppsz_type[i] );
338
339         /* Check if file exists */
340         if( utf8_stat( psz_filename+7, &a ) == 0 )
341         {
342             input_item_SetArtURL( p_item, psz_filename );
343             free( psz_artist );
344             free( psz_album );
345             return VLC_SUCCESS;
346         }
347     }
348     free( psz_artist );
349     free( psz_album );
350     return VLC_EGENERIC;
351 }
352
353 /**
354  * Download the art using the URL or an art downloaded
355  * This function should be called only if data is not already in cache
356  */
357 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
358 {
359     int i_status = VLC_EGENERIC;
360     stream_t *p_stream;
361     char psz_filename[MAX_PATH+1];
362     char *psz_artist = NULL;
363     char *psz_album = NULL;
364     char *psz_title = NULL;
365     char *psz_artist_m, *psz_album_m, *psz_title_m, *psz_name_m, *psz_arturl_m;
366     char *psz_type;
367
368     psz_artist_m = input_item_GetArtist( p_item );
369     psz_album_m = input_item_GetAlbum( p_item );
370     psz_title_m = input_item_GetTitle( p_item );
371     psz_name_m = input_item_GetName( p_item );
372
373     if( psz_artist_m ) psz_artist = ArtCacheCreateString( psz_artist_m );
374     if( psz_album_m ) psz_album = ArtCacheCreateString( psz_album_m );
375     if( psz_title_m ) psz_title = ArtCacheCreateString( psz_title_m );
376     else if( psz_name_m ) psz_title = ArtCacheCreateString( psz_name_m );
377
378     free( psz_artist_m );
379     free( psz_album_m );
380     free( psz_title_m );
381     free( psz_name_m );
382
383     if( !psz_title && (!psz_artist || !psz_album) )
384     {
385         free( psz_title );
386         free( psz_album );
387         free( psz_artist );
388         return VLC_EGENERIC;
389     }
390
391     psz_arturl_m = input_item_GetArtURL( p_item );
392     assert( !EMPTY_STR( psz_arturl_m ) );
393
394     psz_type = strrchr( psz_arturl_m, '.' );
395
396     /* */
397     ArtCacheCreateName( p_playlist, psz_filename, psz_title /* Used only if needed*/,
398                         psz_artist, psz_album, psz_type );
399
400     /* */
401     ArtCacheCreatePath( p_playlist, psz_title, psz_artist, psz_album );
402
403     /* */
404     free( psz_artist );
405     free( psz_album );
406     free( psz_title );
407
408     if( !strncmp( psz_arturl_m , "APIC", 4 ) )
409     {
410         msg_Warn( p_playlist, "APIC fetch not supported yet" );
411         free( psz_arturl_m );
412         return VLC_EGENERIC;
413     }
414
415     p_stream = stream_UrlNew( p_playlist, psz_arturl_m );
416     if( p_stream )
417     {
418         uint8_t p_buffer[65536];
419         long int l_read;
420         FILE *p_file = utf8_fopen( psz_filename+7, "w" );
421         int err = 0;
422         while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
423         {
424             if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
425             {
426                 err = errno;
427                 break;
428             }
429         }
430         if( fclose( p_file ) && !err )
431             err = errno;
432         stream_Delete( p_stream );
433
434         if( err )
435             msg_Err( p_playlist, "%s: %s", psz_filename, strerror( err ) );
436         else
437             msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
438
439         input_item_SetArtURL( p_item, psz_filename );
440         i_status = VLC_SUCCESS;
441     }
442     free( psz_arturl_m );
443     return i_status;
444 }
445
446 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
447 {
448     input_item_t *p_item = p_input->p->input.p_item;
449     char *psz_arturl;
450     char *psz_artist = NULL;
451     char *psz_album = NULL;
452     char *psz_title = NULL;
453     char *psz_type = NULL;
454     char *psz_artist_m, *psz_album_m, *psz_title_m, *psz_name_m;
455     char psz_filename[MAX_PATH+1];
456     FILE *f;
457     input_attachment_t *p_attachment;
458     struct stat s;
459     int i_idx;
460
461     /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH
462      * and then set it here to to be faster */
463
464     psz_arturl = input_item_GetArtURL( p_item );
465     if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
466     {
467         free( psz_arturl );
468         msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
469         return;
470     }
471     input_item_SetArtURL( p_item, NULL );
472
473     if( input_item_IsArtFetched( p_item ) )
474     {
475         /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
476          * condition */
477         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
478         input_FindArtInCache( p_input, p_item );
479         free( psz_arturl );
480         return;
481     }
482
483     /* */
484     for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ )
485     {
486         if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
487                      &psz_arturl[strlen("attachment://")] ) )
488         {
489             p_attachment = p_input->p->attachment[i_idx];
490             break;
491         }
492     }
493     if( !p_attachment || p_attachment->i_data <= 0 )
494     {
495         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
496         goto end;
497     }
498
499     psz_artist_m = input_item_GetArtist( p_item );
500     psz_album_m = input_item_GetAlbum( p_item );
501     psz_title_m = input_item_GetTitle( p_item );
502     psz_name_m = input_item_GetName( p_item );
503
504     if( psz_artist_m ) psz_artist = ArtCacheCreateString( psz_artist_m );
505     if( psz_album_m ) psz_album = ArtCacheCreateString( psz_album_m );
506     if( psz_title_m ) psz_title = ArtCacheCreateString( psz_title_m );
507     else if( psz_name_m ) psz_title = ArtCacheCreateString( psz_name_m );
508
509     free( psz_artist_m );
510     free( psz_album_m );
511     free( psz_title_m );
512     free( psz_name_m );
513
514     if( (!psz_artist || !psz_album ) && !psz_title )
515         goto end;
516
517     /* */
518     psz_type = strrchr( psz_arturl, '.' );
519     ArtCacheCreateName( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
520
521     /* Check if we already dumped it */
522     if( !utf8_stat( psz_filename+7, &s ) )
523         goto end;
524
525     ArtCacheCreatePath( p_input, psz_title, psz_artist, psz_album );
526
527     f = utf8_fopen( psz_filename+7, "w" );
528     if( f )
529     {
530         if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 )
531             msg_Err( p_input, "%s: %s", psz_filename, strerror( errno ) );
532         else
533             msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
534         fclose( f );
535     }
536
537 end:
538     if( psz_artist ) free( psz_artist );
539     if( psz_album ) free( psz_album );
540     if( psz_title ) free( psz_title );
541     if( psz_arturl ) free( psz_arturl );
542 }
543
544
545 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
546 {
547     uint32_t i_meta = 0;
548
549     if( !p_meta )
550         return 0;
551
552 #define CHECK( a, b ) \
553     if( !EMPTY_STR( vlc_meta_Get( p_meta, vlc_meta_ ## a ) ) ) \
554         i_meta |= VLC_META_ENGINE_ ## b;
555
556     CHECK( Title, TITLE )
557     CHECK( Artist, ARTIST )
558     CHECK( Album, COLLECTION )
559 #if 0
560     /* As this is not used at the moment, don't uselessly check for it.
561      * Re-enable this when it is used */
562     CHECK( Genre, GENRE )
563     CHECK( Copyright, COPYRIGHT )
564     CHECK( Tracknum, SEQ_NUM )
565     CHECK( Description, DESCRIPTION )
566     CHECK( Rating, RATING )
567     CHECK( Date, DATE )
568     CHECK( URL, URL )
569     CHECK( Language, LANGUAGE )
570 #endif
571     CHECK( ArtworkURL, ART_URL )
572
573     return i_meta;
574 }