1 /*****************************************************************************
2 * playlist.c : Playlist management functions
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
26 #include <vlc_input.h>
27 #include "vlc_playlist.h"
28 #include "vlc_interaction.h"
29 #include "playlist_internal.h"
31 /*****************************************************************************
33 *****************************************************************************/
34 static void RunControlThread ( playlist_t * );
35 static void RunPreparse( playlist_preparse_t * );
37 static playlist_t * CreatePlaylist( vlc_object_t *p_parent );
38 static void HandlePlaylist( playlist_t * );
39 static void EndPlaylist( playlist_t * );
40 static void DestroyPlaylist( playlist_t * );
42 static void HandleStats( playlist_t *, int );
44 static void HandleInteraction( playlist_t * );
45 static void DestroyInteraction( playlist_t * );
47 /*****************************************************************************
48 * Main functions for the global thread
49 *****************************************************************************/
52 * Create the main playlist thread
53 * Additionally to the playlist, this thread controls :
58 * \return an object with a started thread
60 playlist_t * __playlist_ThreadCreate( vlc_object_t *p_parent )
62 playlist_t *p_playlist;
63 p_playlist = CreatePlaylist( p_parent );
65 if( !p_playlist ) return NULL;
68 p_playlist->p_stats = (global_stats_t *)malloc( sizeof( global_stats_t ) );
69 vlc_mutex_init( p_playlist, &p_playlist->p_stats->lock );
72 p_playlist->p_interaction = NULL;
75 p_playlist->p_preparse = vlc_object_create( p_playlist,
76 sizeof( playlist_preparse_t ) );
77 if( !p_playlist->p_preparse )
79 msg_Err( p_playlist, "unable to create preparser" );
80 vlc_object_destroy( p_playlist );
83 p_playlist->p_preparse->i_waiting = 0;
84 p_playlist->p_preparse->pp_waiting = NULL;
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 ) )
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 );
97 if( vlc_thread_create( p_playlist, "playlist", RunControlThread,
98 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
100 msg_Err( p_playlist, "cannot spawn playlist thread" );
101 vlc_object_destroy( p_playlist );
105 /* The object has been initialized, now attach it */
106 vlc_object_attach( p_playlist, p_parent );
112 * Destroy the playlist global thread.
114 * Deinits all things controlled by the playlist global thread
115 * \param p_playlist the playlist thread to destroy
116 * \return VLC_SUCCESS or an error
118 int playlist_ThreadDestroy( playlist_t * p_playlist )
120 p_playlist->b_die = 1;
122 DestroyInteraction( p_playlist );
123 DestroyPlaylist( p_playlist );
129 * Run the main control thread itself
131 static void RunControlThread ( playlist_t *p_playlist )
135 /* Tell above that we're ready */
136 vlc_thread_ready( p_playlist );
138 while( !p_playlist->b_die )
142 HandleInteraction( p_playlist );
143 HandleStats( p_playlist, i_loops );
145 HandlePlaylist( p_playlist );
147 msleep( INTF_IDLE_SLEEP / 2 );
149 /* Stop sleeping earlier if we have work */
150 /* TODO : statistics about this */
151 if ( p_playlist->request.b_request &&
152 p_playlist->status.i_status == PLAYLIST_RUNNING )
157 msleep( INTF_IDLE_SLEEP / 2 );
160 EndPlaylist( p_playlist );
164 /*****************************************************************************
165 * Playlist-specific functions
166 *****************************************************************************/
167 static playlist_t * CreatePlaylist( vlc_object_t *p_parent )
169 return playlist_Create( p_parent );
172 static void DestroyPlaylist( playlist_t *p_playlist )
174 playlist_Destroy( p_playlist );
177 static void HandlePlaylist( playlist_t *p_playlist )
179 playlist_MainLoop( p_playlist );
182 static void EndPlaylist( playlist_t *p_playlist )
184 playlist_LastLoop( p_playlist );
187 /*****************************************************************************
188 * Preparse-specific functions
189 *****************************************************************************/
190 static void RunPreparse ( playlist_preparse_t *p_obj )
192 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
193 /* Tell above that we're ready */
194 vlc_thread_ready( p_obj );
196 while( !p_playlist->b_die )
198 playlist_PreparseLoop( p_obj );
199 if( p_obj->i_waiting == 0 )
201 msleep( INTF_IDLE_SLEEP );
206 /*****************************************************************************
207 * Interaction functions
208 *****************************************************************************/
209 static void DestroyInteraction( playlist_t *p_playlist )
211 if( p_playlist->p_interaction )
213 intf_InteractionDestroy( p_playlist->p_interaction );
214 fprintf( stderr, "NOW NULL ****\n" );
215 p_playlist->p_interaction = NULL;
219 static void HandleInteraction( playlist_t *p_playlist )
221 if( p_playlist->p_interaction )
223 stats_TimerStart( p_playlist, "Interaction thread",
224 STATS_TIMER_INTERACTION );
225 intf_InteractionManage( p_playlist );
226 stats_TimerStop( p_playlist, STATS_TIMER_INTERACTION );
231 /*****************************************************************************
233 *****************************************************************************/
234 static void HandleStats( playlist_t *p_playlist, int i_loops )
236 if( i_loops %5 == 0 && p_playlist->p_stats )
238 stats_ComputeGlobalStats( p_playlist, p_playlist->p_stats );
239 if( p_playlist->p_input )
241 stats_ComputeInputStats( p_playlist->p_input,
242 p_playlist->p_input->input.p_item->p_stats );