[vlc-devel] [PATCH] access: add rfb/vnc viewonly module.

Rémi Denis-Courmont remi at remlab.net
Thu Feb 21 19:32:25 CET 2013


Le jeudi 21 février 2013 15:37:59, Francois Cartegnie a écrit :
> Uses libvnc to provide 8-32bpp viewer, compression control, rfb & x509
> auth, desktop resize handling and vnc://host:port uri.
> 
> The fake+8bpp remoteosd filter trick being not sufficient and accessible to
> average user.

Yes. Please remove it in a separate patch.

> Francois
> 
> ---
>  configure.ac              |    5 +
>  modules/access/Modules.am |    2 +
>  modules/access/vnc.c      |  454
> +++++++++++++++++++++++++++++++++++++++++++++ po/POTFILES.in            | 
>   1 +
>  4 files changed, 462 insertions(+), 0 deletions(-)
>  create mode 100644 modules/access/vnc.c
> 
> diff --git a/configure.ac b/configure.ac
> index ec2f60b..8a85495 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -1894,6 +1894,11 @@ if test "${enable_screen}" != "no"; then
>  fi
> 
>  dnl
> +dnl  VNC/RFB access module
> +dnl
> +PKG_ENABLE_MODULES_VLC([LIBVNC], [libvnc], [libvncclient >= 0.9.9],
> (VNC/rfb client support (default enabled)), [auto])

auto != enabled

> +
> +dnl
>  dnl  Real RTSP plugin
>  dnl
>  AC_ARG_ENABLE(realrtsp,
> diff --git a/modules/access/Modules.am b/modules/access/Modules.am
> index 64f5dbf..d7f55c3 100644
> --- a/modules/access/Modules.am
> +++ b/modules/access/Modules.am
> @@ -186,6 +186,8 @@ libscreen_plugin_la_LDFLAGS = $(AM_LDFLAGS)
>  libscreen_plugin_la_LDFLAGS +=
> "-Wl,-framework,OpenGL,-framework,ApplicationServices" endif
> 
> +### VNC ###
> +SOURCES_libvnc = vnc.c
> 
>  ### Optical media ###
> 
> diff --git a/modules/access/vnc.c b/modules/access/vnc.c
> new file mode 100644
> index 0000000..0280d0f
> --- /dev/null
> +++ b/modules/access/vnc.c
> @@ -0,0 +1,454 @@
> +/*************************************************************************
> **** + * vnc.c: libVNC access
> +
> **************************************************************************
> *** + * Copyright (C) 2013 VideoLAN Authors
> +
> **************************************************************************
> *** + *
> + * This program is free software; you can redistribute it and/or modify it
> + * under the terms of the GNU Lesser General Public License as published
> by + * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser 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 <vlc_demux.h>
> +#include <vlc_url.h>
> +#include <vlc_meta.h>
> +#include <vlc_fourcc.h>
> +
> +#include <rfb/rfbclient.h>
> +
> +#define RFB_USER N_("RFB auth username")
> +#define RFB_PASSWORD N_("RFB auth password")
> +#define RFB_PASSWORD_LONGTEXT N_("RFB Password (single password for VNC
> auth)") +#define RFB_CHROMA N_("RGB chroma")
> +#define RFB_CHROMA_LONGTEXT N_("RGB chroma (RV32, RV24, RV16, RGB2)")
> +#define RFB_FPS N_("Frame rate")
> +#define RFB_FPS_LONGTEXT N_("Acquisition rate (in fps)")
> +#define RFB_COMPRESS N_("Compression level")
> +#define RFB_COMPRESS_LONGTEXT N_("Transfert compression level from 0
> (none) to 9 (max)") +#define RFB_QUALITY N_("Image quality")
> +#define RFB_QUALITY_LONGTEXT N_("Image quality 1 to 9 (max)")
> +
> +#define DEMUX_TAG (void *)0x1

This is wrong and will cause a warning on 64-bits platforms.

> +#define CFG_PREFIX "rfb-"
> +
> +const char *const rgb_chromas[] = { N_("32 bits RGB"), N_("24 bits RGB"),
> N_("16 bits RGB"), N_("8 bits RGB") };

Please use the same strings as shm.c. Spare the translators.

> +const char *const rgb_chromas_v[]
> = { "RV32", "RV24", "RV16", "RGB2" };
> +
> +/*************************************************************************
> **** + * Module descriptor
> +
> **************************************************************************
> ***/ +static int  Open ( vlc_object_t * );
> +static void Close( vlc_object_t * );
> +
> +vlc_module_begin()
> +    set_shortname( N_("VNC") )
> +    add_shortcut( "vnc" )
> +    set_category( CAT_INPUT )
> +    set_subcategory( SUBCAT_INPUT_ACCESS )
> +    set_description( N_("VNC client access") )
> +    set_capability( "access_demux", 10 )

Priority is 0 for scheme-based selection.

> +
> +    add_string( CFG_PREFIX "user", NULL, RFB_USER, RFB_USER, false )
> +        change_safe()
> +    add_string( CFG_PREFIX "password", NULL, RFB_PASSWORD,
> RFB_PASSWORD_LONGTEXT, false )
> +        change_safe()

add_password

> +    add_integer( CFG_PREFIX "fps", 5, RFB_FPS, RFB_FPS_LONGTEXT, true )
> +        change_safe()

FPS is either a fraction(string) or a float. This should not be safe, as there 
are no sane bounds on the value.

> +    add_string( CFG_PREFIX "chroma", rgb_chromas_v[0], RFB_CHROMA,
> RFB_CHROMA_LONGTEXT, false )
> +        change_string_list (rgb_chromas_v, rgb_chromas)
> +        change_safe()
> +    add_integer( CFG_PREFIX "compress-level", 0, RFB_COMPRESS,
> RFB_COMPRESS_LONGTEXT, true )
> +        change_integer_range (0, 9)
> +        change_safe()
> +    add_integer( CFG_PREFIX "quality-level", 9, RFB_QUALITY,
> RFB_QUALITY_LONGTEXT, true )
> +        change_integer_range (1, 9)
> +        change_safe()
> +
> +    set_callbacks( Open, Close )
> +vlc_module_end()
> +
> +struct demux_sys_t
> +{
> +    rfbClient* p_client;
> +    rfbCredential credential;
> +    int i_framebuffersize;
> +    int i_stride;

Please remove write-only values.
Please use automatic variables where possible.

> +
> +    float f_fps;
> +    int i_frame_interval;
> +    mtime_t i_starttime;

This is not needed. Worse, it will prevent synchronizing with live capture 
slave inputs.

> +
> +    es_format_t fmt;
> +    es_out_id_t *es;
> +
> +    char *psz_hostname;
> +    int i_port;
> +};
> +
> +/*************************************************************************
> **** + * Local prototypes
> +
> **************************************************************************
> ***/ +
> +static rfbBool mallocFrameBufferHandler( rfbClient* p_client )
> +{
> +    vlc_fourcc_t i_chroma;
> +    demux_t *p_demux = (demux_t *) rfbClientGetClientData( p_client,
> DEMUX_TAG );
> +    demux_sys_t *p_sys = p_demux->p_sys;
> +
> +    if ( p_sys->es ) /* Source has changed resolution */
> +    {
> +        es_out_Del( p_demux->out, p_sys->es );
> +        p_sys->es = NULL;
> +        FREENULL( p_client->frameBuffer );
> +    }
> +
> +
> +    int i_width = p_client->width;
> +    int i_height = p_client->height;
> +    int i_depth = p_client->format.bitsPerPixel;
> +
> +    switch( i_depth )
> +    {
> +        case 8:
> +            i_chroma = VLC_CODEC_RGB8;
> +            break;
> +        default:
> +        case 16:
> +            i_chroma = VLC_CODEC_RGB16;
> +            break;
> +        case 24:
> +            i_chroma = VLC_CODEC_RGB24;
> +            break;
> +        case 32:
> +            i_chroma = VLC_CODEC_RGB32;
> +            break;
> +    }
> +
> +
> +    if ( i_chroma != VLC_CODEC_RGB8 ) /* Palette based, no mask */
> +    {
> +        video_format_t fmt;
> +        memset( &fmt, 0, sizeof(video_format_t) );
> +        fmt.i_chroma = i_chroma;
> +        video_format_FixRgb( &fmt );
> +
> +        p_client->format.redShift = fmt.i_lrshift;
> +        p_client->format.greenShift = fmt.i_lgshift;
> +        p_client->format.blueShift = fmt.i_lbshift;
> +        p_client->format.redMax = fmt.i_rmask >> fmt.i_lrshift;
> +        p_client->format.greenMax = fmt.i_gmask >> fmt.i_lgshift;
> +        p_client->format.blueMax = fmt.i_bmask >> fmt.i_lbshift;
> +    }
> +
> +    /* Push our own RGB config */
> +    SetFormatAndEncodings( p_client );
> +
> +    /* Set up framebuffer */
> +    p_sys->i_framebuffersize = i_width * i_height * i_depth / 8;
> +    p_sys->i_stride = i_width;
> +    p_client->frameBuffer = calloc( p_sys->i_framebuffersize,
> sizeof(uint8_t) ); +
> +    /* Now init and fill es format */
> +    es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
> +
> +    /* Fill input format */
> +    p_sys->fmt.video.i_chroma = i_chroma;
> +    p_sys->fmt.video.i_visible_width =
> +            p_sys->fmt.video.i_width = i_width;
> +
> +    p_sys->fmt.video.i_visible_height =
> +            p_sys->fmt.video.i_height = i_height;
> +
> +    p_sys->fmt.video.i_frame_rate_base = 1000;
> +    p_sys->fmt.video.i_frame_rate = 1000 * p_sys->f_fps;
> +
> +    p_sys->fmt.video.i_bits_per_pixel = i_depth;
> +    p_sys->fmt.video.i_rmask = p_client->format.redMax <<
> p_client->format.redShift;
> +    p_sys->fmt.video.i_gmask =
> p_client->format.greenMax << p_client->format.greenShift; +   
> p_sys->fmt.video.i_bmask = p_client->format.blueMax <<
> p_client->format.blueShift;
> +
> +    p_sys->fmt.video.i_sar_num = p_sys->fmt.video.i_sar_den = 1;
> +
> +    /* declare the new es */
> +    p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
> +
> +    return TRUE;
> +}
> +
> +/* Auth */
> +static char *getPasswordHandler( rfbClient *p_client )
> +{
> +    demux_t *p_demux = (demux_t *) rfbClientGetClientData( p_client,
> DEMUX_TAG );
> +    return var_InheritString( p_demux, CFG_PREFIX "password"

Looks leaky.

> );
> +}
> +
> +static rfbCredential* getCredentialHandler( rfbClient *p_client, int
> i_credentialType )
> +{
> +    demux_t *p_demux = (demux_t *) rfbClientGetClientData( p_client,
> DEMUX_TAG );
> +
> +    rfbCredential *credential = calloc( 1, sizeof(rfbCredential) );
> +    if ( !credential ) return NULL;
> +
> +    switch( i_credentialType )
> +    {
> +        case rfbCredentialTypeX509:
> +            credential->x509Credential.x509CACertFile =
> +                    var_InheritString( p_demux, "http-ca" );
> +            credential->x509Credential.x509ClientCertFile =
> +                    var_InheritString( p_demux, "http-cert" );
> +            credential->x509Credential.x509ClientKeyFile =
> +                    var_InheritString( p_demux, "http-key" );
> +            credential->x509Credential.x509CACrlFile =
> +                    var_InheritString( p_demux, "http-crl" );
> +            break;
> +
> +        case rfbCredentialTypeUser:
> +            credential->userCredential.username =
> +                    var_InheritString( p_demux, CFG_PREFIX "user" );
> +            credential->userCredential.password =
> +                    var_InheritString( p_demux, CFG_PREFIX "password" );
> +            break;
> +
> +        default:
> +            free( credential );
> +            return NULL; /* Unsupported Auth */
> +    }
> +
> +    return credential;

Looks very leaky too.

> +}
> +
> +/*************************************************************************
> **** + * Control:
> +
> **************************************************************************
> ***/ +static int Control( demux_t *p_demux, int i_query, va_list args ) +{
> +    bool *pb;
> +    int64_t *pi64;
> +    double *p_dbl;
> +    vlc_meta_t *p_meta;
> +
> +    switch( i_query )
> +    {
> +        case DEMUX_CAN_PAUSE:
> +        case DEMUX_CAN_SEEK:
> +        case DEMUX_CAN_CONTROL_PACE:
> +        case DEMUX_CAN_CONTROL_RATE:
> +        case DEMUX_HAS_UNSUPPORTED_META:
> +            pb = (bool*)va_arg( args, bool * );
> +            *pb = false;
> +            return VLC_SUCCESS;
> +
> +        case DEMUX_CAN_RECORD:
> +            pb = (bool*)va_arg( args, bool * );
> +            *pb = true;
> +            return VLC_SUCCESS;
> +
> +        case DEMUX_GET_PTS_DELAY:
> +            pi64 = (int64_t*)va_arg( args, int64_t * );
> +            *pi64 = INT64_C(1000)
> +                  * var_InheritInteger( p_demux, "live-caching" );

This is network, not live.

> +            return VLC_SUCCESS;
> +
> +        case DEMUX_GET_TIME:
> +            pi64 = (int64_t*)va_arg( args, int64_t * );
> +            *pi64 = mdate() - p_demux->p_sys->i_starttime;
> +            return VLC_SUCCESS;
> +
> +        case DEMUX_GET_LENGTH:
> +            pi64 = (int64_t*)va_arg( args, int64_t * );
> +            *pi64 = 0;
> +            return VLC_SUCCESS;
> +
> +        case DEMUX_GET_FPS:
> +            p_dbl = (double*)va_arg( args, double * );
> +            *p_dbl = p_demux->p_sys->f_fps;
> +            return VLC_SUCCESS;
> +
> +        case DEMUX_GET_META:
> +            p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
> +            vlc_meta_Set( p_meta, vlc_meta_Title, p_demux->psz_location );
> +            return VLC_SUCCESS;
> +
> +        default:
> +            return VLC_EGENERIC;
> +    }
> +}
> +
> +/*************************************************************************
> **** + * Demux:
> +
> **************************************************************************
> ***/ +static int Demux( demux_t *p_demux )
> +{
> +    demux_sys_t *p_sys = p_demux->p_sys;
> +    block_t *p_block;
> +
> +    if ( p_sys->i_starttime == 0 ) p_sys->i_starttime = mdate();
> +
> +    if ( WaitForMessage( p_sys->p_client, p_sys->i_frame_interval / 2 ) >
> 0 )
> +    {
> +        if ( ! HandleRFBServerMessage( p_sys->p_client ) )
> +        {
> +            msg_Warn( p_demux, "cannot get announced data" );
> +            return 0;
> +        }
> +    }

For the umpteenth time. Please don't do arbitrary wakeups.

If you need to sleep in an access_demux, use your own thread (e.g. V4L2).

> +
> +    if( !( p_block = block_Alloc( p_sys->i_framebuffersize ) ) )
> +        return 0; // == EOF
> +
> +    memcpy( p_block->p_buffer, p_sys->p_client->frameBuffer,
> p_sys->i_framebuffersize ); +
> +    p_block->i_buffer = p_sys->i_framebuffersize;
> +    p_block->i_dts = p_block->i_pts = mdate() - p_sys->i_starttime;
> +
> +    es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
> +    es_out_Send( p_demux->out, p_sys->es, p_block );
> +
> +    return 1;
> +}
> +
> +/*************************************************************************
> **** + * Open:
> +
> **************************************************************************
> ***/ +static int Open( vlc_object_t *p_this )
> +{
> +    demux_t      *p_demux = (demux_t*)p_this;
> +    demux_sys_t  *p_sys;
> +
> +    if( strncmp( p_demux->psz_access, "vnc", 3 ) != 0 )
> +        return VLC_EGENERIC;

Pointless.

> +
> +    p_sys = calloc( 1, sizeof(demux_sys_t) );
> +    if( !p_sys ) return VLC_ENOMEM;
> +
> +    p_sys->f_fps = var_InheritInteger( p_demux, CFG_PREFIX "fps" );
> +    p_sys->i_frame_interval = 1000000 / p_sys->f_fps;

SIGFPE.

> +
> +    char *psz_chroma = var_InheritString( p_demux, CFG_PREFIX "chroma" );
> +    vlc_fourcc_t i_chroma = vlc_fourcc_GetCodecFromString( VIDEO_ES,
> psz_chroma );
> +    free( psz_chroma );
> +    if ( !i_chroma || vlc_fourcc_IsYUV( i_chroma ) )
> +    {
> +        msg_Err( p_demux, "Only RGB chroma are supported" );
> +        free( p_sys );
> +        return VLC_EGENERIC;
> +    }
> +
> +    const vlc_chroma_description_t *p_chroma_desc =
> vlc_fourcc_GetChromaDescription( i_chroma );
> +    if ( !p_chroma_desc )
> +    {
> +        msg_Err( p_demux, "Unable to get RGB chroma description" );
> +        free( p_sys );
> +        return VLC_EGENERIC;
> +    }
> +
> +    p_sys->p_client = rfbGetClient( p_chroma_desc->pixel_bits / 3, //
> bitsPerSample
> +                                    3, // samplesPerPixel
> +                                    p_chroma_desc->pixel_size ); //
> bytesPerPixel
> +    if ( ! p_sys->p_client )
> +    {
> +        msg_Dbg( p_demux, "Unable to set up client for %s",
> +                 vlc_fourcc_GetDescription( VIDEO_ES, i_chroma ) );
> +        free( p_sys );
> +        return VLC_EGENERIC;
> +    }
> +
> +    msg_Dbg( p_demux, "set up client for %s %d %d %d",
> +             vlc_fourcc_GetDescription( VIDEO_ES, i_chroma ),
> +             p_chroma_desc->pixel_bits / 3, 3, p_chroma_desc->pixel_size
> );
> +
> +    p_sys->p_client->MallocFrameBuffer = mallocFrameBufferHandler;

Most probably the whole point of this callback is to avoid raw picture memory 
copying. Please avoid memory copying then.

> +    p_sys->p_client->canHandleNewFBSize = TRUE;
> +    p_sys->p_client->GetCredential = getCredentialHandler;
> +    p_sys->p_client->GetPassword = getPasswordHandler; /* VNC simple auth
> */
> +
> +    /* Set compression and quality levels */
> +    p_sys->p_client->appData.compressLevel =
> +            var_InheritInteger( p_demux, CFG_PREFIX "compress-level" );
> +    p_sys->p_client->appData.qualityLevel =
> +            var_InheritInteger( p_demux, CFG_PREFIX "quality-level" );
> +
> +#ifdef NDEBUG
> +    rfbEnableClientLogging = FALSE;
> +#endif
> +
> +    /* Parse uri params */
> +    vlc_url_t url;
> +    vlc_UrlParse( &url, p_demux->psz_location, 0 );
> +
> +    if ( !EMPTY_STR(url.psz_host) )
> +        p_sys->psz_hostname = strdup( url.psz_host );
> +    else
> +        p_sys->psz_hostname = strdup( "localhost" );
> +
> +    p_sys->i_port = ( url.i_port > 0 ) ? url.i_port : 5900;
> +
> +    vlc_UrlClean( &url );
> +
> +    p_sys->p_client->appData.viewOnly = TRUE;
> +    p_sys->p_client->serverHost = strdup( p_sys->psz_hostname );
> +    p_sys->p_client->serverPort = p_sys->i_port;
> +
> +    msg_Dbg( p_demux, "VNC init %s host=%s pass=%s port=%d",
> +             p_demux->psz_location,
> +             p_sys->psz_hostname,
> +             url.psz_password,
> +             p_sys->i_port );
> +
> +    /* make demux available for callback handlers */
> +    rfbClientSetClientData( p_sys->p_client, DEMUX_TAG, p_demux );
> +    p_demux->p_sys = p_sys;
> +
> +    if( !rfbInitClient( p_sys->p_client, NULL, NULL ) )
> +    {
> +        msg_Err( p_demux, "rfbInitClient error" );
> +        free( p_sys->psz_hostname );
> +        free( p_sys );
> +        p_demux->p_sys = NULL;

This is not needed because p_sys is a write-only field w.r.t. Open().

> +        return VLC_EGENERIC;
> +    }
> +
> +    p_demux->pf_demux = Demux;
> +    p_demux->pf_control = Control;
> +
> +    return VLC_SUCCESS;
> +}
> +
> +/*************************************************************************
> ****
> + * Close:
> +
> **************************************************************************
> ***/
> +static void Close( vlc_object_t *p_this )
> +{
> +    demux_t     *p_demux = (demux_t*)p_this;
> +    demux_sys_t *p_sys = p_demux->p_sys;
> +    uint8_t* frameBuffer = p_sys->p_client->frameBuffer;
> +
> +    if ( p_sys->es )
> +        es_out_Del( p_demux->out, p_sys->es );
> +
> +    rfbClientCleanup( p_sys->p_client );
> +    free( frameBuffer );
> +
> +    free( p_sys->psz_hostname );
> +    free( p_sys );
> +}
> diff --git a/po/POTFILES.in b/po/POTFILES.in
> index 1762f1f..376976d 100644
> --- a/po/POTFILES.in
> +++ b/po/POTFILES.in
> @@ -275,6 +275,7 @@ modules/access/vcdx/vcd.h
>  modules/access/vcdx/vcdplayer.c
>  modules/access/vcdx/vcdplayer.h
>  modules/access/vdr.c
> +modules/access/vnc.c
>  modules/access/zip/zipstream.c
>  modules/arm_neon/chroma_yuv.c
>  modules/arm_neon/simple_channel_mixer.c

-- 
Rémi Denis-Courmont
http://www.remlab.net/



More information about the vlc-devel mailing list