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