]> git.sesse.net Git - vlc/blob - modules/access/screen/screen.c
12fb132d2c7cb885ce451d379ba0e1fd972044bd
[vlc] / modules / access / screen / screen.c
1 /*****************************************************************************
2  * screen.c: Screen capture module.
3  *****************************************************************************
4  * Copyright (C) 2004-2008 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          Antoine Cellerier <dionoea at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_modules.h>                 /* module_need for "video blending" */
36 #include "screen.h"
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 #define FPS_TEXT N_("Frame rate")
42 #define FPS_LONGTEXT N_( \
43     "Desired frame rate for the capture." )
44
45 #ifdef _WIN32
46 #define FRAGS_TEXT N_("Capture fragment size")
47 #define FRAGS_LONGTEXT N_( \
48     "Optimize the capture by fragmenting the screen in chunks " \
49     "of predefined height (16 might be a good value, and 0 means disabled)." )
50 #endif
51
52 #ifdef SCREEN_SUBSCREEN
53 #define TOP_TEXT N_( "Subscreen top left corner" )
54 #define TOP_LONGTEXT N_( \
55     "Top coordinate of the subscreen top left corner." )
56
57 #define LEFT_TEXT N_( "Subscreen top left corner" )
58 #define LEFT_LONGTEXT N_( \
59     "Left coordinate of the subscreen top left corner." )
60
61 #define WIDTH_TEXT N_( "Subscreen width" )
62
63 #define HEIGHT_TEXT N_( "Subscreen height" )
64
65 #define FOLLOW_MOUSE_TEXT N_( "Follow the mouse" )
66 #define FOLLOW_MOUSE_LONGTEXT N_( \
67     "Follow the mouse when capturing a subscreen." )
68 #endif
69
70 #ifdef SCREEN_MOUSE
71 #define MOUSE_TEXT N_( "Mouse pointer image" )
72 #define MOUSE_LONGTEXT N_( \
73     "If specified, will use the image to draw the mouse pointer on the " \
74     "capture." )
75 #endif
76
77 #ifdef SCREEN_DISPLAY_ID
78 #define DISPLAY_ID_TEXT N_( "Display ID" )
79 #define DISPLAY_ID_LONGTEXT N_( \
80     "Display ID. If not specified, main display ID is used. " )
81 #define INDEX_TEXT N_( "Screen index" )
82 #define INDEX_LONGTEXT N_( \
83     "Index of screen (1, 2, 3, ...). Alternative to Display ID." )
84 #endif
85
86 static int  Open ( vlc_object_t * );
87 static void Close( vlc_object_t * );
88
89 #ifdef _WIN32
90 #   define SCREEN_FPS 1
91 #else
92 #   define SCREEN_FPS 5
93 #endif
94
95 vlc_module_begin ()
96     set_description( N_("Screen Input") )
97     set_shortname( N_("Screen" ))
98     set_category( CAT_INPUT )
99     set_subcategory( SUBCAT_INPUT_ACCESS )
100
101     add_float( "screen-fps", SCREEN_FPS, FPS_TEXT, FPS_LONGTEXT, false )
102
103 #ifdef SCREEN_SUBSCREEN
104     add_integer( "screen-top", 0, TOP_TEXT, TOP_LONGTEXT, true )
105     add_integer( "screen-left", 0, LEFT_TEXT, LEFT_LONGTEXT, true )
106     add_integer( "screen-width", 0, WIDTH_TEXT, WIDTH_TEXT, true )
107     add_integer( "screen-height", 0, HEIGHT_TEXT, HEIGHT_TEXT, true )
108
109     add_bool( "screen-follow-mouse", false, FOLLOW_MOUSE_TEXT,
110               FOLLOW_MOUSE_LONGTEXT, false )
111 #endif
112
113 #ifdef SCREEN_MOUSE
114     add_loadfile( "screen-mouse-image", "", MOUSE_TEXT, MOUSE_LONGTEXT, true )
115 #endif
116
117 #ifdef _WIN32
118     add_integer( "screen-fragment-size", 0, FRAGS_TEXT, FRAGS_LONGTEXT, true )
119 #endif
120
121 #ifdef SCREEN_DISPLAY_ID
122     add_integer( "screen-display-id", 0, DISPLAY_ID_TEXT, DISPLAY_ID_LONGTEXT, true )
123     add_integer( "screen-index", 0, INDEX_TEXT, INDEX_LONGTEXT, true )
124 #endif
125
126     set_capability( "access_demux", 0 )
127     add_shortcut( "screen" )
128     set_callbacks( Open, Close )
129 vlc_module_end ()
130
131 /*****************************************************************************
132  * Local prototypes
133  *****************************************************************************/
134 static int Control( demux_t *, int, va_list );
135 static int Demux  ( demux_t * );
136
137 /*****************************************************************************
138  * DemuxOpen:
139  *****************************************************************************/
140 static int Open( vlc_object_t *p_this )
141 {
142     demux_t     *p_demux = (demux_t*)p_this;
143     demux_sys_t *p_sys;
144
145     /* Fill p_demux field */
146     p_demux->pf_demux = Demux;
147     p_demux->pf_control = Control;
148     p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
149     if( !p_sys )
150         return VLC_ENOMEM;
151
152     p_sys->f_fps = var_CreateGetFloat( p_demux, "screen-fps" );
153     p_sys->i_incr = 1000000 / p_sys->f_fps;;
154     p_sys->i_next_date = 0;
155
156 #ifdef SCREEN_SUBSCREEN
157     p_sys->i_top = var_CreateGetInteger( p_demux, "screen-top" );
158     p_sys->i_left = var_CreateGetInteger( p_demux, "screen-left" );
159     p_sys->i_width = var_CreateGetInteger( p_demux, "screen-width" );
160     p_sys->i_height = var_CreateGetInteger( p_demux, "screen-height" );
161     if( p_sys->i_width > 0 && p_sys->i_height > 0 )
162         msg_Dbg( p_demux, "capturing subscreen top: %d, left: %d, "
163                           "width: %d, height: %d",
164                           p_sys->i_top,
165                           p_sys->i_left,
166                           p_sys->i_width,
167                           p_sys->i_height );
168 #endif
169
170 #ifdef SCREEN_DISPLAY_ID
171     p_sys->i_display_id = var_CreateGetInteger( p_demux, "screen-display-id" );
172     p_sys->i_screen_index = var_CreateGetInteger( p_demux, "screen-index" );
173 #endif
174
175     if( screen_InitCapture( p_demux ) != VLC_SUCCESS )
176     {
177         free( p_sys );
178         return VLC_EGENERIC;
179     }
180
181     msg_Dbg( p_demux, "screen width: %i, height: %i, depth: %i",
182              p_sys->fmt.video.i_width, p_sys->fmt.video.i_height,
183              p_sys->fmt.video.i_bits_per_pixel );
184
185 #ifdef SCREEN_SUBSCREEN
186     if( p_sys->i_width > 0 && p_sys->i_height > 0 )
187     {
188         if( p_sys->i_left + p_sys->i_width > p_sys->fmt.video.i_width ||
189             p_sys->i_top + p_sys->i_height > p_sys->fmt.video.i_height )
190         {
191             msg_Err( p_demux, "subscreen region overflows the screen" );
192             free( p_sys );
193             return VLC_EGENERIC;
194         }
195         else
196         {
197             p_sys->i_screen_width = p_sys->fmt.video.i_width;
198             p_sys->i_screen_height = p_sys->fmt.video.i_height;
199             p_sys->fmt.video.i_visible_width =
200             p_sys->fmt.video.i_width = p_sys->i_width;
201             p_sys->fmt.video.i_visible_height =
202             p_sys->fmt.video.i_height = p_sys->i_height;
203             p_sys->b_follow_mouse = var_CreateGetBool( p_demux,
204                                                 "screen-follow-mouse" );
205             if( p_sys->b_follow_mouse )
206                 msg_Dbg( p_demux, "mouse following enabled" );
207         }
208     }
209 #endif
210
211 #ifdef SCREEN_MOUSE
212     char * psz_mouse = var_CreateGetNonEmptyString( p_demux,
213                                                     "screen-mouse-image" );
214     if( psz_mouse )
215     {
216         image_handler_t *p_image;
217         video_format_t fmt_in, fmt_out;
218         msg_Dbg( p_demux, "Using %s for the mouse pointer image", psz_mouse );
219         memset( &fmt_in, 0, sizeof( fmt_in ) );
220         memset( &fmt_out, 0, sizeof( fmt_out ) );
221         fmt_out.i_chroma = VLC_CODEC_RGBA;
222         p_image = image_HandlerCreate( p_demux );
223         if( p_image )
224         {
225             p_sys->p_mouse =
226                 image_ReadUrl( p_image, psz_mouse, &fmt_in, &fmt_out );
227             image_HandlerDelete( p_image );
228         }
229         if( !p_sys->p_mouse )
230             msg_Err( p_demux, "Failed to open mouse pointer image (%s)",
231                      psz_mouse );
232         free( psz_mouse );
233     }
234 #endif
235
236     p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
237
238     p_sys->i_start = mdate();
239
240     return VLC_SUCCESS;
241 }
242
243 /*****************************************************************************
244  * Close:
245  *****************************************************************************/
246 static void Close( vlc_object_t *p_this )
247 {
248     demux_t     *p_demux = (demux_t*)p_this;
249     demux_sys_t *p_sys = p_demux->p_sys;
250
251     screen_CloseCapture( p_demux );
252 #ifdef SCREEN_MOUSE
253     if( p_sys->p_mouse )
254         picture_Release( p_sys->p_mouse );
255 #endif
256     free( p_sys );
257 }
258
259 /*****************************************************************************
260  * Demux:
261  *****************************************************************************/
262 static int Demux( demux_t *p_demux )
263 {
264     demux_sys_t *p_sys = p_demux->p_sys;
265     block_t *p_block;
266
267     if( !p_sys->i_next_date ) p_sys->i_next_date = mdate();
268
269     /* Frame skipping if necessary */
270     while( mdate() >= p_sys->i_next_date + p_sys->i_incr )
271         p_sys->i_next_date += p_sys->i_incr;
272
273     mwait( p_sys->i_next_date );
274     p_block = screen_Capture( p_demux );
275     if( !p_block )
276     {
277         p_sys->i_next_date += p_sys->i_incr;
278         return 1;
279     }
280
281     p_block->i_dts = p_block->i_pts = p_sys->i_next_date;
282
283     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
284     es_out_Send( p_demux->out, p_sys->es, p_block );
285
286     p_sys->i_next_date += p_sys->i_incr;
287
288     return 1;
289 }
290
291 /*****************************************************************************
292  * Control:
293  *****************************************************************************/
294 static int Control( demux_t *p_demux, int i_query, va_list args )
295 {
296     bool *pb;
297     int64_t *pi64;
298     demux_sys_t *p_sys = p_demux->p_sys;
299
300     switch( i_query )
301     {
302         /* Special for access_demux */
303         case DEMUX_CAN_PAUSE:
304         case DEMUX_CAN_SEEK:
305         case DEMUX_CAN_CONTROL_PACE:
306             /* TODO */
307             pb = (bool*)va_arg( args, bool * );
308             *pb = false;
309             return VLC_SUCCESS;
310
311         case DEMUX_GET_PTS_DELAY:
312             pi64 = (int64_t*)va_arg( args, int64_t * );
313             *pi64 = INT64_C(1000)
314                   * var_InheritInteger( p_demux, "live-caching" );
315             return VLC_SUCCESS;
316
317         case DEMUX_GET_TIME:
318             pi64 = (int64_t*)va_arg( args, int64_t * );
319             *pi64 = mdate() - p_sys->i_start;
320             return VLC_SUCCESS;
321
322         /* TODO implement others */
323         default:
324             return VLC_EGENERIC;
325     }
326 }
327
328 #ifdef SCREEN_SUBSCREEN
329 void FollowMouse( demux_sys_t *p_sys, int i_x, int i_y )
330 {
331     i_x -= p_sys->i_width/2;
332     if( i_x < 0 ) i_x = 0;
333     p_sys->i_left = __MIN( (unsigned int)i_x,
334     p_sys->i_screen_width - p_sys->i_width );
335
336     i_y -= p_sys->i_height/2;
337     if( i_y < 0 ) i_y = 0;
338     p_sys->i_top = __MIN( (unsigned int)i_y,
339     p_sys->i_screen_height - p_sys->i_height );
340 }
341 #endif
342
343 #ifdef SCREEN_MOUSE
344 void RenderCursor( demux_t *p_demux, int i_x, int i_y,
345                    uint8_t *p_dst )
346 {
347     demux_sys_t *p_sys = p_demux->p_sys;
348     if( !p_sys->dst.i_planes )
349         picture_Setup( &p_sys->dst,
350                        p_sys->fmt.video.i_chroma,
351                        p_sys->fmt.video.i_width,
352                        p_sys->fmt.video.i_height,
353                        p_sys->fmt.video.i_sar_num,
354                        p_sys->fmt.video.i_sar_den );
355     if( !p_sys->p_blend )
356     {
357         p_sys->p_blend = vlc_object_create( p_demux, sizeof(filter_t) );
358         if( p_sys->p_blend )
359         {
360             es_format_Init( &p_sys->p_blend->fmt_in, VIDEO_ES,
361                             VLC_CODEC_RGBA );
362             p_sys->p_blend->fmt_in.video = p_sys->p_mouse->format;
363             p_sys->p_blend->fmt_out = p_sys->fmt;
364             p_sys->p_blend->p_module =
365                 module_need( p_sys->p_blend, "video blending", NULL, false );
366             if( !p_sys->p_blend->p_module )
367             {
368                 msg_Err( p_demux, "Could not load video blending module" );
369                 vlc_object_release( p_sys->p_blend );
370                 p_sys->p_blend = NULL;
371             }
372         }
373     }
374     if( p_sys->p_blend )
375     {
376         p_sys->dst.p->p_pixels = p_dst;
377         p_sys->p_blend->pf_video_blend( p_sys->p_blend,
378                                         &p_sys->dst,
379                                         p_sys->p_mouse,
380 #ifdef SCREEN_SUBSCREEN
381                                         i_x-p_sys->i_left,
382 #else
383                                         i_x,
384 #endif
385 #ifdef SCREEN_SUBSCREEN
386                                         i_y-p_sys->i_top,
387 #else
388                                         i_y,
389 #endif
390                                         255 );
391     }
392     else
393     {
394         picture_Release( p_sys->p_mouse );
395         p_sys->p_mouse = NULL;
396     }
397 }
398 #endif