]> git.sesse.net Git - vlc/blob - src/playlist/engine.c
playlist: preserve first item when enabling random mode
[vlc] / src / playlist / engine.c
1 /*****************************************************************************
2  * engine.c : Run the playlist and handle its control
3  *****************************************************************************
4  * Copyright (C) 1999-2008 VLC authors and VideoLAN
5  *
6  * Authors: Samuel Hocevar <sam@zoy.org>
7  *          ClĂ©ment Stenac <zorglub@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <stddef.h>
29 #include <assert.h>
30 #include <vlc_common.h>
31 #include <vlc_sout.h>
32 #include <vlc_playlist.h>
33 #include <vlc_interface.h>
34 #include "playlist_internal.h"
35 #include "stream_output/stream_output.h" /* sout_DeleteInstance */
36 #include <math.h> /* for fabs() */
37
38 /*****************************************************************************
39  * Local prototypes
40  *****************************************************************************/
41 static void VariablesInit( playlist_t *p_playlist );
42
43 static int RandomCallback( vlc_object_t *p_this, char const *psz_cmd,
44                            vlc_value_t oldval, vlc_value_t newval, void *a )
45 {
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;
49
50     PL_LOCK;
51
52     if( !random ) {
53         pl_priv(p_playlist)->b_reset_currently_playing = true;
54         vlc_cond_signal( &pl_priv(p_playlist)->signal );
55     } else {
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)
59          */
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 );
63         if( p_new )
64             ResyncCurrentIndex( p_playlist, p_new );
65     }
66
67     PL_UNLOCK;
68     return VLC_SUCCESS;
69 }
70
71 static int RateCallback( vlc_object_t *p_this, char const *psz_cmd,
72                          vlc_value_t oldval, vlc_value_t newval, void *p )
73 {
74     (void)psz_cmd; (void)oldval;(void)p;
75     playlist_t *p_playlist = (playlist_t*)p_this;
76
77     PL_LOCK;
78
79     if( pl_priv(p_playlist)->p_input == NULL )
80     {
81         PL_UNLOCK;
82         return VLC_SUCCESS;
83     }
84
85     var_SetFloat( pl_priv( p_playlist )->p_input, "rate", newval.f_float );
86     PL_UNLOCK;
87     return VLC_SUCCESS;
88 }
89
90 static int RateOffsetCallback( vlc_object_t *obj, char const *psz_cmd,
91                                vlc_value_t oldval, vlc_value_t newval, void *p_data )
92 {
93     playlist_t *p_playlist = (playlist_t *)obj;
94     VLC_UNUSED(oldval); VLC_UNUSED(p_data); VLC_UNUSED(newval);
95
96     static const float pf_rate[] = {
97         1.0/64, 1.0/32, 1.0/16, 1.0/8, 1.0/4, 1.0/3, 1.0/2, 2.0/3,
98         1.0/1,
99         3.0/2, 2.0/1, 3.0/1, 4.0/1, 8.0/1, 16.0/1, 32.0/1, 64.0/1,
100     };
101     const size_t i_rate_count = sizeof(pf_rate)/sizeof(*pf_rate);
102
103     float f_rate;
104     struct input_thread_t *input;
105
106     PL_LOCK;
107     input = pl_priv( p_playlist )->p_input;
108     f_rate = var_GetFloat( input ? (vlc_object_t *)input : obj, "rate" );
109     PL_UNLOCK;
110
111     if( !strcmp( psz_cmd, "rate-faster" ) )
112     {
113         /* compensate for input rounding errors */
114         float r = f_rate * 1.1;
115         for( size_t i = 0; i < i_rate_count; i++ )
116             if( r < pf_rate[i] )
117             {
118                 f_rate = pf_rate[i];
119                 break;
120             }
121     }
122     else
123     {
124         /* compensate for input rounding errors */
125         float r = f_rate * .9;
126         for( size_t i = 1; i < i_rate_count; i++ )
127             if( r <= pf_rate[i] )
128             {
129                 f_rate = pf_rate[i - 1];
130                 break;
131             }
132     }
133
134     var_SetFloat( p_playlist, "rate", f_rate );
135     return VLC_SUCCESS;
136 }
137
138 static int VideoSplitterCallback( vlc_object_t *p_this, char const *psz_cmd,
139                                   vlc_value_t oldval, vlc_value_t newval, void *p_data )
140 {
141     playlist_t *p_playlist = (playlist_t*)p_this;
142     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data); VLC_UNUSED(newval);
143
144     PL_LOCK;
145
146     /* Force the input to restart the video ES to force a vout recreation */
147     input_thread_t *p_input = pl_priv( p_playlist )->p_input;
148     if( p_input )
149     {
150         const double f_position = var_GetFloat( p_input, "position" );
151         input_Control( p_input, INPUT_RESTART_ES, -VIDEO_ES );
152         var_SetFloat( p_input, "position", f_position );
153     }
154
155     PL_UNLOCK;
156     return VLC_SUCCESS;
157 }
158
159 /**
160  * Create playlist
161  *
162  * Create a playlist structure.
163  * \param p_parent the vlc object that is to be the parent of this playlist
164  * \return a pointer to the created playlist, or NULL on error
165  */
166 playlist_t * playlist_Create( vlc_object_t *p_parent )
167 {
168     playlist_t *p_playlist;
169     playlist_private_t *p;
170
171     /* Allocate structure */
172     p = vlc_custom_create( p_parent, sizeof( *p ), "playlist" );
173     if( !p )
174         return NULL;
175
176     assert( offsetof( playlist_private_t, public_data ) == 0 );
177     p_playlist = &p->public_data;
178     TAB_INIT( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds );
179
180     libvlc_priv(p_parent->p_libvlc)->p_playlist = p_playlist;
181
182     VariablesInit( p_playlist );
183     vlc_mutex_init( &p->lock );
184     vlc_cond_init( &p->signal );
185     p->killed = false;
186
187     /* Initialise data structures */
188     pl_priv(p_playlist)->i_last_playlist_id = 0;
189     pl_priv(p_playlist)->p_input = NULL;
190
191     ARRAY_INIT( p_playlist->items );
192     ARRAY_INIT( p_playlist->all_items );
193     ARRAY_INIT( pl_priv(p_playlist)->items_to_delete );
194     ARRAY_INIT( p_playlist->current );
195
196     p_playlist->i_current_index = 0;
197     pl_priv(p_playlist)->b_reset_currently_playing = true;
198     pl_priv(p_playlist)->last_rebuild_date = 0;
199
200     pl_priv(p_playlist)->b_tree = var_InheritBool( p_parent, "playlist-tree" );
201
202     pl_priv(p_playlist)->b_doing_ml = false;
203
204     pl_priv(p_playlist)->b_auto_preparse =
205         var_InheritBool( p_parent, "auto-preparse" );
206
207     /* Fetcher */
208     p->p_fetcher = playlist_fetcher_New( p_playlist );
209     if( unlikely(p->p_fetcher == NULL) )
210     {
211         msg_Err( p_playlist, "cannot create fetcher" );
212         p->p_preparser = NULL;
213     }
214     else
215     {   /* Preparse */
216         p->p_preparser = playlist_preparser_New( p_playlist, p->p_fetcher );
217         if( unlikely(p->p_preparser == NULL) )
218             msg_Err( p_playlist, "cannot create preparser" );
219     }
220
221     /* Create the root node */
222     PL_LOCK;
223     p_playlist->p_root = playlist_NodeCreate( p_playlist, NULL, NULL,
224                                     PLAYLIST_END, 0, NULL );
225     PL_UNLOCK;
226     if( !p_playlist->p_root ) return NULL;
227
228     /* Create currently playing items node */
229     PL_LOCK;
230     p_playlist->p_playing = playlist_NodeCreate(
231         p_playlist, _( "Playlist" ), p_playlist->p_root,
232         PLAYLIST_END, PLAYLIST_RO_FLAG, NULL );
233
234     PL_UNLOCK;
235
236     if( !p_playlist->p_playing ) return NULL;
237
238     /* Create media library node */
239     const bool b_ml = var_InheritBool( p_parent, "media-library");
240     if( b_ml )
241     {
242         PL_LOCK;
243         p_playlist->p_media_library = playlist_NodeCreate(
244             p_playlist, _( "Media Library" ), p_playlist->p_root,
245             PLAYLIST_END, PLAYLIST_RO_FLAG, NULL );
246         PL_UNLOCK;
247
248         if(!p_playlist->p_media_library ) return NULL;
249     }
250     else
251     {
252         p_playlist->p_media_library = NULL;
253     }
254
255     p_playlist->p_root_category = p_playlist->p_root;
256     p_playlist->p_root_onelevel = p_playlist->p_root;
257     p_playlist->p_local_category = p_playlist->p_playing;
258     p_playlist->p_local_onelevel = p_playlist->p_playing;
259     p_playlist->p_ml_category = p_playlist->p_media_library;
260     p_playlist->p_ml_onelevel = p_playlist->p_media_library;;
261
262     /* Initial status */
263     pl_priv(p_playlist)->status.p_item = NULL;
264     pl_priv(p_playlist)->status.p_node = p_playlist->p_playing;
265     pl_priv(p_playlist)->request.b_request = false;
266     pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
267
268     if(b_ml)
269     {
270         const bool b_auto_preparse = pl_priv(p_playlist)->b_auto_preparse;
271         pl_priv(p_playlist)->b_auto_preparse = false;
272         playlist_MLLoad( p_playlist );
273         pl_priv(p_playlist)->b_auto_preparse = b_auto_preparse;
274     }
275
276     return p_playlist;
277 }
278
279 /**
280  * Destroy playlist.
281  * This is not thread-safe. Any reference to the playlist is assumed gone.
282  * (In particular, all interface and services threads must have been joined).
283  *
284  * \param p_playlist the playlist object
285  */
286 void playlist_Destroy( playlist_t *p_playlist )
287 {
288     playlist_private_t *p_sys = pl_priv(p_playlist);
289
290     msg_Dbg( p_playlist, "destroying" );
291     if( p_sys->p_preparser )
292         playlist_preparser_Delete( p_sys->p_preparser );
293     if( p_sys->p_fetcher )
294         playlist_fetcher_Delete( p_sys->p_fetcher );
295
296     /* Already cleared when deactivating (if activated anyway) */
297     assert( !p_sys->p_input );
298     assert( !p_sys->p_input_resource );
299
300     vlc_cond_destroy( &p_sys->signal );
301     vlc_mutex_destroy( &p_sys->lock );
302
303     /* Remove all remaining items */
304     FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
305         free( p_del->pp_children );
306         vlc_gc_decref( p_del->p_input );
307         free( p_del );
308     FOREACH_END();
309     ARRAY_RESET( p_playlist->all_items );
310     FOREACH_ARRAY( playlist_item_t *p_del, p_sys->items_to_delete )
311         free( p_del->pp_children );
312         vlc_gc_decref( p_del->p_input );
313         free( p_del );
314     FOREACH_END();
315     ARRAY_RESET( p_sys->items_to_delete );
316
317     ARRAY_RESET( p_playlist->items );
318     ARRAY_RESET( p_playlist->current );
319
320     vlc_object_release( p_playlist );
321 }
322
323 /** Get current playing input.
324  */
325 input_thread_t * playlist_CurrentInput( playlist_t * p_playlist )
326 {
327     input_thread_t * p_input;
328     PL_LOCK;
329     p_input = pl_priv(p_playlist)->p_input;
330     if( p_input ) vlc_object_hold( p_input );
331     PL_UNLOCK;
332     return p_input;
333 }
334
335 /**
336  * @}
337  */
338
339 /** Accessor for status item and status nodes.
340  */
341 playlist_item_t * get_current_status_item( playlist_t * p_playlist )
342 {
343     PL_ASSERT_LOCKED;
344
345     return pl_priv(p_playlist)->status.p_item;
346 }
347
348 playlist_item_t * get_current_status_node( playlist_t * p_playlist )
349 {
350     PL_ASSERT_LOCKED;
351
352     return pl_priv(p_playlist)->status.p_node;
353 }
354
355 void set_current_status_item( playlist_t * p_playlist,
356     playlist_item_t * p_item )
357 {
358     PL_ASSERT_LOCKED;
359
360     if( pl_priv(p_playlist)->status.p_item &&
361         pl_priv(p_playlist)->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG &&
362         pl_priv(p_playlist)->status.p_item != p_item )
363     {
364         /* It's unsafe given current design to delete a playlist item :(
365         playlist_ItemDelete( pl_priv(p_playlist)->status.p_item ); */
366     }
367     pl_priv(p_playlist)->status.p_item = p_item;
368 }
369
370 void set_current_status_node( playlist_t * p_playlist,
371     playlist_item_t * p_node )
372 {
373     PL_ASSERT_LOCKED;
374
375     if( pl_priv(p_playlist)->status.p_node &&
376         pl_priv(p_playlist)->status.p_node->i_flags & PLAYLIST_REMOVE_FLAG &&
377         pl_priv(p_playlist)->status.p_node != p_node )
378     {
379         /* It's unsafe given current design to delete a playlist item :(
380         playlist_ItemDelete( pl_priv(p_playlist)->status.p_node ); */
381     }
382     pl_priv(p_playlist)->status.p_node = p_node;
383 }
384
385 static input_thread_t *playlist_FindInput( vlc_object_t *object )
386 {
387     assert( object == VLC_OBJECT(pl_Get(object)) );
388     return playlist_CurrentInput( (playlist_t *)object );
389 }
390
391 static void VariablesInit( playlist_t *p_playlist )
392 {
393     /* These variables control updates */
394     var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
395     var_SetBool( p_playlist, "intf-change", true );
396
397     var_Create( p_playlist, "item-change", VLC_VAR_ADDRESS );
398     var_Create( p_playlist, "leaf-to-parent", VLC_VAR_INTEGER );
399
400     var_Create( p_playlist, "playlist-item-deleted", VLC_VAR_INTEGER );
401     var_SetInteger( p_playlist, "playlist-item-deleted", -1 );
402
403     var_Create( p_playlist, "playlist-item-append", VLC_VAR_ADDRESS );
404
405     var_Create( p_playlist, "item-current", VLC_VAR_ADDRESS );
406     var_Create( p_playlist, "input-current", VLC_VAR_ADDRESS );
407
408     var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
409     var_SetInteger( p_playlist, "activity", 0 );
410
411     /* Variables to control playback */
412     var_Create( p_playlist, "playlist-autostart", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
413     var_Create( p_playlist, "play-and-stop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
414     var_Create( p_playlist, "play-and-exit", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
415     var_Create( p_playlist, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
416     var_Create( p_playlist, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
417     var_Create( p_playlist, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
418
419     var_Create( p_playlist, "rate", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
420     var_Create( p_playlist, "rate-slower", VLC_VAR_VOID );
421     var_Create( p_playlist, "rate-faster", VLC_VAR_VOID );
422     var_AddCallback( p_playlist, "rate", RateCallback, NULL );
423     var_AddCallback( p_playlist, "rate-slower", RateOffsetCallback, NULL );
424     var_AddCallback( p_playlist, "rate-faster", RateOffsetCallback, NULL );
425
426     var_Create( p_playlist, "video-splitter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
427     var_AddCallback( p_playlist, "video-splitter", VideoSplitterCallback, NULL );
428
429     var_AddCallback( p_playlist, "random", RandomCallback, NULL );
430
431     /* */
432     var_Create( p_playlist, "album-art", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
433
434     /* Variables to preserve video output parameters */
435     var_Create( p_playlist, "fullscreen", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
436     var_Create( p_playlist, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
437
438     /* Audio output parameters */
439     var_Create( p_playlist, "mute", VLC_VAR_BOOL );
440     var_Create( p_playlist, "volume", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
441     /* FIXME: horrible hack for audio output interface code */
442     var_Create( p_playlist, "find-input-callback", VLC_VAR_ADDRESS );
443     var_SetAddress( p_playlist, "find-input-callback", playlist_FindInput );
444 }
445
446 playlist_item_t * playlist_CurrentPlayingItem( playlist_t * p_playlist )
447 {
448     PL_ASSERT_LOCKED;
449
450     return pl_priv(p_playlist)->status.p_item;
451 }
452
453 int playlist_Status( playlist_t * p_playlist )
454 {
455     PL_ASSERT_LOCKED;
456
457     return pl_priv(p_playlist)->status.i_status;
458 }
459