]> git.sesse.net Git - vlc/blob - src/input/meta.c
Simplify (no functionnal changes).
[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 #define input_FindArtInCache(a,b) __input_FindArtInCache(VLC_OBJECT(a),b)
41 static int __input_FindArtInCache( vlc_object_t *, input_item_t *p_item );
42
43 vlc_bool_t input_MetaSatisfied( playlist_t *p_playlist, input_item_t *p_item,
44                                 uint32_t *pi_mandatory, uint32_t *pi_optional )
45 {
46     (void)p_playlist;
47     *pi_mandatory = VLC_META_ENGINE_TITLE | VLC_META_ENGINE_ARTIST;
48     assert( p_item->p_meta );
49
50     uint32_t i_meta = input_CurrentMetaFlags( p_item->p_meta );
51     *pi_mandatory &= ~i_meta;
52     *pi_optional = 0; /// Todo
53     return *pi_mandatory ? VLC_FALSE:VLC_TRUE;
54 }
55
56 int input_MetaFetch( playlist_t *p_playlist, input_item_t *p_item )
57 {
58     struct meta_engine_t *p_me;
59     uint32_t i_mandatory, i_optional;
60
61     assert( p_item->p_meta );
62
63     input_MetaSatisfied( p_playlist, p_item, &i_mandatory, &i_optional );
64     // Meta shouldn't magically appear
65     assert( i_mandatory );
66
67     /* FIXME: object creation is overkill, use p_private */
68     p_me = vlc_object_create( p_playlist, VLC_OBJECT_META_ENGINE );
69     p_me->i_flags |= OBJECT_FLAGS_NOINTERACT;
70     p_me->i_flags |= OBJECT_FLAGS_QUIET;
71     p_me->i_mandatory = i_mandatory;
72     p_me->i_optional = i_optional;
73
74     p_me->p_item = p_item;
75     p_me->p_module = module_Need( p_me, "meta fetcher", 0, VLC_FALSE );
76     if( !p_me->p_module )
77     {
78         vlc_object_destroy( p_me );
79         return VLC_EGENERIC;
80     }
81     module_Unneed( p_me, p_me->p_module );
82     vlc_object_destroy( p_me );
83     return VLC_SUCCESS;
84 }
85
86 /* Return codes:
87  *   0 : Art is in cache
88  *   1 : Art found, need to download
89  *  -X : Error/not found
90  */
91 int input_ArtFind( playlist_t *p_playlist, input_item_t *p_item )
92 {
93     int i_ret = VLC_EGENERIC;
94     module_t *p_module;
95     if( !p_item->p_meta || !p_item->p_meta->psz_album ||
96                            !p_item->p_meta->psz_artist )
97         return VLC_EGENERIC;
98
99     /* If we already checked this album in this session, skip */
100     FOREACH_ARRAY( playlist_album_t album, p_playlist->p_fetcher->albums )
101         if( !strcmp( album.psz_artist, p_item->p_meta->psz_artist ) &&
102             !strcmp( album.psz_album, p_item->p_meta->psz_album ) )
103         {
104             msg_Dbg( p_playlist, " %s - %s has already been searched",
105                      p_item->p_meta->psz_artist,  p_item->p_meta->psz_album );
106     /* TODO-fenrir if we cache art filename too, we can go faster */
107             if( album.b_found )
108             {
109                 /* Actually get URL from cache */
110                 input_FindArtInCache( p_playlist, p_item );
111                 return 0;
112             }
113             else
114             {
115                 return VLC_EGENERIC;
116             }
117         }
118     FOREACH_END();
119
120     input_FindArtInCache( p_playlist, p_item );
121     if( !EMPTY_STR( p_item->p_meta->psz_arturl ) )
122         return 0;
123
124     PL_LOCK;
125     p_playlist->p_private = p_item;
126     msg_Dbg( p_playlist, "searching art for %s - %s",
127              p_item->p_meta->psz_artist,  p_item->p_meta->psz_album );
128     p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
129
130     if( p_module )
131         i_ret = 1;
132     else
133         msg_Dbg( p_playlist, "unable to find art" );
134
135     /* Record this album */
136     playlist_album_t a;
137     a.psz_artist = strdup( p_item->p_meta->psz_artist );
138     a.psz_album = strdup( p_item->p_meta->psz_album );
139     a.b_found = (i_ret == VLC_EGENERIC ? VLC_FALSE : VLC_TRUE );
140     ARRAY_APPEND( p_playlist->p_fetcher->albums, a );
141
142     if( p_module )
143         module_Unneed( p_playlist, p_module );
144     p_playlist->p_private = NULL;
145     PL_UNLOCK;
146
147     return i_ret;
148 }
149
150 #ifndef MAX_PATH
151 #   define MAX_PATH 250
152 #endif
153 #define ArtCacheCreateName( a,b,c,d,e) __ArtCacheCreateName(VLC_OBJECT(a),b,c,d,e)
154 static void __ArtCacheCreateName( vlc_object_t *p_obj,
155                                   char psz_filename[MAX_PATH+1],
156                                   const char *psz_artist, const char *psz_album,
157                                   const char *psz_extension )
158 {
159     snprintf( psz_filename, MAX_PATH,
160               "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
161               DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
162               p_obj->p_libvlc->psz_homedir,
163               psz_artist, psz_album, psz_extension ? psz_extension : "" );
164 }
165 #define ArtCacheCreatePath(a,b,c) __ArtCacheCreatePath(VLC_OBJECT(a),b,c)
166 static void __ArtCacheCreatePath( vlc_object_t *p_obj,
167                                   const char *psz_artist, const char *psz_album )
168 {
169     char psz_dir[MAX_PATH+1];
170     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
171               p_obj->p_libvlc->psz_homedir );
172     utf8_mkdir( psz_dir );
173     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
174               p_obj->p_libvlc->psz_homedir );
175     utf8_mkdir( psz_dir );
176     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
177               "art" DIR_SEP "%s",
178                  p_obj->p_libvlc->psz_homedir, psz_artist );
179     utf8_mkdir( psz_dir );
180     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
181               "art" DIR_SEP "%s" DIR_SEP "%s",
182                       p_obj->p_libvlc->psz_homedir,
183                       psz_artist, psz_album );
184     utf8_mkdir( psz_dir );
185 }
186 static char *ArtCacheCreateString( const char *psz )
187 {
188     char *dup = strdup(psz);
189     int i;
190
191     /* Doesn't create a filename with invalid characters
192      * TODO: several filesystems forbid several characters: list them all
193      */
194     for( i = 0; dup[i] != '\0'; i++ )
195     {
196         if( dup[i] == '/' )
197             dup[i] = ' ';
198     }
199     return dup;
200 }
201
202 static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
203 {
204     char *psz_artist;
205     char *psz_album;
206     char psz_filename[MAX_PATH+1];
207     int i;
208     struct stat a;
209     const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
210
211     if( !p_item->p_meta ) return VLC_EGENERIC;
212
213     psz_artist = p_item->p_meta->psz_artist;
214     psz_album = p_item->p_meta->psz_album;
215
216     for( i = 0; i < 5; i++ )
217     {
218         ArtCacheCreateName( p_obj, psz_filename, psz_artist, psz_album, ppsz_type[i] );
219
220         /* Check if file exists */
221         if( utf8_stat( psz_filename+7, &a ) == 0 )
222         {
223             vlc_meta_SetArtURL( p_item->p_meta, psz_filename );
224             return VLC_SUCCESS;
225         }
226     }
227     return VLC_EGENERIC;
228 }
229
230 /**
231  * Download the art using the URL or an art downloaded
232  * This function should be called only if data is not already in cache
233  */
234 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
235 {
236     int i_status = VLC_EGENERIC;
237     stream_t *p_stream;
238     char psz_filename[MAX_PATH+1];
239     char *psz_artist = NULL;
240     char *psz_album = NULL;
241     char *psz_type;
242     if( p_item->p_meta->psz_artist )
243         psz_artist = ArtCacheCreateString( p_item->p_meta->psz_artist );
244     if( p_item->p_meta->psz_album )
245         psz_album = ArtCacheCreateString( p_item->p_meta->psz_album );
246
247     assert( p_item->p_meta && !EMPTY_STR(p_item->p_meta->psz_arturl) );
248
249     /* FIXME: use an alternate saving filename scheme if we don't have
250      * the artist or album name */
251     if( !psz_artist || !psz_album )
252     {
253         if( psz_artist ) free( psz_artist );
254         if( psz_album ) free( psz_album );
255         return VLC_EGENERIC;
256     }
257
258     psz_type = strrchr( p_item->p_meta->psz_arturl, '.' );
259
260     /* */
261     ArtCacheCreateName( p_playlist, psz_filename, psz_artist, psz_album, psz_type );
262
263     /* */
264     ArtCacheCreatePath( p_playlist, psz_artist, psz_album );
265
266     /* */
267     free( psz_artist );
268     free( psz_album );
269
270     if( !strncmp( p_item->p_meta->psz_arturl , "APIC", 4 ) )
271     {
272         msg_Warn( p_playlist, "APIC fetch not supported yet" );
273         return VLC_EGENERIC;
274     }
275
276     p_stream = stream_UrlNew( p_playlist, p_item->p_meta->psz_arturl );
277     if( p_stream )
278     {
279         uint8_t p_buffer[65536];
280         long int l_read;
281         FILE *p_file = utf8_fopen( psz_filename+7, "w" );
282         int err = 0;
283         while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
284         {
285             if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
286             {
287                 err = errno;
288                 break;
289             }
290         }
291         if( fclose( p_file ) && !err )
292             err = errno;
293         stream_Delete( p_stream );
294         free( p_item->p_meta->psz_arturl );
295
296         if( err )
297             msg_Err( p_playlist, "%s: %s", psz_filename, strerror( err ) );
298         else
299             msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
300
301         p_item->p_meta->psz_arturl = strdup( psz_filename );
302         i_status = VLC_SUCCESS;
303     }
304     return i_status;
305 }
306
307 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
308 {
309     input_item_t *p_item = p_input->p->input.p_item;
310     char *psz_arturl;
311     char *psz_artist = NULL;
312     char *psz_album = NULL;
313     char *psz_type = NULL;
314     char psz_filename[MAX_PATH+1];
315     FILE *f;
316     input_attachment_t *p_attachment;
317     struct stat s;
318     int i_idx;
319
320     /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH
321      * and then set it here to to be faster */
322
323     assert( p_item->p_meta );
324     psz_arturl = p_item->p_meta->psz_arturl;
325     if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
326     {
327         msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
328         return;
329     }
330     p_item->p_meta->psz_arturl = NULL;
331
332     if( p_item->p_meta->i_status & ITEM_ART_FETCHED )
333     {
334         /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
335          * condition */
336         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
337         input_FindArtInCache( p_input, p_item );
338         free( psz_arturl );
339         return;
340     }
341
342     /* */
343     for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ )
344     {
345         if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
346                      &psz_arturl[strlen("attachment://")] ) )
347         {
348             p_attachment = p_input->p->attachment[i_idx];
349             break;
350         }
351     }
352     if( !p_attachment || p_attachment->i_data <= 0 )
353     {
354         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
355         goto end;
356     }
357
358     if( p_item->p_meta->psz_artist )
359         psz_artist = ArtCacheCreateString( p_item->p_meta->psz_artist );
360     if( p_item->p_meta->psz_album )
361         psz_album = ArtCacheCreateString( p_item->p_meta->psz_album );
362
363     if( !psz_artist || !psz_album )
364         goto end;
365
366     /* */
367     psz_type = strrchr( psz_arturl, '.' );
368     ArtCacheCreateName( p_input, psz_filename, psz_artist, psz_album, psz_type );
369
370     /* Check if we already dumped it */
371     if( !utf8_stat( psz_filename+7, &s ) )
372         goto end;
373
374     ArtCacheCreatePath( p_input, psz_artist, psz_album );
375
376     f = utf8_fopen( psz_filename+7, "w" );
377     if( f )
378     {
379         if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 )
380             msg_Err( p_input, "%s: %s", psz_filename, strerror( errno ) );
381         else
382             msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
383         fclose( f );
384     }
385
386 end:
387     if( psz_artist ) free( psz_artist );
388     if( psz_album ) free( psz_album );
389     if( psz_arturl ) free( psz_arturl );
390 }
391
392
393 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
394 {
395     uint32_t i_meta = 0;
396
397 #define CHECK( a, b ) \
398     if( p_meta->psz_ ## a && *p_meta->psz_ ## a ) \
399         i_meta |= VLC_META_ENGINE_ ## b;
400
401     CHECK( title, TITLE )
402     CHECK( artist, ARTIST )
403     CHECK( album, COLLECTION )
404 #if 0
405     /* As this is not used at the moment, don't uselessly check for it.
406      * Re-enable this when it is used */
407     CHECK( genre, GENRE )
408     CHECK( copyright, COPYRIGHT )
409     CHECK( tracknum, SEQ_NUM )
410     CHECK( description, DESCRIPTION )
411     CHECK( rating, RATING )
412     CHECK( date, DATE )
413     CHECK( url, URL )
414     CHECK( language, LANGUAGE )
415 #endif
416     CHECK( arturl, ART_URL )
417
418     return i_meta;
419 }