1 /*****************************************************************************
2 * playlist.c : Playlist management functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: playlist.c,v 1.14 2002/09/29 18:19:53 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"
33 #include "vlc_playlist.h"
35 #define PLAYLIST_STOPPED 0
36 #define PLAYLIST_RUNNING 1
38 /*****************************************************************************
40 *****************************************************************************/
41 static void RunThread ( playlist_t * );
42 static void SkipItem ( playlist_t *, int );
43 static void PlayItem ( playlist_t * );
45 static void Poubellize ( playlist_t *, input_thread_t * );
47 /*****************************************************************************
48 * playlist_Create: create playlist
49 *****************************************************************************
50 * Create a playlist structure.
51 *****************************************************************************/
52 playlist_t * __playlist_Create ( vlc_object_t *p_parent )
54 playlist_t *p_playlist;
56 /* Allocate structure */
57 p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
60 msg_Err( p_parent, "out of memory" );
64 p_playlist->p_input = NULL;
65 p_playlist->i_status = PLAYLIST_STOPPED;
66 p_playlist->i_index = -1;
67 p_playlist->i_size = 0;
68 p_playlist->pp_items = NULL;
70 if( vlc_thread_create( p_playlist, "playlist", RunThread,
71 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
73 msg_Err( p_playlist, "cannot spawn playlist thread" );
74 vlc_object_destroy( p_playlist );
78 /* The object has been initialized, now attach it */
79 vlc_object_attach( p_playlist, p_parent );
84 /*****************************************************************************
85 * playlist_Destroy: destroy the playlist
86 *****************************************************************************
87 * Delete all items in the playlist and free the playlist structure.
88 *****************************************************************************/
89 void playlist_Destroy( playlist_t * p_playlist )
91 p_playlist->b_die = 1;
93 vlc_thread_join( p_playlist );
95 vlc_object_destroy( p_playlist );
98 /*****************************************************************************
99 * playlist_Add: add an item to the playlist
100 *****************************************************************************
101 * Add an item to the playlist at position i_pos. If i_pos is PLAYLIST_END,
102 * add it at the end regardless of the playlist current size.
103 *****************************************************************************/
104 int playlist_Add( playlist_t *p_playlist, const char * psz_target,
105 int i_mode, int i_pos )
107 playlist_item_t *p_item;
109 msg_Dbg( p_playlist, "adding playlist item « %s »", psz_target );
111 /* Create the new playlist item */
112 p_item = malloc( sizeof( playlist_item_t ) );
115 msg_Err( p_playlist, "out of memory" );
118 p_item->psz_name = strdup( psz_target );
120 p_item->i_status = 0;
122 vlc_mutex_lock( &p_playlist->object_lock );
124 /* Do a few boundary checks and allocate space for the item */
125 if( i_pos == PLAYLIST_END )
127 if( i_mode & PLAYLIST_INSERT )
129 i_mode &= ~PLAYLIST_INSERT;
130 i_mode |= PLAYLIST_APPEND;
133 i_pos = p_playlist->i_size - 1;
136 if( !(i_mode & PLAYLIST_REPLACE)
137 || i_pos < 0 || i_pos >= p_playlist->i_size )
141 p_playlist->i_size++;
142 p_playlist->pp_items = realloc( p_playlist->pp_items,
143 p_playlist->i_size * sizeof(void*) );
144 if( p_playlist->pp_items == NULL )
146 msg_Err( p_playlist, "out of memory" );
147 free( p_item->psz_name );
149 vlc_mutex_unlock( &p_playlist->object_lock );
153 /* Additional boundary checks */
154 if( i_mode & PLAYLIST_APPEND )
163 else if( i_pos > p_playlist->i_size - 1 )
165 i_pos = p_playlist->i_size - 1;
168 /* Now we know exactly where it goes. Just renumber the playlist */
169 for( i_index = p_playlist->i_size - 1; i_index > i_pos ; i_index-- )
171 p_playlist->pp_items[i_index] = p_playlist->pp_items[i_index - 1];
174 if( p_playlist->i_index >= i_pos )
176 p_playlist->i_index++;
181 /* i_mode == PLAYLIST_REPLACE and 0 <= i_pos < p_playlist->i_size */
182 free( p_playlist->pp_items[i_pos]->psz_name );
183 free( p_playlist->pp_items[i_pos] );
184 /* XXX: what if the item is still in use? */
187 p_playlist->pp_items[i_pos] = p_item;
189 if( i_mode & PLAYLIST_GO )
191 p_playlist->i_index = i_pos;
192 if( p_playlist->p_input )
194 input_StopThread( p_playlist->p_input );
196 p_playlist->i_status = PLAYLIST_RUNNING;
199 vlc_mutex_unlock( &p_playlist->object_lock );
204 /*****************************************************************************
205 * playlist_Delete: delete an item from the playlist
206 *****************************************************************************
207 * Delete the item in the playlist with position i_pos.
208 *****************************************************************************/
209 int playlist_Delete( playlist_t * p_playlist, int i_pos )
213 vlc_mutex_lock( &p_playlist->object_lock );
215 if( i_pos >= 0 && i_pos < p_playlist->i_size )
217 msg_Dbg( p_playlist, "deleting playlist item « %s »",
218 p_playlist->pp_items[i_pos]->psz_name );
220 free( p_playlist->pp_items[i_pos]->psz_name );
221 free( p_playlist->pp_items[i_pos] );
222 /* XXX: what if the item is still in use? */
224 if( i_pos < p_playlist->i_index )
226 p_playlist->i_index--;
229 /* Renumber the playlist */
230 for( i_index = i_pos + 1; i_index < p_playlist->i_size; i_index++ )
232 p_playlist->pp_items[i_index - 1] = p_playlist->pp_items[i_index];
235 p_playlist->i_size--;
236 if( p_playlist->i_size )
238 p_playlist->pp_items = realloc( p_playlist->pp_items,
239 p_playlist->i_size * sizeof(void*) );
243 free( p_playlist->pp_items );
244 p_playlist->pp_items = NULL;
248 vlc_mutex_unlock( &p_playlist->object_lock );
253 /*****************************************************************************
254 * playlist_Command: do a playlist action
255 *****************************************************************************
257 *****************************************************************************/
258 void playlist_Command( playlist_t * p_playlist, int i_command, int i_arg )
260 vlc_mutex_lock( &p_playlist->object_lock );
265 p_playlist->i_status = PLAYLIST_STOPPED;
266 if( p_playlist->p_input )
268 input_StopThread( p_playlist->p_input );
273 p_playlist->i_status = PLAYLIST_RUNNING;
277 p_playlist->i_status = PLAYLIST_STOPPED;
278 SkipItem( p_playlist, i_arg );
279 if( p_playlist->p_input )
281 input_StopThread( p_playlist->p_input );
283 p_playlist->i_status = PLAYLIST_RUNNING;
287 if( i_arg >= 0 && i_arg < p_playlist->i_size )
289 p_playlist->i_index = i_arg;
290 if( p_playlist->p_input )
292 input_StopThread( p_playlist->p_input );
294 p_playlist->i_status = PLAYLIST_RUNNING;
299 msg_Err( p_playlist, "unknown playlist command" );
303 vlc_mutex_unlock( &p_playlist->object_lock );
308 /* Following functions are local */
310 /*****************************************************************************
311 * RunThread: main playlist thread
312 *****************************************************************************/
313 static void RunThread ( playlist_t *p_playlist )
315 /* Tell above that we're ready */
316 vlc_thread_ready( p_playlist );
318 while( !p_playlist->b_die )
320 vlc_mutex_lock( &p_playlist->object_lock );
322 /* If there is an input, check that it doesn't need to die. */
323 if( p_playlist->p_input )
325 /* This input is dead. Remove it ! */
326 if( p_playlist->p_input->b_dead )
328 input_thread_t *p_input;
330 /* Unlink current input */
331 p_input = p_playlist->p_input;
332 p_playlist->p_input = NULL;
333 vlc_object_detach( p_input );
335 /* Release the playlist lock, because we may get stuck
336 * in input_DestroyThread() for some time. */
337 vlc_mutex_unlock( &p_playlist->object_lock );
340 input_DestroyThread( p_input );
341 vlc_object_destroy( p_input );
344 /* This input is dying, let him do */
345 else if( p_playlist->p_input->b_die )
349 /* This input has finished, ask him to die ! */
350 else if( p_playlist->p_input->b_error
351 || p_playlist->p_input->b_eof )
353 /* Select the next playlist item */
354 SkipItem( p_playlist, 1 );
356 /* Release the playlist lock, because we may get stuck
357 * in input_StopThread() for some time. */
358 vlc_mutex_unlock( &p_playlist->object_lock );
359 input_StopThread( p_playlist->p_input );
363 else if( p_playlist->i_status != PLAYLIST_STOPPED )
365 PlayItem( p_playlist );
368 vlc_mutex_unlock( &p_playlist->object_lock );
370 msleep( INTF_IDLE_SLEEP );
373 /* If there is an input, kill it */
376 vlc_mutex_lock( &p_playlist->object_lock );
378 if( p_playlist->p_input == NULL )
380 vlc_mutex_unlock( &p_playlist->object_lock );
384 if( p_playlist->p_input->b_dead )
386 input_thread_t *p_input;
388 /* Unlink current input */
389 p_input = p_playlist->p_input;
390 p_playlist->p_input = NULL;
391 vlc_object_detach( p_input );
392 vlc_mutex_unlock( &p_playlist->object_lock );
395 input_DestroyThread( p_input );
396 vlc_object_destroy( p_input );
399 else if( p_playlist->p_input->b_die )
401 /* This input is dying, leave him alone */
404 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
406 vlc_mutex_unlock( &p_playlist->object_lock );
407 input_StopThread( p_playlist->p_input );
412 p_playlist->p_input->b_eof = 1;
415 vlc_mutex_unlock( &p_playlist->object_lock );
417 msleep( INTF_IDLE_SLEEP );
421 /*****************************************************************************
422 * SkipItem: go to Xth playlist item
423 *****************************************************************************
424 * This function calculates the position of the next playlist item, depending
425 * on the playlist course mode (forward, backward, random...).
426 *****************************************************************************/
427 static void SkipItem( playlist_t *p_playlist, int i_arg )
429 int i_oldindex = p_playlist->i_index;
432 /* If the playlist is empty, there is no current item */
433 if( p_playlist->i_size == 0 )
435 p_playlist->i_index = -1;
439 b_random = config_GetInt( p_playlist, "random" );
446 /* Simple random stuff - we cheat a bit to minimize the chances to
447 * get the same index again. */
448 i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
451 i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
455 p_playlist->i_index += i_arg;
458 if( p_playlist->i_index >= p_playlist->i_size )
460 if( p_playlist->i_status == PLAYLIST_STOPPED
462 || config_GetInt( p_playlist, "loop" ) )
464 p_playlist->i_index -= p_playlist->i_size
465 * ( p_playlist->i_index / p_playlist->i_size );
469 /* Don't loop by default: stop at playlist end */
470 p_playlist->i_index = i_oldindex;
471 p_playlist->i_status = PLAYLIST_STOPPED;
474 else if( p_playlist->i_index < 0 )
476 p_playlist->i_index = p_playlist->i_size - 1;
480 /*****************************************************************************
481 * PlayItem: play current playlist item
482 *****************************************************************************
483 * This function calculates the position of the next playlist item, depending
484 * on the playlist course mode (forward, backward, random...).
485 *****************************************************************************/
486 static void PlayItem( playlist_t *p_playlist )
488 if( p_playlist->i_index == -1 )
490 if( p_playlist->i_size == 0 )
495 SkipItem( p_playlist, 1 );
498 msg_Dbg( p_playlist, "creating new input thread" );
499 p_playlist->p_input = input_CreateThread( p_playlist,
500 p_playlist->pp_items[p_playlist->i_index], NULL );
503 /*****************************************************************************
504 * Poubellize: put an input thread in the trashcan
505 *****************************************************************************
507 *****************************************************************************/
508 static void Poubellize ( playlist_t *p_playlist, input_thread_t *p_input )