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