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