playlist: Properly clean the playlist using the object destructor.
[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$
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_PAUSE:
126         val.i_int = 0;
127         if( p_playlist->p_input )
128             var_Get( p_playlist->p_input, "state", &val );
129
130         if( val.i_int == PAUSE_S )
131         {
132             p_playlist->status.i_status = PLAYLIST_RUNNING;
133             if( p_playlist->p_input )
134             {
135                 val.i_int = PLAYING_S;
136                 var_Set( p_playlist->p_input, "state", val );
137             }
138         }
139         else
140         {
141             p_playlist->status.i_status = PLAYLIST_PAUSED;
142             if( p_playlist->p_input )
143             {
144                 val.i_int = PAUSE_S;
145                 var_Set( p_playlist->p_input, "state", val );
146             }
147         }
148         break;
149
150     case PLAYLIST_SKIP:
151         p_playlist->request.p_node = p_playlist->status.p_node;
152         p_playlist->request.p_item = p_playlist->status.p_item;
153         p_playlist->request.i_skip = (int) va_arg( args, int );
154         /* if already running, keep running */
155         if( p_playlist->status.i_status != PLAYLIST_STOPPED )
156             p_playlist->request.i_status = p_playlist->status.i_status;
157         p_playlist->request.b_request = VLC_TRUE;
158         break;
159
160     default:
161         msg_Err( p_playlist, "unknown playlist query" );
162         return VLC_EBADVAR;
163         break;
164     }
165     vlc_cond_signal( &p_playlist->object_wait );
166
167     return VLC_SUCCESS;
168 }
169
170 /*****************************************************************************
171  * Preparse control
172  *****************************************************************************/
173 /** Enqueue an item for preparsing */
174 int playlist_PreparseEnqueue( playlist_t *p_playlist,
175                               input_item_t *p_item )
176 {
177     vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
178     vlc_gc_incref( p_item );
179     INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
180                  p_playlist->p_preparse->i_waiting,
181                  p_playlist->p_preparse->i_waiting,
182                  p_item );
183     vlc_cond_signal( &p_playlist->p_preparse->object_wait );
184     vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
185     return VLC_SUCCESS;
186 }
187
188 /** Enqueue a playlist item or a node for peparsing.
189  *  This function should be entered without playlist and preparser locks */
190 int playlist_PreparseEnqueueItem( playlist_t *p_playlist,
191                                   playlist_item_t *p_item )
192 {
193     vlc_mutex_lock( &p_playlist->object_lock );
194     vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
195     PreparseEnqueueItemSub( p_playlist, p_item );
196     vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
197     vlc_mutex_unlock( &p_playlist->object_lock );
198     return VLC_SUCCESS;
199 }
200
201 int playlist_AskForArtEnqueue( playlist_t *p_playlist,
202                                input_item_t *p_item )
203 {
204     int i;
205     preparse_item_t p;
206     p.p_item = p_item;
207     p.b_fetch_art = VLC_TRUE;
208
209     vlc_object_lock( p_playlist->p_fetcher );
210     for( i = 0; i < p_playlist->p_fetcher->i_waiting &&
211          p_playlist->p_fetcher->p_waiting->b_fetch_art == VLC_TRUE;
212          i++ );
213     vlc_gc_incref( p_item );
214     INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
215                  p_playlist->p_fetcher->i_waiting,
216                  i, p );
217     vlc_object_signal_unlocked( p_playlist->p_fetcher );
218     vlc_object_unlock( p_playlist->p_fetcher );
219     return VLC_SUCCESS;
220 }
221
222 static void PreparseEnqueueItemSub( playlist_t *p_playlist,
223                                     playlist_item_t *p_item )
224 {
225     int i;
226     if( p_item->i_children == -1 )
227     {
228         vlc_gc_incref( p_item );
229         INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
230                      p_playlist->p_preparse->i_waiting,
231                      p_playlist->p_preparse->i_waiting,
232                      p_item->p_input );
233     }
234     else
235     {
236         for( i = 0; i < p_item->i_children; i++)
237         {
238             PreparseEnqueueItemSub( p_playlist, p_item->pp_children[i] );
239         }
240     }
241 }
242
243 /*****************************************************************************
244  * Playback logic
245  *****************************************************************************/
246
247 /**
248  * Synchronise the current index of the playlist
249  * to match the index of the current item.
250  *
251  * \param p_playlist the playlist structure
252  * \param p_cur the current playlist item
253  * \return nothing
254  */
255 static void ResyncCurrentIndex( playlist_t *p_playlist, playlist_item_t *p_cur )
256 {
257      PL_DEBUG( "resyncing on %s", PLI_NAME( p_cur ) );
258      /* Simply resync index */
259      int i;
260      p_playlist->i_current_index = -1;
261      for( i = 0 ; i< p_playlist->current.i_size; i++ )
262      {
263           if( ARRAY_VAL( p_playlist->current, i ) == p_cur )
264           {
265               p_playlist->i_current_index = i;
266               break;
267           }
268      }
269      PL_DEBUG( "%s is at %i", PLI_NAME( p_cur ), p_playlist->i_current_index );
270 }
271
272 void ResetCurrentlyPlaying( playlist_t *p_playlist, vlc_bool_t b_random,
273                             playlist_item_t *p_cur )
274 {
275     playlist_item_t *p_next = NULL;
276     stats_TimerStart( p_playlist, "Items array build",
277                       STATS_TIMER_PLAYLIST_BUILD );
278     PL_DEBUG( "rebuilding array of current - root %s",
279               PLI_NAME( p_playlist->status.p_node ) );
280     ARRAY_RESET( p_playlist->current );
281     p_playlist->i_current_index = -1;
282     while( 1 )
283     {
284         /** FIXME: this is *slow* */
285         p_next = playlist_GetNextLeaf( p_playlist,
286                                        p_playlist->status.p_node,
287                                        p_next, VLC_TRUE, VLC_FALSE );
288         if( p_next )
289         {
290             if( p_next == p_cur )
291                 p_playlist->i_current_index = p_playlist->current.i_size;
292             ARRAY_APPEND( p_playlist->current, p_next);
293         }
294         else break;
295     }
296     PL_DEBUG("rebuild done - %i items, index %i", p_playlist->current.i_size,
297                                                   p_playlist->i_current_index);
298     if( b_random )
299     {
300         /* Shuffle the array */
301         srand( (unsigned int)mdate() );
302         int j;
303         for( j = p_playlist->current.i_size - 1; j > 0; j-- )
304         {
305             int i = rand() % (j+1); /* between 0 and j */
306             playlist_item_t *p_tmp;
307             /* swap the two items */
308             p_tmp = ARRAY_VAL(p_playlist->current, i);
309             ARRAY_VAL(p_playlist->current,i) = ARRAY_VAL(p_playlist->current,j);
310             ARRAY_VAL(p_playlist->current,j) = p_tmp;
311         }
312     }
313     p_playlist->b_reset_currently_playing = VLC_FALSE;
314     stats_TimerStop( p_playlist, STATS_TIMER_PLAYLIST_BUILD );
315 }
316
317 /**
318  * Compute the next playlist item depending on
319  * the playlist course mode (forward, backward, random, view,...).
320  *
321  * \param p_playlist the playlist object
322  * \return nothing
323  */
324 playlist_item_t * playlist_NextItem( playlist_t *p_playlist )
325 {
326     playlist_item_t *p_new = NULL;
327     int i_skip = 0, i;
328
329     vlc_bool_t b_loop = var_GetBool( p_playlist, "loop" );
330     vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
331     vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
332     vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
333
334     /* Handle quickly a few special cases */
335     /* No items to play */
336     if( p_playlist->items.i_size == 0 )
337     {
338         msg_Info( p_playlist, "playlist is empty" );
339         return NULL;
340     }
341
342     /* Repeat and play/stop */
343     if( !p_playlist->request.b_request && b_repeat == VLC_TRUE &&
344          p_playlist->status.p_item )
345     {
346         msg_Dbg( p_playlist,"repeating item" );
347         return p_playlist->status.p_item;
348     }
349     if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
350     {
351         msg_Dbg( p_playlist,"stopping (play and stop)" );
352         return NULL;
353     }
354
355     if( !p_playlist->request.b_request && p_playlist->status.p_item )
356     {
357         playlist_item_t *p_parent = p_playlist->status.p_item;
358         while( p_parent )
359         {
360             if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
361             {
362                 msg_Dbg( p_playlist, "blocking item, stopping") ;
363                 return NULL;
364             }
365             p_parent = p_parent->p_parent;
366         }
367     }
368
369     /* Start the real work */
370     if( p_playlist->request.b_request )
371     {
372         p_new = p_playlist->request.p_item;
373         i_skip = p_playlist->request.i_skip;
374         PL_DEBUG( "processing request item %s node %s skip %i",
375                         PLI_NAME( p_playlist->request.p_item ),
376                         PLI_NAME( p_playlist->request.p_node ), i_skip );
377
378         if( p_playlist->request.p_node &&
379             p_playlist->request.p_node != p_playlist->status.p_node )
380         {
381             p_playlist->status.p_node = p_playlist->request.p_node;
382             p_playlist->b_reset_currently_playing = VLC_TRUE;
383         }
384
385         /* If we are asked for a node, don't take it */
386         if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
387             i_skip++;
388
389         if( p_playlist->b_reset_currently_playing )
390             /* A bit too bad to reset twice ... */
391             ResetCurrentlyPlaying( p_playlist, b_random, p_new );
392         else if( p_new )
393             ResyncCurrentIndex( p_playlist, p_new );
394         else
395             p_playlist->i_current_index = -1;
396
397         if( p_playlist->current.i_size && (i_skip > 0) )
398         {
399             if( p_playlist->i_current_index < -1 )
400                 p_playlist->i_current_index = -1;
401             for( i = i_skip; i > 0 ; i-- )
402             {
403                 p_playlist->i_current_index++;
404                 if( p_playlist->i_current_index >= p_playlist->current.i_size )
405                 {
406                     PL_DEBUG( "looping - restarting at beginning of node" );
407                     p_playlist->i_current_index = 0;
408                 }
409             }
410             p_new = ARRAY_VAL( p_playlist->current,
411                                p_playlist->i_current_index );
412         }
413         else if( p_playlist->current.i_size && (i_skip < 0) )
414         {
415             for( i = i_skip; i < 0 ; i++ )
416             {
417                 p_playlist->i_current_index--;
418                 if( p_playlist->i_current_index <= -1 )
419                 {
420                     PL_DEBUG( "looping - restarting at end of node" );
421                     p_playlist->i_current_index = p_playlist->current.i_size-1;
422                 }
423             }
424             p_new = ARRAY_VAL( p_playlist->current,
425                                p_playlist->i_current_index );
426         }
427         /* Clear the request */
428         p_playlist->request.b_request = VLC_FALSE;
429     }
430     /* "Automatic" item change ( next ) */
431     else
432     {
433         PL_DEBUG( "changing item without a request (current %i/%i)",
434                   p_playlist->i_current_index, p_playlist->current.i_size );
435         /* Cant go to next from current item */
436         if( p_playlist->status.p_item &&
437             p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
438             return NULL;
439
440         if( p_playlist->b_reset_currently_playing )
441             ResetCurrentlyPlaying( p_playlist, b_random,
442                                    p_playlist->status.p_item );
443
444         p_playlist->i_current_index++;
445         if( p_playlist->i_current_index == p_playlist->current.i_size )
446         {
447             if( !b_loop || p_playlist->current.i_size == 0 ) return NULL;
448             p_playlist->i_current_index = 0;
449         }
450         PL_DEBUG( "using item %i", p_playlist->i_current_index );
451         if ( p_playlist->current.i_size == 0 ) return NULL;
452
453         p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
454         /* The new item can't be autoselected  */
455         if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
456             return NULL;
457     }
458     return p_new;
459 }
460
461 /**
462  * Start the input for an item
463  *
464  * \param p_playlist the playlist objetc
465  * \param p_item the item to play
466  * \return nothing
467  */
468 int playlist_PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
469 {
470     vlc_value_t val;
471     input_item_t *p_input = p_item->p_input;
472     int i_activity = var_GetInteger( p_playlist, "activity" ) ;
473
474     msg_Dbg( p_playlist, "creating new input thread" );
475
476     p_input->i_nb_played++;
477     p_playlist->status.p_item = p_item;
478
479     p_playlist->status.i_status = PLAYLIST_RUNNING;
480
481     var_SetInteger( p_playlist, "activity", i_activity +
482                     DEFAULT_INPUT_ACTIVITY );
483     p_playlist->p_input = input_CreateThread( p_playlist, p_input );
484
485     char *psz_uri = input_item_GetURI( p_item->p_input );
486     if( psz_uri && ( !strncmp( psz_uri, "directory:", 10 ) ||
487                      !strncmp( psz_uri, "vlc:", 4 ) ) )
488     {
489         free( psz_uri );
490         return VLC_SUCCESS;
491     }
492     free( psz_uri );
493
494     if( p_playlist->p_fetcher &&
495             p_playlist->p_fetcher->i_art_policy == ALBUM_ART_WHEN_PLAYED )
496     {
497         vlc_bool_t b_has_art;
498
499         char *psz_arturl, *psz_name;
500         psz_arturl = input_item_GetArtURL( p_input );
501         psz_name = input_item_GetName( p_input );
502
503         /* p_input->p_meta should not be null after a successfull CreateThread */
504         b_has_art = !EMPTY_STR( psz_arturl );
505
506         if( !b_has_art || strncmp( psz_arturl, "attachment://", 13 ) )
507         {
508             PL_DEBUG( "requesting art for %s", psz_name );
509             playlist_AskForArtEnqueue( p_playlist, p_input );
510         }
511         free( psz_arturl );
512         free( psz_name );
513     }
514
515     val.i_int = p_input->i_id;
516     PL_UNLOCK;
517     var_Set( p_playlist, "playlist-current", val );
518     PL_LOCK;
519
520     return VLC_SUCCESS;
521 }