]> git.sesse.net Git - vlc/blob - src/playlist/control.c
Some more (mostly) untested stuff:
[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->i_size <= 0 )
88     {
89         return VLC_EGENERIC;
90     }
91
92     switch( i_query )
93     {
94     case PLAYLIST_STOP:
95         p_playlist->request.i_status = PLAYLIST_STOPPED;
96         p_playlist->request.b_request = VLC_TRUE;
97         p_playlist->request.p_item = NULL;
98         break;
99
100     // Node can be null, it will keep the same. Use with care ...
101     // Item null = take the first child of node
102     case PLAYLIST_VIEWPLAY:
103         p_playlist->b_reset_random = VLC_TRUE;
104         p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
105         p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
106         if ( p_node == NULL )
107         {
108             p_node = p_playlist->status.p_node;
109             assert( p_node );
110         }
111         p_playlist->request.i_status = PLAYLIST_RUNNING;
112         p_playlist->request.i_skip = 0;
113         p_playlist->request.b_request = VLC_TRUE;
114         p_playlist->request.p_node = p_node;
115         p_playlist->request.p_item = p_item;
116         break;
117
118     case PLAYLIST_PLAY:
119         if( p_playlist->p_input )
120         {
121             val.i_int = PLAYING_S;
122             var_Set( p_playlist->p_input, "state", val );
123             break;
124         }
125         else
126         {
127             p_playlist->request.i_status = PLAYLIST_RUNNING;
128             p_playlist->request.b_request = VLC_TRUE;
129             p_playlist->request.p_node = p_playlist->status.p_node;
130             p_playlist->request.p_item = p_playlist->status.p_item;
131             p_playlist->request.i_skip = 0;
132         }
133         break;
134
135     case PLAYLIST_AUTOPLAY:
136         // AUTOPLAY is an ugly hack for initial status.
137         // Hopefully it will disappear
138         p_playlist->status.i_status = PLAYLIST_RUNNING;
139         p_playlist->request.p_node = p_playlist->status.p_node;
140         p_playlist->request.b_request = VLC_FALSE;
141         break;
142
143     case PLAYLIST_PAUSE:
144         val.i_int = 0;
145         if( p_playlist->p_input )
146             var_Get( p_playlist->p_input, "state", &val );
147
148         if( val.i_int == PAUSE_S )
149         {
150             p_playlist->status.i_status = PLAYLIST_RUNNING;
151             if( p_playlist->p_input )
152             {
153                 val.i_int = PLAYING_S;
154                 var_Set( p_playlist->p_input, "state", val );
155             }
156         }
157         else
158         {
159             p_playlist->status.i_status = PLAYLIST_PAUSED;
160             if( p_playlist->p_input )
161             {
162                 val.i_int = PAUSE_S;
163                 var_Set( p_playlist->p_input, "state", val );
164             }
165         }
166         break;
167
168     case PLAYLIST_SKIP:
169         p_playlist->request.p_node = p_playlist->status.p_node;
170         p_playlist->request.p_item = p_playlist->status.p_item;
171         p_playlist->request.i_skip = (int) va_arg( args, int );
172         /* if already running, keep running */
173         if( p_playlist->status.i_status != PLAYLIST_STOPPED )
174             p_playlist->request.i_status = p_playlist->status.i_status;
175         p_playlist->request.b_request = VLC_TRUE;
176         break;
177
178     default:
179         msg_Err( p_playlist, "unknown playlist query" );
180         return VLC_EBADVAR;
181         break;
182     }
183
184     return VLC_SUCCESS;
185 }
186
187 /*****************************************************************************
188  * Preparse control
189  *****************************************************************************/
190 /** Enqueue an item for preparsing */
191 int playlist_PreparseEnqueue( playlist_t *p_playlist,
192                               input_item_t *p_item )
193 {
194     vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
195     vlc_gc_incref( p_item );
196     INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
197                  p_playlist->p_preparse->i_waiting,
198                  p_playlist->p_preparse->i_waiting,
199                  p_item );
200     vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
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,
233                  p );
234     vlc_mutex_unlock( &p_playlist->p_secondary_preparse->object_lock );
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 /** This function calculates the next playlist item, depending
264  *  on the playlist course mode (forward, backward, random, view,...). */
265 playlist_item_t * playlist_NextItem( playlist_t *p_playlist )
266 {
267     playlist_item_t *p_new = NULL;
268     int i_skip = 0, i;
269
270     vlc_bool_t b_loop = var_GetBool( p_playlist, "loop" );
271     vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
272     vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
273     vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
274
275     /* Handle quickly a few special cases */
276     /* No items to play */
277     if( p_playlist->i_size == 0 )
278     {
279         msg_Info( p_playlist, "playlist is empty" );
280         return NULL;
281     }
282
283     /* Repeat and play/stop */
284     if( !p_playlist->request.b_request && b_repeat == VLC_TRUE &&
285          p_playlist->status.p_item )
286     {
287         msg_Dbg( p_playlist,"repeating item" );
288         return p_playlist->status.p_item;
289     }
290     if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
291     {
292         msg_Dbg( p_playlist,"stopping (play and stop)");
293         return NULL;
294     }
295
296     if( !p_playlist->request.b_request && p_playlist->status.p_item )
297     {
298         playlist_item_t *p_parent = p_playlist->status.p_item;
299         while( p_parent )
300         {
301             if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
302             {
303                 msg_Dbg( p_playlist, "blocking item, stopping") ;
304                 return NULL;
305             }
306             p_parent = p_parent->p_parent;
307         }
308     }
309
310     /* Random case. This is an exception: if request, but request is skip +- 1
311      * we don't go to next item but select a new random one. */
312     if( b_random &&
313         ( !p_playlist->request.b_request ||
314         ( p_playlist->request.b_request &&
315             ( p_playlist->request.p_item == NULL ||
316               p_playlist->request.i_skip == 1    ||
317               p_playlist->request.i_skip == -1 ) ) ) )
318     {
319        PL_DEBUG( "doing random, have %i items, currently at %i, reset %i\n",
320                         p_playlist->i_random, p_playlist->i_random_index,
321                         p_playlist->b_reset_random );
322        if( p_playlist->b_reset_random )
323         {
324             int j;
325             FREE( p_playlist->pp_random );
326             if( !p_playlist->b_reset_random &&  !b_loop ) goto end;
327             p_playlist->i_random = 0;
328             p_playlist->i_random_index = 0;
329             p_playlist->i_random = playlist_GetAllEnabledChildren(
330                                                 p_playlist,
331                                                 p_playlist->status.p_node,
332                                                 &p_playlist->pp_random );
333             /* Shuffle the array */
334             srand( (unsigned int)mdate() );
335             int swap = 0;
336             for( j = p_playlist->i_random -1; j > 0; j-- )
337             {
338                 swap++;
339                 int i = rand() % (j+1); /* between 0 and j */
340                 playlist_item_t *p_tmp;
341                 p_tmp = p_playlist->pp_random[i];
342                 p_playlist->pp_random[i] = p_playlist->pp_random[j];
343                 p_playlist->pp_random[j] = p_tmp;
344             }
345             p_playlist->b_reset_random = VLC_FALSE;
346             PL_DEBUG( "random rebuilt, have %i items", p_playlist->i_random );
347         }
348         else
349         {
350             /* Go backward or forward */
351             if( !p_playlist->request.b_request || !p_playlist->request.p_item ||
352                                                p_playlist->request.i_skip == 1 )
353                 p_playlist->i_random_index++;
354             else
355                 p_playlist->i_random_index--;
356             /* Handle bounds situations */
357             if( p_playlist->i_random_index == -1 )
358             {
359                 if( !b_loop || p_playlist->i_random == 0 ) goto end;
360                 p_playlist->i_random_index = p_playlist->i_random - 1;
361             }
362             else if( p_playlist->i_random_index == p_playlist->i_random )
363             {
364                 if( !b_loop || p_playlist->i_random == 0 ) goto end;
365                 p_playlist->i_random_index = 0;
366             }
367         }
368         PL_DEBUG( "using random item %i", p_playlist->i_random_index );
369         if ( p_playlist->i_random == 0 ) goto end; /* Can this happen ?? */
370         p_new = p_playlist->pp_random[p_playlist->i_random_index];
371 end:
372         if( !p_new ) p_playlist->b_reset_random = VLC_TRUE;
373         p_playlist->request.i_skip = 0;
374         p_playlist->request.b_request = VLC_FALSE;
375         return p_new;
376     }
377
378     /* Start the real work */
379     if( p_playlist->request.b_request )
380     {
381         p_new = p_playlist->request.p_item;
382         i_skip = p_playlist->request.i_skip;
383         PL_DEBUG( "processing request item %s node %s skip %i",
384                         PLI_NAME( p_playlist->request.p_item ),
385                         PLI_NAME( p_playlist->request.p_node ), i_skip );
386
387         if( p_playlist->request.p_node )
388             p_playlist->status.p_node = p_playlist->request.p_node;
389
390         /* If we are asked for a node, dont take it */
391         if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
392             i_skip++;
393
394         if( i_skip > 0 )
395         {
396             for( i = i_skip; i > 0 ; i-- )
397             {
398                 p_new = playlist_GetNextLeaf( p_playlist,
399                                               p_playlist->request.p_node,
400                                               p_new, VLC_TRUE, VLC_FALSE );
401                 if( p_new == NULL )
402                 {
403                     PL_DEBUG( "looping - restarting at beginning of node" );
404                     p_new = playlist_GetNextLeaf( p_playlist,
405                                                   p_playlist->request.p_node,
406                                                   NULL, VLC_TRUE, VLC_FALSE);
407                     if( p_new == NULL ) break;
408                 }
409             }
410         }
411         else if( i_skip < 0 )
412         {
413             for( i = i_skip; i < 0 ; i++ )
414             {
415                 p_new = playlist_GetPrevLeaf( p_playlist,
416                                               p_playlist->request.p_node,
417                                               p_new, VLC_FALSE, VLC_FALSE );
418                 if( p_new == NULL )
419                 {
420                     PL_DEBUG( "looping - restarting at end of node" );
421                     /** \bug This is needed because GetPrevLeaf does not loop
422                       * by itself */
423                     p_new = playlist_GetLastLeaf( p_playlist,
424                                                  p_playlist->request.p_node );
425                 }
426                 if( p_new == NULL ) break;
427             }
428         }
429         /* Clear the request */
430         p_playlist->request.b_request = VLC_FALSE;
431     }
432     /* "Automatic" item change ( next ) */
433     else
434     {
435         PL_DEBUG( "changing item without a request" );
436         /* Cant go to next from current item */
437         if( p_playlist->status.p_item &&
438             p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
439             return NULL;
440
441         p_new = playlist_GetNextLeaf( p_playlist,
442                                       p_playlist->status.p_node,
443                                       p_playlist->status.p_item,
444                                       VLC_TRUE, VLC_FALSE );
445         if( p_new == NULL && b_loop )
446         {
447             PL_DEBUG( "looping" );
448             p_new = playlist_GetNextLeaf( p_playlist,
449                                           p_playlist->status.p_node,
450                                           NULL, VLC_TRUE, VLC_FALSE );
451         }
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     if( p_new == NULL )
457     {
458         msg_Dbg( p_playlist, "did not find something to play" );
459     }
460     return p_new;
461 }
462
463 /** Start the input for an item */
464 int playlist_PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
465 {
466     vlc_value_t val;
467     int i_activity = var_GetInteger( p_playlist, "activity") ;
468
469     msg_Dbg( p_playlist, "creating new input thread" );
470
471     p_item->p_input->i_nb_played++;
472     p_playlist->status.p_item = p_item;
473
474     p_playlist->status.i_status = PLAYLIST_RUNNING;
475
476     var_SetInteger( p_playlist, "activity", i_activity +
477                     DEFAULT_INPUT_ACTIVITY );
478     p_playlist->p_input = input_CreateThread( p_playlist, p_item->p_input );
479
480     val.i_int = p_item->p_input->i_id;
481     /* unlock the playlist to set the var...mmm */
482     vlc_mutex_unlock( &p_playlist->object_lock);
483     var_Set( p_playlist, "playlist-current", val);
484     vlc_mutex_lock( &p_playlist->object_lock);
485
486     return VLC_SUCCESS;
487 }