]> git.sesse.net Git - vlc/blob - src/playlist/engine.c
b6f82ed12a1b214dfa81c52b9c4c2cdb55c9df6d
[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     TAB_INIT( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds );
80
81     libvlc_priv(p_parent->p_libvlc)->p_playlist = p_playlist;
82
83     VariablesInit( p_playlist );
84     vlc_cond_init( &p->signal );
85
86     /* Initialise data structures */
87     pl_priv(p_playlist)->i_last_playlist_id = 0;
88     pl_priv(p_playlist)->p_input = NULL;
89
90     ARRAY_INIT( p_playlist->items );
91     ARRAY_INIT( p_playlist->all_items );
92     ARRAY_INIT( pl_priv(p_playlist)->items_to_delete );
93     ARRAY_INIT( p_playlist->current );
94
95     p_playlist->i_current_index = 0;
96     pl_priv(p_playlist)->b_reset_currently_playing = true;
97     pl_priv(p_playlist)->last_rebuild_date = 0;
98
99     pl_priv(p_playlist)->b_tree = var_CreateGetBool( p_playlist, "playlist-tree" );
100
101     pl_priv(p_playlist)->b_doing_ml = false;
102
103     pl_priv(p_playlist)->b_auto_preparse =
104                         var_CreateGetBool( p_playlist, "auto-preparse" ) ;
105
106     PL_LOCK; /* playlist_NodeCreate will check for it */
107     p_playlist->p_root_category = playlist_NodeCreate( p_playlist, NULL, NULL,
108                                     0, NULL );
109     p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL,
110                                     0, p_playlist->p_root_category->p_input );
111     PL_UNLOCK;
112
113     if( !p_playlist->p_root_category || !p_playlist->p_root_onelevel )
114         return NULL;
115
116     /* Create playlist and media library */
117     PL_LOCK; /* playlist_NodesPairCreate will check for it */
118     playlist_NodesPairCreate( p_playlist, _( "Playlist" ),
119                             &p_playlist->p_local_category,
120                             &p_playlist->p_local_onelevel, false );
121     PL_UNLOCK;
122
123     p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
124     p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;
125
126     if( !p_playlist->p_local_category || !p_playlist->p_local_onelevel ||
127         !p_playlist->p_local_category->p_input ||
128         !p_playlist->p_local_onelevel->p_input )
129         return NULL;
130
131     if( config_GetInt( p_playlist, "media-library") )
132     {
133         PL_LOCK; /* playlist_NodesPairCreate will check for it */
134         playlist_NodesPairCreate( p_playlist, _( "Media Library" ),
135                             &p_playlist->p_ml_category,
136                             &p_playlist->p_ml_onelevel, false );
137         PL_UNLOCK;
138
139         if(!p_playlist->p_ml_category || !p_playlist->p_ml_onelevel)
140             return NULL;
141
142         p_playlist->p_ml_category->i_flags |= PLAYLIST_RO_FLAG;
143         p_playlist->p_ml_onelevel->i_flags |= PLAYLIST_RO_FLAG;
144     }
145     else
146     {
147         p_playlist->p_ml_category = p_playlist->p_ml_onelevel = NULL;
148     }
149
150     /* Initial status */
151     pl_priv(p_playlist)->status.p_item = NULL;
152     pl_priv(p_playlist)->status.p_node = p_playlist->p_local_onelevel;
153     pl_priv(p_playlist)->request.b_request = false;
154     pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
155
156     pl_priv(p_playlist)->b_auto_preparse = false;
157     playlist_MLLoad( p_playlist );
158     pl_priv(p_playlist)->b_auto_preparse = true;
159
160     vlc_object_set_destructor( p_playlist, playlist_Destructor );
161
162     return p_playlist;
163 }
164
165 /**
166  * Destroy playlist
167  *
168  * Destroy a playlist structure.
169  * \param p_playlist the playlist object
170  * \return nothing
171  */
172
173 static void playlist_Destructor( vlc_object_t * p_this )
174 {
175     playlist_t *p_playlist = (playlist_t *)p_this;
176     playlist_private_t *p_sys = pl_priv(p_playlist);
177
178     assert( !p_sys->p_input );
179     assert( !p_sys->p_input_ressource );
180     assert( !p_sys->p_preparser );
181     assert( !p_sys->p_fetcher );
182
183     vlc_cond_destroy( &p_sys->signal );
184
185     /* Remove all remaining items */
186     FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
187         free( p_del->pp_children );
188         vlc_gc_decref( p_del->p_input );
189         free( p_del );
190     FOREACH_END();
191     ARRAY_RESET( p_playlist->all_items );
192     FOREACH_ARRAY( playlist_item_t *p_del, p_sys->items_to_delete )
193         free( p_del->pp_children );
194         vlc_gc_decref( p_del->p_input );
195         free( p_del );
196     FOREACH_END();
197     ARRAY_RESET( p_sys->items_to_delete );
198
199     ARRAY_RESET( p_playlist->items );
200     ARRAY_RESET( p_playlist->current );
201
202     msg_Dbg( p_this, "Destroyed" );
203 }
204
205 /** Get current playing input.
206  */
207 input_thread_t * playlist_CurrentInput( playlist_t * p_playlist )
208 {
209     input_thread_t * p_input;
210     PL_LOCK;
211     p_input = pl_priv(p_playlist)->p_input;
212     if( p_input ) vlc_object_hold( p_input );
213     PL_UNLOCK;
214     return p_input;
215 }
216
217 /**
218  * @}
219  */
220
221 /** Accessor for status item and status nodes.
222  */
223 playlist_item_t * get_current_status_item( playlist_t * p_playlist )
224 {
225     PL_ASSERT_LOCKED;
226
227     return pl_priv(p_playlist)->status.p_item;
228 }
229
230 playlist_item_t * get_current_status_node( playlist_t * p_playlist )
231 {
232     PL_ASSERT_LOCKED;
233
234     return pl_priv(p_playlist)->status.p_node;
235 }
236
237 void set_current_status_item( playlist_t * p_playlist,
238     playlist_item_t * p_item )
239 {
240     PL_ASSERT_LOCKED;
241
242     if( pl_priv(p_playlist)->status.p_item &&
243         pl_priv(p_playlist)->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG &&
244         pl_priv(p_playlist)->status.p_item != p_item )
245     {
246         /* It's unsafe given current design to delete a playlist item :(
247         playlist_ItemDelete( pl_priv(p_playlist)->status.p_item ); */
248     }
249     pl_priv(p_playlist)->status.p_item = p_item;
250 }
251
252 void set_current_status_node( playlist_t * p_playlist,
253     playlist_item_t * p_node )
254 {
255     PL_ASSERT_LOCKED;
256
257     if( pl_priv(p_playlist)->status.p_node &&
258         pl_priv(p_playlist)->status.p_node->i_flags & PLAYLIST_REMOVE_FLAG &&
259         pl_priv(p_playlist)->status.p_node != p_node )
260     {
261         /* It's unsafe given current design to delete a playlist item :(
262         playlist_ItemDelete( pl_priv(p_playlist)->status.p_node ); */
263     }
264     pl_priv(p_playlist)->status.p_node = p_node;
265 }
266
267 static void VariablesInit( playlist_t *p_playlist )
268 {
269     vlc_value_t val;
270     /* These variables control updates */
271     var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
272     val.b_bool = true;
273     var_Set( p_playlist, "intf-change", val );
274
275     var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
276     val.i_int = -1;
277     var_Set( p_playlist, "item-change", val );
278
279     var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
280     val.i_int = -1;
281     var_Set( p_playlist, "item-deleted", val );
282
283     var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
284
285     var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
286     val.i_int = -1;
287     var_Set( p_playlist, "playlist-current", val );
288
289     var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
290     var_SetInteger( p_playlist, "activity", 0 );
291
292     /* Variables to control playback */
293     var_Create( p_playlist, "play-and-stop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
294     var_Create( p_playlist, "play-and-exit", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
295     var_Create( p_playlist, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
296     var_Create( p_playlist, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
297     var_Create( p_playlist, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
298
299     var_AddCallback( p_playlist, "random", RandomCallback, NULL );
300
301     /* */
302     var_Create( p_playlist, "album-art", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
303 }
304
305 int playlist_CurrentId( playlist_t * p_playlist )
306 {
307     return pl_priv(p_playlist)->status.p_item->i_id;
308
309 }
310
311 bool playlist_IsPlaying( playlist_t * p_playlist )
312 {
313     return ( pl_priv(p_playlist)->status.i_status == PLAYLIST_RUNNING &&
314             !(pl_priv(p_playlist)->request.b_request && pl_priv(p_playlist)->request.i_status == PLAYLIST_STOPPED) );
315 }
316
317 playlist_item_t * playlist_CurrentPlayingItem( playlist_t * p_playlist )
318 {
319     return pl_priv(p_playlist)->status.p_item;
320 }
321
322 int playlist_Status( playlist_t * p_playlist )
323 {
324     return pl_priv(p_playlist)->status.i_status;
325 }