]> git.sesse.net Git - vlc/blob - modules/demux/demuxdump.c
* src/input/input.c: honor the --demux config option (will be overriden if demux...
[vlc] / modules / demux / demuxdump.c
1 /*****************************************************************************
2  * demuxdump.c : Pseudo demux module for vlc (dump raw stream)
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: demuxdump.c,v 1.11 2003/11/05 17:57:29 gbazin Exp $
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 #include <sys/types.h>
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static int  Activate ( vlc_object_t * );
40 static int  Demux ( input_thread_t * );
41 static void Desactivate ( vlc_object_t * );
42
43 #define DUMP_BLOCKSIZE  16384
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48 #define FILE_TEXT N_("Dump file name")
49 #define FILE_LONGTEXT N_( \
50     "Specify a file name to which the raw stream will be dumped." )
51
52 vlc_module_begin();
53     set_description( _("file dump demuxer") );
54     set_capability( "demux", 0 );
55     add_category_hint( "File", NULL, VLC_FALSE );
56         add_file( "demuxdump-file", "stream-demux.dump", NULL, FILE_TEXT, 
57                   FILE_LONGTEXT, VLC_FALSE );
58     set_callbacks( Activate, Desactivate );
59     add_shortcut( "dump" );
60 vlc_module_end();
61
62 struct demux_sys_t
63 {
64     char        *psz_name;
65     FILE        *p_file;
66     uint64_t    i_write;
67
68     void        *p_demux_data_sav;
69 };
70
71 /*
72  * Data reading functions
73  */
74
75 /*****************************************************************************
76  * Activate: initializes dump structures
77  *****************************************************************************/
78 static int Activate( vlc_object_t * p_this )
79 {
80     input_thread_t      *p_input = (input_thread_t *)p_this;
81     demux_sys_t         *p_demux;
82     vlc_value_t         val;
83     char                *psz_name;
84
85     /* Set the demux function */
86     p_input->pf_demux = Demux;
87     p_input->pf_demux_control = demux_vaControlDefault;
88
89     /* Initialize access plug-in structures. */
90     if( p_input->i_mtu == 0 )
91     {
92         /* Improve speed. */
93         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
94     }
95     
96     var_Create( p_input, "demuxdump-file", VLC_VAR_FILE|VLC_VAR_DOINHERIT );
97     var_Get( p_input, "demuxdump-file", &val );
98     psz_name = val.psz_string;
99     if( !psz_name || !*psz_name )
100     {
101         msg_Warn( p_input, "no dump file name given" );
102         return VLC_EGENERIC;
103     }
104
105     p_demux = malloc( sizeof( demux_sys_t ) );
106     memset( p_demux, 0, sizeof( demux_sys_t ) );
107
108     if( !strcmp( psz_name, "-" ) )
109     {
110         msg_Info( p_input,
111                   "dumping raw stream to standard output" );
112         p_demux->p_file = stdout;
113         p_demux->psz_name = psz_name;
114     }
115     else if( !( p_demux->p_file = fopen( psz_name, "wb" ) ) )
116     {
117         msg_Err( p_input,
118                  "cannot create `%s' for writing", 
119                  psz_name );
120         free( p_demux );
121         return VLC_EGENERIC;
122     }
123     else
124     {
125         msg_Info( p_input,
126                   "dumping raw stream to file `%s'", 
127                   psz_name );
128         p_demux->psz_name = psz_name;
129     }
130
131     p_demux->i_write = 0;
132     p_demux->p_demux_data_sav = p_input->p_demux_data;
133
134     if( p_input->stream.p_selected_program != NULL )
135     {
136         /* workaround for dvd access */
137         msg_Warn( p_input, "demux data already initializated (by access?)" );
138     }
139     else
140     {
141
142         if( input_InitStream( p_input, 0 ) == -1 )
143         {
144             if( p_demux->p_file != stdout )
145                 fclose( p_demux->p_file );
146             free( p_demux );
147             return VLC_EGENERIC;
148         }
149         input_AddProgram( p_input, 0, 0 );
150         p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
151
152         vlc_mutex_lock( &p_input->stream.stream_lock );
153         p_input->stream.p_selected_area->i_tell = 0;
154         vlc_mutex_unlock( &p_input->stream.stream_lock );
155     }
156
157     p_input->p_demux_data = p_demux;
158
159     vlc_mutex_lock( &p_input->stream.stream_lock );
160     p_input->stream.p_selected_program->b_is_ok = 1;
161     vlc_mutex_unlock( &p_input->stream.stream_lock );
162     
163     return VLC_SUCCESS;
164 }
165
166 /*****************************************************************************
167  * Desctivate: initializes dump structures
168  *****************************************************************************/
169 static void Desactivate ( vlc_object_t *p_this )
170 {
171     input_thread_t      *p_input = (input_thread_t *)p_this;
172     demux_sys_t         *p_demux = (demux_sys_t*)p_input->p_demux_data;
173
174     msg_Info( p_input,
175               "closing %s ("I64Fd" Kbytes dumped)",
176               p_demux->psz_name,
177               p_demux->i_write / 1024 );
178
179     if( p_demux->p_file )
180     {
181         if( p_demux->p_file != stdout )
182             fclose( p_demux->p_file );
183         p_demux->p_file = NULL;
184     }
185     if( p_demux->psz_name )
186     {
187         free( p_demux->psz_name );
188     }
189     p_input->p_demux_data = p_demux->p_demux_data_sav;
190     free( p_demux );
191 }
192
193 /*****************************************************************************
194  * Demux: reads and demuxes data packets
195  *****************************************************************************
196  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
197  *****************************************************************************/
198 static int Demux( input_thread_t * p_input )
199 {
200     demux_sys_t         *p_demux = (demux_sys_t*)p_input->p_demux_data;
201
202     ssize_t         i_read;
203     data_packet_t * p_data;
204     int             i_write;
205
206     p_input->p_demux_data = p_demux->p_demux_data_sav;
207     i_read = input_SplitBuffer( p_input, &p_data, DUMP_BLOCKSIZE );
208     p_input->p_demux_data = p_demux;
209     
210     if ( i_read <= 0 )
211     {
212         return i_read;
213     }
214
215     i_write = fwrite( p_data->p_payload_start,
216                        1,
217                        i_read,
218                        p_demux->p_file );
219     
220     input_DeletePacket( p_input->p_method_data, p_data );
221
222     if( i_write < 0 )
223     {
224         msg_Err( p_input, 
225                  "failed to write %d bytes",
226                  i_write );
227         return( -1 );
228     }
229     else
230     {
231         msg_Dbg( p_input,
232                  "dumped %d bytes",
233                  i_write );
234         p_demux->i_write += i_write;
235     }
236
237
238     if( (p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT)
239          | (input_ClockManageControl( p_input, 
240                       p_input->stream.p_selected_program,
241                          (mtime_t)0 ) == PAUSE_S) )
242     {
243         msg_Warn( p_input, "synchro reinit" );
244         p_input->stream.p_selected_program->i_synchro_state = SYNCHRO_OK;
245     }
246
247     return( 1 );
248 }