]> git.sesse.net Git - vlc/blob - src/playlist/control.c
Finish the playlist API transition (hopefully)
[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: /local/vlc/0.8.6-playlist-vlm/src/playlist/playlist.c 13741 2006-03-21T19:29:39.792444Z zorglub  $
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 #include <vlc/vlc.h>
25 #include <vlc/input.h>
26 #include "vlc_playlist.h"
27 #include "playlist_internal.h"
28 #include <assert.h>
29
30 /*****************************************************************************
31  * Local prototypes
32  *****************************************************************************/
33 static int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args );
34
35 static void PreparseEnqueueItemSub( playlist_t *, playlist_item_t * );
36
37 /*****************************************************************************
38  * Playlist control
39  *****************************************************************************/
40
41 /**
42  * Do a playlist action.
43  * If there is something in the playlist then you can do playlist actions.
44  * Should be entered with playlist lock. See include/vlc_playlist.h for
45  * possible queries
46  *
47  * \param p_playlist the playlist to do the command on
48  * \param i_query the command to do
49  * \param variable number of arguments
50  * \return VLC_SUCCESS or an error
51  */
52 int playlist_Control( playlist_t * p_playlist, int i_query, vlc_bool_t b_locked, ... )
53 {
54     va_list args;
55     int i_result;
56     va_start( args, b_locked );
57     if( !b_locked ) PL_LOCK;
58     i_result = PlaylistVAControl( p_playlist, i_query, args );
59     va_end( args );
60     if( !b_locked ) PL_UNLOCK;
61
62     return i_result;
63 }
64
65 static int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args )
66 {
67     playlist_item_t *p_item, *p_node;
68     vlc_value_t val;
69
70     if( playlist_IsEmpty( p_playlist ) )
71         return VLC_EGENERIC;
72
73     switch( i_query )
74     {
75     case PLAYLIST_STOP:
76         p_playlist->request.i_status = PLAYLIST_STOPPED;
77         p_playlist->request.b_request = VLC_TRUE;
78         p_playlist->request.p_item = NULL;
79         break;
80
81     // Node can be null, it will keep the same. Use with care ...
82     // Item null = take the first child of node
83     case PLAYLIST_VIEWPLAY:
84         p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
85         p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
86         if ( p_node == NULL )
87         {
88             p_node = p_playlist->status.p_node;
89             assert( p_node );
90         }
91         p_playlist->request.i_status = PLAYLIST_RUNNING;
92         p_playlist->request.i_skip = 0;
93         p_playlist->request.b_request = VLC_TRUE;
94         p_playlist->request.p_node = p_node;
95         p_playlist->request.p_item = p_item;
96         if( p_item && var_GetBool( p_playlist, "random" ) )
97             p_playlist->b_reset_currently_playing = VLC_TRUE;
98         break;
99
100     case PLAYLIST_PLAY:
101         if( p_playlist->p_input )
102         {
103             val.i_int = PLAYING_S;
104             var_Set( p_playlist->p_input, "state", val );
105             break;
106         }
107         else
108         {
109             p_playlist->request.i_status = PLAYLIST_RUNNING;
110             p_playlist->request.b_request = VLC_TRUE;
111             p_playlist->request.p_node = p_playlist->status.p_node;
112             p_playlist->request.p_item = p_playlist->status.p_item;
113             p_playlist->request.i_skip = 0;
114         }
115         break;
116
117     case PLAYLIST_AUTOPLAY:
118         // AUTOPLAY is an ugly hack for initial status.
119         // Hopefully it will disappear
120         p_playlist->status.i_status = PLAYLIST_RUNNING;
121         p_playlist->request.p_node = p_playlist->status.p_node;
122         p_playlist->request.b_request = VLC_FALSE;
123         break;
124
125     case PLAYLIST_PAUSE:
126         val.i_int = 0;
127         if( p_playlist->p_input )
128             var_Get( p_playlist->p_input, "state", &val );
129
130         if( val.i_int == PAUSE_S )
131         {
132             p_playlist->status.i_status = PLAYLIST_RUNNING;
133             if( p_playlist->p_input )
134             {
135                 val.i_int = PLAYING_S;
136                 var_Set( p_playlist->p_input, "state", val );
137             }
138         }
139         else
140         {
141             p_playlist->status.i_status = PLAYLIST_PAUSED;
142             if( p_playlist->p_input )
143             {
144                 val.i_int = PAUSE_S;
145                 var_Set( p_playlist->p_input, "state", val );
146             }
147         }
148         break;
149
150     case PLAYLIST_SKIP:
151         p_playlist->request.p_node = p_playlist->status.p_node;
152         p_playlist->request.p_item = p_playlist->status.p_item;
153         p_playlist->request.i_skip = (int) va_arg( args, int );
154         /* if already running, keep running */
155         if( p_playlist->status.i_status != PLAYLIST_STOPPED )
156             p_playlist->request.i_status = p_playlist->status.i_status;
157         p_playlist->request.b_request = VLC_TRUE;
158         break;
159
160     default:
161         msg_Err( p_playlist, "unknown playlist query" );
162         return VLC_EBADVAR;
163         break;
164     }
165     vlc_cond_signal( &p_playlist->object_wait );
166
167     return VLC_SUCCESS;
168 }
169
170 /*****************************************************************************
171  * Preparse control
172  *****************************************************************************/
173 /** Enqueue an item for preparsing */
174 int playlist_PreparseEnqueue( playlist_t *p_playlist,
175                               input_item_t *p_item )
176 {
177     vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
178     vlc_gc_incref( p_item );
179     INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
180                  p_playlist->p_preparse->i_waiting,
181                  p_playlist->p_preparse->i_waiting,
182                  p_item );
183     vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
184     vlc_cond_signal( &p_playlist->p_preparse->object_wait );
185     return VLC_SUCCESS;
186 }
187
188 /** Enqueue a playlist item or a node for peparsing.
189  *  This function should be entered without playlist and preparser locks */
190 int playlist_PreparseEnqueueItem( playlist_t *p_playlist,
191                                   playlist_item_t *p_item )
192 {
193     vlc_mutex_lock( &p_playlist->object_lock );
194     vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
195     PreparseEnqueueItemSub( p_playlist, p_item );
196     vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
197     vlc_mutex_unlock( &p_playlist->object_lock );
198     return VLC_SUCCESS;
199 }
200
201 int playlist_AskForArtEnqueue( playlist_t *p_playlist,
202                               input_item_t *p_item )
203 {
204     int i;
205     preparse_item_t p;
206     p.p_item = p_item;
207     p.b_fetch_art = VLC_TRUE;
208
209     vlc_mutex_lock( &p_playlist->p_fetcher->object_lock );
210     for( i = 0; i < p_playlist->p_fetcher->i_waiting &&
211          p_playlist->p_fetcher->p_waiting->b_fetch_art == VLC_TRUE;
212          i++ );
213     vlc_gc_incref( p_item );
214     INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
215                  p_playlist->p_fetcher->i_waiting,
216                  i, p );
217     vlc_mutex_unlock( &p_playlist->p_fetcher->object_lock );
218     vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
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 static void ResyncCurrentIndex(playlist_t *p_playlist, playlist_item_t *p_cur )
248 {
249      PL_DEBUG("resyncing on %s", PLI_NAME(p_cur) );
250      /* Simply resync index */
251      int i;
252      p_playlist->i_current_index = -1;
253      for( i = 0 ; i< p_playlist->current.i_size; i++ )
254      {
255           if( ARRAY_VAL(p_playlist->current, i) == p_cur )
256           {
257               p_playlist->i_current_index = i;
258               break;
259           }
260      }
261      PL_DEBUG("%s is at %i", PLI_NAME(p_cur), p_playlist->i_current_index );
262 }
263
264 void ResetCurrentlyPlaying( playlist_t *p_playlist, vlc_bool_t b_random,
265                             playlist_item_t *p_cur )
266 {
267     playlist_item_t *p_next = NULL;
268     stats_TimerStart( p_playlist, "Items array build",
269                       STATS_TIMER_PLAYLIST_BUILD );
270     PL_DEBUG("rebuilding array of current - root %s",
271               PLI_NAME(p_playlist->status.p_node) );
272     ARRAY_RESET(p_playlist->current);
273     p_playlist->i_current_index = -1;
274     while( 1 )
275     {
276         /** FIXME: this is *slow* */
277         p_next = playlist_GetNextLeaf( p_playlist,
278                                        p_playlist->status.p_node,
279                                        p_next, VLC_TRUE, VLC_FALSE );
280         if( p_next )
281         {
282             if( p_next == p_cur )
283                 p_playlist->i_current_index = p_playlist->current.i_size;
284             ARRAY_APPEND( p_playlist->current, p_next);
285         }
286         else break;
287     }
288     PL_DEBUG("rebuild done - %i items, index %i", p_playlist->current.i_size,
289                                                   p_playlist->i_current_index);
290     if( b_random )
291     {
292         /* Shuffle the array */
293         srand( (unsigned int)mdate() );
294         int swap = 0;
295         int j;
296         for( j = p_playlist->current.i_size - 1; j > 0; j-- )
297         {
298             swap++;
299             int i = rand() % (j+1); /* between 0 and j */
300             playlist_item_t *p_tmp;
301             p_tmp = ARRAY_VAL(p_playlist->current, i);
302             ARRAY_VAL(p_playlist->current,i) = ARRAY_VAL(p_playlist->current,j);
303             ARRAY_VAL(p_playlist->current,j) = p_tmp;
304         }
305     }
306     p_playlist->b_reset_currently_playing = VLC_FALSE;
307     stats_TimerStop( p_playlist, STATS_TIMER_PLAYLIST_BUILD );
308 }
309
310 /** This function calculates the next playlist item, depending
311  *  on the playlist course mode (forward, backward, random, view,...). */
312 playlist_item_t * playlist_NextItem( playlist_t *p_playlist )
313 {
314     playlist_item_t *p_new = NULL;
315     int i_skip = 0, i;
316
317     vlc_bool_t b_loop = var_GetBool( p_playlist, "loop" );
318     vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
319     vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
320     vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
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     /* Repeat and play/stop */
331     if( !p_playlist->request.b_request && b_repeat == VLC_TRUE &&
332          p_playlist->status.p_item )
333     {
334         msg_Dbg( p_playlist,"repeating item" );
335         return p_playlist->status.p_item;
336     }
337     if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
338     {
339         msg_Dbg( p_playlist,"stopping (play and stop)");
340         return NULL;
341     }
342
343     if( !p_playlist->request.b_request && p_playlist->status.p_item )
344     {
345         playlist_item_t *p_parent = p_playlist->status.p_item;
346         while( p_parent )
347         {
348             if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
349             {
350                 msg_Dbg( p_playlist, "blocking item, stopping") ;
351                 return NULL;
352             }
353             p_parent = p_parent->p_parent;
354         }
355     }
356
357     /* Start the real work */
358     if( p_playlist->request.b_request )
359     {
360         p_new = p_playlist->request.p_item;
361         i_skip = p_playlist->request.i_skip;
362         PL_DEBUG( "processing request item %s node %s skip %i",
363                         PLI_NAME( p_playlist->request.p_item ),
364                         PLI_NAME( p_playlist->request.p_node ), i_skip );
365
366         if( p_playlist->request.p_node &&
367             p_playlist->request.p_node != p_playlist->status.p_node )
368         {
369             p_playlist->status.p_node = p_playlist->request.p_node;
370             p_playlist->b_reset_currently_playing = VLC_TRUE;
371         }
372
373         /* If we are asked for a node, dont take it */
374         if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
375             i_skip++;
376
377         if( p_playlist->b_reset_currently_playing )
378             /* A bit too bad to reset twice ... */
379             ResetCurrentlyPlaying( p_playlist, b_random, p_new );
380         else if( p_new )
381             ResyncCurrentIndex( p_playlist, p_new );
382         else
383             p_playlist->i_current_index = -1;
384
385         if( p_playlist->current.i_size && i_skip > 0 )
386         {
387             for( i = i_skip; i > 0 ; i-- )
388             {
389                 p_playlist->i_current_index++;
390                 if( p_playlist->i_current_index == p_playlist->current.i_size )
391                 {
392                     PL_DEBUG( "looping - restarting at beginning of node" );
393                     p_playlist->i_current_index = 0;
394                 }
395             }
396             p_new = ARRAY_VAL( p_playlist->current,
397                                p_playlist->i_current_index );
398         }
399         else if( p_playlist->current.i_size && i_skip < 0 )
400         {
401             for( i = i_skip; i < 0 ; i++ )
402             {
403                 p_playlist->i_current_index--;
404                 if( p_playlist->i_current_index == -1 )
405                 {
406                     PL_DEBUG( "looping - restarting at end of node" );
407                     p_playlist->i_current_index = p_playlist->current.i_size-1;
408                 }
409             }
410             p_new = ARRAY_VAL( p_playlist->current,
411                                p_playlist->i_current_index );
412         }
413         /* Clear the request */
414         p_playlist->request.b_request = VLC_FALSE;
415     }
416     /* "Automatic" item change ( next ) */
417     else
418     {
419         PL_DEBUG( "changing item without a request (current %i/%i)",
420                   p_playlist->i_current_index, p_playlist->current.i_size );
421         /* Cant go to next from current item */
422         if( p_playlist->status.p_item &&
423             p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
424             return NULL;
425
426         if( p_playlist->b_reset_currently_playing )
427             ResetCurrentlyPlaying( p_playlist, b_random,
428                                    p_playlist->status.p_item );
429
430         p_playlist->i_current_index++;
431         if( p_playlist->i_current_index == p_playlist->current.i_size )
432         {
433             if( !b_loop || p_playlist->current.i_size == 0 ) return NULL;
434             p_playlist->i_current_index = 0;
435         }
436         PL_DEBUG( "using item %i", p_playlist->i_current_index );
437         if ( p_playlist->current.i_size == 0 ) return NULL;
438
439         p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
440         /* The new item can't be autoselected  */
441         if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
442             return NULL;
443     }
444     return p_new;
445 }
446
447 /** Start the input for an item */
448 int playlist_PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
449 {
450     vlc_value_t val;
451     int i_activity = var_GetInteger( p_playlist, "activity") ;
452
453     msg_Dbg( p_playlist, "creating new input thread" );
454
455     p_item->p_input->i_nb_played++;
456     p_playlist->status.p_item = p_item;
457
458     p_playlist->status.i_status = PLAYLIST_RUNNING;
459
460     var_SetInteger( p_playlist, "activity", i_activity +
461                     DEFAULT_INPUT_ACTIVITY );
462     p_playlist->p_input = input_CreateThread( p_playlist, p_item->p_input );
463
464     val.i_int = p_item->p_input->i_id;
465     /* unlock the playlist to set the var...mmm */
466     vlc_mutex_unlock( &p_playlist->object_lock);
467     var_Set( p_playlist, "playlist-current", val);
468     vlc_mutex_lock( &p_playlist->object_lock);
469
470     return VLC_SUCCESS;
471 }