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