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