]> git.sesse.net Git - vlc/blob - src/playlist/control.c
playlist: Make sure we don't crash when we delete the currently playing node.
[vlc] / src / playlist / control.c
1 /*****************************************************************************
2  * control.c : Handle control of the playlist & running through it
3  *****************************************************************************
4  * Copyright (C) 1999-2004 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 <vlc_common.h>
29 #include "vlc_playlist.h"
30 #include "playlist_internal.h"
31 #include <assert.h>
32
33 /*****************************************************************************
34  * Local prototypes
35  *****************************************************************************/
36 static int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args );
37
38 static void PreparseEnqueueItemSub( playlist_t *, playlist_item_t * );
39
40 /*****************************************************************************
41  * Playlist control
42  *****************************************************************************/
43
44 playlist_t *__pl_Yield( vlc_object_t *p_this )
45 {
46     playlist_t *pl;
47
48     barrier ();
49     pl = libvlc_priv (p_this->p_libvlc)->p_playlist;
50     if (pl)
51         vlc_object_yield (pl);
52     return pl;
53 }
54
55 void __pl_Release( vlc_object_t *p_this )
56 {
57     playlist_t *pl = libvlc_priv (p_this->p_libvlc)->p_playlist;
58     assert( pl != NULL );
59     vlc_object_release( pl );
60 }
61
62 int playlist_Control( playlist_t * p_playlist, int i_query,
63                       bool b_locked, ... )
64 {
65     va_list args;
66     int i_result;
67     va_start( args, b_locked );
68     if( !b_locked ) PL_LOCK;
69     i_result = PlaylistVAControl( p_playlist, i_query, args );
70     va_end( args );
71     if( !b_locked ) PL_UNLOCK;
72
73     return i_result;
74 }
75
76 static int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args )
77 {
78     playlist_item_t *p_item, *p_node;
79     vlc_value_t val;
80
81     if( playlist_IsEmpty( p_playlist ) )
82         return VLC_EGENERIC;
83
84     switch( i_query )
85     {
86     case PLAYLIST_STOP:
87         p_playlist->request.i_status = PLAYLIST_STOPPED;
88         p_playlist->request.b_request = true;
89         p_playlist->request.p_item = NULL;
90         break;
91
92     // Node can be null, it will keep the same. Use with care ...
93     // Item null = take the first child of node
94     case PLAYLIST_VIEWPLAY:
95         p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
96         p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
97         if ( p_node == NULL )
98         {
99             p_node = p_playlist->status.p_node;
100             assert( p_node );
101         }
102         p_playlist->request.i_status = PLAYLIST_RUNNING;
103         p_playlist->request.i_skip = 0;
104         p_playlist->request.b_request = true;
105         p_playlist->request.p_node = p_node;
106         p_playlist->request.p_item = p_item;
107         if( p_item && var_GetBool( p_playlist, "random" ) )
108             p_playlist->b_reset_currently_playing = true;
109         break;
110
111     case PLAYLIST_PLAY:
112         if( p_playlist->p_input )
113         {
114             val.i_int = PLAYING_S;
115             var_Set( p_playlist->p_input, "state", val );
116             break;
117         }
118         else
119         {
120             p_playlist->request.i_status = PLAYLIST_RUNNING;
121             p_playlist->request.b_request = true;
122             p_playlist->request.p_node = p_playlist->status.p_node;
123             p_playlist->request.p_item = p_playlist->status.p_item;
124             p_playlist->request.i_skip = 0;
125         }
126         break;
127
128     case PLAYLIST_PAUSE:
129         val.i_int = 0;
130         if( p_playlist->p_input )
131             var_Get( p_playlist->p_input, "state", &val );
132
133         if( val.i_int == PAUSE_S )
134         {
135             p_playlist->status.i_status = PLAYLIST_RUNNING;
136             if( p_playlist->p_input )
137             {
138                 val.i_int = PLAYING_S;
139                 var_Set( p_playlist->p_input, "state", val );
140             }
141         }
142         else
143         {
144             p_playlist->status.i_status = PLAYLIST_PAUSED;
145             if( p_playlist->p_input )
146             {
147                 val.i_int = PAUSE_S;
148                 var_Set( p_playlist->p_input, "state", val );
149             }
150         }
151         break;
152
153     case PLAYLIST_SKIP:
154         p_playlist->request.p_node = p_playlist->status.p_node;
155         p_playlist->request.p_item = p_playlist->status.p_item;
156         p_playlist->request.i_skip = (int) va_arg( args, int );
157         /* if already running, keep running */
158         if( p_playlist->status.i_status != PLAYLIST_STOPPED )
159             p_playlist->request.i_status = p_playlist->status.i_status;
160         p_playlist->request.b_request = true;
161         break;
162
163     default:
164         msg_Err( p_playlist, "unknown playlist query" );
165         return VLC_EBADVAR;
166         break;
167     }
168     vlc_object_signal_maybe( VLC_OBJECT(p_playlist) );
169
170     return VLC_SUCCESS;
171 }
172
173 /*****************************************************************************
174  * Preparse control
175  *****************************************************************************/
176 /** Enqueue an item for preparsing */
177 int playlist_PreparseEnqueue( playlist_t *p_playlist,
178                               input_item_t *p_item )
179 {
180     vlc_object_lock( p_playlist->p_preparse );
181     vlc_gc_incref( p_item );
182     INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
183                  p_playlist->p_preparse->i_waiting,
184                  p_playlist->p_preparse->i_waiting,
185                  p_item );
186     vlc_object_signal_unlocked( p_playlist->p_preparse );
187     vlc_object_unlock( p_playlist->p_preparse );
188     return VLC_SUCCESS;
189 }
190
191 /** Enqueue a playlist item or a node for peparsing.
192  *  This function should be entered without playlist and preparser locks */
193 int playlist_PreparseEnqueueItem( playlist_t *p_playlist,
194                                   playlist_item_t *p_item )
195 {
196     vlc_object_lock( p_playlist );
197     vlc_object_lock( p_playlist->p_preparse );
198     PreparseEnqueueItemSub( p_playlist, p_item );
199     vlc_object_unlock( p_playlist->p_preparse );
200     vlc_object_unlock( p_playlist );
201     return VLC_SUCCESS;
202 }
203
204 int playlist_AskForArtEnqueue( playlist_t *p_playlist,
205                                input_item_t *p_item )
206 {
207     int i;
208
209     vlc_object_lock( p_playlist->p_fetcher );
210     for( i = 0; i < p_playlist->p_fetcher->i_waiting ; i++ );
211     vlc_gc_incref( p_item );
212     INSERT_ELEM( p_playlist->p_fetcher->pp_waiting,
213                  p_playlist->p_fetcher->i_waiting,
214                  i, p_item );
215     vlc_object_signal_unlocked( p_playlist->p_fetcher );
216     vlc_object_unlock( p_playlist->p_fetcher );
217     return VLC_SUCCESS;
218 }
219
220 static void PreparseEnqueueItemSub( playlist_t *p_playlist,
221                                     playlist_item_t *p_item )
222 {
223     int i;
224     if( p_item->i_children == -1 )
225     {
226         vlc_gc_incref( p_item );
227         INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
228                      p_playlist->p_preparse->i_waiting,
229                      p_playlist->p_preparse->i_waiting,
230                      p_item->p_input );
231     }
232     else
233     {
234         for( i = 0; i < p_item->i_children; i++)
235         {
236             PreparseEnqueueItemSub( p_playlist, p_item->pp_children[i] );
237         }
238     }
239 }
240
241 /*****************************************************************************
242  * Playback logic
243  *****************************************************************************/
244
245 /**
246  * Synchronise the current index of the playlist
247  * to match the index of the current item.
248  *
249  * \param p_playlist the playlist structure
250  * \param p_cur the current playlist item
251  * \return nothing
252  */
253 static void ResyncCurrentIndex( playlist_t *p_playlist, playlist_item_t *p_cur )
254 {
255      PL_DEBUG( "resyncing on %s", PLI_NAME( p_cur ) );
256      /* Simply resync index */
257      int i;
258      p_playlist->i_current_index = -1;
259      for( i = 0 ; i< p_playlist->current.i_size; i++ )
260      {
261           if( ARRAY_VAL( p_playlist->current, i ) == p_cur )
262           {
263               p_playlist->i_current_index = i;
264               break;
265           }
266      }
267      PL_DEBUG( "%s is at %i", PLI_NAME( p_cur ), p_playlist->i_current_index );
268 }
269
270 void ResetCurrentlyPlaying( playlist_t *p_playlist, bool b_random,
271                             playlist_item_t *p_cur )
272 {
273     playlist_item_t *p_next = NULL;
274     stats_TimerStart( p_playlist, "Items array build",
275                       STATS_TIMER_PLAYLIST_BUILD );
276     PL_DEBUG( "rebuilding array of current - root %s",
277               PLI_NAME( p_playlist->status.p_node ) );
278     ARRAY_RESET( p_playlist->current );
279     p_playlist->i_current_index = -1;
280     while( 1 )
281     {
282         /** FIXME: this is *slow* */
283         p_next = playlist_GetNextLeaf( p_playlist,
284                                        p_playlist->status.p_node,
285                                        p_next, true, false );
286         if( p_next )
287         {
288             if( p_next == p_cur )
289                 p_playlist->i_current_index = p_playlist->current.i_size;
290             ARRAY_APPEND( p_playlist->current, p_next);
291         }
292         else break;
293     }
294     PL_DEBUG("rebuild done - %i items, index %i", p_playlist->current.i_size,
295                                                   p_playlist->i_current_index);
296     if( b_random )
297     {
298         /* Shuffle the array */
299         srand( (unsigned int)mdate() );
300         int j;
301         for( j = p_playlist->current.i_size - 1; j > 0; j-- )
302         {
303             int i = rand() % (j+1); /* between 0 and j */
304             playlist_item_t *p_tmp;
305             /* swap the two items */
306             p_tmp = ARRAY_VAL(p_playlist->current, i);
307             ARRAY_VAL(p_playlist->current,i) = ARRAY_VAL(p_playlist->current,j);
308             ARRAY_VAL(p_playlist->current,j) = p_tmp;
309         }
310     }
311     p_playlist->b_reset_currently_playing = false;
312     stats_TimerStop( p_playlist, STATS_TIMER_PLAYLIST_BUILD );
313 }
314
315 /**
316  * Compute the next playlist item depending on
317  * the playlist course mode (forward, backward, random, view,...).
318  *
319  * \param p_playlist the playlist object
320  * \return nothing
321  */
322 playlist_item_t * playlist_NextItem( playlist_t *p_playlist )
323 {
324     playlist_item_t *p_new = NULL;
325     int i_skip = 0, i;
326
327     bool b_loop = var_GetBool( p_playlist, "loop" );
328     bool b_random = var_GetBool( p_playlist, "random" );
329     bool b_repeat = var_GetBool( p_playlist, "repeat" );
330     bool b_playstop = var_GetBool( p_playlist, "play-and-stop" );
331
332     /* Handle quickly a few special cases */
333     /* No items to play */
334     if( p_playlist->items.i_size == 0 )
335     {
336         msg_Info( p_playlist, "playlist is empty" );
337         return NULL;
338     }
339
340     /* Repeat and play/stop */
341     if( !p_playlist->request.b_request && b_repeat == true &&
342          p_playlist->status.p_item )
343     {
344         msg_Dbg( p_playlist,"repeating item" );
345         return p_playlist->status.p_item;
346     }
347     if( !p_playlist->request.b_request && b_playstop == true )
348     {
349         msg_Dbg( p_playlist,"stopping (play and stop)" );
350         return NULL;
351     }
352
353     if( !p_playlist->request.b_request && p_playlist->status.p_item )
354     {
355         playlist_item_t *p_parent = p_playlist->status.p_item;
356         while( p_parent )
357         {
358             if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
359             {
360                 msg_Dbg( p_playlist, "blocking item, stopping") ;
361                 return NULL;
362             }
363             p_parent = p_parent->p_parent;
364         }
365     }
366
367     /* Start the real work */
368     if( p_playlist->request.b_request )
369     {
370         p_new = p_playlist->request.p_item;
371         i_skip = p_playlist->request.i_skip;
372         PL_DEBUG( "processing request item %s node %s skip %i",
373                         PLI_NAME( p_playlist->request.p_item ),
374                         PLI_NAME( p_playlist->request.p_node ), i_skip );
375
376         /* Make sure the node wasn't deleted */
377         if( p_playlist->status.p_node &&
378             p_playlist->status.p_node->i_flags & PLAYLIST_REMOVE_FLAG )
379         {
380              PL_DEBUG( "%s was marked for deletion, deleting",
381                              PLI_NAME( p_playlist->status.p_node  ) );
382              playlist_ItemDelete( p_playlist->status.p_node );
383              /* Don't attempt to reuse that node */
384              if( p_playlist->status.p_node == p_playlist->request.p_node )
385                 p_playlist->request.p_node = NULL;
386              p_playlist->status.p_node = NULL;
387         }
388
389         if( p_playlist->request.p_node &&
390             p_playlist->request.p_node != p_playlist->status.p_node )
391         {
392
393             p_playlist->status.p_node = p_playlist->request.p_node;
394             p_playlist->b_reset_currently_playing = true;
395         }
396
397         /* If we are asked for a node, don't take it */
398         if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
399             i_skip++;
400
401         if( p_playlist->b_reset_currently_playing )
402             /* A bit too bad to reset twice ... */
403             ResetCurrentlyPlaying( p_playlist, b_random, p_new );
404         else if( p_new )
405             ResyncCurrentIndex( p_playlist, p_new );
406         else
407             p_playlist->i_current_index = -1;
408
409         if( p_playlist->current.i_size && (i_skip > 0) )
410         {
411             if( p_playlist->i_current_index < -1 )
412                 p_playlist->i_current_index = -1;
413             for( i = i_skip; i > 0 ; i-- )
414             {
415                 p_playlist->i_current_index++;
416                 if( p_playlist->i_current_index >= p_playlist->current.i_size )
417                 {
418                     PL_DEBUG( "looping - restarting at beginning of node" );
419                     p_playlist->i_current_index = 0;
420                 }
421             }
422             p_new = ARRAY_VAL( p_playlist->current,
423                                p_playlist->i_current_index );
424         }
425         else if( p_playlist->current.i_size && (i_skip < 0) )
426         {
427             for( i = i_skip; i < 0 ; i++ )
428             {
429                 p_playlist->i_current_index--;
430                 if( p_playlist->i_current_index <= -1 )
431                 {
432                     PL_DEBUG( "looping - restarting at end of node" );
433                     p_playlist->i_current_index = p_playlist->current.i_size-1;
434                 }
435             }
436             p_new = ARRAY_VAL( p_playlist->current,
437                                p_playlist->i_current_index );
438         }
439         /* Clear the request */
440         p_playlist->request.b_request = false;
441     }
442     /* "Automatic" item change ( next ) */
443     else
444     {
445         PL_DEBUG( "changing item without a request (current %i/%i)",
446                   p_playlist->i_current_index, p_playlist->current.i_size );
447         /* Cant go to next from current item */
448         if( p_playlist->status.p_item &&
449             p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
450             return NULL;
451
452         if( p_playlist->b_reset_currently_playing )
453             ResetCurrentlyPlaying( p_playlist, b_random,
454                                    p_playlist->status.p_item );
455
456         p_playlist->i_current_index++;
457         assert( p_playlist->i_current_index <= p_playlist->current.i_size );
458         if( p_playlist->i_current_index == p_playlist->current.i_size )
459         {
460             if( !b_loop || p_playlist->current.i_size == 0 ) return NULL;
461             p_playlist->i_current_index = 0;
462         }
463         PL_DEBUG( "using item %i", p_playlist->i_current_index );
464         if ( p_playlist->current.i_size == 0 ) return NULL;
465
466         p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
467         /* The new item can't be autoselected  */
468         if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
469             return NULL;
470     }
471     return p_new;
472 }
473
474 /**
475  * Start the input for an item
476  *
477  * \param p_playlist the playlist objetc
478  * \param p_item the item to play
479  * \return nothing
480  */
481 int playlist_PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
482 {
483     input_item_t *p_input = p_item->p_input;
484     sout_instance_t **pp_sout = &libvlc_priv(p_playlist->p_libvlc)->p_sout;
485     int i_activity = var_GetInteger( p_playlist, "activity" ) ;
486
487     msg_Dbg( p_playlist, "creating new input thread" );
488
489     p_input->i_nb_played++;
490     p_playlist->status.p_item = p_item;
491
492     p_playlist->status.i_status = PLAYLIST_RUNNING;
493
494     var_SetInteger( p_playlist, "activity", i_activity +
495                     DEFAULT_INPUT_ACTIVITY );
496
497     input_thread_t * p_input_thread =
498         input_CreateThreadExtended( p_playlist, p_input, NULL, *pp_sout );
499     playlist_set_current_input( p_playlist, p_input_thread );
500     vlc_object_release( p_input_thread );
501
502     *pp_sout = NULL;
503
504     char *psz_uri = input_item_GetURI( p_item->p_input );
505     if( psz_uri && ( !strncmp( psz_uri, "directory:", 10 ) ||
506                      !strncmp( psz_uri, "vlc:", 4 ) ) )
507     {
508         free( psz_uri );
509         return VLC_SUCCESS;
510     }
511     free( psz_uri );
512
513     if( p_playlist->p_fetcher &&
514             p_playlist->p_fetcher->i_art_policy == ALBUM_ART_WHEN_PLAYED )
515     {
516         bool b_has_art;
517
518         char *psz_arturl, *psz_name;
519         psz_arturl = input_item_GetArtURL( p_input );
520         psz_name = input_item_GetName( p_input );
521
522         /* p_input->p_meta should not be null after a successfull CreateThread */
523         b_has_art = !EMPTY_STR( psz_arturl );
524
525         if( !b_has_art || strncmp( psz_arturl, "attachment://", 13 ) )
526         {
527             PL_DEBUG( "requesting art for %s", psz_name );
528             playlist_AskForArtEnqueue( p_playlist, p_input );
529         }
530         free( psz_arturl );
531         free( psz_name );
532     }
533
534     PL_UNLOCK;
535     var_SetInteger( p_playlist, "playlist-current", p_input->i_id );
536     PL_LOCK;
537
538     return VLC_SUCCESS;
539 }