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