]> git.sesse.net Git - vlc/blob - src/playlist/engine.c
playlist/services_discovery.c: Handle removal of a service.
[vlc] / 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, vlc_bool_t b_force )
182 {
183     vlc_object_t *p_obj;
184
185     if( !b_force )
186     {
187         if( mdate() - p_playlist->gc_date < 1000000 )
188         {
189             p_playlist->b_cant_sleep = VLC_TRUE;
190             return;
191         }
192         else if( p_playlist->gc_date == 0 )
193             return;
194     }
195
196     vlc_mutex_lock( &p_playlist->gc_lock );
197     while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_VOUT,
198                                                   FIND_CHILD ) ) )
199     {
200         if( p_obj->p_parent != VLC_OBJECT(p_playlist) )
201         {
202             vlc_object_release( p_obj );
203             break;
204         }
205         msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
206         vlc_object_detach( p_obj );
207         vlc_object_release( p_obj );
208         vout_Destroy( (vout_thread_t *)p_obj );
209     }
210     while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_SOUT,
211                                                   FIND_CHILD ) ) )
212     {
213         if( p_obj->p_parent != VLC_OBJECT(p_playlist) )
214         {
215             vlc_object_release( p_obj );
216             break;
217         }
218         msg_Dbg( p_playlist, "garbage collector destroying 1 sout" );
219         vlc_object_detach( p_obj );
220         vlc_object_release( p_obj );
221         sout_DeleteInstance( (sout_instance_t*)p_obj );
222     }
223     p_playlist->b_cant_sleep = VLC_FALSE;
224     vlc_mutex_unlock( &p_playlist->gc_lock );
225 }
226
227 /** Main loop for the playlist */
228 void playlist_MainLoop( playlist_t *p_playlist )
229 {
230     playlist_item_t *p_item = NULL;
231     vlc_bool_t b_playexit = var_GetBool( p_playlist, "play-and-exit" );
232     PL_LOCK;
233
234     if( p_playlist->b_reset_currently_playing &&
235         mdate() - p_playlist->last_rebuild_date > 30000 ) // 30 ms
236     {
237         ResetCurrentlyPlaying( p_playlist, var_GetBool( p_playlist, "random"),
238                              p_playlist->status.p_item );
239         p_playlist->last_rebuild_date = mdate();
240     }
241
242 check_input:
243     /* If there is an input, check that it doesn't need to die. */
244     if( p_playlist->p_input )
245     {
246         if( p_playlist->request.b_request && !p_playlist->p_input->b_die )
247         {
248             PL_DEBUG( "incoming request - stopping current input" );
249             input_StopThread( p_playlist->p_input );
250         }
251
252         /* This input is dead. Remove it ! */
253         if( p_playlist->p_input->b_dead )
254         {
255             int i_activity;
256             input_thread_t *p_input;
257             PL_DEBUG( "dead input" );
258
259             p_input = p_playlist->p_input;
260             p_playlist->p_input = NULL;
261
262             /* Release the playlist lock, because we may get stuck
263              * in input_DestroyThread() for some time. */
264             PL_UNLOCK
265
266             /* Destroy input */
267             input_DestroyThread( p_input );
268
269             PL_LOCK;
270
271             p_playlist->gc_date = mdate();
272             p_playlist->b_cant_sleep = VLC_TRUE;
273
274             if( p_playlist->status.p_item->i_flags
275                 & PLAYLIST_REMOVE_FLAG )
276             {
277                  PL_DEBUG( "%s was marked for deletion, deleting",
278                                  PLI_NAME( p_playlist->status.p_item  ) );
279                  playlist_ItemDelete( p_playlist->status.p_item );
280                  if( p_playlist->request.p_item == p_playlist->status.p_item )
281                      p_playlist->request.p_item = NULL;
282                  p_playlist->status.p_item = NULL;
283             }
284
285             i_activity= var_GetInteger( p_playlist, "activity") ;
286             var_SetInteger( p_playlist, "activity", i_activity -
287                             DEFAULT_INPUT_ACTIVITY );
288             goto check_input;
289         }
290         /* This input is dying, let it do */
291         else if( p_playlist->p_input->b_die )
292         {
293             PL_DEBUG( "dying input" );
294             PL_UNLOCK;
295             msleep( 25000 ); // 25 ms
296             PL_LOCK;
297             goto check_input;
298         }
299         /* This input has finished, ask it to die ! */
300         else if( p_playlist->p_input->b_error
301                   || p_playlist->p_input->b_eof )
302         {
303             PL_DEBUG( "finished input" );
304             input_StopThread( p_playlist->p_input );
305             /* No need to wait here, we'll wait in the p_input->b_die case */
306             goto check_input;
307         }
308         else if( p_playlist->p_input->i_state != INIT_S )
309         {
310             PL_UNLOCK;
311             ObjectGarbageCollector( p_playlist, VLC_FALSE );
312             PL_LOCK;
313         }
314     }
315     else
316     {
317         /* No input. Several cases
318          *  - No request, running status -> start new item
319          *  - No request, stopped status -> collect garbage
320          *  - Request, running requested -> start new item
321          *  - Request, stopped requested -> collect garbage
322          */
323          if( (!p_playlist->request.b_request &&
324               p_playlist->status.i_status != PLAYLIST_STOPPED) ||
325               ( p_playlist->request.b_request &&
326                 p_playlist->request.i_status != PLAYLIST_STOPPED ) )
327          {
328              msg_Dbg( p_playlist, "starting new item" );
329              p_item = playlist_NextItem( p_playlist );
330
331              if( p_item == NULL )
332              {
333                 msg_Dbg( p_playlist, "nothing to play" );
334                 if( b_playexit == VLC_TRUE )
335                 {
336                     msg_Info( p_playlist, "end of playlist, exiting" );
337                     vlc_object_kill( p_playlist->p_libvlc );
338                 }
339                 p_playlist->status.i_status = PLAYLIST_STOPPED;
340                 PL_UNLOCK
341
342                 ObjectGarbageCollector( p_playlist, VLC_TRUE );
343                 return;
344              }
345              playlist_PlayItem( p_playlist, p_item );
346          }
347          else
348          {
349             const vlc_bool_t b_gc_forced = p_playlist->status.i_status != PLAYLIST_STOPPED;
350
351             p_playlist->status.i_status = PLAYLIST_STOPPED;
352             if( p_playlist->status.p_item &&
353                 p_playlist->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG )
354             {
355                 PL_DEBUG( "deleting item marked for deletion" );
356                 playlist_ItemDelete( p_playlist->status.p_item );
357                 p_playlist->status.p_item = NULL;
358             }
359
360             /* Collect garbage */
361             PL_UNLOCK;
362             ObjectGarbageCollector( p_playlist, b_gc_forced );
363             PL_LOCK;
364         }
365     }
366     PL_UNLOCK
367 }
368
369 /** Playlist dying last loop */
370 void playlist_LastLoop( playlist_t *p_playlist )
371 {
372     vlc_object_t *p_obj;
373
374     /* If there is an input, kill it */
375     while( 1 )
376     {
377         PL_LOCK
378
379         if( p_playlist->p_input == NULL )
380         {
381             PL_UNLOCK
382             break;
383         }
384
385         if( p_playlist->p_input->b_dead )
386         {
387             input_thread_t *p_input;
388
389             /* Unlink current input */
390             p_input = p_playlist->p_input;
391             p_playlist->p_input = NULL;
392             PL_UNLOCK
393
394             /* Destroy input */
395             input_DestroyThread( p_input );
396             continue;
397         }
398         else if( p_playlist->p_input->b_die )
399         {
400             /* This input is dying, leave it alone */
401             ;
402         }
403         else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
404         {
405             input_StopThread( p_playlist->p_input );
406             PL_UNLOCK
407             continue;
408         }
409         else
410         {
411             p_playlist->p_input->b_eof = 1;
412         }
413
414         PL_UNLOCK
415
416         msleep( INTF_IDLE_SLEEP );
417     }
418
419     /* close all remaining sout */
420     while( ( p_obj = vlc_object_find( p_playlist,
421                                       VLC_OBJECT_SOUT, FIND_CHILD ) ) )
422     {
423         vlc_object_detach( p_obj );
424         vlc_object_release( p_obj );
425         sout_DeleteInstance( (sout_instance_t*)p_obj );
426     }
427
428     /* close all remaining vout */
429     while( ( p_obj = vlc_object_find( p_playlist,
430                                       VLC_OBJECT_VOUT, FIND_CHILD ) ) )
431     {
432         vlc_object_detach( p_obj );
433         vlc_object_release( p_obj );
434         vout_Destroy( (vout_thread_t *)p_obj );
435     }
436
437     while( p_playlist->i_asds )
438     {
439         playlist_ServicesDiscoveryRemove( p_playlist,
440                                           p_playlist->pp_asds[0]->p_sd->psz_module );
441     }
442
443     playlist_MLDump( p_playlist );
444
445     PL_LOCK;
446     /* Go through all items, and simply free everything without caring
447      * about the tree structure. Do not decref, it will be done by doing
448      * the same thing on the input items array */
449     FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
450         free( p_del->pp_children );
451         free( p_del );
452     FOREACH_END();
453     ARRAY_RESET( p_playlist->all_items );
454
455     FOREACH_ARRAY( input_item_t *p_del, p_playlist->input_items )
456         input_ItemClean( p_del );
457         free( p_del );
458     FOREACH_END();
459     ARRAY_RESET( p_playlist->input_items );
460
461     ARRAY_RESET( p_playlist->items );
462     ARRAY_RESET( p_playlist->current );
463
464     PL_UNLOCK;
465 }
466
467 /** Main loop for preparser queue */
468 void playlist_PreparseLoop( playlist_preparse_t *p_obj )
469 {
470     playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
471     input_item_t *p_current;
472     int i_activity;
473     uint32_t i_m, i_o;
474
475     while( !p_playlist->b_die )
476     {
477         vlc_mutex_lock( &p_obj->object_lock );
478         while( p_obj->i_waiting == 0 )
479         {
480             vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
481             if( p_playlist->b_die )
482             {
483                 vlc_mutex_unlock( &p_obj->object_lock );
484                 return;
485             }
486         }
487
488         p_current = p_obj->pp_waiting[0];
489         REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
490         vlc_mutex_unlock( &p_obj->object_lock );
491
492         PL_LOCK;
493         if( p_current )
494         {
495             vlc_bool_t b_preparsed = VLC_FALSE;
496             if( strncmp( p_current->psz_uri, "http:", 5 ) &&
497                 strncmp( p_current->psz_uri, "rtsp:", 5 ) &&
498                 strncmp( p_current->psz_uri, "udp:", 4 ) &&
499                 strncmp( p_current->psz_uri, "mms:", 4 ) &&
500                 strncmp( p_current->psz_uri, "cdda:", 4 ) &&
501                 strncmp( p_current->psz_uri, "dvd:", 4 ) &&
502                 strncmp( p_current->psz_uri, "v4l:", 4 ) &&
503                 strncmp( p_current->psz_uri, "dshow:", 6 ) )
504             {
505                 b_preparsed = VLC_TRUE;
506                 stats_TimerStart( p_playlist, "Preparse run",
507                                   STATS_TIMER_PREPARSE );
508                 /* Do not preparse if it is already done (like by playing it) */
509                 if( !input_item_IsPreparsed( p_current ) )
510                 {
511                     PL_UNLOCK;
512                     input_Preparse( p_playlist, p_current );
513                     PL_LOCK;
514                 }
515                 stats_TimerStop( p_playlist, STATS_TIMER_PREPARSE );
516             }
517             PL_UNLOCK;
518             if( b_preparsed )
519             {
520                 input_item_SetPreparsed( p_current, VLC_TRUE );
521                 var_SetInteger( p_playlist, "item-change", p_current->i_id );
522             }
523             PL_LOCK;
524
525             /* If we haven't retrieved enough meta, add to secondary queue
526              * which will run the "meta fetchers".
527              * This only checks for meta, not for art
528              * \todo don't do this for things we won't get meta for, like vids
529              */
530             if( !input_MetaSatisfied( p_playlist, p_current, &i_m, &i_o ) )
531             {
532                 preparse_item_t p;
533                 PL_DEBUG("need to fetch meta for %s", p_current->psz_name );
534                 p.p_item = p_current;
535                 p.b_fetch_art = VLC_FALSE;
536                 vlc_mutex_lock( &p_playlist->p_fetcher->object_lock );
537                 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
538                              p_playlist->p_fetcher->i_waiting,
539                              p_playlist->p_fetcher->i_waiting, p);
540                 vlc_mutex_unlock( &p_playlist->p_fetcher->object_lock );
541                 vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
542             }
543             /* We already have all needed meta, but we need art right now */
544             else if( p_playlist->p_fetcher->i_art_policy == ALBUM_ART_ALL &&
545                      EMPTY_STR( input_item_GetArtURL( p_current ) ) )
546             {
547                 preparse_item_t p;
548                 PL_DEBUG("meta ok for %s, need to fetch art",
549                                                          p_current->psz_name );
550                 p.p_item = p_current;
551                 p.b_fetch_art = VLC_TRUE;
552                 vlc_mutex_lock( &p_playlist->p_fetcher->object_lock );
553                 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
554                              p_playlist->p_fetcher->i_waiting,
555                              p_playlist->p_fetcher->i_waiting, p);
556                 vlc_mutex_unlock( &p_playlist->p_fetcher->object_lock );
557                 vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
558             }
559             else
560             {
561                 PL_DEBUG( "no fetch required for %s (art currently %s)",
562                           p_current->psz_name,
563                           input_item_GetArtURL( p_current ));
564                 vlc_gc_decref( p_current );
565             }
566             PL_UNLOCK;
567         }
568         else
569             PL_UNLOCK;
570
571         vlc_mutex_lock( &p_obj->object_lock );
572         i_activity = var_GetInteger( p_playlist, "activity" );
573         if( i_activity < 0 ) i_activity = 0;
574         vlc_mutex_unlock( &p_obj->object_lock );
575         /* Sleep at least 1ms */
576         msleep( (i_activity+1) * 1000 );
577     }
578 }
579
580 /** Main loop for secondary preparser queue */
581 void playlist_FetcherLoop( playlist_fetcher_t *p_obj )
582 {
583     playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
584     vlc_bool_t b_fetch_art;
585     input_item_t *p_item;
586     int i_activity;
587
588     while( !p_playlist->b_die )
589     {
590         vlc_mutex_lock( &p_obj->object_lock );
591         while( p_obj->i_waiting == 0 )
592         {
593             vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
594             if( p_playlist->b_die )
595             {
596                 vlc_mutex_unlock( &p_obj->object_lock );
597                 return;
598             }
599         }
600
601         b_fetch_art = p_obj->p_waiting->b_fetch_art;
602         p_item = p_obj->p_waiting->p_item;
603         REMOVE_ELEM( p_obj->p_waiting, p_obj->i_waiting, 0 );
604         vlc_mutex_unlock( &p_obj->object_lock );
605         if( p_item )
606         {
607             if( !b_fetch_art )
608             {
609                 input_MetaFetch( p_playlist, p_item );
610                 var_SetInteger( p_playlist, "item-change", p_item->i_id );
611                 /*  Fetch right now */
612                 if( p_playlist->p_fetcher->i_art_policy == ALBUM_ART_ALL )
613                 {
614                     vlc_mutex_lock( &p_obj->object_lock );
615                     preparse_item_t p;
616                     p.p_item = p_item;
617                     p.b_fetch_art = VLC_TRUE;
618                     INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
619                                  p_playlist->p_fetcher->i_waiting,
620                                  0, p );
621                     PL_DEBUG("meta fetched for %s, get art", p_item->psz_name);
622                     vlc_mutex_unlock( &p_obj->object_lock );
623                     continue;
624                 }
625                 else
626                     vlc_gc_decref( p_item );
627             }
628             else
629             {
630                 int i_ret;
631
632                 /* Check if it is not yet preparsed and if so wait for it (at most 0.5s)
633                  * (This can happen if we fetch art on play)
634                  * FIXME this doesn't work if we need to fetch meta before art ... */
635                 for( i_ret = 0; i_ret < 10 && !input_item_IsPreparsed( p_item ); i_ret++ )
636                 {
637                     vlc_bool_t b_break;
638                     PL_LOCK;
639                     b_break = ( !p_playlist->p_input || input_GetItem(p_playlist->p_input) != p_item  ||
640                                 p_playlist->p_input->b_die || p_playlist->p_input->b_eof || p_playlist->p_input->b_error );
641                     PL_UNLOCK;
642                     if( b_break )
643                         break;
644                     msleep( 50000 );
645                 }
646                 
647                 i_ret = input_ArtFind( p_playlist, p_item );
648                 if( i_ret == 1 )
649                 {
650                     PL_DEBUG("downloading art for %s", p_item->psz_name );
651                     if( input_DownloadAndCacheArt( p_playlist, p_item ) )
652                         input_item_SetArtNotFound( p_item, VLC_TRUE );
653                     else {
654                         input_item_SetArtFetched( p_item, VLC_TRUE );
655                         var_SetInteger( p_playlist, "item-change",
656                                         p_item->i_id );
657                     }
658                 }
659                 else if( i_ret == 0 ) /* Was in cache */
660                 {
661                     PL_DEBUG("found art for %s in cache", p_item->psz_name );
662                     input_item_SetArtFetched( p_item, VLC_TRUE );
663                     var_SetInteger( p_playlist, "item-change", p_item->i_id );
664                 }
665                 else
666                 {
667                     PL_DEBUG("art not found for %s", p_item->psz_name );
668                     input_item_SetArtNotFound( p_item, VLC_TRUE );
669                 }
670                 vlc_gc_decref( p_item );
671            }
672         }
673         vlc_mutex_lock( &p_obj->object_lock );
674         i_activity = var_GetInteger( p_playlist, "activity" );
675         if( i_activity < 0 ) i_activity = 0;
676         vlc_mutex_unlock( &p_obj->object_lock );
677         /* Sleep at least 1ms */
678         msleep( (i_activity+1) * 1000 );
679     }
680 }
681
682 static void VariablesInit( playlist_t *p_playlist )
683 {
684     vlc_value_t val;
685     /* These variables control updates */
686     var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
687     val.b_bool = VLC_TRUE;
688     var_Set( p_playlist, "intf-change", val );
689
690     var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
691     val.i_int = -1;
692     var_Set( p_playlist, "item-change", val );
693
694     var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
695     val.i_int = -1;
696     var_Set( p_playlist, "item-deleted", val );
697
698     var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
699
700     var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
701     val.i_int = -1;
702     var_Set( p_playlist, "playlist-current", val );
703
704     var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
705
706     var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
707     val.b_bool = VLC_TRUE;
708     var_Set( p_playlist, "intf-show", val );
709
710     var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
711     var_SetInteger( p_playlist, "activity", 0 );
712
713     /* Variables to control playback */
714     var_CreateGetBool( p_playlist, "play-and-stop" );
715     var_CreateGetBool( p_playlist, "play-and-exit" );
716     var_CreateGetBool( p_playlist, "random" );
717     var_CreateGetBool( p_playlist, "repeat" );
718     var_CreateGetBool( p_playlist, "loop" );
719
720     var_AddCallback( p_playlist, "random", RandomCallback, NULL );
721 }