1 /*****************************************************************************
2 * playlist.c : Playlist management functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: playlist.c,v 1.55 2003/09/22 14:40:12 zorglub 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 var_Create( p_playlist, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
78 var_Create( p_playlist, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
79 var_Create( p_playlist, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
81 p_playlist->p_input = NULL;
82 p_playlist->i_status = PLAYLIST_STOPPED;
83 p_playlist->i_index = -1;
84 p_playlist->i_size = 0;
85 p_playlist->pp_items = NULL;
87 if( vlc_thread_create( p_playlist, "playlist", RunThread,
88 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
90 msg_Err( p_playlist, "cannot spawn playlist thread" );
91 vlc_object_destroy( p_playlist );
95 /* The object has been initialized, now attach it */
96 vlc_object_attach( p_playlist, p_parent );
102 * Destroy the playlist.
104 * Delete all items in the playlist and free the playlist structure.
105 * \param p_playlist the playlist structure to destroy
107 void playlist_Destroy( playlist_t * p_playlist )
109 p_playlist->b_die = 1;
111 vlc_thread_join( p_playlist );
113 var_Destroy( p_playlist, "intf-change" );
115 vlc_object_destroy( p_playlist );
119 * Add an MRL to the playlist. This is a simplified version of
120 * playlist_AddExt inculded for convenince. It equals calling playlist_AddExt
121 * with psz_name == psz_target and i_duration == -1
124 int playlist_Add( playlist_t *p_playlist, const char *psz_target,
125 const char **ppsz_options, int i_options,
126 int i_mode, int i_pos )
128 return playlist_AddExt( p_playlist, psz_target, psz_target, -1,
129 ppsz_options, i_options, i_mode, i_pos );
133 * Add a MRL into the playlist.
135 * \param p_playlist the playlist to add into
136 * \param psz_uri the mrl to add to the playlist
137 * \param psz_name a text giving a name or description of this item
138 * \param i_duration a hint about the duration of this item, in miliseconds, or
140 * \param ppsz_options array of options
141 * \param i_options number of items in ppsz_options
142 * \param i_mode the mode used when adding
143 * \param i_pos the position in the playlist where to add. If this is
144 * PLAYLIST_END the item will be added at the end of the playlist
145 * regardless of it's size
146 * \return always returns 0
148 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
149 const char * psz_name, mtime_t i_duration,
150 const char **ppsz_options, int i_options, int i_mode,
153 playlist_item_t * p_item;
155 p_item = malloc( sizeof( playlist_item_t ) );
158 msg_Err( p_playlist, "out of memory" );
161 p_item->psz_name = strdup( psz_name );
162 p_item->psz_uri = strdup( psz_uri );
163 p_item->i_duration = i_duration;
165 p_item->i_status = 0;
166 p_item->b_autodeletion = VLC_FALSE;
168 p_item->ppsz_options = NULL;
169 p_item->i_options = i_options;
175 p_item->ppsz_options = (char **)malloc( i_options * sizeof(char *) );
176 for( i = 0; i < i_options; i++ )
177 p_item->ppsz_options[i] = strdup( ppsz_options[i] );
181 return playlist_AddItem( p_playlist, p_item, i_mode, i_pos );
185 * Add a playlist item into a playlist
187 * \param p_playlist the playlist to insert into
188 * \param p_item the playlist item to insert
189 * \param i_mode the mode used when adding
190 * \param i_pos the possition in the playlist where to add. If this is
191 * PLAYLIST_END the item will be added at the end of the playlist
192 * regardless of it's size
193 * \return always returns 0
195 int playlist_AddItem( playlist_t *p_playlist, playlist_item_t * p_item,
196 int i_mode, int i_pos)
200 vlc_mutex_lock( &p_playlist->object_lock );
203 * CHECK_INSERT : checks if the item is already enqued before
206 if ( i_mode & PLAYLIST_CHECK_INSERT )
210 if ( p_playlist->pp_items )
212 for ( j = 0; j < p_playlist->i_size; j++ )
214 if ( !strcmp( p_playlist->pp_items[j]->psz_uri, p_item->psz_uri ) )
216 if( p_item->psz_name )
218 free( p_item->psz_name );
220 if( p_item->psz_uri )
222 free( p_item->psz_uri );
225 vlc_mutex_unlock( &p_playlist->object_lock );
230 i_mode &= ~PLAYLIST_CHECK_INSERT;
231 i_mode |= PLAYLIST_APPEND;
235 msg_Dbg( p_playlist, "adding playlist item « %s » ( %s )", p_item->psz_name, p_item->psz_uri);
237 /* Create the new playlist item */
240 /* Do a few boundary checks and allocate space for the item */
241 if( i_pos == PLAYLIST_END )
243 if( i_mode & PLAYLIST_INSERT )
245 i_mode &= ~PLAYLIST_INSERT;
246 i_mode |= PLAYLIST_APPEND;
249 i_pos = p_playlist->i_size - 1;
252 if( !(i_mode & PLAYLIST_REPLACE)
253 || i_pos < 0 || i_pos >= p_playlist->i_size )
255 /* Additional boundary checks */
256 if( i_mode & PLAYLIST_APPEND )
265 else if( i_pos > p_playlist->i_size )
267 i_pos = p_playlist->i_size;
270 INSERT_ELEM( p_playlist->pp_items,
275 if( p_playlist->i_index >= i_pos )
277 p_playlist->i_index++;
282 /* i_mode == PLAYLIST_REPLACE and 0 <= i_pos < p_playlist->i_size */
283 if( p_playlist->pp_items[i_pos]->psz_name )
285 free( p_playlist->pp_items[i_pos]->psz_name );
287 if( p_playlist->pp_items[i_pos]->psz_uri )
289 free( p_playlist->pp_items[i_pos]->psz_uri );
291 /* XXX: what if the item is still in use? */
292 free( p_playlist->pp_items[i_pos] );
293 p_playlist->pp_items[i_pos] = p_item;
296 if( i_mode & PLAYLIST_GO )
298 p_playlist->i_index = i_pos;
299 if( p_playlist->p_input )
301 input_StopThread( p_playlist->p_input );
303 p_playlist->i_status = PLAYLIST_RUNNING;
306 vlc_mutex_unlock( &p_playlist->object_lock );
308 val.b_bool = VLC_TRUE;
309 var_Set( p_playlist, "intf-change", val );
315 * delete an item from a playlist.
317 * \param p_playlist the playlist to remove from.
318 * \param i_pos the position of the item to remove
321 int playlist_Delete( playlist_t * p_playlist, int i_pos )
324 vlc_mutex_lock( &p_playlist->object_lock );
326 if( i_pos >= 0 && i_pos < p_playlist->i_size )
328 msg_Dbg( p_playlist, "deleting playlist item « %s »",
329 p_playlist->pp_items[i_pos]->psz_name );
331 if( p_playlist->pp_items[i_pos]->psz_name )
333 free( p_playlist->pp_items[i_pos]->psz_name );
335 if( p_playlist->pp_items[i_pos]->psz_uri )
337 free( p_playlist->pp_items[i_pos]->psz_uri );
339 if( p_playlist->pp_items[i_pos]->i_options )
343 for( i = 0; i < p_playlist->pp_items[i_pos]->i_options; i++ )
344 free( p_playlist->pp_items[i_pos]->ppsz_options[i] );
346 free( p_playlist->pp_items[i_pos]->ppsz_options );
349 /* XXX: what if the item is still in use? */
350 free( p_playlist->pp_items[i_pos] );
352 if( i_pos <= p_playlist->i_index )
354 p_playlist->i_index--;
357 /* Renumber the playlist */
358 REMOVE_ELEM( p_playlist->pp_items,
363 vlc_mutex_unlock( &p_playlist->object_lock );
365 val.b_bool = VLC_TRUE;
366 var_Set( p_playlist, "intf-change", val );
376 int playlist_Sort( playlist_t * p_playlist , int i_type )
378 int i , i_small , i_position;
379 playlist_item_t *p_temp;
381 vlc_mutex_lock( &p_playlist->object_lock );
383 for( i_position = 0; i_position <= p_playlist->i_size -1 ; i_position ++ )
385 i_small = i_position;
386 for( i = i_position + 1 ; i< p_playlist->i_size ; i++)
390 i_test = strcasecmp( p_playlist->pp_items[i]->psz_name,
391 p_playlist->pp_items[i_small]->psz_name );
393 if( ( i_type == SORT_NORMAL && i_test < 0 ) ||
394 ( i_type == SORT_REVERSE && i_test > 0 ) )
399 p_temp = p_playlist->pp_items[i_position];
400 p_playlist->pp_items[i_position] = p_playlist->pp_items[i_small];
401 p_playlist->pp_items[i_small] = p_temp;
404 vlc_mutex_unlock( &p_playlist->object_lock );
410 * Move an item in a playlist
412 * Move the item in the playlist with position i_pos before the current item
413 * at position i_newpos.
414 * \param p_playlist the playlist to move items in
415 * \param i_pos the position of the item to move
416 * \param i_newpos the position of the item that will be behind the moved item
420 int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos)
423 vlc_mutex_lock( &p_playlist->object_lock );
425 /* take into account that our own row disappears. */
426 if ( i_pos < i_newpos ) i_newpos--;
428 if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size
429 && i_newpos <= p_playlist->i_size )
431 playlist_item_t * temp;
433 msg_Dbg( p_playlist, "moving playlist item « %s »",
434 p_playlist->pp_items[i_pos]->psz_name );
436 if( i_pos == p_playlist->i_index )
438 p_playlist->i_index = i_newpos;
440 else if( i_pos > p_playlist->i_index && i_newpos <= p_playlist->i_index )
442 p_playlist->i_index++;
444 else if( i_pos < p_playlist->i_index && i_newpos >= p_playlist->i_index )
446 p_playlist->i_index--;
449 if ( i_pos < i_newpos )
451 temp = p_playlist->pp_items[i_pos];
452 while ( i_pos < i_newpos )
454 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
457 p_playlist->pp_items[i_newpos] = temp;
459 else if ( i_pos > i_newpos )
461 temp = p_playlist->pp_items[i_pos];
462 while ( i_pos > i_newpos )
464 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
467 p_playlist->pp_items[i_newpos] = temp;
471 vlc_mutex_unlock( &p_playlist->object_lock );
473 val.b_bool = VLC_TRUE;
474 var_Set( p_playlist, "intf-change", val );
480 * Do a playlist action
482 * \param p_playlist the playlist to do the command on
483 * \param i_command the command to do
484 * \param i_arg the argument to the command. See playlist_command_t for details
486 void playlist_Command( playlist_t * p_playlist, playlist_command_t i_command,
491 vlc_mutex_lock( &p_playlist->object_lock );
496 p_playlist->i_status = PLAYLIST_STOPPED;
497 if( p_playlist->p_input )
499 input_StopThread( p_playlist->p_input );
504 p_playlist->i_status = PLAYLIST_RUNNING;
505 if( !p_playlist->p_input )
507 PlayItem( p_playlist );
509 if( p_playlist->p_input )
511 val.i_int = PLAYING_S;
512 var_Set( p_playlist->p_input, "state", val );
517 p_playlist->i_status = PLAYLIST_PAUSED;
518 if( p_playlist->p_input )
521 var_Set( p_playlist->p_input, "state", val );
526 p_playlist->i_status = PLAYLIST_STOPPED;
527 SkipItem( p_playlist, i_arg );
528 if( p_playlist->p_input )
530 input_StopThread( p_playlist->p_input );
532 p_playlist->i_status = PLAYLIST_RUNNING;
536 if( i_arg >= 0 && i_arg < p_playlist->i_size )
538 p_playlist->i_index = i_arg;
539 if( p_playlist->p_input )
541 input_StopThread( p_playlist->p_input );
543 p_playlist->i_status = PLAYLIST_RUNNING;
548 msg_Err( p_playlist, "unknown playlist command" );
552 vlc_mutex_unlock( &p_playlist->object_lock );
556 /* Following functions are local */
558 static void ObjectGarbageCollector( playlist_t *p_playlist,
560 vlc_bool_t *pb_obj_destroyed,
561 mtime_t *pi_obj_destroyed_date )
564 if( *pb_obj_destroyed || *pi_obj_destroyed_date > mdate() )
569 if( *pi_obj_destroyed_date == 0 )
571 /* give a little time */
572 *pi_obj_destroyed_date = mdate() + 300000LL;
576 while( ( p_obj = vlc_object_find( p_playlist,
580 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
582 /* only first chiled (ie unused) */
583 vlc_object_release( p_obj );
586 if( i_type == VLC_OBJECT_VOUT )
588 msg_Dbg( p_playlist, "vout garbage collector destroying 1 vout" );
589 vlc_object_detach( p_obj );
590 vlc_object_release( p_obj );
591 vout_Destroy( (vout_thread_t *)p_obj );
593 else if( i_type == VLC_OBJECT_SOUT )
595 vlc_object_release( p_obj );
596 sout_DeleteInstance( (sout_instance_t*)p_obj );
599 *pb_obj_destroyed = VLC_TRUE;
603 /*****************************************************************************
604 * RunThread: main playlist thread
605 *****************************************************************************/
606 static void RunThread ( playlist_t *p_playlist )
609 vlc_bool_t b_vout_destroyed = VLC_FALSE; /*we do vout garbage collector */
610 mtime_t i_vout_destroyed_date = 0;
612 vlc_bool_t b_sout_destroyed = VLC_FALSE; /*we do vout garbage collector */
613 mtime_t i_sout_destroyed_date = 0;
615 /* Tell above that we're ready */
616 vlc_thread_ready( p_playlist );
618 while( !p_playlist->b_die )
620 vlc_mutex_lock( &p_playlist->object_lock );
622 /* If there is an input, check that it doesn't need to die. */
623 if( p_playlist->p_input )
625 /* This input is dead. Remove it ! */
626 if( p_playlist->p_input->b_dead )
628 input_thread_t *p_input;
630 p_input = p_playlist->p_input;
631 p_playlist->p_input = NULL;
633 /* Release the playlist lock, because we may get stuck
634 * in input_DestroyThread() for some time. */
635 vlc_mutex_unlock( &p_playlist->object_lock );
638 input_DestroyThread( p_input );
640 /* Unlink current input (_after_ input_DestroyThread for vout garbage collector)*/
641 vlc_object_detach( p_input );
644 vlc_object_destroy( p_input );
646 b_vout_destroyed = VLC_FALSE;
647 i_vout_destroyed_date = 0;
648 b_sout_destroyed = VLC_FALSE;
649 i_sout_destroyed_date = 0;
652 /* This input is dying, let him do */
653 else if( p_playlist->p_input->b_die )
657 /* This input has finished, ask him to die ! */
658 else if( p_playlist->p_input->b_error
659 || p_playlist->p_input->b_eof )
661 /* Check for autodeletion */
662 if( p_playlist->pp_items[p_playlist->i_index]->b_autodeletion )
664 vlc_mutex_unlock( &p_playlist->object_lock );
665 playlist_Delete( p_playlist, p_playlist->i_index );
666 vlc_mutex_lock( &p_playlist->object_lock );
669 /* Select the next playlist item */
670 SkipItem( p_playlist, 1 );
672 input_StopThread( p_playlist->p_input );
673 vlc_mutex_unlock( &p_playlist->object_lock );
676 else if( p_playlist->p_input->stream.control.i_status != INIT_S )
678 vlc_mutex_unlock( &p_playlist->object_lock );
679 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
681 &i_vout_destroyed_date );
682 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
684 &i_sout_destroyed_date );
685 vlc_mutex_lock( &p_playlist->object_lock );
688 else if( p_playlist->i_status != PLAYLIST_STOPPED )
690 SkipItem( p_playlist, 0 );
691 PlayItem( p_playlist );
693 else if( p_playlist->i_status == PLAYLIST_STOPPED )
695 vlc_mutex_unlock( &p_playlist->object_lock );
696 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
697 &b_sout_destroyed, &i_sout_destroyed_date );
698 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
699 &b_vout_destroyed, &i_vout_destroyed_date );
700 vlc_mutex_lock( &p_playlist->object_lock );
702 vlc_mutex_unlock( &p_playlist->object_lock );
704 msleep( INTF_IDLE_SLEEP );
707 /* If there is an input, kill it */
710 vlc_mutex_lock( &p_playlist->object_lock );
712 if( p_playlist->p_input == NULL )
714 vlc_mutex_unlock( &p_playlist->object_lock );
718 if( p_playlist->p_input->b_dead )
720 input_thread_t *p_input;
722 /* Unlink current input */
723 p_input = p_playlist->p_input;
724 p_playlist->p_input = NULL;
725 vlc_mutex_unlock( &p_playlist->object_lock );
728 input_DestroyThread( p_input );
729 /* Unlink current input (_after_ input_DestroyThread for vout
730 * garbage collector)*/
731 vlc_object_detach( p_input );
734 vlc_object_destroy( p_input );
737 else if( p_playlist->p_input->b_die )
739 /* This input is dying, leave him alone */
742 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
744 input_StopThread( p_playlist->p_input );
745 vlc_mutex_unlock( &p_playlist->object_lock );
750 p_playlist->p_input->b_eof = 1;
753 vlc_mutex_unlock( &p_playlist->object_lock );
755 msleep( INTF_IDLE_SLEEP );
758 /* close all remaining sout */
759 while( ( p_obj = vlc_object_find( p_playlist,
760 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
762 vlc_object_release( p_obj );
763 sout_DeleteInstance( (sout_instance_t*)p_obj );
766 /* close all remaining vout */
767 while( ( p_obj = vlc_object_find( p_playlist,
768 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
770 vlc_object_detach( p_obj );
771 vlc_object_release( p_obj );
772 vout_Destroy( (vout_thread_t *)p_obj );
776 /*****************************************************************************
777 * SkipItem: go to Xth playlist item
778 *****************************************************************************
779 * This function calculates the position of the next playlist item, depending
780 * on the playlist course mode (forward, backward, random...).
781 *****************************************************************************/
782 static void SkipItem( playlist_t *p_playlist, int i_arg )
784 int i_oldindex = p_playlist->i_index;
785 vlc_bool_t b_random, b_repeat, b_loop;
788 /* If the playlist is empty, there is no current item */
789 if( p_playlist->i_size == 0 )
791 p_playlist->i_index = -1;
795 var_Get( p_playlist, "random", &val );
796 b_random = val.b_bool;
797 var_Get( p_playlist, "repeat", &val );
798 b_repeat = val.b_bool;
799 var_Get( p_playlist, "loop", &val );
805 srand( (unsigned int)mdate() );
807 /* Simple random stuff - we cheat a bit to minimize the chances to
808 * get the same index again. */
809 i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
812 i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
819 p_playlist->i_index += i_arg;
822 if( p_playlist->i_index >= p_playlist->i_size )
824 if( p_playlist->i_status == PLAYLIST_STOPPED
828 p_playlist->i_index -= p_playlist->i_size
829 * ( p_playlist->i_index / p_playlist->i_size );
833 /* Don't loop by default: stop at playlist end */
834 p_playlist->i_index = i_oldindex;
835 p_playlist->i_status = PLAYLIST_STOPPED;
838 else if( p_playlist->i_index < 0 )
840 p_playlist->i_index = p_playlist->i_size - 1;
843 val.b_bool = VLC_TRUE;
844 var_Set( p_playlist, "intf-change", val );
847 /*****************************************************************************
848 * PlayItem: play current playlist item
849 *****************************************************************************
850 * This function calculates the position of the next playlist item, depending
851 * on the playlist course mode (forward, backward, random...).
852 *****************************************************************************/
853 static void PlayItem( playlist_t *p_playlist )
855 if( p_playlist->i_index == -1 )
857 if( p_playlist->i_size == 0 )
862 SkipItem( p_playlist, 1 );
865 msg_Dbg( p_playlist, "creating new input thread" );
866 p_playlist->p_input = input_CreateThread( p_playlist,
867 p_playlist->pp_items[p_playlist->i_index] );
870 /*****************************************************************************
871 * Poubellize: put an input thread in the trashcan
872 *****************************************************************************
874 *****************************************************************************/
875 static void Poubellize ( playlist_t *p_playlist, input_thread_t *p_input )
877 msg_Dbg( p_playlist, "poubellizing input %i\n", p_input->i_object_id );
880 /*****************************************************************************
881 * playlist_LoadFile: load a playlist file.
882 ****************************************************************************/
883 int playlist_LoadFile( playlist_t * p_playlist, const char *psz_filename )
887 int i_current_status;
890 msg_Dbg( p_playlist, "opening playlist file %s", psz_filename );
892 file = fopen( psz_filename, "rt" );
895 msg_Err( p_playlist, "playlist file %s does not exist", psz_filename );
898 fseek( file, 0L, SEEK_SET );
900 /* check the file is not empty */
901 if ( ! fgets( line, 1024, file ) )
903 msg_Err( p_playlist, "playlist file %s is empty", psz_filename );
908 /* get rid of line feed */
909 if( line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r' )
911 line[strlen(line)-1] = (char)0;
912 if( line[strlen(line)-1] == '\r' ) line[strlen(line)-1] = (char)0;
914 /* check the file format is valid */
915 if ( strcmp ( line , PLAYLIST_FILE_HEADER_0_5 ) )
917 msg_Err( p_playlist, "playlist file %s format is unsupported"
924 i_current_status = p_playlist->i_status;
925 if ( p_playlist->i_status != PLAYLIST_STOPPED )
927 playlist_Stop ( p_playlist );
930 /* delete current content of the playlist */
931 for( i = p_playlist->i_size - 1; i >= 0; i-- )
933 playlist_Delete ( p_playlist , i );
936 /* simply add each line */
937 while( fgets( line, 1024, file ) )
939 /* ignore comments or empty lines */
940 if( (line[0] == '#') || (line[0] == '\r') || (line[0] == '\n')
941 || (line[0] == (char)0) )
944 /* get rid of line feed */
945 if( line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r' )
947 line[strlen(line)-1] = (char)0;
948 if( line[strlen(line)-1] == '\r' ) line[strlen(line)-1] = (char)0;
951 playlist_Add ( p_playlist , (char *)&line ,
952 0, 0, PLAYLIST_APPEND , PLAYLIST_END );
956 if ( i_current_status != PLAYLIST_STOPPED )
958 playlist_Play ( p_playlist );
966 /*****************************************************************************
967 * playlist_SaveFile: Save a playlist in a file.
968 *****************************************************************************/
969 int playlist_SaveFile( playlist_t * p_playlist, const char * psz_filename )
974 vlc_mutex_lock( &p_playlist->object_lock );
976 msg_Dbg( p_playlist, "saving playlist file %s", psz_filename );
978 file = fopen( psz_filename, "wt" );
981 msg_Err( p_playlist , "could not create playlist file %s"
986 fprintf( file , PLAYLIST_FILE_HEADER_0_5 "\n" );
988 for ( i = 0 ; i < p_playlist->i_size ; i++ )
990 fprintf( file , p_playlist->pp_items[i]->psz_uri );
991 fprintf( file , "\n" );
996 vlc_mutex_unlock( &p_playlist->object_lock );