1 /*****************************************************************************
2 * playlist.c : Playlist management functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: playlist.c,v 1.62 2003/10/29 17:32:55 zorglub 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() */
31 #include "stream_control.h"
32 #include "input_ext-intf.h"
34 #include "vlc_playlist.h"
36 #define PLAYLIST_FILE_HEADER_0_5 "# vlc playlist file version 0.5"
37 #define PLAYLIST_FILE_HEADER_0_6 "# vlc playlist file version 0.6"
39 /*****************************************************************************
41 *****************************************************************************/
42 static void RunThread ( playlist_t * );
43 static void SkipItem ( playlist_t *, int );
44 static void PlayItem ( playlist_t * );
46 static void Poubellize ( playlist_t *, input_thread_t * );
51 * Create a playlist structure.
52 * \param p_parent the vlc object that is to be the parent of this playlist
53 * \return a pointer to the created playlist, or NULL on error
55 playlist_t * __playlist_Create ( vlc_object_t *p_parent )
57 playlist_t *p_playlist;
60 /* Allocate structure */
61 p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
64 msg_Err( p_parent, "out of memory" );
68 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
69 val.b_bool = VLC_TRUE;
70 var_Set( p_playlist, "intf-change", val );
72 var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
74 var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
75 val.b_bool = VLC_TRUE;
76 var_Set( p_playlist, "intf-show", val );
78 var_Create( p_playlist, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
79 var_Create( p_playlist, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
80 var_Create( p_playlist, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
82 p_playlist->p_input = NULL;
83 p_playlist->i_status = PLAYLIST_STOPPED;
84 p_playlist->i_index = -1;
85 p_playlist->i_size = 0;
86 p_playlist->pp_items = NULL;
88 p_playlist->i_groups = 0;
89 p_playlist->pp_groups = NULL;
90 p_playlist->i_max_id = 0;
92 playlist_CreateGroup( p_playlist, strdup("Normal") );
94 msg_Dbg(p_playlist,"pouet");
95 if( vlc_thread_create( p_playlist, "playlist", RunThread,
96 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
98 msg_Err( p_playlist, "cannot spawn playlist thread" );
99 vlc_object_destroy( p_playlist );
103 msg_Dbg(p_playlist,"pouet2");
104 /* The object has been initialized, now attach it */
105 vlc_object_attach( p_playlist, p_parent );
111 * Destroy the playlist.
113 * Delete all items in the playlist and free the playlist structure.
114 * \param p_playlist the playlist structure to destroy
116 void playlist_Destroy( playlist_t * p_playlist )
118 p_playlist->b_die = 1;
120 vlc_thread_join( p_playlist );
122 var_Destroy( p_playlist, "intf-change" );
124 vlc_object_destroy( p_playlist );
129 * Do a playlist action
131 * \param p_playlist the playlist to do the command on
132 * \param i_command the command to do
133 * \param i_arg the argument to the command. See playlist_command_t for details
135 void playlist_Command( playlist_t * p_playlist, playlist_command_t i_command,
140 vlc_mutex_lock( &p_playlist->object_lock );
145 p_playlist->i_status = PLAYLIST_STOPPED;
146 if( p_playlist->p_input )
148 input_StopThread( p_playlist->p_input );
153 p_playlist->i_status = PLAYLIST_RUNNING;
154 if( !p_playlist->p_input && p_playlist->i_enabled != 0 )
156 PlayItem( p_playlist );
158 if( p_playlist->p_input )
160 val.i_int = PLAYING_S;
161 var_Set( p_playlist->p_input, "state", val );
166 p_playlist->i_status = PLAYLIST_PAUSED;
167 if( p_playlist->p_input )
170 var_Set( p_playlist->p_input, "state", val );
175 p_playlist->i_status = PLAYLIST_STOPPED;
176 if( p_playlist->i_enabled == 0)
180 SkipItem( p_playlist, i_arg );
181 if( p_playlist->p_input )
183 input_StopThread( p_playlist->p_input );
185 p_playlist->i_status = PLAYLIST_RUNNING;
189 if( i_arg >= 0 && i_arg < p_playlist->i_size &&
190 p_playlist->i_enabled != 0 )
192 p_playlist->i_index = i_arg;
193 if( p_playlist->p_input )
195 input_StopThread( p_playlist->p_input );
197 p_playlist->i_status = PLAYLIST_RUNNING;
202 msg_Err( p_playlist, "unknown playlist command" );
206 vlc_mutex_unlock( &p_playlist->object_lock );
208 val.b_bool = VLC_TRUE;
209 var_Set( p_playlist, "intf-change", val );
213 /* Following functions are local */
215 static void ObjectGarbageCollector( playlist_t *p_playlist,
217 vlc_bool_t *pb_obj_destroyed,
218 mtime_t *pi_obj_destroyed_date )
221 if( *pb_obj_destroyed || *pi_obj_destroyed_date > mdate() )
226 if( *pi_obj_destroyed_date == 0 )
228 /* give a little time */
229 *pi_obj_destroyed_date = mdate() + 300000LL;
233 while( ( p_obj = vlc_object_find( p_playlist,
237 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
239 /* only first chiled (ie unused) */
240 vlc_object_release( p_obj );
243 if( i_type == VLC_OBJECT_VOUT )
245 msg_Dbg( p_playlist, "vout garbage collector destroying 1 vout" );
246 vlc_object_detach( p_obj );
247 vlc_object_release( p_obj );
248 vout_Destroy( (vout_thread_t *)p_obj );
250 else if( i_type == VLC_OBJECT_SOUT )
252 vlc_object_release( p_obj );
253 sout_DeleteInstance( (sout_instance_t*)p_obj );
256 *pb_obj_destroyed = VLC_TRUE;
260 /*****************************************************************************
261 * RunThread: main playlist thread
262 *****************************************************************************/
263 static void RunThread ( playlist_t *p_playlist )
268 vlc_bool_t b_vout_destroyed = VLC_FALSE; /*we do vout garbage collector */
269 mtime_t i_vout_destroyed_date = 0;
271 vlc_bool_t b_sout_destroyed = VLC_FALSE; /*we do vout garbage collector */
272 mtime_t i_sout_destroyed_date = 0;
274 /* Tell above that we're ready */
275 vlc_thread_ready( p_playlist );
277 msg_Dbg(p_playlist,"pouet3");
279 while( !p_playlist->b_die )
281 vlc_mutex_lock( &p_playlist->object_lock );
283 /* If there is an input, check that it doesn't need to die. */
284 if( p_playlist->p_input )
286 /* This input is dead. Remove it ! */
287 if( p_playlist->p_input->b_dead )
289 input_thread_t *p_input;
291 p_input = p_playlist->p_input;
292 p_playlist->p_input = NULL;
294 /* Release the playlist lock, because we may get stuck
295 * in input_DestroyThread() for some time. */
296 vlc_mutex_unlock( &p_playlist->object_lock );
299 input_DestroyThread( p_input );
301 /* Unlink current input
302 * (_after_ input_DestroyThread for vout garbage collector) */
303 vlc_object_detach( p_input );
306 vlc_object_destroy( p_input );
308 b_vout_destroyed = VLC_FALSE;
309 i_vout_destroyed_date = 0;
310 b_sout_destroyed = VLC_FALSE;
311 i_sout_destroyed_date = 0;
314 /* This input is dying, let him do */
315 else if( p_playlist->p_input->b_die )
319 /* This input has finished, ask him to die ! */
320 else if( p_playlist->p_input->b_error
321 || p_playlist->p_input->b_eof )
323 /* Check for autodeletion */
324 if( p_playlist->pp_items[p_playlist->i_index]->b_autodeletion )
326 vlc_mutex_unlock( &p_playlist->object_lock );
327 playlist_Delete( p_playlist, p_playlist->i_index );
328 vlc_mutex_lock( &p_playlist->object_lock );
331 /* Select the next playlist item */
332 SkipItem( p_playlist, 1 );
334 input_StopThread( p_playlist->p_input );
335 vlc_mutex_unlock( &p_playlist->object_lock );
337 val.b_bool = VLC_TRUE;
338 var_Set( p_playlist, "intf-change", val );
341 else if( p_playlist->p_input->stream.control.i_status != INIT_S )
343 vlc_mutex_unlock( &p_playlist->object_lock );
344 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
346 &i_vout_destroyed_date );
347 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
349 &i_sout_destroyed_date );
350 vlc_mutex_lock( &p_playlist->object_lock );
353 else if( p_playlist->i_status != PLAYLIST_STOPPED )
355 SkipItem( p_playlist, 0 );
356 PlayItem( p_playlist );
358 else if( p_playlist->i_status == PLAYLIST_STOPPED )
360 vlc_mutex_unlock( &p_playlist->object_lock );
361 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
362 &b_sout_destroyed, &i_sout_destroyed_date );
363 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
364 &b_vout_destroyed, &i_vout_destroyed_date );
365 vlc_mutex_lock( &p_playlist->object_lock );
367 vlc_mutex_unlock( &p_playlist->object_lock );
369 msleep( INTF_IDLE_SLEEP );
372 /* If there is an input, kill it */
375 vlc_mutex_lock( &p_playlist->object_lock );
377 if( p_playlist->p_input == NULL )
379 vlc_mutex_unlock( &p_playlist->object_lock );
383 if( p_playlist->p_input->b_dead )
385 input_thread_t *p_input;
387 /* Unlink current input */
388 p_input = p_playlist->p_input;
389 p_playlist->p_input = NULL;
390 vlc_mutex_unlock( &p_playlist->object_lock );
393 input_DestroyThread( p_input );
394 /* Unlink current input (_after_ input_DestroyThread for vout
395 * garbage collector)*/
396 vlc_object_detach( p_input );
399 vlc_object_destroy( p_input );
402 else if( p_playlist->p_input->b_die )
404 /* This input is dying, leave him alone */
407 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
409 input_StopThread( p_playlist->p_input );
410 vlc_mutex_unlock( &p_playlist->object_lock );
415 p_playlist->p_input->b_eof = 1;
418 vlc_mutex_unlock( &p_playlist->object_lock );
420 msleep( INTF_IDLE_SLEEP );
423 /* close all remaining sout */
424 while( ( p_obj = vlc_object_find( p_playlist,
425 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
427 vlc_object_release( p_obj );
428 sout_DeleteInstance( (sout_instance_t*)p_obj );
431 /* close all remaining vout */
432 while( ( p_obj = vlc_object_find( p_playlist,
433 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
435 vlc_object_detach( p_obj );
436 vlc_object_release( p_obj );
437 vout_Destroy( (vout_thread_t *)p_obj );
441 /*****************************************************************************
442 * SkipItem: go to Xth playlist item
443 *****************************************************************************
444 * This function calculates the position of the next playlist item, depending
445 * on the playlist course mode (forward, backward, random...).
446 *****************************************************************************/
447 static void SkipItem( playlist_t *p_playlist, int i_arg )
449 int i_oldindex = p_playlist->i_index;
450 vlc_bool_t b_random, b_repeat, b_loop;
453 /* If the playlist is empty, there is no current item */
454 if( p_playlist->i_size == 0 )
456 p_playlist->i_index = -1;
460 var_Get( p_playlist, "random", &val );
461 b_random = val.b_bool;
462 var_Get( p_playlist, "repeat", &val );
463 b_repeat = val.b_bool;
464 var_Get( p_playlist, "loop", &val );
470 srand( (unsigned int)mdate() );
472 /* Simple random stuff - we cheat a bit to minimize the chances to
473 * get the same index again. */
474 i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
477 i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
484 p_playlist->i_index += i_arg;
487 if( p_playlist->i_index >= p_playlist->i_size )
489 if( p_playlist->i_status == PLAYLIST_STOPPED
493 p_playlist->i_index -= p_playlist->i_size
494 * ( p_playlist->i_index / p_playlist->i_size );
498 /* Don't loop by default: stop at playlist end */
499 p_playlist->i_index = i_oldindex;
500 p_playlist->i_status = PLAYLIST_STOPPED;
503 else if( p_playlist->i_index < 0 )
505 p_playlist->i_index = p_playlist->i_size - 1;
508 /* Check that the item is enabled */
509 if( p_playlist->pp_items[p_playlist->i_index]->b_enabled == VLC_FALSE &&
510 p_playlist->i_enabled != 0)
512 SkipItem( p_playlist , 1 );
516 /*****************************************************************************
517 * PlayItem: play current playlist item
518 *****************************************************************************
519 * This function calculates the position of the next playlist item, depending
520 * on the playlist course mode (forward, backward, random...).
521 *****************************************************************************/
522 static void PlayItem( playlist_t *p_playlist )
524 if( p_playlist->i_index == -1 )
526 if( p_playlist->i_size == 0 || p_playlist->i_enabled == 0)
531 SkipItem( p_playlist, 1 );
534 if( p_playlist->i_enabled == 0)
539 msg_Dbg( p_playlist, "creating new input thread" );
540 p_playlist->p_input = input_CreateThread( p_playlist,
541 p_playlist->pp_items[p_playlist->i_index] );