]> git.sesse.net Git - vlc/blob - src/playlist/playlist.c
2a34b7eb04b74ce61470319b179eb5cdf6771f6a
[vlc] / src / playlist / playlist.c
1 /*****************************************************************************
2  * playlist.c : Playlist management functions
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id$
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 #include <vlc/vout.h>
29 #include <vlc/sout.h>
30 #include <vlc/input.h>
31
32 #include "stream_control.h"
33 #include "input_ext-intf.h"
34
35 #include "vlc_playlist.h"
36
37 #define PLAYLIST_FILE_HEADER_0_5  "# vlc playlist file version 0.5"
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static void RunThread ( playlist_t * );
43 static void SkipItem  ( playlist_t *, int );
44 static void PlayItem  ( playlist_t * );
45
46 /**
47  * Create playlist
48  *
49  * Create a playlist structure.
50  * \param p_parent the vlc object that is to be the parent of this playlist
51  * \return a pointer to the created playlist, or NULL on error
52  */
53 playlist_t * __playlist_Create ( vlc_object_t *p_parent )
54 {
55     playlist_t *p_playlist;
56     vlc_value_t     val;
57
58     /* Allocate structure */
59     p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
60     if( !p_playlist )
61     {
62         msg_Err( p_parent, "out of memory" );
63         return NULL;
64     }
65
66     var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
67     val.b_bool = VLC_TRUE;
68     var_Set( p_playlist, "intf-change", val );
69
70     var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
71     val.i_int = -1;
72     var_Set( p_playlist, "item-change", val );
73
74     var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
75     val.i_int = -1;
76     var_Set( p_playlist, "playlist-current", val );
77
78     var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
79
80     var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
81     val.b_bool = VLC_TRUE;
82     var_Set( p_playlist, "intf-show", val );
83
84
85     var_Create( p_playlist, "prevent-skip", VLC_VAR_BOOL );
86     val.b_bool = VLC_FALSE;
87     var_Set( p_playlist, "prevent-skip", val );
88
89     var_Create( p_playlist, "play-and-stop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
90     var_Create( p_playlist, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
91     var_Create( p_playlist, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
92     var_Create( p_playlist, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
93
94     p_playlist->p_input = NULL;
95     p_playlist->i_status = PLAYLIST_STOPPED;
96     p_playlist->i_index = -1;
97     p_playlist->i_size = 0;
98     p_playlist->pp_items = NULL;
99
100     p_playlist->i_groups = 0;
101     p_playlist->pp_groups = NULL;
102     p_playlist->i_last_group = 0;
103     p_playlist->i_last_id = 0;
104     p_playlist->i_sort = SORT_ID;
105     p_playlist->i_order = ORDER_NORMAL;
106
107     playlist_CreateGroup( p_playlist, _("Normal") );
108
109     if( vlc_thread_create( p_playlist, "playlist", RunThread,
110                            VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
111     {
112         msg_Err( p_playlist, "cannot spawn playlist thread" );
113         vlc_object_destroy( p_playlist );
114         return NULL;
115     }
116
117     /* The object has been initialized, now attach it */
118     vlc_object_attach( p_playlist, p_parent );
119
120     return p_playlist;
121 }
122
123 /**
124  * Destroy the playlist.
125  *
126  * Delete all items in the playlist and free the playlist structure.
127  * \param p_playlist the playlist structure to destroy
128  */
129 void playlist_Destroy( playlist_t * p_playlist )
130 {
131     p_playlist->b_die = 1;
132
133     vlc_thread_join( p_playlist );
134
135     var_Destroy( p_playlist, "intf-change" );
136     var_Destroy( p_playlist, "item-change" );
137     var_Destroy( p_playlist, "playlist-current" );
138     var_Destroy( p_playlist, "intf-popmenu" );
139     var_Destroy( p_playlist, "intf-show" );
140     var_Destroy( p_playlist, "prevent-skip" );
141     var_Destroy( p_playlist, "play-and-stop" );
142     var_Destroy( p_playlist, "random" );
143     var_Destroy( p_playlist, "repeat" );
144     var_Destroy( p_playlist, "loop" );
145
146     while( p_playlist->i_groups > 0 )
147     {
148         playlist_DeleteGroup( p_playlist, p_playlist->pp_groups[0]->i_id );
149     }
150
151     while( p_playlist->i_size > 0 )
152     {
153         playlist_Delete( p_playlist, 0 );
154     }
155
156     vlc_object_destroy( p_playlist );
157 }
158
159
160 /**
161  * Do a playlist action.
162  *
163  * If there is something in the playlist then you can do playlist actions.
164  * \param p_playlist the playlist to do the command on
165  * \param i_command the command to do
166  * \param i_arg the argument to the command. See playlist_command_t for details
167  */
168 void playlist_Command( playlist_t * p_playlist, playlist_command_t i_command,
169                        int i_arg )
170 {
171     vlc_value_t val;
172
173     vlc_mutex_lock( &p_playlist->object_lock );
174
175     if( p_playlist->i_size <= 0 )
176     {
177         vlc_mutex_unlock( &p_playlist->object_lock );
178         return;
179     }
180
181     switch( i_command )
182     {
183     case PLAYLIST_STOP:
184         p_playlist->i_status = PLAYLIST_STOPPED;
185         if( p_playlist->p_input )
186         {
187             input_StopThread( p_playlist->p_input );
188             val.i_int = p_playlist->i_index;
189             /* Does not matter if we unlock here */
190             vlc_mutex_unlock( &p_playlist->object_lock );
191             var_Set( p_playlist, "item-change",val );
192             vlc_mutex_lock( &p_playlist->object_lock );
193         }
194         break;
195
196     case PLAYLIST_PLAY:
197         p_playlist->i_status = PLAYLIST_RUNNING;
198         if( !p_playlist->p_input && p_playlist->i_enabled != 0 )
199         {
200             PlayItem( p_playlist );
201         }
202         if( p_playlist->p_input )
203         {
204             val.i_int = PLAYING_S;
205             var_Set( p_playlist->p_input, "state", val );
206         }
207         break;
208
209     case PLAYLIST_PAUSE:
210         val.i_int = 0;
211         if( p_playlist->p_input )
212             var_Get( p_playlist->p_input, "state", &val );
213
214         if( val.i_int == PAUSE_S )
215         {
216             p_playlist->i_status = PLAYLIST_RUNNING;
217             if( p_playlist->p_input )
218             {
219                 val.i_int = PLAYING_S;
220                 var_Set( p_playlist->p_input, "state", val );
221             }
222         }
223         else
224         {
225             p_playlist->i_status = PLAYLIST_PAUSED;
226             if( p_playlist->p_input )
227             {
228                 val.i_int = PAUSE_S;
229                 var_Set( p_playlist->p_input, "state", val );
230             }
231         }
232         break;
233
234     case PLAYLIST_SKIP:
235         p_playlist->i_status = PLAYLIST_STOPPED;
236         if( p_playlist->i_enabled == 0)
237         {
238             break;
239         }
240         SkipItem( p_playlist, i_arg );
241         if( p_playlist->p_input )
242         {
243             input_StopThread( p_playlist->p_input );
244         }
245         p_playlist->i_status = PLAYLIST_RUNNING;
246         break;
247
248     case PLAYLIST_GOTO:
249         if( i_arg >= 0 && i_arg < p_playlist->i_size &&
250             p_playlist->i_enabled != 0 )
251         {
252             p_playlist->i_index = i_arg;
253             if( p_playlist->p_input )
254             {
255                 input_StopThread( p_playlist->p_input );
256             }
257             val.b_bool = VLC_TRUE;
258             var_Set( p_playlist, "prevent-skip", val );
259             p_playlist->i_status = PLAYLIST_RUNNING;
260         }
261         break;
262
263     default:
264         msg_Err( p_playlist, "unknown playlist command" );
265         break;
266     }
267
268     vlc_mutex_unlock( &p_playlist->object_lock );
269 #if 0
270     val.b_bool = VLC_TRUE;
271     var_Set( p_playlist, "intf-change", val );
272 #endif
273     return;
274 }
275
276
277 static mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
278                                        mtime_t destroy_date )
279 {
280     vlc_object_t *p_obj;
281
282     if( destroy_date > mdate() ) return destroy_date;
283
284     if( destroy_date == 0 )
285     {
286         /* give a little time */
287         return mdate() + I64C(1000000);
288     }
289     else
290     {
291         while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
292         {
293             if( p_obj->p_parent != (vlc_object_t*)p_playlist )
294             {
295                 /* only first child (ie unused) */
296                 vlc_object_release( p_obj );
297                 break;
298             }
299             if( i_type == VLC_OBJECT_VOUT )
300             {
301                 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
302                 vlc_object_detach( p_obj );
303                 vlc_object_release( p_obj );
304                 vout_Destroy( (vout_thread_t *)p_obj );
305             }
306             else if( i_type == VLC_OBJECT_SOUT )
307             {
308                 vlc_object_release( p_obj );
309                 sout_DeleteInstance( (sout_instance_t*)p_obj );
310             }
311         }
312         return 0;
313     }
314 }
315
316 /*****************************************************************************
317  * RunThread: main playlist thread
318  *****************************************************************************/
319 static void RunThread ( playlist_t *p_playlist )
320 {
321     vlc_object_t *p_obj;
322     vlc_value_t val;
323
324     mtime_t    i_vout_destroyed_date = 0;
325     mtime_t    i_sout_destroyed_date = 0;
326
327     /* Tell above that we're ready */
328     vlc_thread_ready( p_playlist );
329
330     while( !p_playlist->b_die )
331     {
332         vlc_mutex_lock( &p_playlist->object_lock );
333
334         /* If there is an input, check that it doesn't need to die. */
335         if( p_playlist->p_input )
336         {
337             /* This input is dead. Remove it ! */
338             if( p_playlist->p_input->b_dead )
339             {
340                 input_thread_t *p_input;
341
342                 p_input = p_playlist->p_input;
343                 p_playlist->p_input = NULL;
344
345                 /* Release the playlist lock, because we may get stuck
346                  * in input_DestroyThread() for some time. */
347                 vlc_mutex_unlock( &p_playlist->object_lock );
348
349                 /* Destroy input */
350                 input_DestroyThread( p_input );
351
352                 /* Unlink current input
353                  * (_after_ input_DestroyThread for vout garbage collector) */
354                 vlc_object_detach( p_input );
355
356                 /* Destroy object */
357                 vlc_object_destroy( p_input );
358
359                 i_vout_destroyed_date = 0;
360                 i_sout_destroyed_date = 0;
361                 continue;
362             }
363             /* This input is dying, let him do */
364             else if( p_playlist->p_input->b_die )
365             {
366                 ;
367             }
368             /* This input has finished, ask him to die ! */
369             else if( p_playlist->p_input->b_error
370                       || p_playlist->p_input->b_eof )
371             {
372                 /* Check for autodeletion */
373                 if( p_playlist->pp_items[p_playlist->i_index]->b_autodeletion )
374                 {
375                     vlc_mutex_unlock( &p_playlist->object_lock );
376                     playlist_Delete( p_playlist, p_playlist->i_index );
377                     p_playlist->i_index++;
378                     p_playlist->i_status = PLAYLIST_RUNNING;
379                 }
380                 else
381                 {
382                     /* Select the next playlist item */
383                     SkipItem( p_playlist, 1 );
384                     input_StopThread( p_playlist->p_input );
385                     vlc_mutex_unlock( &p_playlist->object_lock );
386                 }
387                 continue;
388             }
389             else if( p_playlist->p_input->stream.control.i_status != INIT_S )
390             {
391                 vlc_mutex_unlock( &p_playlist->object_lock );
392                 i_vout_destroyed_date =
393                     ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
394                                             i_vout_destroyed_date );
395                 i_sout_destroyed_date =
396                     ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
397                                             i_sout_destroyed_date );
398                 vlc_mutex_lock( &p_playlist->object_lock );
399             }
400         }
401         else if( p_playlist->i_status != PLAYLIST_STOPPED )
402         {
403             /* Start another input. Let's check if that item has
404              * been forced. In that case, we override random (by not skipping)
405              * and play-and-stop */
406             vlc_bool_t b_forced;
407             var_Get( p_playlist, "prevent-skip", &val );
408             b_forced = val.b_bool;
409             if( val.b_bool == VLC_FALSE )
410             {
411                 SkipItem( p_playlist, 0 );
412             }
413             /* Reset forced status */
414             val.b_bool = VLC_FALSE;
415             var_Set( p_playlist, "prevent-skip", val );
416             /* Check for play-and-stop */
417             var_Get( p_playlist, "play-and-stop", &val );
418             if( val.b_bool == VLC_FALSE || b_forced == VLC_TRUE )
419             {
420                 PlayItem( p_playlist );
421             }
422         }
423         else if( p_playlist->i_status == PLAYLIST_STOPPED )
424         {
425             vlc_mutex_unlock( &p_playlist->object_lock );
426             i_sout_destroyed_date =
427                 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
428             i_vout_destroyed_date =
429                 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
430             vlc_mutex_lock( &p_playlist->object_lock );
431         }
432         vlc_mutex_unlock( &p_playlist->object_lock );
433
434         msleep( INTF_IDLE_SLEEP );
435     }
436
437     /* If there is an input, kill it */
438     while( 1 )
439     {
440         vlc_mutex_lock( &p_playlist->object_lock );
441
442         if( p_playlist->p_input == NULL )
443         {
444             vlc_mutex_unlock( &p_playlist->object_lock );
445             break;
446         }
447
448         if( p_playlist->p_input->b_dead )
449         {
450             input_thread_t *p_input;
451
452             /* Unlink current input */
453             p_input = p_playlist->p_input;
454             p_playlist->p_input = NULL;
455             vlc_mutex_unlock( &p_playlist->object_lock );
456
457             /* Destroy input */
458             input_DestroyThread( p_input );
459             /* Unlink current input (_after_ input_DestroyThread for vout
460              * garbage collector)*/
461             vlc_object_detach( p_input );
462
463             /* Destroy object */
464             vlc_object_destroy( p_input );
465             continue;
466         }
467         else if( p_playlist->p_input->b_die )
468         {
469             /* This input is dying, leave him alone */
470             ;
471         }
472         else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
473         {
474             input_StopThread( p_playlist->p_input );
475             vlc_mutex_unlock( &p_playlist->object_lock );
476             continue;
477         }
478         else
479         {
480             p_playlist->p_input->b_eof = 1;
481         }
482
483         vlc_mutex_unlock( &p_playlist->object_lock );
484
485         msleep( INTF_IDLE_SLEEP );
486     }
487
488     /* close all remaining sout */
489     while( ( p_obj = vlc_object_find( p_playlist,
490                                       VLC_OBJECT_SOUT, FIND_CHILD ) ) )
491     {
492         vlc_object_release( p_obj );
493         sout_DeleteInstance( (sout_instance_t*)p_obj );
494     }
495
496     /* close all remaining vout */
497     while( ( p_obj = vlc_object_find( p_playlist,
498                                       VLC_OBJECT_VOUT, FIND_CHILD ) ) )
499     {
500         vlc_object_detach( p_obj );
501         vlc_object_release( p_obj );
502         vout_Destroy( (vout_thread_t *)p_obj );
503     }
504 }
505
506 /*****************************************************************************
507  * SkipItem: go to Xth playlist item
508  *****************************************************************************
509  * This function calculates the position of the next playlist item, depending
510  * on the playlist course mode (forward, backward, random...).
511  *****************************************************************************/
512 static void SkipItem( playlist_t *p_playlist, int i_arg )
513 {
514     int i_oldindex = p_playlist->i_index;
515     vlc_bool_t b_random, b_repeat, b_loop;
516     vlc_value_t val;
517     int i_count;
518
519     /* If the playlist is empty, there is no current item */
520     if( p_playlist->i_size == 0 )
521     {
522         p_playlist->i_index = -1;
523         return;
524     }
525
526     var_Get( p_playlist, "random", &val );
527     b_random = val.b_bool;
528     var_Get( p_playlist, "repeat", &val );
529     b_repeat = val.b_bool;
530     var_Get( p_playlist, "loop", &val );
531     b_loop = val.b_bool;
532
533     /* Increment */
534     if( b_random )
535     {
536         srand( (unsigned int)mdate() );
537         i_count = 0;
538         while( i_count < p_playlist->i_size )
539         {
540             p_playlist->i_index =
541                 (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
542             /* Check if the item has not already been played */
543             if( p_playlist->pp_items[p_playlist->i_index]->i_nb_played == 0 )
544                 break;
545             i_count++;
546         }
547         if( i_count == p_playlist->i_size )
548         {
549             /* The whole playlist has been played: reset the counters */
550             while( i_count > 0 )
551             {
552                 p_playlist->pp_items[--i_count]->i_nb_played = 0;
553             }
554             if( !b_loop )
555             {
556                 p_playlist->i_status = PLAYLIST_STOPPED;
557             }
558         }
559     }
560     else if( !b_repeat )
561     {
562         p_playlist->i_index += i_arg;
563     }
564
565     /* Boundary check */
566     if( p_playlist->i_index >= p_playlist->i_size )
567     {
568         if( p_playlist->i_status == PLAYLIST_STOPPED || b_random || b_loop )
569         {
570             p_playlist->i_index -= p_playlist->i_size
571                          * ( p_playlist->i_index / p_playlist->i_size );
572         }
573         else
574         {
575             /* Don't loop by default: stop at playlist end */
576             p_playlist->i_index = i_oldindex;
577             p_playlist->i_status = PLAYLIST_STOPPED;
578         }
579     }
580     else if( p_playlist->i_index < 0 )
581     {
582         p_playlist->i_index = p_playlist->i_size - 1;
583     }
584
585     /* Check that the item is enabled */
586     if( p_playlist->pp_items[p_playlist->i_index]->b_enabled == VLC_FALSE &&
587         p_playlist->i_enabled != 0)
588     {
589         SkipItem( p_playlist , 1 );
590     }
591 }
592
593 /*****************************************************************************
594  * PlayItem: play current playlist item
595  *****************************************************************************
596  * This function calculates the position of the next playlist item, depending
597  * on the playlist course mode (forward, backward, random...).
598  *****************************************************************************/
599 static void PlayItem( playlist_t *p_playlist )
600 {
601     playlist_item_t *p_item;
602     vlc_value_t val;
603     if( p_playlist->i_index == -1 )
604     {
605         if( p_playlist->i_size == 0 || p_playlist->i_enabled == 0)
606         {
607             return;
608         }
609         SkipItem( p_playlist, 1 );
610     }
611     if( p_playlist->i_enabled == 0)
612     {
613         return;
614     }
615
616     msg_Dbg( p_playlist, "creating new input thread" );
617     p_item = p_playlist->pp_items[p_playlist->i_index];
618
619     p_item->i_nb_played++;
620     p_playlist->p_input = input_CreateThread( p_playlist, &p_item->input );
621
622     val.i_int = p_playlist->i_index;
623     /* unlock the playlist to set the var...mmm */
624     vlc_mutex_unlock( &p_playlist->object_lock);
625     var_Set( p_playlist, "playlist-current", val);
626     vlc_mutex_lock( &p_playlist->object_lock);
627 }