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