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