1 /*****************************************************************************
2 * playlist.c : Playlist management functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: playlist.c,v 1.61 2003/10/20 22:28:26 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 )
761 vlc_bool_t b_vout_destroyed = VLC_FALSE; /*we do vout garbage collector */
762 mtime_t i_vout_destroyed_date = 0;
764 vlc_bool_t b_sout_destroyed = VLC_FALSE; /*we do vout garbage collector */
765 mtime_t i_sout_destroyed_date = 0;
767 /* Tell above that we're ready */
768 vlc_thread_ready( p_playlist );
770 while( !p_playlist->b_die )
772 vlc_mutex_lock( &p_playlist->object_lock );
774 /* If there is an input, check that it doesn't need to die. */
775 if( p_playlist->p_input )
777 /* This input is dead. Remove it ! */
778 if( p_playlist->p_input->b_dead )
780 input_thread_t *p_input;
782 p_input = p_playlist->p_input;
783 p_playlist->p_input = NULL;
785 /* Release the playlist lock, because we may get stuck
786 * in input_DestroyThread() for some time. */
787 vlc_mutex_unlock( &p_playlist->object_lock );
790 input_DestroyThread( p_input );
792 /* Unlink current input
793 * (_after_ input_DestroyThread for vout garbage collector) */
794 vlc_object_detach( p_input );
797 vlc_object_destroy( p_input );
799 b_vout_destroyed = VLC_FALSE;
800 i_vout_destroyed_date = 0;
801 b_sout_destroyed = VLC_FALSE;
802 i_sout_destroyed_date = 0;
805 /* This input is dying, let him do */
806 else if( p_playlist->p_input->b_die )
810 /* This input has finished, ask him to die ! */
811 else if( p_playlist->p_input->b_error
812 || p_playlist->p_input->b_eof )
814 /* Check for autodeletion */
815 if( p_playlist->pp_items[p_playlist->i_index]->b_autodeletion )
817 vlc_mutex_unlock( &p_playlist->object_lock );
818 playlist_Delete( p_playlist, p_playlist->i_index );
819 vlc_mutex_lock( &p_playlist->object_lock );
822 /* Select the next playlist item */
823 SkipItem( p_playlist, 1 );
825 input_StopThread( p_playlist->p_input );
826 vlc_mutex_unlock( &p_playlist->object_lock );
828 val.b_bool = VLC_TRUE;
829 var_Set( p_playlist, "intf-change", val );
832 else if( p_playlist->p_input->stream.control.i_status != INIT_S )
834 vlc_mutex_unlock( &p_playlist->object_lock );
835 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
837 &i_vout_destroyed_date );
838 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
840 &i_sout_destroyed_date );
841 vlc_mutex_lock( &p_playlist->object_lock );
844 else if( p_playlist->i_status != PLAYLIST_STOPPED )
846 SkipItem( p_playlist, 0 );
847 PlayItem( p_playlist );
849 else if( p_playlist->i_status == PLAYLIST_STOPPED )
851 vlc_mutex_unlock( &p_playlist->object_lock );
852 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
853 &b_sout_destroyed, &i_sout_destroyed_date );
854 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
855 &b_vout_destroyed, &i_vout_destroyed_date );
856 vlc_mutex_lock( &p_playlist->object_lock );
858 vlc_mutex_unlock( &p_playlist->object_lock );
860 msleep( INTF_IDLE_SLEEP );
863 /* If there is an input, kill it */
866 vlc_mutex_lock( &p_playlist->object_lock );
868 if( p_playlist->p_input == NULL )
870 vlc_mutex_unlock( &p_playlist->object_lock );
874 if( p_playlist->p_input->b_dead )
876 input_thread_t *p_input;
878 /* Unlink current input */
879 p_input = p_playlist->p_input;
880 p_playlist->p_input = NULL;
881 vlc_mutex_unlock( &p_playlist->object_lock );
884 input_DestroyThread( p_input );
885 /* Unlink current input (_after_ input_DestroyThread for vout
886 * garbage collector)*/
887 vlc_object_detach( p_input );
890 vlc_object_destroy( p_input );
893 else if( p_playlist->p_input->b_die )
895 /* This input is dying, leave him alone */
898 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
900 input_StopThread( p_playlist->p_input );
901 vlc_mutex_unlock( &p_playlist->object_lock );
906 p_playlist->p_input->b_eof = 1;
909 vlc_mutex_unlock( &p_playlist->object_lock );
911 msleep( INTF_IDLE_SLEEP );
914 /* close all remaining sout */
915 while( ( p_obj = vlc_object_find( p_playlist,
916 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
918 vlc_object_release( p_obj );
919 sout_DeleteInstance( (sout_instance_t*)p_obj );
922 /* close all remaining vout */
923 while( ( p_obj = vlc_object_find( p_playlist,
924 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
926 vlc_object_detach( p_obj );
927 vlc_object_release( p_obj );
928 vout_Destroy( (vout_thread_t *)p_obj );
932 /*****************************************************************************
933 * SkipItem: go to Xth playlist item
934 *****************************************************************************
935 * This function calculates the position of the next playlist item, depending
936 * on the playlist course mode (forward, backward, random...).
937 *****************************************************************************/
938 static void SkipItem( playlist_t *p_playlist, int i_arg )
940 int i_oldindex = p_playlist->i_index;
941 vlc_bool_t b_random, b_repeat, b_loop;
944 /* If the playlist is empty, there is no current item */
945 if( p_playlist->i_size == 0 )
947 p_playlist->i_index = -1;
951 var_Get( p_playlist, "random", &val );
952 b_random = val.b_bool;
953 var_Get( p_playlist, "repeat", &val );
954 b_repeat = val.b_bool;
955 var_Get( p_playlist, "loop", &val );
961 srand( (unsigned int)mdate() );
963 /* Simple random stuff - we cheat a bit to minimize the chances to
964 * get the same index again. */
965 i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
968 i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
975 p_playlist->i_index += i_arg;
978 if( p_playlist->i_index >= p_playlist->i_size )
980 if( p_playlist->i_status == PLAYLIST_STOPPED
984 p_playlist->i_index -= p_playlist->i_size
985 * ( p_playlist->i_index / p_playlist->i_size );
989 /* Don't loop by default: stop at playlist end */
990 p_playlist->i_index = i_oldindex;
991 p_playlist->i_status = PLAYLIST_STOPPED;
994 else if( p_playlist->i_index < 0 )
996 p_playlist->i_index = p_playlist->i_size - 1;
999 /* Check that the item is enabled */
1000 if( p_playlist->pp_items[p_playlist->i_index]->b_enabled == VLC_FALSE &&
1001 p_playlist->i_enabled != 0)
1003 SkipItem( p_playlist , 1 );
1007 /*****************************************************************************
1008 * PlayItem: play current playlist item
1009 *****************************************************************************
1010 * This function calculates the position of the next playlist item, depending
1011 * on the playlist course mode (forward, backward, random...).
1012 *****************************************************************************/
1013 static void PlayItem( playlist_t *p_playlist )
1015 if( p_playlist->i_index == -1 )
1017 if( p_playlist->i_size == 0 || p_playlist->i_enabled == 0)
1022 SkipItem( p_playlist, 1 );
1025 if( p_playlist->i_enabled == 0)
1030 msg_Dbg( p_playlist, "creating new input thread" );
1031 p_playlist->p_input = input_CreateThread( p_playlist,
1032 p_playlist->pp_items[p_playlist->i_index] );
1035 /*****************************************************************************
1036 * playlist_LoadFile: load a playlist file.
1037 ****************************************************************************/
1038 int playlist_LoadFile( playlist_t * p_playlist, const char *psz_filename )
1042 int i_current_status;
1046 msg_Dbg( p_playlist, "opening playlist file %s", psz_filename );
1048 file = fopen( psz_filename, "rt" );
1051 msg_Err( p_playlist, "playlist file %s does not exist", psz_filename );
1054 fseek( file, 0L, SEEK_SET );
1056 /* check the file is not empty */
1057 if ( ! fgets( line, 1024, file ) )
1059 msg_Err( p_playlist, "playlist file %s is empty", psz_filename );
1064 /* get rid of line feed */
1065 if( line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r' )
1067 line[strlen(line)-1] = (char)0;
1068 if( line[strlen(line)-1] == '\r' ) line[strlen(line)-1] = (char)0;
1070 /* check the file format is valid */
1071 if ( !strcmp ( line , PLAYLIST_FILE_HEADER_0_5 ) )
1075 else if( !strcmp ( line , PLAYLIST_FILE_HEADER_0_6 ) )
1081 msg_Err( p_playlist, "playlist file %s format is unsupported"
1088 i_current_status = p_playlist->i_status;
1089 if ( p_playlist->i_status != PLAYLIST_STOPPED )
1091 playlist_Stop ( p_playlist );
1094 /* delete current content of the playlist */
1095 for( i = p_playlist->i_size - 1; i >= 0; i-- )
1097 playlist_Delete ( p_playlist , i );
1100 /* simply add each line */
1101 while( fgets( line, 1024, file ) )
1103 /* ignore comments or empty lines */
1104 if( (line[0] == '#') || (line[0] == '\r') || (line[0] == '\n')
1105 || (line[0] == (char)0) )
1108 /* get rid of line feed */
1109 if( line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r' )
1111 line[strlen(line)-1] = (char)0;
1112 if( line[strlen(line)-1] == '\r' ) line[strlen(line)-1] = (char)0;
1116 playlist_Add ( p_playlist , (char *)&line ,
1117 0, 0, PLAYLIST_APPEND , PLAYLIST_END );
1121 msg_Warn( p_playlist, "Not supported yet");
1126 if ( i_current_status != PLAYLIST_STOPPED )
1128 playlist_Play ( p_playlist );
1136 /*****************************************************************************
1137 * playlist_SaveFile: Save a playlist in a file.
1138 *****************************************************************************/
1139 int playlist_SaveFile( playlist_t * p_playlist, const char * psz_filename )
1144 vlc_mutex_lock( &p_playlist->object_lock );
1146 msg_Dbg( p_playlist, "saving playlist file %s", psz_filename );
1148 file = fopen( psz_filename, "wt" );
1151 msg_Err( p_playlist , "could not create playlist file %s"
1155 /* Save is done in 0_5 mode at the moment*/
1157 fprintf( file , PLAYLIST_FILE_HEADER_0_5 "\n" );
1159 for ( i = 0 ; i < p_playlist->i_size ; i++ )
1161 fprintf( file , p_playlist->pp_items[i]->psz_uri );
1162 fprintf( file , "\n" );
1165 fprintf( file, PLAYLIST_FILE_HEADER_0_6 "\n" );
1167 for ( i=0 ; i< p_playlist->i_size ; i++ )
1169 fprintf( file, p_playlist->pp_items[i]->psz_uri );
1170 fprintf( file, "||" );
1171 fprintf( file, p_playlist->pp_items[i]->psz_name );
1172 fprintf( file, "||" );
1173 fprintf( file, "%i",p_playlist->pp_items[i]->b_enabled = VLC_TRUE ?
1175 fprintf( file, "||" );
1176 fprintf( file, "%i", p_playlist->pp_items[i]->i_group );
1177 fprintf( file, "||" );
1178 fprintf( file, p_playlist->pp_items[i]->psz_author );
1179 fprintf( file , "\n" );
1184 vlc_mutex_unlock( &p_playlist->object_lock );