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