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
54 input_item_t **pp_waiting;
56 DECL_ARRAY(playlist_album_t) albums;
59 static void *Thread( void * );
62 /*****************************************************************************
64 *****************************************************************************/
65 playlist_fetcher_t *playlist_fetcher_New( vlc_object_t *parent )
67 playlist_fetcher_t *p_fetcher = malloc( sizeof(*p_fetcher) );
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 );
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" );
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 );
123 /*****************************************************************************
125 *****************************************************************************/
127 * This function locates the art associated to an input item.
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 )
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 );
141 psz_title = input_item_GetName( p_item );
143 if( !psz_title && !psz_artist && !psz_album )
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 */
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 );
179 if ( playlist_FindArtInCacheUsingItemUID( p_item ) != VLC_SUCCESS )
180 playlist_FindArtInCache( p_item );
182 msg_Dbg( p_fetcher->object, "successfully retrieved arturl by uid" );
184 char *psz_arturl = input_item_GetArtURL( p_item );
187 /* We already have a URL */
188 if( !strncmp( psz_arturl, "file://", strlen( "file://" ) ) )
191 return 0; /* Art is in cache, no need to go further */
196 /* Art need to be put in cache */
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 );
210 psz_title = input_item_GetTitle( p_item );
212 psz_title = input_item_GetName( p_item );
214 msg_Dbg( p_fetcher->object, "searching art for %s", 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)
228 p_finder->p_item = p_item;
230 p_module = module_need( p_finder, "art finder", NULL, false );
233 module_unneed( p_finder, p_module );
234 /* Try immediately if found in cache by download URL */
235 if( !playlist_FindArtInCache( p_item ) )
240 vlc_object_release( p_finder );
243 /* Record this album */
244 if( psz_artist && psz_album )
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 );
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" );
279 if( !strncmp( psz_arturl , "APIC", 4 ) )
281 msg_Warn( p_fetcher->object, "APIC fetch not supported yet" );
285 stream_t *p_stream = stream_UrlNew( p_fetcher->object, psz_arturl );
289 uint8_t *p_data = NULL;
295 if( i_data >= INT_MAX - i_read )
298 p_data = realloc_or_free( p_data, i_data + i_read );
302 i_read = stream_Read( p_stream, &p_data[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 );
331 * FetchMeta, run the "meta fetcher". They are going to do network
332 * connections, and gather information upon the playing media.
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" );
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 );
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;
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 );
368 p_fetcher->b_live = false;
369 vlc_cond_signal( &p_fetcher->wait );
371 vlc_mutex_unlock( &p_fetcher->lock );
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 );
385 i_ret = DownloadArt( p_fetcher, p_item );
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 );
397 msg_Dbg( obj, "art not found for %s", psz_name );
398 input_item_SetArtNotFound( p_item, true );
401 vlc_gc_decref( p_item );