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