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