]> git.sesse.net Git - vlc/blob - src/playlist/preparser.c
x264: remove no-op
[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 struct playlist_preparser_t
38 {
39     vlc_object_t        *object;
40     playlist_fetcher_t  *p_fetcher;
41
42     vlc_mutex_t     lock;
43     vlc_cond_t      wait;
44     bool            b_live;
45     input_item_t  **pp_waiting;
46     int             i_waiting;
47
48     int             i_art_policy;
49 };
50
51 static void *Thread( void * );
52
53 /*****************************************************************************
54  * Public functions
55  *****************************************************************************/
56 playlist_preparser_t *playlist_preparser_New( vlc_object_t *parent )
57 {
58     playlist_preparser_t *p_preparser = malloc( sizeof(*p_preparser) );
59     if( !p_preparser )
60         return NULL;
61
62     p_preparser->object = parent;
63     p_preparser->p_fetcher = playlist_fetcher_New( parent );
64     if( unlikely(p_preparser->p_fetcher == NULL) )
65         msg_Err( parent, "cannot create fetcher" );
66
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_InheritInteger( parent, "album-art" );
71     p_preparser->i_waiting = 0;
72     p_preparser->pp_waiting = NULL;
73
74     return p_preparser;
75 }
76
77 void playlist_preparser_Push( playlist_preparser_t *p_preparser, input_item_t *p_item )
78 {
79     vlc_gc_incref( p_item );
80
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 )
85     {
86         if( vlc_clone_detach( NULL, Thread, p_preparser,
87                               VLC_THREAD_PRIORITY_LOW ) )
88             msg_Warn( p_preparser->object, "cannot spawn pre-parser thread" );
89         else
90             p_preparser->b_live = true;
91     }
92     vlc_mutex_unlock( &p_preparser->lock );
93 }
94
95 void playlist_preparser_fetcher_Push( playlist_preparser_t *p_preparser,
96                                       input_item_t *p_item )
97 {
98     if( p_preparser->p_fetcher != NULL )
99         playlist_fetcher_Push( p_preparser->p_fetcher, p_item );
100 }
101
102 void playlist_preparser_Delete( playlist_preparser_t *p_preparser )
103 {
104     vlc_mutex_lock( &p_preparser->lock );
105     /* Remove pending item to speed up preparser thread exit */
106     while( p_preparser->i_waiting > 0 )
107     {
108         vlc_gc_decref( p_preparser->pp_waiting[0] );
109         REMOVE_ELEM( p_preparser->pp_waiting, p_preparser->i_waiting, 0 );
110     }
111
112     while( p_preparser->b_live )
113         vlc_cond_wait( &p_preparser->wait, &p_preparser->lock );
114     vlc_mutex_unlock( &p_preparser->lock );
115
116     /* Destroy the item preparser */
117     vlc_cond_destroy( &p_preparser->wait );
118     vlc_mutex_destroy( &p_preparser->lock );
119
120     if( p_preparser->p_fetcher != NULL )
121         playlist_fetcher_Delete( p_preparser->p_fetcher );
122     free( p_preparser );
123 }
124
125 /*****************************************************************************
126  * Privates functions
127  *****************************************************************************/
128 /**
129  * This function preparses an item when needed.
130  */
131 static void Preparse( vlc_object_t *obj, input_item_t *p_item )
132 {
133     vlc_mutex_lock( &p_item->lock );
134     int i_type = p_item->i_type;
135     vlc_mutex_unlock( &p_item->lock );
136
137     if( i_type != ITEM_TYPE_FILE )
138     {
139         input_item_SetPreparsed( p_item, true );
140         return;
141     }
142
143     /* Do not preparse if it is already done (like by playing it) */
144     if( !input_item_IsPreparsed( p_item ) )
145     {
146         input_Preparse( obj, p_item );
147         input_item_SetPreparsed( p_item, true );
148
149         var_SetAddress( obj, "item-change", p_item );
150     }
151 }
152
153 /**
154  * This function ask the fetcher object to fetch the art when needed
155  */
156 static void Art( playlist_preparser_t *p_preparser, input_item_t *p_item )
157 {
158     vlc_object_t *obj = p_preparser->object;
159     playlist_fetcher_t *p_fetcher = p_preparser->p_fetcher;
160
161     bool b_fetch = false;
162     /* If we haven't retrieved enough meta, add to secondary queue
163      * which will run the "meta fetchers".
164      * This only checks for meta, not for art
165      * \todo don't do this for things we won't get meta for, like vids
166      */
167
168     vlc_mutex_lock( &p_item->lock );
169     if( p_item->p_meta )
170     {
171         const char *psz_arturl = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
172         const char *psz_name = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
173
174         if( p_preparser->i_art_policy == ALBUM_ART_ALL &&
175                 ( !psz_arturl ||
176                   ( strncmp( psz_arturl, "file://", 7 ) &&
177                     strncmp( psz_arturl, "attachment://", 13 ) ) ) )
178         {
179             msg_Dbg( obj, "meta ok for %s, need to fetch art",
180                      psz_name ? psz_name : "(null)" );
181             b_fetch = true;
182         }
183         else
184         {
185             msg_Dbg( obj, "no fetch required for %s (art currently %s)",
186                      psz_name ? psz_name : "(null)",
187                      psz_arturl ? psz_arturl : "(null)" );
188         }
189     }
190     vlc_mutex_unlock( &p_item->lock );
191
192     if( b_fetch && p_fetcher )
193         playlist_fetcher_Push( p_fetcher, p_item );
194 }
195
196 /**
197  * This function does the preparsing and issues the art fetching requests
198  */
199 static void *Thread( void *data )
200 {
201     playlist_preparser_t *p_preparser = data;
202     vlc_object_t *obj = p_preparser->object;
203
204     for( ;; )
205     {
206         input_item_t *p_current;
207
208         /* */
209         vlc_mutex_lock( &p_preparser->lock );
210         if( p_preparser->i_waiting > 0 )
211         {
212             p_current = p_preparser->pp_waiting[0];
213             REMOVE_ELEM( p_preparser->pp_waiting, p_preparser->i_waiting, 0 );
214         }
215         else
216         {
217             p_current = NULL;
218             p_preparser->b_live = false;
219             vlc_cond_signal( &p_preparser->wait );
220         }
221         vlc_mutex_unlock( &p_preparser->lock );
222
223         if( !p_current )
224             break;
225
226         Preparse( obj, p_current );
227
228         Art( p_preparser, p_current );
229         vlc_gc_decref(p_current);
230     }
231     return NULL;
232 }
233