]> git.sesse.net Git - vlc/blob - src/playlist/thread.c
playlist: Add an option to disable meta-fetch. (Need to be merged with art-fetch).
[vlc] / src / playlist / thread.c
1 /*****************************************************************************
2  * playlist.c : Playlist management functions
3  *****************************************************************************
4  * Copyright (C) 1999-2004 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 #include <vlc/vlc.h>
25 #include <vlc_es.h>
26 #include <vlc_input.h>
27 #include <vlc_interface.h>
28 #include <vlc_playlist.h>
29 #include "playlist_internal.h"
30 #include "interface/interface.h"
31
32 /*****************************************************************************
33  * Local prototypes
34  *****************************************************************************/
35 static void RunControlThread ( playlist_t * );
36 static void RunPreparse( playlist_preparse_t * );
37 static void RunFetcher( playlist_fetcher_t * );
38
39 static void DestroyInteraction( playlist_t * );
40
41 /*****************************************************************************
42  * Main functions for the global thread
43  *****************************************************************************/
44
45 /**
46  * Create the main playlist thread
47  * Additionally to the playlist, this thread controls :
48  *    - Interaction
49  *    - Statistics
50  *    - VLM
51  * \param p_parent
52  * \return an object with a started thread
53  */
54 void __playlist_ThreadCreate( vlc_object_t *p_parent )
55 {
56     playlist_t *p_playlist = playlist_Create( p_parent );
57     if( !p_playlist ) return;
58
59     // Stats
60     p_playlist->p_stats = (global_stats_t *)malloc( sizeof( global_stats_t ) );
61     vlc_mutex_init( p_playlist, &p_playlist->p_stats->lock );
62     p_playlist->p_stats_computer = NULL;
63
64     // Interaction
65     p_playlist->p_interaction = NULL;
66
67     // Preparse
68     p_playlist->p_preparse = vlc_object_create( p_playlist,
69                                   sizeof( playlist_preparse_t ) );
70     if( !p_playlist->p_preparse )
71     {
72         msg_Err( p_playlist, "unable to create preparser" );
73         vlc_object_destroy( p_playlist );
74         return;
75     }
76     p_playlist->p_preparse->i_waiting = 0;
77     p_playlist->p_preparse->pp_waiting = NULL;
78
79     vlc_object_attach( p_playlist->p_preparse, p_playlist );
80     if( vlc_thread_create( p_playlist->p_preparse, "preparser",
81                            RunPreparse, VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
82     {
83         msg_Err( p_playlist, "cannot spawn preparse thread" );
84         vlc_object_detach( p_playlist->p_preparse );
85         vlc_object_destroy( p_playlist->p_preparse );
86         return;
87     }
88
89     // Secondary Preparse
90     p_playlist->p_fetcher = vlc_object_create( p_playlist,
91                               sizeof( playlist_fetcher_t ) );
92     if( !p_playlist->p_fetcher )
93     {
94         msg_Err( p_playlist, "unable to create secondary preparser" );
95         vlc_object_destroy( p_playlist );
96         return;
97     }
98     p_playlist->p_fetcher->i_waiting = 0;
99     p_playlist->p_fetcher->p_waiting = NULL;
100     p_playlist->p_fetcher->b_fetch_meta = var_CreateGetInteger( p_playlist,
101                                                                  "meta-fetch" );
102     p_playlist->p_fetcher->i_art_policy = var_CreateGetInteger( p_playlist,
103                                                                 "album-art" );
104
105     vlc_object_attach( p_playlist->p_fetcher, p_playlist );
106     if( vlc_thread_create( p_playlist->p_fetcher,
107                            "fetcher",
108                            RunFetcher,
109                            VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
110     {
111         msg_Err( p_playlist, "cannot spawn secondary preparse thread" );
112         vlc_object_detach( p_playlist->p_fetcher );
113         vlc_object_destroy( p_playlist->p_fetcher );
114         return;
115     }
116
117     // Start the thread
118     if( vlc_thread_create( p_playlist, "playlist", RunControlThread,
119                            VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
120     {
121         msg_Err( p_playlist, "cannot spawn playlist thread" );
122         vlc_object_destroy( p_playlist );
123         return;
124     }
125
126     /* The object has been initialized, now attach it */
127     vlc_object_attach( p_playlist, p_parent );
128
129     return;
130 }
131
132 /**
133  * Destroy the playlist global thread.
134  *
135  * Deinits all things controlled by the playlist global thread
136  * \param p_playlist the playlist thread to destroy
137  * \return VLC_SUCCESS or an error
138  */
139 int playlist_ThreadDestroy( playlist_t * p_playlist )
140 {
141     // Tell playlist to go to last loop
142     vlc_object_kill( p_playlist );
143
144     // Kill preparser
145     if( p_playlist->p_preparse )
146     {
147         vlc_object_kill( p_playlist->p_preparse );
148         vlc_thread_join( p_playlist->p_preparse );
149         free( p_playlist->p_preparse->pp_waiting );
150         vlc_object_detach( p_playlist->p_preparse );
151         vlc_object_destroy( p_playlist->p_preparse );
152     }
153
154     // Kill meta fetcher
155     if( p_playlist->p_fetcher )
156     {
157         vlc_object_kill( p_playlist->p_fetcher );
158         vlc_thread_join( p_playlist->p_fetcher );
159         free( p_playlist->p_fetcher->p_waiting );
160         vlc_object_detach( p_playlist->p_fetcher );
161         vlc_object_destroy( p_playlist->p_fetcher );
162     }
163
164     // Wait for thread to complete
165     vlc_thread_join( p_playlist );
166
167     // Stats
168     vlc_mutex_destroy( &p_playlist->p_stats->lock );
169     if( p_playlist->p_stats )
170         free( p_playlist->p_stats );
171
172     DestroyInteraction( p_playlist );
173
174     playlist_Destroy( p_playlist );
175
176     return VLC_SUCCESS;
177 }
178
179 /**
180  * Run the main control thread itself
181  */
182 static void RunControlThread ( playlist_t *p_playlist )
183 {
184     int i_loops = 0;
185
186     /* Tell above that we're ready */
187     vlc_thread_ready( p_playlist );
188     while( !p_playlist->b_die )
189     {
190         i_loops++;
191
192         if( p_playlist->p_interaction )
193             intf_InteractionManage( p_playlist );
194
195         playlist_MainLoop( p_playlist );
196         if( p_playlist->b_cant_sleep )
197         {
198             /* 100 ms is an acceptable delay for playlist operations */
199             msleep( INTF_IDLE_SLEEP*2 );
200         }
201         else
202         {
203             PL_LOCK;
204             vlc_cond_wait( &p_playlist->object_wait, &p_playlist->object_lock );
205             PL_UNLOCK;
206         }
207     }
208     playlist_LastLoop( p_playlist );
209 }
210
211 /*****************************************************************************
212  * Preparse-specific functions
213  *****************************************************************************/
214 static void RunPreparse ( playlist_preparse_t *p_obj )
215 {
216     /* Tell above that we're ready */
217     vlc_thread_ready( p_obj );
218     playlist_PreparseLoop( p_obj );
219 }
220
221 static void RunFetcher( playlist_fetcher_t *p_obj )
222 {
223     /* Tell above that we're ready */
224     vlc_thread_ready( p_obj );
225     playlist_FetcherLoop( p_obj );
226 }
227
228 /*****************************************************************************
229  * Interaction functions
230  *****************************************************************************/
231 static void DestroyInteraction( playlist_t *p_playlist )
232 {
233     if( p_playlist->p_interaction )
234     {
235         intf_InteractionDestroy( p_playlist->p_interaction );
236         p_playlist->p_interaction = NULL;
237     }
238 }