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