]> git.sesse.net Git - vlc/blob - modules/access/screen/screen.c
ALL: splitted screen module (needed as BeOS requires C++)
[vlc] / modules / access / screen / screen.c
1 /*****************************************************************************
2  * screen.c: Screen capture module.
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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     "Allows you to modify the default caching value for screen capture " \
40     "streams. This value should be set in millisecond units." )
41 #define FPS_TEXT N_("Frame rate")
42 #define FPS_LONGTEXT N_( \
43     "Allows you to set the desired frame rate for the capture." )
44
45 static int  Open ( vlc_object_t * );
46 static void Close( vlc_object_t * );
47
48 vlc_module_begin();
49     set_description( _("Screen Input") );
50
51     add_integer( "screen-caching", DEFAULT_PTS_DELAY / 1000, NULL,
52         CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
53     add_float( "screen-fps", 5, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
54
55     set_capability( "access_demux", 0 );
56     add_shortcut( "screen" );
57     set_callbacks( Open, Close );
58 vlc_module_end();
59
60 /*****************************************************************************
61  * Local prototypes
62  *****************************************************************************/
63 static int Control( demux_t *, int, va_list );
64 static int Demux  ( demux_t * );
65
66 /*****************************************************************************
67  * DemuxOpen:
68  *****************************************************************************/
69 static int Open( vlc_object_t *p_this )
70 {
71     demux_t     *p_demux = (demux_t*)p_this;
72     demux_sys_t *p_sys;
73     vlc_value_t val;
74
75     /* Fill p_demux field */
76     p_demux->pf_demux = Demux;
77     p_demux->pf_control = Control;
78     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
79     memset( p_sys, 0, sizeof( demux_sys_t ) );
80
81     if( screen_InitCapture( p_demux ) != VLC_SUCCESS )
82     {
83         free( p_sys );
84         return VLC_EGENERIC;
85     }
86
87     msg_Dbg( p_demux, "screen width: %i, height: %i, depth: %i",
88              p_sys->fmt.video.i_width, p_sys->fmt.video.i_height,
89              p_sys->fmt.video.i_bits_per_pixel );
90
91     p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
92
93     /* Update default_pts to a suitable value for screen access */
94     var_Create( p_demux, "screen-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
95
96     var_Create( p_demux, "screen-fps", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT );
97     var_Get( p_demux, "screen-fps", &val );
98     p_sys->f_fps = val.f_float;
99     p_sys->i_incr = 1000000 / val.f_float;
100     p_sys->i_next_date = 0;
101
102     return VLC_SUCCESS;
103 }
104
105 /*****************************************************************************
106  * Close:
107  *****************************************************************************/
108 static void Close( vlc_object_t *p_this )
109 {
110     demux_t     *p_demux = (demux_t*)p_this;
111     demux_sys_t *p_sys = p_demux->p_sys;
112
113     screen_CloseCapture( p_demux );
114     free( p_sys );
115 }
116
117 /*****************************************************************************
118  * Demux:
119  *****************************************************************************/
120 static int Demux( demux_t *p_demux )
121 {
122     demux_sys_t *p_sys = p_demux->p_sys;
123     block_t *p_block;
124
125     if( !p_sys->i_next_date ) p_sys->i_next_date = mdate();
126
127     /* Frame skipping if necessary */
128     while( mdate() >= p_sys->i_next_date + p_sys->i_incr )
129         p_sys->i_next_date += p_sys->i_incr;
130
131     mwait( p_sys->i_next_date );
132     p_block = screen_Capture( p_demux );
133     if( !p_block ) return 0;
134
135     p_block->i_dts = p_block->i_pts = p_sys->i_next_date;
136
137     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
138     es_out_Send( p_demux->out, p_sys->es, p_block );
139
140     p_sys->i_next_date += p_sys->i_incr;
141
142     return 1;
143 }
144
145 /*****************************************************************************
146  * Control:
147  *****************************************************************************/
148 static int Control( demux_t *p_demux, int i_query, va_list args )
149 {
150     vlc_bool_t *pb;
151     int64_t *pi64;
152
153     switch( i_query )
154     {
155         /* Special for access_demux */
156         case DEMUX_CAN_PAUSE:
157         case DEMUX_CAN_CONTROL_PACE:
158             /* TODO */
159             pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
160             *pb = VLC_FALSE;
161             return VLC_SUCCESS;
162
163         case DEMUX_GET_PTS_DELAY:
164             pi64 = (int64_t*)va_arg( args, int64_t * );
165             *pi64 = (int64_t)var_GetInteger( p_demux, "screen-caching" ) *1000;
166             return VLC_SUCCESS;
167
168         /* TODO implement others */
169         default:
170             return VLC_EGENERIC;
171     }
172 }