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