1 /*****************************************************************************
2 * playlist.c : Playlist management functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: playlist.c,v 1.59 2003/10/19 12:20:00 gbazin 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 if( vlc_thread_create( p_playlist, "playlist", RunThread,
89 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
91 msg_Err( p_playlist, "cannot spawn playlist thread" );
92 vlc_object_destroy( p_playlist );
96 /* The object has been initialized, now attach it */
97 vlc_object_attach( p_playlist, p_parent );
103 * Destroy the playlist.
105 * Delete all items in the playlist and free the playlist structure.
106 * \param p_playlist the playlist structure to destroy
108 void playlist_Destroy( playlist_t * p_playlist )
110 p_playlist->b_die = 1;
112 vlc_thread_join( p_playlist );
114 var_Destroy( p_playlist, "intf-change" );
116 vlc_object_destroy( p_playlist );
120 * Add an MRL to the playlist. This is a simplified version of
121 * playlist_AddExt inculded for convenince. It equals calling playlist_AddExt
122 * with psz_name == psz_target and i_duration == -1
125 int playlist_Add( playlist_t *p_playlist, const char *psz_target,
126 const char **ppsz_options, int i_options,
127 int i_mode, int i_pos )
129 return playlist_AddExt( p_playlist, psz_target, psz_target, -1,
130 ppsz_options, i_options, i_mode, i_pos );
134 * Add a MRL into the playlist.
136 * \param p_playlist the playlist to add into
137 * \param psz_uri the mrl to add to the playlist
138 * \param psz_name a text giving a name or description of this item
139 * \param i_duration a hint about the duration of this item, in miliseconds, or
141 * \param ppsz_options array of options
142 * \param i_options number of items in ppsz_options
143 * \param i_mode the mode used when adding
144 * \param i_pos the position in the playlist where to add. If this is
145 * PLAYLIST_END the item will be added at the end of the playlist
146 * regardless of it's size
147 * \return always returns 0
149 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
150 const char * psz_name, mtime_t i_duration,
151 const char **ppsz_options, int i_options, int i_mode,
154 playlist_item_t * p_item;
156 p_item = malloc( sizeof( playlist_item_t ) );
159 msg_Err( p_playlist, "out of memory" );
162 p_item->psz_name = strdup( psz_name );
163 p_item->psz_uri = strdup( psz_uri );
164 p_item->psz_author = strdup( "" );
165 p_item->i_duration = i_duration;
167 p_item->i_status = 0;
168 p_item->b_autodeletion = VLC_FALSE;
169 p_item->b_enabled = VLC_TRUE;
170 p_item->i_group = PLAYLIST_TYPE_MANUAL;
172 p_item->ppsz_options = NULL;
173 p_item->i_options = i_options;
179 p_item->ppsz_options = (char **)malloc( i_options * sizeof(char *) );
180 for( i = 0; i < i_options; i++ )
181 p_item->ppsz_options[i] = strdup( ppsz_options[i] );
185 return playlist_AddItem( p_playlist, p_item, i_mode, i_pos );
189 * Add a playlist item into a playlist
191 * \param p_playlist the playlist to insert into
192 * \param p_item the playlist item to insert
193 * \param i_mode the mode used when adding
194 * \param i_pos the possition in the playlist where to add. If this is
195 * PLAYLIST_END the item will be added at the end of the playlist
196 * regardless of it's size
197 * \return always returns 0
199 int playlist_AddItem( playlist_t *p_playlist, playlist_item_t * p_item,
200 int i_mode, int i_pos)
204 vlc_mutex_lock( &p_playlist->object_lock );
207 * CHECK_INSERT : checks if the item is already enqued before
210 if ( i_mode & PLAYLIST_CHECK_INSERT )
214 if ( p_playlist->pp_items )
216 for ( j = 0; j < p_playlist->i_size; j++ )
218 if ( !strcmp( p_playlist->pp_items[j]->psz_uri, p_item->psz_uri ) )
220 if( p_item->psz_name )
222 free( p_item->psz_name );
224 if( p_item->psz_uri )
226 free( p_item->psz_uri );
229 vlc_mutex_unlock( &p_playlist->object_lock );
234 i_mode &= ~PLAYLIST_CHECK_INSERT;
235 i_mode |= PLAYLIST_APPEND;
239 msg_Dbg( p_playlist, "adding playlist item « %s » ( %s )", p_item->psz_name, p_item->psz_uri);
241 /* Create the new playlist item */
244 /* Do a few boundary checks and allocate space for the item */
245 if( i_pos == PLAYLIST_END )
247 if( i_mode & PLAYLIST_INSERT )
249 i_mode &= ~PLAYLIST_INSERT;
250 i_mode |= PLAYLIST_APPEND;
253 i_pos = p_playlist->i_size - 1;
256 if( !(i_mode & PLAYLIST_REPLACE)
257 || i_pos < 0 || i_pos >= p_playlist->i_size )
259 /* Additional boundary checks */
260 if( i_mode & PLAYLIST_APPEND )
269 else if( i_pos > p_playlist->i_size )
271 i_pos = p_playlist->i_size;
274 INSERT_ELEM( p_playlist->pp_items,
278 p_playlist->i_enabled ++;
280 if( p_playlist->i_index >= i_pos )
282 p_playlist->i_index++;
287 /* i_mode == PLAYLIST_REPLACE and 0 <= i_pos < p_playlist->i_size */
288 if( p_playlist->pp_items[i_pos]->psz_name )
290 free( p_playlist->pp_items[i_pos]->psz_name );
292 if( p_playlist->pp_items[i_pos]->psz_uri )
294 free( p_playlist->pp_items[i_pos]->psz_uri );
296 /* XXX: what if the item is still in use? */
297 free( p_playlist->pp_items[i_pos] );
298 p_playlist->pp_items[i_pos] = p_item;
301 if( i_mode & PLAYLIST_GO )
303 p_playlist->i_index = i_pos;
304 if( p_playlist->p_input )
306 input_StopThread( p_playlist->p_input );
308 p_playlist->i_status = PLAYLIST_RUNNING;
311 vlc_mutex_unlock( &p_playlist->object_lock );
313 val.b_bool = VLC_TRUE;
314 var_Set( p_playlist, "intf-change", val );
320 * delete an item from a playlist.
322 * \param p_playlist the playlist to remove from.
323 * \param i_pos the position of the item to remove
326 int playlist_Delete( playlist_t * p_playlist, int i_pos )
329 vlc_mutex_lock( &p_playlist->object_lock );
331 if( i_pos >= 0 && i_pos < p_playlist->i_size )
333 msg_Dbg( p_playlist, "deleting playlist item « %s »",
334 p_playlist->pp_items[i_pos]->psz_name );
336 if( p_playlist->pp_items[i_pos]->psz_name )
338 free( p_playlist->pp_items[i_pos]->psz_name );
340 if( p_playlist->pp_items[i_pos]->psz_uri )
342 free( p_playlist->pp_items[i_pos]->psz_uri );
344 if( p_playlist->pp_items[i_pos]->i_options )
348 for( i = 0; i < p_playlist->pp_items[i_pos]->i_options; i++ )
349 free( p_playlist->pp_items[i_pos]->ppsz_options[i] );
351 free( p_playlist->pp_items[i_pos]->ppsz_options );
354 /* XXX: what if the item is still in use? */
355 free( p_playlist->pp_items[i_pos] );
357 if( i_pos <= p_playlist->i_index )
359 p_playlist->i_index--;
362 /* Renumber the playlist */
363 REMOVE_ELEM( p_playlist->pp_items,
366 if( p_playlist->i_enabled > 0 )
367 p_playlist->i_enabled--;
370 vlc_mutex_unlock( &p_playlist->object_lock );
372 val.b_bool = VLC_TRUE;
373 var_Set( p_playlist, "intf-change", val );
379 * Disables a playlist item
381 * \param p_playlist the playlist to disable from.
382 * \param i_pos the position of the item to disable
385 int playlist_Disable( playlist_t * p_playlist, int i_pos )
388 vlc_mutex_lock( &p_playlist->object_lock );
391 if( i_pos >= 0 && i_pos < p_playlist->i_size )
393 msg_Dbg( p_playlist, "disabling playlist item « %s »",
394 p_playlist->pp_items[i_pos]->psz_name );
396 if( p_playlist->pp_items[i_pos]->b_enabled == VLC_TRUE )
397 p_playlist->i_enabled--;
398 p_playlist->pp_items[i_pos]->b_enabled = VLC_FALSE;
401 vlc_mutex_unlock( &p_playlist->object_lock );
403 val.b_bool = VLC_TRUE;
404 var_Set( p_playlist, "intf-change", val );
410 * Enables a playlist item
412 * \param p_playlist the playlist to enable from.
413 * \param i_pos the position of the item to enable
416 int playlist_Enable( playlist_t * p_playlist, int i_pos )
419 vlc_mutex_lock( &p_playlist->object_lock );
421 if( i_pos >= 0 && i_pos < p_playlist->i_size )
423 msg_Dbg( p_playlist, "enabling playlist item « %s »",
424 p_playlist->pp_items[i_pos]->psz_name );
426 if( p_playlist->pp_items[i_pos]->b_enabled == VLC_FALSE )
427 p_playlist->i_enabled++;
429 p_playlist->pp_items[i_pos]->b_enabled = VLC_TRUE;
432 vlc_mutex_unlock( &p_playlist->object_lock );
434 val.b_bool = VLC_TRUE;
435 var_Set( p_playlist, "intf-change", val );
441 * Disables a playlist group
443 * \param p_playlist the playlist to disable from.
444 * \param i_pos the id of the group to disable
447 int playlist_DisableGroup( playlist_t * p_playlist, int i_group)
451 vlc_mutex_lock( &p_playlist->object_lock );
453 msg_Dbg(p_playlist,"Disabling group %i",i_group);
454 for( i = 0 ; i< p_playlist->i_size; i++ )
456 if( p_playlist->pp_items[i]->i_group == i_group )
458 msg_Dbg( p_playlist, "disabling playlist item « %s »",
459 p_playlist->pp_items[i]->psz_name );
461 if( p_playlist->pp_items[i]->b_enabled == VLC_TRUE )
462 p_playlist->i_enabled--;
464 p_playlist->pp_items[i]->b_enabled = VLC_FALSE;
467 vlc_mutex_unlock( &p_playlist->object_lock );
469 val.b_bool = VLC_TRUE;
470 var_Set( p_playlist, "intf-change", val );
476 * Enables a playlist group
478 * \param p_playlist the playlist to enable from.
479 * \param i_pos the id of the group to enable
482 int playlist_EnableGroup( playlist_t * p_playlist, int i_group)
486 vlc_mutex_lock( &p_playlist->object_lock );
488 for( i = 0 ; i< p_playlist->i_size; i++ )
490 if( p_playlist->pp_items[i]->i_group == i_group )
492 msg_Dbg( p_playlist, "enabling playlist item « %s »",
493 p_playlist->pp_items[i]->psz_name );
495 if( p_playlist->pp_items[i]->b_enabled == VLC_FALSE )
496 p_playlist->i_enabled++;
498 p_playlist->pp_items[i]->b_enabled = VLC_TRUE;
501 vlc_mutex_unlock( &p_playlist->object_lock );
503 val.b_bool = VLC_TRUE;
504 var_Set( p_playlist, "intf-change", val );
513 int playlist_Sort( playlist_t * p_playlist , int i_type )
515 int i , i_small , i_position;
516 playlist_item_t *p_temp;
518 vlc_mutex_lock( &p_playlist->object_lock );
520 for( i_position = 0; i_position < p_playlist->i_size -1 ; i_position ++ )
522 i_small = i_position;
523 for( i = i_position + 1 ; i< p_playlist->i_size ; i++)
527 i_test = strcasecmp( p_playlist->pp_items[i]->psz_name,
528 p_playlist->pp_items[i_small]->psz_name );
530 if( ( i_type == SORT_NORMAL && i_test < 0 ) ||
531 ( i_type == SORT_REVERSE && i_test > 0 ) )
536 /* Keep the correct current index */
537 if( i_small == p_playlist->i_index )
538 p_playlist->i_index = i_position;
539 else if( i_position == p_playlist->i_index )
540 p_playlist->i_index = i_small;
542 p_temp = p_playlist->pp_items[i_position];
543 p_playlist->pp_items[i_position] = p_playlist->pp_items[i_small];
544 p_playlist->pp_items[i_small] = p_temp;
546 vlc_mutex_unlock( &p_playlist->object_lock );
552 * Move an item in a playlist
554 * Move the item in the playlist with position i_pos before the current item
555 * at position i_newpos.
556 * \param p_playlist the playlist to move items in
557 * \param i_pos the position of the item to move
558 * \param i_newpos the position of the item that will be behind the moved item
562 int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos)
565 vlc_mutex_lock( &p_playlist->object_lock );
567 /* take into account that our own row disappears. */
568 if ( i_pos < i_newpos ) i_newpos--;
570 if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size
571 && i_newpos <= p_playlist->i_size )
573 playlist_item_t * temp;
575 msg_Dbg( p_playlist, "moving playlist item « %s »",
576 p_playlist->pp_items[i_pos]->psz_name );
578 if( i_pos == p_playlist->i_index )
580 p_playlist->i_index = i_newpos;
582 else if( i_pos > p_playlist->i_index && i_newpos <= p_playlist->i_index )
584 p_playlist->i_index++;
586 else if( i_pos < p_playlist->i_index && i_newpos >= p_playlist->i_index )
588 p_playlist->i_index--;
591 if ( i_pos < i_newpos )
593 temp = p_playlist->pp_items[i_pos];
594 while ( i_pos < i_newpos )
596 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
599 p_playlist->pp_items[i_newpos] = temp;
601 else if ( i_pos > i_newpos )
603 temp = p_playlist->pp_items[i_pos];
604 while ( i_pos > i_newpos )
606 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
609 p_playlist->pp_items[i_newpos] = temp;
613 vlc_mutex_unlock( &p_playlist->object_lock );
615 val.b_bool = VLC_TRUE;
616 var_Set( p_playlist, "intf-change", val );
622 * Do a playlist action
624 * \param p_playlist the playlist to do the command on
625 * \param i_command the command to do
626 * \param i_arg the argument to the command. See playlist_command_t for details
628 void playlist_Command( playlist_t * p_playlist, playlist_command_t i_command,
633 vlc_mutex_lock( &p_playlist->object_lock );
638 p_playlist->i_status = PLAYLIST_STOPPED;
639 if( p_playlist->p_input )
641 input_StopThread( p_playlist->p_input );
646 p_playlist->i_status = PLAYLIST_RUNNING;
647 if( !p_playlist->p_input && p_playlist->i_enabled != 0 )
649 PlayItem( p_playlist );
651 if( p_playlist->p_input )
653 val.i_int = PLAYING_S;
654 var_Set( p_playlist->p_input, "state", val );
659 p_playlist->i_status = PLAYLIST_PAUSED;
660 if( p_playlist->p_input )
663 var_Set( p_playlist->p_input, "state", val );
668 p_playlist->i_status = PLAYLIST_STOPPED;
669 if( p_playlist->i_enabled == 0)
673 SkipItem( p_playlist, i_arg );
674 if( p_playlist->p_input )
676 input_StopThread( p_playlist->p_input );
678 p_playlist->i_status = PLAYLIST_RUNNING;
682 if( i_arg >= 0 && i_arg < p_playlist->i_size &&
683 p_playlist->i_enabled != 0 )
685 p_playlist->i_index = i_arg;
686 if( p_playlist->p_input )
688 input_StopThread( p_playlist->p_input );
690 p_playlist->i_status = PLAYLIST_RUNNING;
695 msg_Err( p_playlist, "unknown playlist command" );
699 vlc_mutex_unlock( &p_playlist->object_lock );
701 val.b_bool = VLC_TRUE;
702 var_Set( p_playlist, "intf-change", val );
706 /* Following functions are local */
708 static void ObjectGarbageCollector( playlist_t *p_playlist,
710 vlc_bool_t *pb_obj_destroyed,
711 mtime_t *pi_obj_destroyed_date )
714 if( *pb_obj_destroyed || *pi_obj_destroyed_date > mdate() )
719 if( *pi_obj_destroyed_date == 0 )
721 /* give a little time */
722 *pi_obj_destroyed_date = mdate() + 300000LL;
726 while( ( p_obj = vlc_object_find( p_playlist,
730 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
732 /* only first chiled (ie unused) */
733 vlc_object_release( p_obj );
736 if( i_type == VLC_OBJECT_VOUT )
738 msg_Dbg( p_playlist, "vout garbage collector destroying 1 vout" );
739 vlc_object_detach( p_obj );
740 vlc_object_release( p_obj );
741 vout_Destroy( (vout_thread_t *)p_obj );
743 else if( i_type == VLC_OBJECT_SOUT )
745 vlc_object_release( p_obj );
746 sout_DeleteInstance( (sout_instance_t*)p_obj );
749 *pb_obj_destroyed = VLC_TRUE;
753 /*****************************************************************************
754 * RunThread: main playlist thread
755 *****************************************************************************/
756 static void RunThread ( playlist_t *p_playlist )
759 vlc_bool_t b_vout_destroyed = VLC_FALSE; /*we do vout garbage collector */
760 mtime_t i_vout_destroyed_date = 0;
762 vlc_bool_t b_sout_destroyed = VLC_FALSE; /*we do vout garbage collector */
763 mtime_t i_sout_destroyed_date = 0;
765 /* Tell above that we're ready */
766 vlc_thread_ready( p_playlist );
768 while( !p_playlist->b_die )
770 vlc_mutex_lock( &p_playlist->object_lock );
772 /* If there is an input, check that it doesn't need to die. */
773 if( p_playlist->p_input )
775 /* This input is dead. Remove it ! */
776 if( p_playlist->p_input->b_dead )
778 input_thread_t *p_input;
780 p_input = p_playlist->p_input;
781 p_playlist->p_input = NULL;
783 /* Release the playlist lock, because we may get stuck
784 * in input_DestroyThread() for some time. */
785 vlc_mutex_unlock( &p_playlist->object_lock );
788 input_DestroyThread( p_input );
790 /* Unlink current input (_after_ input_DestroyThread for vout garbage collector)*/
791 vlc_object_detach( p_input );
794 vlc_object_destroy( p_input );
796 b_vout_destroyed = VLC_FALSE;
797 i_vout_destroyed_date = 0;
798 b_sout_destroyed = VLC_FALSE;
799 i_sout_destroyed_date = 0;
802 /* This input is dying, let him do */
803 else if( p_playlist->p_input->b_die )
807 /* This input has finished, ask him to die ! */
808 else if( p_playlist->p_input->b_error
809 || p_playlist->p_input->b_eof )
811 /* Check for autodeletion */
812 if( p_playlist->pp_items[p_playlist->i_index]->b_autodeletion )
814 vlc_mutex_unlock( &p_playlist->object_lock );
815 playlist_Delete( p_playlist, p_playlist->i_index );
816 vlc_mutex_lock( &p_playlist->object_lock );
819 /* Select the next playlist item */
820 SkipItem( p_playlist, 1 );
822 input_StopThread( p_playlist->p_input );
823 vlc_mutex_unlock( &p_playlist->object_lock );
826 else if( p_playlist->p_input->stream.control.i_status != INIT_S )
828 vlc_mutex_unlock( &p_playlist->object_lock );
829 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
831 &i_vout_destroyed_date );
832 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
834 &i_sout_destroyed_date );
835 vlc_mutex_lock( &p_playlist->object_lock );
838 else if( p_playlist->i_status != PLAYLIST_STOPPED )
840 SkipItem( p_playlist, 0 );
841 PlayItem( p_playlist );
843 else if( p_playlist->i_status == PLAYLIST_STOPPED )
845 vlc_mutex_unlock( &p_playlist->object_lock );
846 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
847 &b_sout_destroyed, &i_sout_destroyed_date );
848 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
849 &b_vout_destroyed, &i_vout_destroyed_date );
850 vlc_mutex_lock( &p_playlist->object_lock );
852 vlc_mutex_unlock( &p_playlist->object_lock );
854 msleep( INTF_IDLE_SLEEP );
857 /* If there is an input, kill it */
860 vlc_mutex_lock( &p_playlist->object_lock );
862 if( p_playlist->p_input == NULL )
864 vlc_mutex_unlock( &p_playlist->object_lock );
868 if( p_playlist->p_input->b_dead )
870 input_thread_t *p_input;
872 /* Unlink current input */
873 p_input = p_playlist->p_input;
874 p_playlist->p_input = NULL;
875 vlc_mutex_unlock( &p_playlist->object_lock );
878 input_DestroyThread( p_input );
879 /* Unlink current input (_after_ input_DestroyThread for vout
880 * garbage collector)*/
881 vlc_object_detach( p_input );
884 vlc_object_destroy( p_input );
887 else if( p_playlist->p_input->b_die )
889 /* This input is dying, leave him alone */
892 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
894 input_StopThread( p_playlist->p_input );
895 vlc_mutex_unlock( &p_playlist->object_lock );
900 p_playlist->p_input->b_eof = 1;
903 vlc_mutex_unlock( &p_playlist->object_lock );
905 msleep( INTF_IDLE_SLEEP );
908 /* close all remaining sout */
909 while( ( p_obj = vlc_object_find( p_playlist,
910 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
912 vlc_object_release( p_obj );
913 sout_DeleteInstance( (sout_instance_t*)p_obj );
916 /* close all remaining vout */
917 while( ( p_obj = vlc_object_find( p_playlist,
918 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
920 vlc_object_detach( p_obj );
921 vlc_object_release( p_obj );
922 vout_Destroy( (vout_thread_t *)p_obj );
926 /*****************************************************************************
927 * SkipItem: go to Xth playlist item
928 *****************************************************************************
929 * This function calculates the position of the next playlist item, depending
930 * on the playlist course mode (forward, backward, random...).
931 *****************************************************************************/
932 static void SkipItem( playlist_t *p_playlist, int i_arg )
934 int i_oldindex = p_playlist->i_index;
935 vlc_bool_t b_random, b_repeat, b_loop;
938 msg_Dbg(p_playlist,"%i",p_playlist->i_enabled);
939 /* If the playlist is empty, there is no current item */
940 if( p_playlist->i_size == 0 )
942 p_playlist->i_index = -1;
946 var_Get( p_playlist, "random", &val );
947 b_random = val.b_bool;
948 var_Get( p_playlist, "repeat", &val );
949 b_repeat = val.b_bool;
950 var_Get( p_playlist, "loop", &val );
956 srand( (unsigned int)mdate() );
958 /* Simple random stuff - we cheat a bit to minimize the chances to
959 * get the same index again. */
960 i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
963 i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
970 p_playlist->i_index += i_arg;
973 if( p_playlist->i_index >= p_playlist->i_size )
975 if( p_playlist->i_status == PLAYLIST_STOPPED
979 p_playlist->i_index -= p_playlist->i_size
980 * ( p_playlist->i_index / p_playlist->i_size );
984 /* Don't loop by default: stop at playlist end */
985 p_playlist->i_index = i_oldindex;
986 p_playlist->i_status = PLAYLIST_STOPPED;
989 else if( p_playlist->i_index < 0 )
991 p_playlist->i_index = p_playlist->i_size - 1;
994 /* Check that the item is enabled */
995 if( p_playlist->pp_items[p_playlist->i_index]->b_enabled == VLC_FALSE &&
996 p_playlist->i_enabled != 0)
998 SkipItem( p_playlist , 1 );
1002 /*****************************************************************************
1003 * PlayItem: play current playlist item
1004 *****************************************************************************
1005 * This function calculates the position of the next playlist item, depending
1006 * on the playlist course mode (forward, backward, random...).
1007 *****************************************************************************/
1008 static void PlayItem( playlist_t *p_playlist )
1010 if( p_playlist->i_index == -1 )
1012 if( p_playlist->i_size == 0 || p_playlist->i_enabled == 0)
1017 SkipItem( p_playlist, 1 );
1020 if( p_playlist->i_enabled == 0)
1025 msg_Dbg( p_playlist, "creating new input thread" );
1026 p_playlist->p_input = input_CreateThread( p_playlist,
1027 p_playlist->pp_items[p_playlist->i_index] );
1030 /*****************************************************************************
1031 * Poubellize: put an input thread in the trashcan
1032 *****************************************************************************
1034 *****************************************************************************/
1035 static void Poubellize ( playlist_t *p_playlist, input_thread_t *p_input )
1037 msg_Dbg( p_playlist, "poubellizing input %i\n", p_input->i_object_id );
1040 /*****************************************************************************
1041 * playlist_LoadFile: load a playlist file.
1042 ****************************************************************************/
1043 int playlist_LoadFile( playlist_t * p_playlist, const char *psz_filename )
1047 int i_current_status;
1051 msg_Dbg( p_playlist, "opening playlist file %s", psz_filename );
1053 file = fopen( psz_filename, "rt" );
1056 msg_Err( p_playlist, "playlist file %s does not exist", psz_filename );
1059 fseek( file, 0L, SEEK_SET );
1061 /* check the file is not empty */
1062 if ( ! fgets( line, 1024, file ) )
1064 msg_Err( p_playlist, "playlist file %s is empty", psz_filename );
1069 /* get rid of line feed */
1070 if( line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r' )
1072 line[strlen(line)-1] = (char)0;
1073 if( line[strlen(line)-1] == '\r' ) line[strlen(line)-1] = (char)0;
1075 /* check the file format is valid */
1076 if ( !strcmp ( line , PLAYLIST_FILE_HEADER_0_5 ) )
1080 else if( !strcmp ( line , PLAYLIST_FILE_HEADER_0_6 ) )
1086 msg_Err( p_playlist, "playlist file %s format is unsupported"
1093 i_current_status = p_playlist->i_status;
1094 if ( p_playlist->i_status != PLAYLIST_STOPPED )
1096 playlist_Stop ( p_playlist );
1099 /* delete current content of the playlist */
1100 for( i = p_playlist->i_size - 1; i >= 0; i-- )
1102 playlist_Delete ( p_playlist , i );
1105 /* simply add each line */
1106 while( fgets( line, 1024, file ) )
1108 /* ignore comments or empty lines */
1109 if( (line[0] == '#') || (line[0] == '\r') || (line[0] == '\n')
1110 || (line[0] == (char)0) )
1113 /* get rid of line feed */
1114 if( line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r' )
1116 line[strlen(line)-1] = (char)0;
1117 if( line[strlen(line)-1] == '\r' ) line[strlen(line)-1] = (char)0;
1121 playlist_Add ( p_playlist , (char *)&line ,
1122 0, 0, PLAYLIST_APPEND , PLAYLIST_END );
1126 msg_Warn( p_playlist, "Not supported yet");
1131 if ( i_current_status != PLAYLIST_STOPPED )
1133 playlist_Play ( p_playlist );
1141 /*****************************************************************************
1142 * playlist_SaveFile: Save a playlist in a file.
1143 *****************************************************************************/
1144 int playlist_SaveFile( playlist_t * p_playlist, const char * psz_filename )
1149 vlc_mutex_lock( &p_playlist->object_lock );
1151 msg_Dbg( p_playlist, "saving playlist file %s", psz_filename );
1153 file = fopen( psz_filename, "wt" );
1156 msg_Err( p_playlist , "could not create playlist file %s"
1160 /* Save is done in 0_5 mode at the moment*/
1162 fprintf( file , PLAYLIST_FILE_HEADER_0_5 "\n" );
1164 for ( i = 0 ; i < p_playlist->i_size ; i++ )
1166 fprintf( file , p_playlist->pp_items[i]->psz_uri );
1167 fprintf( file , "\n" );
1170 fprintf( file, PLAYLIST_FILE_HEADER_0_6 "\n" );
1172 for ( i=0 ; i< p_playlist->i_size ; i++ )
1174 fprintf( file, p_playlist->pp_items[i]->psz_uri );
1175 fprintf( file, "||" );
1176 fprintf( file, p_playlist->pp_items[i]->psz_name );
1177 fprintf( file, "||" );
1178 fprintf( file, "%i",p_playlist->pp_items[i]->b_enabled = VLC_TRUE ?
1180 fprintf( file, "||" );
1181 fprintf( file, "%i", p_playlist->pp_items[i]->i_group );
1182 fprintf( file, "||" );
1183 fprintf( file, p_playlist->pp_items[i]->psz_author );
1184 fprintf( file , "\n" );
1189 vlc_mutex_unlock( &p_playlist->object_lock );