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