demuxdump.c 5.61 KB
Newer Older
1 2 3
/*****************************************************************************
 * demuxdump.c : Pseudo demux module for vlc (dump raw stream)
 *****************************************************************************
4 5
 * Copyright (C) 2001-2004 VideoLAN
 * $Id$
6 7 8 9 10 11 12
 *
 * 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.
13
 *
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 * 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>

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

41 42 43
static int  Open( vlc_object_t * );
static void Close ( vlc_object_t * );

44
vlc_module_begin();
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
45
    set_description( _("Filedump demuxer") );
46 47
    set_capability( "demux2", 0 );
    add_file( "demuxdump-file", "stream-demux.dump", NULL, FILE_TEXT,
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
48
              FILE_LONGTEXT, VLC_FALSE );
49
    set_callbacks( Open, Close );
50 51 52
    add_shortcut( "dump" );
vlc_module_end();

53 54 55 56 57 58 59 60 61

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int Demux( demux_t * );
static int Control( demux_t *, int,va_list );

#define DUMP_BLOCKSIZE  16384

62 63
struct demux_sys_t
{
64
    char        *psz_file;
65 66 67
    FILE        *p_file;
    uint64_t    i_write;

68
    uint8_t     buffer[DUMP_BLOCKSIZE];
69 70 71 72 73 74 75
};

/*
 * Data reading functions
 */

/*****************************************************************************
76
 * Open: initializes dump structures
77
 *****************************************************************************/
78
static int Open( vlc_object_t * p_this )
79
{
80 81
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
82

83 84
    /* Accept only if forced */
    if( strcasecmp( p_demux->psz_demux, "dump" ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
85
        return VLC_EGENERIC;
86

87 88 89 90 91 92 93
    p_demux->pf_demux = Demux;
    p_demux->pf_control = Control;
    p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
    p_sys->i_write = 0;
    p_sys->p_file = NULL;
    p_sys->psz_file = var_CreateGetString( p_demux, "demuxdump-file" );
    if( *p_sys->psz_file == '\0' )
94
    {
95
        msg_Warn( p_demux, "no dump file name given" );
Gildas Bazin's avatar
 
Gildas Bazin committed
96
        return VLC_EGENERIC;
97
    }
98

99
    if( !strcmp( p_sys->psz_file, "-" ) )
100
    {
101 102
        msg_Info( p_demux, "dumping raw stream to standard output" );
        p_sys->p_file = stdout;
103
    }
104
    else if( ( p_sys->p_file = fopen( p_sys->psz_file, "wb" ) ) == NULL )
105
    {
106
        msg_Err( p_demux,"cannot create `%s' for writing", p_sys->psz_file );
107

108 109
        free( p_sys );
        return VLC_EGENERIC;
110
    }
111
    msg_Info( p_demux, "dumping raw stream to file `%s'", p_sys->psz_file );
112

Gildas Bazin's avatar
 
Gildas Bazin committed
113
    return VLC_SUCCESS;
114 115 116
}

/*****************************************************************************
117
 * Close:
118
 *****************************************************************************/
119
static void Close( vlc_object_t *p_this )
120
{
121

122 123
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys = p_demux->p_sys;
124

125 126 127 128
    msg_Info( p_demux ,"closing %s ("I64Fd" Kbytes dumped)", p_sys->psz_file,
              p_sys->i_write / 1024 );

    if( p_sys->p_file != stdout )
129
    {
130
        fclose( p_sys->p_file );
131
    }
132 133 134
    free( p_sys->psz_file );

    free( p_sys );
135 136 137 138 139 140 141
}

/*****************************************************************************
 * Demux: reads and demuxes data packets
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
142
static int Demux( demux_t *p_demux )
143
{
144
    demux_sys_t *p_sys = p_demux->p_sys;
145

146
    int i_data;
147

148 149 150 151
    /* I'm pretty sure that stream_Peek,stream_Read( , NULL ) would be faster*/
    i_data = stream_Read( p_demux->s, p_sys->buffer, DUMP_BLOCKSIZE );
    if ( i_data <= 0 )
        return i_data;
152

153
    i_data = fwrite( p_sys->buffer, 1, i_data, p_sys->p_file );
154

155
    if( i_data < 0 )
156
    {
157 158
        msg_Err( p_demux, "failed to write data" );
        return -1;
159
    }
160
    msg_Dbg( p_demux, "dumped %d bytes", i_data );
161

162
    p_sys->i_write += i_data;
163

164 165
    return 1;
}
166

167 168 169 170 171 172
/*****************************************************************************
 * Demux: reads and demuxes data packets
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
    return demux2_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
173
}
174