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