1 /*****************************************************************************
2  * thread.c : Playlist management functions
3  *****************************************************************************
4  * Copyright © 1999-2008 VLC authors and VideoLAN
5  * $Id: b78f31a7eacde94f150b245a80b029bacafc6a05 $
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 <vlc_renderer_discovery.h>
37 #include "playlist_internal.h"
38 
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static void *Thread   ( void * );
43 
44 /*****************************************************************************
45  * Main functions for the global thread
46  *****************************************************************************/
47 
48 /**
49  * Creates the main playlist thread.
50  */
playlist_Activate(playlist_t * p_playlist)51 void playlist_Activate( playlist_t *p_playlist )
52 {
53     playlist_private_t *p_sys = pl_priv(p_playlist);
54 
55     if( vlc_clone( &p_sys->thread, Thread, p_playlist,
56                    VLC_THREAD_PRIORITY_LOW ) )
57     {
58         msg_Err( p_playlist, "cannot spawn playlist thread" );
59         abort();
60     }
61 }
62 
63 /**
64  * Stops the playlist forever (but do not destroy it yet).
65  * Any input is stopped.
66  * \return Nothing but waits for the playlist to be deactivated.
67  */
playlist_Deactivate(playlist_t * p_playlist)68 void playlist_Deactivate( playlist_t *p_playlist )
69 {
70     playlist_private_t *p_sys = pl_priv(p_playlist);
71 
72     PL_LOCK;
73     /* WARNING: There is a latent bug. It is assumed that only one thread will
74      * be waiting for playlist deactivation at a time. So far, that works
75      * as playlist_Deactivate() is only ever called while closing an
76      * interface and interfaces are shut down serially by intf_DestroyAll(). */
77     if( p_sys->killed )
78     {
79         PL_UNLOCK;
80         return;
81     }
82 
83     msg_Dbg( p_playlist, "deactivating the playlist" );
84     p_sys->killed = true;
85     vlc_cond_signal( &p_sys->signal );
86     PL_UNLOCK;
87 
88     vlc_join( p_sys->thread, NULL );
89 }
90 
91 /* */
92 
93 /* Input Callback */
InputEvent(vlc_object_t * p_this,char const * psz_cmd,vlc_value_t oldval,vlc_value_t newval,void * p_data)94 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
95                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
96 {
97     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
98     playlist_t *p_playlist = p_data;
99 
100     if( newval.i_int == INPUT_EVENT_DEAD )
101     {
102         playlist_private_t *sys = pl_priv(p_playlist);
103 
104         PL_LOCK;
105         sys->request.input_dead = true;
106         vlc_cond_signal( &sys->signal );
107         PL_UNLOCK;
108     }
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  */
ResyncCurrentIndex(playlist_t * p_playlist,playlist_item_t * p_cur)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  */
ResetCurrentlyPlaying(playlist_t * p_playlist,playlist_item_t * p_cur)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  */
PlayItem(playlist_t * p_playlist,playlist_item_t * p_item)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     vlc_renderer_item_t *p_renderer;
199 
200     PL_ASSERT_LOCKED;
201 
202     msg_Dbg( p_playlist, "creating new input thread" );
203 
204     p_item->i_nb_played++;
205     set_current_status_item( p_playlist, p_item );
206     p_renderer = p_sys->p_renderer;
207     /* Retain the renderer now to avoid it to be released by
208      * playlist_SetRenderer when we exit the locked scope. If the last reference
209      * was to be released, we would use a dangling pointer */
210     if( p_renderer )
211         vlc_renderer_item_hold( p_renderer );
212     assert( p_sys->p_input == NULL );
213     PL_UNLOCK;
214 
215     libvlc_MetadataCancel( p_playlist->obj.libvlc, p_item );
216 
217     input_thread_t *p_input_thread = input_Create( p_playlist, p_input, NULL,
218                                                    p_sys->p_input_resource,
219                                                    p_renderer );
220     if( p_renderer )
221         vlc_renderer_item_release( p_renderer );
222     if( likely(p_input_thread != NULL) )
223     {
224         var_AddCallback( p_input_thread, "intf-event",
225                          InputEvent, p_playlist );
226 
227         if( input_Start( p_input_thread ) )
228         {
229             var_DelCallback( p_input_thread, "intf-event",
230                              InputEvent, p_playlist );
231             vlc_object_release( p_input_thread );
232             p_input_thread = NULL;
233         }
234     }
235 
236     /* TODO store art policy in playlist private data */
237     char *psz_arturl = input_item_GetArtURL( p_input );
238     /* p_input->p_meta should not be null after a successful CreateThread */
239     bool b_has_art = !EMPTY_STR( psz_arturl );
240 
241     if( !b_has_art || strncmp( psz_arturl, "attachment://", 13 ) )
242     {
243         PL_DEBUG( "requesting art for new input thread" );
244         libvlc_ArtRequest( p_playlist->obj.libvlc, p_input, META_REQUEST_OPTION_NONE );
245     }
246     free( psz_arturl );
247 
248     PL_LOCK;
249     p_sys->p_input = p_input_thread;
250     PL_UNLOCK;
251 
252     var_SetAddress( p_playlist, "input-current", p_input_thread );
253 
254     PL_LOCK;
255     return p_input_thread != NULL;
256 }
257 
258 /**
259  * Compute the next playlist item depending on
260  * the playlist course mode (forward, backward, random, view,...).
261  *
262  * \param p_playlist the playlist object
263  * \return nothing
264  */
NextItem(playlist_t * p_playlist)265 static playlist_item_t *NextItem( playlist_t *p_playlist )
266 {
267     playlist_private_t *p_sys = pl_priv(p_playlist);
268     playlist_item_t *p_new = NULL;
269     bool requested = p_sys->request.b_request;
270 
271     /* Clear the request */
272     p_sys->request.b_request = false;
273 
274     /* Handle quickly a few special cases */
275     /* No items to play */
276     if( p_playlist->items.i_size == 0 )
277     {
278         msg_Info( p_playlist, "playlist is empty" );
279         return NULL;
280     }
281 
282     /* Start the real work */
283     if( requested )
284     {
285         p_new = p_sys->request.p_item;
286 
287         if( p_new == NULL && p_sys->request.p_node == NULL )
288             return NULL; /* Stop request! */
289 
290         int i_skip = p_sys->request.i_skip;
291         PL_DEBUG( "processing request item: %s, node: %s, skip: %i",
292                         PLI_NAME( p_sys->request.p_item ),
293                         PLI_NAME( p_sys->request.p_node ), i_skip );
294 
295         if( p_sys->request.p_node &&
296             p_sys->request.p_node != get_current_status_node( p_playlist ) )
297         {
298 
299             set_current_status_node( p_playlist, p_sys->request.p_node );
300             p_sys->request.p_node = NULL;
301             p_sys->b_reset_currently_playing = true;
302         }
303 
304         /* If we are asked for a node, go to it's first child */
305         if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
306         {
307             i_skip++;
308             if( p_new != NULL )
309             {
310                 p_new = playlist_GetNextLeaf( p_playlist, p_new, NULL, true, false );
311                 for( int i = 0; i < p_playlist->current.i_size; i++ )
312                 {
313                     if( p_new == ARRAY_VAL( p_playlist->current, i ) )
314                     {
315                         p_playlist->i_current_index = i;
316                         i_skip = 0;
317                     }
318                 }
319             }
320         }
321 
322         if( p_sys->b_reset_currently_playing )
323             /* A bit too bad to reset twice ... */
324             ResetCurrentlyPlaying( p_playlist, p_new );
325         else if( p_new )
326             ResyncCurrentIndex( p_playlist, p_new );
327         else
328             p_playlist->i_current_index = -1;
329 
330         if( p_playlist->current.i_size && (i_skip > 0) )
331         {
332             if( p_playlist->i_current_index < -1 )
333                 p_playlist->i_current_index = -1;
334             for( int i = i_skip; i > 0 ; i-- )
335             {
336                 p_playlist->i_current_index++;
337                 if( p_playlist->i_current_index >= p_playlist->current.i_size )
338                 {
339                     PL_DEBUG( "looping - restarting at beginning of node" );
340                     /* reshuffle playlist when end is reached */
341                     if( var_GetBool( p_playlist, "random" ) ) {
342                         PL_DEBUG( "reshuffle playlist" );
343                         ResetCurrentlyPlaying( p_playlist,
344                                 get_current_status_item( p_playlist ) );
345                     }
346                     p_playlist->i_current_index = 0;
347                 }
348             }
349             p_new = ARRAY_VAL( p_playlist->current,
350                                p_playlist->i_current_index );
351         }
352         else if( p_playlist->current.i_size && (i_skip < 0) )
353         {
354             for( int i = i_skip; i < 0 ; i++ )
355             {
356                 p_playlist->i_current_index--;
357                 if( p_playlist->i_current_index <= -1 )
358                 {
359                     PL_DEBUG( "looping - restarting at end of node" );
360                     /* reshuffle playlist when beginning is reached */
361                     if( var_GetBool( p_playlist, "random" ) ) {
362                         PL_DEBUG( "reshuffle playlist" );
363                         ResetCurrentlyPlaying( p_playlist,
364                                 get_current_status_item( p_playlist ) );
365                     }
366                     p_playlist->i_current_index = p_playlist->current.i_size-1;
367                 }
368             }
369             p_new = ARRAY_VAL( p_playlist->current,
370                                p_playlist->i_current_index );
371         }
372     }
373     /* "Automatic" item change ( next ) */
374     else
375     {
376         bool b_loop = var_GetBool( p_playlist, "loop" );
377         bool b_repeat = var_GetBool( p_playlist, "repeat" );
378         bool b_playstop = var_InheritBool( p_playlist, "play-and-stop" );
379 
380         /* Repeat and play/stop */
381         if( b_repeat && get_current_status_item( p_playlist ) )
382         {
383             msg_Dbg( p_playlist,"repeating item" );
384             return get_current_status_item( p_playlist );
385         }
386         if( b_playstop && get_current_status_item( p_playlist ) )
387         {
388             msg_Dbg( p_playlist,"stopping (play and stop)" );
389             return NULL;
390         }
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         /* Can't go to next from current item */
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     }
421     return p_new;
422 }
423 
LoopInput(playlist_t * p_playlist)424 static bool LoopInput( playlist_t *p_playlist )
425 {
426     playlist_private_t *p_sys = pl_priv(p_playlist);
427     input_thread_t *p_input = p_sys->p_input;
428 
429     assert( p_input != NULL );
430 
431     /* Wait for input to end or be stopped */
432     while( !p_sys->request.input_dead )
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 );
438         }
439         vlc_cond_wait( &p_sys->signal, &p_sys->lock );
440     }
441 
442     input_item_t *item = input_GetItem(p_input);
443     assert(item);
444     bool ok = !input_item_HasErrorWhenReading(item);
445 
446     /* This input is dead. Remove it ! */
447     PL_DEBUG( "dead input" );
448     p_sys->p_input = NULL;
449     p_sys->request.input_dead = false;
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     PL_LOCK;
462 
463     return ok;
464 }
465 
Next(playlist_t * p_playlist)466 static bool Next( playlist_t *p_playlist )
467 {
468     playlist_item_t *p_item = NextItem( p_playlist );
469     if( p_item == NULL )
470         return false;
471 
472     msg_Dbg( p_playlist, "starting playback of new item" );
473     ResyncCurrentIndex( p_playlist, p_item );
474     return PlayItem( p_playlist, p_item );
475 }
476 
477 /**
478  * Run the main control thread itself
479  */
Thread(void * data)480 static void *Thread ( void *data )
481 {
482     playlist_t *p_playlist = data;
483     playlist_private_t *p_sys = pl_priv(p_playlist);
484     bool played = false;
485 
486     PL_LOCK;
487     while( !p_sys->killed )
488     {
489         /* Playlist in stopped state */
490         assert(p_sys->p_input == NULL);
491 
492         if( !p_sys->request.b_request )
493         {
494             vlc_cond_wait( &p_sys->signal, &p_sys->lock );
495             continue;
496         }
497 
498         /* Playlist in running state */
499         while( !p_sys->killed && Next( p_playlist ) )
500         {
501             bool ok = LoopInput( p_playlist );
502             if (ok)
503                 p_sys->i_consecutive_errors = 0;
504             else
505             {
506                 if (p_sys->i_consecutive_errors < 6)
507                     p_sys->i_consecutive_errors++;
508 
509                 int slowdown = 1 << (p_sys->i_consecutive_errors - 1);
510                 /* 100ms, 200ms, 400ms, 800ms, 1.6s, 3.2s */
511                 mtime_t deadline = mdate() + slowdown * 100000L; /* usecs */
512                 vlc_cond_timedwait(&p_sys->signal, &p_sys->lock, deadline);
513             }
514             played = true;
515         }
516 
517         /* Playlist stopping */
518         msg_Dbg( p_playlist, "nothing to play" );
519         if( played && var_InheritBool( p_playlist, "play-and-exit" ) )
520         {
521             msg_Info( p_playlist, "end of playlist, exiting" );
522             libvlc_Quit( p_playlist->obj.libvlc );
523         }
524 
525         /* Destroy any video display now (XXX: ugly hack) */
526         if( input_resource_HasVout( p_sys->p_input_resource ) )
527         {
528             PL_UNLOCK; /* Mind: NO LOCKS while manipulating input resources! */
529             input_resource_TerminateVout( p_sys->p_input_resource );
530             PL_LOCK;
531         }
532     }
533     PL_UNLOCK;
534 
535     input_resource_Terminate( p_sys->p_input_resource );
536     return NULL;
537 }
538