]> git.sesse.net Git - vlc/blob - src/playlist/preparser.c
input: add b_net variable in item
[vlc] / src / playlist / preparser.c
1 /*****************************************************************************
2  * preparser.c: Preparser thread.
3  *****************************************************************************
4  * Copyright © 1999-2009 VLC authors and VideoLAN
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 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.
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 Lesser General Public License for more details.
19  *
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  *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29
30 #include "fetcher.h"
31 #include "preparser.h"
32 #include "input/input_interface.h"
33
34 /*****************************************************************************
35  * Structures/definitions
36  *****************************************************************************/
37 typedef struct preparser_entry_t preparser_entry_t;
38
39 struct preparser_entry_t
40 {
41     input_item_t    *p_item;
42     input_item_meta_request_option_t i_options;
43 };
44
45 struct playlist_preparser_t
46 {
47     vlc_object_t        *object;
48     playlist_fetcher_t  *p_fetcher;
49
50     vlc_mutex_t     lock;
51     vlc_cond_t      wait;
52     bool            b_live;
53     preparser_entry_t  **pp_waiting;
54     int             i_waiting;
55 };
56
57 static void *Thread( void * );
58
59 /*****************************************************************************
60  * Public functions
61  *****************************************************************************/
62 playlist_preparser_t *playlist_preparser_New( vlc_object_t *parent )
63 {
64     playlist_preparser_t *p_preparser = malloc( sizeof(*p_preparser) );
65     if( !p_preparser )
66         return NULL;
67
68     p_preparser->object = parent;
69     p_preparser->p_fetcher = playlist_fetcher_New( parent );
70     if( unlikely(p_preparser->p_fetcher == NULL) )
71         msg_Err( parent, "cannot create fetcher" );
72
73     vlc_mutex_init( &p_preparser->lock );
74     vlc_cond_init( &p_preparser->wait );
75     p_preparser->b_live = false;
76     p_preparser->i_waiting = 0;
77     p_preparser->pp_waiting = NULL;
78
79     return p_preparser;
80 }
81
82 void playlist_preparser_Push( playlist_preparser_t *p_preparser, input_item_t *p_item,
83                               input_item_meta_request_option_t i_options )
84 {
85     preparser_entry_t *p_entry = malloc( sizeof(preparser_entry_t) );
86
87     if ( !p_entry )
88         return;
89     p_entry->p_item = p_item;
90     p_entry->i_options = i_options;
91     vlc_gc_incref( p_entry->p_item );
92
93     vlc_mutex_lock( &p_preparser->lock );
94     INSERT_ELEM( p_preparser->pp_waiting, p_preparser->i_waiting,
95                  p_preparser->i_waiting, p_entry );
96     if( !p_preparser->b_live )
97     {
98         if( vlc_clone_detach( NULL, Thread, p_preparser,
99                               VLC_THREAD_PRIORITY_LOW ) )
100             msg_Warn( p_preparser->object, "cannot spawn pre-parser thread" );
101         else
102             p_preparser->b_live = true;
103     }
104     vlc_mutex_unlock( &p_preparser->lock );
105 }
106
107 void playlist_preparser_fetcher_Push( playlist_preparser_t *p_preparser,
108              input_item_t *p_item, input_item_meta_request_option_t i_options )
109 {
110     if( p_preparser->p_fetcher != NULL )
111         playlist_fetcher_Push( p_preparser->p_fetcher, p_item, i_options );
112 }
113
114 void playlist_preparser_Delete( playlist_preparser_t *p_preparser )
115 {
116     vlc_mutex_lock( &p_preparser->lock );
117     /* Remove pending item to speed up preparser thread exit */
118     while( p_preparser->i_waiting > 0 )
119     {
120         preparser_entry_t *p_entry = p_preparser->pp_waiting[0];
121         vlc_gc_decref( p_entry->p_item );
122         free( p_entry );
123         REMOVE_ELEM( p_preparser->pp_waiting, p_preparser->i_waiting, 0 );
124     }
125
126     while( p_preparser->b_live )
127         vlc_cond_wait( &p_preparser->wait, &p_preparser->lock );
128     vlc_mutex_unlock( &p_preparser->lock );
129
130     /* Destroy the item preparser */
131     vlc_cond_destroy( &p_preparser->wait );
132     vlc_mutex_destroy( &p_preparser->lock );
133
134     if( p_preparser->p_fetcher != NULL )
135         playlist_fetcher_Delete( p_preparser->p_fetcher );
136     free( p_preparser );
137 }
138
139 /*****************************************************************************
140  * Privates functions
141  *****************************************************************************/
142 /**
143  * This function preparses an item when needed.
144  */
145 static void Preparse( vlc_object_t *obj, input_item_t *p_item,
146                       input_item_meta_request_option_t i_options )
147 {
148     vlc_mutex_lock( &p_item->lock );
149     int i_type = p_item->i_type;
150     bool b_net = p_item->b_net;
151     vlc_mutex_unlock( &p_item->lock );
152
153     bool b_preparse = false;
154     switch (i_type) {
155     case ITEM_TYPE_FILE:
156     case ITEM_TYPE_DIRECTORY:
157     case ITEM_TYPE_PLAYLIST:
158     case ITEM_TYPE_NODE:
159         if (!b_net || i_options & META_REQUEST_OPTION_SCOPE_NETWORK)
160             b_preparse = true;
161         break;
162     }
163     if( !b_preparse )
164     {
165         input_item_SetPreparsed( p_item, true );
166         input_item_SignalPreparseEnded( p_item );
167         return;
168     }
169
170     /* Do not preparse if it is already done (like by playing it) */
171     if( !input_item_IsPreparsed( p_item ) )
172     {
173         input_Preparse( obj, p_item );
174         input_item_SetPreparsed( p_item, true );
175
176         var_SetAddress( obj, "item-change", p_item );
177     }
178     input_item_SignalPreparseEnded( p_item );
179 }
180
181 /**
182  * This function ask the fetcher object to fetch the art when needed
183  */
184 static void Art( playlist_preparser_t *p_preparser, input_item_t *p_item )
185 {
186     vlc_object_t *obj = p_preparser->object;
187     playlist_fetcher_t *p_fetcher = p_preparser->p_fetcher;
188
189     bool b_fetch = false;
190     /* If we haven't retrieved enough meta, add to secondary queue
191      * which will run the "meta fetchers".
192      * This only checks for meta, not for art
193      * \todo don't do this for things we won't get meta for, like vids
194      */
195
196     vlc_mutex_lock( &p_item->lock );
197     if( p_item->p_meta )
198     {
199         const char *psz_arturl = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
200         const char *psz_name = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
201
202         if( !psz_arturl || ( strncmp( psz_arturl, "file://", 7 ) &&
203                              strncmp( psz_arturl, "attachment://", 13 ) ) )
204         {
205             msg_Dbg( obj, "meta ok for %s, need to fetch art",
206                      psz_name ? psz_name : "(null)" );
207             b_fetch = true;
208         }
209         else
210         {
211             msg_Dbg( obj, "no fetch required for %s (art currently %s)",
212                      psz_name ? psz_name : "(null)",
213                      psz_arturl ? psz_arturl : "(null)" );
214         }
215     }
216     vlc_mutex_unlock( &p_item->lock );
217
218     if( b_fetch && p_fetcher )
219         playlist_fetcher_Push( p_fetcher, p_item, 0 );
220 }
221
222 /**
223  * This function does the preparsing and issues the art fetching requests
224  */
225 static void *Thread( void *data )
226 {
227     playlist_preparser_t *p_preparser = data;
228     vlc_object_t *obj = p_preparser->object;
229
230     for( ;; )
231     {
232         input_item_t *p_current;
233         input_item_meta_request_option_t i_options;
234
235         /* */
236         vlc_mutex_lock( &p_preparser->lock );
237         if( p_preparser->i_waiting > 0 )
238         {
239             preparser_entry_t *p_entry = p_preparser->pp_waiting[0];
240             p_current = p_entry->p_item;
241             i_options = p_entry->i_options;
242             free( p_entry );
243             REMOVE_ELEM( p_preparser->pp_waiting, p_preparser->i_waiting, 0 );
244         }
245         else
246         {
247             p_current = NULL;
248             p_preparser->b_live = false;
249             vlc_cond_signal( &p_preparser->wait );
250         }
251         vlc_mutex_unlock( &p_preparser->lock );
252
253         if( !p_current )
254             break;
255
256         Preparse( obj, p_current, i_options );
257
258         Art( p_preparser, p_current );
259         vlc_gc_decref(p_current);
260     }
261     return NULL;
262 }
263