]> git.sesse.net Git - vlc/blob - modules/access/fake.c
Default duration of 10s when fake isn't forced. Make it possible to use fake:///path...
[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 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_access.h>
35 #include <vlc_demux.h>
36 #include <vlc_image.h>
37
38 /*****************************************************************************
39  * Module descriptior
40  *****************************************************************************/
41 static int  Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
43
44 #define CACHING_TEXT N_("Caching value in ms")
45 #define CACHING_LONGTEXT N_( \
46     "Caching value for fake streams. This " \
47     "value should be set in milliseconds." )
48 #define FPS_TEXT N_("Framerate")
49 #define FPS_LONGTEXT N_( \
50     "Number of frames per second (eg. 24, 25, 29.97, 30).")
51 #define ID_TEXT N_("ID")
52 #define ID_LONGTEXT N_( \
53     "Set the ID of the fake elementary stream for use in " \
54     "#duplicate{} constructs (default 0).")
55 #define DURATION_TEXT N_("Duration in ms")
56 #define DURATION_LONGTEXT N_( \
57     "Duration of the fake streaming before faking an " \
58     "end-of-file (default is -1 meaning that the stream is unlimited when " \
59     "fake is forced, or lasts for 10 seconds otherwise. 0, means that the " \
60     "stream is unlimited).")
61
62 vlc_module_begin ()
63     set_shortname( N_("Fake") )
64     set_description( N_("Fake input") )
65     set_category( CAT_INPUT )
66     set_subcategory( SUBCAT_INPUT_ACCESS )
67
68     add_integer( "fake-caching", DEFAULT_PTS_DELAY / 1000, NULL,
69                  CACHING_TEXT, CACHING_LONGTEXT, true );
70     add_float( "fake-fps", 25.0, NULL, FPS_TEXT, FPS_LONGTEXT, true )
71     add_integer( "fake-id", 0, NULL, ID_TEXT, ID_LONGTEXT, true )
72     add_integer( "fake-duration", -1, NULL, DURATION_TEXT, DURATION_LONGTEXT,
73                  true );
74
75     add_shortcut( "fake" )
76     set_capability( "access_demux", 100 )
77     set_callbacks( Open, Close )
78 vlc_module_end ()
79
80 /*****************************************************************************
81  * Access: local prototypes
82  *****************************************************************************/
83 static int Demux  ( demux_t * );
84 static int Control( demux_t *, int, va_list );
85
86 struct demux_sys_t
87 {
88     float f_fps;
89     mtime_t i_last_pts, i_duration, i_first_pts, i_end_pts, i_pause_pts;
90
91     es_out_id_t  *p_es_video;
92 };
93
94 /*****************************************************************************
95  * Open: opens fake device
96  *****************************************************************************/
97 static int Open( vlc_object_t *p_this )
98 {
99     demux_t     *p_demux = (demux_t*)p_this;
100     demux_sys_t *p_sys;
101     es_format_t fmt;
102
103     if( *p_demux->psz_access != '\0' )
104     {
105        /* if an access is provided, then it has to be "fake" */
106        if( strcmp( p_demux->psz_access, "fake" ) != 0 )
107         return VLC_EGENERIC;
108
109         msg_Dbg( p_demux, "fake:// access_demux detected" );
110     }
111     else
112     {
113        /**
114         * access is not provided,
115         * then let's see if path could be an image
116         **/
117
118         if( !p_demux->psz_path || !*p_demux->psz_path )
119             return VLC_EGENERIC;
120
121         vlc_fourcc_t i_codec = image_Ext2Fourcc( p_demux->psz_path );
122         if( !i_codec )
123             return VLC_EGENERIC;
124         char* p_codec = (char*) &i_codec;
125         msg_Dbg( p_demux, "still image detected with codec format %c%c%c%c",
126                    p_codec[0], p_codec[1], p_codec[2], p_codec[3] );
127     }
128
129     if( p_demux->psz_path && *p_demux->psz_path )
130     {
131         vlc_object_t* p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT,
132                                                  FIND_PARENT );
133         if( !p_input )
134             return VLC_EGENERIC;
135
136         /* set up fake-file on the fly */
137         var_Create( p_input, "fake-file", VLC_VAR_STRING );
138         var_SetString( p_input, "fake-file", p_demux->psz_path );
139
140         vlc_object_release( p_input );
141     }
142
143     /* Set up p_demux */
144     DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
145     p_demux->info.i_update = 0;
146     p_demux->info.i_title = 0;
147     p_demux->info.i_seekpoint = 0;
148
149     p_sys->i_duration =
150         (mtime_t)var_CreateGetInteger( p_demux, "fake-duration" ) * 1000;
151     if( p_sys->i_duration < 0 )
152     {
153         if( !strcmp( p_demux->psz_access, "fake" ) )
154             p_sys->i_duration = 0;
155         else
156             p_sys->i_duration = 10000*1000;
157     }
158     p_sys->f_fps = var_CreateGetFloat( p_demux, "fake-fps" );
159
160     /* Declare the elementary stream */
161     es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC('f','a','k','e') );
162     fmt.i_id = var_CreateGetInteger( p_demux, "fake-id" );
163     p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
164
165     /* Update default_pts to a suitable value for access */
166     var_Create( p_demux, "fake-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
167
168     return VLC_SUCCESS;
169 }
170
171 /*****************************************************************************
172  * Close: close device, free resources
173  *****************************************************************************/
174 static void Close( vlc_object_t *p_this )
175 {
176     demux_t     *p_demux = (demux_t *)p_this;
177     demux_sys_t *p_sys   = p_demux->p_sys;
178
179     free( p_sys );
180 }
181
182 /*****************************************************************************
183  * Control:
184  *****************************************************************************/
185 static int Control( demux_t *p_demux, int i_query, va_list args )
186 {
187     demux_sys_t *p_sys = p_demux->p_sys;
188     bool *pb, b;
189     int64_t    *pi64, i64;
190     double     *pf, f;
191
192     switch( i_query )
193     {
194         /* Special for access_demux */
195         case DEMUX_CAN_PAUSE:
196         case DEMUX_CAN_SEEK:
197         case DEMUX_CAN_CONTROL_PACE:
198             pb = (bool *)va_arg( args, bool * );
199             *pb = true;
200             return VLC_SUCCESS;
201
202         case DEMUX_SET_PAUSE_STATE:
203             b = (bool)va_arg( args, int );
204             if ( b )
205             {
206                 p_sys->i_pause_pts = mdate();
207             }
208             else if ( p_sys->i_pause_pts )
209             {
210                 mtime_t i_pause_duration = mdate() - p_sys->i_pause_pts;
211                 p_sys->i_first_pts += i_pause_duration;
212                 p_sys->i_last_pts += i_pause_duration;
213                 if ( p_sys->i_duration )
214                     p_sys->i_end_pts += i_pause_duration;
215                 p_sys->i_pause_pts = 0;
216             }
217             return VLC_SUCCESS;
218
219         case DEMUX_GET_PTS_DELAY:
220             pi64 = (int64_t *)va_arg( args, int64_t * );
221             *pi64 = (int64_t)var_GetInteger( p_demux, "fake-caching" ) * 1000;
222             return VLC_SUCCESS;
223
224         case DEMUX_GET_POSITION:
225             pf = (double*)va_arg( args, double* );
226             if( p_sys->i_duration > 0 )
227             {
228                 *pf = (double)( p_sys->i_last_pts - p_sys->i_first_pts )
229                                 / (double)(p_sys->i_duration);
230             }
231             else
232             {
233                 *pf = 0;
234             }
235             return VLC_SUCCESS;
236
237         case DEMUX_SET_POSITION:
238             f = (double)va_arg( args, double );
239             i64 = f * (double)p_sys->i_duration;
240             p_sys->i_first_pts = p_sys->i_last_pts - i64;
241             p_sys->i_end_pts = p_sys->i_first_pts + p_sys->i_duration;
242             return VLC_SUCCESS;
243
244         case DEMUX_GET_TIME:
245             pi64 = (int64_t *)va_arg( args, int64_t * );
246             if ( p_sys->i_duration )
247                 *pi64 = p_sys->i_last_pts - p_sys->i_first_pts;
248             else
249                 *pi64 = p_sys->i_last_pts;
250             return VLC_SUCCESS;
251
252         case DEMUX_GET_LENGTH:
253             pi64 = (int64_t*)va_arg( args, int64_t * );
254             *pi64 = p_sys->i_duration;
255             return VLC_SUCCESS;
256
257         case DEMUX_SET_TIME:
258             i64 = (int64_t)va_arg( args, int64_t );
259             p_sys->i_first_pts = p_sys->i_last_pts - i64;
260             p_sys->i_end_pts = p_sys->i_first_pts + p_sys->i_duration;
261             return VLC_SUCCESS;
262
263         /* TODO implement others */
264         default:
265             return VLC_EGENERIC;
266     }
267
268     return VLC_EGENERIC;
269 }
270
271 /*****************************************************************************
272  * Demux:
273  *****************************************************************************/
274 static int Demux( demux_t *p_demux )
275 {
276     demux_sys_t *p_sys = p_demux->p_sys;
277
278     if ( !p_sys->i_last_pts )
279     {
280         p_sys->i_last_pts = p_sys->i_first_pts = mdate();
281         if ( p_sys->i_duration )
282             p_sys->i_end_pts = p_sys->i_first_pts + p_sys->i_duration;
283     }
284     else
285     {
286         p_sys->i_last_pts += (mtime_t)(1000000.0 / p_sys->f_fps);
287         if ( p_sys->i_duration && p_sys->i_last_pts > p_sys->i_end_pts )
288             return 0;
289         mwait( p_sys->i_last_pts );
290     }
291
292     block_t *p_block = block_New( p_demux, 1 );
293     p_block->i_flags |= BLOCK_FLAG_TYPE_I;
294     p_block->i_dts = p_block->i_pts = p_sys->i_last_pts;
295
296     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
297     es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
298
299     return 1;
300 }