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