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