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