]> git.sesse.net Git - vlc/blob - modules/access/fake.c
Remove stdio while we're at it.
[vlc] / modules / access / fake.c
1 /*****************************************************************************
2  * fake.c : Fake video input for VLC
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Author: Christophe Massiot <massiot@via.ecp.fr>
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 #include <vlc/vlc.h>
29 #include <vlc_access.h>
30 #include <vlc_demux.h>
31
32 /*****************************************************************************
33  * Module descriptior
34  *****************************************************************************/
35 static int  Open ( vlc_object_t * );
36 static void Close( vlc_object_t * );
37
38 #define CACHING_TEXT N_("Caching value in ms")
39 #define CACHING_LONGTEXT N_( \
40     "Caching value for fake streams. This " \
41     "value should be set in milliseconds." )
42 #define FPS_TEXT N_("Framerate")
43 #define FPS_LONGTEXT N_( \
44     "Number of frames per second (eg. 24, 25, 29.97, 30).")
45 #define ID_TEXT N_("ID")
46 #define ID_LONGTEXT N_( \
47     "Set the ID of the fake elementary stream for use in " \
48     "#duplicate{} constructs (default 0).")
49 #define DURATION_TEXT N_("Duration in ms")
50 #define DURATION_LONGTEXT N_( \
51     "Duration of the fake streaming before faking an " \
52     "end-of-file (default is 0, meaning that the stream is unlimited).")
53
54 vlc_module_begin();
55     set_shortname( _("Fake") );
56     set_description( _("Fake input") );
57     set_category( CAT_INPUT );
58     set_subcategory( SUBCAT_INPUT_ACCESS );
59
60     add_integer( "fake-caching", DEFAULT_PTS_DELAY / 1000, NULL,
61                  CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
62     add_float( "fake-fps", 25.0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
63     add_integer( "fake-id", 0, NULL, ID_TEXT, ID_LONGTEXT, VLC_TRUE );
64     add_integer( "fake-duration", 0, NULL, DURATION_TEXT, DURATION_LONGTEXT,
65                  VLC_TRUE );
66
67     add_shortcut( "fake" );
68     set_capability( "access_demux", 0 );
69     set_callbacks( Open, Close );
70 vlc_module_end();
71
72 /*****************************************************************************
73  * Access: local prototypes
74  *****************************************************************************/
75 static int Demux  ( demux_t * );
76 static int Control( demux_t *, int, va_list );
77
78 struct demux_sys_t
79 {
80     float f_fps;
81     mtime_t i_last_pts, i_duration, i_first_pts, i_end_pts, i_pause_pts;
82
83     es_out_id_t  *p_es_video;
84 };
85
86 /*****************************************************************************
87  * Open: opens fake device
88  *****************************************************************************/
89 static int Open( vlc_object_t *p_this )
90 {
91     demux_t     *p_demux = (demux_t*)p_this;
92     demux_sys_t *p_sys;
93     es_format_t fmt;
94
95     /* Only when selected */
96     if( *p_demux->psz_access == '\0' )
97         return VLC_EGENERIC;
98
99     /* Set up p_demux */
100     STANDARD_DEMUX_INIT; p_sys = p_demux->p_sys;
101     p_demux->info.i_update = 0;
102     p_demux->info.i_title = 0;
103     p_demux->info.i_seekpoint = 0;
104
105     p_sys->i_duration = var_CreateGetInteger( p_demux, "fake-duration" ) * 1000;
106     p_sys->f_fps = var_CreateGetFloat( p_demux, "fake-fps" );
107
108     /* Declare the elementary stream */
109     es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC('f','a','k','e') );
110     fmt.i_id = var_CreateGetInteger( p_demux, "fake-id" );
111     p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
112
113     /* Update default_pts to a suitable value for access */
114     var_Create( p_demux, "fake-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
115
116     return VLC_SUCCESS;
117 }
118
119 /*****************************************************************************
120  * Close: close device, free resources
121  *****************************************************************************/
122 static void Close( vlc_object_t *p_this )
123 {
124     demux_t     *p_demux = (demux_t *)p_this;
125     demux_sys_t *p_sys   = p_demux->p_sys;
126
127     free( p_sys );
128 }
129
130 /*****************************************************************************
131  * Control:
132  *****************************************************************************/
133 static int Control( demux_t *p_demux, int i_query, va_list args )
134 {
135     demux_sys_t *p_sys = p_demux->p_sys;
136     vlc_bool_t *pb, b;
137     int64_t    *pi64, i64;
138     double     *pf, f;
139
140     switch( i_query )
141     {
142         /* Special for access_demux */
143         case DEMUX_CAN_PAUSE:
144         case DEMUX_CAN_CONTROL_PACE:
145             pb = (vlc_bool_t *)va_arg( args, vlc_bool_t * );
146             *pb = VLC_TRUE;
147             return VLC_SUCCESS;
148
149         case DEMUX_SET_PAUSE_STATE:
150             b = (vlc_bool_t)va_arg( args, vlc_bool_t );
151             if ( b )
152             {
153                 p_sys->i_pause_pts = mdate();
154             }
155             else if ( p_sys->i_pause_pts )
156             {
157                 mtime_t i_pause_duration = mdate() - p_sys->i_pause_pts;
158                 p_sys->i_first_pts += i_pause_duration;
159                 p_sys->i_last_pts += i_pause_duration;
160                 if ( p_sys->i_duration )
161                     p_sys->i_end_pts += i_pause_duration;
162                 p_sys->i_pause_pts = 0;
163             }
164             return VLC_SUCCESS;
165
166         case DEMUX_GET_PTS_DELAY:
167             pi64 = (int64_t *)va_arg( args, int64_t * );
168             *pi64 = (int64_t)var_GetInteger( p_demux, "fake-caching" ) * 1000;
169             return VLC_SUCCESS;
170
171         case DEMUX_GET_POSITION:
172             pf = (double*)va_arg( args, double* );
173             if( p_sys->i_duration > 0 )
174             {
175                 *pf = (double)( p_sys->i_last_pts - p_sys->i_first_pts )
176                                 / (double)(p_sys->i_duration);
177             }
178             else
179             {
180                 *pf = 0;
181             }
182             return VLC_SUCCESS;
183
184         case DEMUX_SET_POSITION:
185             f = (double)va_arg( args, double );
186             i64 = f * (double)p_sys->i_duration;
187             p_sys->i_first_pts = p_sys->i_last_pts - i64;
188             p_sys->i_end_pts = p_sys->i_first_pts + p_sys->i_duration;
189             return VLC_SUCCESS;
190
191         case DEMUX_GET_TIME:
192             pi64 = (int64_t *)va_arg( args, int64_t * );
193             if ( p_sys->i_duration )
194                 *pi64 = p_sys->i_last_pts - p_sys->i_first_pts;
195             else
196                 *pi64 = p_sys->i_last_pts;
197             return VLC_SUCCESS;
198
199         case DEMUX_GET_LENGTH:
200             pi64 = (int64_t*)va_arg( args, int64_t * );
201             *pi64 = p_sys->i_duration;
202             return VLC_SUCCESS;
203
204         case DEMUX_SET_TIME:
205             i64 = (int64_t)va_arg( args, int64_t );
206             p_sys->i_first_pts = p_sys->i_last_pts - i64;
207             p_sys->i_end_pts = p_sys->i_first_pts + p_sys->i_duration;
208             return VLC_SUCCESS;
209
210         /* TODO implement others */
211         default:
212             return VLC_EGENERIC;
213     }
214
215     return VLC_EGENERIC;
216 }
217
218 /*****************************************************************************
219  * Demux:
220  *****************************************************************************/
221 static int Demux( demux_t *p_demux )
222 {
223     demux_sys_t *p_sys = p_demux->p_sys;
224     block_t *p_block = block_New( p_demux, 1 );
225
226     if ( !p_sys->i_last_pts )
227     {
228         p_sys->i_last_pts = p_sys->i_first_pts = mdate();
229         if ( p_sys->i_duration )
230             p_sys->i_end_pts = p_sys->i_first_pts + p_sys->i_duration;
231     }
232     else
233     {
234         p_sys->i_last_pts += (mtime_t)(1000000.0 / p_sys->f_fps);
235         if ( p_sys->i_duration && p_sys->i_last_pts > p_sys->i_end_pts )
236             return 0;
237         mwait( p_sys->i_last_pts );
238     }
239
240     p_block->i_flags |= BLOCK_FLAG_TYPE_I;
241     p_block->i_dts = p_block->i_pts = p_sys->i_last_pts;
242
243     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
244     es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
245
246     return 1;
247 }