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