1 /*****************************************************************************
2 * media_list_player.c: libvlc new API media_list player functions
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
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"
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;
44 if ( !p_mlp->current_playing_item_path )
46 if( !libvlc_media_list_count( p_mlp->p_mlist, NULL ) )
48 return libvlc_media_list_path_with_root_index(0);
51 p_sublist_of_playing_item = libvlc_media_list_sublist_at_path(
53 p_mlp->current_playing_item_path );
55 /* If item just gained a sublist just play it */
56 if( p_sublist_of_playing_item )
58 libvlc_media_list_release( p_sublist_of_playing_item );
59 return libvlc_media_list_path_copy_by_appending( p_mlp->current_playing_item_path, 0 );
62 /* Try to catch next element */
63 p_parent_of_playing_item = libvlc_media_list_parentlist_at_path(
65 p_mlp->current_playing_item_path );
67 int depth = libvlc_media_list_path_depth( p_mlp->current_playing_item_path );
68 if( depth < 1 || !p_parent_of_playing_item )
71 ret = libvlc_media_list_path_copy( p_mlp->current_playing_item_path );
73 while( ret[depth-1] >= libvlc_media_list_count( p_parent_of_playing_item, NULL ) )
79 libvlc_media_list_release( p_parent_of_playing_item );
84 p_parent_of_playing_item = libvlc_media_list_parentlist_at_path(
88 libvlc_media_list_release( p_parent_of_playing_item );
92 /**************************************************************************
93 * media_player_reached_end (private) (Event Callback)
94 **************************************************************************/
96 media_player_reached_end( const libvlc_event_t * p_event,
99 libvlc_media_list_player_t * p_mlp = p_user_data;
100 libvlc_media_player_t * p_mi = p_event->p_obj;
101 libvlc_media_t *p_md, * p_current_md;
103 p_md = libvlc_media_player_get_media( p_mi, NULL );
104 /* XXX: need if p_mlp->p_current_playing_index is beyond */
105 p_current_md = libvlc_media_list_item_at_path(
107 p_mlp->current_playing_item_path );
108 if( p_md != p_current_md )
110 msg_Warn( p_mlp->p_libvlc_instance->p_libvlc_int,
111 "We are not sync-ed with the media instance" );
112 libvlc_media_release( p_md );
113 libvlc_media_release( p_current_md );
116 libvlc_media_release( p_md );
117 libvlc_media_release( p_current_md );
118 libvlc_media_list_player_next( p_mlp, NULL );
121 /**************************************************************************
122 * playlist_item_deleted (private) (Event Callback)
123 **************************************************************************/
125 mlist_item_deleted( const libvlc_event_t * p_event, void * p_user_data )
127 libvlc_media_t * p_current_md;
128 libvlc_media_list_player_t * p_mlp = p_user_data;
129 libvlc_media_list_t * p_emitting_mlist = p_event->p_obj;
130 /* XXX: need if p_mlp->p_current_playing_index is beyond */
131 p_current_md = libvlc_media_list_item_at_path(
133 p_mlp->current_playing_item_path );
135 if( p_event->u.media_list_item_deleted.item == p_current_md &&
136 p_emitting_mlist == p_mlp->p_mlist )
138 /* We are playing this item, we choose to stop */
139 libvlc_media_list_player_stop( p_mlp, NULL );
143 /**************************************************************************
144 * install_playlist_observer (private)
145 **************************************************************************/
147 install_playlist_observer( libvlc_media_list_player_t * p_mlp )
149 libvlc_event_attach( libvlc_media_list_event_manager( p_mlp->p_mlist, NULL ),
150 libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL );
153 /**************************************************************************
154 * uninstall_playlist_observer (private)
155 **************************************************************************/
157 uninstall_playlist_observer( libvlc_media_list_player_t * p_mlp )
159 if ( !p_mlp->p_mlist )
164 libvlc_event_detach( libvlc_media_list_event_manager( p_mlp->p_mlist, NULL ),
165 libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL );
168 /**************************************************************************
169 * install_media_player_observer (private)
170 **************************************************************************/
172 install_media_player_observer( libvlc_media_list_player_t * p_mlp )
174 libvlc_event_attach( libvlc_media_player_event_manager( p_mlp->p_mi, NULL ),
175 libvlc_MediaPlayerEndReached,
176 media_player_reached_end, p_mlp, NULL );
180 /**************************************************************************
181 * uninstall_media_player_observer (private)
182 **************************************************************************/
184 uninstall_media_player_observer( libvlc_media_list_player_t * p_mlp )
191 libvlc_event_detach( libvlc_media_player_event_manager( p_mlp->p_mi, NULL ),
192 libvlc_MediaPlayerEndReached,
193 media_player_reached_end, p_mlp, NULL );
196 /**************************************************************************
197 * set_current_playing_item (private)
199 * Playlist lock should be held
200 **************************************************************************/
202 set_current_playing_item( libvlc_media_list_player_t * p_mlp,
203 libvlc_media_list_path_t path,
204 libvlc_exception_t * p_e )
208 libvlc_media_t * p_md;
210 p_md = libvlc_media_list_item_at_path( p_mlp->p_mlist, path );
211 vlc_mutex_lock( &p_mlp->object_lock );
213 if( p_mlp->current_playing_item_path != path )
215 free( p_mlp->current_playing_item_path );
216 p_mlp->current_playing_item_path = path;
221 vlc_mutex_unlock( &p_mlp->object_lock );
225 /* We are not interested in getting media stop event now */
226 uninstall_media_player_observer( p_mlp );
230 p_mlp->p_mi = libvlc_media_player_new_from_media(p_md, p_e);
233 if( p_md->p_subitems && libvlc_media_list_count( p_md->p_subitems, NULL ) > 0 )
235 libvlc_media_t * p_submd;
236 p_submd = libvlc_media_list_item_at_index( p_md->p_subitems, 0, NULL );
237 libvlc_media_player_set_media( p_mlp->p_mi, p_submd, NULL );
238 libvlc_media_release( p_submd );
241 libvlc_media_player_set_media( p_mlp->p_mi, p_md, NULL );
242 // wait_playing_state(); /* If we want to be synchronous */
243 install_media_player_observer( p_mlp );
245 vlc_mutex_unlock( &p_mlp->object_lock );
247 libvlc_media_release( p_md ); /* for libvlc_media_list_item_at_index */
251 * Public libvlc functions
254 /**************************************************************************
256 **************************************************************************/
257 libvlc_media_list_player_t *
258 libvlc_media_list_player_new( libvlc_instance_t * p_instance,
259 libvlc_exception_t * p_e )
262 libvlc_media_list_player_t * p_mlp;
263 p_mlp = malloc(sizeof(libvlc_media_list_player_t));
267 p_mlp->current_playing_item_path = NULL;
269 p_mlp->p_mlist = NULL;
270 vlc_mutex_init( &p_mlp->object_lock );
271 p_mlp->p_event_manager = libvlc_event_manager_new( p_mlp,
274 libvlc_event_manager_register_event_type( p_mlp->p_event_manager,
275 libvlc_MediaListPlayerNextItemSet, p_e );
280 /**************************************************************************
282 **************************************************************************/
283 void libvlc_media_list_player_release( libvlc_media_list_player_t * p_mlp )
288 vlc_mutex_lock( &p_mlp->object_lock );
291 if( p_mlp->i_refcount > 0 )
293 vlc_mutex_unlock( &p_mlp->object_lock );
296 vlc_mutex_unlock( &p_mlp->object_lock );
297 vlc_mutex_destroy( &p_mlp->object_lock );
299 libvlc_event_manager_release( p_mlp->p_event_manager );
300 libvlc_media_player_release( p_mlp->p_mi );
304 uninstall_playlist_observer( p_mlp );
305 libvlc_media_list_release( p_mlp->p_mlist );
308 free( p_mlp->current_playing_item_path );
312 /**************************************************************************
313 * set_media_player (Public)
314 **************************************************************************/
315 void libvlc_media_list_player_set_media_player(
316 libvlc_media_list_player_t * p_mlp,
317 libvlc_media_player_t * p_mi,
318 libvlc_exception_t * p_e )
322 vlc_mutex_lock( &p_mlp->object_lock );
326 uninstall_media_player_observer( p_mlp );
327 libvlc_media_player_release( p_mlp->p_mi );
329 libvlc_media_player_retain( p_mi );
332 install_media_player_observer( p_mlp );
334 vlc_mutex_unlock( &p_mlp->object_lock );
337 /**************************************************************************
338 * set_media_list (Public)
339 **************************************************************************/
340 void libvlc_media_list_player_set_media_list(
341 libvlc_media_list_player_t * p_mlp,
342 libvlc_media_list_t * p_mlist,
343 libvlc_exception_t * p_e )
345 vlc_mutex_lock( &p_mlp->object_lock );
347 if( libvlc_media_list_player_is_playing( p_mlp, p_e ) )
349 libvlc_media_player_stop( p_mlp->p_mi, p_e );
350 /* Don't bother if there was an error. */
351 libvlc_exception_clear( p_e );
356 uninstall_playlist_observer( p_mlp );
357 libvlc_media_list_release( p_mlp->p_mlist );
359 libvlc_media_list_retain( p_mlist );
360 p_mlp->p_mlist = p_mlist;
362 install_playlist_observer( p_mlp );
364 vlc_mutex_unlock( &p_mlp->object_lock );
367 /**************************************************************************
369 **************************************************************************/
370 void libvlc_media_list_player_play( libvlc_media_list_player_t * p_mlp,
371 libvlc_exception_t * p_e )
373 if( !p_mlp->current_playing_item_path )
375 libvlc_media_list_player_next( p_mlp, p_e );
376 return; /* Will set to play */
379 libvlc_media_player_play( p_mlp->p_mi, p_e );
383 /**************************************************************************
385 **************************************************************************/
386 void libvlc_media_list_player_pause( libvlc_media_list_player_t * p_mlp,
387 libvlc_exception_t * p_e )
391 libvlc_media_player_pause( p_mlp->p_mi, p_e );
394 /**************************************************************************
395 * is_playing (Public)
396 **************************************************************************/
398 libvlc_media_list_player_is_playing( libvlc_media_list_player_t * p_mlp,
399 libvlc_exception_t * p_e )
401 libvlc_state_t state = libvlc_media_player_get_state( p_mlp->p_mi, p_e );
402 return (state == libvlc_Opening) || (state == libvlc_Buffering) ||
403 (state == libvlc_Playing);
406 /**************************************************************************
408 **************************************************************************/
410 libvlc_media_list_player_get_state( libvlc_media_list_player_t * p_mlp,
411 libvlc_exception_t * p_e )
415 return libvlc_media_player_get_state( p_mlp->p_mi, p_e );
418 /**************************************************************************
419 * Play item at index (Public)
420 **************************************************************************/
421 void libvlc_media_list_player_play_item_at_index(
422 libvlc_media_list_player_t * p_mlp,
424 libvlc_exception_t * p_e )
426 set_current_playing_item( p_mlp, libvlc_media_list_path_with_root_index(i_index), p_e );
428 if( libvlc_exception_raised( p_e ) )
431 /* Send the next item event */
432 libvlc_event_t event;
433 event.type = libvlc_MediaListPlayerNextItemSet;
434 libvlc_event_send( p_mlp->p_event_manager, &event );
436 libvlc_media_player_play( p_mlp->p_mi, p_e );
439 /**************************************************************************
441 **************************************************************************/
442 void libvlc_media_list_player_play_item(
443 libvlc_media_list_player_t * p_mlp,
444 libvlc_media_t * p_md,
445 libvlc_exception_t * p_e )
447 libvlc_media_list_path_t path = libvlc_media_list_path_of_item( p_mlp->p_mlist, p_md );
450 libvlc_exception_raise( p_e, "No such item in media list" );
453 set_current_playing_item( p_mlp, path, p_e );
455 if( libvlc_exception_raised( p_e ) )
458 libvlc_media_player_play( p_mlp->p_mi, p_e );
461 /**************************************************************************
463 **************************************************************************/
464 void libvlc_media_list_player_stop( libvlc_media_list_player_t * p_mlp,
465 libvlc_exception_t * p_e )
469 /* We are not interested in getting media stop event now */
470 uninstall_media_player_observer( p_mlp );
471 libvlc_media_player_stop( p_mlp->p_mi, p_e );
472 install_media_player_observer( p_mlp );
475 vlc_mutex_lock( &p_mlp->object_lock );
476 free( p_mlp->current_playing_item_path );
477 p_mlp->current_playing_item_path = NULL;
478 vlc_mutex_unlock( &p_mlp->object_lock );
481 /**************************************************************************
483 **************************************************************************/
484 void libvlc_media_list_player_next( libvlc_media_list_player_t * p_mlp,
485 libvlc_exception_t * p_e )
487 libvlc_media_list_path_t path;
489 if (! p_mlp->p_mlist )
491 libvlc_exception_raise( p_e, "No more element to play" );
495 libvlc_media_list_lock( p_mlp->p_mlist );
497 path = get_next_path( p_mlp );
501 libvlc_media_list_unlock( p_mlp->p_mlist );
502 libvlc_exception_raise( p_e, "No more element to play" );
503 libvlc_media_list_player_stop( p_mlp, p_e );
507 set_current_playing_item( p_mlp, path, p_e );
509 libvlc_media_player_play( p_mlp->p_mi, p_e );
511 libvlc_media_list_unlock( p_mlp->p_mlist );
513 /* Send the next item event */
514 libvlc_event_t event;
515 event.type = libvlc_MediaListPlayerNextItemSet;
516 libvlc_event_send( p_mlp->p_event_manager, &event);