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 *****************************************************************************/
24 #include <vlc/libvlc.h>
25 #include <vlc/libvlc_media.h>
26 #include <vlc/libvlc_media_list.h>
27 #include <vlc/libvlc_media_player.h>
28 #include <vlc/libvlc_media_list_player.h>
29 #include <vlc/libvlc_events.h>
31 #include "libvlc_internal.h"
33 #include "media_internal.h" // Abuse, could and should be removed
34 #include "media_list_path.h"
37 struct libvlc_media_list_player_t
39 libvlc_event_manager_t * p_event_manager;
40 libvlc_instance_t * p_libvlc_instance;
42 vlc_mutex_t object_lock;
43 libvlc_media_list_path_t current_playing_item_path;
44 libvlc_media_t * p_current_playing_item;
45 libvlc_media_list_t * p_mlist;
46 libvlc_media_player_t * p_mi;
53 /**************************************************************************
54 * get_next_index (private)
56 * Simple next item fetcher.
57 **************************************************************************/
58 static libvlc_media_list_path_t
59 get_next_path( libvlc_media_list_player_t * p_mlp )
61 /* We are entered with libvlc_media_list_lock( p_mlp->p_list ) */
62 libvlc_media_list_path_t ret;
63 libvlc_media_list_t * p_parent_of_playing_item;
64 libvlc_media_list_t * p_sublist_of_playing_item;
66 if ( !p_mlp->current_playing_item_path )
68 if( !libvlc_media_list_count( p_mlp->p_mlist, NULL ) )
70 return libvlc_media_list_path_with_root_index(0);
73 p_sublist_of_playing_item = libvlc_media_list_sublist_at_path(
75 p_mlp->current_playing_item_path );
77 /* If item just gained a sublist just play it */
78 if( p_sublist_of_playing_item )
80 libvlc_media_list_release( p_sublist_of_playing_item );
81 return libvlc_media_list_path_copy_by_appending( p_mlp->current_playing_item_path, 0 );
84 /* Try to catch next element */
85 p_parent_of_playing_item = libvlc_media_list_parentlist_at_path(
87 p_mlp->current_playing_item_path );
89 int depth = libvlc_media_list_path_depth( p_mlp->current_playing_item_path );
90 if( depth < 1 || !p_parent_of_playing_item )
93 ret = libvlc_media_list_path_copy( p_mlp->current_playing_item_path );
95 while( ret[depth-1] >= libvlc_media_list_count( p_parent_of_playing_item, NULL ) )
101 libvlc_media_list_release( p_parent_of_playing_item );
106 p_parent_of_playing_item = libvlc_media_list_parentlist_at_path(
110 libvlc_media_list_release( p_parent_of_playing_item );
114 /**************************************************************************
115 * media_player_reached_end (private) (Event Callback)
116 **************************************************************************/
118 media_player_reached_end( const libvlc_event_t * p_event,
121 libvlc_media_list_player_t * p_mlp = p_user_data;
122 libvlc_media_player_t * p_mi = p_event->p_obj;
123 libvlc_media_t *p_md, * p_current_md;
125 p_md = libvlc_media_player_get_media( p_mi, NULL );
126 /* XXX: need if p_mlp->p_current_playing_index is beyond */
127 p_current_md = libvlc_media_list_item_at_path(
129 p_mlp->current_playing_item_path );
130 if( p_md != p_current_md )
132 msg_Warn( p_mlp->p_libvlc_instance->p_libvlc_int,
133 "We are not sync-ed with the media instance" );
134 libvlc_media_release( p_md );
135 libvlc_media_release( p_current_md );
138 libvlc_media_release( p_md );
139 libvlc_media_release( p_current_md );
140 libvlc_media_list_player_next( p_mlp, NULL );
143 /**************************************************************************
144 * playlist_item_deleted (private) (Event Callback)
145 **************************************************************************/
147 mlist_item_deleted( const libvlc_event_t * p_event, void * p_user_data )
149 libvlc_media_t * p_current_md;
150 libvlc_media_list_player_t * p_mlp = p_user_data;
151 libvlc_media_list_t * p_emitting_mlist = p_event->p_obj;
152 /* XXX: need if p_mlp->p_current_playing_index is beyond */
153 p_current_md = libvlc_media_list_item_at_path(
155 p_mlp->current_playing_item_path );
157 if( p_event->u.media_list_item_deleted.item == p_current_md &&
158 p_emitting_mlist == p_mlp->p_mlist )
160 /* We are playing this item, we choose to stop */
161 libvlc_media_list_player_stop( p_mlp, NULL );
165 /**************************************************************************
166 * install_playlist_observer (private)
167 **************************************************************************/
169 install_playlist_observer( libvlc_media_list_player_t * p_mlp )
171 libvlc_event_attach( libvlc_media_list_event_manager( p_mlp->p_mlist, NULL ),
172 libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL );
175 /**************************************************************************
176 * uninstall_playlist_observer (private)
177 **************************************************************************/
179 uninstall_playlist_observer( libvlc_media_list_player_t * p_mlp )
181 if ( !p_mlp->p_mlist )
186 libvlc_event_detach( libvlc_media_list_event_manager( p_mlp->p_mlist, NULL ),
187 libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL );
190 /**************************************************************************
191 * install_media_player_observer (private)
192 **************************************************************************/
194 install_media_player_observer( libvlc_media_list_player_t * p_mlp )
196 libvlc_event_attach( libvlc_media_player_event_manager( p_mlp->p_mi, NULL ),
197 libvlc_MediaPlayerEndReached,
198 media_player_reached_end, p_mlp, NULL );
202 /**************************************************************************
203 * uninstall_media_player_observer (private)
204 **************************************************************************/
206 uninstall_media_player_observer( libvlc_media_list_player_t * p_mlp )
213 libvlc_event_detach( libvlc_media_player_event_manager( p_mlp->p_mi, NULL ),
214 libvlc_MediaPlayerEndReached,
215 media_player_reached_end, p_mlp, NULL );
218 /**************************************************************************
219 * set_current_playing_item (private)
221 * Playlist lock should be held
222 **************************************************************************/
224 set_current_playing_item( libvlc_media_list_player_t * p_mlp,
225 libvlc_media_list_path_t path,
226 libvlc_exception_t * p_e )
230 libvlc_media_t * p_md;
232 p_md = libvlc_media_list_item_at_path( p_mlp->p_mlist, path );
233 vlc_mutex_lock( &p_mlp->object_lock );
235 if( p_mlp->current_playing_item_path != path )
237 free( p_mlp->current_playing_item_path );
238 p_mlp->current_playing_item_path = path;
243 vlc_mutex_unlock( &p_mlp->object_lock );
247 /* We are not interested in getting media stop event now */
248 uninstall_media_player_observer( p_mlp );
252 p_mlp->p_mi = libvlc_media_player_new_from_media(p_md, p_e);
255 if( p_md->p_subitems && libvlc_media_list_count( p_md->p_subitems, NULL ) > 0 )
257 libvlc_media_t * p_submd;
258 p_submd = libvlc_media_list_item_at_index( p_md->p_subitems, 0, NULL );
259 libvlc_media_player_set_media( p_mlp->p_mi, p_submd, NULL );
260 libvlc_media_release( p_submd );
263 libvlc_media_player_set_media( p_mlp->p_mi, p_md, NULL );
264 // wait_playing_state(); /* If we want to be synchronous */
265 install_media_player_observer( p_mlp );
267 vlc_mutex_unlock( &p_mlp->object_lock );
269 libvlc_media_release( p_md ); /* for libvlc_media_list_item_at_index */
273 * Public libvlc functions
276 /**************************************************************************
278 **************************************************************************/
279 libvlc_media_list_player_t *
280 libvlc_media_list_player_new( libvlc_instance_t * p_instance,
281 libvlc_exception_t * p_e )
284 libvlc_media_list_player_t * p_mlp;
285 p_mlp = malloc(sizeof(libvlc_media_list_player_t));
289 libvlc_retain( p_instance );
290 p_mlp->p_libvlc_instance = p_instance;
291 p_mlp->i_refcount = 0;
292 vlc_mutex_init( &p_mlp->object_lock );
293 p_mlp->current_playing_item_path = NULL;
294 p_mlp->p_mlist = NULL;
296 p_mlp->p_event_manager = libvlc_event_manager_new( p_mlp,
299 libvlc_event_manager_register_event_type( p_mlp->p_event_manager,
300 libvlc_MediaListPlayerNextItemSet, p_e );
305 /**************************************************************************
307 **************************************************************************/
308 void libvlc_media_list_player_release( libvlc_media_list_player_t * p_mlp )
313 vlc_mutex_lock( &p_mlp->object_lock );
316 if( p_mlp->i_refcount > 0 )
318 vlc_mutex_unlock( &p_mlp->object_lock );
321 vlc_mutex_unlock( &p_mlp->object_lock );
322 vlc_mutex_destroy( &p_mlp->object_lock );
324 libvlc_event_manager_release( p_mlp->p_event_manager );
325 libvlc_media_player_release( p_mlp->p_mi );
329 uninstall_playlist_observer( p_mlp );
330 libvlc_media_list_release( p_mlp->p_mlist );
333 free( p_mlp->current_playing_item_path );
334 libvlc_release( p_mlp->p_libvlc_instance );
338 /**************************************************************************
339 * set_media_player (Public)
340 **************************************************************************/
341 void libvlc_media_list_player_set_media_player(
342 libvlc_media_list_player_t * p_mlp,
343 libvlc_media_player_t * p_mi,
344 libvlc_exception_t * p_e )
348 vlc_mutex_lock( &p_mlp->object_lock );
352 uninstall_media_player_observer( p_mlp );
353 libvlc_media_player_release( p_mlp->p_mi );
355 libvlc_media_player_retain( p_mi );
358 install_media_player_observer( p_mlp );
360 vlc_mutex_unlock( &p_mlp->object_lock );
363 /**************************************************************************
364 * set_media_list (Public)
365 **************************************************************************/
366 void libvlc_media_list_player_set_media_list(
367 libvlc_media_list_player_t * p_mlp,
368 libvlc_media_list_t * p_mlist,
369 libvlc_exception_t * p_e )
371 vlc_mutex_lock( &p_mlp->object_lock );
373 if( libvlc_media_list_player_is_playing( p_mlp, p_e ) )
375 libvlc_media_player_stop( p_mlp->p_mi, p_e );
376 /* Don't bother if there was an error. */
377 libvlc_exception_clear( p_e );
382 uninstall_playlist_observer( p_mlp );
383 libvlc_media_list_release( p_mlp->p_mlist );
385 libvlc_media_list_retain( p_mlist );
386 p_mlp->p_mlist = p_mlist;
388 install_playlist_observer( p_mlp );
390 vlc_mutex_unlock( &p_mlp->object_lock );
393 /**************************************************************************
395 **************************************************************************/
396 void libvlc_media_list_player_play( libvlc_media_list_player_t * p_mlp,
397 libvlc_exception_t * p_e )
399 if( !p_mlp->current_playing_item_path )
401 libvlc_media_list_player_next( p_mlp, p_e );
402 return; /* Will set to play */
405 libvlc_media_player_play( p_mlp->p_mi, p_e );
409 /**************************************************************************
411 **************************************************************************/
412 void libvlc_media_list_player_pause( libvlc_media_list_player_t * p_mlp,
413 libvlc_exception_t * p_e )
417 libvlc_media_player_pause( p_mlp->p_mi, p_e );
420 /**************************************************************************
421 * is_playing (Public)
422 **************************************************************************/
424 libvlc_media_list_player_is_playing( libvlc_media_list_player_t * p_mlp,
425 libvlc_exception_t * p_e )
427 libvlc_state_t state = libvlc_media_player_get_state( p_mlp->p_mi, p_e );
428 return (state == libvlc_Opening) || (state == libvlc_Buffering) ||
429 (state == libvlc_Playing);
432 /**************************************************************************
434 **************************************************************************/
436 libvlc_media_list_player_get_state( libvlc_media_list_player_t * p_mlp,
437 libvlc_exception_t * p_e )
441 return libvlc_media_player_get_state( p_mlp->p_mi, p_e );
444 /**************************************************************************
445 * Play item at index (Public)
446 **************************************************************************/
447 void libvlc_media_list_player_play_item_at_index(
448 libvlc_media_list_player_t * p_mlp,
450 libvlc_exception_t * p_e )
452 set_current_playing_item( p_mlp, libvlc_media_list_path_with_root_index(i_index), p_e );
454 if( libvlc_exception_raised( p_e ) )
457 /* Send the next item event */
458 libvlc_event_t event;
459 event.type = libvlc_MediaListPlayerNextItemSet;
460 libvlc_event_send( p_mlp->p_event_manager, &event );
462 libvlc_media_player_play( p_mlp->p_mi, p_e );
465 /**************************************************************************
467 **************************************************************************/
468 void libvlc_media_list_player_play_item(
469 libvlc_media_list_player_t * p_mlp,
470 libvlc_media_t * p_md,
471 libvlc_exception_t * p_e )
473 libvlc_media_list_path_t path = libvlc_media_list_path_of_item( p_mlp->p_mlist, p_md );
476 libvlc_exception_raise( p_e, "No such item in media list" );
479 set_current_playing_item( p_mlp, path, p_e );
481 if( libvlc_exception_raised( p_e ) )
484 libvlc_media_player_play( p_mlp->p_mi, p_e );
487 /**************************************************************************
489 **************************************************************************/
490 void libvlc_media_list_player_stop( libvlc_media_list_player_t * p_mlp,
491 libvlc_exception_t * p_e )
495 /* We are not interested in getting media stop event now */
496 uninstall_media_player_observer( p_mlp );
497 libvlc_media_player_stop( p_mlp->p_mi, p_e );
498 install_media_player_observer( p_mlp );
501 vlc_mutex_lock( &p_mlp->object_lock );
502 free( p_mlp->current_playing_item_path );
503 p_mlp->current_playing_item_path = NULL;
504 vlc_mutex_unlock( &p_mlp->object_lock );
507 /**************************************************************************
509 **************************************************************************/
510 void libvlc_media_list_player_next( libvlc_media_list_player_t * p_mlp,
511 libvlc_exception_t * p_e )
513 libvlc_media_list_path_t path;
515 if (! p_mlp->p_mlist )
517 libvlc_exception_raise( p_e, "No more element to play" );
521 libvlc_media_list_lock( p_mlp->p_mlist );
523 path = get_next_path( p_mlp );
527 libvlc_media_list_unlock( p_mlp->p_mlist );
528 libvlc_exception_raise( p_e, "No more element to play" );
529 libvlc_media_list_player_stop( p_mlp, p_e );
533 set_current_playing_item( p_mlp, path, p_e );
535 libvlc_media_player_play( p_mlp->p_mi, p_e );
537 libvlc_media_list_unlock( p_mlp->p_mlist );
539 /* Send the next item event */
540 libvlc_event_t event;
541 event.type = libvlc_MediaListPlayerNextItemSet;
542 libvlc_event_send( p_mlp->p_event_manager, &event);