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