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 /*****************************************************************************
23 *****************************************************************************/
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_demux.h>
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>
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
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 /*****************************************************************************
76 *****************************************************************************/
77 static int Open ( vlc_object_t
* );
78 static void Close( vlc_object_t
* );
81 set_shortname( N_("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 )
90 add_password( CFG_PREFIX
"password", NULL
, RDP_PASSWORD
, RDP_PASSWORD_LONGTEXT
, false )
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 )
97 set_callbacks( Open
, Close
)
100 #define RDP_MAX_FD 32
107 int i_framebuffersize
;
110 int i_frame_interval
;
115 /* pre-connect params */
124 struct vlcrdp_context_t
126 rdpContext rdp_context
; /* Extending API's struct */
128 rdpSettings
* p_settings
;
130 typedef struct vlcrdp_context_t vlcrdp_context_t
;
132 /*****************************************************************************
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
;
146 es_out_Del( p_vlccontext
->p_demux
->out
, p_sys
->es
);
150 /* Now init and fill es format */
151 vlc_fourcc_t i_chroma
;
152 switch( p_gdi
->bytesPerPixel
)
156 i_chroma
= VLC_CODEC_RGB16
;
159 i_chroma
= VLC_CODEC_RGB24
;
162 i_chroma
= VLC_CODEC_RGB32
;
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
);
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" );
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
241 p_instance
->settings
->width
,
242 p_instance
->settings
->height
,
243 p_instance
->settings
->color_depth
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
);
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" );
267 /*****************************************************************************
269 *****************************************************************************/
270 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
279 case DEMUX_CAN_PAUSE
:
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 * );
288 case DEMUX_CAN_RECORD
:
289 pb
= (bool*)va_arg( args
, bool * );
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" );
300 pi64
= (int64_t*)va_arg( args
, int64_t * );
301 *pi64
= mdate() - p_demux
->p_sys
->i_starttime
;
304 case DEMUX_GET_LENGTH
:
305 pi64
= (int64_t*)va_arg( args
, int64_t * );
310 p_dbl
= (double*)va_arg( args
, double * );
311 *p_dbl
= p_demux
->p_sys
->f_fps
;
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
);
324 /*****************************************************************************
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
;
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
);
350 void* pp_rfds
[RDP_MAX_FD
]; /* Declared by rdp */
351 void* pp_wfds
[RDP_MAX_FD
];
354 struct pollfd ufds
[RDP_MAX_FD
];
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" );
366 if ( (fds
.i_nbr
+ fds
.i_nbw
) > 0 && p_sys
->es
)
368 vlc_restorecancel( p_sys
->i_cancel_state
);
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 );
385 vlc_restorecancel( p_sys
->i_cancel_state
);
388 mwait( i_next_frame_date
);
389 i_next_frame_date
+= p_sys
->i_frame_interval
;
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
;
410 /*****************************************************************************
412 *****************************************************************************/
413 static int Open( vlc_object_t
*p_this
)
415 demux_t
*p_demux
= (demux_t
*)p_this
;
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" );
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 */
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
);
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" );
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
);
473 p_demux
->pf_demux
= NULL
;
474 p_demux
->pf_control
= Control
;
479 freerdp_free( p_sys
->p_instance
);
480 free( p_sys
->psz_hostname
);
485 /*****************************************************************************
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
);
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
);