]> git.sesse.net Git - vlc/blob - src/playlist/thread.c
win32: remove non-standard empty struct
[vlc] / 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  */
194 static bool PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
195 {
196     playlist_private_t *p_sys = pl_priv(p_playlist);
197     input_item_t *p_input = p_item->p_input;
198
199     PL_ASSERT_LOCKED;
200
201     msg_Dbg( p_playlist, "creating new input thread" );
202
203     p_item->i_nb_played++;
204     set_current_status_item( p_playlist, p_item );
205     assert( p_sys->p_input == NULL );
206     PL_UNLOCK;
207
208     input_thread_t *p_input_thread = input_Create( p_playlist, p_input, NULL,
209                                                    p_sys->p_input_resource );
210     if( likely(p_input_thread != NULL) )
211     {
212         var_AddCallback( p_input_thread, "intf-event",
213                          InputEvent, p_playlist );
214
215         if( input_Start( p_input_thread ) )
216         {
217             var_DelCallback( p_input_thread, "intf-event",
218                              InputEvent, p_playlist );
219             vlc_object_release( p_input_thread );
220             p_input_thread = NULL;
221         }
222     }
223
224     var_SetAddress( p_playlist, "input-current", p_input_thread );
225
226     /* TODO store art policy in playlist private data */
227     char *psz_arturl = input_item_GetArtURL( p_input );
228     /* p_input->p_meta should not be null after a successful CreateThread */
229     bool b_has_art = !EMPTY_STR( psz_arturl );
230
231     if( !b_has_art || strncmp( psz_arturl, "attachment://", 13 ) )
232     {
233         PL_DEBUG( "requesting art for new input thread" );
234         libvlc_ArtRequest( p_playlist->p_libvlc, p_input, META_REQUEST_OPTION_NONE );
235     }
236     free( psz_arturl );
237
238     var_TriggerCallback( p_playlist, "activity" );
239
240     PL_LOCK;
241     p_sys->p_input = p_input_thread;
242     return p_input_thread != NULL;
243 }
244
245 /**
246  * Compute the next playlist item depending on
247  * the playlist course mode (forward, backward, random, view,...).
248  *
249  * \param p_playlist the playlist object
250  * \return nothing
251  */
252 static playlist_item_t *NextItem( playlist_t *p_playlist )
253 {
254     playlist_private_t *p_sys = pl_priv(p_playlist);
255     playlist_item_t *p_new = NULL;
256     bool requested = p_sys->request.b_request;
257
258     /* Clear the request */
259     p_sys->request.b_request = false;
260
261     /* Handle quickly a few special cases */
262     /* No items to play */
263     if( p_playlist->items.i_size == 0 )
264     {
265         msg_Info( p_playlist, "playlist is empty" );
266         return NULL;
267     }
268
269     /* Start the real work */
270     if( requested )
271     {
272         p_new = p_sys->request.p_item;
273
274         if( p_new == NULL && p_sys->request.p_node == NULL )
275             return NULL; /* Stop request! */
276
277         int i_skip = p_sys->request.i_skip;
278         PL_DEBUG( "processing request item: %s, node: %s, skip: %i",
279                         PLI_NAME( p_sys->request.p_item ),
280                         PLI_NAME( p_sys->request.p_node ), i_skip );
281
282         if( p_sys->request.p_node &&
283             p_sys->request.p_node != get_current_status_node( p_playlist ) )
284         {
285
286             set_current_status_node( p_playlist, p_sys->request.p_node );
287             p_sys->request.p_node = NULL;
288             p_sys->b_reset_currently_playing = true;
289         }
290
291         /* If we are asked for a node, go to it's first child */
292         if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
293         {
294             i_skip++;
295             if( p_new != NULL )
296             {
297                 p_new = playlist_GetNextLeaf( p_playlist, p_new, NULL, true, false );
298                 for( int i = 0; i < p_playlist->current.i_size; i++ )
299                 {
300                     if( p_new == ARRAY_VAL( p_playlist->current, i ) )
301                     {
302                         p_playlist->i_current_index = i;
303                         i_skip = 0;
304                     }
305                 }
306             }
307         }
308
309         if( p_sys->b_reset_currently_playing )
310             /* A bit too bad to reset twice ... */
311             ResetCurrentlyPlaying( p_playlist, p_new );
312         else if( p_new )
313             ResyncCurrentIndex( p_playlist, p_new );
314         else
315             p_playlist->i_current_index = -1;
316
317         if( p_playlist->current.i_size && (i_skip > 0) )
318         {
319             if( p_playlist->i_current_index < -1 )
320                 p_playlist->i_current_index = -1;
321             for( int i = i_skip; i > 0 ; i-- )
322             {
323                 p_playlist->i_current_index++;
324                 if( p_playlist->i_current_index >= p_playlist->current.i_size )
325                 {
326                     PL_DEBUG( "looping - restarting at beginning of node" );
327                     /* reshuffle playlist when end is reached */
328                     if( var_GetBool( p_playlist, "random" ) ) {
329                         PL_DEBUG( "reshuffle playlist" );
330                         ResetCurrentlyPlaying( p_playlist,
331                                 get_current_status_item( p_playlist ) );
332                     }
333                     p_playlist->i_current_index = 0;
334                 }
335             }
336             p_new = ARRAY_VAL( p_playlist->current,
337                                p_playlist->i_current_index );
338         }
339         else if( p_playlist->current.i_size && (i_skip < 0) )
340         {
341             for( int i = i_skip; i < 0 ; i++ )
342             {
343                 p_playlist->i_current_index--;
344                 if( p_playlist->i_current_index <= -1 )
345                 {
346                     PL_DEBUG( "looping - restarting at end of node" );
347                     /* reshuffle playlist when beginning is reached */
348                     if( var_GetBool( p_playlist, "random" ) ) {
349                         PL_DEBUG( "reshuffle playlist" );
350                         ResetCurrentlyPlaying( p_playlist,
351                                 get_current_status_item( p_playlist ) );
352                     }
353                     p_playlist->i_current_index = p_playlist->current.i_size-1;
354                 }
355             }
356             p_new = ARRAY_VAL( p_playlist->current,
357                                p_playlist->i_current_index );
358         }
359     }
360     /* "Automatic" item change ( next ) */
361     else
362     {
363         bool b_loop = var_GetBool( p_playlist, "loop" );
364         bool b_repeat = var_GetBool( p_playlist, "repeat" );
365         bool b_playstop = var_InheritBool( p_playlist, "play-and-stop" );
366
367         /* Repeat and play/stop */
368         if( b_repeat && get_current_status_item( p_playlist ) )
369         {
370             msg_Dbg( p_playlist,"repeating item" );
371             return get_current_status_item( p_playlist );
372         }
373         if( b_playstop )
374         {
375             msg_Dbg( p_playlist,"stopping (play and stop)" );
376             return NULL;
377         }
378
379         /* */
380         if( get_current_status_item( p_playlist ) )
381         {
382             playlist_item_t *p_parent = get_current_status_item( p_playlist );
383             while( p_parent )
384             {
385                 if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
386                 {
387                     msg_Dbg( p_playlist, "blocking item, stopping") ;
388                     return NULL;
389                 }
390                 p_parent = p_parent->p_parent;
391             }
392         }
393
394         PL_DEBUG( "changing item without a request (current %i/%i)",
395                   p_playlist->i_current_index, p_playlist->current.i_size );
396         /* Cant go to next from current item */
397         if( get_current_status_item( p_playlist ) &&
398             get_current_status_item( p_playlist )->i_flags & PLAYLIST_SKIP_FLAG )
399             return NULL;
400
401         if( p_sys->b_reset_currently_playing )
402             ResetCurrentlyPlaying( p_playlist,
403                                    get_current_status_item( p_playlist ) );
404
405         p_playlist->i_current_index++;
406         assert( p_playlist->i_current_index <= p_playlist->current.i_size );
407         if( p_playlist->i_current_index == p_playlist->current.i_size )
408         {
409             if( !b_loop || p_playlist->current.i_size == 0 )
410                 return NULL;
411             /* reshuffle after last item has been played */
412             if( var_GetBool( p_playlist, "random" ) ) {
413                 PL_DEBUG( "reshuffle playlist" );
414                 ResetCurrentlyPlaying( p_playlist,
415                                        get_current_status_item( p_playlist ) );
416             }
417             p_playlist->i_current_index = 0;
418         }
419         PL_DEBUG( "using item %i", p_playlist->i_current_index );
420         if ( p_playlist->current.i_size == 0 )
421             return NULL;
422
423         p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
424         /* The new item can't be autoselected  */
425         if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
426             return NULL;
427     }
428     return p_new;
429 }
430
431 static void LoopInput( playlist_t *p_playlist )
432 {
433     playlist_private_t *p_sys = pl_priv(p_playlist);
434     input_thread_t *p_input = p_sys->p_input;
435
436     assert( p_input != NULL );
437
438     if( p_sys->request.b_request || p_sys->killed )
439     {
440         PL_DEBUG( "incoming request - stopping current input" );
441         input_Stop( p_input );
442     }
443
444 #warning Unsynchronized access to *p_input flags...
445     /* This input is dead. Remove it ! */
446     if( p_input->b_dead )
447     {
448         p_sys->p_input = NULL;
449         PL_DEBUG( "dead input" );
450         PL_UNLOCK;
451
452         var_SetAddress( p_playlist, "input-current", NULL );
453
454         /* WARNING: Input resource manipulation and callback deletion are
455          * incompatible with the playlist lock. */
456         if( !var_InheritBool( p_input, "sout-keep" ) )
457             input_resource_TerminateSout( p_sys->p_input_resource );
458         var_DelCallback( p_input, "intf-event", InputEvent, p_playlist );
459
460         input_Close( p_input );
461         var_TriggerCallback( p_playlist, "activity" );
462         PL_LOCK;
463         return;
464     }
465     /* This input has finished, ask it to die ! */
466     else if( p_input->b_error || p_input->b_eof )
467     {
468         PL_DEBUG( "finished input" );
469         input_Stop( p_input );
470     }
471
472     vlc_cond_wait( &p_sys->signal, &p_sys->lock );
473 }
474
475 static bool Next( playlist_t *p_playlist )
476 {
477     playlist_item_t *p_item = NextItem( p_playlist );
478     if( p_item == NULL )
479         return false;
480
481     msg_Dbg( p_playlist, "starting playback of new item" );
482     ResyncCurrentIndex( p_playlist, p_item );
483     return PlayItem( p_playlist, p_item );
484 }
485
486 /**
487  * Run the main control thread itself
488  */
489 static void *Thread ( void *data )
490 {
491     playlist_t *p_playlist = data;
492     playlist_private_t *p_sys = pl_priv(p_playlist);
493
494     PL_LOCK;
495     while( !p_sys->killed )
496     {
497         /* Playlist in stopped state */
498         assert(p_sys->p_input == NULL);
499
500         if( !p_sys->request.b_request )
501         {
502             vlc_cond_wait( &p_sys->signal, &p_sys->lock );
503             continue;
504         }
505
506         while( !p_sys->killed && Next( p_playlist ) )
507         {   /* Playlist in running state */
508             assert(p_sys->p_input != NULL);
509
510             do
511                 LoopInput( p_playlist );
512             while( p_sys->p_input != NULL );
513         }
514
515         msg_Dbg( p_playlist, "nothing to play" );
516         if( var_InheritBool( p_playlist, "play-and-exit" ) )
517         {
518             msg_Info( p_playlist, "end of playlist, exiting" );
519             libvlc_Quit( p_playlist->p_libvlc );
520         }
521
522         /* Destroy any video display now (XXX: ugly hack) */
523         if( input_resource_HasVout( p_sys->p_input_resource ) )
524         {
525             PL_UNLOCK; /* Mind: NO LOCKS while manipulating input resources! */
526             input_resource_TerminateVout( p_sys->p_input_resource );
527             PL_LOCK;
528         }
529     }
530     PL_UNLOCK;
531
532     input_resource_Terminate( p_sys->p_input_resource );
533     return NULL;
534 }