1 /*****************************************************************************
2 * playlist.c : Playlist management functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: playlist.c,v 1.24 2002/12/03 16:29:04 gitan 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() */
29 #include "stream_control.h"
30 #include "input_ext-intf.h"
32 #include "vlc_playlist.h"
34 #define PLAYLIST_STOPPED 0
35 #define PLAYLIST_RUNNING 1
37 /*****************************************************************************
39 *****************************************************************************/
40 static void RunThread ( playlist_t * );
41 static void SkipItem ( playlist_t *, int );
42 static void PlayItem ( playlist_t * );
44 static void Poubellize ( playlist_t *, input_thread_t * );
46 /*****************************************************************************
47 * playlist_Create: create playlist
48 *****************************************************************************
49 * Create a playlist structure.
50 *****************************************************************************/
51 playlist_t * __playlist_Create ( vlc_object_t *p_parent )
53 playlist_t *p_playlist;
55 /* Allocate structure */
56 p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
59 msg_Err( p_parent, "out of memory" );
63 p_playlist->p_input = NULL;
64 p_playlist->i_status = PLAYLIST_STOPPED;
65 p_playlist->i_index = -1;
66 p_playlist->i_size = 0;
67 p_playlist->pp_items = NULL;
69 if( vlc_thread_create( p_playlist, "playlist", RunThread,
70 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
72 msg_Err( p_playlist, "cannot spawn playlist thread" );
73 vlc_object_destroy( p_playlist );
77 /* The object has been initialized, now attach it */
78 vlc_object_attach( p_playlist, p_parent );
83 /*****************************************************************************
84 * playlist_Destroy: destroy the playlist
85 *****************************************************************************
86 * Delete all items in the playlist and free the playlist structure.
87 *****************************************************************************/
88 void playlist_Destroy( playlist_t * p_playlist )
90 p_playlist->b_die = 1;
92 vlc_thread_join( p_playlist );
94 vlc_object_destroy( p_playlist );
97 /*****************************************************************************
98 * playlist_Add: add an item to the playlist
99 *****************************************************************************
100 * Add an item to the playlist at position i_pos. If i_pos is PLAYLIST_END,
101 * add it at the end regardless of the playlist current size.
102 *****************************************************************************/
103 int playlist_Add( playlist_t *p_playlist, const char * psz_target,
104 int i_mode, int i_pos )
106 playlist_item_t *p_item;
109 vlc_mutex_lock( &p_playlist->object_lock );
112 * CHECK_INSERT : checks if the item is already enqued before
115 if ( i_mode & PLAYLIST_CHECK_INSERT )
119 if ( p_playlist->pp_items )
121 for ( j = 0; j < p_playlist->i_size; j++ )
123 if ( !strcmp( p_playlist->pp_items[j]->psz_name, psz_target ) )
125 msg_Dbg( p_playlist, "item « %s » already enqued",
127 vlc_mutex_unlock( &p_playlist->object_lock );
132 i_mode &= ~PLAYLIST_CHECK_INSERT;
133 i_mode |= PLAYLIST_APPEND;
137 msg_Dbg( p_playlist, "adding playlist item « %s »", psz_target );
139 /* Create the new playlist item */
140 p_item = malloc( sizeof( playlist_item_t ) );
143 msg_Err( p_playlist, "out of memory" );
146 p_item->psz_name = strdup( psz_target );
148 p_item->i_status = 0;
149 p_item->b_autodeletion = VLC_FALSE;
152 /* Do a few boundary checks and allocate space for the item */
153 if( i_pos == PLAYLIST_END )
155 if( i_mode & PLAYLIST_INSERT )
157 i_mode &= ~PLAYLIST_INSERT;
158 i_mode |= PLAYLIST_APPEND;
161 i_pos = p_playlist->i_size - 1;
164 if( !(i_mode & PLAYLIST_REPLACE)
165 || i_pos < 0 || i_pos >= p_playlist->i_size )
167 /* Additional boundary checks */
168 if( i_mode & PLAYLIST_APPEND )
177 else if( i_pos > p_playlist->i_size )
179 i_pos = p_playlist->i_size;
182 INSERT_ELEM( p_playlist->pp_items,
187 if( p_playlist->i_index >= i_pos )
189 p_playlist->i_index++;
194 /* i_mode == PLAYLIST_REPLACE and 0 <= i_pos < p_playlist->i_size */
195 free( p_playlist->pp_items[i_pos]->psz_name );
196 /* XXX: what if the item is still in use? */
197 free( p_playlist->pp_items[i_pos] );
198 p_playlist->pp_items[i_pos] = p_item;
201 if( i_mode & PLAYLIST_GO )
203 p_playlist->i_index = i_pos;
204 if( p_playlist->p_input )
206 input_StopThread( p_playlist->p_input );
208 p_playlist->i_status = PLAYLIST_RUNNING;
211 vlc_mutex_unlock( &p_playlist->object_lock );
216 /*****************************************************************************
217 * playlist_Delete: delete an item from the playlist
218 *****************************************************************************
219 * Delete the item in the playlist with position i_pos.
220 *****************************************************************************/
221 int playlist_Delete( playlist_t * p_playlist, int i_pos )
223 vlc_mutex_lock( &p_playlist->object_lock );
225 if( i_pos >= 0 && i_pos < p_playlist->i_size )
227 msg_Dbg( p_playlist, "deleting playlist item « %s »",
228 p_playlist->pp_items[i_pos]->psz_name );
230 free( p_playlist->pp_items[i_pos]->psz_name );
231 /* XXX: what if the item is still in use? */
232 free( p_playlist->pp_items[i_pos] );
234 if( i_pos <= p_playlist->i_index )
236 p_playlist->i_index--;
239 /* Renumber the playlist */
240 REMOVE_ELEM( p_playlist->pp_items,
245 vlc_mutex_unlock( &p_playlist->object_lock );
250 /*****************************************************************************
251 * playlist_Command: do a playlist action
252 *****************************************************************************
254 *****************************************************************************/
255 void playlist_Command( playlist_t * p_playlist, int i_command, int i_arg )
257 vlc_mutex_lock( &p_playlist->object_lock );
262 p_playlist->i_status = PLAYLIST_STOPPED;
263 if( p_playlist->p_input )
265 input_StopThread( p_playlist->p_input );
270 p_playlist->i_status = PLAYLIST_RUNNING;
274 p_playlist->i_status = PLAYLIST_STOPPED;
275 SkipItem( p_playlist, i_arg );
276 if( p_playlist->p_input )
278 input_StopThread( p_playlist->p_input );
280 p_playlist->i_status = PLAYLIST_RUNNING;
284 if( i_arg >= 0 && i_arg < p_playlist->i_size )
286 p_playlist->i_index = i_arg;
287 if( p_playlist->p_input )
289 input_StopThread( p_playlist->p_input );
291 p_playlist->i_status = PLAYLIST_RUNNING;
296 msg_Err( p_playlist, "unknown playlist command" );
300 vlc_mutex_unlock( &p_playlist->object_lock );
305 /* Following functions are local */
307 /*****************************************************************************
308 * RunThread: main playlist thread
309 *****************************************************************************/
310 static void RunThread ( playlist_t *p_playlist )
312 /* Tell above that we're ready */
313 vlc_thread_ready( p_playlist );
315 while( !p_playlist->b_die )
317 vlc_mutex_lock( &p_playlist->object_lock );
319 /* If there is an input, check that it doesn't need to die. */
320 if( p_playlist->p_input )
322 /* This input is dead. Remove it ! */
323 if( p_playlist->p_input->b_dead )
325 input_thread_t *p_input;
327 /* Unlink current input */
328 p_input = p_playlist->p_input;
329 p_playlist->p_input = NULL;
330 vlc_object_detach( p_input );
332 /* Release the playlist lock, because we may get stuck
333 * in input_DestroyThread() for some time. */
334 vlc_mutex_unlock( &p_playlist->object_lock );
337 input_DestroyThread( p_input );
338 vlc_object_destroy( p_input );
341 /* This input is dying, let him do */
342 else if( p_playlist->p_input->b_die )
346 /* This input has finished, ask him to die ! */
347 else if( p_playlist->p_input->b_error
348 || p_playlist->p_input->b_eof )
350 /* Check for autodeletion */
351 if( p_playlist->pp_items[p_playlist->i_index]->b_autodeletion )
353 vlc_mutex_unlock( &p_playlist->object_lock );
354 playlist_Delete( p_playlist, p_playlist->i_index );
355 vlc_mutex_lock( &p_playlist->object_lock );
358 /* Select the next playlist item */
359 SkipItem( p_playlist, 1 );
361 /* Release the playlist lock, because we may get stuck
362 * in input_StopThread() for some time. */
363 vlc_mutex_unlock( &p_playlist->object_lock );
364 input_StopThread( p_playlist->p_input );
368 else if( p_playlist->i_status != PLAYLIST_STOPPED )
370 PlayItem( p_playlist );
373 vlc_mutex_unlock( &p_playlist->object_lock );
375 msleep( INTF_IDLE_SLEEP );
378 /* If there is an input, kill it */
381 vlc_mutex_lock( &p_playlist->object_lock );
383 if( p_playlist->p_input == NULL )
385 vlc_mutex_unlock( &p_playlist->object_lock );
389 if( p_playlist->p_input->b_dead )
391 input_thread_t *p_input;
393 /* Unlink current input */
394 p_input = p_playlist->p_input;
395 p_playlist->p_input = NULL;
396 vlc_object_detach( p_input );
397 vlc_mutex_unlock( &p_playlist->object_lock );
400 input_DestroyThread( p_input );
401 vlc_object_destroy( p_input );
404 else if( p_playlist->p_input->b_die )
406 /* This input is dying, leave him alone */
409 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
411 vlc_mutex_unlock( &p_playlist->object_lock );
412 input_StopThread( p_playlist->p_input );
417 p_playlist->p_input->b_eof = 1;
420 vlc_mutex_unlock( &p_playlist->object_lock );
422 msleep( INTF_IDLE_SLEEP );
426 /*****************************************************************************
427 * SkipItem: go to Xth playlist item
428 *****************************************************************************
429 * This function calculates the position of the next playlist item, depending
430 * on the playlist course mode (forward, backward, random...).
431 *****************************************************************************/
432 static void SkipItem( playlist_t *p_playlist, int i_arg )
434 int i_oldindex = p_playlist->i_index;
437 /* If the playlist is empty, there is no current item */
438 if( p_playlist->i_size == 0 )
440 p_playlist->i_index = -1;
444 b_random = config_GetInt( p_playlist, "random" );
449 srand( (unsigned int)mdate() );
451 /* Simple random stuff - we cheat a bit to minimize the chances to
452 * get the same index again. */
453 i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
456 i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
460 p_playlist->i_index += i_arg;
463 if( p_playlist->i_index >= p_playlist->i_size )
465 if( p_playlist->i_status == PLAYLIST_STOPPED
467 || config_GetInt( p_playlist, "loop" ) )
469 p_playlist->i_index -= p_playlist->i_size
470 * ( p_playlist->i_index / p_playlist->i_size );
474 /* Don't loop by default: stop at playlist end */
475 p_playlist->i_index = i_oldindex;
476 p_playlist->i_status = PLAYLIST_STOPPED;
479 else if( p_playlist->i_index < 0 )
481 p_playlist->i_index = p_playlist->i_size - 1;
485 /*****************************************************************************
486 * PlayItem: play current playlist item
487 *****************************************************************************
488 * This function calculates the position of the next playlist item, depending
489 * on the playlist course mode (forward, backward, random...).
490 *****************************************************************************/
491 static void PlayItem( playlist_t *p_playlist )
493 if( p_playlist->i_index == -1 )
495 if( p_playlist->i_size == 0 )
500 SkipItem( p_playlist, 1 );
503 msg_Dbg( p_playlist, "creating new input thread" );
504 p_playlist->p_input = input_CreateThread( p_playlist,
505 p_playlist->pp_items[p_playlist->i_index], NULL );
508 /*****************************************************************************
509 * Poubellize: put an input thread in the trashcan
510 *****************************************************************************
512 *****************************************************************************/
513 static void Poubellize ( playlist_t *p_playlist, input_thread_t *p_input )