AVFormat Demuxer : Set fourcc based on source container.
[vlc/solaris.git] / src / playlist / engine.c
blob8a40f47a50023eb769d26dd0532d26977b0b2e2e
1 /*****************************************************************************
2 * engine.c : Run the playlist and handle its control
3 *****************************************************************************
4 * Copyright (C) 1999-2008 the VideoLAN team
6 * Authors: Samuel Hocevar <sam@zoy.org>
7 * Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
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 */
37 /*****************************************************************************
38 * Local prototypes
39 *****************************************************************************/
40 static void VariablesInit( playlist_t *p_playlist );
42 static int RandomCallback( vlc_object_t *p_this, char const *psz_cmd,
43 vlc_value_t oldval, vlc_value_t newval, void *a )
45 (void)psz_cmd; (void)oldval; (void)newval; (void)a;
46 playlist_t *p_playlist = (playlist_t*)p_this;
48 PL_LOCK;
50 pl_priv(p_playlist)->b_reset_currently_playing = true;
51 vlc_cond_signal( &pl_priv(p_playlist)->signal );
53 PL_UNLOCK;
54 return VLC_SUCCESS;
57 /**
58 * Create playlist
60 * Create a playlist structure.
61 * \param p_parent the vlc object that is to be the parent of this playlist
62 * \return a pointer to the created playlist, or NULL on error
64 playlist_t * playlist_Create( vlc_object_t *p_parent )
66 static const char playlist_name[] = "playlist";
67 playlist_t *p_playlist;
68 playlist_private_t *p;
70 /* Allocate structure */
71 p = vlc_custom_create( p_parent, sizeof( *p ),
72 VLC_OBJECT_GENERIC, playlist_name );
73 if( !p )
74 return NULL;
76 assert( offsetof( playlist_private_t, public_data ) == 0 );
77 p_playlist = &p->public_data;
78 vlc_object_attach( p_playlist, p_parent );
79 TAB_INIT( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds );
81 libvlc_priv(p_parent->p_libvlc)->p_playlist = p_playlist;
83 VariablesInit( p_playlist );
84 vlc_mutex_init( &p->lock );
85 vlc_cond_init( &p->signal );
87 /* Initialise data structures */
88 pl_priv(p_playlist)->i_last_playlist_id = 0;
89 pl_priv(p_playlist)->p_input = NULL;
91 ARRAY_INIT( p_playlist->items );
92 ARRAY_INIT( p_playlist->all_items );
93 ARRAY_INIT( pl_priv(p_playlist)->items_to_delete );
94 ARRAY_INIT( p_playlist->current );
96 p_playlist->i_current_index = 0;
97 pl_priv(p_playlist)->b_reset_currently_playing = true;
98 pl_priv(p_playlist)->last_rebuild_date = 0;
100 pl_priv(p_playlist)->b_tree = var_InheritBool( p_parent, "playlist-tree" );
102 pl_priv(p_playlist)->b_doing_ml = false;
104 pl_priv(p_playlist)->b_auto_preparse =
105 var_InheritBool( p_parent, "auto-preparse" );
107 /* Fetcher */
108 p->p_fetcher = playlist_fetcher_New( p_playlist );
109 if( unlikely(p->p_fetcher == NULL) )
111 msg_Err( p_playlist, "cannot create fetcher" );
112 p->p_preparser = NULL;
114 else
115 { /* Preparse */
116 p->p_preparser = playlist_preparser_New( p_playlist, p->p_fetcher );
117 if( unlikely(p->p_preparser == NULL) )
118 msg_Err( p_playlist, "cannot create preparser" );
121 /* Create the root node */
122 PL_LOCK;
123 p_playlist->p_root = playlist_NodeCreate( p_playlist, NULL, NULL,
124 PLAYLIST_END, 0, NULL );
125 PL_UNLOCK;
126 if( !p_playlist->p_root ) return NULL;
128 /* Create currently playing items node */
129 PL_LOCK;
130 p_playlist->p_playing = playlist_NodeCreate(
131 p_playlist, _( "Playlist" ), p_playlist->p_root,
132 PLAYLIST_END, PLAYLIST_RO_FLAG, NULL );
134 PL_UNLOCK;
136 if( !p_playlist->p_playing ) return NULL;
138 /* Create media library node */
139 const bool b_ml = var_InheritBool( p_parent, "media-library");
140 if( b_ml )
142 PL_LOCK;
143 p_playlist->p_media_library = playlist_NodeCreate(
144 p_playlist, _( "Media Library" ), p_playlist->p_root,
145 PLAYLIST_END, PLAYLIST_RO_FLAG, NULL );
146 PL_UNLOCK;
148 if(!p_playlist->p_media_library ) return NULL;
150 else
152 p_playlist->p_media_library = NULL;
155 p_playlist->p_root_category = p_playlist->p_root;
156 p_playlist->p_root_onelevel = p_playlist->p_root;
157 p_playlist->p_local_category = p_playlist->p_playing;
158 p_playlist->p_local_onelevel = p_playlist->p_playing;
159 p_playlist->p_ml_category = p_playlist->p_media_library;
160 p_playlist->p_ml_onelevel = p_playlist->p_media_library;;
162 /* Initial status */
163 pl_priv(p_playlist)->status.p_item = NULL;
164 pl_priv(p_playlist)->status.p_node = p_playlist->p_playing;
165 pl_priv(p_playlist)->request.b_request = false;
166 pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
168 if(b_ml)
170 const bool b_auto_preparse = pl_priv(p_playlist)->b_auto_preparse;
171 pl_priv(p_playlist)->b_auto_preparse = false;
172 playlist_MLLoad( p_playlist );
173 pl_priv(p_playlist)->b_auto_preparse = b_auto_preparse;
176 return p_playlist;
180 * Destroy playlist.
181 * This is not thread-safe. Any reference to the playlist is assumed gone.
182 * (In particular, all interface and services threads must have been joined).
184 * \param p_playlist the playlist object
186 void playlist_Destroy( playlist_t *p_playlist )
188 playlist_private_t *p_sys = pl_priv(p_playlist);
190 msg_Dbg( p_playlist, "destroying" );
191 if( p_sys->p_preparser )
192 playlist_preparser_Delete( p_sys->p_preparser );
193 if( p_sys->p_fetcher )
194 playlist_fetcher_Delete( p_sys->p_fetcher );
196 /* Already cleared when deactivating (if activated anyway) */
197 assert( !p_sys->p_input );
198 assert( !p_sys->p_input_resource );
200 vlc_cond_destroy( &p_sys->signal );
201 vlc_mutex_destroy( &p_sys->lock );
203 /* Remove all remaining items */
204 FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
205 free( p_del->pp_children );
206 vlc_gc_decref( p_del->p_input );
207 free( p_del );
208 FOREACH_END();
209 ARRAY_RESET( p_playlist->all_items );
210 FOREACH_ARRAY( playlist_item_t *p_del, p_sys->items_to_delete )
211 free( p_del->pp_children );
212 vlc_gc_decref( p_del->p_input );
213 free( p_del );
214 FOREACH_END();
215 ARRAY_RESET( p_sys->items_to_delete );
217 ARRAY_RESET( p_playlist->items );
218 ARRAY_RESET( p_playlist->current );
220 vlc_object_release( p_playlist );
223 /** Get current playing input.
225 input_thread_t * playlist_CurrentInput( playlist_t * p_playlist )
227 input_thread_t * p_input;
228 PL_LOCK;
229 p_input = pl_priv(p_playlist)->p_input;
230 if( p_input ) vlc_object_hold( p_input );
231 PL_UNLOCK;
232 return p_input;
236 * @}
239 /** Accessor for status item and status nodes.
241 playlist_item_t * get_current_status_item( playlist_t * p_playlist )
243 PL_ASSERT_LOCKED;
245 return pl_priv(p_playlist)->status.p_item;
248 playlist_item_t * get_current_status_node( playlist_t * p_playlist )
250 PL_ASSERT_LOCKED;
252 return pl_priv(p_playlist)->status.p_node;
255 void set_current_status_item( playlist_t * p_playlist,
256 playlist_item_t * p_item )
258 PL_ASSERT_LOCKED;
260 if( pl_priv(p_playlist)->status.p_item &&
261 pl_priv(p_playlist)->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG &&
262 pl_priv(p_playlist)->status.p_item != p_item )
264 /* It's unsafe given current design to delete a playlist item :(
265 playlist_ItemDelete( pl_priv(p_playlist)->status.p_item ); */
267 pl_priv(p_playlist)->status.p_item = p_item;
270 void set_current_status_node( playlist_t * p_playlist,
271 playlist_item_t * p_node )
273 PL_ASSERT_LOCKED;
275 if( pl_priv(p_playlist)->status.p_node &&
276 pl_priv(p_playlist)->status.p_node->i_flags & PLAYLIST_REMOVE_FLAG &&
277 pl_priv(p_playlist)->status.p_node != p_node )
279 /* It's unsafe given current design to delete a playlist item :(
280 playlist_ItemDelete( pl_priv(p_playlist)->status.p_node ); */
282 pl_priv(p_playlist)->status.p_node = p_node;
285 static input_thread_t *playlist_FindInput( vlc_object_t *object )
287 assert( object == VLC_OBJECT(pl_Get(object)) );
288 return playlist_CurrentInput( (playlist_t *)object );
291 static void VariablesInit( playlist_t *p_playlist )
293 /* These variables control updates */
294 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
295 var_SetBool( p_playlist, "intf-change", true );
297 var_Create( p_playlist, "item-change", VLC_VAR_ADDRESS );
298 var_Create( p_playlist, "leaf-to-parent", VLC_VAR_ADDRESS );
300 var_Create( p_playlist, "playlist-item-deleted", VLC_VAR_INTEGER );
301 var_SetInteger( p_playlist, "playlist-item-deleted", -1 );
303 var_Create( p_playlist, "playlist-item-append", VLC_VAR_ADDRESS );
305 var_Create( p_playlist, "item-current", VLC_VAR_ADDRESS );
306 var_Create( p_playlist, "input-current", VLC_VAR_ADDRESS );
308 var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
309 var_SetInteger( p_playlist, "activity", 0 );
311 /* Variables to control playback */
312 var_Create( p_playlist, "play-and-stop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
313 var_Create( p_playlist, "play-and-exit", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
314 var_Create( p_playlist, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
315 var_Create( p_playlist, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
316 var_Create( p_playlist, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
318 var_AddCallback( p_playlist, "random", RandomCallback, NULL );
320 /* */
321 var_Create( p_playlist, "album-art", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
323 /* Variables to preserve video output parameters */
324 var_Create( p_playlist, "fullscreen", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
325 var_Create( p_playlist, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
327 /* Audio output parameters */
328 var_Create( p_playlist, "volume-muted", VLC_VAR_BOOL );
329 var_Create( p_playlist, "saved-volume", VLC_VAR_INTEGER );
330 var_Create( p_playlist, "volume-change", VLC_VAR_VOID );
331 /* FIXME: horrible hack for audio output interface code */
332 var_Create( p_playlist, "find-input-callback", VLC_VAR_ADDRESS );
333 var_SetAddress( p_playlist, "find-input-callback", playlist_FindInput );
336 playlist_item_t * playlist_CurrentPlayingItem( playlist_t * p_playlist )
338 PL_ASSERT_LOCKED;
340 return pl_priv(p_playlist)->status.p_item;
343 int playlist_Status( playlist_t * p_playlist )
345 PL_ASSERT_LOCKED;
347 return pl_priv(p_playlist)->status.i_status;