demuxdump.c 6.27 KB
Newer Older
1 2 3
/*****************************************************************************
 * demuxdump.c : Pseudo demux module for vlc (dump raw stream)
 *****************************************************************************
4
 * Copyright (C) 2001-2004 the VideoLAN team
5
 * $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
 * 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
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <string.h>                                              /* strdup() */
#include <errno.h>

#include <vlc/vlc.h>
Clément Stenac's avatar
Clément Stenac committed
31 32
#include <vlc_demux.h>
#include <vlc_charset.h>
33 34 35 36

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Clément Stenac's avatar
Clément Stenac committed
37
#define FILE_TEXT N_("Dump filename")
Gildas Bazin's avatar
 
Gildas Bazin committed
38
#define FILE_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
39 40
    "Name of the file to which the raw stream will be dumped." )
#define APPEND_TEXT N_("Append to existing file")
41
#define APPEND_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
42
    "If the file already exists, it will not be overwritten." )
Gildas Bazin's avatar
 
Gildas Bazin committed
43

44 45 46
static int  Open( vlc_object_t * );
static void Close ( vlc_object_t * );

47
vlc_module_begin();
48
    set_shortname("Dump");
Clément Stenac's avatar
Clément Stenac committed
49 50
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_DEMUX );
Clément Stenac's avatar
Clément Stenac committed
51
    set_description( _("File dumpper") );
52 53
    set_capability( "demux2", 0 );
    add_file( "demuxdump-file", "stream-demux.dump", NULL, FILE_TEXT,
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
54
              FILE_LONGTEXT, VLC_FALSE );
55 56
    add_bool( "demuxdump-append", 0, NULL, APPEND_TEXT, APPEND_LONGTEXT,
              VLC_FALSE );
57
    set_callbacks( Open, Close );
58 59 60
    add_shortcut( "dump" );
vlc_module_end();

61 62 63 64 65 66 67 68 69

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

#define DUMP_BLOCKSIZE  16384

70 71
struct demux_sys_t
{
72
    char        *psz_file;
73 74 75
    FILE        *p_file;
    uint64_t    i_write;

76
    uint8_t     buffer[DUMP_BLOCKSIZE];
77 78 79 80 81 82 83
};

/*
 * Data reading functions
 */

/*****************************************************************************
84
 * Open: initializes dump structures
85
 *****************************************************************************/
86
static int Open( vlc_object_t * p_this )
87
{
88 89
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
90
    const char  *psz_mode;
91 92
    vlc_value_t val;
    vlc_bool_t  b_append;
93

94 95
    /* Accept only if forced */
    if( strcasecmp( p_demux->psz_demux, "dump" ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
96
        return VLC_EGENERIC;
97

98 99 100 101 102 103 104 105
    var_Create( p_demux, "demuxdump-append", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
    var_Get( p_demux, "demuxdump-append", &val );
    b_append = val.b_bool;
    if ( b_append )
        psz_mode = "ab";
    else
        psz_mode = "wb";

106 107 108 109 110 111 112
    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' )
113
    {
114
        msg_Warn( p_demux, "no dump file name given" );
Gildas Bazin's avatar
 
Gildas Bazin committed
115
        return VLC_EGENERIC;
116
    }
117

118
    if( !strcmp( p_sys->psz_file, "-" ) )
119
    {
120 121
        msg_Info( p_demux, "dumping raw stream to standard output" );
        p_sys->p_file = stdout;
122
    }
123
    else if( ( p_sys->p_file = utf8_fopen( p_sys->psz_file, psz_mode ) ) == NULL )
124
    {
125
        msg_Err( p_demux, "cannot create `%s' for writing", p_sys->psz_file );
126

127 128
        free( p_sys );
        return VLC_EGENERIC;
129
    }
130 131
    msg_Info( p_demux, "%s raw stream to file `%s'",
              b_append ? "appending" : "dumping", p_sys->psz_file );
132

Gildas Bazin's avatar
 
Gildas Bazin committed
133
    return VLC_SUCCESS;
134 135 136
}

/*****************************************************************************
137
 * Close:
138
 *****************************************************************************/
139
static void Close( vlc_object_t *p_this )
140
{
141

142 143
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys = p_demux->p_sys;
144

145 146 147 148
    msg_Info( p_demux ,"closing %s ("I64Fd" Kbytes dumped)", p_sys->psz_file,
              p_sys->i_write / 1024 );

    if( p_sys->p_file != stdout )
149
    {
150
        fclose( p_sys->p_file );
151
    }
152 153 154
    free( p_sys->psz_file );

    free( p_sys );
155 156 157 158 159 160 161
}

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

166
    int i_data;
167

168 169 170 171
    /* 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;
172

173
    i_data = fwrite( p_sys->buffer, 1, i_data, p_sys->p_file );
174

175
    if( i_data == 0 )
176
    {
177 178
        msg_Err( p_demux, "failed to write data" );
        return -1;
179
    }
180
#if 0
181
    msg_Dbg( p_demux, "dumped %d bytes", i_data );
182
#endif
183

184
    p_sys->i_write += i_data;
185

186 187
    return 1;
}
188

189 190 191 192 193 194
/*****************************************************************************
 * 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 );
195
}
196