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>
30 /**************************************************************************
31 * get_next_index (private)
33 * Simple next item fetcher.
34 **************************************************************************/
35 static int get_next_index( libvlc_media_list_player_t * p_mlp )
37 /* We are entered with libvlc_media_list_lock( p_mlp->p_list ) */
39 int next = p_mlp->i_current_playing_index + 1;
41 if( libvlc_media_list_count( p_mlp->p_mlist, NULL ) >= next )
42 return -1; /* no more to play */
47 /**************************************************************************
48 * media_instance_reached_end (private) (Event Callback)
49 **************************************************************************/
51 media_instance_reached_end( const libvlc_event_t * p_event,
54 libvlc_media_list_player_t * p_mlp = p_user_data;
55 libvlc_media_instance_t * p_mi = p_event->p_obj;
56 libvlc_media_descriptor_t *p_md, * p_current_md;
57 p_md = libvlc_media_instance_get_media_descriptor( p_mi, NULL );
58 /* XXX: need if p_mlp->p_current_playing_index is beyond */
59 p_current_md = libvlc_media_list_item_at_index(
61 p_mlp->i_current_playing_index,
63 if( p_md != p_current_md )
65 msg_Warn( p_mlp->p_libvlc_instance->p_libvlc_int,
66 "We are not sync-ed with the media instance" );
67 libvlc_media_descriptor_release( p_md );
68 libvlc_media_descriptor_release( p_current_md );
71 libvlc_media_descriptor_release( p_md );
72 libvlc_media_descriptor_release( p_current_md );
73 libvlc_media_list_player_next( p_mlp, NULL );
76 /**************************************************************************
77 * playlist_item_deleted (private) (Event Callback)
78 **************************************************************************/
80 mlist_item_deleted( const libvlc_event_t * p_event, void * p_user_data )
82 libvlc_media_descriptor_t * p_current_md;
83 libvlc_media_list_player_t * p_mlp = p_user_data;
84 libvlc_media_list_t * p_emitting_mlist = p_event->p_obj;
85 /* XXX: need if p_mlp->p_current_playing_index is beyond */
86 p_current_md = libvlc_media_list_item_at_index(
88 p_mlp->i_current_playing_index,
91 if( p_event->u.media_list_item_deleted.item == p_current_md &&
92 p_emitting_mlist == p_mlp->p_mlist )
94 /* We are playing this item, we choose to stop */
95 libvlc_media_list_player_stop( p_mlp, NULL );
99 /**************************************************************************
100 * install_playlist_observer (private)
101 **************************************************************************/
103 install_playlist_observer( libvlc_media_list_player_t * p_mlp )
105 libvlc_event_attach( libvlc_media_list_event_manager( p_mlp->p_mlist, NULL ),
106 libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL );
109 /**************************************************************************
110 * uninstall_playlist_observer (private)
111 **************************************************************************/
113 uninstall_playlist_observer( libvlc_media_list_player_t * p_mlp )
115 libvlc_event_detach( libvlc_media_list_event_manager( p_mlp->p_mlist, NULL ),
116 libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL );
119 /**************************************************************************
120 * install_media_instance_observer (private)
121 **************************************************************************/
123 install_media_instance_observer( libvlc_media_list_player_t * p_mlp )
125 libvlc_event_attach( libvlc_media_instance_event_manager( p_mlp->p_mi, NULL ),
126 libvlc_MediaInstanceReachedEnd,
127 media_instance_reached_end, p_mlp, NULL );
131 /**************************************************************************
132 * uninstall_media_instance_observer (private)
133 **************************************************************************/
135 uninstall_media_instance_observer( libvlc_media_list_player_t * p_mlp )
137 libvlc_event_detach( libvlc_media_instance_event_manager( p_mlp->p_mi, NULL ),
138 libvlc_MediaInstanceReachedEnd,
139 media_instance_reached_end, p_mlp, NULL );
141 /**************************************************************************
143 **************************************************************************/
145 libvlc_media_list_player_is_playing( libvlc_media_list_player_t * p_mlp,
146 libvlc_exception_t * p_e )
148 libvlc_exception_raise( p_e, "Unimplemented" );
152 /**************************************************************************
155 * Playlist lock should be held
156 **************************************************************************/
158 media_list_player_set_next( libvlc_media_list_player_t * p_mlp, int index,
159 libvlc_exception_t * p_e )
161 libvlc_media_descriptor_t * p_md;
163 p_md = libvlc_media_list_item_at_index( p_mlp->p_mlist, index, p_e );
166 libvlc_media_list_unlock( p_mlp->p_mlist );
167 if( !libvlc_exception_raised( p_e ) )
168 libvlc_exception_raise( p_e, "Can't obtain a media" );
172 vlc_mutex_lock( &p_mlp->object_lock );
174 p_mlp->i_current_playing_index = index;
176 /* We are not interested in getting media_descriptor stop event now */
177 uninstall_media_instance_observer( p_mlp );
178 libvlc_media_instance_set_media_descriptor( p_mlp->p_mi, p_md, NULL );
179 // wait_playing_state(); /* If we want to be synchronous */
180 install_media_instance_observer( p_mlp );
182 vlc_mutex_unlock( &p_mlp->object_lock );
184 libvlc_media_list_unlock( p_mlp->p_mlist );
186 libvlc_media_descriptor_release( p_md ); /* for libvlc_media_list_item_at_index */
190 * Public libvlc functions
193 /**************************************************************************
194 * libvlc_media_list_player_new (Public)
195 **************************************************************************/
196 libvlc_media_list_player_t *
197 libvlc_media_list_player_new( libvlc_instance_t * p_instance,
198 libvlc_exception_t * p_e )
201 libvlc_media_list_player_t * p_mlp;
202 p_mlp = malloc(sizeof(libvlc_media_list_player_t));
203 p_mlp->i_current_playing_index = -1;
205 p_mlp->p_mlist = NULL;
206 vlc_mutex_init( p_instance->p_libvlc_int, &p_mlp->object_lock );
211 /**************************************************************************
212 * libvlc_media_list_player_release (Public)
213 **************************************************************************/
214 void libvlc_media_list_player_release( libvlc_media_list_player_t * p_mlp )
219 /**************************************************************************
220 * libvlc_media_list_player_set_media_instance (Public)
221 **************************************************************************/
222 void libvlc_media_list_player_set_media_instance(
223 libvlc_media_list_player_t * p_mlp,
224 libvlc_media_instance_t * p_mi,
225 libvlc_exception_t * p_e )
227 vlc_mutex_lock( &p_mlp->object_lock );
231 uninstall_media_instance_observer( p_mlp );
232 libvlc_media_instance_release( p_mlp->p_mi );
234 libvlc_media_instance_retain( p_mi );
237 install_media_instance_observer( p_mlp );
239 vlc_mutex_unlock( &p_mlp->object_lock );
242 /**************************************************************************
243 * Set a playlist (Public)
244 **************************************************************************/
245 void libvlc_media_list_player_set_media_list(
246 libvlc_media_list_player_t * p_mlp,
247 libvlc_media_list_t * p_mlist,
248 libvlc_exception_t * p_e )
250 vlc_mutex_lock( &p_mlp->object_lock );
252 if( libvlc_media_list_player_is_playing( p_mlp, p_e ) )
253 libvlc_media_list_player_stop( p_mlp, p_e );
257 uninstall_playlist_observer( p_mlp );
258 libvlc_media_list_release( p_mlp->p_mlist );
260 libvlc_media_list_retain( p_mlist );
261 p_mlp->p_mlist = p_mlist;
263 install_playlist_observer( p_mlp );
265 vlc_mutex_unlock( &p_mlp->object_lock );
268 /**************************************************************************
270 **************************************************************************/
271 void libvlc_media_list_player_play( libvlc_media_list_player_t * p_mlp,
272 libvlc_exception_t * p_e )
274 libvlc_media_list_player_next( p_mlp, p_e );
276 if( libvlc_exception_raised( p_e ) )
279 libvlc_media_instance_play( p_mlp->p_mi, p_e );
282 /**************************************************************************
283 * Play item at index (Public)
285 * Playlist lock should be help
286 **************************************************************************/
287 void libvlc_media_list_player_play_item_at_index(
288 libvlc_media_list_player_t * p_mlp,
290 libvlc_exception_t * p_e )
292 media_list_player_set_next( p_mlp, i_index, p_e );
294 if( libvlc_exception_raised( p_e ) )
297 libvlc_media_instance_play( p_mlp->p_mi, p_e );
301 /**************************************************************************
303 **************************************************************************/
304 void libvlc_media_list_player_stop( libvlc_media_list_player_t * p_mlp,
305 libvlc_exception_t * p_e )
307 libvlc_media_instance_stop( p_mlp->p_mi, p_e );
309 vlc_mutex_lock( &p_mlp->object_lock );
310 p_mlp->i_current_playing_index = -1;
311 vlc_mutex_unlock( &p_mlp->object_lock );
314 /**************************************************************************
316 **************************************************************************/
317 void libvlc_media_list_player_next( libvlc_media_list_player_t * p_mlp,
318 libvlc_exception_t * p_e )
322 libvlc_media_list_lock( p_mlp->p_mlist );
324 index = get_next_index( p_mlp );
328 libvlc_media_list_unlock( p_mlp->p_mlist );
329 libvlc_exception_raise( p_e, "No more element to play" );
330 libvlc_media_list_player_stop( p_mlp, p_e );
334 media_list_player_set_next( p_mlp, index, p_e );
336 libvlc_media_list_unlock( p_mlp->p_mlist );