1 /*****************************************************************************
2 * engine.c : Run the playlist and handle its control
3 *****************************************************************************
4 * Copyright (C) 1999-2008 VLC authors and VideoLAN
6 * Authors: Samuel Hocevar <sam@zoy.org>
7 * Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
31 #include <vlc_common.h>
33 #include <vlc_playlist.h>
34 #include <vlc_interface.h>
36 #include "playlist_internal.h"
37 #include "input/resource.h"
39 /*****************************************************************************
41 *****************************************************************************/
42 static void VariablesInit( playlist_t *p_playlist );
44 static int RandomCallback( vlc_object_t *p_this, char const *psz_cmd,
45 vlc_value_t oldval, vlc_value_t newval, void *a )
47 (void)psz_cmd; (void)oldval; (void)newval; (void)a;
48 playlist_t *p_playlist = (playlist_t*)p_this;
49 bool random = newval.b_bool;
54 pl_priv(p_playlist)->b_reset_currently_playing = true;
55 vlc_cond_signal( &pl_priv(p_playlist)->signal );
57 /* Shuffle and sync the playlist on activation of random mode.
58 * This preserves the current playing item, so that the user
59 * can return to it if needed. (See #4472)
61 playlist_private_t *p_sys = pl_priv(p_playlist);
62 playlist_item_t *p_new = p_sys->status.p_item;
63 ResetCurrentlyPlaying( p_playlist, NULL );
65 ResyncCurrentIndex( p_playlist, p_new );
73 * When there are one or more pending corks, playback should be paused.
74 * This is used for audio policy.
75 * \warning Always add and remove a cork with var_IncInteger() and var_DecInteger().
76 * var_Get() and var_Set() are prone to race conditions.
78 static int CorksCallback( vlc_object_t *obj, char const *var,
79 vlc_value_t old, vlc_value_t cur, void *dummy )
81 playlist_t *pl = (playlist_t *)obj;
83 msg_Dbg( obj, "corks count: %"PRId64" -> %"PRId64, old.i_int, cur.i_int );
84 if( !old.i_int == !cur.i_int )
85 return VLC_SUCCESS; /* nothing to do */
87 if( !var_InheritBool( obj, "playlist-cork" ) )
92 msg_Dbg( obj, "corked" );
97 msg_Dbg( obj, "uncorked" );
98 playlist_Resume( pl );
101 (void) var; (void) dummy;
105 static int RateCallback( vlc_object_t *p_this, char const *psz_cmd,
106 vlc_value_t oldval, vlc_value_t newval, void *p )
108 (void)psz_cmd; (void)oldval;(void)p;
109 playlist_t *p_playlist = (playlist_t*)p_this;
113 if( pl_priv(p_playlist)->p_input == NULL )
119 var_SetFloat( pl_priv( p_playlist )->p_input, "rate", newval.f_float );
124 static int RateOffsetCallback( vlc_object_t *obj, char const *psz_cmd,
125 vlc_value_t oldval, vlc_value_t newval, void *p_data )
127 playlist_t *p_playlist = (playlist_t *)obj;
128 VLC_UNUSED(oldval); VLC_UNUSED(p_data); VLC_UNUSED(newval);
130 static const float pf_rate[] = {
131 1.0/64, 1.0/32, 1.0/16, 1.0/8, 1.0/4, 1.0/3, 1.0/2, 2.0/3,
133 3.0/2, 2.0/1, 3.0/1, 4.0/1, 8.0/1, 16.0/1, 32.0/1, 64.0/1,
135 const size_t i_rate_count = sizeof(pf_rate)/sizeof(*pf_rate);
138 struct input_thread_t *input;
141 input = pl_priv( p_playlist )->p_input;
142 f_rate = var_GetFloat( input ? (vlc_object_t *)input : obj, "rate" );
145 if( !strcmp( psz_cmd, "rate-faster" ) )
147 /* compensate for input rounding errors */
148 float r = f_rate * 1.1f;
149 for( size_t i = 0; i < i_rate_count; i++ )
158 /* compensate for input rounding errors */
159 float r = f_rate * .9f;
160 for( size_t i = 1; i < i_rate_count; i++ )
161 if( r <= pf_rate[i] )
163 f_rate = pf_rate[i - 1];
168 var_SetFloat( p_playlist, "rate", f_rate );
172 static int VideoSplitterCallback( vlc_object_t *p_this, char const *psz_cmd,
173 vlc_value_t oldval, vlc_value_t newval, void *p_data )
175 playlist_t *p_playlist = (playlist_t*)p_this;
176 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data); VLC_UNUSED(newval);
180 /* Force the input to restart the video ES to force a vout recreation */
181 input_thread_t *p_input = pl_priv( p_playlist )->p_input;
184 const double f_position = var_GetFloat( p_input, "position" );
185 input_Control( p_input, INPUT_RESTART_ES, -VIDEO_ES );
186 var_SetFloat( p_input, "position", f_position );
196 * Create a playlist structure.
197 * \param p_parent the vlc object that is to be the parent of this playlist
198 * \return a pointer to the created playlist, or NULL on error
200 playlist_t *playlist_Create( vlc_object_t *p_parent )
202 playlist_t *p_playlist;
203 playlist_private_t *p;
205 /* Allocate structure */
206 p = vlc_custom_create( p_parent, sizeof( *p ), "playlist" );
210 assert( offsetof( playlist_private_t, public_data ) == 0 );
211 p_playlist = &p->public_data;
212 TAB_INIT( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds );
214 VariablesInit( p_playlist );
215 vlc_mutex_init( &p->lock );
216 vlc_cond_init( &p->signal );
219 /* Initialise data structures */
220 pl_priv(p_playlist)->i_last_playlist_id = 0;
221 pl_priv(p_playlist)->p_input = NULL;
223 ARRAY_INIT( p_playlist->items );
224 ARRAY_INIT( p_playlist->all_items );
225 ARRAY_INIT( pl_priv(p_playlist)->items_to_delete );
226 ARRAY_INIT( p_playlist->current );
228 p_playlist->i_current_index = 0;
229 pl_priv(p_playlist)->b_reset_currently_playing = true;
231 pl_priv(p_playlist)->b_tree = var_InheritBool( p_parent, "playlist-tree" );
233 /* Create the root, playing items and meida library nodes */
234 playlist_item_t *root, *playing, *ml;
237 root = playlist_NodeCreate( p_playlist, NULL, NULL,
238 PLAYLIST_END, 0, NULL );
239 playing = playlist_NodeCreate( p_playlist, _( "Playlist" ), root,
240 PLAYLIST_END, PLAYLIST_RO_FLAG, NULL );
241 if( var_InheritBool( p_parent, "media-library") )
242 ml = playlist_NodeCreate( p_playlist, _( "Media Library" ), root,
243 PLAYLIST_END, PLAYLIST_RO_FLAG, NULL );
248 if( unlikely(root == NULL || playing == NULL) )
251 p_playlist->p_root = root;
252 p_playlist->p_playing = playing;
253 p_playlist->p_media_library = ml;
254 p_playlist->p_root_category = p_playlist->p_root;
255 p_playlist->p_root_onelevel = p_playlist->p_root;
256 p_playlist->p_local_category = p_playlist->p_playing;
257 p_playlist->p_local_onelevel = p_playlist->p_playing;
258 p_playlist->p_ml_category = p_playlist->p_media_library;
259 p_playlist->p_ml_onelevel = p_playlist->p_media_library;;
262 pl_priv(p_playlist)->status.p_item = NULL;
263 pl_priv(p_playlist)->status.p_node = p_playlist->p_playing;
264 pl_priv(p_playlist)->request.b_request = false;
267 playlist_MLLoad( p_playlist );
269 /* Preparser (and meta retriever) _after_ the Media Library*/
270 if( var_InheritBool( p_parent, "auto-preparse" ) )
272 p->p_preparser = playlist_preparser_New( VLC_OBJECT(p_playlist) );
273 if( unlikely(p->p_preparser == NULL) )
274 msg_Err( p_playlist, "cannot create preparser" );
277 /* Input resources */
278 p->p_input_resource = input_resource_New( VLC_OBJECT( p_playlist ) );
279 if( unlikely(p->p_input_resource == NULL) )
282 /* Audio output (needed for volume and device controls). */
283 audio_output_t *aout = input_resource_GetAout( p->p_input_resource );
285 input_resource_PutAout( p->p_input_resource, aout );
287 /* Initialize the shared HTTP cookie jar */
289 cookies.p_address = vlc_http_cookies_new();
290 if ( likely(cookies.p_address) )
292 var_Create( p_playlist, "http-cookies", VLC_VAR_ADDRESS );
293 var_SetChecked( p_playlist, "http-cookies", VLC_VAR_ADDRESS, cookies );
297 playlist_Activate (p_playlist);
299 /* Add service discovery modules */
300 char *mods = var_InheritString( p_playlist, "services-discovery" );
304 while( (m = strsep( &s, " :," )) != NULL )
305 playlist_ServicesDiscoveryAdd( p_playlist, m );
314 * This is not thread-safe. Any reference to the playlist is assumed gone.
315 * (In particular, all interface and services threads must have been joined).
317 * \param p_playlist the playlist object
319 void playlist_Destroy( playlist_t *p_playlist )
321 playlist_private_t *p_sys = pl_priv(p_playlist);
323 /* Remove all services discovery */
324 playlist_ServicesDiscoveryKillAll( p_playlist );
326 msg_Dbg( p_playlist, "destroying" );
328 playlist_Deactivate( p_playlist );
329 if( p_sys->p_preparser )
330 playlist_preparser_Delete( p_sys->p_preparser );
332 /* Release input resources */
333 assert( p_sys->p_input == NULL );
334 input_resource_Release( p_sys->p_input_resource );
336 if( p_playlist->p_media_library != NULL )
337 playlist_MLDump( p_playlist );
340 /* Release the current node */
341 set_current_status_node( p_playlist, NULL );
342 /* Release the current item */
343 set_current_status_item( p_playlist, NULL );
346 vlc_cond_destroy( &p_sys->signal );
347 vlc_mutex_destroy( &p_sys->lock );
349 /* Remove all remaining items */
350 FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
351 free( p_del->pp_children );
352 vlc_gc_decref( p_del->p_input );
355 ARRAY_RESET( p_playlist->all_items );
356 FOREACH_ARRAY( playlist_item_t *p_del, p_sys->items_to_delete )
357 free( p_del->pp_children );
358 vlc_gc_decref( p_del->p_input );
361 ARRAY_RESET( p_sys->items_to_delete );
363 ARRAY_RESET( p_playlist->items );
364 ARRAY_RESET( p_playlist->current );
366 vlc_http_cookie_jar_t *cookies = var_GetAddress( p_playlist, "http-cookies" );
369 var_Destroy( p_playlist, "http-cookies" );
370 vlc_http_cookies_destroy( cookies );
373 vlc_object_release( p_playlist );
376 /** Get current playing input.
378 input_thread_t * playlist_CurrentInput( playlist_t * p_playlist )
380 input_thread_t * p_input;
382 p_input = pl_priv(p_playlist)->p_input;
383 if( p_input ) vlc_object_hold( p_input );
392 /** Accessor for status item and status nodes.
394 playlist_item_t * get_current_status_item( playlist_t * p_playlist )
398 return pl_priv(p_playlist)->status.p_item;
401 playlist_item_t * get_current_status_node( playlist_t * p_playlist )
405 return pl_priv(p_playlist)->status.p_node;
408 void set_current_status_item( playlist_t * p_playlist,
409 playlist_item_t * p_item )
413 if( pl_priv(p_playlist)->status.p_item &&
414 pl_priv(p_playlist)->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG &&
415 pl_priv(p_playlist)->status.p_item != p_item )
417 /* It's unsafe given current design to delete a playlist item :(
418 playlist_ItemDelete( pl_priv(p_playlist)->status.p_item ); */
420 pl_priv(p_playlist)->status.p_item = p_item;
423 void set_current_status_node( playlist_t * p_playlist,
424 playlist_item_t * p_node )
428 if( pl_priv(p_playlist)->status.p_node &&
429 pl_priv(p_playlist)->status.p_node->i_flags & PLAYLIST_REMOVE_FLAG &&
430 pl_priv(p_playlist)->status.p_node != p_node )
432 /* It's unsafe given current design to delete a playlist item :(
433 playlist_ItemDelete( pl_priv(p_playlist)->status.p_node ); */
435 pl_priv(p_playlist)->status.p_node = p_node;
438 static void VariablesInit( playlist_t *p_playlist )
440 /* These variables control updates */
441 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
442 var_SetBool( p_playlist, "intf-change", true );
444 var_Create( p_playlist, "item-change", VLC_VAR_ADDRESS );
445 var_Create( p_playlist, "leaf-to-parent", VLC_VAR_INTEGER );
447 var_Create( p_playlist, "playlist-item-deleted", VLC_VAR_INTEGER );
448 var_SetInteger( p_playlist, "playlist-item-deleted", -1 );
450 var_Create( p_playlist, "playlist-item-append", VLC_VAR_ADDRESS );
452 var_Create( p_playlist, "input-current", VLC_VAR_ADDRESS );
454 /* Variables to control playback */
455 var_Create( p_playlist, "playlist-autostart", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
456 var_Create( p_playlist, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
457 var_AddCallback( p_playlist, "random", RandomCallback, NULL );
458 var_Create( p_playlist, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
459 var_Create( p_playlist, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
460 var_Create( p_playlist, "corks", VLC_VAR_INTEGER );
461 var_AddCallback( p_playlist, "corks", CorksCallback, NULL );
463 var_Create( p_playlist, "rate", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
464 var_AddCallback( p_playlist, "rate", RateCallback, NULL );
465 var_Create( p_playlist, "rate-slower", VLC_VAR_VOID );
466 var_AddCallback( p_playlist, "rate-slower", RateOffsetCallback, NULL );
467 var_Create( p_playlist, "rate-faster", VLC_VAR_VOID );
468 var_AddCallback( p_playlist, "rate-faster", RateOffsetCallback, NULL );
470 var_Create( p_playlist, "video-splitter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
471 var_AddCallback( p_playlist, "video-splitter", VideoSplitterCallback, NULL );
474 var_Create( p_playlist, "album-art", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
475 var_Create( p_playlist, "metadata-network-access", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
477 /* Variables to preserve video output parameters */
478 var_Create( p_playlist, "fullscreen", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
479 var_Create( p_playlist, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
480 var_Create( p_playlist, "video-wallpaper", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
482 /* Audio output parameters */
483 var_Create( p_playlist, "mute", VLC_VAR_BOOL );
484 var_Create( p_playlist, "volume", VLC_VAR_FLOAT );
485 var_SetFloat( p_playlist, "volume", -1.f );
488 playlist_item_t * playlist_CurrentPlayingItem( playlist_t * p_playlist )
492 return pl_priv(p_playlist)->status.p_item;
495 int playlist_Status( playlist_t * p_playlist )
497 input_thread_t *p_input = pl_priv(p_playlist)->p_input;
501 if( p_input == NULL )
502 return PLAYLIST_STOPPED;
503 if( var_GetInteger( p_input, "state" ) == PAUSE_S )
504 return PLAYLIST_PAUSED;
505 return PLAYLIST_RUNNING;