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