]> git.sesse.net Git - vlc/blob - src/control/media_list_player.c
libvlc_media_list_player_release: do not leak memory
[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 deepness = libvlc_media_list_path_deepness( p_mlp->current_playing_item_path );
81     if( deepness < 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[deepness-1] >= libvlc_media_list_count( p_parent_of_playing_item, NULL ) )
87     {
88         deepness--;
89         if( deepness <= 0 )
90         {
91             free( ret );
92             libvlc_media_list_release( p_parent_of_playing_item );
93             return NULL;
94         }
95         ret[deepness] = -1;
96         ret[deepness-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     p_mlp->current_playing_item_path = NULL;
281     p_mlp->p_mi = NULL;
282     p_mlp->p_mlist = NULL;
283     vlc_mutex_init( &p_mlp->object_lock );
284     p_mlp->p_event_manager = libvlc_event_manager_new( p_mlp,
285                                                        p_instance,
286                                                        p_e );
287     libvlc_event_manager_register_event_type( p_mlp->p_event_manager,
288             libvlc_MediaListPlayerNextItemSet, p_e );
289
290     return p_mlp;
291 }
292
293 /**************************************************************************
294  *         release (Public)
295  **************************************************************************/
296 void libvlc_media_list_player_release( libvlc_media_list_player_t * p_mlp )
297 {
298     if( !p_mlp )
299         return;
300
301     vlc_mutex_lock( &p_mlp->object_lock );
302
303     p_mlp->i_refcount--;
304     if( p_mlp->i_refcount > 0 )
305     {
306         vlc_mutex_unlock( &p_mlp->object_lock );
307         return;
308     }
309     vlc_mutex_unlock( &p_mlp->object_lock );
310     vlc_mutex_destroy( &p_mlp->object_lock );
311
312     libvlc_event_manager_release( p_mlp->p_event_manager );
313     libvlc_media_player_release( p_mlp->p_mi );
314
315     if( p_mlp->p_mlist )
316     {
317         uninstall_playlist_observer( p_mlp );
318         libvlc_media_list_release( p_mlp->p_mlist );
319     }
320
321     free( p_mlp->current_playing_item_path );
322     free( p_mlp );
323 }
324
325 /**************************************************************************
326  *        set_media_player (Public)
327  **************************************************************************/
328 void libvlc_media_list_player_set_media_player(
329                                      libvlc_media_list_player_t * p_mlp,
330                                      libvlc_media_player_t * p_mi,
331                                      libvlc_exception_t * p_e )
332 {
333     VLC_UNUSED(p_e);
334
335     vlc_mutex_lock( &p_mlp->object_lock );
336
337     if( p_mlp->p_mi )
338     {
339         uninstall_media_player_observer( p_mlp );
340         libvlc_media_player_release( p_mlp->p_mi );
341     }
342     libvlc_media_player_retain( p_mi );
343     p_mlp->p_mi = p_mi;
344
345     install_media_player_observer( p_mlp );
346
347     vlc_mutex_unlock( &p_mlp->object_lock );
348 }
349
350 /**************************************************************************
351  *       set_media_list (Public)
352  **************************************************************************/
353 void libvlc_media_list_player_set_media_list(
354                                      libvlc_media_list_player_t * p_mlp,
355                                      libvlc_media_list_t * p_mlist,
356                                      libvlc_exception_t * p_e )
357 {
358     vlc_mutex_lock( &p_mlp->object_lock );
359
360     if( libvlc_media_list_player_is_playing( p_mlp, p_e ) )
361     {
362         libvlc_media_player_stop( p_mlp->p_mi, p_e );
363         /* Don't bother if there was an error. */
364         libvlc_exception_clear( p_e );
365     }
366
367     if( p_mlp->p_mlist )
368     {
369         uninstall_playlist_observer( p_mlp );
370         libvlc_media_list_release( p_mlp->p_mlist );
371     }
372     libvlc_media_list_retain( p_mlist );
373     p_mlp->p_mlist = p_mlist;
374  
375     install_playlist_observer( p_mlp );
376
377     vlc_mutex_unlock( &p_mlp->object_lock );
378 }
379
380 /**************************************************************************
381  *        Play (Public)
382  **************************************************************************/
383 void libvlc_media_list_player_play( libvlc_media_list_player_t * p_mlp,
384                                   libvlc_exception_t * p_e )
385 {
386     if( !p_mlp->current_playing_item_path )
387     {
388         libvlc_media_list_player_next( p_mlp, p_e );
389         return; /* Will set to play */
390     }
391
392     libvlc_media_player_play( p_mlp->p_mi, p_e );
393 }
394
395
396 /**************************************************************************
397  *        Pause (Public)
398  **************************************************************************/
399 void libvlc_media_list_player_pause( libvlc_media_list_player_t * p_mlp,
400                                      libvlc_exception_t * p_e )
401 {
402     if( !p_mlp->p_mi )
403         return;
404     libvlc_media_player_pause( p_mlp->p_mi, p_e );
405 }
406
407 /**************************************************************************
408  *        is_playing (Public)
409  **************************************************************************/
410 int
411 libvlc_media_list_player_is_playing( libvlc_media_list_player_t * p_mlp,
412                                      libvlc_exception_t * p_e )
413 {
414     libvlc_state_t state = libvlc_media_player_get_state( p_mlp->p_mi, p_e );
415     return (state == libvlc_Opening) || (state == libvlc_Buffering) ||
416            (state == libvlc_Playing);
417 }
418
419 /**************************************************************************
420  *        State (Public)
421  **************************************************************************/
422 libvlc_state_t
423 libvlc_media_list_player_get_state( libvlc_media_list_player_t * p_mlp,
424                                     libvlc_exception_t * p_e )
425 {
426     if( !p_mlp->p_mi )
427         return libvlc_Ended;
428     return libvlc_media_player_get_state( p_mlp->p_mi, p_e );
429 }
430
431 /**************************************************************************
432  *        Play item at index (Public)
433  **************************************************************************/
434 void libvlc_media_list_player_play_item_at_index(
435                         libvlc_media_list_player_t * p_mlp,
436                         int i_index,
437                         libvlc_exception_t * p_e )
438 {
439     set_current_playing_item( p_mlp, libvlc_media_list_path_with_root_index(i_index), p_e );
440
441     if( libvlc_exception_raised( p_e ) )
442         return;
443
444     /* Send the next item event */
445     libvlc_event_t event;
446     event.type = libvlc_MediaListPlayerNextItemSet;
447     libvlc_event_send( p_mlp->p_event_manager, &event );
448
449     libvlc_media_player_play( p_mlp->p_mi, p_e );
450 }
451
452 /**************************************************************************
453  *        Play item (Public)
454  **************************************************************************/
455 void libvlc_media_list_player_play_item(
456                         libvlc_media_list_player_t * p_mlp,
457                         libvlc_media_t * p_md,
458                         libvlc_exception_t * p_e )
459 {
460     libvlc_media_list_path_t path = libvlc_media_list_path_of_item( p_mlp->p_mlist, p_md );
461     if( !path )
462     {
463         libvlc_exception_raise( p_e, "No such item in media list" );
464         return;
465     }
466     set_current_playing_item( p_mlp, path, p_e );
467
468     if( libvlc_exception_raised( p_e ) )
469         return;
470
471     libvlc_media_player_play( p_mlp->p_mi, p_e );
472 }
473
474 /**************************************************************************
475  *       Stop (Public)
476  **************************************************************************/
477 void libvlc_media_list_player_stop( libvlc_media_list_player_t * p_mlp,
478                                     libvlc_exception_t * p_e )
479 {
480     if ( p_mlp->p_mi )
481     {
482         /* We are not interested in getting media stop event now */
483         uninstall_media_player_observer( p_mlp );
484         libvlc_media_player_stop( p_mlp->p_mi, p_e );
485         install_media_player_observer( p_mlp );
486     }
487
488     vlc_mutex_lock( &p_mlp->object_lock );
489     free( p_mlp->current_playing_item_path );
490     p_mlp->current_playing_item_path = NULL;
491     vlc_mutex_unlock( &p_mlp->object_lock );
492 }
493
494 /**************************************************************************
495  *       Next (Public)
496  **************************************************************************/
497 void libvlc_media_list_player_next( libvlc_media_list_player_t * p_mlp,
498                                     libvlc_exception_t * p_e )
499 {
500     libvlc_media_list_path_t path;
501
502     if (! p_mlp->p_mlist )
503     {
504         libvlc_exception_raise( p_e, "No more element to play" );
505         return;
506     }
507
508     libvlc_media_list_lock( p_mlp->p_mlist );
509
510     path = get_next_path( p_mlp );
511
512     if( !path )
513     {
514         libvlc_media_list_unlock( p_mlp->p_mlist );
515         libvlc_exception_raise( p_e, "No more element to play" );
516         libvlc_media_list_player_stop( p_mlp, p_e );
517         return;
518     }
519
520     set_current_playing_item( p_mlp, path, p_e );
521
522     libvlc_media_player_play( p_mlp->p_mi, p_e );
523
524     libvlc_media_list_unlock( p_mlp->p_mlist );
525
526     /* Send the next item event */
527     libvlc_event_t event;
528     event.type = libvlc_MediaListPlayerNextItemSet;
529     libvlc_event_send( p_mlp->p_event_manager, &event);
530 }