8ecceb60a3fa03ec3d3db635d6ea7f5228820c49
[vlc.git] / modules / access / rdp.c
1 /*****************************************************************************
2  * rdp.c: libfreeRDP based Remote Desktop access
3  *****************************************************************************
4  * Copyright (C) 2013 VideoLAN and VLC 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_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 USER_TEXT N_("Username")
66 #define USER_LONGTEXT N_("Username that will be used for the connection, " \
67         "if no username is set in the URL.")
68 #define PASS_TEXT N_("Password")
69 #define PASS_LONGTEXT N_("Password that will be used for the connection, " \
70         "if no username or password are set in URL.")
71
72 #define RDP_ENCRYPT N_("Encrypted connexion")
73 #define RDP_FPS N_("Frame rate")
74 #define RDP_FPS_LONGTEXT N_("Acquisition rate (in fps)")
75
76 #define CFG_PREFIX "rdp-"
77
78 /*****************************************************************************
79  * Module descriptor
80  *****************************************************************************/
81 static int  Open ( vlc_object_t * );
82 static void Close( vlc_object_t * );
83
84 vlc_module_begin()
85     set_shortname( N_("RDP") )
86     add_shortcut( "rdp" )
87     set_category( CAT_INPUT )
88     set_subcategory( SUBCAT_INPUT_ACCESS )
89     set_description( N_("RDP Remote Desktop") )
90     set_capability( "access", 0 )
91
92     add_string( CFG_PREFIX "user", NULL, USER_TEXT, USER_LONGTEXT, false )
93         change_safe()
94     add_password( CFG_PREFIX "password", NULL, PASS_TEXT, PASS_LONGTEXT, false )
95         change_safe()
96     add_float( CFG_PREFIX "fps", 5, RDP_FPS, RDP_FPS_LONGTEXT, true )
97
98     add_bool( CFG_PREFIX "encrypt", false, RDP_ENCRYPT, RDP_ENCRYPT, true )
99         change_safe()
100
101     set_callbacks( Open, Close )
102 vlc_module_end()
103
104 #define RDP_MAX_FD 32
105
106 struct demux_sys_t
107 {
108     vlc_thread_t thread;
109     freerdp *p_instance;
110     block_t *p_block;
111     int i_framebuffersize;
112
113     float f_fps;
114     int i_frame_interval;
115     mtime_t i_starttime;
116
117     es_out_id_t *es;
118
119     /* pre-connect params */
120     char *psz_hostname;
121     int i_port;
122     /* cancelability */
123     int i_cancel_state;
124 };
125
126 /* context */
127
128 struct vlcrdp_context_t
129 {
130     rdpContext rdp_context; /* Extending API's struct */
131     demux_t *p_demux;
132     rdpSettings* p_settings;
133 };
134 typedef struct vlcrdp_context_t vlcrdp_context_t;
135
136 /*****************************************************************************
137  * Local prototypes
138  *****************************************************************************/
139
140 /* updates handlers */
141
142 static void desktopResizeHandler( rdpContext *p_context )
143 {
144     vlcrdp_context_t * p_vlccontext = (vlcrdp_context_t *) p_context;
145     demux_sys_t *p_sys = p_vlccontext->p_demux->p_sys;
146     rdpGdi *p_gdi = p_context->gdi;
147
148     if ( p_sys->es )
149     {
150         es_out_Del( p_vlccontext->p_demux->out, p_sys->es );
151         p_sys->es = NULL;
152     }
153
154     /* Now init and fill es format */
155     vlc_fourcc_t i_chroma;
156     switch( p_gdi->bytesPerPixel )
157     {
158         default:
159         case 16:
160             i_chroma = VLC_CODEC_RGB16;
161             break;
162         case 24:
163             i_chroma = VLC_CODEC_RGB24;
164             break;
165         case 32:
166             i_chroma = VLC_CODEC_RGB32;
167             break;
168     }
169     es_format_t fmt;
170     es_format_Init( &fmt, VIDEO_ES, i_chroma );
171
172     fmt.video.i_chroma = i_chroma;
173     fmt.video.i_visible_width =
174     fmt.video.i_width = p_gdi->width;
175     fmt.video.i_visible_height =
176     fmt.video.i_height = p_gdi->height;
177     fmt.video.i_frame_rate_base = 1000;
178     fmt.video.i_frame_rate = 1000 * p_sys->f_fps;
179     p_sys->i_framebuffersize = p_gdi->width * p_gdi->height * p_gdi->bytesPerPixel;
180
181     if ( p_sys->p_block )
182         p_sys->p_block = block_Realloc( p_sys->p_block, 0, p_sys->i_framebuffersize );
183     else
184         p_sys->p_block = block_Alloc( p_sys->i_framebuffersize );
185
186     p_sys->es = es_out_Add( p_vlccontext->p_demux->out, &fmt );
187 }
188
189 static void beginPaintHandler( rdpContext *p_context )
190 {
191     vlcrdp_context_t * p_vlccontext = (vlcrdp_context_t *) p_context;
192     demux_sys_t *p_sys = p_vlccontext->p_demux->p_sys;
193     rdpGdi *p_gdi = p_context->gdi;
194     p_gdi->primary->hdc->hwnd->invalid->null = 1;
195     p_gdi->primary->hdc->hwnd->ninvalid = 0;
196     if ( ! p_sys->p_block && p_sys->i_framebuffersize )
197         p_sys->p_block = block_Alloc( p_sys->i_framebuffersize );
198 }
199
200 static void endPaintHandler( rdpContext *p_context )
201 {
202     vlcrdp_context_t * p_vlccontext = (vlcrdp_context_t *) p_context;
203     demux_sys_t *p_sys = p_vlccontext->p_demux->p_sys;
204     rdpGdi *p_gdi = p_context->gdi;
205
206     if ( p_sys->p_block )
207     {
208         p_sys->p_block->i_buffer = p_sys->i_framebuffersize;
209         memcpy( p_sys->p_block->p_buffer, p_gdi->primary_buffer, p_sys->p_block->i_buffer );
210     }
211 }
212
213 /* instance handlers */
214
215 static bool preConnectHandler( freerdp *p_instance )
216 {
217     vlcrdp_context_t * p_vlccontext = (vlcrdp_context_t *) p_instance->context;
218     demux_sys_t *p_sys = p_vlccontext->p_demux->p_sys;
219
220     /* Configure connexion */
221     p_instance->settings->SoftwareGdi = true; /* render in buffer */
222     p_instance->settings->Fullscreen = true;
223     p_instance->settings->ServerHostname = strdup( p_sys->psz_hostname );
224     p_instance->settings->Username =
225             var_InheritString( p_vlccontext->p_demux, CFG_PREFIX "user" );
226     p_instance->settings->Password =
227             var_InheritString( p_vlccontext->p_demux, CFG_PREFIX "password" );
228     p_instance->settings->ServerPort = p_sys->i_port;
229     p_instance->settings->EncryptionMethods =
230             var_InheritBool( p_vlccontext->p_demux, CFG_PREFIX "encrypt" );
231
232     return true;
233 }
234
235 static bool postConnectHandler( freerdp *p_instance )
236 {
237     vlcrdp_context_t * p_vlccontext = (vlcrdp_context_t *) p_instance->context;
238
239     msg_Dbg( p_vlccontext->p_demux, "connected to desktop %dx%d (%d bpp)",
240 #if defined(FREERDP_VERSION_MAJOR) && (FREERDP_VERSION_MAJOR > 1 || (FREERDP_VERSION_MAJOR == 1 && FREERDP_VERSION_MINOR >= 1))
241              p_instance->settings->DesktopWidth,
242              p_instance->settings->DesktopHeight,
243              p_instance->settings->ColorDepth
244 #else
245              p_instance->settings->width,
246              p_instance->settings->height,
247              p_instance->settings->color_depth
248 #endif
249              );
250
251     p_instance->update->DesktopResize = desktopResizeHandler;
252     p_instance->update->BeginPaint = beginPaintHandler;
253     p_instance->update->EndPaint = endPaintHandler;
254
255     gdi_init( p_instance,
256                 CLRBUF_16BPP |
257 #if defined(FREERDP_VERSION_MAJOR) && defined(FREERDP_VERSION_MINOR) && \
258     !(FREERDP_VERSION_MAJOR > 1 || (FREERDP_VERSION_MAJOR == 1 && FREERDP_VERSION_MINOR >= 2))
259                 CLRBUF_24BPP |
260 #endif
261                 CLRBUF_32BPP, NULL );
262
263     desktopResizeHandler( p_instance->context );
264     return true;
265 }
266
267 static bool authenticateHandler( freerdp *p_instance, char** ppsz_username,
268                                  char** ppsz_password, char** ppsz_domain )
269 {
270     VLC_UNUSED(ppsz_domain);
271     vlcrdp_context_t * p_vlccontext = (vlcrdp_context_t *) p_instance->context;
272     *ppsz_username = var_InheritString( p_vlccontext->p_demux, CFG_PREFIX "user" );
273     *ppsz_password = var_InheritString( p_vlccontext->p_demux, CFG_PREFIX "password" );
274     return true;
275 }
276
277 /*****************************************************************************
278  * Control:
279  *****************************************************************************/
280 static int Control( demux_t *p_demux, int i_query, va_list args )
281 {
282     demux_sys_t *p_sys = p_demux->p_sys;
283     bool *pb;
284     int64_t *pi64;
285     double *p_dbl;
286     vlc_meta_t *p_meta;
287
288     switch( i_query )
289     {
290         case DEMUX_CAN_PAUSE:
291         case DEMUX_CAN_SEEK:
292         case DEMUX_CAN_CONTROL_PACE:
293         case DEMUX_CAN_CONTROL_RATE:
294         case DEMUX_HAS_UNSUPPORTED_META:
295             pb = va_arg( args, bool * );
296             *pb = false;
297             return VLC_SUCCESS;
298
299         case DEMUX_CAN_RECORD:
300             pb = va_arg( args, bool * );
301             *pb = true;
302             return VLC_SUCCESS;
303
304         case DEMUX_GET_PTS_DELAY:
305             pi64 = va_arg( args, int64_t * );
306             *pi64 = INT64_C(1000)
307                   * var_InheritInteger( p_demux, "live-caching" );
308             return VLC_SUCCESS;
309
310         case DEMUX_GET_TIME:
311             pi64 = va_arg( args, int64_t * );
312             *pi64 = mdate() - p_sys->i_starttime;
313             return VLC_SUCCESS;
314
315         case DEMUX_GET_LENGTH:
316             pi64 = va_arg( args, int64_t * );
317             *pi64 = 0;
318             return VLC_SUCCESS;
319
320         case DEMUX_GET_FPS:
321             p_dbl = va_arg( args, double * );
322             *p_dbl = p_sys->f_fps;
323             return VLC_SUCCESS;
324
325         case DEMUX_GET_META:
326             p_meta = va_arg( args, vlc_meta_t * );
327             vlc_meta_Set( p_meta, vlc_meta_Title, p_demux->psz_location );
328             return VLC_SUCCESS;
329
330         default:
331             return VLC_EGENERIC;
332     }
333 }
334
335 /*****************************************************************************
336  * Demux:
337  *****************************************************************************/
338 static void *DemuxThread( void *p_data )
339 {
340     demux_t *p_demux = (demux_t *) p_data;
341     demux_sys_t *p_sys = p_demux->p_sys;
342     p_sys->i_starttime = mdate();
343     mtime_t i_next_frame_date = mdate() + p_sys->i_frame_interval;
344     int i_ret;
345
346     for(;;)
347     {
348         i_ret = 0;
349         p_sys->i_cancel_state = vlc_savecancel();
350         if ( freerdp_shall_disconnect( p_sys->p_instance ) )
351         {
352             vlc_restorecancel( p_sys->i_cancel_state );
353             msg_Warn( p_demux, "RDP server closed session" );
354             es_out_Del( p_demux->out, p_sys->es );
355             p_sys->es = NULL;
356             return NULL;
357         }
358
359         struct
360         {
361             void* pp_rfds[RDP_MAX_FD]; /* Declared by rdp */
362             void* pp_wfds[RDP_MAX_FD];
363             int i_nbr;
364             int i_nbw;
365             struct pollfd ufds[RDP_MAX_FD];
366         } fds;
367
368         fds.i_nbr = fds.i_nbw = 0;
369
370         if ( freerdp_get_fds( p_sys->p_instance, fds.pp_rfds, &fds.i_nbr,
371                               fds.pp_wfds, &fds.i_nbw ) != true )
372         {
373             vlc_restorecancel( p_sys->i_cancel_state );
374             msg_Err( p_demux, "cannot get FDS" );
375         }
376         else
377         if ( (fds.i_nbr + fds.i_nbw) > 0 && p_sys->es )
378         {
379             vlc_restorecancel( p_sys->i_cancel_state );
380             int i_count = 0;
381
382             for( int i = 0; i < fds.i_nbr; i++ )
383             {
384                 fds.ufds[ i_count ].fd = (long) fds.pp_rfds[ i ];
385                 fds.ufds[ i_count ].events = POLLIN ;
386                 fds.ufds[ i_count++ ].revents = 0;
387             }
388             for( int i = 0; i < fds.i_nbw && i_count < RDP_MAX_FD; i++ )
389             {   /* may be useless */
390                 fds.ufds[ i_count ].fd = (long) fds.pp_wfds[ i ];
391                 fds.ufds[ i_count ].events = POLLOUT;
392                 fds.ufds[ i_count++ ].revents = 0;
393             }
394             i_ret = poll( fds.ufds, i_count, p_sys->i_frame_interval * 1000/2 );
395         } else {
396             vlc_restorecancel( p_sys->i_cancel_state );
397         }
398
399         mwait( i_next_frame_date );
400         i_next_frame_date += p_sys->i_frame_interval;
401
402         if ( i_ret >= 0 )
403         {
404             /* Do the rendering */
405             p_sys->i_cancel_state = vlc_savecancel();
406             freerdp_check_fds( p_sys->p_instance );
407             vlc_restorecancel( p_sys->i_cancel_state );
408             block_t *p_block = block_Duplicate( p_sys->p_block );
409             if (likely( p_block && p_sys->p_block ))
410             {
411                 p_sys->p_block->i_dts = p_sys->p_block->i_pts = mdate() - p_sys->i_starttime;
412                 es_out_SetPCR( p_demux->out, p_sys->p_block->i_pts );
413                 es_out_Send( p_demux->out, p_sys->es, p_sys->p_block );
414                 p_sys->p_block = p_block;
415             }
416         }
417     }
418     return NULL;
419 }
420
421 /*****************************************************************************
422  * Open:
423  *****************************************************************************/
424 static int Open( vlc_object_t *p_this )
425 {
426     demux_t      *p_demux = (demux_t*)p_this;
427     demux_sys_t  *p_sys;
428
429     if (p_demux->out == NULL)
430         return VLC_EGENERIC;
431
432     p_sys = vlc_obj_calloc( p_this, 1, sizeof(demux_sys_t) );
433     if( !p_sys ) return VLC_ENOMEM;
434
435     p_sys->f_fps = var_InheritFloat( p_demux, CFG_PREFIX "fps" );
436     if ( p_sys->f_fps <= 0 ) p_sys->f_fps = 1.0;
437     p_sys->i_frame_interval = 1000000 / p_sys->f_fps;
438
439 #if FREERDP_VERSION_MAJOR == 1 && FREERDP_VERSION_MINOR < 2
440     freerdp_channels_global_init();
441 #endif
442
443     p_sys->p_instance = freerdp_new();
444     if ( !p_sys->p_instance )
445     {
446         msg_Err( p_demux, "rdp instantiation error" );
447         return VLC_EGENERIC;
448     }
449
450     p_demux->p_sys = p_sys;
451     p_sys->p_instance->PreConnect = preConnectHandler;
452     p_sys->p_instance->PostConnect = postConnectHandler;
453     p_sys->p_instance->Authenticate = authenticateHandler;
454
455     /* Set up context handlers and let it be allocated */
456     p_sys->p_instance->ContextSize = sizeof( vlcrdp_context_t );
457     freerdp_context_new( p_sys->p_instance );
458
459     vlcrdp_context_t * p_vlccontext = (vlcrdp_context_t *) p_sys->p_instance->context;
460     p_vlccontext->p_demux = p_demux;
461
462     /* Parse uri params for pre-connect */
463     vlc_url_t url;
464     vlc_UrlParse( &url, p_demux->psz_location );
465
466     if ( !EMPTY_STR(url.psz_host) )
467         p_sys->psz_hostname = strdup( url.psz_host );
468     else
469         p_sys->psz_hostname = strdup( "localhost" );
470
471     p_sys->i_port = ( url.i_port > 0 ) ? url.i_port : 3389;
472
473     vlc_UrlClean( &url );
474
475     if ( ! freerdp_connect( p_sys->p_instance ) )
476     {
477         msg_Err( p_demux, "can't connect to rdp server" );
478         goto error;
479     }
480
481     if ( vlc_clone( &p_sys->thread, DemuxThread, p_demux, VLC_THREAD_PRIORITY_INPUT ) != VLC_SUCCESS )
482     {
483         msg_Err( p_demux, "can't spawn thread" );
484         freerdp_disconnect( p_sys->p_instance );
485         goto error;
486     }
487
488     p_demux->pf_demux = NULL;
489     p_demux->pf_control = Control;
490
491     return VLC_SUCCESS;
492
493 error:
494     freerdp_free( p_sys->p_instance );
495     free( p_sys->psz_hostname );
496     return VLC_EGENERIC;
497 }
498
499 /*****************************************************************************
500  * Close:
501  *****************************************************************************/
502 static void Close( vlc_object_t *p_this )
503 {
504     demux_t     *p_demux = (demux_t*)p_this;
505     demux_sys_t *p_sys = p_demux->p_sys;
506
507     vlc_cancel( p_sys->thread );
508     vlc_join( p_sys->thread, NULL );
509
510     if ( p_sys->es )
511         es_out_Del( p_demux->out, p_sys->es );
512
513     freerdp_disconnect( p_sys->p_instance );
514     freerdp_free( p_sys->p_instance );
515 #if FREERDP_VERSION_MAJOR == 1 && FREERDP_VERSION_MINOR < 2
516     freerdp_channels_global_uninit();
517 #endif
518
519     if ( p_sys->p_block )
520         block_Release( p_sys->p_block );
521
522     free( p_sys->psz_hostname );
523 }