]> git.sesse.net Git - vlc/blob - modules/access/screen/screen.c
Remove most stray semi-colons in module descriptions
[vlc] / modules / access / screen / screen.c
1 /*****************************************************************************
2  * screen.c: Screen capture module.
3  *****************************************************************************
4  * Copyright (C) 2004-2008 the VideoLAN team
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
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 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 General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, 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 "screen.h"
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 #define CACHING_TEXT N_("Caching value in ms")
41 #define CACHING_LONGTEXT N_( \
42     "Caching value for screen capture. "\
43     "This value should be set in milliseconds." )
44 #define FPS_TEXT N_("Frame rate")
45 #define FPS_LONGTEXT N_( \
46     "Desired frame rate for the capture." )
47
48 #ifdef WIN32
49 #define FRAGS_TEXT N_("Capture fragment size")
50 #define FRAGS_LONGTEXT N_( \
51     "Optimize the capture by fragmenting the screen in chunks " \
52     "of predefined height (16 might be a good value, and 0 means disabled)." )
53 #endif
54
55 #ifdef SCREEN_SUBSCREEN
56 #define TOP_TEXT N_( "Subscreen top left corner" )
57 #define TOP_LONGTEXT N_( \
58     "Top coordinate of the subscreen top left corner." )
59
60 #define LEFT_TEXT N_( "Subscreen top left corner" )
61 #define LEFT_LONGTEXT N_( \
62     "Left coordinate of the subscreen top left corner." )
63
64 #define WIDTH_TEXT N_( "Subscreen width" )
65 #define WIDTH_LONGTEXT N_( \
66     "Subscreen width" )
67
68 #define HEIGHT_TEXT N_( "Subscreen height" )
69 #define HEIGHT_LONGTEXT N_( \
70     "Subscreen height"  )
71
72 #define FOLLOW_MOUSE_TEXT N_( "Follow the mouse" )
73 #define FOLLOW_MOUSE_LONGTEXT N_( \
74     "Follow the mouse when capturing a subscreen." )
75 #endif
76
77 #ifdef SCREEN_MOUSE
78 #define MOUSE_TEXT N_( "Mouse pointer image" )
79 #define MOUSE_LONGTEXT N_( \
80     "If specifed, will use the image to draw the mouse pointer on the " \
81     "capture." )
82 #endif
83
84 static int  Open ( vlc_object_t * );
85 static void Close( vlc_object_t * );
86
87 #ifdef WIN32
88 #   define SCREEN_FPS 1
89 #else
90 #   define SCREEN_FPS 5
91 #endif
92
93 vlc_module_begin ()
94     set_description( N_("Screen Input") )
95     set_shortname( N_("Screen" ))
96     set_category( CAT_INPUT )
97     set_subcategory( SUBCAT_INPUT_ACCESS )
98
99     add_integer( "screen-caching", DEFAULT_PTS_DELAY / 1000, NULL,
100         CACHING_TEXT, CACHING_LONGTEXT, true );
101     add_float( "screen-fps", SCREEN_FPS, 0, FPS_TEXT, FPS_LONGTEXT, true )
102
103 #ifdef SCREEN_SUBSCREEN
104     add_integer( "screen-top", 0, NULL, TOP_TEXT, TOP_LONGTEXT, true )
105     add_integer( "screen-left", 0, NULL, LEFT_TEXT, LEFT_LONGTEXT, true )
106     add_integer( "screen-width", 0, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, true )
107     add_integer( "screen-height", 0, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT, true )
108     add_bool( "screen-follow-mouse", false, NULL, FOLLOW_MOUSE_TEXT,
109               FOLLOW_MOUSE_LONGTEXT, true );
110 #endif
111
112 #ifdef SCREEN_MOUSE
113     add_file( "screen-mouse-image", "", NULL, MOUSE_TEXT, MOUSE_LONGTEXT,
114               true );
115 #endif
116
117 #ifdef WIN32
118     add_integer( "screen-fragment-size", 0, NULL, FRAGS_TEXT,
119         FRAGS_LONGTEXT, true );
120 #endif
121
122     set_capability( "access_demux", 0 )
123     add_shortcut( "screen" )
124     set_callbacks( Open, Close )
125 vlc_module_end ()
126
127 /*****************************************************************************
128  * Local prototypes
129  *****************************************************************************/
130 static int Control( demux_t *, int, va_list );
131 static int Demux  ( demux_t * );
132
133 /*****************************************************************************
134  * DemuxOpen:
135  *****************************************************************************/
136 static int Open( vlc_object_t *p_this )
137 {
138     demux_t     *p_demux = (demux_t*)p_this;
139     demux_sys_t *p_sys;
140     vlc_value_t val;
141
142     /* Fill p_demux field */
143     p_demux->pf_demux = Demux;
144     p_demux->pf_control = Control;
145     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
146     memset( p_sys, 0, sizeof( demux_sys_t ) );
147
148     /* Update default_pts to a suitable value for screen access */
149     var_Create( p_demux, "screen-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
150
151     var_Create( p_demux, "screen-fps", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT );
152     var_Get( p_demux, "screen-fps", &val );
153     p_sys->f_fps = val.f_float;
154     p_sys->i_incr = 1000000 / val.f_float;
155     p_sys->i_next_date = 0;
156
157 #ifdef SCREEN_SUBSCREEN
158     p_sys->i_top = var_CreateGetInteger( p_demux, "screen-top" );
159     p_sys->i_left = var_CreateGetInteger( p_demux, "screen-left" );
160     p_sys->i_width = var_CreateGetInteger( p_demux, "screen-width" );
161     p_sys->i_height = var_CreateGetInteger( p_demux, "screen-height" );
162     if( p_sys->i_width > 0 && p_sys->i_height > 0 )
163         msg_Dbg( p_demux, "capturing subscreen top: %d, left: %d, "
164                           "width: %d, height: %d",
165                           p_sys->i_top,
166                           p_sys->i_left,
167                           p_sys->i_width,
168                           p_sys->i_height );
169 #endif
170
171     if( screen_InitCapture( p_demux ) != VLC_SUCCESS )
172     {
173         free( p_sys );
174         return VLC_EGENERIC;
175     }
176
177     msg_Dbg( p_demux, "screen width: %i, height: %i, depth: %i",
178              p_sys->fmt.video.i_width, p_sys->fmt.video.i_height,
179              p_sys->fmt.video.i_bits_per_pixel );
180
181 #ifdef SCREEN_SUBSCREEN
182     if( p_sys->i_width > 0 && p_sys->i_height > 0 )
183     {
184         if( p_sys->i_left + p_sys->i_width > p_sys->fmt.video.i_width ||
185             p_sys->i_top + p_sys->i_height > p_sys->fmt.video.i_height )
186         {
187             msg_Err( p_demux, "subscreen region overflows the screen" );
188             free( p_sys );
189             return VLC_EGENERIC;
190         }
191         else
192         {
193             p_sys->i_screen_width = p_sys->fmt.video.i_width;
194             p_sys->i_screen_height = p_sys->fmt.video.i_height;
195             p_sys->fmt.video.i_visible_width =
196             p_sys->fmt.video.i_width = p_sys->i_width;
197             p_sys->fmt.video.i_visible_height =
198             p_sys->fmt.video.i_height = p_sys->i_height;
199             p_sys->b_follow_mouse = var_CreateGetInteger( p_demux,
200                                                 "screen-follow-mouse" );
201             if( p_sys->b_follow_mouse )
202                 msg_Dbg( p_demux, "mouse following enabled" );
203         }
204     }
205 #endif
206
207 #ifdef SCREEN_MOUSE
208     char * psz_mouse = var_CreateGetNonEmptyString( p_demux,
209                                                     "screen-mouse-image" );
210     if( psz_mouse )
211     {
212         image_handler_t *p_image;
213         video_format_t fmt_in, fmt_out;
214         msg_Dbg( p_demux, "Using %s for the mouse pointer image", psz_mouse );
215         memset( &fmt_in, 0, sizeof( fmt_in ) );
216         memset( &fmt_out, 0, sizeof( fmt_out ) );
217         fmt_out.i_chroma = VLC_FOURCC('R','G','B','A');
218         p_image = image_HandlerCreate( p_demux );
219         if( p_image )
220         {
221             p_sys->p_mouse =
222                 image_ReadUrl( p_image, psz_mouse, &fmt_in, &fmt_out );
223             image_HandlerDelete( p_image );
224         }
225         if( !p_sys->p_mouse )
226             msg_Err( p_demux, "Failed to open mouse pointer image (%s)",
227                      psz_mouse );
228         free( psz_mouse );
229     }
230 #endif
231
232     p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
233
234     return VLC_SUCCESS;
235 }
236
237 /*****************************************************************************
238  * Close:
239  *****************************************************************************/
240 static void Close( vlc_object_t *p_this )
241 {
242     demux_t     *p_demux = (demux_t*)p_this;
243     demux_sys_t *p_sys = p_demux->p_sys;
244
245     screen_CloseCapture( p_demux );
246 #ifdef SCREEN_MOUSE
247     if( p_sys->p_mouse )
248         picture_Release( p_sys->p_mouse );
249 #endif
250     free( p_sys );
251 }
252
253 /*****************************************************************************
254  * Demux:
255  *****************************************************************************/
256 static int Demux( demux_t *p_demux )
257 {
258     demux_sys_t *p_sys = p_demux->p_sys;
259     block_t *p_block;
260
261     if( !p_sys->i_next_date ) p_sys->i_next_date = mdate();
262
263     /* Frame skipping if necessary */
264     while( mdate() >= p_sys->i_next_date + p_sys->i_incr )
265         p_sys->i_next_date += p_sys->i_incr;
266
267     mwait( p_sys->i_next_date );
268     p_block = screen_Capture( p_demux );
269     if( !p_block )
270     {
271         p_sys->i_next_date += p_sys->i_incr;
272         return 1;
273     }
274
275     p_block->i_dts = p_block->i_pts = p_sys->i_next_date;
276
277     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
278     es_out_Send( p_demux->out, p_sys->es, p_block );
279
280     p_sys->i_next_date += p_sys->i_incr;
281
282     return 1;
283 }
284
285 /*****************************************************************************
286  * Control:
287  *****************************************************************************/
288 static int Control( demux_t *p_demux, int i_query, va_list args )
289 {
290     bool *pb;
291     int64_t *pi64;
292
293     switch( i_query )
294     {
295         /* Special for access_demux */
296         case DEMUX_CAN_PAUSE:
297         case DEMUX_CAN_SEEK:
298         case DEMUX_CAN_CONTROL_PACE:
299             /* TODO */
300             pb = (bool*)va_arg( args, bool * );
301             *pb = false;
302             return VLC_SUCCESS;
303
304         case DEMUX_GET_PTS_DELAY:
305             pi64 = (int64_t*)va_arg( args, int64_t * );
306             *pi64 = (int64_t)var_GetInteger( p_demux, "screen-caching" ) *1000;
307             return VLC_SUCCESS;
308
309         /* TODO implement others */
310         default:
311             return VLC_EGENERIC;
312     }
313 }
314
315 #ifdef SCREEN_SUBSCREEN
316 void FollowMouse( demux_sys_t *p_sys, int i_x, int i_y )
317 {
318     i_x -= p_sys->i_width/2;
319     if( i_x < 0 ) i_x = 0;
320     p_sys->i_left = __MIN( (unsigned int)i_x,
321     p_sys->i_screen_width - p_sys->i_width );
322
323     i_y -= p_sys->i_height/2;
324     if( i_y < 0 ) i_y = 0;
325     p_sys->i_top = __MIN( (unsigned int)i_y,
326     p_sys->i_screen_height - p_sys->i_height );
327 }
328 #endif
329
330 #ifdef SCREEN_MOUSE
331 void RenderCursor( demux_t *p_demux, int i_x, int i_y,
332                    uint8_t *p_dst )
333 {
334     demux_sys_t *p_sys = p_demux->p_sys;
335     if( !p_sys->dst.i_planes )
336         vout_InitPicture( p_demux, &p_sys->dst,
337                           p_sys->fmt.video.i_chroma,
338                           p_sys->fmt.video.i_width,
339                           p_sys->fmt.video.i_height,
340                           p_sys->fmt.video.i_aspect );
341     if( !p_sys->p_blend )
342     {
343         p_sys->p_blend = vlc_object_create( p_demux, sizeof(filter_t) );
344         if( p_sys->p_blend )
345         {
346             es_format_Init( &p_sys->p_blend->fmt_in, VIDEO_ES,
347                             VLC_FOURCC('R','G','B','A') );
348             p_sys->p_blend->fmt_in.video = p_sys->p_mouse->format;
349             p_sys->p_blend->fmt_out = p_sys->fmt;
350             p_sys->p_blend->p_module =
351                 module_need( p_sys->p_blend, "video blending", 0, 0 );
352             if( !p_sys->p_blend->p_module )
353             {
354                 msg_Err( p_demux, "Could not load video blending module" );
355                 vlc_object_detach( p_sys->p_blend );
356                 vlc_object_release( p_sys->p_blend );
357                 p_sys->p_blend = NULL;
358             }
359         }
360     }
361     if( p_sys->p_blend )
362     {
363         p_sys->dst.p->p_pixels = p_dst;
364         p_sys->p_blend->pf_video_blend( p_sys->p_blend,
365                                         &p_sys->dst,
366                                         p_sys->p_mouse,
367 #ifdef SCREEN_SUBSCREEN
368                                         i_x-p_sys->i_left,
369 #else
370                                         i_x,
371 #endif
372 #ifdef SCREEN_SUBSCREEN
373                                         i_y-p_sys->i_top,
374 #else
375                                         i_y,
376 #endif
377                                         255 );
378     }
379     else
380     {
381         picture_Release( p_sys->p_mouse );
382         p_sys->p_mouse = NULL;
383     }
384 }
385 #endif