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