Compile fixes, round 1
[vlc.git] / 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 #include "interface/interface.h"
31
32 /*****************************************************************************
33  * Local prototypes
34  *****************************************************************************/
35 static void RunControlThread ( playlist_t * );
36 static void RunPreparse( playlist_preparse_t * );
37 static void RunFetcher( playlist_fetcher_t * );
38
39 static playlist_t * CreatePlaylist( vlc_object_t *p_parent );
40 static void EndPlaylist( playlist_t * );
41 static void DestroyPlaylist( playlist_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;
61     p_playlist = CreatePlaylist( p_parent );
62
63     if( !p_playlist ) return;
64
65     // Stats
66     p_playlist->p_stats = (global_stats_t *)malloc( sizeof( global_stats_t ) );
67     vlc_mutex_init( p_playlist, &p_playlist->p_stats->lock );
68     p_playlist->p_stats_computer = NULL;
69
70     // Interaction
71     p_playlist->p_interaction = NULL;
72
73     // Preparse
74     p_playlist->p_preparse = vlc_object_create( p_playlist,
75                                   sizeof( playlist_preparse_t ) );
76     if( !p_playlist->p_preparse )
77     {
78         msg_Err( p_playlist, "unable to create preparser" );
79         vlc_object_destroy( p_playlist );
80         return;
81     }
82     p_playlist->p_preparse->i_waiting = 0;
83     p_playlist->p_preparse->pp_waiting = NULL;
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_detach( p_playlist->p_preparse );
91         vlc_object_destroy( p_playlist->p_preparse );
92         return;
93     }
94
95     // Secondary Preparse
96     p_playlist->p_fetcher = vlc_object_create( p_playlist,
97                               sizeof( playlist_fetcher_t ) );
98     if( !p_playlist->p_fetcher )
99     {
100         msg_Err( p_playlist, "unable to create secondary preparser" );
101         vlc_object_destroy( p_playlist );
102         return;
103     }
104     p_playlist->p_fetcher->i_waiting = 0;
105     p_playlist->p_fetcher->p_waiting = NULL;
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     p_playlist->b_die = VLC_TRUE;
146     playlist_Signal( p_playlist );
147     if( p_playlist->p_preparse )
148     {
149         vlc_cond_signal( &p_playlist->p_preparse->object_wait );
150         free( p_playlist->p_preparse->pp_waiting );
151     }
152     if( p_playlist->p_fetcher )
153     {
154         vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
155         free( p_playlist->p_fetcher->p_waiting );
156     }
157
158     DestroyInteraction( p_playlist );
159     DestroyPlaylist( p_playlist );
160
161     return VLC_SUCCESS;
162 }
163
164 /**
165  * Run the main control thread itself
166  */
167 static void RunControlThread ( playlist_t *p_playlist )
168 {
169    int i_loops = 0;
170
171    /* Tell above that we're ready */
172    vlc_thread_ready( p_playlist );
173
174     while( !p_playlist->b_die )
175     {
176         i_loops++;
177
178         if( p_playlist->p_interaction )
179             intf_InteractionManage( p_playlist );
180
181         playlist_MainLoop( p_playlist );
182         if( p_playlist->b_cant_sleep )
183         {
184             /* 100 ms is an acceptable delay for playlist operations */
185             msleep( INTF_IDLE_SLEEP*2 );
186         }
187         else
188         {
189             PL_LOCK;
190             vlc_cond_wait( &p_playlist->object_wait, &p_playlist->object_lock );
191             PL_UNLOCK;
192         }
193     }
194
195     EndPlaylist( p_playlist );
196 }
197
198
199 /*****************************************************************************
200  * Playlist-specific functions
201  *****************************************************************************/
202 static playlist_t * CreatePlaylist( vlc_object_t *p_parent )
203 {
204     return playlist_Create( p_parent );
205 }
206
207 static void DestroyPlaylist( playlist_t *p_playlist )
208 {
209     playlist_Destroy( p_playlist );
210 }
211
212 static void EndPlaylist( playlist_t *p_playlist )
213 {
214     playlist_LastLoop( p_playlist );
215 }
216
217 /*****************************************************************************
218  * Preparse-specific functions
219  *****************************************************************************/
220 static void RunPreparse ( playlist_preparse_t *p_obj )
221 {
222     /* Tell above that we're ready */
223     vlc_thread_ready( p_obj );
224     playlist_PreparseLoop( p_obj );
225 }
226
227 static void RunFetcher( playlist_fetcher_t *p_obj )
228 {
229     /* Tell above that we're ready */
230     vlc_thread_ready( p_obj );
231     playlist_FetcherLoop( p_obj );
232 }
233
234 /*****************************************************************************
235  * Interaction functions
236  *****************************************************************************/
237 static void DestroyInteraction( playlist_t *p_playlist )
238 {
239     if( p_playlist->p_interaction )
240     {
241         intf_InteractionDestroy( p_playlist->p_interaction );
242         fprintf( stderr, "NOW NULL ****\n" );
243         p_playlist->p_interaction = NULL;
244     }
245 }