1 /*****************************************************************************
2 * playlist.c : Playlist management functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: playlist.c,v 1.53 2003/09/19 21:53:48 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() */
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"
38 /*****************************************************************************
40 *****************************************************************************/
41 static void RunThread ( playlist_t * );
42 static void SkipItem ( playlist_t *, int );
43 static void PlayItem ( playlist_t * );
45 static void Poubellize ( playlist_t *, input_thread_t * );
50 * Create a playlist structure.
51 * \param p_parent the vlc object that is to be the parent of this playlist
52 * \return a pointer to the created playlist, or NULL on error
54 playlist_t * __playlist_Create ( vlc_object_t *p_parent )
56 playlist_t *p_playlist;
59 /* Allocate structure */
60 p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
63 msg_Err( p_parent, "out of memory" );
67 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
68 val.b_bool = VLC_TRUE;
69 var_Set( p_playlist, "intf-change", val );
71 var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
73 var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
74 val.b_bool = VLC_TRUE;
75 var_Set( p_playlist, "intf-show", val );
77 p_playlist->p_input = NULL;
78 p_playlist->i_status = PLAYLIST_STOPPED;
79 p_playlist->i_index = -1;
80 p_playlist->i_size = 0;
81 p_playlist->pp_items = NULL;
83 if( vlc_thread_create( p_playlist, "playlist", RunThread,
84 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
86 msg_Err( p_playlist, "cannot spawn playlist thread" );
87 vlc_object_destroy( p_playlist );
91 /* The object has been initialized, now attach it */
92 vlc_object_attach( p_playlist, p_parent );
98 * Destroy the playlist.
100 * Delete all items in the playlist and free the playlist structure.
101 * \param p_playlist the playlist structure to destroy
103 void playlist_Destroy( playlist_t * p_playlist )
105 p_playlist->b_die = 1;
107 vlc_thread_join( p_playlist );
109 var_Destroy( p_playlist, "intf-change" );
111 vlc_object_destroy( p_playlist );
115 * Add an MRL to the playlist. This is a simplified version of
116 * playlist_AddExt inculded for convenince. It equals calling playlist_AddExt
117 * with psz_name == psz_target and i_duration == -1
120 int playlist_Add( playlist_t *p_playlist, const char *psz_target,
121 const char **ppsz_options, int i_options,
122 int i_mode, int i_pos )
124 return playlist_AddExt( p_playlist, psz_target, psz_target, -1,
125 ppsz_options, i_options, i_mode, i_pos );
129 * Add a MRL into the playlist.
131 * \param p_playlist the playlist to add into
132 * \param psz_uri the mrl to add to the playlist
133 * \param psz_name a text giving a name or description of this item
134 * \param i_duration a hint about the duration of this item, in miliseconds, or
136 * \param ppsz_options array of options
137 * \param i_options number of items in ppsz_options
138 * \param i_mode the mode used when adding
139 * \param i_pos the position in the playlist where to add. If this is
140 * PLAYLIST_END the item will be added at the end of the playlist
141 * regardless of it's size
142 * \return always returns 0
144 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
145 const char * psz_name, mtime_t i_duration,
146 const char **ppsz_options, int i_options, int i_mode,
149 playlist_item_t * p_item;
151 p_item = malloc( sizeof( playlist_item_t ) );
154 msg_Err( p_playlist, "out of memory" );
157 p_item->psz_name = strdup( psz_name );
158 p_item->psz_uri = strdup( psz_uri );
159 p_item->i_duration = i_duration;
161 p_item->i_status = 0;
162 p_item->b_autodeletion = VLC_FALSE;
164 p_item->ppsz_options = NULL;
165 p_item->i_options = i_options;
171 p_item->ppsz_options = (char **)malloc( i_options * sizeof(char *) );
172 for( i = 0; i < i_options; i++ )
173 p_item->ppsz_options[i] = strdup( ppsz_options[i] );
177 return playlist_AddItem( p_playlist, p_item, i_mode, i_pos );
181 * Add a playlist item into a playlist
183 * \param p_playlist the playlist to insert into
184 * \param p_item the playlist item to insert
185 * \param i_mode the mode used when adding
186 * \param i_pos the possition in the playlist where to add. If this is
187 * PLAYLIST_END the item will be added at the end of the playlist
188 * regardless of it's size
189 * \return always returns 0
191 int playlist_AddItem( playlist_t *p_playlist, playlist_item_t * p_item,
192 int i_mode, int i_pos)
196 vlc_mutex_lock( &p_playlist->object_lock );
199 * CHECK_INSERT : checks if the item is already enqued before
202 if ( i_mode & PLAYLIST_CHECK_INSERT )
206 if ( p_playlist->pp_items )
208 for ( j = 0; j < p_playlist->i_size; j++ )
210 if ( !strcmp( p_playlist->pp_items[j]->psz_uri, p_item->psz_uri ) )
212 if( p_item->psz_name )
214 free( p_item->psz_name );
216 if( p_item->psz_uri )
218 free( p_item->psz_uri );
221 vlc_mutex_unlock( &p_playlist->object_lock );
226 i_mode &= ~PLAYLIST_CHECK_INSERT;
227 i_mode |= PLAYLIST_APPEND;
231 msg_Dbg( p_playlist, "adding playlist item « %s » ( %s )", p_item->psz_name, p_item->psz_uri);
233 /* Create the new playlist item */
236 /* Do a few boundary checks and allocate space for the item */
237 if( i_pos == PLAYLIST_END )
239 if( i_mode & PLAYLIST_INSERT )
241 i_mode &= ~PLAYLIST_INSERT;
242 i_mode |= PLAYLIST_APPEND;
245 i_pos = p_playlist->i_size - 1;
248 if( !(i_mode & PLAYLIST_REPLACE)
249 || i_pos < 0 || i_pos >= p_playlist->i_size )
251 /* Additional boundary checks */
252 if( i_mode & PLAYLIST_APPEND )
261 else if( i_pos > p_playlist->i_size )
263 i_pos = p_playlist->i_size;
266 INSERT_ELEM( p_playlist->pp_items,
271 if( p_playlist->i_index >= i_pos )
273 p_playlist->i_index++;
278 /* i_mode == PLAYLIST_REPLACE and 0 <= i_pos < p_playlist->i_size */
279 if( p_playlist->pp_items[i_pos]->psz_name )
281 free( p_playlist->pp_items[i_pos]->psz_name );
283 if( p_playlist->pp_items[i_pos]->psz_uri )
285 free( p_playlist->pp_items[i_pos]->psz_uri );
287 /* XXX: what if the item is still in use? */
288 free( p_playlist->pp_items[i_pos] );
289 p_playlist->pp_items[i_pos] = p_item;
292 if( i_mode & PLAYLIST_GO )
294 p_playlist->i_index = i_pos;
295 if( p_playlist->p_input )
297 input_StopThread( p_playlist->p_input );
299 p_playlist->i_status = PLAYLIST_RUNNING;
302 vlc_mutex_unlock( &p_playlist->object_lock );
304 val.b_bool = VLC_TRUE;
305 var_Set( p_playlist, "intf-change", val );
311 * delete an item from a playlist.
313 * \param p_playlist the playlist to remove from.
314 * \param i_pos the position of the item to remove
317 int playlist_Delete( playlist_t * p_playlist, int i_pos )
320 vlc_mutex_lock( &p_playlist->object_lock );
322 if( i_pos >= 0 && i_pos < p_playlist->i_size )
324 msg_Dbg( p_playlist, "deleting playlist item « %s »",
325 p_playlist->pp_items[i_pos]->psz_name );
327 if( p_playlist->pp_items[i_pos]->psz_name )
329 free( p_playlist->pp_items[i_pos]->psz_name );
331 if( p_playlist->pp_items[i_pos]->psz_uri )
333 free( p_playlist->pp_items[i_pos]->psz_uri );
335 if( p_playlist->pp_items[i_pos]->i_options )
339 for( i = 0; i < p_playlist->pp_items[i_pos]->i_options; i++ )
340 free( p_playlist->pp_items[i_pos]->ppsz_options[i] );
342 free( p_playlist->pp_items[i_pos]->ppsz_options );
345 /* XXX: what if the item is still in use? */
346 free( p_playlist->pp_items[i_pos] );
348 if( i_pos <= p_playlist->i_index )
350 p_playlist->i_index--;
353 /* Renumber the playlist */
354 REMOVE_ELEM( p_playlist->pp_items,
359 vlc_mutex_unlock( &p_playlist->object_lock );
361 val.b_bool = VLC_TRUE;
362 var_Set( p_playlist, "intf-change", val );
372 int playlist_Sort( playlist_t * p_playlist , int i_type )
374 int i , i_small , i_position;
375 playlist_item_t *p_temp;
377 vlc_mutex_lock( &p_playlist->object_lock );
379 for( i_position = 0; i_position < p_playlist->i_size -1 ; i_position ++ )
381 i_small = i_position;
382 for( i = i_position + 1 ; i< p_playlist->i_size ; i++)
386 i_test = strcasecmp( p_playlist->pp_items[i]->psz_name,
387 p_playlist->pp_items[i_small]->psz_name );
389 if( ( i_type == SORT_NORMAL && i_test < 0 ) ||
390 ( i_type == SORT_REVERSE && i_test > 0 ) )
395 p_temp = p_playlist->pp_items[i_position];
396 p_playlist->pp_items[i_position] = p_playlist->pp_items[i_small];
397 p_playlist->pp_items[i_small] = p_temp;
401 for( i=0; i < p_playlist->i_size; i++ )
403 msg_Dbg( p_playlist, "%s", p_playlist->pp_items[i]->psz_name );
406 vlc_mutex_unlock( &p_playlist->object_lock );
412 * Move an item in a playlist
414 * Move the item in the playlist with position i_pos before the current item
415 * at position i_newpos.
416 * \param p_playlist the playlist to move items in
417 * \param i_pos the position of the item to move
418 * \param i_newpos the position of the item that will be behind the moved item
422 int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos)
425 vlc_mutex_lock( &p_playlist->object_lock );
427 /* take into account that our own row disappears. */
428 if ( i_pos < i_newpos ) i_newpos--;
430 if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size
431 && i_newpos <= p_playlist->i_size )
433 playlist_item_t * temp;
435 msg_Dbg( p_playlist, "moving playlist item « %s »",
436 p_playlist->pp_items[i_pos]->psz_name );
438 if( i_pos == p_playlist->i_index )
440 p_playlist->i_index = i_newpos;
442 else if( i_pos > p_playlist->i_index && i_newpos <= p_playlist->i_index )
444 p_playlist->i_index++;
446 else if( i_pos < p_playlist->i_index && i_newpos >= p_playlist->i_index )
448 p_playlist->i_index--;
451 if ( i_pos < i_newpos )
453 temp = p_playlist->pp_items[i_pos];
454 while ( i_pos < i_newpos )
456 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
459 p_playlist->pp_items[i_newpos] = temp;
461 else if ( i_pos > i_newpos )
463 temp = p_playlist->pp_items[i_pos];
464 while ( i_pos > i_newpos )
466 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
469 p_playlist->pp_items[i_newpos] = temp;
473 vlc_mutex_unlock( &p_playlist->object_lock );
475 val.b_bool = VLC_TRUE;
476 var_Set( p_playlist, "intf-change", val );
482 * Do a playlist action
484 * \param p_playlist the playlist to do the command on
485 * \param i_command the command to do
486 * \param i_arg the argument to the command. See playlist_command_t for details
488 void playlist_Command( playlist_t * p_playlist, playlist_command_t i_command,
493 vlc_mutex_lock( &p_playlist->object_lock );
498 p_playlist->i_status = PLAYLIST_STOPPED;
499 if( p_playlist->p_input )
501 input_StopThread( p_playlist->p_input );
506 p_playlist->i_status = PLAYLIST_RUNNING;
507 if( !p_playlist->p_input )
509 PlayItem( p_playlist );
511 if( p_playlist->p_input )
513 val.i_int = PLAYING_S;
514 var_Set( p_playlist->p_input, "state", val );
519 p_playlist->i_status = PLAYLIST_PAUSED;
520 if( p_playlist->p_input )
523 var_Set( p_playlist->p_input, "state", val );
528 p_playlist->i_status = PLAYLIST_STOPPED;
529 SkipItem( p_playlist, i_arg );
530 if( p_playlist->p_input )
532 input_StopThread( p_playlist->p_input );
534 p_playlist->i_status = PLAYLIST_RUNNING;
538 if( i_arg >= 0 && i_arg < p_playlist->i_size )
540 p_playlist->i_index = i_arg;
541 if( p_playlist->p_input )
543 input_StopThread( p_playlist->p_input );
545 p_playlist->i_status = PLAYLIST_RUNNING;
550 msg_Err( p_playlist, "unknown playlist command" );
554 vlc_mutex_unlock( &p_playlist->object_lock );
558 /* Following functions are local */
560 static void ObjectGarbageCollector( playlist_t *p_playlist,
562 vlc_bool_t *pb_obj_destroyed,
563 mtime_t *pi_obj_destroyed_date )
566 if( *pb_obj_destroyed || *pi_obj_destroyed_date > mdate() )
571 if( *pi_obj_destroyed_date == 0 )
573 /* give a little time */
574 *pi_obj_destroyed_date = mdate() + 300000LL;
578 while( ( p_obj = vlc_object_find( p_playlist,
582 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
584 /* only first chiled (ie unused) */
585 vlc_object_release( p_obj );
588 if( i_type == VLC_OBJECT_VOUT )
590 msg_Dbg( p_playlist, "vout garbage collector destroying 1 vout" );
591 vlc_object_detach( p_obj );
592 vlc_object_release( p_obj );
593 vout_Destroy( (vout_thread_t *)p_obj );
595 else if( i_type == VLC_OBJECT_SOUT )
597 vlc_object_release( p_obj );
598 sout_DeleteInstance( (sout_instance_t*)p_obj );
601 *pb_obj_destroyed = VLC_TRUE;
605 /*****************************************************************************
606 * RunThread: main playlist thread
607 *****************************************************************************/
608 static void RunThread ( playlist_t *p_playlist )
611 vlc_bool_t b_vout_destroyed = VLC_FALSE; /*we do vout garbage collector */
612 mtime_t i_vout_destroyed_date = 0;
614 vlc_bool_t b_sout_destroyed = VLC_FALSE; /*we do vout garbage collector */
615 mtime_t i_sout_destroyed_date = 0;
617 /* Tell above that we're ready */
618 vlc_thread_ready( p_playlist );
620 while( !p_playlist->b_die )
622 vlc_mutex_lock( &p_playlist->object_lock );
624 /* If there is an input, check that it doesn't need to die. */
625 if( p_playlist->p_input )
627 /* This input is dead. Remove it ! */
628 if( p_playlist->p_input->b_dead )
630 input_thread_t *p_input;
632 p_input = p_playlist->p_input;
633 p_playlist->p_input = NULL;
635 /* Release the playlist lock, because we may get stuck
636 * in input_DestroyThread() for some time. */
637 vlc_mutex_unlock( &p_playlist->object_lock );
640 input_DestroyThread( p_input );
642 /* Unlink current input (_after_ input_DestroyThread for vout garbage collector)*/
643 vlc_object_detach( p_input );
646 vlc_object_destroy( p_input );
648 b_vout_destroyed = VLC_FALSE;
649 i_vout_destroyed_date = 0;
650 b_sout_destroyed = VLC_FALSE;
651 i_sout_destroyed_date = 0;
654 /* This input is dying, let him do */
655 else if( p_playlist->p_input->b_die )
659 /* This input has finished, ask him to die ! */
660 else if( p_playlist->p_input->b_error
661 || p_playlist->p_input->b_eof )
663 /* Check for autodeletion */
664 if( p_playlist->pp_items[p_playlist->i_index]->b_autodeletion )
666 vlc_mutex_unlock( &p_playlist->object_lock );
667 playlist_Delete( p_playlist, p_playlist->i_index );
668 vlc_mutex_lock( &p_playlist->object_lock );
671 /* Select the next playlist item */
672 SkipItem( p_playlist, 1 );
674 input_StopThread( p_playlist->p_input );
675 vlc_mutex_unlock( &p_playlist->object_lock );
678 else if( p_playlist->p_input->stream.control.i_status != INIT_S )
680 vlc_mutex_unlock( &p_playlist->object_lock );
681 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
683 &i_vout_destroyed_date );
684 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
686 &i_sout_destroyed_date );
687 vlc_mutex_lock( &p_playlist->object_lock );
690 else if( p_playlist->i_status != PLAYLIST_STOPPED )
692 SkipItem( p_playlist, 0 );
693 PlayItem( p_playlist );
695 else if( p_playlist->i_status == PLAYLIST_STOPPED )
697 vlc_mutex_unlock( &p_playlist->object_lock );
698 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
699 &b_sout_destroyed, &i_sout_destroyed_date );
700 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
701 &b_vout_destroyed, &i_vout_destroyed_date );
702 vlc_mutex_lock( &p_playlist->object_lock );
704 vlc_mutex_unlock( &p_playlist->object_lock );
706 msleep( INTF_IDLE_SLEEP );
709 /* If there is an input, kill it */
712 vlc_mutex_lock( &p_playlist->object_lock );
714 if( p_playlist->p_input == NULL )
716 vlc_mutex_unlock( &p_playlist->object_lock );
720 if( p_playlist->p_input->b_dead )
722 input_thread_t *p_input;
724 /* Unlink current input */
725 p_input = p_playlist->p_input;
726 p_playlist->p_input = NULL;
727 vlc_mutex_unlock( &p_playlist->object_lock );
730 input_DestroyThread( p_input );
731 /* Unlink current input (_after_ input_DestroyThread for vout
732 * garbage collector)*/
733 vlc_object_detach( p_input );
736 vlc_object_destroy( p_input );
739 else if( p_playlist->p_input->b_die )
741 /* This input is dying, leave him alone */
744 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
746 input_StopThread( p_playlist->p_input );
747 vlc_mutex_unlock( &p_playlist->object_lock );
752 p_playlist->p_input->b_eof = 1;
755 vlc_mutex_unlock( &p_playlist->object_lock );
757 msleep( INTF_IDLE_SLEEP );
760 /* close all remaining sout */
761 while( ( p_obj = vlc_object_find( p_playlist,
762 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
764 vlc_object_release( p_obj );
765 sout_DeleteInstance( (sout_instance_t*)p_obj );
768 /* close all remaining vout */
769 while( ( p_obj = vlc_object_find( p_playlist,
770 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
772 vlc_object_detach( p_obj );
773 vlc_object_release( p_obj );
774 vout_Destroy( (vout_thread_t *)p_obj );
778 /*****************************************************************************
779 * SkipItem: go to Xth playlist item
780 *****************************************************************************
781 * This function calculates the position of the next playlist item, depending
782 * on the playlist course mode (forward, backward, random...).
783 *****************************************************************************/
784 static void SkipItem( playlist_t *p_playlist, int i_arg )
786 int i_oldindex = p_playlist->i_index;
790 /* If the playlist is empty, there is no current item */
791 if( p_playlist->i_size == 0 )
793 p_playlist->i_index = -1;
797 b_random = config_GetInt( p_playlist, "random" );
802 srand( (unsigned int)mdate() );
804 /* Simple random stuff - we cheat a bit to minimize the chances to
805 * get the same index again. */
806 i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
809 i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
813 p_playlist->i_index += i_arg;
816 if( p_playlist->i_index >= p_playlist->i_size )
818 if( p_playlist->i_status == PLAYLIST_STOPPED
820 || config_GetInt( p_playlist, "loop" ) )
822 p_playlist->i_index -= p_playlist->i_size
823 * ( p_playlist->i_index / p_playlist->i_size );
827 /* Don't loop by default: stop at playlist end */
828 p_playlist->i_index = i_oldindex;
829 p_playlist->i_status = PLAYLIST_STOPPED;
832 else if( p_playlist->i_index < 0 )
834 p_playlist->i_index = p_playlist->i_size - 1;
837 val.b_bool = VLC_TRUE;
838 var_Set( p_playlist, "intf-change", val );
841 /*****************************************************************************
842 * PlayItem: play current playlist item
843 *****************************************************************************
844 * This function calculates the position of the next playlist item, depending
845 * on the playlist course mode (forward, backward, random...).
846 *****************************************************************************/
847 static void PlayItem( playlist_t *p_playlist )
849 if( p_playlist->i_index == -1 )
851 if( p_playlist->i_size == 0 )
856 SkipItem( p_playlist, 1 );
859 msg_Dbg( p_playlist, "creating new input thread" );
860 p_playlist->p_input = input_CreateThread( p_playlist,
861 p_playlist->pp_items[p_playlist->i_index] );
864 /*****************************************************************************
865 * Poubellize: put an input thread in the trashcan
866 *****************************************************************************
868 *****************************************************************************/
869 static void Poubellize ( playlist_t *p_playlist, input_thread_t *p_input )
871 msg_Dbg( p_playlist, "poubellizing input %i\n", p_input->i_object_id );
874 /*****************************************************************************
875 * playlist_LoadFile: load a playlist file.
876 ****************************************************************************/
877 int playlist_LoadFile( playlist_t * p_playlist, const char *psz_filename )
881 int i_current_status;
884 msg_Dbg( p_playlist, "opening playlist file %s", psz_filename );
886 file = fopen( psz_filename, "rt" );
889 msg_Err( p_playlist, "playlist file %s does not exist", psz_filename );
892 fseek( file, 0L, SEEK_SET );
894 /* check the file is not empty */
895 if ( ! fgets( line, 1024, file ) )
897 msg_Err( p_playlist, "playlist file %s is empty", psz_filename );
902 /* get rid of line feed */
903 if( line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r' )
905 line[strlen(line)-1] = (char)0;
906 if( line[strlen(line)-1] == '\r' ) line[strlen(line)-1] = (char)0;
908 /* check the file format is valid */
909 if ( strcmp ( line , PLAYLIST_FILE_HEADER_0_5 ) )
911 msg_Err( p_playlist, "playlist file %s format is unsupported"
918 i_current_status = p_playlist->i_status;
919 if ( p_playlist->i_status != PLAYLIST_STOPPED )
921 playlist_Stop ( p_playlist );
924 /* delete current content of the playlist */
925 for( i = p_playlist->i_size - 1; i >= 0; i-- )
927 playlist_Delete ( p_playlist , i );
930 /* simply add each line */
931 while( fgets( line, 1024, file ) )
933 /* ignore comments or empty lines */
934 if( (line[0] == '#') || (line[0] == '\r') || (line[0] == '\n')
935 || (line[0] == (char)0) )
938 /* get rid of line feed */
939 if( line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r' )
941 line[strlen(line)-1] = (char)0;
942 if( line[strlen(line)-1] == '\r' ) line[strlen(line)-1] = (char)0;
945 playlist_Add ( p_playlist , (char *)&line ,
946 0, 0, PLAYLIST_APPEND , PLAYLIST_END );
950 if ( i_current_status != PLAYLIST_STOPPED )
952 playlist_Play ( p_playlist );
960 /*****************************************************************************
961 * playlist_SaveFile: Save a playlist in a file.
962 *****************************************************************************/
963 int playlist_SaveFile( playlist_t * p_playlist, const char * psz_filename )
968 vlc_mutex_lock( &p_playlist->object_lock );
970 msg_Dbg( p_playlist, "saving playlist file %s", psz_filename );
972 file = fopen( psz_filename, "wt" );
975 msg_Err( p_playlist , "could not create playlist file %s"
980 fprintf( file , PLAYLIST_FILE_HEADER_0_5 "\n" );
982 for ( i = 0 ; i < p_playlist->i_size ; i++ )
984 fprintf( file , p_playlist->pp_items[i]->psz_uri );
985 fprintf( file , "\n" );
990 vlc_mutex_unlock( &p_playlist->object_lock );