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