1 /*****************************************************************************
2 * demuxdump.c : Pseudo demux module for vlc (dump raw stream)
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <string.h> /* strdup() */
32 #include <vlc_demux.h>
33 #include <vlc_charset.h>
35 /*****************************************************************************
37 *****************************************************************************/
38 #define FILE_TEXT N_("Dump filename")
39 #define FILE_LONGTEXT N_( \
40 "Name of the file to which the raw stream will be dumped." )
41 #define APPEND_TEXT N_("Append to existing file")
42 #define APPEND_LONGTEXT N_( \
43 "If the file already exists, it will not be overwritten." )
45 static int Open( vlc_object_t
* );
46 static void Close ( vlc_object_t
* );
49 set_shortname("Dump");
50 set_category( CAT_INPUT
);
51 set_subcategory( SUBCAT_INPUT_DEMUX
);
52 set_description( _("File dumpper") );
53 set_capability( "demux2", 0 );
54 add_file( "demuxdump-file", "stream-demux.dump", NULL
, FILE_TEXT
,
55 FILE_LONGTEXT
, VLC_FALSE
);
56 add_bool( "demuxdump-append", 0, NULL
, APPEND_TEXT
, APPEND_LONGTEXT
,
58 set_callbacks( Open
, Close
);
59 add_shortcut( "dump" );
63 /*****************************************************************************
65 *****************************************************************************/
66 static int Demux( demux_t
* );
67 static int Control( demux_t
*, int,va_list );
69 #define DUMP_BLOCKSIZE 16384
77 uint8_t buffer
[DUMP_BLOCKSIZE
];
81 * Data reading functions
84 /*****************************************************************************
85 * Open: initializes dump structures
86 *****************************************************************************/
87 static int Open( vlc_object_t
* p_this
)
89 demux_t
*p_demux
= (demux_t
*)p_this
;
95 /* Accept only if forced */
96 if( strcasecmp( p_demux
->psz_demux
, "dump" ) )
99 var_Create( p_demux
, "demuxdump-append", VLC_VAR_BOOL
|VLC_VAR_DOINHERIT
);
100 var_Get( p_demux
, "demuxdump-append", &val
);
101 b_append
= val
.b_bool
;
107 p_demux
->pf_demux
= Demux
;
108 p_demux
->pf_control
= Control
;
109 p_demux
->p_sys
= p_sys
= malloc( sizeof( demux_sys_t
) );
111 p_sys
->p_file
= NULL
;
112 p_sys
->psz_file
= var_CreateGetString( p_demux
, "demuxdump-file" );
113 if( *p_sys
->psz_file
== '\0' )
115 msg_Warn( p_demux
, "no dump file name given" );
119 if( !strcmp( p_sys
->psz_file
, "-" ) )
121 msg_Info( p_demux
, "dumping raw stream to standard output" );
122 p_sys
->p_file
= stdout
;
124 else if( ( p_sys
->p_file
= utf8_fopen( p_sys
->psz_file
, psz_mode
) ) == NULL
)
126 msg_Err( p_demux
, "cannot create `%s' for writing", p_sys
->psz_file
);
131 msg_Info( p_demux
, "%s raw stream to file `%s'",
132 b_append
? "appending" : "dumping", p_sys
->psz_file
);
137 /*****************************************************************************
139 *****************************************************************************/
140 static void Close( vlc_object_t
*p_this
)
143 demux_t
*p_demux
= (demux_t
*)p_this
;
144 demux_sys_t
*p_sys
= p_demux
->p_sys
;
146 msg_Info( p_demux
,"closing %s ("I64Fd
" Kbytes dumped)", p_sys
->psz_file
,
147 p_sys
->i_write
/ 1024 );
149 if( p_sys
->p_file
!= stdout
)
151 fclose( p_sys
->p_file
);
153 free( p_sys
->psz_file
);
158 /*****************************************************************************
159 * Demux: reads and demuxes data packets
160 *****************************************************************************
161 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
162 *****************************************************************************/
163 static int Demux( demux_t
*p_demux
)
165 demux_sys_t
*p_sys
= p_demux
->p_sys
;
169 /* I'm pretty sure that stream_Peek,stream_Read( , NULL ) would be faster*/
170 i_data
= stream_Read( p_demux
->s
, p_sys
->buffer
, DUMP_BLOCKSIZE
);
174 i_data
= fwrite( p_sys
->buffer
, 1, i_data
, p_sys
->p_file
);
178 msg_Err( p_demux
, "failed to write data" );
182 msg_Dbg( p_demux
, "dumped %d bytes", i_data
);
185 p_sys
->i_write
+= i_data
;
190 /*****************************************************************************
191 * Demux: reads and demuxes data packets
192 *****************************************************************************/
193 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
195 return demux2_vaControlHelper( p_demux
->s
, 0, -1, 0, 1, i_query
, args
);