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