1 /*****************************************************************************
2 * remoteosd.c: remote osd over vnc filter module
3 *****************************************************************************
4 * Copyright (C) 2007-2008 Matthias Bauer
7 * Authors: Matthias Bauer <matthias dot bauer #_at_# gmx dot ch>
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.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implid warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
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 *****************************************************************************/
24 /*****************************************************************************
25 * RemoteOSD uses the RFB-Protocol of VNC to display an On-Screen-Display
26 * menu generated by a streaming server as overlay for the streamed video.
28 * The streaming server that implements this is the ffnetdev plugin for VDR.
29 * VDR (VideoDiskRecorder) is an Linux based OpenSource harddisk recorder
31 * The VDR ffnetdev plugin emulates the hardware MPEG decoder and streams the
32 * video over the network instead of hardware video outputs.
33 * The OSD menu of VDR is offered with the RFB protocol to a VNC client.
35 * In fact this video-filter is a simple VNC client that could be also used to
36 * connect to a real VNC host.
37 * Only 8-bit color is supported at the moment.
38 * Using password protected VNC hosts is supported but not recommended, because
39 * you need to insert the used password in the plugin configuration page of
40 * VLC configuration in plain text and it's saved in plain text.
41 *****************************************************************************/
45 /*****************************************************************************
47 *****************************************************************************/
53 #include <vlc_common.h>
54 #include <vlc_plugin.h>
57 #include "vlc_filter.h"
58 #include "filter_common.h"
59 #include "vlc_image.h"
63 #include <vlc_network.h>
64 #include <gcrypt.h> /* to encrypt password */
65 #include <vlc_gcrypt.h>
67 #define CHALLENGESIZE 16
68 #define MAX_VNC_SERVER_NAME_LENGTH 255
70 #include "remoteosd_rfbproto.h" /* type definitions of the RFB protocol for VNC */
72 /*****************************************************************************
74 *****************************************************************************/
75 /* subfilter functions */
76 static int CreateFilter ( vlc_object_t * );
77 static void DestroyFilter( vlc_object_t * );
78 static subpicture_t *Filter( filter_t *, mtime_t );
80 static int MouseEvent ( vlc_object_t *p_this, char const *psz_var,
81 vlc_value_t oldval, vlc_value_t newval, void *p_data );
83 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
84 vlc_value_t oldval, vlc_value_t newval, void *p_data );
86 static void stop_osdvnc ( filter_t *p_filter );
88 static void vnc_worker_thread ( vlc_object_t *p_thread_obj );
90 static void update_request_thread( vlc_object_t *p_thread_obj );
92 static bool open_vnc_connection ( filter_t *p_filter );
94 static bool handshaking ( filter_t *p_filter );
96 static bool process_server_message ( filter_t *p_filter,
97 rfbServerToClientMsg *msg );
99 static bool read_exact( filter_t *p_filter,
104 static bool write_exact( filter_t *p_filter,
109 static inline void rgb_to_yuv( uint8_t *y, uint8_t *u, uint8_t *v,
110 int r, int g, int b );
112 static inline bool fill_rect( filter_sys_t* p_sys,
113 uint16_t i_x, uint16_t i_y,
114 uint16_t i_w, uint16_t i_h,
117 static inline bool raw_line( filter_sys_t* p_sys,
118 uint16_t i_x, uint16_t i_y,
121 static void vnc_encrypt_bytes( unsigned char *bytes, char *passwd );
124 /*****************************************************************************
126 *****************************************************************************/
127 #define READ_BUFFER_SIZE 1000000
129 #define RMTOSD_HOST_TEXT N_("VNC Host")
130 #define RMTOSD_HOST_LONGTEXT N_( \
131 "VNC hostname or IP address." )
133 #define RMTOSD_PORT_TEXT N_("VNC Port")
134 #define RMTOSD_PORT_LONGTEXT N_( \
137 #define RMTOSD_PASSWORD_TEXT N_("VNC Password")
138 #define RMTOSD_PASSWORD_LONGTEXT N_( \
141 #define RMTOSD_UPDATE_TEXT N_("VNC poll interval" )
142 #define RMTOSD_UPDATE_LONGTEXT N_( \
143 "In this interval an update from VNC is requested, default every 300 ms. ")
145 #define RMTOSD_POLL_TEXT N_("VNC polling")
146 #define RMTOSD_POLL_LONGTEXT N_( \
147 "Activate VNC polling. Do NOT activate for use as VDR ffnetdev client." )
149 #define RMTOSD_MOUSE_TEXT N_("Mouse events")
150 #define RMTOSD_MOUSE_LONGTEXT N_( \
151 "Send mouse events to VNC host. Not needed for use as VDR ffnetdev client." )
153 #define RMTOSD_KEYS_TEXT N_("Key events")
154 #define RMTOSD_KEYS_LONGTEXT N_( \
155 "Send key events to VNC host." )
157 #define RMTOSD_ALPHA_TEXT N_("Alpha transparency value (default 255)")
158 #define RMTOSD_ALPHA_LONGTEXT N_( \
159 "The transparency of the OSD VNC can be changed by giving a value " \
160 "between 0 and 255. A lower value specifies more transparency a higher " \
161 "means less transparency. The default is being not transparent " \
162 "(value 255) the minimum is fully transparent (value 0)." )
164 #define RMTOSD_CFG "rmtosd-"
166 #define RMTOSD_UPDATE_MIN 200
167 #define RMTOSD_UPDATE_DEFAULT 1000
168 #define RMTOSD_UPDATE_MAX 300
172 set_description( N_("Remote-OSD over VNC") );
173 set_capability( "sub filter", 100 );
174 set_shortname( N_("Remote-OSD") );
175 set_category( CAT_VIDEO );
176 set_subcategory( SUBCAT_VIDEO_SUBPIC );
177 add_shortcut( "rmtosd" );
178 set_callbacks( CreateFilter, DestroyFilter );
180 add_string( RMTOSD_CFG "host", "myvdr", NULL, RMTOSD_HOST_TEXT,
181 RMTOSD_HOST_LONGTEXT, false );
182 add_integer_with_range( RMTOSD_CFG "port", 20001, 1, 0xFFFF, NULL,
183 RMTOSD_PORT_TEXT, RMTOSD_PORT_LONGTEXT, false );
184 add_password( RMTOSD_CFG "password", "", NULL, RMTOSD_PASSWORD_TEXT,
185 RMTOSD_PASSWORD_LONGTEXT, false );
186 add_integer_with_range( RMTOSD_CFG "update", RMTOSD_UPDATE_DEFAULT,
187 RMTOSD_UPDATE_MIN, RMTOSD_UPDATE_MAX, NULL, RMTOSD_UPDATE_TEXT,
188 RMTOSD_UPDATE_LONGTEXT, true );
189 add_bool( RMTOSD_CFG "vnc-polling", 0, NULL,
190 RMTOSD_POLL_TEXT , RMTOSD_POLL_LONGTEXT, false );
191 add_bool( RMTOSD_CFG "mouse-events", 0, NULL,
192 RMTOSD_MOUSE_TEXT , RMTOSD_MOUSE_LONGTEXT, false );
193 add_bool( RMTOSD_CFG "key-events", 0, NULL,
194 RMTOSD_KEYS_TEXT , RMTOSD_KEYS_LONGTEXT, false );
195 add_integer_with_range( RMTOSD_CFG "alpha", 255, 0, 255, NULL,
196 RMTOSD_ALPHA_TEXT, RMTOSD_ALPHA_LONGTEXT, true );
200 /*****************************************************************************
202 *****************************************************************************/
204 /*****************************************************************************
206 *****************************************************************************/
211 bool b_need_update; /* VNC picture is updated, do update the OSD*/
212 mtime_t i_vnc_poll_interval; /* Update the OSD menu every n ms */
214 uint8_t i_alpha; /* alpha transparency value */
216 char *psz_host; /* VNC host */
219 char *psz_passwd; /* VNC password */
221 bool b_vnc_poll; /* Activate VNC polling ? */
222 bool b_vnc_mouse_events; /* Send MouseEvents ? */
223 bool b_vnc_key_events; /* Send KeyEvents ? */
225 bool b_connection_active; /* Handshaking finished ? */
227 vlc_mutex_t lock; /* To lock for read/write on picture */
229 picture_t *p_pic; /* The picture with OSD data from VNC */
231 vout_thread_t *p_vout; /* Pointer to video-out thread */
233 int i_socket; /* Socket used for VNC */
235 uint16_t i_vnc_width; /* The with of the VNC screen */
236 uint16_t i_vnc_height; /* The height of the VNC screen */
237 uint32_t i_vnc_pixels; /* The pixels of the VNC screen */
239 bool b_alpha_from_vnc; /* Special ffnetdev alpha feature enabled ? */
241 char read_buffer[READ_BUFFER_SIZE];
245 vlc_object_t* p_worker_thread;
246 vlc_object_t* p_update_request_thread;
248 uint8_t ar_color_table_yuv[256][4];
251 /*****************************************************************************
252 * CreateFilter: Create the filter and open the definition file
253 *****************************************************************************/
254 static int CreateFilter ( vlc_object_t *p_this )
256 filter_t *p_filter = (filter_t *)p_this;
257 filter_sys_t *p_sys = NULL;
259 msg_Dbg( p_filter, "Creating vnc osd filter..." );
261 p_filter->p_sys = p_sys = (filter_sys_t *) malloc( sizeof(filter_sys_t) );
262 if( !p_filter->p_sys )
264 memset( p_sys, 0, sizeof(filter_sys_t) );
266 /* Populating struct */
267 vlc_mutex_init( &p_sys->lock );
268 p_sys->b_continue = true;
269 p_sys->i_socket = -1;
271 p_sys->psz_host = var_CreateGetString( p_this, RMTOSD_CFG "host" );
272 if( EMPTY_STR(p_sys->psz_host) )
274 msg_Err( p_filter, "unable to get vnc host" );
278 p_sys->psz_passwd = var_CreateGetString( p_this, RMTOSD_CFG "password" );
279 if( !p_sys->psz_passwd )
281 msg_Err( p_filter, "unable to get vnc password" );
285 p_sys->i_port = var_CreateGetIntegerCommand( p_this, RMTOSD_CFG "port" );
287 p_sys->i_alpha = var_CreateGetIntegerCommand( p_this, RMTOSD_CFG "alpha" );
289 /* in miliseconds, 0 disables polling, should not be lower than 100 */
290 p_sys->i_vnc_poll_interval = var_CreateGetIntegerCommand( p_this,
291 RMTOSD_CFG "update" );
292 if ( p_sys->i_vnc_poll_interval < 100)
294 p_sys->i_vnc_poll_interval = 100;
297 for ( int i = 0; i < 256; i++ )
299 p_sys->ar_color_table_yuv[i][0] = 255;
300 p_sys->ar_color_table_yuv[i][1] = 255;
301 p_sys->ar_color_table_yuv[i][2] = 255;
302 p_sys->ar_color_table_yuv[i][3] = 255;
305 p_sys->b_vnc_poll = var_CreateGetBoolCommand( p_this,
306 RMTOSD_CFG "vnc-polling" );
307 p_sys->b_vnc_mouse_events = var_CreateGetBoolCommand( p_this,
308 RMTOSD_CFG "mouse-events" );
309 p_sys->b_vnc_key_events = var_CreateGetBoolCommand( p_this,
310 RMTOSD_CFG "key-events" );
312 /* Keep track of OSD Events */
313 p_sys->b_need_update = false;
315 /* Attach subpicture filter callback */
316 p_filter->pf_sub_filter = Filter;
318 p_sys->p_vout = vlc_object_find( p_this, VLC_OBJECT_VOUT, FIND_PARENT );
322 var_AddCallback( p_sys->p_vout, "mouse-moved",
323 MouseEvent, p_this );
324 var_AddCallback( p_sys->p_vout, "mouse-button-down",
325 MouseEvent, p_this );
326 var_AddCallback( p_sys->p_vout->p_libvlc, "key-pressed",
330 es_format_Init( &p_filter->fmt_out, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
331 p_filter->fmt_out.i_priority = 0;
335 /* create the vnc worker thread */
336 p_sys->p_worker_thread = vlc_object_create( p_this,
337 sizeof( vlc_object_t ) );
338 vlc_object_attach( p_sys->p_worker_thread, p_this );
339 if( vlc_thread_create( p_sys->p_worker_thread, "vnc worker thread",
341 VLC_THREAD_PRIORITY_LOW, false ) )
343 vlc_object_detach( p_sys->p_worker_thread );
344 vlc_object_release( p_sys->p_worker_thread );
345 p_sys->p_worker_thread = NULL;
346 msg_Err( p_filter, "cannot spawn vnc message reader thread" );
350 msg_Dbg( p_filter, "osdvnc filter started" );
355 msg_Err( p_filter, "osdvnc filter discarded" );
357 p_sys->b_continue = false;
359 stop_osdvnc( p_filter );
361 free( p_sys->psz_host );
362 free( p_sys->psz_passwd );
368 /*****************************************************************************
369 * DestroyFilter: Make a clean exit of this plugin
370 *****************************************************************************/
371 static void DestroyFilter( vlc_object_t *p_this )
373 filter_t *p_filter = (filter_t*)p_this;
374 filter_sys_t *p_sys = p_filter->p_sys;
376 msg_Dbg( p_filter, "DestroyFilter called." );
378 stop_osdvnc( p_filter );
382 var_DelCallback( p_sys->p_vout, "mouse-moved",
383 MouseEvent, p_this );
384 var_DelCallback( p_sys->p_vout, "mouse-button-down",
385 MouseEvent, p_this );
386 var_DelCallback( p_sys->p_vout->p_libvlc, "key-pressed",
389 vlc_object_release( p_sys->p_vout );
390 p_sys->p_vout = NULL;
393 var_Destroy( p_this, RMTOSD_CFG "host" );
394 var_Destroy( p_this, RMTOSD_CFG "port" );
395 var_Destroy( p_this, RMTOSD_CFG "password" );
396 var_Destroy( p_this, RMTOSD_CFG "update" );
397 var_Destroy( p_this, RMTOSD_CFG "vnc-polling" );
398 var_Destroy( p_this, RMTOSD_CFG "mouse-events" );
399 var_Destroy( p_this, RMTOSD_CFG "key-events" );
400 var_Destroy( p_this, RMTOSD_CFG "alpha" );
402 free( p_sys->psz_host );
403 free( p_sys->psz_passwd );
407 static void stop_osdvnc ( filter_t *p_filter )
409 filter_sys_t *p_sys = p_filter->p_sys;
411 if (p_sys->i_socket >= 0)
413 net_Close(p_sys->i_socket);
415 p_sys->b_continue = false; /* this causes the threads to stop */
417 if ( p_sys->p_worker_thread )
419 msg_Dbg( p_filter, "joining worker_thread" );
420 vlc_thread_join( p_sys->p_worker_thread );
421 vlc_object_detach( p_sys->p_worker_thread );
422 vlc_object_release( p_sys->p_worker_thread );
423 msg_Dbg( p_filter, "released worker_thread" );
426 if ( p_sys->p_update_request_thread )
428 msg_Dbg( p_filter, "joining update_request_thread" );
429 vlc_thread_join( p_sys->p_update_request_thread );
430 vlc_object_detach( p_sys->p_update_request_thread );
431 vlc_object_release( p_sys->p_update_request_thread );
432 msg_Dbg( p_filter, "released update_request_thread" );
435 msg_Dbg( p_filter, "osdvnc stopped" );
438 static bool read_exact( filter_t *p_filter,
443 return i_bytes == net_Read( p_filter, i_socket, NULL,
444 (unsigned char*)p_readbuf,
449 static bool write_exact( filter_t *p_filter,
454 return i_bytes == net_Write( p_filter, i_socket, NULL,
455 (unsigned char*)p_writebuf, i_bytes );
458 static bool open_vnc_connection ( filter_t *p_filter )
460 filter_sys_t *p_sys = p_filter->p_sys;
462 msg_Dbg( p_filter, "Open socket to vnc server on %s:%u.",
463 p_sys->psz_host, p_sys->i_port );
465 p_sys->i_socket = net_ConnectTCP( p_filter, p_sys->psz_host, p_sys->i_port );
467 if( p_sys->i_socket < 0 )
469 msg_Err( p_filter, "Could not open socket" );
473 msg_Dbg( p_filter, "socket is open." );
478 static bool handshaking ( filter_t *p_filter )
480 filter_sys_t *p_sys = p_filter->p_sys;
482 msg_Dbg( p_filter, "Reading protocol version" );
484 rfbProtocolVersionMsg pv;
485 if ( !read_exact( p_filter, p_sys->i_socket, pv,
486 sz_rfbProtocolVersionMsg ) )
488 msg_Err( p_filter, "Could not read version message" );
491 pv[sz_rfbProtocolVersionMsg] = '\0'; /* pv size is sz_rfbProtocolVersionMsg+1 */
493 msg_Dbg( p_filter, "Server version is %s", pv );
495 strncpy(pv, "RFB 003.003\n", sz_rfbProtocolVersionMsg);
497 if( !write_exact(p_filter, p_sys->i_socket, pv,
498 sz_rfbProtocolVersionMsg) )
500 msg_Err( p_filter, "Could not write version message" );
504 msg_Dbg( p_filter, "Reading authentication scheme" );
505 uint32_t i_authScheme;
506 if( !read_exact( p_filter, p_sys->i_socket, (char*)&i_authScheme, 4 ) )
508 msg_Err( p_filter, "Could not read authentication scheme" );
511 i_authScheme = htonl(i_authScheme);
513 msg_Dbg( p_filter, "Authentication scheme = %x", i_authScheme );
514 if ( i_authScheme == rfbConnFailed )
516 msg_Err( p_filter, "Connection rejected by server" );
519 if (i_authScheme == rfbVncAuth)
521 unsigned char challenge[CHALLENGESIZE];
522 if ( !read_exact( p_filter, p_sys->i_socket,
523 (char*)challenge, CHALLENGESIZE ) )
525 msg_Err( p_filter, "Could not read password challenge" );
529 vnc_encrypt_bytes( challenge, p_sys->psz_passwd );
531 if( !write_exact(p_filter, p_sys->i_socket,
532 (char*)challenge, CHALLENGESIZE ) )
534 msg_Err( p_filter, "Could not write password" );
537 uint32_t i_authResult;
538 if( !read_exact( p_filter, p_sys->i_socket, (char*)&i_authResult, 4 ) )
540 msg_Err( p_filter, "Could not read authentication result" );
543 i_authResult = htonl(i_authResult);
544 if (i_authResult != rfbVncAuthOK)
546 msg_Err( p_filter, "VNC authentication failed" );
551 msg_Dbg( p_filter, "Writing client init message" );
554 if( !write_exact( p_filter, p_sys->i_socket,
555 (char*)&ci, sz_rfbClientInitMsg ) )
557 msg_Err( p_filter, "Could not write client init message" );
561 msg_Dbg( p_filter, "Reading server init message" );
563 if( !read_exact( p_filter, p_sys->i_socket,
564 (char*)&si, sz_rfbServerInitMsg ) )
566 msg_Err( p_filter, "Could not read server init message" );
569 si.framebufferWidth = htons(si.framebufferWidth);
570 si.framebufferHeight = htons(si.framebufferHeight);
571 si.format.redMax = htons(si.format.redMax);
572 si.format.greenMax = htons(si.format.greenMax);
573 si.format.blueMax = htons(si.format.blueMax);
575 p_sys->i_vnc_width = si.framebufferWidth;
576 p_sys->i_vnc_height = si.framebufferHeight;
578 msg_Dbg( p_filter, "Servers preferred pixelformat: "
579 "%ux%u, R(%u),G(%u),B(%u), %u bit, depht=%u, %s",
581 si.framebufferHeight,
585 si.format.bitsPerPixel,
587 si.format.trueColour ? "TrueColor" : "Not-TrueColor");
589 uint32_t i_nameLength = htonl(si.nameLength);
590 if( i_nameLength > MAX_VNC_SERVER_NAME_LENGTH )
592 msg_Err( p_filter, "Server name too long" );
595 char s_ServerName[MAX_VNC_SERVER_NAME_LENGTH+1];
597 msg_Dbg( p_filter, "Reading server name with size = %u", i_nameLength );
598 if( !read_exact( p_filter, p_sys->i_socket, s_ServerName, i_nameLength ) )
600 msg_Err( p_filter, "Could not read server name" );
603 s_ServerName[i_nameLength] = '\0';
605 if( strcmp( s_ServerName, "VDR-OSD") == 0 )
607 msg_Dbg( p_filter, "Server is a VDR" );
608 p_sys->b_alpha_from_vnc = true;
612 msg_Dbg( p_filter, "Server is a normal VNC" );
613 p_sys->b_alpha_from_vnc = false;
617 msg_Dbg( p_filter, "Server init message read properly" );
618 msg_Dbg( p_filter, "Server name is %s", s_ServerName );
620 msg_Dbg( p_filter, "Writing SetPixelFormat message" );
622 rfbSetPixelFormatMsg sp;
623 sp.type = rfbSetPixelFormat;
624 sp.format.bitsPerPixel = 8;
625 sp.format.depth = 8 ;
626 sp.format.bigEndian = 1;
627 sp.format.trueColour = 0;
628 sp.format.redMax = htons(31);
629 sp.format.greenMax = htons(31);
630 sp.format.blueMax = htons(31);
631 sp.format.redShift = 10;
632 sp.format.greenShift = 5;
633 sp.format.blueShift = 0;
635 if( !write_exact( p_filter, p_sys->i_socket,
636 (char*)&sp, sz_rfbSetPixelFormatMsg) )
638 msg_Err( p_filter, "Could not write SetPixelFormat message" );
642 msg_Dbg( p_filter, "Writing SetEncodings message" );
644 rfbSetEncodingsMsg se;
645 se.type = rfbSetEncodings;
646 se.nEncodings = htons( p_sys->b_alpha_from_vnc ? 3 : 2 );
648 if( !write_exact( p_filter, p_sys->i_socket,
649 (char*)&se, sz_rfbSetEncodingsMsg) )
651 msg_Err( p_filter, "Could not write SetEncodings message begin" );
657 msg_Dbg( p_filter, "Writing SetEncodings rfbEncodingCopyRect" );
658 i_encoding = htonl(rfbEncodingCopyRect);
659 if( !write_exact( p_filter, p_sys->i_socket, (char*)&i_encoding, 4) )
661 msg_Err( p_filter, "Could not write encoding type rfbEncodingCopyRect." );
665 msg_Dbg( p_filter, "Writing SetEncodings rfbEncodingRRE" );
666 i_encoding = htonl(rfbEncodingRRE);
667 if( !write_exact(p_filter, p_sys->i_socket, (char*)&i_encoding, 4) )
669 msg_Err( p_filter, "Could not write encoding type rfbEncodingRRE." );
673 if( p_sys->b_alpha_from_vnc )
675 msg_Dbg( p_filter, "Writing SetEncodings rfbEncSpecialUseAlpha" );
676 i_encoding = 0x00F0FFFF; /* rfbEncSpecialUseAlpha is 0xFFFFF000
677 * before we swap it */
678 if( !write_exact(p_filter, p_sys->i_socket, (char*)&i_encoding, 4) )
680 msg_Err( p_filter, "Could not write encoding type rfbEncSpecialUseAlpha." );
688 static void vnc_worker_thread( vlc_object_t *p_thread_obj )
690 filter_t* p_filter = (filter_t*)(p_thread_obj->p_parent);
691 filter_sys_t *p_sys = p_filter->p_sys;
693 msg_Dbg( p_filter, "VNC worker thread started" );
695 if ( open_vnc_connection ( p_filter ) == false )
697 msg_Err( p_filter, "Could not connect to vnc host" );
701 if ( handshaking ( p_filter ) == false )
703 msg_Err( p_filter, "Error occured while handshaking vnc host" );
707 p_sys->b_connection_active = true; /* to enable sending key
708 * and mouse events to host */
710 /* Create an empty picture for VNC the data */
711 vlc_mutex_lock( &p_sys->lock );
712 p_sys->p_pic = malloc( sizeof(picture_t) );
715 vlc_mutex_unlock( &p_sys->lock );
718 vout_AllocatePicture( VLC_OBJECT(p_filter), p_sys->p_pic,
719 VLC_FOURCC('Y','U','V','A'),
722 VOUT_ASPECT_FACTOR );
723 if( !p_sys->p_pic->i_planes )
725 free( p_sys->p_pic );
727 vlc_mutex_unlock( &p_sys->lock );
730 p_sys->i_vnc_pixels = p_sys->i_vnc_width * p_sys->i_vnc_height;
732 vlc_mutex_unlock( &p_sys->lock );
734 /* create the update request thread */
735 p_sys->p_update_request_thread = vlc_object_create( p_filter,
736 sizeof( vlc_object_t ) );
737 vlc_object_attach( p_sys->p_update_request_thread, p_filter );
738 if( vlc_thread_create( p_sys->p_update_request_thread,
739 "vnc update request thread",
740 update_request_thread,
741 VLC_THREAD_PRIORITY_LOW, false ) )
743 vlc_object_detach( p_sys->p_update_request_thread );
744 vlc_object_release( p_sys->p_update_request_thread );
745 p_sys->p_update_request_thread = NULL;
746 msg_Err( p_filter, "cannot spawn vnc update request thread" );
750 /* connection is initialized, now read and handle server messages */
754 rfbServerToClientMsg msg;
756 while (p_sys->b_continue)
759 if ( !read_exact(p_filter, p_sys->i_socket, (char*)&msg, 1 ) )
761 msg_Err( p_filter, "Error while waiting for next server message");
762 p_sys->b_continue = false;
768 case rfbFramebufferUpdate:
769 i_msgSize = sz_rfbFramebufferUpdateMsg;
771 case rfbSetColourMapEntries:
772 i_msgSize = sz_rfbSetColourMapEntriesMsg;
775 i_msgSize = sz_rfbBellMsg;
777 case rfbServerCutText:
778 i_msgSize = sz_rfbServerCutTextMsg;
780 case rfbReSizeFrameBuffer:
781 i_msgSize = sz_rfbReSizeFrameBufferMsg;
785 msg_Err( p_filter, "Invalid message %u received", msg.type );
786 p_sys->b_continue = false;
788 if (p_sys->b_continue == true)
792 if ( !read_exact( p_filter, p_sys->i_socket,
793 ((char*)&msg)+1, i_msgSize ) )
795 msg_Err( p_filter, "Error while reading message of type %u",
797 p_sys->b_continue = false;
801 process_server_message( p_filter, &msg);
806 process_server_message( p_filter, &msg);
812 if (p_sys->p_worker_thread->b_die ||
813 p_sys->p_worker_thread->b_error)
815 p_sys->b_continue = false;
820 msg_Dbg( p_filter, "VNC message reader thread ended" );
824 static void update_request_thread( vlc_object_t *p_thread_obj )
826 filter_t* p_filter = (filter_t*)(p_thread_obj->p_parent);
827 filter_sys_t *p_sys = p_filter->p_sys;
829 msg_Dbg( p_filter, "VNC update request thread started" );
831 rfbFramebufferUpdateRequestMsg udr;
832 udr.type = rfbFramebufferUpdateRequest;
836 udr.w = htons(p_sys->i_vnc_width);
837 udr.h = htons(p_sys->i_vnc_height);
839 if( write_exact(p_filter, p_sys->i_socket, (char*)&udr,
840 sz_rfbFramebufferUpdateRequestMsg) == false)
842 msg_Err( p_filter, "Could not write rfbFramebufferUpdateRequestMsg." );
843 p_sys->b_continue = false;
847 mtime_t i_poll_interval_microsec = p_sys->i_vnc_poll_interval * 1000;
849 if (p_sys->b_vnc_poll)
851 while ( p_sys->b_continue == true )
853 msleep( i_poll_interval_microsec );
854 if( write_exact(p_filter, p_sys->i_socket, (char*)&udr,
855 sz_rfbFramebufferUpdateRequestMsg) == false)
857 msg_Err( p_filter, "Could not write rfbFramebufferUpdateRequestMsg." );
858 p_sys->b_continue = false;
860 if (p_sys->p_update_request_thread->b_die ||
861 p_sys->p_update_request_thread->b_error)
863 p_sys->b_continue = false;
869 msg_Dbg( p_filter, "VNC polling disabled." );
871 msg_Dbg( p_filter, "VNC update request thread ended" );
874 static bool process_server_message ( filter_t *p_filter,
875 rfbServerToClientMsg *msg )
877 filter_sys_t *p_sys = p_filter->p_sys;
881 case rfbFramebufferUpdate:
883 msg->fu.nRects = htons(msg->fu.nRects);
884 rfbFramebufferUpdateRectHeader hdr;
886 for (int i_rect = 0; i_rect < msg->fu.nRects; i_rect++)
888 if (!read_exact(p_filter, p_sys->i_socket, (char*)&hdr,
889 sz_rfbFramebufferUpdateRectHeader ) )
891 msg_Err( p_filter, "Could not read FrameBufferUpdate header" );
894 hdr.r.x = htons(hdr.r.x);
895 hdr.r.y = htons(hdr.r.y);
896 hdr.r.w = htons(hdr.r.w);
897 hdr.r.h = htons(hdr.r.h);
898 hdr.encoding = htonl(hdr.encoding);
900 switch (hdr.encoding)
905 for (i_line = 0; i_line < hdr.r.h; i_line++)
907 if ( !read_exact( p_filter, p_sys->i_socket,
908 p_sys->read_buffer, hdr.r.w ) )
911 "Could not read FrameBufferUpdate line data" );
914 vlc_mutex_lock( &p_sys->lock );
915 if ( !raw_line( p_sys, hdr.r.x,
919 msg_Err( p_filter, "raw_line failed." );
920 vlc_mutex_unlock( &p_sys->lock );
923 vlc_mutex_unlock( &p_sys->lock );
928 case rfbEncodingCopyRect:
931 "Rect in unsupported encoding rfbEncodingCopyRect" );
938 if ( !read_exact( p_filter, p_sys->i_socket,
942 msg_Err( p_filter, "Could not read rfbRREHeader" );
946 if ( !read_exact(p_filter, p_sys->i_socket,
947 (char*)&i_pixcolor, 1 ) )
949 msg_Err( p_filter, "Could not read RRE pixcolor" );
953 vlc_mutex_lock( &p_sys->lock );
954 if ( !fill_rect( p_sys,
959 msg_Err( p_filter, "main fill_rect failed." );
960 vlc_mutex_unlock( &p_sys->lock );
963 vlc_mutex_unlock( &p_sys->lock );
965 rrehdr.nSubrects = htonl(rrehdr.nSubrects);
967 int i_datasize = rrehdr.nSubrects *
968 ( sizeof(i_pixcolor) + sz_rfbRectangle ) ;
969 if ( i_datasize > READ_BUFFER_SIZE )
971 msg_Err( p_filter, "Buffer too small, "
972 "need %u bytes", i_datasize );
975 if ( !read_exact( p_filter, p_sys->i_socket,
976 p_sys->read_buffer, i_datasize ) )
979 "Could not read RRE subrect data" );
984 rfbRectangle* p_subrect;
986 vlc_mutex_lock( &p_sys->lock );
988 i_subrect < rrehdr.nSubrects; i_subrect++)
990 i_pixcolor = p_sys->read_buffer[i_offset];
991 i_offset += sizeof(i_pixcolor);
993 (rfbRectangle*)(p_sys->read_buffer + i_offset);
994 i_offset += sz_rfbRectangle;
996 if (!fill_rect( p_sys,
997 htons(p_subrect->x) + hdr.r.x,
998 htons(p_subrect->y) + hdr.r.y,
1000 htons(p_subrect->h),
1004 "subrect %u fill_rect failed.", i_subrect );
1005 vlc_mutex_unlock( &p_sys->lock );
1009 vlc_mutex_unlock( &p_sys->lock );
1016 vlc_mutex_lock( &p_sys->lock );
1017 p_sys->b_need_update = true;
1018 vlc_mutex_unlock( &p_sys->lock );
1022 case rfbSetColourMapEntries:
1024 msg->scme.nColours = htons(msg->scme.nColours);
1025 msg->scme.firstColour = htons(msg->scme.firstColour);
1027 if ( p_sys->b_alpha_from_vnc == true )
1029 i_datasize = 2 * msg->scme.nColours * 4;
1033 i_datasize = 2 * msg->scme.nColours * 3;
1035 if ( i_datasize > READ_BUFFER_SIZE )
1037 msg_Err( p_filter, "Buffer too small, need %u bytes",
1042 if ( !read_exact( p_filter, p_sys->i_socket,
1043 p_sys->read_buffer, i_datasize ) )
1045 msg_Err( p_filter, "Could not read color map data" );
1049 uint8_t i_red, i_green, i_blue, i_alpha, i_color_index;
1050 uint16_t i_offset = 0;
1053 for (int i = 0; i < msg->scme.nColours; i++)
1055 i_color_index = i+msg->scme.firstColour;
1056 if ( p_sys->b_alpha_from_vnc == true )
1058 i_alpha = p_sys->read_buffer[i_offset];
1061 i_red = p_sys->read_buffer[i_offset];
1063 i_green = p_sys->read_buffer[i_offset];
1065 i_blue = p_sys->read_buffer[i_offset];
1067 rgb_to_yuv( &p_sys->ar_color_table_yuv[i_color_index][0],
1068 &p_sys->ar_color_table_yuv[i_color_index][1],
1069 &p_sys->ar_color_table_yuv[i_color_index][2],
1073 p_sys->ar_color_table_yuv[i][3] = i_alpha;
1079 msg_Err( p_filter, "rfbBell received" );
1082 case rfbServerCutText:
1083 msg->sct.length = htons(msg->sct.length);
1084 if ( msg->sct.length > READ_BUFFER_SIZE )
1086 msg_Err( p_filter, "Buffer too small, need %u bytes", msg->sct.length );
1089 if ( !read_exact(p_filter, p_sys->i_socket,
1090 p_sys->read_buffer, msg->sct.length ) )
1092 msg_Err( p_filter, "Could not read Reading rfbServerCutText data" );
1097 case rfbReSizeFrameBuffer:
1098 msg_Err( p_filter, "Reading rfbReSizeFrameBuffer not implemented" );
1102 msg_Err( p_filter, "Invalid message %u received", msg->type );
1108 /****************************************************************************
1109 * Filter: the whole thing
1110 ****************************************************************************
1111 * This function outputs subpictures at regular time intervals.
1112 ****************************************************************************/
1113 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
1115 filter_sys_t *p_sys = p_filter->p_sys;
1116 subpicture_t *p_spu;
1117 subpicture_region_t *p_region;
1121 if( !p_sys->b_need_update )
1126 vlc_mutex_lock( &p_sys->lock );
1128 p_pic = p_sys->p_pic;
1132 vlc_mutex_unlock( &p_sys->lock );
1136 /* Allocate the subpicture internal data. */
1137 p_spu = p_filter->pf_sub_buffer_new( p_filter );
1140 vlc_mutex_unlock( &p_sys->lock );
1144 p_spu->b_absolute = false;
1145 p_spu->i_start = date;
1147 p_spu->b_ephemer = true;
1149 /* Create new SPU region */
1150 memset( &fmt, 0, sizeof(video_format_t) );
1151 fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
1152 fmt.i_aspect = VOUT_ASPECT_FACTOR;
1153 fmt.i_sar_num = fmt.i_sar_den = 1;
1154 fmt.i_width = fmt.i_visible_width = p_pic->p[Y_PLANE].i_visible_pitch;
1155 fmt.i_height = fmt.i_visible_height = p_pic->p[Y_PLANE].i_visible_lines;
1156 fmt.i_x_offset = fmt.i_y_offset = 0;
1157 p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
1160 msg_Err( p_filter, "cannot allocate SPU region" );
1161 p_filter->pf_sub_buffer_del( p_filter, p_spu );
1162 vlc_mutex_unlock( &p_sys->lock );
1166 vout_CopyPicture( p_filter, &p_region->picture, p_pic );
1168 p_sys->b_need_update = false;
1170 vlc_mutex_unlock( &p_sys->lock );
1172 /* set to one of the 9 relative locations */
1173 p_region->i_align = 0; //=CENTER
1174 p_spu->b_absolute = false;
1180 p_spu->p_region = p_region;
1182 p_spu->i_alpha = ( p_sys->i_alpha );
1188 static inline void rgb_to_yuv( uint8_t *y, uint8_t *u, uint8_t *v,
1189 int r, int g, int b )
1191 *y = ( ( ( 66 * r + 129 * g + 25 * b + 128 ) >> 8 ) + 16 );
1192 *u = ( ( -38 * r - 74 * g + 112 * b + 128 ) >> 8 ) + 128 ;
1193 *v = ( ( 112 * r - 94 * g - 18 * b + 128 ) >> 8 ) + 128 ;
1196 static inline bool fill_rect( filter_sys_t* p_sys,
1197 uint16_t i_x, uint16_t i_y,
1198 uint16_t i_w, uint16_t i_h,
1201 plane_t *p_outY = p_sys->p_pic->p+Y_PLANE;
1202 plane_t *p_outU = p_sys->p_pic->p+U_PLANE;
1203 plane_t *p_outV = p_sys->p_pic->p+V_PLANE;
1204 plane_t *p_outA = p_sys->p_pic->p+A_PLANE;
1205 int i_pitch = p_outY->i_pitch;
1206 int i_lines = p_outY->i_lines;
1207 if ( i_x + i_w > i_pitch)
1209 if ( i_y + i_h > i_lines)
1211 int i_line_offset = i_y * i_pitch;
1212 uint8_t i_yuv_y = p_sys->ar_color_table_yuv[i_color][0];
1213 uint8_t i_yuv_u = p_sys->ar_color_table_yuv[i_color][1];
1214 uint8_t i_yuv_v = p_sys->ar_color_table_yuv[i_color][2];
1215 uint8_t i_alpha = p_sys->ar_color_table_yuv[i_color][3];
1216 for( int i_line = 0; i_line < i_h; i_line++ )
1218 for( int i_column = 0; i_column < i_w; i_column++ )
1220 int i_total_offset = i_line_offset + i_x + i_column;
1221 p_outY->p_pixels[ i_total_offset ] = i_yuv_y;
1222 p_outU->p_pixels[ i_total_offset ] = i_yuv_u;
1223 p_outV->p_pixels[ i_total_offset ] = i_yuv_v;
1224 p_outA->p_pixels[ i_total_offset ] = i_alpha;
1226 i_line_offset += i_pitch;
1231 static inline bool raw_line( filter_sys_t* p_sys,
1232 uint16_t i_x, uint16_t i_y,
1235 plane_t *p_outY = p_sys->p_pic->p+Y_PLANE;
1236 plane_t *p_outU = p_sys->p_pic->p+U_PLANE;
1237 plane_t *p_outV = p_sys->p_pic->p+V_PLANE;
1238 plane_t *p_outA = p_sys->p_pic->p+A_PLANE;
1239 int i_pitch = p_outY->i_pitch;
1240 int i_lines = p_outY->i_lines;
1241 if ( i_x + i_w > i_pitch)
1246 int i_line_offset = i_y * i_pitch + i_x;
1248 for( int i_column = 0; i_column < i_w; i_column++ )
1250 int i_offset = i_line_offset + i_column;
1251 uint8_t i_color = p_sys->read_buffer[i_column];
1252 p_outY->p_pixels[ i_offset ] = p_sys->ar_color_table_yuv[i_color][0];
1253 p_outU->p_pixels[ i_offset ] = p_sys->ar_color_table_yuv[i_color][1];
1254 p_outV->p_pixels[ i_offset ] = p_sys->ar_color_table_yuv[i_color][2];
1255 p_outA->p_pixels[ i_offset ] = p_sys->ar_color_table_yuv[i_color][3];
1262 /*****************************************************************************
1263 * MouseEvent: callback for mouse events
1264 *****************************************************************************/
1265 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
1266 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1268 VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(psz_var);
1270 filter_t *p_filter = (filter_t *)p_data;
1271 filter_sys_t *p_sys = p_filter->p_sys;
1273 if( !p_sys->b_connection_active )
1276 if( !p_sys->b_vnc_mouse_events )
1279 vout_thread_t *p_vout = (vout_thread_t*)p_sys->p_vout;
1283 int v_h = p_vout->output.i_height;
1284 int v_w = p_vout->output.i_width;
1286 i_v = var_GetInteger( p_sys->p_vout, "mouse-button-down" );
1287 i_y = var_GetInteger( p_sys->p_vout, "mouse-y" );
1288 i_x = var_GetInteger( p_sys->p_vout, "mouse-x" );
1290 if( i_y < 0 || i_x < 0 || i_y >= v_h || i_x >= v_w )
1292 msg_Dbg( p_this, "invalid mouse event? x=%d y=%d btn=%x", i_x, i_y, i_v );
1296 msg_Dbg( p_this, "mouse event x=%d y=%d btn=%x", i_x, i_y, i_v );
1299 /* FIXME: calculate x and y coordinates for scaled output */
1301 /* buttonMask bits 0-7 are buttons 1-8, 0=up, 1=down */
1302 rfbPointerEventMsg ev;
1303 ev.type = rfbPointerEvent;
1304 ev.buttonMask = i_v;
1308 write_exact( p_filter, p_sys->i_socket,
1309 (char*)&ev, sz_rfbPointerEventMsg);
1314 /*****************************************************************************
1315 * KeyEvent: callback for keyboard events
1316 *****************************************************************************/
1317 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
1318 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1320 VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
1322 filter_t *p_filter = (filter_t *)p_data;
1323 filter_sys_t *p_sys = p_filter->p_sys;
1325 if( !p_sys->b_connection_active )
1328 if( !p_sys->b_vnc_key_events )
1331 msg_Dbg( p_this, "key pressed (%d) ", newval.i_int );
1333 if ( !newval.i_int )
1335 msg_Err( p_this, "Received invalid key event %d", newval.i_int );
1336 return VLC_EGENERIC;
1339 uint32_t i_key32 = newval.i_int;
1340 i_key32 = htonl(i_key32);
1342 ev.type = rfbKeyEvent;
1345 /* first key-down for modifier-keys */
1346 if (newval.i_int & KEY_MODIFIER_CTRL)
1349 write_exact( p_filter, p_sys->i_socket,
1350 (char*)&ev, sz_rfbKeyEventMsg);
1352 if (newval.i_int & KEY_MODIFIER_SHIFT)
1355 write_exact( p_filter, p_sys->i_socket,
1356 (char*)&ev, sz_rfbKeyEventMsg);
1358 if (newval.i_int & KEY_MODIFIER_ALT)
1361 write_exact( p_filter, p_sys->i_socket,
1362 (char*)&ev, sz_rfbKeyEventMsg);
1365 /* then key-down for the pressed key */
1367 write_exact( p_filter, p_sys->i_socket,
1368 (char*)&ev, sz_rfbKeyEventMsg);
1372 /* then key-up for the pressed key */
1373 write_exact( p_filter, p_sys->i_socket,
1374 (char*)&ev, sz_rfbKeyEventMsg);
1376 /* last key-down for modifier-keys */
1377 if (newval.i_int & KEY_MODIFIER_CTRL)
1380 write_exact( p_filter, p_sys->i_socket,
1381 (char*)&ev, sz_rfbKeyEventMsg);
1383 if (newval.i_int & KEY_MODIFIER_SHIFT)
1386 write_exact( p_filter, p_sys->i_socket,
1387 (char*)&ev, sz_rfbKeyEventMsg);
1389 if (newval.i_int & KEY_MODIFIER_ALT)
1392 write_exact( p_filter, p_sys->i_socket,
1393 (char*)&ev, sz_rfbKeyEventMsg);
1399 static void vnc_encrypt_bytes( unsigned char *bytes, char *passwd )
1401 unsigned char key[8];
1404 for (i = 0; i < 8; i++)
1405 key[i] = i < strlen( passwd ) ? passwd[i] : '\0';
1407 gcry_cipher_hd_t ctx;
1408 gcry_cipher_open( &ctx, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB,0);
1410 /* reverse bits of the key */
1411 for( i = 0 ; i < 8 ; i ++ )
1414 (((key[i] >> 6) & 0x01 ) << 1 ) +
1415 (((key[i] >> 5) & 0x01 ) << 2 ) +
1416 (((key[i] >> 4) & 0x01 ) << 3 ) +
1417 (((key[i] >> 3) & 0x01 ) << 4 ) +
1418 (((key[i] >> 2) & 0x01 ) << 5 ) +
1419 (((key[i] >> 1) & 0x01 ) << 6 ) +
1420 ((key[i] & 0x01) << 7 );
1422 gcry_cipher_setkey( ctx, key, 8 );
1423 gcry_cipher_encrypt( ctx, bytes, CHALLENGESIZE, bytes, CHALLENGESIZE );
1424 gcry_cipher_close( ctx );