]> git.sesse.net Git - vlc/blob - src/input/meta.c
Added const wheen needed for stream_Peek (demuxer/access)
[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 #include <limits.h>                                             /* PATH_MAX */
35
36 #ifdef HAVE_SYS_STAT_H
37 #   include <sys/stat.h>
38 #endif
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"),
47     [vlc_meta_TrackNumber]      = N_("Track number"),
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_("Now Playing"),
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     VLC_UNUSED(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 or is a local file
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_title, *psz_artist, *psz_album;
123
124     psz_artist = input_item_GetArtist( p_item );
125     psz_album = input_item_GetAlbum( p_item );
126     psz_title = input_item_GetTitle( p_item );
127     if(!psz_title)
128         psz_title = input_item_GetName( p_item );
129
130     if( !psz_title && !psz_artist && !psz_album )
131         return VLC_EGENERIC;
132
133     free( psz_title );
134
135     /* If we already checked this album in this session, skip */
136     if( psz_artist && psz_album )
137     {
138         FOREACH_ARRAY( playlist_album_t album, p_playlist->p_fetcher->albums )
139             if( !strcmp( album.psz_artist, psz_artist ) &&
140                 !strcmp( album.psz_album, psz_album ) )
141             {
142                 msg_Dbg( p_playlist, " %s - %s has already been searched",
143                          psz_artist, psz_album );
144         /* TODO-fenrir if we cache art filename too, we can go faster */
145                 free( psz_artist );
146                 free( psz_album );
147                 if( album.b_found )
148                 {
149                     if( !strncmp( album.psz_arturl, "file://", 7 ) )
150                         input_item_SetArtURL( p_item, album.psz_arturl );
151                     else /* 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     input_FindArtInCache( p_playlist, p_item );
166
167     char *psz_arturl = input_item_GetArtURL( p_item );
168     if( !EMPTY_STR( psz_arturl ) )
169     {
170         free( psz_arturl );
171         return 0; /* Art is in cache, no need to go further */
172     }
173     free( psz_arturl );
174
175     PL_LOCK;
176     p_playlist->p_private = p_item;
177     psz_album = input_item_GetAlbum( p_item );
178     psz_artist = input_item_GetArtist( p_item );
179     psz_title = input_item_GetTitle( p_item );
180     if( !psz_title )
181         psz_title = input_item_GetName( p_item );
182
183     if( psz_album && psz_artist )
184     {
185         msg_Dbg( p_playlist, "searching art for %s - %s",
186              psz_artist, psz_album );
187     }
188     else
189     {
190         msg_Dbg( p_playlist, "searching art for %s",
191              psz_title );
192     }
193     free( psz_title );
194
195     p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
196
197     if( p_module )
198         i_ret = 1;
199     else
200         msg_Dbg( p_playlist, "unable to find art" );
201
202     /* Record this album */
203     if( psz_artist && psz_album )
204     {
205         playlist_album_t a;
206         a.psz_artist = psz_artist;
207         a.psz_album = psz_album;
208         a.psz_arturl = input_item_GetArtURL( p_item );
209         a.b_found = (i_ret == VLC_EGENERIC ? VLC_FALSE : VLC_TRUE );
210         ARRAY_APPEND( p_playlist->p_fetcher->albums, a );
211     }
212     else
213     {
214         free( psz_artist );
215         free( psz_album );
216     }
217
218     if( p_module )
219         module_Unneed( p_playlist, p_module );
220     p_playlist->p_private = NULL;
221     PL_UNLOCK;
222
223     return i_ret;
224 }
225
226 static void ArtCacheCreateDir( const char *psz_dir )
227 {
228     char newdir[strlen( psz_dir ) + 1];
229     strcpy( newdir, psz_dir );
230     char * psz_newdir = newdir;
231     char * psz = psz_newdir;
232
233     while( *psz )
234     {
235         while( *psz && *psz != DIR_SEP_CHAR) psz++;
236         if( !*psz ) break;
237         *psz = 0;
238         if( !EMPTY_STR( psz_newdir ) )
239             utf8_mkdir( psz_newdir, 0700 );
240         *psz = DIR_SEP_CHAR;
241         psz++;
242     }
243     utf8_mkdir( psz_dir, 0700 );
244 }
245
246 static char * ArtCacheGetSanitizedFileName( const char *psz )
247 {
248     char *dup = strdup(psz);
249     int i;
250
251     filename_sanitize( dup );
252
253     /* Doesn't create a filename with invalid characters
254      * TODO: several filesystems forbid several characters: list them all
255      */
256     for( i = 0; dup[i] != '\0'; i++ )
257     {
258         if( dup[i] == DIR_SEP_CHAR )
259             dup[i] = ' ';
260     }
261     return dup;
262 }
263
264 #define ArtCacheGetDirPath(a,b,c,d,e) __ArtCacheGetDirPath(VLC_OBJECT(a),b,c,d,e)
265 static void __ArtCacheGetDirPath( vlc_object_t *p_obj,
266                                   char *psz_dir,
267                                   const char *psz_title,
268                                   const char *psz_artist, const char *psz_album )
269 {
270     if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
271     {
272         char * psz_album_sanitized = ArtCacheGetSanitizedFileName( psz_album );
273         char * psz_artist_sanitized = ArtCacheGetSanitizedFileName( psz_artist );
274
275         snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
276                   "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
277                       p_obj->p_libvlc->psz_cachedir,
278                       psz_artist_sanitized, psz_album_sanitized );
279         free( psz_album_sanitized );
280         free( psz_artist_sanitized );
281     }
282     else
283     {
284         char * psz_title_sanitized = ArtCacheGetSanitizedFileName( psz_title );
285         snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
286                   "art" DIR_SEP "title" DIR_SEP "%s",
287                   p_obj->p_libvlc->psz_cachedir,
288                   psz_title_sanitized );
289         free( psz_title_sanitized );
290     }
291 }
292
293
294
295 #define ArtCacheGetFilePath(a,b,c,d,e,f) __ArtCacheGetFilePath(VLC_OBJECT(a),b,c,d,e,f)
296 static void __ArtCacheGetFilePath( vlc_object_t *p_obj,
297                                    char * psz_filename,
298                                    const char *psz_title,
299                                    const char *psz_artist, const char *psz_album,
300                                    const char *psz_extension )
301 {
302     char psz_dir[PATH_MAX+1];
303     char * psz_ext;
304     ArtCacheGetDirPath( p_obj, psz_dir, psz_title, psz_artist, psz_album );
305
306     if( psz_extension )
307     {
308         psz_ext = strndup( psz_extension, 6 );
309         filename_sanitize( psz_ext );
310     }
311     else psz_ext = strdup( "" );
312
313     snprintf( psz_filename, PATH_MAX, "file://%s" DIR_SEP "art%s",
314               psz_dir, psz_ext );
315
316     free( psz_ext );
317 }
318
319 static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
320 {
321     char *psz_artist;
322     char *psz_album;
323     char *psz_title;
324     char psz_dirpath[PATH_MAX+1];
325     char psz_filepath[PATH_MAX+1];
326     char * psz_filename;
327     DIR * p_dir;
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_dirpath, 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_dirpath );
351     if( !p_dir )
352         return VLC_EGENERIC;
353
354     while( (psz_filename = utf8_readdir( p_dir )) )
355     {
356         if( !strncmp( psz_filename, "art", 3 ) )
357         {
358             snprintf( psz_filepath, PATH_MAX, "file://%s" DIR_SEP "%s",
359                       psz_dirpath, psz_filename );
360             input_item_SetArtURL( p_item, psz_filepath );
361             free( psz_filename );
362             closedir( p_dir );
363             return VLC_SUCCESS;
364         }
365         free( psz_filename );
366     }
367
368     /* Not found */
369     closedir( p_dir );
370     return VLC_EGENERIC;
371 }
372
373 /**
374  * Download the art using the URL or an art downloaded
375  * This function should be called only if data is not already in cache
376  */
377 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
378 {
379     int i_status = VLC_EGENERIC;
380     stream_t *p_stream;
381     char psz_filename[PATH_MAX+1];
382     char *psz_artist = NULL;
383     char *psz_album = NULL;
384     char *psz_title = NULL;
385     char *psz_arturl;
386     char *psz_type;
387
388     psz_artist = input_item_GetArtist( p_item );
389     psz_album = input_item_GetAlbum( p_item );
390     psz_title = input_item_GetTitle( p_item );
391     if( !psz_title )
392         psz_title = input_item_GetName( p_item );
393
394     if( !psz_title && (!psz_artist || !psz_album) )
395     {
396         free( psz_title );
397         free( psz_album );
398         free( psz_artist );
399         return VLC_EGENERIC;
400     }
401
402     psz_arturl = input_item_GetArtURL( p_item );
403     assert( !EMPTY_STR( psz_arturl ) );
404
405     if( !strncmp( psz_arturl , "file://", 7 ) )
406     {
407         msg_Dbg( p_playlist, "Album art is local file, no need to cache" );
408         free( psz_arturl );
409         return VLC_SUCCESS;
410     }
411     else if( !strncmp( psz_arturl , "APIC", 4 ) )
412     {
413         msg_Warn( p_playlist, "APIC fetch not supported yet" );
414         free( psz_arturl );
415         return VLC_EGENERIC;
416     }
417
418     psz_type = strrchr( psz_arturl, '.' );
419     if( strlen( psz_type ) > 5 )
420         psz_type = NULL; /* remove extension if it's > to 4 characters */
421
422     /* Warning: psz_title, psz_artist, psz_album may change in ArtCache*() */
423
424     ArtCacheGetDirPath( p_playlist, psz_filename, psz_title, psz_artist,
425                         psz_album );
426     ArtCacheCreateDir( psz_filename );
427     ArtCacheGetFilePath( p_playlist, psz_filename, psz_title, psz_artist,
428                          psz_album, psz_type );
429
430     free( psz_artist );
431     free( psz_album );
432     free( psz_title );
433
434     p_stream = stream_UrlNew( p_playlist, psz_arturl );
435     if( p_stream )
436     {
437         uint8_t p_buffer[65536];
438         long int l_read;
439         FILE *p_file = utf8_fopen( psz_filename+7, "w" );
440         if( p_file == NULL ) {
441             msg_Err( p_playlist, "Unable write album art in %s",
442                      psz_filename + 7 );
443             free( psz_arturl );
444             return VLC_EGENERIC;
445         }
446         int err = 0;
447         while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
448         {
449             if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
450             {
451                 err = errno;
452                 break;
453             }
454         }
455         if( fclose( p_file ) && !err )
456             err = errno;
457         stream_Delete( p_stream );
458
459         if( err )
460         {
461             errno = err;
462             msg_Err( p_playlist, "%s: %m", psz_filename );
463         }
464         else
465             msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
466
467         input_item_SetArtURL( p_item, psz_filename );
468         i_status = VLC_SUCCESS;
469     }
470     free( psz_arturl );
471     return i_status;
472 }
473
474 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
475 {
476     input_item_t *p_item = p_input->p->input.p_item;
477     char *psz_arturl;
478     char *psz_artist = NULL;
479     char *psz_album = NULL;
480     char *psz_title = NULL;
481     char *psz_type = NULL;
482     char psz_filename[PATH_MAX+1];
483     FILE *f;
484     input_attachment_t *p_attachment;
485     struct stat s;
486     int i_idx;
487
488     /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH
489      * and then set it here to to be faster */
490
491     psz_arturl = input_item_GetArtURL( p_item );
492     if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
493     {
494         free( psz_arturl );
495         msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
496         return;
497     }
498     input_item_SetArtURL( p_item, NULL );
499
500     if( input_item_IsArtFetched( p_item ) )
501     {
502         /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
503          * condition */
504         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
505         input_FindArtInCache( p_input, p_item );
506         free( psz_arturl );
507         return;
508     }
509
510     /* */
511     for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ )
512     {
513         if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
514                      &psz_arturl[strlen("attachment://")] ) )
515         {
516             p_attachment = p_input->p->attachment[i_idx];
517             break;
518         }
519     }
520     if( !p_attachment || p_attachment->i_data <= 0 )
521     {
522         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
523         goto end;
524     }
525
526     psz_artist = input_item_GetArtist( p_item );
527     psz_album = input_item_GetAlbum( p_item );
528     psz_title = input_item_GetTitle( p_item );
529     if( !psz_title )
530         psz_title = input_item_GetName( p_item );
531
532     if( (!psz_artist || !psz_album ) && !psz_title )
533         goto end;
534
535     /* */
536     psz_type = strrchr( psz_arturl, '.' );
537
538     ArtCacheGetDirPath( p_input, psz_filename, psz_title, psz_artist, psz_album );
539     ArtCacheCreateDir( psz_filename );
540     ArtCacheGetFilePath( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
541
542     /* Check if we already dumped it */
543     if( !utf8_stat( psz_filename+7, &s ) )
544         goto end;
545
546     f = utf8_fopen( psz_filename+7, "w" );
547     if( f )
548     {
549         if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 )
550             msg_Err( p_input, "%s: %m", psz_filename );
551         else
552             msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
553         fclose( f );
554     }
555
556 end:
557     if( psz_artist ) free( psz_artist );
558     if( psz_album ) free( psz_album );
559     if( psz_title ) free( psz_title );
560     if( psz_arturl ) free( psz_arturl );
561 }
562
563
564 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
565 {
566     uint32_t i_meta = 0;
567
568     if( !p_meta )
569         return 0;
570
571 #define CHECK( a, b ) \
572     if( !EMPTY_STR( vlc_meta_Get( p_meta, vlc_meta_ ## a ) ) ) \
573         i_meta |= VLC_META_ENGINE_ ## b;
574
575     CHECK( Title, TITLE )
576     CHECK( Artist, ARTIST )
577     CHECK( Album, COLLECTION )
578 #if 0
579     /* As this is not used at the moment, don't uselessly check for it.
580      * Re-enable this when it is used */
581     CHECK( Genre, GENRE )
582     CHECK( Copyright, COPYRIGHT )
583     CHECK( Tracknum, SEQ_NUM )
584     CHECK( Description, DESCRIPTION )
585     CHECK( Rating, RATING )
586     CHECK( Date, DATE )
587     CHECK( URL, URL )
588     CHECK( Language, LANGUAGE )
589 #endif
590     CHECK( ArtworkURL, ART_URL )
591
592     return i_meta;
593 }