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