demuxdump.c 7.77 KB
Newer Older
1 2 3
/*****************************************************************************
 * demuxdump.c : Pseudo demux module for vlc (dump raw stream)
 *****************************************************************************
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
4 5
 * Copyright (C) 2001-2003 VideoLAN
 * $Id: demuxdump.c,v 1.12 2004/01/25 20:05:28 hartman Exp $
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 *
 * Authors: Laurent Aimar <fenrir@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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */
#include <string.h>                                              /* strdup() */
#include <errno.h>

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

#include <sys/types.h>

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Activate ( vlc_object_t * );
static int  Demux ( input_thread_t * );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
41
static void Deactivate ( vlc_object_t * );
42 43 44 45 46 47

#define DUMP_BLOCKSIZE  16384

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
48
#define FILE_TEXT N_("Filename of dump")
Gildas Bazin's avatar
 
Gildas Bazin committed
49
#define FILE_LONGTEXT N_( \
Christophe Massiot's avatar
Christophe Massiot committed
50
    "Specify a file name to which the raw stream will be dumped." )
Gildas Bazin's avatar
 
Gildas Bazin committed
51

52
vlc_module_begin();
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
53
    set_description( _("Filedump demuxer") );
54
    set_capability( "demux", 0 );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
55 56 57
    add_file( "demuxdump-file", "stream-demux.dump", NULL, FILE_TEXT, 
              FILE_LONGTEXT, VLC_FALSE );
    set_callbacks( Activate, Deactivate );
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    add_shortcut( "dump" );
vlc_module_end();

struct demux_sys_t
{
    char        *psz_name;
    FILE        *p_file;
    uint64_t    i_write;

    void        *p_demux_data_sav;
};

/*
 * Data reading functions
 */

/*****************************************************************************
 * Activate: initializes dump structures
 *****************************************************************************/
static int Activate( vlc_object_t * p_this )
{
    input_thread_t      *p_input = (input_thread_t *)p_this;
    demux_sys_t         *p_demux;
Gildas Bazin's avatar
 
Gildas Bazin committed
81
    vlc_value_t         val;
82 83 84 85
    char                *psz_name;

    /* Set the demux function */
    p_input->pf_demux = Demux;
86
    p_input->pf_demux_control = demux_vaControlDefault;
87 88 89 90 91 92 93 94

    /* Initialize access plug-in structures. */
    if( p_input->i_mtu == 0 )
    {
        /* Improve speed. */
        p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
    }
    
Gildas Bazin's avatar
 
Gildas Bazin committed
95 96 97
    var_Create( p_input, "demuxdump-file", VLC_VAR_FILE|VLC_VAR_DOINHERIT );
    var_Get( p_input, "demuxdump-file", &val );
    psz_name = val.psz_string;
98 99
    if( !psz_name || !*psz_name )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
100 101
        msg_Warn( p_input, "no dump file name given" );
        return VLC_EGENERIC;
102 103 104 105 106
    }

    p_demux = malloc( sizeof( demux_sys_t ) );
    memset( p_demux, 0, sizeof( demux_sys_t ) );

107 108 109 110 111 112 113 114
    if( !strcmp( psz_name, "-" ) )
    {
        msg_Info( p_input,
                  "dumping raw stream to standard output" );
        p_demux->p_file = stdout;
        p_demux->psz_name = psz_name;
    }
    else if( !( p_demux->p_file = fopen( psz_name, "wb" ) ) )
115 116 117 118 119
    {
        msg_Err( p_input,
                 "cannot create `%s' for writing", 
                 psz_name );
        free( p_demux );
Gildas Bazin's avatar
 
Gildas Bazin committed
120
        return VLC_EGENERIC;
121 122 123 124 125 126 127 128
    }
    else
    {
        msg_Info( p_input,
                  "dumping raw stream to file `%s'", 
                  psz_name );
        p_demux->psz_name = psz_name;
    }
129

130 131 132 133 134 135 136 137 138 139
    p_demux->i_write = 0;
    p_demux->p_demux_data_sav = p_input->p_demux_data;

    if( p_input->stream.p_selected_program != NULL )
    {
        /* workaround for dvd access */
        msg_Warn( p_input, "demux data already initializated (by access?)" );
    }
    else
    {
140

141 142
        if( input_InitStream( p_input, 0 ) == -1 )
        {
143 144
            if( p_demux->p_file != stdout )
                fclose( p_demux->p_file );
145
            free( p_demux );
Gildas Bazin's avatar
 
Gildas Bazin committed
146
            return VLC_EGENERIC;
147 148 149
        }
        input_AddProgram( p_input, 0, 0 );
        p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
150

151 152 153 154 155 156 157 158 159 160 161
        vlc_mutex_lock( &p_input->stream.stream_lock );
        p_input->stream.p_selected_area->i_tell = 0;
        vlc_mutex_unlock( &p_input->stream.stream_lock );
    }

    p_input->p_demux_data = p_demux;

    vlc_mutex_lock( &p_input->stream.stream_lock );
    p_input->stream.p_selected_program->b_is_ok = 1;
    vlc_mutex_unlock( &p_input->stream.stream_lock );
    
Gildas Bazin's avatar
 
Gildas Bazin committed
162
    return VLC_SUCCESS;
163 164 165
}

/*****************************************************************************
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
166
 * Deactivate: initializes dump structures
167
 *****************************************************************************/
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
168
static void Deactivate ( vlc_object_t *p_this )
169 170 171
{
    input_thread_t      *p_input = (input_thread_t *)p_this;
    demux_sys_t         *p_demux = (demux_sys_t*)p_input->p_demux_data;
172 173 174

    msg_Info( p_input,
              "closing %s ("I64Fd" Kbytes dumped)",
175 176
              p_demux->psz_name,
              p_demux->i_write / 1024 );
177

178 179
    if( p_demux->p_file )
    {
180 181
        if( p_demux->p_file != stdout )
            fclose( p_demux->p_file );
182 183 184 185
        p_demux->p_file = NULL;
    }
    if( p_demux->psz_name )
    {
186
        free( p_demux->psz_name );
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    }
    p_input->p_demux_data = p_demux->p_demux_data_sav;
    free( p_demux );
}

/*****************************************************************************
 * Demux: reads and demuxes data packets
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
static int Demux( input_thread_t * p_input )
{
    demux_sys_t         *p_demux = (demux_sys_t*)p_input->p_demux_data;

    ssize_t         i_read;
    data_packet_t * p_data;
    int             i_write;

    p_input->p_demux_data = p_demux->p_demux_data_sav;
    i_read = input_SplitBuffer( p_input, &p_data, DUMP_BLOCKSIZE );
    p_input->p_demux_data = p_demux;
    
    if ( i_read <= 0 )
    {
        return i_read;
    }

    i_write = fwrite( p_data->p_payload_start,
                       1,
                       i_read,
                       p_demux->p_file );
    
    input_DeletePacket( p_input->p_method_data, p_data );

    if( i_write < 0 )
    {
        msg_Err( p_input, 
                 "failed to write %d bytes",
                 i_write );
        return( -1 );
    }
    else
    {
        msg_Dbg( p_input,
                 "dumped %d bytes",
                 i_write );
        p_demux->i_write += i_write;
    }


    if( (p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT)
         | (input_ClockManageControl( p_input, 
                      p_input->stream.p_selected_program,
                         (mtime_t)0 ) == PAUSE_S) )
    {
        msg_Warn( p_input, "synchro reinit" );
        p_input->stream.p_selected_program->i_synchro_state = SYNCHRO_OK;
    }

    return( 1 );
}