]> git.sesse.net Git - vlc/blob - modules/access/screen/screen.c
c4f7422ab85f24fab6c1df32b4fd918bfd446cbd
[vlc] / modules / access / screen / screen.c
1 /*****************************************************************************
2  * screen.c: Screen capture module.
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include "screen.h"
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 #define CACHING_TEXT N_("Caching value in ms")
40 #define CACHING_LONGTEXT N_( \
41     "Caching value for screen capture. "\
42     "This value should be set in milliseconds." )
43 #define FPS_TEXT N_("Frame rate")
44 #define FPS_LONGTEXT N_( \
45     "Desired frame rate for the capture." )
46
47 #ifdef WIN32
48 #define FRAGS_TEXT N_("Capture fragment size")
49 #define FRAGS_LONGTEXT N_( \
50     "Optimize the capture by fragmenting the screen in chunks " \
51     "of predefined height (16 might be a good value, and 0 means disabled)." )
52 #endif
53
54 #ifdef SCREEN_SUBSCREEN
55 #define TOP_TEXT N_( "Subscreen top left corner" )
56 #define TOP_LONGTEXT N_( \
57     "Top coordinate of the subscreen top left corner." )
58
59 #define LEFT_TEXT N_( "Subscreen top left corner" )
60 #define LEFT_LONGTEXT N_( \
61     "Left coordinate of the subscreen top left corner." )
62
63 #define WIDTH_TEXT N_( "Subscreen width" )
64 #define WIDTH_LONGTEXT N_( \
65     "Subscreen width." )
66
67 #define HEIGHT_TEXT N_( "Subscreen height" )
68 #define HEIGHT_LONGTEXT N_( \
69     "Subscreen height."  )
70
71 #define FOLLOW_MOUSE_TEXT N_( "Follow the mouse" )
72 #define FOLLOW_MOUSE_LONGTEXT N_( \
73     "Follow the mouse when capturing a subscreen" )
74 #endif
75
76 static int  Open ( vlc_object_t * );
77 static void Close( vlc_object_t * );
78
79 #ifdef WIN32
80 #   define SCREEN_FPS 1
81 #else
82 #   define SCREEN_FPS 5
83 #endif
84
85 vlc_module_begin();
86     set_description( N_("Screen Input") );
87     set_shortname( N_("Screen" ));
88     set_category( CAT_INPUT );
89     set_subcategory( SUBCAT_INPUT_ACCESS );
90
91     add_integer( "screen-caching", DEFAULT_PTS_DELAY / 1000, NULL,
92         CACHING_TEXT, CACHING_LONGTEXT, true );
93     add_float( "screen-fps", SCREEN_FPS, 0, FPS_TEXT, FPS_LONGTEXT, true );
94
95 #ifdef SCREEN_SUBSCREEN
96     add_integer( "screen-top", 0, NULL, TOP_TEXT, TOP_LONGTEXT, true );
97     add_integer( "screen-left", 0, NULL, LEFT_TEXT, LEFT_LONGTEXT, true );
98     add_integer( "screen-width", 0, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, true );
99     add_integer( "screen-height", 0, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT, true );
100     add_bool( "screen-follow-mouse", false, NULL, FOLLOW_MOUSE_TEXT,
101               FOLLOW_MOUSE_LONGTEXT, true );
102 #endif
103
104 #ifdef WIN32
105     add_integer( "screen-fragment-size", 0, NULL, FRAGS_TEXT,
106         FRAGS_LONGTEXT, true );
107 #endif
108
109     set_capability( "access_demux", 0 );
110     add_shortcut( "screen" );
111     set_callbacks( Open, Close );
112 vlc_module_end();
113
114 /*****************************************************************************
115  * Local prototypes
116  *****************************************************************************/
117 static int Control( demux_t *, int, va_list );
118 static int Demux  ( demux_t * );
119
120 /*****************************************************************************
121  * DemuxOpen:
122  *****************************************************************************/
123 static int Open( vlc_object_t *p_this )
124 {
125     demux_t     *p_demux = (demux_t*)p_this;
126     demux_sys_t *p_sys;
127     vlc_value_t val;
128
129     /* Fill p_demux field */
130     p_demux->pf_demux = Demux;
131     p_demux->pf_control = Control;
132     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
133     memset( p_sys, 0, sizeof( demux_sys_t ) );
134
135     /* Update default_pts to a suitable value for screen access */
136     var_Create( p_demux, "screen-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
137
138     var_Create( p_demux, "screen-fps", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT );
139     var_Get( p_demux, "screen-fps", &val );
140     p_sys->f_fps = val.f_float;
141     p_sys->i_incr = 1000000 / val.f_float;
142     p_sys->i_next_date = 0;
143
144 #ifdef SCREEN_SUBSCREEN
145     p_sys->i_top = var_CreateGetInteger( p_demux, "screen-top" );
146     p_sys->i_left = var_CreateGetInteger( p_demux, "screen-left" );
147     p_sys->i_width = var_CreateGetInteger( p_demux, "screen-width" );
148     p_sys->i_height = var_CreateGetInteger( p_demux, "screen-height" );
149     if( p_sys->i_width > 0 && p_sys->i_height > 0 )
150         msg_Dbg( p_demux, "capturing subscreen top: %d, left: %d, "
151                           "width: %d, height: %d",
152                           p_sys->i_top,
153                           p_sys->i_left,
154                           p_sys->i_width,
155                           p_sys->i_height );
156 #endif
157
158     if( screen_InitCapture( p_demux ) != VLC_SUCCESS )
159     {
160         free( p_sys );
161         return VLC_EGENERIC;
162     }
163
164     msg_Dbg( p_demux, "screen width: %i, height: %i, depth: %i",
165              p_sys->fmt.video.i_width, p_sys->fmt.video.i_height,
166              p_sys->fmt.video.i_bits_per_pixel );
167
168 #ifdef SCREEN_SUBSCREEN
169     if( p_sys->i_width > 0 && p_sys->i_height > 0 )
170     {
171         if( p_sys->i_left + p_sys->i_width > p_sys->fmt.video.i_width ||
172             p_sys->i_top + p_sys->i_height > p_sys->fmt.video.i_height )
173         {
174             msg_Err( p_demux, "subscreen region overflows the screen" );
175             free( p_sys );
176             return VLC_EGENERIC;
177         }
178         else
179         {
180             p_sys->i_screen_width = p_sys->fmt.video.i_width;
181             p_sys->i_screen_height = p_sys->fmt.video.i_height;
182             p_sys->fmt.video.i_width = p_sys->i_width;
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     p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
193
194     return VLC_SUCCESS;
195 }
196
197 /*****************************************************************************
198  * Close:
199  *****************************************************************************/
200 static void Close( vlc_object_t *p_this )
201 {
202     demux_t     *p_demux = (demux_t*)p_this;
203     demux_sys_t *p_sys = p_demux->p_sys;
204
205     screen_CloseCapture( p_demux );
206     free( p_sys );
207 }
208
209 /*****************************************************************************
210  * Demux:
211  *****************************************************************************/
212 static int Demux( demux_t *p_demux )
213 {
214     demux_sys_t *p_sys = p_demux->p_sys;
215     block_t *p_block;
216
217     if( !p_sys->i_next_date ) p_sys->i_next_date = mdate();
218
219     /* Frame skipping if necessary */
220     while( mdate() >= p_sys->i_next_date + p_sys->i_incr )
221         p_sys->i_next_date += p_sys->i_incr;
222
223     mwait( p_sys->i_next_date );
224     p_block = screen_Capture( p_demux );
225     if( !p_block )
226     {
227         p_sys->i_next_date += p_sys->i_incr;
228         return 1;
229     }
230
231     p_block->i_dts = p_block->i_pts = p_sys->i_next_date;
232
233     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
234     es_out_Send( p_demux->out, p_sys->es, p_block );
235
236     p_sys->i_next_date += p_sys->i_incr;
237
238     return 1;
239 }
240
241 /*****************************************************************************
242  * Control:
243  *****************************************************************************/
244 static int Control( demux_t *p_demux, int i_query, va_list args )
245 {
246     bool *pb;
247     int64_t *pi64;
248
249     switch( i_query )
250     {
251         /* Special for access_demux */
252         case DEMUX_CAN_PAUSE:
253         case DEMUX_CAN_SEEK:
254         case DEMUX_CAN_CONTROL_PACE:
255             /* TODO */
256             pb = (bool*)va_arg( args, bool * );
257             *pb = false;
258             return VLC_SUCCESS;
259
260         case DEMUX_GET_PTS_DELAY:
261             pi64 = (int64_t*)va_arg( args, int64_t * );
262             *pi64 = (int64_t)var_GetInteger( p_demux, "screen-caching" ) *1000;
263             return VLC_SUCCESS;
264
265         /* TODO implement others */
266         default:
267             return VLC_EGENERIC;
268     }
269 }