1 /*****************************************************************************
2 * screen.c: Screen capture module.
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc/input.h>
33 # include <X11/Xlib.h>
34 # include <X11/Xutil.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 #define CACHING_TEXT N_("Caching value in ms")
41 #define CACHING_LONGTEXT N_( \
42 "Allows you to modify the default caching value for screen capture " \
43 "streams. This value should be set in millisecond units." )
44 #define FPS_TEXT N_("Frame rate")
45 #define FPS_LONGTEXT N_( \
46 "Allows you to set the desired frame rate for the capture." )
48 static int Open ( vlc_object_t * );
49 static void Close( vlc_object_t * );
52 set_description( _("Screen Input") );
54 add_integer( "screen-caching", DEFAULT_PTS_DELAY / 1000, NULL,
55 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
56 add_float( "screen-fps", 5, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
58 set_capability( "access_demux", 0 );
59 add_shortcut( "screen" );
60 set_callbacks( Open, Close );
63 /*****************************************************************************
65 *****************************************************************************/
80 static int Control( demux_t *, int, va_list );
81 static int Demux ( demux_t * );
83 static int InitCapture ( demux_t * );
84 static int CloseCapture( demux_t * );
85 static block_t *Capture( demux_t * );
87 /*****************************************************************************
89 *****************************************************************************/
90 static int Open( vlc_object_t *p_this )
92 demux_t *p_demux = (demux_t*)p_this;
96 /* Fill p_demux field */
97 p_demux->pf_demux = Demux;
98 p_demux->pf_control = Control;
99 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
100 memset( p_sys, 0, sizeof( demux_sys_t ) );
103 if( InitCapture( p_demux ) != VLC_SUCCESS )
109 p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
111 /* Update default_pts to a suitable value for screen access */
112 var_Create( p_demux, "screen-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
114 var_Create( p_demux, "screen-fps", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT );
115 var_Get( p_demux, "screen-fps", &val );
116 p_sys->f_fps = val.f_float;
117 p_sys->i_incr = 1000000 / val.f_float;
118 p_sys->i_next_date = 0;
123 /*****************************************************************************
125 *****************************************************************************/
126 static void Close( vlc_object_t *p_this )
128 demux_t *p_demux = (demux_t*)p_this;
129 demux_sys_t *p_sys = p_demux->p_sys;
131 CloseCapture( p_demux );
135 /*****************************************************************************
137 *****************************************************************************/
138 static int Demux( demux_t *p_demux )
140 demux_sys_t *p_sys = p_demux->p_sys;
143 if( !p_sys->i_next_date ) p_sys->i_next_date = mdate();
145 /* Frame skipping if necessary */
146 while( mdate() >= p_sys->i_next_date + p_sys->i_incr )
147 p_sys->i_next_date += p_sys->i_incr;
149 mwait( p_sys->i_next_date );
150 p_block = Capture( p_demux );
151 if( !p_block ) return 0;
153 p_block->i_dts = p_block->i_pts = p_sys->i_next_date;
155 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
156 es_out_Send( p_demux->out, p_sys->es, p_block );
158 p_sys->i_next_date += p_sys->i_incr;
163 /*****************************************************************************
164 * Platform dependant capture functions
165 *****************************************************************************/
167 static int InitCapture( demux_t *p_demux )
173 static int InitCapture( demux_t *p_demux )
175 demux_sys_t *p_sys = p_demux->p_sys;
176 XWindowAttributes win_info;
179 /* Open the display */
180 p_sys->p_display = XOpenDisplay( NULL );
181 if( !p_sys->p_display )
183 msg_Err( p_demux, "cannot open display" );
187 /* Get the parameters of the root window */
188 if( !XGetWindowAttributes( p_sys->p_display,
189 DefaultRootWindow( p_sys->p_display ),
192 msg_Err( p_demux, "can't get root window attributes" );
193 XCloseDisplay( p_sys->p_display );
197 switch( win_info.depth )
199 case 8: /* FIXME: set the palette */
200 i_chroma = VLC_FOURCC('R','G','B','2'); break;
202 i_chroma = VLC_FOURCC('R','V','1','5'); break;
204 i_chroma = VLC_FOURCC('R','V','1','6'); break;
206 i_chroma = VLC_FOURCC('R','V','2','4'); break;
208 i_chroma = VLC_FOURCC('R','V','3','2'); break;
210 msg_Err( p_demux, "unknown screen depth %i", win_info.depth );
211 XCloseDisplay( p_sys->p_display );
215 es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
216 p_sys->fmt.video.i_width = win_info.width;
217 p_sys->fmt.video.i_height = win_info.height;
220 win_info.visual->red_mask;
221 win_info.visual->green_mask;
222 win_info.visual->blue_mask;
223 win_info.visual->bits_per_rgb;
231 static int CloseCapture( demux_t *p_demux )
236 static int CloseCapture( demux_t *p_demux )
238 demux_sys_t *p_sys = p_demux->p_sys;
240 XCloseDisplay( p_sys->p_display );
246 static block_t *Capture( demux_t *p_demux )
251 static block_t *Capture( demux_t *p_demux )
253 demux_sys_t *p_sys = p_demux->p_sys;
258 image = XGetImage( p_sys->p_display, DefaultRootWindow( p_sys->p_display ),
259 0, 0, p_sys->fmt.video.i_width,
260 p_sys->fmt.video.i_height, AllPlanes, ZPixmap );
264 msg_Warn( p_demux, "cannot get image" );
268 i_size = image->bytes_per_line * image->height;
270 if( !( p_block = block_New( p_demux, i_size ) ) )
272 msg_Warn( p_demux, "cannot get block" );
273 XDestroyImage( image );
277 memcpy( p_block->p_buffer, image->data, i_size );
279 XDestroyImage( image );
285 /*****************************************************************************
287 *****************************************************************************/
288 static int Control( demux_t *p_demux, int i_query, va_list args )
295 /* Special for access_demux */
296 case DEMUX_CAN_PAUSE:
297 case DEMUX_CAN_CONTROL_PACE:
299 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
303 case DEMUX_GET_PTS_DELAY:
304 pi64 = (int64_t*)va_arg( args, int64_t * );
305 *pi64 = (int64_t)var_GetInteger( p_demux, "screen-caching" ) *1000;
308 /* TODO implement others */