1 /*****************************************************************************
2 * fake.c : Fake video input for VLC
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Author: Christophe Massiot <massiot@via.ecp.fr>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc/input.h>
34 /*****************************************************************************
36 *****************************************************************************/
37 static int Open ( vlc_object_t * );
38 static void Close( vlc_object_t * );
40 #define CACHING_TEXT N_("Caching value in ms")
41 #define CACHING_LONGTEXT N_( \
42 "Caching value for fake streams. This " \
43 "value should be set in milliseconds." )
44 #define FPS_TEXT N_("Framerate")
45 #define FPS_LONGTEXT N_( \
46 "Number of frames per second (eg. 24, 25, 29.97, 30).")
47 #define ID_TEXT N_("ID")
48 #define ID_LONGTEXT N_( \
49 "Set the ID of the fake elementary stream for use in " \
50 "#duplicate{} constructs (default 0).")
51 #define DURATION_TEXT N_("Duration in ms")
52 #define DURATION_LONGTEXT N_( \
53 "Duration of the fake streaming before faking an " \
54 "end-of-file (default is 0, meaning that the stream is unlimited).")
57 set_shortname( _("Fake") );
58 set_description( _("Fake input") );
59 set_category( CAT_INPUT );
60 set_subcategory( SUBCAT_INPUT_ACCESS );
62 add_integer( "fake-caching", DEFAULT_PTS_DELAY / 1000, NULL,
63 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
64 add_float( "fake-fps", 25.0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
65 add_integer( "fake-id", 0, NULL, ID_TEXT, ID_LONGTEXT, VLC_TRUE );
66 add_integer( "fake-duration", 0, NULL, DURATION_TEXT, DURATION_LONGTEXT,
69 add_shortcut( "fake" );
70 set_capability( "access_demux", 0 );
71 set_callbacks( Open, Close );
74 /*****************************************************************************
75 * Access: local prototypes
76 *****************************************************************************/
77 static int Demux ( demux_t * );
78 static int Control( demux_t *, int, va_list );
83 mtime_t i_last_pts, i_duration, i_first_pts, i_end_pts, i_pause_pts;
85 es_out_id_t *p_es_video;
88 /*****************************************************************************
89 * Open: opens fake device
90 *****************************************************************************/
91 static int Open( vlc_object_t *p_this )
93 demux_t *p_demux = (demux_t*)p_this;
97 /* Only when selected */
98 if( *p_demux->psz_access == '\0' )
102 STANDARD_DEMUX_INIT; p_sys = p_demux->p_sys;
103 p_demux->info.i_update = 0;
104 p_demux->info.i_title = 0;
105 p_demux->info.i_seekpoint = 0;
107 p_sys->i_duration = var_CreateGetInteger( p_demux, "fake-duration" ) * 1000;
108 p_sys->f_fps = var_CreateGetFloat( p_demux, "fake-fps" );
110 /* Declare the elementary stream */
111 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC('f','a','k','e') );
112 fmt.i_id = var_CreateGetInteger( p_demux, "fake-id" );
113 p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
115 /* Update default_pts to a suitable value for access */
116 var_Create( p_demux, "fake-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
121 /*****************************************************************************
122 * Close: close device, free resources
123 *****************************************************************************/
124 static void Close( vlc_object_t *p_this )
126 demux_t *p_demux = (demux_t *)p_this;
127 demux_sys_t *p_sys = p_demux->p_sys;
132 /*****************************************************************************
134 *****************************************************************************/
135 static int Control( demux_t *p_demux, int i_query, va_list args )
137 demux_sys_t *p_sys = p_demux->p_sys;
144 /* Special for access_demux */
145 case DEMUX_CAN_PAUSE:
146 case DEMUX_CAN_CONTROL_PACE:
147 pb = (vlc_bool_t *)va_arg( args, vlc_bool_t * );
151 case DEMUX_SET_PAUSE_STATE:
152 b = (vlc_bool_t)va_arg( args, vlc_bool_t );
155 p_sys->i_pause_pts = mdate();
157 else if ( p_sys->i_pause_pts )
159 mtime_t i_pause_duration = mdate() - p_sys->i_pause_pts;
160 p_sys->i_first_pts += i_pause_duration;
161 p_sys->i_last_pts += i_pause_duration;
162 if ( p_sys->i_duration )
163 p_sys->i_end_pts += i_pause_duration;
164 p_sys->i_pause_pts = 0;
168 case DEMUX_GET_PTS_DELAY:
169 pi64 = (int64_t *)va_arg( args, int64_t * );
170 *pi64 = (int64_t)var_GetInteger( p_demux, "fake-caching" ) * 1000;
173 case DEMUX_GET_POSITION:
174 pf = (double*)va_arg( args, double* );
175 if( p_sys->i_duration > 0 )
177 *pf = (double)( p_sys->i_last_pts - p_sys->i_first_pts )
178 / (double)(p_sys->i_duration);
186 case DEMUX_SET_POSITION:
187 f = (double)va_arg( args, double );
188 i64 = f * (double)p_sys->i_duration;
189 p_sys->i_first_pts = p_sys->i_last_pts - i64;
190 p_sys->i_end_pts = p_sys->i_first_pts + p_sys->i_duration;
194 pi64 = (int64_t *)va_arg( args, int64_t * );
195 if ( p_sys->i_duration )
196 *pi64 = p_sys->i_last_pts - p_sys->i_first_pts;
198 *pi64 = p_sys->i_last_pts;
201 case DEMUX_GET_LENGTH:
202 pi64 = (int64_t*)va_arg( args, int64_t * );
203 *pi64 = p_sys->i_duration;
207 i64 = (int64_t)va_arg( args, int64_t );
208 p_sys->i_first_pts = p_sys->i_last_pts - i64;
209 p_sys->i_end_pts = p_sys->i_first_pts + p_sys->i_duration;
212 /* TODO implement others */
220 /*****************************************************************************
222 *****************************************************************************/
223 static int Demux( demux_t *p_demux )
225 demux_sys_t *p_sys = p_demux->p_sys;
226 block_t *p_block = block_New( p_demux, 1 );
228 if ( !p_sys->i_last_pts )
230 p_sys->i_last_pts = p_sys->i_first_pts = mdate();
231 if ( p_sys->i_duration )
232 p_sys->i_end_pts = p_sys->i_first_pts + p_sys->i_duration;
236 p_sys->i_last_pts += (mtime_t)(1000000.0 / p_sys->f_fps);
237 if ( p_sys->i_duration && p_sys->i_last_pts > p_sys->i_end_pts )
239 mwait( p_sys->i_last_pts );
242 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
243 p_block->i_dts = p_block->i_pts = p_sys->i_last_pts;
245 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
246 es_out_Send( p_demux->out, p_sys->p_es_video, p_block );