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