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