]> git.sesse.net Git - vlc/blob - src/playlist/fetcher.c
Assume UTF-8 operating system by default
[vlc] / src / playlist / fetcher.c
1 /*****************************************************************************
2  * fetcher.c: Art fetcher thread.
3  *****************************************************************************
4  * Copyright © 1999-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <assert.h>
29
30 #include <vlc_common.h>
31 #include <vlc_playlist.h>
32 #include <vlc_stream.h>
33 #include <limits.h>
34 #include <vlc_art_finder.h>
35 #include <vlc_memory.h>
36 #include <vlc_demux.h>
37
38 #include "art.h"
39 #include "fetcher.h"
40 #include "playlist_internal.h"
41
42 /*****************************************************************************
43  * Structures/definitions
44  *****************************************************************************/
45 struct playlist_fetcher_t
46 {
47     playlist_t      *p_playlist;
48
49     vlc_mutex_t     lock;
50     vlc_cond_t      wait;
51     bool            b_live;
52     int             i_art_policy;
53     int             i_waiting;
54     input_item_t    **pp_waiting;
55
56     DECL_ARRAY(playlist_album_t) albums;
57 };
58
59 static void *Thread( void * );
60
61
62 /*****************************************************************************
63  * Public functions
64  *****************************************************************************/
65 playlist_fetcher_t *playlist_fetcher_New( playlist_t *p_playlist )
66 {
67     playlist_fetcher_t *p_fetcher = malloc( sizeof(*p_fetcher) );
68     if( !p_fetcher )
69         return NULL;
70
71     p_fetcher->p_playlist = p_playlist;
72     vlc_mutex_init( &p_fetcher->lock );
73     vlc_cond_init( &p_fetcher->wait );
74     p_fetcher->b_live = false;
75     p_fetcher->i_waiting = 0;
76     p_fetcher->pp_waiting = NULL;
77     p_fetcher->i_art_policy = var_GetInteger( p_playlist, "album-art" );
78     ARRAY_INIT( p_fetcher->albums );
79
80     return p_fetcher;
81 }
82
83 void playlist_fetcher_Push( playlist_fetcher_t *p_fetcher,
84                             input_item_t *p_item )
85 {
86     vlc_gc_incref( p_item );
87
88     vlc_mutex_lock( &p_fetcher->lock );
89     INSERT_ELEM( p_fetcher->pp_waiting, p_fetcher->i_waiting,
90                  p_fetcher->i_waiting, p_item );
91     if( !p_fetcher->b_live )
92     {
93         vlc_thread_t th;
94
95         if( vlc_clone( &th, Thread, p_fetcher, VLC_THREAD_PRIORITY_LOW ) )
96             msg_Err( p_fetcher->p_playlist,
97                      "cannot spawn secondary preparse thread" );
98         else
99         {
100             vlc_detach( th );
101             p_fetcher->b_live = true;
102         }
103     }
104     vlc_mutex_unlock( &p_fetcher->lock );
105 }
106
107 void playlist_fetcher_Delete( playlist_fetcher_t *p_fetcher )
108 {
109     vlc_mutex_lock( &p_fetcher->lock );
110     /* Remove any left-over item, the fetcher will exit */
111     while( p_fetcher->i_waiting > 0 )
112     {
113         vlc_gc_decref( p_fetcher->pp_waiting[0] );
114         REMOVE_ELEM( p_fetcher->pp_waiting, p_fetcher->i_waiting, 0 );
115     }
116
117     while( p_fetcher->b_live )
118         vlc_cond_wait( &p_fetcher->wait, &p_fetcher->lock );
119     vlc_mutex_unlock( &p_fetcher->lock );
120
121     vlc_cond_destroy( &p_fetcher->wait );
122     vlc_mutex_destroy( &p_fetcher->lock );
123     free( p_fetcher );
124 }
125
126
127 /*****************************************************************************
128  * Privates functions
129  *****************************************************************************/
130 /**
131  * This function locates the art associated to an input item.
132  * Return codes:
133  *   0 : Art is in cache or is a local file
134  *   1 : Art found, need to download
135  *  -X : Error/not found
136  */
137 static int FindArt( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
138 {
139     int i_ret;
140
141     char *psz_artist = input_item_GetArtist( p_item );
142     char *psz_album = input_item_GetAlbum( p_item );
143     char *psz_title = input_item_GetTitle( p_item );
144     if( !psz_title )
145         psz_title = input_item_GetName( p_item );
146
147     if( !psz_title && !psz_artist && !psz_album )
148         return VLC_EGENERIC;
149
150     free( psz_title );
151
152     /* If we already checked this album in this session, skip */
153     if( psz_artist && psz_album )
154     {
155         FOREACH_ARRAY( playlist_album_t album, p_fetcher->albums )
156             if( !strcmp( album.psz_artist, psz_artist ) &&
157                 !strcmp( album.psz_album, psz_album ) )
158             {
159                 msg_Dbg( p_fetcher->p_playlist,
160                          " %s - %s has already been searched",
161                          psz_artist, psz_album );
162                 /* TODO-fenrir if we cache art filename too, we can go faster */
163                 free( psz_artist );
164                 free( psz_album );
165                 if( album.b_found )
166                 {
167                     if( !strncmp( album.psz_arturl, "file://", 7 ) )
168                         input_item_SetArtURL( p_item, album.psz_arturl );
169                     else /* Actually get URL from cache */
170                         playlist_FindArtInCache( p_item );
171                     return 0;
172                 }
173                 else
174                 {
175                     return VLC_EGENERIC;
176                 }
177             }
178         FOREACH_END();
179     }
180     free( psz_artist );
181     free( psz_album );
182
183     playlist_FindArtInCache( p_item );
184
185     char *psz_arturl = input_item_GetArtURL( p_item );
186     if( psz_arturl )
187     {
188         /* We already have an URL */
189         if( !strncmp( psz_arturl, "file://", strlen( "file://" ) ) )
190         {
191             free( psz_arturl );
192             return 0; /* Art is in cache, no need to go further */
193         }
194
195         free( psz_arturl );
196
197         /* Art need to be put in cache */
198         return 1;
199     }
200
201     /* */
202     psz_album = input_item_GetAlbum( p_item );
203     psz_artist = input_item_GetArtist( p_item );
204     if( psz_album && psz_artist )
205     {
206         msg_Dbg( p_fetcher->p_playlist, "searching art for %s - %s",
207              psz_artist, psz_album );
208     }
209     else
210     {
211         psz_title = input_item_GetTitle( p_item );
212         if( !psz_title )
213             psz_title = input_item_GetName( p_item );
214
215         msg_Dbg( p_fetcher->p_playlist, "searching art for %s", psz_title );
216         free( psz_title );
217     }
218
219     /* Fetch the art url */
220     i_ret = VLC_EGENERIC;
221
222     vlc_object_t *p_parent = VLC_OBJECT(p_fetcher->p_playlist);
223     art_finder_t *p_finder =
224         vlc_custom_create( p_parent, sizeof( *p_finder ), VLC_OBJECT_GENERIC,
225                            "art finder" );
226     if( p_finder != NULL)
227     {
228         module_t *p_module;
229
230         vlc_object_attach( p_finder, p_parent );
231         p_finder->p_item = p_item;
232
233         p_module = module_need( p_finder, "art finder", NULL, false );
234         if( p_module )
235         {
236             module_unneed( p_finder, p_module );
237             /* Try immediately if found in cache by download URL */
238             if( !playlist_FindArtInCache( p_item ) )
239                 i_ret = 0;
240             else
241                 i_ret = 1;
242         }
243         vlc_object_release( p_finder );
244     }
245
246     /* Record this album */
247     if( psz_artist && psz_album )
248     {
249         playlist_album_t a;
250         a.psz_artist = psz_artist;
251         a.psz_album = psz_album;
252         a.psz_arturl = input_item_GetArtURL( p_item );
253         a.b_found = (i_ret == VLC_EGENERIC ? false : true );
254         ARRAY_APPEND( p_fetcher->albums, a );
255     }
256     else
257     {
258         free( psz_artist );
259         free( psz_album );
260     }
261
262     return i_ret;
263 }
264
265 /**
266  * Download the art using the URL or an art downloaded
267  * This function should be called only if data is not already in cache
268  */
269 static int DownloadArt( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
270 {
271     char *psz_arturl = input_item_GetArtURL( p_item );
272     assert( *psz_arturl );
273
274     if( !strncmp( psz_arturl , "file://", 7 ) )
275     {
276         msg_Dbg( p_fetcher->p_playlist,
277                  "Album art is local file, no need to cache" );
278         free( psz_arturl );
279         return VLC_SUCCESS;
280     }
281
282     if( !strncmp( psz_arturl , "APIC", 4 ) )
283     {
284         msg_Warn( p_fetcher->p_playlist, "APIC fetch not supported yet" );
285         goto error;
286     }
287
288     stream_t *p_stream = stream_UrlNew( p_fetcher->p_playlist, psz_arturl );
289     if( !p_stream )
290         goto error;
291
292     uint8_t *p_data = NULL;
293     int i_data = 0;
294     for( ;; )
295     {
296         int i_read = 65536;
297
298         if( i_data >= INT_MAX - i_read )
299             break;
300
301         p_data = realloc_or_free( p_data, i_data + i_read );
302         if( !p_data )
303             break;
304
305         i_read = stream_Read( p_stream, &p_data[i_data], i_read );
306         if( i_read <= 0 )
307             break;
308
309         i_data += i_read;
310     }
311     stream_Delete( p_stream );
312
313     if( p_data && i_data > 0 )
314     {
315         char *psz_type = strrchr( psz_arturl, '.' );
316         if( psz_type && strlen( psz_type ) > 5 )
317             psz_type = NULL; /* remove extension if it's > to 4 characters */
318
319         playlist_SaveArt( p_fetcher->p_playlist, p_item, p_data, i_data, psz_type );
320     }
321
322     free( p_data );
323
324     free( psz_arturl );
325     return VLC_SUCCESS;
326
327 error:
328     free( psz_arturl );
329     return VLC_EGENERIC;
330 }
331
332 /**
333  * FetchMeta, run the "meta fetcher". They are going to do network
334  * connections, and gather information upon the playing media.
335  * (even artwork).
336  */
337 static void FetchMeta( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
338 {
339     demux_meta_t *p_demux_meta = vlc_custom_create(p_fetcher->p_playlist,
340                                        sizeof(*p_demux_meta),
341                                        VLC_OBJECT_GENERIC, "demux meta" );
342     if( !p_demux_meta )
343         return;
344
345     vlc_object_attach( p_demux_meta, p_fetcher->p_playlist );
346     p_demux_meta->p_demux = NULL;
347     p_demux_meta->p_item = p_item;
348
349     module_t *p_meta_fetcher = module_need( p_demux_meta, "meta fetcher", NULL, false );
350     if( p_meta_fetcher )
351         module_unneed( p_demux_meta, p_meta_fetcher );
352     vlc_object_release( p_demux_meta );
353 }
354
355 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
356                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
357 {
358     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
359     vlc_cond_t *p_cond = p_data;
360
361     if( newval.i_int == INPUT_EVENT_ITEM_META ||
362         newval.i_int == INPUT_EVENT_DEAD )
363         vlc_cond_signal( p_cond );
364
365     return VLC_SUCCESS;
366 }
367
368
369 /* Check if it is not yet preparsed and if so wait for it
370  * (at most 0.5s)
371  * (This can happen if we fetch art on play)
372  * FIXME this doesn't work if we need to fetch meta before art...
373  */
374 static void WaitPreparsed( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
375 {
376     if( input_item_IsPreparsed( p_item ) )
377         return;
378
379     input_thread_t *p_input = playlist_CurrentInput( p_fetcher->p_playlist );
380     if( !p_input )
381         return;
382
383     if( input_GetItem( p_input ) != p_item )
384         goto exit;
385
386     vlc_cond_t cond;
387     vlc_cond_init( &cond );
388     var_AddCallback( p_input, "intf-event", InputEvent, &cond );
389
390     const mtime_t i_deadline = mdate() + 500*1000;
391     bool b_timeout = false;
392
393     while( !p_input->b_eof && !p_input->b_error
394         && !input_item_IsPreparsed( p_item ) && !b_timeout )
395     {
396         /* A bit weird, but input_item_IsPreparsed holds the protected value */
397         /* FIXME: locking looks wrong here */
398         vlc_mutex_lock( &p_fetcher->lock );
399         if( vlc_cond_timedwait( &cond, &p_fetcher->lock, i_deadline ) )
400             b_timeout = true;
401         vlc_mutex_unlock( &p_fetcher->lock );
402     }
403
404     var_DelCallback( p_input, "intf-event", InputEvent, &cond );
405     vlc_cond_destroy( &cond );
406
407 exit:
408     vlc_object_release( p_input );
409 }
410
411 static void *Thread( void *p_data )
412 {
413     playlist_fetcher_t *p_fetcher = p_data;
414     playlist_t *p_playlist = p_fetcher->p_playlist;
415
416     for( ;; )
417     {
418         input_item_t *p_item = NULL;
419
420         vlc_mutex_lock( &p_fetcher->lock );
421         if( p_fetcher->i_waiting != 0 )
422         {
423             p_item = p_fetcher->pp_waiting[0];
424             REMOVE_ELEM( p_fetcher->pp_waiting, p_fetcher->i_waiting, 0 );
425         }
426         else
427         {
428             p_fetcher->b_live = false;
429             vlc_cond_signal( &p_fetcher->wait );
430         }
431         vlc_mutex_unlock( &p_fetcher->lock );
432
433         if( !p_item )
434             break;
435
436         /* */
437
438         /* Wait that the input item is preparsed if it is being played */
439         WaitPreparsed( p_fetcher, p_item );
440
441         /* Triggers "meta fetcher", eventually fetch meta on the network.
442          * They are identical to "meta reader" expect that may actually
443          * takes time. That's why they are running here.
444          * The result of this fetch is not cached. */
445         FetchMeta( p_fetcher, p_item );
446
447         /* Find art, and download it if needed */
448         int i_ret = FindArt( p_fetcher, p_item );
449         if( i_ret == 1 )
450             i_ret = DownloadArt( p_fetcher, p_item );
451
452         /* */
453         char *psz_name = input_item_GetName( p_item );
454         if( !i_ret ) /* Art is now in cache */
455         {
456             PL_DEBUG( "found art for %s in cache", psz_name );
457             input_item_SetArtFetched( p_item, true );
458             var_SetAddress( p_playlist, "item-change", p_item );
459         }
460         else
461         {
462             PL_DEBUG( "art not found for %s", psz_name );
463             input_item_SetArtNotFound( p_item, true );
464         }
465         free( psz_name );
466         vlc_gc_decref( p_item );
467     }
468     return NULL;
469 }