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