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