]> git.sesse.net Git - vlc/blob - modules/access/screen/screen.c
Unify (ACCESS|DEMUX)_GET_PTS_DELAY
[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 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 static int  Open ( vlc_object_t * );
78 static void Close( vlc_object_t * );
79
80 #ifdef WIN32
81 #   define SCREEN_FPS 1
82 #else
83 #   define SCREEN_FPS 5
84 #endif
85
86 vlc_module_begin ()
87     set_description( N_("Screen Input") )
88     set_shortname( N_("Screen" ))
89     set_category( CAT_INPUT )
90     set_subcategory( SUBCAT_INPUT_ACCESS )
91
92     add_float( "screen-fps", SCREEN_FPS, FPS_TEXT, FPS_LONGTEXT, false )
93
94 #ifdef SCREEN_SUBSCREEN
95     add_integer( "screen-top", 0, TOP_TEXT, TOP_LONGTEXT, true )
96     add_integer( "screen-left", 0, LEFT_TEXT, LEFT_LONGTEXT, true )
97     add_integer( "screen-width", 0, WIDTH_TEXT, WIDTH_TEXT, true )
98     add_integer( "screen-height", 0, HEIGHT_TEXT, HEIGHT_TEXT, true )
99
100     add_bool( "screen-follow-mouse", false, FOLLOW_MOUSE_TEXT,
101               FOLLOW_MOUSE_LONGTEXT, false )
102 #endif
103
104 #ifdef SCREEN_MOUSE
105     add_loadfile( "screen-mouse-image", "", MOUSE_TEXT, MOUSE_LONGTEXT, true )
106 #endif
107
108 #ifdef WIN32
109     add_integer( "screen-fragment-size", 0, FRAGS_TEXT, FRAGS_LONGTEXT, true )
110 #endif
111
112     set_capability( "access_demux", 0 )
113     add_shortcut( "screen" )
114     set_callbacks( Open, Close )
115 vlc_module_end ()
116
117 /*****************************************************************************
118  * Local prototypes
119  *****************************************************************************/
120 static int Control( demux_t *, int, va_list );
121 static int Demux  ( demux_t * );
122
123 /*****************************************************************************
124  * DemuxOpen:
125  *****************************************************************************/
126 static int Open( vlc_object_t *p_this )
127 {
128     demux_t     *p_demux = (demux_t*)p_this;
129     demux_sys_t *p_sys;
130
131     /* Fill p_demux field */
132     p_demux->pf_demux = Demux;
133     p_demux->pf_control = Control;
134     p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
135     if( !p_sys )
136         return VLC_ENOMEM;
137
138     p_sys->f_fps = var_CreateGetFloat( p_demux, "screen-fps" );
139     p_sys->i_incr = 1000000 / p_sys->f_fps;;
140     p_sys->i_next_date = 0;
141
142 #ifdef SCREEN_SUBSCREEN
143     p_sys->i_top = var_CreateGetInteger( p_demux, "screen-top" );
144     p_sys->i_left = var_CreateGetInteger( p_demux, "screen-left" );
145     p_sys->i_width = var_CreateGetInteger( p_demux, "screen-width" );
146     p_sys->i_height = var_CreateGetInteger( p_demux, "screen-height" );
147     if( p_sys->i_width > 0 && p_sys->i_height > 0 )
148         msg_Dbg( p_demux, "capturing subscreen top: %d, left: %d, "
149                           "width: %d, height: %d",
150                           p_sys->i_top,
151                           p_sys->i_left,
152                           p_sys->i_width,
153                           p_sys->i_height );
154 #endif
155
156     if( screen_InitCapture( p_demux ) != VLC_SUCCESS )
157     {
158         free( p_sys );
159         return VLC_EGENERIC;
160     }
161
162     msg_Dbg( p_demux, "screen width: %i, height: %i, depth: %i",
163              p_sys->fmt.video.i_width, p_sys->fmt.video.i_height,
164              p_sys->fmt.video.i_bits_per_pixel );
165
166 #ifdef SCREEN_SUBSCREEN
167     if( p_sys->i_width > 0 && p_sys->i_height > 0 )
168     {
169         if( p_sys->i_left + p_sys->i_width > p_sys->fmt.video.i_width ||
170             p_sys->i_top + p_sys->i_height > p_sys->fmt.video.i_height )
171         {
172             msg_Err( p_demux, "subscreen region overflows the screen" );
173             free( p_sys );
174             return VLC_EGENERIC;
175         }
176         else
177         {
178             p_sys->i_screen_width = p_sys->fmt.video.i_width;
179             p_sys->i_screen_height = p_sys->fmt.video.i_height;
180             p_sys->fmt.video.i_visible_width =
181             p_sys->fmt.video.i_width = p_sys->i_width;
182             p_sys->fmt.video.i_visible_height =
183             p_sys->fmt.video.i_height = p_sys->i_height;
184             p_sys->b_follow_mouse = var_CreateGetInteger( p_demux,
185                                                 "screen-follow-mouse" );
186             if( p_sys->b_follow_mouse )
187                 msg_Dbg( p_demux, "mouse following enabled" );
188         }
189     }
190 #endif
191
192 #ifdef SCREEN_MOUSE
193     char * psz_mouse = var_CreateGetNonEmptyString( p_demux,
194                                                     "screen-mouse-image" );
195     if( psz_mouse )
196     {
197         image_handler_t *p_image;
198         video_format_t fmt_in, fmt_out;
199         msg_Dbg( p_demux, "Using %s for the mouse pointer image", psz_mouse );
200         memset( &fmt_in, 0, sizeof( fmt_in ) );
201         memset( &fmt_out, 0, sizeof( fmt_out ) );
202         fmt_out.i_chroma = VLC_CODEC_RGBA;
203         p_image = image_HandlerCreate( p_demux );
204         if( p_image )
205         {
206             p_sys->p_mouse =
207                 image_ReadUrl( p_image, psz_mouse, &fmt_in, &fmt_out );
208             image_HandlerDelete( p_image );
209         }
210         if( !p_sys->p_mouse )
211             msg_Err( p_demux, "Failed to open mouse pointer image (%s)",
212                      psz_mouse );
213         free( psz_mouse );
214     }
215 #endif
216
217     p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
218
219     return VLC_SUCCESS;
220 }
221
222 /*****************************************************************************
223  * Close:
224  *****************************************************************************/
225 static void Close( vlc_object_t *p_this )
226 {
227     demux_t     *p_demux = (demux_t*)p_this;
228     demux_sys_t *p_sys = p_demux->p_sys;
229
230     screen_CloseCapture( p_demux );
231 #ifdef SCREEN_MOUSE
232     if( p_sys->p_mouse )
233         picture_Release( p_sys->p_mouse );
234 #endif
235     free( p_sys );
236 }
237
238 /*****************************************************************************
239  * Demux:
240  *****************************************************************************/
241 static int Demux( demux_t *p_demux )
242 {
243     demux_sys_t *p_sys = p_demux->p_sys;
244     block_t *p_block;
245
246     if( !p_sys->i_next_date ) p_sys->i_next_date = mdate();
247
248     /* Frame skipping if necessary */
249     while( mdate() >= p_sys->i_next_date + p_sys->i_incr )
250         p_sys->i_next_date += p_sys->i_incr;
251
252     mwait( p_sys->i_next_date );
253     p_block = screen_Capture( p_demux );
254     if( !p_block )
255     {
256         p_sys->i_next_date += p_sys->i_incr;
257         return 1;
258     }
259
260     p_block->i_dts = p_block->i_pts = p_sys->i_next_date;
261
262     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
263     es_out_Send( p_demux->out, p_sys->es, p_block );
264
265     p_sys->i_next_date += p_sys->i_incr;
266
267     return 1;
268 }
269
270 /*****************************************************************************
271  * Control:
272  *****************************************************************************/
273 static int Control( demux_t *p_demux, int i_query, va_list args )
274 {
275     bool *pb;
276     int64_t *pi64;
277
278     switch( i_query )
279     {
280         /* Special for access_demux */
281         case DEMUX_CAN_PAUSE:
282         case DEMUX_CAN_SEEK:
283         case DEMUX_CAN_CONTROL_PACE:
284             /* TODO */
285             pb = (bool*)va_arg( args, bool * );
286             *pb = false;
287             return VLC_SUCCESS;
288
289         case DEMUX_GET_PTS_DELAY:
290             pi64 = (int64_t*)va_arg( args, int64_t * );
291             *pi64 = INT64_C(1000)
292                   * var_InheritInteger( p_demux, "live-caching" );
293             return VLC_SUCCESS;
294
295         case DEMUX_GET_TIME:
296             pi64 = (int64_t*)va_arg( args, int64_t * );
297             *pi64 = mdate();
298             return VLC_SUCCESS;
299
300         /* TODO implement others */
301         default:
302             return VLC_EGENERIC;
303     }
304 }
305
306 #ifdef SCREEN_SUBSCREEN
307 void FollowMouse( demux_sys_t *p_sys, int i_x, int i_y )
308 {
309     i_x -= p_sys->i_width/2;
310     if( i_x < 0 ) i_x = 0;
311     p_sys->i_left = __MIN( (unsigned int)i_x,
312     p_sys->i_screen_width - p_sys->i_width );
313
314     i_y -= p_sys->i_height/2;
315     if( i_y < 0 ) i_y = 0;
316     p_sys->i_top = __MIN( (unsigned int)i_y,
317     p_sys->i_screen_height - p_sys->i_height );
318 }
319 #endif
320
321 #ifdef SCREEN_MOUSE
322 void RenderCursor( demux_t *p_demux, int i_x, int i_y,
323                    uint8_t *p_dst )
324 {
325     demux_sys_t *p_sys = p_demux->p_sys;
326     if( !p_sys->dst.i_planes )
327         picture_Setup( &p_sys->dst,
328                        p_sys->fmt.video.i_chroma,
329                        p_sys->fmt.video.i_width,
330                        p_sys->fmt.video.i_height,
331                        p_sys->fmt.video.i_sar_num,
332                        p_sys->fmt.video.i_sar_den );
333     if( !p_sys->p_blend )
334     {
335         p_sys->p_blend = vlc_object_create( p_demux, sizeof(filter_t) );
336         if( p_sys->p_blend )
337         {
338             es_format_Init( &p_sys->p_blend->fmt_in, VIDEO_ES,
339                             VLC_CODEC_RGBA );
340             p_sys->p_blend->fmt_in.video = p_sys->p_mouse->format;
341             p_sys->p_blend->fmt_out = p_sys->fmt;
342             p_sys->p_blend->p_module =
343                 module_need( p_sys->p_blend, "video blending", NULL, false );
344             if( !p_sys->p_blend->p_module )
345             {
346                 msg_Err( p_demux, "Could not load video blending module" );
347                 vlc_object_release( p_sys->p_blend );
348                 p_sys->p_blend = NULL;
349             }
350         }
351     }
352     if( p_sys->p_blend )
353     {
354         p_sys->dst.p->p_pixels = p_dst;
355         p_sys->p_blend->pf_video_blend( p_sys->p_blend,
356                                         &p_sys->dst,
357                                         p_sys->p_mouse,
358 #ifdef SCREEN_SUBSCREEN
359                                         i_x-p_sys->i_left,
360 #else
361                                         i_x,
362 #endif
363 #ifdef SCREEN_SUBSCREEN
364                                         i_y-p_sys->i_top,
365 #else
366                                         i_y,
367 #endif
368                                         255 );
369     }
370     else
371     {
372         picture_Release( p_sys->p_mouse );
373         p_sys->p_mouse = NULL;
374     }
375 }
376 #endif