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