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