demux: avi: fix left shift ub
[vlc.git] / modules / spu / remoteosd.c
blob1e23a3e234e7867e5b9d7b51ed61bcd76358f5a3
1 /*****************************************************************************
2 * remoteosd.c: remote osd over vnc filter module
3 *****************************************************************************
4 * Copyright (C) 2007-2008 Matthias Bauer
5 * $Id$
7 * Authors: Matthias Bauer <matthias dot bauer #_at_# gmx dot ch>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * 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
30 * software.
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 *****************************************************************************/
43 //#define VNC_DEBUG
45 /*****************************************************************************
46 * Preamble
47 *****************************************************************************/
49 #ifdef HAVE_CONFIG_H
50 # include "config.h"
51 #endif
53 #include <vlc_common.h>
54 #include <vlc_plugin.h>
55 #include <vlc_filter.h>
56 #include <vlc_mouse.h>
57 #include <vlc_subpicture.h>
58 #include <vlc_keys.h> /* KEY_MODIFIER_CTRL */
59 #include <vlc_network.h> /* net_*, htonl */
60 #include <gcrypt.h> /* to encrypt password */
61 #include <vlc_gcrypt.h>
63 #include "remoteosd_rfbproto.h" /* type definitions of the RFB protocol for VNC */
65 /*****************************************************************************
66 * Module descriptor
67 *****************************************************************************/
68 #define READ_BUFFER_SIZE 1000000
70 #define RMTOSD_HOST_TEXT N_("VNC Host")
71 #define RMTOSD_HOST_LONGTEXT N_( \
72 "VNC hostname or IP address." )
74 #define RMTOSD_PORT_TEXT N_("VNC Port")
75 #define RMTOSD_PORT_LONGTEXT N_( \
76 "VNC port number." )
78 #define RMTOSD_PASSWORD_TEXT N_("VNC Password")
79 #define RMTOSD_PASSWORD_LONGTEXT N_( \
80 "VNC password." )
82 #define RMTOSD_UPDATE_TEXT N_("VNC poll interval" )
83 #define RMTOSD_UPDATE_LONGTEXT N_( \
84 "In this interval an update from VNC is requested, default every 300 ms. ")
86 #define RMTOSD_POLL_TEXT N_("VNC polling")
87 #define RMTOSD_POLL_LONGTEXT N_( \
88 "Activate VNC polling. Do NOT activate for use as VDR ffnetdev client." )
90 #define RMTOSD_MOUSE_TEXT N_("Mouse events")
91 #define RMTOSD_MOUSE_LONGTEXT N_( \
92 "Send mouse events to VNC host. Not needed for use as VDR ffnetdev client." )
94 #define RMTOSD_KEYS_TEXT N_("Key events")
95 #define RMTOSD_KEYS_LONGTEXT N_( \
96 "Send key events to VNC host." )
98 #define RMTOSD_ALPHA_TEXT N_("Alpha transparency value (default 255)")
99 #define RMTOSD_ALPHA_LONGTEXT N_( \
100 "The transparency of the OSD VNC can be changed by giving a value " \
101 "between 0 and 255. A lower value specifies more transparency a higher " \
102 "means less transparency. The default is being not transparent " \
103 "(value 255) the minimum is fully transparent (value 0)." )
105 #define RMTOSD_CFG "rmtosd-"
107 #define RMTOSD_UPDATE_MIN 200
108 #define RMTOSD_UPDATE_DEFAULT 1000
109 #define RMTOSD_UPDATE_MAX 300
111 static int CreateFilter ( vlc_object_t * );
112 static void DestroyFilter( vlc_object_t * );
114 vlc_module_begin ()
115 set_description( N_("Remote-OSD over VNC") )
116 set_capability( "sub source", 100 )
117 set_shortname( N_("Remote-OSD") )
118 set_category( CAT_VIDEO )
119 set_subcategory( SUBCAT_VIDEO_SUBPIC )
120 add_shortcut( "rmtosd" )
121 set_callbacks( CreateFilter, DestroyFilter )
123 add_string( RMTOSD_CFG "host", "myvdr", RMTOSD_HOST_TEXT,
124 RMTOSD_HOST_LONGTEXT, false )
125 add_integer_with_range( RMTOSD_CFG "port", 20001, 1, 0xFFFF,
126 RMTOSD_PORT_TEXT, RMTOSD_PORT_LONGTEXT, false )
127 add_password( RMTOSD_CFG "password", "", RMTOSD_PASSWORD_TEXT,
128 RMTOSD_PASSWORD_LONGTEXT, false )
129 add_integer_with_range( RMTOSD_CFG "update", RMTOSD_UPDATE_DEFAULT,
130 RMTOSD_UPDATE_MIN, RMTOSD_UPDATE_MAX, RMTOSD_UPDATE_TEXT,
131 RMTOSD_UPDATE_LONGTEXT, true )
132 add_bool( RMTOSD_CFG "vnc-polling", false,
133 RMTOSD_POLL_TEXT , RMTOSD_POLL_LONGTEXT, false )
134 add_bool( RMTOSD_CFG "mouse-events", false,
135 RMTOSD_MOUSE_TEXT , RMTOSD_MOUSE_LONGTEXT, false )
136 add_bool( RMTOSD_CFG "key-events", false,
137 RMTOSD_KEYS_TEXT , RMTOSD_KEYS_LONGTEXT, false )
138 add_integer_with_range( RMTOSD_CFG "alpha", 255, 0, 255,
139 RMTOSD_ALPHA_TEXT, RMTOSD_ALPHA_LONGTEXT, true )
141 vlc_module_end ()
144 /*****************************************************************************
145 * Local prototypes
146 *****************************************************************************/
147 #define CHALLENGESIZE 16
148 #define MAX_VNC_SERVER_NAME_LENGTH 255
150 /* subsource functions */
151 static subpicture_t *Filter( filter_t *, mtime_t );
153 static int MouseEvent( filter_t *,
154 const vlc_mouse_t *,
155 const vlc_mouse_t *,
156 const video_format_t * );
158 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
159 vlc_value_t oldval, vlc_value_t newval, void *p_data );
161 static void* vnc_worker_thread ( void * );
163 static void* update_request_thread( void * );
165 static bool process_server_message ( filter_t *p_filter,
166 rfbServerToClientMsg *msg );
168 static inline void rgb_to_yuv( uint8_t *y, uint8_t *u, uint8_t *v,
169 int r, int g, int b );
171 static inline bool fill_rect( filter_sys_t* p_sys,
172 uint16_t i_x, uint16_t i_y,
173 uint16_t i_w, uint16_t i_h,
174 uint8_t i_color );
175 static inline bool copy_rect( filter_sys_t* p_sys,
176 uint16_t i_x, uint16_t i_y,
177 uint16_t i_w, uint16_t i_h,
178 uint16_t i_sx, uint16_t i_sy );
181 static inline bool raw_line( filter_sys_t* p_sys,
182 uint16_t i_x, uint16_t i_y,
183 uint16_t i_w );
185 static void vnc_encrypt_bytes( unsigned char *bytes, char *passwd );
188 /*****************************************************************************
189 * Sub source code
190 *****************************************************************************/
192 /*****************************************************************************
193 * Local prototypes
194 *****************************************************************************/
195 struct filter_sys_t
197 vlc_mutex_t lock; /* To lock for read/write on picture */
199 bool b_need_update; /* VNC picture is updated, do update the OSD*/
200 uint8_t i_alpha; /* alpha transparency value */
202 char *psz_host; /* VNC host */
204 char *psz_passwd; /* VNC password */
206 picture_t *p_pic; /* The picture with OSD data from VNC */
208 int i_socket; /* Socket used for VNC */
210 uint16_t i_vnc_width; /* The with of the VNC screen */
211 uint16_t i_vnc_height; /* The height of the VNC screen */
213 bool b_vnc_key_events; /* Send KeyEvents ? */
214 bool b_alpha_from_vnc; /* Special ffnetdev alpha feature enabled ? */
216 char read_buffer[READ_BUFFER_SIZE];
218 vlc_thread_t worker_thread;
220 uint8_t ar_color_table_yuv[256][4];
223 /*****************************************************************************
224 * CreateFilter: Create the filter and open the definition file
225 *****************************************************************************/
226 static int CreateFilter ( vlc_object_t *p_this )
228 filter_t *p_filter = (filter_t *)p_this;
230 filter_sys_t *p_sys = malloc( sizeof (*p_sys) );
231 if( unlikely(p_sys == NULL) )
232 return VLC_ENOMEM;
234 /* Populating struct */
235 vlc_mutex_init( &p_sys->lock );
236 p_sys->b_need_update = false;
237 p_sys->psz_host = var_InheritString( p_this, RMTOSD_CFG "host" );
238 p_sys->psz_passwd = var_InheritString( p_this, RMTOSD_CFG "password" );
239 p_sys->i_alpha = var_InheritInteger( p_this, RMTOSD_CFG "alpha" );
240 p_sys->p_pic = NULL;
241 p_sys->i_socket = -1;
243 memset( p_sys->ar_color_table_yuv, 255,
244 sizeof( p_sys->ar_color_table_yuv ) );
246 if( p_sys->psz_host == NULL )
248 msg_Err( p_filter, "unable to get vnc host" );
249 goto error;
252 if( p_sys->psz_passwd == NULL )
254 msg_Err( p_filter, "unable to get vnc password" );
255 goto error;
258 p_filter->p_sys = p_sys;
260 vlc_gcrypt_init();
262 /* create the vnc worker thread */
263 if( vlc_clone( &p_sys->worker_thread,
264 vnc_worker_thread, p_filter, VLC_THREAD_PRIORITY_LOW ) )
266 msg_Err( p_filter, "cannot spawn vnc message reader thread" );
267 goto error;
270 /* Attach subpicture source callback */
271 p_filter->pf_sub_source = Filter;
273 es_format_Init( &p_filter->fmt_out, SPU_ES, VLC_CODEC_SPU );
274 p_filter->fmt_out.i_priority = ES_PRIORITY_SELECTABLE_MIN;
276 if( var_InheritBool( p_this, RMTOSD_CFG "mouse-events" ) )
277 p_filter->pf_sub_mouse = MouseEvent;
279 p_sys->b_vnc_key_events = var_InheritBool( p_this,
280 RMTOSD_CFG "key-events" );
281 if( p_sys->b_vnc_key_events )
282 var_AddCallback( p_filter->obj.libvlc, "key-pressed", KeyEvent, p_this );
284 msg_Dbg( p_filter, "osdvnc filter started" );
286 return VLC_SUCCESS;
288 error:
289 msg_Err( p_filter, "osdvnc filter discarded" );
291 vlc_mutex_destroy( &p_sys->lock );
292 free( p_sys->psz_host );
293 free( p_sys->psz_passwd );
294 free( p_sys );
296 return VLC_EGENERIC;
299 /*****************************************************************************
300 * DestroyFilter: Make a clean exit of this plugin
301 *****************************************************************************/
302 static void DestroyFilter( vlc_object_t *p_this )
304 filter_t *p_filter = (filter_t*)p_this;
305 filter_sys_t *p_sys = p_filter->p_sys;
307 msg_Dbg( p_filter, "DestroyFilter called." );
309 if( p_sys->b_vnc_key_events )
310 var_DelCallback( p_filter->obj.libvlc, "key-pressed", KeyEvent, p_this );
312 vlc_cancel( p_sys->worker_thread );
313 vlc_join( p_sys->worker_thread, NULL );
315 if( p_sys->p_pic != NULL )
316 picture_Release( p_sys->p_pic );
317 if( p_sys->i_socket >= 0 )
318 net_Close( p_sys->i_socket );
320 vlc_mutex_destroy( &p_sys->lock );
321 free( p_sys->psz_host );
322 free( p_sys->psz_passwd );
323 free( p_sys );
326 static bool read_exact( filter_t *obj, int fd, void *buf, size_t len )
328 return (ssize_t)len == net_Read( obj, fd, buf, len );
332 static bool write_exact( filter_t *obj, int fd, const void *buf, size_t len )
334 return (ssize_t)len == net_Write( obj, fd, buf, len );
337 static int vnc_connect( filter_t *p_filter )
339 filter_sys_t *p_sys = p_filter->p_sys;
341 int port = var_InheritInteger( p_filter, RMTOSD_CFG "port" );
343 int fd = net_ConnectTCP( p_filter, p_sys->psz_host, port );
344 if( fd == -1 )
346 msg_Err( p_filter, "Could not connect to VNC host" );
347 return -1;
350 msg_Dbg( p_filter, "Reading protocol version" );
352 rfbProtocolVersionMsg pv;
353 if ( !read_exact( p_filter, fd, pv, sz_rfbProtocolVersionMsg ) )
355 msg_Err( p_filter, "Could not read version message" );
356 goto error;
358 pv[sz_rfbProtocolVersionMsg] = '\0'; /* pv size is sz_rfbProtocolVersionMsg+1 */
360 msg_Dbg( p_filter, "Server version is %s", pv );
362 strncpy(pv, "RFB 003.003\n", sz_rfbProtocolVersionMsg);
364 if( !write_exact(p_filter, fd, pv, sz_rfbProtocolVersionMsg) )
366 msg_Err( p_filter, "Could not write version message" );
367 goto error;
370 msg_Dbg( p_filter, "Reading authentication scheme" );
371 uint32_t i_authScheme;
372 if( !read_exact( p_filter, fd, &i_authScheme, 4 ) )
374 msg_Err( p_filter, "Could not read authentication scheme" );
375 goto error;
377 i_authScheme = htonl(i_authScheme);
379 msg_Dbg( p_filter, "Authentication scheme = %x", i_authScheme );
380 if ( i_authScheme == rfbConnFailed )
382 msg_Err( p_filter, "Connection rejected by server" );
383 goto error;
385 if (i_authScheme == rfbVncAuth)
387 unsigned char challenge[CHALLENGESIZE];
388 if ( !read_exact( p_filter, fd, challenge, CHALLENGESIZE ) )
390 msg_Err( p_filter, "Could not read password challenge" );
391 goto error;
394 vnc_encrypt_bytes( challenge, p_sys->psz_passwd );
396 if( !write_exact(p_filter, fd, challenge, CHALLENGESIZE ) )
398 msg_Err( p_filter, "Could not write password" );
399 goto error;
401 uint32_t i_authResult;
402 if( !read_exact( p_filter, fd, &i_authResult, 4 ) )
404 msg_Err( p_filter, "Could not read authentication result" );
405 goto error;
407 i_authResult = htonl(i_authResult);
408 if (i_authResult != rfbVncAuthOK)
410 msg_Err( p_filter, "VNC authentication failed" );
411 goto error;
415 msg_Dbg( p_filter, "Writing client init message" );
416 rfbClientInitMsg ci;
417 ci.shared = 1;
418 if( !write_exact( p_filter, fd, &ci, sz_rfbClientInitMsg ) )
420 msg_Err( p_filter, "Could not write client init message" );
421 goto error;
424 msg_Dbg( p_filter, "Reading server init message" );
425 rfbServerInitMsg si;
426 if( !read_exact( p_filter, fd, &si, sz_rfbServerInitMsg ) )
428 msg_Err( p_filter, "Could not read server init message" );
429 goto error;
431 si.framebufferWidth = htons(si.framebufferWidth);
432 si.framebufferHeight = htons(si.framebufferHeight);
433 si.format.redMax = htons(si.format.redMax);
434 si.format.greenMax = htons(si.format.greenMax);
435 si.format.blueMax = htons(si.format.blueMax);
437 p_sys->i_vnc_width = si.framebufferWidth;
438 p_sys->i_vnc_height = si.framebufferHeight;
440 msg_Dbg( p_filter, "Servers preferred pixelformat: "
441 "%ux%u, R(%u),G(%u),B(%u), %u bit, depht=%u, %s",
442 si.framebufferWidth,
443 si.framebufferHeight,
444 si.format.redMax,
445 si.format.greenMax,
446 si.format.blueMax,
447 si.format.bitsPerPixel,
448 si.format.depth,
449 si.format.trueColour ? "TrueColor" : "Not-TrueColor");
451 uint32_t i_nameLength = htonl(si.nameLength);
452 if( i_nameLength > MAX_VNC_SERVER_NAME_LENGTH )
454 msg_Err( p_filter, "Server name too long" );
455 goto error;
457 char s_ServerName[MAX_VNC_SERVER_NAME_LENGTH+1];
459 msg_Dbg( p_filter, "Reading server name with size = %u", i_nameLength );
460 if( !read_exact( p_filter, fd, s_ServerName, i_nameLength ) )
462 msg_Err( p_filter, "Could not read server name" );
463 goto error;
465 s_ServerName[i_nameLength] = '\0';
467 if( strcmp( s_ServerName, "VDR-OSD") == 0 )
469 msg_Dbg( p_filter, "Server is a VDR" );
470 p_sys->b_alpha_from_vnc = true;
472 else
474 msg_Dbg( p_filter, "Server is a normal VNC" );
475 p_sys->b_alpha_from_vnc = false;
479 msg_Dbg( p_filter, "Server init message read properly" );
480 msg_Dbg( p_filter, "Server name is %s", s_ServerName );
482 msg_Dbg( p_filter, "Writing SetPixelFormat message" );
484 rfbSetPixelFormatMsg sp;
485 sp.type = rfbSetPixelFormat;
486 sp.pad1 = sp.pad2 = 0;
487 sp.format.bitsPerPixel = 8;
488 sp.format.depth = 8 ;
489 sp.format.bigEndian = 1;
490 sp.format.trueColour = 0;
491 sp.format.redMax = htons(31);
492 sp.format.greenMax = htons(31);
493 sp.format.blueMax = htons(31);
494 sp.format.redShift = 10;
495 sp.format.greenShift = 5;
496 sp.format.blueShift = 0;
497 sp.format.pad1 = sp.format.pad2 = 0;
499 if( !write_exact( p_filter, fd, &sp, sz_rfbSetPixelFormatMsg) )
501 msg_Err( p_filter, "Could not write SetPixelFormat message" );
502 goto error;
505 msg_Dbg( p_filter, "Writing SetEncodings message" );
507 rfbSetEncodingsMsg se;
508 se.type = rfbSetEncodings;
509 se.pad = 0;
510 se.nEncodings = htons( p_sys->b_alpha_from_vnc ? 3 : 2 );
512 if( !write_exact( p_filter, fd, &se, sz_rfbSetEncodingsMsg) )
514 msg_Err( p_filter, "Could not write SetEncodings message begin" );
515 goto error;
518 uint32_t i_encoding;
520 msg_Dbg( p_filter, "Writing SetEncodings rfbEncodingCopyRect" );
521 i_encoding = htonl(rfbEncodingCopyRect);
522 if( !write_exact( p_filter, fd, &i_encoding, 4) )
524 msg_Err( p_filter, "Could not write encoding type rfbEncodingCopyRect." );
525 goto error;
528 msg_Dbg( p_filter, "Writing SetEncodings rfbEncodingRRE" );
529 i_encoding = htonl(rfbEncodingRRE);
530 if( !write_exact(p_filter, fd, &i_encoding, 4) )
532 msg_Err( p_filter, "Could not write encoding type rfbEncodingRRE." );
533 goto error;
536 if( p_sys->b_alpha_from_vnc )
538 msg_Dbg( p_filter, "Writing SetEncodings rfbEncSpecialUseAlpha" );
539 i_encoding = 0x00F0FFFF; /* rfbEncSpecialUseAlpha is 0xFFFFF000
540 * before we swap it */
541 if( !write_exact(p_filter, fd, &i_encoding, 4) )
543 msg_Err( p_filter, "Could not write encoding type rfbEncSpecialUseAlpha." );
544 goto error;
548 return fd;
549 error:
550 net_Close( fd );
551 return -1;
554 static int write_update_request(filter_t *p_filter, bool incremental)
556 filter_sys_t *p_sys = p_filter->p_sys;
557 rfbFramebufferUpdateRequestMsg udr;
559 udr.type = rfbFramebufferUpdateRequest;
560 udr.incremental = incremental;
561 udr.x = 0;
562 udr.y = 0;
563 udr.w = htons(p_sys->i_vnc_width);
564 udr.h = htons(p_sys->i_vnc_height);
566 int w = write_exact(p_filter, p_sys->i_socket, &udr,
567 sz_rfbFramebufferUpdateRequestMsg);
568 if( !w )
569 msg_Err( p_filter, "Could not write rfbFramebufferUpdateRequestMsg." );
570 return w;
573 static void update_thread_cleanup( void *p )
575 vlc_thread_t *th = p;
577 vlc_cancel( *th );
578 vlc_join( *th, NULL );
581 static void dummy_cleanup( void *p )
583 (void) p;
586 static void* vnc_worker_thread( void *obj )
588 filter_t* p_filter = (filter_t*)obj;
589 filter_sys_t *p_sys = p_filter->p_sys;
590 vlc_thread_t update_thread;
591 int canc = vlc_savecancel ();
593 msg_Dbg( p_filter, "VNC worker thread started" );
595 int fd = vnc_connect( p_filter );
596 if( fd == -1 )
598 msg_Err( p_filter, "Error occurred while handshaking VNC host" );
599 return NULL;
602 /* Create an empty picture for VNC the data */
603 picture_t *pic = picture_New( VLC_CODEC_YUVA, p_sys->i_vnc_width,
604 p_sys->i_vnc_height, 1, 1 );
605 if( likely(pic != NULL) )
607 vlc_mutex_lock( &p_sys->lock );
608 p_sys->i_socket = fd;
609 p_sys->p_pic = pic;
610 vlc_mutex_unlock( &p_sys->lock );
612 else
614 net_Close( fd );
615 return NULL;
618 write_update_request( p_filter, false );
620 /* create the update request thread */
621 bool polling = var_InheritBool( p_filter, RMTOSD_CFG "vnc-polling" );
622 if( polling
623 && vlc_clone( &update_thread, update_request_thread,
624 p_filter, VLC_THREAD_PRIORITY_LOW ) )
626 msg_Err( p_filter, "cannot spawn VNC update request thread" );
627 polling = false;
630 vlc_cleanup_push( polling ? update_thread_cleanup : dummy_cleanup,
631 &update_thread );
633 /* connection is initialized, now read and handle server messages */
634 for( ;; )
636 rfbServerToClientMsg msg;
637 int i_msgSize;
639 memset( &msg, 0, sizeof(msg) );
640 vlc_restorecancel (canc);
642 if( !read_exact(p_filter, fd, &msg, 1 ) )
644 msg_Err( p_filter, "Error while waiting for next server message");
645 break;
647 switch (msg.type)
649 case rfbFramebufferUpdate:
650 i_msgSize = sz_rfbFramebufferUpdateMsg;
651 break;
652 case rfbSetColourMapEntries:
653 i_msgSize = sz_rfbSetColourMapEntriesMsg;
654 break;
655 case rfbBell:
656 i_msgSize = sz_rfbBellMsg;
657 break;
658 case rfbServerCutText:
659 i_msgSize = sz_rfbServerCutTextMsg;
660 break;
661 case rfbReSizeFrameBuffer:
662 i_msgSize = sz_rfbReSizeFrameBufferMsg;
663 break;
664 default:
665 i_msgSize = 0;
666 msg_Err( p_filter, "Invalid message %u received", msg.type );
667 break;
670 if( i_msgSize <= 0 )
671 break;
673 if( --i_msgSize > 0 )
675 if ( !read_exact( p_filter, fd, ((char *)&msg) + 1, i_msgSize ) )
677 msg_Err( p_filter, "Error while reading message of type %u",
678 msg.type );
679 break;
683 canc = vlc_savecancel ();
684 process_server_message( p_filter, &msg);
687 vlc_cleanup_pop();
688 if( polling )
689 update_thread_cleanup( &update_thread );
691 msg_Dbg( p_filter, "VNC message reader thread ended" );
692 vlc_restorecancel (canc);
693 return NULL;
696 static void* update_request_thread( void *obj )
698 filter_t* p_filter = (filter_t*)obj;
699 int canc = vlc_savecancel();
700 mtime_t interval = var_InheritInteger( p_filter, RMTOSD_CFG "update" );
701 vlc_restorecancel(canc);
703 if( interval < 100 )
704 interval = 100;
705 interval *= 1000; /* ms -> µs */
708 msleep( interval );
709 while( write_update_request( p_filter, true ) );
711 return NULL;
714 static bool process_server_message ( filter_t *p_filter,
715 rfbServerToClientMsg *msg )
717 filter_sys_t *p_sys = p_filter->p_sys;
719 switch (msg->type)
721 case rfbFramebufferUpdate:
723 msg->fu.nRects = htons(msg->fu.nRects);
724 rfbFramebufferUpdateRectHeader hdr;
726 for (int i_rect = 0; i_rect < msg->fu.nRects; i_rect++)
728 if (!read_exact(p_filter, p_sys->i_socket, &hdr,
729 sz_rfbFramebufferUpdateRectHeader ) )
731 msg_Err( p_filter, "Could not read FrameBufferUpdate header" );
732 return false;
734 hdr.r.x = htons(hdr.r.x);
735 hdr.r.y = htons(hdr.r.y);
736 hdr.r.w = htons(hdr.r.w);
737 hdr.r.h = htons(hdr.r.h);
738 hdr.encoding = htonl(hdr.encoding);
740 switch (hdr.encoding)
742 case rfbEncodingRaw:
744 int i_line;
745 for (i_line = 0; i_line < hdr.r.h; i_line++)
747 if ( !read_exact( p_filter, p_sys->i_socket,
748 p_sys->read_buffer, hdr.r.w ) )
750 msg_Err( p_filter,
751 "Could not read FrameBufferUpdate line data" );
752 return false;
754 vlc_mutex_lock( &p_sys->lock );
755 if ( !raw_line( p_sys, hdr.r.x,
756 hdr.r.y + i_line,
757 hdr.r.w ) )
759 msg_Err( p_filter, "raw_line failed." );
760 vlc_mutex_unlock( &p_sys->lock );
761 return false;
763 vlc_mutex_unlock( &p_sys->lock );
766 break;
768 case rfbEncodingCopyRect:
770 rfbCopyRect rect;
772 if ( !read_exact( p_filter, p_sys->i_socket,
773 &rect, sz_rfbCopyRect ) )
775 msg_Err( p_filter, "Could not read rfbCopyRect" );
776 return false;
778 rect.srcX = htons( rect.srcX );
779 rect.srcY = htons( rect.srcY );
781 vlc_mutex_lock( &p_sys->lock );
782 if ( !copy_rect( p_sys,
783 hdr.r.x, hdr.r.y,
784 hdr.r.w, hdr.r.h,
785 rect.srcX, rect.srcY ) )
787 msg_Err( p_filter, "copy_rect failed." );
788 vlc_mutex_unlock( &p_sys->lock );
789 return false;
791 vlc_mutex_unlock( &p_sys->lock );
793 break;
795 case rfbEncodingRRE:
797 rfbRREHeader rrehdr;
798 if ( !read_exact( p_filter, p_sys->i_socket,
799 &rrehdr, sz_rfbRREHeader ) )
801 msg_Err( p_filter, "Could not read rfbRREHeader" );
802 return false;
804 uint8_t i_pixcolor;
805 if ( !read_exact( p_filter, p_sys->i_socket,
806 &i_pixcolor, 1 ) )
808 msg_Err( p_filter, "Could not read RRE pixcolor" );
809 return false;
812 vlc_mutex_lock( &p_sys->lock );
813 if ( !fill_rect( p_sys,
814 hdr.r.x, hdr.r.y,
815 hdr.r.w, hdr.r.h,
816 i_pixcolor) )
818 msg_Err( p_filter, "main fill_rect failed." );
819 vlc_mutex_unlock( &p_sys->lock );
820 return false;
822 vlc_mutex_unlock( &p_sys->lock );
824 rrehdr.nSubrects = htonl(rrehdr.nSubrects);
826 int i_datasize = rrehdr.nSubrects *
827 ( sizeof(i_pixcolor) + sz_rfbRectangle ) ;
828 if ( i_datasize > READ_BUFFER_SIZE )
830 msg_Err( p_filter, "Buffer too small, "
831 "need %u bytes", i_datasize );
832 return false;
834 if ( !read_exact( p_filter, p_sys->i_socket,
835 p_sys->read_buffer, i_datasize ) )
837 msg_Err( p_filter,
838 "Could not read RRE subrect data" );
839 return false;
842 uint32_t i_subrect;
843 rfbRectangle* p_subrect;
844 int i_offset = 0;
845 vlc_mutex_lock( &p_sys->lock );
846 for ( i_subrect = 0;
847 i_subrect < rrehdr.nSubrects; i_subrect++)
849 i_pixcolor = p_sys->read_buffer[i_offset];
850 i_offset += sizeof(i_pixcolor);
851 p_subrect =
852 (rfbRectangle*)(p_sys->read_buffer + i_offset);
853 i_offset += sz_rfbRectangle;
855 if (!fill_rect( p_sys,
856 htons(p_subrect->x) + hdr.r.x,
857 htons(p_subrect->y) + hdr.r.y,
858 htons(p_subrect->w),
859 htons(p_subrect->h),
860 i_pixcolor) )
862 msg_Err( p_filter,
863 "subrect %u fill_rect failed.", i_subrect );
864 vlc_mutex_unlock( &p_sys->lock );
865 return false;
868 vlc_mutex_unlock( &p_sys->lock );
870 break;
875 vlc_mutex_lock( &p_sys->lock );
876 p_sys->b_need_update = true;
877 vlc_mutex_unlock( &p_sys->lock );
879 return true;
881 case rfbSetColourMapEntries:
883 msg->scme.nColours = htons(msg->scme.nColours);
884 msg->scme.firstColour = htons(msg->scme.firstColour);
885 int i_datasize;
886 if ( p_sys->b_alpha_from_vnc )
888 i_datasize = 2 * msg->scme.nColours * 4;
890 else
892 i_datasize = 2 * msg->scme.nColours * 3;
894 if ( i_datasize > READ_BUFFER_SIZE )
896 msg_Err( p_filter, "Buffer too small, need %u bytes",
897 i_datasize );
898 return false;
901 if ( !read_exact( p_filter, p_sys->i_socket,
902 p_sys->read_buffer, i_datasize ) )
904 msg_Err( p_filter, "Could not read color map data" );
905 return false;
908 uint8_t i_red, i_green, i_blue, i_alpha, i_color_index;
909 uint16_t i_offset = 0;
910 i_alpha = 255;
912 for (int i = 0; i < msg->scme.nColours; i++)
914 i_color_index = i+msg->scme.firstColour;
915 if ( p_sys->b_alpha_from_vnc )
917 i_alpha = p_sys->read_buffer[i_offset];
918 i_offset += 2;
920 i_red = p_sys->read_buffer[i_offset];
921 i_offset += 2;
922 i_green = p_sys->read_buffer[i_offset];
923 i_offset += 2;
924 i_blue = p_sys->read_buffer[i_offset];
925 i_offset += 2;
926 rgb_to_yuv( &p_sys->ar_color_table_yuv[i_color_index][0],
927 &p_sys->ar_color_table_yuv[i_color_index][1],
928 &p_sys->ar_color_table_yuv[i_color_index][2],
929 i_red,
930 i_green,
931 i_blue );
932 p_sys->ar_color_table_yuv[i][3] = i_alpha;
935 return true;
937 case rfbBell:
938 msg_Err( p_filter, "rfbBell received" );
939 return true;
941 case rfbServerCutText:
942 msg->sct.length = htons(msg->sct.length);
943 if ( msg->sct.length > READ_BUFFER_SIZE )
945 msg_Err( p_filter, "Buffer too small, need %u bytes", msg->sct.length );
946 return false;
948 if ( !read_exact( p_filter, p_sys->i_socket,
949 p_sys->read_buffer, msg->sct.length ) )
951 msg_Err( p_filter, "Could not read Reading rfbServerCutText data" );
952 return false;
954 return true;
956 case rfbReSizeFrameBuffer:
957 msg_Err( p_filter, "Reading rfbReSizeFrameBuffer not implemented" );
958 return false;
960 default:
961 msg_Err( p_filter, "Invalid message %u received", msg->type );
962 return false;
964 return false;
967 /****************************************************************************
968 * Filter: the whole thing
969 ****************************************************************************
970 * This function outputs subpictures at regular time intervals.
971 ****************************************************************************/
972 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
974 filter_sys_t *p_sys = p_filter->p_sys;
975 subpicture_t *p_spu;
976 subpicture_region_t *p_region;
977 video_format_t fmt;
978 picture_t *p_pic = NULL;
980 vlc_mutex_lock( &p_sys->lock );
982 if( p_sys->b_need_update )
983 p_pic = p_sys->p_pic;
985 if( p_pic == NULL )
987 vlc_mutex_unlock( &p_sys->lock );
988 return NULL;
991 /* Allocate the subpicture internal data. */
992 p_spu = filter_NewSubpicture( p_filter );
993 if( !p_spu )
995 vlc_mutex_unlock( &p_sys->lock );
996 return NULL;
999 p_spu->b_absolute = false;
1000 p_spu->i_start = date;
1001 p_spu->i_stop = 0;
1002 p_spu->b_ephemer = true;
1004 /* Create new SPU region */
1005 memset( &fmt, 0, sizeof(video_format_t) );
1006 fmt.i_chroma = VLC_CODEC_YUVA;
1007 fmt.i_sar_num = fmt.i_sar_den = 1;
1008 fmt.i_width = fmt.i_visible_width = p_pic->p[Y_PLANE].i_visible_pitch;
1009 fmt.i_height = fmt.i_visible_height = p_pic->p[Y_PLANE].i_visible_lines;
1010 fmt.i_x_offset = fmt.i_y_offset = 0;
1011 p_region = subpicture_region_New( &fmt );
1012 if( !p_region )
1014 msg_Err( p_filter, "cannot allocate SPU region" );
1015 subpicture_Delete( p_spu );
1016 vlc_mutex_unlock( &p_sys->lock );
1017 return NULL;
1020 /* FIXME the copy is probably not needed anymore */
1021 picture_Copy( p_region->p_picture, p_pic );
1023 p_sys->b_need_update = false;
1025 vlc_mutex_unlock( &p_sys->lock );
1027 /* set to one of the 9 relative locations */
1028 p_region->i_align = 0; /* Center */
1029 p_spu->b_absolute = false;
1032 p_spu->i_original_picture_width = 0; /*Let vout core do the horizontal scaling */
1033 p_spu->i_original_picture_height = fmt.i_height;
1035 p_spu->p_region = p_region;
1037 p_spu->i_alpha = ( p_sys->i_alpha );
1039 return p_spu;
1043 static inline void rgb_to_yuv( uint8_t *y, uint8_t *u, uint8_t *v,
1044 int r, int g, int b )
1046 *y = ( ( ( 66 * r + 129 * g + 25 * b + 128 ) >> 8 ) + 16 );
1047 *u = ( ( -38 * r - 74 * g + 112 * b + 128 ) >> 8 ) + 128 ;
1048 *v = ( ( 112 * r - 94 * g - 18 * b + 128 ) >> 8 ) + 128 ;
1051 static inline bool fill_rect( filter_sys_t* p_sys,
1052 uint16_t i_x, uint16_t i_y,
1053 uint16_t i_w, uint16_t i_h,
1054 uint8_t i_color)
1056 plane_t *p_outY = p_sys->p_pic->p+Y_PLANE;
1057 plane_t *p_outU = p_sys->p_pic->p+U_PLANE;
1058 plane_t *p_outV = p_sys->p_pic->p+V_PLANE;
1059 plane_t *p_outA = p_sys->p_pic->p+A_PLANE;
1060 int i_pitch = p_outY->i_pitch;
1061 int i_lines = p_outY->i_lines;
1062 if ( i_x + i_w > i_pitch)
1063 return false;
1064 if ( i_y + i_h > i_lines)
1065 return false;
1066 int i_line_offset = i_y * i_pitch;
1067 uint8_t i_yuv_y = p_sys->ar_color_table_yuv[i_color][0];
1068 uint8_t i_yuv_u = p_sys->ar_color_table_yuv[i_color][1];
1069 uint8_t i_yuv_v = p_sys->ar_color_table_yuv[i_color][2];
1070 uint8_t i_alpha = p_sys->ar_color_table_yuv[i_color][3];
1071 for( int i_line = 0; i_line < i_h; i_line++ )
1073 for( int i_column = 0; i_column < i_w; i_column++ )
1075 int i_total_offset = i_line_offset + i_x + i_column;
1076 p_outY->p_pixels[ i_total_offset ] = i_yuv_y;
1077 p_outU->p_pixels[ i_total_offset ] = i_yuv_u;
1078 p_outV->p_pixels[ i_total_offset ] = i_yuv_v;
1079 p_outA->p_pixels[ i_total_offset ] = i_alpha;
1081 i_line_offset += i_pitch;
1083 return true;
1086 static inline bool copy_rect( filter_sys_t* p_sys,
1087 uint16_t i_x, uint16_t i_y,
1088 uint16_t i_w, uint16_t i_h,
1089 uint16_t i_sx, uint16_t i_sy )
1091 plane_t *p_Y = p_sys->p_pic->p+Y_PLANE;
1092 plane_t *p_U = p_sys->p_pic->p+U_PLANE;
1093 plane_t *p_V = p_sys->p_pic->p+V_PLANE;
1094 plane_t *p_A = p_sys->p_pic->p+A_PLANE;
1096 int i_pitch = p_Y->i_pitch;
1097 int i_lines = p_Y->i_lines;
1099 fprintf( stderr, "copy_rect: (%d,%d)+(%d,%d) -> (%d,%d)\n", i_x, i_y, i_w, i_h, i_sx, i_sy );
1101 if( i_x + i_w > i_pitch || i_sx + i_w > i_pitch )
1102 return false;
1103 if( i_y + i_h > i_lines || i_sy + i_h > i_lines)
1104 return false;
1106 if( i_w <= 0 || i_h <= 0 )
1107 return true;
1109 uint8_t *pb_buffer = calloc( i_w * i_h, 4 );
1110 if( !pb_buffer )
1111 return false;
1113 for( int i_line = 0; i_line < i_h; i_line++ )
1115 for( int i_column = 0; i_column < i_w; i_column++ )
1117 const int i_src_offset = ( i_sy + i_line ) * i_pitch + i_sx + i_column;
1118 const int i_tmp_offset = ( 0 + i_line ) * i_w + 0 + i_column;
1120 pb_buffer[4*i_tmp_offset + 0] = p_Y->p_pixels[i_src_offset];
1121 pb_buffer[4*i_tmp_offset + 1] = p_U->p_pixels[i_src_offset];
1122 pb_buffer[4*i_tmp_offset + 2] = p_V->p_pixels[i_src_offset];
1123 pb_buffer[4*i_tmp_offset + 3] = p_A->p_pixels[i_src_offset];
1127 for( int i_line = 0; i_line < i_h; i_line++ )
1129 for( int i_column = 0; i_column < i_w; i_column++ )
1131 const int i_tmp_offset = ( 0 + i_line ) * i_w + 0 + i_column;
1132 const int i_dst_offset = ( i_y + i_line ) * i_pitch + i_x + i_column;
1134 p_Y->p_pixels[i_dst_offset] = pb_buffer[4*i_tmp_offset + 0];
1135 p_U->p_pixels[i_dst_offset] = pb_buffer[4*i_tmp_offset + 1];
1136 p_V->p_pixels[i_dst_offset] = pb_buffer[4*i_tmp_offset + 2];
1137 p_A->p_pixels[i_dst_offset] = pb_buffer[4*i_tmp_offset + 3];
1140 free( pb_buffer );
1141 return true;
1145 static inline bool raw_line( filter_sys_t* p_sys,
1146 uint16_t i_x, uint16_t i_y,
1147 uint16_t i_w )
1149 plane_t *p_outY = p_sys->p_pic->p+Y_PLANE;
1150 plane_t *p_outU = p_sys->p_pic->p+U_PLANE;
1151 plane_t *p_outV = p_sys->p_pic->p+V_PLANE;
1152 plane_t *p_outA = p_sys->p_pic->p+A_PLANE;
1153 int i_pitch = p_outY->i_pitch;
1154 int i_lines = p_outY->i_lines;
1155 if ( i_x + i_w > i_pitch)
1156 return false;
1157 if ( i_y > i_lines)
1158 return false;
1160 int i_line_offset = i_y * i_pitch + i_x;
1162 for( int i_column = 0; i_column < i_w; i_column++ )
1164 int i_offset = i_line_offset + i_column;
1165 uint8_t i_color = p_sys->read_buffer[i_column];
1166 p_outY->p_pixels[ i_offset ] = p_sys->ar_color_table_yuv[i_color][0];
1167 p_outU->p_pixels[ i_offset ] = p_sys->ar_color_table_yuv[i_color][1];
1168 p_outV->p_pixels[ i_offset ] = p_sys->ar_color_table_yuv[i_color][2];
1169 p_outA->p_pixels[ i_offset ] = p_sys->ar_color_table_yuv[i_color][3];
1172 return true;
1176 /*****************************************************************************
1177 * MouseEvent: callback for mouse events
1178 *****************************************************************************/
1179 static int MouseEvent( filter_t *p_filter,
1180 const vlc_mouse_t *p_old,
1181 const vlc_mouse_t *p_new,
1182 const video_format_t *p_fmt )
1184 filter_sys_t *p_sys = p_filter->p_sys;
1185 VLC_UNUSED(p_old);
1187 int i_v = p_new->i_pressed;
1188 int i_x = p_new->i_x;
1189 int i_y = p_new->i_y;
1191 vlc_mutex_lock( &p_sys->lock );
1193 const int v_h = p_fmt->i_visible_height;
1194 const int v_w = p_sys->i_vnc_width * v_h / p_sys->i_vnc_height;
1195 const int v_x = (p_fmt->i_visible_width-v_w)/2;
1197 i_x -= v_x;
1199 if( i_y < 0 || i_x < 0 || i_y >= v_h || i_x >= v_w )
1201 vlc_mutex_unlock( &p_sys->lock );
1202 msg_Dbg( p_filter, "invalid mouse event? x=%d y=%d btn=%x", i_x, i_y, i_v );
1203 return VLC_SUCCESS;
1206 if( p_sys->i_socket == -1 )
1208 vlc_mutex_unlock( &p_sys->lock );
1209 return VLC_SUCCESS;
1212 #ifdef VNC_DEBUG
1213 msg_Dbg( p_filter, "mouse event x=%d y=%d btn=%x", i_x, i_y, i_v );
1214 #endif
1216 /* */
1217 i_x = i_x * p_sys->i_vnc_width / v_w;
1218 i_y = i_y * p_sys->i_vnc_height / v_h;
1220 /* buttonMask bits 0-7 are buttons 1-8, 0=up, 1=down */
1221 rfbPointerEventMsg ev;
1222 ev.type = rfbPointerEvent;
1223 ev.buttonMask = i_v;
1224 ev.x = htons(i_x);
1225 ev.y = htons(i_y);
1227 write_exact( p_filter, p_sys->i_socket, &ev, sz_rfbPointerEventMsg);
1228 vlc_mutex_unlock( &p_sys->lock );
1230 return VLC_EGENERIC;
1233 /*****************************************************************************
1234 * KeyEvent: callback for keyboard events
1235 *****************************************************************************/
1236 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
1237 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1239 VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
1241 filter_t *p_filter = (filter_t *)p_data;
1242 filter_sys_t *p_sys = p_filter->p_sys;
1244 msg_Dbg( p_this, "key pressed (%"PRId64") ", newval.i_int );
1246 if ( !newval.i_int )
1248 msg_Err( p_this, "Received invalid key event 0" );
1249 return VLC_EGENERIC;
1252 vlc_mutex_lock( &p_sys->lock );
1253 if( p_sys->i_socket == -1 )
1255 vlc_mutex_unlock( &p_sys->lock );
1256 return VLC_SUCCESS;
1259 uint32_t i_key32 = newval.i_int;
1260 i_key32 = htonl(i_key32);
1261 rfbKeyEventMsg ev;
1263 ev.type = rfbKeyEvent;
1264 ev.down = 1;
1265 ev.pad = 0;
1267 /* first key-down for modifier-keys */
1268 if (newval.i_int & KEY_MODIFIER_CTRL)
1270 ev.key = 0xffe3;
1271 write_exact( p_filter, p_sys->i_socket, &ev, sz_rfbKeyEventMsg );
1273 if (newval.i_int & KEY_MODIFIER_SHIFT)
1275 ev.key = 0xffe1;
1276 write_exact( p_filter, p_sys->i_socket, &ev, sz_rfbKeyEventMsg );
1278 if (newval.i_int & KEY_MODIFIER_ALT)
1280 ev.key = 0xffe9;
1281 write_exact( p_filter, p_sys->i_socket, &ev, sz_rfbKeyEventMsg );
1284 /* then key-down for the pressed key */
1285 ev.key = i_key32;
1286 write_exact( p_filter, p_sys->i_socket, &ev, sz_rfbKeyEventMsg );
1288 ev.down = 0;
1290 /* then key-up for the pressed key */
1291 write_exact( p_filter, p_sys->i_socket, &ev, sz_rfbKeyEventMsg );
1293 /* last key-down for modifier-keys */
1294 if (newval.i_int & KEY_MODIFIER_CTRL)
1296 ev.key = 0xffe3;
1297 write_exact( p_filter, p_sys->i_socket, &ev, sz_rfbKeyEventMsg );
1299 if (newval.i_int & KEY_MODIFIER_SHIFT)
1301 ev.key = 0xffe1;
1302 write_exact( p_filter, p_sys->i_socket, &ev, sz_rfbKeyEventMsg );
1304 if (newval.i_int & KEY_MODIFIER_ALT)
1306 ev.key = 0xffe9;
1307 write_exact( p_filter, p_sys->i_socket, &ev, sz_rfbKeyEventMsg );
1309 vlc_mutex_unlock( &p_sys->lock );
1311 return VLC_SUCCESS;
1314 static void vnc_encrypt_bytes( unsigned char *bytes, char *passwd )
1316 unsigned char key[8];
1318 for( unsigned i = 0; i < 8; i++ )
1319 key[i] = i < strlen( passwd ) ? passwd[i] : '\0';
1321 gcry_cipher_hd_t ctx;
1322 gcry_cipher_open( &ctx, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB,0);
1324 /* reverse bits of the key */
1325 for( unsigned i = 0 ; i < 8 ; i++ )
1326 key[i] =
1327 (key[i] >> 7) +
1328 (((key[i] >> 6) & 0x01 ) << 1 ) +
1329 (((key[i] >> 5) & 0x01 ) << 2 ) +
1330 (((key[i] >> 4) & 0x01 ) << 3 ) +
1331 (((key[i] >> 3) & 0x01 ) << 4 ) +
1332 (((key[i] >> 2) & 0x01 ) << 5 ) +
1333 (((key[i] >> 1) & 0x01 ) << 6 ) +
1334 ((key[i] & 0x01) << 7 );
1336 gcry_cipher_setkey( ctx, key, 8 );
1337 gcry_cipher_encrypt( ctx, bytes, CHALLENGESIZE, bytes, CHALLENGESIZE );
1338 gcry_cipher_close( ctx );