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