1 /*****************************************************************************
2 * fetcher.c: Art fetcher thread.
3 *****************************************************************************
4 * Copyright © 1999-2009 VLC authors and VideoLAN
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 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_playlist.h>
32 #include <vlc_stream.h>
34 #include <vlc_art_finder.h>
35 #include <vlc_memory.h>
36 #include <vlc_demux.h>
37 #include <vlc_modules.h>
41 #include "playlist_internal.h"
43 /*****************************************************************************
44 * Structures/definitions
45 *****************************************************************************/
46 struct playlist_fetcher_t
48 playlist_t *p_playlist;
55 input_item_t **pp_waiting;
57 DECL_ARRAY(playlist_album_t) albums;
60 static void *Thread( void * );
63 /*****************************************************************************
65 *****************************************************************************/
66 playlist_fetcher_t *playlist_fetcher_New( playlist_t *p_playlist )
68 playlist_fetcher_t *p_fetcher = malloc( sizeof(*p_fetcher) );
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 );
84 void playlist_fetcher_Push( playlist_fetcher_t *p_fetcher,
85 input_item_t *p_item )
87 vlc_gc_incref( p_item );
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 )
94 if( vlc_clone_detach( NULL, Thread, p_fetcher,
95 VLC_THREAD_PRIORITY_LOW ) )
96 msg_Err( p_fetcher->p_playlist,
97 "cannot spawn secondary preparse thread" );
99 p_fetcher->b_live = true;
101 vlc_mutex_unlock( &p_fetcher->lock );
104 void playlist_fetcher_Delete( playlist_fetcher_t *p_fetcher )
106 vlc_mutex_lock( &p_fetcher->lock );
107 /* Remove any left-over item, the fetcher will exit */
108 while( p_fetcher->i_waiting > 0 )
110 vlc_gc_decref( p_fetcher->pp_waiting[0] );
111 REMOVE_ELEM( p_fetcher->pp_waiting, p_fetcher->i_waiting, 0 );
114 while( p_fetcher->b_live )
115 vlc_cond_wait( &p_fetcher->wait, &p_fetcher->lock );
116 vlc_mutex_unlock( &p_fetcher->lock );
118 vlc_cond_destroy( &p_fetcher->wait );
119 vlc_mutex_destroy( &p_fetcher->lock );
124 /*****************************************************************************
126 *****************************************************************************/
128 * This function locates the art associated to an input item.
130 * 0 : Art is in cache or is a local file
131 * 1 : Art found, need to download
132 * -X : Error/not found
134 static int FindArt( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
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 );
142 psz_title = input_item_GetName( p_item );
144 if( !psz_title && !psz_artist && !psz_album )
149 /* If we already checked this album in this session, skip */
150 if( psz_artist && psz_album )
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 ) )
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 */
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 );
180 playlist_FindArtInCache( p_item );
182 char *psz_arturl = input_item_GetArtURL( p_item );
185 /* We already have a URL */
186 if( !strncmp( psz_arturl, "file://", strlen( "file://" ) ) )
189 return 0; /* Art is in cache, no need to go further */
194 /* Art need to be put in cache */
199 psz_album = input_item_GetAlbum( p_item );
200 psz_artist = input_item_GetArtist( p_item );
201 if( psz_album && psz_artist )
203 msg_Dbg( p_fetcher->p_playlist, "searching art for %s - %s",
204 psz_artist, psz_album );
208 psz_title = input_item_GetTitle( p_item );
210 psz_title = input_item_GetName( p_item );
212 msg_Dbg( p_fetcher->p_playlist, "searching art for %s", psz_title );
216 /* Fetch the art url */
217 i_ret = VLC_EGENERIC;
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 ), "art finder" );
222 if( p_finder != NULL)
226 p_finder->p_item = p_item;
228 p_module = module_need( p_finder, "art finder", NULL, false );
231 module_unneed( p_finder, p_module );
232 /* Try immediately if found in cache by download URL */
233 if( !playlist_FindArtInCache( p_item ) )
238 vlc_object_release( p_finder );
241 /* Record this album */
242 if( psz_artist && psz_album )
245 a.psz_artist = psz_artist;
246 a.psz_album = psz_album;
247 a.psz_arturl = input_item_GetArtURL( p_item );
248 a.b_found = (i_ret == VLC_EGENERIC ? false : true );
249 ARRAY_APPEND( p_fetcher->albums, a );
261 * Download the art using the URL or an art downloaded
262 * This function should be called only if data is not already in cache
264 static int DownloadArt( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
266 char *psz_arturl = input_item_GetArtURL( p_item );
267 assert( *psz_arturl );
269 if( !strncmp( psz_arturl , "file://", 7 ) )
271 msg_Dbg( p_fetcher->p_playlist,
272 "Album art is local file, no need to cache" );
277 if( !strncmp( psz_arturl , "APIC", 4 ) )
279 msg_Warn( p_fetcher->p_playlist, "APIC fetch not supported yet" );
283 stream_t *p_stream = stream_UrlNew( p_fetcher->p_playlist, psz_arturl );
287 uint8_t *p_data = NULL;
293 if( i_data >= INT_MAX - i_read )
296 p_data = realloc_or_free( p_data, i_data + i_read );
300 i_read = stream_Read( p_stream, &p_data[i_data], i_read );
306 stream_Delete( p_stream );
308 if( p_data && i_data > 0 )
310 char *psz_type = strrchr( psz_arturl, '.' );
311 if( psz_type && strlen( psz_type ) > 5 )
312 psz_type = NULL; /* remove extension if it's > to 4 characters */
314 playlist_SaveArt( p_fetcher->p_playlist, p_item, p_data, i_data, psz_type );
328 * FetchMeta, run the "meta fetcher". They are going to do network
329 * connections, and gather information upon the playing media.
332 static void FetchMeta( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
334 demux_meta_t *p_demux_meta = vlc_custom_create(p_fetcher->p_playlist,
335 sizeof(*p_demux_meta), "demux meta" );
339 p_demux_meta->p_demux = NULL;
340 p_demux_meta->p_item = p_item;
342 module_t *p_meta_fetcher = module_need( p_demux_meta, "meta fetcher", NULL, false );
344 module_unneed( p_demux_meta, p_meta_fetcher );
345 vlc_object_release( p_demux_meta );
348 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
349 vlc_value_t oldval, vlc_value_t newval, void *p_data )
351 VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
352 vlc_cond_t *p_cond = p_data;
354 if( newval.i_int == INPUT_EVENT_ITEM_META ||
355 newval.i_int == INPUT_EVENT_DEAD )
356 vlc_cond_signal( p_cond );
362 /* Check if it is not yet preparsed and if so wait for it
364 * (This can happen if we fetch art on play)
365 * FIXME this doesn't work if we need to fetch meta before art...
367 static void WaitPreparsed( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
369 if( input_item_IsPreparsed( p_item ) )
372 input_thread_t *p_input = playlist_CurrentInput( p_fetcher->p_playlist );
376 if( input_GetItem( p_input ) != p_item )
380 vlc_cond_init( &cond );
381 var_AddCallback( p_input, "intf-event", InputEvent, &cond );
383 const mtime_t i_deadline = mdate() + 500*1000;
384 bool b_timeout = false;
386 while( !p_input->b_eof && !p_input->b_error
387 && !input_item_IsPreparsed( p_item ) && !b_timeout )
389 /* A bit weird, but input_item_IsPreparsed holds the protected value */
390 /* FIXME: locking looks wrong here */
391 vlc_mutex_lock( &p_fetcher->lock );
392 if( vlc_cond_timedwait( &cond, &p_fetcher->lock, i_deadline ) )
394 vlc_mutex_unlock( &p_fetcher->lock );
397 var_DelCallback( p_input, "intf-event", InputEvent, &cond );
398 vlc_cond_destroy( &cond );
401 vlc_object_release( p_input );
404 static void *Thread( void *p_data )
406 playlist_fetcher_t *p_fetcher = p_data;
407 playlist_t *p_playlist = p_fetcher->p_playlist;
411 input_item_t *p_item = NULL;
413 vlc_mutex_lock( &p_fetcher->lock );
414 if( p_fetcher->i_waiting != 0 )
416 p_item = p_fetcher->pp_waiting[0];
417 REMOVE_ELEM( p_fetcher->pp_waiting, p_fetcher->i_waiting, 0 );
421 p_fetcher->b_live = false;
422 vlc_cond_signal( &p_fetcher->wait );
424 vlc_mutex_unlock( &p_fetcher->lock );
431 /* Wait that the input item is preparsed if it is being played */
432 WaitPreparsed( p_fetcher, p_item );
434 /* Triggers "meta fetcher", eventually fetch meta on the network.
435 * They are identical to "meta reader" expect that may actually
436 * takes time. That's why they are running here.
437 * The result of this fetch is not cached. */
438 FetchMeta( p_fetcher, p_item );
440 /* Find art, and download it if needed */
441 int i_ret = FindArt( p_fetcher, p_item );
443 i_ret = DownloadArt( p_fetcher, p_item );
446 char *psz_name = input_item_GetName( p_item );
447 if( !i_ret ) /* Art is now in cache */
449 PL_DEBUG( "found art for %s in cache", psz_name );
450 input_item_SetArtFetched( p_item, true );
451 var_SetAddress( p_playlist, "item-change", p_item );
455 PL_DEBUG( "art not found for %s", psz_name );
456 input_item_SetArtNotFound( p_item, true );
459 vlc_gc_decref( p_item );