]> git.sesse.net Git - vlc/blob - modules/demux/demuxdump.c
* include/configuration.h: added the add_directory() config macro.
[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.7 2003/03/30 14:24:20 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( _("Dump Demux input") );
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
83     char                *psz_name;
84
85     /* Set the demux function */
86     p_input->pf_demux = Demux;
87
88     /* Initialize access plug-in structures. */
89     if( p_input->i_mtu == 0 )
90     {
91         /* Improve speed. */
92         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
93     }
94     
95     psz_name = config_GetPsz( p_input, "demuxdump-file" );
96     if( !psz_name || !*psz_name )
97     {
98         msg_Warn( p_input, "no dump file name given" );
99         return VLC_EGENERIC;
100     }
101
102     p_demux = malloc( sizeof( demux_sys_t ) );
103     memset( p_demux, 0, sizeof( demux_sys_t ) );
104
105     if( !strcmp( psz_name, "-" ) )
106     {
107         msg_Info( p_input,
108                   "dumping raw stream to standard output" );
109         p_demux->p_file = stdout;
110         p_demux->psz_name = psz_name;
111     }
112     else if( !( p_demux->p_file = fopen( psz_name, "wb" ) ) )
113     {
114         msg_Err( p_input,
115                  "cannot create `%s' for writing", 
116                  psz_name );
117         free( p_demux );
118         return VLC_EGENERIC;
119     }
120     else
121     {
122         msg_Info( p_input,
123                   "dumping raw stream to file `%s'", 
124                   psz_name );
125         p_demux->psz_name = psz_name;
126     }
127
128     p_demux->i_write = 0;
129     p_demux->p_demux_data_sav = p_input->p_demux_data;
130
131     if( p_input->stream.p_selected_program != NULL )
132     {
133         /* workaround for dvd access */
134         msg_Warn( p_input, "demux data already initializated (by access?)" );
135     }
136     else
137     {
138
139         if( input_InitStream( p_input, 0 ) == -1 )
140         {
141             if( p_demux->p_file != stdout )
142                 fclose( p_demux->p_file );
143             free( p_demux );
144             return VLC_EGENERIC;
145         }
146         input_AddProgram( p_input, 0, 0 );
147         p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
148
149         vlc_mutex_lock( &p_input->stream.stream_lock );
150         p_input->stream.p_selected_area->i_tell = 0;
151         vlc_mutex_unlock( &p_input->stream.stream_lock );
152     }
153
154     p_input->p_demux_data = p_demux;
155
156     vlc_mutex_lock( &p_input->stream.stream_lock );
157     p_input->stream.p_selected_program->b_is_ok = 1;
158     vlc_mutex_unlock( &p_input->stream.stream_lock );
159     
160     return VLC_SUCCESS;
161 }
162
163 /*****************************************************************************
164  * Desctivate: initializes dump structures
165  *****************************************************************************/
166 static void Desactivate ( vlc_object_t *p_this )
167 {
168     input_thread_t      *p_input = (input_thread_t *)p_this;
169     demux_sys_t         *p_demux = (demux_sys_t*)p_input->p_demux_data;
170
171     msg_Info( p_input,
172               "closing %s ("I64Fd" Kbytes dumped)",
173               p_demux->psz_name,
174               p_demux->i_write / 1024 );
175
176     if( p_demux->p_file )
177     {
178         if( p_demux->p_file != stdout )
179             fclose( p_demux->p_file );
180         p_demux->p_file = NULL;
181     }
182     if( p_demux->psz_name )
183     {
184         free( p_demux->psz_name );
185     }
186     p_input->p_demux_data = p_demux->p_demux_data_sav;
187     free( p_demux );
188 }
189
190 /*****************************************************************************
191  * Demux: reads and demuxes data packets
192  *****************************************************************************
193  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
194  *****************************************************************************/
195 static int Demux( input_thread_t * p_input )
196 {
197     demux_sys_t         *p_demux = (demux_sys_t*)p_input->p_demux_data;
198
199     ssize_t         i_read;
200     data_packet_t * p_data;
201     int             i_write;
202
203     p_input->p_demux_data = p_demux->p_demux_data_sav;
204     i_read = input_SplitBuffer( p_input, &p_data, DUMP_BLOCKSIZE );
205     p_input->p_demux_data = p_demux;
206     
207     if ( i_read <= 0 )
208     {
209         return i_read;
210     }
211
212     i_write = fwrite( p_data->p_payload_start,
213                        1,
214                        i_read,
215                        p_demux->p_file );
216     
217     input_DeletePacket( p_input->p_method_data, p_data );
218
219     if( i_write < 0 )
220     {
221         msg_Err( p_input, 
222                  "failed to write %d bytes",
223                  i_write );
224         return( -1 );
225     }
226     else
227     {
228         msg_Dbg( p_input,
229                  "dumped %d bytes",
230                  i_write );
231         p_demux->i_write += i_write;
232     }
233
234
235     if( (p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT)
236          | (input_ClockManageControl( p_input, 
237                       p_input->stream.p_selected_program,
238                          (mtime_t)0 ) == PAUSE_S) )
239     {
240         msg_Warn( p_input, "synchro reinit" );
241         p_input->stream.p_selected_program->i_synchro_state = SYNCHRO_OK;
242     }
243
244     return( 1 );
245 }