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