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