]> git.sesse.net Git - vlc/blob - src/control/media_list_player.c
a1239f8c295d6e9292dacdaaa0f602ec9616aa4d
[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 #include "media_list_path.h"
26
27 /*
28  * Private functions
29  */
30
31 /**************************************************************************
32  *       get_next_index (private)
33  *
34  * Simple next item fetcher.
35  **************************************************************************/
36 static libvlc_media_list_path_t
37 get_next_path( libvlc_media_list_player_t * p_mlp )
38 {
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;
43
44     p_sublist_of_playing_item = libvlc_media_list_sublist_at_path(
45                             p_mlp->p_mlist,
46                             p_mlp->current_playing_item_path );
47  
48     /* If item just gained a sublist just play it */
49     if( p_sublist_of_playing_item )
50     {
51         libvlc_media_list_release( p_sublist_of_playing_item );
52         return libvlc_media_list_path_copy_by_appending( p_mlp->current_playing_item_path, 0 );
53     }
54
55     /* Try to catch next element */
56     p_parent_of_playing_item = libvlc_media_list_parentlist_at_path(
57                             p_mlp->p_mlist,
58                             p_mlp->current_playing_item_path );
59
60     int deepness = libvlc_media_list_path_deepness( p_mlp->current_playing_item_path );
61     if( deepness < 1 || !p_parent_of_playing_item )
62         return NULL;
63
64     ret = libvlc_media_list_path_copy( p_mlp->current_playing_item_path );
65
66     while( ret[deepness-1] >= libvlc_media_list_count( p_parent_of_playing_item, NULL ) )
67     {
68         deepness--;
69         if( deepness <= 0 )
70         {
71             free( ret );
72             libvlc_media_list_release( p_parent_of_playing_item );
73             return NULL;
74         }
75         ret[deepness] = -1;
76         ret[deepness-1]++;
77         p_parent_of_playing_item  = libvlc_media_list_parentlist_at_path(
78                                         p_mlp->p_mlist,
79                                         ret );
80     }
81     libvlc_media_list_release( p_parent_of_playing_item );
82     return ret;
83 }
84
85 /**************************************************************************
86  *       media_instance_reached_end (private) (Event Callback)
87  **************************************************************************/
88 static void
89 media_instance_reached_end( const libvlc_event_t * p_event,
90                             void * p_user_data )
91 {
92     libvlc_media_list_player_t * p_mlp = p_user_data;
93     libvlc_media_instance_t * p_mi = p_event->p_obj;
94     libvlc_media_descriptor_t *p_md, * p_current_md;
95
96     p_md = libvlc_media_instance_get_media_descriptor( p_mi, NULL );
97     /* XXX: need if p_mlp->p_current_playing_index is beyond */
98     p_current_md = libvlc_media_list_item_at_path(
99                         p_mlp->p_mlist,
100                         p_mlp->current_playing_item_path );
101     if( p_md != p_current_md )
102     {
103         msg_Warn( p_mlp->p_libvlc_instance->p_libvlc_int,
104                   "We are not sync-ed with the media instance" );
105         libvlc_media_descriptor_release( p_md );
106         libvlc_media_descriptor_release( p_current_md );
107         return;
108     }
109     libvlc_media_descriptor_release( p_md );
110     libvlc_media_descriptor_release( p_current_md );
111     libvlc_media_list_player_next( p_mlp, NULL );
112 }
113
114 /**************************************************************************
115  *       playlist_item_deleted (private) (Event Callback)
116  **************************************************************************/
117 static void
118 mlist_item_deleted( const libvlc_event_t * p_event, void * p_user_data )
119 {
120     libvlc_media_descriptor_t * p_current_md;
121     libvlc_media_list_player_t * p_mlp = p_user_data;
122     libvlc_media_list_t * p_emitting_mlist = p_event->p_obj;
123     /* XXX: need if p_mlp->p_current_playing_index is beyond */
124     p_current_md = libvlc_media_list_item_at_path(
125                         p_mlp->p_mlist,
126                         p_mlp->current_playing_item_path );
127
128     if( p_event->u.media_list_item_deleted.item == p_current_md &&
129         p_emitting_mlist == p_mlp->p_mlist )
130     {
131         /* We are playing this item, we choose to stop */
132         libvlc_media_list_player_stop( p_mlp, NULL );
133     }
134 }
135
136 /**************************************************************************
137  *       install_playlist_observer (private)
138  **************************************************************************/
139 static void
140 install_playlist_observer( libvlc_media_list_player_t * p_mlp )
141 {
142     libvlc_event_attach( libvlc_media_list_event_manager( p_mlp->p_mlist, NULL ),
143             libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL );
144 }
145
146 /**************************************************************************
147  *       uninstall_playlist_observer (private)
148  **************************************************************************/
149 static void
150 uninstall_playlist_observer( libvlc_media_list_player_t * p_mlp )
151 {
152     libvlc_event_detach( libvlc_media_list_event_manager( p_mlp->p_mlist, NULL ),
153             libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL );
154 }
155
156 /**************************************************************************
157  *       install_media_instance_observer (private)
158  **************************************************************************/
159 static void
160 install_media_instance_observer( libvlc_media_list_player_t * p_mlp )
161 {
162     libvlc_event_attach( libvlc_media_instance_event_manager( p_mlp->p_mi, NULL ),
163                          libvlc_MediaInstanceReachedEnd,
164                           media_instance_reached_end, p_mlp, NULL );
165 }
166
167
168 /**************************************************************************
169  *       uninstall_media_instance_observer (private)
170  **************************************************************************/
171 static void
172 uninstall_media_instance_observer( libvlc_media_list_player_t * p_mlp )
173 {
174     libvlc_event_detach( libvlc_media_instance_event_manager( p_mlp->p_mi, NULL ),
175                          libvlc_MediaInstanceReachedEnd,
176                          media_instance_reached_end, p_mlp, NULL );
177 }
178
179 /**************************************************************************
180  *       set_current_playing_item (private)
181  *
182  * Playlist lock should be held
183  **************************************************************************/
184 static void
185 set_current_playing_item( libvlc_media_list_player_t * p_mlp,
186                           libvlc_media_list_path_t path,
187                           libvlc_exception_t * p_e )
188 {
189     VLC_UNUSED(p_e);
190
191     libvlc_media_descriptor_t * p_md;
192     
193     p_md = libvlc_media_list_item_at_path( p_mlp->p_mlist, path ); 
194     vlc_mutex_lock( &p_mlp->object_lock );
195     
196     free( p_mlp->current_playing_item_path );
197     p_mlp->current_playing_item_path = path;
198
199     if( !p_md )
200     {
201         vlc_mutex_unlock( &p_mlp->object_lock );
202         return;
203     }
204
205     /* We are not interested in getting media_descriptor stop event now */
206     uninstall_media_instance_observer( p_mlp );
207     if( p_md->p_subitems && libvlc_media_list_count( p_md->p_subitems, NULL ) > 0 )
208     {
209         libvlc_media_descriptor_t * p_submd;
210         p_submd = libvlc_media_list_item_at_index( p_md->p_subitems, 0, NULL );
211         libvlc_media_instance_set_media_descriptor( p_mlp->p_mi, p_submd, NULL );
212         libvlc_media_descriptor_release( p_submd );
213     }
214     else
215         libvlc_media_instance_set_media_descriptor( p_mlp->p_mi, p_md, NULL );
216 //    wait_playing_state(); /* If we want to be synchronous */
217     install_media_instance_observer( p_mlp );
218
219     vlc_mutex_unlock( &p_mlp->object_lock );
220
221     libvlc_media_list_unlock( p_mlp->p_mlist );        
222     
223     libvlc_media_descriptor_release( p_md ); /* for libvlc_media_list_item_at_index */
224 }
225
226 /*
227  * Public libvlc functions
228  */
229
230 /**************************************************************************
231  *         new (Public)
232  **************************************************************************/
233 libvlc_media_list_player_t *
234 libvlc_media_list_player_new( libvlc_instance_t * p_instance,
235                               libvlc_exception_t * p_e )
236 {
237     (void)p_e;
238     libvlc_media_list_player_t * p_mlp;
239     p_mlp = malloc(sizeof(libvlc_media_list_player_t));
240     p_mlp->current_playing_item_path = NULL;
241     p_mlp->p_mi = NULL;
242     p_mlp->p_mlist = NULL;
243     vlc_mutex_init( p_instance->p_libvlc_int, &p_mlp->object_lock );
244     p_mlp->p_event_manager = libvlc_event_manager_new( p_mlp,
245                                                        p_instance,
246                                                        p_e );
247     libvlc_event_manager_register_event_type( p_mlp->p_event_manager,
248             libvlc_MediaListPlayerNextItemSet, p_e );
249
250     return p_mlp;
251 }
252
253 /**************************************************************************
254  *         release (Public)
255  **************************************************************************/
256 void libvlc_media_list_player_release( libvlc_media_list_player_t * p_mlp )
257 {
258     free(p_mlp);
259 }
260
261 /**************************************************************************
262  *        set_media_instance (Public)
263  **************************************************************************/
264 void libvlc_media_list_player_set_media_instance(
265                                      libvlc_media_list_player_t * p_mlp,
266                                      libvlc_media_instance_t * p_mi,
267                                      libvlc_exception_t * p_e )
268 {
269     VLC_UNUSED(p_e);
270
271     vlc_mutex_lock( &p_mlp->object_lock );
272
273     if( p_mlp->p_mi )
274     {
275         uninstall_media_instance_observer( p_mlp );
276         libvlc_media_instance_release( p_mlp->p_mi );
277     }
278     libvlc_media_instance_retain( p_mi );
279     p_mlp->p_mi = p_mi;
280
281     install_media_instance_observer( p_mlp );
282
283     vlc_mutex_unlock( &p_mlp->object_lock );
284 }
285
286 /**************************************************************************
287  *       set_media_list (Public)
288  **************************************************************************/
289 void libvlc_media_list_player_set_media_list(
290                                      libvlc_media_list_player_t * p_mlp,
291                                      libvlc_media_list_t * p_mlist,
292                                      libvlc_exception_t * p_e )
293 {
294     vlc_mutex_lock( &p_mlp->object_lock );
295  
296     if( libvlc_media_list_player_is_playing( p_mlp, p_e ) )
297     {
298         libvlc_media_instance_stop( p_mlp->p_mi, p_e );
299         /* Don't bother if there was an error. */
300         libvlc_exception_clear( p_e );
301     }
302
303     if( p_mlp->p_mlist )
304     {
305         uninstall_playlist_observer( p_mlp );
306         libvlc_media_list_release( p_mlp->p_mlist );
307     }
308     libvlc_media_list_retain( p_mlist );
309     p_mlp->p_mlist = p_mlist;
310  
311     install_playlist_observer( p_mlp );
312
313     vlc_mutex_unlock( &p_mlp->object_lock );
314 }
315
316 /**************************************************************************
317  *        Play (Public)
318  **************************************************************************/
319 void libvlc_media_list_player_play( libvlc_media_list_player_t * p_mlp,
320                                   libvlc_exception_t * p_e )
321 {
322     if( !p_mlp->current_playing_item_path )
323     {
324         libvlc_media_list_player_next( p_mlp, p_e );
325         return; /* Will set to play */
326     }
327
328     libvlc_media_instance_play( p_mlp->p_mi, p_e );
329 }
330
331
332 /**************************************************************************
333  *        Pause (Public)
334  **************************************************************************/
335 void libvlc_media_list_player_pause( libvlc_media_list_player_t * p_mlp,
336                                      libvlc_exception_t * p_e )
337 {
338     if( !p_mlp->p_mi )
339         return;
340     libvlc_media_instance_pause( p_mlp->p_mi, p_e );
341 }
342
343 /**************************************************************************
344  *        is_playing (Public)
345  **************************************************************************/
346 int
347 libvlc_media_list_player_is_playing( libvlc_media_list_player_t * p_mlp,
348                                      libvlc_exception_t * p_e )
349 {
350     libvlc_state_t state = libvlc_media_instance_get_state( p_mlp->p_mi, p_e );
351     return (state == libvlc_Opening) || (state == libvlc_Buffering) ||
352            (state == libvlc_Playing);
353 }
354
355 /**************************************************************************
356  *        State (Public)
357  **************************************************************************/
358 libvlc_state_t
359 libvlc_media_list_player_get_state( libvlc_media_list_player_t * p_mlp,
360                                     libvlc_exception_t * p_e )
361 {
362     if( !p_mlp->p_mi )
363         return libvlc_Stopped;
364     return libvlc_media_instance_get_state( p_mlp->p_mi, p_e );
365 }
366
367 /**************************************************************************
368  *        Play item at index (Public)
369  **************************************************************************/
370 void libvlc_media_list_player_play_item_at_index(
371                         libvlc_media_list_player_t * p_mlp,
372                         int i_index,
373                         libvlc_exception_t * p_e )
374 {
375     set_current_playing_item( p_mlp, libvlc_media_list_path_with_root_index(i_index), p_e );
376
377     if( libvlc_exception_raised( p_e ) )
378         return;
379
380     /* Send the next item event */
381     libvlc_event_t event;
382     event.type = libvlc_MediaListPlayerNextItemSet;
383     libvlc_event_send( p_mlp->p_event_manager, &event );
384
385     libvlc_media_instance_play( p_mlp->p_mi, p_e );
386 }
387
388 /**************************************************************************
389  *        Play item (Public)
390  **************************************************************************/
391 void libvlc_media_list_player_play_item(
392                         libvlc_media_list_player_t * p_mlp,
393                         libvlc_media_descriptor_t * p_md,
394                         libvlc_exception_t * p_e )
395 {
396     libvlc_media_list_path_t path = libvlc_media_list_path_of_item( p_mlp->p_mlist, p_md );
397     if( !path )
398     {
399         libvlc_exception_raise( p_e, "No such item in media list" );
400         return;
401     }
402     set_current_playing_item( p_mlp, path, p_e );
403
404     if( libvlc_exception_raised( p_e ) )
405         return;
406
407     libvlc_media_instance_play( p_mlp->p_mi, p_e );
408 }
409
410 /**************************************************************************
411  *       Stop (Public)
412  **************************************************************************/
413 void libvlc_media_list_player_stop( libvlc_media_list_player_t * p_mlp,
414                                     libvlc_exception_t * p_e )
415 {
416     libvlc_media_instance_stop( p_mlp->p_mi, p_e );
417
418     vlc_mutex_lock( &p_mlp->object_lock );
419     free( p_mlp->current_playing_item_path );
420     p_mlp->current_playing_item_path = NULL;
421     vlc_mutex_unlock( &p_mlp->object_lock );
422 }
423
424 /**************************************************************************
425  *       Next (Public)
426  **************************************************************************/
427 void libvlc_media_list_player_next( libvlc_media_list_player_t * p_mlp,
428                                     libvlc_exception_t * p_e )
429 {    
430     libvlc_media_list_path_t path;
431
432     if (! p_mlp->p_mlist )
433     {
434         libvlc_exception_raise( p_e, "No more element to play" );
435         return;
436     }
437     
438     libvlc_media_list_lock( p_mlp->p_mlist );
439
440     path = get_next_path( p_mlp );
441
442     if( !path )
443     {
444         libvlc_media_list_unlock( p_mlp->p_mlist );
445         libvlc_exception_raise( p_e, "No more element to play" );
446         libvlc_media_list_player_stop( p_mlp, p_e );
447         return;
448     }
449
450     set_current_playing_item( p_mlp, path, p_e );
451     
452     libvlc_media_instance_play( p_mlp->p_mi, p_e );
453
454     libvlc_media_list_unlock( p_mlp->p_mlist );
455
456     /* Send the next item event */
457     libvlc_event_t event;
458     event.type = libvlc_MediaListPlayerNextItemSet;
459     libvlc_event_send( p_mlp->p_event_manager, &event);
460 }