]> git.sesse.net Git - vlc/blob - src/playlist/preparser.c
preparser: shut threat down when not needed
[vlc] / src / playlist / preparser.c
1 /*****************************************************************************
2  * preparse.c: Preparser thread.
3  *****************************************************************************
4  * Copyright © 1999-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Clément Stenac <zorglub@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_playlist.h>
30
31 #include "art.h"
32 #include "fetcher.h"
33 #include "preparser.h"
34 #include "../input/input_interface.h"
35
36
37 /*****************************************************************************
38  * Structures/definitions
39  *****************************************************************************/
40 struct playlist_preparser_t
41 {
42     playlist_t          *p_playlist;
43     playlist_fetcher_t  *p_fetcher;
44
45     vlc_thread_t    thread;
46     vlc_mutex_t     lock;
47     bool            b_live, b_zombie;
48     input_item_t  **pp_waiting;
49     int             i_waiting;
50
51     int             i_art_policy;
52 };
53
54 static void *Thread( void * );
55
56 /*****************************************************************************
57  * Public functions
58  *****************************************************************************/
59 playlist_preparser_t *playlist_preparser_New( playlist_t *p_playlist, playlist_fetcher_t *p_fetcher )
60 {
61     playlist_preparser_t *p_preparser = malloc( sizeof(*p_preparser) );
62     if( !p_preparser )
63         return NULL;
64
65     p_preparser->p_playlist = p_playlist;
66     p_preparser->p_fetcher = p_fetcher;
67     vlc_mutex_init( &p_preparser->lock );
68     p_preparser->b_zombie = p_preparser->b_live = false;
69     p_preparser->i_art_policy = var_GetInteger( p_playlist, "album-art" );
70     p_preparser->i_waiting = 0;
71     p_preparser->pp_waiting = NULL;
72
73     return p_preparser;
74 }
75
76 void playlist_preparser_Push( playlist_preparser_t *p_preparser, input_item_t *p_item )
77 {
78     vlc_gc_incref( p_item );
79
80     vlc_mutex_lock( &p_preparser->lock );
81     if( p_preparser->b_zombie )
82     {
83         vlc_join( p_preparser->thread, NULL );
84         p_preparser->b_zombie = false;
85     }
86     INSERT_ELEM( p_preparser->pp_waiting, p_preparser->i_waiting,
87                  p_preparser->i_waiting, p_item );
88     if( !p_preparser->b_live )
89     {
90         if( vlc_clone( &p_preparser->thread, Thread, p_preparser,
91                        VLC_THREAD_PRIORITY_LOW ) )
92             msg_Warn( p_preparser->p_playlist,
93                       "cannot spawn pre-parser thread" );
94         else
95             p_preparser->b_live = true;
96     }
97     vlc_mutex_unlock( &p_preparser->lock );
98 }
99
100 void playlist_preparser_Delete( playlist_preparser_t *p_preparser )
101 {
102     bool b_join;
103
104     /* Destroy the item preparser */
105     vlc_mutex_lock( &p_preparser->lock );
106     if( p_preparser->b_live )
107         vlc_cancel( p_preparser->thread );
108     b_join = p_preparser->b_live || p_preparser->b_zombie;
109     vlc_mutex_unlock( &p_preparser->lock );
110     if( b_join )
111         vlc_join( p_preparser->thread, NULL );
112
113     while( p_preparser->i_waiting > 0 )
114     {   /* Any left-over unparsed item? */
115         vlc_gc_decref( p_preparser->pp_waiting[0] );
116         REMOVE_ELEM( p_preparser->pp_waiting, p_preparser->i_waiting, 0 );
117     }
118     vlc_mutex_destroy( &p_preparser->lock );
119     free( p_preparser );
120 }
121
122 /*****************************************************************************
123  * Privates functions
124  *****************************************************************************/
125 /**
126  * This function preparses an item when needed.
127  */
128 static void Preparse( playlist_t *p_playlist, input_item_t *p_item )
129 {
130     vlc_mutex_lock( &p_item->lock );
131     int i_type = p_item->i_type;
132     vlc_mutex_unlock( &p_item->lock );
133
134     if( i_type != ITEM_TYPE_FILE )
135         return;
136
137     stats_TimerStart( p_playlist, "Preparse run", STATS_TIMER_PREPARSE );
138
139     /* Do not preparse if it is already done (like by playing it) */
140     if( !input_item_IsPreparsed( p_item ) )
141     {
142         input_Preparse( VLC_OBJECT(p_playlist), p_item );
143         input_item_SetPreparsed( p_item, true );
144
145         var_SetAddress( p_playlist, "item-change", p_item );
146     }
147
148     stats_TimerStop( p_playlist, STATS_TIMER_PREPARSE );
149 }
150
151 /**
152  * This function ask the fetcher object to fetch the art when needed
153  */
154 static void Art( playlist_preparser_t *p_preparser, input_item_t *p_item )
155 {
156     playlist_t *p_playlist = p_preparser->p_playlist;
157     playlist_fetcher_t *p_fetcher = p_preparser->p_fetcher;
158
159     bool b_fetch = false;
160     /* If we haven't retrieved enough meta, add to secondary queue
161      * which will run the "meta fetchers".
162      * This only checks for meta, not for art
163      * \todo don't do this for things we won't get meta for, like vids
164      */
165
166     vlc_mutex_lock( &p_item->lock );
167     if( p_item->p_meta )
168     {
169         const char *psz_arturl = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
170         const char *psz_name = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
171
172         if( p_preparser->i_art_policy == ALBUM_ART_ALL &&
173             ( !psz_arturl || strncmp( psz_arturl, "file://", 7 ) ) )
174         {
175             msg_Dbg( p_playlist, "meta ok for %s, need to fetch art", psz_name );
176             b_fetch = true;
177         }
178         else
179         {
180             msg_Dbg( p_playlist, "no fetch required for %s (art currently %s)",
181                      psz_name, psz_arturl );
182         }
183     }
184     vlc_mutex_unlock( &p_item->lock );
185
186     if( b_fetch && p_fetcher )
187         playlist_fetcher_Push( p_fetcher, p_item );
188 }
189
190 /**
191  * This function does the preparsing and issues the art fetching requests
192  */
193 static void *Thread( void *data )
194 {
195     playlist_preparser_t *p_preparser = data;
196     playlist_t *p_playlist = p_preparser->p_playlist;
197
198     for( ;; )
199     {
200         input_item_t *p_current;
201
202         /* */
203         vlc_mutex_lock( &p_preparser->lock );
204         if( p_preparser->i_waiting > 0 )
205         {
206             p_current = p_preparser->pp_waiting[0];
207             REMOVE_ELEM( p_preparser->pp_waiting, p_preparser->i_waiting, 0 );
208         }
209         else
210         {
211             p_current = NULL;
212             p_preparser->b_live = false;
213             p_preparser->b_zombie = true;
214         }
215         vlc_mutex_unlock( &p_preparser->lock );
216
217         if( !p_current )
218             break;
219
220         int canc = vlc_savecancel();
221
222         Preparse( p_playlist, p_current );
223
224         Art( p_preparser, p_current );
225
226         vlc_restorecancel( canc );
227
228         /* */
229         int i_activity = var_GetInteger( p_playlist, "activity" );
230         if( i_activity < 0 )
231             i_activity = 0;
232
233         /* Sleep at least 1ms and cancel thread if needed */
234         msleep( (i_activity+1) * 1000 );
235     }
236     return NULL;
237 }
238