]> git.sesse.net Git - vlc/blob - src/playlist/thread.c
fa6534074f6bbf5618bacd723bfe93df34c7105b
[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     // Preparse
69     p_playlist->p_preparse = vlc_object_create( p_playlist,
70                                   sizeof( playlist_preparse_t ) );
71     if( !p_playlist->p_preparse )
72     {
73         msg_Err( p_playlist, "unable to create preparser" );
74         vlc_object_release( p_playlist );
75         return;
76     }
77     p_playlist->p_preparse->i_waiting = 0;
78     p_playlist->p_preparse->pp_waiting = NULL;
79
80     vlc_object_attach( p_playlist->p_preparse, p_playlist );
81     if( vlc_thread_create( p_playlist->p_preparse, "preparser",
82                            RunPreparse, VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
83     {
84         msg_Err( p_playlist, "cannot spawn preparse thread" );
85         vlc_object_detach( p_playlist->p_preparse );
86         vlc_object_release( p_playlist->p_preparse );
87         return;
88     }
89
90     // Secondary Preparse
91     p_playlist->p_fetcher = vlc_object_create( p_playlist,
92                               sizeof( playlist_fetcher_t ) );
93     if( !p_playlist->p_fetcher )
94     {
95         msg_Err( p_playlist, "unable to create secondary preparser" );
96         vlc_object_release( p_playlist );
97         return;
98     }
99     p_playlist->p_fetcher->i_waiting = 0;
100     p_playlist->p_fetcher->p_waiting = NULL;
101     p_playlist->p_fetcher->b_fetch_meta = var_CreateGetInteger( p_playlist,
102                                                                  "fetch-meta" );
103     p_playlist->p_fetcher->i_art_policy = var_CreateGetInteger( p_playlist,
104                                                                 "album-art" );
105
106     vlc_object_attach( p_playlist->p_fetcher, p_playlist );
107     if( vlc_thread_create( p_playlist->p_fetcher,
108                            "fetcher",
109                            RunFetcher,
110                            VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
111     {
112         msg_Err( p_playlist, "cannot spawn secondary preparse thread" );
113         vlc_object_detach( p_playlist->p_fetcher );
114         vlc_object_release( p_playlist->p_fetcher );
115         return;
116     }
117
118     // Start the thread
119     if( vlc_thread_create( p_playlist, "playlist", RunControlThread,
120                            VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
121     {
122         msg_Err( p_playlist, "cannot spawn playlist thread" );
123         vlc_object_release( p_playlist );
124         return;
125     }
126
127     /* The object has been initialized, now attach it */
128     vlc_object_attach( p_playlist, p_parent );
129
130     return;
131 }
132
133 /**
134  * Destroy the playlist global thread.
135  *
136  * Deinits all things controlled by the playlist global thread
137  * \param p_playlist the playlist thread to destroy
138  * \return VLC_SUCCESS or an error
139  */
140 int playlist_ThreadDestroy( playlist_t * p_playlist )
141 {
142     // Tell playlist to go to last loop
143     vlc_object_kill( p_playlist );
144
145     // Kill preparser
146     if( p_playlist->p_preparse )
147     {
148         vlc_object_kill( p_playlist->p_preparse );
149         vlc_thread_join( p_playlist->p_preparse );
150         free( p_playlist->p_preparse->pp_waiting );
151         vlc_object_detach( p_playlist->p_preparse );
152         vlc_object_release( p_playlist->p_preparse );
153     }
154
155     // Kill meta fetcher
156     if( p_playlist->p_fetcher )
157     {
158         vlc_object_kill( p_playlist->p_fetcher );
159         vlc_thread_join( p_playlist->p_fetcher );
160         free( p_playlist->p_fetcher->p_waiting );
161         vlc_object_detach( p_playlist->p_fetcher );
162         vlc_object_release( p_playlist->p_fetcher );
163     }
164
165     // Wait for thread to complete
166     vlc_thread_join( p_playlist );
167
168     // Stats
169     vlc_mutex_destroy( &p_playlist->p_stats->lock );
170     if( p_playlist->p_stats )
171         free( p_playlist->p_stats );
172
173     DestroyInteraction( p_playlist );
174
175     playlist_Destroy( p_playlist );
176
177     return VLC_SUCCESS;
178 }
179
180 /**
181  * Run the main control thread itself
182  */
183 static void RunControlThread ( playlist_t *p_playlist )
184 {
185     int i_loops = 0;
186
187     /* Tell above that we're ready */
188     vlc_thread_ready( p_playlist );
189     while( !p_playlist->b_die )
190     {
191         i_loops++;
192
193         if( p_playlist->p_interaction )
194             intf_InteractionManage( p_playlist );
195
196         playlist_MainLoop( p_playlist );
197         if( p_playlist->b_cant_sleep )
198         {
199             /* 100 ms is an acceptable delay for playlist operations */
200             msleep( INTF_IDLE_SLEEP*2 );
201         }
202         else
203         {
204             PL_LOCK;
205             vlc_cond_wait( &p_playlist->object_wait, &p_playlist->object_lock );
206             PL_UNLOCK;
207         }
208     }
209     playlist_LastLoop( p_playlist );
210 }
211
212 /*****************************************************************************
213  * Preparse-specific functions
214  *****************************************************************************/
215 static void RunPreparse ( playlist_preparse_t *p_obj )
216 {
217     /* Tell above that we're ready */
218     vlc_thread_ready( p_obj );
219     playlist_PreparseLoop( p_obj );
220 }
221
222 static void RunFetcher( playlist_fetcher_t *p_obj )
223 {
224     /* Tell above that we're ready */
225     vlc_thread_ready( p_obj );
226     playlist_FetcherLoop( p_obj );
227 }
228
229 /*****************************************************************************
230  * Interaction functions
231  *****************************************************************************/
232 static void DestroyInteraction( playlist_t *p_playlist )
233 {
234     if( p_playlist->p_interaction )
235     {
236         intf_InteractionDestroy( p_playlist->p_interaction );
237         p_playlist->p_interaction = NULL;
238     }
239 }