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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * playlist.c
  3.  *****************************************************************************
  4.  * Copyright (C) 2007-2008 the VideoLAN team
  5.  * $Id: f442fe2ed3829b7cb4a95cd4e52feca94213566d $
  6.  *
  7.  * Authors: Antoine Cellerier <dionoea at videolan tod 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.  * Preamble
  25.  *****************************************************************************/
  26. #ifndef  _GNU_SOURCE
  27. #   define  _GNU_SOURCE
  28. #endif
  29. #ifdef HAVE_CONFIG_H
  30. # include "config.h"
  31. #endif
  32. #include <vlc_common.h>
  33. #include <vlc_interface.h>
  34. #include <vlc_playlist.h>
  35. #include <lua.h>        /* Low level lua C API */
  36. #include <lauxlib.h>    /* Higher level C API */
  37. #include "../vlc.h"
  38. #include "../libs.h"
  39. #include "playlist.h"
  40. #include "variables.h"
  41. /*****************************************************************************
  42.  * Internal lua<->vlc utils
  43.  *****************************************************************************/
  44. playlist_t *vlclua_get_playlist_internal( lua_State *L )
  45. {
  46.     vlc_object_t *p_this = vlclua_get_this( L );
  47.     return pl_Hold( p_this );
  48. }
  49. void vlclua_release_playlist_internal( playlist_t *p_playlist )
  50. {
  51.     vlc_object_release( p_playlist );
  52. }
  53. static int vlclua_playlist_prev( lua_State * L )
  54. {
  55.     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
  56.     playlist_Prev( p_playlist );
  57.     vlclua_release_playlist_internal( p_playlist );
  58.     return 0;
  59. }
  60. static int vlclua_playlist_next( lua_State * L )
  61. {
  62.     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
  63.     playlist_Next( p_playlist );
  64.     vlclua_release_playlist_internal( p_playlist );
  65.     return 0;
  66. }
  67. static int vlclua_playlist_skip( lua_State * L )
  68. {
  69.     int i_skip = luaL_checkint( L, 1 );
  70.     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
  71.     playlist_Skip( p_playlist, i_skip );
  72.     vlclua_release_playlist_internal( p_playlist );
  73.     return 0;
  74. }
  75. static int vlclua_playlist_play( lua_State * L )
  76. {
  77.     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
  78.     playlist_Play( p_playlist );
  79.     vlclua_release_playlist_internal( p_playlist );
  80.     return 0;
  81. }
  82. static int vlclua_playlist_pause( lua_State * L )
  83. {
  84.     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
  85.     playlist_Pause( p_playlist );
  86.     vlclua_release_playlist_internal( p_playlist );
  87.     return 0;
  88. }
  89. static int vlclua_playlist_stop( lua_State * L )
  90. {
  91.     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
  92.     playlist_Stop( p_playlist );
  93.     vlclua_release_playlist_internal( p_playlist );
  94.     return 0;
  95. }
  96. static int vlclua_playlist_clear( lua_State * L )
  97. {
  98.     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
  99.     playlist_Stop( p_playlist ); /* Isn't this already implied by Clear? */
  100.     playlist_Clear( p_playlist, pl_Unlocked );
  101.     vlclua_release_playlist_internal( p_playlist );
  102.     return 0;
  103. }
  104. static int vlclua_playlist_repeat( lua_State * L )
  105. {
  106.     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
  107.     int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "repeat" );
  108.     vlclua_release_playlist_internal( p_playlist );
  109.     return i_ret;
  110. }
  111. static int vlclua_playlist_loop( lua_State * L )
  112. {
  113.     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
  114.     int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "loop" );
  115.     vlclua_release_playlist_internal( p_playlist );
  116.     return i_ret;
  117. }
  118. static int vlclua_playlist_random( lua_State * L )
  119. {
  120.     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
  121.     int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "random" );
  122.     vlclua_release_playlist_internal( p_playlist );
  123.     return i_ret;
  124. }
  125. static int vlclua_playlist_goto( lua_State * L )
  126. {
  127.     int i_id = luaL_checkint( L, 1 );
  128.     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
  129.     PL_LOCK;
  130.     int i_ret = playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
  131.                                   true, NULL,
  132.                                   playlist_ItemGetById( p_playlist, i_id ) );
  133.     PL_UNLOCK;
  134.     vlclua_release_playlist_internal( p_playlist );
  135.     return vlclua_push_ret( L, i_ret );
  136. }
  137. static int vlclua_playlist_add( lua_State *L )
  138. {
  139.     int i_count;
  140.     vlc_object_t *p_this = vlclua_get_this( L );
  141.     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
  142.     i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
  143.                                             NULL, true );
  144.     vlclua_release_playlist_internal( p_playlist );
  145.     lua_pushinteger( L, i_count );
  146.     return 1;
  147. }
  148. static int vlclua_playlist_enqueue( lua_State *L )
  149. {
  150.     int i_count;
  151.     vlc_object_t *p_this = vlclua_get_this( L );
  152.     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
  153.     i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
  154.                                             NULL, false );
  155.     vlclua_release_playlist_internal( p_playlist );
  156.     lua_pushinteger( L, i_count );
  157.     return 1;
  158. }
  159. static void push_playlist_item( lua_State *L, playlist_item_t *p_item );
  160. static void push_playlist_item( lua_State *L, playlist_item_t *p_item )
  161. {
  162.     input_item_t *p_input = p_item->p_input;
  163.     int i_flags = 0;
  164.     i_flags = p_item->i_flags;
  165.     lua_newtable( L );
  166.     lua_pushinteger( L, p_item->i_id );
  167.     lua_setfield( L, -2, "id" );
  168.     lua_newtable( L );
  169. #define CHECK_AND_SET_FLAG( name, label ) 
  170.     if( i_flags & PLAYLIST_ ## name ## _FLAG ) 
  171.     { 
  172.         lua_pushboolean( L, 1 ); 
  173.         lua_setfield( L, -2, #label ); 
  174.     }
  175.     CHECK_AND_SET_FLAG( SAVE, save )
  176.     CHECK_AND_SET_FLAG( SKIP, skip )
  177.     CHECK_AND_SET_FLAG( DBL, disabled )
  178.     CHECK_AND_SET_FLAG( RO, ro )
  179.     CHECK_AND_SET_FLAG( REMOVE, remove )
  180.     CHECK_AND_SET_FLAG( EXPANDED, expanded )
  181. #undef CHECK_AND_SET_FLAG
  182.     lua_setfield( L, -2, "flags" );
  183.     if( p_input )
  184.     {
  185.         lua_pushstring( L, p_input->psz_name );
  186.         lua_setfield( L, -2, "name" );
  187.         lua_pushstring( L, p_input->psz_uri );
  188.         lua_setfield( L, -2, "path" );
  189.         if( p_input->i_duration < 0 )
  190.             lua_pushnumber( L, -1 );
  191.         else
  192.             lua_pushnumber( L, ((double)p_input->i_duration)*1e-6 );
  193.         lua_setfield( L, -2, "duration" );
  194.         lua_pushinteger( L, p_input->i_nb_played );
  195.         lua_setfield( L, -2, "nb_played" );
  196.         /* TODO: add (optional) info categories, meta, options, es */
  197.     }
  198.     if( p_item->i_children >= 0 )
  199.     {
  200.         int i;
  201.         lua_createtable( L, p_item->i_children, 0 );
  202.         for( i = 0; i < p_item->i_children; i++ )
  203.         {
  204.             push_playlist_item( L, p_item->pp_children[i] );
  205.             lua_rawseti( L, -2, i+1 );
  206.         }
  207.         lua_setfield( L, -2, "children" );
  208.     }
  209. }
  210. static int vlclua_playlist_get( lua_State *L )
  211. {
  212.     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
  213.     PL_LOCK;
  214.     int b_category = luaL_optboolean( L, 2, 1 ); /* Default to tree playlist (discared when 1st argument is a playlist_item's id) */
  215.     playlist_item_t *p_item = NULL;
  216.     if( lua_isnumber( L, 1 ) )
  217.     {
  218.         int i_id = lua_tointeger( L, 1 );
  219.         p_item = playlist_ItemGetById( p_playlist, i_id );
  220.         if( !p_item )
  221.         {
  222.             PL_UNLOCK;
  223.             vlclua_release_playlist_internal( p_playlist );
  224.             return 0; /* Should we return an error instead? */
  225.         }
  226.     }
  227.     else if( lua_isstring( L, 1 ) )
  228.     {
  229.         const char *psz_what = lua_tostring( L, 1 );
  230.         if( !strcasecmp( psz_what, "normal" )
  231.          || !strcasecmp( psz_what, "playlist" ) )
  232.             p_item = b_category ? p_playlist->p_local_category
  233.                                 : p_playlist->p_local_onelevel;
  234.         else if( !strcasecmp( psz_what, "ml" )
  235.               || !strcasecmp( psz_what, "media library" ) )
  236.             p_item = b_category ? p_playlist->p_ml_category
  237.                                 : p_playlist->p_ml_onelevel;
  238.         else if( !strcasecmp( psz_what, "root" ) )
  239.             p_item = b_category ? p_playlist->p_root_category
  240.                                 : p_playlist->p_root_onelevel;
  241.         else
  242.         {
  243. #ifdef FIX_THAT_CODE_NOT_TO_MESS_WITH_PLAYLIST_INTERNALS
  244.             for( int i = 0; i < p_playlist->i_sds; i++ )
  245.             {
  246.                 if( !strcasecmp( psz_what,
  247.                                  p_playlist->pp_sds[i]->p_sd->psz_module ) )
  248.                 {
  249.                     p_item = b_category ? p_playlist->pp_sds[i]->p_cat
  250.                                         : p_playlist->pp_sds[i]->p_one;
  251.                     break;
  252.                 }
  253.             }
  254. #else
  255. # warning "Don't access playlist iternal, broken code here."
  256.             abort();
  257. #endif
  258.             if( !p_item )
  259.             {
  260.                 PL_UNLOCK;
  261.                 vlclua_release_playlist_internal( p_playlist );
  262.                 return 0; /* Should we return an error instead? */
  263.             }
  264.         }
  265.     }
  266.     else
  267.     {
  268.         p_item = b_category ? p_playlist->p_root_category
  269.                             : p_playlist->p_root_onelevel;
  270.     }
  271.     push_playlist_item( L, p_item );
  272.     PL_UNLOCK;
  273.     vlclua_release_playlist_internal( p_playlist );
  274.     return 1;
  275. }
  276. static int vlclua_playlist_search( lua_State *L )
  277. {
  278.     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
  279.     const char *psz_string = luaL_optstring( L, 1, "" );
  280.     int b_category = luaL_optboolean( L, 2, 1 ); /* default to category */
  281.     playlist_item_t *p_item = b_category ? p_playlist->p_root_category
  282.                                          : p_playlist->p_root_onelevel;
  283.     PL_LOCK;
  284.     playlist_LiveSearchUpdate( p_playlist, p_item, psz_string );
  285.     PL_UNLOCK;
  286.     push_playlist_item( L, p_item );
  287.     vlclua_release_playlist_internal( p_playlist );
  288.     return 1;
  289. }
  290. static int vlclua_playlist_current( lua_State *L )
  291. {
  292.     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
  293.     lua_pushinteger( L, var_GetInteger( p_playlist, "item-current" ) );
  294.     vlclua_release_playlist_internal( p_playlist );
  295.     return 1;
  296. }
  297. static int vlc_sort_key_from_string( const char *psz_name )
  298. {
  299.     static const struct
  300.     {
  301.         const char *psz_name;
  302.         int i_key;
  303.     } pp_keys[] =
  304.         { { "id", SORT_ID },
  305.           { "title", SORT_TITLE },
  306.           { "title nodes first", SORT_TITLE_NODES_FIRST },
  307.           { "artist", SORT_ARTIST },
  308.           { "genre", SORT_GENRE },
  309.           { "random", SORT_RANDOM },
  310.           { "duration", SORT_DURATION },
  311.           { "title numeric", SORT_TITLE_NUMERIC },
  312.           { "album", SORT_ALBUM },
  313.           { NULL, -1 } };
  314.     int i;
  315.     for( i = 0; pp_keys[i].psz_name; i++ )
  316.     {
  317.         if( !strcmp( psz_name, pp_keys[i].psz_name ) )
  318.             return pp_keys[i].i_key;
  319.     }
  320.     return -1;
  321. }
  322. static int vlclua_playlist_sort( lua_State *L )
  323. {
  324.     /* allow setting the different sort keys */
  325.     int i_mode = vlc_sort_key_from_string( luaL_checkstring( L, 1 ) );
  326.     if( i_mode == -1 )
  327.         return luaL_error( L, "Invalid search key." );
  328.     int i_type = luaL_optboolean( L, 2, 0 ) ? ORDER_REVERSE : ORDER_NORMAL;
  329.     int b_category = luaL_optboolean( L, 3, 1 ); /* default to category */
  330.     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
  331.     PL_LOCK;
  332.     playlist_item_t *p_root = b_category ? p_playlist->p_local_category
  333.                                          : p_playlist->p_local_onelevel;
  334.     int i_ret = playlist_RecursiveNodeSort( p_playlist, p_root, i_mode,
  335.                                             i_type );
  336.     PL_UNLOCK;
  337.     vlclua_release_playlist_internal( p_playlist );
  338.     return vlclua_push_ret( L, i_ret );
  339. }
  340. /* FIXME: split this in 3 different functions? */
  341. static int vlclua_playlist_status( lua_State *L )
  342. {
  343.     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
  344.     /*
  345.     int i_count = 0;
  346.     lua_settop( L, 0 );*/
  347.     input_thread_t * p_input = playlist_CurrentInput( p_playlist );
  348.     if( p_input )
  349.     {
  350.         /*char *psz_uri =
  351.             input_item_GetURI( input_GetItem( p_playlist->p_input ) );
  352.         lua_pushstring( L, psz_uri );
  353.         free( psz_uri );
  354.         lua_pushnumber( L, config_GetInt( p_intf, "volume" ) );*/
  355.         PL_LOCK;
  356.         switch( playlist_Status( p_playlist ) )
  357.         {
  358.             case PLAYLIST_STOPPED:
  359.                 lua_pushstring( L, "stopped" );
  360.                 break;
  361.             case PLAYLIST_RUNNING:
  362.                 lua_pushstring( L, "playing" );
  363.                 break;
  364.             case PLAYLIST_PAUSED:
  365.                 lua_pushstring( L, "paused" );
  366.                 break;
  367.             default:
  368.                 lua_pushstring( L, "unknown" );
  369.                 break;
  370.         }
  371.         PL_UNLOCK;
  372.         /*i_count += 3;*/
  373.         vlc_object_release( p_input );
  374.     }
  375.     else
  376.     {
  377.         lua_pushstring( L, "stopped" );
  378.     }
  379.     vlclua_release_playlist_internal( p_playlist );
  380.     return 1;
  381. }
  382. /*****************************************************************************
  383.  *
  384.  *****************************************************************************/
  385. static const luaL_Reg vlclua_playlist_reg[] = {
  386.     { "prev", vlclua_playlist_prev },
  387.     { "next", vlclua_playlist_next },
  388.     { "skip", vlclua_playlist_skip },
  389.     { "play", vlclua_playlist_play },
  390.     { "pause", vlclua_playlist_pause },
  391.     { "stop", vlclua_playlist_stop },
  392.     { "clear", vlclua_playlist_clear },
  393.     { "repeat", vlclua_playlist_repeat },
  394.     { "loop", vlclua_playlist_loop },
  395.     { "random", vlclua_playlist_random },
  396.     { "goto", vlclua_playlist_goto },
  397.     { "add", vlclua_playlist_add },
  398.     { "enqueue", vlclua_playlist_enqueue },
  399.     { "get", vlclua_playlist_get },
  400.     { "search", vlclua_playlist_search },
  401.     { "current", vlclua_playlist_current },
  402.     { "sort", vlclua_playlist_sort },
  403.     { "status", vlclua_playlist_status },
  404.     { NULL, NULL }
  405. };
  406. void luaopen_playlist( lua_State *L )
  407. {
  408.     lua_newtable( L );
  409.     luaL_register( L, NULL, vlclua_playlist_reg );
  410.     lua_setfield( L, -2, "playlist" );
  411. }