1 /*****************************************************************************
2 * preparser.c: Preparser 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 *****************************************************************************/
28 #include <vlc_common.h>
31 #include "preparser.h"
32 #include "input/input_interface.h"
34 /*****************************************************************************
35 * Structures/definitions
36 *****************************************************************************/
37 struct playlist_preparser_t
40 playlist_fetcher_t *p_fetcher;
45 input_item_t **pp_waiting;
49 static void *Thread( void * );
51 /*****************************************************************************
53 *****************************************************************************/
54 playlist_preparser_t *playlist_preparser_New( vlc_object_t *parent )
56 playlist_preparser_t *p_preparser = malloc( sizeof(*p_preparser) );
60 p_preparser->object = parent;
61 p_preparser->p_fetcher = playlist_fetcher_New( parent );
62 if( unlikely(p_preparser->p_fetcher == NULL) )
63 msg_Err( parent, "cannot create fetcher" );
65 vlc_mutex_init( &p_preparser->lock );
66 vlc_cond_init( &p_preparser->wait );
67 p_preparser->b_live = false;
68 p_preparser->i_waiting = 0;
69 p_preparser->pp_waiting = NULL;
74 void playlist_preparser_Push( playlist_preparser_t *p_preparser, input_item_t *p_item,
75 input_item_meta_request_option_t i_options )
77 vlc_gc_incref( p_item );
78 VLC_UNUSED( i_options );
80 vlc_mutex_lock( &p_preparser->lock );
81 INSERT_ELEM( p_preparser->pp_waiting, p_preparser->i_waiting,
82 p_preparser->i_waiting, p_item );
83 if( !p_preparser->b_live )
85 if( vlc_clone_detach( NULL, Thread, p_preparser,
86 VLC_THREAD_PRIORITY_LOW ) )
87 msg_Warn( p_preparser->object, "cannot spawn pre-parser thread" );
89 p_preparser->b_live = true;
91 vlc_mutex_unlock( &p_preparser->lock );
94 void playlist_preparser_fetcher_Push( playlist_preparser_t *p_preparser,
95 input_item_t *p_item, input_item_meta_request_option_t i_options )
97 if( p_preparser->p_fetcher != NULL )
98 playlist_fetcher_Push( p_preparser->p_fetcher, p_item, i_options );
101 void playlist_preparser_Delete( playlist_preparser_t *p_preparser )
103 vlc_mutex_lock( &p_preparser->lock );
104 /* Remove pending item to speed up preparser thread exit */
105 while( p_preparser->i_waiting > 0 )
107 vlc_gc_decref( p_preparser->pp_waiting[0] );
108 REMOVE_ELEM( p_preparser->pp_waiting, p_preparser->i_waiting, 0 );
111 while( p_preparser->b_live )
112 vlc_cond_wait( &p_preparser->wait, &p_preparser->lock );
113 vlc_mutex_unlock( &p_preparser->lock );
115 /* Destroy the item preparser */
116 vlc_cond_destroy( &p_preparser->wait );
117 vlc_mutex_destroy( &p_preparser->lock );
119 if( p_preparser->p_fetcher != NULL )
120 playlist_fetcher_Delete( p_preparser->p_fetcher );
124 /*****************************************************************************
126 *****************************************************************************/
128 * This function preparses an item when needed.
130 static void Preparse( vlc_object_t *obj, input_item_t *p_item )
132 vlc_mutex_lock( &p_item->lock );
133 int i_type = p_item->i_type;
134 vlc_mutex_unlock( &p_item->lock );
136 if( i_type != ITEM_TYPE_FILE )
138 input_item_SetPreparsed( p_item, true );
142 /* Do not preparse if it is already done (like by playing it) */
143 if( !input_item_IsPreparsed( p_item ) )
145 input_Preparse( obj, p_item );
146 input_item_SetPreparsed( p_item, true );
148 var_SetAddress( obj, "item-change", p_item );
153 * This function ask the fetcher object to fetch the art when needed
155 static void Art( playlist_preparser_t *p_preparser, input_item_t *p_item )
157 vlc_object_t *obj = p_preparser->object;
158 playlist_fetcher_t *p_fetcher = p_preparser->p_fetcher;
160 bool b_fetch = false;
161 /* If we haven't retrieved enough meta, add to secondary queue
162 * which will run the "meta fetchers".
163 * This only checks for meta, not for art
164 * \todo don't do this for things we won't get meta for, like vids
167 vlc_mutex_lock( &p_item->lock );
170 const char *psz_arturl = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
171 const char *psz_name = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
173 if( !psz_arturl || ( strncmp( psz_arturl, "file://", 7 ) &&
174 strncmp( psz_arturl, "attachment://", 13 ) ) )
176 msg_Dbg( obj, "meta ok for %s, need to fetch art",
177 psz_name ? psz_name : "(null)" );
182 msg_Dbg( obj, "no fetch required for %s (art currently %s)",
183 psz_name ? psz_name : "(null)",
184 psz_arturl ? psz_arturl : "(null)" );
187 vlc_mutex_unlock( &p_item->lock );
189 if( b_fetch && p_fetcher )
190 playlist_fetcher_Push( p_fetcher, p_item, 0 );
194 * This function does the preparsing and issues the art fetching requests
196 static void *Thread( void *data )
198 playlist_preparser_t *p_preparser = data;
199 vlc_object_t *obj = p_preparser->object;
203 input_item_t *p_current;
206 vlc_mutex_lock( &p_preparser->lock );
207 if( p_preparser->i_waiting > 0 )
209 p_current = p_preparser->pp_waiting[0];
210 REMOVE_ELEM( p_preparser->pp_waiting, p_preparser->i_waiting, 0 );
215 p_preparser->b_live = false;
216 vlc_cond_signal( &p_preparser->wait );
218 vlc_mutex_unlock( &p_preparser->lock );
223 Preparse( obj, p_current );
225 Art( p_preparser, p_current );
226 vlc_gc_decref(p_current);