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