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