1 /*****************************************************************************
2 * demuxdump.c : Pseudo demux module for vlc (dump raw stream)
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: demuxdump.c,v 1.12 2004/01/25 20:05:28 hartman Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <string.h> /* strdup() */
32 #include <vlc/input.h>
34 #include <sys/types.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Activate ( vlc_object_t * );
40 static int Demux ( input_thread_t * );
41 static void Deactivate ( vlc_object_t * );
43 #define DUMP_BLOCKSIZE 16384
45 /*****************************************************************************
47 *****************************************************************************/
48 #define FILE_TEXT N_("Filename of dump")
49 #define FILE_LONGTEXT N_( \
50 "Specify a file name to which the raw stream will be dumped." )
53 set_description( _("Filedump demuxer") );
54 set_capability( "demux", 0 );
55 add_file( "demuxdump-file", "stream-demux.dump", NULL, FILE_TEXT,
56 FILE_LONGTEXT, VLC_FALSE );
57 set_callbacks( Activate, Deactivate );
58 add_shortcut( "dump" );
67 void *p_demux_data_sav;
71 * Data reading functions
74 /*****************************************************************************
75 * Activate: initializes dump structures
76 *****************************************************************************/
77 static int Activate( vlc_object_t * p_this )
79 input_thread_t *p_input = (input_thread_t *)p_this;
84 /* Set the demux function */
85 p_input->pf_demux = Demux;
86 p_input->pf_demux_control = demux_vaControlDefault;
88 /* Initialize access plug-in structures. */
89 if( p_input->i_mtu == 0 )
92 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
95 var_Create( p_input, "demuxdump-file", VLC_VAR_FILE|VLC_VAR_DOINHERIT );
96 var_Get( p_input, "demuxdump-file", &val );
97 psz_name = val.psz_string;
98 if( !psz_name || !*psz_name )
100 msg_Warn( p_input, "no dump file name given" );
104 p_demux = malloc( sizeof( demux_sys_t ) );
105 memset( p_demux, 0, sizeof( demux_sys_t ) );
107 if( !strcmp( psz_name, "-" ) )
110 "dumping raw stream to standard output" );
111 p_demux->p_file = stdout;
112 p_demux->psz_name = psz_name;
114 else if( !( p_demux->p_file = fopen( psz_name, "wb" ) ) )
117 "cannot create `%s' for writing",
125 "dumping raw stream to file `%s'",
127 p_demux->psz_name = psz_name;
130 p_demux->i_write = 0;
131 p_demux->p_demux_data_sav = p_input->p_demux_data;
133 if( p_input->stream.p_selected_program != NULL )
135 /* workaround for dvd access */
136 msg_Warn( p_input, "demux data already initializated (by access?)" );
141 if( input_InitStream( p_input, 0 ) == -1 )
143 if( p_demux->p_file != stdout )
144 fclose( p_demux->p_file );
148 input_AddProgram( p_input, 0, 0 );
149 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
151 vlc_mutex_lock( &p_input->stream.stream_lock );
152 p_input->stream.p_selected_area->i_tell = 0;
153 vlc_mutex_unlock( &p_input->stream.stream_lock );
156 p_input->p_demux_data = p_demux;
158 vlc_mutex_lock( &p_input->stream.stream_lock );
159 p_input->stream.p_selected_program->b_is_ok = 1;
160 vlc_mutex_unlock( &p_input->stream.stream_lock );
165 /*****************************************************************************
166 * Deactivate: initializes dump structures
167 *****************************************************************************/
168 static void Deactivate ( vlc_object_t *p_this )
170 input_thread_t *p_input = (input_thread_t *)p_this;
171 demux_sys_t *p_demux = (demux_sys_t*)p_input->p_demux_data;
174 "closing %s ("I64Fd" Kbytes dumped)",
176 p_demux->i_write / 1024 );
178 if( p_demux->p_file )
180 if( p_demux->p_file != stdout )
181 fclose( p_demux->p_file );
182 p_demux->p_file = NULL;
184 if( p_demux->psz_name )
186 free( p_demux->psz_name );
188 p_input->p_demux_data = p_demux->p_demux_data_sav;
192 /*****************************************************************************
193 * Demux: reads and demuxes data packets
194 *****************************************************************************
195 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
196 *****************************************************************************/
197 static int Demux( input_thread_t * p_input )
199 demux_sys_t *p_demux = (demux_sys_t*)p_input->p_demux_data;
202 data_packet_t * p_data;
205 p_input->p_demux_data = p_demux->p_demux_data_sav;
206 i_read = input_SplitBuffer( p_input, &p_data, DUMP_BLOCKSIZE );
207 p_input->p_demux_data = p_demux;
214 i_write = fwrite( p_data->p_payload_start,
219 input_DeletePacket( p_input->p_method_data, p_data );
224 "failed to write %d bytes",
233 p_demux->i_write += i_write;
237 if( (p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT)
238 | (input_ClockManageControl( p_input,
239 p_input->stream.p_selected_program,
240 (mtime_t)0 ) == PAUSE_S) )
242 msg_Warn( p_input, "synchro reinit" );
243 p_input->stream.p_selected_program->i_synchro_state = SYNCHRO_OK;