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