]> git.sesse.net Git - vlc/blob - modules/access/vnc.c
vpx: fix leak
[vlc] / modules / access / vnc.c
1 /*****************************************************************************
2  * vnc.c: libVNC access
3  *****************************************************************************
4  * Copyright (C) 2013 VideoLAN Authors
5  *****************************************************************************
6  *
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.
11  *
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.
16  *
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  *****************************************************************************/
21
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  *****************************************************************************/
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
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>
43
44 #include <rfb/rfbclient.h>
45
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")
56
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)")
65
66 #define CFG_PREFIX "rfb-"
67
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" };
70
71 /*****************************************************************************
72  * Module descriptor
73  *****************************************************************************/
74 static int  Open ( vlc_object_t * );
75 static void Close( vlc_object_t * );
76
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_demux", 0 )
84
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, false )
88         change_safe()
89     add_loadfile( CFG_PREFIX "x509-ca", NULL, RFB_CA_TEXT, RFB_CA_LONGTEXT, true )
90         change_safe()
91     add_loadfile( CFG_PREFIX "x509-crl", NULL, RFB_CRL_TEXT, RFB_CRL_LONGTEXT, true )
92         change_safe()
93     add_loadfile( CFG_PREFIX "x509-client-cert", NULL, RFB_CERT_TEXT, RFB_CERT_LONGTEXT, true )
94         change_safe()
95     add_loadfile( CFG_PREFIX "x509-client-key", NULL, RFB_KEY_TEXT, RFB_KEY_LONGTEXT, true )
96         change_safe()
97
98     add_float( CFG_PREFIX "fps", 5, RFB_FPS, RFB_FPS_LONGTEXT, true )
99     add_string( CFG_PREFIX "chroma", rgb_chromas_v[0], RFB_CHROMA, RFB_CHROMA_LONGTEXT, false )
100         change_string_list (rgb_chromas_v, rgb_chromas)
101         change_safe()
102     add_integer( CFG_PREFIX "compress-level", 0, RFB_COMPRESS, RFB_COMPRESS_LONGTEXT, true )
103         change_integer_range (0, 9)
104         change_safe()
105     add_integer( CFG_PREFIX "quality-level", 9, RFB_QUALITY, RFB_QUALITY_LONGTEXT, true )
106         change_integer_range (1, 9)
107         change_safe()
108
109     set_callbacks( Open, Close )
110 vlc_module_end()
111
112 struct demux_sys_t
113 {
114     vlc_thread_t thread;
115     int i_cancel_state;
116
117     rfbClient* p_client;
118     int i_framebuffersize;
119     block_t *p_block;
120
121     float f_fps;
122     int i_frame_interval;
123     mtime_t i_starttime;
124
125     es_out_id_t *es;
126 };
127
128 static void *DemuxThread( void *p_data );
129
130 /*****************************************************************************
131  * Local prototypes
132  *****************************************************************************/
133
134 static rfbBool mallocFrameBufferHandler( rfbClient* p_client )
135 {
136     vlc_fourcc_t i_chroma;
137     demux_t *p_demux = (demux_t *) rfbClientGetClientData( p_client, DemuxThread );
138     demux_sys_t *p_sys = p_demux->p_sys;
139
140     if ( p_sys->es ) /* Source has changed resolution */
141     {
142         es_out_Del( p_demux->out, p_sys->es );
143         p_sys->es = NULL;
144     }
145
146     int i_width = p_client->width;
147     int i_height = p_client->height;
148     int i_depth = p_client->format.bitsPerPixel;
149
150     switch( i_depth )
151     {
152         case 8:
153             i_chroma = VLC_CODEC_RGB8;
154             break;
155         default:
156         case 16:
157             i_chroma = VLC_CODEC_RGB16;
158             break;
159         case 24:
160             i_chroma = VLC_CODEC_RGB24;
161             break;
162         case 32:
163             i_chroma = VLC_CODEC_RGB32;
164             break;
165     }
166
167     if ( i_chroma != VLC_CODEC_RGB8 ) /* Palette based, no mask */
168     {
169         video_format_t videofmt;
170         memset( &videofmt, 0, sizeof(video_format_t) );
171         videofmt.i_chroma = i_chroma;
172         video_format_FixRgb( &videofmt );
173
174         p_client->format.redShift = videofmt.i_lrshift;
175         p_client->format.greenShift = videofmt.i_lgshift;
176         p_client->format.blueShift = videofmt.i_lbshift;
177         p_client->format.redMax = videofmt.i_rmask >> videofmt.i_lrshift;
178         p_client->format.greenMax = videofmt.i_gmask >> videofmt.i_lgshift;
179         p_client->format.blueMax = videofmt.i_bmask >> videofmt.i_lbshift;
180     }
181
182     /* Set up framebuffer */
183     p_sys->i_framebuffersize = i_width * i_height * i_depth / 8;
184
185     /* Reuse unsent block */
186     if ( p_sys->p_block )
187         p_sys->p_block = block_Realloc( p_sys->p_block, 0, p_sys->i_framebuffersize );
188     else
189         p_sys->p_block = block_Alloc( p_sys->i_framebuffersize );
190
191     if ( p_sys->p_block )
192         p_sys->p_block->i_buffer = p_sys->i_framebuffersize;
193     else
194         return FALSE;
195
196     /* Push our VNC config */
197     SetFormatAndEncodings( p_client );
198
199     /* Now init and fill es format */
200     es_format_t fmt;
201     es_format_Init( &fmt, VIDEO_ES, i_chroma );
202
203     /* Fill input format */
204     fmt.video.i_chroma = i_chroma;
205     fmt.video.i_visible_width =
206             fmt.video.i_width = i_width;
207
208     fmt.video.i_visible_height =
209             fmt.video.i_height = i_height;
210
211     fmt.video.i_frame_rate_base = 1000;
212     fmt.video.i_frame_rate = 1000 * p_sys->f_fps;
213
214     fmt.video.i_bits_per_pixel = i_depth;
215     fmt.video.i_rmask = p_client->format.redMax << p_client->format.redShift;
216     fmt.video.i_gmask = p_client->format.greenMax << p_client->format.greenShift;
217     fmt.video.i_bmask = p_client->format.blueMax << p_client->format.blueShift;
218
219     fmt.video.i_sar_num = fmt.video.i_sar_den = 1;
220
221     /* declare the new es */
222     p_sys->es = es_out_Add( p_demux->out, &fmt );
223
224     return TRUE;
225 }
226
227 /* Auth */
228 static char *getPasswordHandler( rfbClient *p_client )
229 {
230     demux_t *p_demux = (demux_t *) rfbClientGetClientData( p_client, DemuxThread );
231     /* freed by libvnc */
232     return var_InheritString( p_demux, CFG_PREFIX "password" );
233 }
234
235 static rfbCredential* getCredentialHandler( rfbClient *p_client, int i_credentialType )
236 {
237     demux_t *p_demux = (demux_t *) rfbClientGetClientData( p_client, DemuxThread );
238
239     rfbCredential *credential = calloc( 1, sizeof(rfbCredential) );
240     if ( !credential ) return NULL;
241
242     switch( i_credentialType )
243     {
244         case rfbCredentialTypeX509:
245             /* X509None, X509Vnc, X509Plain */
246             credential->x509Credential.x509CACertFile =
247                     var_InheritString( p_demux, CFG_PREFIX "x509-ca" );
248             credential->x509Credential.x509CACrlFile =
249                     var_InheritString( p_demux, CFG_PREFIX "x509-crl" );
250             /* client auth by certificate */
251             credential->x509Credential.x509ClientCertFile =
252                     var_InheritString( p_demux, CFG_PREFIX "x509-client-cert" );
253             credential->x509Credential.x509ClientKeyFile =
254                     var_InheritString( p_demux, CFG_PREFIX "x509-client-key" );
255             break;
256
257         case rfbCredentialTypeUser:
258             credential->userCredential.username =
259                     var_InheritString( p_demux, CFG_PREFIX "user" );
260             credential->userCredential.password =
261                     var_InheritString( p_demux, CFG_PREFIX "password" );
262             break;
263
264         default:
265             free( credential );
266             return NULL; /* Unsupported Auth */
267     }
268     /* freed by libvnc */
269     return credential;
270 }
271
272 /*****************************************************************************
273  * Control:
274  *****************************************************************************/
275 static int Control( demux_t *p_demux, int i_query, va_list args )
276 {
277     bool *pb;
278     int64_t *pi64;
279     double *p_dbl;
280     vlc_meta_t *p_meta;
281
282     switch( i_query )
283     {
284         case DEMUX_CAN_PAUSE:
285         case DEMUX_CAN_SEEK:
286         case DEMUX_CAN_CONTROL_PACE:
287         case DEMUX_CAN_CONTROL_RATE:
288         case DEMUX_HAS_UNSUPPORTED_META:
289             pb = (bool*)va_arg( args, bool * );
290             *pb = false;
291             return VLC_SUCCESS;
292
293         case DEMUX_CAN_RECORD:
294             pb = (bool*)va_arg( args, bool * );
295             *pb = true;
296             return VLC_SUCCESS;
297
298         case DEMUX_GET_PTS_DELAY:
299             pi64 = (int64_t*)va_arg( args, int64_t * );
300             *pi64 = INT64_C(1000)
301                   * var_InheritInteger( p_demux, "network-caching" );
302             return VLC_SUCCESS;
303
304         case DEMUX_GET_TIME:
305             pi64 = (int64_t*)va_arg( args, int64_t * );
306             *pi64 = mdate() - p_demux->p_sys->i_starttime;
307             return VLC_SUCCESS;
308
309         case DEMUX_GET_LENGTH:
310             pi64 = (int64_t*)va_arg( args, int64_t * );
311             *pi64 = 0;
312             return VLC_SUCCESS;
313
314         case DEMUX_GET_FPS:
315             p_dbl = (double*)va_arg( args, double * );
316             *p_dbl = p_demux->p_sys->f_fps;
317             return VLC_SUCCESS;
318
319         case DEMUX_GET_META:
320             p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
321             vlc_meta_Set( p_meta, vlc_meta_Title, p_demux->psz_location );
322             return VLC_SUCCESS;
323
324         default:
325             return VLC_EGENERIC;
326     }
327 }
328
329 /*****************************************************************************
330  * Demux:
331  *****************************************************************************/
332
333 static void *DemuxThread( void *p_data )
334 {
335     demux_t *p_demux = (demux_t *) p_data;
336     demux_sys_t  *p_sys = p_demux->p_sys;
337     mtime_t i_next_frame_date = mdate() + p_sys->i_frame_interval;
338     int i_status;
339
340     for(;;)
341     {
342         p_sys->i_cancel_state = vlc_savecancel();
343         i_status = WaitForMessage( p_sys->p_client, p_sys->i_frame_interval );
344         vlc_restorecancel( p_sys->i_cancel_state );
345
346         /* Ensure we're not building frames too fast */
347         /* as WaitForMessage takes only a maximum wait */
348         mwait( i_next_frame_date );
349         i_next_frame_date += p_sys->i_frame_interval;
350
351         if ( i_status > 0 )
352         {
353             p_sys->p_client->frameBuffer = p_sys->p_block->p_buffer;
354             p_sys->i_cancel_state = vlc_savecancel();
355             i_status = HandleRFBServerMessage( p_sys->p_client );
356             vlc_restorecancel( p_sys->i_cancel_state );
357             if ( ! i_status )
358             {
359                 msg_Warn( p_demux, "Cannot get announced data. Server closed ?" );
360                 es_out_Del( p_demux->out, p_sys->es );
361                 p_sys->es = NULL;
362                 return NULL;
363             }
364             else
365             {
366                 block_t *p_block = block_Duplicate( p_sys->p_block );
367                 if ( p_block ) /* drop frame/content if no next block */
368                 {
369                     p_sys->p_block->i_dts = p_sys->p_block->i_pts = mdate();
370                     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->p_block->i_pts );
371                     es_out_Send( p_demux->out, p_sys->es, p_sys->p_block );
372                     p_sys->p_block = p_block;
373                 }
374             }
375         }
376     }
377     return NULL;
378 }
379
380 /*****************************************************************************
381  * Open:
382  *****************************************************************************/
383 static int Open( vlc_object_t *p_this )
384 {
385     demux_t      *p_demux = (demux_t*)p_this;
386     demux_sys_t  *p_sys;
387
388     p_sys = calloc( 1, sizeof(demux_sys_t) );
389     if( !p_sys ) return VLC_ENOMEM;
390
391     p_sys->f_fps = var_InheritFloat( p_demux, CFG_PREFIX "fps" );
392     if ( p_sys->f_fps <= 0 ) p_sys->f_fps = 1.0;
393     p_sys->i_frame_interval = 1000000 / p_sys->f_fps ;
394
395     char *psz_chroma = var_InheritString( p_demux, CFG_PREFIX "chroma" );
396     vlc_fourcc_t i_chroma = vlc_fourcc_GetCodecFromString( VIDEO_ES, psz_chroma );
397     free( psz_chroma );
398     if ( !i_chroma || vlc_fourcc_IsYUV( i_chroma ) )
399     {
400         msg_Err( p_demux, "Only RGB chroma are supported" );
401         free( p_sys );
402         return VLC_EGENERIC;
403     }
404
405     const vlc_chroma_description_t *p_chroma_desc = vlc_fourcc_GetChromaDescription( i_chroma );
406     if ( !p_chroma_desc )
407     {
408         msg_Err( p_demux, "Unable to get RGB chroma description" );
409         free( p_sys );
410         return VLC_EGENERIC;
411     }
412
413 #ifdef NDEBUG
414     rfbEnableClientLogging = FALSE;
415 #endif
416
417     p_sys->p_client = rfbGetClient( p_chroma_desc->pixel_bits / 3, // bitsPerSample
418                                     3, // samplesPerPixel
419                                     p_chroma_desc->pixel_size ); // bytesPerPixel
420     if ( ! p_sys->p_client )
421     {
422         msg_Dbg( p_demux, "Unable to set up client for %s",
423                  vlc_fourcc_GetDescription( VIDEO_ES, i_chroma ) );
424         free( p_sys );
425         return VLC_EGENERIC;
426     }
427
428     msg_Dbg( p_demux, "set up client for %s %d %d %d",
429              vlc_fourcc_GetDescription( VIDEO_ES, i_chroma ),
430              p_chroma_desc->pixel_bits / 3, 3, p_chroma_desc->pixel_size );
431
432     p_sys->p_client->MallocFrameBuffer = mallocFrameBufferHandler;
433     p_sys->p_client->canHandleNewFBSize = TRUE;
434     p_sys->p_client->GetCredential = getCredentialHandler;
435     p_sys->p_client->GetPassword = getPasswordHandler; /* VNC simple auth */
436
437     /* Set compression and quality levels */
438     p_sys->p_client->appData.compressLevel =
439             var_InheritInteger( p_demux, CFG_PREFIX "compress-level" );
440     p_sys->p_client->appData.qualityLevel =
441             var_InheritInteger( p_demux, CFG_PREFIX "quality-level" );
442
443     /* Parse uri params */
444     vlc_url_t url;
445     vlc_UrlParse( &url, p_demux->psz_location, 0 );
446
447     if ( !EMPTY_STR(url.psz_host) )
448         p_sys->p_client->serverHost = strdup( url.psz_host );
449     else
450         p_sys->p_client->serverHost = strdup( "localhost" );
451
452     p_sys->p_client->appData.viewOnly = TRUE;
453     p_sys->p_client->serverPort = ( url.i_port > 0 ) ? url.i_port : 5900;
454
455     msg_Dbg( p_demux, "VNC init %s host=%s port=%d",
456              p_demux->psz_location,
457              p_sys->p_client->serverHost,
458              p_sys->p_client->serverPort );
459
460     vlc_UrlClean( &url );
461
462     /* make demux available for callback handlers */
463     rfbClientSetClientData( p_sys->p_client, DemuxThread, p_demux );
464     p_demux->p_sys = p_sys;
465
466     if( !rfbInitClient( p_sys->p_client, NULL, NULL ) )
467     {
468         msg_Err( p_demux, "can't connect to RFB server" );
469         goto error;
470     }
471
472     p_sys->i_starttime = mdate();
473
474     if ( vlc_clone( &p_sys->thread, DemuxThread, p_demux, VLC_THREAD_PRIORITY_INPUT ) != VLC_SUCCESS )
475     {
476         msg_Err( p_demux, "can't spawn thread" );
477         goto error;
478     }
479
480     p_demux->pf_demux = NULL;
481     p_demux->pf_control = Control;
482
483     return VLC_SUCCESS;
484
485 error:
486     free( p_sys );
487     return VLC_EGENERIC;
488 }
489
490 /*****************************************************************************
491  * Close:
492  *****************************************************************************/
493 static void Close( vlc_object_t *p_this )
494 {
495     demux_t     *p_demux = (demux_t*)p_this;
496     demux_sys_t *p_sys = p_demux->p_sys;
497
498     vlc_cancel( p_sys->thread );
499     vlc_join( p_sys->thread, NULL );
500
501     if ( p_sys->es )
502         es_out_Del( p_demux->out, p_sys->es );
503
504     rfbClientCleanup( p_sys->p_client );
505
506     if ( p_sys->p_block )
507         block_Release( p_sys->p_block );
508
509     free( p_sys );
510 }