⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rc.c

📁 VLC Player Source Code
💻 C
📖 第 1 页 / 共 5 页
字号:
/***************************************************************************** * rc.c : remote control stdin/stdout module for vlc ***************************************************************************** * Copyright (C) 2004-2007 the VideoLAN team * $Id: 5e45c61a6272ef15f651812a5e85d39c30ae68a5 $ * * Author: Peter Surda <shurdeek@panorama.sth.ac.at> *         Jean-Paul Saman <jpsaman #_at_# m2x _replaceWith#dot_ nl> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************//***************************************************************************** * Preamble *****************************************************************************/#ifdef HAVE_CONFIG_H# include "config.h"#endif#include <vlc_common.h>#include <vlc_plugin.h>#include <errno.h>                                                 /* ENOMEM */#include <ctype.h>#include <signal.h>#include <vlc_interface.h>#include <vlc_aout.h>#include <vlc_vout.h>#include <vlc_osd.h>#include <vlc_playlist.h>#ifdef HAVE_UNISTD_H#    include <unistd.h>#endif#ifdef HAVE_SYS_TIME_H#    include <sys/time.h>#endif#include <sys/types.h>#include <vlc_network.h>#include "vlc_url.h"#include <vlc_charset.h>#if defined(AF_UNIX) && !defined(AF_LOCAL)#    define AF_LOCAL AF_UNIX#endif#if defined(AF_LOCAL) && ! defined(WIN32)#    include <sys/un.h>#endif#define MAX_LINE_LENGTH 256#define STATUS_CHANGE "status change: "/* input_state_e from <vlc_input.h> */static const char *ppsz_input_state[] = {    N_("Initializing"),    N_("Opening"),    N_("Buffer"),    N_("Play"),    N_("Pause"),    N_("Stop"),    N_("Forward"),    N_("Backward"),    N_("End"),    N_("Error"),};/***************************************************************************** * Local prototypes *****************************************************************************/static int  Activate     ( vlc_object_t * );static void Deactivate   ( vlc_object_t * );static void Run          ( intf_thread_t * );static void Help         ( intf_thread_t *, bool );static void RegisterCallbacks( intf_thread_t * );static bool ReadCommand( intf_thread_t *, char *, int * );static input_item_t *parse_MRL( intf_thread_t *, char * );static int  Input        ( vlc_object_t *, char const *,                           vlc_value_t, vlc_value_t, void * );static int  Playlist     ( vlc_object_t *, char const *,                           vlc_value_t, vlc_value_t, void * );static int  Quit         ( vlc_object_t *, char const *,                           vlc_value_t, vlc_value_t, void * );static int  Intf         ( vlc_object_t *, char const *,                           vlc_value_t, vlc_value_t, void * );static int  Volume       ( vlc_object_t *, char const *,                           vlc_value_t, vlc_value_t, void * );static int  VolumeMove   ( vlc_object_t *, char const *,                           vlc_value_t, vlc_value_t, void * );static int  VideoConfig  ( vlc_object_t *, char const *,                           vlc_value_t, vlc_value_t, void * );static int  AudioConfig  ( vlc_object_t *, char const *,                           vlc_value_t, vlc_value_t, void * );static int  Menu         ( vlc_object_t *, char const *,                           vlc_value_t, vlc_value_t, void * );static int  Statistics   ( vlc_object_t *, char const *,                           vlc_value_t, vlc_value_t, void * );static int updateStatistics( intf_thread_t *, input_item_t *);/* Status Callbacks */static int TimeOffsetChanged( vlc_object_t *, char const *,                              vlc_value_t, vlc_value_t , void * );static int VolumeChanged    ( vlc_object_t *, char const *,                              vlc_value_t, vlc_value_t, void * );static int StateChanged     ( vlc_object_t *, char const *,                              vlc_value_t, vlc_value_t, void * );static int RateChanged      ( vlc_object_t *, char const *,                              vlc_value_t, vlc_value_t, void * );struct intf_sys_t{    int *pi_socket_listen;    int i_socket;    char *psz_unix_path;    /* status changes */    vlc_mutex_t       status_lock;    playlist_status_t i_last_state;#ifdef WIN32    HANDLE hConsoleIn;    bool b_quiet;#endif};#define msg_rc( ... ) __msg_rc( p_intf, __VA_ARGS__ )static void __msg_rc( intf_thread_t *p_intf, const char *psz_fmt, ... ){    va_list args;    va_start( args, psz_fmt );    if( p_intf->p_sys->i_socket == -1 )    {        utf8_vfprintf( stdout, psz_fmt, args );        printf( "\r\n" );    }    else    {        net_vaPrintf( p_intf, p_intf->p_sys->i_socket, NULL, psz_fmt, args );        net_Write( p_intf, p_intf->p_sys->i_socket, NULL, (uint8_t*)"\r\n", 2 );    }    va_end( args );}/***************************************************************************** * Module descriptor *****************************************************************************/#define POS_TEXT N_("Show stream position")#define POS_LONGTEXT N_("Show the current position in seconds within the " \                        "stream from time to time." )#define TTY_TEXT N_("Fake TTY")#define TTY_LONGTEXT N_("Force the rc module to use stdin as if it was a TTY.")#define UNIX_TEXT N_("UNIX socket command input")#define UNIX_LONGTEXT N_("Accept commands over a Unix socket rather than " \                         "stdin." )#define HOST_TEXT N_("TCP command input")#define HOST_LONGTEXT N_("Accept commands over a socket rather than stdin. " \            "You can set the address and port the interface will bind to." )#ifdef WIN32#define QUIET_TEXT N_("Do not open a DOS command box interface")#define QUIET_LONGTEXT N_( \    "By default the rc interface plugin will start a DOS command box. " \    "Enabling the quiet mode will not bring this command box but can also " \    "be pretty annoying when you want to stop VLC and no video window is " \    "open." )#endifvlc_module_begin();    set_shortname( N_("RC"));    set_category( CAT_INTERFACE );    set_subcategory( SUBCAT_INTERFACE_MAIN );    set_description( N_("Remote control interface") );    add_bool( "rc-show-pos", 0, NULL, POS_TEXT, POS_LONGTEXT, true );#ifdef WIN32    add_bool( "rc-quiet", 0, NULL, QUIET_TEXT, QUIET_LONGTEXT, false );#else#if defined (HAVE_ISATTY)    add_bool( "rc-fake-tty", 0, NULL, TTY_TEXT, TTY_LONGTEXT, true );#endif    add_string( "rc-unix", 0, NULL, UNIX_TEXT, UNIX_LONGTEXT, true );#endif    add_string( "rc-host", 0, NULL, HOST_TEXT, HOST_LONGTEXT, true );    set_capability( "interface", 20 );    set_callbacks( Activate, Deactivate );vlc_module_end();/***************************************************************************** * Activate: initialize and create stuff *****************************************************************************/static int Activate( vlc_object_t *p_this ){    intf_thread_t *p_intf = (intf_thread_t*)p_this;    char *psz_host, *psz_unix_path;    int  *pi_socket = NULL;#ifndef WIN32#if defined(HAVE_ISATTY)    /* Check that stdin is a TTY */    if( !config_GetInt( p_intf, "rc-fake-tty" ) && !isatty( 0 ) )    {        msg_Warn( p_intf, "fd 0 is not a TTY" );        return VLC_EGENERIC;    }#endif    psz_unix_path = config_GetPsz( p_intf, "rc-unix" );    if( psz_unix_path )    {        int i_socket;#ifndef AF_LOCAL        msg_Warn( p_intf, "your OS doesn't support filesystem sockets" );        free( psz_unix_path );        return VLC_EGENERIC;#else        struct sockaddr_un addr;        memset( &addr, 0, sizeof(struct sockaddr_un) );        msg_Dbg( p_intf, "trying UNIX socket" );        if( (i_socket = socket( AF_LOCAL, SOCK_STREAM, 0 ) ) < 0 )        {            msg_Warn( p_intf, "can't open socket: %m" );            free( psz_unix_path );            return VLC_EGENERIC;        }        addr.sun_family = AF_LOCAL;        strncpy( addr.sun_path, psz_unix_path, sizeof( addr.sun_path ) );        addr.sun_path[sizeof( addr.sun_path ) - 1] = '\0';        if (bind (i_socket, (struct sockaddr *)&addr, sizeof (addr))         && (errno == EADDRINUSE)         && connect (i_socket, (struct sockaddr *)&addr, sizeof (addr))         && (errno == ECONNREFUSED))        {            msg_Info (p_intf, "Removing dead UNIX socket: %s", psz_unix_path);            unlink (psz_unix_path);            if (bind (i_socket, (struct sockaddr *)&addr, sizeof (addr)))            {                msg_Err (p_intf, "cannot bind UNIX socket at %s: %m",                         psz_unix_path);                free (psz_unix_path);                net_Close (i_socket);                return VLC_EGENERIC;            }        }        if( listen( i_socket, 1 ) )        {            msg_Warn( p_intf, "can't listen on socket: %m");            free( psz_unix_path );            net_Close( i_socket );            return VLC_EGENERIC;        }        /* FIXME: we need a core function to merge listening sockets sets */        pi_socket = calloc( 2, sizeof( int ) );        if( pi_socket == NULL )        {            free( psz_unix_path );            net_Close( i_socket );            return VLC_ENOMEM;        }        pi_socket[0] = i_socket;        pi_socket[1] = -1;#endif /* AF_LOCAL */    }#endif /* !WIN32 */    if( ( pi_socket == NULL ) &&        ( psz_host = config_GetPsz( p_intf, "rc-host" ) ) != NULL )    {        vlc_url_t url;        vlc_UrlParse( &url, psz_host, 0 );        msg_Dbg( p_intf, "base: %s, port: %d", url.psz_host, url.i_port );        pi_socket = net_ListenTCP(p_this, url.psz_host, url.i_port);        if( pi_socket == NULL )        {            msg_Warn( p_intf, "can't listen to %s port %i",                      url.psz_host, url.i_port );            vlc_UrlClean( &url );            free( psz_host );            return VLC_EGENERIC;        }        vlc_UrlClean( &url );        free( psz_host );    }    p_intf->p_sys = malloc( sizeof( intf_sys_t ) );    if( !p_intf->p_sys )        return VLC_ENOMEM;    p_intf->p_sys->pi_socket_listen = pi_socket;    p_intf->p_sys->i_socket = -1;    p_intf->p_sys->psz_unix_path = psz_unix_path;    vlc_mutex_init( &p_intf->p_sys->status_lock );    p_intf->p_sys->i_last_state = PLAYLIST_STOPPED;    /* Non-buffered stdout */    setvbuf( stdout, (char *)NULL, _IOLBF, 0 );    p_intf->pf_run = Run;#ifdef WIN32    p_intf->p_sys->b_quiet = config_GetInt( p_intf, "rc-quiet" );    if( !p_intf->p_sys->b_quiet ) { CONSOLE_INTRO_MSG; }#else    CONSOLE_INTRO_MSG;#endif    msg_rc( _("Remote control interface initialized. Type `help' for help.") );    return VLC_SUCCESS;}/***************************************************************************** * Deactivate: uninitialize and cleanup *****************************************************************************/static void Deactivate( vlc_object_t *p_this ){    intf_thread_t *p_intf = (intf_thread_t*)p_this;    net_ListenClose( p_intf->p_sys->pi_socket_listen );    if( p_intf->p_sys->i_socket != -1 )        net_Close( p_intf->p_sys->i_socket );    if( p_intf->p_sys->psz_unix_path != NULL )    {#if defined(AF_LOCAL) && !defined(WIN32)        unlink( p_intf->p_sys->psz_unix_path );#endif        free( p_intf->p_sys->psz_unix_path );    }    vlc_mutex_destroy( &p_intf->p_sys->status_lock );    free( p_intf->p_sys );}/***************************************************************************** * RegisterCallbacks: Register callbacks to dynamic variables *****************************************************************************/static void RegisterCallbacks( intf_thread_t *p_intf ){    /* Register commands that will be cleaned up upon object destruction */#define ADD( name, type, target )                                   \    var_Create( p_intf, name, VLC_VAR_ ## type | VLC_VAR_ISCOMMAND ); \    var_AddCallback( p_intf, name, target, NULL );    ADD( "quit", VOID, Quit )    ADD( "intf", STRING, Intf )    ADD( "add", STRING, Playlist )    ADD( "repeat", STRING, Playlist )    ADD( "loop", STRING, Playlist )    ADD( "random", STRING, Playlist )    ADD( "enqueue", STRING, Playlist )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -