]> git.sesse.net Git - vlc/blob - src/playlist/thread.c
68d11a203442dbb735eb9d15aef2c0ce313e9c82
[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 "stream_output/stream_output.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  * Create the main playlist threads.
50  * Additionally to the playlist, this thread controls :
51  *    - Statistics
52  *    - VLM
53  * \param p_parent
54  * \return an object with a started thread
55  */
56 void playlist_Activate( playlist_t *p_playlist )
57 {
58     /* */
59     playlist_private_t *p_sys = pl_priv(p_playlist);
60
61     p_sys->p_input_resource = input_resource_New( VLC_OBJECT( p_playlist ) );
62     if( unlikely(p_sys->p_input_resource == NULL) )
63         abort();
64
65     /* Start the playlist thread */
66     if( vlc_clone( &p_sys->thread, Thread, p_playlist,
67                    VLC_THREAD_PRIORITY_LOW ) )
68     {
69         msg_Err( p_playlist, "cannot spawn playlist thread" );
70     }
71     msg_Dbg( p_playlist, "playlist threads correctly activated" );
72 }
73
74 void playlist_Deactivate( playlist_t *p_playlist )
75 {
76     /* */
77     playlist_private_t *p_sys = pl_priv(p_playlist);
78
79     msg_Dbg( p_playlist, "deactivating the playlist" );
80
81     PL_LOCK;
82     p_sys->killed = true;
83     vlc_cond_signal( &p_sys->signal );
84     PL_UNLOCK;
85
86     vlc_join( p_sys->thread, NULL );
87     assert( !p_sys->p_input );
88
89     /* release input resources */
90     input_resource_Terminate( p_sys->p_input_resource );
91     input_resource_Release( p_sys->p_input_resource );
92
93     if( var_InheritBool( p_playlist, "media-library" ) )
94         playlist_MLDump( p_playlist );
95
96     PL_LOCK;
97
98     /* Release the current node */
99     set_current_status_node( p_playlist, NULL );
100
101     /* Release the current item */
102     set_current_status_item( p_playlist, NULL );
103
104     PL_UNLOCK;
105
106     msg_Dbg( p_playlist, "playlist correctly deactivated" );
107 }
108
109 /* */
110
111 /* Input Callback */
112 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
113                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
114 {
115     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
116     playlist_t *p_playlist = p_data;
117
118     if( newval.i_int != INPUT_EVENT_STATE &&
119         newval.i_int != INPUT_EVENT_DEAD )
120         return VLC_SUCCESS;
121
122     PL_LOCK;
123
124     /* XXX: signaling while not changing any parameter... suspicious... */
125     vlc_cond_signal( &pl_priv(p_playlist)->signal );
126
127     PL_UNLOCK;
128     return VLC_SUCCESS;
129 }
130
131 static void UpdateActivity( playlist_t *p_playlist, int i_delta )
132 {
133     PL_ASSERT_LOCKED;
134
135     const int i_activity = var_GetInteger( p_playlist, "activity" ) ;
136     var_SetInteger( p_playlist, "activity", i_activity + i_delta );
137 }
138
139 /**
140  * Synchronise the current index of the playlist
141  * to match the index of the current item.
142  *
143  * \param p_playlist the playlist structure
144  * \param p_cur the current playlist item
145  * \return nothing
146  */
147 void ResyncCurrentIndex( playlist_t *p_playlist, playlist_item_t *p_cur )
148 {
149     PL_ASSERT_LOCKED;
150
151     PL_DEBUG( "resyncing on %s", PLI_NAME( p_cur ) );
152     /* Simply resync index */
153     int i;
154     p_playlist->i_current_index = -1;
155     for( i = 0 ; i< p_playlist->current.i_size; i++ )
156     {
157         if( ARRAY_VAL( p_playlist->current, i ) == p_cur )
158         {
159             p_playlist->i_current_index = i;
160             break;
161         }
162     }
163     PL_DEBUG( "%s is at %i", PLI_NAME( p_cur ), p_playlist->i_current_index );
164 }
165
166 /**
167  * Reset the currently playing playlist.
168  *
169  * \param p_playlist the playlist structure
170  * \param p_cur the current playlist item
171  * \return nothing
172  */
173 void ResetCurrentlyPlaying( playlist_t *p_playlist,
174                                    playlist_item_t *p_cur )
175 {
176     playlist_private_t *p_sys = pl_priv(p_playlist);
177
178     PL_DEBUG( "rebuilding array of current - root %s",
179               PLI_NAME( p_sys->status.p_node ) );
180     ARRAY_RESET( p_playlist->current );
181     p_playlist->i_current_index = -1;
182     for( playlist_item_t *p_next = NULL; ; )
183     {
184         /** FIXME: this is *slow* */
185         p_next = playlist_GetNextLeaf( p_playlist,
186                                        p_sys->status.p_node,
187                                        p_next, true, false );
188         if( !p_next )
189             break;
190
191         if( p_next == p_cur )
192             p_playlist->i_current_index = p_playlist->current.i_size;
193         ARRAY_APPEND( p_playlist->current, p_next);
194     }
195     PL_DEBUG("rebuild done - %i items, index %i", p_playlist->current.i_size,
196                                                   p_playlist->i_current_index);
197
198     if( var_GetBool( p_playlist, "random" ) && ( p_playlist->current.i_size > 0 ) )
199     {
200         /* Shuffle the array */
201         for( unsigned j = p_playlist->current.i_size - 1; j > 0; j-- )
202         {
203             unsigned i = vlc_lrand48() % (j+1); /* between 0 and j */
204             playlist_item_t *p_tmp;
205             /* swap the two items */
206             p_tmp = ARRAY_VAL(p_playlist->current, i);
207             ARRAY_VAL(p_playlist->current,i) = ARRAY_VAL(p_playlist->current,j);
208             ARRAY_VAL(p_playlist->current,j) = p_tmp;
209         }
210     }
211     p_sys->b_reset_currently_playing = false;
212 }
213
214
215 /**
216  * Start the input for an item
217  *
218  * \param p_playlist the playlist object
219  * \param p_item the item to play
220  * \return nothing
221  */
222 static int PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
223 {
224     playlist_private_t *p_sys = pl_priv(p_playlist);
225     input_item_t *p_input = p_item->p_input;
226
227     PL_ASSERT_LOCKED;
228
229     msg_Dbg( p_playlist, "creating new input thread" );
230
231     p_input->i_nb_played++;
232     set_current_status_item( p_playlist, p_item );
233
234     p_sys->status.i_status = PLAYLIST_RUNNING;
235
236     UpdateActivity( p_playlist, DEFAULT_INPUT_ACTIVITY );
237
238     assert( p_sys->p_input == NULL );
239
240     input_thread_t *p_input_thread = input_Create( p_playlist, p_input, NULL, p_sys->p_input_resource );
241     if( p_input_thread )
242     {
243         p_sys->p_input = p_input_thread;
244         var_AddCallback( p_input_thread, "intf-event", InputEvent, p_playlist );
245
246         var_SetAddress( p_playlist, "input-current", p_input_thread );
247
248         if( input_Start( p_sys->p_input ) )
249         {
250             vlc_object_release( p_input_thread );
251             p_sys->p_input = p_input_thread = NULL;
252         }
253     }
254
255     char *psz_uri = input_item_GetURI( p_item->p_input );
256     if( psz_uri && ( !strncmp( psz_uri, "directory:", 10 ) ||
257                      !strncmp( psz_uri, "vlc:", 4 ) ) )
258     {
259         free( psz_uri );
260         return VLC_SUCCESS;
261     }
262     free( psz_uri );
263
264     /* TODO store art policy in playlist private data */
265     if( var_GetInteger( p_playlist, "album-art" ) == ALBUM_ART_WHEN_PLAYED )
266     {
267         bool b_has_art;
268
269         char *psz_arturl, *psz_name;
270         psz_arturl = input_item_GetArtURL( p_input );
271         psz_name = input_item_GetName( p_input );
272
273         /* p_input->p_meta should not be null after a successful CreateThread */
274         b_has_art = !EMPTY_STR( psz_arturl );
275
276         if( !b_has_art || strncmp( psz_arturl, "attachment://", 13 ) )
277         {
278             PL_DEBUG( "requesting art for %s", psz_name );
279             playlist_AskForArtEnqueue( p_playlist, p_input );
280         }
281         free( psz_arturl );
282         free( psz_name );
283     }
284     /* FIXME: this is not safe !!*/
285     PL_UNLOCK;
286     var_SetAddress( p_playlist, "item-current", p_input );
287     PL_LOCK;
288
289     return VLC_SUCCESS;
290 }
291
292 /**
293  * Compute the next playlist item depending on
294  * the playlist course mode (forward, backward, random, view,...).
295  *
296  * \param p_playlist the playlist object
297  * \return nothing
298  */
299 static playlist_item_t *NextItem( playlist_t *p_playlist )
300 {
301     playlist_private_t *p_sys = pl_priv(p_playlist);
302     playlist_item_t *p_new = NULL;
303
304     /* Handle quickly a few special cases */
305     /* No items to play */
306     if( p_playlist->items.i_size == 0 )
307     {
308         msg_Info( p_playlist, "playlist is empty" );
309         return NULL;
310     }
311
312     /* Start the real work */
313     if( p_sys->request.b_request )
314     {
315         p_new = p_sys->request.p_item;
316         int i_skip = p_sys->request.i_skip;
317         PL_DEBUG( "processing request item: %s, node: %s, skip: %i",
318                         PLI_NAME( p_sys->request.p_item ),
319                         PLI_NAME( p_sys->request.p_node ), i_skip );
320
321         if( p_sys->request.p_node &&
322             p_sys->request.p_node != get_current_status_node( p_playlist ) )
323         {
324
325             set_current_status_node( p_playlist, p_sys->request.p_node );
326             p_sys->request.p_node = NULL;
327             p_sys->b_reset_currently_playing = true;
328         }
329
330         /* If we are asked for a node, go to it's first child */
331         if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
332         {
333             i_skip++;
334             if( p_new != NULL )
335             {
336                 p_new = playlist_GetNextLeaf( p_playlist, p_new, NULL, true, false );
337                 for( int i = 0; i < p_playlist->current.i_size; i++ )
338                 {
339                     if( p_new == ARRAY_VAL( p_playlist->current, i ) )
340                     {
341                         p_playlist->i_current_index = i;
342                         i_skip = 0;
343                     }
344                 }
345             }
346         }
347
348         if( p_sys->b_reset_currently_playing )
349             /* A bit too bad to reset twice ... */
350             ResetCurrentlyPlaying( p_playlist, p_new );
351         else if( p_new )
352             ResyncCurrentIndex( p_playlist, p_new );
353         else
354             p_playlist->i_current_index = -1;
355
356         if( p_playlist->current.i_size && (i_skip > 0) )
357         {
358             if( p_playlist->i_current_index < -1 )
359                 p_playlist->i_current_index = -1;
360             for( int i = i_skip; i > 0 ; i-- )
361             {
362                 p_playlist->i_current_index++;
363                 if( p_playlist->i_current_index >= p_playlist->current.i_size )
364                 {
365                     PL_DEBUG( "looping - restarting at beginning of node" );
366                     p_playlist->i_current_index = 0;
367                 }
368             }
369             p_new = ARRAY_VAL( p_playlist->current,
370                                p_playlist->i_current_index );
371         }
372         else if( p_playlist->current.i_size && (i_skip < 0) )
373         {
374             for( int i = i_skip; i < 0 ; i++ )
375             {
376                 p_playlist->i_current_index--;
377                 if( p_playlist->i_current_index <= -1 )
378                 {
379                     PL_DEBUG( "looping - restarting at end of node" );
380                     p_playlist->i_current_index = p_playlist->current.i_size-1;
381                 }
382             }
383             p_new = ARRAY_VAL( p_playlist->current,
384                                p_playlist->i_current_index );
385         }
386         /* Clear the request */
387         p_sys->request.b_request = false;
388     }
389     /* "Automatic" item change ( next ) */
390     else
391     {
392         bool b_loop = var_GetBool( p_playlist, "loop" );
393         bool b_repeat = var_GetBool( p_playlist, "repeat" );
394         bool b_playstop = var_GetBool( p_playlist, "play-and-stop" );
395
396         /* Repeat and play/stop */
397         if( b_repeat && get_current_status_item( p_playlist ) )
398         {
399             msg_Dbg( p_playlist,"repeating item" );
400             return get_current_status_item( p_playlist );
401         }
402         if( b_playstop )
403         {
404             msg_Dbg( p_playlist,"stopping (play and stop)" );
405             return NULL;
406         }
407
408         /* */
409         if( get_current_status_item( p_playlist ) )
410         {
411             playlist_item_t *p_parent = get_current_status_item( p_playlist );
412             while( p_parent )
413             {
414                 if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
415                 {
416                     msg_Dbg( p_playlist, "blocking item, stopping") ;
417                     return NULL;
418                 }
419                 p_parent = p_parent->p_parent;
420             }
421         }
422
423         PL_DEBUG( "changing item without a request (current %i/%i)",
424                   p_playlist->i_current_index, p_playlist->current.i_size );
425         /* Cant go to next from current item */
426         if( get_current_status_item( p_playlist ) &&
427             get_current_status_item( p_playlist )->i_flags & PLAYLIST_SKIP_FLAG )
428             return NULL;
429
430         if( p_sys->b_reset_currently_playing )
431             ResetCurrentlyPlaying( p_playlist,
432                                    get_current_status_item( p_playlist ) );
433
434         p_playlist->i_current_index++;
435         assert( p_playlist->i_current_index <= p_playlist->current.i_size );
436         if( p_playlist->i_current_index == p_playlist->current.i_size )
437         {
438             if( !b_loop || p_playlist->current.i_size == 0 )
439                 return NULL;
440             p_playlist->i_current_index = 0;
441         }
442         PL_DEBUG( "using item %i", p_playlist->i_current_index );
443         if ( p_playlist->current.i_size == 0 )
444             return NULL;
445
446         p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
447         /* The new item can't be autoselected  */
448         if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
449             return NULL;
450     }
451     return p_new;
452 }
453
454 static int LoopInput( playlist_t *p_playlist )
455 {
456     playlist_private_t *p_sys = pl_priv(p_playlist);
457     input_thread_t *p_input = p_sys->p_input;
458
459     if( !p_input )
460         return VLC_EGENERIC;
461
462     if( ( p_sys->request.b_request || p_sys->killed ) && !p_input->b_die )
463     {
464         PL_DEBUG( "incoming request - stopping current input" );
465         input_Stop( p_input, true );
466     }
467
468     /* This input is dead. Remove it ! */
469     if( p_input->b_dead )
470     {
471         PL_DEBUG( "dead input" );
472
473         PL_UNLOCK;
474         /* We can unlock as we return VLC_EGENERIC (no event will be lost) */
475
476         /* input_resource_t must be manipulated without playlist lock */
477         if( !var_CreateGetBool( p_input, "sout-keep" ) )
478             input_resource_TerminateSout( p_sys->p_input_resource );
479
480         /* The DelCallback must be issued without playlist lock */
481         var_DelCallback( p_input, "intf-event", InputEvent, p_playlist );
482
483         PL_LOCK;
484
485         p_sys->p_input = NULL;
486         input_Close( p_input );
487
488         UpdateActivity( p_playlist, -DEFAULT_INPUT_ACTIVITY );
489
490         return VLC_EGENERIC;
491     }
492     /* This input is dying, let it do */
493     else if( p_input->b_die )
494     {
495         PL_DEBUG( "dying input" );
496     }
497     /* This input has finished, ask it to die ! */
498     else if( p_input->b_error || p_input->b_eof )
499     {
500         PL_DEBUG( "finished input" );
501         input_Stop( p_input, false );
502     }
503     return VLC_SUCCESS;
504 }
505
506 static void LoopRequest( playlist_t *p_playlist )
507 {
508     playlist_private_t *p_sys = pl_priv(p_playlist);
509     assert( !p_sys->p_input );
510
511     /* No input. Several cases
512      *  - No request, running status -> start new item
513      *  - No request, stopped status -> collect garbage
514      *  - Request, running requested -> start new item
515      *  - Request, stopped requested -> collect garbage
516     */
517     const int i_status = p_sys->request.b_request ?
518                          p_sys->request.i_status : p_sys->status.i_status;
519
520     if( i_status == PLAYLIST_STOPPED || p_sys->killed )
521     {
522         p_sys->status.i_status = PLAYLIST_STOPPED;
523
524         if( input_resource_HasVout( p_sys->p_input_resource ) )
525         {
526             /* XXX We can unlock if we don't issue the wait as we will be
527              * call again without anything else done between the calls */
528             PL_UNLOCK;
529
530             /* input_resource_t must be manipulated without playlist lock */
531             input_resource_TerminateVout( p_sys->p_input_resource );
532
533             PL_LOCK;
534         }
535         else
536         {
537             if( !p_sys->killed )
538                 vlc_cond_wait( &p_sys->signal, &p_sys->lock );
539         }
540         return;
541     }
542
543     playlist_item_t *p_item = NextItem( p_playlist );
544     if( p_item )
545     {
546         msg_Dbg( p_playlist, "starting playback of the new playlist item" );
547         ResyncCurrentIndex( p_playlist, p_item );
548         PlayItem( p_playlist, p_item );
549         return;
550     }
551
552     msg_Dbg( p_playlist, "nothing to play" );
553     p_sys->status.i_status = PLAYLIST_STOPPED;
554
555     if( var_GetBool( p_playlist, "play-and-exit" ) )
556     {
557         msg_Info( p_playlist, "end of playlist, exiting" );
558         libvlc_Quit( p_playlist->p_libvlc );
559     }
560 }
561
562 /**
563  * Run the main control thread itself
564  */
565 static void *Thread ( void *data )
566 {
567     playlist_t *p_playlist = data;
568     playlist_private_t *p_sys = pl_priv(p_playlist);
569
570     playlist_Lock( p_playlist );
571     while( !p_sys->killed || p_sys->p_input )
572     {
573         /* FIXME: what's that ! */
574         if( p_sys->b_reset_currently_playing &&
575             mdate() - p_sys->last_rebuild_date > 30000 ) // 30 ms
576         {
577             ResetCurrentlyPlaying( p_playlist,
578                                    get_current_status_item( p_playlist ) );
579             p_sys->last_rebuild_date = mdate();
580         }
581
582         /* If there is an input, check that it doesn't need to die. */
583         while( !LoopInput( p_playlist ) )
584             vlc_cond_wait( &p_sys->signal, &p_sys->lock );
585
586         LoopRequest( p_playlist );
587     }
588     playlist_Unlock( p_playlist );
589
590     return NULL;
591 }
592