]> git.sesse.net Git - vlc/blob - src/playlist/fetcher.c
Use var_Inherit* instead of var_CreateGet*.
[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 #include <vlc_memory.h>
36 #include <vlc_demux.h>
37 #include <vlc_modules.h>
38
39 #include "art.h"
40 #include "fetcher.h"
41 #include "playlist_internal.h"
42
43 /*****************************************************************************
44  * Structures/definitions
45  *****************************************************************************/
46 struct playlist_fetcher_t
47 {
48     playlist_t      *p_playlist;
49
50     vlc_mutex_t     lock;
51     vlc_cond_t      wait;
52     bool            b_live;
53     int             i_art_policy;
54     int             i_waiting;
55     input_item_t    **pp_waiting;
56
57     DECL_ARRAY(playlist_album_t) albums;
58 };
59
60 static void *Thread( void * );
61
62
63 /*****************************************************************************
64  * Public functions
65  *****************************************************************************/
66 playlist_fetcher_t *playlist_fetcher_New( playlist_t *p_playlist )
67 {
68     playlist_fetcher_t *p_fetcher = malloc( sizeof(*p_fetcher) );
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->b_live = false;
76     p_fetcher->i_waiting = 0;
77     p_fetcher->pp_waiting = NULL;
78     p_fetcher->i_art_policy = var_GetInteger( p_playlist, "album-art" );
79     ARRAY_INIT( p_fetcher->albums );
80
81     return p_fetcher;
82 }
83
84 void playlist_fetcher_Push( playlist_fetcher_t *p_fetcher,
85                             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->p_playlist,
98                      "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     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     free( 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
142     char *psz_artist = input_item_GetArtist( p_item );
143     char *psz_album = input_item_GetAlbum( p_item );
144     char *psz_title = input_item_GetTitle( p_item );
145     if( !psz_title )
146         psz_title = input_item_GetName( p_item );
147
148     if( !psz_title && !psz_artist && !psz_album )
149         return VLC_EGENERIC;
150
151     free( psz_title );
152
153     /* If we already checked this album in this session, skip */
154     if( psz_artist && psz_album )
155     {
156         FOREACH_ARRAY( playlist_album_t album, p_fetcher->albums )
157             if( !strcmp( album.psz_artist, psz_artist ) &&
158                 !strcmp( album.psz_album, psz_album ) )
159             {
160                 msg_Dbg( p_fetcher->p_playlist,
161                          " %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->p_playlist, "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->p_playlist, "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_finder, "art finder", NULL, false );
235         if( p_module )
236         {
237             module_unneed( p_finder, p_module );
238             /* Try immediately if found in cache by download URL */
239             if( !playlist_FindArtInCache( p_item ) )
240                 i_ret = 0;
241             else
242                 i_ret = 1;
243         }
244         vlc_object_release( p_finder );
245     }
246
247     /* Record this album */
248     if( psz_artist && psz_album )
249     {
250         playlist_album_t a;
251         a.psz_artist = psz_artist;
252         a.psz_album = psz_album;
253         a.psz_arturl = input_item_GetArtURL( p_item );
254         a.b_found = (i_ret == VLC_EGENERIC ? false : true );
255         ARRAY_APPEND( p_fetcher->albums, a );
256     }
257     else
258     {
259         free( psz_artist );
260         free( psz_album );
261     }
262
263     return i_ret;
264 }
265
266 /**
267  * Download the art using the URL or an art downloaded
268  * This function should be called only if data is not already in cache
269  */
270 static int DownloadArt( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
271 {
272     char *psz_arturl = input_item_GetArtURL( p_item );
273     assert( *psz_arturl );
274
275     if( !strncmp( psz_arturl , "file://", 7 ) )
276     {
277         msg_Dbg( p_fetcher->p_playlist,
278                  "Album art is local file, no need to cache" );
279         free( psz_arturl );
280         return VLC_SUCCESS;
281     }
282
283     if( !strncmp( psz_arturl , "APIC", 4 ) )
284     {
285         msg_Warn( p_fetcher->p_playlist, "APIC fetch not supported yet" );
286         goto error;
287     }
288
289     stream_t *p_stream = stream_UrlNew( p_fetcher->p_playlist, psz_arturl );
290     if( !p_stream )
291         goto error;
292
293     uint8_t *p_data = NULL;
294     int i_data = 0;
295     for( ;; )
296     {
297         int i_read = 65536;
298
299         if( i_data >= INT_MAX - i_read )
300             break;
301
302         p_data = realloc_or_free( p_data, i_data + i_read );
303         if( !p_data )
304             break;
305
306         i_read = stream_Read( p_stream, &p_data[i_data], i_read );
307         if( i_read <= 0 )
308             break;
309
310         i_data += i_read;
311     }
312     stream_Delete( p_stream );
313
314     if( p_data && i_data > 0 )
315     {
316         char *psz_type = strrchr( psz_arturl, '.' );
317         if( psz_type && strlen( psz_type ) > 5 )
318             psz_type = NULL; /* remove extension if it's > to 4 characters */
319
320         playlist_SaveArt( p_fetcher->p_playlist, p_item, p_data, i_data, psz_type );
321     }
322
323     free( p_data );
324
325     free( psz_arturl );
326     return VLC_SUCCESS;
327
328 error:
329     free( psz_arturl );
330     return VLC_EGENERIC;
331 }
332
333 /**
334  * FetchMeta, run the "meta fetcher". They are going to do network
335  * connections, and gather information upon the playing media.
336  * (even artwork).
337  */
338 static void FetchMeta( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
339 {
340     demux_meta_t *p_demux_meta = vlc_custom_create(p_fetcher->p_playlist,
341                                        sizeof(*p_demux_meta),
342                                        VLC_OBJECT_GENERIC, "demux meta" );
343     if( !p_demux_meta )
344         return;
345
346     vlc_object_attach( p_demux_meta, p_fetcher->p_playlist );
347     p_demux_meta->p_demux = NULL;
348     p_demux_meta->p_item = p_item;
349
350     module_t *p_meta_fetcher = module_need( p_demux_meta, "meta fetcher", NULL, false );
351     if( p_meta_fetcher )
352         module_unneed( p_demux_meta, p_meta_fetcher );
353     vlc_object_release( p_demux_meta );
354 }
355
356 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
357                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
358 {
359     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
360     vlc_cond_t *p_cond = p_data;
361
362     if( newval.i_int == INPUT_EVENT_ITEM_META ||
363         newval.i_int == INPUT_EVENT_DEAD )
364         vlc_cond_signal( p_cond );
365
366     return VLC_SUCCESS;
367 }
368
369
370 /* Check if it is not yet preparsed and if so wait for it
371  * (at most 0.5s)
372  * (This can happen if we fetch art on play)
373  * FIXME this doesn't work if we need to fetch meta before art...
374  */
375 static void WaitPreparsed( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
376 {
377     if( input_item_IsPreparsed( p_item ) )
378         return;
379
380     input_thread_t *p_input = playlist_CurrentInput( p_fetcher->p_playlist );
381     if( !p_input )
382         return;
383
384     if( input_GetItem( p_input ) != p_item )
385         goto exit;
386
387     vlc_cond_t cond;
388     vlc_cond_init( &cond );
389     var_AddCallback( p_input, "intf-event", InputEvent, &cond );
390
391     const mtime_t i_deadline = mdate() + 500*1000;
392     bool b_timeout = false;
393
394     while( !p_input->b_eof && !p_input->b_error
395         && !input_item_IsPreparsed( p_item ) && !b_timeout )
396     {
397         /* A bit weird, but input_item_IsPreparsed holds the protected value */
398         /* FIXME: locking looks wrong here */
399         vlc_mutex_lock( &p_fetcher->lock );
400         if( vlc_cond_timedwait( &cond, &p_fetcher->lock, i_deadline ) )
401             b_timeout = true;
402         vlc_mutex_unlock( &p_fetcher->lock );
403     }
404
405     var_DelCallback( p_input, "intf-event", InputEvent, &cond );
406     vlc_cond_destroy( &cond );
407
408 exit:
409     vlc_object_release( p_input );
410 }
411
412 static void *Thread( void *p_data )
413 {
414     playlist_fetcher_t *p_fetcher = p_data;
415     playlist_t *p_playlist = p_fetcher->p_playlist;
416
417     for( ;; )
418     {
419         input_item_t *p_item = NULL;
420
421         vlc_mutex_lock( &p_fetcher->lock );
422         if( p_fetcher->i_waiting != 0 )
423         {
424             p_item = p_fetcher->pp_waiting[0];
425             REMOVE_ELEM( p_fetcher->pp_waiting, p_fetcher->i_waiting, 0 );
426         }
427         else
428         {
429             p_fetcher->b_live = false;
430             vlc_cond_signal( &p_fetcher->wait );
431         }
432         vlc_mutex_unlock( &p_fetcher->lock );
433
434         if( !p_item )
435             break;
436
437         /* */
438
439         /* Wait that the input item is preparsed if it is being played */
440         WaitPreparsed( p_fetcher, p_item );
441
442         /* Triggers "meta fetcher", eventually fetch meta on the network.
443          * They are identical to "meta reader" expect that may actually
444          * takes time. That's why they are running here.
445          * The result of this fetch is not cached. */
446         FetchMeta( p_fetcher, p_item );
447
448         /* Find art, and download it if needed */
449         int i_ret = FindArt( p_fetcher, p_item );
450         if( i_ret == 1 )
451             i_ret = DownloadArt( p_fetcher, p_item );
452
453         /* */
454         char *psz_name = input_item_GetName( p_item );
455         if( !i_ret ) /* Art is now in cache */
456         {
457             PL_DEBUG( "found art for %s in cache", psz_name );
458             input_item_SetArtFetched( p_item, true );
459             var_SetAddress( p_playlist, "item-change", p_item );
460         }
461         else
462         {
463             PL_DEBUG( "art not found for %s", psz_name );
464             input_item_SetArtNotFound( p_item, true );
465         }
466         free( psz_name );
467         vlc_gc_decref( p_item );
468     }
469     return NULL;
470 }