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, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
90 var_Create( p_playlist, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
91 var_Create( p_playlist, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
93 p_playlist->p_input = NULL;
94 p_playlist->i_status = PLAYLIST_STOPPED;
95 p_playlist->i_index = -1;
96 p_playlist->i_size = 0;
97 p_playlist->pp_items = NULL;
99 p_playlist->i_groups = 0;
100 p_playlist->pp_groups = NULL;
101 p_playlist->i_last_group = 0;
102 p_playlist->i_last_id = 0;
103 p_playlist->i_sort = SORT_ID;
104 p_playlist->i_order = ORDER_NORMAL;
106 playlist_CreateGroup( p_playlist, _("Normal") );
108 if( vlc_thread_create( p_playlist, "playlist", RunThread,
109 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
111 msg_Err( p_playlist, "cannot spawn playlist thread" );
112 vlc_object_destroy( p_playlist );
116 /* The object has been initialized, now attach it */
117 vlc_object_attach( p_playlist, p_parent );
123 * Destroy the playlist.
125 * Delete all items in the playlist and free the playlist structure.
126 * \param p_playlist the playlist structure to destroy
128 void playlist_Destroy( playlist_t * p_playlist )
130 p_playlist->b_die = 1;
132 vlc_thread_join( p_playlist );
134 var_Destroy( p_playlist, "intf-change" );
135 var_Destroy( p_playlist, "item-change" );
136 var_Destroy( p_playlist, "playlist-current" );
137 var_Destroy( p_playlist, "intf-popmenu" );
138 var_Destroy( p_playlist, "intf-show" );
139 var_Destroy( p_playlist, "prevent-skip" );
140 var_Destroy( p_playlist, "random" );
141 var_Destroy( p_playlist, "repeat" );
142 var_Destroy( p_playlist, "loop" );
144 while( p_playlist->i_groups > 0 )
146 playlist_DeleteGroup( p_playlist, p_playlist->pp_groups[0]->i_id );
149 while( p_playlist->i_size > 0 )
151 playlist_Delete( p_playlist, 0 );
154 vlc_object_destroy( p_playlist );
159 * Do a playlist action
161 * \param p_playlist the playlist to do the command on
162 * \param i_command the command to do
163 * \param i_arg the argument to the command. See playlist_command_t for details
165 void playlist_Command( playlist_t * p_playlist, playlist_command_t i_command,
170 vlc_mutex_lock( &p_playlist->object_lock );
175 p_playlist->i_status = PLAYLIST_STOPPED;
176 if( p_playlist->p_input )
178 input_StopThread( p_playlist->p_input );
179 val.i_int = p_playlist->i_index;
180 var_Set( p_playlist, "item-change",val );
185 p_playlist->i_status = PLAYLIST_RUNNING;
186 if( !p_playlist->p_input && p_playlist->i_enabled != 0 )
188 PlayItem( p_playlist );
190 if( p_playlist->p_input )
192 val.i_int = PLAYING_S;
193 var_Set( p_playlist->p_input, "state", val );
199 if( p_playlist->p_input )
200 var_Get( p_playlist->p_input, "state", &val );
202 if( val.i_int == PAUSE_S )
204 p_playlist->i_status = PLAYLIST_RUNNING;
205 if( p_playlist->p_input )
207 val.i_int = PLAYING_S;
208 var_Set( p_playlist->p_input, "state", val );
213 p_playlist->i_status = PLAYLIST_PAUSED;
214 if( p_playlist->p_input )
217 var_Set( p_playlist->p_input, "state", val );
223 p_playlist->i_status = PLAYLIST_STOPPED;
224 if( p_playlist->i_enabled == 0)
228 SkipItem( p_playlist, i_arg );
229 if( p_playlist->p_input )
231 input_StopThread( p_playlist->p_input );
233 p_playlist->i_status = PLAYLIST_RUNNING;
237 if( i_arg >= 0 && i_arg < p_playlist->i_size &&
238 p_playlist->i_enabled != 0 )
240 p_playlist->i_index = i_arg;
241 if( p_playlist->p_input )
243 input_StopThread( p_playlist->p_input );
245 val.b_bool = VLC_TRUE;
246 var_Set( p_playlist, "prevent-skip", val );
247 p_playlist->i_status = PLAYLIST_RUNNING;
252 msg_Err( p_playlist, "unknown playlist command" );
256 vlc_mutex_unlock( &p_playlist->object_lock );
258 val.b_bool = VLC_TRUE;
259 var_Set( p_playlist, "intf-change", val );
265 static mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
266 mtime_t destroy_date )
270 if( destroy_date > mdate() ) return destroy_date;
272 if( destroy_date == 0 )
274 /* give a little time */
275 return mdate() + I64C(1000000);
279 while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
281 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
283 /* only first child (ie unused) */
284 vlc_object_release( p_obj );
287 if( i_type == VLC_OBJECT_VOUT )
289 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
290 vlc_object_detach( p_obj );
291 vlc_object_release( p_obj );
292 vout_Destroy( (vout_thread_t *)p_obj );
294 else if( i_type == VLC_OBJECT_SOUT )
296 vlc_object_release( p_obj );
297 sout_DeleteInstance( (sout_instance_t*)p_obj );
304 /*****************************************************************************
305 * RunThread: main playlist thread
306 *****************************************************************************/
307 static void RunThread ( playlist_t *p_playlist )
312 mtime_t i_vout_destroyed_date = 0;
313 mtime_t i_sout_destroyed_date = 0;
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 p_input = p_playlist->p_input;
331 p_playlist->p_input = NULL;
333 /* Release the playlist lock, because we may get stuck
334 * in input_DestroyThread() for some time. */
335 vlc_mutex_unlock( &p_playlist->object_lock );
338 input_DestroyThread( p_input );
340 /* Unlink current input
341 * (_after_ input_DestroyThread for vout garbage collector) */
342 vlc_object_detach( p_input );
345 vlc_object_destroy( p_input );
347 i_vout_destroyed_date = 0;
348 i_sout_destroyed_date = 0;
351 /* This input is dying, let him do */
352 else if( p_playlist->p_input->b_die )
356 /* This input has finished, ask him to die ! */
357 else if( p_playlist->p_input->b_error
358 || p_playlist->p_input->b_eof )
360 /* Check for autodeletion */
361 if( p_playlist->pp_items[p_playlist->i_index]->b_autodeletion )
363 vlc_mutex_unlock( &p_playlist->object_lock );
364 playlist_Delete( p_playlist, p_playlist->i_index );
365 p_playlist->i_index++;
366 p_playlist->i_status = PLAYLIST_RUNNING;
370 /* Select the next playlist item */
371 SkipItem( p_playlist, 1 );
372 input_StopThread( p_playlist->p_input );
373 vlc_mutex_unlock( &p_playlist->object_lock );
377 else if( p_playlist->p_input->stream.control.i_status != INIT_S )
379 vlc_mutex_unlock( &p_playlist->object_lock );
380 i_vout_destroyed_date =
381 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
382 i_vout_destroyed_date );
383 i_sout_destroyed_date =
384 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
385 i_sout_destroyed_date );
386 vlc_mutex_lock( &p_playlist->object_lock );
389 else if( p_playlist->i_status != PLAYLIST_STOPPED )
391 var_Get( p_playlist, "prevent-skip", &val);
392 if( val.b_bool == VLC_FALSE)
394 SkipItem( p_playlist, 0 );
396 val.b_bool = VLC_TRUE;
397 var_Set( p_playlist, "prevent-skip", val);
398 PlayItem( p_playlist );
400 else if( p_playlist->i_status == PLAYLIST_STOPPED )
402 vlc_mutex_unlock( &p_playlist->object_lock );
403 i_sout_destroyed_date =
404 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
405 i_vout_destroyed_date =
406 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
407 vlc_mutex_lock( &p_playlist->object_lock );
409 vlc_mutex_unlock( &p_playlist->object_lock );
411 msleep( INTF_IDLE_SLEEP );
414 /* If there is an input, kill it */
417 vlc_mutex_lock( &p_playlist->object_lock );
419 if( p_playlist->p_input == NULL )
421 vlc_mutex_unlock( &p_playlist->object_lock );
425 if( p_playlist->p_input->b_dead )
427 input_thread_t *p_input;
429 /* Unlink current input */
430 p_input = p_playlist->p_input;
431 p_playlist->p_input = NULL;
432 vlc_mutex_unlock( &p_playlist->object_lock );
435 input_DestroyThread( p_input );
436 /* Unlink current input (_after_ input_DestroyThread for vout
437 * garbage collector)*/
438 vlc_object_detach( p_input );
441 vlc_object_destroy( p_input );
444 else if( p_playlist->p_input->b_die )
446 /* This input is dying, leave him alone */
449 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
451 input_StopThread( p_playlist->p_input );
452 vlc_mutex_unlock( &p_playlist->object_lock );
457 p_playlist->p_input->b_eof = 1;
460 vlc_mutex_unlock( &p_playlist->object_lock );
462 msleep( INTF_IDLE_SLEEP );
465 /* close all remaining sout */
466 while( ( p_obj = vlc_object_find( p_playlist,
467 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
469 vlc_object_release( p_obj );
470 sout_DeleteInstance( (sout_instance_t*)p_obj );
473 /* close all remaining vout */
474 while( ( p_obj = vlc_object_find( p_playlist,
475 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
477 vlc_object_detach( p_obj );
478 vlc_object_release( p_obj );
479 vout_Destroy( (vout_thread_t *)p_obj );
483 /*****************************************************************************
484 * SkipItem: go to Xth playlist item
485 *****************************************************************************
486 * This function calculates the position of the next playlist item, depending
487 * on the playlist course mode (forward, backward, random...).
488 *****************************************************************************/
489 static void SkipItem( playlist_t *p_playlist, int i_arg )
491 int i_oldindex = p_playlist->i_index;
492 vlc_bool_t b_random, b_repeat, b_loop;
495 /* If the playlist is empty, there is no current item */
496 if( p_playlist->i_size == 0 )
498 p_playlist->i_index = -1;
502 var_Get( p_playlist, "random", &val );
503 b_random = val.b_bool;
504 var_Get( p_playlist, "repeat", &val );
505 b_repeat = val.b_bool;
506 var_Get( p_playlist, "loop", &val );
512 srand( (unsigned int)mdate() );
514 /* Simple random stuff - we cheat a bit to minimize the chances to
515 * get the same index again. */
516 i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
519 i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
526 p_playlist->i_index += i_arg;
529 if( p_playlist->i_index >= p_playlist->i_size )
531 if( p_playlist->i_status == PLAYLIST_STOPPED || b_random || b_loop )
533 p_playlist->i_index -= p_playlist->i_size
534 * ( p_playlist->i_index / p_playlist->i_size );
538 /* Don't loop by default: stop at playlist end */
539 p_playlist->i_index = i_oldindex;
540 p_playlist->i_status = PLAYLIST_STOPPED;
543 else if( p_playlist->i_index < 0 )
545 p_playlist->i_index = p_playlist->i_size - 1;
548 /* Check that the item is enabled */
549 if( p_playlist->pp_items[p_playlist->i_index]->b_enabled == VLC_FALSE &&
550 p_playlist->i_enabled != 0)
552 SkipItem( p_playlist , 1 );
556 /*****************************************************************************
557 * PlayItem: play current playlist item
558 *****************************************************************************
559 * This function calculates the position of the next playlist item, depending
560 * on the playlist course mode (forward, backward, random...).
561 *****************************************************************************/
562 static void PlayItem( playlist_t *p_playlist )
564 playlist_item_t *p_item;
566 if( p_playlist->i_index == -1 )
568 if( p_playlist->i_size == 0 || p_playlist->i_enabled == 0)
572 SkipItem( p_playlist, 1 );
574 if( p_playlist->i_enabled == 0)
579 msg_Dbg( p_playlist, "creating new input thread" );
580 p_item = p_playlist->pp_items[p_playlist->i_index];
582 p_item->i_nb_played++;
583 p_playlist->p_input = input_CreateThread( p_playlist, p_item->psz_uri,
584 p_item->ppsz_options,
587 val.i_int = p_playlist->i_index;
588 /* unlock the playlist to set the var...mmm */
589 vlc_mutex_unlock( &p_playlist->object_lock);
590 var_Set( p_playlist, "playlist-current", val);
591 vlc_mutex_lock( &p_playlist->object_lock);