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, VLC_OBJECT_GENERIC );
337 vlc_object_attach( p_sys->p_worker_thread, p_this );
338 if( vlc_thread_create( p_sys->p_worker_thread, "vnc worker thread",
340 VLC_THREAD_PRIORITY_LOW, false ) )
342 vlc_object_detach( p_sys->p_worker_thread );
343 vlc_object_release( p_sys->p_worker_thread );
344 p_sys->p_worker_thread = NULL;
345 msg_Err( p_filter, "cannot spawn vnc message reader thread" );
349 msg_Dbg( p_filter, "osdvnc filter started" );
354 msg_Err( p_filter, "osdvnc filter discarded" );
356 p_sys->b_continue = false;
358 stop_osdvnc( p_filter );
360 free( p_sys->psz_host );
361 free( p_sys->psz_passwd );
367 /*****************************************************************************
368 * DestroyFilter: Make a clean exit of this plugin
369 *****************************************************************************/
370 static void DestroyFilter( vlc_object_t *p_this )
372 filter_t *p_filter = (filter_t*)p_this;
373 filter_sys_t *p_sys = p_filter->p_sys;
375 msg_Dbg( p_filter, "DestroyFilter called." );
377 stop_osdvnc( p_filter );
381 var_DelCallback( p_sys->p_vout, "mouse-moved",
382 MouseEvent, p_this );
383 var_DelCallback( p_sys->p_vout, "mouse-button-down",
384 MouseEvent, p_this );
385 var_DelCallback( p_sys->p_vout->p_libvlc, "key-pressed",
388 vlc_object_release( p_sys->p_vout );
389 p_sys->p_vout = NULL;
392 var_Destroy( p_this, RMTOSD_CFG "host" );
393 var_Destroy( p_this, RMTOSD_CFG "port" );
394 var_Destroy( p_this, RMTOSD_CFG "password" );
395 var_Destroy( p_this, RMTOSD_CFG "update" );
396 var_Destroy( p_this, RMTOSD_CFG "vnc-polling" );
397 var_Destroy( p_this, RMTOSD_CFG "mouse-events" );
398 var_Destroy( p_this, RMTOSD_CFG "key-events" );
399 var_Destroy( p_this, RMTOSD_CFG "alpha" );
401 free( p_sys->psz_host );
402 free( p_sys->psz_passwd );
406 static void stop_osdvnc ( filter_t *p_filter )
408 filter_sys_t *p_sys = p_filter->p_sys;
410 if (p_sys->i_socket >= 0)
412 net_Close(p_sys->i_socket);
414 p_sys->b_continue = false; /* this causes the threads to stop */
416 if ( p_sys->p_worker_thread )
418 msg_Dbg( p_filter, "joining worker_thread" );
419 vlc_thread_join( p_sys->p_worker_thread );
420 vlc_object_detach( p_sys->p_worker_thread );
421 vlc_object_release( p_sys->p_worker_thread );
422 msg_Dbg( p_filter, "released worker_thread" );
425 if ( p_sys->p_update_request_thread )
427 msg_Dbg( p_filter, "joining update_request_thread" );
428 vlc_thread_join( p_sys->p_update_request_thread );
429 vlc_object_detach( p_sys->p_update_request_thread );
430 vlc_object_release( p_sys->p_update_request_thread );
431 msg_Dbg( p_filter, "released update_request_thread" );
434 msg_Dbg( p_filter, "osdvnc stopped" );
437 static bool read_exact( filter_t *p_filter,
442 return i_bytes == net_Read( p_filter, i_socket, NULL,
443 (unsigned char*)p_readbuf,
448 static bool write_exact( filter_t *p_filter,
453 return i_bytes == net_Write( p_filter, i_socket, NULL,
454 (unsigned char*)p_writebuf, i_bytes );
457 static bool open_vnc_connection ( filter_t *p_filter )
459 filter_sys_t *p_sys = p_filter->p_sys;
461 msg_Dbg( p_filter, "Open socket to vnc server on %s:%u.",
462 p_sys->psz_host, p_sys->i_port );
464 p_sys->i_socket = net_ConnectTCP( p_filter, p_sys->psz_host, p_sys->i_port );
466 if( p_sys->i_socket < 0 )
468 msg_Err( p_filter, "Could not open socket" );
472 msg_Dbg( p_filter, "socket is open." );
477 static bool handshaking ( filter_t *p_filter )
479 filter_sys_t *p_sys = p_filter->p_sys;
481 msg_Dbg( p_filter, "Reading protocol version" );
483 rfbProtocolVersionMsg pv;
484 if ( !read_exact( p_filter, p_sys->i_socket, pv,
485 sz_rfbProtocolVersionMsg ) )
487 msg_Err( p_filter, "Could not read version message" );
490 pv[sz_rfbProtocolVersionMsg] = '\0'; /* pv size is sz_rfbProtocolVersionMsg+1 */
492 msg_Dbg( p_filter, "Server version is %s", pv );
494 strncpy(pv, "RFB 003.003\n", sz_rfbProtocolVersionMsg);
496 if( !write_exact(p_filter, p_sys->i_socket, pv,
497 sz_rfbProtocolVersionMsg) )
499 msg_Err( p_filter, "Could not write version message" );
503 msg_Dbg( p_filter, "Reading authentication scheme" );
504 uint32_t i_authScheme;
505 if( !read_exact( p_filter, p_sys->i_socket, (char*)&i_authScheme, 4 ) )
507 msg_Err( p_filter, "Could not read authentication scheme" );
510 i_authScheme = htonl(i_authScheme);
512 msg_Dbg( p_filter, "Authentication scheme = %x", i_authScheme );
513 if ( i_authScheme == rfbConnFailed )
515 msg_Err( p_filter, "Connection rejected by server" );
518 if (i_authScheme == rfbVncAuth)
520 unsigned char challenge[CHALLENGESIZE];
521 if ( !read_exact( p_filter, p_sys->i_socket,
522 (char*)challenge, CHALLENGESIZE ) )
524 msg_Err( p_filter, "Could not read password challenge" );
528 vnc_encrypt_bytes( challenge, p_sys->psz_passwd );
530 if( !write_exact(p_filter, p_sys->i_socket,
531 (char*)challenge, CHALLENGESIZE ) )
533 msg_Err( p_filter, "Could not write password" );
536 uint32_t i_authResult;
537 if( !read_exact( p_filter, p_sys->i_socket, (char*)&i_authResult, 4 ) )
539 msg_Err( p_filter, "Could not read authentication result" );
542 i_authResult = htonl(i_authResult);
543 if (i_authResult != rfbVncAuthOK)
545 msg_Err( p_filter, "VNC authentication failed" );
550 msg_Dbg( p_filter, "Writing client init message" );
553 if( !write_exact( p_filter, p_sys->i_socket,
554 (char*)&ci, sz_rfbClientInitMsg ) )
556 msg_Err( p_filter, "Could not write client init message" );
560 msg_Dbg( p_filter, "Reading server init message" );
562 if( !read_exact( p_filter, p_sys->i_socket,
563 (char*)&si, sz_rfbServerInitMsg ) )
565 msg_Err( p_filter, "Could not read server init message" );
568 si.framebufferWidth = htons(si.framebufferWidth);
569 si.framebufferHeight = htons(si.framebufferHeight);
570 si.format.redMax = htons(si.format.redMax);
571 si.format.greenMax = htons(si.format.greenMax);
572 si.format.blueMax = htons(si.format.blueMax);
574 p_sys->i_vnc_width = si.framebufferWidth;
575 p_sys->i_vnc_height = si.framebufferHeight;
577 msg_Dbg( p_filter, "Servers preferred pixelformat: "
578 "%ux%u, R(%u),G(%u),B(%u), %u bit, depht=%u, %s",
580 si.framebufferHeight,
584 si.format.bitsPerPixel,
586 si.format.trueColour ? "TrueColor" : "Not-TrueColor");
588 uint32_t i_nameLength = htonl(si.nameLength);
589 if( i_nameLength > MAX_VNC_SERVER_NAME_LENGTH )
591 msg_Err( p_filter, "Server name too long" );
594 char s_ServerName[MAX_VNC_SERVER_NAME_LENGTH+1];
596 msg_Dbg( p_filter, "Reading server name with size = %u", i_nameLength );
597 if( !read_exact( p_filter, p_sys->i_socket, s_ServerName, i_nameLength ) )
599 msg_Err( p_filter, "Could not read server name" );
602 s_ServerName[i_nameLength] = '\0';
604 if( strcmp( s_ServerName, "VDR-OSD") == 0 )
606 msg_Dbg( p_filter, "Server is a VDR" );
607 p_sys->b_alpha_from_vnc = true;
611 msg_Dbg( p_filter, "Server is a normal VNC" );
612 p_sys->b_alpha_from_vnc = false;
616 msg_Dbg( p_filter, "Server init message read properly" );
617 msg_Dbg( p_filter, "Server name is %s", s_ServerName );
619 msg_Dbg( p_filter, "Writing SetPixelFormat message" );
621 rfbSetPixelFormatMsg sp;
622 sp.type = rfbSetPixelFormat;
623 sp.format.bitsPerPixel = 8;
624 sp.format.depth = 8 ;
625 sp.format.bigEndian = 1;
626 sp.format.trueColour = 0;
627 sp.format.redMax = htons(31);
628 sp.format.greenMax = htons(31);
629 sp.format.blueMax = htons(31);
630 sp.format.redShift = 10;
631 sp.format.greenShift = 5;
632 sp.format.blueShift = 0;
634 if( !write_exact( p_filter, p_sys->i_socket,
635 (char*)&sp, sz_rfbSetPixelFormatMsg) )
637 msg_Err( p_filter, "Could not write SetPixelFormat message" );
641 msg_Dbg( p_filter, "Writing SetEncodings message" );
643 rfbSetEncodingsMsg se;
644 se.type = rfbSetEncodings;
645 se.nEncodings = htons( p_sys->b_alpha_from_vnc ? 3 : 2 );
647 if( !write_exact( p_filter, p_sys->i_socket,
648 (char*)&se, sz_rfbSetEncodingsMsg) )
650 msg_Err( p_filter, "Could not write SetEncodings message begin" );
656 msg_Dbg( p_filter, "Writing SetEncodings rfbEncodingCopyRect" );
657 i_encoding = htonl(rfbEncodingCopyRect);
658 if( !write_exact( p_filter, p_sys->i_socket, (char*)&i_encoding, 4) )
660 msg_Err( p_filter, "Could not write encoding type rfbEncodingCopyRect." );
664 msg_Dbg( p_filter, "Writing SetEncodings rfbEncodingRRE" );
665 i_encoding = htonl(rfbEncodingRRE);
666 if( !write_exact(p_filter, p_sys->i_socket, (char*)&i_encoding, 4) )
668 msg_Err( p_filter, "Could not write encoding type rfbEncodingRRE." );
672 if( p_sys->b_alpha_from_vnc )
674 msg_Dbg( p_filter, "Writing SetEncodings rfbEncSpecialUseAlpha" );
675 i_encoding = 0x00F0FFFF; /* rfbEncSpecialUseAlpha is 0xFFFFF000
676 * before we swap it */
677 if( !write_exact(p_filter, p_sys->i_socket, (char*)&i_encoding, 4) )
679 msg_Err( p_filter, "Could not write encoding type rfbEncSpecialUseAlpha." );
687 static void vnc_worker_thread( vlc_object_t *p_thread_obj )
689 filter_t* p_filter = (filter_t*)(p_thread_obj->p_parent);
690 filter_sys_t *p_sys = p_filter->p_sys;
692 msg_Dbg( p_filter, "VNC worker thread started" );
694 if ( open_vnc_connection ( p_filter ) == false )
696 msg_Err( p_filter, "Could not connect to vnc host" );
700 if ( handshaking ( p_filter ) == false )
702 msg_Err( p_filter, "Error occured while handshaking vnc host" );
706 p_sys->b_connection_active = true; /* to enable sending key
707 * and mouse events to host */
709 /* Create an empty picture for VNC the data */
710 vlc_mutex_lock( &p_sys->lock );
711 p_sys->p_pic = malloc( sizeof(picture_t) );
714 vlc_mutex_unlock( &p_sys->lock );
717 vout_AllocatePicture( VLC_OBJECT(p_filter), p_sys->p_pic,
718 VLC_FOURCC('Y','U','V','A'),
721 VOUT_ASPECT_FACTOR );
722 if( !p_sys->p_pic->i_planes )
724 free( p_sys->p_pic );
726 vlc_mutex_unlock( &p_sys->lock );
729 p_sys->i_vnc_pixels = p_sys->i_vnc_width * p_sys->i_vnc_height;
731 vlc_mutex_unlock( &p_sys->lock );
733 /* create the update request thread */
734 p_sys->p_update_request_thread = vlc_object_create( p_filter,
735 VLC_OBJECT_GENERIC );
736 vlc_object_attach( p_sys->p_update_request_thread, p_filter );
737 if( vlc_thread_create( p_sys->p_update_request_thread,
738 "vnc update request thread",
739 update_request_thread,
740 VLC_THREAD_PRIORITY_LOW, false ) )
742 vlc_object_detach( p_sys->p_update_request_thread );
743 vlc_object_release( p_sys->p_update_request_thread );
744 p_sys->p_update_request_thread = NULL;
745 msg_Err( p_filter, "cannot spawn vnc update request thread" );
749 /* connection is initialized, now read and handle server messages */
753 rfbServerToClientMsg msg;
755 while (p_sys->b_continue)
758 if ( !read_exact(p_filter, p_sys->i_socket, (char*)&msg, 1 ) )
760 msg_Err( p_filter, "Error while waiting for next server message");
761 p_sys->b_continue = false;
767 case rfbFramebufferUpdate:
768 i_msgSize = sz_rfbFramebufferUpdateMsg;
770 case rfbSetColourMapEntries:
771 i_msgSize = sz_rfbSetColourMapEntriesMsg;
774 i_msgSize = sz_rfbBellMsg;
776 case rfbServerCutText:
777 i_msgSize = sz_rfbServerCutTextMsg;
779 case rfbReSizeFrameBuffer:
780 i_msgSize = sz_rfbReSizeFrameBufferMsg;
784 msg_Err( p_filter, "Invalid message %u received", msg.type );
785 p_sys->b_continue = false;
787 if (p_sys->b_continue == true)
791 if ( !read_exact( p_filter, p_sys->i_socket,
792 ((char*)&msg)+1, i_msgSize ) )
794 msg_Err( p_filter, "Error while reading message of type %u",
796 p_sys->b_continue = false;
800 process_server_message( p_filter, &msg);
805 process_server_message( p_filter, &msg);
811 if (p_sys->p_worker_thread->b_die ||
812 p_sys->p_worker_thread->b_error)
814 p_sys->b_continue = false;
819 msg_Dbg( p_filter, "VNC message reader thread ended" );
823 static void update_request_thread( vlc_object_t *p_thread_obj )
825 filter_t* p_filter = (filter_t*)(p_thread_obj->p_parent);
826 filter_sys_t *p_sys = p_filter->p_sys;
828 msg_Dbg( p_filter, "VNC update request thread started" );
830 rfbFramebufferUpdateRequestMsg udr;
831 udr.type = rfbFramebufferUpdateRequest;
835 udr.w = htons(p_sys->i_vnc_width);
836 udr.h = htons(p_sys->i_vnc_height);
838 if( write_exact(p_filter, p_sys->i_socket, (char*)&udr,
839 sz_rfbFramebufferUpdateRequestMsg) == false)
841 msg_Err( p_filter, "Could not write rfbFramebufferUpdateRequestMsg." );
842 p_sys->b_continue = false;
846 mtime_t i_poll_interval_microsec = p_sys->i_vnc_poll_interval * 1000;
848 if (p_sys->b_vnc_poll)
850 while ( p_sys->b_continue == true )
852 msleep( i_poll_interval_microsec );
853 if( write_exact(p_filter, p_sys->i_socket, (char*)&udr,
854 sz_rfbFramebufferUpdateRequestMsg) == false)
856 msg_Err( p_filter, "Could not write rfbFramebufferUpdateRequestMsg." );
857 p_sys->b_continue = false;
859 if (p_sys->p_update_request_thread->b_die ||
860 p_sys->p_update_request_thread->b_error)
862 p_sys->b_continue = false;
868 msg_Dbg( p_filter, "VNC polling disabled." );
870 msg_Dbg( p_filter, "VNC update request thread ended" );
873 static bool process_server_message ( filter_t *p_filter,
874 rfbServerToClientMsg *msg )
876 filter_sys_t *p_sys = p_filter->p_sys;
880 case rfbFramebufferUpdate:
882 msg->fu.nRects = htons(msg->fu.nRects);
883 rfbFramebufferUpdateRectHeader hdr;
885 for (int i_rect = 0; i_rect < msg->fu.nRects; i_rect++)
887 if (!read_exact(p_filter, p_sys->i_socket, (char*)&hdr,
888 sz_rfbFramebufferUpdateRectHeader ) )
890 msg_Err( p_filter, "Could not read FrameBufferUpdate header" );
893 hdr.r.x = htons(hdr.r.x);
894 hdr.r.y = htons(hdr.r.y);
895 hdr.r.w = htons(hdr.r.w);
896 hdr.r.h = htons(hdr.r.h);
897 hdr.encoding = htonl(hdr.encoding);
899 switch (hdr.encoding)
904 for (i_line = 0; i_line < hdr.r.h; i_line++)
906 if ( !read_exact( p_filter, p_sys->i_socket,
907 p_sys->read_buffer, hdr.r.w ) )
910 "Could not read FrameBufferUpdate line data" );
913 vlc_mutex_lock( &p_sys->lock );
914 if ( !raw_line( p_sys, hdr.r.x,
918 msg_Err( p_filter, "raw_line failed." );
919 vlc_mutex_unlock( &p_sys->lock );
922 vlc_mutex_unlock( &p_sys->lock );
927 case rfbEncodingCopyRect:
930 "Rect in unsupported encoding rfbEncodingCopyRect" );
937 if ( !read_exact( p_filter, p_sys->i_socket,
941 msg_Err( p_filter, "Could not read rfbRREHeader" );
945 if ( !read_exact(p_filter, p_sys->i_socket,
946 (char*)&i_pixcolor, 1 ) )
948 msg_Err( p_filter, "Could not read RRE pixcolor" );
952 vlc_mutex_lock( &p_sys->lock );
953 if ( !fill_rect( p_sys,
958 msg_Err( p_filter, "main fill_rect failed." );
959 vlc_mutex_unlock( &p_sys->lock );
962 vlc_mutex_unlock( &p_sys->lock );
964 rrehdr.nSubrects = htonl(rrehdr.nSubrects);
966 int i_datasize = rrehdr.nSubrects *
967 ( sizeof(i_pixcolor) + sz_rfbRectangle ) ;
968 if ( i_datasize > READ_BUFFER_SIZE )
970 msg_Err( p_filter, "Buffer too small, "
971 "need %u bytes", i_datasize );
974 if ( !read_exact( p_filter, p_sys->i_socket,
975 p_sys->read_buffer, i_datasize ) )
978 "Could not read RRE subrect data" );
983 rfbRectangle* p_subrect;
985 vlc_mutex_lock( &p_sys->lock );
987 i_subrect < rrehdr.nSubrects; i_subrect++)
989 i_pixcolor = p_sys->read_buffer[i_offset];
990 i_offset += sizeof(i_pixcolor);
992 (rfbRectangle*)(p_sys->read_buffer + i_offset);
993 i_offset += sz_rfbRectangle;
995 if (!fill_rect( p_sys,
996 htons(p_subrect->x) + hdr.r.x,
997 htons(p_subrect->y) + hdr.r.y,
1003 "subrect %u fill_rect failed.", i_subrect );
1004 vlc_mutex_unlock( &p_sys->lock );
1008 vlc_mutex_unlock( &p_sys->lock );
1015 vlc_mutex_lock( &p_sys->lock );
1016 p_sys->b_need_update = true;
1017 vlc_mutex_unlock( &p_sys->lock );
1021 case rfbSetColourMapEntries:
1023 msg->scme.nColours = htons(msg->scme.nColours);
1024 msg->scme.firstColour = htons(msg->scme.firstColour);
1026 if ( p_sys->b_alpha_from_vnc == true )
1028 i_datasize = 2 * msg->scme.nColours * 4;
1032 i_datasize = 2 * msg->scme.nColours * 3;
1034 if ( i_datasize > READ_BUFFER_SIZE )
1036 msg_Err( p_filter, "Buffer too small, need %u bytes",
1041 if ( !read_exact( p_filter, p_sys->i_socket,
1042 p_sys->read_buffer, i_datasize ) )
1044 msg_Err( p_filter, "Could not read color map data" );
1048 uint8_t i_red, i_green, i_blue, i_alpha, i_color_index;
1049 uint16_t i_offset = 0;
1052 for (int i = 0; i < msg->scme.nColours; i++)
1054 i_color_index = i+msg->scme.firstColour;
1055 if ( p_sys->b_alpha_from_vnc == true )
1057 i_alpha = p_sys->read_buffer[i_offset];
1060 i_red = p_sys->read_buffer[i_offset];
1062 i_green = p_sys->read_buffer[i_offset];
1064 i_blue = p_sys->read_buffer[i_offset];
1066 rgb_to_yuv( &p_sys->ar_color_table_yuv[i_color_index][0],
1067 &p_sys->ar_color_table_yuv[i_color_index][1],
1068 &p_sys->ar_color_table_yuv[i_color_index][2],
1072 p_sys->ar_color_table_yuv[i][3] = i_alpha;
1078 msg_Err( p_filter, "rfbBell received" );
1081 case rfbServerCutText:
1082 msg->sct.length = htons(msg->sct.length);
1083 if ( msg->sct.length > READ_BUFFER_SIZE )
1085 msg_Err( p_filter, "Buffer too small, need %u bytes", msg->sct.length );
1088 if ( !read_exact(p_filter, p_sys->i_socket,
1089 p_sys->read_buffer, msg->sct.length ) )
1091 msg_Err( p_filter, "Could not read Reading rfbServerCutText data" );
1096 case rfbReSizeFrameBuffer:
1097 msg_Err( p_filter, "Reading rfbReSizeFrameBuffer not implemented" );
1101 msg_Err( p_filter, "Invalid message %u received", msg->type );
1107 /****************************************************************************
1108 * Filter: the whole thing
1109 ****************************************************************************
1110 * This function outputs subpictures at regular time intervals.
1111 ****************************************************************************/
1112 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
1114 filter_sys_t *p_sys = p_filter->p_sys;
1115 subpicture_t *p_spu;
1116 subpicture_region_t *p_region;
1120 if( !p_sys->b_need_update )
1125 vlc_mutex_lock( &p_sys->lock );
1127 p_pic = p_sys->p_pic;
1131 vlc_mutex_unlock( &p_sys->lock );
1135 /* Allocate the subpicture internal data. */
1136 p_spu = p_filter->pf_sub_buffer_new( p_filter );
1139 vlc_mutex_unlock( &p_sys->lock );
1143 p_spu->b_absolute = false;
1144 p_spu->i_start = date;
1146 p_spu->b_ephemer = true;
1148 /* Create new SPU region */
1149 memset( &fmt, 0, sizeof(video_format_t) );
1150 fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
1151 fmt.i_aspect = VOUT_ASPECT_FACTOR;
1152 fmt.i_sar_num = fmt.i_sar_den = 1;
1153 fmt.i_width = fmt.i_visible_width = p_pic->p[Y_PLANE].i_visible_pitch;
1154 fmt.i_height = fmt.i_visible_height = p_pic->p[Y_PLANE].i_visible_lines;
1155 fmt.i_x_offset = fmt.i_y_offset = 0;
1156 p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
1159 msg_Err( p_filter, "cannot allocate SPU region" );
1160 p_filter->pf_sub_buffer_del( p_filter, p_spu );
1161 vlc_mutex_unlock( &p_sys->lock );
1165 vout_CopyPicture( p_filter, &p_region->picture, p_pic );
1167 p_sys->b_need_update = false;
1169 vlc_mutex_unlock( &p_sys->lock );
1171 /* set to one of the 9 relative locations */
1172 p_region->i_align = 0; //=CENTER
1173 p_spu->b_absolute = false;
1179 p_spu->p_region = p_region;
1181 p_spu->i_alpha = ( p_sys->i_alpha );
1187 static inline void rgb_to_yuv( uint8_t *y, uint8_t *u, uint8_t *v,
1188 int r, int g, int b )
1190 *y = ( ( ( 66 * r + 129 * g + 25 * b + 128 ) >> 8 ) + 16 );
1191 *u = ( ( -38 * r - 74 * g + 112 * b + 128 ) >> 8 ) + 128 ;
1192 *v = ( ( 112 * r - 94 * g - 18 * b + 128 ) >> 8 ) + 128 ;
1195 static inline bool fill_rect( filter_sys_t* p_sys,
1196 uint16_t i_x, uint16_t i_y,
1197 uint16_t i_w, uint16_t i_h,
1200 plane_t *p_outY = p_sys->p_pic->p+Y_PLANE;
1201 plane_t *p_outU = p_sys->p_pic->p+U_PLANE;
1202 plane_t *p_outV = p_sys->p_pic->p+V_PLANE;
1203 plane_t *p_outA = p_sys->p_pic->p+A_PLANE;
1204 int i_pitch = p_outY->i_pitch;
1205 int i_lines = p_outY->i_lines;
1206 if ( i_x + i_w > i_pitch)
1208 if ( i_y + i_h > i_lines)
1210 int i_line_offset = i_y * i_pitch;
1211 uint8_t i_yuv_y = p_sys->ar_color_table_yuv[i_color][0];
1212 uint8_t i_yuv_u = p_sys->ar_color_table_yuv[i_color][1];
1213 uint8_t i_yuv_v = p_sys->ar_color_table_yuv[i_color][2];
1214 uint8_t i_alpha = p_sys->ar_color_table_yuv[i_color][3];
1215 for( int i_line = 0; i_line < i_h; i_line++ )
1217 for( int i_column = 0; i_column < i_w; i_column++ )
1219 int i_total_offset = i_line_offset + i_x + i_column;
1220 p_outY->p_pixels[ i_total_offset ] = i_yuv_y;
1221 p_outU->p_pixels[ i_total_offset ] = i_yuv_u;
1222 p_outV->p_pixels[ i_total_offset ] = i_yuv_v;
1223 p_outA->p_pixels[ i_total_offset ] = i_alpha;
1225 i_line_offset += i_pitch;
1230 static inline bool raw_line( filter_sys_t* p_sys,
1231 uint16_t i_x, uint16_t i_y,
1234 plane_t *p_outY = p_sys->p_pic->p+Y_PLANE;
1235 plane_t *p_outU = p_sys->p_pic->p+U_PLANE;
1236 plane_t *p_outV = p_sys->p_pic->p+V_PLANE;
1237 plane_t *p_outA = p_sys->p_pic->p+A_PLANE;
1238 int i_pitch = p_outY->i_pitch;
1239 int i_lines = p_outY->i_lines;
1240 if ( i_x + i_w > i_pitch)
1245 int i_line_offset = i_y * i_pitch + i_x;
1247 for( int i_column = 0; i_column < i_w; i_column++ )
1249 int i_offset = i_line_offset + i_column;
1250 uint8_t i_color = p_sys->read_buffer[i_column];
1251 p_outY->p_pixels[ i_offset ] = p_sys->ar_color_table_yuv[i_color][0];
1252 p_outU->p_pixels[ i_offset ] = p_sys->ar_color_table_yuv[i_color][1];
1253 p_outV->p_pixels[ i_offset ] = p_sys->ar_color_table_yuv[i_color][2];
1254 p_outA->p_pixels[ i_offset ] = p_sys->ar_color_table_yuv[i_color][3];
1261 /*****************************************************************************
1262 * MouseEvent: callback for mouse events
1263 *****************************************************************************/
1264 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
1265 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1267 VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(psz_var);
1269 filter_t *p_filter = (filter_t *)p_data;
1270 filter_sys_t *p_sys = p_filter->p_sys;
1272 if( !p_sys->b_connection_active )
1275 if( !p_sys->b_vnc_mouse_events )
1278 vout_thread_t *p_vout = (vout_thread_t*)p_sys->p_vout;
1282 int v_h = p_vout->output.i_height;
1283 int v_w = p_vout->output.i_width;
1285 i_v = var_GetInteger( p_sys->p_vout, "mouse-button-down" );
1286 i_y = var_GetInteger( p_sys->p_vout, "mouse-y" );
1287 i_x = var_GetInteger( p_sys->p_vout, "mouse-x" );
1289 if( i_y < 0 || i_x < 0 || i_y >= v_h || i_x >= v_w )
1291 msg_Dbg( p_this, "invalid mouse event? x=%d y=%d btn=%x", i_x, i_y, i_v );
1295 msg_Dbg( p_this, "mouse event x=%d y=%d btn=%x", i_x, i_y, i_v );
1298 /* FIXME: calculate x and y coordinates for scaled output */
1300 /* buttonMask bits 0-7 are buttons 1-8, 0=up, 1=down */
1301 rfbPointerEventMsg ev;
1302 ev.type = rfbPointerEvent;
1303 ev.buttonMask = i_v;
1307 write_exact( p_filter, p_sys->i_socket,
1308 (char*)&ev, sz_rfbPointerEventMsg);
1313 /*****************************************************************************
1314 * KeyEvent: callback for keyboard events
1315 *****************************************************************************/
1316 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
1317 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1319 VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
1321 filter_t *p_filter = (filter_t *)p_data;
1322 filter_sys_t *p_sys = p_filter->p_sys;
1324 if( !p_sys->b_connection_active )
1327 if( !p_sys->b_vnc_key_events )
1330 msg_Dbg( p_this, "key pressed (%d) ", newval.i_int );
1332 if ( !newval.i_int )
1334 msg_Err( p_this, "Received invalid key event %d", newval.i_int );
1335 return VLC_EGENERIC;
1338 uint32_t i_key32 = newval.i_int;
1339 i_key32 = htonl(i_key32);
1341 ev.type = rfbKeyEvent;
1344 /* first key-down for modifier-keys */
1345 if (newval.i_int & KEY_MODIFIER_CTRL)
1348 write_exact( p_filter, p_sys->i_socket,
1349 (char*)&ev, sz_rfbKeyEventMsg);
1351 if (newval.i_int & KEY_MODIFIER_SHIFT)
1354 write_exact( p_filter, p_sys->i_socket,
1355 (char*)&ev, sz_rfbKeyEventMsg);
1357 if (newval.i_int & KEY_MODIFIER_ALT)
1360 write_exact( p_filter, p_sys->i_socket,
1361 (char*)&ev, sz_rfbKeyEventMsg);
1364 /* then key-down for the pressed key */
1366 write_exact( p_filter, p_sys->i_socket,
1367 (char*)&ev, sz_rfbKeyEventMsg);
1371 /* then key-up for the pressed key */
1372 write_exact( p_filter, p_sys->i_socket,
1373 (char*)&ev, sz_rfbKeyEventMsg);
1375 /* last key-down for modifier-keys */
1376 if (newval.i_int & KEY_MODIFIER_CTRL)
1379 write_exact( p_filter, p_sys->i_socket,
1380 (char*)&ev, sz_rfbKeyEventMsg);
1382 if (newval.i_int & KEY_MODIFIER_SHIFT)
1385 write_exact( p_filter, p_sys->i_socket,
1386 (char*)&ev, sz_rfbKeyEventMsg);
1388 if (newval.i_int & KEY_MODIFIER_ALT)
1391 write_exact( p_filter, p_sys->i_socket,
1392 (char*)&ev, sz_rfbKeyEventMsg);
1398 static void vnc_encrypt_bytes( unsigned char *bytes, char *passwd )
1400 unsigned char key[8];
1403 for (i = 0; i < 8; i++)
1404 key[i] = i < strlen( passwd ) ? passwd[i] : '\0';
1406 gcry_cipher_hd_t ctx;
1407 gcry_cipher_open( &ctx, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB,0);
1409 /* reverse bits of the key */
1410 for( i = 0 ; i < 8 ; i ++ )
1413 (((key[i] >> 6) & 0x01 ) << 1 ) +
1414 (((key[i] >> 5) & 0x01 ) << 2 ) +
1415 (((key[i] >> 4) & 0x01 ) << 3 ) +
1416 (((key[i] >> 3) & 0x01 ) << 4 ) +
1417 (((key[i] >> 2) & 0x01 ) << 5 ) +
1418 (((key[i] >> 1) & 0x01 ) << 6 ) +
1419 ((key[i] & 0x01) << 7 );
1421 gcry_cipher_setkey( ctx, key, 8 );
1422 gcry_cipher_encrypt( ctx, bytes, CHALLENGESIZE, bytes, CHALLENGESIZE );
1423 gcry_cipher_close( ctx );