mux:ts: convert vlc_tick_t to seconds explicitly using SEC_FROM_VLC_TICK()
[vlc.git] / modules / access / vnc.c
blobc6a8ae426a03e225e4f67429c149d19b3478806f
1 /*****************************************************************************
2 * vnc.c: libVNC access
3 *****************************************************************************
4 * Copyright (C) 2013 VideoLAN and VLC Authors
5 *****************************************************************************
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20 *****************************************************************************/
22 /*****************************************************************************
23 * NOTA BENE: this module requires the linking against a library which is
24 * known to require licensing under the GNU General Public License version 2
25 * (or later). Therefore, the result of compiling this module will normally
26 * be subject to the terms of that later license.
27 *****************************************************************************/
29 /*****************************************************************************
30 * Preamble
31 *****************************************************************************/
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_demux.h>
40 #include <vlc_url.h>
41 #include <vlc_meta.h>
42 #include <vlc_fourcc.h>
44 #include <rfb/rfbclient.h>
46 #define RFB_USER N_("Username")
47 #define RFB_PASSWORD N_("Password")
48 #define RFB_CA_TEXT N_("X.509 Certificate Authority")
49 #define RFB_CA_LONGTEXT N_("Certificate of the Authority to verify server's against")
50 #define RFB_CRL_TEXT N_("X.509 Certificate Revocation List")
51 #define RFB_CRL_LONGTEXT N_("List of revoked servers certificates")
52 #define RFB_CERT_TEXT N_("X.509 Client certificate")
53 #define RFB_CERT_LONGTEXT N_("Certificate for client authentication")
54 #define RFB_KEY_TEXT N_("X.509 Client private key")
55 #define RFB_KEY_LONGTEXT N_("Private key for authentication by certificate")
57 #define RFB_CHROMA N_("Frame buffer depth")
58 #define RFB_CHROMA_LONGTEXT N_("RGB chroma (RV32, RV24, RV16, RGB2)")
59 #define RFB_FPS N_("Frame rate")
60 #define RFB_FPS_LONGTEXT N_("How many times the screen content should be refreshed per second.")
61 #define RFB_COMPRESS N_("Compression level")
62 #define RFB_COMPRESS_LONGTEXT N_("Transfer compression level from 0 (none) to 9 (max)")
63 #define RFB_QUALITY N_("Image quality")
64 #define RFB_QUALITY_LONGTEXT N_("Image quality 1 to 9 (max)")
66 #define CFG_PREFIX "rfb-"
68 const char *const rgb_chromas[] = { N_("32 bits"), N_("24 bits"), N_("16 bits"), N_("8 bits") };
69 const char *const rgb_chromas_v[] = { "RV32", "RV24", "RV16", "RGB8" };
71 /*****************************************************************************
72 * Module descriptor
73 *****************************************************************************/
74 static int Open ( vlc_object_t * );
75 static void Close( vlc_object_t * );
77 vlc_module_begin()
78 set_shortname( N_("VNC") )
79 add_shortcut( "vnc" )
80 set_category( CAT_INPUT )
81 set_subcategory( SUBCAT_INPUT_ACCESS )
82 set_description( N_("VNC client access") )
83 set_capability( "access", 0 )
85 add_string( CFG_PREFIX "user", NULL, RFB_USER, RFB_USER, false )
86 change_safe()
87 add_password(CFG_PREFIX "password", NULL, RFB_PASSWORD, RFB_PASSWORD)
88 change_safe()
89 add_loadfile(CFG_PREFIX "x509-ca", NULL, RFB_CA_TEXT, RFB_CA_LONGTEXT)
90 change_safe()
91 add_loadfile(CFG_PREFIX "x509-crl", NULL, RFB_CRL_TEXT, RFB_CRL_LONGTEXT)
92 change_safe()
93 add_loadfile(CFG_PREFIX "x509-client-cert", NULL,
94 RFB_CERT_TEXT, RFB_CERT_LONGTEXT)
95 change_safe()
96 add_loadfile(CFG_PREFIX "x509-client-key", NULL,
97 RFB_KEY_TEXT, RFB_KEY_LONGTEXT)
98 change_safe()
100 add_float( CFG_PREFIX "fps", 5, RFB_FPS, RFB_FPS_LONGTEXT, true )
101 add_string( CFG_PREFIX "chroma", rgb_chromas_v[0], RFB_CHROMA, RFB_CHROMA_LONGTEXT, false )
102 change_string_list (rgb_chromas_v, rgb_chromas)
103 change_safe()
104 add_integer( CFG_PREFIX "compress-level", 0, RFB_COMPRESS, RFB_COMPRESS_LONGTEXT, true )
105 change_integer_range (0, 9)
106 change_safe()
107 add_integer( CFG_PREFIX "quality-level", 9, RFB_QUALITY, RFB_QUALITY_LONGTEXT, true )
108 change_integer_range (1, 9)
109 change_safe()
111 set_callbacks( Open, Close )
112 vlc_module_end()
114 typedef struct
116 vlc_thread_t thread;
117 int i_cancel_state;
119 rfbClient* p_client;
120 int i_framebuffersize;
121 block_t *p_block;
123 float f_fps;
124 int i_frame_interval;
125 vlc_tick_t i_starttime;
127 es_out_id_t *es;
128 } demux_sys_t;
130 static void *DemuxThread( void *p_data );
132 /*****************************************************************************
133 * Local prototypes
134 *****************************************************************************/
136 static rfbBool mallocFrameBufferHandler( rfbClient* p_client )
138 vlc_fourcc_t i_chroma;
139 demux_t *p_demux = (demux_t *) rfbClientGetClientData( p_client, DemuxThread );
140 demux_sys_t *p_sys = p_demux->p_sys;
142 if ( p_sys->es ) /* Source has changed resolution */
144 es_out_Del( p_demux->out, p_sys->es );
145 p_sys->es = NULL;
148 int i_width = p_client->width;
149 int i_height = p_client->height;
150 int i_depth = p_client->format.bitsPerPixel;
152 switch( i_depth )
154 case 8:
155 i_chroma = VLC_CODEC_RGB8;
156 break;
157 default:
158 case 16:
159 i_chroma = VLC_CODEC_RGB16;
160 break;
161 case 24:
162 i_chroma = VLC_CODEC_RGB24;
163 break;
164 case 32:
165 i_chroma = VLC_CODEC_RGB32;
166 break;
169 if ( i_chroma != VLC_CODEC_RGB8 ) /* Palette based, no mask */
171 video_format_t videofmt;
172 video_format_Init( &videofmt, i_chroma );
173 video_format_FixRgb( &videofmt );
175 p_client->format.redShift = videofmt.i_lrshift;
176 p_client->format.greenShift = videofmt.i_lgshift;
177 p_client->format.blueShift = videofmt.i_lbshift;
178 p_client->format.redMax = videofmt.i_rmask >> videofmt.i_lrshift;
179 p_client->format.greenMax = videofmt.i_gmask >> videofmt.i_lgshift;
180 p_client->format.blueMax = videofmt.i_bmask >> videofmt.i_lbshift;
181 video_format_Clean( &videofmt );
184 /* Set up framebuffer */
185 p_sys->i_framebuffersize = i_width * i_height * i_depth / 8;
187 /* Reuse unsent block */
188 if ( p_sys->p_block )
189 p_sys->p_block = block_Realloc( p_sys->p_block, 0, p_sys->i_framebuffersize );
190 else
191 p_sys->p_block = block_Alloc( p_sys->i_framebuffersize );
193 if ( p_sys->p_block )
194 p_sys->p_block->i_buffer = p_sys->i_framebuffersize;
195 else
196 return FALSE;
198 /* Push our VNC config */
199 SetFormatAndEncodings( p_client );
201 /* Now init and fill es format */
202 es_format_t fmt;
203 es_format_Init( &fmt, VIDEO_ES, i_chroma );
205 /* Fill input format */
206 fmt.video.i_chroma = i_chroma;
207 fmt.video.i_visible_width =
208 fmt.video.i_width = i_width;
210 fmt.video.i_visible_height =
211 fmt.video.i_height = i_height;
213 fmt.video.i_frame_rate_base = 1000;
214 fmt.video.i_frame_rate = 1000 * p_sys->f_fps;
216 fmt.video.i_bits_per_pixel = i_depth;
217 fmt.video.i_rmask = p_client->format.redMax << p_client->format.redShift;
218 fmt.video.i_gmask = p_client->format.greenMax << p_client->format.greenShift;
219 fmt.video.i_bmask = p_client->format.blueMax << p_client->format.blueShift;
221 fmt.video.i_sar_num = fmt.video.i_sar_den = 1;
223 /* declare the new es */
224 p_sys->es = es_out_Add( p_demux->out, &fmt );
226 return TRUE;
229 /* Auth */
230 static char *getPasswordHandler( rfbClient *p_client )
232 demux_t *p_demux = (demux_t *) rfbClientGetClientData( p_client, DemuxThread );
233 /* freed by libvnc */
234 return var_InheritString( p_demux, CFG_PREFIX "password" );
237 static rfbCredential* getCredentialHandler( rfbClient *p_client, int i_credentialType )
239 demux_t *p_demux = (demux_t *) rfbClientGetClientData( p_client, DemuxThread );
241 rfbCredential *credential = calloc( 1, sizeof(rfbCredential) );
242 if ( !credential ) return NULL;
244 switch( i_credentialType )
246 case rfbCredentialTypeX509:
247 /* X509None, X509Vnc, X509Plain */
248 credential->x509Credential.x509CACertFile =
249 var_InheritString( p_demux, CFG_PREFIX "x509-ca" );
250 credential->x509Credential.x509CACrlFile =
251 var_InheritString( p_demux, CFG_PREFIX "x509-crl" );
252 /* client auth by certificate */
253 credential->x509Credential.x509ClientCertFile =
254 var_InheritString( p_demux, CFG_PREFIX "x509-client-cert" );
255 credential->x509Credential.x509ClientKeyFile =
256 var_InheritString( p_demux, CFG_PREFIX "x509-client-key" );
257 break;
259 case rfbCredentialTypeUser:
260 credential->userCredential.username =
261 var_InheritString( p_demux, CFG_PREFIX "user" );
262 credential->userCredential.password =
263 var_InheritString( p_demux, CFG_PREFIX "password" );
264 break;
266 default:
267 free( credential );
268 return NULL; /* Unsupported Auth */
270 /* freed by libvnc */
271 return credential;
274 /*****************************************************************************
275 * Control:
276 *****************************************************************************/
277 static int Control( demux_t *p_demux, int i_query, va_list args )
279 demux_sys_t *p_sys = p_demux->p_sys;
280 bool *pb;
281 int64_t *pi64;
282 double *p_dbl;
283 vlc_meta_t *p_meta;
285 switch( i_query )
287 case DEMUX_CAN_PAUSE:
288 case DEMUX_CAN_SEEK:
289 case DEMUX_CAN_CONTROL_PACE:
290 case DEMUX_CAN_CONTROL_RATE:
291 case DEMUX_HAS_UNSUPPORTED_META:
292 pb = va_arg( args, bool * );
293 *pb = false;
294 return VLC_SUCCESS;
296 case DEMUX_CAN_RECORD:
297 pb = va_arg( args, bool * );
298 *pb = true;
299 return VLC_SUCCESS;
301 case DEMUX_GET_PTS_DELAY:
302 pi64 = va_arg( args, int64_t * );
303 *pi64 = INT64_C(1000)
304 * var_InheritInteger( p_demux, "network-caching" );
305 return VLC_SUCCESS;
307 case DEMUX_GET_TIME:
308 pi64 = va_arg( args, int64_t * );
309 *pi64 = vlc_tick_now() - p_sys->i_starttime;
310 return VLC_SUCCESS;
312 case DEMUX_GET_LENGTH:
313 pi64 = va_arg( args, int64_t * );
314 *pi64 = 0;
315 return VLC_SUCCESS;
317 case DEMUX_GET_FPS:
318 p_dbl = va_arg( args, double * );
319 *p_dbl = p_sys->f_fps;
320 return VLC_SUCCESS;
322 case DEMUX_GET_META:
323 p_meta = va_arg( args, vlc_meta_t * );
324 vlc_meta_Set( p_meta, vlc_meta_Title, p_demux->psz_location );
325 return VLC_SUCCESS;
327 default:
328 return VLC_EGENERIC;
332 /*****************************************************************************
333 * Demux:
334 *****************************************************************************/
336 static void *DemuxThread( void *p_data )
338 demux_t *p_demux = (demux_t *) p_data;
339 demux_sys_t *p_sys = p_demux->p_sys;
340 vlc_tick_t i_next_frame_date = vlc_tick_now() + p_sys->i_frame_interval;
341 int i_status;
343 for(;;)
345 p_sys->i_cancel_state = vlc_savecancel();
346 i_status = WaitForMessage( p_sys->p_client, p_sys->i_frame_interval );
347 vlc_restorecancel( p_sys->i_cancel_state );
349 /* Ensure we're not building frames too fast */
350 /* as WaitForMessage takes only a maximum wait */
351 vlc_tick_wait( i_next_frame_date );
352 i_next_frame_date += p_sys->i_frame_interval;
354 if ( i_status > 0 )
356 p_sys->p_client->frameBuffer = p_sys->p_block->p_buffer;
357 p_sys->i_cancel_state = vlc_savecancel();
358 i_status = HandleRFBServerMessage( p_sys->p_client );
359 vlc_restorecancel( p_sys->i_cancel_state );
360 if ( ! i_status )
362 msg_Warn( p_demux, "Cannot get announced data. Server closed ?" );
363 es_out_Del( p_demux->out, p_sys->es );
364 p_sys->es = NULL;
365 return NULL;
367 else
369 block_t *p_block = block_Duplicate( p_sys->p_block );
370 if ( p_block ) /* drop frame/content if no next block */
372 p_sys->p_block->i_dts = p_sys->p_block->i_pts = vlc_tick_now();
373 es_out_SetPCR( p_demux->out, p_sys->p_block->i_pts );
374 es_out_Send( p_demux->out, p_sys->es, p_sys->p_block );
375 p_sys->p_block = p_block;
380 return NULL;
383 /*****************************************************************************
384 * Open:
385 *****************************************************************************/
386 static int Open( vlc_object_t *p_this )
388 demux_t *p_demux = (demux_t*)p_this;
389 demux_sys_t *p_sys;
391 if (p_demux->out == NULL)
392 return VLC_EGENERIC;
394 p_sys = vlc_obj_calloc( p_this, 1, sizeof(demux_sys_t) );
395 if( !p_sys ) return VLC_ENOMEM;
397 p_sys->f_fps = var_InheritFloat( p_demux, CFG_PREFIX "fps" );
398 if ( p_sys->f_fps <= 0 ) p_sys->f_fps = 1.0;
399 p_sys->i_frame_interval = CLOCK_FREQ / p_sys->f_fps ;
401 char *psz_chroma = var_InheritString( p_demux, CFG_PREFIX "chroma" );
402 vlc_fourcc_t i_chroma = vlc_fourcc_GetCodecFromString( VIDEO_ES, psz_chroma );
403 free( psz_chroma );
404 if ( !i_chroma || vlc_fourcc_IsYUV( i_chroma ) )
406 msg_Err( p_demux, "Only RGB chroma are supported" );
407 return VLC_EGENERIC;
410 const vlc_chroma_description_t *p_chroma_desc = vlc_fourcc_GetChromaDescription( i_chroma );
411 if ( !p_chroma_desc )
413 msg_Err( p_demux, "Unable to get RGB chroma description" );
414 return VLC_EGENERIC;
417 #ifdef NDEBUG
418 rfbEnableClientLogging = FALSE;
419 #endif
421 p_sys->p_client = rfbGetClient( p_chroma_desc->pixel_bits / 3, // bitsPerSample
422 3, // samplesPerPixel
423 p_chroma_desc->pixel_size ); // bytesPerPixel
424 if ( ! p_sys->p_client )
426 msg_Dbg( p_demux, "Unable to set up client for %s",
427 vlc_fourcc_GetDescription( VIDEO_ES, i_chroma ) );
428 return VLC_EGENERIC;
431 msg_Dbg( p_demux, "set up client for %s %d %d %d",
432 vlc_fourcc_GetDescription( VIDEO_ES, i_chroma ),
433 p_chroma_desc->pixel_bits / 3, 3, p_chroma_desc->pixel_size );
435 p_sys->p_client->MallocFrameBuffer = mallocFrameBufferHandler;
436 p_sys->p_client->canHandleNewFBSize = TRUE;
437 p_sys->p_client->GetCredential = getCredentialHandler;
438 p_sys->p_client->GetPassword = getPasswordHandler; /* VNC simple auth */
440 /* Set compression and quality levels */
441 p_sys->p_client->appData.compressLevel =
442 var_InheritInteger( p_demux, CFG_PREFIX "compress-level" );
443 p_sys->p_client->appData.qualityLevel =
444 var_InheritInteger( p_demux, CFG_PREFIX "quality-level" );
446 /* Parse uri params */
447 vlc_url_t url;
448 vlc_UrlParse( &url, p_demux->psz_location );
450 if ( !EMPTY_STR(url.psz_host) )
451 p_sys->p_client->serverHost = strdup( url.psz_host );
452 else
453 p_sys->p_client->serverHost = strdup( "localhost" );
455 p_sys->p_client->appData.viewOnly = TRUE;
456 p_sys->p_client->serverPort = ( url.i_port > 0 ) ? url.i_port : 5900;
458 msg_Dbg( p_demux, "VNC init %s host=%s port=%d",
459 p_demux->psz_location,
460 p_sys->p_client->serverHost,
461 p_sys->p_client->serverPort );
463 vlc_UrlClean( &url );
465 /* make demux available for callback handlers */
466 rfbClientSetClientData( p_sys->p_client, DemuxThread, p_demux );
467 p_demux->p_sys = p_sys;
469 if( !rfbInitClient( p_sys->p_client, NULL, NULL ) )
471 msg_Err( p_demux, "can't connect to RFB server" );
472 return VLC_EGENERIC;
475 p_sys->i_starttime = vlc_tick_now();
477 if ( vlc_clone( &p_sys->thread, DemuxThread, p_demux, VLC_THREAD_PRIORITY_INPUT ) != VLC_SUCCESS )
479 msg_Err( p_demux, "can't spawn thread" );
480 return VLC_EGENERIC;
483 p_demux->pf_demux = NULL;
484 p_demux->pf_control = Control;
486 return VLC_SUCCESS;
489 /*****************************************************************************
490 * Close:
491 *****************************************************************************/
492 static void Close( vlc_object_t *p_this )
494 demux_t *p_demux = (demux_t*)p_this;
495 demux_sys_t *p_sys = p_demux->p_sys;
497 vlc_cancel( p_sys->thread );
498 vlc_join( p_sys->thread, NULL );
500 if ( p_sys->es )
501 es_out_Del( p_demux->out, p_sys->es );
503 rfbClientCleanup( p_sys->p_client );
505 if ( p_sys->p_block )
506 block_Release( p_sys->p_block );