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