1 /*****************************************************************************
2 * media_list_player.c: libvlc new API media_list player functions
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
7 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
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.
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.
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 *****************************************************************************/
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>
32 #include "libvlc_internal.h"
34 #include "media_internal.h" // Abuse, could and should be removed
35 #include "media_list_path.h"
37 //#define DEBUG_MEDIA_LIST_PLAYER
39 /* This is a very dummy implementation of playlist on top of
40 * media_list and media_player.
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().
47 * This is thread safe, and we use a two keys (locks) scheme
48 * to discriminate between callbacks and regular uses.
51 struct libvlc_media_list_player_t
53 libvlc_event_manager_t * p_event_manager;
54 libvlc_instance_t * p_libvlc_instance;
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;
68 /* This is not yet exported by libvlccore */
69 static inline void vlc_assert_locked(vlc_mutex_t *mutex)
78 static void next(libvlc_media_list_player_t * p_mlp, libvlc_exception_t * p_e);
84 /**************************************************************************
86 **************************************************************************/
87 static inline void lock(libvlc_media_list_player_t * p_mlp)
89 // Obtain an access to this structure
90 vlc_mutex_lock(&p_mlp->object_lock);
92 // Make sure no callback will occurs at the same time
93 vlc_mutex_lock(&p_mlp->mp_callback_lock);
96 static inline void unlock(libvlc_media_list_player_t * p_mlp)
98 vlc_mutex_unlock(&p_mlp->mp_callback_lock);
99 vlc_mutex_unlock(&p_mlp->object_lock);
102 static inline void assert_locked(libvlc_media_list_player_t * p_mlp)
104 vlc_assert_locked(&p_mlp->mp_callback_lock);
107 static inline libvlc_event_manager_t * mlist_em(libvlc_media_list_player_t * p_mlp)
109 assert_locked(p_mlp);
110 return libvlc_media_list_event_manager(p_mlp->p_mlist, NULL);
113 static inline libvlc_event_manager_t * mplayer_em(libvlc_media_list_player_t * p_mlp)
115 assert_locked(p_mlp);
116 return libvlc_media_player_event_manager(p_mlp->p_mi, NULL);
120 /**************************************************************************
121 * get_next_path (private)
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)
128 assert_locked(p_mlp);
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;
135 if (!p_mlp->current_playing_item_path)
137 if (!libvlc_media_list_count(p_mlp->p_mlist, NULL))
139 return libvlc_media_list_path_with_root_index(0);
142 p_sublist_of_playing_item = libvlc_media_list_sublist_at_path(
144 p_mlp->current_playing_item_path);
146 /* If item just gained a sublist just play it */
147 if (p_sublist_of_playing_item)
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);
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);
157 int depth = libvlc_media_list_path_depth(p_mlp->current_playing_item_path);
158 if (depth < 1 || !p_parent_of_playing_item)
161 ret = libvlc_media_list_path_copy(p_mlp->current_playing_item_path);
163 ret[depth-1]++; // Play next element
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))
172 libvlc_media_list_release(p_parent_of_playing_item);
177 p_parent_of_playing_item = libvlc_media_list_parentlist_at_path(
181 libvlc_media_list_release(p_parent_of_playing_item);
185 /**************************************************************************
186 * media_player_reached_end (private) (Event Callback)
187 **************************************************************************/
189 media_player_reached_end(const libvlc_event_t * p_event, void * p_user_data)
192 libvlc_media_list_player_t * p_mlp = p_user_data;
193 libvlc_exception_t e;
194 libvlc_exception_init(&e);
196 vlc_mutex_lock(&p_mlp->mp_callback_lock);
197 if (!p_mlp->are_mp_callback_cancelled)
199 vlc_mutex_unlock(&p_mlp->mp_callback_lock);
201 // There is no point in reporting an error from this callback
202 libvlc_exception_clear(&e);
205 /**************************************************************************
206 * playlist_item_deleted (private) (Event Callback)
207 **************************************************************************/
209 mlist_item_deleted(const libvlc_event_t * p_event, void * p_user_data)
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);
216 if (p_event->u.media_list_item_deleted.item == p_current_md &&
217 p_emitting_mlist == p_mlp->p_mlist)
219 /* We are playing this item, let's stop */
220 libvlc_media_list_player_stop(p_mlp, NULL);
225 /**************************************************************************
226 * install_playlist_observer (private)
227 **************************************************************************/
229 install_playlist_observer(libvlc_media_list_player_t * p_mlp)
231 assert_locked(p_mlp);
232 libvlc_event_attach(mlist_em(p_mlp), libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL);
235 /**************************************************************************
236 * uninstall_playlist_observer (private)
237 **************************************************************************/
239 uninstall_playlist_observer(libvlc_media_list_player_t * p_mlp)
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);
246 /**************************************************************************
247 * install_media_player_observer (private)
248 **************************************************************************/
250 install_media_player_observer(libvlc_media_list_player_t * p_mlp)
252 assert_locked(p_mlp);
253 libvlc_event_attach_async(mplayer_em(p_mlp), libvlc_MediaPlayerEndReached, media_player_reached_end, p_mlp, NULL);
257 /**************************************************************************
258 * uninstall_media_player_observer (private)
259 **************************************************************************/
261 uninstall_media_player_observer(libvlc_media_list_player_t * p_mlp)
263 assert_locked(p_mlp);
264 if (!p_mlp->p_mi) return;
266 // From now on, media_player callback won't be relevant.
267 p_mlp->are_mp_callback_cancelled = true;
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);
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;
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.
282 /**************************************************************************
283 * set_current_playing_item (private)
285 * Playlist lock should be held
286 **************************************************************************/
288 set_current_playing_item(libvlc_media_list_player_t * p_mlp, libvlc_media_list_path_t path)
290 assert_locked(p_mlp);
292 /* First, save the new path that we are going to play */
293 if (p_mlp->current_playing_item_path != path)
295 free(p_mlp->current_playing_item_path);
296 p_mlp->current_playing_item_path = path;
299 libvlc_media_t * p_md;
300 p_md = libvlc_media_list_item_at_path(p_mlp->p_mlist, path);
304 /* Make sure media_player_reached_end() won't get called */
305 uninstall_media_player_observer(p_mlp);
307 /* Create a new media_player if there is none */
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 */
315 * Public libvlc functions
318 /**************************************************************************
320 **************************************************************************/
321 libvlc_media_list_player_t *
322 libvlc_media_list_player_new(libvlc_instance_t * p_instance, libvlc_exception_t * p_e)
325 libvlc_media_list_player_t * p_mlp;
326 p_mlp = calloc(sizeof(libvlc_media_list_player_t), 1);
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);
341 /**************************************************************************
343 **************************************************************************/
344 void libvlc_media_list_player_release(libvlc_media_list_player_t * p_mlp)
351 if (p_mlp->i_refcount > 0)
357 assert(p_mlp->i_refcount == 0);
359 /* Keep the lock(), because the uninstall functions
360 * check for it. That's convenient. */
364 uninstall_media_player_observer(p_mlp);
365 libvlc_media_player_release(p_mlp->p_mi);
369 uninstall_playlist_observer(p_mlp);
370 libvlc_media_list_release(p_mlp->p_mlist);
374 vlc_mutex_destroy(&p_mlp->object_lock);
375 vlc_mutex_destroy(&p_mlp->mp_callback_lock);
377 libvlc_event_manager_release(p_mlp->p_event_manager);
379 free(p_mlp->current_playing_item_path);
380 libvlc_release(p_mlp->p_libvlc_instance);
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)
395 uninstall_media_player_observer(p_mlp);
396 libvlc_media_player_release(p_mlp->p_mi);
398 libvlc_media_player_retain(p_mi);
401 install_media_player_observer(p_mlp);
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)
415 libvlc_exception_raise(p_e, "No media list provided");
421 uninstall_playlist_observer(p_mlp);
422 libvlc_media_list_release(p_mlp->p_mlist);
424 libvlc_media_list_retain(p_mlist);
425 p_mlp->p_mlist = p_mlist;
427 install_playlist_observer(p_mlp);
432 /**************************************************************************
434 **************************************************************************/
435 void libvlc_media_list_player_play(libvlc_media_list_player_t * p_mlp, libvlc_exception_t * p_e)
437 if (!p_mlp->current_playing_item_path)
439 libvlc_media_list_player_next(p_mlp, p_e);
440 return; /* Will set to play */
443 libvlc_media_player_play(p_mlp->p_mi, p_e);
447 /**************************************************************************
449 **************************************************************************/
450 void libvlc_media_list_player_pause(libvlc_media_list_player_t * p_mlp, libvlc_exception_t * p_e)
454 libvlc_media_player_pause(p_mlp->p_mi, p_e);
457 /**************************************************************************
458 * is_playing (Public)
459 **************************************************************************/
461 libvlc_media_list_player_is_playing(libvlc_media_list_player_t * p_mlp, libvlc_exception_t * p_e)
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);
468 /**************************************************************************
470 **************************************************************************/
472 libvlc_media_list_player_get_state(libvlc_media_list_player_t * p_mlp, libvlc_exception_t * p_e)
476 return libvlc_media_player_get_state(p_mlp->p_mi, p_e);
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)
485 set_current_playing_item(p_mlp, libvlc_media_list_path_with_root_index(i_index));
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);
492 libvlc_media_player_play(p_mlp->p_mi, p_e);
495 /**************************************************************************
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)
500 libvlc_media_list_path_t path = libvlc_media_list_path_of_item(p_mlp->p_mlist, p_md);
503 libvlc_exception_raise(p_e, "No such item in media list");
506 set_current_playing_item(p_mlp, path);
507 libvlc_media_player_play(p_mlp->p_mi, p_e);
510 /**************************************************************************
512 **************************************************************************/
513 void libvlc_media_list_player_stop(libvlc_media_list_player_t * p_mlp, libvlc_exception_t * p_e)
517 if (p_mlp->p_mi && p_mlp->current_playing_item_path)
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);
525 free(p_mlp->current_playing_item_path);
526 p_mlp->current_playing_item_path = NULL;
530 /**************************************************************************
532 **************************************************************************/
533 static void next(libvlc_media_list_player_t * p_mlp, libvlc_exception_t * p_e)
535 assert_locked(p_mlp);
539 libvlc_exception_raise(p_e, "No media list");
543 libvlc_media_list_lock(p_mlp->p_mlist);
545 libvlc_media_list_path_t path = get_next_path(p_mlp);
547 #ifdef DEBUG_MEDIA_LIST_PLAYER
549 libvlc_media_list_path_dump(path);
552 set_current_playing_item(p_mlp, path);
556 libvlc_media_list_unlock(p_mlp->p_mlist);
557 libvlc_media_list_player_stop(p_mlp, p_e);
561 libvlc_media_player_play(p_mlp->p_mi, p_e);
563 libvlc_media_list_unlock(p_mlp->p_mlist);
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);
571 /**************************************************************************
573 **************************************************************************/
574 void libvlc_media_list_player_next(libvlc_media_list_player_t * p_mlp, libvlc_exception_t * p_e)