Fix the video controls extended panels (make extensive use of the object's name).
[vlc.git] / src / control / media_list_player.c
blob7fc516665c9d8989b5111d3366e38716b3759bc2
1 /*****************************************************************************
2 * media_list_player.c: libvlc new API media_list player functions
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
5 * $Id$
7 * Authors: Pierre d'Herbemont <pdherbemont # 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 *****************************************************************************/
23 #include "libvlc_internal.h"
24 #include <vlc/libvlc.h>
25 #include "media_list_path.h"
28 * Private functions
31 /**************************************************************************
32 * get_next_index (private)
34 * Simple next item fetcher.
35 **************************************************************************/
36 static libvlc_media_list_path_t
37 get_next_path( libvlc_media_list_player_t * p_mlp )
39 /* We are entered with libvlc_media_list_lock( p_mlp->p_list ) */
40 libvlc_media_list_path_t ret;
41 libvlc_media_list_t * p_parent_of_playing_item;
42 libvlc_media_list_t * p_sublist_of_playing_item;
43 p_sublist_of_playing_item = libvlc_media_list_sublist_at_path(
44 p_mlp->p_mlist,
45 p_mlp->current_playing_item_path );
47 /* If item just gained a sublist just play it */
48 if( p_sublist_of_playing_item )
50 libvlc_media_list_release( p_sublist_of_playing_item );
51 return libvlc_media_list_path_copy_by_appending( p_mlp->current_playing_item_path, 0 );
54 /* Try to catch next element */
55 p_parent_of_playing_item = libvlc_media_list_parentlist_at_path(
56 p_mlp->p_mlist,
57 p_mlp->current_playing_item_path );
59 int deepness = libvlc_media_list_path_deepness( p_mlp->current_playing_item_path );
60 if( deepness < 1 || !p_parent_of_playing_item )
61 return NULL;
63 ret = libvlc_media_list_path_copy( p_mlp->current_playing_item_path );
65 while( ret[deepness-1] >= libvlc_media_list_count( p_parent_of_playing_item, NULL ) )
67 deepness--;
68 if( deepness <= 0 )
70 free( ret );
71 libvlc_media_list_release( p_parent_of_playing_item );
72 return NULL;
74 ret[deepness] = -1;
75 ret[deepness-1]++;
76 p_parent_of_playing_item = libvlc_media_list_parentlist_at_path(
77 p_mlp->p_mlist,
78 ret );
80 libvlc_media_list_release( p_parent_of_playing_item );
81 return ret;
84 /**************************************************************************
85 * media_instance_reached_end (private) (Event Callback)
86 **************************************************************************/
87 static void
88 media_instance_reached_end( const libvlc_event_t * p_event,
89 void * p_user_data )
91 libvlc_media_list_player_t * p_mlp = p_user_data;
92 libvlc_media_instance_t * p_mi = p_event->p_obj;
93 libvlc_media_descriptor_t *p_md, * p_current_md;
95 p_md = libvlc_media_instance_get_media_descriptor( p_mi, NULL );
96 /* XXX: need if p_mlp->p_current_playing_index is beyond */
97 p_current_md = libvlc_media_list_item_at_path(
98 p_mlp->p_mlist,
99 p_mlp->current_playing_item_path );
100 if( p_md != p_current_md )
102 msg_Warn( p_mlp->p_libvlc_instance->p_libvlc_int,
103 "We are not sync-ed with the media instance" );
104 libvlc_media_descriptor_release( p_md );
105 libvlc_media_descriptor_release( p_current_md );
106 return;
108 libvlc_media_descriptor_release( p_md );
109 libvlc_media_descriptor_release( p_current_md );
110 libvlc_media_list_player_next( p_mlp, NULL );
113 /**************************************************************************
114 * playlist_item_deleted (private) (Event Callback)
115 **************************************************************************/
116 static void
117 mlist_item_deleted( const libvlc_event_t * p_event, void * p_user_data )
119 libvlc_media_descriptor_t * p_current_md;
120 libvlc_media_list_player_t * p_mlp = p_user_data;
121 libvlc_media_list_t * p_emitting_mlist = p_event->p_obj;
122 /* XXX: need if p_mlp->p_current_playing_index is beyond */
123 p_current_md = libvlc_media_list_item_at_path(
124 p_mlp->p_mlist,
125 p_mlp->current_playing_item_path );
127 if( p_event->u.media_list_item_deleted.item == p_current_md &&
128 p_emitting_mlist == p_mlp->p_mlist )
130 /* We are playing this item, we choose to stop */
131 libvlc_media_list_player_stop( p_mlp, NULL );
135 /**************************************************************************
136 * install_playlist_observer (private)
137 **************************************************************************/
138 static void
139 install_playlist_observer( libvlc_media_list_player_t * p_mlp )
141 libvlc_event_attach( libvlc_media_list_event_manager( p_mlp->p_mlist, NULL ),
142 libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL );
145 /**************************************************************************
146 * uninstall_playlist_observer (private)
147 **************************************************************************/
148 static void
149 uninstall_playlist_observer( libvlc_media_list_player_t * p_mlp )
151 libvlc_event_detach( libvlc_media_list_event_manager( p_mlp->p_mlist, NULL ),
152 libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL );
155 /**************************************************************************
156 * install_media_instance_observer (private)
157 **************************************************************************/
158 static void
159 install_media_instance_observer( libvlc_media_list_player_t * p_mlp )
161 libvlc_event_attach( libvlc_media_instance_event_manager( p_mlp->p_mi, NULL ),
162 libvlc_MediaInstanceReachedEnd,
163 media_instance_reached_end, p_mlp, NULL );
167 /**************************************************************************
168 * uninstall_media_instance_observer (private)
169 **************************************************************************/
170 static void
171 uninstall_media_instance_observer( libvlc_media_list_player_t * p_mlp )
173 libvlc_event_detach( libvlc_media_instance_event_manager( p_mlp->p_mi, NULL ),
174 libvlc_MediaInstanceReachedEnd,
175 media_instance_reached_end, p_mlp, NULL );
178 /**************************************************************************
179 * set_current_playing_item (private)
181 * Playlist lock should be held
182 **************************************************************************/
183 static void
184 set_current_playing_item( libvlc_media_list_player_t * p_mlp,
185 libvlc_media_list_path_t path,
186 libvlc_exception_t * p_e )
188 libvlc_media_descriptor_t * p_md;
190 p_md = libvlc_media_list_item_at_path( p_mlp->p_mlist, path );
191 vlc_mutex_lock( &p_mlp->object_lock );
193 free( p_mlp->current_playing_item_path );
194 p_mlp->current_playing_item_path = path;
196 if( !p_md )
198 vlc_mutex_unlock( &p_mlp->object_lock );
199 return;
202 /* We are not interested in getting media_descriptor stop event now */
203 uninstall_media_instance_observer( p_mlp );
204 if( p_md->p_subitems && libvlc_media_list_count( p_md->p_subitems, NULL ) > 0 )
206 libvlc_media_descriptor_t * p_submd;
207 p_submd = libvlc_media_list_item_at_index( p_md->p_subitems, 0, NULL );
208 libvlc_media_instance_set_media_descriptor( p_mlp->p_mi, p_submd, NULL );
209 libvlc_media_descriptor_release( p_submd );
211 else
212 libvlc_media_instance_set_media_descriptor( p_mlp->p_mi, p_md, NULL );
213 // wait_playing_state(); /* If we want to be synchronous */
214 install_media_instance_observer( p_mlp );
216 vlc_mutex_unlock( &p_mlp->object_lock );
218 libvlc_media_list_unlock( p_mlp->p_mlist );
220 libvlc_media_descriptor_release( p_md ); /* for libvlc_media_list_item_at_index */
224 * Public libvlc functions
227 /**************************************************************************
228 * new (Public)
229 **************************************************************************/
230 libvlc_media_list_player_t *
231 libvlc_media_list_player_new( libvlc_instance_t * p_instance,
232 libvlc_exception_t * p_e )
234 (void)p_e;
235 libvlc_media_list_player_t * p_mlp;
236 p_mlp = malloc(sizeof(libvlc_media_list_player_t));
237 p_mlp->current_playing_item_path = NULL;
238 p_mlp->p_mi = NULL;
239 p_mlp->p_mlist = NULL;
240 vlc_mutex_init( p_instance->p_libvlc_int, &p_mlp->object_lock );
241 p_mlp->p_event_manager = libvlc_event_manager_new( p_mlp,
242 p_instance,
243 p_e );
244 libvlc_event_manager_register_event_type( p_mlp->p_event_manager,
245 libvlc_MediaListPlayerNextItemSet, p_e );
247 return p_mlp;
250 /**************************************************************************
251 * release (Public)
252 **************************************************************************/
253 void libvlc_media_list_player_release( libvlc_media_list_player_t * p_mlp )
255 free(p_mlp);
258 /**************************************************************************
259 * set_media_instance (Public)
260 **************************************************************************/
261 void libvlc_media_list_player_set_media_instance(
262 libvlc_media_list_player_t * p_mlp,
263 libvlc_media_instance_t * p_mi,
264 libvlc_exception_t * p_e )
266 vlc_mutex_lock( &p_mlp->object_lock );
268 if( p_mlp->p_mi )
270 uninstall_media_instance_observer( p_mlp );
271 libvlc_media_instance_release( p_mlp->p_mi );
273 libvlc_media_instance_retain( p_mi );
274 p_mlp->p_mi = p_mi;
276 install_media_instance_observer( p_mlp );
278 vlc_mutex_unlock( &p_mlp->object_lock );
281 /**************************************************************************
282 * set_media_list (Public)
283 **************************************************************************/
284 void libvlc_media_list_player_set_media_list(
285 libvlc_media_list_player_t * p_mlp,
286 libvlc_media_list_t * p_mlist,
287 libvlc_exception_t * p_e )
289 vlc_mutex_lock( &p_mlp->object_lock );
291 if( libvlc_media_list_player_is_playing( p_mlp, p_e ) )
293 libvlc_media_instance_stop( p_mlp->p_mi, p_e );
294 /* Don't bother if there was an error. */
295 libvlc_exception_clear( p_e );
298 if( p_mlp->p_mlist )
300 uninstall_playlist_observer( p_mlp );
301 libvlc_media_list_release( p_mlp->p_mlist );
303 libvlc_media_list_retain( p_mlist );
304 p_mlp->p_mlist = p_mlist;
306 install_playlist_observer( p_mlp );
308 vlc_mutex_unlock( &p_mlp->object_lock );
311 /**************************************************************************
312 * Play (Public)
313 **************************************************************************/
314 void libvlc_media_list_player_play( libvlc_media_list_player_t * p_mlp,
315 libvlc_exception_t * p_e )
317 if( !p_mlp->current_playing_item_path )
319 libvlc_media_list_player_next( p_mlp, p_e );
320 return; /* Will set to play */
323 libvlc_media_instance_play( p_mlp->p_mi, p_e );
327 /**************************************************************************
328 * Pause (Public)
329 **************************************************************************/
330 void libvlc_media_list_player_pause( libvlc_media_list_player_t * p_mlp,
331 libvlc_exception_t * p_e )
333 if( !p_mlp->p_mi )
334 return;
335 libvlc_media_instance_pause( p_mlp->p_mi, p_e );
338 /**************************************************************************
339 * is_playing (Public)
340 **************************************************************************/
341 vlc_bool_t
342 libvlc_media_list_player_is_playing( libvlc_media_list_player_t * p_mlp,
343 libvlc_exception_t * p_e )
345 libvlc_state_t state = libvlc_media_instance_get_state( p_mlp->p_mi, p_e );
346 return (state == libvlc_Opening) || (state == libvlc_Buffering) ||
347 (state == libvlc_Playing);
350 /**************************************************************************
351 * State (Public)
352 **************************************************************************/
353 libvlc_state_t
354 libvlc_media_list_player_get_state( libvlc_media_list_player_t * p_mlp,
355 libvlc_exception_t * p_e )
357 if( !p_mlp->p_mi )
358 return libvlc_Stopped;
359 return libvlc_media_instance_get_state( p_mlp->p_mi, p_e );
362 /**************************************************************************
363 * Play item at index (Public)
364 **************************************************************************/
365 void libvlc_media_list_player_play_item_at_index(
366 libvlc_media_list_player_t * p_mlp,
367 int i_index,
368 libvlc_exception_t * p_e )
370 set_current_playing_item( p_mlp, libvlc_media_list_path_with_root_index(i_index), p_e );
372 if( libvlc_exception_raised( p_e ) )
373 return;
375 /* Send the next item event */
376 libvlc_event_t event;
377 event.type = libvlc_MediaListPlayerNextItemSet;
378 libvlc_event_send( p_mlp->p_event_manager, &event );
380 libvlc_media_instance_play( p_mlp->p_mi, p_e );
383 /**************************************************************************
384 * Play item (Public)
385 **************************************************************************/
386 void libvlc_media_list_player_play_item(
387 libvlc_media_list_player_t * p_mlp,
388 libvlc_media_descriptor_t * p_md,
389 libvlc_exception_t * p_e )
391 libvlc_media_list_path_t path = libvlc_media_list_path_of_item( p_mlp->p_mlist, p_md );
392 if( !path )
394 libvlc_exception_raise( p_e, "No such item in media list" );
395 return;
397 set_current_playing_item( p_mlp, path, p_e );
399 if( libvlc_exception_raised( p_e ) )
400 return;
402 libvlc_media_instance_play( p_mlp->p_mi, p_e );
405 /**************************************************************************
406 * Stop (Public)
407 **************************************************************************/
408 void libvlc_media_list_player_stop( libvlc_media_list_player_t * p_mlp,
409 libvlc_exception_t * p_e )
411 libvlc_media_instance_stop( p_mlp->p_mi, p_e );
413 vlc_mutex_lock( &p_mlp->object_lock );
414 free( p_mlp->current_playing_item_path );
415 p_mlp->current_playing_item_path = NULL;
416 vlc_mutex_unlock( &p_mlp->object_lock );
419 /**************************************************************************
420 * Next (Public)
421 **************************************************************************/
422 void libvlc_media_list_player_next( libvlc_media_list_player_t * p_mlp,
423 libvlc_exception_t * p_e )
425 libvlc_media_list_path_t path;
427 libvlc_media_list_lock( p_mlp->p_mlist );
429 path = get_next_path( p_mlp );
431 if( !path )
433 libvlc_media_list_unlock( p_mlp->p_mlist );
434 libvlc_exception_raise( p_e, "No more element to play" );
435 libvlc_media_list_player_stop( p_mlp, p_e );
436 return;
439 set_current_playing_item( p_mlp, path, p_e );
441 libvlc_media_instance_play( p_mlp->p_mi, p_e );
443 libvlc_media_list_unlock( p_mlp->p_mlist );
445 /* Send the next item event */
446 libvlc_event_t event;
447 event.type = libvlc_MediaListPlayerNextItemSet;
448 libvlc_event_send( p_mlp->p_event_manager, &event);