]> git.sesse.net Git - vlc/blob - src/playlist/engine.c
Improve meta/art logic
[vlc] / 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 #include <vlc/vlc.h>
25 #include <vlc/vout.h>
26 #include <vlc/sout.h>
27 #include <vlc/input.h>
28 #include "vlc_playlist.h"
29 #include "vlc_interaction.h"
30 #include "playlist_internal.h"
31
32 /*****************************************************************************
33  * Local prototypes
34  *****************************************************************************/
35 static void VariablesInit( playlist_t *p_playlist );
36
37 /**
38  * Create playlist
39  *
40  * Create a playlist structure.
41  * \param p_parent the vlc object that is to be the parent of this playlist
42  * \return a pointer to the created playlist, or NULL on error
43  */
44 playlist_t * playlist_Create( vlc_object_t *p_parent )
45 {
46     playlist_t *p_playlist;
47     int i_tree;
48
49     /* Allocate structure */
50     p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
51     if( !p_playlist )
52     {
53         msg_Err( p_parent, "out of memory" );
54         return NULL;
55     }
56     p_parent->p_libvlc->p_playlist = p_playlist;
57
58     VariablesInit( p_playlist );
59
60     /* Initialise data structures */
61     vlc_mutex_init( p_playlist, &p_playlist->gc_lock );
62     p_playlist->i_last_playlist_id = 0;
63     p_playlist->i_last_input_id = 0;
64     p_playlist->p_input = NULL;
65
66     p_playlist->i_vout_destroyed_date = 0;
67     p_playlist->i_sout_destroyed_date = 0;
68
69     p_playlist->i_size = 0;
70     p_playlist->pp_items = NULL;
71     p_playlist->i_all_size = 0;
72     p_playlist->pp_all_items = NULL;
73
74     p_playlist->i_input_items = 0;
75     p_playlist->pp_input_items = NULL;
76
77     p_playlist->i_random = 0;
78     p_playlist->pp_random = NULL;
79     p_playlist->i_random_index = 0;
80     p_playlist->b_reset_random = VLC_TRUE;
81
82     i_tree = var_CreateGetBool( p_playlist, "playlist-tree" );
83     p_playlist->b_always_tree = (i_tree == 1);
84     p_playlist->b_never_tree = (i_tree == 2);
85
86     p_playlist->b_doing_ml = VLC_FALSE;
87
88     p_playlist->p_root_category = playlist_NodeCreate( p_playlist, NULL, NULL);
89     p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL);
90
91     /* Create playlist and media library */
92     p_playlist->p_local_category = playlist_NodeCreate( p_playlist,
93                                  _( "Playlist" ),p_playlist->p_root_category );
94     p_playlist->p_local_onelevel =  playlist_NodeCreate( p_playlist,
95                                 _( "Playlist" ), p_playlist->p_root_onelevel );
96     p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
97     p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;
98
99     /* Link the nodes together. Todo: actually create them from the same input*/
100     p_playlist->p_local_onelevel->p_input->i_id =
101         p_playlist->p_local_category->p_input->i_id;
102
103     if( config_GetInt( p_playlist, "media-library") )
104     {
105         p_playlist->p_ml_category =   playlist_NodeCreate( p_playlist,
106                            _( "Media Library" ), p_playlist->p_root_category );
107         p_playlist->p_ml_onelevel =  playlist_NodeCreate( p_playlist,
108                            _( "Media Library" ), p_playlist->p_root_onelevel );
109         p_playlist->p_ml_category->i_flags |= PLAYLIST_RO_FLAG;
110         p_playlist->p_ml_onelevel->i_flags |= PLAYLIST_RO_FLAG;
111         p_playlist->p_ml_onelevel->p_input->i_id =
112              p_playlist->p_ml_category->p_input->i_id;
113
114     }
115     else
116     {
117         p_playlist->p_ml_category = p_playlist->p_ml_onelevel = NULL;
118     }
119
120     /* Initial status */
121     p_playlist->status.p_item = NULL;
122     p_playlist->status.p_node = p_playlist->p_local_onelevel;
123     p_playlist->request.b_request = VLC_FALSE;
124     p_playlist->status.i_status = PLAYLIST_STOPPED;
125
126     p_playlist->i_sort = SORT_ID;
127     p_playlist->i_order = ORDER_NORMAL;
128
129     vlc_object_attach( p_playlist, p_parent );
130
131     playlist_MLLoad( p_playlist );
132     return p_playlist;
133 }
134
135 void playlist_Destroy( playlist_t *p_playlist )
136 {
137     while( p_playlist->i_sds )
138     {
139         playlist_ServicesDiscoveryRemove( p_playlist,
140                                           p_playlist->pp_sds[0]->psz_module );
141     }
142
143     playlist_MLDump( p_playlist );
144
145     vlc_thread_join( p_playlist->p_preparse );
146     vlc_thread_join( p_playlist->p_secondary_preparse );
147     vlc_thread_join( p_playlist );
148
149     vlc_object_detach( p_playlist->p_preparse );
150     vlc_object_detach( p_playlist->p_secondary_preparse );
151
152     var_Destroy( p_playlist, "intf-change" );
153     var_Destroy( p_playlist, "item-change" );
154     var_Destroy( p_playlist, "playlist-current" );
155     var_Destroy( p_playlist, "intf-popmenu" );
156     var_Destroy( p_playlist, "intf-show" );
157     var_Destroy( p_playlist, "play-and-stop" );
158     var_Destroy( p_playlist, "play-and-exit" );
159     var_Destroy( p_playlist, "random" );
160     var_Destroy( p_playlist, "repeat" );
161     var_Destroy( p_playlist, "loop" );
162     var_Destroy( p_playlist, "activity" );
163
164     PL_LOCK;
165     playlist_NodeDelete( p_playlist, p_playlist->p_root_category, VLC_TRUE,
166                          VLC_TRUE );
167     playlist_NodeDelete( p_playlist, p_playlist->p_root_onelevel, VLC_TRUE,
168                          VLC_TRUE );
169     PL_UNLOCK;
170
171     if( p_playlist->p_stats )
172         free( p_playlist->p_stats );
173
174     vlc_mutex_destroy( &p_playlist->gc_lock );
175     vlc_object_destroy( p_playlist->p_preparse );
176     vlc_object_destroy( p_playlist->p_secondary_preparse );
177     vlc_object_detach( p_playlist );
178     vlc_object_destroy( p_playlist );
179
180 }
181 /* Destroy remaining objects */
182 static mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
183                                        mtime_t destroy_date )
184 {
185     vlc_object_t *p_obj;
186
187     if( destroy_date > mdate() ) return destroy_date;
188
189     if( destroy_date == 0 )
190     {
191         /* give a little time */
192         return mdate() + I64C(1000000);
193     }
194     else
195     {
196         vlc_mutex_lock( &p_playlist->gc_lock );
197         while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
198         {
199             if( p_obj->p_parent != (vlc_object_t*)p_playlist )
200             {
201                 /* only first child (ie unused) */
202                 vlc_object_release( p_obj );
203                 break;
204             }
205             if( i_type == VLC_OBJECT_VOUT )
206             {
207                 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
208                 vlc_object_detach( p_obj );
209                 vlc_object_release( p_obj );
210                 vout_Destroy( (vout_thread_t *)p_obj );
211             }
212             else if( i_type == VLC_OBJECT_SOUT )
213             {
214                 vlc_object_release( p_obj );
215                 sout_DeleteInstance( (sout_instance_t*)p_obj );
216             }
217         }
218         vlc_mutex_unlock( &p_playlist->gc_lock );
219         return 0;
220     }
221 }
222
223 /** Main loop for the playlist */
224 void playlist_MainLoop( playlist_t *p_playlist )
225 {
226     playlist_item_t *p_item = NULL;
227     vlc_bool_t b_playexit = var_GetBool( p_playlist, "play-and-exit" );
228     PL_LOCK
229
230     /* First, check if we have something to do */
231     /* FIXME : this can be called several times */
232     if( p_playlist->request.b_request )
233     {
234         /* Stop the existing input */
235         if( p_playlist->p_input && !p_playlist->p_input->b_die )
236         {
237             PL_DEBUG( "incoming request - stopping current input" );
238             input_StopThread( p_playlist->p_input );
239         }
240     }
241
242     /* If there is an input, check that it doesn't need to die. */
243     if( p_playlist->p_input )
244     {
245         /* This input is dead. Remove it ! */
246         if( p_playlist->p_input->b_dead )
247         {
248             int i_activity;
249             input_thread_t *p_input;
250             PL_DEBUG( "dead input" );
251
252             p_input = p_playlist->p_input;
253             p_playlist->p_input = NULL;
254
255             /* Release the playlist lock, because we may get stuck
256              * in input_DestroyThread() for some time. */
257             PL_UNLOCK
258
259             /* Destroy input */
260             input_DestroyThread( p_input );
261
262             /* Unlink current input
263              * (_after_ input_DestroyThread for vout garbage collector) */
264             vlc_object_detach( p_input );
265
266             /* Destroy object */
267             vlc_object_destroy( p_input );
268
269             p_playlist->i_vout_destroyed_date = 0;
270             p_playlist->i_sout_destroyed_date = 0;
271
272             if( p_playlist->status.p_item->i_flags
273                 & PLAYLIST_REMOVE_FLAG )
274             {
275                  PL_DEBUG( "%s was marked for deletion, deleting",
276                                  PLI_NAME( p_playlist->status.p_item  ) );
277                  playlist_ItemDelete( p_playlist->status.p_item );
278                  if( p_playlist->request.p_item == p_playlist->status.p_item )
279                      p_playlist->request.p_item = NULL;
280                  p_playlist->status.p_item = NULL;
281             }
282
283             i_activity= var_GetInteger( p_playlist, "activity") ;
284             var_SetInteger( p_playlist, "activity", i_activity -
285                             DEFAULT_INPUT_ACTIVITY );
286
287             return;
288         }
289         /* This input is dying, let it do */
290         else if( p_playlist->p_input->b_die )
291         {
292             PL_DEBUG( "dying input" );
293         }
294         /* This input has finished, ask it to die ! */
295         else if( p_playlist->p_input->b_error
296                   || p_playlist->p_input->b_eof )
297         {
298             PL_DEBUG( "finished input" );
299             input_StopThread( p_playlist->p_input );
300             /* Select the next playlist item */
301             PL_UNLOCK
302             return;
303         }
304         else if( p_playlist->p_input->i_state != INIT_S )
305         {
306             PL_UNLOCK;
307             p_playlist->i_vout_destroyed_date =
308                 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
309                                         p_playlist->i_vout_destroyed_date );
310             p_playlist->i_sout_destroyed_date =
311                 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
312                                         p_playlist->i_sout_destroyed_date );
313             PL_LOCK
314         }
315     }
316     else
317     {
318         /* No input. Several cases
319          *  - No request, running status -> start new item
320          *  - No request, stopped status -> collect garbage
321          *  - Request, running requested -> start new item
322          *  - Request, stopped requested -> collect garbage
323          */
324          if( (!p_playlist->request.b_request &&
325               p_playlist->status.i_status != PLAYLIST_STOPPED) ||
326               ( p_playlist->request.b_request &&
327                 p_playlist->request.i_status != PLAYLIST_STOPPED ) )
328          {
329              msg_Dbg( p_playlist, "starting new item" );
330              stats_TimerStart( p_playlist, "Playlist walk",
331                                   STATS_TIMER_PLAYLIST_WALK );
332              p_item = playlist_NextItem( p_playlist );
333              stats_TimerStop( p_playlist, STATS_TIMER_PLAYLIST_WALK );
334
335              if( p_item == NULL )
336              {
337                 msg_Dbg( p_playlist, "nothing to play" );
338                 if( b_playexit == VLC_TRUE )
339                 {
340                     msg_Info( p_playlist, "end of playlist, exiting" );
341                     p_playlist->p_libvlc->b_die = VLC_TRUE;
342                 }
343                 p_playlist->status.i_status = PLAYLIST_STOPPED;
344                 PL_UNLOCK
345                 return;
346              }
347              playlist_PlayItem( p_playlist, p_item );
348          }
349          else
350          {
351              if( p_playlist->status.p_item &&
352                  p_playlist->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG )
353              {
354                  PL_DEBUG( "deleting item marked for deletion" );
355                  playlist_ItemDelete( p_playlist->status.p_item );
356                  p_playlist->status.p_item = NULL;
357              }
358
359              /* Collect garbage */
360              PL_UNLOCK
361              p_playlist->i_sout_destroyed_date =
362              ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
363              p_playlist->i_vout_destroyed_date =
364              ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
365              PL_LOCK
366          }
367     }
368     PL_UNLOCK
369 }
370
371 /** Playlist dying last loop */
372 void playlist_LastLoop( playlist_t *p_playlist )
373 {
374     vlc_object_t *p_obj;
375
376     /* If there is an input, kill it */
377     while( 1 )
378     {
379         PL_LOCK
380
381         if( p_playlist->p_input == NULL )
382         {
383             PL_UNLOCK
384             break;
385         }
386
387         if( p_playlist->p_input->b_dead )
388         {
389             input_thread_t *p_input;
390
391             /* Unlink current input */
392             p_input = p_playlist->p_input;
393             p_playlist->p_input = NULL;
394             PL_UNLOCK
395
396             /* Destroy input */
397             input_DestroyThread( p_input );
398             /* Unlink current input (_after_ input_DestroyThread for vout
399              * garbage collector)*/
400             vlc_object_detach( p_input );
401
402             /* Destroy object */
403             vlc_object_destroy( p_input );
404             continue;
405         }
406         else if( p_playlist->p_input->b_die )
407         {
408             /* This input is dying, leave it alone */
409             ;
410         }
411         else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
412         {
413             input_StopThread( p_playlist->p_input );
414             PL_UNLOCK
415             continue;
416         }
417         else
418         {
419             p_playlist->p_input->b_eof = 1;
420         }
421
422         PL_UNLOCK
423
424         msleep( INTF_IDLE_SLEEP );
425     }
426
427     /* close all remaining sout */
428     while( ( p_obj = vlc_object_find( p_playlist,
429                                       VLC_OBJECT_SOUT, FIND_CHILD ) ) )
430     {
431         vlc_object_release( p_obj );
432         sout_DeleteInstance( (sout_instance_t*)p_obj );
433     }
434
435     /* close all remaining vout */
436     while( ( p_obj = vlc_object_find( p_playlist,
437                                       VLC_OBJECT_VOUT, FIND_CHILD ) ) )
438     {
439         vlc_object_detach( p_obj );
440         vlc_object_release( p_obj );
441         vout_Destroy( (vout_thread_t *)p_obj );
442     }
443 }
444
445 /** Main loop for preparser queue */
446 void playlist_PreparseLoop( playlist_preparse_t *p_obj )
447 {
448     playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
449     int i_activity;
450     uint32_t i_m, i_o;
451
452     vlc_mutex_lock( &p_obj->object_lock );
453
454     if( p_obj->i_waiting > 0 )
455     {
456         input_item_t *p_current = p_obj->pp_waiting[0];
457         REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
458         vlc_mutex_unlock( &p_obj->object_lock );
459         PL_LOCK;
460         if( p_current )
461         {
462             vlc_bool_t b_preparsed = VLC_FALSE;
463             if( strncmp( p_current->psz_uri, "http:", 5 ) &&
464                 strncmp( p_current->psz_uri, "rtsp:", 5 ) &&
465                 strncmp( p_current->psz_uri, "udp:", 4 ) &&
466                 strncmp( p_current->psz_uri, "mms:", 4 ) &&
467                 strncmp( p_current->psz_uri, "cdda:", 4 ) &&
468                 strncmp( p_current->psz_uri, "dvd:", 4 ) &&
469                 strncmp( p_current->psz_uri, "v4l:", 4 ) &&
470                 strncmp( p_current->psz_uri, "dshow:", 6 ) )
471             {
472                 b_preparsed = VLC_TRUE;
473                 stats_TimerStart( p_playlist, "Preparse run",
474                                   STATS_TIMER_PREPARSE );
475                 PL_UNLOCK;
476                 input_Preparse( p_playlist, p_current );
477                 PL_LOCK;
478                 stats_TimerStop( p_playlist, STATS_TIMER_PREPARSE );
479             }
480             PL_UNLOCK;
481             if( b_preparsed )
482             {
483                 p_current->p_meta->i_status |= ITEM_PREPARSED;
484                 var_SetInteger( p_playlist, "item-change", p_current->i_id );
485             }
486             PL_LOCK;
487             /* If we haven't retrieved enough meta, add to secondary queue
488              * which will run the "meta fetchers"
489              * TODO:
490              *  don't do this for things we won't get meta for, like
491              *  videos
492              */
493             if( !input_MetaSatisfied( p_playlist, p_current, &i_m, &i_o,
494                                       VLC_TRUE ) )
495             {
496                 preparse_item_t p;
497                 p.p_item = p_current;
498                 p.b_fetch_art = VLC_FALSE;
499                 vlc_mutex_lock( &p_playlist->p_secondary_preparse->object_lock);
500                 INSERT_ELEM( p_playlist->p_secondary_preparse->p_waiting,
501                              p_playlist->p_secondary_preparse->i_waiting,
502                              p_playlist->p_secondary_preparse->i_waiting,
503                              p );
504                 vlc_mutex_unlock(
505                             &p_playlist->p_secondary_preparse->object_lock);
506             }
507             else
508                 vlc_gc_decref( p_current );
509             PL_UNLOCK;
510         }
511         else
512             PL_UNLOCK;
513
514         vlc_mutex_lock( &p_obj->object_lock );
515         i_activity = var_GetInteger( p_playlist, "activity" );
516         if( i_activity < 0 ) i_activity = 0;
517         vlc_mutex_unlock( &p_obj->object_lock );
518         msleep( (i_activity+1) * 1000 );
519         return;
520     }
521     vlc_mutex_unlock( &p_obj->object_lock );
522 }
523
524 /** Main loop for secondary preparser queue */
525 void playlist_SecondaryPreparseLoop( playlist_secondary_preparse_t *p_obj )
526 {
527     playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
528
529     vlc_mutex_lock( &p_obj->object_lock );
530
531     if( p_obj->i_waiting > 0 )
532     {
533         vlc_bool_t b_fetch_art = p_obj->p_waiting->b_fetch_art;
534         input_item_t *p_item = p_obj->p_waiting->p_item;
535         REMOVE_ELEM( p_obj->p_waiting, p_obj->i_waiting, 0 );
536         vlc_mutex_unlock( &p_obj->object_lock );
537         if( p_item )
538         {
539             if( !b_fetch_art )
540             {
541                 input_MetaFetch( p_playlist, p_item );
542                 p_item->p_meta->i_status |= ITEM_META_FETCHED;
543             }
544             else
545             {
546                 input_ArtFetch( p_playlist, p_item );
547                 p_item->p_meta->i_status |= ITEM_ART_FETCHED;
548             }
549             var_SetInteger( p_playlist, "item-change", p_item->i_id );
550             vlc_gc_decref( p_item );
551         }
552         return;
553     }
554     vlc_mutex_unlock( &p_obj->object_lock );
555 }
556
557 static void VariablesInit( playlist_t *p_playlist )
558 {
559     vlc_value_t val;
560     /* These variables control updates */
561     var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
562     val.b_bool = VLC_TRUE;
563     var_Set( p_playlist, "intf-change", val );
564
565     var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
566     val.i_int = -1;
567     var_Set( p_playlist, "item-change", val );
568
569     var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
570     val.i_int = -1;
571     var_Set( p_playlist, "item-deleted", val );
572
573     var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
574
575     var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
576     val.i_int = -1;
577     var_Set( p_playlist, "playlist-current", val );
578
579     var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
580
581     var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
582     val.b_bool = VLC_TRUE;
583     var_Set( p_playlist, "intf-show", val );
584
585     var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
586     var_SetInteger( p_playlist, "activity", 0 );
587
588     /* Variables to control playback */
589     var_CreateGetBool( p_playlist, "play-and-stop" );
590     var_CreateGetBool( p_playlist, "play-and-exit" );
591     var_CreateGetBool( p_playlist, "random" );
592     var_CreateGetBool( p_playlist, "repeat" );
593     var_CreateGetBool( p_playlist, "loop" );
594 }