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