direct3d11: set the texture size after we know we use a staging one
[vlc.git] / src / playlist / thread.c
blob9f51054089ac2ed4a103479a219d1fcecf2caf58
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_DEAD )
102 playlist_private_t *sys = pl_priv(p_playlist);
104 PL_LOCK;
105 sys->request.input_dead = true;
106 vlc_cond_signal( &sys->signal );
107 PL_UNLOCK;
109 return VLC_SUCCESS;
113 * Synchronise the current index of the playlist
114 * to match the index of the current item.
116 * \param p_playlist the playlist structure
117 * \param p_cur the current playlist item
118 * \return nothing
120 void ResyncCurrentIndex( playlist_t *p_playlist, playlist_item_t *p_cur )
122 PL_ASSERT_LOCKED;
124 PL_DEBUG( "resyncing on %s", PLI_NAME( p_cur ) );
125 /* Simply resync index */
126 int i;
127 p_playlist->i_current_index = -1;
128 for( i = 0 ; i< p_playlist->current.i_size; i++ )
130 if( ARRAY_VAL( p_playlist->current, i ) == p_cur )
132 p_playlist->i_current_index = i;
133 break;
136 PL_DEBUG( "%s is at %i", PLI_NAME( p_cur ), p_playlist->i_current_index );
140 * Reset the currently playing playlist.
142 * \param p_playlist the playlist structure
143 * \param p_cur the current playlist item
144 * \return nothing
146 void ResetCurrentlyPlaying( playlist_t *p_playlist,
147 playlist_item_t *p_cur )
149 playlist_private_t *p_sys = pl_priv(p_playlist);
151 PL_DEBUG( "rebuilding array of current - root %s",
152 PLI_NAME( p_sys->status.p_node ) );
153 ARRAY_RESET( p_playlist->current );
154 p_playlist->i_current_index = -1;
155 for( playlist_item_t *p_next = NULL; ; )
157 /** FIXME: this is *slow* */
158 p_next = playlist_GetNextLeaf( p_playlist,
159 p_sys->status.p_node,
160 p_next, true, false );
161 if( !p_next )
162 break;
164 if( p_next == p_cur )
165 p_playlist->i_current_index = p_playlist->current.i_size;
166 ARRAY_APPEND( p_playlist->current, p_next);
168 PL_DEBUG("rebuild done - %i items, index %i", p_playlist->current.i_size,
169 p_playlist->i_current_index);
171 if( var_GetBool( p_playlist, "random" ) && ( p_playlist->current.i_size > 0 ) )
173 /* Shuffle the array */
174 for( unsigned j = p_playlist->current.i_size - 1; j > 0; j-- )
176 unsigned i = vlc_lrand48() % (j+1); /* between 0 and j */
177 playlist_item_t *p_tmp;
178 /* swap the two items */
179 p_tmp = ARRAY_VAL(p_playlist->current, i);
180 ARRAY_VAL(p_playlist->current,i) = ARRAY_VAL(p_playlist->current,j);
181 ARRAY_VAL(p_playlist->current,j) = p_tmp;
184 p_sys->b_reset_currently_playing = false;
189 * Start the input for an item
191 * \param p_playlist the playlist object
192 * \param p_item the item to play
194 static bool PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
196 playlist_private_t *p_sys = pl_priv(p_playlist);
197 input_item_t *p_input = p_item->p_input;
198 vlc_renderer_item_t *p_renderer;
200 PL_ASSERT_LOCKED;
202 msg_Dbg( p_playlist, "creating new input thread" );
204 p_item->i_nb_played++;
205 set_current_status_item( p_playlist, p_item );
206 p_renderer = p_sys->p_renderer;
207 /* Retain the renderer now to avoid it to be released by
208 * playlist_SetRenderer when we exit the locked scope. If the last reference
209 * was to be released, we would use a dangling pointer */
210 if( p_renderer )
211 vlc_renderer_item_hold( p_renderer );
212 assert( p_sys->p_input == NULL );
213 PL_UNLOCK;
215 libvlc_MetadataCancel( p_playlist->obj.libvlc, p_item );
217 input_thread_t *p_input_thread = input_Create( p_playlist, p_input, NULL,
218 p_sys->p_input_resource,
219 p_renderer );
220 if( likely(p_input_thread != NULL) )
222 var_AddCallback( p_input_thread, "intf-event",
223 InputEvent, p_playlist );
225 if( input_Start( p_input_thread ) )
227 var_DelCallback( p_input_thread, "intf-event",
228 InputEvent, p_playlist );
229 vlc_object_release( p_input_thread );
230 p_input_thread = NULL;
234 /* TODO store art policy in playlist private data */
235 char *psz_arturl = input_item_GetArtURL( p_input );
236 /* p_input->p_meta should not be null after a successful CreateThread */
237 bool b_has_art = !EMPTY_STR( psz_arturl );
239 if( !b_has_art || strncmp( psz_arturl, "attachment://", 13 ) )
241 PL_DEBUG( "requesting art for new input thread" );
242 libvlc_ArtRequest( p_playlist->obj.libvlc, p_input, META_REQUEST_OPTION_NONE );
244 free( psz_arturl );
246 PL_LOCK;
247 p_sys->p_input = p_input_thread;
248 PL_UNLOCK;
250 var_SetAddress( p_playlist, "input-current", p_input_thread );
252 PL_LOCK;
253 return p_input_thread != NULL;
257 * Compute the next playlist item depending on
258 * the playlist course mode (forward, backward, random, view,...).
260 * \param p_playlist the playlist object
261 * \return nothing
263 static playlist_item_t *NextItem( playlist_t *p_playlist )
265 playlist_private_t *p_sys = pl_priv(p_playlist);
266 playlist_item_t *p_new = NULL;
267 bool requested = p_sys->request.b_request;
269 /* Clear the request */
270 p_sys->request.b_request = false;
272 /* Handle quickly a few special cases */
273 /* No items to play */
274 if( p_playlist->items.i_size == 0 )
276 msg_Info( p_playlist, "playlist is empty" );
277 return NULL;
280 /* Start the real work */
281 if( requested )
283 p_new = p_sys->request.p_item;
285 if( p_new == NULL && p_sys->request.p_node == NULL )
286 return NULL; /* Stop request! */
288 int i_skip = p_sys->request.i_skip;
289 PL_DEBUG( "processing request item: %s, node: %s, skip: %i",
290 PLI_NAME( p_sys->request.p_item ),
291 PLI_NAME( p_sys->request.p_node ), i_skip );
293 if( p_sys->request.p_node &&
294 p_sys->request.p_node != get_current_status_node( p_playlist ) )
297 set_current_status_node( p_playlist, p_sys->request.p_node );
298 p_sys->request.p_node = NULL;
299 p_sys->b_reset_currently_playing = true;
302 /* If we are asked for a node, go to it's first child */
303 if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
305 i_skip++;
306 if( p_new != NULL )
308 p_new = playlist_GetNextLeaf( p_playlist, p_new, NULL, true, false );
309 for( int i = 0; i < p_playlist->current.i_size; i++ )
311 if( p_new == ARRAY_VAL( p_playlist->current, i ) )
313 p_playlist->i_current_index = i;
314 i_skip = 0;
320 if( p_sys->b_reset_currently_playing )
321 /* A bit too bad to reset twice ... */
322 ResetCurrentlyPlaying( p_playlist, p_new );
323 else if( p_new )
324 ResyncCurrentIndex( p_playlist, p_new );
325 else
326 p_playlist->i_current_index = -1;
328 if( p_playlist->current.i_size && (i_skip > 0) )
330 if( p_playlist->i_current_index < -1 )
331 p_playlist->i_current_index = -1;
332 for( int i = i_skip; i > 0 ; i-- )
334 p_playlist->i_current_index++;
335 if( p_playlist->i_current_index >= p_playlist->current.i_size )
337 PL_DEBUG( "looping - restarting at beginning of node" );
338 /* reshuffle playlist when end is reached */
339 if( var_GetBool( p_playlist, "random" ) ) {
340 PL_DEBUG( "reshuffle playlist" );
341 ResetCurrentlyPlaying( p_playlist,
342 get_current_status_item( p_playlist ) );
344 p_playlist->i_current_index = 0;
347 p_new = ARRAY_VAL( p_playlist->current,
348 p_playlist->i_current_index );
350 else if( p_playlist->current.i_size && (i_skip < 0) )
352 for( int i = i_skip; i < 0 ; i++ )
354 p_playlist->i_current_index--;
355 if( p_playlist->i_current_index <= -1 )
357 PL_DEBUG( "looping - restarting at end of node" );
358 /* reshuffle playlist when beginning is reached */
359 if( var_GetBool( p_playlist, "random" ) ) {
360 PL_DEBUG( "reshuffle playlist" );
361 ResetCurrentlyPlaying( p_playlist,
362 get_current_status_item( p_playlist ) );
364 p_playlist->i_current_index = p_playlist->current.i_size-1;
367 p_new = ARRAY_VAL( p_playlist->current,
368 p_playlist->i_current_index );
371 /* "Automatic" item change ( next ) */
372 else
374 bool b_loop = var_GetBool( p_playlist, "loop" );
375 bool b_repeat = var_GetBool( p_playlist, "repeat" );
376 bool b_playstop = var_InheritBool( p_playlist, "play-and-stop" );
378 /* Repeat and play/stop */
379 if( b_repeat && get_current_status_item( p_playlist ) )
381 msg_Dbg( p_playlist,"repeating item" );
382 return get_current_status_item( p_playlist );
384 if( b_playstop && get_current_status_item( p_playlist ) )
386 msg_Dbg( p_playlist,"stopping (play and stop)" );
387 return NULL;
390 /* */
392 PL_DEBUG( "changing item without a request (current %i/%i)",
393 p_playlist->i_current_index, p_playlist->current.i_size );
394 /* Can't go to next from current item */
395 if( p_sys->b_reset_currently_playing )
396 ResetCurrentlyPlaying( p_playlist,
397 get_current_status_item( p_playlist ) );
399 p_playlist->i_current_index++;
400 assert( p_playlist->i_current_index <= p_playlist->current.i_size );
401 if( p_playlist->i_current_index == p_playlist->current.i_size )
403 if( !b_loop || p_playlist->current.i_size == 0 )
404 return NULL;
405 /* reshuffle after last item has been played */
406 if( var_GetBool( p_playlist, "random" ) ) {
407 PL_DEBUG( "reshuffle playlist" );
408 ResetCurrentlyPlaying( p_playlist,
409 get_current_status_item( p_playlist ) );
411 p_playlist->i_current_index = 0;
413 PL_DEBUG( "using item %i", p_playlist->i_current_index );
414 if ( p_playlist->current.i_size == 0 )
415 return NULL;
417 p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
419 return p_new;
422 static void LoopInput( playlist_t *p_playlist )
424 playlist_private_t *p_sys = pl_priv(p_playlist);
425 input_thread_t *p_input = p_sys->p_input;
427 assert( p_input != NULL );
429 /* Wait for input to end or be stopped */
430 while( !p_sys->request.input_dead )
432 if( p_sys->request.b_request || p_sys->killed )
434 PL_DEBUG( "incoming request - stopping current input" );
435 input_Stop( p_input );
437 vlc_cond_wait( &p_sys->signal, &p_sys->lock );
440 /* This input is dead. Remove it ! */
441 PL_DEBUG( "dead input" );
442 p_sys->p_input = NULL;
443 p_sys->request.input_dead = false;
444 PL_UNLOCK;
446 var_SetAddress( p_playlist, "input-current", NULL );
448 /* WARNING: Input resource manipulation and callback deletion are
449 * incompatible with the playlist lock. */
450 if( !var_InheritBool( p_input, "sout-keep" ) )
451 input_resource_TerminateSout( p_sys->p_input_resource );
452 var_DelCallback( p_input, "intf-event", InputEvent, p_playlist );
454 input_Close( p_input );
455 PL_LOCK;
458 static bool Next( playlist_t *p_playlist )
460 playlist_item_t *p_item = NextItem( p_playlist );
461 if( p_item == NULL )
462 return false;
464 msg_Dbg( p_playlist, "starting playback of new item" );
465 ResyncCurrentIndex( p_playlist, p_item );
466 return PlayItem( p_playlist, p_item );
470 * Run the main control thread itself
472 static void *Thread ( void *data )
474 playlist_t *p_playlist = data;
475 playlist_private_t *p_sys = pl_priv(p_playlist);
476 bool played = false;
478 PL_LOCK;
479 while( !p_sys->killed )
481 /* Playlist in stopped state */
482 assert(p_sys->p_input == NULL);
484 if( !p_sys->request.b_request )
486 vlc_cond_wait( &p_sys->signal, &p_sys->lock );
487 continue;
490 /* Playlist in running state */
491 while( !p_sys->killed && Next( p_playlist ) )
493 LoopInput( p_playlist );
494 played = true;
497 /* Playlist stopping */
498 msg_Dbg( p_playlist, "nothing to play" );
499 if( played && var_InheritBool( p_playlist, "play-and-exit" ) )
501 msg_Info( p_playlist, "end of playlist, exiting" );
502 libvlc_Quit( p_playlist->obj.libvlc );
505 /* Destroy any video display now (XXX: ugly hack) */
506 if( input_resource_HasVout( p_sys->p_input_resource ) )
508 PL_UNLOCK; /* Mind: NO LOCKS while manipulating input resources! */
509 input_resource_TerminateVout( p_sys->p_input_resource );
510 PL_LOCK;
513 PL_UNLOCK;
515 input_resource_Terminate( p_sys->p_input_resource );
516 return NULL;