]> git.sesse.net Git - vlc/blob - modules/demux/demuxdump.c
Added const wheen needed for stream_Peek (demuxer/access)
[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 #include <vlc/vlc.h>
30 #include <vlc_demux.h>
31 #include <vlc_charset.h>
32
33 /*****************************************************************************
34  * Module descriptor
35  *****************************************************************************/
36 #define FILE_TEXT N_("Dump filename")
37 #define FILE_LONGTEXT N_( \
38     "Name of the file to which the raw stream will be dumped." )
39 #define APPEND_TEXT N_("Append to existing file")
40 #define APPEND_LONGTEXT N_( \
41     "If the file already exists, it will not be overwritten." )
42
43 static int  Open( vlc_object_t * );
44 static void Close ( vlc_object_t * );
45
46 vlc_module_begin();
47     set_shortname("Dump");
48     set_category( CAT_INPUT );
49     set_subcategory( SUBCAT_INPUT_DEMUX );
50     set_description( _("File dumper") );
51     set_capability( "demux2", 0 );
52     add_file( "demuxdump-file", "stream-demux.dump", NULL, FILE_TEXT,
53               FILE_LONGTEXT, VLC_FALSE );
54     add_bool( "demuxdump-append", 0, NULL, APPEND_TEXT, APPEND_LONGTEXT,
55               VLC_FALSE );
56     set_callbacks( Open, Close );
57     add_shortcut( "dump" );
58 vlc_module_end();
59
60
61 /*****************************************************************************
62  * Local prototypes
63  *****************************************************************************/
64 static int Demux( demux_t * );
65 static int Control( demux_t *, int,va_list );
66
67 #define DUMP_BLOCKSIZE  16384
68
69 struct demux_sys_t
70 {
71     char        *psz_file;
72     FILE        *p_file;
73     uint64_t    i_write;
74
75     uint8_t     buffer[DUMP_BLOCKSIZE];
76 };
77
78 /*
79  * Data reading functions
80  */
81
82 /*****************************************************************************
83  * Open: initializes dump structures
84  *****************************************************************************/
85 static int Open( vlc_object_t * p_this )
86 {
87     demux_t     *p_demux = (demux_t*)p_this;
88     demux_sys_t *p_sys;
89     const char  *psz_mode;
90     vlc_value_t val;
91     vlc_bool_t  b_append;
92
93     /* Accept only if forced */
94     if( !p_demux->b_force )
95         return VLC_EGENERIC;
96
97     var_Create( p_demux, "demuxdump-append", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
98     var_Get( p_demux, "demuxdump-append", &val );
99     b_append = val.b_bool;
100     if ( b_append )
101         psz_mode = "ab";
102     else
103         psz_mode = "wb";
104
105     p_demux->pf_demux = Demux;
106     p_demux->pf_control = Control;
107     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
108     p_sys->i_write = 0;
109     p_sys->p_file = NULL;
110     p_sys->psz_file = var_CreateGetString( p_demux, "demuxdump-file" );
111     if( *p_sys->psz_file == '\0' )
112     {
113         msg_Warn( p_demux, "no dump file name given" );
114         return VLC_EGENERIC;
115     }
116
117     if( !strcmp( p_sys->psz_file, "-" ) )
118     {
119         msg_Info( p_demux, "dumping raw stream to standard output" );
120         p_sys->p_file = stdout;
121     }
122     else if( ( p_sys->p_file = utf8_fopen( p_sys->psz_file, psz_mode ) ) == NULL )
123     {
124         msg_Err( p_demux, "cannot create `%s' for writing", p_sys->psz_file );
125
126         free( p_sys );
127         return VLC_EGENERIC;
128     }
129     msg_Info( p_demux, "%s raw stream to file `%s'",
130               b_append ? "appending" : "dumping", p_sys->psz_file );
131
132     return VLC_SUCCESS;
133 }
134
135 /*****************************************************************************
136  * Close:
137  *****************************************************************************/
138 static void Close( vlc_object_t *p_this )
139 {
140
141     demux_t     *p_demux = (demux_t*)p_this;
142     demux_sys_t *p_sys = p_demux->p_sys;
143
144     msg_Info( p_demux ,"closing %s ("I64Fd" Kbytes dumped)", p_sys->psz_file,
145               p_sys->i_write / 1024 );
146
147     if( p_sys->p_file != stdout )
148     {
149         fclose( p_sys->p_file );
150     }
151     free( p_sys->psz_file );
152
153     free( p_sys );
154 }
155
156 /*****************************************************************************
157  * Demux: reads and demuxes data packets
158  *****************************************************************************
159  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
160  *****************************************************************************/
161 static int Demux( demux_t *p_demux )
162 {
163     demux_sys_t *p_sys = p_demux->p_sys;
164
165     int i_data;
166
167     /* I'm pretty sure that stream_Peek,stream_Read( , NULL ) would be faster*/
168     i_data = stream_Read( p_demux->s, p_sys->buffer, DUMP_BLOCKSIZE );
169     if ( i_data <= 0 )
170         return i_data;
171
172     i_data = fwrite( p_sys->buffer, 1, i_data, p_sys->p_file );
173
174     if( i_data == 0 )
175     {
176         msg_Err( p_demux, "failed to write data" );
177         return -1;
178     }
179 #if 0
180     msg_Dbg( p_demux, "dumped %d bytes", i_data );
181 #endif
182
183     p_sys->i_write += i_data;
184
185     return 1;
186 }
187
188 /*****************************************************************************
189  * Demux: reads and demuxes data packets
190  *****************************************************************************/
191 static int Control( demux_t *p_demux, int i_query, va_list args )
192 {
193     return demux2_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
194 }
195