]> git.sesse.net Git - vlc/blob - src/playlist/control.c
Fix CDDA and a corner-case in playlist handling
[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 /*****************************************************************************
29  * Local prototypes
30  *****************************************************************************/
31 int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args );
32
33 void PreparseEnqueueItemSub( playlist_t *, playlist_item_t * );
34
35 playlist_item_t *playlist_RecursiveFindLast(playlist_t *p_playlist,
36                                             playlist_item_t *p_node );
37
38 /*****************************************************************************
39  * Playlist control
40  *****************************************************************************/
41
42 /**
43  * Do a playlist action. Should be entered without playlist lock
44  * \see playlist_Control
45  */
46 int playlist_LockControl( playlist_t * p_playlist, int i_query, ... )
47 {
48     va_list args;
49     int i_result;
50     va_start( args, i_query );
51     vlc_mutex_lock( &p_playlist->object_lock );
52     i_result = PlaylistVAControl( p_playlist, i_query, args );
53     va_end( args );
54     vlc_mutex_unlock( &p_playlist->object_lock );
55     return i_result;
56 }
57
58 /**
59  * Do a playlist action.
60  * If there is something in the playlist then you can do playlist actions.
61  * Should be entered with playlist lock. See include/vlc_playlist.h for
62  * possible queries
63  *
64  * \param p_playlist the playlist to do the command on
65  * \param i_query the command to do
66  * \param variable number of arguments
67  * \return VLC_SUCCESS or an error
68  */
69 int playlist_Control( playlist_t * p_playlist, int i_query, ... )
70 {
71     va_list args;
72     int i_result;
73     va_start( args, i_query );
74     i_result = PlaylistVAControl( p_playlist, i_query, args );
75     va_end( args );
76
77     return i_result;
78 }
79
80 int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args )
81 {
82     int i_view;
83     playlist_item_t *p_item, *p_node;
84     vlc_value_t val;
85
86     if( p_playlist->i_size <= 0 )
87     {
88         return VLC_EGENERIC;
89     }
90
91     switch( i_query )
92     {
93     case PLAYLIST_STOP:
94         p_playlist->request.i_status = PLAYLIST_STOPPED;
95         p_playlist->request.b_request = VLC_TRUE;
96         p_playlist->request.p_item = NULL;
97         break;
98
99     case PLAYLIST_ITEMPLAY:
100         p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
101         if ( p_item == NULL || p_item->p_input->psz_uri == NULL )
102             return VLC_EGENERIC;
103         p_playlist->request.i_status = PLAYLIST_RUNNING;
104         p_playlist->request.i_skip = 0;
105         p_playlist->request.b_request = VLC_TRUE;
106         p_playlist->request.p_item = p_item;
107         p_playlist->request.p_node = p_playlist->status.p_node;
108         break;
109
110     case PLAYLIST_VIEWPLAY:
111         i_view = (int) va_arg( args, playlist_item_t *);
112         p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
113         p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
114         if ( p_node == NULL )
115         {
116             p_node = p_playlist->status.p_node;
117             if( !p_node )
118             {
119                 p_playlist->status.i_status = PLAYLIST_STOPPED;
120                 p_playlist->request.b_request = VLC_TRUE;
121                 msg_Err( p_playlist, "null node" );
122                 return VLC_SUCCESS;
123             }
124         }
125         p_playlist->request.i_status = PLAYLIST_RUNNING;
126         p_playlist->request.i_skip = 0;
127         p_playlist->request.b_request = VLC_TRUE;
128         p_playlist->request.p_node = p_node;
129         p_playlist->request.p_item = p_item;
130         break;
131
132     case PLAYLIST_PLAY:
133         p_playlist->request.i_status = PLAYLIST_RUNNING;
134         p_playlist->request.b_request = VLC_TRUE;
135
136         if( p_playlist->p_input )
137         {
138             val.i_int = PLAYING_S;
139             var_Set( p_playlist->p_input, "state", val );
140             break;
141         }
142         p_playlist->request.p_node = p_playlist->status.p_node;
143         p_playlist->request.p_item = p_playlist->status.p_item;
144         p_playlist->request.i_skip = 0;
145         break;
146
147     case PLAYLIST_AUTOPLAY:
148         p_playlist->status.i_status = PLAYLIST_RUNNING;
149         p_playlist->status.p_node = p_playlist->p_local_category;
150         p_playlist->request.b_request = VLC_FALSE;
151         break;
152
153     case PLAYLIST_PAUSE:
154         val.i_int = 0;
155         if( p_playlist->p_input )
156             var_Get( p_playlist->p_input, "state", &val );
157
158         if( val.i_int == PAUSE_S )
159         {
160             p_playlist->status.i_status = PLAYLIST_RUNNING;
161             if( p_playlist->p_input )
162             {
163                 val.i_int = PLAYING_S;
164                 var_Set( p_playlist->p_input, "state", val );
165             }
166         }
167         else
168         {
169             p_playlist->status.i_status = PLAYLIST_PAUSED;
170             if( p_playlist->p_input )
171             {
172                 val.i_int = PAUSE_S;
173                 var_Set( p_playlist->p_input, "state", val );
174             }
175         }
176         break;
177
178     case PLAYLIST_SKIP:
179         p_playlist->request.p_node = p_playlist->status.p_node;
180         p_playlist->request.p_item = p_playlist->status.p_item;
181         p_playlist->request.i_skip = (int) va_arg( args, int );
182         p_playlist->request.b_request = VLC_TRUE;
183         break;
184
185     default:
186         msg_Err( p_playlist, "unknown playlist query" );
187         return VLC_EBADVAR;
188         break;
189     }
190
191     return VLC_SUCCESS;
192 }
193
194 /*****************************************************************************
195  * Preparse control
196  *****************************************************************************/
197 /** Enqueue an item for preparsing */
198 int playlist_PreparseEnqueue( playlist_t *p_playlist,
199                               input_item_t *p_item )
200 {
201     vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
202     vlc_gc_incref( p_item );
203     INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
204                  p_playlist->p_preparse->i_waiting,
205                  p_playlist->p_preparse->i_waiting,
206                  p_item );
207     vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
208     return VLC_SUCCESS;
209 }
210
211 /** Enqueue a playlist item or a node for peparsing.
212  *  This function should be entered without playlist and preparser locks */
213 int playlist_PreparseEnqueueItem( playlist_t *p_playlist,
214                                   playlist_item_t *p_item )
215 {
216     vlc_mutex_lock( &p_playlist->object_lock );
217     vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
218     PreparseEnqueueItemSub( p_playlist, p_item );
219     vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
220     vlc_mutex_unlock( &p_playlist->object_lock );
221     return VLC_SUCCESS;
222 }
223
224 void PreparseEnqueueItemSub( playlist_t *p_playlist,
225                              playlist_item_t *p_item )
226 {
227     int i;
228     if( p_item->i_children == -1 )
229     {
230         vlc_gc_incref( p_item );
231         INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
232                      p_playlist->p_preparse->i_waiting,
233                      p_playlist->p_preparse->i_waiting,
234                      p_item->p_input );
235     }
236     else
237     {
238         for( i = 0; i < p_item->i_children; i++)
239         {
240             PreparseEnqueueItemSub( p_playlist,
241                                              p_item->pp_children[i] );
242         }
243     }
244 }
245
246 /*****************************************************************************
247  * Playback logic
248  *****************************************************************************/
249
250 /** This function calculates the next playlist item, depending
251  *  on the playlist course mode (forward, backward, random, view,...). */
252 playlist_item_t * playlist_NextItem( playlist_t *p_playlist )
253 {
254     playlist_item_t *p_new = NULL;
255     int i_skip,i;
256
257     vlc_bool_t b_loop = var_GetBool( p_playlist, "loop" );
258     vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
259     vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
260     vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
261
262     /* Handle quickly a few special cases */
263
264     /* No items to play */
265     if( p_playlist->i_size == 0 )
266     {
267         msg_Info( p_playlist, "playlist is empty" );
268         return NULL;
269     }
270
271     /* Repeat and play/stop */
272     if( !p_playlist->request.b_request && b_repeat == VLC_TRUE &&
273          p_playlist->status.p_item )
274     {
275         msg_Dbg( p_playlist,"repeating item" );
276         return p_playlist->status.p_item;
277     }
278     if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
279     {
280         msg_Dbg( p_playlist,"stopping (play and stop)");
281         return NULL;
282     }
283
284     if( !p_playlist->request.b_request && p_playlist->status.p_item &&
285          p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
286     {
287         msg_Dbg( p_playlist, "blocking item, stopping") ;
288         return NULL;
289     }
290
291     /* Random case. This is an exception: if request, but request is skip +- 1
292      * we don't go to next item but select a new random one. */
293     if( b_random )
294         msg_Err( p_playlist, "random unsupported" );
295 #if 0
296             &&
297         ( !p_playlist->request.b_request ||
298         ( p_playlist->request.b_request && ( p_playlist->request.p_item == NULL ||
299           p_playlist->request.i_skip == 1 || p_playlist->request.i_skip == -1 ) ) ) )
300     {
301         /* how many items to choose from ? */
302         i_count = 0;
303         for ( i = 0; i < p_playlist->i_size; i++ )
304         {
305             if ( p_playlist->pp_items[i]->p_input->i_nb_played == 0 )
306                 i_count++;
307         }
308         /* Nothing left? */
309         if ( i_count == 0 )
310         {
311             /* Don't loop? Exit! */
312             if( !b_loop )
313                 return NULL;
314             /* Otherwise reset the counter */
315             for ( i = 0; i < p_playlist->i_size; i++ )
316             {
317                 p_playlist->pp_items[i]->p_input->i_nb_played = 0;
318             }
319             i_count = p_playlist->i_size;
320         }
321         srand( (unsigned int)mdate() );
322         i = rand() % i_count + 1 ;
323         /* loop thru the list and count down the unplayed items to the selected one */
324         for ( i_new = 0; i_new < p_playlist->i_size && i > 0; i_new++ )
325         {
326             if ( p_playlist->pp_items[i_new]->p_input->i_nb_played == 0 )
327                 i--;
328         }
329         i_new--;
330
331         p_playlist->request.i_skip = 0;
332         p_playlist->request.b_request = VLC_FALSE;
333         return p_playlist->pp_items[i_new];
334     }
335 #endif
336
337     /* Start the real work */
338     if( p_playlist->request.b_request )
339     {
340         PL_DEBUG( "processing request" );
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                     PL_DEBUG( "looping - restarting at beginning of node" );
363                     p_new = playlist_GetNextLeaf( p_playlist,
364                                                   p_playlist->request.p_node,
365                                                   NULL );
366                     if( p_new == NULL ) break;
367                 }
368             }
369         }
370         else if( i_skip < 0 )
371         {
372             for( i = i_skip; i < 0 ; i++ )
373             {
374                 p_new = playlist_GetPrevLeaf( p_playlist,
375                                               p_playlist->request.p_node,
376                                               p_new );
377                 if( p_new == NULL )
378                 {
379                     PL_DEBUG( "looping - restarting at end of node" );
380                     /** \bug This is needed because GetPrevLeaf does not loop
381                       * by itself */
382                     p_new = playlist_GetLastLeaf( p_playlist,
383                                                  p_playlist->request.p_node );
384                 }
385                 if( p_new == NULL ) break;
386             }
387         }
388         /* Clear the request */
389         p_playlist->request.b_request = VLC_FALSE;
390     }
391     /* "Automatic" item change ( next ) */
392     else
393     {
394         PL_DEBUG( "changing item without a request" );
395         /* Cant go to next from current item */
396         if( p_playlist->status.p_item &&
397             p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
398             return NULL;
399
400         p_new = playlist_GetNextLeaf( p_playlist,
401                                       p_playlist->status.p_node,
402                                       p_playlist->status.p_item );
403         if( p_new == NULL && b_loop )
404         {
405             PL_DEBUG( "looping" );
406             p_new = playlist_GetNextLeaf( p_playlist,
407                                           p_playlist->status.p_node,
408                                           NULL );
409         }
410         /* The new item can't be autoselected  */
411         if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
412             return NULL;
413     }
414     if( p_new == NULL )
415     {
416         msg_Dbg( p_playlist, "did not find something to play" );
417     }
418     return p_new;
419 }
420
421 /** Start the input for an item */
422 int playlist_PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
423 {
424     vlc_value_t val;
425     int i_activity = var_GetInteger( p_playlist, "activity") ;
426
427     msg_Dbg( p_playlist, "creating new input thread" );
428
429     p_item->p_input->i_nb_played++;
430     p_playlist->status.p_item = p_item;
431
432     p_playlist->status.i_status = PLAYLIST_RUNNING;
433
434     var_SetInteger( p_playlist, "activity", i_activity +
435                     DEFAULT_INPUT_ACTIVITY );
436     p_playlist->p_input = input_CreateThread( p_playlist, p_item->p_input );
437
438     val.i_int = p_item->p_input->i_id;
439     /* unlock the playlist to set the var...mmm */
440     vlc_mutex_unlock( &p_playlist->object_lock);
441     var_Set( p_playlist, "playlist-current", val);
442     vlc_mutex_lock( &p_playlist->object_lock);
443
444     return VLC_SUCCESS;
445 }