demuxdump.c 6.36 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
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

28 29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32 33
#include <errno.h>

34
#include <vlc_common.h>
35
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
36 37
#include <vlc_demux.h>
#include <vlc_charset.h>
38 39 40 41

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

49 50 51
static int  Open( vlc_object_t * );
static void Close ( vlc_object_t * );

52
vlc_module_begin();
53
    set_shortname("Dump");
Clément Stenac's avatar
Clément Stenac committed
54 55
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_DEMUX );
56
    set_description( N_("File dumper") );
57
    set_capability( "demux", 0 );
58
    add_file( "demuxdump-file", "stream-demux.dump", NULL, FILE_TEXT,
59
              FILE_LONGTEXT, false );
60
    add_bool( "demuxdump-append", 0, NULL, APPEND_TEXT, APPEND_LONGTEXT,
61
              false );
62
    set_callbacks( Open, Close );
63 64 65
    add_shortcut( "dump" );
vlc_module_end();

66 67 68 69 70 71 72 73 74

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

#define DUMP_BLOCKSIZE  16384

75 76
struct demux_sys_t
{
77
    char        *psz_file;
78 79 80
    FILE        *p_file;
    uint64_t    i_write;

81
    uint8_t     buffer[DUMP_BLOCKSIZE];
82 83 84 85 86 87 88
};

/*
 * Data reading functions
 */

/*****************************************************************************
89
 * Open: initializes dump structures
90
 *****************************************************************************/
91
static int Open( vlc_object_t * p_this )
92
{
93 94
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
95
    const char  *psz_mode;
96
    vlc_value_t val;
97
    bool  b_append;
98

99
    /* Accept only if forced */
100
    if( !p_demux->b_force )
Gildas Bazin's avatar
 
Gildas Bazin committed
101
        return VLC_EGENERIC;
102

103 104 105 106
    p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
    if( !p_sys )
        return VLC_ENOMEM;

107 108 109 110 111 112 113 114
    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";

115 116
    p_demux->pf_demux = Demux;
    p_demux->pf_control = Control;
117

118 119 120 121
    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' )
122
    {
123
        msg_Warn( p_demux, "no dump file name given" );
124 125
        free( p_sys->psz_file );
        free( p_sys );
Gildas Bazin's avatar
 
Gildas Bazin committed
126
        return VLC_EGENERIC;
127
    }
128

129
    if( !strcmp( p_sys->psz_file, "-" ) )
130
    {
131 132
        msg_Info( p_demux, "dumping raw stream to standard output" );
        p_sys->p_file = stdout;
133
    }
134
    else if( ( p_sys->p_file = utf8_fopen( p_sys->psz_file, psz_mode ) ) == NULL )
135
    {
136
        msg_Err( p_demux, "cannot create `%s' for writing", p_sys->psz_file );
137
        free( p_sys->psz_file );
138 139
        free( p_sys );
        return VLC_EGENERIC;
140
    }
141 142
    msg_Info( p_demux, "%s raw stream to file `%s'",
              b_append ? "appending" : "dumping", p_sys->psz_file );
143

Gildas Bazin's avatar
 
Gildas Bazin committed
144
    return VLC_SUCCESS;
145 146 147
}

/*****************************************************************************
148
 * Close:
149
 *****************************************************************************/
150
static void Close( vlc_object_t *p_this )
151
{
152

153 154
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys = p_demux->p_sys;
155

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
156
    msg_Info( p_demux ,"closing %s (%"PRId64" Kbytes dumped)", p_sys->psz_file,
157 158 159
              p_sys->i_write / 1024 );

    if( p_sys->p_file != stdout )
160
    {
161
        fclose( p_sys->p_file );
162
    }
163 164
    free( p_sys->psz_file );
    free( p_sys );
165 166 167 168 169 170 171
}

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

176
    int i_data;
177

178 179 180 181
    /* 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;
182

183
    i_data = fwrite( p_sys->buffer, 1, i_data, p_sys->p_file );
184

185
    if( i_data == 0 )
186
    {
187 188
        msg_Err( p_demux, "failed to write data" );
        return -1;
189
    }
190
#if 0
191
    msg_Dbg( p_demux, "dumped %d bytes", i_data );
192
#endif
193

194
    p_sys->i_write += i_data;
195

196 197
    return 1;
}
198

199 200 201 202 203
/*****************************************************************************
 * Demux: reads and demuxes data packets
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
204
    return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
205
}
206