]> git.sesse.net Git - vlc/blob - src/playlist/thread.c
725751a124987e3815ae4bcc8c1445d747fa56b6
[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_playlist.h"
28 #include "vlc_interaction.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 RunSecondaryPreparse( playlist_secondary_preparse_t * );
37
38 static playlist_t * CreatePlaylist( vlc_object_t *p_parent );
39 static void HandlePlaylist( playlist_t * );
40 static void EndPlaylist( playlist_t * );
41 static void DestroyPlaylist( playlist_t * );
42
43 static void HandleInteraction( playlist_t * );
44 static void DestroyInteraction( playlist_t * );
45
46 /*****************************************************************************
47  * Main functions for the global thread
48  *****************************************************************************/
49
50 /**
51  * Create the main playlist thread
52  * Additionally to the playlist, this thread controls :
53  *    - Interaction
54  *    - Statistics
55  *    - VLM
56  * \param p_parent
57  * \return an object with a started thread
58  */
59 void __playlist_ThreadCreate( vlc_object_t *p_parent )
60 {
61     playlist_t *p_playlist;
62     p_playlist = CreatePlaylist( p_parent );
63
64     if( !p_playlist ) return;
65
66     // Stats
67     p_playlist->p_stats = (global_stats_t *)malloc( sizeof( global_stats_t ) );
68     vlc_mutex_init( p_playlist, &p_playlist->p_stats->lock );
69     p_playlist->p_stats_computer = NULL;
70
71     // Interaction
72     p_playlist->p_interaction = NULL;
73
74     // Preparse
75     p_playlist->p_preparse = vlc_object_create( p_playlist,
76                                   sizeof( playlist_preparse_t ) );
77     if( !p_playlist->p_preparse )
78     {
79         msg_Err( p_playlist, "unable to create preparser" );
80         vlc_object_destroy( p_playlist );
81         return;
82     }
83     p_playlist->p_preparse->i_waiting = 0;
84     p_playlist->p_preparse->pp_waiting = NULL;
85
86     vlc_object_attach( p_playlist->p_preparse, p_playlist );
87     if( vlc_thread_create( p_playlist->p_preparse, "preparser",
88                            RunPreparse, VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
89     {
90         msg_Err( p_playlist, "cannot spawn preparse thread" );
91         vlc_object_detach( p_playlist->p_preparse );
92         vlc_object_destroy( p_playlist->p_preparse );
93         return;
94     }
95
96     // Secondary Preparse
97     p_playlist->p_secondary_preparse = vlc_object_create( p_playlist,
98                               sizeof( playlist_secondary_preparse_t ) );
99     if( !p_playlist->p_secondary_preparse )
100     {
101         msg_Err( p_playlist, "unable to create secondary preparser" );
102         vlc_object_destroy( p_playlist );
103         return;
104     }
105     p_playlist->p_secondary_preparse->i_waiting = 0;
106     p_playlist->p_secondary_preparse->p_waiting = NULL;
107
108     vlc_object_attach( p_playlist->p_secondary_preparse, p_playlist );
109     if( vlc_thread_create( p_playlist->p_secondary_preparse,
110                            "secondary preparser",
111                            RunSecondaryPreparse,
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_secondary_preparse );
116         vlc_object_destroy( p_playlist->p_secondary_preparse );
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     if( p_playlist->p_preparse )
146     {
147         vlc_cond_signal( &p_playlist->p_preparse->object_wait );
148         free( p_playlist->p_preparse->pp_waiting );
149     }
150     if( p_playlist->p_secondary_preparse )
151     {
152         vlc_cond_signal( &p_playlist->p_secondary_preparse->object_wait );
153         free( p_playlist->p_secondary_preparse->p_waiting );
154     }
155
156     DestroyInteraction( p_playlist );
157     DestroyPlaylist( p_playlist );
158
159     return VLC_SUCCESS;
160 }
161
162 /**
163  * Run the main control thread itself
164  */
165 static void RunControlThread ( playlist_t *p_playlist )
166 {
167    int i_loops = 0;
168
169    /* Tell above that we're ready */
170    vlc_thread_ready( p_playlist );
171
172     while( !p_playlist->b_die )
173     {
174         i_loops++;
175
176         HandleInteraction( p_playlist );
177         HandlePlaylist( p_playlist );
178
179         /* 100 ms is an acceptable delay for playlist operations */
180         msleep( INTF_IDLE_SLEEP*2 );
181     }
182
183     EndPlaylist( p_playlist );
184 }
185
186
187 /*****************************************************************************
188  * Playlist-specific functions
189  *****************************************************************************/
190 static playlist_t * CreatePlaylist( vlc_object_t *p_parent )
191 {
192     return playlist_Create( p_parent );
193 }
194
195 static void DestroyPlaylist( playlist_t *p_playlist )
196 {
197     playlist_Destroy( p_playlist );
198 }
199
200 static void HandlePlaylist( playlist_t *p_playlist )
201 {
202     playlist_MainLoop( p_playlist );
203 }
204
205 static void EndPlaylist( playlist_t *p_playlist )
206 {
207     playlist_LastLoop( p_playlist );
208 }
209
210 /*****************************************************************************
211  * Preparse-specific functions
212  *****************************************************************************/
213 static void RunPreparse ( playlist_preparse_t *p_obj )
214 {
215     /* Tell above that we're ready */
216     vlc_thread_ready( p_obj );
217     playlist_PreparseLoop( p_obj );
218 }
219
220 static void RunSecondaryPreparse( playlist_secondary_preparse_t *p_obj )
221 {
222     /* Tell above that we're ready */
223     vlc_thread_ready( p_obj );
224     playlist_SecondaryPreparseLoop( p_obj );
225 }
226
227 /*****************************************************************************
228  * Interaction functions
229  *****************************************************************************/
230 static void DestroyInteraction( playlist_t *p_playlist )
231 {
232     if( p_playlist->p_interaction )
233     {
234         intf_InteractionDestroy( p_playlist->p_interaction );
235         fprintf( stderr, "NOW NULL ****\n" );
236         p_playlist->p_interaction = NULL;
237     }
238 }
239
240 static void HandleInteraction( playlist_t *p_playlist )
241 {
242     if( p_playlist->p_interaction )
243     {
244         stats_TimerStart( p_playlist, "Interaction thread",
245                           STATS_TIMER_INTERACTION );
246         intf_InteractionManage( p_playlist );
247         stats_TimerStop( p_playlist, STATS_TIMER_INTERACTION );
248     }
249 }