1 /*****************************************************************************
2 * playlist.c : Playlist management functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: playlist.c,v 1.1 2002/06/01 12:32:02 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 vlc_object_attach( p_playlist, p_parent );
62 p_playlist->p_input = NULL;
63 p_playlist->i_status = PLAYLIST_RUNNING;
64 p_playlist->i_index = -1;
65 p_playlist->i_size = 0;
66 p_playlist->pp_items = NULL;
67 vlc_mutex_init( p_playlist, &p_playlist->change_lock );
69 if( vlc_thread_create( p_playlist, "playlist", RunThread, 0 ) )
71 msg_Err( p_playlist, "cannot spawn playlist thread" );
72 vlc_mutex_destroy( &p_playlist->change_lock );
73 vlc_object_unlink_all( p_playlist );
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( vlc_object_t *p_this, int i_pos, const char * psz_item )
104 playlist_t *p_playlist;
106 p_playlist = vlc_object_find( p_this->p_vlc, VLC_OBJECT_PLAYLIST,
109 if( p_playlist == NULL )
111 msg_Dbg( p_this, "no playlist present, creating one" );
112 p_playlist = playlist_Create( p_this->p_vlc->p_this );
114 if( p_playlist == NULL )
119 vlc_object_yield( p_playlist );
122 msg_Warn( p_this, "adding playlist item « %s »", psz_item );
124 vlc_mutex_lock( &p_playlist->change_lock );
126 p_playlist->i_size++;
127 p_playlist->pp_items = realloc( p_playlist->pp_items,
128 p_playlist->i_size * sizeof(void*) );
129 if( p_playlist->pp_items == NULL )
131 msg_Err( p_playlist, "out of memory" );
132 vlc_mutex_unlock( &p_playlist->change_lock );
133 vlc_object_release( p_playlist );
137 i_pos = p_playlist->i_size - 1; /* FIXME */
138 p_playlist->pp_items[i_pos] = malloc( sizeof( playlist_item_t ) );
139 p_playlist->pp_items[i_pos]->psz_name = strdup( psz_item );
140 p_playlist->pp_items[i_pos]->i_type = 0;
141 p_playlist->pp_items[i_pos]->i_status = 0;
143 vlc_mutex_unlock( &p_playlist->change_lock );
144 vlc_object_release( p_playlist );
149 /*****************************************************************************
150 * playlist_Delete: delete an item from the playlist
151 *****************************************************************************
152 * Delete the item in the playlist with position i_pos.
153 *****************************************************************************/
154 int playlist_Delete( playlist_t * p_playlist, int i_pos )
159 /*****************************************************************************
160 * playlist_Command: do a playlist action
161 *****************************************************************************
162 * Delete the item in the playlist with position i_pos.
163 *****************************************************************************/
164 void playlist_Command( playlist_t * p_playlist, int i_command, int i_arg )
169 msg_Dbg( p_playlist, "stopping" );
170 p_playlist->i_status = PLAYLIST_STOPPED;
173 msg_Dbg( p_playlist, "running" );
174 p_playlist->i_status = PLAYLIST_RUNNING;
177 msg_Dbg( p_playlist, "next" );
178 if( p_playlist->i_size )
180 p_playlist->i_index = 0;
181 p_playlist->i_status = PLAYLIST_RUNNING;
191 /* Following functions are local */
193 /*****************************************************************************
194 * RunThread: main playlist thread
195 *****************************************************************************/
196 static void RunThread ( playlist_t *p_playlist )
198 while( !p_playlist->b_die )
200 /* If there is an input, check that it doesn't need to die. */
201 if( p_playlist->p_input )
203 if( p_playlist->p_input->i_status == THREAD_OVER )
205 input_thread_t *p_input;
207 /* Unlink current input */
208 vlc_mutex_lock( &p_playlist->change_lock );
209 p_input = p_playlist->p_input;
210 p_playlist->p_input = NULL;
211 vlc_mutex_unlock( &p_playlist->change_lock );
214 vlc_object_unlink_all( p_input );
215 vlc_object_release( p_input );
216 input_DestroyThread( p_input );
218 else if( ( p_playlist->p_input->i_status == THREAD_READY
219 || p_playlist->p_input->i_status == THREAD_ERROR )
220 && ( p_playlist->p_input->b_error
221 || p_playlist->p_input->b_eof ) )
223 input_StopThread( p_playlist->p_input, NULL );
226 else if( p_playlist->i_status != PLAYLIST_STOPPED )
228 /* Select the next playlist item */
229 playlist_Next( p_playlist );
231 /* don't loop by default: stop at playlist end */
232 if( p_playlist->i_index == -1 )
234 p_playlist->i_status = PLAYLIST_STOPPED;
238 input_thread_t *p_input;
240 //p_playlist->i_mode = PLAYLIST_FORWARD +
241 // config_GetInt( p_playlist, "loop-playlist" );
242 msg_Dbg( p_playlist, "creating new input thread" );
243 p_input = input_CreateThread( p_playlist->p_this,
244 p_playlist->pp_items[p_playlist->i_index], NULL );
245 if( p_input != NULL )
247 /* Link current input */
248 vlc_mutex_lock( &p_playlist->change_lock );
249 p_playlist->p_input = p_input;
250 vlc_mutex_unlock( &p_playlist->change_lock );
255 msleep( INTF_IDLE_SLEEP );
258 /* If there is an input, kill it */
259 while( p_playlist->p_input )
261 if( p_playlist->p_input->i_status == THREAD_OVER )
263 input_thread_t *p_input;
265 /* Unlink current input */
266 vlc_mutex_lock( &p_playlist->change_lock );
267 p_input = p_playlist->p_input;
268 p_playlist->p_input = NULL;
269 vlc_mutex_unlock( &p_playlist->change_lock );
272 vlc_object_unlink_all( p_input );
273 vlc_object_release( p_input );
274 input_DestroyThread( p_input );
276 else if( ( p_playlist->p_input->i_status == THREAD_READY
277 || p_playlist->p_input->i_status == THREAD_ERROR )
278 && ( p_playlist->p_input->b_error
279 || p_playlist->p_input->b_eof ) )
281 input_StopThread( p_playlist->p_input, NULL );
285 p_playlist->p_input->b_eof = 1;
288 msleep( INTF_IDLE_SLEEP );