fake.c 8.92 KB
Newer Older
1 2 3
/*****************************************************************************
 * fake.c : Fake video input for VLC
 *****************************************************************************
4
 * Copyright (C) 2005 the VideoLAN team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * $Id$
 *
 * Author: Christophe Massiot <massiot@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <vlc/vlc.h>
#include <vlc/input.h>

/*****************************************************************************
 * Module descriptior
 *****************************************************************************/
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

#define CACHING_TEXT N_("Caching value in ms")
#define CACHING_LONGTEXT N_( \
42
    "Caching value for fake streams. This " \
Clément Stenac's avatar
Clément Stenac committed
43
    "value should be set in milliseconds." )
44 45
#define FPS_TEXT N_("Framerate")
#define FPS_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
46
    "Number of frames per second (eg. 24, 25, 29.97, 30).")
47 48
#define ID_TEXT N_("ID")
#define ID_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
49
    "Set the ID of the fake elementary stream for use in " \
50
    "#duplicate{} constructs (default 0).")
51 52
#define DURATION_TEXT N_("Duration in ms")
#define DURATION_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
53 54
    "Duration of the fake streaming before faking an " \
    "end-of-file (default is 0, meaning that the stream is unlimited).")
55 56 57 58 59 60 61 62 63 64

vlc_module_begin();
    set_shortname( _("Fake") );
    set_description( _("Fake input") );
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_ACCESS );

    add_integer( "fake-caching", DEFAULT_PTS_DELAY / 1000, NULL,
                 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
    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 67
    add_integer( "fake-duration", 0, NULL, DURATION_TEXT, DURATION_LONGTEXT,
                 VLC_TRUE );
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

    add_shortcut( "fake" );
    set_capability( "access_demux", 0 );
    set_callbacks( Open, Close );
vlc_module_end();

/*****************************************************************************
 * Access: local prototypes
 *****************************************************************************/
static int Demux  ( demux_t * );
static int Control( demux_t *, int, va_list );

struct demux_sys_t
{
    float f_fps;
83
    mtime_t i_last_pts, i_duration, i_first_pts, i_end_pts, i_pause_pts;
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

    es_out_id_t  *p_es_video;
};

/*****************************************************************************
 * Open: opens fake device
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
    vlc_value_t val;
    es_format_t fmt;

    /* Only when selected */
    if( *p_demux->psz_access == '\0' )
        return VLC_EGENERIC;

    /* Set up p_demux */
    p_demux->pf_demux = Demux;
    p_demux->pf_control = Control;
    p_demux->info.i_update = 0;
    p_demux->info.i_title = 0;
    p_demux->info.i_seekpoint = 0;
    p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
    memset( p_sys, 0, sizeof( demux_sys_t ) );

111 112 113 114
    var_Create( p_demux, "fake-duration", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Get( p_demux, "fake-duration", &val );
    p_sys->i_duration = val.i_int * 1000;

115 116 117 118
    var_Create( p_demux, "fake-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
    var_Get( p_demux, "fake-fps", &val );
    p_sys->f_fps = val.f_float;

119 120
    /* Declare the elementary stream */
    es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC('f','a','k','e') );
121 122 123
    var_Create( p_demux, "fake-id", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Get( p_demux, "fake-id", &val );
    fmt.i_id = val.i_int;
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );

    /* Update default_pts to a suitable value for access */
    var_Create( p_demux, "fake-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: close device, free resources
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t *)p_this;
    demux_sys_t *p_sys   = p_demux->p_sys;

    free( p_sys );
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
    demux_sys_t *p_sys = p_demux->p_sys;
149 150 151
    vlc_bool_t *pb, b;
    int64_t    *pi64, i64;
    double     *pf, f;
152 153 154 155 156 157

    switch( i_query )
    {
        /* Special for access_demux */
        case DEMUX_CAN_PAUSE:
        case DEMUX_CAN_CONTROL_PACE:
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
            pb = (vlc_bool_t *)va_arg( args, vlc_bool_t * );
            *pb = VLC_TRUE;
            return VLC_SUCCESS;

        case DEMUX_SET_PAUSE_STATE:
            b = (vlc_bool_t)va_arg( args, vlc_bool_t );
            if ( b )
            {
                p_sys->i_pause_pts = mdate();
            }
            else if ( p_sys->i_pause_pts )
            {
                mtime_t i_pause_duration = mdate() - p_sys->i_pause_pts;
                p_sys->i_first_pts += i_pause_duration;
                p_sys->i_last_pts += i_pause_duration;
                if ( p_sys->i_duration )
                    p_sys->i_end_pts += i_pause_duration;
                p_sys->i_pause_pts = 0;
            }
177 178 179
            return VLC_SUCCESS;

        case DEMUX_GET_PTS_DELAY:
180
            pi64 = (int64_t *)va_arg( args, int64_t * );
181 182 183
            *pi64 = (int64_t)var_GetInteger( p_demux, "fake-caching" ) * 1000;
            return VLC_SUCCESS;

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
        case DEMUX_GET_POSITION:
            pf = (double*)va_arg( args, double* );
            if( p_sys->i_duration > 0 )
            {
                *pf = (double)( p_sys->i_last_pts - p_sys->i_first_pts )
                                / (double)(p_sys->i_duration);
            }
            else
            {
                *pf = 0;
            }
            return VLC_SUCCESS;

        case DEMUX_SET_POSITION:
            f = (double)va_arg( args, double );
            i64 = f * (double)p_sys->i_duration;
            p_sys->i_first_pts = p_sys->i_last_pts - i64;
            p_sys->i_end_pts = p_sys->i_first_pts + p_sys->i_duration;
            return VLC_SUCCESS;

204
        case DEMUX_GET_TIME:
205 206 207 208 209 210 211 212
            pi64 = (int64_t *)va_arg( args, int64_t * );
            if ( p_sys->i_duration )
                *pi64 = p_sys->i_last_pts - p_sys->i_first_pts;
            else
                *pi64 = p_sys->i_last_pts;
            return VLC_SUCCESS;

        case DEMUX_GET_LENGTH:
213
            pi64 = (int64_t*)va_arg( args, int64_t * );
214 215 216 217 218 219 220
            *pi64 = p_sys->i_duration;
            return VLC_SUCCESS;

        case DEMUX_SET_TIME:
            i64 = (int64_t)va_arg( args, int64_t );
            p_sys->i_first_pts = p_sys->i_last_pts - i64;
            p_sys->i_end_pts = p_sys->i_first_pts + p_sys->i_duration;
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
            return VLC_SUCCESS;

        /* TODO implement others */
        default:
            return VLC_EGENERIC;
    }

    return VLC_EGENERIC;
}

/*****************************************************************************
 * Demux:
 *****************************************************************************/
static int Demux( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    block_t *p_block = block_New( p_demux, 1 );

    if ( !p_sys->i_last_pts )
    {
241 242 243
        p_sys->i_last_pts = p_sys->i_first_pts = mdate();
        if ( p_sys->i_duration )
            p_sys->i_end_pts = p_sys->i_first_pts + p_sys->i_duration;
244 245 246
    }
    else
    {
247
        p_sys->i_last_pts += (mtime_t)(1000000.0 / p_sys->f_fps);
248 249
        if ( p_sys->i_duration && p_sys->i_last_pts > p_sys->i_end_pts )
            return 0;
250 251 252 253 254 255 256 257 258 259 260 261
        mwait( p_sys->i_last_pts );
    }

    p_block->i_flags |= BLOCK_FLAG_TYPE_I;
    p_block->i_dts = p_block->i_pts = p_sys->i_last_pts;

    es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
    es_out_Send( p_demux->out, p_sys->p_es_video, p_block );

    return 1;
}