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 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
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>
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 /*****************************************************************************
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_( \
78 #define RMTOSD_PASSWORD_TEXT N_("VNC Password")
79 #define RMTOSD_PASSWORD_LONGTEXT N_( \
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
* );
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 )
144 /*****************************************************************************
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
*,
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
,
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
,
185 static void vnc_encrypt_bytes( unsigned char *bytes
, char *passwd
);
188 /*****************************************************************************
190 *****************************************************************************/
192 /*****************************************************************************
194 *****************************************************************************/
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
) )
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" );
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" );
252 if( p_sys
->psz_passwd
== NULL
)
254 msg_Err( p_filter
, "unable to get vnc password" );
258 p_filter
->p_sys
= p_sys
;
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" );
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" );
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
);
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
);
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
);
346 msg_Err( p_filter
, "Could not connect to VNC host" );
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" );
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" );
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" );
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" );
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" );
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" );
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" );
407 i_authResult
= htonl(i_authResult
);
408 if (i_authResult
!= rfbVncAuthOK
)
410 msg_Err( p_filter
, "VNC authentication failed" );
415 msg_Dbg( p_filter
, "Writing client init message" );
418 if( !write_exact( p_filter
, fd
, &ci
, sz_rfbClientInitMsg
) )
420 msg_Err( p_filter
, "Could not write client init message" );
424 msg_Dbg( p_filter
, "Reading server init message" );
426 if( !read_exact( p_filter
, fd
, &si
, sz_rfbServerInitMsg
) )
428 msg_Err( p_filter
, "Could not read server init message" );
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",
443 si
.framebufferHeight
,
447 si
.format
.bitsPerPixel
,
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" );
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" );
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;
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" );
505 msg_Dbg( p_filter
, "Writing SetEncodings message" );
507 rfbSetEncodingsMsg se
;
508 se
.type
= rfbSetEncodings
;
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" );
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." );
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." );
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." );
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
;
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
);
569 msg_Err( p_filter
, "Could not write rfbFramebufferUpdateRequestMsg." );
573 static void update_thread_cleanup( void *p
)
575 vlc_thread_t
*th
= p
;
578 vlc_join( *th
, NULL
);
581 static void dummy_cleanup( 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
);
598 msg_Err( p_filter
, "Error occurred while handshaking VNC host" );
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
;
610 vlc_mutex_unlock( &p_sys
->lock
);
618 write_update_request( p_filter
, false );
620 /* create the update request thread */
621 bool polling
= var_InheritBool( p_filter
, RMTOSD_CFG
"vnc-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" );
630 vlc_cleanup_push( polling
? update_thread_cleanup
: dummy_cleanup
,
633 /* connection is initialized, now read and handle server messages */
636 rfbServerToClientMsg msg
;
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");
649 case rfbFramebufferUpdate
:
650 i_msgSize
= sz_rfbFramebufferUpdateMsg
;
652 case rfbSetColourMapEntries
:
653 i_msgSize
= sz_rfbSetColourMapEntriesMsg
;
656 i_msgSize
= sz_rfbBellMsg
;
658 case rfbServerCutText
:
659 i_msgSize
= sz_rfbServerCutTextMsg
;
661 case rfbReSizeFrameBuffer
:
662 i_msgSize
= sz_rfbReSizeFrameBufferMsg
;
666 msg_Err( p_filter
, "Invalid message %u received", msg
.type
);
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",
683 canc
= vlc_savecancel ();
684 process_server_message( p_filter
, &msg
);
689 update_thread_cleanup( &update_thread
);
691 msg_Dbg( p_filter
, "VNC message reader thread ended" );
692 vlc_restorecancel (canc
);
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
);
705 interval
*= 1000; /* ms -> µs */
709 while( write_update_request( p_filter
, true ) );
714 static bool process_server_message ( filter_t
*p_filter
,
715 rfbServerToClientMsg
*msg
)
717 filter_sys_t
*p_sys
= p_filter
->p_sys
;
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" );
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
)
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
) )
751 "Could not read FrameBufferUpdate line data" );
754 vlc_mutex_lock( &p_sys
->lock
);
755 if ( !raw_line( p_sys
, hdr
.r
.x
,
759 msg_Err( p_filter
, "raw_line failed." );
760 vlc_mutex_unlock( &p_sys
->lock
);
763 vlc_mutex_unlock( &p_sys
->lock
);
768 case rfbEncodingCopyRect
:
772 if ( !read_exact( p_filter
, p_sys
->i_socket
,
773 &rect
, sz_rfbCopyRect
) )
775 msg_Err( p_filter
, "Could not read rfbCopyRect" );
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
,
785 rect
.srcX
, rect
.srcY
) )
787 msg_Err( p_filter
, "copy_rect failed." );
788 vlc_mutex_unlock( &p_sys
->lock
);
791 vlc_mutex_unlock( &p_sys
->lock
);
798 if ( !read_exact( p_filter
, p_sys
->i_socket
,
799 &rrehdr
, sz_rfbRREHeader
) )
801 msg_Err( p_filter
, "Could not read rfbRREHeader" );
805 if ( !read_exact( p_filter
, p_sys
->i_socket
,
808 msg_Err( p_filter
, "Could not read RRE pixcolor" );
812 vlc_mutex_lock( &p_sys
->lock
);
813 if ( !fill_rect( p_sys
,
818 msg_Err( p_filter
, "main fill_rect failed." );
819 vlc_mutex_unlock( &p_sys
->lock
);
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
);
834 if ( !read_exact( p_filter
, p_sys
->i_socket
,
835 p_sys
->read_buffer
, i_datasize
) )
838 "Could not read RRE subrect data" );
843 rfbRectangle
* p_subrect
;
845 vlc_mutex_lock( &p_sys
->lock
);
847 i_subrect
< rrehdr
.nSubrects
; i_subrect
++)
849 i_pixcolor
= p_sys
->read_buffer
[i_offset
];
850 i_offset
+= sizeof(i_pixcolor
);
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
,
863 "subrect %u fill_rect failed.", i_subrect
);
864 vlc_mutex_unlock( &p_sys
->lock
);
868 vlc_mutex_unlock( &p_sys
->lock
);
875 vlc_mutex_lock( &p_sys
->lock
);
876 p_sys
->b_need_update
= true;
877 vlc_mutex_unlock( &p_sys
->lock
);
881 case rfbSetColourMapEntries
:
883 msg
->scme
.nColours
= htons(msg
->scme
.nColours
);
884 msg
->scme
.firstColour
= htons(msg
->scme
.firstColour
);
886 if ( p_sys
->b_alpha_from_vnc
)
888 i_datasize
= 2 * msg
->scme
.nColours
* 4;
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",
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" );
908 uint8_t i_red
, i_green
, i_blue
, i_alpha
, i_color_index
;
909 uint16_t i_offset
= 0;
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
];
920 i_red
= p_sys
->read_buffer
[i_offset
];
922 i_green
= p_sys
->read_buffer
[i_offset
];
924 i_blue
= p_sys
->read_buffer
[i_offset
];
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],
932 p_sys
->ar_color_table_yuv
[i
][3] = i_alpha
;
938 msg_Err( p_filter
, "rfbBell received" );
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
);
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" );
956 case rfbReSizeFrameBuffer
:
957 msg_Err( p_filter
, "Reading rfbReSizeFrameBuffer not implemented" );
961 msg_Err( p_filter
, "Invalid message %u received", msg
->type
);
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
;
976 subpicture_region_t
*p_region
;
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
;
987 vlc_mutex_unlock( &p_sys
->lock
);
991 /* Allocate the subpicture internal data. */
992 p_spu
= filter_NewSubpicture( p_filter
);
995 vlc_mutex_unlock( &p_sys
->lock
);
999 p_spu
->b_absolute
= false;
1000 p_spu
->i_start
= date
;
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
);
1014 msg_Err( p_filter
, "cannot allocate SPU region" );
1015 subpicture_Delete( p_spu
);
1016 vlc_mutex_unlock( &p_sys
->lock
);
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
);
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
,
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
)
1064 if ( i_y
+ i_h
> i_lines
)
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
;
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
)
1103 if( i_y
+ i_h
> i_lines
|| i_sy
+ i_h
> i_lines
)
1106 if( i_w
<= 0 || i_h
<= 0 )
1109 uint8_t *pb_buffer
= calloc( i_w
* i_h
, 4 );
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];
1145 static inline bool raw_line( filter_sys_t
* p_sys
,
1146 uint16_t i_x
, uint16_t i_y
,
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
)
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];
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
;
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;
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
);
1206 if( p_sys
->i_socket
== -1 )
1208 vlc_mutex_unlock( &p_sys
->lock
);
1213 msg_Dbg( p_filter
, "mouse event x=%d y=%d btn=%x", i_x
, i_y
, i_v
);
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
;
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
);
1259 uint32_t i_key32
= newval
.i_int
;
1260 i_key32
= htonl(i_key32
);
1263 ev
.type
= rfbKeyEvent
;
1267 /* first key-down for modifier-keys */
1268 if (newval
.i_int
& KEY_MODIFIER_CTRL
)
1271 write_exact( p_filter
, p_sys
->i_socket
, &ev
, sz_rfbKeyEventMsg
);
1273 if (newval
.i_int
& KEY_MODIFIER_SHIFT
)
1276 write_exact( p_filter
, p_sys
->i_socket
, &ev
, sz_rfbKeyEventMsg
);
1278 if (newval
.i_int
& KEY_MODIFIER_ALT
)
1281 write_exact( p_filter
, p_sys
->i_socket
, &ev
, sz_rfbKeyEventMsg
);
1284 /* then key-down for the pressed key */
1286 write_exact( p_filter
, p_sys
->i_socket
, &ev
, sz_rfbKeyEventMsg
);
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
)
1297 write_exact( p_filter
, p_sys
->i_socket
, &ev
, sz_rfbKeyEventMsg
);
1299 if (newval
.i_int
& KEY_MODIFIER_SHIFT
)
1302 write_exact( p_filter
, p_sys
->i_socket
, &ev
, sz_rfbKeyEventMsg
);
1304 if (newval
.i_int
& KEY_MODIFIER_ALT
)
1307 write_exact( p_filter
, p_sys
->i_socket
, &ev
, sz_rfbKeyEventMsg
);
1309 vlc_mutex_unlock( &p_sys
->lock
);
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
++ )
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
);