b49b8761dcdd6be21323cbc2bef1127ea7342d81
[vlc.git] / src / playlist / thread.c
1 /*****************************************************************************
2  * thread.c : Playlist management functions
3  *****************************************************************************
4  * Copyright © 1999-2008 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
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 static void RunControlThread ( playlist_t * );
39 static void RunPreparse( playlist_preparse_t * );
40 static void RunFetcher( playlist_fetcher_t * );
41
42 /*****************************************************************************
43  * Main functions for the global thread
44  *****************************************************************************/
45
46 /**
47  * Create the main playlist thread
48  * Additionally to the playlist, this thread controls :
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     if( !p_playlist->p_stats )
62     {
63         vlc_object_release( p_playlist );
64         return;
65     }
66     vlc_mutex_init( p_playlist, &p_playlist->p_stats->lock );
67     p_playlist->p_stats_computer = NULL;
68
69     // Preparse
70     p_playlist->p_preparse = vlc_object_create( p_playlist,
71                                   sizeof( playlist_preparse_t ) );
72     if( !p_playlist->p_preparse )
73     {
74         msg_Err( p_playlist, "unable to create preparser" );
75         vlc_object_release( p_playlist );
76         return;
77     }
78     p_playlist->p_preparse->i_waiting = 0;
79     p_playlist->p_preparse->pp_waiting = NULL;
80
81     vlc_object_attach( p_playlist->p_preparse, p_playlist );
82     if( vlc_thread_create( p_playlist->p_preparse, "preparser",
83                            RunPreparse, VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
84     {
85         msg_Err( p_playlist, "cannot spawn preparse thread" );
86         vlc_object_detach( p_playlist->p_preparse );
87         vlc_object_release( p_playlist->p_preparse );
88         return;
89     }
90
91     // Secondary Preparse
92     p_playlist->p_fetcher = vlc_object_create( p_playlist,
93                               sizeof( playlist_fetcher_t ) );
94     if( !p_playlist->p_fetcher )
95     {
96         msg_Err( p_playlist, "unable to create secondary preparser" );
97         vlc_object_release( p_playlist );
98         return;
99     }
100     p_playlist->p_fetcher->i_waiting = 0;
101     p_playlist->p_fetcher->p_waiting = NULL;
102     p_playlist->p_fetcher->b_fetch_meta = var_CreateGetInteger( p_playlist,
103                                                                  "fetch-meta" );
104     p_playlist->p_fetcher->i_art_policy = var_CreateGetInteger( p_playlist,
105                                                                 "album-art" );
106
107     vlc_object_attach( p_playlist->p_fetcher, p_playlist );
108     if( vlc_thread_create( p_playlist->p_fetcher,
109                            "fetcher",
110                            RunFetcher,
111                            VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
112     {
113         msg_Err( p_playlist, "cannot spawn secondary preparse thread" );
114         vlc_object_detach( p_playlist->p_fetcher );
115         vlc_object_release( p_playlist->p_fetcher );
116         return;
117     }
118
119     // Start the thread
120     if( vlc_thread_create( p_playlist, "playlist", RunControlThread,
121                            VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
122     {
123         msg_Err( p_playlist, "cannot spawn playlist thread" );
124         vlc_object_release( p_playlist );
125         return;
126     }
127
128     /* The object has been initialized, now attach it */
129     vlc_object_attach( p_playlist, p_parent );
130
131     return;
132 }
133
134 /**
135  * Destroy the playlist global thread.
136  *
137  * Deinits all things controlled by the playlist global thread
138  * \param p_playlist the playlist thread to destroy
139  * \return VLC_SUCCESS or an error
140  */
141 int playlist_ThreadDestroy( playlist_t * p_playlist )
142 {
143     // Tell playlist to go to last loop
144     vlc_object_kill( p_playlist );
145
146     // Kill preparser
147     if( p_playlist->p_preparse )
148     {
149         vlc_object_kill( p_playlist->p_preparse );
150         vlc_thread_join( p_playlist->p_preparse );
151         free( p_playlist->p_preparse->pp_waiting );
152         vlc_object_detach( p_playlist->p_preparse );
153         vlc_object_release( p_playlist->p_preparse );
154     }
155
156     // Kill meta fetcher
157     if( p_playlist->p_fetcher )
158     {
159         vlc_object_kill( p_playlist->p_fetcher );
160         vlc_thread_join( p_playlist->p_fetcher );
161         free( p_playlist->p_fetcher->p_waiting );
162         vlc_object_detach( p_playlist->p_fetcher );
163         vlc_object_release( p_playlist->p_fetcher );
164     }
165
166     // Wait for thread to complete
167     vlc_thread_join( p_playlist );
168
169     // Stats
170     vlc_mutex_destroy( &p_playlist->p_stats->lock );
171     if( p_playlist->p_stats )
172         free( p_playlist->p_stats );
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         playlist_MainLoop( p_playlist );
193         if( p_playlist->b_cant_sleep )
194         {
195             /* 100 ms is an acceptable delay for playlist operations */
196             msleep( INTF_IDLE_SLEEP*2 );
197         }
198         else
199         {
200             PL_LOCK;
201             vlc_cond_wait( &p_playlist->object_wait, &p_playlist->object_lock );
202             PL_UNLOCK;
203         }
204     }
205     playlist_LastLoop( p_playlist );
206 }
207
208 /*****************************************************************************
209  * Preparse-specific functions
210  *****************************************************************************/
211 static void RunPreparse ( playlist_preparse_t *p_obj )
212 {
213     /* Tell above that we're ready */
214     vlc_thread_ready( p_obj );
215     playlist_PreparseLoop( p_obj );
216 }
217
218 static void RunFetcher( playlist_fetcher_t *p_obj )
219 {
220     /* Tell above that we're ready */
221     vlc_thread_ready( p_obj );
222     playlist_FetcherLoop( p_obj );
223 }