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 * );
36 static void RunSecondaryPreparse( playlist_secondary_preparse_t * );
38 static playlist_t * CreatePlaylist( vlc_object_t *p_parent );
39 static void EndPlaylist( playlist_t * );
40 static void DestroyPlaylist( playlist_t * );
42 static void DestroyInteraction( playlist_t * );
44 /*****************************************************************************
45 * Main functions for the global thread
46 *****************************************************************************/
49 * Create the main playlist thread
50 * Additionally to the playlist, this thread controls :
55 * \return an object with a started thread
57 void __playlist_ThreadCreate( vlc_object_t *p_parent )
59 playlist_t *p_playlist;
60 p_playlist = CreatePlaylist( p_parent );
62 if( !p_playlist ) return;
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;
70 p_playlist->p_interaction = NULL;
73 p_playlist->p_preparse = vlc_object_create( p_playlist,
74 sizeof( playlist_preparse_t ) );
75 if( !p_playlist->p_preparse )
77 msg_Err( p_playlist, "unable to create preparser" );
78 vlc_object_destroy( p_playlist );
81 p_playlist->p_preparse->i_waiting = 0;
82 p_playlist->p_preparse->pp_waiting = NULL;
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 ) )
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 );
95 p_playlist->p_secondary_preparse = vlc_object_create( p_playlist,
96 sizeof( playlist_secondary_preparse_t ) );
97 if( !p_playlist->p_secondary_preparse )
99 msg_Err( p_playlist, "unable to create secondary preparser" );
100 vlc_object_destroy( p_playlist );
103 p_playlist->p_secondary_preparse->i_waiting = 0;
104 p_playlist->p_secondary_preparse->p_waiting = NULL;
106 vlc_object_attach( p_playlist->p_secondary_preparse, p_playlist );
107 if( vlc_thread_create( p_playlist->p_secondary_preparse,
108 "secondary preparser",
109 RunSecondaryPreparse,
110 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
112 msg_Err( p_playlist, "cannot spawn secondary preparse thread" );
113 vlc_object_detach( p_playlist->p_secondary_preparse );
114 vlc_object_destroy( p_playlist->p_secondary_preparse );
119 if( vlc_thread_create( p_playlist, "playlist", RunControlThread,
120 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
122 msg_Err( p_playlist, "cannot spawn playlist thread" );
123 vlc_object_destroy( p_playlist );
127 /* The object has been initialized, now attach it */
128 vlc_object_attach( p_playlist, p_parent );
134 * Destroy the playlist global thread.
136 * Deinits all things controlled by the playlist global thread
137 * \param p_playlist the playlist thread to destroy
138 * \return VLC_SUCCESS or an error
140 int playlist_ThreadDestroy( playlist_t * p_playlist )
142 p_playlist->b_die = VLC_TRUE;
143 playlist_Signal( p_playlist );
144 if( p_playlist->p_preparse )
146 vlc_cond_signal( &p_playlist->p_preparse->object_wait );
147 free( p_playlist->p_preparse->pp_waiting );
149 if( p_playlist->p_secondary_preparse )
151 vlc_cond_signal( &p_playlist->p_secondary_preparse->object_wait );
152 free( p_playlist->p_secondary_preparse->p_waiting );
155 DestroyInteraction( p_playlist );
156 DestroyPlaylist( p_playlist );
162 * Run the main control thread itself
164 static void RunControlThread ( playlist_t *p_playlist )
168 /* Tell above that we're ready */
169 vlc_thread_ready( p_playlist );
171 while( !p_playlist->b_die )
175 if( p_playlist->p_interaction )
176 intf_InteractionManage( p_playlist );
178 playlist_MainLoop( p_playlist );
179 if( p_playlist->b_cant_sleep )
181 /* 100 ms is an acceptable delay for playlist operations */
182 msleep( INTF_IDLE_SLEEP*2 );
187 vlc_cond_wait( &p_playlist->object_wait, &p_playlist->object_lock );
192 EndPlaylist( p_playlist );
196 /*****************************************************************************
197 * Playlist-specific functions
198 *****************************************************************************/
199 static playlist_t * CreatePlaylist( vlc_object_t *p_parent )
201 return playlist_Create( p_parent );
204 static void DestroyPlaylist( playlist_t *p_playlist )
206 playlist_Destroy( p_playlist );
209 static void EndPlaylist( playlist_t *p_playlist )
211 playlist_LastLoop( p_playlist );
214 /*****************************************************************************
215 * Preparse-specific functions
216 *****************************************************************************/
217 static void RunPreparse ( playlist_preparse_t *p_obj )
219 /* Tell above that we're ready */
220 vlc_thread_ready( p_obj );
221 playlist_PreparseLoop( p_obj );
224 static void RunSecondaryPreparse( playlist_secondary_preparse_t *p_obj )
226 /* Tell above that we're ready */
227 vlc_thread_ready( p_obj );
228 playlist_SecondaryPreparseLoop( p_obj );
231 /*****************************************************************************
232 * Interaction functions
233 *****************************************************************************/
234 static void DestroyInteraction( playlist_t *p_playlist )
236 if( p_playlist->p_interaction )
238 intf_InteractionDestroy( p_playlist->p_interaction );
239 fprintf( stderr, "NOW NULL ****\n" );
240 p_playlist->p_interaction = NULL;