playlist: useless #include
[vlc.git] / src / playlist / thread.c
1 /*****************************************************************************
2  * thread.c : Playlist management functions
3  *****************************************************************************
4  * Copyright © 1999-2008 VLC authors and 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 it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <assert.h>
29
30 #include <vlc_common.h>
31 #include <vlc_es.h>
32 #include <vlc_input.h>
33 #include <vlc_interface.h>
34 #include <vlc_playlist.h>
35 #include <vlc_rand.h>
36 #include "playlist_internal.h"
37
38 /*****************************************************************************
39  * Local prototypes
40  *****************************************************************************/
41 static void *Thread   ( void * );
42
43 /*****************************************************************************
44  * Main functions for the global thread
45  *****************************************************************************/
46
47 /**
48  * Creates the main playlist thread.
49  */
50 void playlist_Activate( playlist_t *p_playlist )
51 {
52     playlist_private_t *p_sys = pl_priv(p_playlist);
53
54     if( vlc_clone( &p_sys->thread, Thread, p_playlist,
55                    VLC_THREAD_PRIORITY_LOW ) )
56     {
57         msg_Err( p_playlist, "cannot spawn playlist thread" );
58         abort();
59     }
60 }
61
62 /**
63  * Stops the playlist forever (but do not destroy it yet).
64  * Any input is stopped.
65  * \return Nothing but waits for the playlist to be deactivated.
66  */
67 void playlist_Deactivate( playlist_t *p_playlist )
68 {
69     playlist_private_t *p_sys = pl_priv(p_playlist);
70
71     PL_LOCK;
72     /* WARNING: There is a latent bug. It is assumed that only one thread will
73      * be waiting for playlist deactivation at a time. So far, that works
74      * as playlist_Deactivate() is only ever called while closing an
75      * interface and interfaces are shut down serially by intf_DestroyAll(). */
76     if( p_sys->killed )
77     {
78         PL_UNLOCK;
79         return;
80     }
81
82     msg_Dbg( p_playlist, "deactivating the playlist" );
83     p_sys->killed = true;
84     vlc_cond_signal( &p_sys->signal );
85     PL_UNLOCK;
86
87     vlc_join( p_sys->thread, NULL );
88 }
89
90 /* */
91
92 /* Input Callback */
93 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
94                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
95 {
96     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
97     playlist_t *p_playlist = p_data;
98
99     if( newval.i_int != INPUT_EVENT_STATE &&
100         newval.i_int != INPUT_EVENT_DEAD )
101         return VLC_SUCCESS;
102
103     PL_LOCK;
104
105     /* XXX: signaling while not changing any parameter... suspicious... */
106     vlc_cond_signal( &pl_priv(p_playlist)->signal );
107
108     PL_UNLOCK;
109     return VLC_SUCCESS;
110 }
111
112 /**
113  * Synchronise the current index of the playlist
114  * to match the index of the current item.
115  *
116  * \param p_playlist the playlist structure
117  * \param p_cur the current playlist item
118  * \return nothing
119  */
120 void ResyncCurrentIndex( playlist_t *p_playlist, playlist_item_t *p_cur )
121 {
122     PL_ASSERT_LOCKED;
123
124     PL_DEBUG( "resyncing on %s", PLI_NAME( p_cur ) );
125     /* Simply resync index */
126     int i;
127     p_playlist->i_current_index = -1;
128     for( i = 0 ; i< p_playlist->current.i_size; i++ )
129     {
130         if( ARRAY_VAL( p_playlist->current, i ) == p_cur )
131         {
132             p_playlist->i_current_index = i;
133             break;
134         }
135     }
136     PL_DEBUG( "%s is at %i", PLI_NAME( p_cur ), p_playlist->i_current_index );
137 }
138
139 /**
140  * Reset the currently playing playlist.
141  *
142  * \param p_playlist the playlist structure
143  * \param p_cur the current playlist item
144  * \return nothing
145  */
146 void ResetCurrentlyPlaying( playlist_t *p_playlist,
147                                    playlist_item_t *p_cur )
148 {
149     playlist_private_t *p_sys = pl_priv(p_playlist);
150
151     PL_DEBUG( "rebuilding array of current - root %s",
152               PLI_NAME( p_sys->status.p_node ) );
153     ARRAY_RESET( p_playlist->current );
154     p_playlist->i_current_index = -1;
155     for( playlist_item_t *p_next = NULL; ; )
156     {
157         /** FIXME: this is *slow* */
158         p_next = playlist_GetNextLeaf( p_playlist,
159                                        p_sys->status.p_node,
160                                        p_next, true, false );
161         if( !p_next )
162             break;
163
164         if( p_next == p_cur )
165             p_playlist->i_current_index = p_playlist->current.i_size;
166         ARRAY_APPEND( p_playlist->current, p_next);
167     }
168     PL_DEBUG("rebuild done - %i items, index %i", p_playlist->current.i_size,
169                                                   p_playlist->i_current_index);
170
171     if( var_GetBool( p_playlist, "random" ) && ( p_playlist->current.i_size > 0 ) )
172     {
173         /* Shuffle the array */
174         for( unsigned j = p_playlist->current.i_size - 1; j > 0; j-- )
175         {
176             unsigned i = vlc_lrand48() % (j+1); /* between 0 and j */
177             playlist_item_t *p_tmp;
178             /* swap the two items */
179             p_tmp = ARRAY_VAL(p_playlist->current, i);
180             ARRAY_VAL(p_playlist->current,i) = ARRAY_VAL(p_playlist->current,j);
181             ARRAY_VAL(p_playlist->current,j) = p_tmp;
182         }
183     }
184     p_sys->b_reset_currently_playing = false;
185 }
186
187
188 /**
189  * Start the input for an item
190  *
191  * \param p_playlist the playlist object
192  * \param p_item the item to play
193  * \return nothing
194  */
195 static int PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
196 {
197     playlist_private_t *p_sys = pl_priv(p_playlist);
198     input_item_t *p_input = p_item->p_input;
199
200     PL_ASSERT_LOCKED;
201
202     msg_Dbg( p_playlist, "creating new input thread" );
203
204     p_input->i_nb_played++;
205     set_current_status_item( p_playlist, p_item );
206
207     p_sys->status.i_status = PLAYLIST_RUNNING;
208
209     assert( p_sys->p_input == NULL );
210
211     input_thread_t *p_input_thread = input_Create( p_playlist, p_input, NULL, p_sys->p_input_resource );
212     if( p_input_thread )
213     {
214         p_sys->p_input = p_input_thread;
215         var_AddCallback( p_input_thread, "intf-event", InputEvent, p_playlist );
216
217         var_SetAddress( p_playlist, "input-current", p_input_thread );
218
219         if( input_Start( p_sys->p_input ) )
220         {
221             vlc_object_release( p_input_thread );
222             p_sys->p_input = p_input_thread = NULL;
223         }
224     }
225
226     /* TODO store art policy in playlist private data */
227     char *psz_arturl = input_item_GetArtURL( p_input );
228     char *psz_name = input_item_GetName( p_input );
229     /* p_input->p_meta should not be null after a successful CreateThread */
230     bool b_has_art = !EMPTY_STR( psz_arturl );
231
232     if( !b_has_art || strncmp( psz_arturl, "attachment://", 13 ) )
233     {
234         PL_DEBUG( "requesting art for %s", psz_name );
235         libvlc_ArtRequest( p_playlist->p_libvlc, p_input, META_REQUEST_OPTION_NONE );
236     }
237     free( psz_arturl );
238     free( psz_name );
239
240     PL_UNLOCK;
241     var_TriggerCallback( p_playlist, "activity" );
242     PL_LOCK;
243
244     return VLC_SUCCESS;
245 }
246
247 /**
248  * Compute the next playlist item depending on
249  * the playlist course mode (forward, backward, random, view,...).
250  *
251  * \param p_playlist the playlist object
252  * \return nothing
253  */
254 static playlist_item_t *NextItem( playlist_t *p_playlist )
255 {
256     playlist_private_t *p_sys = pl_priv(p_playlist);
257     playlist_item_t *p_new = NULL;
258
259     /* Handle quickly a few special cases */
260     /* No items to play */
261     if( p_playlist->items.i_size == 0 )
262     {
263         msg_Info( p_playlist, "playlist is empty" );
264         return NULL;
265     }
266
267     /* Start the real work */
268     if( p_sys->request.b_request )
269     {
270         p_new = p_sys->request.p_item;
271         int i_skip = p_sys->request.i_skip;
272         PL_DEBUG( "processing request item: %s, node: %s, skip: %i",
273                         PLI_NAME( p_sys->request.p_item ),
274                         PLI_NAME( p_sys->request.p_node ), i_skip );
275
276         if( p_sys->request.p_node &&
277             p_sys->request.p_node != get_current_status_node( p_playlist ) )
278         {
279
280             set_current_status_node( p_playlist, p_sys->request.p_node );
281             p_sys->request.p_node = NULL;
282             p_sys->b_reset_currently_playing = true;
283         }
284
285         /* If we are asked for a node, go to it's first child */
286         if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
287         {
288             i_skip++;
289             if( p_new != NULL )
290             {
291                 p_new = playlist_GetNextLeaf( p_playlist, p_new, NULL, true, false );
292                 for( int i = 0; i < p_playlist->current.i_size; i++ )
293                 {
294                     if( p_new == ARRAY_VAL( p_playlist->current, i ) )
295                     {
296                         p_playlist->i_current_index = i;
297                         i_skip = 0;
298                     }
299                 }
300             }
301         }
302
303         if( p_sys->b_reset_currently_playing )
304             /* A bit too bad to reset twice ... */
305             ResetCurrentlyPlaying( p_playlist, p_new );
306         else if( p_new )
307             ResyncCurrentIndex( p_playlist, p_new );
308         else
309             p_playlist->i_current_index = -1;
310
311         if( p_playlist->current.i_size && (i_skip > 0) )
312         {
313             if( p_playlist->i_current_index < -1 )
314                 p_playlist->i_current_index = -1;
315             for( int i = i_skip; i > 0 ; i-- )
316             {
317                 p_playlist->i_current_index++;
318                 if( p_playlist->i_current_index >= p_playlist->current.i_size )
319                 {
320                     PL_DEBUG( "looping - restarting at beginning of node" );
321                     /* reshuffle playlist when end is reached */
322                     if( var_GetBool( p_playlist, "random" ) ) {
323                         PL_DEBUG( "reshuffle playlist" );
324                         ResetCurrentlyPlaying( p_playlist,
325                                 get_current_status_item( p_playlist ) );
326                     }
327                     p_playlist->i_current_index = 0;
328                 }
329             }
330             p_new = ARRAY_VAL( p_playlist->current,
331                                p_playlist->i_current_index );
332         }
333         else if( p_playlist->current.i_size && (i_skip < 0) )
334         {
335             for( int i = i_skip; i < 0 ; i++ )
336             {
337                 p_playlist->i_current_index--;
338                 if( p_playlist->i_current_index <= -1 )
339                 {
340                     PL_DEBUG( "looping - restarting at end of node" );
341                     /* reshuffle playlist when beginning is reached */
342                     if( var_GetBool( p_playlist, "random" ) ) {
343                         PL_DEBUG( "reshuffle playlist" );
344                         ResetCurrentlyPlaying( p_playlist,
345                                 get_current_status_item( p_playlist ) );
346                     }
347                     p_playlist->i_current_index = p_playlist->current.i_size-1;
348                 }
349             }
350             p_new = ARRAY_VAL( p_playlist->current,
351                                p_playlist->i_current_index );
352         }
353         /* Clear the request */
354         p_sys->request.b_request = false;
355     }
356     /* "Automatic" item change ( next ) */
357     else
358     {
359         bool b_loop = var_GetBool( p_playlist, "loop" );
360         bool b_repeat = var_GetBool( p_playlist, "repeat" );
361         bool b_playstop = var_InheritBool( p_playlist, "play-and-stop" );
362
363         /* Repeat and play/stop */
364         if( b_repeat && get_current_status_item( p_playlist ) )
365         {
366             msg_Dbg( p_playlist,"repeating item" );
367             return get_current_status_item( p_playlist );
368         }
369         if( b_playstop )
370         {
371             msg_Dbg( p_playlist,"stopping (play and stop)" );
372             return NULL;
373         }
374
375         /* */
376         if( get_current_status_item( p_playlist ) )
377         {
378             playlist_item_t *p_parent = get_current_status_item( p_playlist );
379             while( p_parent )
380             {
381                 if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
382                 {
383                     msg_Dbg( p_playlist, "blocking item, stopping") ;
384                     return NULL;
385                 }
386                 p_parent = p_parent->p_parent;
387             }
388         }
389
390         PL_DEBUG( "changing item without a request (current %i/%i)",
391                   p_playlist->i_current_index, p_playlist->current.i_size );
392         /* Cant go to next from current item */
393         if( get_current_status_item( p_playlist ) &&
394             get_current_status_item( p_playlist )->i_flags & PLAYLIST_SKIP_FLAG )
395             return NULL;
396
397         if( p_sys->b_reset_currently_playing )
398             ResetCurrentlyPlaying( p_playlist,
399                                    get_current_status_item( p_playlist ) );
400
401         p_playlist->i_current_index++;
402         assert( p_playlist->i_current_index <= p_playlist->current.i_size );
403         if( p_playlist->i_current_index == p_playlist->current.i_size )
404         {
405             if( !b_loop || p_playlist->current.i_size == 0 )
406                 return NULL;
407             /* reshuffle after last item has been played */
408             if( var_GetBool( p_playlist, "random" ) ) {
409                 PL_DEBUG( "reshuffle playlist" );
410                 ResetCurrentlyPlaying( p_playlist,
411                                        get_current_status_item( p_playlist ) );
412             }
413             p_playlist->i_current_index = 0;
414         }
415         PL_DEBUG( "using item %i", p_playlist->i_current_index );
416         if ( p_playlist->current.i_size == 0 )
417             return NULL;
418
419         p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
420         /* The new item can't be autoselected  */
421         if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
422             return NULL;
423     }
424     return p_new;
425 }
426
427 static void LoopInput( playlist_t *p_playlist )
428 {
429     playlist_private_t *p_sys = pl_priv(p_playlist);
430     input_thread_t *p_input = p_sys->p_input;
431
432     assert( p_input != NULL );
433
434     if( p_sys->request.b_request || p_sys->killed )
435     {
436         PL_DEBUG( "incoming request - stopping current input" );
437         input_Stop( p_input, true );
438     }
439
440 #warning Unsynchronized access to *p_input flags...
441     /* This input is dead. Remove it ! */
442     if( p_input->b_dead )
443     {
444         p_sys->p_input = NULL;
445         PL_DEBUG( "dead input" );
446         PL_UNLOCK;
447
448         /* WARNING: Input resource manipulation and callback deletion are
449          * incompatible with the playlist lock. */
450         if( !var_InheritBool( p_input, "sout-keep" ) )
451             input_resource_TerminateSout( p_sys->p_input_resource );
452         var_DelCallback( p_input, "intf-event", InputEvent, p_playlist );
453
454         input_Close( p_input );
455         var_TriggerCallback( p_playlist, "activity" );
456         PL_LOCK;
457         return;
458     }
459     /* This input has finished, ask it to die ! */
460     else if( p_input->b_error || p_input->b_eof )
461     {
462         PL_DEBUG( "finished input" );
463         input_Stop( p_input, false );
464     }
465
466     vlc_cond_wait( &p_sys->signal, &p_sys->lock );
467 }
468
469 static void LoopRequest( playlist_t *p_playlist, int i_status )
470 {
471     playlist_private_t *p_sys = pl_priv(p_playlist);
472     assert( !p_sys->p_input );
473
474     /* No input. Several cases
475      *  - No request, running status -> start new item
476      *  - No request, stopped status -> collect garbage
477      *  - Request, running requested -> start new item
478      *  - Request, stopped requested -> collect garbage
479     */
480     if( i_status == PLAYLIST_STOPPED )
481     {
482         p_sys->status.i_status = PLAYLIST_STOPPED;
483         vlc_cond_wait( &p_sys->signal, &p_sys->lock );
484         return;
485     }
486
487     playlist_item_t *p_item = NextItem( p_playlist );
488     if( p_item )
489     {
490         msg_Dbg( p_playlist, "starting playback of the new playlist item" );
491         ResyncCurrentIndex( p_playlist, p_item );
492         PlayItem( p_playlist, p_item );
493         return;
494     }
495
496     msg_Dbg( p_playlist, "nothing to play" );
497     p_sys->status.i_status = PLAYLIST_STOPPED;
498
499     if( var_InheritBool( p_playlist, "play-and-exit" ) )
500     {
501         msg_Info( p_playlist, "end of playlist, exiting" );
502         libvlc_Quit( p_playlist->p_libvlc );
503     }
504 }
505
506 /**
507  * Run the main control thread itself
508  */
509 static void *Thread ( void *data )
510 {
511     playlist_t *p_playlist = data;
512     playlist_private_t *p_sys = pl_priv(p_playlist);
513
514     PL_LOCK;
515     for( ;; )
516     {
517         while( p_sys->p_input != NULL )
518             LoopInput( p_playlist );
519
520         if( p_sys->killed )
521             break; /* THE END */
522
523         const int status = p_sys->request.b_request ?
524                            p_sys->request.i_status : p_sys->status.i_status;
525
526         /* Destroy any video display if the playlist is supposed to stop */
527         if( status == PLAYLIST_STOPPED
528          && input_resource_HasVout( p_sys->p_input_resource ) )
529         {
530             PL_UNLOCK; /* Mind: NO LOCKS while manipulating input resources! */
531             input_resource_TerminateVout( p_sys->p_input_resource );
532             PL_LOCK;
533             continue; /* lost lock = lost state */
534         }
535
536         LoopRequest( p_playlist, status );
537     }
538     p_sys->status.i_status = PLAYLIST_STOPPED;
539     PL_UNLOCK;
540
541     input_resource_Terminate( p_sys->p_input_resource );
542     return NULL;
543 }