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