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