]> git.sesse.net Git - vlc/blob - modules/demux/demuxdump.c
0d507820438adf5390c1b1b2064f7ccde1497fae
[vlc] / modules / demux / demuxdump.c
1 /*****************************************************************************
2  * demuxdump.c : Pseudo demux module for vlc (dump raw stream)
3  *****************************************************************************
4  * Copyright (C) 2001-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <errno.h>
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc/vlc.h>
34 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
36 #include <vlc_charset.h>
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 #define FILE_TEXT N_("Dump filename")
42 #define FILE_LONGTEXT N_( \
43     "Name of the file to which the raw stream will be dumped." )
44 #define APPEND_TEXT N_("Append to existing file")
45 #define APPEND_LONGTEXT N_( \
46     "If the file already exists, it will not be overwritten." )
47
48 static int  Open( vlc_object_t * );
49 static void Close ( vlc_object_t * );
50
51 vlc_module_begin();
52     set_shortname("Dump");
53     set_category( CAT_INPUT );
54     set_subcategory( SUBCAT_INPUT_DEMUX );
55     set_description( _("File dumper") );
56     set_capability( "demux", 0 );
57     add_file( "demuxdump-file", "stream-demux.dump", NULL, FILE_TEXT,
58               FILE_LONGTEXT, false );
59         change_unsafe();
60     add_bool( "demuxdump-append", 0, NULL, APPEND_TEXT, APPEND_LONGTEXT,
61               false );
62     set_callbacks( Open, Close );
63     add_shortcut( "dump" );
64 vlc_module_end();
65
66
67 /*****************************************************************************
68  * Local prototypes
69  *****************************************************************************/
70 static int Demux( demux_t * );
71 static int Control( demux_t *, int,va_list );
72
73 #define DUMP_BLOCKSIZE  16384
74
75 struct demux_sys_t
76 {
77     char        *psz_file;
78     FILE        *p_file;
79     uint64_t    i_write;
80
81     uint8_t     buffer[DUMP_BLOCKSIZE];
82 };
83
84 /*
85  * Data reading functions
86  */
87
88 /*****************************************************************************
89  * Open: initializes dump structures
90  *****************************************************************************/
91 static int Open( vlc_object_t * p_this )
92 {
93     demux_t     *p_demux = (demux_t*)p_this;
94     demux_sys_t *p_sys;
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 )
101         return VLC_EGENERIC;
102
103     var_Create( p_demux, "demuxdump-append", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
104     var_Get( p_demux, "demuxdump-append", &val );
105     b_append = val.b_bool;
106     if ( b_append )
107         psz_mode = "ab";
108     else
109         psz_mode = "wb";
110
111     p_demux->pf_demux = Demux;
112     p_demux->pf_control = Control;
113     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
114     p_sys->i_write = 0;
115     p_sys->p_file = NULL;
116     p_sys->psz_file = var_CreateGetString( p_demux, "demuxdump-file" );
117     if( *p_sys->psz_file == '\0' )
118     {
119         msg_Warn( p_demux, "no dump file name given" );
120         return VLC_EGENERIC;
121     }
122
123     if( !strcmp( p_sys->psz_file, "-" ) )
124     {
125         msg_Info( p_demux, "dumping raw stream to standard output" );
126         p_sys->p_file = stdout;
127     }
128     else if( ( p_sys->p_file = utf8_fopen( p_sys->psz_file, psz_mode ) ) == NULL )
129     {
130         msg_Err( p_demux, "cannot create `%s' for writing", p_sys->psz_file );
131
132         free( p_sys );
133         return VLC_EGENERIC;
134     }
135     msg_Info( p_demux, "%s raw stream to file `%s'",
136               b_append ? "appending" : "dumping", p_sys->psz_file );
137
138     return VLC_SUCCESS;
139 }
140
141 /*****************************************************************************
142  * Close:
143  *****************************************************************************/
144 static void Close( vlc_object_t *p_this )
145 {
146
147     demux_t     *p_demux = (demux_t*)p_this;
148     demux_sys_t *p_sys = p_demux->p_sys;
149
150     msg_Info( p_demux ,"closing %s (%"PRId64" Kbytes dumped)", p_sys->psz_file,
151               p_sys->i_write / 1024 );
152
153     if( p_sys->p_file != stdout )
154     {
155         fclose( p_sys->p_file );
156     }
157     free( p_sys->psz_file );
158
159     free( p_sys );
160 }
161
162 /*****************************************************************************
163  * Demux: reads and demuxes data packets
164  *****************************************************************************
165  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
166  *****************************************************************************/
167 static int Demux( demux_t *p_demux )
168 {
169     demux_sys_t *p_sys = p_demux->p_sys;
170
171     int i_data;
172
173     /* I'm pretty sure that stream_Peek,stream_Read( , NULL ) would be faster*/
174     i_data = stream_Read( p_demux->s, p_sys->buffer, DUMP_BLOCKSIZE );
175     if ( i_data <= 0 )
176         return i_data;
177
178     i_data = fwrite( p_sys->buffer, 1, i_data, p_sys->p_file );
179
180     if( i_data == 0 )
181     {
182         msg_Err( p_demux, "failed to write data" );
183         return -1;
184     }
185 #if 0
186     msg_Dbg( p_demux, "dumped %d bytes", i_data );
187 #endif
188
189     p_sys->i_write += i_data;
190
191     return 1;
192 }
193
194 /*****************************************************************************
195  * Demux: reads and demuxes data packets
196  *****************************************************************************/
197 static int Control( demux_t *p_demux, int i_query, va_list args )
198 {
199     return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
200 }
201