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 p_mlp->current_playing_item_path = libvlc_media_list_path_empty();
49 p_sublist_of_playing_item = libvlc_media_list_sublist_at_path(
51 p_mlp->current_playing_item_path );
53 /* If item just gained a sublist just play it */
54 if( p_sublist_of_playing_item )
56 libvlc_media_list_release( p_sublist_of_playing_item );
57 return libvlc_media_list_path_copy_by_appending( p_mlp->current_playing_item_path, 0 );
60 /* Try to catch next element */
61 p_parent_of_playing_item = libvlc_media_list_parentlist_at_path(
63 p_mlp->current_playing_item_path );
65 int deepness = libvlc_media_list_path_deepness( p_mlp->current_playing_item_path );
66 if( deepness < 1 || !p_parent_of_playing_item )
69 ret = libvlc_media_list_path_copy( p_mlp->current_playing_item_path );
71 while( ret[deepness-1] >= libvlc_media_list_count( p_parent_of_playing_item, NULL ) )
77 libvlc_media_list_release( p_parent_of_playing_item );
82 p_parent_of_playing_item = libvlc_media_list_parentlist_at_path(
86 libvlc_media_list_release( p_parent_of_playing_item );
90 /**************************************************************************
91 * media_player_reached_end (private) (Event Callback)
92 **************************************************************************/
94 media_player_reached_end( const libvlc_event_t * p_event,
97 libvlc_media_list_player_t * p_mlp = p_user_data;
98 libvlc_media_player_t * p_mi = p_event->p_obj;
99 libvlc_media_t *p_md, * p_current_md;
101 p_md = libvlc_media_player_get_media( p_mi, NULL );
102 /* XXX: need if p_mlp->p_current_playing_index is beyond */
103 p_current_md = libvlc_media_list_item_at_path(
105 p_mlp->current_playing_item_path );
106 if( p_md != p_current_md )
108 msg_Warn( p_mlp->p_libvlc_instance->p_libvlc_int,
109 "We are not sync-ed with the media instance" );
110 libvlc_media_release( p_md );
111 libvlc_media_release( p_current_md );
114 libvlc_media_release( p_md );
115 libvlc_media_release( p_current_md );
116 libvlc_media_list_player_next( p_mlp, NULL );
119 /**************************************************************************
120 * playlist_item_deleted (private) (Event Callback)
121 **************************************************************************/
123 mlist_item_deleted( const libvlc_event_t * p_event, void * p_user_data )
125 libvlc_media_t * p_current_md;
126 libvlc_media_list_player_t * p_mlp = p_user_data;
127 libvlc_media_list_t * p_emitting_mlist = p_event->p_obj;
128 /* XXX: need if p_mlp->p_current_playing_index is beyond */
129 p_current_md = libvlc_media_list_item_at_path(
131 p_mlp->current_playing_item_path );
133 if( p_event->u.media_list_item_deleted.item == p_current_md &&
134 p_emitting_mlist == p_mlp->p_mlist )
136 /* We are playing this item, we choose to stop */
137 libvlc_media_list_player_stop( p_mlp, NULL );
141 /**************************************************************************
142 * install_playlist_observer (private)
143 **************************************************************************/
145 install_playlist_observer( libvlc_media_list_player_t * p_mlp )
147 libvlc_event_attach( libvlc_media_list_event_manager( p_mlp->p_mlist, NULL ),
148 libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL );
151 /**************************************************************************
152 * uninstall_playlist_observer (private)
153 **************************************************************************/
155 uninstall_playlist_observer( libvlc_media_list_player_t * p_mlp )
157 if ( !p_mlp->p_mlist )
162 libvlc_event_detach( libvlc_media_list_event_manager( p_mlp->p_mlist, NULL ),
163 libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL );
166 /**************************************************************************
167 * install_media_player_observer (private)
168 **************************************************************************/
170 install_media_player_observer( libvlc_media_list_player_t * p_mlp )
172 libvlc_event_attach( libvlc_media_player_event_manager( p_mlp->p_mi, NULL ),
173 libvlc_MediaPlayerEndReached,
174 media_player_reached_end, p_mlp, NULL );
178 /**************************************************************************
179 * uninstall_media_player_observer (private)
180 **************************************************************************/
182 uninstall_media_player_observer( libvlc_media_list_player_t * p_mlp )
189 libvlc_event_detach( libvlc_media_player_event_manager( p_mlp->p_mi, NULL ),
190 libvlc_MediaPlayerEndReached,
191 media_player_reached_end, p_mlp, NULL );
194 /**************************************************************************
195 * set_current_playing_item (private)
197 * Playlist lock should be held
198 **************************************************************************/
200 set_current_playing_item( libvlc_media_list_player_t * p_mlp,
201 libvlc_media_list_path_t path,
202 libvlc_exception_t * p_e )
206 libvlc_media_t * p_md;
208 p_md = libvlc_media_list_item_at_path( p_mlp->p_mlist, path );
209 vlc_mutex_lock( &p_mlp->object_lock );
211 free( p_mlp->current_playing_item_path );
212 p_mlp->current_playing_item_path = path;
216 vlc_mutex_unlock( &p_mlp->object_lock );
220 /* We are not interested in getting media stop event now */
221 uninstall_media_player_observer( p_mlp );
225 p_mlp->p_mi = libvlc_media_player_new_from_media(p_md, p_e);
228 if( p_md->p_subitems && libvlc_media_list_count( p_md->p_subitems, NULL ) > 0 )
230 libvlc_media_t * p_submd;
231 p_submd = libvlc_media_list_item_at_index( p_md->p_subitems, 0, NULL );
232 libvlc_media_player_set_media( p_mlp->p_mi, p_submd, NULL );
233 libvlc_media_release( p_submd );
236 libvlc_media_player_set_media( p_mlp->p_mi, p_md, NULL );
237 // wait_playing_state(); /* If we want to be synchronous */
238 install_media_player_observer( p_mlp );
240 vlc_mutex_unlock( &p_mlp->object_lock );
242 libvlc_media_release( p_md ); /* for libvlc_media_list_item_at_index */
246 * Public libvlc functions
249 /**************************************************************************
251 **************************************************************************/
252 libvlc_media_list_player_t *
253 libvlc_media_list_player_new( libvlc_instance_t * p_instance,
254 libvlc_exception_t * p_e )
257 libvlc_media_list_player_t * p_mlp;
258 p_mlp = malloc(sizeof(libvlc_media_list_player_t));
259 p_mlp->current_playing_item_path = NULL;
261 p_mlp->p_mlist = NULL;
262 vlc_mutex_init( &p_mlp->object_lock );
263 p_mlp->p_event_manager = libvlc_event_manager_new( p_mlp,
266 libvlc_event_manager_register_event_type( p_mlp->p_event_manager,
267 libvlc_MediaListPlayerNextItemSet, p_e );
272 /**************************************************************************
274 **************************************************************************/
275 void libvlc_media_list_player_release( libvlc_media_list_player_t * p_mlp )
280 /**************************************************************************
281 * set_media_player (Public)
282 **************************************************************************/
283 void libvlc_media_list_player_set_media_player(
284 libvlc_media_list_player_t * p_mlp,
285 libvlc_media_player_t * p_mi,
286 libvlc_exception_t * p_e )
290 vlc_mutex_lock( &p_mlp->object_lock );
294 uninstall_media_player_observer( p_mlp );
295 libvlc_media_player_release( p_mlp->p_mi );
297 libvlc_media_player_retain( p_mi );
300 install_media_player_observer( p_mlp );
302 vlc_mutex_unlock( &p_mlp->object_lock );
305 /**************************************************************************
306 * set_media_list (Public)
307 **************************************************************************/
308 void libvlc_media_list_player_set_media_list(
309 libvlc_media_list_player_t * p_mlp,
310 libvlc_media_list_t * p_mlist,
311 libvlc_exception_t * p_e )
313 vlc_mutex_lock( &p_mlp->object_lock );
315 if( libvlc_media_list_player_is_playing( p_mlp, p_e ) )
317 libvlc_media_player_stop( p_mlp->p_mi, p_e );
318 /* Don't bother if there was an error. */
319 libvlc_exception_clear( p_e );
324 uninstall_playlist_observer( p_mlp );
325 libvlc_media_list_release( p_mlp->p_mlist );
327 libvlc_media_list_retain( p_mlist );
328 p_mlp->p_mlist = p_mlist;
330 install_playlist_observer( p_mlp );
332 vlc_mutex_unlock( &p_mlp->object_lock );
335 /**************************************************************************
337 **************************************************************************/
338 void libvlc_media_list_player_play( libvlc_media_list_player_t * p_mlp,
339 libvlc_exception_t * p_e )
341 if( !p_mlp->current_playing_item_path )
343 libvlc_media_list_player_next( p_mlp, p_e );
344 return; /* Will set to play */
347 libvlc_media_player_play( p_mlp->p_mi, p_e );
351 /**************************************************************************
353 **************************************************************************/
354 void libvlc_media_list_player_pause( libvlc_media_list_player_t * p_mlp,
355 libvlc_exception_t * p_e )
359 libvlc_media_player_pause( p_mlp->p_mi, p_e );
362 /**************************************************************************
363 * is_playing (Public)
364 **************************************************************************/
366 libvlc_media_list_player_is_playing( libvlc_media_list_player_t * p_mlp,
367 libvlc_exception_t * p_e )
369 libvlc_state_t state = libvlc_media_player_get_state( p_mlp->p_mi, p_e );
370 return (state == libvlc_Opening) || (state == libvlc_Buffering) ||
371 (state == libvlc_Forward) || (state == libvlc_Backward) ||
372 (state == libvlc_Playing);
375 /**************************************************************************
377 **************************************************************************/
379 libvlc_media_list_player_get_state( libvlc_media_list_player_t * p_mlp,
380 libvlc_exception_t * p_e )
384 return libvlc_media_player_get_state( p_mlp->p_mi, p_e );
387 /**************************************************************************
388 * Play item at index (Public)
389 **************************************************************************/
390 void libvlc_media_list_player_play_item_at_index(
391 libvlc_media_list_player_t * p_mlp,
393 libvlc_exception_t * p_e )
395 set_current_playing_item( p_mlp, libvlc_media_list_path_with_root_index(i_index), p_e );
397 if( libvlc_exception_raised( p_e ) )
400 /* Send the next item event */
401 libvlc_event_t event;
402 event.type = libvlc_MediaListPlayerNextItemSet;
403 libvlc_event_send( p_mlp->p_event_manager, &event );
405 libvlc_media_player_play( p_mlp->p_mi, p_e );
408 /**************************************************************************
410 **************************************************************************/
411 void libvlc_media_list_player_play_item(
412 libvlc_media_list_player_t * p_mlp,
413 libvlc_media_t * p_md,
414 libvlc_exception_t * p_e )
416 libvlc_media_list_path_t path = libvlc_media_list_path_of_item( p_mlp->p_mlist, p_md );
419 libvlc_exception_raise( p_e, "No such item in media list" );
422 set_current_playing_item( p_mlp, path, p_e );
424 if( libvlc_exception_raised( p_e ) )
427 libvlc_media_player_play( p_mlp->p_mi, p_e );
430 /**************************************************************************
432 **************************************************************************/
433 void libvlc_media_list_player_stop( libvlc_media_list_player_t * p_mlp,
434 libvlc_exception_t * p_e )
438 /* We are not interested in getting media stop event now */
439 uninstall_media_player_observer( p_mlp );
440 libvlc_media_player_stop( p_mlp->p_mi, p_e );
441 install_media_player_observer( p_mlp );
444 vlc_mutex_lock( &p_mlp->object_lock );
445 free( p_mlp->current_playing_item_path );
446 p_mlp->current_playing_item_path = NULL;
447 vlc_mutex_unlock( &p_mlp->object_lock );
450 /**************************************************************************
452 **************************************************************************/
453 void libvlc_media_list_player_next( libvlc_media_list_player_t * p_mlp,
454 libvlc_exception_t * p_e )
456 libvlc_media_list_path_t path;
458 if (! p_mlp->p_mlist )
460 libvlc_exception_raise( p_e, "No more element to play" );
464 libvlc_media_list_lock( p_mlp->p_mlist );
466 path = get_next_path( p_mlp );
470 libvlc_media_list_unlock( p_mlp->p_mlist );
471 libvlc_exception_raise( p_e, "No more element to play" );
472 libvlc_media_list_player_stop( p_mlp, p_e );
476 set_current_playing_item( p_mlp, path, p_e );
478 libvlc_media_player_play( p_mlp->p_mi, p_e );
480 libvlc_media_list_unlock( p_mlp->p_mlist );
482 /* Send the next item event */
483 libvlc_event_t event;
484 event.type = libvlc_MediaListPlayerNextItemSet;
485 libvlc_event_send( p_mlp->p_event_manager, &event);