]> git.sesse.net Git - vlc/blob - src/playlist/playlist.c
207bd15df06d662914527b52d1ea4a37104b5d6d
[vlc] / src / playlist / playlist.c
1 /*****************************************************************************
2  * playlist.c : Playlist management functions
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: playlist.c,v 1.35 2003/05/12 17:33:20 gbazin Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
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.
13  *
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.
18  *
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() */
26
27 #include <vlc/vlc.h>
28
29 #include "stream_control.h"
30 #include "input_ext-intf.h"
31
32 #include "vlc_playlist.h"
33
34 #define PLAYLIST_FILE_HEADER_0_5  "# vlc playlist file version 0.5"
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static void RunThread ( playlist_t * );
40 static void SkipItem  ( playlist_t *, int );
41 static void PlayItem  ( playlist_t * );
42
43 static void Poubellize ( playlist_t *, input_thread_t * );
44
45 /*****************************************************************************
46  * playlist_Create: create playlist
47  *****************************************************************************
48  * Create a playlist structure.
49  *****************************************************************************/
50 playlist_t * __playlist_Create ( vlc_object_t *p_parent )
51 {
52     playlist_t *p_playlist;
53     vlc_value_t     val;
54
55     /* Allocate structure */
56     p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
57     if( !p_playlist )
58     {
59         msg_Err( p_parent, "out of memory" );
60         return NULL;
61     }
62
63     var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
64     val.b_bool = VLC_TRUE;
65     var_Set( p_playlist, "intf-change", val );
66
67     p_playlist->p_input = NULL;
68     p_playlist->i_status = PLAYLIST_STOPPED;
69     p_playlist->i_index = -1;
70     p_playlist->i_size = 0;
71     p_playlist->pp_items = NULL;
72
73     if( vlc_thread_create( p_playlist, "playlist", RunThread,
74                            VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
75     {
76         msg_Err( p_playlist, "cannot spawn playlist thread" );
77         vlc_object_destroy( p_playlist );
78         return NULL;
79     }
80
81     /* The object has been initialized, now attach it */
82     vlc_object_attach( p_playlist, p_parent );
83
84     return p_playlist;
85 }
86
87 /*****************************************************************************
88  * playlist_Destroy: destroy the playlist
89  *****************************************************************************
90  * Delete all items in the playlist and free the playlist structure.
91  *****************************************************************************/
92 void playlist_Destroy( playlist_t * p_playlist )
93 {
94     p_playlist->b_die = 1;
95
96     vlc_thread_join( p_playlist );
97
98     var_Destroy( p_playlist, "intf-change" );
99
100     vlc_object_destroy( p_playlist );
101 }
102
103 /*****************************************************************************
104  * playlist_Add: add an item to the playlist
105  *****************************************************************************
106  * Add an item to the playlist at position i_pos. If i_pos is PLAYLIST_END,
107  * add it at the end regardless of the playlist current size.
108  *****************************************************************************/
109 int playlist_Add( playlist_t *p_playlist, const char * psz_target,
110                                           int i_mode, int i_pos )
111 {
112     playlist_item_t * p_item;
113
114     p_item = malloc( sizeof( playlist_item_t ) );
115     if( p_item == NULL )
116     {
117         msg_Err( p_playlist, "out of memory" );
118     }
119
120     p_item->psz_name = strdup( psz_target );
121     p_item->psz_uri  = strdup( psz_target );
122     p_item->i_type = 0;
123     p_item->i_status = 0;
124     p_item->b_autodeletion = VLC_FALSE;
125
126     return playlist_AddItem( p_playlist, p_item, i_mode, i_pos );
127 }
128
129
130 int playlist_AddItem( playlist_t *p_playlist, playlist_item_t * p_item,
131                 int i_mode, int i_pos)
132 {
133     vlc_value_t     val;
134
135     vlc_mutex_lock( &p_playlist->object_lock );
136
137     /*
138      * CHECK_INSERT : checks if the item is already enqued before
139      * enqueing it
140      */
141     if ( i_mode & PLAYLIST_CHECK_INSERT )
142     {
143          int j;
144
145          if ( p_playlist->pp_items )
146          {
147              for ( j = 0; j < p_playlist->i_size; j++ )
148              {
149                  if ( !strcmp( p_playlist->pp_items[j]->psz_uri, p_item->psz_uri ) )
150                  {
151                       if( p_item->psz_name )
152                       {
153                           free( p_item->psz_name );
154                       }
155                       if( p_item->psz_uri )
156                       {
157                           free( p_item->psz_uri );
158                       }
159                       free( p_item );
160                       vlc_mutex_unlock( &p_playlist->object_lock );
161                       return 0;
162                  }
163              }
164          }
165          i_mode &= ~PLAYLIST_CHECK_INSERT;
166          i_mode |= PLAYLIST_APPEND;
167     }
168
169
170     msg_Dbg( p_playlist, "adding playlist item « %s »", p_item->psz_name );
171
172     /* Create the new playlist item */
173
174
175     /* Do a few boundary checks and allocate space for the item */
176     if( i_pos == PLAYLIST_END )
177     {
178         if( i_mode & PLAYLIST_INSERT )
179         {
180             i_mode &= ~PLAYLIST_INSERT;
181             i_mode |= PLAYLIST_APPEND;
182         }
183
184         i_pos = p_playlist->i_size - 1;
185     }
186
187     if( !(i_mode & PLAYLIST_REPLACE)
188          || i_pos < 0 || i_pos >= p_playlist->i_size )
189     {
190         /* Additional boundary checks */
191         if( i_mode & PLAYLIST_APPEND )
192         {
193             i_pos++;
194         }
195
196         if( i_pos < 0 )
197         {
198             i_pos = 0;
199         }
200         else if( i_pos > p_playlist->i_size )
201         {
202             i_pos = p_playlist->i_size;
203         }
204
205         INSERT_ELEM( p_playlist->pp_items,
206                      p_playlist->i_size,
207                      i_pos,
208                      p_item );
209
210         if( p_playlist->i_index >= i_pos )
211         {
212             p_playlist->i_index++;
213         }
214     }
215     else
216     {
217         /* i_mode == PLAYLIST_REPLACE and 0 <= i_pos < p_playlist->i_size */
218         if( p_playlist->pp_items[i_pos]->psz_name )
219         {
220             free( p_playlist->pp_items[i_pos]->psz_name );
221         }
222         if( p_playlist->pp_items[i_pos]->psz_uri )
223         {
224             free( p_playlist->pp_items[i_pos]->psz_uri );
225         }
226         /* XXX: what if the item is still in use? */
227         free( p_playlist->pp_items[i_pos] );
228         p_playlist->pp_items[i_pos] = p_item;
229     }
230
231     if( i_mode & PLAYLIST_GO )
232     {
233         p_playlist->i_index = i_pos;
234         if( p_playlist->p_input )
235         {
236             input_StopThread( p_playlist->p_input );
237         }
238         p_playlist->i_status = PLAYLIST_RUNNING;
239     }
240
241     vlc_mutex_unlock( &p_playlist->object_lock );
242
243     val.b_bool = VLC_TRUE;
244     var_Set( p_playlist, "intf-change", val );
245
246     return 0;
247 }
248
249 /*****************************************************************************
250  * playlist_Delete: delete an item from the playlist
251  *****************************************************************************
252  * Delete the item in the playlist with position i_pos.
253  *****************************************************************************/
254 int playlist_Delete( playlist_t * p_playlist, int i_pos )
255 {
256     vlc_value_t     val;
257     vlc_mutex_lock( &p_playlist->object_lock );
258
259     if( i_pos >= 0 && i_pos < p_playlist->i_size )
260     {
261         msg_Dbg( p_playlist, "deleting playlist item « %s »",
262                              p_playlist->pp_items[i_pos]->psz_name );
263
264         if( p_playlist->pp_items[i_pos]->psz_name )
265         {
266             free( p_playlist->pp_items[i_pos]->psz_name );
267         }
268         if( p_playlist->pp_items[i_pos]->psz_uri )
269         {
270             free( p_playlist->pp_items[i_pos]->psz_uri );
271         }
272
273         /* XXX: what if the item is still in use? */
274         free( p_playlist->pp_items[i_pos] );
275
276         if( i_pos <= p_playlist->i_index )
277         {
278             p_playlist->i_index--;
279         }
280
281         /* Renumber the playlist */
282         REMOVE_ELEM( p_playlist->pp_items,
283                      p_playlist->i_size,
284                      i_pos );
285     }
286
287     vlc_mutex_unlock( &p_playlist->object_lock );
288
289     val.b_bool = VLC_TRUE;
290     var_Set( p_playlist, "intf-change", val );
291
292     return 0;
293 }
294
295 /*****************************************************************************
296  * playlist_Move: move an item in the playlist
297  *****************************************************************************
298  * Move the item in the playlist with position i_pos before the current item
299  * at position i_newpos.
300  *****************************************************************************/
301 int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos)
302 {
303     vlc_value_t     val;
304     vlc_mutex_lock( &p_playlist->object_lock );
305
306     /* take into account that our own row disappears. */
307     if ( i_pos < i_newpos ) i_newpos--;
308
309     if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size 
310                      && i_newpos <= p_playlist->i_size )
311     {
312         playlist_item_t * temp;
313
314         msg_Dbg( p_playlist, "moving playlist item « %s »",
315                              p_playlist->pp_items[i_pos]->psz_name );
316
317         if( i_pos == p_playlist->i_index )
318         {
319             p_playlist->i_index = i_newpos;
320         }
321         else if( i_pos > p_playlist->i_index && i_newpos <= p_playlist->i_index )
322         {
323             p_playlist->i_index++;
324         }
325         else if( i_pos < p_playlist->i_index && i_newpos >= p_playlist->i_index )
326         {
327             p_playlist->i_index--;
328         }
329
330         if ( i_pos < i_newpos )
331         {
332             temp = p_playlist->pp_items[i_pos];
333             while ( i_pos < i_newpos )
334             {
335                 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
336                 i_pos++;
337             }
338             p_playlist->pp_items[i_newpos] = temp;
339         }
340         else if ( i_pos > i_newpos )
341         {
342             temp = p_playlist->pp_items[i_pos];
343             while ( i_pos > i_newpos )
344             {
345                 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
346                 i_pos--;
347             }
348             p_playlist->pp_items[i_newpos] = temp;
349         }
350     }
351
352     vlc_mutex_unlock( &p_playlist->object_lock );
353
354     val.b_bool = VLC_TRUE;
355     var_Set( p_playlist, "intf-change", val );
356
357     return 0;
358 }
359
360 /*****************************************************************************
361  * playlist_Command: do a playlist action
362  *****************************************************************************
363  *
364  *****************************************************************************/
365 void playlist_Command( playlist_t * p_playlist, int i_command, int i_arg )
366 {
367     vlc_mutex_lock( &p_playlist->object_lock );
368
369     switch( i_command )
370     {
371     case PLAYLIST_STOP:
372         p_playlist->i_status = PLAYLIST_STOPPED;
373         if( p_playlist->p_input )
374         {
375             input_StopThread( p_playlist->p_input );
376         }
377         break;
378
379     case PLAYLIST_PLAY:
380         p_playlist->i_status = PLAYLIST_RUNNING;
381         if( p_playlist->p_input )
382         {
383             input_SetStatus( p_playlist->p_input, INPUT_STATUS_PLAY );
384         }
385         break;
386
387     case PLAYLIST_PAUSE:
388         p_playlist->i_status = PLAYLIST_PAUSED;
389         if( p_playlist->p_input )
390         {
391             input_SetStatus( p_playlist->p_input, INPUT_STATUS_PAUSE );
392         }
393         break;
394
395     case PLAYLIST_SKIP:
396         p_playlist->i_status = PLAYLIST_STOPPED;
397         SkipItem( p_playlist, i_arg );
398         if( p_playlist->p_input )
399         {
400             input_StopThread( p_playlist->p_input );
401         }
402         p_playlist->i_status = PLAYLIST_RUNNING;
403         break;
404
405     case PLAYLIST_GOTO:
406         if( i_arg >= 0 && i_arg < p_playlist->i_size )
407         {
408             p_playlist->i_index = i_arg;
409             if( p_playlist->p_input )
410             {
411                 input_StopThread( p_playlist->p_input );
412             }
413             p_playlist->i_status = PLAYLIST_RUNNING;
414         }
415         break;
416
417     default:
418         msg_Err( p_playlist, "unknown playlist command" );
419         break;
420     }
421
422     vlc_mutex_unlock( &p_playlist->object_lock );
423
424     return;
425 }
426
427 /* Following functions are local */
428
429 /*****************************************************************************
430  * RunThread: main playlist thread
431  *****************************************************************************/
432 static void RunThread ( playlist_t *p_playlist )
433 {
434     /* Tell above that we're ready */
435     vlc_thread_ready( p_playlist );
436
437     while( !p_playlist->b_die )
438     {
439         vlc_mutex_lock( &p_playlist->object_lock );
440
441         /* If there is an input, check that it doesn't need to die. */
442         if( p_playlist->p_input )
443         {
444             /* This input is dead. Remove it ! */
445             if( p_playlist->p_input->b_dead )
446             {
447                 input_thread_t *p_input;
448
449                 /* Unlink current input */
450                 p_input = p_playlist->p_input;
451                 p_playlist->p_input = NULL;
452                 vlc_object_detach( p_input );
453
454                 /* Release the playlist lock, because we may get stuck
455                  * in input_DestroyThread() for some time. */
456                 vlc_mutex_unlock( &p_playlist->object_lock );
457
458                 /* Destroy input */
459                 input_DestroyThread( p_input );
460                 vlc_object_destroy( p_input );
461                 continue;
462             }
463             /* This input is dying, let him do */
464             else if( p_playlist->p_input->b_die )
465             {
466                 ;
467             }
468             /* This input has finished, ask him to die ! */
469             else if( p_playlist->p_input->b_error
470                       || p_playlist->p_input->b_eof )
471             {
472                 /* Check for autodeletion */
473                 if( p_playlist->pp_items[p_playlist->i_index]->b_autodeletion )
474                 {
475                     vlc_mutex_unlock( &p_playlist->object_lock );
476                     playlist_Delete( p_playlist, p_playlist->i_index );
477                     vlc_mutex_lock( &p_playlist->object_lock );
478                 }
479
480                 /* Select the next playlist item */
481                 SkipItem( p_playlist, 1 );
482
483                 /* Release the playlist lock, because we may get stuck
484                  * in input_StopThread() for some time. */
485                 vlc_mutex_unlock( &p_playlist->object_lock );
486                 input_StopThread( p_playlist->p_input );
487                 continue;
488             }
489         }
490         else if( p_playlist->i_status != PLAYLIST_STOPPED )
491         {
492             PlayItem( p_playlist );
493         }
494
495         vlc_mutex_unlock( &p_playlist->object_lock );
496
497         msleep( INTF_IDLE_SLEEP );
498     }
499
500     /* If there is an input, kill it */
501     while( 1 )
502     {
503         vlc_mutex_lock( &p_playlist->object_lock );
504
505         if( p_playlist->p_input == NULL )
506         {
507             vlc_mutex_unlock( &p_playlist->object_lock );
508             break;
509         }
510
511         if( p_playlist->p_input->b_dead )
512         {
513             input_thread_t *p_input;
514
515             /* Unlink current input */
516             p_input = p_playlist->p_input;
517             p_playlist->p_input = NULL;
518             vlc_object_detach( p_input );
519             vlc_mutex_unlock( &p_playlist->object_lock );
520
521             /* Destroy input */
522             input_DestroyThread( p_input );
523             vlc_object_destroy( p_input );
524             continue;
525         }
526         else if( p_playlist->p_input->b_die )
527         {
528             /* This input is dying, leave him alone */
529             ;
530         }
531         else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
532         {
533             vlc_mutex_unlock( &p_playlist->object_lock );
534             input_StopThread( p_playlist->p_input );
535             continue;
536         }
537         else
538         {
539             p_playlist->p_input->b_eof = 1;
540         }
541
542         vlc_mutex_unlock( &p_playlist->object_lock );
543
544         msleep( INTF_IDLE_SLEEP );
545     }
546 }
547
548 /*****************************************************************************
549  * SkipItem: go to Xth playlist item
550  *****************************************************************************
551  * This function calculates the position of the next playlist item, depending
552  * on the playlist course mode (forward, backward, random...).
553  *****************************************************************************/
554 static void SkipItem( playlist_t *p_playlist, int i_arg )
555 {
556     int i_oldindex = p_playlist->i_index;
557     vlc_bool_t b_random;
558
559     /* If the playlist is empty, there is no current item */
560     if( p_playlist->i_size == 0 )
561     {
562         p_playlist->i_index = -1;
563         return;
564     }
565
566     b_random = config_GetInt( p_playlist, "random" );
567
568     /* Increment */
569     if( b_random )
570     {
571         srand( (unsigned int)mdate() );
572
573         /* Simple random stuff - we cheat a bit to minimize the chances to
574          * get the same index again. */
575         i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
576         if( i_arg == 0 )
577         {
578             i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
579         }
580     }
581
582     p_playlist->i_index += i_arg;
583
584     /* Boundary check */
585     if( p_playlist->i_index >= p_playlist->i_size )
586     {
587         if( p_playlist->i_status == PLAYLIST_STOPPED
588              || b_random
589              || config_GetInt( p_playlist, "loop" ) )
590         {
591             p_playlist->i_index -= p_playlist->i_size
592                          * ( p_playlist->i_index / p_playlist->i_size );
593         }
594         else
595         {
596             /* Don't loop by default: stop at playlist end */
597             p_playlist->i_index = i_oldindex;
598             p_playlist->i_status = PLAYLIST_STOPPED;
599         }
600     }
601     else if( p_playlist->i_index < 0 )
602     {
603         p_playlist->i_index = p_playlist->i_size - 1;
604     }
605 }
606
607 /*****************************************************************************
608  * PlayItem: play current playlist item
609  *****************************************************************************
610  * This function calculates the position of the next playlist item, depending
611  * on the playlist course mode (forward, backward, random...).
612  *****************************************************************************/
613 static void PlayItem( playlist_t *p_playlist )
614 {
615     if( p_playlist->i_index == -1 )
616     {
617         if( p_playlist->i_size == 0 )
618         {
619             return;
620         }
621
622         SkipItem( p_playlist, 1 );
623     }
624
625     msg_Dbg( p_playlist, "creating new input thread" );
626     p_playlist->p_input = input_CreateThread( p_playlist,
627                                   p_playlist->pp_items[p_playlist->i_index] );
628 }
629
630 /*****************************************************************************
631  * Poubellize: put an input thread in the trashcan
632  *****************************************************************************
633  * XXX: unused
634  *****************************************************************************/
635 static void Poubellize ( playlist_t *p_playlist, input_thread_t *p_input )
636 {
637     msg_Dbg( p_playlist, "poubellizing input %i\n", p_input->i_object_id );
638 }
639
640 /*****************************************************************************
641  * playlist_LoadFile: load a playlist file.
642  ****************************************************************************/
643 int playlist_LoadFile( playlist_t * p_playlist, const char *psz_filename )
644 {
645     FILE *file;
646     char line[1024];
647     int i_current_status;
648     int i;
649
650     msg_Dbg( p_playlist, "opening playlist file %s", psz_filename );
651
652     file = fopen( psz_filename, "rt" );
653     if( !file )
654     {
655         msg_Err( p_playlist, "playlist file %s does not exist", psz_filename );
656         return -1;
657     }
658     fseek( file, 0L, SEEK_SET );
659
660     /* check the file is not empty */
661     if ( ! fgets( line, 1024, file ) )
662     {
663         msg_Err( p_playlist, "playlist file %s is empty", psz_filename );
664         fclose( file );
665         return -1;
666     }
667
668     /* get rid of line feed */
669     if( line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r' )
670     {
671        line[strlen(line)-1] = (char)0;
672        if( line[strlen(line)-1] == '\r' ) line[strlen(line)-1] = (char)0;
673     }
674     /* check the file format is valid */
675     if ( strcmp ( line , PLAYLIST_FILE_HEADER_0_5 ) )
676     {
677         msg_Err( p_playlist, "playlist file %s format is unsupported"
678                 , psz_filename );
679         fclose( file );
680         return -1;
681     }
682
683     /* stop playing */
684     i_current_status = p_playlist->i_status;
685     if ( p_playlist->i_status != PLAYLIST_STOPPED )
686     {
687         playlist_Stop ( p_playlist );
688     }
689
690     /* delete current content of the playlist */
691     for( i = p_playlist->i_size - 1; i >= 0; i-- )
692     {
693         playlist_Delete ( p_playlist , i );
694     }
695
696     /* simply add each line */
697     while( fgets( line, 1024, file ) )
698     {
699        /* ignore comments or empty lines */
700        if( (line[0] == '#') || (line[0] == '\r') || (line[0] == '\n')
701                || (line[0] == (char)0) )
702            continue;
703
704        /* get rid of line feed */
705        if( line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r' )
706        {
707            line[strlen(line)-1] = (char)0;
708            if( line[strlen(line)-1] == '\r' ) line[strlen(line)-1] = (char)0;
709        }
710
711        playlist_Add ( p_playlist , (char*) &line , PLAYLIST_APPEND , PLAYLIST_END );
712     }
713
714     /* start playing */
715     if ( i_current_status != PLAYLIST_STOPPED )
716     {
717         playlist_Play ( p_playlist );
718     }
719
720     fclose( file );
721
722     return 0;
723 }
724
725 /*****************************************************************************
726  * playlist_SaveFile: Save a playlist in a file.
727  *****************************************************************************/
728 int playlist_SaveFile( playlist_t * p_playlist, const char * psz_filename )
729 {
730     FILE *file;
731     int i;
732
733     vlc_mutex_lock( &p_playlist->object_lock );
734
735     msg_Dbg( p_playlist, "saving playlist file %s", psz_filename );
736
737     file = fopen( psz_filename, "wt" );
738     if( !file )
739     {
740         msg_Err( p_playlist , "could not create playlist file %s"
741                 , psz_filename );
742         return -1;
743     }
744
745     fprintf( file , PLAYLIST_FILE_HEADER_0_5 "\n" );
746
747     for ( i = 0 ; i < p_playlist->i_size ; i++ )
748     {
749         fprintf( file , p_playlist->pp_items[i]->psz_uri );
750         fprintf( file , "\n" );
751     }
752
753     fclose( file );
754
755     vlc_mutex_unlock( &p_playlist->object_lock );
756
757     return 0;
758 }