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