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