]> git.sesse.net Git - vlc/blob - src/playlist/playlist.c
1c667fdf788595c7491301733664a6f47afb4477
[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  *          ClĂ©ment Stenac <zorglub@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24 #include <stdlib.h>                                      /* free(), strtol() */
25 #include <stdio.h>                                              /* sprintf() */
26 #include <string.h>                                            /* strerror() */
27
28 #include <vlc/vlc.h>
29 #include <vlc/vout.h>
30 #include <vlc/sout.h>
31 #include <vlc/input.h>
32
33 #include "vlc_playlist.h"
34
35 #define TITLE_CATEGORY N_( "By category" )
36 #define TITLE_SIMPLE   N_( "Manually added" )
37 #define TITLE_ALL      N_( "All items, unsorted" )
38
39 #undef PLAYLIST_PROFILE
40 #undef PLAYLIST_DEBUG
41
42 /*****************************************************************************
43  * Local prototypes
44  *****************************************************************************/
45 static void RunThread ( playlist_t * );
46 static void RunPreparse( playlist_preparse_t * );
47 static playlist_item_t * NextItem  ( playlist_t * );
48 static int PlayItem  ( playlist_t *, playlist_item_t * );
49
50 static int ItemChange( vlc_object_t *, const char *,
51                        vlc_value_t, vlc_value_t, void * );
52
53 int playlist_vaControl( playlist_t * p_playlist, int i_query, va_list args );
54
55
56 /**
57  * Create playlist
58  *
59  * Create a playlist structure.
60  * \param p_parent the vlc object that is to be the parent of this playlist
61  * \return a pointer to the created playlist, or NULL on error
62  */
63 playlist_t * __playlist_Create ( vlc_object_t *p_parent )
64 {
65     playlist_t *p_playlist;
66     playlist_view_t *p_view;
67     vlc_value_t     val;
68
69     /* Allocate structure */
70     p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
71     if( !p_playlist )
72     {
73         msg_Err( p_parent, "out of memory" );
74         return NULL;
75     }
76
77     /* These variables control updates */
78     var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
79     val.b_bool = VLC_TRUE;
80     var_Set( p_playlist, "intf-change", val );
81
82     var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
83     val.i_int = -1;
84     var_Set( p_playlist, "item-change", val );
85
86     var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
87     val.i_int = -1;
88     var_Set( p_playlist, "item-deleted", val );
89
90     var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
91
92     var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
93     val.i_int = -1;
94     var_Set( p_playlist, "playlist-current", val );
95
96     var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
97
98     var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
99     val.b_bool = VLC_TRUE;
100     var_Set( p_playlist, "intf-show", val );
101
102
103     /* Variables to control playback */
104     var_CreateGetBool( p_playlist, "play-and-stop" );
105     var_CreateGetBool( p_playlist, "random" );
106     var_CreateGetBool( p_playlist, "repeat" );
107     var_CreateGetBool( p_playlist, "loop" );
108
109     /* Initialise data structures */
110     vlc_mutex_init( p_playlist, &p_playlist->gc_lock );
111     p_playlist->i_last_id = 0;
112     p_playlist->b_go_next = VLC_TRUE;
113     p_playlist->p_input = NULL;
114
115     p_playlist->request_date = 0;
116
117     p_playlist->i_views = 0;
118     p_playlist->pp_views = NULL;
119
120     p_playlist->i_index = -1;
121     p_playlist->i_size = 0;
122     p_playlist->pp_items = NULL;
123     p_playlist->i_all_size = 0;
124     p_playlist->pp_all_items = 0;
125
126     playlist_ViewInsert( p_playlist, VIEW_CATEGORY, TITLE_CATEGORY );
127     playlist_ViewInsert( p_playlist, VIEW_ALL, TITLE_ALL );
128
129     p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
130
131     p_playlist->p_general =
132         playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
133                              _( "General" ), p_view->p_root );
134     p_playlist->p_general->i_flags |= PLAYLIST_RO_FLAG;
135
136     /* Set startup status
137      * We set to simple view on startup for interfaces that don't do
138      * anything */
139     p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
140     p_playlist->status.i_view = VIEW_CATEGORY;
141     p_playlist->status.p_item = NULL;
142     p_playlist->status.p_node = p_view->p_root;
143     p_playlist->request.b_request = VLC_FALSE;
144     p_playlist->status.i_status = PLAYLIST_STOPPED;
145
146
147     p_playlist->i_sort = SORT_ID;
148     p_playlist->i_order = ORDER_NORMAL;
149
150     /* Finally, launch the thread ! */
151     if( vlc_thread_create( p_playlist, "playlist", RunThread,
152                            VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
153     {
154         msg_Err( p_playlist, "cannot spawn playlist thread" );
155         vlc_object_destroy( p_playlist );
156         return NULL;
157     }
158
159     /* Preparsing stuff */
160     p_playlist->p_preparse = vlc_object_create( p_playlist,
161                                                 sizeof( playlist_preparse_t ) );
162     if( !p_playlist->p_preparse )
163     {
164         msg_Err( p_playlist, "unable to create preparser" );
165         vlc_object_destroy( p_playlist );
166         return NULL;
167     }
168
169     p_playlist->p_preparse->i_waiting = 0;
170     p_playlist->p_preparse->pp_waiting = NULL;
171
172     vlc_object_attach( p_playlist->p_preparse, p_playlist );
173     if( vlc_thread_create( p_playlist->p_preparse, "preparser",
174                            RunPreparse, VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
175     {
176         msg_Err( p_playlist, "cannot spawn preparse thread" );
177         vlc_object_detach( p_playlist->p_preparse );
178         vlc_object_destroy( p_playlist->p_preparse );
179         return NULL;
180     }
181
182     /* The object has been initialized, now attach it */
183     vlc_object_attach( p_playlist, p_parent );
184
185     return p_playlist;
186 }
187
188 /**
189  * Destroy the playlist.
190  *
191  * Delete all items in the playlist and free the playlist structure.
192  * \param p_playlist the playlist structure to destroy
193  * \return VLC_SUCCESS or an error
194  */
195 int playlist_Destroy( playlist_t * p_playlist )
196 {
197     int i;
198     p_playlist->b_die = 1;
199
200     while( p_playlist->i_sds )
201     {
202         playlist_ServicesDiscoveryRemove( p_playlist,
203                                           p_playlist->pp_sds[0]->psz_module );
204     }
205
206     vlc_thread_join( p_playlist->p_preparse );
207     vlc_thread_join( p_playlist );
208
209     vlc_object_detach( p_playlist->p_preparse );
210
211     var_Destroy( p_playlist, "intf-change" );
212     var_Destroy( p_playlist, "item-change" );
213     var_Destroy( p_playlist, "playlist-current" );
214     var_Destroy( p_playlist, "intf-popmenu" );
215     var_Destroy( p_playlist, "intf-show" );
216     var_Destroy( p_playlist, "play-and-stop" );
217     var_Destroy( p_playlist, "random" );
218     var_Destroy( p_playlist, "repeat" );
219     var_Destroy( p_playlist, "loop" );
220
221     playlist_Clear( p_playlist );
222
223     for( i = p_playlist->i_views - 1; i >= 0 ; i-- )
224     {
225         playlist_view_t *p_view = p_playlist->pp_views[i];
226         if( p_view->psz_name )
227             free( p_view->psz_name );
228         playlist_ItemDelete( p_view->p_root );
229         REMOVE_ELEM( p_playlist->pp_views, p_playlist->i_views, i );
230         free( p_view );
231     }
232
233     vlc_mutex_destroy( &p_playlist->gc_lock );
234     vlc_object_destroy( p_playlist->p_preparse );
235     vlc_object_destroy( p_playlist );
236
237     return VLC_SUCCESS;
238 }
239
240
241 /**
242  * Do a playlist action.
243  *
244  * If there is something in the playlist then you can do playlist actions.
245  *
246  * Playlist lock must not be taken when calling this function
247  *
248  * \param p_playlist the playlist to do the command on
249  * \param i_query the command to do
250  * \param variable number of arguments
251  * \return VLC_SUCCESS or an error
252  */
253 int playlist_LockControl( playlist_t * p_playlist, int i_query, ... )
254 {
255     va_list args;
256     int i_result;
257     va_start( args, i_query );
258     vlc_mutex_lock( &p_playlist->object_lock );
259     i_result = playlist_vaControl( p_playlist, i_query, args );
260     va_end( args );
261     vlc_mutex_unlock( &p_playlist->object_lock );
262     return i_result;
263 }
264
265 /**
266  * Do a playlist action.
267  *
268  * If there is something in the playlist then you can do playlist actions.
269  *
270  * Playlist lock must be taken when calling this function
271  *
272  * \param p_playlist the playlist to do the command on
273  * \param i_query the command to do
274  * \param variable number of arguments
275  * \return VLC_SUCCESS or an error
276  */
277 int playlist_Control( playlist_t * p_playlist, int i_query, ... )
278 {
279     va_list args;
280     int i_result;
281     va_start( args, i_query );
282     i_result = playlist_vaControl( p_playlist, i_query, args );
283     va_end( args );
284
285     return i_result;
286 }
287
288 int playlist_vaControl( playlist_t * p_playlist, int i_query, va_list args )
289 {
290     playlist_view_t *p_view;
291     playlist_item_t *p_item, *p_node;
292     int i_view;
293     vlc_value_t val;
294
295 #ifdef PLAYLIST_PROFILE
296     p_playlist->request_date = mdate();
297 #endif
298
299     if( p_playlist->i_size <= 0 )
300     {
301         return VLC_EGENERIC;
302     }
303
304     switch( i_query )
305     {
306     case PLAYLIST_STOP:
307         p_playlist->status.i_status = PLAYLIST_STOPPED;
308         p_playlist->request.b_request = VLC_TRUE;
309         break;
310
311     case PLAYLIST_ITEMPLAY:
312         p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
313         if ( p_item == NULL || p_item->input.psz_uri == NULL )
314             return VLC_EGENERIC;
315         p_playlist->status.i_status = PLAYLIST_RUNNING;
316         p_playlist->request.i_skip = 0;
317         p_playlist->request.b_request = VLC_TRUE;
318         p_playlist->request.p_item = p_item;
319         p_playlist->request.i_view = p_playlist->status.i_view;
320         p_view = playlist_ViewFind( p_playlist, p_playlist->status.i_view );
321         if( p_view )
322         {
323             p_playlist->request.p_node = p_view->p_root;
324         }
325         else
326         {
327             p_playlist->request.p_node = NULL;
328         }
329         break;
330
331     case PLAYLIST_VIEWPLAY:
332         i_view = (int)va_arg( args,int );
333         p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
334         p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
335         if ( p_node == NULL || p_item == NULL || p_item->input.psz_uri == NULL )
336         {
337             p_playlist->status.i_status = PLAYLIST_STOPPED;
338             p_playlist->request.b_request = VLC_TRUE;
339             return VLC_SUCCESS;
340         }
341         p_playlist->status.i_status = PLAYLIST_RUNNING;
342         p_playlist->request.i_skip = 0;
343         p_playlist->request.b_request = VLC_TRUE;
344         p_playlist->request.i_view = i_view;
345         p_playlist->request.p_node = p_node;
346         p_playlist->request.p_item = p_item;
347
348         /* If we select a node, play only it.
349          * If we select an item, continue */
350         if( p_playlist->request.p_item == NULL ||
351             ! p_playlist->request.p_node->i_flags & PLAYLIST_SKIP_FLAG )
352         {
353             p_playlist->b_go_next = VLC_FALSE;
354         }
355         else
356         {
357             p_playlist->b_go_next = VLC_TRUE;
358         }
359         break;
360
361     case PLAYLIST_PLAY:
362         p_playlist->status.i_status = PLAYLIST_RUNNING;
363
364         if( p_playlist->p_input )
365         {
366             val.i_int = PLAYING_S;
367             var_Set( p_playlist->p_input, "state", val );
368             break;
369         }
370
371         /* FIXME : needed ? */
372         p_playlist->request.b_request = VLC_TRUE;
373         p_playlist->request.i_view = p_playlist->status.i_view;
374         p_playlist->request.p_node = p_playlist->status.p_node;
375         p_playlist->request.p_item = p_playlist->status.p_item;
376         p_playlist->request.i_skip = 0;
377         p_playlist->request.i_goto = -1;
378         break;
379
380     case PLAYLIST_AUTOPLAY:
381         p_playlist->status.i_status = PLAYLIST_RUNNING;
382
383         p_playlist->request.b_request = VLC_FALSE;
384         break;
385
386     case PLAYLIST_PAUSE:
387         val.i_int = 0;
388         if( p_playlist->p_input )
389             var_Get( p_playlist->p_input, "state", &val );
390
391         if( val.i_int == PAUSE_S )
392         {
393             p_playlist->status.i_status = PLAYLIST_RUNNING;
394             if( p_playlist->p_input )
395             {
396                 val.i_int = PLAYING_S;
397                 var_Set( p_playlist->p_input, "state", val );
398             }
399         }
400         else
401         {
402             p_playlist->status.i_status = PLAYLIST_PAUSED;
403             if( p_playlist->p_input )
404             {
405                 val.i_int = PAUSE_S;
406                 var_Set( p_playlist->p_input, "state", val );
407             }
408         }
409         break;
410
411     case PLAYLIST_SKIP:
412         if( p_playlist->status.i_view > -1 )
413         {
414             p_playlist->request.i_view = p_playlist->status.i_view;
415             p_playlist->request.p_node = p_playlist->status.p_node;
416             p_playlist->request.p_item = p_playlist->status.p_item;
417         }
418         p_playlist->request.i_skip = (int) va_arg( args, int );
419         p_playlist->request.b_request = VLC_TRUE;
420         break;
421
422     case PLAYLIST_GOTO:
423         p_playlist->status.i_status = PLAYLIST_RUNNING;
424         p_playlist->request.p_node = NULL;
425         p_playlist->request.p_item = NULL;
426         p_playlist->request.i_view = -1;
427         p_playlist->request.i_goto = (int) va_arg( args, int );
428         p_playlist->request.b_request = VLC_TRUE;
429         break;
430
431     default:
432         msg_Err( p_playlist, "unimplemented playlist query" );
433         return VLC_EBADVAR;
434         break;
435     }
436
437     return VLC_SUCCESS;
438 }
439
440 int playlist_PreparseEnqueue( playlist_t *p_playlist,
441                               input_item_t *p_item )
442 {
443     vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
444     INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
445                  p_playlist->p_preparse->i_waiting,
446                  p_playlist->p_preparse->i_waiting,
447                  p_item );
448     vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
449     return VLC_SUCCESS;
450 }
451
452
453 /* Destroy remaining objects */
454 static mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
455                                        mtime_t destroy_date )
456 {
457     vlc_object_t *p_obj;
458
459     if( destroy_date > mdate() ) return destroy_date;
460
461     if( destroy_date == 0 )
462     {
463         /* give a little time */
464         return mdate() + I64C(1000000);
465     }
466     else
467     {
468         vlc_mutex_lock( &p_playlist->gc_lock );
469         while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
470         {
471             if( p_obj->p_parent != (vlc_object_t*)p_playlist )
472             {
473                 /* only first child (ie unused) */
474                 vlc_object_release( p_obj );
475                 break;
476             }
477             if( i_type == VLC_OBJECT_VOUT )
478             {
479                 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
480                 vlc_object_detach( p_obj );
481                 vlc_object_release( p_obj );
482                 vout_Destroy( (vout_thread_t *)p_obj );
483             }
484             else if( i_type == VLC_OBJECT_SOUT )
485             {
486                 vlc_object_release( p_obj );
487                 sout_DeleteInstance( (sout_instance_t*)p_obj );
488             }
489         }
490         vlc_mutex_unlock( &p_playlist->gc_lock );
491         return 0;
492     }
493 }
494
495 /*****************************************************************************
496  * RunThread: main playlist thread
497  *****************************************************************************/
498 static void RunThread ( playlist_t *p_playlist )
499 {
500     vlc_object_t *p_obj;
501     playlist_item_t *p_item = NULL;
502
503     mtime_t    i_vout_destroyed_date = 0;
504     mtime_t    i_sout_destroyed_date = 0;
505
506     playlist_item_t *p_autodelete_item = NULL;
507
508     /* Tell above that we're ready */
509     vlc_thread_ready( p_playlist );
510
511     while( !p_playlist->b_die )
512     {
513         vlc_mutex_lock( &p_playlist->object_lock );
514
515         /* First, check if we have something to do */
516         /* FIXME : this can be called several times */
517         if( p_playlist->request.b_request )
518         {
519 #ifdef PLAYLIST_PROFILE
520             msg_Dbg(p_playlist, "beginning processing of request, "
521                          I64Fi" us ", mdate() - p_playlist->request_date );
522 #endif
523             /* Stop the existing input */
524             if( p_playlist->p_input )
525             {
526                 input_StopThread( p_playlist->p_input );
527             }
528             /* The code below will start the next input for us */
529             if( p_playlist->status.i_status == PLAYLIST_STOPPED )
530             {
531                 p_playlist->request.b_request = VLC_FALSE;
532             }
533         }
534
535         /* If there is an input, check that it doesn't need to die. */
536         if( p_playlist->p_input )
537         {
538             /* This input is dead. Remove it ! */
539             if( p_playlist->p_input->b_dead )
540             {
541                 input_thread_t *p_input;
542
543                 p_input = p_playlist->p_input;
544                 p_playlist->p_input = NULL;
545
546                 /* Release the playlist lock, because we may get stuck
547                  * in input_DestroyThread() for some time. */
548                 vlc_mutex_unlock( &p_playlist->object_lock );
549
550                 /* Destroy input */
551                 input_DestroyThread( p_input );
552
553                 /* Unlink current input
554                  * (_after_ input_DestroyThread for vout garbage collector) */
555                 vlc_object_detach( p_input );
556
557                 /* Destroy object */
558                 vlc_object_destroy( p_input );
559
560                 i_vout_destroyed_date = 0;
561                 i_sout_destroyed_date = 0;
562
563                 if( p_playlist->status.p_item->i_flags
564                     & PLAYLIST_REMOVE_FLAG )
565                 {
566                      playlist_ItemDelete( p_item );
567                      p_playlist->status.p_item = NULL;
568                 }
569
570                 continue;
571             }
572             /* This input is dying, let him do */
573             else if( p_playlist->p_input->b_die )
574             {
575                 ;
576             }
577             /* This input has finished, ask him to die ! */
578             else if( p_playlist->p_input->b_error
579                       || p_playlist->p_input->b_eof )
580             {
581                 /* TODO FIXME XXX TODO FIXME XXX */
582                 /* Check for autodeletion */
583
584                 if( p_playlist->status.p_item->i_flags & PLAYLIST_DEL_FLAG )
585                 {
586                     p_autodelete_item = p_playlist->status.p_item;
587                 }
588                 input_StopThread( p_playlist->p_input );
589                 /* Select the next playlist item */
590                 vlc_mutex_unlock( &p_playlist->object_lock );
591                 continue;
592             }
593             else if( p_playlist->p_input->i_state != INIT_S )
594             {
595                 vlc_mutex_unlock( &p_playlist->object_lock );
596                 i_vout_destroyed_date =
597                     ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
598                                             i_vout_destroyed_date );
599                 i_sout_destroyed_date =
600                     ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
601                                             i_sout_destroyed_date );
602                 vlc_mutex_lock( &p_playlist->object_lock );
603             }
604         }
605         else if( p_playlist->status.i_status != PLAYLIST_STOPPED )
606         {
607             /* Start another input.
608              * Get the next item to play */
609             p_item = NextItem( p_playlist );
610
611
612             /* We must stop */
613             if( p_item == NULL )
614             {
615                 if( p_autodelete_item )
616                 {
617                     playlist_Delete( p_playlist,
618                                      p_autodelete_item->input.i_id );
619                     p_autodelete_item = NULL;
620                 }
621                 p_playlist->status.i_status = PLAYLIST_STOPPED;
622                 vlc_mutex_unlock( &p_playlist->object_lock );
623                 continue;
624             }
625
626             PlayItem( p_playlist, p_item );
627
628             if( p_autodelete_item )
629             {
630                 playlist_Delete( p_playlist, p_autodelete_item->input.i_id );
631                 p_autodelete_item = NULL;
632             }
633         }
634         else if( p_playlist->status.i_status == PLAYLIST_STOPPED )
635         {
636             /* Collect garbage */
637             vlc_mutex_unlock( &p_playlist->object_lock );
638             i_sout_destroyed_date =
639                 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
640             i_vout_destroyed_date =
641                 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
642             vlc_mutex_lock( &p_playlist->object_lock );
643         }
644         vlc_mutex_unlock( &p_playlist->object_lock );
645
646         msleep( INTF_IDLE_SLEEP / 2 );
647
648         /* Stop sleeping earlier if we have work */
649         /* TODO : statistics about this */
650         if ( p_playlist->request.b_request &&
651                         p_playlist->status.i_status == PLAYLIST_RUNNING )
652         {
653             continue;
654         }
655
656         msleep( INTF_IDLE_SLEEP / 2 );
657     }
658
659     /* Playlist dying */
660
661     /* If there is an input, kill it */
662     while( 1 )
663     {
664         vlc_mutex_lock( &p_playlist->object_lock );
665
666         if( p_playlist->p_input == NULL )
667         {
668             vlc_mutex_unlock( &p_playlist->object_lock );
669             break;
670         }
671
672         if( p_playlist->p_input->b_dead )
673         {
674             input_thread_t *p_input;
675
676             /* Unlink current input */
677             p_input = p_playlist->p_input;
678             p_playlist->p_input = NULL;
679             vlc_mutex_unlock( &p_playlist->object_lock );
680
681             /* Destroy input */
682             input_DestroyThread( p_input );
683             /* Unlink current input (_after_ input_DestroyThread for vout
684              * garbage collector)*/
685             vlc_object_detach( p_input );
686
687             /* Destroy object */
688             vlc_object_destroy( p_input );
689             continue;
690         }
691         else if( p_playlist->p_input->b_die )
692         {
693             /* This input is dying, leave him alone */
694             ;
695         }
696         else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
697         {
698             input_StopThread( p_playlist->p_input );
699             vlc_mutex_unlock( &p_playlist->object_lock );
700             continue;
701         }
702         else
703         {
704             p_playlist->p_input->b_eof = 1;
705         }
706
707         vlc_mutex_unlock( &p_playlist->object_lock );
708
709         msleep( INTF_IDLE_SLEEP );
710     }
711
712     /* close all remaining sout */
713     while( ( p_obj = vlc_object_find( p_playlist,
714                                       VLC_OBJECT_SOUT, FIND_CHILD ) ) )
715     {
716         vlc_object_release( p_obj );
717         sout_DeleteInstance( (sout_instance_t*)p_obj );
718     }
719
720     /* close all remaining vout */
721     while( ( p_obj = vlc_object_find( p_playlist,
722                                       VLC_OBJECT_VOUT, FIND_CHILD ) ) )
723     {
724         vlc_object_detach( p_obj );
725         vlc_object_release( p_obj );
726         vout_Destroy( (vout_thread_t *)p_obj );
727     }
728 }
729
730 /* Queue for items to preparse */
731 static void RunPreparse ( playlist_preparse_t *p_obj )
732 {
733     playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
734     vlc_bool_t b_sleep;
735
736     /* Tell above that we're ready */
737     vlc_thread_ready( p_obj );
738
739     while( !p_playlist->b_die )
740     {
741         vlc_mutex_lock( &p_obj->object_lock );
742
743         if( p_obj->i_waiting > 0 )
744         {
745             input_item_t *p_current = p_obj->pp_waiting[0];
746             REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
747             vlc_mutex_unlock( &p_obj->object_lock );
748             input_Preparse( p_playlist, p_current );
749             var_SetInteger( p_playlist, "item-change", p_current->i_id );
750             vlc_mutex_lock( &p_obj->object_lock );
751         }
752         b_sleep = ( p_obj->i_waiting == 0 );
753
754         vlc_mutex_unlock( &p_obj->object_lock );
755
756         if( p_obj->i_waiting == 0 )
757         {
758             msleep( INTF_IDLE_SLEEP );
759         }
760     }
761 }
762
763 /*****************************************************************************
764  * NextItem
765  *****************************************************************************
766  * This function calculates the next playlist item, depending
767  * on the playlist course mode (forward, backward, random, view,...).
768  *****************************************************************************/
769 static playlist_item_t * NextItem( playlist_t *p_playlist )
770 {
771     playlist_item_t *p_new = NULL;
772     int i_skip,i_goto,i, i_new, i_count ;
773     playlist_view_t *p_view;
774
775     vlc_bool_t b_loop = var_GetBool( p_playlist, "loop" );
776     vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
777     vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
778     vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
779
780 #ifdef PLAYLIST_PROFILE
781     /* Calculate time needed */
782     int64_t start = mdate();
783 #endif
784
785     /* Handle quickly a few special cases */
786
787     /* No items to play */
788     if( p_playlist->i_size == 0 )
789     {
790         msg_Info( p_playlist, "playlist is empty" );
791         return NULL;
792     }
793     /* Nothing requested */
794     if( !p_playlist->request.b_request && p_playlist->status.p_item == NULL )
795     {
796         msg_Dbg( p_playlist,"nothing requested, starting" );
797     }
798
799     /* Repeat and play/stop */
800     if( !p_playlist->request.b_request && b_repeat == VLC_TRUE )
801     {
802         msg_Dbg( p_playlist,"repeating item" );
803         return p_playlist->status.p_item;
804     }
805
806     if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
807     {
808         msg_Dbg( p_playlist,"stopping (play and stop)");
809         return NULL;
810     }
811
812     if( !p_playlist->request.b_request && p_playlist->status.p_item &&
813         !( p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG ) )
814     {
815         msg_Dbg( p_playlist, "no-skip mode, stopping") ;
816         return NULL;
817     }
818
819     /* TODO: improve this (only use current node) */
820     /* TODO: use the "shuffled view" internally ? */
821     /* Random case. This is an exception: if request, but request is skip +- 1
822      * we don't go to next item but select a new random one. */
823     if( b_random && (!p_playlist->request.b_request ||
824         p_playlist->request.i_skip == 1 || p_playlist->request.i_skip == -1 ) )
825     {
826         srand( (unsigned int)mdate() );
827         i_new = 0;
828         for( i_count = 0; i_count < p_playlist->i_size - 1 ; i_count ++ )
829         {
830             i_new =
831                 (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
832             /* Check if the item has not already been played */
833             if( p_playlist->pp_items[i_new]->i_nb_played == 0 )
834                 break;
835         }
836         if( i_count == p_playlist->i_size )
837         {
838             /* The whole playlist has been played: reset the counters */
839             while( i_count > 0 )
840             {
841                 p_playlist->pp_items[--i_count]->i_nb_played = 0;
842             }
843             if( !b_loop )
844             {
845                 return NULL;
846             }
847         }
848         p_playlist->request.i_skip = 0;
849         p_playlist->request.b_request = VLC_FALSE;
850         return p_playlist->pp_items[i_new];
851     }
852
853     /* Start the real work */
854     if( p_playlist->request.b_request )
855     {
856 #ifdef PLAYLIST_DEBUG
857         msg_Dbg( p_playlist,"processing request" );
858 #endif
859         /* We are not playing from a view */
860         if(  p_playlist->request.i_view == -1  )
861         {
862 #ifdef PLAYLIST_DEBUG
863             msg_Dbg( p_playlist, "non-view mode request");
864 #endif
865             /* Directly select the item, just like now */
866             i_skip = p_playlist->request.i_skip;
867             i_goto = p_playlist->request.i_goto;
868
869             if( p_playlist->i_index == -1 ) p_playlist->i_index = 0;
870             p_new = p_playlist->pp_items[p_playlist->i_index];
871
872             if( i_goto >= 0  && i_goto < p_playlist->i_size )
873             {
874                 p_playlist->i_index = i_goto;
875                 p_new = p_playlist->pp_items[p_playlist->i_index];
876                 p_playlist->request.i_goto = -1;
877             }
878
879             if( i_skip != 0 )
880             {
881                 if( p_playlist->i_index + i_skip < p_playlist->i_size &&
882                     p_playlist->i_index + i_skip >=  0 )
883                 {
884                     p_playlist->i_index += i_skip;
885                     p_new = p_playlist->pp_items[p_playlist->i_index];
886                 }
887                 p_playlist->request.i_skip = 0;
888             }
889         }
890         else
891         {
892 #ifdef PLAYLIST_DEBUG
893             msg_Dbg( p_playlist, "view mode request" );
894 #endif
895             p_new = p_playlist->request.p_item;
896             i_skip = p_playlist->request.i_skip;
897
898             /* If we are asked for a node, take its first item */
899             if( p_playlist->request.p_item == NULL && i_skip == 0 )
900             {
901                 i_skip++;
902             }
903
904             p_view = playlist_ViewFind( p_playlist,p_playlist->request.i_view );
905             p_playlist->status.p_node = p_playlist->request.p_node;
906             p_playlist->status.i_view = p_playlist->request.i_view;
907             if( !p_view )
908             {
909                 msg_Err( p_playlist, "p_view is NULL and should not! (requested view is %i", p_playlist->request.i_view );
910             }
911             else if( i_skip > 0 )
912             {
913                 for( i = i_skip; i > 0 ; i-- )
914                 {
915                     p_new = playlist_FindNextFromParent( p_playlist,
916                                     p_playlist->request.i_view,
917                                     p_view->p_root,
918                                     p_playlist->request.p_node,
919                                     p_new );
920                     if( p_new == NULL )
921                     {
922                         if( b_loop )
923                         {
924 #ifdef PLAYLIST_DEBUG
925                             msg_Dbg( p_playlist, "looping" );
926 #endif
927                             p_new = playlist_FindNextFromParent( p_playlist,
928                                       p_playlist->request.i_view,
929                                       p_view->p_root,
930                                       p_playlist->request.p_node,
931                                       NULL );
932                             if( p_new == NULL ) break;
933                         }
934                         else
935                         {
936                             break;
937                         }
938                     }
939                 }
940             }
941             else if( i_skip < 0 )
942             {
943                 for( i = i_skip; i < 0 ; i++ )
944                 {
945                     p_new = playlist_FindPrevFromParent( p_playlist,
946                                     p_playlist->request.i_view,
947                                     p_view->p_root,
948                                     p_playlist->request.p_node,
949                                     p_new );
950                     if( p_new == NULL ) break;
951                 }
952
953             }
954         }
955         /* Clear the request */
956         p_playlist->request.b_request = VLC_FALSE;
957     }
958     /* "Automatic" item change ( next ) */
959     else
960     {
961         p_playlist->request_date = 0;
962
963         if( p_playlist->status.i_view == -1 )
964         {
965 #ifdef PLAYLIST_DEBUG
966         msg_Dbg( p_playlist,"no request - old mode" );
967 #endif
968             if( p_playlist->i_index + 1 < p_playlist->i_size )
969             {
970                 p_playlist->i_index++;
971                 p_new = p_playlist->pp_items[p_playlist->i_index];
972                 if( !( p_new->i_flags & PLAYLIST_SKIP_FLAG ) )
973                 {
974                     return NULL;
975                 }
976             }
977             else
978             {
979                 if( b_loop && p_playlist->i_size > 0)
980                 {
981                     p_playlist->i_index = 0;
982                     p_new = p_playlist->pp_items[0];
983                 }
984                 else
985                     p_new = NULL;
986             }
987         }
988         /* We are playing with a view */
989         else
990         {
991 #ifdef PLAYLIST_DEBUG
992             msg_Dbg( p_playlist,"no request - from a view" );
993 #endif
994             playlist_view_t *p_view =
995                     playlist_ViewFind( p_playlist,
996                                    p_playlist->status.i_view );
997             if( !p_view )
998             {
999                 msg_Err( p_playlist, "p_view is NULL and should not! (FIXME)" );
1000             }
1001             else
1002             {
1003                 p_new = playlist_FindNextFromParent( p_playlist,
1004                             p_playlist->status.i_view,
1005                             p_view->p_root,
1006                             p_playlist->status.p_node,
1007                             p_playlist->status.p_item );
1008                 if( p_new == NULL && b_loop )
1009                 {
1010 #ifdef PLAYLIST_DEBUG
1011                     msg_Dbg( p_playlist, "looping" );
1012 #endif
1013                     p_new = playlist_FindNextFromParent( p_playlist,
1014                                    p_playlist->status.i_view,
1015                                    p_view->p_root,
1016                                    p_playlist->status.p_node,
1017                                    NULL );
1018                 }
1019                 if( p_new != NULL && !(p_new->i_flags & PLAYLIST_SKIP_FLAG) )
1020                     return NULL;
1021             }
1022         }
1023     }
1024
1025     /* Reset index */
1026     if( p_playlist->i_index >= 0 && p_new != NULL &&
1027             p_playlist->pp_items[p_playlist->i_index] != p_new )
1028     {
1029         p_playlist->i_index = playlist_GetPositionById( p_playlist,
1030                                                         p_new->input.i_id );
1031     }
1032
1033 #ifdef PLAYLIST_PROFILE
1034     msg_Dbg(p_playlist,"next item found in "I64Fi " us", mdate()-start );
1035 #endif
1036
1037     if( p_new == NULL )
1038     {
1039         msg_Info( p_playlist, "nothing to play" );
1040     }
1041     return p_new;
1042 }
1043
1044 /*****************************************************************************
1045  * PlayItem: start the input thread for an item
1046  ****************************************************************************/
1047 static int PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
1048 {
1049     vlc_value_t val;
1050
1051     msg_Dbg( p_playlist, "creating new input thread" );
1052
1053     p_item->i_nb_played++;
1054     p_playlist->status.p_item = p_item;
1055
1056     p_playlist->i_index = playlist_GetPositionById( p_playlist,
1057                                                     p_item->input.i_id );
1058
1059 #ifdef PLAYLIST_PROFILE
1060     if( p_playlist->request_date != 0 )
1061     {
1062         msg_Dbg( p_playlist, "request processed after "I64Fi " us",
1063                   mdate() - p_playlist->request_date );
1064     }
1065 #endif
1066
1067     p_playlist->p_input = input_CreateThread( p_playlist, &p_item->input );
1068
1069     var_AddCallback( p_playlist->p_input, "item-change",
1070                          ItemChange, p_playlist );
1071
1072     val.i_int = p_item->input.i_id;
1073     /* unlock the playlist to set the var...mmm */
1074     vlc_mutex_unlock( &p_playlist->object_lock);
1075     var_Set( p_playlist, "playlist-current", val);
1076     vlc_mutex_lock( &p_playlist->object_lock);
1077
1078     return VLC_SUCCESS;
1079
1080 }
1081
1082 /* Forward item change from input */
1083 static int ItemChange( vlc_object_t *p_obj, const char *psz_var,
1084                        vlc_value_t oldval, vlc_value_t newval, void *param )
1085 {
1086     playlist_t *p_playlist = (playlist_t *)param;
1087
1088     //p_playlist->b_need_update = VLC_TRUE;
1089     var_SetInteger( p_playlist, "item-change", newval.i_int );
1090
1091     /* Update view */
1092     /* FIXME: Make that automatic */
1093 //    playlist_ViewUpdate( p_playlist, VIEW_S_AUTHOR );
1094
1095     return VLC_SUCCESS;
1096 }