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