access: http: only warn on deflate errors
[vlc.git] / src / playlist / fetcher.c
blobf682c8e75fd79edfc32815470eb05380a675c943
1 /*****************************************************************************
2 * fetcher.c: Art fetcher thread.
3 *****************************************************************************
4 * Copyright © 1999-2009 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Clément Stenac <zorglub@videolan.org>
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.
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.
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
28 #include <assert.h>
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>
39 #include "art.h"
40 #include "fetcher.h"
41 #include "playlist_internal.h"
43 /*****************************************************************************
44 * Structures/definitions
45 *****************************************************************************/
46 struct playlist_fetcher_t
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;
56 DECL_ARRAY(playlist_album_t) albums;
59 static void *Thread( void * );
62 /*****************************************************************************
63 * Public functions
64 *****************************************************************************/
65 playlist_fetcher_t *playlist_fetcher_New( vlc_object_t *parent )
67 playlist_fetcher_t *p_fetcher = malloc( sizeof(*p_fetcher) );
68 if( !p_fetcher )
69 return NULL;
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 );
80 return p_fetcher;
83 void playlist_fetcher_Push( playlist_fetcher_t *p_fetcher,
84 input_item_t *p_item )
86 vlc_gc_incref( p_item );
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 )
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;
100 vlc_mutex_unlock( &p_fetcher->lock );
103 void playlist_fetcher_Delete( playlist_fetcher_t *p_fetcher )
105 vlc_mutex_lock( &p_fetcher->lock );
106 /* Remove any left-over item, the fetcher will exit */
107 while( p_fetcher->i_waiting > 0 )
109 vlc_gc_decref( p_fetcher->pp_waiting[0] );
110 REMOVE_ELEM( p_fetcher->pp_waiting, p_fetcher->i_waiting, 0 );
113 while( p_fetcher->b_live )
114 vlc_cond_wait( &p_fetcher->wait, &p_fetcher->lock );
115 vlc_mutex_unlock( &p_fetcher->lock );
117 vlc_cond_destroy( &p_fetcher->wait );
118 vlc_mutex_destroy( &p_fetcher->lock );
119 free( p_fetcher );
123 /*****************************************************************************
124 * Privates functions
125 *****************************************************************************/
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
133 static int FindArt( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
135 int i_ret;
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 );
143 if( !psz_title && !psz_artist && !psz_album )
144 return VLC_EGENERIC;
146 free( psz_title );
148 /* If we already checked this album in this session, skip */
149 if( psz_artist && psz_album )
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 ) )
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 )
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;
169 else
171 return VLC_EGENERIC;
174 FOREACH_END();
176 free( psz_artist );
177 free( psz_album );
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" );
184 char *psz_arturl = input_item_GetArtURL( p_item );
185 if( psz_arturl )
187 /* We already have a URL */
188 if( !strncmp( psz_arturl, "file://", strlen( "file://" ) ) )
190 free( psz_arturl );
191 return 0; /* Art is in cache, no need to go further */
194 free( psz_arturl );
196 /* Art need to be put in cache */
197 return 1;
200 /* */
201 psz_album = input_item_GetAlbum( p_item );
202 psz_artist = input_item_GetArtist( p_item );
203 if( psz_album && psz_artist )
205 msg_Dbg( p_fetcher->object, "searching art for %s - %s",
206 psz_artist, psz_album );
208 else
210 psz_title = input_item_GetTitle( p_item );
211 if( !psz_title )
212 psz_title = input_item_GetName( p_item );
214 msg_Dbg( p_fetcher->object, "searching art for %s", psz_title );
215 free( psz_title );
218 /* Fetch the art url */
219 i_ret = VLC_EGENERIC;
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)
226 module_t *p_module;
228 p_finder->p_item = p_item;
230 p_module = module_need( p_finder, "art finder", NULL, false );
231 if( p_module )
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;
240 vlc_object_release( p_finder );
243 /* Record this album */
244 if( psz_artist && psz_album )
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 );
253 else
255 free( psz_artist );
256 free( psz_album );
259 return i_ret;
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
266 static int DownloadArt( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
268 char *psz_arturl = input_item_GetArtURL( p_item );
269 assert( *psz_arturl );
271 if( !strncmp( psz_arturl , "file://", 7 ) )
273 msg_Dbg( p_fetcher->object,
274 "Album art is local file, no need to cache" );
275 free( psz_arturl );
276 return VLC_SUCCESS;
279 if( !strncmp( psz_arturl , "APIC", 4 ) )
281 msg_Warn( p_fetcher->object, "APIC fetch not supported yet" );
282 goto error;
285 stream_t *p_stream = stream_UrlNew( p_fetcher->object, psz_arturl );
286 if( !p_stream )
287 goto error;
289 uint8_t *p_data = NULL;
290 int i_data = 0;
291 for( ;; )
293 int i_read = 65536;
295 if( i_data >= INT_MAX - i_read )
296 break;
298 p_data = realloc_or_free( p_data, i_data + i_read );
299 if( !p_data )
300 break;
302 i_read = stream_Read( p_stream, &p_data[i_data], i_read );
303 if( i_read <= 0 )
304 break;
306 i_data += i_read;
308 stream_Delete( p_stream );
310 if( p_data && i_data > 0 )
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 */
316 playlist_SaveArt( p_fetcher->object, p_item,
317 p_data, i_data, psz_type );
320 free( p_data );
322 free( psz_arturl );
323 return VLC_SUCCESS;
325 error:
326 free( psz_arturl );
327 return VLC_EGENERIC;
331 * FetchMeta, run the "meta fetcher". They are going to do network
332 * connections, and gather information upon the playing media.
333 * (even artwork).
335 static void FetchMeta( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
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;
342 p_demux_meta->p_demux = NULL;
343 p_demux_meta->p_item = p_item;
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 );
351 static void *Thread( void *p_data )
353 playlist_fetcher_t *p_fetcher = p_data;
354 vlc_object_t *obj = p_fetcher->object;
356 for( ;; )
358 input_item_t *p_item = NULL;
360 vlc_mutex_lock( &p_fetcher->lock );
361 if( p_fetcher->i_waiting != 0 )
363 p_item = p_fetcher->pp_waiting[0];
364 REMOVE_ELEM( p_fetcher->pp_waiting, p_fetcher->i_waiting, 0 );
366 else
368 p_fetcher->b_live = false;
369 vlc_cond_signal( &p_fetcher->wait );
371 vlc_mutex_unlock( &p_fetcher->lock );
373 if( !p_item )
374 break;
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 );
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 );
387 /* */
388 char *psz_name = input_item_GetName( p_item );
389 if( !i_ret ) /* Art is now in cache */
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 );
395 else
397 msg_Dbg( obj, "art not found for %s", psz_name );
398 input_item_SetArtNotFound( p_item, true );
400 free( psz_name );
401 vlc_gc_decref( p_item );
403 return NULL;