]> git.sesse.net Git - vlc/blob - src/input/meta.c
input/meta.c: Better handle NULL meta.
[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
123     if( !p_item->p_meta )
124         return VLC_EGENERIC;
125
126     if(  !p_item->psz_name && !input_item_GetTitle( p_item ) &&
127         (!input_item_GetArtist( p_item ) || !input_item_GetAlbum( p_item )) )
128         return VLC_EGENERIC;
129
130     /* If we already checked this album in this session, skip */
131     if( input_item_GetArtist( p_item ) && input_item_GetAlbum( p_item ) )
132     {
133         FOREACH_ARRAY( playlist_album_t album, p_playlist->p_fetcher->albums )
134             if( !strcmp( album.psz_artist, input_item_GetArtist( p_item ) ) &&
135                 !strcmp( album.psz_album, input_item_GetAlbum( p_item ) ) )
136             {
137                 msg_Dbg( p_playlist, " %s - %s has already been searched",
138                          input_item_GetArtist( p_item ),  input_item_GetAlbum( p_item ) );
139         /* TODO-fenrir if we cache art filename too, we can go faster */
140                 if( album.b_found )
141                 {
142                     /* Actually get URL from cache */
143                     input_FindArtInCache( p_playlist, p_item );
144                     return 0;
145                 }
146                 else
147                 {
148                     return VLC_EGENERIC;
149                 }
150             }
151         FOREACH_END();
152     }
153
154     input_FindArtInCache( p_playlist, p_item );
155     if( !EMPTY_STR(input_item_GetArtURL( p_item )) )
156         return 0;
157
158     PL_LOCK;
159     p_playlist->p_private = p_item;
160     if( input_item_GetAlbum( p_item ) && input_item_GetArtist( p_item ) )
161     {
162         msg_Dbg( p_playlist, "searching art for %s - %s",
163              input_item_GetArtist( p_item ),  input_item_GetAlbum( p_item ) );
164     }
165     else
166     {
167         msg_Dbg( p_playlist, "searching art for %s",
168              input_item_GetTitle( p_item ) ? input_item_GetTitle( p_item ) : p_item->psz_name );
169     }
170
171     p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
172
173     if( p_module )
174         i_ret = 1;
175     else
176         msg_Dbg( p_playlist, "unable to find art" );
177
178     /* Record this album */
179     if( input_item_GetArtist( p_item ) && input_item_GetAlbum( p_item ) )
180     {
181         playlist_album_t a;
182         a.psz_artist = strdup( input_item_GetArtist( p_item ) );
183         a.psz_album = strdup( input_item_GetAlbum( p_item ) );
184         a.b_found = (i_ret == VLC_EGENERIC ? VLC_FALSE : VLC_TRUE );
185         ARRAY_APPEND( p_playlist->p_fetcher->albums, a );
186     }
187
188     if( p_module )
189         module_Unneed( p_playlist, p_module );
190     p_playlist->p_private = NULL;
191     PL_UNLOCK;
192
193     return i_ret;
194 }
195
196 #ifndef MAX_PATH
197 #   define MAX_PATH 250
198 #endif
199 #define ArtCacheCreateName(a,b,c,d,e,f) __ArtCacheCreateName(VLC_OBJECT(a),b,c,d,e,f)
200 static void __ArtCacheCreateName( vlc_object_t *p_obj,
201                                   char psz_filename[MAX_PATH+1],
202                                   const char *psz_title,
203                                   const char *psz_artist, const char *psz_album,
204                                   const char *psz_extension )
205 {
206     if( psz_artist && psz_artist )
207     {
208         snprintf( psz_filename, MAX_PATH,
209               "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art" DIR_SEP "artistalbum"
210               DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
211               p_obj->p_libvlc->psz_homedir,
212               psz_artist, psz_album, psz_extension ? psz_extension : "" );
213     }
214     else
215     {
216         /* We will use the psz_title name to store the art */
217         snprintf( psz_filename, MAX_PATH,
218               "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art" DIR_SEP "title"
219               DIR_SEP "%s" DIR_SEP "art%s",
220               p_obj->p_libvlc->psz_homedir,
221               psz_title, psz_extension ? psz_extension : "" );
222     }
223 }
224 #define ArtCacheCreatePath(a,b,c,d) __ArtCacheCreatePath(VLC_OBJECT(a),b,c,d)
225 static void __ArtCacheCreatePath( vlc_object_t *p_obj,
226                                   const char *psz_title,
227                                   const char *psz_artist, const char *psz_album )
228 {
229     char psz_dir[MAX_PATH+1];
230     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
231               p_obj->p_libvlc->psz_homedir );
232     utf8_mkdir( psz_dir );
233     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
234               p_obj->p_libvlc->psz_homedir );
235     utf8_mkdir( psz_dir );
236
237     if( psz_artist && psz_artist )
238     {
239         snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
240                   "art" DIR_SEP "artistalbum",
241                        p_obj->p_libvlc->psz_homedir );
242         utf8_mkdir( psz_dir );
243         snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
244                   "art" DIR_SEP "artistalbum" DIR_SEP "%s",
245                       p_obj->p_libvlc->psz_homedir, psz_artist );
246         utf8_mkdir( psz_dir );
247         snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
248                   "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
249                       p_obj->p_libvlc->psz_homedir,
250                       psz_artist, psz_album );
251         utf8_mkdir( psz_dir );
252     }
253     else
254     {
255         snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
256                   "art" DIR_SEP "title",
257                       p_obj->p_libvlc->psz_homedir );
258         utf8_mkdir( psz_dir );
259         snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
260                   "art" DIR_SEP "title" DIR_SEP "%s",
261                       p_obj->p_libvlc->psz_homedir, psz_title );
262         utf8_mkdir( psz_dir );
263     }
264 }
265 static char *ArtCacheCreateString( const char *psz )
266 {
267     char *dup = strdup(psz);
268     int i;
269
270     /* Doesn't create a filename with invalid characters
271      * TODO: several filesystems forbid several characters: list them all
272      */
273     for( i = 0; dup[i] != '\0'; i++ )
274     {
275         if( dup[i] == '/' )
276             dup[i] = ' ';
277     }
278     return dup;
279 }
280
281 static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
282 {
283     const char *psz_artist;
284     const char *psz_album;
285     const char *psz_title;
286     char psz_filename[MAX_PATH+1];
287     int i;
288     struct stat a;
289     const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
290
291     if( !p_item->p_meta ) return VLC_EGENERIC;
292
293     psz_artist = input_item_GetArtist( p_item );
294     psz_album = input_item_GetAlbum( p_item );
295     psz_title = input_item_GetTitle( p_item );
296     if( !psz_title ) psz_title = p_item->psz_name;
297
298     if( (!psz_artist || !psz_album) && !psz_title ) return VLC_EGENERIC;
299
300     for( i = 0; i < 5; i++ )
301     {
302         ArtCacheCreateName( p_obj, psz_filename, psz_title /* Used if none artist nor album is defined */,
303                             psz_artist, psz_album, ppsz_type[i] );
304
305         /* Check if file exists */
306         if( utf8_stat( psz_filename+7, &a ) == 0 )
307         {
308             input_item_SetArtURL( p_item, psz_filename );
309             return VLC_SUCCESS;
310         }
311     }
312     return VLC_EGENERIC;
313 }
314
315 /**
316  * Download the art using the URL or an art downloaded
317  * This function should be called only if data is not already in cache
318  */
319 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
320 {
321     int i_status = VLC_EGENERIC;
322     stream_t *p_stream;
323     char psz_filename[MAX_PATH+1];
324     char *psz_artist = NULL;
325     char *psz_album = NULL;
326     char *psz_title = NULL;
327     char *psz_type;
328     if( input_item_GetArtist( p_item ) )
329         psz_artist = ArtCacheCreateString( input_item_GetArtist( p_item ) );
330     if( input_item_GetAlbum( p_item ) )
331         psz_album = ArtCacheCreateString( input_item_GetAlbum( p_item ) );
332     if( input_item_GetTitle( p_item ) )
333         psz_title = ArtCacheCreateString( input_item_GetTitle( p_item ) );
334     else if( p_item->psz_name )
335         psz_title = ArtCacheCreateString( p_item->psz_name );
336
337     if( (!psz_title || !psz_album) && !psz_artist )
338     {
339         free( psz_title );
340         free( psz_album );
341         free( psz_artist );
342         return VLC_EGENERIC;
343     }
344
345     assert( !EMPTY_STR(input_item_GetArtURL( p_item )) );
346
347     psz_type = strrchr( input_item_GetArtURL( p_item ), '.' );
348
349     /* */
350     ArtCacheCreateName( p_playlist, psz_filename, psz_title /* Used only if needed*/,
351                         psz_artist, psz_album, psz_type );
352
353     /* */
354     ArtCacheCreatePath( p_playlist, psz_title, psz_artist, psz_album );
355
356     /* */
357     free( psz_artist );
358     free( psz_album );
359     free( psz_title );
360
361     if( !strncmp( input_item_GetArtURL( p_item ) , "APIC", 4 ) )
362     {
363         msg_Warn( p_playlist, "APIC fetch not supported yet" );
364         return VLC_EGENERIC;
365     }
366
367     p_stream = stream_UrlNew( p_playlist, input_item_GetArtURL( p_item ) );
368     if( p_stream )
369     {
370         uint8_t p_buffer[65536];
371         long int l_read;
372         FILE *p_file = utf8_fopen( psz_filename+7, "w" );
373         int err = 0;
374         while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
375         {
376             if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
377             {
378                 err = errno;
379                 break;
380             }
381         }
382         if( fclose( p_file ) && !err )
383             err = errno;
384         stream_Delete( p_stream );
385
386         if( err )
387             msg_Err( p_playlist, "%s: %s", psz_filename, strerror( err ) );
388         else
389             msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
390
391         input_item_SetArtURL( p_item, psz_filename );
392         i_status = VLC_SUCCESS;
393     }
394     return i_status;
395 }
396
397 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
398 {
399     input_item_t *p_item = p_input->p->input.p_item;
400     char *psz_arturl;
401     char *psz_artist = NULL;
402     char *psz_album = NULL;
403     char *psz_title = NULL;
404     char *psz_type = NULL;
405     char psz_filename[MAX_PATH+1];
406     FILE *f;
407     input_attachment_t *p_attachment;
408     struct stat s;
409     int i_idx;
410
411     /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH
412      * and then set it here to to be faster */
413
414     psz_arturl = strdup( input_item_GetArtURL( p_item ) );
415     if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
416     {
417         msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
418         return;
419     }
420     input_item_SetArtURL( p_item, NULL );
421
422     if( input_item_IsArtFetched( p_item ) )
423     {
424         /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
425          * condition */
426         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
427         input_FindArtInCache( p_input, p_item );
428         free( psz_arturl );
429         return;
430     }
431
432     /* */
433     for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ )
434     {
435         if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
436                      &psz_arturl[strlen("attachment://")] ) )
437         {
438             p_attachment = p_input->p->attachment[i_idx];
439             break;
440         }
441     }
442     if( !p_attachment || p_attachment->i_data <= 0 )
443     {
444         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
445         goto end;
446     }
447
448     if( input_item_GetArtist( p_item ) )
449         psz_artist = ArtCacheCreateString( input_item_GetArtist( p_item ) );
450     if( input_item_GetAlbum( p_item ) )
451         psz_album = ArtCacheCreateString( input_item_GetAlbum( p_item ) );
452     if( input_item_GetTitle( p_item ) )
453         psz_title = ArtCacheCreateString( input_item_GetTitle( p_item ) );
454     else if( p_item->psz_name )
455         psz_title = ArtCacheCreateString( p_item->psz_name );
456
457     if( (!psz_artist || !psz_album ) && !psz_title )
458         goto end;
459
460     /* */
461     psz_type = strrchr( psz_arturl, '.' );
462     ArtCacheCreateName( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
463
464     /* Check if we already dumped it */
465     if( !utf8_stat( psz_filename+7, &s ) )
466         goto end;
467
468     ArtCacheCreatePath( p_input, psz_title, psz_artist, psz_album );
469
470     f = utf8_fopen( psz_filename+7, "w" );
471     if( f )
472     {
473         if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 )
474             msg_Err( p_input, "%s: %s", psz_filename, strerror( errno ) );
475         else
476             msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
477         fclose( f );
478     }
479
480 end:
481     if( psz_artist ) free( psz_artist );
482     if( psz_album ) free( psz_album );
483     if( psz_title ) free( psz_title );
484     if( psz_arturl ) free( psz_arturl );
485 }
486
487
488 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
489 {
490     uint32_t i_meta = 0;
491
492     if( !p_meta )
493         return 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 }