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