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 *****************************************************************************/
28 #include <vlc/libvlc.h>
29 #include <vlc/libvlc_media.h>
30 #include <vlc/libvlc_media_list.h>
31 #include <vlc/libvlc_media_player.h>
32 #include <vlc/libvlc_media_list_player.h>
33 #include <vlc/libvlc_events.h>
36 #include "libvlc_internal.h"
38 #include "media_internal.h" // Abuse, could and should be removed
39 #include "media_list_path.h"
41 //#define DEBUG_MEDIA_LIST_PLAYER
43 /* This is a very dummy implementation of playlist on top of
44 * media_list and media_player.
46 * All this code is doing is simply computing the next item
47 * of a tree of media_list (see get_next_index()), and play
48 * the next item when the current is over. This is happening
49 * via the event callback media_player_reached_end().
51 * This is thread safe, and we use a two keys (locks) scheme
52 * to discriminate between callbacks and regular uses.
55 struct libvlc_media_list_player_t
57 libvlc_event_manager_t * p_event_manager;
58 libvlc_instance_t * p_libvlc_instance;
60 /* Protect access to this structure. */
61 vlc_mutex_t object_lock;
62 /* Protect access to this structure and from callback execution. */
63 vlc_mutex_t mp_callback_lock;
64 /* Indicate to media player callbacks that they are cancelled. */
65 bool are_mp_callback_cancelled;
66 libvlc_media_list_path_t current_playing_item_path;
67 libvlc_media_t * p_current_playing_item;
68 libvlc_media_list_t * p_mlist;
69 libvlc_media_player_t * p_mi;
70 libvlc_playback_mode_t e_playback_mode;
73 /* This is not yet exported by libvlccore */
74 static inline void vlc_assert_locked(vlc_mutex_t *mutex)
83 static void set_relative_playlist_position_and_play(
84 libvlc_media_list_player_t * p_mlp,
85 int i_relative_position,
86 libvlc_exception_t * p_e);
87 static void stop(libvlc_media_list_player_t * p_mlp);
93 /**************************************************************************
95 **************************************************************************/
96 static inline void lock(libvlc_media_list_player_t * p_mlp)
98 // Obtain an access to this structure
99 vlc_mutex_lock(&p_mlp->object_lock);
101 // Make sure no callback will occurs at the same time
102 vlc_mutex_lock(&p_mlp->mp_callback_lock);
105 static inline void unlock(libvlc_media_list_player_t * p_mlp)
107 vlc_mutex_unlock(&p_mlp->mp_callback_lock);
108 vlc_mutex_unlock(&p_mlp->object_lock);
111 static inline void assert_locked(libvlc_media_list_player_t * p_mlp)
113 vlc_assert_locked(&p_mlp->mp_callback_lock);
116 static inline libvlc_event_manager_t * mlist_em(libvlc_media_list_player_t * p_mlp)
118 assert_locked(p_mlp);
119 return libvlc_media_list_event_manager(p_mlp->p_mlist);
122 static inline libvlc_event_manager_t * mplayer_em(libvlc_media_list_player_t * p_mlp)
124 assert_locked(p_mlp);
125 return libvlc_media_player_event_manager(p_mlp->p_mi);
128 /**************************************************************************
129 * get_next_path (private)
131 * Returns the path to the next item in the list.
132 * If looping is specified and the current item is the last list item in
133 * the list it will return the first item in the list.
134 **************************************************************************/
135 static libvlc_media_list_path_t
136 get_next_path(libvlc_media_list_player_t * p_mlp, bool b_loop)
138 assert_locked(p_mlp);
140 /* We are entered with libvlc_media_list_lock(p_mlp->p_list) */
141 libvlc_media_list_path_t ret;
142 libvlc_media_list_t * p_parent_of_playing_item;
143 libvlc_media_list_t * p_sublist_of_playing_item;
145 if (!p_mlp->current_playing_item_path)
147 if (!libvlc_media_list_count(p_mlp->p_mlist))
149 return libvlc_media_list_path_with_root_index(0);
152 p_sublist_of_playing_item = libvlc_media_list_sublist_at_path(
154 p_mlp->current_playing_item_path);
156 /* If item just gained a sublist just play it */
157 if (p_sublist_of_playing_item)
159 libvlc_media_list_release(p_sublist_of_playing_item);
160 return libvlc_media_list_path_copy_by_appending(p_mlp->current_playing_item_path, 0);
163 /* Try to catch parent element */
164 p_parent_of_playing_item = libvlc_media_list_parentlist_at_path(p_mlp->p_mlist,
165 p_mlp->current_playing_item_path);
167 int depth = libvlc_media_list_path_depth(p_mlp->current_playing_item_path);
168 if (depth < 1 || !p_parent_of_playing_item)
171 ret = libvlc_media_list_path_copy(p_mlp->current_playing_item_path);
172 ret[depth - 1]++; /* set to next element */
174 /* If this goes beyond the end of the list */
175 while(ret[depth-1] >= libvlc_media_list_count(p_parent_of_playing_item))
189 libvlc_media_list_release(p_parent_of_playing_item);
195 p_parent_of_playing_item = libvlc_media_list_parentlist_at_path(
200 libvlc_media_list_release(p_parent_of_playing_item);
204 /**************************************************************************
205 * find_last_item (private)
207 * Returns the path of the last descendant of a given item path.
208 * Note: Due to the recursive nature of the function and the need to free
209 * media list paths, paths passed in may be freed if they are replaced.
210 Recommended usage is to set return value to the same path that was
211 passed to the function (i.e. item = find_last_item(list, item); )
212 **************************************************************************/
213 static libvlc_media_list_path_t
214 find_last_item( libvlc_media_list_t * p_mlist, libvlc_media_list_path_t current_item )
216 libvlc_media_list_t * p_sublist = libvlc_media_list_sublist_at_path(p_mlist, current_item);
217 libvlc_media_list_path_t last_item_path = current_item;
221 int i_count = libvlc_media_list_count(p_sublist);
224 /* Add the last sublist item to the path. */
225 last_item_path = libvlc_media_list_path_copy_by_appending(current_item, i_count - 1);
227 /* Check that sublist item for more descendants. */
228 last_item_path = find_last_item(p_mlist, last_item_path);
231 libvlc_media_list_release(p_sublist);
234 return last_item_path;
237 /**************************************************************************
238 * get_previous_path (private)
240 * Returns the path to the preceding item in the list.
241 * If looping is specified and the current item is the first list item in
242 * the list it will return the last descendant of the last item in the list.
243 **************************************************************************/
244 static libvlc_media_list_path_t
245 get_previous_path(libvlc_media_list_player_t * p_mlp, bool b_loop)
247 assert_locked(p_mlp);
249 /* We are entered with libvlc_media_list_lock(p_mlp->p_list) */
250 libvlc_media_list_path_t ret;
251 libvlc_media_list_t * p_parent_of_playing_item;
253 if (!p_mlp->current_playing_item_path)
255 if (!libvlc_media_list_count(p_mlp->p_mlist))
257 return libvlc_media_list_path_with_root_index(0);
260 /* Try to catch parent element */
261 p_parent_of_playing_item = libvlc_media_list_parentlist_at_path(
263 p_mlp->current_playing_item_path);
265 int depth = libvlc_media_list_path_depth(p_mlp->current_playing_item_path);
266 if (depth < 1 || !p_parent_of_playing_item)
269 /* Set the return path to the current path */
270 ret = libvlc_media_list_path_copy(p_mlp->current_playing_item_path);
272 /* Change the return path to the previous list entry */
273 ret[depth - 1]--; /* set to previous element */
276 /* Is the return path is beyond the start of the current list? */
277 if(ret[depth - 1] < 0)
279 /* Move to parent of current item */
282 /* Are we at the root level of the tree? */
285 // Is looping enabled?
288 int i_count = libvlc_media_list_count(p_parent_of_playing_item);
290 /* Set current play item to the last element in the list */
291 ret[0] = i_count - 1;
294 /* Set the return path to the last descendant item of the current item */
295 ret = find_last_item(p_mlp->p_mlist, ret);
299 /* No looping so return empty path. */
306 /* This is the case of moving backward from the beginning of the
307 * subitem list to its parent item.
308 * This ensures that current path is properly terminated to
316 ret = find_last_item(p_mlp->p_mlist, ret);
319 libvlc_media_list_release(p_parent_of_playing_item);
323 /**************************************************************************
324 * media_player_reached_end (private) (Event Callback)
325 **************************************************************************/
327 media_player_reached_end(const libvlc_event_t * p_event, void * p_user_data)
330 libvlc_media_list_player_t * p_mlp = p_user_data;
331 libvlc_exception_t e;
332 libvlc_exception_init(&e);
334 vlc_mutex_lock(&p_mlp->mp_callback_lock);
335 if (!p_mlp->are_mp_callback_cancelled)
336 set_relative_playlist_position_and_play(p_mlp, 1, &e);
337 vlc_mutex_unlock(&p_mlp->mp_callback_lock);
339 // There is no point in reporting an error from this callback
340 libvlc_exception_clear(&e);
343 /**************************************************************************
344 * playlist_item_deleted (private) (Event Callback)
345 **************************************************************************/
347 mlist_item_deleted(const libvlc_event_t * p_event, void * p_user_data)
349 // Nothing to do. For now.
350 (void)p_event; (void)p_user_data;
354 /**************************************************************************
355 * install_playlist_observer (private)
356 **************************************************************************/
358 install_playlist_observer(libvlc_media_list_player_t * p_mlp)
360 assert_locked(p_mlp);
361 libvlc_event_attach(mlist_em(p_mlp), libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp);
364 /**************************************************************************
365 * uninstall_playlist_observer (private)
366 **************************************************************************/
368 uninstall_playlist_observer(libvlc_media_list_player_t * p_mlp)
370 assert_locked(p_mlp);
371 if (!p_mlp->p_mlist) return;
372 libvlc_event_detach(mlist_em(p_mlp), libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp);
375 /**************************************************************************
376 * install_media_player_observer (private)
377 **************************************************************************/
379 install_media_player_observer(libvlc_media_list_player_t * p_mlp)
381 assert_locked(p_mlp);
382 libvlc_event_attach_async(mplayer_em(p_mlp), libvlc_MediaPlayerEndReached, media_player_reached_end, p_mlp);
386 /**************************************************************************
387 * uninstall_media_player_observer (private)
388 **************************************************************************/
390 uninstall_media_player_observer(libvlc_media_list_player_t * p_mlp)
392 assert_locked(p_mlp);
393 if (!p_mlp->p_mi) return;
395 // From now on, media_player callback won't be relevant.
396 p_mlp->are_mp_callback_cancelled = true;
398 // Allow callbacks to run, because detach() will wait until all callbacks are processed.
399 // This is safe because only callbacks are allowed, and there execution will be cancelled.
400 vlc_mutex_unlock(&p_mlp->mp_callback_lock);
401 libvlc_event_detach(mplayer_em(p_mlp), libvlc_MediaPlayerEndReached, media_player_reached_end, p_mlp);
403 // Now, lock back the callback lock. No more callback will be present from this point.
404 vlc_mutex_lock(&p_mlp->mp_callback_lock);
405 p_mlp->are_mp_callback_cancelled = false;
407 // What is here is safe, because we guarantee that we won't be able to anything concurrently,
408 // - except (cancelled) callbacks - thanks to the object_lock.
411 /**************************************************************************
412 * set_current_playing_item (private)
414 * Playlist lock should be held
415 **************************************************************************/
417 set_current_playing_item(libvlc_media_list_player_t * p_mlp, libvlc_media_list_path_t path)
419 assert_locked(p_mlp);
421 /* First, save the new path that we are going to play */
422 if (p_mlp->current_playing_item_path != path)
424 free(p_mlp->current_playing_item_path);
425 p_mlp->current_playing_item_path = path;
431 libvlc_media_t * p_md;
432 p_md = libvlc_media_list_item_at_path(p_mlp->p_mlist, path);
436 /* Make sure media_player_reached_end() won't get called */
437 uninstall_media_player_observer(p_mlp);
439 /* Create a new media_player if there is none */
441 p_mlp->p_mi = libvlc_media_player_new_from_media(p_md);
443 libvlc_media_player_set_media(p_mlp->p_mi, p_md);
445 install_media_player_observer(p_mlp);
446 libvlc_media_release(p_md); /* for libvlc_media_list_item_at_index */
450 * Public libvlc functions
453 /**************************************************************************
455 **************************************************************************/
456 libvlc_media_list_player_t *
457 libvlc_media_list_player_new(libvlc_instance_t * p_instance)
459 libvlc_media_list_player_t * p_mlp;
460 p_mlp = calloc( 1, sizeof(libvlc_media_list_player_t) );
461 if (unlikely(p_mlp == NULL))
463 libvlc_printerr("Not enough memory");
467 p_mlp->p_event_manager = libvlc_event_manager_new(p_mlp, p_instance);
468 if (unlikely(p_mlp->p_event_manager == NULL))
474 libvlc_retain(p_instance);
475 p_mlp->p_libvlc_instance = p_instance;
476 p_mlp->i_refcount = 1;
477 vlc_mutex_init(&p_mlp->object_lock);
478 vlc_mutex_init(&p_mlp->mp_callback_lock);
479 libvlc_event_manager_register_event_type(p_mlp->p_event_manager, libvlc_MediaListPlayerNextItemSet);
480 p_mlp->e_playback_mode = libvlc_playback_mode_default;
485 /**************************************************************************
487 **************************************************************************/
488 void libvlc_media_list_player_release(libvlc_media_list_player_t * p_mlp)
495 if (p_mlp->i_refcount > 0)
501 assert(p_mlp->i_refcount == 0);
503 /* Keep the lock(), because the uninstall functions
504 * check for it. That's convenient. */
508 uninstall_media_player_observer(p_mlp);
509 libvlc_media_player_release(p_mlp->p_mi);
513 uninstall_playlist_observer(p_mlp);
514 libvlc_media_list_release(p_mlp->p_mlist);
518 vlc_mutex_destroy(&p_mlp->object_lock);
519 vlc_mutex_destroy(&p_mlp->mp_callback_lock);
521 libvlc_event_manager_release(p_mlp->p_event_manager);
523 free(p_mlp->current_playing_item_path);
524 libvlc_release(p_mlp->p_libvlc_instance);
528 /**************************************************************************
529 * event_manager (Public)
530 **************************************************************************/
531 libvlc_event_manager_t *
532 libvlc_media_list_player_event_manager(libvlc_media_list_player_t * p_mlp)
534 return p_mlp->p_event_manager;
537 /**************************************************************************
538 * set_media_player (Public)
539 **************************************************************************/
540 void libvlc_media_list_player_set_media_player(libvlc_media_list_player_t * p_mlp, libvlc_media_player_t * p_mi)
546 uninstall_media_player_observer(p_mlp);
547 libvlc_media_player_release(p_mlp->p_mi);
549 libvlc_media_player_retain(p_mi);
552 install_media_player_observer(p_mlp);
557 /**************************************************************************
558 * set_media_list (Public)
559 **************************************************************************/
560 void libvlc_media_list_player_set_media_list(libvlc_media_list_player_t * p_mlp, libvlc_media_list_t * p_mlist)
567 uninstall_playlist_observer(p_mlp);
568 libvlc_media_list_release(p_mlp->p_mlist);
570 libvlc_media_list_retain(p_mlist);
571 p_mlp->p_mlist = p_mlist;
573 install_playlist_observer(p_mlp);
578 /**************************************************************************
580 **************************************************************************/
581 void libvlc_media_list_player_play(libvlc_media_list_player_t * p_mlp, libvlc_exception_t * p_e)
584 if (!p_mlp->current_playing_item_path)
586 set_relative_playlist_position_and_play(p_mlp, 1, p_e);
588 return; /* Will set to play */
590 libvlc_media_player_play(p_mlp->p_mi);
595 /**************************************************************************
597 **************************************************************************/
598 void libvlc_media_list_player_pause(libvlc_media_list_player_t * p_mlp, libvlc_exception_t * p_e)
606 libvlc_media_player_pause(p_mlp->p_mi);
610 /**************************************************************************
611 * is_playing (Public)
612 **************************************************************************/
614 libvlc_media_list_player_is_playing(libvlc_media_list_player_t * p_mlp)
616 libvlc_state_t state = libvlc_media_player_get_state(p_mlp->p_mi);
617 return (state == libvlc_Opening) || (state == libvlc_Buffering) ||
618 (state == libvlc_Playing);
621 /**************************************************************************
623 **************************************************************************/
625 libvlc_media_list_player_get_state(libvlc_media_list_player_t * p_mlp)
629 return libvlc_media_player_get_state(p_mlp->p_mi);
632 /**************************************************************************
633 * Play item at index (Public)
634 **************************************************************************/
635 void libvlc_media_list_player_play_item_at_index(libvlc_media_list_player_t * p_mlp, int i_index, libvlc_exception_t *p_e)
638 set_current_playing_item(p_mlp, libvlc_media_list_path_with_root_index(i_index));
639 libvlc_media_player_play(p_mlp->p_mi);
642 /* Send the next item event */
643 libvlc_event_t event;
644 event.type = libvlc_MediaListPlayerNextItemSet;
645 libvlc_event_send(p_mlp->p_event_manager, &event);
648 /**************************************************************************
650 **************************************************************************/
651 void libvlc_media_list_player_play_item(libvlc_media_list_player_t * p_mlp, libvlc_media_t * p_md, libvlc_exception_t * p_e)
654 libvlc_media_list_path_t path = libvlc_media_list_path_of_item(p_mlp->p_mlist, p_md);
657 libvlc_exception_raise(p_e);
658 libvlc_printerr("Item not found in media list");
663 set_current_playing_item(p_mlp, path);
664 libvlc_media_player_play(p_mlp->p_mi);
668 /**************************************************************************
672 **************************************************************************/
673 static void stop(libvlc_media_list_player_t * p_mlp)
675 assert_locked(p_mlp);
679 /* We are not interested in getting media stop event now */
680 uninstall_media_player_observer(p_mlp);
681 libvlc_media_player_stop(p_mlp->p_mi);
682 install_media_player_observer(p_mlp);
685 free(p_mlp->current_playing_item_path);
686 p_mlp->current_playing_item_path = NULL;
689 /**************************************************************************
691 **************************************************************************/
692 void libvlc_media_list_player_stop(libvlc_media_list_player_t * p_mlp)
699 /**************************************************************************
700 * Set relative playlist position and play (Private)
702 * Sets the currently played item to the given relative play item position
703 * (based on the currently playing item) and then begins the new item playback.
705 **************************************************************************/
706 static void set_relative_playlist_position_and_play(
707 libvlc_media_list_player_t * p_mlp,
708 int i_relative_position,
709 libvlc_exception_t * p_e)
711 assert_locked(p_mlp);
715 libvlc_exception_raise(p_e);
716 libvlc_printerr("No media list");
720 libvlc_media_list_lock(p_mlp->p_mlist);
722 libvlc_media_list_path_t path = p_mlp->current_playing_item_path;
724 if(p_mlp->e_playback_mode != libvlc_playback_mode_repeat)
726 bool b_loop = (p_mlp->e_playback_mode == libvlc_playback_mode_loop);
728 if(i_relative_position > 0)
732 path = get_next_path(p_mlp, b_loop);
733 set_current_playing_item(p_mlp, path);
734 --i_relative_position;
736 while(i_relative_position > 0);
738 else if(i_relative_position < 0)
742 path = get_previous_path(p_mlp, b_loop);
743 set_current_playing_item(p_mlp, path);
744 ++i_relative_position;
746 while (i_relative_position < 0);
751 set_current_playing_item(p_mlp, path);
754 #ifdef DEBUG_MEDIA_LIST_PLAYER
756 libvlc_media_list_path_dump(path);
761 libvlc_media_list_unlock(p_mlp->p_mlist);
765 libvlc_media_player_play(p_mlp->p_mi);
767 libvlc_media_list_unlock(p_mlp->p_mlist);
769 /* Send the next item event */
770 libvlc_event_t event;
771 event.type = libvlc_MediaListPlayerNextItemSet;
772 libvlc_media_t * p_md = libvlc_media_list_item_at_path(p_mlp->p_mlist, path);
773 event.u.media_list_player_next_item_set.item = p_md;
774 libvlc_event_send(p_mlp->p_event_manager, &event);
775 libvlc_media_release(p_md);
778 /**************************************************************************
780 **************************************************************************/
781 void libvlc_media_list_player_next(libvlc_media_list_player_t * p_mlp,
782 libvlc_exception_t * p_e)
785 set_relative_playlist_position_and_play(p_mlp, 1, p_e);
789 /**************************************************************************
791 **************************************************************************/
792 void libvlc_media_list_player_previous(libvlc_media_list_player_t * p_mlp,
793 libvlc_exception_t * p_e)
796 set_relative_playlist_position_and_play(p_mlp, -1, p_e);
800 /**************************************************************************
801 * Set Playback Mode (Public)
802 **************************************************************************/
803 void libvlc_media_list_player_set_playback_mode(
804 libvlc_media_list_player_t * p_mlp,
805 libvlc_playback_mode_t e_mode )
808 p_mlp->e_playback_mode = e_mode;