]> git.sesse.net Git - vlc/blob - src/playlist/fetcher.c
use pid for tmp file patterns
[vlc] / src / playlist / fetcher.c
1 /*****************************************************************************
2  * fetcher.c: Art fetcher thread.
3  *****************************************************************************
4  * Copyright © 1999-2009 VLC authors and VideoLAN
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 it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * 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 <limits.h>
29 #include <assert.h>
30
31 #include <vlc_common.h>
32 #include <vlc_stream.h>
33 #include <vlc_art_finder.h>
34 #include <vlc_memory.h>
35 #include <vlc_demux.h>
36 #include <vlc_modules.h>
37
38 #include "libvlc.h"
39 #include "art.h"
40 #include "fetcher.h"
41 #include "input/input_interface.h"
42
43 /*****************************************************************************
44  * Structures/definitions
45  *****************************************************************************/
46 struct playlist_fetcher_t
47 {
48     vlc_object_t   *object;
49     vlc_mutex_t     lock;
50     vlc_cond_t      wait;
51     bool            b_live;
52     int             i_art_policy;
53     int             i_waiting;
54     input_item_t    **pp_waiting;
55
56     DECL_ARRAY(playlist_album_t) albums;
57 };
58
59 static void *Thread( void * );
60
61
62 /*****************************************************************************
63  * Public functions
64  *****************************************************************************/
65 playlist_fetcher_t *playlist_fetcher_New( vlc_object_t *parent )
66 {
67     playlist_fetcher_t *p_fetcher = malloc( sizeof(*p_fetcher) );
68     if( !p_fetcher )
69         return NULL;
70
71     p_fetcher->object = parent;
72     vlc_mutex_init( &p_fetcher->lock );
73     vlc_cond_init( &p_fetcher->wait );
74     p_fetcher->b_live = false;
75     p_fetcher->i_waiting = 0;
76     p_fetcher->pp_waiting = NULL;
77     p_fetcher->i_art_policy = var_GetInteger( parent, "album-art" );
78     ARRAY_INIT( p_fetcher->albums );
79
80     return p_fetcher;
81 }
82
83 void playlist_fetcher_Push( playlist_fetcher_t *p_fetcher,
84                             input_item_t *p_item )
85 {
86     vlc_gc_incref( p_item );
87
88     vlc_mutex_lock( &p_fetcher->lock );
89     INSERT_ELEM( p_fetcher->pp_waiting, p_fetcher->i_waiting,
90                  p_fetcher->i_waiting, p_item );
91     if( !p_fetcher->b_live )
92     {
93         if( vlc_clone_detach( NULL, Thread, p_fetcher,
94                               VLC_THREAD_PRIORITY_LOW ) )
95             msg_Err( p_fetcher->object,
96                      "cannot spawn secondary preparse thread" );
97         else
98             p_fetcher->b_live = true;
99     }
100     vlc_mutex_unlock( &p_fetcher->lock );
101 }
102
103 void playlist_fetcher_Delete( playlist_fetcher_t *p_fetcher )
104 {
105     vlc_mutex_lock( &p_fetcher->lock );
106     /* Remove any left-over item, the fetcher will exit */
107     while( p_fetcher->i_waiting > 0 )
108     {
109         vlc_gc_decref( p_fetcher->pp_waiting[0] );
110         REMOVE_ELEM( p_fetcher->pp_waiting, p_fetcher->i_waiting, 0 );
111     }
112
113     while( p_fetcher->b_live )
114         vlc_cond_wait( &p_fetcher->wait, &p_fetcher->lock );
115     vlc_mutex_unlock( &p_fetcher->lock );
116
117     vlc_cond_destroy( &p_fetcher->wait );
118     vlc_mutex_destroy( &p_fetcher->lock );
119     free( p_fetcher );
120 }
121
122
123 /*****************************************************************************
124  * Privates functions
125  *****************************************************************************/
126 /**
127  * This function locates the art associated to an input item.
128  * Return codes:
129  *   0 : Art is in cache or is a local file
130  *   1 : Art found, need to download
131  *  -X : Error/not found
132  */
133 static int FindArt( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
134 {
135     int i_ret;
136
137     char *psz_artist = input_item_GetArtist( p_item );
138     char *psz_album = input_item_GetAlbum( p_item );
139     char *psz_title = input_item_GetTitle( p_item );
140     if( !psz_title )
141         psz_title = input_item_GetName( p_item );
142
143     if( !psz_title && !psz_artist && !psz_album )
144         return VLC_EGENERIC;
145
146     free( psz_title );
147
148     /* If we already checked this album in this session, skip */
149     if( psz_artist && psz_album )
150     {
151         FOREACH_ARRAY( playlist_album_t album, p_fetcher->albums )
152             if( !strcmp( album.psz_artist, psz_artist ) &&
153                 !strcmp( album.psz_album, psz_album ) )
154             {
155                 msg_Dbg( p_fetcher->object,
156                          " %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     if ( playlist_FindArtInCacheUsingItemUID( p_item ) != VLC_SUCCESS )
180         playlist_FindArtInCache( p_item );
181     else
182         msg_Dbg( p_fetcher->object, "successfully retrieved arturl by uid" );
183
184     char *psz_arturl = input_item_GetArtURL( p_item );
185     if( psz_arturl )
186     {
187         /* We already have a 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->object, "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->object, "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 = p_fetcher->object;
222     art_finder_t *p_finder =
223         vlc_custom_create( p_parent, sizeof( *p_finder ), "art finder" );
224     if( p_finder != NULL)
225     {
226         module_t *p_module;
227
228         p_finder->p_item = p_item;
229
230         p_module = module_need( p_finder, "art finder", NULL, false );
231         if( p_module )
232         {
233             module_unneed( p_finder, p_module );
234             /* Try immediately if found in cache by download URL */
235             if( !playlist_FindArtInCache( p_item ) )
236                 i_ret = 0;
237             else
238                 i_ret = 1;
239         }
240         vlc_object_release( p_finder );
241     }
242
243     /* Record this album */
244     if( psz_artist && psz_album )
245     {
246         playlist_album_t a;
247         a.psz_artist = psz_artist;
248         a.psz_album = psz_album;
249         a.psz_arturl = input_item_GetArtURL( p_item );
250         a.b_found = (i_ret == VLC_EGENERIC ? false : true );
251         ARRAY_APPEND( p_fetcher->albums, a );
252     }
253     else
254     {
255         free( psz_artist );
256         free( psz_album );
257     }
258
259     return i_ret;
260 }
261
262 /**
263  * Download the art using the URL or an art downloaded
264  * This function should be called only if data is not already in cache
265  */
266 static int DownloadArt( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
267 {
268     char *psz_arturl = input_item_GetArtURL( p_item );
269     assert( *psz_arturl );
270
271     if( !strncmp( psz_arturl , "file://", 7 ) )
272     {
273         msg_Dbg( p_fetcher->object,
274                  "Album art is local file, no need to cache" );
275         free( psz_arturl );
276         return VLC_SUCCESS;
277     }
278
279     if( !strncmp( psz_arturl , "APIC", 4 ) )
280     {
281         msg_Warn( p_fetcher->object, "APIC fetch not supported yet" );
282         goto error;
283     }
284
285     stream_t *p_stream = stream_UrlNew( p_fetcher->object, psz_arturl );
286     if( !p_stream )
287         goto error;
288
289     uint8_t *p_data = NULL;
290     int i_data = 0;
291     for( ;; )
292     {
293         int i_read = 65536;
294
295         if( i_data >= INT_MAX - i_read )
296             break;
297
298         p_data = realloc_or_free( p_data, i_data + i_read );
299         if( !p_data )
300             break;
301
302         i_read = stream_Read( p_stream, &p_data[i_data], i_read );
303         if( i_read <= 0 )
304             break;
305
306         i_data += i_read;
307     }
308     stream_Delete( p_stream );
309
310     if( p_data && i_data > 0 )
311     {
312         char *psz_type = strrchr( psz_arturl, '.' );
313         if( psz_type && strlen( psz_type ) > 5 )
314             psz_type = NULL; /* remove extension if it's > to 4 characters */
315
316         playlist_SaveArt( p_fetcher->object, p_item,
317                           p_data, i_data, psz_type );
318     }
319
320     free( p_data );
321
322     free( psz_arturl );
323     return VLC_SUCCESS;
324
325 error:
326     free( psz_arturl );
327     return VLC_EGENERIC;
328 }
329
330 /**
331  * FetchMeta, run the "meta fetcher". They are going to do network
332  * connections, and gather information upon the playing media.
333  * (even artwork).
334  */
335 static void FetchMeta( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
336 {
337     demux_meta_t *p_demux_meta = vlc_custom_create(p_fetcher->object,
338                                          sizeof(*p_demux_meta), "demux meta" );
339     if( !p_demux_meta )
340         return;
341
342     p_demux_meta->p_demux = NULL;
343     p_demux_meta->p_item = p_item;
344
345     module_t *p_meta_fetcher = module_need( p_demux_meta, "meta fetcher", NULL, false );
346     if( p_meta_fetcher )
347         module_unneed( p_demux_meta, p_meta_fetcher );
348     vlc_object_release( p_demux_meta );
349 }
350
351 static void *Thread( void *p_data )
352 {
353     playlist_fetcher_t *p_fetcher = p_data;
354     vlc_object_t *obj = p_fetcher->object;
355
356     for( ;; )
357     {
358         input_item_t *p_item = NULL;
359
360         vlc_mutex_lock( &p_fetcher->lock );
361         if( p_fetcher->i_waiting != 0 )
362         {
363             p_item = p_fetcher->pp_waiting[0];
364             REMOVE_ELEM( p_fetcher->pp_waiting, p_fetcher->i_waiting, 0 );
365         }
366         else
367         {
368             p_fetcher->b_live = false;
369             vlc_cond_signal( &p_fetcher->wait );
370         }
371         vlc_mutex_unlock( &p_fetcher->lock );
372
373         if( !p_item )
374             break;
375
376         /* Triggers "meta fetcher", eventually fetch meta on the network.
377          * They are identical to "meta reader" expect that may actually
378          * takes time. That's why they are running here.
379          * The result of this fetch is not cached. */
380         FetchMeta( p_fetcher, p_item );
381
382         /* Find art, and download it if needed */
383         int i_ret = FindArt( p_fetcher, p_item );
384         if( i_ret == 1 )
385             i_ret = DownloadArt( p_fetcher, p_item );
386
387         /* */
388         char *psz_name = input_item_GetName( p_item );
389         if( !i_ret ) /* Art is now in cache */
390         {
391             msg_Dbg( obj, "found art for %s in cache", psz_name );
392             input_item_SetArtFetched( p_item, true );
393             var_SetAddress( obj, "item-change", p_item );
394         }
395         else
396         {
397             msg_Dbg( obj, "art not found for %s", psz_name );
398             input_item_SetArtNotFound( p_item, true );
399         }
400         free( psz_name );
401         vlc_gc_decref( p_item );
402     }
403     return NULL;
404 }