1 /*****************************************************************************
2 * playlist.c : Playlist management functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: playlist.c,v 1.5 2002/06/04 00:11:12 sam Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
23 #include <stdlib.h> /* free(), strtol() */
24 #include <stdio.h> /* sprintf() */
25 #include <string.h> /* strerror() */
26 #include <errno.h> /* ENOMEM */
30 #include "stream_control.h"
31 #include "input_ext-intf.h"
35 #define PLAYLIST_STOPPED 0
36 #define PLAYLIST_RUNNING 1
38 /*****************************************************************************
40 *****************************************************************************/
41 static void RunThread ( playlist_t * );
43 /*****************************************************************************
44 * playlist_Create: create playlist
45 *****************************************************************************
46 * Create a playlist structure.
47 *****************************************************************************/
48 playlist_t * __playlist_Create ( vlc_object_t *p_parent )
50 playlist_t *p_playlist;
52 /* Allocate structure */
53 p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
56 msg_Err( p_parent, "out of memory" );
60 p_playlist->p_input = NULL;
61 p_playlist->i_status = PLAYLIST_RUNNING;
62 p_playlist->i_index = -1;
63 p_playlist->i_size = 0;
64 p_playlist->pp_items = NULL;
65 vlc_mutex_init( p_playlist, &p_playlist->change_lock );
67 vlc_object_attach( p_playlist, p_parent );
69 if( vlc_thread_create( p_playlist, "playlist", RunThread, 0 ) )
71 msg_Err( p_playlist, "cannot spawn playlist thread" );
72 vlc_object_detach_all( p_playlist );
73 vlc_mutex_destroy( &p_playlist->change_lock );
74 vlc_object_destroy( p_playlist );
81 /*****************************************************************************
82 * playlist_Destroy: destroy the playlist
83 *****************************************************************************
84 * Delete all items in the playlist and free the playlist structure.
85 *****************************************************************************/
86 void playlist_Destroy( playlist_t * p_playlist )
88 p_playlist->b_die = 1;
90 vlc_thread_join( p_playlist );
92 vlc_mutex_destroy( &p_playlist->change_lock );
93 vlc_object_destroy( p_playlist );
96 /*****************************************************************************
97 * playlist_Add: add an item to the playlist
98 *****************************************************************************
99 * Add an item to the playlist at position i_pos. If i_pos is PLAYLIST_END,
100 * add it at the end regardless of the playlist current size.
101 *****************************************************************************/
102 int playlist_Add( playlist_t *p_playlist, int i_pos, const char * psz_item )
104 msg_Warn( p_playlist, "adding playlist item « %s »", psz_item );
106 vlc_mutex_lock( &p_playlist->change_lock );
108 p_playlist->i_size++;
109 p_playlist->pp_items = realloc( p_playlist->pp_items,
110 p_playlist->i_size * sizeof(void*) );
111 if( p_playlist->pp_items == NULL )
113 msg_Err( p_playlist, "out of memory" );
114 vlc_mutex_unlock( &p_playlist->change_lock );
115 vlc_object_release( p_playlist );
119 i_pos = p_playlist->i_size - 1; /* FIXME */
120 p_playlist->pp_items[i_pos] = malloc( sizeof( playlist_item_t ) );
121 p_playlist->pp_items[i_pos]->psz_name = strdup( psz_item );
122 p_playlist->pp_items[i_pos]->i_type = 0;
123 p_playlist->pp_items[i_pos]->i_status = 0;
125 p_playlist->i_status = PLAYLIST_RUNNING;
127 vlc_mutex_unlock( &p_playlist->change_lock );
132 /*****************************************************************************
133 * playlist_Delete: delete an item from the playlist
134 *****************************************************************************
135 * Delete the item in the playlist with position i_pos.
136 *****************************************************************************/
137 int playlist_Delete( playlist_t * p_playlist, int i_pos )
139 vlc_mutex_lock( &p_playlist->change_lock );
141 vlc_mutex_unlock( &p_playlist->change_lock );
146 /*****************************************************************************
147 * playlist_Command: do a playlist action
148 *****************************************************************************
150 *****************************************************************************/
151 void playlist_Command( playlist_t * p_playlist, int i_command, int i_arg )
153 vlc_mutex_lock( &p_playlist->change_lock );
158 msg_Dbg( p_playlist, "stopping" );
159 p_playlist->i_status = PLAYLIST_STOPPED;
162 msg_Dbg( p_playlist, "running" );
163 p_playlist->i_status = PLAYLIST_RUNNING;
166 msg_Dbg( p_playlist, "next" );
167 if( p_playlist->i_size )
169 p_playlist->i_index = 0;
170 p_playlist->i_status = PLAYLIST_RUNNING;
177 vlc_mutex_unlock( &p_playlist->change_lock );
182 /* Following functions are local */
184 /*****************************************************************************
185 * RunThread: main playlist thread
186 *****************************************************************************/
187 static void RunThread ( playlist_t *p_playlist )
189 while( !p_playlist->b_die )
191 /* If there is an input, check that it doesn't need to die. */
192 if( p_playlist->p_input )
194 /* This input is dead. Remove it ! */
195 if( p_playlist->p_input->b_dead )
197 input_thread_t *p_input;
199 /* Unlink current input */
200 vlc_mutex_lock( &p_playlist->change_lock );
201 p_input = p_playlist->p_input;
202 p_playlist->p_input = NULL;
203 vlc_object_detach_all( p_input );
204 vlc_mutex_unlock( &p_playlist->change_lock );
207 vlc_object_release( p_input );
208 input_DestroyThread( p_input );
209 vlc_object_destroy( p_input );
211 /* This input is dying, let him do */
212 else if( p_playlist->p_input->b_die )
216 /* This input has finished, ask him to die ! */
217 else if( p_playlist->p_input->b_error
218 || p_playlist->p_input->b_eof )
220 input_StopThread( p_playlist->p_input );
223 else if( p_playlist->i_status != PLAYLIST_STOPPED )
225 /* Select the next playlist item */
226 playlist_Next( p_playlist );
228 /* don't loop by default: stop at playlist end */
229 if( p_playlist->i_index == -1 )
231 p_playlist->i_status = PLAYLIST_STOPPED;
235 input_thread_t *p_input;
237 //p_playlist->i_mode = PLAYLIST_FORWARD +
238 // config_GetInt( p_playlist, "loop-playlist" );
239 msg_Dbg( p_playlist, "creating new input thread" );
240 p_input = input_CreateThread( p_playlist,
241 p_playlist->pp_items[p_playlist->i_index], NULL );
242 if( p_input != NULL )
244 /* Link current input */
245 vlc_mutex_lock( &p_playlist->change_lock );
246 p_playlist->p_input = p_input;
247 vlc_mutex_unlock( &p_playlist->change_lock );
252 msleep( INTF_IDLE_SLEEP );
255 /* If there is an input, kill it */
256 while( p_playlist->p_input )
258 if( p_playlist->p_input->b_dead )
260 input_thread_t *p_input;
262 /* Unlink current input */
263 vlc_mutex_lock( &p_playlist->change_lock );
264 p_input = p_playlist->p_input;
265 p_playlist->p_input = NULL;
266 vlc_object_detach_all( p_input );
267 vlc_mutex_unlock( &p_playlist->change_lock );
270 vlc_object_release( p_input );
271 input_DestroyThread( p_input );
272 vlc_object_destroy( p_input );
274 /* This input is dying, let him do */
275 else if( p_playlist->p_input->b_die )
279 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
281 input_StopThread( p_playlist->p_input );
285 p_playlist->p_input->b_eof = 1;
288 msleep( INTF_IDLE_SLEEP );