1 /*****************************************************************************
2 * playlist.c : Playlist management functions
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
5 * $Id: playlist.c,v 1.78 2004/01/26 23:30:18 fenrir 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() */
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 void ObjectGarbageCollector( playlist_t *p_playlist,
267 mtime_t *pi_obj_destroyed_date )
270 if( *pi_obj_destroyed_date > mdate() )
275 if( *pi_obj_destroyed_date == 0 )
277 /* give a little time */
278 *pi_obj_destroyed_date = mdate() + I64C(300000);
282 while( ( p_obj = vlc_object_find( p_playlist,
286 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
288 /* only first chiled (ie unused) */
289 vlc_object_release( p_obj );
292 if( i_type == VLC_OBJECT_VOUT )
294 msg_Dbg( p_playlist, "vout garbage collector destroying 1 vout" );
295 vlc_object_detach( p_obj );
296 vlc_object_release( p_obj );
297 vout_Destroy( (vout_thread_t *)p_obj );
299 else if( i_type == VLC_OBJECT_SOUT )
301 vlc_object_release( p_obj );
302 sout_DeleteInstance( (sout_instance_t*)p_obj );
305 *pi_obj_destroyed_date = 0;
309 /*****************************************************************************
310 * RunThread: main playlist thread
311 *****************************************************************************/
312 static void RunThread ( playlist_t *p_playlist )
317 mtime_t i_vout_destroyed_date = 0;
318 mtime_t i_sout_destroyed_date = 0;
320 /* Tell above that we're ready */
321 vlc_thread_ready( p_playlist );
323 while( !p_playlist->b_die )
325 vlc_mutex_lock( &p_playlist->object_lock );
327 /* If there is an input, check that it doesn't need to die. */
328 if( p_playlist->p_input )
330 /* This input is dead. Remove it ! */
331 if( p_playlist->p_input->b_dead )
333 input_thread_t *p_input;
335 p_input = p_playlist->p_input;
336 p_playlist->p_input = NULL;
338 /* Release the playlist lock, because we may get stuck
339 * in input_DestroyThread() for some time. */
340 vlc_mutex_unlock( &p_playlist->object_lock );
343 input_DestroyThread( p_input );
345 /* Unlink current input
346 * (_after_ input_DestroyThread for vout garbage collector) */
347 vlc_object_detach( p_input );
350 vlc_object_destroy( p_input );
352 i_vout_destroyed_date = 0;
353 i_sout_destroyed_date = 0;
356 /* This input is dying, let him do */
357 else if( p_playlist->p_input->b_die )
361 /* This input has finished, ask him to die ! */
362 else if( p_playlist->p_input->b_error
363 || p_playlist->p_input->b_eof )
365 /* Check for autodeletion */
366 if( p_playlist->pp_items[p_playlist->i_index]->b_autodeletion )
368 vlc_mutex_unlock( &p_playlist->object_lock );
369 playlist_Delete( p_playlist, p_playlist->i_index );
370 p_playlist->i_index++;
371 p_playlist->i_status = PLAYLIST_RUNNING;
375 /* Select the next playlist item */
376 SkipItem( p_playlist, 1 );
377 input_StopThread( p_playlist->p_input );
378 vlc_mutex_unlock( &p_playlist->object_lock );
382 else if( p_playlist->p_input->stream.control.i_status != INIT_S )
384 vlc_mutex_unlock( &p_playlist->object_lock );
385 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
386 &i_vout_destroyed_date );
387 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
388 &i_sout_destroyed_date );
389 vlc_mutex_lock( &p_playlist->object_lock );
392 else if( p_playlist->i_status != PLAYLIST_STOPPED )
394 var_Get( p_playlist, "prevent-skip", &val);
395 if( val.b_bool == VLC_FALSE)
397 SkipItem( p_playlist, 0 );
399 val.b_bool = VLC_TRUE;
400 var_Set( p_playlist, "prevent-skip", val);
401 PlayItem( p_playlist );
403 else if( p_playlist->i_status == PLAYLIST_STOPPED )
405 vlc_mutex_unlock( &p_playlist->object_lock );
406 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
407 &i_sout_destroyed_date );
408 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
409 &i_vout_destroyed_date );
410 vlc_mutex_lock( &p_playlist->object_lock );
412 vlc_mutex_unlock( &p_playlist->object_lock );
414 msleep( INTF_IDLE_SLEEP );
417 /* If there is an input, kill it */
420 vlc_mutex_lock( &p_playlist->object_lock );
422 if( p_playlist->p_input == NULL )
424 vlc_mutex_unlock( &p_playlist->object_lock );
428 if( p_playlist->p_input->b_dead )
430 input_thread_t *p_input;
432 /* Unlink current input */
433 p_input = p_playlist->p_input;
434 p_playlist->p_input = NULL;
435 vlc_mutex_unlock( &p_playlist->object_lock );
438 input_DestroyThread( p_input );
439 /* Unlink current input (_after_ input_DestroyThread for vout
440 * garbage collector)*/
441 vlc_object_detach( p_input );
444 vlc_object_destroy( p_input );
447 else if( p_playlist->p_input->b_die )
449 /* This input is dying, leave him alone */
452 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
454 input_StopThread( p_playlist->p_input );
455 vlc_mutex_unlock( &p_playlist->object_lock );
460 p_playlist->p_input->b_eof = 1;
463 vlc_mutex_unlock( &p_playlist->object_lock );
465 msleep( INTF_IDLE_SLEEP );
468 /* close all remaining sout */
469 while( ( p_obj = vlc_object_find( p_playlist,
470 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
472 vlc_object_release( p_obj );
473 sout_DeleteInstance( (sout_instance_t*)p_obj );
476 /* close all remaining vout */
477 while( ( p_obj = vlc_object_find( p_playlist,
478 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
480 vlc_object_detach( p_obj );
481 vlc_object_release( p_obj );
482 vout_Destroy( (vout_thread_t *)p_obj );
486 /*****************************************************************************
487 * SkipItem: go to Xth playlist item
488 *****************************************************************************
489 * This function calculates the position of the next playlist item, depending
490 * on the playlist course mode (forward, backward, random...).
491 *****************************************************************************/
492 static void SkipItem( playlist_t *p_playlist, int i_arg )
494 int i_oldindex = p_playlist->i_index;
495 vlc_bool_t b_random, b_repeat, b_loop;
498 /* If the playlist is empty, there is no current item */
499 if( p_playlist->i_size == 0 )
501 p_playlist->i_index = -1;
505 var_Get( p_playlist, "random", &val );
506 b_random = val.b_bool;
507 var_Get( p_playlist, "repeat", &val );
508 b_repeat = val.b_bool;
509 var_Get( p_playlist, "loop", &val );
515 srand( (unsigned int)mdate() );
517 /* Simple random stuff - we cheat a bit to minimize the chances to
518 * get the same index again. */
519 i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
522 i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
529 p_playlist->i_index += i_arg;
532 if( p_playlist->i_index >= p_playlist->i_size )
534 if( p_playlist->i_status == PLAYLIST_STOPPED
538 p_playlist->i_index -= p_playlist->i_size
539 * ( p_playlist->i_index / p_playlist->i_size );
543 /* Don't loop by default: stop at playlist end */
544 p_playlist->i_index = i_oldindex;
545 p_playlist->i_status = PLAYLIST_STOPPED;
548 else if( p_playlist->i_index < 0 )
550 p_playlist->i_index = p_playlist->i_size - 1;
553 /* Check that the item is enabled */
554 if( p_playlist->pp_items[p_playlist->i_index]->b_enabled == VLC_FALSE &&
555 p_playlist->i_enabled != 0)
557 SkipItem( p_playlist , 1 );
561 /*****************************************************************************
562 * PlayItem: play current playlist item
563 *****************************************************************************
564 * This function calculates the position of the next playlist item, depending
565 * on the playlist course mode (forward, backward, random...).
566 *****************************************************************************/
567 static void PlayItem( playlist_t *p_playlist )
569 playlist_item_t *p_item;
574 if( p_playlist->i_index == -1 )
576 if( p_playlist->i_size == 0 || p_playlist->i_enabled == 0)
580 SkipItem( p_playlist, 1 );
582 if( p_playlist->i_enabled == 0)
587 msg_Dbg( p_playlist, "creating new input thread" );
588 p_item = p_playlist->pp_items[p_playlist->i_index];
593 /* Beurk, who the hell have done that ???????, why moving options
594 * to playlist in a such *bad* way ? --fenrir_is_asking ...*/
595 /* Parse input options */
596 for( i = 0 ; i < p_item->i_categories ; i++ )
598 if( !strcmp( p_item->pp_categories[i]->psz_name, _("Options") ) )
600 msg_Dbg( p_playlist, "Parsing %i options for item", p_item->pp_categories[i]->i_infos );
601 for( j = 0; j< p_item->pp_categories[i]->i_infos ; j++ )
603 msg_Dbg( p_playlist, "Option : %s",
604 p_item->pp_categories[i]->pp_infos[j]->psz_value );
605 TAB_APPEND( i_options, ppsz_options,
606 p_item->pp_categories[i]->pp_infos[j]->psz_value );
612 p_playlist->p_input = input_CreateThread( p_playlist, p_item->psz_uri,
613 ppsz_options, i_options );
617 free( ppsz_options );
620 val.i_int = p_playlist->i_index;
621 var_Set( p_playlist, "playlist-current", val);