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"
36 //#define DEBUG_MEDIA_LIST_PLAYER
38 struct libvlc_media_list_player_t
40 libvlc_event_manager_t * p_event_manager;
41 libvlc_instance_t * p_libvlc_instance;
43 vlc_mutex_t object_lock;
44 libvlc_media_list_path_t current_playing_item_path;
45 libvlc_media_t * p_current_playing_item;
46 libvlc_media_list_t * p_mlist;
47 libvlc_media_player_t * p_mi;
54 /**************************************************************************
55 * get_next_index (private)
57 * Simple next item fetcher.
58 **************************************************************************/
59 static libvlc_media_list_path_t
60 get_next_path( libvlc_media_list_player_t * p_mlp )
62 /* We are entered with libvlc_media_list_lock( p_mlp->p_list ) */
63 libvlc_media_list_path_t ret;
64 libvlc_media_list_t * p_parent_of_playing_item;
65 libvlc_media_list_t * p_sublist_of_playing_item;
67 if ( !p_mlp->current_playing_item_path )
69 if( !libvlc_media_list_count( p_mlp->p_mlist, NULL ) )
71 return libvlc_media_list_path_with_root_index(0);
74 p_sublist_of_playing_item = libvlc_media_list_sublist_at_path(
76 p_mlp->current_playing_item_path );
78 /* If item just gained a sublist just play it */
79 if( p_sublist_of_playing_item )
81 libvlc_media_list_release( p_sublist_of_playing_item );
82 return libvlc_media_list_path_copy_by_appending( p_mlp->current_playing_item_path, 0 );
85 /* Try to catch next element */
86 p_parent_of_playing_item = libvlc_media_list_parentlist_at_path(
88 p_mlp->current_playing_item_path );
90 int depth = libvlc_media_list_path_depth( p_mlp->current_playing_item_path );
91 if( depth < 1 || !p_parent_of_playing_item )
94 ret = libvlc_media_list_path_copy( p_mlp->current_playing_item_path );
96 ret[depth-1]++; // Play next element
98 /* If this goes beyong the end of the list */
99 while( ret[depth-1] >= libvlc_media_list_count( p_parent_of_playing_item, NULL ) )
105 libvlc_media_list_release( p_parent_of_playing_item );
110 p_parent_of_playing_item = libvlc_media_list_parentlist_at_path(
114 libvlc_media_list_release( p_parent_of_playing_item );
118 /**************************************************************************
119 * media_player_reached_end (private) (Event Callback)
120 **************************************************************************/
122 media_player_reached_end( const libvlc_event_t * p_event,
125 libvlc_media_list_player_t * p_mlp = p_user_data;
126 libvlc_media_player_t * p_mi = p_event->p_obj;
127 libvlc_media_t *p_md, * p_current_md;
129 p_md = libvlc_media_player_get_media( p_mi, NULL );
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 );
134 if( p_md != p_current_md )
136 msg_Warn( p_mlp->p_libvlc_instance->p_libvlc_int,
137 "We are not sync-ed with the media instance" );
138 libvlc_media_release( p_md );
139 libvlc_media_release( p_current_md );
142 libvlc_media_release( p_md );
143 libvlc_media_release( p_current_md );
144 libvlc_exception_t e;
145 libvlc_exception_init(&e);
146 libvlc_media_list_player_next(p_mlp, &e);
147 libvlc_exception_clear(&e); // Don't worry if there was an error
150 /**************************************************************************
151 * playlist_item_deleted (private) (Event Callback)
152 **************************************************************************/
154 mlist_item_deleted( const libvlc_event_t * p_event, void * p_user_data )
156 libvlc_media_t * p_current_md;
157 libvlc_media_list_player_t * p_mlp = p_user_data;
158 libvlc_media_list_t * p_emitting_mlist = p_event->p_obj;
159 /* XXX: need if p_mlp->p_current_playing_index is beyond */
160 p_current_md = libvlc_media_list_item_at_path(
162 p_mlp->current_playing_item_path );
164 if( p_event->u.media_list_item_deleted.item == p_current_md &&
165 p_emitting_mlist == p_mlp->p_mlist )
167 /* We are playing this item, we choose to stop */
168 libvlc_media_list_player_stop( p_mlp, NULL );
172 /**************************************************************************
173 * install_playlist_observer (private)
174 **************************************************************************/
176 install_playlist_observer( libvlc_media_list_player_t * p_mlp )
178 libvlc_event_attach( libvlc_media_list_event_manager( p_mlp->p_mlist, NULL ),
179 libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL );
182 /**************************************************************************
183 * uninstall_playlist_observer (private)
184 **************************************************************************/
186 uninstall_playlist_observer( libvlc_media_list_player_t * p_mlp )
188 if ( !p_mlp->p_mlist )
193 libvlc_event_detach( libvlc_media_list_event_manager( p_mlp->p_mlist, NULL ),
194 libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL );
197 /**************************************************************************
198 * install_media_player_observer (private)
199 **************************************************************************/
201 install_media_player_observer( libvlc_media_list_player_t * p_mlp )
203 libvlc_event_attach_async( libvlc_media_player_event_manager( p_mlp->p_mi, NULL ),
204 libvlc_MediaPlayerEndReached,
205 media_player_reached_end, p_mlp, NULL );
209 /**************************************************************************
210 * uninstall_media_player_observer (private)
211 **************************************************************************/
213 uninstall_media_player_observer( libvlc_media_list_player_t * p_mlp )
218 libvlc_event_detach( libvlc_media_player_event_manager( p_mlp->p_mi, NULL ),
219 libvlc_MediaPlayerEndReached,
220 media_player_reached_end, p_mlp, NULL );
223 /**************************************************************************
224 * set_current_playing_item (private)
226 * Playlist lock should be held
227 **************************************************************************/
229 set_current_playing_item( libvlc_media_list_player_t * p_mlp,
230 libvlc_media_list_path_t path,
231 libvlc_exception_t * p_e )
235 libvlc_media_t * p_md;
237 p_md = libvlc_media_list_item_at_path( p_mlp->p_mlist, path );
238 vlc_mutex_lock( &p_mlp->object_lock );
240 if( p_mlp->current_playing_item_path != path )
242 free( p_mlp->current_playing_item_path );
243 p_mlp->current_playing_item_path = path;
248 vlc_mutex_unlock( &p_mlp->object_lock );
252 /* We are not interested in getting media stop event now */
253 uninstall_media_player_observer( p_mlp );
257 p_mlp->p_mi = libvlc_media_player_new_from_media(p_md, p_e);
260 if( p_md->p_subitems && libvlc_media_list_count( p_md->p_subitems, NULL ) > 0 )
262 libvlc_media_t * p_submd;
263 p_submd = libvlc_media_list_item_at_index( p_md->p_subitems, 0, NULL );
264 libvlc_media_player_set_media( p_mlp->p_mi, p_submd, NULL );
265 libvlc_media_release( p_submd );
268 libvlc_media_player_set_media( p_mlp->p_mi, p_md, NULL );
269 // wait_playing_state(); /* If we want to be synchronous */
270 install_media_player_observer( p_mlp );
272 vlc_mutex_unlock( &p_mlp->object_lock );
274 libvlc_media_release( p_md ); /* for libvlc_media_list_item_at_index */
278 * Public libvlc functions
281 /**************************************************************************
283 **************************************************************************/
284 libvlc_media_list_player_t *
285 libvlc_media_list_player_new( libvlc_instance_t * p_instance,
286 libvlc_exception_t * p_e )
289 libvlc_media_list_player_t * p_mlp;
290 p_mlp = malloc(sizeof(libvlc_media_list_player_t));
294 libvlc_retain( p_instance );
295 p_mlp->p_libvlc_instance = p_instance;
296 p_mlp->i_refcount = 0;
297 vlc_mutex_init( &p_mlp->object_lock );
298 p_mlp->current_playing_item_path = NULL;
299 p_mlp->p_mlist = NULL;
301 p_mlp->p_event_manager = libvlc_event_manager_new( p_mlp,
304 libvlc_event_manager_register_event_type( p_mlp->p_event_manager,
305 libvlc_MediaListPlayerNextItemSet, p_e );
310 /**************************************************************************
312 **************************************************************************/
313 void libvlc_media_list_player_release( libvlc_media_list_player_t * p_mlp )
318 vlc_mutex_lock( &p_mlp->object_lock );
321 if( p_mlp->i_refcount > 0 )
323 vlc_mutex_unlock( &p_mlp->object_lock );
326 vlc_mutex_unlock( &p_mlp->object_lock );
327 vlc_mutex_destroy( &p_mlp->object_lock );
329 libvlc_event_manager_release( p_mlp->p_event_manager );
330 libvlc_media_player_release( p_mlp->p_mi );
334 uninstall_playlist_observer( p_mlp );
335 libvlc_media_list_release( p_mlp->p_mlist );
338 free( p_mlp->current_playing_item_path );
339 libvlc_release( p_mlp->p_libvlc_instance );
343 /**************************************************************************
344 * set_media_player (Public)
345 **************************************************************************/
346 void libvlc_media_list_player_set_media_player(
347 libvlc_media_list_player_t * p_mlp,
348 libvlc_media_player_t * p_mi,
349 libvlc_exception_t * p_e )
353 vlc_mutex_lock( &p_mlp->object_lock );
357 uninstall_media_player_observer( p_mlp );
358 libvlc_media_player_release( p_mlp->p_mi );
360 libvlc_media_player_retain( p_mi );
363 install_media_player_observer( p_mlp );
365 vlc_mutex_unlock( &p_mlp->object_lock );
368 /**************************************************************************
369 * set_media_list (Public)
370 **************************************************************************/
371 void libvlc_media_list_player_set_media_list(
372 libvlc_media_list_player_t * p_mlp,
373 libvlc_media_list_t * p_mlist,
374 libvlc_exception_t * p_e )
376 vlc_mutex_lock( &p_mlp->object_lock );
380 libvlc_exception_raise( p_e, "No media list provided");
384 if( libvlc_media_list_player_is_playing( p_mlp, p_e ) )
386 libvlc_media_player_stop( p_mlp->p_mi, p_e );
387 /* Don't bother if there was an error. */
388 libvlc_exception_clear( p_e );
393 uninstall_playlist_observer( p_mlp );
394 libvlc_media_list_release( p_mlp->p_mlist );
396 libvlc_media_list_retain( p_mlist );
397 p_mlp->p_mlist = p_mlist;
399 install_playlist_observer( p_mlp );
401 vlc_mutex_unlock( &p_mlp->object_lock );
404 /**************************************************************************
406 **************************************************************************/
407 void libvlc_media_list_player_play( libvlc_media_list_player_t * p_mlp,
408 libvlc_exception_t * p_e )
410 if( !p_mlp->current_playing_item_path )
412 libvlc_media_list_player_next( p_mlp, p_e );
413 return; /* Will set to play */
416 libvlc_media_player_play( p_mlp->p_mi, p_e );
420 /**************************************************************************
422 **************************************************************************/
423 void libvlc_media_list_player_pause( libvlc_media_list_player_t * p_mlp,
424 libvlc_exception_t * p_e )
428 libvlc_media_player_pause( p_mlp->p_mi, p_e );
431 /**************************************************************************
432 * is_playing (Public)
433 **************************************************************************/
435 libvlc_media_list_player_is_playing( libvlc_media_list_player_t * p_mlp,
436 libvlc_exception_t * p_e )
438 libvlc_state_t state = libvlc_media_player_get_state( p_mlp->p_mi, p_e );
439 return (state == libvlc_Opening) || (state == libvlc_Buffering) ||
440 (state == libvlc_Playing);
443 /**************************************************************************
445 **************************************************************************/
447 libvlc_media_list_player_get_state( libvlc_media_list_player_t * p_mlp,
448 libvlc_exception_t * p_e )
452 return libvlc_media_player_get_state( p_mlp->p_mi, p_e );
455 /**************************************************************************
456 * Play item at index (Public)
457 **************************************************************************/
458 void libvlc_media_list_player_play_item_at_index(
459 libvlc_media_list_player_t * p_mlp,
461 libvlc_exception_t * p_e )
463 set_current_playing_item( p_mlp, libvlc_media_list_path_with_root_index(i_index), p_e );
465 if( libvlc_exception_raised( p_e ) )
468 /* Send the next item event */
469 libvlc_event_t event;
470 event.type = libvlc_MediaListPlayerNextItemSet;
471 libvlc_event_send( p_mlp->p_event_manager, &event );
473 libvlc_media_player_play( p_mlp->p_mi, p_e );
476 /**************************************************************************
478 **************************************************************************/
479 void libvlc_media_list_player_play_item(
480 libvlc_media_list_player_t * p_mlp,
481 libvlc_media_t * p_md,
482 libvlc_exception_t * p_e )
484 libvlc_media_list_path_t path = libvlc_media_list_path_of_item( p_mlp->p_mlist, p_md );
487 libvlc_exception_raise( p_e, "No such item in media list" );
490 set_current_playing_item( p_mlp, path, p_e );
492 if( libvlc_exception_raised( p_e ) )
495 libvlc_media_player_play( p_mlp->p_mi, p_e );
498 /**************************************************************************
500 **************************************************************************/
501 void libvlc_media_list_player_stop( libvlc_media_list_player_t * p_mlp,
502 libvlc_exception_t * p_e )
506 /* We are not interested in getting media stop event now */
507 uninstall_media_player_observer( p_mlp );
508 libvlc_media_player_stop( p_mlp->p_mi, p_e );
509 install_media_player_observer( p_mlp );
512 vlc_mutex_lock( &p_mlp->object_lock );
513 free( p_mlp->current_playing_item_path );
514 p_mlp->current_playing_item_path = NULL;
515 vlc_mutex_unlock( &p_mlp->object_lock );
518 /**************************************************************************
520 **************************************************************************/
521 void libvlc_media_list_player_next( libvlc_media_list_player_t * p_mlp,
522 libvlc_exception_t * p_e )
524 libvlc_media_list_path_t path;
526 if (! p_mlp->p_mlist )
528 libvlc_exception_raise( p_e, "No more element to play" );
532 libvlc_media_list_lock( p_mlp->p_mlist );
534 path = get_next_path( p_mlp );
536 #ifdef DEBUG_MEDIA_LIST_PLAYER
538 libvlc_media_list_path_dump(path);
543 libvlc_media_list_unlock( p_mlp->p_mlist );
544 libvlc_exception_raise( p_e, "No more element to play" );
545 libvlc_media_list_player_stop( p_mlp, p_e );
549 set_current_playing_item( p_mlp, path, p_e );
551 libvlc_media_player_play( p_mlp->p_mi, p_e );
553 libvlc_media_list_unlock( p_mlp->p_mlist );
555 /* Send the next item event */
556 libvlc_event_t event;
557 event.type = libvlc_MediaListPlayerNextItemSet;
558 libvlc_event_send( p_mlp->p_event_manager, &event);