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