]> git.sesse.net Git - vlc/blob - src/playlist/engine.c
26bc7ee4e36e0c81c829670af2c883297231c173
[vlc] / src / playlist / engine.c
1 /*****************************************************************************
2  * engine.c : Run the playlist and handle its control
3  *****************************************************************************
4  * Copyright (C) 1999-2008 the VideoLAN team
5  *
6  * Authors: Samuel Hocevar <sam@zoy.org>
7  *          ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <stddef.h>
29 #include <assert.h>
30 #include <vlc_common.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     playlist_t *p_playlist = (playlist_t*)p_this;
48
49     PL_LOCK;
50
51     pl_priv(p_playlist)->b_reset_currently_playing = true;
52     vlc_cond_signal( &pl_priv(p_playlist)->signal );
53
54     PL_UNLOCK;
55     return VLC_SUCCESS;
56 }
57
58 /**
59  * Create playlist
60  *
61  * Create a playlist structure.
62  * \param p_parent the vlc object that is to be the parent of this playlist
63  * \return a pointer to the created playlist, or NULL on error
64  */
65 playlist_t * playlist_Create( vlc_object_t *p_parent )
66 {
67     static const char playlist_name[] = "playlist";
68     playlist_t *p_playlist;
69     playlist_private_t *p;
70
71     /* Allocate structure */
72     p = vlc_custom_create( p_parent, sizeof( *p ),
73                            VLC_OBJECT_GENERIC, playlist_name );
74     if( !p )
75         return NULL;
76
77     assert( offsetof( playlist_private_t, public_data ) == 0 );
78     p_playlist = &p->public_data;
79     vlc_object_attach( p_playlist, p_parent );
80     TAB_INIT( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds );
81
82     libvlc_priv(p_parent->p_libvlc)->p_playlist = p_playlist;
83
84     VariablesInit( p_playlist );
85     vlc_mutex_init( &p->lock );
86     vlc_cond_init( &p->signal );
87
88     /* Initialise data structures */
89     pl_priv(p_playlist)->i_last_playlist_id = 0;
90     pl_priv(p_playlist)->p_input = NULL;
91
92     ARRAY_INIT( p_playlist->items );
93     ARRAY_INIT( p_playlist->all_items );
94     ARRAY_INIT( pl_priv(p_playlist)->items_to_delete );
95     ARRAY_INIT( p_playlist->current );
96
97     p_playlist->i_current_index = 0;
98     pl_priv(p_playlist)->b_reset_currently_playing = true;
99     pl_priv(p_playlist)->last_rebuild_date = 0;
100
101     pl_priv(p_playlist)->b_tree = var_InheritBool( p_parent, "playlist-tree" );
102
103     pl_priv(p_playlist)->b_doing_ml = false;
104
105     pl_priv(p_playlist)->b_auto_preparse =
106         var_InheritBool( p_parent, "auto-preparse" );
107
108     /* Fetcher */
109     p->p_fetcher = playlist_fetcher_New( p_playlist );
110     if( unlikely(p->p_fetcher == NULL) )
111     {
112         msg_Err( p_playlist, "cannot create fetcher" );
113         p->p_preparser = NULL;
114     }
115     else
116     {   /* Preparse */
117         p->p_preparser = playlist_preparser_New( p_playlist, p->p_fetcher );
118         if( unlikely(p->p_preparser == NULL) )
119             msg_Err( p_playlist, "cannot create preparser" );
120     }
121
122     /* Create the root node */
123     PL_LOCK;
124     p_playlist->p_root = playlist_NodeCreate( p_playlist, NULL, NULL,
125                                     0, NULL );
126     PL_UNLOCK;
127     if( !p_playlist->p_root ) return NULL;
128
129     /* Create currently playing items node */
130     PL_LOCK;
131     p_playlist->p_playing = playlist_NodeCreate(
132         p_playlist, _( "Playlist" ), p_playlist->p_root,
133         PLAYLIST_RO_FLAG, NULL );
134
135     PL_UNLOCK;
136
137     if( !p_playlist->p_playing ) return NULL;
138
139     /* Create media library node */
140     const bool b_ml = var_InheritBool( p_parent, "media-library");
141     if( b_ml )
142     {
143         PL_LOCK;
144         p_playlist->p_media_library = playlist_NodeCreate(
145             p_playlist, _( "Media Library" ), p_playlist->p_root,
146             PLAYLIST_RO_FLAG, NULL );
147         PL_UNLOCK;
148
149         if(!p_playlist->p_media_library ) return NULL;
150     }
151     else
152     {
153         p_playlist->p_media_library = NULL;
154     }
155
156     p_playlist->p_root_category = p_playlist->p_root;
157     p_playlist->p_root_onelevel = p_playlist->p_root;
158     p_playlist->p_local_category = p_playlist->p_playing;
159     p_playlist->p_local_onelevel = p_playlist->p_playing;
160     p_playlist->p_ml_category = p_playlist->p_media_library;
161     p_playlist->p_ml_onelevel = p_playlist->p_media_library;;
162
163     /* Initial status */
164     pl_priv(p_playlist)->status.p_item = NULL;
165     pl_priv(p_playlist)->status.p_node = p_playlist->p_playing;
166     pl_priv(p_playlist)->request.b_request = false;
167     pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
168
169     if(b_ml)
170     {
171         const bool b_auto_preparse = pl_priv(p_playlist)->b_auto_preparse;
172         pl_priv(p_playlist)->b_auto_preparse = false;
173         playlist_MLLoad( p_playlist );
174         pl_priv(p_playlist)->b_auto_preparse = b_auto_preparse;
175     }
176
177     vlc_object_set_destructor( p_playlist, playlist_Destructor );
178
179     return p_playlist;
180 }
181
182 void playlist_Destroy( playlist_t *p_playlist )
183 {
184     playlist_private_t *p_sys = pl_priv(p_playlist);
185
186     msg_Dbg( p_playlist, "destroying" );
187     if( p_sys->p_preparser )
188         playlist_preparser_Delete( p_sys->p_preparser );
189     if( p_sys->p_fetcher )
190         playlist_fetcher_Delete( p_sys->p_fetcher );
191     vlc_object_release( p_playlist );
192 }
193
194 /**
195  * Destroy playlist
196  *
197  * Destroy a playlist structure.
198  * \param p_playlist the playlist object
199  * \return nothing
200  */
201
202 static void playlist_Destructor( vlc_object_t * p_this )
203 {
204     playlist_t *p_playlist = (playlist_t *)p_this;
205     playlist_private_t *p_sys = pl_priv(p_playlist);
206
207     assert( !p_sys->p_input );
208     assert( !p_sys->p_input_resource );
209
210     vlc_cond_destroy( &p_sys->signal );
211     vlc_mutex_destroy( &p_sys->lock );
212
213     /* Remove all remaining items */
214     FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
215         free( p_del->pp_children );
216         vlc_gc_decref( p_del->p_input );
217         free( p_del );
218     FOREACH_END();
219     ARRAY_RESET( p_playlist->all_items );
220     FOREACH_ARRAY( playlist_item_t *p_del, p_sys->items_to_delete )
221         free( p_del->pp_children );
222         vlc_gc_decref( p_del->p_input );
223         free( p_del );
224     FOREACH_END();
225     ARRAY_RESET( p_sys->items_to_delete );
226
227     ARRAY_RESET( p_playlist->items );
228     ARRAY_RESET( p_playlist->current );
229
230     msg_Dbg( p_this, "Destroyed" );
231 }
232
233 /** Get current playing input.
234  */
235 input_thread_t * playlist_CurrentInput( playlist_t * p_playlist )
236 {
237     input_thread_t * p_input;
238     PL_LOCK;
239     p_input = pl_priv(p_playlist)->p_input;
240     if( p_input ) vlc_object_hold( p_input );
241     PL_UNLOCK;
242     return p_input;
243 }
244
245 /**
246  * @}
247  */
248
249 /** Accessor for status item and status nodes.
250  */
251 playlist_item_t * get_current_status_item( playlist_t * p_playlist )
252 {
253     PL_ASSERT_LOCKED;
254
255     return pl_priv(p_playlist)->status.p_item;
256 }
257
258 playlist_item_t * get_current_status_node( playlist_t * p_playlist )
259 {
260     PL_ASSERT_LOCKED;
261
262     return pl_priv(p_playlist)->status.p_node;
263 }
264
265 void set_current_status_item( playlist_t * p_playlist,
266     playlist_item_t * p_item )
267 {
268     PL_ASSERT_LOCKED;
269
270     if( pl_priv(p_playlist)->status.p_item &&
271         pl_priv(p_playlist)->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG &&
272         pl_priv(p_playlist)->status.p_item != p_item )
273     {
274         /* It's unsafe given current design to delete a playlist item :(
275         playlist_ItemDelete( pl_priv(p_playlist)->status.p_item ); */
276     }
277     pl_priv(p_playlist)->status.p_item = p_item;
278 }
279
280 void set_current_status_node( playlist_t * p_playlist,
281     playlist_item_t * p_node )
282 {
283     PL_ASSERT_LOCKED;
284
285     if( pl_priv(p_playlist)->status.p_node &&
286         pl_priv(p_playlist)->status.p_node->i_flags & PLAYLIST_REMOVE_FLAG &&
287         pl_priv(p_playlist)->status.p_node != p_node )
288     {
289         /* It's unsafe given current design to delete a playlist item :(
290         playlist_ItemDelete( pl_priv(p_playlist)->status.p_node ); */
291     }
292     pl_priv(p_playlist)->status.p_node = p_node;
293 }
294
295 static void VariablesInit( playlist_t *p_playlist )
296 {
297     /* These variables control updates */
298     var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
299     var_SetBool( p_playlist, "intf-change", true );
300
301     var_Create( p_playlist, "item-change", VLC_VAR_ADDRESS );
302     var_Create( p_playlist, "leaf-to-parent", VLC_VAR_ADDRESS );
303
304     var_Create( p_playlist, "playlist-item-deleted", VLC_VAR_INTEGER );
305     var_SetInteger( p_playlist, "playlist-item-deleted", -1 );
306
307     var_Create( p_playlist, "playlist-item-append", VLC_VAR_ADDRESS );
308
309     var_Create( p_playlist, "item-current", VLC_VAR_ADDRESS );
310     var_Create( p_playlist, "input-current", VLC_VAR_ADDRESS );
311
312     var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
313     var_SetInteger( p_playlist, "activity", 0 );
314
315     /* Variables to control playback */
316     var_Create( p_playlist, "play-and-stop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
317     var_Create( p_playlist, "play-and-exit", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
318     var_Create( p_playlist, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
319     var_Create( p_playlist, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
320     var_Create( p_playlist, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
321
322     var_AddCallback( p_playlist, "random", RandomCallback, NULL );
323
324     /* */
325     var_Create( p_playlist, "album-art", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
326 }
327
328 playlist_item_t * playlist_CurrentPlayingItem( playlist_t * p_playlist )
329 {
330     PL_ASSERT_LOCKED;
331
332     return pl_priv(p_playlist)->status.p_item;
333 }
334
335 int playlist_Status( playlist_t * p_playlist )
336 {
337     PL_ASSERT_LOCKED;
338
339     return pl_priv(p_playlist)->status.i_status;
340 }
341