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