]> git.sesse.net Git - vlc/blob - src/playlist/engine.c
376c482a046dcb8c70c2dba636c4799531ac4f10
[vlc] / src / playlist / engine.c
1 /*****************************************************************************
2  * engine.c : Run the playlist and handle its control
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          ClĂ©ment Stenac <zorglub@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 #include <vlc/vlc.h>
25 #include <vlc/vout.h>
26 #include <vlc/sout.h>
27 #include <vlc/input.h>
28 #include "vlc_playlist.h"
29 #include "vlc_interaction.h"
30 #include "playlist_internal.h"
31
32 /*****************************************************************************
33  * Local prototypes
34  *****************************************************************************/
35 static void VariablesInit( playlist_t *p_playlist );
36
37 /**
38  * Create playlist
39  *
40  * Create a playlist structure.
41  * \param p_parent the vlc object that is to be the parent of this playlist
42  * \return a pointer to the created playlist, or NULL on error
43  */
44 playlist_t * playlist_Create( vlc_object_t *p_parent )
45 {
46     playlist_t *p_playlist;
47     int i_tree;
48
49     /* Allocate structure */
50     p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
51     if( !p_playlist )
52     {
53         msg_Err( p_parent, "out of memory" );
54         return NULL;
55     }
56     p_parent->p_libvlc->p_playlist = p_playlist;
57
58     VariablesInit( p_playlist );
59
60     /* Initialise data structures */
61     vlc_mutex_init( p_playlist, &p_playlist->gc_lock );
62     p_playlist->i_last_playlist_id = 0;
63     p_playlist->i_last_input_id = 0;
64     p_playlist->p_input = NULL;
65
66     p_playlist->i_vout_destroyed_date = 0;
67     p_playlist->i_sout_destroyed_date = 0;
68
69     p_playlist->i_size = 0;
70     p_playlist->pp_items = NULL;
71     p_playlist->i_all_size = 0;
72     p_playlist->pp_all_items = NULL;
73
74     p_playlist->i_input_items = 0;
75     p_playlist->pp_input_items = NULL;
76
77     p_playlist->i_random = 0;
78     p_playlist->pp_random = NULL;
79     p_playlist->i_random_index = 0;
80     p_playlist->b_reset_random = VLC_TRUE;
81
82     i_tree = var_CreateGetBool( p_playlist, "playlist-tree" );
83     p_playlist->b_always_tree = (i_tree == 1);
84     p_playlist->b_never_tree = (i_tree == 2);
85
86     p_playlist->b_doing_ml = VLC_FALSE;
87
88     p_playlist->p_root_category = playlist_NodeCreate( p_playlist, NULL, NULL);
89     p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL);
90
91     /* Create playlist and media library */
92     p_playlist->p_local_category = playlist_NodeCreate( p_playlist,
93                                  _( "Playlist" ),p_playlist->p_root_category );
94     p_playlist->p_local_onelevel =  playlist_NodeCreate( p_playlist,
95                                 _( "Playlist" ), p_playlist->p_root_onelevel );
96     p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
97     p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;
98
99     /* Link the nodes together. Todo: actually create them from the same input*/
100     p_playlist->p_local_onelevel->p_input->i_id =
101         p_playlist->p_local_category->p_input->i_id;
102
103     if( config_GetInt( p_playlist, "media-library") )
104     {
105         p_playlist->p_ml_category =   playlist_NodeCreate( p_playlist,
106                            _( "Media Library" ), p_playlist->p_root_category );
107         p_playlist->p_ml_onelevel =  playlist_NodeCreate( p_playlist,
108                            _( "Media Library" ), p_playlist->p_root_onelevel );
109         p_playlist->p_ml_category->i_flags |= PLAYLIST_RO_FLAG;
110         p_playlist->p_ml_onelevel->i_flags |= PLAYLIST_RO_FLAG;
111         p_playlist->p_ml_onelevel->p_input->i_id =
112              p_playlist->p_ml_category->p_input->i_id;
113
114     }
115     else
116     {
117         p_playlist->p_ml_category = p_playlist->p_ml_onelevel = NULL;
118     }
119
120     /* Initial status */
121     p_playlist->status.p_item = NULL;
122     p_playlist->status.p_node = p_playlist->p_local_onelevel;
123     p_playlist->request.b_request = VLC_FALSE;
124     p_playlist->status.i_status = PLAYLIST_STOPPED;
125
126     p_playlist->i_sort = SORT_ID;
127     p_playlist->i_order = ORDER_NORMAL;
128
129     vlc_object_attach( p_playlist, p_parent );
130
131     playlist_MLLoad( p_playlist );
132     return p_playlist;
133 }
134
135 void playlist_Destroy( playlist_t *p_playlist )
136 {
137     while( p_playlist->i_sds )
138     {
139         playlist_ServicesDiscoveryRemove( p_playlist,
140                                           p_playlist->pp_sds[0]->psz_module );
141     }
142
143     playlist_MLDump( p_playlist );
144
145     vlc_thread_join( p_playlist->p_preparse );
146     vlc_thread_join( p_playlist );
147
148     vlc_object_detach( p_playlist->p_preparse );
149
150     var_Destroy( p_playlist, "intf-change" );
151     var_Destroy( p_playlist, "item-change" );
152     var_Destroy( p_playlist, "playlist-current" );
153     var_Destroy( p_playlist, "intf-popmenu" );
154     var_Destroy( p_playlist, "intf-show" );
155     var_Destroy( p_playlist, "play-and-stop" );
156     var_Destroy( p_playlist, "play-and-exit" );
157     var_Destroy( p_playlist, "random" );
158     var_Destroy( p_playlist, "repeat" );
159     var_Destroy( p_playlist, "loop" );
160     var_Destroy( p_playlist, "activity" );
161
162     PL_LOCK;
163     playlist_NodeDelete( p_playlist, p_playlist->p_root_category, VLC_TRUE,
164                          VLC_TRUE );
165     playlist_NodeDelete( p_playlist, p_playlist->p_root_onelevel, VLC_TRUE,
166                          VLC_TRUE );
167     PL_UNLOCK;
168
169     if( p_playlist->p_stats )
170         free( p_playlist->p_stats );
171
172     vlc_mutex_destroy( &p_playlist->gc_lock );
173     vlc_object_destroy( p_playlist->p_preparse );
174     vlc_object_detach( p_playlist );
175     vlc_object_destroy( p_playlist );
176
177 }
178 /* Destroy remaining objects */
179 static mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
180                                        mtime_t destroy_date )
181 {
182     vlc_object_t *p_obj;
183
184     if( destroy_date > mdate() ) return destroy_date;
185
186     if( destroy_date == 0 )
187     {
188         /* give a little time */
189         return mdate() + I64C(1000000);
190     }
191     else
192     {
193         vlc_mutex_lock( &p_playlist->gc_lock );
194         while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
195         {
196             if( p_obj->p_parent != (vlc_object_t*)p_playlist )
197             {
198                 /* only first child (ie unused) */
199                 vlc_object_release( p_obj );
200                 break;
201             }
202             if( i_type == VLC_OBJECT_VOUT )
203             {
204                 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
205                 vlc_object_detach( p_obj );
206                 vlc_object_release( p_obj );
207                 vout_Destroy( (vout_thread_t *)p_obj );
208             }
209             else if( i_type == VLC_OBJECT_SOUT )
210             {
211                 vlc_object_release( p_obj );
212                 sout_DeleteInstance( (sout_instance_t*)p_obj );
213             }
214         }
215         vlc_mutex_unlock( &p_playlist->gc_lock );
216         return 0;
217     }
218 }
219
220 /** Main loop for the playlist */
221 void playlist_MainLoop( playlist_t *p_playlist )
222 {
223     playlist_item_t *p_item = NULL;
224     vlc_bool_t b_playexit = var_GetBool( p_playlist, "play-and-exit" );
225     PL_LOCK
226
227     /* First, check if we have something to do */
228     /* FIXME : this can be called several times */
229     if( p_playlist->request.b_request )
230     {
231         /* Stop the existing input */
232         if( p_playlist->p_input && !p_playlist->p_input->b_die )
233         {
234             PL_DEBUG( "incoming request - stopping current input" );
235             input_StopThread( p_playlist->p_input );
236         }
237     }
238
239     /* If there is an input, check that it doesn't need to die. */
240     if( p_playlist->p_input )
241     {
242         /* This input is dead. Remove it ! */
243         if( p_playlist->p_input->b_dead )
244         {
245             int i_activity;
246             input_thread_t *p_input;
247             PL_DEBUG( "dead input" );
248
249             p_input = p_playlist->p_input;
250             p_playlist->p_input = NULL;
251
252             /* Release the playlist lock, because we may get stuck
253              * in input_DestroyThread() for some time. */
254             PL_UNLOCK
255
256             /* Destroy input */
257             input_DestroyThread( p_input );
258
259             /* Unlink current input
260              * (_after_ input_DestroyThread for vout garbage collector) */
261             vlc_object_detach( p_input );
262
263             /* Destroy object */
264             vlc_object_destroy( p_input );
265
266             p_playlist->i_vout_destroyed_date = 0;
267             p_playlist->i_sout_destroyed_date = 0;
268
269             if( p_playlist->status.p_item->i_flags
270                 & PLAYLIST_REMOVE_FLAG )
271             {
272                  PL_DEBUG( "%s was marked for deletion, deleting",
273                                  PLI_NAME( p_playlist->status.p_item  ) );
274                  playlist_ItemDelete( p_playlist->status.p_item );
275                  if( p_playlist->request.p_item == p_playlist->status.p_item )
276                      p_playlist->request.p_item = NULL;
277                  p_playlist->status.p_item = NULL;
278             }
279
280             i_activity= var_GetInteger( p_playlist, "activity") ;
281             var_SetInteger( p_playlist, "activity", i_activity -
282                             DEFAULT_INPUT_ACTIVITY );
283
284             return;
285         }
286         /* This input is dying, let it do */
287         else if( p_playlist->p_input->b_die )
288         {
289             PL_DEBUG( "dying input" );
290         }
291         /* This input has finished, ask it to die ! */
292         else if( p_playlist->p_input->b_error
293                   || p_playlist->p_input->b_eof )
294         {
295             PL_DEBUG( "finished input" );
296             input_StopThread( p_playlist->p_input );
297             /* Select the next playlist item */
298             PL_UNLOCK
299             return;
300         }
301         else if( p_playlist->p_input->i_state != INIT_S )
302         {
303             PL_UNLOCK;
304             p_playlist->i_vout_destroyed_date =
305                 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
306                                         p_playlist->i_vout_destroyed_date );
307             p_playlist->i_sout_destroyed_date =
308                 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
309                                         p_playlist->i_sout_destroyed_date );
310             PL_LOCK
311         }
312     }
313     else
314     {
315         /* No input. Several cases
316          *  - No request, running status -> start new item
317          *  - No request, stopped status -> collect garbage
318          *  - Request, running requested -> start new item
319          *  - Request, stopped requested -> collect garbage
320          */
321          if( (!p_playlist->request.b_request &&
322               p_playlist->status.i_status != PLAYLIST_STOPPED) ||
323               ( p_playlist->request.b_request &&
324                 p_playlist->request.i_status != PLAYLIST_STOPPED ) )
325          {
326              msg_Dbg( p_playlist, "starting new item" );
327              stats_TimerStart( p_playlist, "Playlist walk",
328                                   STATS_TIMER_PLAYLIST_WALK );
329              p_item = playlist_NextItem( p_playlist );
330              stats_TimerStop( p_playlist, STATS_TIMER_PLAYLIST_WALK );
331
332              if( p_item == NULL )
333              {
334                 msg_Dbg( p_playlist, "nothing to play" );
335                 if( b_playexit == VLC_TRUE )
336                 {
337                     msg_Info( p_playlist, "end of playlist, exiting" );
338                     p_playlist->p_libvlc->b_die = VLC_TRUE;
339                 }
340                 p_playlist->status.i_status = PLAYLIST_STOPPED;
341                 PL_UNLOCK
342                 return;
343              }
344              playlist_PlayItem( p_playlist, p_item );
345          }
346          else
347          {
348              if( p_playlist->status.p_item &&
349                  p_playlist->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG )
350              {
351                  PL_DEBUG( "deleting item marked for deletion" );
352                  playlist_ItemDelete( p_playlist->status.p_item );
353                  p_playlist->status.p_item = NULL;
354              }
355
356              /* Collect garbage */
357              PL_UNLOCK
358              p_playlist->i_sout_destroyed_date =
359              ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
360              p_playlist->i_vout_destroyed_date =
361              ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
362              PL_LOCK
363          }
364     }
365     PL_UNLOCK
366 }
367
368 /** Playlist dying last loop */
369 void playlist_LastLoop( playlist_t *p_playlist )
370 {
371     vlc_object_t *p_obj;
372
373     /* If there is an input, kill it */
374     while( 1 )
375     {
376         PL_LOCK
377
378         if( p_playlist->p_input == NULL )
379         {
380             PL_UNLOCK
381             break;
382         }
383
384         if( p_playlist->p_input->b_dead )
385         {
386             input_thread_t *p_input;
387
388             /* Unlink current input */
389             p_input = p_playlist->p_input;
390             p_playlist->p_input = NULL;
391             PL_UNLOCK
392
393             /* Destroy input */
394             input_DestroyThread( p_input );
395             /* Unlink current input (_after_ input_DestroyThread for vout
396              * garbage collector)*/
397             vlc_object_detach( p_input );
398
399             /* Destroy object */
400             vlc_object_destroy( p_input );
401             continue;
402         }
403         else if( p_playlist->p_input->b_die )
404         {
405             /* This input is dying, leave it alone */
406             ;
407         }
408         else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
409         {
410             input_StopThread( p_playlist->p_input );
411             PL_UNLOCK
412             continue;
413         }
414         else
415         {
416             p_playlist->p_input->b_eof = 1;
417         }
418
419         PL_UNLOCK
420
421         msleep( INTF_IDLE_SLEEP );
422     }
423
424     /* close all remaining sout */
425     while( ( p_obj = vlc_object_find( p_playlist,
426                                       VLC_OBJECT_SOUT, FIND_CHILD ) ) )
427     {
428         vlc_object_release( p_obj );
429         sout_DeleteInstance( (sout_instance_t*)p_obj );
430     }
431
432     /* close all remaining vout */
433     while( ( p_obj = vlc_object_find( p_playlist,
434                                       VLC_OBJECT_VOUT, FIND_CHILD ) ) )
435     {
436         vlc_object_detach( p_obj );
437         vlc_object_release( p_obj );
438         vout_Destroy( (vout_thread_t *)p_obj );
439     }
440 }
441
442 /** Main loop for preparser queue */
443 void playlist_PreparseLoop( playlist_preparse_t *p_obj )
444 {
445     playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
446     int i_activity;
447
448     vlc_mutex_lock( &p_obj->object_lock );
449
450     if( p_obj->i_waiting > 0 )
451     {
452         input_item_t *p_current = p_playlist->p_preparse->pp_waiting[0];
453         REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
454         vlc_mutex_unlock( &p_obj->object_lock );
455         PL_LOCK;
456         if( p_current )
457         {
458             vlc_bool_t b_preparsed = VLC_FALSE;
459             if( strncmp( p_current->psz_uri, "http:", 5 ) &&
460                 strncmp( p_current->psz_uri, "rtsp:", 5 ) &&
461                 strncmp( p_current->psz_uri, "udp:", 4 ) &&
462                 strncmp( p_current->psz_uri, "mms:", 4 ) &&
463                 strncmp( p_current->psz_uri, "cdda:", 4 ) &&
464                 strncmp( p_current->psz_uri, "dvd:", 4 ) &&
465                 strncmp( p_current->psz_uri, "v4l:", 4 ) &&
466                 strncmp( p_current->psz_uri, "dshow:", 6 ) )
467             {
468                 b_preparsed = VLC_TRUE;
469                 stats_TimerStart( p_playlist, "Preparse run",
470                                   STATS_TIMER_PREPARSE );
471                 PL_UNLOCK;
472                 input_Preparse( p_playlist, p_current );
473                 PL_LOCK;
474                 stats_TimerStop( p_playlist, STATS_TIMER_PREPARSE );
475             }
476             PL_UNLOCK;
477             if( b_preparsed )
478             {
479                 var_SetInteger( p_playlist, "item-change",
480                                 p_current->i_id );
481             }
482             vlc_gc_decref( p_current );
483         }
484         else
485         {
486             vlc_mutex_unlock( &p_playlist->object_lock );
487         }
488         vlc_mutex_lock( &p_obj->object_lock );
489         i_activity=  var_GetInteger( p_playlist, "activity" );
490         if( i_activity < 0 ) i_activity = 0;
491         vlc_mutex_unlock( &p_obj->object_lock );
492         msleep( (i_activity+1) * 1000 );
493         return;
494     }
495     vlc_mutex_unlock( &p_obj->object_lock );
496 }
497
498 static void VariablesInit( playlist_t *p_playlist )
499 {
500     vlc_value_t val;
501     /* These variables control updates */
502     var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
503     val.b_bool = VLC_TRUE;
504     var_Set( p_playlist, "intf-change", val );
505
506     var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
507     val.i_int = -1;
508     var_Set( p_playlist, "item-change", val );
509
510     var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
511     val.i_int = -1;
512     var_Set( p_playlist, "item-deleted", val );
513
514     var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
515
516     var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
517     val.i_int = -1;
518     var_Set( p_playlist, "playlist-current", val );
519
520     var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
521
522     var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
523     val.b_bool = VLC_TRUE;
524     var_Set( p_playlist, "intf-show", val );
525
526     var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
527     var_SetInteger( p_playlist, "activity", 0 );
528
529     /* Variables to control playback */
530     var_CreateGetBool( p_playlist, "play-and-stop" );
531     var_CreateGetBool( p_playlist, "play-and-exit" );
532     var_CreateGetBool( p_playlist, "random" );
533     var_CreateGetBool( p_playlist, "repeat" );
534     var_CreateGetBool( p_playlist, "loop" );
535 }