]> git.sesse.net Git - vlc/blob - modules/access/screen/screen.c
fae5e95a384ba45d869f112a8ef9c0782fba3368
[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 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include "screen.h"
33
34 /*****************************************************************************
35  * Module descriptor
36  *****************************************************************************/
37 #define CACHING_TEXT N_("Caching value in ms")
38 #define CACHING_LONGTEXT N_( \
39     "Caching value for screen capture. "\
40     "This value should be set in milliseconds." )
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 static int  Open ( vlc_object_t * );
53 static void Close( vlc_object_t * );
54
55 #ifdef WIN32
56 #   define SCREEN_FPS 1
57 #else
58 #   define SCREEN_FPS 5
59 #endif
60
61 vlc_module_begin();
62     set_description( _("Screen Input") );
63     set_shortname( N_("Screen" ));
64     set_category( CAT_INPUT );
65     set_subcategory( SUBCAT_INPUT_ACCESS );
66
67     add_integer( "screen-caching", DEFAULT_PTS_DELAY / 1000, NULL,
68         CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
69     add_float( "screen-fps", SCREEN_FPS, 0, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
70
71 #ifdef WIN32
72     add_integer( "screen-fragment-size", 0, NULL, FRAGS_TEXT,
73         FRAGS_LONGTEXT, VLC_TRUE );
74 #endif
75
76     set_capability( "access_demux", 0 );
77     add_shortcut( "screen" );
78     set_callbacks( Open, Close );
79 vlc_module_end();
80
81 /*****************************************************************************
82  * Local prototypes
83  *****************************************************************************/
84 static int Control( demux_t *, int, va_list );
85 static int Demux  ( demux_t * );
86
87 /*****************************************************************************
88  * DemuxOpen:
89  *****************************************************************************/
90 static int Open( vlc_object_t *p_this )
91 {
92     demux_t     *p_demux = (demux_t*)p_this;
93     demux_sys_t *p_sys;
94     vlc_value_t val;
95
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 ) );
101
102     /* Update default_pts to a suitable value for screen access */
103     var_Create( p_demux, "screen-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
104
105     var_Create( p_demux, "screen-fps", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT );
106     var_Get( p_demux, "screen-fps", &val );
107     p_sys->f_fps = val.f_float;
108     p_sys->i_incr = 1000000 / val.f_float;
109     p_sys->i_next_date = 0;
110
111     if( screen_InitCapture( p_demux ) != VLC_SUCCESS )
112     {
113         free( p_sys );
114         return VLC_EGENERIC;
115     }
116
117     msg_Dbg( p_demux, "screen width: %i, height: %i, depth: %i",
118              p_sys->fmt.video.i_width, p_sys->fmt.video.i_height,
119              p_sys->fmt.video.i_bits_per_pixel );
120
121     p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
122
123     return VLC_SUCCESS;
124 }
125
126 /*****************************************************************************
127  * Close:
128  *****************************************************************************/
129 static void Close( vlc_object_t *p_this )
130 {
131     demux_t     *p_demux = (demux_t*)p_this;
132     demux_sys_t *p_sys = p_demux->p_sys;
133
134     screen_CloseCapture( p_demux );
135     free( p_sys );
136 }
137
138 /*****************************************************************************
139  * Demux:
140  *****************************************************************************/
141 static int Demux( demux_t *p_demux )
142 {
143     demux_sys_t *p_sys = p_demux->p_sys;
144     block_t *p_block;
145
146     if( !p_sys->i_next_date ) p_sys->i_next_date = mdate();
147
148     /* Frame skipping if necessary */
149     while( mdate() >= p_sys->i_next_date + p_sys->i_incr )
150         p_sys->i_next_date += p_sys->i_incr;
151
152     mwait( p_sys->i_next_date );
153     p_block = screen_Capture( p_demux );
154     if( !p_block )
155     {
156         p_sys->i_next_date += p_sys->i_incr;
157         return 1;
158     }
159
160     p_block->i_dts = p_block->i_pts = p_sys->i_next_date;
161
162     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
163     es_out_Send( p_demux->out, p_sys->es, p_block );
164
165     p_sys->i_next_date += p_sys->i_incr;
166
167     return 1;
168 }
169
170 /*****************************************************************************
171  * Control:
172  *****************************************************************************/
173 static int Control( demux_t *p_demux, int i_query, va_list args )
174 {
175     vlc_bool_t *pb;
176     int64_t *pi64;
177
178     switch( i_query )
179     {
180         /* Special for access_demux */
181         case DEMUX_CAN_PAUSE:
182         case DEMUX_CAN_CONTROL_PACE:
183             /* TODO */
184             pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
185             *pb = VLC_FALSE;
186             return VLC_SUCCESS;
187
188         case DEMUX_GET_PTS_DELAY:
189             pi64 = (int64_t*)va_arg( args, int64_t * );
190             *pi64 = (int64_t)var_GetInteger( p_demux, "screen-caching" ) *1000;
191             return VLC_SUCCESS;
192
193         /* TODO implement others */
194         default:
195             return VLC_EGENERIC;
196     }
197 }