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