]> git.sesse.net Git - vlc/blob - modules/demux/demuxdump.c
* all : fixed some memory leaks thanks valgrind.
[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.3 2003/01/25 16:58:34 fenrir 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 vlc_module_begin();
49     set_description( _("Dump Demux input") );
50     set_capability( "demux", 0 );
51     add_category_hint( "File", NULL );
52         add_string( "demuxdump-file", NULL, NULL, 
53                     "dump file name", 
54                     "file name for dumping raw stream read by demux" );
55     set_callbacks( Activate, Desactivate );
56     add_shortcut( "dump" );
57 vlc_module_end();
58
59 struct demux_sys_t
60 {
61     char        *psz_name;
62     FILE        *p_file;
63     uint64_t    i_write;
64
65     void        *p_demux_data_sav;
66 };
67
68 /*
69  * Data reading functions
70  */
71
72 /*****************************************************************************
73  * Activate: initializes dump structures
74  *****************************************************************************/
75 static int Activate( vlc_object_t * p_this )
76 {
77     input_thread_t      *p_input = (input_thread_t *)p_this;
78     demux_sys_t         *p_demux;
79
80     char                *psz_name;
81
82     /* Set the demux function */
83     p_input->pf_demux = Demux;
84
85     /* Initialize access plug-in structures. */
86     if( p_input->i_mtu == 0 )
87     {
88         /* Improve speed. */
89         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
90     }
91     
92     psz_name = config_GetPsz( p_input, "demuxdump-file" );
93     if( !psz_name || !*psz_name )
94     {
95         psz_name = strdup( "stream-demux.dump" );
96     }
97
98     p_demux = malloc( sizeof( demux_sys_t ) );
99     memset( p_demux, 0, sizeof( demux_sys_t ) );
100
101     if( !( p_demux->p_file = fopen( psz_name, "wb" ) ) )
102     {
103         msg_Err( p_input,
104                  "cannot create `%s' for writing", 
105                  psz_name );
106         free( p_demux );
107         return( -1 );
108     }
109     else
110     {
111         msg_Info( p_input,
112                   "dumping raw stream to file `%s'", 
113                   psz_name );
114         p_demux->psz_name = psz_name;
115     }
116
117     p_demux->i_write = 0;
118     p_demux->p_demux_data_sav = p_input->p_demux_data;
119
120     if( p_input->stream.p_selected_program != NULL )
121     {
122         /* workaround for dvd access */
123         msg_Warn( p_input, "demux data already initializated (by access?)" );
124     }
125     else
126     {
127
128         if( input_InitStream( p_input, 0 ) == -1 )
129         {
130             fclose( p_demux->p_file );
131             free( p_demux );
132             return( -1 );
133         }
134         input_AddProgram( p_input, 0, 0 );
135         p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
136
137         vlc_mutex_lock( &p_input->stream.stream_lock );
138         p_input->stream.p_selected_area->i_tell = 0;
139         vlc_mutex_unlock( &p_input->stream.stream_lock );
140     }
141
142     p_input->p_demux_data = p_demux;
143
144     vlc_mutex_lock( &p_input->stream.stream_lock );
145     p_input->stream.p_selected_program->b_is_ok = 1;
146     vlc_mutex_unlock( &p_input->stream.stream_lock );
147     
148     return( 0 );
149 }
150
151 /*****************************************************************************
152  * Desctivate: initializes dump structures
153  *****************************************************************************/
154 static void Desactivate ( vlc_object_t *p_this )
155 {
156     input_thread_t      *p_input = (input_thread_t *)p_this;
157     demux_sys_t         *p_demux = (demux_sys_t*)p_input->p_demux_data;
158
159     msg_Info( p_input,
160               "closing %s ("I64Fd" Kbytes dumped)",
161               p_demux->psz_name,
162               p_demux->i_write / 1024 );
163
164     if( p_demux->p_file )
165     {
166         fclose( p_demux->p_file );
167         p_demux->p_file = NULL;
168     }
169     if( p_demux->psz_name )
170     {
171         free( p_demux->psz_name );
172     }
173     p_input->p_demux_data = p_demux->p_demux_data_sav;
174     free( p_demux );
175 }
176
177 /*****************************************************************************
178  * Demux: reads and demuxes data packets
179  *****************************************************************************
180  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
181  *****************************************************************************/
182 static int Demux( input_thread_t * p_input )
183 {
184     demux_sys_t         *p_demux = (demux_sys_t*)p_input->p_demux_data;
185
186     ssize_t         i_read;
187     data_packet_t * p_data;
188     int             i_write;
189
190     p_input->p_demux_data = p_demux->p_demux_data_sav;
191     i_read = input_SplitBuffer( p_input, &p_data, DUMP_BLOCKSIZE );
192     p_input->p_demux_data = p_demux;
193     
194     if ( i_read <= 0 )
195     {
196         return i_read;
197     }
198
199     i_write = fwrite( p_data->p_payload_start,
200                        1,
201                        i_read,
202                        p_demux->p_file );
203     
204     input_DeletePacket( p_input->p_method_data, p_data );
205
206     if( i_write < 0 )
207     {
208         msg_Err( p_input, 
209                  "failed to write %d bytes",
210                  i_write );
211         return( -1 );
212     }
213     else
214     {
215         msg_Dbg( p_input,
216                  "dumped %d bytes",
217                  i_write );
218         p_demux->i_write += i_write;
219     }
220
221
222     if( (p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT)
223          | (input_ClockManageControl( p_input, 
224                       p_input->stream.p_selected_program,
225                          (mtime_t)0 ) == PAUSE_S) )
226     {
227         msg_Warn( p_input, "synchro reinit" );
228         p_input->stream.p_selected_program->i_synchro_state = SYNCHRO_OK;
229     }
230
231     return( 1 );
232 }
233