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