]> git.sesse.net Git - vlc/blob - src/playlist/playlist.c
removed unused variables
[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, "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 mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
266                                        mtime_t destroy_date )
267 {
268     vlc_object_t *p_obj;
269
270     if( destroy_date > mdate() ) return destroy_date;
271
272     if( destroy_date == 0 )
273     {
274         /* give a little time */
275         return mdate() + I64C(1000000);
276     }
277     else
278     {
279         while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
280         {
281             if( p_obj->p_parent != (vlc_object_t*)p_playlist )
282             {
283                 /* only first child (ie unused) */
284                 vlc_object_release( p_obj );
285                 break;
286             }
287             if( i_type == VLC_OBJECT_VOUT )
288             {
289                 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
290                 vlc_object_detach( p_obj );
291                 vlc_object_release( p_obj );
292                 vout_Destroy( (vout_thread_t *)p_obj );
293             }
294             else if( i_type == VLC_OBJECT_SOUT )
295             {
296                 vlc_object_release( p_obj );
297                 sout_DeleteInstance( (sout_instance_t*)p_obj );
298             }
299         }
300         return 0;
301     }
302 }
303
304 /*****************************************************************************
305  * RunThread: main playlist thread
306  *****************************************************************************/
307 static void RunThread ( playlist_t *p_playlist )
308 {
309     vlc_object_t *p_obj;
310     vlc_value_t val;
311
312     mtime_t    i_vout_destroyed_date = 0;
313     mtime_t    i_sout_destroyed_date = 0;
314
315     /* Tell above that we're ready */
316     vlc_thread_ready( p_playlist );
317
318     while( !p_playlist->b_die )
319     {
320         vlc_mutex_lock( &p_playlist->object_lock );
321
322         /* If there is an input, check that it doesn't need to die. */
323         if( p_playlist->p_input )
324         {
325             /* This input is dead. Remove it ! */
326             if( p_playlist->p_input->b_dead )
327             {
328                 input_thread_t *p_input;
329
330                 p_input = p_playlist->p_input;
331                 p_playlist->p_input = NULL;
332
333                 /* Release the playlist lock, because we may get stuck
334                  * in input_DestroyThread() for some time. */
335                 vlc_mutex_unlock( &p_playlist->object_lock );
336
337                 /* Destroy input */
338                 input_DestroyThread( p_input );
339
340                 /* Unlink current input
341                  * (_after_ input_DestroyThread for vout garbage collector) */
342                 vlc_object_detach( p_input );
343
344                 /* Destroy object */
345                 vlc_object_destroy( p_input );
346
347                 i_vout_destroyed_date = 0;
348                 i_sout_destroyed_date = 0;
349                 continue;
350             }
351             /* This input is dying, let him do */
352             else if( p_playlist->p_input->b_die )
353             {
354                 ;
355             }
356             /* This input has finished, ask him to die ! */
357             else if( p_playlist->p_input->b_error
358                       || p_playlist->p_input->b_eof )
359             {
360                 /* Check for autodeletion */
361                 if( p_playlist->pp_items[p_playlist->i_index]->b_autodeletion )
362                 {
363                     vlc_mutex_unlock( &p_playlist->object_lock );
364                     playlist_Delete( p_playlist, p_playlist->i_index );
365                     p_playlist->i_index++;
366                     p_playlist->i_status = PLAYLIST_RUNNING;
367                 }
368                 else
369                 {
370                     /* Select the next playlist item */
371                     SkipItem( p_playlist, 1 );
372                     input_StopThread( p_playlist->p_input );
373                     vlc_mutex_unlock( &p_playlist->object_lock );
374                 }
375                 continue;
376             }
377             else if( p_playlist->p_input->stream.control.i_status != INIT_S )
378             {
379                 vlc_mutex_unlock( &p_playlist->object_lock );
380                 i_vout_destroyed_date =
381                     ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
382                                             i_vout_destroyed_date );
383                 i_sout_destroyed_date =
384                     ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
385                                             i_sout_destroyed_date );
386                 vlc_mutex_lock( &p_playlist->object_lock );
387             }
388         }
389         else if( p_playlist->i_status != PLAYLIST_STOPPED )
390         {
391             var_Get( p_playlist, "prevent-skip", &val);
392             if( val.b_bool == VLC_FALSE)
393             {
394                 SkipItem( p_playlist, 0 );
395             }
396             val.b_bool = VLC_TRUE;
397             var_Set( p_playlist, "prevent-skip", val);
398             PlayItem( p_playlist );
399         }
400         else if( p_playlist->i_status == PLAYLIST_STOPPED )
401         {
402             vlc_mutex_unlock( &p_playlist->object_lock );
403             i_sout_destroyed_date =
404                 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
405             i_vout_destroyed_date =
406                 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
407             vlc_mutex_lock( &p_playlist->object_lock );
408         }
409         vlc_mutex_unlock( &p_playlist->object_lock );
410
411         msleep( INTF_IDLE_SLEEP );
412     }
413
414     /* If there is an input, kill it */
415     while( 1 )
416     {
417         vlc_mutex_lock( &p_playlist->object_lock );
418
419         if( p_playlist->p_input == NULL )
420         {
421             vlc_mutex_unlock( &p_playlist->object_lock );
422             break;
423         }
424
425         if( p_playlist->p_input->b_dead )
426         {
427             input_thread_t *p_input;
428
429             /* Unlink current input */
430             p_input = p_playlist->p_input;
431             p_playlist->p_input = NULL;
432             vlc_mutex_unlock( &p_playlist->object_lock );
433
434             /* Destroy input */
435             input_DestroyThread( p_input );
436             /* Unlink current input (_after_ input_DestroyThread for vout
437              * garbage collector)*/
438             vlc_object_detach( p_input );
439
440             /* Destroy object */
441             vlc_object_destroy( p_input );
442             continue;
443         }
444         else if( p_playlist->p_input->b_die )
445         {
446             /* This input is dying, leave him alone */
447             ;
448         }
449         else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
450         {
451             input_StopThread( p_playlist->p_input );
452             vlc_mutex_unlock( &p_playlist->object_lock );
453             continue;
454         }
455         else
456         {
457             p_playlist->p_input->b_eof = 1;
458         }
459
460         vlc_mutex_unlock( &p_playlist->object_lock );
461
462         msleep( INTF_IDLE_SLEEP );
463     }
464
465     /* close all remaining sout */
466     while( ( p_obj = vlc_object_find( p_playlist,
467                                       VLC_OBJECT_SOUT, FIND_CHILD ) ) )
468     {
469         vlc_object_release( p_obj );
470         sout_DeleteInstance( (sout_instance_t*)p_obj );
471     }
472
473     /* close all remaining vout */
474     while( ( p_obj = vlc_object_find( p_playlist,
475                                       VLC_OBJECT_VOUT, FIND_CHILD ) ) )
476     {
477         vlc_object_detach( p_obj );
478         vlc_object_release( p_obj );
479         vout_Destroy( (vout_thread_t *)p_obj );
480     }
481 }
482
483 /*****************************************************************************
484  * SkipItem: go to Xth playlist item
485  *****************************************************************************
486  * This function calculates the position of the next playlist item, depending
487  * on the playlist course mode (forward, backward, random...).
488  *****************************************************************************/
489 static void SkipItem( playlist_t *p_playlist, int i_arg )
490 {
491     int i_oldindex = p_playlist->i_index;
492     vlc_bool_t b_random, b_repeat, b_loop;
493     vlc_value_t val;
494
495     /* If the playlist is empty, there is no current item */
496     if( p_playlist->i_size == 0 )
497     {
498         p_playlist->i_index = -1;
499         return;
500     }
501
502     var_Get( p_playlist, "random", &val );
503     b_random = val.b_bool;
504     var_Get( p_playlist, "repeat", &val );
505     b_repeat = val.b_bool;
506     var_Get( p_playlist, "loop", &val );
507     b_loop = val.b_bool;
508
509     /* Increment */
510     if( b_random )
511     {
512         srand( (unsigned int)mdate() );
513
514         /* Simple random stuff - we cheat a bit to minimize the chances to
515          * get the same index again. */
516         i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
517         if( i_arg == 0 )
518         {
519             i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
520         }
521     }
522     if( b_repeat )
523     {
524         i_arg = 0;
525     }
526     p_playlist->i_index += i_arg;
527
528     /* Boundary check */
529     if( p_playlist->i_index >= p_playlist->i_size )
530     {
531         if( p_playlist->i_status == PLAYLIST_STOPPED || b_random || b_loop )
532         {
533             p_playlist->i_index -= p_playlist->i_size
534                          * ( p_playlist->i_index / p_playlist->i_size );
535         }
536         else
537         {
538             /* Don't loop by default: stop at playlist end */
539             p_playlist->i_index = i_oldindex;
540             p_playlist->i_status = PLAYLIST_STOPPED;
541         }
542     }
543     else if( p_playlist->i_index < 0 )
544     {
545         p_playlist->i_index = p_playlist->i_size - 1;
546     }
547
548     /* Check that the item is enabled */
549     if( p_playlist->pp_items[p_playlist->i_index]->b_enabled == VLC_FALSE &&
550         p_playlist->i_enabled != 0)
551     {
552         SkipItem( p_playlist , 1 );
553     }
554 }
555
556 /*****************************************************************************
557  * PlayItem: play current playlist item
558  *****************************************************************************
559  * This function calculates the position of the next playlist item, depending
560  * on the playlist course mode (forward, backward, random...).
561  *****************************************************************************/
562 static void PlayItem( playlist_t *p_playlist )
563 {
564     playlist_item_t *p_item;
565     vlc_value_t val;
566     if( p_playlist->i_index == -1 )
567     {
568         if( p_playlist->i_size == 0 || p_playlist->i_enabled == 0)
569         {
570             return;
571         }
572         SkipItem( p_playlist, 1 );
573     }
574     if( p_playlist->i_enabled == 0)
575     {
576         return;
577     }
578
579     msg_Dbg( p_playlist, "creating new input thread" );
580     p_item = p_playlist->pp_items[p_playlist->i_index];
581
582     p_item->i_nb_played++;
583
584     p_playlist->p_input = input_CreateThread( p_playlist, p_item->psz_uri,
585                                               p_item->ppsz_options,
586                                               p_item->i_options );
587
588     val.i_int = p_playlist->i_index;
589     var_Set( p_playlist, "playlist-current", val);
590 }