A little more detail regarding using my github copies of the code with where it's...
[vlc/adversarial.git] / modules / access / rdp.c
blobed3b9e020d132bef69cc7cdaac679a7c6dce74cd
1 /*****************************************************************************
2 * rdp.c: libfreeRDP based Remote Desktop access
3 *****************************************************************************
4 * Copyright (C) 2013 VideoLAN Authors
5 *****************************************************************************
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *****************************************************************************/
21 /*****************************************************************************
22 * Preamble
23 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_demux.h>
32 #include <vlc_url.h>
33 #include <vlc_meta.h>
35 #define boolean bool
37 /* see MS-RDPBCGR http://msdn.microsoft.com/en-us/library/cc240445.aspx */
39 #include <freerdp/freerdp.h>
40 #include <freerdp/settings.h>
41 #include <freerdp/channels/channels.h>
42 #include <freerdp/gdi/gdi.h>
44 #if !defined(FREERDP_INTERFACE_VERSION)
45 # include <freerdp/version.h>
46 #endif
48 #if !defined(FREERDP_VERSION_MAJOR) || \
49 (defined(FREERDP_VERSION_MAJOR) && !(FREERDP_VERSION_MAJOR >= 1 && FREERDP_VERSION_MINOR >= 1 ))
50 # define SoftwareGdi sw_gdi
51 # define Fullscreen fullscreen
52 # define ServerHostname hostname
53 # define Username username
54 # define Password password
55 # define ServerPort port
56 # define EncryptionMethods encryption
57 # define ContextSize context_size
58 #endif
60 #include <errno.h>
61 #ifdef HAVE_POLL
62 # include <poll.h>
63 #endif
65 #define RDP_USER N_("RDP auth username")
66 #define RDP_PASSWORD N_("RDP auth password")
67 #define RDP_PASSWORD_LONGTEXT N_("RDP Password")
68 #define RDP_ENCRYPT N_("Encrypted connexion")
69 #define RDP_FPS N_("Frame rate")
70 #define RDP_FPS_LONGTEXT N_("Acquisition rate (in fps)")
72 #define CFG_PREFIX "rdp-"
74 /*****************************************************************************
75 * Module descriptor
76 *****************************************************************************/
77 static int Open ( vlc_object_t * );
78 static void Close( vlc_object_t * );
80 vlc_module_begin()
81 set_shortname( N_("RDP") )
82 add_shortcut( "rdp" )
83 set_category( CAT_INPUT )
84 set_subcategory( SUBCAT_INPUT_ACCESS )
85 set_description( N_("RDP Remote Desktop") )
86 set_capability( "access_demux", 10 )
88 add_string( CFG_PREFIX "user", NULL, RDP_USER, RDP_USER, false )
89 change_safe()
90 add_password( CFG_PREFIX "password", NULL, RDP_PASSWORD, RDP_PASSWORD_LONGTEXT, false )
91 change_safe()
92 add_float( CFG_PREFIX "fps", 5, RDP_FPS, RDP_FPS_LONGTEXT, true )
94 add_bool( CFG_PREFIX "encrypt", false, RDP_ENCRYPT, RDP_ENCRYPT, true )
95 change_safe()
97 set_callbacks( Open, Close )
98 vlc_module_end()
100 #define RDP_MAX_FD 32
102 struct demux_sys_t
104 vlc_thread_t thread;
105 freerdp *p_instance;
106 block_t *p_block;
107 int i_framebuffersize;
109 float f_fps;
110 int i_frame_interval;
111 mtime_t i_starttime;
113 es_out_id_t *es;
115 /* pre-connect params */
116 char *psz_hostname;
117 int i_port;
118 /* cancelability */
119 int i_cancel_state;
122 /* context */
124 struct vlcrdp_context_t
126 rdpContext rdp_context; /* Extending API's struct */
127 demux_t *p_demux;
128 rdpSettings* p_settings;
130 typedef struct vlcrdp_context_t vlcrdp_context_t;
132 /*****************************************************************************
133 * Local prototypes
134 *****************************************************************************/
136 /* updates handlers */
138 static void desktopResizeHandler( rdpContext *p_context )
140 vlcrdp_context_t * p_vlccontext = (vlcrdp_context_t *) p_context;
141 demux_sys_t *p_sys = p_vlccontext->p_demux->p_sys;
142 rdpGdi *p_gdi = p_context->gdi;
144 if ( p_sys->es )
146 es_out_Del( p_vlccontext->p_demux->out, p_sys->es );
147 p_sys->es = NULL;
150 /* Now init and fill es format */
151 vlc_fourcc_t i_chroma;
152 switch( p_gdi->bytesPerPixel )
154 default:
155 case 16:
156 i_chroma = VLC_CODEC_RGB16;
157 break;
158 case 24:
159 i_chroma = VLC_CODEC_RGB24;
160 break;
161 case 32:
162 i_chroma = VLC_CODEC_RGB32;
163 break;
165 es_format_t fmt;
166 es_format_Init( &fmt, VIDEO_ES, i_chroma );
168 fmt.video.i_chroma = i_chroma;
169 fmt.video.i_visible_width =
170 fmt.video.i_width = p_gdi->width;
171 fmt.video.i_visible_height =
172 fmt.video.i_height = p_gdi->height;
173 fmt.video.i_frame_rate_base = 1000;
174 fmt.video.i_frame_rate = 1000 * p_sys->f_fps;
175 p_sys->i_framebuffersize = p_gdi->width * p_gdi->height * p_gdi->bytesPerPixel;
177 if ( p_sys->p_block )
178 p_sys->p_block = block_Realloc( p_sys->p_block, 0, p_sys->i_framebuffersize );
179 else
180 p_sys->p_block = block_Alloc( p_sys->i_framebuffersize );
182 p_sys->es = es_out_Add( p_vlccontext->p_demux->out, &fmt );
185 static void beginPaintHandler( rdpContext *p_context )
187 vlcrdp_context_t * p_vlccontext = (vlcrdp_context_t *) p_context;
188 demux_sys_t *p_sys = p_vlccontext->p_demux->p_sys;
189 rdpGdi *p_gdi = p_context->gdi;
190 p_gdi->primary->hdc->hwnd->invalid->null = 1;
191 p_gdi->primary->hdc->hwnd->ninvalid = 0;
192 if ( ! p_sys->p_block && p_sys->i_framebuffersize )
193 p_sys->p_block = block_Alloc( p_sys->i_framebuffersize );
196 static void endPaintHandler( rdpContext *p_context )
198 vlcrdp_context_t * p_vlccontext = (vlcrdp_context_t *) p_context;
199 demux_sys_t *p_sys = p_vlccontext->p_demux->p_sys;
200 rdpGdi *p_gdi = p_context->gdi;
202 if ( p_sys->p_block )
204 p_sys->p_block->i_buffer = p_sys->i_framebuffersize;
205 memcpy( p_sys->p_block->p_buffer, p_gdi->primary_buffer, p_sys->p_block->i_buffer );
209 /* instance handlers */
211 static bool preConnectHandler( freerdp *p_instance )
213 vlcrdp_context_t * p_vlccontext = (vlcrdp_context_t *) p_instance->context;
214 demux_sys_t *p_sys = p_vlccontext->p_demux->p_sys;
216 /* Configure connexion */
217 p_instance->settings->SoftwareGdi = true; /* render in buffer */
218 p_instance->settings->Fullscreen = true;
219 p_instance->settings->ServerHostname = strdup( p_sys->psz_hostname );
220 p_instance->settings->Username =
221 var_InheritString( p_vlccontext->p_demux, CFG_PREFIX "user" );
222 p_instance->settings->Password =
223 var_InheritString( p_vlccontext->p_demux, CFG_PREFIX "password" );
224 p_instance->settings->ServerPort = p_sys->i_port;
225 p_instance->settings->EncryptionMethods =
226 var_InheritBool( p_vlccontext->p_demux, CFG_PREFIX "encrypt" );
228 return true;
231 static bool postConnectHandler( freerdp *p_instance )
233 vlcrdp_context_t * p_vlccontext = (vlcrdp_context_t *) p_instance->context;
235 msg_Dbg( p_vlccontext->p_demux, "connected to desktop %dx%d (%d bpp)",
236 #if defined(FREERDP_VERSION_MAJOR) && (FREERDP_VERSION_MAJOR >= 1 && FREERDP_VERSION_MINOR >= 1 )
237 p_instance->settings->DesktopWidth,
238 p_instance->settings->DesktopHeight,
239 p_instance->settings->ColorDepth
240 #else
241 p_instance->settings->width,
242 p_instance->settings->height,
243 p_instance->settings->color_depth
244 #endif
247 p_instance->update->DesktopResize = desktopResizeHandler;
248 p_instance->update->BeginPaint = beginPaintHandler;
249 p_instance->update->EndPaint = endPaintHandler;
251 gdi_init( p_instance, CLRBUF_16BPP | CLRBUF_24BPP | CLRBUF_32BPP, NULL );
253 desktopResizeHandler( p_instance->context );
254 return true;
257 static bool authenticateHandler( freerdp *p_instance, char** ppsz_username,
258 char** ppsz_password, char** ppsz_domain )
260 VLC_UNUSED(ppsz_domain);
261 vlcrdp_context_t * p_vlccontext = (vlcrdp_context_t *) p_instance->context;
262 *ppsz_username = var_InheritString( p_vlccontext->p_demux, CFG_PREFIX "user" );
263 *ppsz_password = var_InheritString( p_vlccontext->p_demux, CFG_PREFIX "password" );
264 return true;
267 /*****************************************************************************
268 * Control:
269 *****************************************************************************/
270 static int Control( demux_t *p_demux, int i_query, va_list args )
272 bool *pb;
273 int64_t *pi64;
274 double *p_dbl;
275 vlc_meta_t *p_meta;
277 switch( i_query )
279 case DEMUX_CAN_PAUSE:
280 case DEMUX_CAN_SEEK:
281 case DEMUX_CAN_CONTROL_PACE:
282 case DEMUX_CAN_CONTROL_RATE:
283 case DEMUX_HAS_UNSUPPORTED_META:
284 pb = (bool*)va_arg( args, bool * );
285 *pb = false;
286 return VLC_SUCCESS;
288 case DEMUX_CAN_RECORD:
289 pb = (bool*)va_arg( args, bool * );
290 *pb = true;
291 return VLC_SUCCESS;
293 case DEMUX_GET_PTS_DELAY:
294 pi64 = (int64_t*)va_arg( args, int64_t * );
295 *pi64 = INT64_C(1000)
296 * var_InheritInteger( p_demux, "live-caching" );
297 return VLC_SUCCESS;
299 case DEMUX_GET_TIME:
300 pi64 = (int64_t*)va_arg( args, int64_t * );
301 *pi64 = mdate() - p_demux->p_sys->i_starttime;
302 return VLC_SUCCESS;
304 case DEMUX_GET_LENGTH:
305 pi64 = (int64_t*)va_arg( args, int64_t * );
306 *pi64 = 0;
307 return VLC_SUCCESS;
309 case DEMUX_GET_FPS:
310 p_dbl = (double*)va_arg( args, double * );
311 *p_dbl = p_demux->p_sys->f_fps;
312 return VLC_SUCCESS;
314 case DEMUX_GET_META:
315 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
316 vlc_meta_Set( p_meta, vlc_meta_Title, p_demux->psz_location );
317 return VLC_SUCCESS;
319 default:
320 return VLC_EGENERIC;
324 /*****************************************************************************
325 * Demux:
326 *****************************************************************************/
327 static void *DemuxThread( void *p_data )
329 demux_t *p_demux = (demux_t *) p_data;
330 demux_sys_t *p_sys = p_demux->p_sys;
331 p_sys->i_starttime = mdate();
332 mtime_t i_next_frame_date = mdate() + p_sys->i_frame_interval;
333 int i_ret;
335 for(;;)
337 i_ret = 0;
338 p_sys->i_cancel_state = vlc_savecancel();
339 if ( freerdp_shall_disconnect( p_sys->p_instance ) )
341 vlc_restorecancel( p_sys->i_cancel_state );
342 msg_Warn( p_demux, "RDP server closed session" );
343 es_out_Del( p_demux->out, p_sys->es );
344 p_sys->es = NULL;
345 return NULL;
348 struct
350 void* pp_rfds[RDP_MAX_FD]; /* Declared by rdp */
351 void* pp_wfds[RDP_MAX_FD];
352 int i_nbr;
353 int i_nbw;
354 struct pollfd ufds[RDP_MAX_FD];
355 } fds;
357 fds.i_nbr = fds.i_nbw = 0;
359 if ( freerdp_get_fds( p_sys->p_instance, fds.pp_rfds, &fds.i_nbr,
360 fds.pp_wfds, &fds.i_nbw ) != true )
362 vlc_restorecancel( p_sys->i_cancel_state );
363 msg_Err( p_demux, "cannot get FDS" );
365 else
366 if ( (fds.i_nbr + fds.i_nbw) > 0 && p_sys->es )
368 vlc_restorecancel( p_sys->i_cancel_state );
369 int i_count = 0;
371 for( int i = 0; i < fds.i_nbr; i++ )
373 fds.ufds[ i_count ].fd = (long) fds.pp_rfds[ i ];
374 fds.ufds[ i_count ].events = POLLIN ;
375 fds.ufds[ i_count++ ].revents = 0;
377 for( int i = 0; i < fds.i_nbw && i_count < RDP_MAX_FD; i++ )
378 { /* may be useless */
379 fds.ufds[ i_count ].fd = (long) fds.pp_wfds[ i ];
380 fds.ufds[ i_count ].events = POLLOUT;
381 fds.ufds[ i_count++ ].revents = 0;
383 i_ret = poll( fds.ufds, i_count, p_sys->i_frame_interval * 1000/2 );
384 } else {
385 vlc_restorecancel( p_sys->i_cancel_state );
388 mwait( i_next_frame_date );
389 i_next_frame_date += p_sys->i_frame_interval;
391 if ( i_ret >= 0 )
393 /* Do the rendering */
394 p_sys->i_cancel_state = vlc_savecancel();
395 freerdp_check_fds( p_sys->p_instance );
396 vlc_restorecancel( p_sys->i_cancel_state );
397 block_t *p_block = block_Duplicate( p_sys->p_block );
398 if (likely( p_block && p_sys->p_block ))
400 p_sys->p_block->i_dts = p_sys->p_block->i_pts = mdate() - p_sys->i_starttime;
401 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->p_block->i_pts );
402 es_out_Send( p_demux->out, p_sys->es, p_sys->p_block );
403 p_sys->p_block = p_block;
407 return NULL;
410 /*****************************************************************************
411 * Open:
412 *****************************************************************************/
413 static int Open( vlc_object_t *p_this )
415 demux_t *p_demux = (demux_t*)p_this;
416 demux_sys_t *p_sys;
418 p_sys = calloc( 1, sizeof(demux_sys_t) );
419 if( !p_sys ) return VLC_ENOMEM;
421 p_sys->f_fps = var_InheritFloat( p_demux, CFG_PREFIX "fps" );
422 if ( p_sys->f_fps <= 0 ) p_sys->f_fps = 1.0;
423 p_sys->i_frame_interval = 1000000 / p_sys->f_fps;
425 freerdp_channels_global_init();
427 p_sys->p_instance = freerdp_new();
428 if ( !p_sys->p_instance )
430 msg_Err( p_demux, "rdp instanciation error" );
431 free( p_sys );
432 return VLC_EGENERIC;
435 p_demux->p_sys = p_sys;
436 p_sys->p_instance->PreConnect = preConnectHandler;
437 p_sys->p_instance->PostConnect = postConnectHandler;
438 p_sys->p_instance->Authenticate = authenticateHandler;
440 /* Set up context handlers and let it be allocated */
441 p_sys->p_instance->ContextSize = sizeof( vlcrdp_context_t );
442 freerdp_context_new( p_sys->p_instance );
444 vlcrdp_context_t * p_vlccontext = (vlcrdp_context_t *) p_sys->p_instance->context;
445 p_vlccontext->p_demux = p_demux;
447 /* Parse uri params for pre-connect */
448 vlc_url_t url;
449 vlc_UrlParse( &url, p_demux->psz_location, 0 );
451 if ( !EMPTY_STR(url.psz_host) )
452 p_sys->psz_hostname = strdup( url.psz_host );
453 else
454 p_sys->psz_hostname = strdup( "localhost" );
456 p_sys->i_port = ( url.i_port > 0 ) ? url.i_port : 3389;
458 vlc_UrlClean( &url );
460 if ( ! freerdp_connect( p_sys->p_instance ) )
462 msg_Err( p_demux, "can't connect to rdp server" );
463 goto error;
466 if ( vlc_clone( &p_sys->thread, DemuxThread, p_demux, VLC_THREAD_PRIORITY_INPUT ) != VLC_SUCCESS )
468 msg_Err( p_demux, "can't spawn thread" );
469 freerdp_disconnect( p_sys->p_instance );
470 goto error;
473 p_demux->pf_demux = NULL;
474 p_demux->pf_control = Control;
476 return VLC_SUCCESS;
478 error:
479 freerdp_free( p_sys->p_instance );
480 free( p_sys->psz_hostname );
481 free( p_sys );
482 return VLC_EGENERIC;
485 /*****************************************************************************
486 * Close:
487 *****************************************************************************/
488 static void Close( vlc_object_t *p_this )
490 demux_t *p_demux = (demux_t*)p_this;
491 demux_sys_t *p_sys = p_demux->p_sys;
493 vlc_cancel( p_sys->thread );
494 vlc_join( p_sys->thread, NULL );
496 if ( p_sys->es )
497 es_out_Del( p_demux->out, p_sys->es );
499 freerdp_disconnect( p_sys->p_instance );
500 freerdp_free( p_sys->p_instance );
501 freerdp_channels_global_uninit();
503 if ( p_sys->p_block )
504 block_Release( p_sys->p_block );
506 free( p_sys->psz_hostname );
507 free( p_sys );