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>
35 #include "playlist_internal.h"
36 #include "input/resource.h"
38 /*****************************************************************************
40 *****************************************************************************/
41 static void VariablesInit( playlist_t
*p_playlist
);
43 static int RandomCallback( vlc_object_t
*p_this
, char const *psz_cmd
,
44 vlc_value_t oldval
, vlc_value_t newval
, void *a
)
46 (void)psz_cmd
; (void)oldval
; (void)newval
; (void)a
;
47 playlist_t
*p_playlist
= (playlist_t
*)p_this
;
48 bool random
= newval
.b_bool
;
53 pl_priv(p_playlist
)->b_reset_currently_playing
= true;
54 vlc_cond_signal( &pl_priv(p_playlist
)->signal
);
56 /* Shuffle and sync the playlist on activation of random mode.
57 * This preserves the current playing item, so that the user
58 * can return to it if needed. (See #4472)
60 playlist_private_t
*p_sys
= pl_priv(p_playlist
);
61 playlist_item_t
*p_new
= p_sys
->status
.p_item
;
62 ResetCurrentlyPlaying( p_playlist
, NULL
);
64 ResyncCurrentIndex( p_playlist
, p_new
);
72 * When there are one or more pending corks, playback should be paused.
73 * This is used for audio policy.
74 * \warning Always add and remove a cork with var_IncInteger() and var_DecInteger().
75 * var_Get() and var_Set() are prone to race conditions.
77 static int CorksCallback( vlc_object_t
*obj
, char const *var
,
78 vlc_value_t old
, vlc_value_t cur
, void *dummy
)
80 playlist_t
*pl
= (playlist_t
*)obj
;
82 msg_Dbg( obj
, "corks count: %"PRId64
" -> %"PRId64
, old
.i_int
, cur
.i_int
);
83 if( !old
.i_int
== !cur
.i_int
)
84 return VLC_SUCCESS
; /* nothing to do */
88 if( var_InheritBool( obj
, "playlist-cork" ) )
90 msg_Dbg( obj
, "corked" );
94 msg_Dbg( obj
, "not corked" );
97 msg_Dbg( obj
, "uncorked" );
99 (void) var
; (void) dummy
;
103 static int RateCallback( vlc_object_t
*p_this
, char const *psz_cmd
,
104 vlc_value_t oldval
, vlc_value_t newval
, void *p
)
106 (void)psz_cmd
; (void)oldval
;(void)p
;
107 playlist_t
*p_playlist
= (playlist_t
*)p_this
;
111 if( pl_priv(p_playlist
)->p_input
== NULL
)
117 var_SetFloat( pl_priv( p_playlist
)->p_input
, "rate", newval
.f_float
);
122 static int RateOffsetCallback( vlc_object_t
*obj
, char const *psz_cmd
,
123 vlc_value_t oldval
, vlc_value_t newval
, void *p_data
)
125 playlist_t
*p_playlist
= (playlist_t
*)obj
;
126 VLC_UNUSED(oldval
); VLC_UNUSED(p_data
); VLC_UNUSED(newval
);
128 static const float pf_rate
[] = {
129 1.0/64, 1.0/32, 1.0/16, 1.0/8, 1.0/4, 1.0/3, 1.0/2, 2.0/3,
131 3.0/2, 2.0/1, 3.0/1, 4.0/1, 8.0/1, 16.0/1, 32.0/1, 64.0/1,
133 const size_t i_rate_count
= sizeof(pf_rate
)/sizeof(*pf_rate
);
136 struct input_thread_t
*input
;
139 input
= pl_priv( p_playlist
)->p_input
;
140 f_rate
= var_GetFloat( input
? (vlc_object_t
*)input
: obj
, "rate" );
143 if( !strcmp( psz_cmd
, "rate-faster" ) )
145 /* compensate for input rounding errors */
146 float r
= f_rate
* 1.1;
147 for( size_t i
= 0; i
< i_rate_count
; i
++ )
156 /* compensate for input rounding errors */
157 float r
= f_rate
* .9;
158 for( size_t i
= 1; i
< i_rate_count
; i
++ )
159 if( r
<= pf_rate
[i
] )
161 f_rate
= pf_rate
[i
- 1];
166 var_SetFloat( p_playlist
, "rate", f_rate
);
170 static int VideoSplitterCallback( vlc_object_t
*p_this
, char const *psz_cmd
,
171 vlc_value_t oldval
, vlc_value_t newval
, void *p_data
)
173 playlist_t
*p_playlist
= (playlist_t
*)p_this
;
174 VLC_UNUSED(psz_cmd
); VLC_UNUSED(oldval
); VLC_UNUSED(p_data
); VLC_UNUSED(newval
);
178 /* Force the input to restart the video ES to force a vout recreation */
179 input_thread_t
*p_input
= pl_priv( p_playlist
)->p_input
;
182 const double f_position
= var_GetFloat( p_input
, "position" );
183 input_Control( p_input
, INPUT_RESTART_ES
, -VIDEO_ES
);
184 var_SetFloat( p_input
, "position", f_position
);
194 * Create a playlist structure.
195 * \param p_parent the vlc object that is to be the parent of this playlist
196 * \return a pointer to the created playlist, or NULL on error
198 static playlist_t
*playlist_Create( vlc_object_t
*p_parent
)
200 playlist_t
*p_playlist
;
201 playlist_private_t
*p
;
203 /* Allocate structure */
204 p
= vlc_custom_create( p_parent
, sizeof( *p
), "playlist" );
208 assert( offsetof( playlist_private_t
, public_data
) == 0 );
209 p_playlist
= &p
->public_data
;
210 TAB_INIT( pl_priv(p_playlist
)->i_sds
, pl_priv(p_playlist
)->pp_sds
);
212 VariablesInit( p_playlist
);
213 vlc_mutex_init( &p
->lock
);
214 vlc_cond_init( &p
->signal
);
217 /* Initialise data structures */
218 pl_priv(p_playlist
)->i_last_playlist_id
= 0;
219 pl_priv(p_playlist
)->p_input
= NULL
;
221 ARRAY_INIT( p_playlist
->items
);
222 ARRAY_INIT( p_playlist
->all_items
);
223 ARRAY_INIT( pl_priv(p_playlist
)->items_to_delete
);
224 ARRAY_INIT( p_playlist
->current
);
226 p_playlist
->i_current_index
= 0;
227 pl_priv(p_playlist
)->b_reset_currently_playing
= true;
229 pl_priv(p_playlist
)->b_tree
= var_InheritBool( p_parent
, "playlist-tree" );
231 pl_priv(p_playlist
)->b_doing_ml
= false;
233 pl_priv(p_playlist
)->b_auto_preparse
=
234 var_InheritBool( p_parent
, "auto-preparse" );
237 p
->p_fetcher
= playlist_fetcher_New( VLC_OBJECT(p_playlist
) );
238 if( unlikely(p
->p_fetcher
== NULL
) )
239 msg_Err( p_playlist
, "cannot create fetcher" );
241 p
->p_preparser
= playlist_preparser_New( VLC_OBJECT(p_playlist
),
243 if( unlikely(p
->p_preparser
== NULL
) )
244 msg_Err( p_playlist
, "cannot create preparser" );
246 /* Create the root node */
248 p_playlist
->p_root
= playlist_NodeCreate( p_playlist
, NULL
, NULL
,
249 PLAYLIST_END
, 0, NULL
);
251 if( !p_playlist
->p_root
) return NULL
;
253 /* Create currently playing items node */
255 p_playlist
->p_playing
= playlist_NodeCreate(
256 p_playlist
, _( "Playlist" ), p_playlist
->p_root
,
257 PLAYLIST_END
, PLAYLIST_RO_FLAG
, NULL
);
261 if( !p_playlist
->p_playing
) return NULL
;
263 /* Create media library node */
264 const bool b_ml
= var_InheritBool( p_parent
, "media-library");
268 p_playlist
->p_media_library
= playlist_NodeCreate(
269 p_playlist
, _( "Media Library" ), p_playlist
->p_root
,
270 PLAYLIST_END
, PLAYLIST_RO_FLAG
, NULL
);
275 p_playlist
->p_media_library
= NULL
;
278 p_playlist
->p_root_category
= p_playlist
->p_root
;
279 p_playlist
->p_root_onelevel
= p_playlist
->p_root
;
280 p_playlist
->p_local_category
= p_playlist
->p_playing
;
281 p_playlist
->p_local_onelevel
= p_playlist
->p_playing
;
282 p_playlist
->p_ml_category
= p_playlist
->p_media_library
;
283 p_playlist
->p_ml_onelevel
= p_playlist
->p_media_library
;;
286 pl_priv(p_playlist
)->status
.p_item
= NULL
;
287 pl_priv(p_playlist
)->status
.p_node
= p_playlist
->p_playing
;
288 pl_priv(p_playlist
)->request
.b_request
= false;
289 pl_priv(p_playlist
)->status
.i_status
= PLAYLIST_STOPPED
;
293 const bool b_auto_preparse
= pl_priv(p_playlist
)->b_auto_preparse
;
294 pl_priv(p_playlist
)->b_auto_preparse
= false;
295 playlist_MLLoad( p_playlist
);
296 pl_priv(p_playlist
)->b_auto_preparse
= b_auto_preparse
;
299 /* Input resources */
300 p
->p_input_resource
= input_resource_New( VLC_OBJECT( p_playlist
) );
301 if( unlikely(p
->p_input_resource
== NULL
) )
304 /* Audio output (needed for volume and device controls). */
305 audio_output_t
*aout
= input_resource_GetAout( p
->p_input_resource
);
307 input_resource_PutAout( p
->p_input_resource
, aout
);
310 playlist_Activate (p_playlist
);
312 /* Add service discovery modules */
313 char *mods
= var_InheritString( p_playlist
, "services-discovery" );
317 while( (m
= strsep( &p
, " :," )) != NULL
)
318 playlist_ServicesDiscoveryAdd( p_playlist
, m
);
327 * This is not thread-safe. Any reference to the playlist is assumed gone.
328 * (In particular, all interface and services threads must have been joined).
330 * \param p_playlist the playlist object
332 void playlist_Destroy( playlist_t
*p_playlist
)
334 playlist_private_t
*p_sys
= pl_priv(p_playlist
);
336 /* Remove all services discovery */
337 playlist_ServicesDiscoveryKillAll( p_playlist
);
339 msg_Dbg( p_playlist
, "destroying" );
341 playlist_Deactivate( p_playlist
);
342 if( p_sys
->p_preparser
)
343 playlist_preparser_Delete( p_sys
->p_preparser
);
344 if( p_sys
->p_fetcher
)
345 playlist_fetcher_Delete( p_sys
->p_fetcher
);
347 /* Release input resources */
348 assert( p_sys
->p_input
== NULL
);
349 input_resource_Release( p_sys
->p_input_resource
);
351 if( p_playlist
->p_media_library
!= NULL
)
352 playlist_MLDump( p_playlist
);
355 /* Release the current node */
356 set_current_status_node( p_playlist
, NULL
);
357 /* Release the current item */
358 set_current_status_item( p_playlist
, NULL
);
361 vlc_cond_destroy( &p_sys
->signal
);
362 vlc_mutex_destroy( &p_sys
->lock
);
364 /* Remove all remaining items */
365 FOREACH_ARRAY( playlist_item_t
*p_del
, p_playlist
->all_items
)
366 free( p_del
->pp_children
);
367 vlc_gc_decref( p_del
->p_input
);
370 ARRAY_RESET( p_playlist
->all_items
);
371 FOREACH_ARRAY( playlist_item_t
*p_del
, p_sys
->items_to_delete
)
372 free( p_del
->pp_children
);
373 vlc_gc_decref( p_del
->p_input
);
376 ARRAY_RESET( p_sys
->items_to_delete
);
378 ARRAY_RESET( p_playlist
->items
);
379 ARRAY_RESET( p_playlist
->current
);
381 vlc_object_release( p_playlist
);
385 playlist_t
*pl_Get (vlc_object_t
*obj
)
387 static vlc_mutex_t lock
= VLC_STATIC_MUTEX
;
388 libvlc_int_t
*p_libvlc
= obj
->p_libvlc
;
391 vlc_mutex_lock (&lock
);
392 pl
= libvlc_priv (p_libvlc
)->p_playlist
;
393 if (unlikely(pl
== NULL
))
395 pl
= playlist_Create (VLC_OBJECT(p_libvlc
));
396 if (unlikely(pl
== NULL
))
398 libvlc_priv (p_libvlc
)->p_playlist
= pl
;
400 vlc_mutex_unlock (&lock
);
404 /** Get current playing input.
406 input_thread_t
* playlist_CurrentInput( playlist_t
* p_playlist
)
408 input_thread_t
* p_input
;
410 p_input
= pl_priv(p_playlist
)->p_input
;
411 if( p_input
) vlc_object_hold( p_input
);
420 /** Accessor for status item and status nodes.
422 playlist_item_t
* get_current_status_item( playlist_t
* p_playlist
)
426 return pl_priv(p_playlist
)->status
.p_item
;
429 playlist_item_t
* get_current_status_node( playlist_t
* p_playlist
)
433 return pl_priv(p_playlist
)->status
.p_node
;
436 void set_current_status_item( playlist_t
* p_playlist
,
437 playlist_item_t
* p_item
)
441 if( pl_priv(p_playlist
)->status
.p_item
&&
442 pl_priv(p_playlist
)->status
.p_item
->i_flags
& PLAYLIST_REMOVE_FLAG
&&
443 pl_priv(p_playlist
)->status
.p_item
!= p_item
)
445 /* It's unsafe given current design to delete a playlist item :(
446 playlist_ItemDelete( pl_priv(p_playlist)->status.p_item ); */
448 pl_priv(p_playlist
)->status
.p_item
= p_item
;
451 void set_current_status_node( playlist_t
* p_playlist
,
452 playlist_item_t
* p_node
)
456 if( pl_priv(p_playlist
)->status
.p_node
&&
457 pl_priv(p_playlist
)->status
.p_node
->i_flags
& PLAYLIST_REMOVE_FLAG
&&
458 pl_priv(p_playlist
)->status
.p_node
!= p_node
)
460 /* It's unsafe given current design to delete a playlist item :(
461 playlist_ItemDelete( pl_priv(p_playlist)->status.p_node ); */
463 pl_priv(p_playlist
)->status
.p_node
= p_node
;
466 static void VariablesInit( playlist_t
*p_playlist
)
468 /* These variables control updates */
469 var_Create( p_playlist
, "intf-change", VLC_VAR_BOOL
);
470 var_SetBool( p_playlist
, "intf-change", true );
472 var_Create( p_playlist
, "item-change", VLC_VAR_ADDRESS
);
473 var_Create( p_playlist
, "leaf-to-parent", VLC_VAR_INTEGER
);
475 var_Create( p_playlist
, "playlist-item-deleted", VLC_VAR_INTEGER
);
476 var_SetInteger( p_playlist
, "playlist-item-deleted", -1 );
478 var_Create( p_playlist
, "playlist-item-append", VLC_VAR_ADDRESS
);
480 var_Create( p_playlist
, "input-current", VLC_VAR_ADDRESS
);
482 var_Create( p_playlist
, "activity", VLC_VAR_VOID
);
484 /* Variables to control playback */
485 var_Create( p_playlist
, "playlist-autostart", VLC_VAR_BOOL
| VLC_VAR_DOINHERIT
);
486 var_Create( p_playlist
, "play-and-stop", VLC_VAR_BOOL
| VLC_VAR_DOINHERIT
);
487 var_Create( p_playlist
, "play-and-exit", VLC_VAR_BOOL
| VLC_VAR_DOINHERIT
);
488 var_Create( p_playlist
, "random", VLC_VAR_BOOL
| VLC_VAR_DOINHERIT
);
489 var_AddCallback( p_playlist
, "random", RandomCallback
, NULL
);
490 var_Create( p_playlist
, "repeat", VLC_VAR_BOOL
| VLC_VAR_DOINHERIT
);
491 var_Create( p_playlist
, "loop", VLC_VAR_BOOL
| VLC_VAR_DOINHERIT
);
492 var_Create( p_playlist
, "corks", VLC_VAR_INTEGER
);
493 var_AddCallback( p_playlist
, "corks", CorksCallback
, NULL
);
495 var_Create( p_playlist
, "rate", VLC_VAR_FLOAT
| VLC_VAR_DOINHERIT
);
496 var_AddCallback( p_playlist
, "rate", RateCallback
, NULL
);
497 var_Create( p_playlist
, "rate-slower", VLC_VAR_VOID
);
498 var_AddCallback( p_playlist
, "rate-slower", RateOffsetCallback
, NULL
);
499 var_Create( p_playlist
, "rate-faster", VLC_VAR_VOID
);
500 var_AddCallback( p_playlist
, "rate-faster", RateOffsetCallback
, NULL
);
502 var_Create( p_playlist
, "video-splitter", VLC_VAR_STRING
| VLC_VAR_DOINHERIT
);
503 var_AddCallback( p_playlist
, "video-splitter", VideoSplitterCallback
, NULL
);
506 var_Create( p_playlist
, "album-art", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
508 /* Variables to preserve video output parameters */
509 var_Create( p_playlist
, "fullscreen", VLC_VAR_BOOL
| VLC_VAR_DOINHERIT
);
510 var_Create( p_playlist
, "video-on-top", VLC_VAR_BOOL
| VLC_VAR_DOINHERIT
);
512 /* Audio output parameters */
513 var_Create( p_playlist
, "mute", VLC_VAR_BOOL
);
514 var_Create( p_playlist
, "volume", VLC_VAR_FLOAT
);
515 var_SetFloat( p_playlist
, "volume", -1.f
);
518 playlist_item_t
* playlist_CurrentPlayingItem( playlist_t
* p_playlist
)
522 return pl_priv(p_playlist
)->status
.p_item
;
525 int playlist_Status( playlist_t
* p_playlist
)
529 return pl_priv(p_playlist
)->status
.i_status
;