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