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