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