]> git.sesse.net Git - vlc/blob - src/playlist/control.c
Merge back branch 0.8.6-playlist-vlm to trunk.
[vlc] / src / playlist / control.c
1 /*****************************************************************************
2  * control.c : Hanle 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
28 #define PLAYLIST_DEBUG 1
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     int i_view;
85     playlist_item_t *p_item, *p_node;
86     vlc_value_t val;
87
88     if( p_playlist->i_size <= 0 )
89     {
90         return VLC_EGENERIC;
91     }
92
93     switch( i_query )
94     {
95     case PLAYLIST_STOP:
96         p_playlist->request.i_status = PLAYLIST_STOPPED;
97         p_playlist->request.b_request = VLC_TRUE;
98         p_playlist->request.p_item = NULL;
99         break;
100
101     case PLAYLIST_ITEMPLAY:
102         p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
103         if ( p_item == NULL || p_item->p_input->psz_uri == NULL )
104             return VLC_EGENERIC;
105         p_playlist->request.i_status = PLAYLIST_RUNNING;
106         p_playlist->request.i_skip = 0;
107         p_playlist->request.b_request = VLC_TRUE;
108         p_playlist->request.p_item = p_item;
109         p_playlist->request.p_node = p_playlist->status.p_node;
110         break;
111
112     case PLAYLIST_VIEWPLAY:
113         i_view = (int) va_arg( args, playlist_item_t *);
114         p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
115         p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
116         if ( p_node == NULL )
117         {
118             p_playlist->status.i_status = PLAYLIST_STOPPED;
119             p_playlist->request.b_request = VLC_TRUE;
120             msg_Err( p_playlist, "null node" );
121             return VLC_SUCCESS;
122         }
123         p_playlist->request.i_status = PLAYLIST_RUNNING;
124         p_playlist->request.i_skip = 0;
125         p_playlist->request.b_request = VLC_TRUE;
126         p_playlist->request.p_node = p_node;
127         p_playlist->request.p_item = p_item;
128         break;
129
130     case PLAYLIST_PLAY:
131         p_playlist->request.i_status = PLAYLIST_RUNNING;
132         p_playlist->request.b_request = VLC_TRUE;
133
134         if( p_playlist->p_input )
135         {
136             val.i_int = PLAYING_S;
137             var_Set( p_playlist->p_input, "state", val );
138             break;
139         }
140         p_playlist->request.p_node = p_playlist->status.p_node;
141         p_playlist->request.p_item = p_playlist->status.p_item;
142         p_playlist->request.i_skip = 0;
143         break;
144
145     case PLAYLIST_AUTOPLAY:
146         p_playlist->status.i_status = PLAYLIST_RUNNING;
147         p_playlist->status.p_node = p_playlist->p_local_category;
148         p_playlist->request.b_request = VLC_FALSE;
149         break;
150
151     case PLAYLIST_PAUSE:
152         val.i_int = 0;
153         if( p_playlist->p_input )
154             var_Get( p_playlist->p_input, "state", &val );
155
156         if( val.i_int == PAUSE_S )
157         {
158             p_playlist->status.i_status = PLAYLIST_RUNNING;
159             if( p_playlist->p_input )
160             {
161                 val.i_int = PLAYING_S;
162                 var_Set( p_playlist->p_input, "state", val );
163             }
164         }
165         else
166         {
167             p_playlist->status.i_status = PLAYLIST_PAUSED;
168             if( p_playlist->p_input )
169             {
170                 val.i_int = PAUSE_S;
171                 var_Set( p_playlist->p_input, "state", val );
172             }
173         }
174         break;
175
176     case PLAYLIST_SKIP:
177         p_playlist->request.p_node = p_playlist->status.p_node;
178         p_playlist->request.p_item = p_playlist->status.p_item;
179         p_playlist->request.i_skip = (int) va_arg( args, int );
180         p_playlist->request.b_request = VLC_TRUE;
181         break;
182
183     default:
184         msg_Err( p_playlist, "unknown playlist query" );
185         return VLC_EBADVAR;
186         break;
187     }
188
189     return VLC_SUCCESS;
190 }
191
192 /*****************************************************************************
193  * Preparse control
194  *****************************************************************************/
195 /** Enqueue an item for preparsing */
196 int playlist_PreparseEnqueue( playlist_t *p_playlist,
197                               input_item_t *p_item )
198 {
199     vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
200     vlc_gc_incref( p_item );
201     INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
202                  p_playlist->p_preparse->i_waiting,
203                  p_playlist->p_preparse->i_waiting,
204                  p_item );
205     vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
206     return VLC_SUCCESS;
207 }
208
209 /** Enqueue a playlist item or a node for peparsing.
210  *  This function should be entered without playlist and preparser locks */
211 int playlist_PreparseEnqueueItem( playlist_t *p_playlist,
212                                   playlist_item_t *p_item )
213 {
214     vlc_mutex_lock( &p_playlist->object_lock );
215     vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
216     PreparseEnqueueItemSub( p_playlist, p_item );
217     vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
218     vlc_mutex_unlock( &p_playlist->object_lock );
219     return VLC_SUCCESS;
220 }
221
222 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,
239                                              p_item->pp_children[i] );
240         }
241     }
242 }
243
244 /*****************************************************************************
245  * Playback logic
246  *****************************************************************************/
247
248 /** This function calculates the next playlist item, depending
249  *  on the playlist course mode (forward, backward, random, view,...). */
250 playlist_item_t * playlist_NextItem( playlist_t *p_playlist )
251 {
252     playlist_item_t *p_new = NULL;
253     int i_skip,i;
254
255     vlc_bool_t b_loop = var_GetBool( p_playlist, "loop" );
256     vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
257     vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
258     vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
259
260     /* Handle quickly a few special cases */
261
262     /* No items to play */
263     if( p_playlist->i_size == 0 )
264     {
265         msg_Info( p_playlist, "playlist is empty" );
266         return NULL;
267     }
268
269     /* Repeat and play/stop */
270     if( !p_playlist->request.b_request && b_repeat == VLC_TRUE &&
271          p_playlist->status.p_item )
272     {
273         msg_Dbg( p_playlist,"repeating item" );
274         return p_playlist->status.p_item;
275     }
276     if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
277     {
278         msg_Dbg( p_playlist,"stopping (play and stop)");
279         return NULL;
280     }
281
282     if( !p_playlist->request.b_request && p_playlist->status.p_item &&
283          p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
284     {
285         msg_Dbg( p_playlist, "blocking item, stopping") ;
286         return NULL;
287     }
288
289     /* Random case. This is an exception: if request, but request is skip +- 1
290      * we don't go to next item but select a new random one. */
291     if( b_random )
292         msg_Err( p_playlist, "random unsupported" );
293 #if 0
294             &&
295         ( !p_playlist->request.b_request ||
296         ( p_playlist->request.b_request && ( p_playlist->request.p_item == NULL ||
297           p_playlist->request.i_skip == 1 || p_playlist->request.i_skip == -1 ) ) ) )
298     {
299         /* how many items to choose from ? */
300         i_count = 0;
301         for ( i = 0; i < p_playlist->i_size; i++ )
302         {
303             if ( p_playlist->pp_items[i]->p_input->i_nb_played == 0 )
304                 i_count++;
305         }
306         /* Nothing left? */
307         if ( i_count == 0 )
308         {
309             /* Don't loop? Exit! */
310             if( !b_loop )
311                 return NULL;
312             /* Otherwise reset the counter */
313             for ( i = 0; i < p_playlist->i_size; i++ )
314             {
315                 p_playlist->pp_items[i]->p_input->i_nb_played = 0;
316             }
317             i_count = p_playlist->i_size;
318         }
319         srand( (unsigned int)mdate() );
320         i = rand() % i_count + 1 ;
321         /* loop thru the list and count down the unplayed items to the selected one */
322         for ( i_new = 0; i_new < p_playlist->i_size && i > 0; i_new++ )
323         {
324             if ( p_playlist->pp_items[i_new]->p_input->i_nb_played == 0 )
325                 i--;
326         }
327         i_new--;
328
329         p_playlist->request.i_skip = 0;
330         p_playlist->request.b_request = VLC_FALSE;
331         return p_playlist->pp_items[i_new];
332     }
333 #endif
334
335     /* Start the real work */
336     if( p_playlist->request.b_request )
337     {
338 #ifdef PLAYLIST_DEBUG
339         msg_Dbg( p_playlist,"processing request" );
340 #endif
341         p_new = p_playlist->request.p_item;
342         i_skip = p_playlist->request.i_skip;
343
344         p_playlist->status.p_node = p_playlist->request.p_node;
345
346         /* If we are asked for a node, take its first item */
347         if( i_skip == 0 &&
348               ( p_new == NULL || p_new->i_children != -1 ) )
349         {
350             i_skip++;
351         }
352
353         if( i_skip > 0 )
354         {
355             for( i = i_skip; i > 0 ; i-- )
356             {
357                 p_new = playlist_GetNextEnabledLeaf( p_playlist,
358                                                      p_playlist->request.p_node,
359                                                      p_new );
360                 if( p_new == NULL )
361                 {
362 #ifdef PLAYLIST_DEBUG
363                     msg_Dbg( p_playlist, "looping - restarting at beginning "
364                                          "of node" );
365 #endif
366                     p_new = playlist_GetNextLeaf( p_playlist,
367                                                   p_playlist->request.p_node,
368                                                   NULL );
369                     if( p_new == NULL ) break;
370                 }
371             }
372         }
373         else if( i_skip < 0 )
374         {
375             for( i = i_skip; i < 0 ; i++ )
376             {
377                 p_new = playlist_GetPrevLeaf( p_playlist,
378                                               p_playlist->request.p_node,
379                                               p_new );
380                 if( p_new == NULL )
381                 {
382 #ifdef PLAYLIST_DEBUG
383                     msg_Dbg( p_playlist, "looping - restarting at end "
384                                          "of node" );
385 #endif
386                     /** \bug This is needed because GetPrevLeaf does not loop
387                       * by itself */
388                     p_new = playlist_GetLastLeaf( p_playlist,
389                                                  p_playlist->request.p_node );
390                 }
391                 if( p_new == NULL ) break;
392             }
393         }
394         /* Clear the request */
395         p_playlist->request.b_request = VLC_FALSE;
396     }
397     /* "Automatic" item change ( next ) */
398     else
399     {
400 #ifdef PLAYLIST_DEBUG
401         msg_Dbg( p_playlist,"changing item without a request" );
402 #endif
403         /* Cant go to next from current item */
404         if( p_playlist->status.p_item &&
405             p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
406             return NULL;
407
408         p_new = playlist_GetNextLeaf( p_playlist,
409                                       p_playlist->status.p_node,
410                                       p_playlist->status.p_item );
411         if( p_new == NULL && b_loop )
412         {
413 #ifdef PLAYLIST_DEBUG
414             msg_Dbg( p_playlist, "looping" );
415 #endif
416             p_new = playlist_GetNextLeaf( p_playlist,
417                                           p_playlist->status.p_node,
418                                           NULL );
419         }
420         /* The new item can't be autoselected  */
421         if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
422             return NULL;
423     }
424     if( p_new == NULL )
425     {
426         msg_Dbg( p_playlist, "did not find something to play" );
427     }
428     return p_new;
429 }
430
431 /** Start the input for an item */
432 int playlist_PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
433 {
434     vlc_value_t val;
435     int i_activity = var_GetInteger( p_playlist, "activity") ;
436
437     msg_Dbg( p_playlist, "creating new input thread" );
438
439     p_item->p_input->i_nb_played++;
440     p_playlist->status.p_item = p_item;
441
442     p_playlist->status.i_status = PLAYLIST_RUNNING;
443
444     var_SetInteger( p_playlist, "activity", i_activity +
445                     DEFAULT_INPUT_ACTIVITY );
446     p_playlist->p_input = input_CreateThread( p_playlist, p_item->p_input );
447
448     val.i_int = p_item->p_input->i_id;
449     /* unlock the playlist to set the var...mmm */
450     vlc_mutex_unlock( &p_playlist->object_lock);
451     var_Set( p_playlist, "playlist-current", val);
452     vlc_mutex_lock( &p_playlist->object_lock);
453
454     return VLC_SUCCESS;
455 }