]> git.sesse.net Git - vlc/blob - modules/access/rdp.c
FreeRDP: support 1.1.0-beta2 API
[vlc] / modules / access / rdp.c
1 /*****************************************************************************
2  * rdp.c: libfreeRDP based Remote Desktop access
3  *****************************************************************************
4  * Copyright (C) 2013 VideoLAN Authors
5  *****************************************************************************
6  *
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.
11  *
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.
16  *
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  *****************************************************************************/
20
21 /*****************************************************************************
22  * Preamble
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
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>
34
35 #define boolean bool
36
37 /* see MS-RDPBCGR http://msdn.microsoft.com/en-us/library/cc240445.aspx */
38
39 #include <freerdp/freerdp.h>
40 #include <freerdp/settings.h>
41 #include <freerdp/channels/channels.h>
42 #include <freerdp/gdi/gdi.h>
43
44 #if !defined(FREERDP_INTERFACE_VERSION)
45 # include <freerdp/version.h>
46 #endif
47
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
59
60 #include <errno.h>
61 #ifdef HAVE_POLL
62 # include <poll.h>
63 #endif
64
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)")
71
72 #define CFG_PREFIX "rdp-"
73
74 /*****************************************************************************
75  * Module descriptor
76  *****************************************************************************/
77 static int  Open ( vlc_object_t * );
78 static void Close( vlc_object_t * );
79
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 )
87
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 )
93
94     add_bool( CFG_PREFIX "encrypt", false, RDP_ENCRYPT, RDP_ENCRYPT, true )
95         change_safe()
96
97     set_callbacks( Open, Close )
98 vlc_module_end()
99
100 #define RDP_MAX_FD 32
101
102 struct demux_sys_t
103 {
104     vlc_thread_t thread;
105     freerdp *p_instance;
106     block_t *p_block;
107     int i_framebuffersize;
108
109     float f_fps;
110     int i_frame_interval;
111     mtime_t i_starttime;
112
113     es_out_id_t *es;
114
115     /* pre-connect params */
116     char *psz_hostname;
117     int i_port;
118     /* cancelability */
119     int i_cancel_state;
120 };
121
122 /* context */
123
124 struct vlcrdp_context_t
125 {
126     rdpContext rdp_context; /* Extending API's struct */
127     demux_t *p_demux;
128     rdpSettings* p_settings;
129 };
130 typedef struct vlcrdp_context_t vlcrdp_context_t;
131
132 /*****************************************************************************
133  * Local prototypes
134  *****************************************************************************/
135
136 /* updates handlers */
137
138 static void desktopResizeHandler( rdpContext *p_context )
139 {
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;
143
144     if ( p_sys->es )
145     {
146         es_out_Del( p_vlccontext->p_demux->out, p_sys->es );
147         p_sys->es = NULL;
148     }
149
150     /* Now init and fill es format */
151     vlc_fourcc_t i_chroma;
152     switch( p_gdi->bytesPerPixel )
153     {
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;
164     }
165     es_format_t fmt;
166     es_format_Init( &fmt, VIDEO_ES, i_chroma );
167
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;
176
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 );
181
182     p_sys->es = es_out_Add( p_vlccontext->p_demux->out, &fmt );
183 }
184
185 static void beginPaintHandler( rdpContext *p_context )
186 {
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 );
194 }
195
196 static void endPaintHandler( rdpContext *p_context )
197 {
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;
201
202     if ( p_sys->p_block )
203     {
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 );
206     }
207 }
208
209 /* instance handlers */
210
211 static bool preConnectHandler( freerdp *p_instance )
212 {
213     vlcrdp_context_t * p_vlccontext = (vlcrdp_context_t *) p_instance->context;
214     demux_sys_t *p_sys = p_vlccontext->p_demux->p_sys;
215
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" );
227
228     return true;
229 }
230
231 static bool postConnectHandler( freerdp *p_instance )
232 {
233     vlcrdp_context_t * p_vlccontext = (vlcrdp_context_t *) p_instance->context;
234
235     msg_Dbg( p_vlccontext->p_demux, "connected to desktop %dx%d (%d bpp)",
236 #if (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
245              );
246
247     p_instance->update->DesktopResize = desktopResizeHandler;
248     p_instance->update->BeginPaint = beginPaintHandler;
249     p_instance->update->EndPaint = endPaintHandler;
250
251     gdi_init( p_instance, CLRBUF_16BPP | CLRBUF_24BPP | CLRBUF_32BPP, NULL );
252
253     desktopResizeHandler( p_instance->context );
254     return true;
255 }
256
257 static bool authenticateHandler( freerdp *p_instance, char** ppsz_username,
258                                  char** ppsz_password, char** ppsz_domain )
259 {
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;
265 }
266
267 /*****************************************************************************
268  * Control:
269  *****************************************************************************/
270 static int Control( demux_t *p_demux, int i_query, va_list args )
271 {
272     bool *pb;
273     int64_t *pi64;
274     double *p_dbl;
275     vlc_meta_t *p_meta;
276
277     switch( i_query )
278     {
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;
287
288         case DEMUX_CAN_RECORD:
289             pb = (bool*)va_arg( args, bool * );
290             *pb = true;
291             return VLC_SUCCESS;
292
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;
298
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;
303
304         case DEMUX_GET_LENGTH:
305             pi64 = (int64_t*)va_arg( args, int64_t * );
306             *pi64 = 0;
307             return VLC_SUCCESS;
308
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;
313
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;
318
319         default:
320             return VLC_EGENERIC;
321     }
322 }
323
324 /*****************************************************************************
325  * Demux:
326  *****************************************************************************/
327 static void *DemuxThread( void *p_data )
328 {
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;
334
335     for(;;)
336     {
337         i_ret = 0;
338         p_sys->i_cancel_state = vlc_savecancel();
339         if ( freerdp_shall_disconnect( p_sys->p_instance ) )
340         {
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;
346         }
347
348         struct
349         {
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;
356
357         fds.i_nbr = fds.i_nbw = 0;
358
359         if ( freerdp_get_fds( p_sys->p_instance, fds.pp_rfds, &fds.i_nbr,
360                               fds.pp_wfds, &fds.i_nbw ) != true )
361         {
362             vlc_restorecancel( p_sys->i_cancel_state );
363             msg_Err( p_demux, "cannot get FDS" );
364         }
365         else
366         if ( (fds.i_nbr + fds.i_nbw) > 0 && p_sys->es )
367         {
368             vlc_restorecancel( p_sys->i_cancel_state );
369             int i_count = 0;
370
371             for( int i = 0; i < fds.i_nbr; i++ )
372             {
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;
376             }
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;
382             }
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 );
386         }
387
388         mwait( i_next_frame_date );
389         i_next_frame_date += p_sys->i_frame_interval;
390
391         if ( i_ret >= 0 )
392         {
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 ))
399             {
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;
404             }
405         }
406     }
407     return NULL;
408 }
409
410 /*****************************************************************************
411  * Open:
412  *****************************************************************************/
413 static int Open( vlc_object_t *p_this )
414 {
415     demux_t      *p_demux = (demux_t*)p_this;
416     demux_sys_t  *p_sys;
417
418     p_sys = calloc( 1, sizeof(demux_sys_t) );
419     if( !p_sys ) return VLC_ENOMEM;
420
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;
424
425     freerdp_channels_global_init();
426
427     p_sys->p_instance = freerdp_new();
428     if ( !p_sys->p_instance )
429     {
430         msg_Err( p_demux, "rdp instanciation error" );
431         free( p_sys );
432         return VLC_EGENERIC;
433     }
434
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;
439
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 );
443
444     vlcrdp_context_t * p_vlccontext = (vlcrdp_context_t *) p_sys->p_instance->context;
445     p_vlccontext->p_demux = p_demux;
446
447     /* Parse uri params for pre-connect */
448     vlc_url_t url;
449     vlc_UrlParse( &url, p_demux->psz_location, 0 );
450
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" );
455
456     p_sys->i_port = ( url.i_port > 0 ) ? url.i_port : 3389;
457
458     vlc_UrlClean( &url );
459
460     if ( ! freerdp_connect( p_sys->p_instance ) )
461     {
462         msg_Err( p_demux, "can't connect to rdp server" );
463         goto error;
464     }
465
466     if ( vlc_clone( &p_sys->thread, DemuxThread, p_demux, VLC_THREAD_PRIORITY_INPUT ) != VLC_SUCCESS )
467     {
468         msg_Err( p_demux, "can't spawn thread" );
469         freerdp_disconnect( p_sys->p_instance );
470         goto error;
471     }
472
473     p_demux->pf_demux = NULL;
474     p_demux->pf_control = Control;
475
476     return VLC_SUCCESS;
477
478 error:
479     freerdp_free( p_sys->p_instance );
480     free( p_sys->psz_hostname );
481     free( p_sys );
482     return VLC_EGENERIC;
483 }
484
485 /*****************************************************************************
486  * Close:
487  *****************************************************************************/
488 static void Close( vlc_object_t *p_this )
489 {
490     demux_t     *p_demux = (demux_t*)p_this;
491     demux_sys_t *p_sys = p_demux->p_sys;
492
493     vlc_cancel( p_sys->thread );
494     vlc_join( p_sys->thread, NULL );
495
496     if ( p_sys->es )
497         es_out_Del( p_demux->out, p_sys->es );
498
499     freerdp_disconnect( p_sys->p_instance );
500     freerdp_free( p_sys->p_instance );
501     freerdp_channels_global_uninit();
502
503     if ( p_sys->p_block )
504         block_Release( p_sys->p_block );
505
506     free( p_sys->psz_hostname );
507     free( p_sys );
508 }