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