playlist.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:9k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * playlist.c: libvlc new API playlist handling functions
  3.  *****************************************************************************
  4.  * Copyright (C) 2005 the VideoLAN team
  5.  * $Id: fa5652f6788b65fc203ce9bb061cba9f200cee59 $
  6.  *
  7.  * Authors: Clément Stenac <zorglub@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 "media_player_internal.h"
  25. #include "libvlc.h"
  26. #include <vlc/vlc.h>
  27. #include <vlc_playlist.h>
  28. #include <assert.h>
  29. #define PL (libvlc_priv (p_instance->p_libvlc_int)->p_playlist)
  30. static inline int playlist_was_locked( libvlc_instance_t *p_instance )
  31. {
  32.     int was_locked;
  33.     vlc_mutex_lock( &p_instance->instance_lock );
  34.     was_locked = p_instance->b_playlist_locked;
  35.     vlc_mutex_unlock( &p_instance->instance_lock );
  36.     return was_locked;
  37. }
  38. static inline void playlist_mark_locked( libvlc_instance_t *p_instance,
  39.                                          int locked )
  40. {
  41.     vlc_mutex_lock( &p_instance->instance_lock );
  42.     p_instance->b_playlist_locked = locked;
  43.     vlc_mutex_unlock( &p_instance->instance_lock );
  44. }
  45. void libvlc_playlist_loop( libvlc_instance_t *p_instance, int loop,
  46.                            libvlc_exception_t *p_e)
  47. {
  48.     VLC_UNUSED(p_e);
  49.     assert( PL );
  50.     var_SetBool( PL, "loop", loop );
  51. }
  52. void libvlc_playlist_play( libvlc_instance_t *p_instance, int i_id,
  53.                            int i_options, char **ppsz_options,
  54.                            libvlc_exception_t *p_e )
  55. {
  56.     VLC_UNUSED(p_e); VLC_UNUSED(i_options); VLC_UNUSED(ppsz_options);
  57.     int did_lock = 0;
  58.     assert( PL );
  59.     ///todo Handle additionnal options
  60.     if( PL->items.i_size == 0 ) RAISEVOID( "Empty playlist" );
  61.     if( i_id > 0 )
  62.     {
  63.         playlist_item_t *p_item;
  64.         if (! playlist_was_locked( p_instance ) )
  65.         {
  66.             playlist_mark_locked( p_instance, 1 );
  67.             playlist_Lock( PL );
  68.             did_lock = 1;
  69.         }
  70.         p_item = playlist_ItemGetByInputId( PL, i_id,
  71.                                             PL->p_root_category );
  72.         if( p_item )
  73.             playlist_Control( PL, PLAYLIST_VIEWPLAY, pl_Locked,
  74.                               PL->p_root_category, p_item );
  75.        else
  76.             RAISEVOID( "Unable to find item" );
  77.         if( did_lock == 1 )
  78.         {
  79.             playlist_Unlock( PL );
  80.             playlist_mark_locked( p_instance, 0 );
  81.         }
  82.     }
  83.     else
  84.     {
  85.         playlist_Control( PL, PLAYLIST_PLAY,
  86.                           playlist_was_locked( p_instance ) );
  87.     }
  88. }
  89. void libvlc_playlist_pause( libvlc_instance_t *p_instance,
  90.                             libvlc_exception_t *p_e )
  91. {
  92.     assert( PL );
  93.     if( playlist_Control( PL, PLAYLIST_PAUSE,
  94.                           playlist_was_locked( p_instance ) ) != VLC_SUCCESS )
  95.         RAISEVOID( "Empty playlist" );
  96. }
  97. void libvlc_playlist_stop( libvlc_instance_t *p_instance,
  98.                            libvlc_exception_t *p_e )
  99. {
  100.     assert( PL );
  101.     if( playlist_Control( PL, PLAYLIST_STOP,
  102.                           playlist_was_locked( p_instance ) ) != VLC_SUCCESS )
  103.         RAISEVOID( "Empty playlist" );
  104. }
  105. void libvlc_playlist_clear( libvlc_instance_t *p_instance,
  106.                             libvlc_exception_t *p_e )
  107. {
  108.     VLC_UNUSED(p_e);
  109.     assert( PL );
  110.     playlist_Clear( PL, playlist_was_locked( p_instance ) );
  111. }
  112. void libvlc_playlist_next( libvlc_instance_t *p_instance,
  113.                            libvlc_exception_t *p_e )
  114. {
  115.     assert( PL );
  116.     if( playlist_Control( PL, PLAYLIST_SKIP, playlist_was_locked( p_instance ),
  117.                           1 ) != VLC_SUCCESS )
  118.         RAISEVOID( "Empty playlist" );
  119. }
  120. void libvlc_playlist_prev( libvlc_instance_t *p_instance,
  121.                            libvlc_exception_t *p_e )
  122. {
  123.     if( playlist_Control( PL, PLAYLIST_SKIP, playlist_was_locked( p_instance ),
  124.                           -1  ) != VLC_SUCCESS )
  125.         RAISEVOID( "Empty playlist" );
  126. }
  127. int libvlc_playlist_add( libvlc_instance_t *p_instance, const char *psz_uri,
  128.                          const char *psz_name, libvlc_exception_t *p_e )
  129. {
  130.     return libvlc_playlist_add_extended( p_instance, psz_uri, psz_name,
  131.                                          0, NULL, p_e );
  132. }
  133. static int PlaylistAddExtended( libvlc_instance_t *p_instance,
  134.                                 const char *psz_uri, const char *psz_name,
  135.                                 int i_options, const char **ppsz_options,
  136.                                 unsigned i_option_flags,
  137.                                 libvlc_exception_t *p_e )
  138. {
  139.     assert( PL );
  140.     if( playlist_was_locked( p_instance ) )
  141.     {
  142.         libvlc_exception_raise( p_e, "You must unlock playlist before "
  143.                                "calling libvlc_playlist_add" );
  144.         return VLC_EGENERIC;
  145.     }
  146.     return playlist_AddExt( PL, psz_uri, psz_name,
  147.                             PLAYLIST_INSERT, PLAYLIST_END, -1,
  148.                             i_options, ppsz_options, i_option_flags,
  149.                             true, pl_Unlocked );
  150. }
  151. int libvlc_playlist_add_extended( libvlc_instance_t *p_instance,
  152.                                   const char *psz_uri, const char *psz_name,
  153.                                   int i_options, const char **ppsz_options,
  154.                                   libvlc_exception_t *p_e )
  155. {
  156.     return PlaylistAddExtended( p_instance, psz_uri, psz_name,
  157.                                 i_options, ppsz_options, VLC_INPUT_OPTION_TRUSTED,
  158.                                 p_e );
  159. }
  160. int libvlc_playlist_add_extended_untrusted( libvlc_instance_t *p_instance,
  161.                                             const char *psz_uri, const char *psz_name,
  162.                                             int i_options, const char **ppsz_options,
  163.                                             libvlc_exception_t *p_e )
  164. {
  165.     return PlaylistAddExtended( p_instance, psz_uri, psz_name,
  166.                                 i_options, ppsz_options, 0,
  167.                                 p_e );
  168. }
  169. int libvlc_playlist_delete_item( libvlc_instance_t *p_instance, int i_id,
  170.                                  libvlc_exception_t *p_e )
  171. {
  172.     assert( PL );
  173.     if( playlist_DeleteFromInput( PL, i_id,
  174.                                   playlist_was_locked( p_instance ) ) )
  175.     {
  176.         libvlc_exception_raise( p_e, "deletion failed" );
  177.         return VLC_ENOITEM;
  178.     }
  179.     return VLC_SUCCESS;
  180. }
  181. int libvlc_playlist_isplaying( libvlc_instance_t *p_instance,
  182.                                libvlc_exception_t *p_e )
  183. {
  184.     VLC_UNUSED(p_e);
  185.     assert( PL );
  186.     return playlist_Status( PL ) == PLAYLIST_RUNNING;
  187. }
  188. int libvlc_playlist_items_count( libvlc_instance_t *p_instance,
  189.                                  libvlc_exception_t *p_e )
  190. {
  191.     VLC_UNUSED(p_e);
  192.     assert( PL );
  193.     return playlist_CurrentSize( PL );
  194. }
  195. int libvlc_playlist_get_current_index ( libvlc_instance_t *p_instance,
  196.                                         libvlc_exception_t *p_e )
  197. {
  198.     VLC_UNUSED(p_e);
  199.     assert( PL );
  200.     playlist_item_t *p_item = playlist_CurrentPlayingItem( PL );
  201.     if( !p_item )
  202.         return -1;
  203.     return p_item->i_id;
  204. }
  205. void libvlc_playlist_lock( libvlc_instance_t *p_instance )
  206. {
  207.     assert( PL );
  208.     playlist_Lock( PL );
  209.     p_instance->b_playlist_locked = 1;
  210. }
  211. void libvlc_playlist_unlock( libvlc_instance_t *p_instance )
  212. {
  213.     assert( PL );
  214.     p_instance->b_playlist_locked = 0;
  215.     playlist_Unlock( PL );
  216. }
  217. libvlc_media_player_t * libvlc_playlist_get_media_player(
  218.                                 libvlc_instance_t *p_instance,
  219.                                 libvlc_exception_t *p_e )
  220. {
  221.     libvlc_media_player_t *p_mi;
  222.     assert( PL );
  223.     input_thread_t * input = playlist_CurrentInput( PL );
  224.     if( input )
  225.     {
  226.         p_mi = libvlc_media_player_new_from_input_thread(
  227.                             p_instance, input, p_e );
  228.         vlc_object_release( input );
  229.     }
  230.     else
  231.     {
  232.         /* no active input */
  233.         p_mi = NULL;
  234.         libvlc_exception_raise( p_e, "No active input" );
  235.     }
  236.     return p_mi;
  237. }