]> git.sesse.net Git - vlc/blob - src/playlist/engine.c
6c798e1fc69ae4050bec030ffe0a488b211ba799
[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
25 #include <vlc/vlc.h>
26 #include <vlc/vout.h>
27 #include <vlc/sout.h>
28 #include <vlc/input.h>
29 #include "vlc_playlist.h"
30 #include "vlc_interaction.h"
31 #include "playlist_internal.h"
32
33 /*****************************************************************************
34  * Local prototypes
35  *****************************************************************************/
36 static void VariablesInit( playlist_t *p_playlist );
37
38 static int RandomCallback( vlc_object_t *p_this, char const *psz_cmd,
39                            vlc_value_t oldval, vlc_value_t newval, void *a )
40 {
41     ((playlist_t*)p_this)->b_reset_currently_playing = VLC_TRUE;
42     playlist_Signal( ((playlist_t*)p_this) );
43     return VLC_SUCCESS;
44 }
45
46 /**
47  * Create playlist
48  *
49  * Create a playlist structure.
50  * \param p_parent the vlc object that is to be the parent of this playlist
51  * \return a pointer to the created playlist, or NULL on error
52  */
53 playlist_t * playlist_Create( vlc_object_t *p_parent )
54 {
55     playlist_t *p_playlist;
56     int i_tree;
57
58     /* Allocate structure */
59     p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
60     if( !p_playlist )
61     {
62         msg_Err( p_parent, "out of memory" );
63         return NULL;
64     }
65     p_parent->p_libvlc->p_playlist = p_playlist;
66
67     VariablesInit( p_playlist );
68
69     /* Initialise data structures */
70     vlc_mutex_init( p_playlist, &p_playlist->gc_lock );
71     p_playlist->i_last_playlist_id = 0;
72     p_playlist->i_last_input_id = 0;
73     p_playlist->p_input = NULL;
74
75     p_playlist->gc_date = 0;
76     p_playlist->b_cant_sleep = VLC_FALSE;
77
78     ARRAY_INIT( p_playlist->items );
79     ARRAY_INIT( p_playlist->all_items );
80     ARRAY_INIT( p_playlist->input_items );
81     ARRAY_INIT( p_playlist->current );
82
83     p_playlist->i_current_index = 0;
84     p_playlist->b_reset_currently_playing = VLC_TRUE;
85     p_playlist->last_rebuild_date = 0;
86
87     i_tree = var_CreateGetBool( p_playlist, "playlist-tree" );
88     p_playlist->b_always_tree = (i_tree == 1);
89     p_playlist->b_never_tree = (i_tree == 2);
90
91     p_playlist->b_doing_ml = VLC_FALSE;
92
93     p_playlist->p_root_category = playlist_NodeCreate( p_playlist, NULL, NULL);
94     p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL);
95
96     /* Create playlist and media library */
97     p_playlist->p_local_category = playlist_NodeCreate( p_playlist,
98                                  _( "Playlist" ),p_playlist->p_root_category );
99     p_playlist->p_local_onelevel =  playlist_NodeCreate( p_playlist,
100                                 _( "Playlist" ), p_playlist->p_root_onelevel );
101     p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
102     p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;
103
104     /* Link the nodes together. Todo: actually create them from the same input*/
105     p_playlist->p_local_onelevel->p_input->i_id =
106         p_playlist->p_local_category->p_input->i_id;
107
108     if( config_GetInt( p_playlist, "media-library") )
109     {
110         p_playlist->p_ml_category =   playlist_NodeCreate( p_playlist,
111                            _( "Media Library" ), p_playlist->p_root_category );
112         p_playlist->p_ml_onelevel =  playlist_NodeCreate( p_playlist,
113                            _( "Media Library" ), p_playlist->p_root_onelevel );
114         p_playlist->p_ml_category->i_flags |= PLAYLIST_RO_FLAG;
115         p_playlist->p_ml_onelevel->i_flags |= PLAYLIST_RO_FLAG;
116         p_playlist->p_ml_onelevel->p_input->i_id =
117              p_playlist->p_ml_category->p_input->i_id;
118
119     }
120     else
121     {
122         p_playlist->p_ml_category = p_playlist->p_ml_onelevel = NULL;
123     }
124
125     /* Initial status */
126     p_playlist->status.p_item = NULL;
127     p_playlist->status.p_node = p_playlist->p_local_onelevel;
128     p_playlist->request.b_request = VLC_FALSE;
129     p_playlist->status.i_status = PLAYLIST_STOPPED;
130
131     p_playlist->i_sort = SORT_ID;
132     p_playlist->i_order = ORDER_NORMAL;
133
134     vlc_object_attach( p_playlist, p_parent );
135
136     playlist_MLLoad( p_playlist );
137     return p_playlist;
138 }
139
140 void playlist_Destroy( playlist_t *p_playlist )
141 {
142     while( p_playlist->i_sds )
143     {
144         playlist_ServicesDiscoveryRemove( p_playlist,
145                                           p_playlist->pp_sds[0]->psz_module );
146     }
147
148     playlist_MLDump( p_playlist );
149
150     vlc_thread_join( p_playlist->p_preparse );
151     vlc_thread_join( p_playlist->p_fetcher );
152     vlc_thread_join( p_playlist );
153
154     vlc_object_detach( p_playlist->p_preparse );
155     vlc_object_detach( p_playlist->p_fetcher );
156
157     var_Destroy( p_playlist, "intf-change" );
158     var_Destroy( p_playlist, "item-change" );
159     var_Destroy( p_playlist, "playlist-current" );
160     var_Destroy( p_playlist, "intf-popmenu" );
161     var_Destroy( p_playlist, "intf-show" );
162     var_Destroy( p_playlist, "play-and-stop" );
163     var_Destroy( p_playlist, "play-and-exit" );
164     var_Destroy( p_playlist, "random" );
165     var_Destroy( p_playlist, "repeat" );
166     var_Destroy( p_playlist, "loop" );
167     var_Destroy( p_playlist, "activity" );
168
169     PL_LOCK;
170     /* Go through all items, and simply free everything without caring
171      * about the tree structure. Do not decref, it will be done by doing
172      * the same thing on the input items array */
173     FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
174         free( p_del->pp_children );
175         free( p_del );
176     FOREACH_END();
177     ARRAY_RESET( p_playlist->all_items );
178
179     FOREACH_ARRAY( input_item_t *p_del, p_playlist->input_items )
180         input_ItemClean( p_del );
181         free( p_del );
182     FOREACH_END();
183     ARRAY_RESET( p_playlist->input_items );
184
185     ARRAY_RESET( p_playlist->items );
186     ARRAY_RESET( p_playlist->current );
187
188     PL_UNLOCK;
189
190     if( p_playlist->p_stats )
191         free( p_playlist->p_stats );
192
193     vlc_mutex_destroy( &p_playlist->gc_lock );
194     vlc_object_destroy( p_playlist->p_preparse );
195     vlc_object_destroy( p_playlist->p_fetcher );
196     vlc_object_detach( p_playlist );
197     vlc_object_destroy( p_playlist );
198
199 }
200
201 /* Destroy remaining objects */
202 static void ObjectGarbageCollector( playlist_t *p_playlist )
203 {
204     vlc_object_t *p_obj;
205
206     if( mdate() - p_playlist->gc_date < 1000000 )
207     {
208         p_playlist->b_cant_sleep = VLC_TRUE;
209         return;
210     }
211     else if( p_playlist->gc_date == 0 )
212         return;
213
214     vlc_mutex_lock( &p_playlist->gc_lock );
215     while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_VOUT,
216                                                   FIND_CHILD ) ) )
217     {
218         if( p_obj->p_parent != (vlc_object_t*)p_playlist )
219         {
220             vlc_object_release( p_obj );
221             break;
222         }
223         msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
224         vlc_object_detach( p_obj );
225         vlc_object_release( p_obj );
226         vout_Destroy( (vout_thread_t *)p_obj );
227     }
228     while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_SOUT,
229                                                   FIND_CHILD ) ) )
230     {
231         if( p_obj->p_parent != (vlc_object_t*)p_playlist )
232         {
233             vlc_object_release( p_obj );
234             break;
235         }
236         vlc_object_release( p_obj );
237         sout_DeleteInstance( (sout_instance_t*)p_obj );
238     }
239     p_playlist->b_cant_sleep = VLC_FALSE;
240     vlc_mutex_unlock( &p_playlist->gc_lock );
241 }
242
243 /** Main loop for the playlist */
244 void playlist_MainLoop( playlist_t *p_playlist )
245 {
246     playlist_item_t *p_item = NULL;
247     vlc_bool_t b_playexit = var_GetBool( p_playlist, "play-and-exit" );
248     PL_LOCK;
249
250     if( p_playlist->b_reset_currently_playing &&
251         mdate() - p_playlist->last_rebuild_date > 30000 ) // 30 ms
252     {
253         ResetCurrentlyPlaying( p_playlist, var_GetBool( p_playlist, "random"),
254                              p_playlist->status.p_item );
255         p_playlist->last_rebuild_date = mdate();
256     }
257
258 check_input:
259     /* If there is an input, check that it doesn't need to die. */
260     if( p_playlist->p_input )
261     {
262         if( p_playlist->request.b_request && !p_playlist->p_input->b_die )
263         {
264             PL_DEBUG( "incoming request - stopping current input" );
265             input_StopThread( p_playlist->p_input );
266         }
267
268         /* This input is dead. Remove it ! */
269         if( p_playlist->p_input->b_dead )
270         {
271             int i_activity;
272             input_thread_t *p_input;
273             PL_DEBUG( "dead input" );
274
275             p_input = p_playlist->p_input;
276             p_playlist->p_input = NULL;
277
278             /* Release the playlist lock, because we may get stuck
279              * in input_DestroyThread() for some time. */
280             PL_UNLOCK
281
282             /* Destroy input */
283             input_DestroyThread( p_input );
284
285             /* Unlink current input
286              * (_after_ input_DestroyThread for vout garbage collector) */
287             vlc_object_detach( p_input );
288
289             /* Destroy object */
290             vlc_object_destroy( p_input );
291
292             PL_LOCK;
293
294             p_playlist->gc_date = mdate();
295             p_playlist->b_cant_sleep = VLC_TRUE;
296
297             if( p_playlist->status.p_item->i_flags
298                 & PLAYLIST_REMOVE_FLAG )
299             {
300                  PL_DEBUG( "%s was marked for deletion, deleting",
301                                  PLI_NAME( p_playlist->status.p_item  ) );
302                  playlist_ItemDelete( p_playlist->status.p_item );
303                  if( p_playlist->request.p_item == p_playlist->status.p_item )
304                      p_playlist->request.p_item = NULL;
305                  p_playlist->status.p_item = NULL;
306             }
307
308             i_activity= var_GetInteger( p_playlist, "activity") ;
309             var_SetInteger( p_playlist, "activity", i_activity -
310                             DEFAULT_INPUT_ACTIVITY );
311             goto check_input;
312         }
313         /* This input is dying, let it do */
314         else if( p_playlist->p_input->b_die )
315         {
316             PL_DEBUG( "dying input" );
317             msleep( 25000 ); // 25 ms
318             goto check_input;
319         }
320         /* This input has finished, ask it to die ! */
321         else if( p_playlist->p_input->b_error
322                   || p_playlist->p_input->b_eof )
323         {
324             PL_DEBUG( "finished input" );
325             input_StopThread( p_playlist->p_input );
326             /* No need to wait here, we'll wait in the p_input->b_die case */
327             goto check_input;
328         }
329         else if( p_playlist->p_input->i_state != INIT_S )
330         {
331             PL_UNLOCK;
332             ObjectGarbageCollector( p_playlist );
333             PL_LOCK;
334         }
335     }
336     else
337     {
338         /* No input. Several cases
339          *  - No request, running status -> start new item
340          *  - No request, stopped status -> collect garbage
341          *  - Request, running requested -> start new item
342          *  - Request, stopped requested -> collect garbage
343          */
344          if( (!p_playlist->request.b_request &&
345               p_playlist->status.i_status != PLAYLIST_STOPPED) ||
346               ( p_playlist->request.b_request &&
347                 p_playlist->request.i_status != PLAYLIST_STOPPED ) )
348          {
349              msg_Dbg( p_playlist, "starting new item" );
350              p_item = playlist_NextItem( p_playlist );
351
352              if( p_item == NULL )
353              {
354                 msg_Dbg( p_playlist, "nothing to play" );
355                 if( b_playexit == VLC_TRUE )
356                 {
357                     msg_Info( p_playlist, "end of playlist, exiting" );
358                     p_playlist->p_libvlc->b_die = VLC_TRUE;
359                 }
360                 p_playlist->status.i_status = PLAYLIST_STOPPED;
361                 PL_UNLOCK
362                 return;
363              }
364              playlist_PlayItem( p_playlist, p_item );
365          }
366          else
367          {
368             p_playlist->status.i_status = PLAYLIST_STOPPED;
369             if( p_playlist->status.p_item &&
370                 p_playlist->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG )
371             {
372                 PL_DEBUG( "deleting item marked for deletion" );
373                 playlist_ItemDelete( p_playlist->status.p_item );
374                 p_playlist->status.p_item = NULL;
375             }
376
377             /* Collect garbage */
378             PL_UNLOCK;
379             ObjectGarbageCollector( p_playlist );
380             PL_LOCK;
381         }
382     }
383     PL_UNLOCK
384 }
385
386 /** Playlist dying last loop */
387 void playlist_LastLoop( playlist_t *p_playlist )
388 {
389     vlc_object_t *p_obj;
390
391     /* If there is an input, kill it */
392     while( 1 )
393     {
394         PL_LOCK
395
396         if( p_playlist->p_input == NULL )
397         {
398             PL_UNLOCK
399             break;
400         }
401
402         if( p_playlist->p_input->b_dead )
403         {
404             input_thread_t *p_input;
405
406             /* Unlink current input */
407             p_input = p_playlist->p_input;
408             p_playlist->p_input = NULL;
409             PL_UNLOCK
410
411             /* Destroy input */
412             input_DestroyThread( p_input );
413             /* Unlink current input (_after_ input_DestroyThread for vout
414              * garbage collector)*/
415             vlc_object_detach( p_input );
416
417             /* Destroy object */
418             vlc_object_destroy( p_input );
419             continue;
420         }
421         else if( p_playlist->p_input->b_die )
422         {
423             /* This input is dying, leave it alone */
424             ;
425         }
426         else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
427         {
428             input_StopThread( p_playlist->p_input );
429             PL_UNLOCK
430             continue;
431         }
432         else
433         {
434             p_playlist->p_input->b_eof = 1;
435         }
436
437         PL_UNLOCK
438
439         msleep( INTF_IDLE_SLEEP );
440     }
441
442     /* close all remaining sout */
443     while( ( p_obj = vlc_object_find( p_playlist,
444                                       VLC_OBJECT_SOUT, FIND_CHILD ) ) )
445     {
446         vlc_object_release( p_obj );
447         sout_DeleteInstance( (sout_instance_t*)p_obj );
448     }
449
450     /* close all remaining vout */
451     while( ( p_obj = vlc_object_find( p_playlist,
452                                       VLC_OBJECT_VOUT, FIND_CHILD ) ) )
453     {
454         vlc_object_detach( p_obj );
455         vlc_object_release( p_obj );
456         vout_Destroy( (vout_thread_t *)p_obj );
457     }
458 }
459
460 /** Main loop for preparser queue */
461 void playlist_PreparseLoop( playlist_preparse_t *p_obj )
462 {
463     playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
464     input_item_t *p_current;
465     int i_activity;
466     uint32_t i_m, i_o;
467
468     while( !p_playlist->b_die )
469     {
470         vlc_mutex_lock( &p_obj->object_lock );
471         while( p_obj->i_waiting == 0 )
472         {
473             vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
474             if( p_playlist->b_die )
475             {
476                 vlc_mutex_unlock( &p_obj->object_lock );
477                 return;
478             }
479         }
480
481         p_current = p_obj->pp_waiting[0];
482         REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
483         vlc_mutex_unlock( &p_obj->object_lock );
484
485         PL_LOCK;
486         if( p_current )
487         {
488             vlc_bool_t b_preparsed = VLC_FALSE;
489             if( strncmp( p_current->psz_uri, "http:", 5 ) &&
490                 strncmp( p_current->psz_uri, "rtsp:", 5 ) &&
491                 strncmp( p_current->psz_uri, "udp:", 4 ) &&
492                 strncmp( p_current->psz_uri, "mms:", 4 ) &&
493                 strncmp( p_current->psz_uri, "cdda:", 4 ) &&
494                 strncmp( p_current->psz_uri, "dvd:", 4 ) &&
495                 strncmp( p_current->psz_uri, "v4l:", 4 ) &&
496                 strncmp( p_current->psz_uri, "dshow:", 6 ) )
497             {
498                 b_preparsed = VLC_TRUE;
499                 stats_TimerStart( p_playlist, "Preparse run",
500                                   STATS_TIMER_PREPARSE );
501                 PL_UNLOCK;
502                 input_Preparse( p_playlist, p_current );
503                 PL_LOCK;
504                 stats_TimerStop( p_playlist, STATS_TIMER_PREPARSE );
505             }
506             PL_UNLOCK;
507             if( b_preparsed )
508             {
509                 p_current->p_meta->i_status |= ITEM_PREPARSED;
510                 var_SetInteger( p_playlist, "item-change", p_current->i_id );
511             }
512             PL_LOCK;
513
514             /* If we haven't retrieved enough meta, add to secondary queue
515              * which will run the "meta fetchers".
516              * This only checks for meta, not for art
517              * \todo don't do this for things we won't get meta for, like vids
518              */
519             if( !input_MetaSatisfied( p_playlist, p_current, &i_m, &i_o ) )
520             {
521                 preparse_item_t p;
522                 PL_DEBUG("need to fetch meta for %s", p_current->psz_name );
523                 p.p_item = p_current;
524                 p.b_fetch_art = VLC_FALSE;
525                 vlc_mutex_lock( &p_playlist->p_fetcher->object_lock );
526                 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
527                              p_playlist->p_fetcher->i_waiting,
528                              p_playlist->p_fetcher->i_waiting,
529                              p );
530                 vlc_mutex_unlock( &p_playlist->p_fetcher->object_lock );
531                 vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
532             }
533             /* We already have all needed meta, but we need art right now */
534             else if( p_playlist->p_fetcher->i_art_policy == ALBUM_ART_ALL &&
535                      EMPTY_STR( p_current->p_meta->psz_arturl ) )
536             {
537                 preparse_item_t p;
538                 PL_DEBUG("meta ok for %s, need to fetch art",
539                                                          p_current->psz_name );
540                 p.p_item = p_current;
541                 p.b_fetch_art = VLC_TRUE;
542                 vlc_mutex_lock( &p_playlist->p_fetcher->object_lock );
543                 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
544                              p_playlist->p_fetcher->i_waiting,
545                              p_playlist->p_fetcher->i_waiting,
546                              p );
547                 vlc_mutex_unlock( &p_playlist->p_fetcher->object_lock );
548                 vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
549             }
550             else
551             {
552                 PL_DEBUG( "no fetch required for %s (art currently %s)",
553                           p_current->psz_name, p_current->p_meta->psz_arturl );
554                 vlc_gc_decref( p_current );
555             }
556             PL_UNLOCK;
557         }
558         else
559             PL_UNLOCK;
560
561         vlc_mutex_lock( &p_obj->object_lock );
562         i_activity = var_GetInteger( p_playlist, "activity" );
563         if( i_activity < 0 ) i_activity = 0;
564         vlc_mutex_unlock( &p_obj->object_lock );
565         /* Sleep at least 1ms */
566         msleep( (i_activity+1) * 1000 );
567     }
568 }
569
570 /** Main loop for secondary preparser queue */
571 void playlist_FetcherLoop( playlist_fetcher_t *p_obj )
572 {
573     playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
574     vlc_bool_t b_fetch_art;
575     input_item_t *p_item;
576     int i_activity;
577
578     while( !p_playlist->b_die )
579     {
580         vlc_mutex_lock( &p_obj->object_lock );
581         while( p_obj->i_waiting == 0 )
582         {
583             vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
584             if( p_playlist->b_die )
585             {
586                 vlc_mutex_unlock( &p_obj->object_lock );
587                 return;
588             }
589         }
590
591         b_fetch_art = p_obj->p_waiting->b_fetch_art;
592         p_item = p_obj->p_waiting->p_item;
593         REMOVE_ELEM( p_obj->p_waiting, p_obj->i_waiting, 0 );
594         vlc_mutex_unlock( &p_obj->object_lock );
595         if( p_item )
596         {
597             if( !b_fetch_art )
598             {
599                 input_MetaFetch( p_playlist, p_item );
600                 p_item->p_meta->i_status |= ITEM_META_FETCHED;
601                 var_SetInteger( p_playlist, "item-change", p_item->i_id );
602                 /*  Fetch right now */
603                 if( p_playlist->p_fetcher->i_art_policy == ALBUM_ART_ALL )
604                 {
605                     vlc_mutex_lock( &p_obj->object_lock );
606                     preparse_item_t p;
607                     p.p_item = p_item;
608                     p.b_fetch_art = VLC_TRUE;
609                     INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
610                                  p_playlist->p_fetcher->i_waiting,
611                                  0, p );
612                     PL_DEBUG("meta fetched for %s, get art", p_item->psz_name);
613                     vlc_mutex_unlock( &p_obj->object_lock );
614                     continue;
615                 }
616                 else
617                     vlc_gc_decref( p_item );
618             }
619             else
620             {
621                 int i_ret = input_ArtFind( p_playlist, p_item );
622                 if( i_ret == 1 )
623                 {
624                     PL_DEBUG("downloading art for %s", p_item->psz_name );
625                     if( !input_DownloadAndCacheArt( p_playlist, p_item ) )
626                         p_item->p_meta->i_status |= ITEM_ART_NOTFOUND;
627                     else
628                         p_item->p_meta->i_status |= ITEM_ART_FETCHED;
629                 }
630                 else if( i_ret == 0 ) /* Was in cache */
631                 {
632                     PL_DEBUG("found art for %s in cache", p_item->psz_name );
633                     p_item->p_meta->i_status |= ITEM_ART_FETCHED;
634                 }
635                 else
636                 {
637                     PL_DEBUG("art not found for %s", p_item->psz_name );
638                     p_item->p_meta->i_status |= ITEM_ART_NOTFOUND;
639                 }
640                 vlc_gc_decref( p_item );
641            }
642         }
643         vlc_mutex_lock( &p_obj->object_lock );
644         i_activity = var_GetInteger( p_playlist, "activity" );
645         if( i_activity < 0 ) i_activity = 0;
646         vlc_mutex_unlock( &p_obj->object_lock );
647         /* Sleep at least 1ms */
648         msleep( (i_activity+1) * 1000 );
649     }
650 }
651
652 static void VariablesInit( playlist_t *p_playlist )
653 {
654     vlc_value_t val;
655     /* These variables control updates */
656     var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
657     val.b_bool = VLC_TRUE;
658     var_Set( p_playlist, "intf-change", val );
659
660     var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
661     val.i_int = -1;
662     var_Set( p_playlist, "item-change", val );
663
664     var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
665     val.i_int = -1;
666     var_Set( p_playlist, "item-deleted", val );
667
668     var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
669
670     var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
671     val.i_int = -1;
672     var_Set( p_playlist, "playlist-current", val );
673
674     var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
675
676     var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
677     val.b_bool = VLC_TRUE;
678     var_Set( p_playlist, "intf-show", val );
679
680     var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
681     var_SetInteger( p_playlist, "activity", 0 );
682
683     /* Variables to control playback */
684     var_CreateGetBool( p_playlist, "play-and-stop" );
685     var_CreateGetBool( p_playlist, "play-and-exit" );
686     var_CreateGetBool( p_playlist, "random" );
687     var_CreateGetBool( p_playlist, "repeat" );
688     var_CreateGetBool( p_playlist, "loop" );
689
690     var_AddCallback( p_playlist, "random", RandomCallback, NULL );
691 }