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