1 /*****************************************************************************
2 * preparse.c: Preparser thread.
3 *****************************************************************************
4 * Copyright © 1999-2009 the VideoLAN team
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
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.
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.
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 *****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_playlist.h>
33 #include "preparser.h"
34 #include "../input/input_interface.h"
37 /*****************************************************************************
38 * Structures/definitions
39 *****************************************************************************/
40 struct playlist_preparser_t
42 playlist_t *p_playlist;
43 playlist_fetcher_t *p_fetcher;
48 input_item_t **pp_waiting;
54 static void *Thread( void * );
56 /*****************************************************************************
58 *****************************************************************************/
59 playlist_preparser_t *playlist_preparser_New( playlist_t *p_playlist, playlist_fetcher_t *p_fetcher )
61 playlist_preparser_t *p_preparser = malloc( sizeof(*p_preparser) );
65 p_preparser->p_playlist = p_playlist;
66 p_preparser->p_fetcher = p_fetcher;
67 vlc_mutex_init( &p_preparser->lock );
68 vlc_cond_init( &p_preparser->wait );
69 p_preparser->b_live = false;
70 p_preparser->i_art_policy = var_GetInteger( p_playlist, "album-art" );
71 p_preparser->i_waiting = 0;
72 p_preparser->pp_waiting = NULL;
77 void playlist_preparser_Push( playlist_preparser_t *p_preparser, input_item_t *p_item )
79 vlc_gc_incref( p_item );
81 vlc_mutex_lock( &p_preparser->lock );
82 INSERT_ELEM( p_preparser->pp_waiting, p_preparser->i_waiting,
83 p_preparser->i_waiting, p_item );
84 if( !p_preparser->b_live )
88 if( vlc_clone( &th, Thread, p_preparser, VLC_THREAD_PRIORITY_LOW ) )
89 msg_Warn( p_preparser->p_playlist,
90 "cannot spawn pre-parser thread" );
94 p_preparser->b_live = true;
97 vlc_mutex_unlock( &p_preparser->lock );
100 void playlist_preparser_Delete( playlist_preparser_t *p_preparser )
102 vlc_mutex_lock( &p_preparser->lock );
103 /* Remove pending item to speed up preparser thread exit */
104 while( p_preparser->i_waiting > 0 )
106 vlc_gc_decref( p_preparser->pp_waiting[0] );
107 REMOVE_ELEM( p_preparser->pp_waiting, p_preparser->i_waiting, 0 );
110 while( p_preparser->b_live )
111 vlc_cond_wait( &p_preparser->wait, &p_preparser->lock );
112 vlc_mutex_unlock( &p_preparser->lock );
114 /* Destroy the item preparser */
115 vlc_cond_destroy( &p_preparser->wait );
116 vlc_mutex_destroy( &p_preparser->lock );
120 /*****************************************************************************
122 *****************************************************************************/
124 * This function preparses an item when needed.
126 static void Preparse( playlist_t *p_playlist, input_item_t *p_item )
128 vlc_mutex_lock( &p_item->lock );
129 int i_type = p_item->i_type;
130 vlc_mutex_unlock( &p_item->lock );
132 if( i_type != ITEM_TYPE_FILE )
135 stats_TimerStart( p_playlist, "Preparse run", STATS_TIMER_PREPARSE );
137 /* Do not preparse if it is already done (like by playing it) */
138 if( !input_item_IsPreparsed( p_item ) )
140 input_Preparse( VLC_OBJECT(p_playlist), p_item );
141 input_item_SetPreparsed( p_item, true );
143 var_SetAddress( p_playlist, "item-change", p_item );
146 stats_TimerStop( p_playlist, STATS_TIMER_PREPARSE );
150 * This function ask the fetcher object to fetch the art when needed
152 static void Art( playlist_preparser_t *p_preparser, input_item_t *p_item )
154 playlist_t *p_playlist = p_preparser->p_playlist;
155 playlist_fetcher_t *p_fetcher = p_preparser->p_fetcher;
157 bool b_fetch = false;
158 /* If we haven't retrieved enough meta, add to secondary queue
159 * which will run the "meta fetchers".
160 * This only checks for meta, not for art
161 * \todo don't do this for things we won't get meta for, like vids
164 vlc_mutex_lock( &p_item->lock );
167 const char *psz_arturl = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
168 const char *psz_name = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
170 if( p_preparser->i_art_policy == ALBUM_ART_ALL &&
171 ( !psz_arturl || strncmp( psz_arturl, "file://", 7 ) ) )
173 msg_Dbg( p_playlist, "meta ok for %s, need to fetch art", psz_name );
178 msg_Dbg( p_playlist, "no fetch required for %s (art currently %s)",
179 psz_name, psz_arturl );
182 vlc_mutex_unlock( &p_item->lock );
184 if( b_fetch && p_fetcher )
185 playlist_fetcher_Push( p_fetcher, p_item );
189 * This function does the preparsing and issues the art fetching requests
191 static void *Thread( void *data )
193 playlist_preparser_t *p_preparser = data;
194 playlist_t *p_playlist = p_preparser->p_playlist;
198 input_item_t *p_current;
201 vlc_mutex_lock( &p_preparser->lock );
202 if( p_preparser->i_waiting > 0 )
204 p_current = p_preparser->pp_waiting[0];
205 REMOVE_ELEM( p_preparser->pp_waiting, p_preparser->i_waiting, 0 );
210 p_preparser->b_live = false;
212 vlc_mutex_unlock( &p_preparser->lock );
217 Preparse( p_playlist, p_current );
219 Art( p_preparser, p_current );