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