1 /*****************************************************************************
2 * playlist.c : Playlist management functions
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
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() */
30 #include <vlc/input.h>
32 #include "stream_control.h"
33 #include "input_ext-intf.h"
35 #include "vlc_playlist.h"
37 #define PLAYLIST_FILE_HEADER_0_5 "# vlc playlist file version 0.5"
39 /*****************************************************************************
41 *****************************************************************************/
42 static void RunThread ( playlist_t * );
43 static void SkipItem ( playlist_t *, int );
44 static void PlayItem ( playlist_t * );
49 * Create a playlist structure.
50 * \param p_parent the vlc object that is to be the parent of this playlist
51 * \return a pointer to the created playlist, or NULL on error
53 playlist_t * __playlist_Create ( vlc_object_t *p_parent )
55 playlist_t *p_playlist;
58 /* Allocate structure */
59 p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
62 msg_Err( p_parent, "out of memory" );
66 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
67 val.b_bool = VLC_TRUE;
68 var_Set( p_playlist, "intf-change", val );
70 var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
72 var_Set( p_playlist, "item-change", val );
74 var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
76 var_Set( p_playlist, "playlist-current", val );
78 var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
80 var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
81 val.b_bool = VLC_TRUE;
82 var_Set( p_playlist, "intf-show", val );
85 var_Create( p_playlist, "prevent-skip", VLC_VAR_BOOL );
86 val.b_bool = VLC_FALSE;
87 var_Set( p_playlist, "prevent-skip", val );
89 var_Create( p_playlist, "play-and-stop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
90 var_Create( p_playlist, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
91 var_Create( p_playlist, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
92 var_Create( p_playlist, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
94 p_playlist->p_input = NULL;
95 p_playlist->i_status = PLAYLIST_STOPPED;
96 p_playlist->i_index = -1;
97 p_playlist->i_size = 0;
98 p_playlist->pp_items = NULL;
100 p_playlist->i_groups = 0;
101 p_playlist->pp_groups = NULL;
102 p_playlist->i_last_group = 0;
103 p_playlist->i_last_id = 0;
104 p_playlist->i_sort = SORT_ID;
105 p_playlist->i_order = ORDER_NORMAL;
107 playlist_CreateGroup( p_playlist, _("Normal") );
109 if( vlc_thread_create( p_playlist, "playlist", RunThread,
110 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
112 msg_Err( p_playlist, "cannot spawn playlist thread" );
113 vlc_object_destroy( p_playlist );
117 /* The object has been initialized, now attach it */
118 vlc_object_attach( p_playlist, p_parent );
124 * Destroy the playlist.
126 * Delete all items in the playlist and free the playlist structure.
127 * \param p_playlist the playlist structure to destroy
129 void playlist_Destroy( playlist_t * p_playlist )
131 p_playlist->b_die = 1;
133 vlc_thread_join( p_playlist );
135 var_Destroy( p_playlist, "intf-change" );
136 var_Destroy( p_playlist, "item-change" );
137 var_Destroy( p_playlist, "playlist-current" );
138 var_Destroy( p_playlist, "intf-popmenu" );
139 var_Destroy( p_playlist, "intf-show" );
140 var_Destroy( p_playlist, "prevent-skip" );
141 var_Destroy( p_playlist, "play-and-stop" );
142 var_Destroy( p_playlist, "random" );
143 var_Destroy( p_playlist, "repeat" );
144 var_Destroy( p_playlist, "loop" );
146 while( p_playlist->i_groups > 0 )
148 playlist_DeleteGroup( p_playlist, p_playlist->pp_groups[0]->i_id );
151 while( p_playlist->i_size > 0 )
153 playlist_Delete( p_playlist, 0 );
156 vlc_object_destroy( p_playlist );
161 * Do a playlist action.
163 * If there is something in the playlist then you can do playlist actions.
164 * \param p_playlist the playlist to do the command on
165 * \param i_command the command to do
166 * \param i_arg the argument to the command. See playlist_command_t for details
168 void playlist_Command( playlist_t * p_playlist, playlist_command_t i_command,
173 vlc_mutex_lock( &p_playlist->object_lock );
175 if( p_playlist->i_size <= 0 )
177 vlc_mutex_unlock( &p_playlist->object_lock );
184 p_playlist->i_status = PLAYLIST_STOPPED;
185 if( p_playlist->p_input )
187 input_StopThread( p_playlist->p_input );
188 val.i_int = p_playlist->i_index;
189 /* Does not matter if we unlock here */
190 vlc_mutex_unlock( &p_playlist->object_lock );
191 var_Set( p_playlist, "item-change",val );
192 vlc_mutex_lock( &p_playlist->object_lock );
197 p_playlist->i_status = PLAYLIST_RUNNING;
198 if( !p_playlist->p_input && p_playlist->i_enabled != 0 )
200 PlayItem( p_playlist );
202 if( p_playlist->p_input )
204 val.i_int = PLAYING_S;
205 var_Set( p_playlist->p_input, "state", val );
211 if( p_playlist->p_input )
212 var_Get( p_playlist->p_input, "state", &val );
214 if( val.i_int == PAUSE_S )
216 p_playlist->i_status = PLAYLIST_RUNNING;
217 if( p_playlist->p_input )
219 val.i_int = PLAYING_S;
220 var_Set( p_playlist->p_input, "state", val );
225 p_playlist->i_status = PLAYLIST_PAUSED;
226 if( p_playlist->p_input )
229 var_Set( p_playlist->p_input, "state", val );
235 p_playlist->i_status = PLAYLIST_STOPPED;
236 if( p_playlist->i_enabled == 0)
240 SkipItem( p_playlist, i_arg );
241 if( p_playlist->p_input )
243 input_StopThread( p_playlist->p_input );
245 p_playlist->i_status = PLAYLIST_RUNNING;
249 if( i_arg >= 0 && i_arg < p_playlist->i_size &&
250 p_playlist->i_enabled != 0 )
252 p_playlist->i_index = i_arg;
253 if( p_playlist->p_input )
255 input_StopThread( p_playlist->p_input );
257 val.b_bool = VLC_TRUE;
258 var_Set( p_playlist, "prevent-skip", val );
259 p_playlist->i_status = PLAYLIST_RUNNING;
264 msg_Err( p_playlist, "unknown playlist command" );
268 vlc_mutex_unlock( &p_playlist->object_lock );
270 val.b_bool = VLC_TRUE;
271 var_Set( p_playlist, "intf-change", val );
277 static mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
278 mtime_t destroy_date )
282 if( destroy_date > mdate() ) return destroy_date;
284 if( destroy_date == 0 )
286 /* give a little time */
287 return mdate() + I64C(1000000);
291 while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
293 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
295 /* only first child (ie unused) */
296 vlc_object_release( p_obj );
299 if( i_type == VLC_OBJECT_VOUT )
301 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
302 vlc_object_detach( p_obj );
303 vlc_object_release( p_obj );
304 vout_Destroy( (vout_thread_t *)p_obj );
306 else if( i_type == VLC_OBJECT_SOUT )
308 vlc_object_release( p_obj );
309 sout_DeleteInstance( (sout_instance_t*)p_obj );
316 /*****************************************************************************
317 * RunThread: main playlist thread
318 *****************************************************************************/
319 static void RunThread ( playlist_t *p_playlist )
324 mtime_t i_vout_destroyed_date = 0;
325 mtime_t i_sout_destroyed_date = 0;
327 /* Tell above that we're ready */
328 vlc_thread_ready( p_playlist );
330 while( !p_playlist->b_die )
332 vlc_mutex_lock( &p_playlist->object_lock );
334 /* If there is an input, check that it doesn't need to die. */
335 if( p_playlist->p_input )
337 /* This input is dead. Remove it ! */
338 if( p_playlist->p_input->b_dead )
340 input_thread_t *p_input;
342 p_input = p_playlist->p_input;
343 p_playlist->p_input = NULL;
345 /* Release the playlist lock, because we may get stuck
346 * in input_DestroyThread() for some time. */
347 vlc_mutex_unlock( &p_playlist->object_lock );
350 input_DestroyThread( p_input );
352 /* Unlink current input
353 * (_after_ input_DestroyThread for vout garbage collector) */
354 vlc_object_detach( p_input );
357 vlc_object_destroy( p_input );
359 i_vout_destroyed_date = 0;
360 i_sout_destroyed_date = 0;
363 /* This input is dying, let him do */
364 else if( p_playlist->p_input->b_die )
368 /* This input has finished, ask him to die ! */
369 else if( p_playlist->p_input->b_error
370 || p_playlist->p_input->b_eof )
372 /* Check for autodeletion */
373 if( p_playlist->pp_items[p_playlist->i_index]->b_autodeletion )
375 vlc_mutex_unlock( &p_playlist->object_lock );
376 playlist_Delete( p_playlist, p_playlist->i_index );
377 p_playlist->i_index++;
378 p_playlist->i_status = PLAYLIST_RUNNING;
382 /* Select the next playlist item */
383 SkipItem( p_playlist, 1 );
384 input_StopThread( p_playlist->p_input );
385 vlc_mutex_unlock( &p_playlist->object_lock );
389 else if( p_playlist->p_input->stream.control.i_status != INIT_S )
391 vlc_mutex_unlock( &p_playlist->object_lock );
392 i_vout_destroyed_date =
393 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
394 i_vout_destroyed_date );
395 i_sout_destroyed_date =
396 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
397 i_sout_destroyed_date );
398 vlc_mutex_lock( &p_playlist->object_lock );
401 else if( p_playlist->i_status != PLAYLIST_STOPPED )
403 /* Start another input. Let's check if that item has
404 * been forced. In that case, we override random (by not skipping)
405 * and play-and-stop */
407 var_Get( p_playlist, "prevent-skip", &val );
408 b_forced = val.b_bool;
409 if( val.b_bool == VLC_FALSE )
411 SkipItem( p_playlist, 0 );
413 /* Reset forced status */
414 val.b_bool = VLC_FALSE;
415 var_Set( p_playlist, "prevent-skip", val );
416 /* Check for play-and-stop */
417 var_Get( p_playlist, "play-and-stop", &val );
418 if( val.b_bool == VLC_FALSE || b_forced == VLC_TRUE )
420 PlayItem( p_playlist );
423 else if( p_playlist->i_status == PLAYLIST_STOPPED )
425 vlc_mutex_unlock( &p_playlist->object_lock );
426 i_sout_destroyed_date =
427 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
428 i_vout_destroyed_date =
429 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
430 vlc_mutex_lock( &p_playlist->object_lock );
432 vlc_mutex_unlock( &p_playlist->object_lock );
434 msleep( INTF_IDLE_SLEEP );
437 /* If there is an input, kill it */
440 vlc_mutex_lock( &p_playlist->object_lock );
442 if( p_playlist->p_input == NULL )
444 vlc_mutex_unlock( &p_playlist->object_lock );
448 if( p_playlist->p_input->b_dead )
450 input_thread_t *p_input;
452 /* Unlink current input */
453 p_input = p_playlist->p_input;
454 p_playlist->p_input = NULL;
455 vlc_mutex_unlock( &p_playlist->object_lock );
458 input_DestroyThread( p_input );
459 /* Unlink current input (_after_ input_DestroyThread for vout
460 * garbage collector)*/
461 vlc_object_detach( p_input );
464 vlc_object_destroy( p_input );
467 else if( p_playlist->p_input->b_die )
469 /* This input is dying, leave him alone */
472 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
474 input_StopThread( p_playlist->p_input );
475 vlc_mutex_unlock( &p_playlist->object_lock );
480 p_playlist->p_input->b_eof = 1;
483 vlc_mutex_unlock( &p_playlist->object_lock );
485 msleep( INTF_IDLE_SLEEP );
488 /* close all remaining sout */
489 while( ( p_obj = vlc_object_find( p_playlist,
490 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
492 vlc_object_release( p_obj );
493 sout_DeleteInstance( (sout_instance_t*)p_obj );
496 /* close all remaining vout */
497 while( ( p_obj = vlc_object_find( p_playlist,
498 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
500 vlc_object_detach( p_obj );
501 vlc_object_release( p_obj );
502 vout_Destroy( (vout_thread_t *)p_obj );
506 /*****************************************************************************
507 * SkipItem: go to Xth playlist item
508 *****************************************************************************
509 * This function calculates the position of the next playlist item, depending
510 * on the playlist course mode (forward, backward, random...).
511 *****************************************************************************/
512 static void SkipItem( playlist_t *p_playlist, int i_arg )
514 int i_oldindex = p_playlist->i_index;
515 vlc_bool_t b_random, b_repeat, b_loop;
519 /* If the playlist is empty, there is no current item */
520 if( p_playlist->i_size == 0 )
522 p_playlist->i_index = -1;
526 var_Get( p_playlist, "random", &val );
527 b_random = val.b_bool;
528 var_Get( p_playlist, "repeat", &val );
529 b_repeat = val.b_bool;
530 var_Get( p_playlist, "loop", &val );
536 srand( (unsigned int)mdate() );
538 while( i_count < p_playlist->i_size )
540 p_playlist->i_index =
541 (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
542 /* Check if the item has not already been played */
543 if( p_playlist->pp_items[p_playlist->i_index]->i_nb_played == 0 )
547 if( i_count == p_playlist->i_size )
549 /* The whole playlist has been played: reset the counters */
552 p_playlist->pp_items[--i_count]->i_nb_played = 0;
556 p_playlist->i_status = PLAYLIST_STOPPED;
562 p_playlist->i_index += i_arg;
566 if( p_playlist->i_index >= p_playlist->i_size )
568 if( p_playlist->i_status == PLAYLIST_STOPPED || b_random || b_loop )
570 p_playlist->i_index -= p_playlist->i_size
571 * ( p_playlist->i_index / p_playlist->i_size );
575 /* Don't loop by default: stop at playlist end */
576 p_playlist->i_index = i_oldindex;
577 p_playlist->i_status = PLAYLIST_STOPPED;
580 else if( p_playlist->i_index < 0 )
582 p_playlist->i_index = p_playlist->i_size - 1;
585 /* Check that the item is enabled */
586 if( p_playlist->pp_items[p_playlist->i_index]->b_enabled == VLC_FALSE &&
587 p_playlist->i_enabled != 0)
589 SkipItem( p_playlist , 1 );
593 /*****************************************************************************
594 * PlayItem: play current playlist item
595 *****************************************************************************
596 * This function calculates the position of the next playlist item, depending
597 * on the playlist course mode (forward, backward, random...).
598 *****************************************************************************/
599 static void PlayItem( playlist_t *p_playlist )
601 playlist_item_t *p_item;
603 if( p_playlist->i_index == -1 )
605 if( p_playlist->i_size == 0 || p_playlist->i_enabled == 0)
609 SkipItem( p_playlist, 1 );
611 if( p_playlist->i_enabled == 0)
616 msg_Dbg( p_playlist, "creating new input thread" );
617 p_item = p_playlist->pp_items[p_playlist->i_index];
619 p_item->i_nb_played++;
620 p_playlist->p_input = input_CreateThread( p_playlist, &p_item->input );
622 val.i_int = p_playlist->i_index;
623 /* unlock the playlist to set the var...mmm */
624 vlc_mutex_unlock( &p_playlist->object_lock);
625 var_Set( p_playlist, "playlist-current", val);
626 vlc_mutex_lock( &p_playlist->object_lock);