]> git.sesse.net Git - vlc/blob - src/playlist/engine.c
Simplify a bit main playlist loop.
[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
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static void VariablesInit( playlist_t *p_playlist );
41 static void playlist_Destructor( vlc_object_t * p_this );
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_object_signal_unlocked( p_playlist );
53
54     PL_UNLOCK;
55     return VLC_SUCCESS;
56 }
57
58 /**
59  * Create playlist
60  *
61  * Create a playlist structure.
62  * \param p_parent the vlc object that is to be the parent of this playlist
63  * \return a pointer to the created playlist, or NULL on error
64  */
65 playlist_t * playlist_Create( vlc_object_t *p_parent )
66 {
67     static const char playlist_name[] = "playlist";
68     playlist_t *p_playlist;
69     playlist_private_t *p;
70
71     /* Allocate structure */
72     p = vlc_custom_create( p_parent, sizeof( *p ),
73                            VLC_OBJECT_GENERIC, playlist_name );
74     if( !p )
75         return NULL;
76
77     assert( offsetof( playlist_private_t, public_data ) == 0 );
78     p_playlist = &p->public_data;
79     TAB_INIT( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds );
80
81     libvlc_priv(p_parent->p_libvlc)->p_playlist = p_playlist;
82
83     VariablesInit( p_playlist );
84
85     /* Initialise data structures */
86     pl_priv(p_playlist)->i_last_playlist_id = 0;
87     pl_priv(p_playlist)->p_input = NULL;
88
89     ARRAY_INIT( p_playlist->items );
90     ARRAY_INIT( p_playlist->all_items );
91     ARRAY_INIT( pl_priv(p_playlist)->items_to_delete );
92     ARRAY_INIT( p_playlist->current );
93
94     p_playlist->i_current_index = 0;
95     pl_priv(p_playlist)->b_reset_currently_playing = true;
96     pl_priv(p_playlist)->last_rebuild_date = 0;
97
98     pl_priv(p_playlist)->b_tree = var_CreateGetBool( p_playlist, "playlist-tree" );
99
100     pl_priv(p_playlist)->b_doing_ml = false;
101
102     pl_priv(p_playlist)->b_auto_preparse =
103                         var_CreateGetBool( p_playlist, "auto-preparse" ) ;
104
105     PL_LOCK; /* playlist_NodeCreate will check for it */
106     p_playlist->p_root_category = playlist_NodeCreate( p_playlist, NULL, NULL,
107                                     0, NULL );
108     p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL,
109                                     0, p_playlist->p_root_category->p_input );
110     PL_UNLOCK;
111
112     if( !p_playlist->p_root_category || !p_playlist->p_root_onelevel )
113         return NULL;
114
115     /* Create playlist and media library */
116     PL_LOCK; /* playlist_NodesPairCreate will check for it */
117     playlist_NodesPairCreate( p_playlist, _( "Playlist" ),
118                             &p_playlist->p_local_category,
119                             &p_playlist->p_local_onelevel, false );
120     PL_UNLOCK;
121
122     p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
123     p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;
124
125     if( !p_playlist->p_local_category || !p_playlist->p_local_onelevel ||
126         !p_playlist->p_local_category->p_input ||
127         !p_playlist->p_local_onelevel->p_input )
128         return NULL;
129
130     if( config_GetInt( p_playlist, "media-library") )
131     {
132         PL_LOCK; /* playlist_NodesPairCreate will check for it */
133         playlist_NodesPairCreate( p_playlist, _( "Media Library" ),
134                             &p_playlist->p_ml_category,
135                             &p_playlist->p_ml_onelevel, false );
136         PL_UNLOCK;
137
138         if(!p_playlist->p_ml_category || !p_playlist->p_ml_onelevel)
139             return NULL;
140
141         p_playlist->p_ml_category->i_flags |= PLAYLIST_RO_FLAG;
142         p_playlist->p_ml_onelevel->i_flags |= PLAYLIST_RO_FLAG;
143     }
144     else
145     {
146         p_playlist->p_ml_category = p_playlist->p_ml_onelevel = NULL;
147     }
148
149     /* Initial status */
150     pl_priv(p_playlist)->status.p_item = NULL;
151     pl_priv(p_playlist)->status.p_node = p_playlist->p_local_onelevel;
152     pl_priv(p_playlist)->request.b_request = false;
153     pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
154
155     pl_priv(p_playlist)->b_auto_preparse = false;
156     playlist_MLLoad( p_playlist );
157     pl_priv(p_playlist)->b_auto_preparse = true;
158
159     vlc_object_set_destructor( p_playlist, playlist_Destructor );
160
161     return p_playlist;
162 }
163
164 /**
165  * Destroy playlist
166  *
167  * Destroy a playlist structure.
168  * \param p_playlist the playlist object
169  * \return nothing
170  */
171
172 static void playlist_Destructor( vlc_object_t * p_this )
173 {
174     playlist_t *p_playlist = (playlist_t *)p_this;
175     playlist_private_t *p_sys = pl_priv(p_playlist);
176
177     assert( !p_sys->p_sout );
178     assert( !p_sys->p_preparser );
179     assert( !p_sys->p_fetcher );
180
181     msg_Err( p_this, "Destroyed" );
182 }
183
184 /* Input Callback */
185 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
186                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
187 {
188     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
189     playlist_t *p_playlist = p_data;
190
191     if( newval.i_int != INPUT_EVENT_STATE &&
192         newval.i_int != INPUT_EVENT_ES )
193         return VLC_SUCCESS;
194
195     PL_LOCK;
196
197     vlc_object_signal_unlocked( p_playlist );
198
199     PL_UNLOCK;
200     return VLC_SUCCESS;
201 }
202
203 /* Internals */
204 void playlist_release_current_input( playlist_t * p_playlist )
205 {
206     PL_ASSERT_LOCKED;
207
208     if( !pl_priv(p_playlist)->p_input ) return;
209
210     input_thread_t * p_input = pl_priv(p_playlist)->p_input;
211
212     var_DelCallback( p_input, "intf-event", InputEvent, p_playlist );
213     pl_priv(p_playlist)->p_input = NULL;
214
215     /* Release the playlist lock, because we may get stuck
216      * in vlc_object_release() for some time. */
217     PL_UNLOCK;
218     vlc_thread_join( p_input );
219     vlc_object_release( p_input );
220     PL_LOCK;
221 }
222
223 void playlist_set_current_input(
224     playlist_t * p_playlist, input_thread_t * p_input )
225 {
226     PL_ASSERT_LOCKED;
227
228     playlist_release_current_input( p_playlist );
229
230     if( p_input )
231     {
232         vlc_object_hold( p_input );
233         pl_priv(p_playlist)->p_input = p_input;
234
235         var_AddCallback( p_input, "intf-event", InputEvent, p_playlist );
236     }
237 }
238
239 /** Get current playing input.
240  */
241 input_thread_t * playlist_CurrentInput( playlist_t * p_playlist )
242 {
243     input_thread_t * p_input;
244     PL_LOCK;
245     p_input = pl_priv(p_playlist)->p_input;
246     if( p_input ) vlc_object_hold( p_input );
247     PL_UNLOCK;
248     return p_input;
249 }
250
251 /**
252  * @}
253  */
254
255 /** Accessor for status item and status nodes.
256  */
257 playlist_item_t * get_current_status_item( playlist_t * p_playlist )
258 {
259     PL_ASSERT_LOCKED;
260
261     return pl_priv(p_playlist)->status.p_item;
262 }
263
264 playlist_item_t * get_current_status_node( playlist_t * p_playlist )
265 {
266     PL_ASSERT_LOCKED;
267
268     return pl_priv(p_playlist)->status.p_node;
269 }
270
271 void set_current_status_item( playlist_t * p_playlist,
272     playlist_item_t * p_item )
273 {
274     PL_ASSERT_LOCKED;
275
276     if( pl_priv(p_playlist)->status.p_item &&
277         pl_priv(p_playlist)->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG &&
278         pl_priv(p_playlist)->status.p_item != p_item )
279     {
280         /* It's unsafe given current design to delete a playlist item :(
281         playlist_ItemDelete( pl_priv(p_playlist)->status.p_item ); */
282     }
283     pl_priv(p_playlist)->status.p_item = p_item;
284 }
285
286 void set_current_status_node( playlist_t * p_playlist,
287     playlist_item_t * p_node )
288 {
289     PL_ASSERT_LOCKED;
290
291     if( pl_priv(p_playlist)->status.p_node &&
292         pl_priv(p_playlist)->status.p_node->i_flags & PLAYLIST_REMOVE_FLAG &&
293         pl_priv(p_playlist)->status.p_node != p_node )
294     {
295         /* It's unsafe given current design to delete a playlist item :(
296         playlist_ItemDelete( pl_priv(p_playlist)->status.p_node ); */
297     }
298     pl_priv(p_playlist)->status.p_node = p_node;
299 }
300
301 /**
302  * Main loop
303  *
304  * Main loop for the playlist. It should be entered with the
305  * playlist lock (otherwise input event may be lost)
306  * \param p_playlist the playlist object
307  * \return nothing
308  */
309 void playlist_MainLoop( playlist_t *p_playlist )
310 {
311     bool b_playexit = var_GetBool( p_playlist, "play-and-exit" );
312
313     PL_ASSERT_LOCKED;
314
315     if( pl_priv(p_playlist)->b_reset_currently_playing &&
316         mdate() - pl_priv(p_playlist)->last_rebuild_date > 30000 ) // 30 ms
317     {
318         ResetCurrentlyPlaying( p_playlist, var_GetBool( p_playlist, "random" ),
319                                get_current_status_item( p_playlist ) );
320         pl_priv(p_playlist)->last_rebuild_date = mdate();
321     }
322
323 check_input:
324     /* If there is an input, check that it doesn't need to die. */
325     if( pl_priv(p_playlist)->p_input )
326     {
327         input_thread_t *p_input = pl_priv(p_playlist)->p_input;
328
329         if( pl_priv(p_playlist)->request.b_request && !p_input->b_die )
330         {
331             PL_DEBUG( "incoming request - stopping current input" );
332             input_StopThread( p_input );
333         }
334
335         /* This input is dead. Remove it ! */
336         if( p_input->b_dead )
337         {
338             sout_instance_t **pp_sout = &pl_priv(p_playlist)->p_sout;
339
340             PL_DEBUG( "dead input" );
341
342             assert( *pp_sout == NULL );
343             if( var_CreateGetBool( p_input, "sout-keep" ) )
344                 *pp_sout = input_DetachSout( p_input );
345
346             /* Destroy input */
347             playlist_release_current_input( p_playlist );
348
349             int i_activity= var_GetInteger( p_playlist, "activity" );
350             var_SetInteger( p_playlist, "activity",
351                             i_activity - DEFAULT_INPUT_ACTIVITY );
352             goto check_input;
353         }
354         /* This input is dying, let it do */
355         else if( p_input->b_die )
356         {
357             PL_DEBUG( "dying input" );
358             PL_UNLOCK;
359             msleep( INTF_IDLE_SLEEP );
360             PL_LOCK;
361             goto check_input;
362         }
363         /* This input has finished, ask it to die ! */
364         else if( p_input->b_error || p_input->b_eof )
365         {
366             PL_DEBUG( "finished input" );
367             input_StopThread( p_input );
368             /* No need to wait here, we'll wait in the p_input->b_die case */
369             goto check_input;
370         }
371     }
372     else
373     {
374         /* No input. Several cases
375          *  - No request, running status -> start new item
376          *  - No request, stopped status -> collect garbage
377          *  - Request, running requested -> start new item
378          *  - Request, stopped requested -> collect garbage
379         */
380         int i_status = pl_priv(p_playlist)->request.b_request ?
381             pl_priv(p_playlist)->request.i_status : pl_priv(p_playlist)->status.i_status;
382         if( i_status != PLAYLIST_STOPPED )
383         {
384             msg_Dbg( p_playlist, "starting new item" );
385             playlist_item_t *p_item = playlist_NextItem( p_playlist );
386
387             if( p_item == NULL )
388             {
389                 msg_Dbg( p_playlist, "nothing to play" );
390                 pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
391
392                 if( b_playexit == true )
393                 {
394                     msg_Info( p_playlist, "end of playlist, exiting" );
395                     vlc_object_kill( p_playlist->p_libvlc );
396                 }
397                 return;
398             }
399             playlist_PlayItem( p_playlist, p_item );
400             /* playlist_PlayItem loose input event, we need to recheck */
401             goto check_input;
402         }
403         else
404         {
405             pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
406         }
407     }
408 }
409
410 static void VariablesInit( playlist_t *p_playlist )
411 {
412     vlc_value_t val;
413     /* These variables control updates */
414     var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
415     val.b_bool = true;
416     var_Set( p_playlist, "intf-change", val );
417
418     var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
419     val.i_int = -1;
420     var_Set( p_playlist, "item-change", val );
421
422     var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
423     val.i_int = -1;
424     var_Set( p_playlist, "item-deleted", val );
425
426     var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
427
428     var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
429     val.i_int = -1;
430     var_Set( p_playlist, "playlist-current", val );
431
432     var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
433     var_SetInteger( p_playlist, "activity", 0 );
434
435     /* Variables to control playback */
436     var_Create( p_playlist, "play-and-stop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
437     var_Create( p_playlist, "play-and-exit", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
438     var_Create( p_playlist, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
439     var_Create( p_playlist, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
440     var_Create( p_playlist, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
441
442     var_AddCallback( p_playlist, "random", RandomCallback, NULL );
443
444     /* */
445     var_Create( p_playlist, "album-art", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
446 }
447
448 int playlist_CurrentId( playlist_t * p_playlist )
449 {
450     return pl_priv(p_playlist)->status.p_item->i_id;
451
452 }
453
454 bool playlist_IsPlaying( playlist_t * p_playlist )
455 {
456     return ( pl_priv(p_playlist)->status.i_status == PLAYLIST_RUNNING &&
457             !(pl_priv(p_playlist)->request.b_request && pl_priv(p_playlist)->request.i_status == PLAYLIST_STOPPED) );
458 }
459
460 playlist_item_t * playlist_CurrentPlayingItem( playlist_t * p_playlist )
461 {
462     return pl_priv(p_playlist)->status.p_item;
463 }
464
465 int playlist_Status( playlist_t * p_playlist )
466 {
467     return pl_priv(p_playlist)->status.i_status;
468 }