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