src/ : missing assert.h include
[vlc.git] / 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     /* */
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         if( input_Start( p_sys->p_input ) )
263         {
264             vlc_object_release( p_input_thread );
265             p_sys->p_input = p_input_thread = NULL;
266         }
267     }
268
269     p_sys->p_input_resource = NULL;
270
271     char *psz_uri = input_item_GetURI( p_item->p_input );
272     if( psz_uri && ( !strncmp( psz_uri, "directory:", 10 ) ||
273                      !strncmp( psz_uri, "vlc:", 4 ) ) )
274     {
275         free( psz_uri );
276         return VLC_SUCCESS;
277     }
278     free( psz_uri );
279
280     /* TODO store art policy in playlist private data */
281     if( var_GetInteger( p_playlist, "album-art" ) == ALBUM_ART_WHEN_PLAYED )
282     {
283         bool b_has_art;
284
285         char *psz_arturl, *psz_name;
286         psz_arturl = input_item_GetArtURL( p_input );
287         psz_name = input_item_GetName( p_input );
288
289         /* p_input->p_meta should not be null after a successfull CreateThread */
290         b_has_art = !EMPTY_STR( psz_arturl );
291
292         if( !b_has_art || strncmp( psz_arturl, "attachment://", 13 ) )
293         {
294             PL_DEBUG( "requesting art for %s", psz_name );
295             playlist_AskForArtEnqueue( p_playlist, p_input, pl_Locked );
296         }
297         free( psz_arturl );
298         free( psz_name );
299     }
300
301     PL_UNLOCK;
302     var_SetAddress( p_playlist, "item-current", p_input );
303     PL_LOCK;
304
305     return VLC_SUCCESS;
306 }
307
308 /**
309  * Compute the next playlist item depending on
310  * the playlist course mode (forward, backward, random, view,...).
311  *
312  * \param p_playlist the playlist object
313  * \return nothing
314  */
315 static playlist_item_t *NextItem( playlist_t *p_playlist )
316 {
317     playlist_private_t *p_sys = pl_priv(p_playlist);
318     playlist_item_t *p_new = NULL;
319
320     /* Handle quickly a few special cases */
321     /* No items to play */
322     if( p_playlist->items.i_size == 0 )
323     {
324         msg_Info( p_playlist, "playlist is empty" );
325         return NULL;
326     }
327
328     /* Start the real work */
329     if( p_sys->request.b_request )
330     {
331         p_new = p_sys->request.p_item;
332         int i_skip = p_sys->request.i_skip;
333         PL_DEBUG( "processing request item %s node %s skip %i",
334                         PLI_NAME( p_sys->request.p_item ),
335                         PLI_NAME( p_sys->request.p_node ), i_skip );
336
337         if( p_sys->request.p_node &&
338             p_sys->request.p_node != get_current_status_node( p_playlist ) )
339         {
340
341             set_current_status_node( p_playlist, p_sys->request.p_node );
342             p_sys->request.p_node = NULL;
343             p_sys->b_reset_currently_playing = true;
344         }
345
346         /* If we are asked for a node, go to it's first child */
347         if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
348         {
349             i_skip++;
350             if( p_new != NULL )
351             {
352                 p_new = playlist_GetNextLeaf( p_playlist, p_new, NULL, true, false );
353                 for( int i = 0; i < p_playlist->current.i_size; i++ )
354                 {
355                     if( p_new == ARRAY_VAL( p_playlist->current, i ) )
356                     {
357                         p_playlist->i_current_index = i;
358                         i_skip = 0;
359                     }
360                 }
361             }
362         }
363
364         if( p_sys->b_reset_currently_playing )
365             /* A bit too bad to reset twice ... */
366             ResetCurrentlyPlaying( p_playlist, p_new );
367         else if( p_new )
368             ResyncCurrentIndex( p_playlist, p_new );
369         else
370             p_playlist->i_current_index = -1;
371
372         if( p_playlist->current.i_size && (i_skip > 0) )
373         {
374             if( p_playlist->i_current_index < -1 )
375                 p_playlist->i_current_index = -1;
376             for( int i = i_skip; i > 0 ; i-- )
377             {
378                 p_playlist->i_current_index++;
379                 if( p_playlist->i_current_index >= p_playlist->current.i_size )
380                 {
381                     PL_DEBUG( "looping - restarting at beginning of node" );
382                     p_playlist->i_current_index = 0;
383                 }
384             }
385             p_new = ARRAY_VAL( p_playlist->current,
386                                p_playlist->i_current_index );
387         }
388         else if( p_playlist->current.i_size && (i_skip < 0) )
389         {
390             for( int i = i_skip; i < 0 ; i++ )
391             {
392                 p_playlist->i_current_index--;
393                 if( p_playlist->i_current_index <= -1 )
394                 {
395                     PL_DEBUG( "looping - restarting at end of node" );
396                     p_playlist->i_current_index = p_playlist->current.i_size-1;
397                 }
398             }
399             p_new = ARRAY_VAL( p_playlist->current,
400                                p_playlist->i_current_index );
401         }
402         /* Clear the request */
403         p_sys->request.b_request = false;
404     }
405     /* "Automatic" item change ( next ) */
406     else
407     {
408         bool b_loop = var_GetBool( p_playlist, "loop" );
409         bool b_repeat = var_GetBool( p_playlist, "repeat" );
410         bool b_playstop = var_GetBool( p_playlist, "play-and-stop" );
411
412         /* Repeat and play/stop */
413         if( b_repeat && get_current_status_item( p_playlist ) )
414         {
415             msg_Dbg( p_playlist,"repeating item" );
416             return get_current_status_item( p_playlist );
417         }
418         if( b_playstop )
419         {
420             msg_Dbg( p_playlist,"stopping (play and stop)" );
421             return NULL;
422         }
423
424         /* */
425         if( get_current_status_item( p_playlist ) )
426         {
427             playlist_item_t *p_parent = get_current_status_item( p_playlist );
428             while( p_parent )
429             {
430                 if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
431                 {
432                     msg_Dbg( p_playlist, "blocking item, stopping") ;
433                     return NULL;
434                 }
435                 p_parent = p_parent->p_parent;
436             }
437         }
438
439         PL_DEBUG( "changing item without a request (current %i/%i)",
440                   p_playlist->i_current_index, p_playlist->current.i_size );
441         /* Cant go to next from current item */
442         if( get_current_status_item( p_playlist ) &&
443             get_current_status_item( p_playlist )->i_flags & PLAYLIST_SKIP_FLAG )
444             return NULL;
445
446         if( p_sys->b_reset_currently_playing )
447             ResetCurrentlyPlaying( p_playlist,
448                                    get_current_status_item( p_playlist ) );
449
450         p_playlist->i_current_index++;
451         assert( p_playlist->i_current_index <= p_playlist->current.i_size );
452         if( p_playlist->i_current_index == p_playlist->current.i_size )
453         {
454             if( !b_loop || p_playlist->current.i_size == 0 )
455                 return NULL;
456             p_playlist->i_current_index = 0;
457         }
458         PL_DEBUG( "using item %i", p_playlist->i_current_index );
459         if ( p_playlist->current.i_size == 0 )
460             return NULL;
461
462         p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
463         /* The new item can't be autoselected  */
464         if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
465             return NULL;
466     }
467     return p_new;
468 }
469
470 static int LoopInput( playlist_t *p_playlist )
471 {
472     playlist_private_t *p_sys = pl_priv(p_playlist);
473     input_thread_t *p_input = p_sys->p_input;
474
475     if( !p_input )
476         return VLC_EGENERIC;
477
478     if( ( p_sys->request.b_request || !vlc_object_alive( p_playlist ) ) && !p_input->b_die )
479     {
480         PL_DEBUG( "incoming request - stopping current input" );
481         input_Stop( p_input, true );
482     }
483
484     /* This input is dead. Remove it ! */
485     if( p_input->b_dead )
486     {
487         PL_DEBUG( "dead input" );
488
489         assert( p_sys->p_input_resource == NULL );
490
491         p_sys->p_input_resource = input_DetachResource( p_input );
492
493         PL_UNLOCK;
494         /* We can unlock as we return VLC_EGENERIC (no event will be lost) */
495
496         /* input_resource_t must be manipulated without playlist lock */
497         if( !var_CreateGetBool( p_input, "sout-keep" ) )
498             input_resource_TerminateSout( p_sys->p_input_resource );
499
500         /* The DelCallback must be issued without playlist lock */
501         var_DelCallback( p_input, "intf-event", InputEvent, p_playlist );
502
503         PL_LOCK;
504
505         p_sys->p_input = NULL;
506         vlc_thread_join( p_input );
507         vlc_object_release( p_input );
508
509         UpdateActivity( p_playlist, -DEFAULT_INPUT_ACTIVITY );
510
511         return VLC_EGENERIC;
512     }
513     /* This input is dying, let it do */
514     else if( p_input->b_die )
515     {
516         PL_DEBUG( "dying input" );
517     }
518     /* This input has finished, ask it to die ! */
519     else if( p_input->b_error || p_input->b_eof )
520     {
521         PL_DEBUG( "finished input" );
522         input_Stop( p_input, false );
523     }
524     return VLC_SUCCESS;
525 }
526
527 static void LoopRequest( playlist_t *p_playlist )
528 {
529     playlist_private_t *p_sys = pl_priv(p_playlist);
530     assert( !p_sys->p_input );
531
532     /* No input. Several cases
533      *  - No request, running status -> start new item
534      *  - No request, stopped status -> collect garbage
535      *  - Request, running requested -> start new item
536      *  - Request, stopped requested -> collect garbage
537     */
538     const int i_status = p_sys->request.b_request ?
539                          p_sys->request.i_status : p_sys->status.i_status;
540
541     if( i_status == PLAYLIST_STOPPED || !vlc_object_alive( p_playlist ) )
542     {
543         p_sys->status.i_status = PLAYLIST_STOPPED;
544
545         if( p_sys->p_input_resource &&
546             input_resource_HasVout( p_sys->p_input_resource ) )
547         {
548             /* XXX We can unlock if we don't issue the wait as we will be
549              * call again without anything else done between the calls */
550             PL_UNLOCK;
551
552             /* input_resource_t must be manipulated without playlist lock */
553             input_resource_TerminateVout( p_sys->p_input_resource );
554
555             PL_LOCK;
556         }
557         else
558         {
559             if( vlc_object_alive( p_playlist ) )
560                 vlc_cond_wait( &p_sys->signal, &p_sys->lock );
561         }
562         return;
563     }
564
565     playlist_item_t *p_item = NextItem( p_playlist );
566     if( p_item )
567     {
568         msg_Dbg( p_playlist, "starting new item" );
569         PlayItem( p_playlist, p_item );
570         return;
571     }
572
573     msg_Dbg( p_playlist, "nothing to play" );
574     p_sys->status.i_status = PLAYLIST_STOPPED;
575
576     if( var_GetBool( p_playlist, "play-and-exit" ) )
577     {
578         msg_Info( p_playlist, "end of playlist, exiting" );
579         libvlc_Quit( p_playlist->p_libvlc );
580     }
581 }
582
583 /**
584  * Run the main control thread itself
585  */
586 static void *Thread ( void *data )
587 {
588     playlist_t *p_playlist = data;
589     playlist_private_t *p_sys = pl_priv(p_playlist);
590
591     playlist_Lock( p_playlist );
592     while( vlc_object_alive( p_playlist ) || p_sys->p_input )
593     {
594         /* FIXME: what's that ! */
595         if( p_sys->b_reset_currently_playing &&
596             mdate() - p_sys->last_rebuild_date > 30000 ) // 30 ms
597         {
598             ResetCurrentlyPlaying( p_playlist,
599                                    get_current_status_item( p_playlist ) );
600             p_sys->last_rebuild_date = mdate();
601         }
602
603         /* If there is an input, check that it doesn't need to die. */
604         while( !LoopInput( p_playlist ) )
605             vlc_cond_wait( &p_sys->signal, &p_sys->lock );
606
607         LoopRequest( p_playlist );
608     }
609     playlist_Unlock( p_playlist );
610
611     return NULL;
612 }
613