]> git.sesse.net Git - vlc/blob - src/playlist/engine.c
Split creation/destruction and activation/deactivation of playlist.
[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     pl_priv(p_playlist)->gc_date = 0;
90     pl_priv(p_playlist)->b_cant_sleep = false;
91
92     ARRAY_INIT( p_playlist->items );
93     ARRAY_INIT( p_playlist->all_items );
94     ARRAY_INIT( pl_priv(p_playlist)->items_to_delete );
95     ARRAY_INIT( p_playlist->current );
96
97     p_playlist->i_current_index = 0;
98     pl_priv(p_playlist)->b_reset_currently_playing = true;
99     pl_priv(p_playlist)->last_rebuild_date = 0;
100
101     pl_priv(p_playlist)->b_tree = var_CreateGetBool( p_playlist, "playlist-tree" );
102
103     pl_priv(p_playlist)->b_doing_ml = false;
104
105     pl_priv(p_playlist)->b_auto_preparse =
106                         var_CreateGetBool( p_playlist, "auto-preparse" ) ;
107
108     PL_LOCK; /* playlist_NodeCreate will check for it */
109     p_playlist->p_root_category = playlist_NodeCreate( p_playlist, NULL, NULL,
110                                     0, NULL );
111     p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL,
112                                     0, p_playlist->p_root_category->p_input );
113     PL_UNLOCK;
114
115     if( !p_playlist->p_root_category || !p_playlist->p_root_onelevel )
116         return NULL;
117
118     /* Create playlist and media library */
119     PL_LOCK; /* playlist_NodesPairCreate will check for it */
120     playlist_NodesPairCreate( p_playlist, _( "Playlist" ),
121                             &p_playlist->p_local_category,
122                             &p_playlist->p_local_onelevel, false );
123     PL_UNLOCK;
124
125     p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
126     p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;
127
128     if( !p_playlist->p_local_category || !p_playlist->p_local_onelevel ||
129         !p_playlist->p_local_category->p_input ||
130         !p_playlist->p_local_onelevel->p_input )
131         return NULL;
132
133     if( config_GetInt( p_playlist, "media-library") )
134     {
135         PL_LOCK; /* playlist_NodesPairCreate will check for it */
136         playlist_NodesPairCreate( p_playlist, _( "Media Library" ),
137                             &p_playlist->p_ml_category,
138                             &p_playlist->p_ml_onelevel, false );
139         PL_UNLOCK;
140
141         if(!p_playlist->p_ml_category || !p_playlist->p_ml_onelevel)
142             return NULL;
143
144         p_playlist->p_ml_category->i_flags |= PLAYLIST_RO_FLAG;
145         p_playlist->p_ml_onelevel->i_flags |= PLAYLIST_RO_FLAG;
146     }
147     else
148     {
149         p_playlist->p_ml_category = p_playlist->p_ml_onelevel = NULL;
150     }
151
152     /* Initial status */
153     pl_priv(p_playlist)->status.p_item = NULL;
154     pl_priv(p_playlist)->status.p_node = p_playlist->p_local_onelevel;
155     pl_priv(p_playlist)->request.b_request = false;
156     pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
157
158     pl_priv(p_playlist)->b_auto_preparse = false;
159     playlist_MLLoad( p_playlist );
160     pl_priv(p_playlist)->b_auto_preparse = true;
161
162     vlc_object_set_destructor( p_playlist, playlist_Destructor );
163
164     return p_playlist;
165 }
166
167 /**
168  * Destroy playlist
169  *
170  * Destroy a playlist structure.
171  * \param p_playlist the playlist object
172  * \return nothing
173  */
174
175 static void playlist_Destructor( vlc_object_t * p_this )
176 {
177     playlist_t *p_playlist = (playlist_t *)p_this;
178     playlist_private_t *p_sys = pl_priv(p_playlist);
179
180     assert( !p_sys->p_preparser );
181     assert( !p_sys->p_fetcher );
182
183     msg_Err( p_this, "Destroyed" );
184 }
185
186 /* Destroy remaining objects */
187 static void ObjectGarbageCollector( playlist_t *p_playlist, bool b_force )
188 {
189     if( !b_force )
190     {
191         if( mdate() - pl_priv(p_playlist)->gc_date < 1000000 )
192         {
193            pl_priv(p_playlist)->b_cant_sleep = true;
194             return;
195         }
196         else if( pl_priv(p_playlist)->gc_date == 0 )
197             return;
198     }
199
200     pl_priv(p_playlist)->b_cant_sleep = false;
201 }
202
203 /* Input Callback */
204 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
205                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
206 {
207     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
208     playlist_t *p_playlist = p_data;
209
210     if( newval.i_int != INPUT_EVENT_STATE &&
211         newval.i_int != INPUT_EVENT_ES )
212         return VLC_SUCCESS;
213
214     PL_LOCK;
215
216     if( newval.i_int == INPUT_EVENT_ES )
217         pl_priv(p_playlist)->gc_date = mdate();
218
219     vlc_object_signal_unlocked( p_playlist );
220
221     PL_UNLOCK;
222     return VLC_SUCCESS;
223 }
224
225 /* Internals */
226 void playlist_release_current_input( playlist_t * p_playlist )
227 {
228     PL_ASSERT_LOCKED;
229
230     if( !pl_priv(p_playlist)->p_input ) return;
231
232     input_thread_t * p_input = pl_priv(p_playlist)->p_input;
233
234     var_DelCallback( p_input, "intf-event", InputEvent, p_playlist );
235     pl_priv(p_playlist)->p_input = NULL;
236
237     /* Release the playlist lock, because we may get stuck
238      * in vlc_object_release() for some time. */
239     PL_UNLOCK;
240     vlc_thread_join( p_input );
241     vlc_object_release( p_input );
242     PL_LOCK;
243 }
244
245 void playlist_set_current_input(
246     playlist_t * p_playlist, input_thread_t * p_input )
247 {
248     PL_ASSERT_LOCKED;
249
250     playlist_release_current_input( p_playlist );
251
252     if( p_input )
253     {
254         vlc_object_hold( p_input );
255         pl_priv(p_playlist)->p_input = p_input;
256
257         var_AddCallback( p_input, "intf-event", InputEvent, p_playlist );
258     }
259 }
260
261 /** Get current playing input.
262  */
263 input_thread_t * playlist_CurrentInput( playlist_t * p_playlist )
264 {
265     input_thread_t * p_input;
266     PL_LOCK;
267     p_input = pl_priv(p_playlist)->p_input;
268     if( p_input ) vlc_object_hold( p_input );
269     PL_UNLOCK;
270     return p_input;
271 }
272
273 /**
274  * @}
275  */
276
277 /** Accessor for status item and status nodes.
278  */
279 playlist_item_t * get_current_status_item( playlist_t * p_playlist )
280 {
281     PL_ASSERT_LOCKED;
282
283     return pl_priv(p_playlist)->status.p_item;
284 }
285
286 playlist_item_t * get_current_status_node( playlist_t * p_playlist )
287 {
288     PL_ASSERT_LOCKED;
289
290     return pl_priv(p_playlist)->status.p_node;
291 }
292
293 void set_current_status_item( playlist_t * p_playlist,
294     playlist_item_t * p_item )
295 {
296     PL_ASSERT_LOCKED;
297
298     if( pl_priv(p_playlist)->status.p_item &&
299         pl_priv(p_playlist)->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG &&
300         pl_priv(p_playlist)->status.p_item != p_item )
301     {
302         /* It's unsafe given current design to delete a playlist item :(
303         playlist_ItemDelete( pl_priv(p_playlist)->status.p_item ); */
304     }
305     pl_priv(p_playlist)->status.p_item = p_item;
306 }
307
308 void set_current_status_node( playlist_t * p_playlist,
309     playlist_item_t * p_node )
310 {
311     PL_ASSERT_LOCKED;
312
313     if( pl_priv(p_playlist)->status.p_node &&
314         pl_priv(p_playlist)->status.p_node->i_flags & PLAYLIST_REMOVE_FLAG &&
315         pl_priv(p_playlist)->status.p_node != p_node )
316     {
317         /* It's unsafe given current design to delete a playlist item :(
318         playlist_ItemDelete( pl_priv(p_playlist)->status.p_node ); */
319     }
320     pl_priv(p_playlist)->status.p_node = p_node;
321 }
322
323 /**
324  * Main loop
325  *
326  * Main loop for the playlist. It should be entered with the
327  * playlist lock (otherwise input event may be lost)
328  * \param p_playlist the playlist object
329  * \return nothing
330  */
331 void playlist_MainLoop( playlist_t *p_playlist )
332 {
333     playlist_item_t *p_item = NULL;
334     bool b_playexit = var_GetBool( p_playlist, "play-and-exit" );
335
336     PL_ASSERT_LOCKED;
337
338     if( pl_priv(p_playlist)->b_reset_currently_playing &&
339         mdate() - pl_priv(p_playlist)->last_rebuild_date > 30000 ) // 30 ms
340     {
341         ResetCurrentlyPlaying( p_playlist, var_GetBool( p_playlist, "random" ),
342                                get_current_status_item( p_playlist ) );
343         pl_priv(p_playlist)->last_rebuild_date = mdate();
344     }
345
346 check_input:
347     /* If there is an input, check that it doesn't need to die. */
348     if( pl_priv(p_playlist)->p_input )
349     {
350         if( pl_priv(p_playlist)->request.b_request && !pl_priv(p_playlist)->p_input->b_die )
351         {
352             PL_DEBUG( "incoming request - stopping current input" );
353             input_StopThread( pl_priv(p_playlist)->p_input );
354         }
355
356         /* This input is dead. Remove it ! */
357         if( pl_priv(p_playlist)->p_input->b_dead )
358         {
359             int i_activity;
360             input_thread_t *p_input;
361             sout_instance_t **pp_sout = &pl_priv(p_playlist)->p_sout;
362
363             PL_DEBUG( "dead input" );
364
365             p_input = pl_priv(p_playlist)->p_input;
366
367             assert( *pp_sout == NULL );
368             if( var_CreateGetBool( p_input, "sout-keep" ) )
369                 *pp_sout = input_DetachSout( p_input );
370
371             /* Destroy input */
372             playlist_release_current_input( p_playlist );
373
374             pl_priv(p_playlist)->gc_date = mdate();
375             pl_priv(p_playlist)->b_cant_sleep = true;
376
377             i_activity= var_GetInteger( p_playlist, "activity" );
378             var_SetInteger( p_playlist, "activity", i_activity -
379                             DEFAULT_INPUT_ACTIVITY );
380
381             goto check_input;
382         }
383         /* This input is dying, let it do */
384         else if( pl_priv(p_playlist)->p_input->b_die )
385         {
386             PL_DEBUG( "dying input" );
387             PL_UNLOCK;
388             msleep( INTF_IDLE_SLEEP );
389             PL_LOCK;
390             goto check_input;
391         }
392         /* This input has finished, ask it to die ! */
393         else if( pl_priv(p_playlist)->p_input->b_error
394                   || pl_priv(p_playlist)->p_input->b_eof )
395         {
396             PL_DEBUG( "finished input" );
397             input_StopThread( pl_priv(p_playlist)->p_input );
398             /* No need to wait here, we'll wait in the p_input->b_die case */
399             goto check_input;
400         }
401         else if( pl_priv(p_playlist)->p_input->i_state != INIT_S )
402         {
403             ObjectGarbageCollector( p_playlist, false );
404         }
405     }
406     else
407     {
408         /* No input. Several cases
409          *  - No request, running status -> start new item
410          *  - No request, stopped status -> collect garbage
411          *  - Request, running requested -> start new item
412          *  - Request, stopped requested -> collect garbage
413         */
414         int i_status = pl_priv(p_playlist)->request.b_request ?
415             pl_priv(p_playlist)->request.i_status : pl_priv(p_playlist)->status.i_status;
416         if( i_status != PLAYLIST_STOPPED )
417         {
418             msg_Dbg( p_playlist, "starting new item" );
419             p_item = playlist_NextItem( p_playlist );
420
421             if( p_item == NULL )
422             {
423                 msg_Dbg( p_playlist, "nothing to play" );
424                 pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
425
426                 if( b_playexit == true )
427                 {
428                     msg_Info( p_playlist, "end of playlist, exiting" );
429                     vlc_object_kill( p_playlist->p_libvlc );
430                 }
431                 ObjectGarbageCollector( p_playlist, true );
432                 return;
433             }
434             playlist_PlayItem( p_playlist, p_item );
435             /* playlist_PlayItem loose input event, we need to recheck */
436             goto check_input;
437         }
438         else
439         {
440             const bool b_gc_forced = pl_priv(p_playlist)->status.i_status != PLAYLIST_STOPPED;
441
442             pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
443
444             /* Collect garbage */
445             ObjectGarbageCollector( p_playlist, b_gc_forced );
446         }
447     }
448 }
449
450 /**
451  * Last loop
452  *
453  * The playlist is dying so do the last loop
454  * \param p_playlist the playlist object
455  * \return nothing
456 */
457 void playlist_LastLoop( playlist_t *p_playlist )
458 {
459     /* If there is an input, kill it */
460     while( 1 )
461     {
462         PL_LOCK;
463         if( pl_priv(p_playlist)->p_input == NULL )
464         {
465             PL_UNLOCK;
466             break;
467         }
468
469         if( pl_priv(p_playlist)->p_input->b_dead )
470         {
471             /* remove input */
472             playlist_release_current_input( p_playlist );
473
474             /* sout-keep: no need to anything here.
475              * The last input will destroy its sout, if any, by itself */
476
477             PL_UNLOCK;
478             continue;
479         }
480         else if( pl_priv(p_playlist)->p_input->b_die )
481         {
482             /* This input is dying, leave it alone */
483             ;
484         }
485         else if( pl_priv(p_playlist)->p_input->b_error || pl_priv(p_playlist)->p_input->b_eof )
486         {
487             input_StopThread( pl_priv(p_playlist)->p_input );
488             PL_UNLOCK;
489             continue;
490         }
491         else
492         {
493             pl_priv(p_playlist)->p_input->b_eof = 1;
494         }
495         PL_UNLOCK;
496
497         msleep( INTF_IDLE_SLEEP );
498     }
499
500 #ifdef ENABLE_SOUT
501     /* close the remaining sout-keep (if there was no input atm) */
502     sout_instance_t *p_sout = pl_priv(p_playlist)->p_sout;
503     if (p_sout)
504         sout_DeleteInstance( p_sout );
505 #endif
506
507     /* Core should have terminated all SDs before the playlist */
508     /* TODO: It fails to do so when not playing anything -- Courmisch */
509     playlist_ServicesDiscoveryKillAll( p_playlist );
510     playlist_MLDump( p_playlist );
511
512     PL_LOCK;
513
514     /* Release the current node */
515     set_current_status_node( p_playlist, NULL );
516
517     /* Release the current item */
518     set_current_status_item( p_playlist, NULL );
519
520     FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
521         free( p_del->pp_children );
522         vlc_gc_decref( p_del->p_input );
523         free( p_del );
524     FOREACH_END();
525     ARRAY_RESET( p_playlist->all_items );
526     FOREACH_ARRAY( playlist_item_t *p_del, pl_priv(p_playlist)->items_to_delete )
527         free( p_del->pp_children );
528         vlc_gc_decref( p_del->p_input );
529         free( p_del );
530     FOREACH_END();
531     ARRAY_RESET( pl_priv(p_playlist)->items_to_delete );
532
533     ARRAY_RESET( p_playlist->items );
534     ARRAY_RESET( p_playlist->current );
535
536     PL_UNLOCK;
537 }
538
539 static void VariablesInit( playlist_t *p_playlist )
540 {
541     vlc_value_t val;
542     /* These variables control updates */
543     var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
544     val.b_bool = true;
545     var_Set( p_playlist, "intf-change", val );
546
547     var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
548     val.i_int = -1;
549     var_Set( p_playlist, "item-change", val );
550
551     var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
552     val.i_int = -1;
553     var_Set( p_playlist, "item-deleted", val );
554
555     var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
556
557     var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
558     val.i_int = -1;
559     var_Set( p_playlist, "playlist-current", val );
560
561     var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
562     var_SetInteger( p_playlist, "activity", 0 );
563
564     /* Variables to control playback */
565     var_Create( p_playlist, "play-and-stop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
566     var_Create( p_playlist, "play-and-exit", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
567     var_Create( p_playlist, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
568     var_Create( p_playlist, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
569     var_Create( p_playlist, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
570
571     var_AddCallback( p_playlist, "random", RandomCallback, NULL );
572
573     /* */
574     var_Create( p_playlist, "album-art", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
575 }
576
577 int playlist_CurrentId( playlist_t * p_playlist )
578 {
579     return pl_priv(p_playlist)->status.p_item->i_id;
580 }
581
582 bool playlist_IsPlaying( playlist_t * p_playlist )
583 {
584     return ( pl_priv(p_playlist)->status.i_status == PLAYLIST_RUNNING &&
585             !(pl_priv(p_playlist)->request.b_request && pl_priv(p_playlist)->request.i_status == PLAYLIST_STOPPED) );
586 }
587
588 playlist_item_t * playlist_CurrentPlayingItem( playlist_t * p_playlist )
589 {
590     return pl_priv(p_playlist)->status.p_item;
591 }
592
593 int playlist_Status( playlist_t * p_playlist )
594 {
595     return pl_priv(p_playlist)->status.i_status;
596 }