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