]> git.sesse.net Git - vlc/blob - src/control/media_list_player.c
Libvlc: Start the implementation of the libvlc playlist. Still in progress.
[vlc] / src / control / media_list_player.c
1 /*****************************************************************************
2  * media_list_player.c: libvlc new API media_list player functions
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
8  *
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.
13  *
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.
18  *
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
26 /*
27  * Private functions
28  */
29
30 /**************************************************************************
31  *       get_next_index (private)
32  *
33  * Simple next item fetcher.
34  **************************************************************************/
35 static int get_next_index( libvlc_media_list_player_t * p_mlp )
36 {
37         /* We are entered with libvlc_media_list_lock( p_mlp->p_list ) */
38         
39         int next = p_mlp->i_current_playing_index + 1;
40
41         if( libvlc_media_list_count( p_mlp->p_mlist, NULL ) >= next )
42                 return -1; /* no more to play */
43
44         return next;
45 }
46
47 /**************************************************************************
48  *       media_instance_reached_end (private) (Event Callback)
49  **************************************************************************/
50 static void 
51 media_instance_reached_end( const libvlc_event_t * p_event,
52                             void * p_user_data )
53 {
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(
60                         p_mlp->p_mlist,
61                         p_mlp->i_current_playing_index,
62                         NULL );
63         if( p_md != p_current_md )
64         {
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 );
69                 return;
70         }
71     libvlc_media_descriptor_release( p_md );
72     libvlc_media_descriptor_release( p_current_md );
73         libvlc_media_list_player_next( p_mlp, NULL );
74 }
75
76 /**************************************************************************
77  *       playlist_item_deleted (private) (Event Callback)
78  **************************************************************************/
79 static void 
80 mlist_item_deleted( const libvlc_event_t * p_event, void * p_user_data )
81 {
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(
87                         p_mlp->p_mlist,
88                         p_mlp->i_current_playing_index,
89                         NULL );
90
91         if( p_event->u.media_list_item_deleted.item == p_current_md &&
92             p_emitting_mlist == p_mlp->p_mlist )
93         {
94                 /* We are playing this item, we choose to stop */
95                 libvlc_media_list_player_stop( p_mlp, NULL );
96         }
97 }
98
99 /**************************************************************************
100  *       install_playlist_observer (private)
101  **************************************************************************/
102 static void
103 install_playlist_observer( libvlc_media_list_player_t * p_mlp )
104 {
105         libvlc_event_attach( libvlc_media_list_event_manager( p_mlp->p_mlist, NULL ),
106             libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL );
107 }
108
109 /**************************************************************************
110  *       uninstall_playlist_observer (private)
111  **************************************************************************/
112 static void
113 uninstall_playlist_observer( libvlc_media_list_player_t * p_mlp )
114 {
115     libvlc_event_detach( libvlc_media_list_event_manager( p_mlp->p_mlist, NULL ),
116             libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL );
117 }
118
119 /**************************************************************************
120  *       install_media_instance_observer (private)
121  **************************************************************************/
122 static void
123 install_media_instance_observer( libvlc_media_list_player_t * p_mlp )
124 {
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 );
128 }
129
130
131 /**************************************************************************
132  *       uninstall_media_instance_observer (private)
133  **************************************************************************/
134 static void
135 uninstall_media_instance_observer( libvlc_media_list_player_t * p_mlp )
136 {
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 );
140 }
141 /**************************************************************************
142  *       Stop (Public)
143  **************************************************************************/
144 static vlc_bool_t
145 libvlc_media_list_player_is_playing( libvlc_media_list_player_t * p_mlp,
146                                     libvlc_exception_t * p_e )
147 {
148     libvlc_exception_raise( p_e, "Unimplemented" );
149     return 0;
150 }
151
152
153 /*
154  * Public libvlc functions
155  */
156
157 /**************************************************************************
158  *         libvlc_media_list_player_new (Public)
159  **************************************************************************/
160 libvlc_media_list_player_t *
161 libvlc_media_list_player_new( libvlc_instance_t * p_instance,
162                               libvlc_exception_t * p_e )
163 {
164     (void)p_e;
165     libvlc_media_list_player_t * p_mlp;
166     p_mlp = malloc(sizeof(libvlc_media_list_player_t));
167         p_mlp->i_current_playing_index = -1;
168
169     vlc_mutex_init( p_instance->p_libvlc_int, &p_mlp->object_lock );
170     
171         return p_mlp;
172 }
173
174 /**************************************************************************
175  *         libvlc_media_list_player_release (Public)
176  **************************************************************************/
177 void libvlc_media_list_player_release( libvlc_media_list_player_t * p_mlp )
178 {
179     free(p_mlp);
180 }
181
182 /**************************************************************************
183  *        libvlc_media_list_player_set_media_instance (Public)
184  **************************************************************************/
185 void libvlc_media_list_player_set_media_instance(
186                                      libvlc_media_list_player_t * p_mlp,
187                                      libvlc_media_instance_t * p_mi,
188                                      libvlc_exception_t * p_e )
189 {
190     vlc_mutex_lock( &p_mlp->object_lock );
191
192         if( p_mlp->p_mi )
193         {
194                 uninstall_playlist_observer( p_mlp );
195                 libvlc_media_instance_release( p_mlp->p_mi );
196         }
197         libvlc_media_instance_retain( p_mi );
198         p_mlp->p_mi = p_mi;
199
200     install_media_instance_observer( p_mlp );
201
202     vlc_mutex_unlock( &p_mlp->object_lock );
203 }
204
205 /**************************************************************************
206  *       Set a playlist (Public)
207  **************************************************************************/
208 void libvlc_media_list_player_set_media_list(
209                                      libvlc_media_list_player_t * p_mlp,
210                                      libvlc_media_list_t * p_mlist,
211                                      libvlc_exception_t * p_e )
212 {
213     vlc_mutex_lock( &p_mlp->object_lock );
214     
215         if( libvlc_media_list_player_is_playing( p_mlp, p_e ) )
216                 libvlc_media_list_player_stop( p_mlp, p_e );
217
218         if( p_mlp->p_mlist )
219         {
220                 uninstall_playlist_observer( p_mlp );
221                 libvlc_media_list_release( p_mlp->p_mlist );
222         }
223         libvlc_media_list_retain( p_mlist );
224         p_mlp->p_mlist = p_mlist;
225     
226         install_playlist_observer( p_mlp );
227
228     vlc_mutex_unlock( &p_mlp->object_lock );
229 }
230
231 /**************************************************************************
232  *        Play (Public)
233  **************************************************************************/
234 void libvlc_media_list_player_play( libvlc_media_list_player_t * p_mlp,
235                                   libvlc_exception_t * p_e )
236 {
237         libvlc_media_list_player_next( p_mlp, p_e );
238
239         if( libvlc_exception_raised( p_e ) )
240                 return;
241
242         libvlc_media_instance_play( p_mlp->p_mi, p_e );
243 }
244
245 /**************************************************************************
246  *       Stop (Public)
247  **************************************************************************/
248 void libvlc_media_list_player_stop( libvlc_media_list_player_t * p_mlp,
249                                     libvlc_exception_t * p_e )
250 {
251         libvlc_media_instance_stop( p_mlp->p_mi, p_e );
252
253     vlc_mutex_lock( &p_mlp->object_lock );
254         p_mlp->i_current_playing_index = -1;
255     vlc_mutex_unlock( &p_mlp->object_lock );
256 }
257
258 /**************************************************************************
259  *       Next (Public)
260  **************************************************************************/
261 void libvlc_media_list_player_next( libvlc_media_list_player_t * p_mlp,
262                                     libvlc_exception_t * p_e )
263 {
264         libvlc_media_descriptor_t * p_md;
265         
266         int index;
267         
268         libvlc_media_list_lock( p_mlp->p_mlist );
269
270         index = get_next_index( p_mlp );
271
272         if( index < 0 )
273         {
274                 libvlc_media_list_unlock( p_mlp->p_mlist );
275                 libvlc_exception_raise( p_e, "No more element to play" );
276                 libvlc_media_list_player_stop( p_mlp, p_e );
277                 return;
278         }
279
280         p_md = libvlc_media_list_item_at_index( p_mlp->p_mlist, index, p_e );
281         if( !p_md )
282         {
283                 libvlc_media_list_unlock( p_mlp->p_mlist );             
284                 if( !libvlc_exception_raised( p_e ) )
285                         libvlc_exception_raise( p_e, "Can't obtain a media" );
286                 return;
287         }
288
289     vlc_mutex_lock( &p_mlp->object_lock );
290         
291     p_mlp->i_current_playing_index = index;
292
293         /* We are not interested in getting media_descriptor stop event now */
294         uninstall_media_instance_observer( p_mlp );
295     libvlc_media_instance_set_media_descriptor( p_mlp->p_mi, p_md, NULL );
296 //      wait_playing_state(); /* If we want to be synchronous */
297         install_media_instance_observer( p_mlp );
298
299     vlc_mutex_unlock( &p_mlp->object_lock );
300
301         libvlc_media_list_unlock( p_mlp->p_mlist );             
302         
303         libvlc_media_descriptor_release( p_md ); /* for libvlc_media_list_item_at_index */
304 }
305