VT: only use hw decoding unless explicitely requested
[vlc.git] / src / playlist / thread.c
blob076260fb2987dea15757474d1a09682643d98e1a
1 /*****************************************************************************
2 * thread.c : Playlist management functions
3 *****************************************************************************
4 * Copyright © 1999-2008 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Clément Stenac <zorglub@videolan.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <assert.h>
30 #include <vlc_common.h>
31 #include <vlc_es.h>
32 #include <vlc_input.h>
33 #include <vlc_interface.h>
34 #include <vlc_playlist.h>
35 #include <vlc_rand.h>
36 #include <vlc_renderer_discovery.h>
37 #include "playlist_internal.h"
39 /*****************************************************************************
40 * Local prototypes
41 *****************************************************************************/
42 static void *Thread ( void * );
44 /*****************************************************************************
45 * Main functions for the global thread
46 *****************************************************************************/
48 /**
49 * Creates the main playlist thread.
51 void playlist_Activate( playlist_t *p_playlist )
53 playlist_private_t *p_sys = pl_priv(p_playlist);
55 if( vlc_clone( &p_sys->thread, Thread, p_playlist,
56 VLC_THREAD_PRIORITY_LOW ) )
58 msg_Err( p_playlist, "cannot spawn playlist thread" );
59 abort();
63 /**
64 * Stops the playlist forever (but do not destroy it yet).
65 * Any input is stopped.
66 * \return Nothing but waits for the playlist to be deactivated.
68 void playlist_Deactivate( playlist_t *p_playlist )
70 playlist_private_t *p_sys = pl_priv(p_playlist);
72 PL_LOCK;
73 /* WARNING: There is a latent bug. It is assumed that only one thread will
74 * be waiting for playlist deactivation at a time. So far, that works
75 * as playlist_Deactivate() is only ever called while closing an
76 * interface and interfaces are shut down serially by intf_DestroyAll(). */
77 if( p_sys->killed )
79 PL_UNLOCK;
80 return;
83 msg_Dbg( p_playlist, "deactivating the playlist" );
84 p_sys->killed = true;
85 vlc_cond_signal( &p_sys->signal );
86 PL_UNLOCK;
88 vlc_join( p_sys->thread, NULL );
91 /* */
93 /* Input Callback */
94 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
95 vlc_value_t oldval, vlc_value_t newval, void *p_data )
97 VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
98 playlist_t *p_playlist = p_data;
100 if( newval.i_int != INPUT_EVENT_STATE &&
101 newval.i_int != INPUT_EVENT_DEAD )
102 return VLC_SUCCESS;
104 PL_LOCK;
106 /* XXX: signaling while not changing any parameter... suspicious... */
107 vlc_cond_signal( &pl_priv(p_playlist)->signal );
109 PL_UNLOCK;
110 return VLC_SUCCESS;
114 * Synchronise the current index of the playlist
115 * to match the index of the current item.
117 * \param p_playlist the playlist structure
118 * \param p_cur the current playlist item
119 * \return nothing
121 void ResyncCurrentIndex( playlist_t *p_playlist, playlist_item_t *p_cur )
123 PL_ASSERT_LOCKED;
125 PL_DEBUG( "resyncing on %s", PLI_NAME( p_cur ) );
126 /* Simply resync index */
127 int i;
128 p_playlist->i_current_index = -1;
129 for( i = 0 ; i< p_playlist->current.i_size; i++ )
131 if( ARRAY_VAL( p_playlist->current, i ) == p_cur )
133 p_playlist->i_current_index = i;
134 break;
137 PL_DEBUG( "%s is at %i", PLI_NAME( p_cur ), p_playlist->i_current_index );
141 * Reset the currently playing playlist.
143 * \param p_playlist the playlist structure
144 * \param p_cur the current playlist item
145 * \return nothing
147 void ResetCurrentlyPlaying( playlist_t *p_playlist,
148 playlist_item_t *p_cur )
150 playlist_private_t *p_sys = pl_priv(p_playlist);
152 PL_DEBUG( "rebuilding array of current - root %s",
153 PLI_NAME( p_sys->status.p_node ) );
154 ARRAY_RESET( p_playlist->current );
155 p_playlist->i_current_index = -1;
156 for( playlist_item_t *p_next = NULL; ; )
158 /** FIXME: this is *slow* */
159 p_next = playlist_GetNextLeaf( p_playlist,
160 p_sys->status.p_node,
161 p_next, true, false );
162 if( !p_next )
163 break;
165 if( p_next == p_cur )
166 p_playlist->i_current_index = p_playlist->current.i_size;
167 ARRAY_APPEND( p_playlist->current, p_next);
169 PL_DEBUG("rebuild done - %i items, index %i", p_playlist->current.i_size,
170 p_playlist->i_current_index);
172 if( var_GetBool( p_playlist, "random" ) && ( p_playlist->current.i_size > 0 ) )
174 /* Shuffle the array */
175 for( unsigned j = p_playlist->current.i_size - 1; j > 0; j-- )
177 unsigned i = vlc_lrand48() % (j+1); /* between 0 and j */
178 playlist_item_t *p_tmp;
179 /* swap the two items */
180 p_tmp = ARRAY_VAL(p_playlist->current, i);
181 ARRAY_VAL(p_playlist->current,i) = ARRAY_VAL(p_playlist->current,j);
182 ARRAY_VAL(p_playlist->current,j) = p_tmp;
185 p_sys->b_reset_currently_playing = false;
190 * Start the input for an item
192 * \param p_playlist the playlist object
193 * \param p_item the item to play
195 static bool PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
197 playlist_private_t *p_sys = pl_priv(p_playlist);
198 input_item_t *p_input = p_item->p_input;
199 vlc_renderer_item_t *p_renderer;
201 PL_ASSERT_LOCKED;
203 msg_Dbg( p_playlist, "creating new input thread" );
205 p_item->i_nb_played++;
206 set_current_status_item( p_playlist, p_item );
207 p_renderer = p_sys->p_renderer;
208 /* Retain the renderer now to avoid it to be released by
209 * playlist_SetRenderer when we exit the locked scope. If the last reference
210 * was to be released, we would use a dangling pointer */
211 if( p_renderer )
212 vlc_renderer_item_hold( p_renderer );
213 assert( p_sys->p_input == NULL );
214 PL_UNLOCK;
216 libvlc_MetadataCancel( p_playlist->obj.libvlc, p_item );
218 input_thread_t *p_input_thread = input_Create( p_playlist, p_input, NULL,
219 p_sys->p_input_resource,
220 p_renderer );
221 if( likely(p_input_thread != NULL) )
223 var_AddCallback( p_input_thread, "intf-event",
224 InputEvent, p_playlist );
226 if( input_Start( p_input_thread ) )
228 var_DelCallback( p_input_thread, "intf-event",
229 InputEvent, p_playlist );
230 vlc_object_release( p_input_thread );
231 p_input_thread = NULL;
235 /* TODO store art policy in playlist private data */
236 char *psz_arturl = input_item_GetArtURL( p_input );
237 /* p_input->p_meta should not be null after a successful CreateThread */
238 bool b_has_art = !EMPTY_STR( psz_arturl );
240 if( !b_has_art || strncmp( psz_arturl, "attachment://", 13 ) )
242 PL_DEBUG( "requesting art for new input thread" );
243 libvlc_ArtRequest( p_playlist->obj.libvlc, p_input, META_REQUEST_OPTION_NONE );
245 free( psz_arturl );
247 PL_LOCK;
248 p_sys->p_input = p_input_thread;
249 PL_UNLOCK;
251 var_SetAddress( p_playlist, "input-current", p_input_thread );
253 PL_LOCK;
254 return p_input_thread != NULL;
258 * Compute the next playlist item depending on
259 * the playlist course mode (forward, backward, random, view,...).
261 * \param p_playlist the playlist object
262 * \return nothing
264 static playlist_item_t *NextItem( playlist_t *p_playlist )
266 playlist_private_t *p_sys = pl_priv(p_playlist);
267 playlist_item_t *p_new = NULL;
268 bool requested = p_sys->request.b_request;
270 /* Clear the request */
271 p_sys->request.b_request = false;
273 /* Handle quickly a few special cases */
274 /* No items to play */
275 if( p_playlist->items.i_size == 0 )
277 msg_Info( p_playlist, "playlist is empty" );
278 return NULL;
281 /* Start the real work */
282 if( requested )
284 p_new = p_sys->request.p_item;
286 if( p_new == NULL && p_sys->request.p_node == NULL )
287 return NULL; /* Stop request! */
289 int i_skip = p_sys->request.i_skip;
290 PL_DEBUG( "processing request item: %s, node: %s, skip: %i",
291 PLI_NAME( p_sys->request.p_item ),
292 PLI_NAME( p_sys->request.p_node ), i_skip );
294 if( p_sys->request.p_node &&
295 p_sys->request.p_node != get_current_status_node( p_playlist ) )
298 set_current_status_node( p_playlist, p_sys->request.p_node );
299 p_sys->request.p_node = NULL;
300 p_sys->b_reset_currently_playing = true;
303 /* If we are asked for a node, go to it's first child */
304 if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
306 i_skip++;
307 if( p_new != NULL )
309 p_new = playlist_GetNextLeaf( p_playlist, p_new, NULL, true, false );
310 for( int i = 0; i < p_playlist->current.i_size; i++ )
312 if( p_new == ARRAY_VAL( p_playlist->current, i ) )
314 p_playlist->i_current_index = i;
315 i_skip = 0;
321 if( p_sys->b_reset_currently_playing )
322 /* A bit too bad to reset twice ... */
323 ResetCurrentlyPlaying( p_playlist, p_new );
324 else if( p_new )
325 ResyncCurrentIndex( p_playlist, p_new );
326 else
327 p_playlist->i_current_index = -1;
329 if( p_playlist->current.i_size && (i_skip > 0) )
331 if( p_playlist->i_current_index < -1 )
332 p_playlist->i_current_index = -1;
333 for( int i = i_skip; i > 0 ; i-- )
335 p_playlist->i_current_index++;
336 if( p_playlist->i_current_index >= p_playlist->current.i_size )
338 PL_DEBUG( "looping - restarting at beginning of node" );
339 /* reshuffle playlist when end is reached */
340 if( var_GetBool( p_playlist, "random" ) ) {
341 PL_DEBUG( "reshuffle playlist" );
342 ResetCurrentlyPlaying( p_playlist,
343 get_current_status_item( p_playlist ) );
345 p_playlist->i_current_index = 0;
348 p_new = ARRAY_VAL( p_playlist->current,
349 p_playlist->i_current_index );
351 else if( p_playlist->current.i_size && (i_skip < 0) )
353 for( int i = i_skip; i < 0 ; i++ )
355 p_playlist->i_current_index--;
356 if( p_playlist->i_current_index <= -1 )
358 PL_DEBUG( "looping - restarting at end of node" );
359 /* reshuffle playlist when beginning is reached */
360 if( var_GetBool( p_playlist, "random" ) ) {
361 PL_DEBUG( "reshuffle playlist" );
362 ResetCurrentlyPlaying( p_playlist,
363 get_current_status_item( p_playlist ) );
365 p_playlist->i_current_index = p_playlist->current.i_size-1;
368 p_new = ARRAY_VAL( p_playlist->current,
369 p_playlist->i_current_index );
372 /* "Automatic" item change ( next ) */
373 else
375 bool b_loop = var_GetBool( p_playlist, "loop" );
376 bool b_repeat = var_GetBool( p_playlist, "repeat" );
377 bool b_playstop = var_InheritBool( p_playlist, "play-and-stop" );
379 /* Repeat and play/stop */
380 if( b_repeat && get_current_status_item( p_playlist ) )
382 msg_Dbg( p_playlist,"repeating item" );
383 return get_current_status_item( p_playlist );
385 if( b_playstop && get_current_status_item( p_playlist ) )
387 msg_Dbg( p_playlist,"stopping (play and stop)" );
388 return NULL;
391 /* */
393 PL_DEBUG( "changing item without a request (current %i/%i)",
394 p_playlist->i_current_index, p_playlist->current.i_size );
395 /* Can't go to next from current item */
396 if( p_sys->b_reset_currently_playing )
397 ResetCurrentlyPlaying( p_playlist,
398 get_current_status_item( p_playlist ) );
400 p_playlist->i_current_index++;
401 assert( p_playlist->i_current_index <= p_playlist->current.i_size );
402 if( p_playlist->i_current_index == p_playlist->current.i_size )
404 if( !b_loop || p_playlist->current.i_size == 0 )
405 return NULL;
406 /* reshuffle after last item has been played */
407 if( var_GetBool( p_playlist, "random" ) ) {
408 PL_DEBUG( "reshuffle playlist" );
409 ResetCurrentlyPlaying( p_playlist,
410 get_current_status_item( p_playlist ) );
412 p_playlist->i_current_index = 0;
414 PL_DEBUG( "using item %i", p_playlist->i_current_index );
415 if ( p_playlist->current.i_size == 0 )
416 return NULL;
418 p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
420 return p_new;
423 static void LoopInput( playlist_t *p_playlist )
425 playlist_private_t *p_sys = pl_priv(p_playlist);
426 input_thread_t *p_input = p_sys->p_input;
428 assert( p_input != NULL );
430 if( p_sys->request.b_request || p_sys->killed )
432 PL_DEBUG( "incoming request - stopping current input" );
433 input_Stop( p_input );
436 switch( var_GetInteger( p_input, "state" ) )
438 case END_S:
439 case ERROR_S:
440 /* This input is dead. Remove it ! */
441 p_sys->p_input = NULL;
442 PL_DEBUG( "dead input" );
443 PL_UNLOCK;
445 var_SetAddress( p_playlist, "input-current", NULL );
447 /* WARNING: Input resource manipulation and callback deletion are
448 * incompatible with the playlist lock. */
449 if( !var_InheritBool( p_input, "sout-keep" ) )
450 input_resource_TerminateSout( p_sys->p_input_resource );
451 var_DelCallback( p_input, "intf-event", InputEvent, p_playlist );
453 input_Close( p_input );
454 PL_LOCK;
455 break;
456 default:
457 vlc_cond_wait( &p_sys->signal, &p_sys->lock );
461 static bool Next( playlist_t *p_playlist )
463 playlist_item_t *p_item = NextItem( p_playlist );
464 if( p_item == NULL )
465 return false;
467 msg_Dbg( p_playlist, "starting playback of new item" );
468 ResyncCurrentIndex( p_playlist, p_item );
469 return PlayItem( p_playlist, p_item );
473 * Run the main control thread itself
475 static void *Thread ( void *data )
477 playlist_t *p_playlist = data;
478 playlist_private_t *p_sys = pl_priv(p_playlist);
480 PL_LOCK;
481 while( !p_sys->killed )
483 /* Playlist in stopped state */
484 assert(p_sys->p_input == NULL);
486 if( !p_sys->request.b_request )
488 vlc_cond_wait( &p_sys->signal, &p_sys->lock );
489 continue;
492 while( !p_sys->killed && Next( p_playlist ) )
493 { /* Playlist in running state */
494 assert(p_sys->p_input != NULL);
497 LoopInput( p_playlist );
498 while( p_sys->p_input != NULL );
501 msg_Dbg( p_playlist, "nothing to play" );
502 if( var_InheritBool( p_playlist, "play-and-exit" ) )
504 msg_Info( p_playlist, "end of playlist, exiting" );
505 libvlc_Quit( p_playlist->obj.libvlc );
508 /* Destroy any video display now (XXX: ugly hack) */
509 if( input_resource_HasVout( p_sys->p_input_resource ) )
511 PL_UNLOCK; /* Mind: NO LOCKS while manipulating input resources! */
512 input_resource_TerminateVout( p_sys->p_input_resource );
513 PL_LOCK;
516 PL_UNLOCK;
518 input_resource_Terminate( p_sys->p_input_resource );
519 return NULL;