Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

demuxdump.c

00001 /*****************************************************************************
00002  * demuxdump.c : Pseudo demux module for vlc (dump raw stream)
00003  *****************************************************************************
00004  * Copyright (C) 2001-2004 the VideoLAN team
00005  * $Id: demuxdump.c 12236 2005-08-18 15:58:05Z massiot $
00006  *
00007  * Authors: Laurent Aimar <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Preamble
00026  *****************************************************************************/
00027 #include <stdlib.h>                                      /* malloc(), free() */
00028 #include <string.h>                                              /* strdup() */
00029 #include <errno.h>
00030 
00031 #include <vlc/vlc.h>
00032 #include <vlc/input.h>
00033 
00034 /*****************************************************************************
00035  * Module descriptor
00036  *****************************************************************************/
00037 #define FILE_TEXT N_("Filename of dump")
00038 #define FILE_LONGTEXT N_( \
00039     "Specify a file name to which the raw stream will be dumped." )
00040 #define APPEND_TEXT N_("Append")
00041 #define APPEND_LONGTEXT N_( \
00042     "If the file exists and this option is selected, the existing file " \
00043     "will not be overwritten." )
00044 
00045 static int  Open( vlc_object_t * );
00046 static void Close ( vlc_object_t * );
00047 
00048 vlc_module_begin();
00049     set_shortname("Dump");
00050     set_category( CAT_INPUT );
00051     set_subcategory( SUBCAT_INPUT_DEMUX );
00052     set_description( _("Filedump demuxer") );
00053     set_capability( "demux2", 0 );
00054     add_file( "demuxdump-file", "stream-demux.dump", NULL, FILE_TEXT,
00055               FILE_LONGTEXT, VLC_FALSE );
00056     add_bool( "demuxdump-append", 0, NULL, APPEND_TEXT, APPEND_LONGTEXT,
00057               VLC_FALSE );
00058     set_callbacks( Open, Close );
00059     add_shortcut( "dump" );
00060 vlc_module_end();
00061 
00062 
00063 /*****************************************************************************
00064  * Local prototypes
00065  *****************************************************************************/
00066 static int Demux( demux_t * );
00067 static int Control( demux_t *, int,va_list );
00068 
00069 #define DUMP_BLOCKSIZE  16384
00070 
00071 struct demux_sys_t
00072 {
00073     char        *psz_file;
00074     FILE        *p_file;
00075     uint64_t    i_write;
00076 
00077     uint8_t     buffer[DUMP_BLOCKSIZE];
00078 };
00079 
00080 /*
00081  * Data reading functions
00082  */
00083 
00084 /*****************************************************************************
00085  * Open: initializes dump structures
00086  *****************************************************************************/
00087 static int Open( vlc_object_t * p_this )
00088 {
00089     demux_t     *p_demux = (demux_t*)p_this;
00090     demux_sys_t *p_sys;
00091     char        *psz_mode;
00092     vlc_value_t val;
00093     vlc_bool_t  b_append;
00094 
00095     /* Accept only if forced */
00096     if( strcasecmp( p_demux->psz_demux, "dump" ) )
00097         return VLC_EGENERIC;
00098 
00099     var_Create( p_demux, "demuxdump-append", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
00100     var_Get( p_demux, "demuxdump-append", &val );
00101     b_append = val.b_bool;
00102     if ( b_append )
00103         psz_mode = "ab";
00104     else
00105         psz_mode = "wb";
00106 
00107     p_demux->pf_demux = Demux;
00108     p_demux->pf_control = Control;
00109     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
00110     p_sys->i_write = 0;
00111     p_sys->p_file = NULL;
00112     p_sys->psz_file = var_CreateGetString( p_demux, "demuxdump-file" );
00113     if( *p_sys->psz_file == '\0' )
00114     {
00115         msg_Warn( p_demux, "no dump file name given" );
00116         return VLC_EGENERIC;
00117     }
00118 
00119     if( !strcmp( p_sys->psz_file, "-" ) )
00120     {
00121         msg_Info( p_demux, "dumping raw stream to standard output" );
00122         p_sys->p_file = stdout;
00123     }
00124     else if( ( p_sys->p_file = fopen( p_sys->psz_file, psz_mode ) ) == NULL )
00125     {
00126         msg_Err( p_demux, "cannot create `%s' for writing", p_sys->psz_file );
00127 
00128         free( p_sys );
00129         return VLC_EGENERIC;
00130     }
00131     msg_Info( p_demux, "%s raw stream to file `%s'",
00132               b_append ? "appending" : "dumping", p_sys->psz_file );
00133 
00134     return VLC_SUCCESS;
00135 }
00136 
00137 /*****************************************************************************
00138  * Close:
00139  *****************************************************************************/
00140 static void Close( vlc_object_t *p_this )
00141 {
00142 
00143     demux_t     *p_demux = (demux_t*)p_this;
00144     demux_sys_t *p_sys = p_demux->p_sys;
00145 
00146     msg_Info( p_demux ,"closing %s ("I64Fd" Kbytes dumped)", p_sys->psz_file,
00147               p_sys->i_write / 1024 );
00148 
00149     if( p_sys->p_file != stdout )
00150     {
00151         fclose( p_sys->p_file );
00152     }
00153     free( p_sys->psz_file );
00154 
00155     free( p_sys );
00156 }
00157 
00158 /*****************************************************************************
00159  * Demux: reads and demuxes data packets
00160  *****************************************************************************
00161  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
00162  *****************************************************************************/
00163 static int Demux( demux_t *p_demux )
00164 {
00165     demux_sys_t *p_sys = p_demux->p_sys;
00166 
00167     int i_data;
00168 
00169     /* I'm pretty sure that stream_Peek,stream_Read( , NULL ) would be faster*/
00170     i_data = stream_Read( p_demux->s, p_sys->buffer, DUMP_BLOCKSIZE );
00171     if ( i_data <= 0 )
00172         return i_data;
00173 
00174     i_data = fwrite( p_sys->buffer, 1, i_data, p_sys->p_file );
00175 
00176     if( i_data == 0 )
00177     {
00178         msg_Err( p_demux, "failed to write data" );
00179         return -1;
00180     }
00181 #if 0
00182     msg_Dbg( p_demux, "dumped %d bytes", i_data );
00183 #endif
00184 
00185     p_sys->i_write += i_data;
00186 
00187     return 1;
00188 }
00189 
00190 /*****************************************************************************
00191  * Demux: reads and demuxes data packets
00192  *****************************************************************************/
00193 static int Control( demux_t *p_demux, int i_query, va_list args )
00194 {
00195     return demux2_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
00196 }
00197 

Generated on Tue Dec 20 10:14:31 2005 for vlc-0.8.4a by  doxygen 1.4.2