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